diff options
author | denis <denis@cvs.openbsd.org> | 2018-10-22 07:08:02 +0000 |
---|---|---|
committer | denis <denis@cvs.openbsd.org> | 2018-10-22 07:08:02 +0000 |
commit | e5c95710fbafe9ba45d2fa005b78ba4a126979c3 (patch) | |
tree | 4fc3c2cb7b3076a726ca68e30dbc8c2c7df756fb /usr.sbin/relayd | |
parent | ec82a75bbed4000b636580a1ff20ef824b45ef26 (diff) |
Make host_*() AF-agnostic
Merge host_v{4,6}() into much simpler host_ip() using just getaddrinfo().
With input & test by kn@ and benno@
OK benno@ kn@
Diffstat (limited to 'usr.sbin/relayd')
-rw-r--r-- | usr.sbin/relayd/parse.y | 116 |
1 files changed, 34 insertions, 82 deletions
diff --git a/usr.sbin/relayd/parse.y b/usr.sbin/relayd/parse.y index c3fa2aef4dd..06789862a91 100644 --- a/usr.sbin/relayd/parse.y +++ b/usr.sbin/relayd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.228 2018/09/07 07:35:31 miko Exp $ */ +/* $OpenBSD: parse.y,v 1.229 2018/10/22 07:08:01 denis Exp $ */ /* * Copyright (c) 2007 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -123,8 +123,7 @@ static enum direction dir = RELAY_DIR_ANY; static char *rulefile = NULL; static union hashkey *hashkey = NULL; -struct address *host_v4(const char *); -struct address *host_v6(const char *); +struct address *host_ip(const char *); int host_dns(const char *, struct addresslist *, int, struct portrange *, const char *, int); int host_if(const char *, struct addresslist *, @@ -2929,49 +2928,22 @@ symget(const char *nam) } struct address * -host_v4(const char *s) +host_ip(const char *s) { - struct in_addr ina; - struct sockaddr_in *sain; - struct address *h; - - bzero(&ina, sizeof(ina)); - if (inet_pton(AF_INET, s, &ina) != 1) - return (NULL); - - if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(__func__); - sain = (struct sockaddr_in *)&h->ss; - sain->sin_len = sizeof(struct sockaddr_in); - sain->sin_family = AF_INET; - sain->sin_addr.s_addr = ina.s_addr; - - return (h); -} - -struct address * -host_v6(const char *s) -{ - struct addrinfo hints, *res; - struct sockaddr_in6 *sa_in6; - struct address *h = NULL; + struct addrinfo hints, *res; + struct address *h = NULL; - bzero(&hints, sizeof(hints)); - hints.ai_family = AF_INET6; - hints.ai_socktype = SOCK_DGRAM; /* dummy */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(s, "0", &hints, &res) == 0) { - if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(__func__); - sa_in6 = (struct sockaddr_in6 *)&h->ss; - sa_in6->sin6_len = sizeof(struct sockaddr_in6); - sa_in6->sin6_family = AF_INET6; - memcpy(&sa_in6->sin6_addr, - &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, - sizeof(sa_in6->sin6_addr)); - sa_in6->sin6_scope_id = - ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; - + if (res->ai_family == AF_INET || + res->ai_family == AF_INET6) { + if ((h = calloc(1, sizeof(*h))) == NULL) + fatal(NULL); + memcpy(&h->ss, res->ai_addr, res->ai_addrlen); + } freeaddrinfo(res); } @@ -2984,15 +2956,13 @@ host_dns(const char *s, struct addresslist *al, int max, { struct addrinfo hints, *res0, *res; int error, cnt = 0; - struct sockaddr_in *sain; - struct sockaddr_in6 *sin6; struct address *h; if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0) return (cnt); bzero(&hints, sizeof(hints)); - hints.ai_family = PF_UNSPEC; + hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; /* DUMMY */ hints.ai_flags = AI_ADDRCONFIG; error = getaddrinfo(s, NULL, &hints, &res0); @@ -3024,19 +2994,8 @@ host_dns(const char *s, struct addresslist *al, int max, } if (ipproto != -1) h->ipproto = ipproto; - h->ss.ss_family = res->ai_family; - if (res->ai_family == AF_INET) { - sain = (struct sockaddr_in *)&h->ss; - sain->sin_len = sizeof(struct sockaddr_in); - sain->sin_addr.s_addr = ((struct sockaddr_in *) - res->ai_addr)->sin_addr.s_addr; - } else { - sin6 = (struct sockaddr_in6 *)&h->ss; - sin6->sin6_len = sizeof(struct sockaddr_in6); - memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *) - res->ai_addr)->sin6_addr, sizeof(struct in6_addr)); - } + memcpy(&h->ss, res->ai_addr, res->ai_addrlen); TAILQ_INSERT_HEAD(al, h, entry); cnt++; @@ -3123,34 +3082,27 @@ int host(const char *s, struct addresslist *al, int max, struct portrange *port, const char *ifname, int ipproto) { - struct address *h; - - h = host_v4(s); - - /* IPv6 address? */ - if (h == NULL) - h = host_v6(s); - - if (h != NULL) { - if (port != NULL) - bcopy(port, &h->port, sizeof(h->port)); - if (ifname != NULL) { - if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >= - sizeof(h->ifname)) { - log_warnx("%s: interface name truncated", - __func__); - free(h); - return (-1); - } + struct address *h; + + if ((h = host_ip(s)) == NULL) + return (host_dns(s, al, max, port, ifname, ipproto)); + + if (port != NULL) + bcopy(port, &h->port, sizeof(h->port)); + if (ifname != NULL) { + if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >= + sizeof(h->ifname)) { + log_warnx("%s: interface name truncated", + __func__); + free(h); + return (-1); } - if (ipproto != -1) - h->ipproto = ipproto; - - TAILQ_INSERT_HEAD(al, h, entry); - return (1); } + if (ipproto != -1) + h->ipproto = ipproto; - return (host_dns(s, al, max, port, ifname, ipproto)); + TAILQ_INSERT_HEAD(al, h, entry); + return (1); } void |