Sync changes to host_*() from ntpd to httpd.

Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/parse.y,v
retrieving revision 1.106
diff -u -p -r1.106 parse.y
--- parse.y     7 Sep 2018 07:35:30 -0000       1.106
+++ parse.y     21 Oct 2018 11:43:42 -0000
@@ -103,8 +103,7 @@ static struct server_config *srv_conf = 
 struct serverlist       servers;
 struct media_type       media;
 
-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 *,
@@ -1846,60 +1845,25 @@ symget(const char *nam)
 }
 
 struct address *
-host_v4(const char *s)
-{
-       struct in_addr           ina;
-       struct sockaddr_in      *sain;
-       struct address          *h;
-
-       memset(&ina, 0, 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;
-       if (sain->sin_addr.s_addr == INADDR_ANY)
-               h->prefixlen = 0; /* 0.0.0.0 address */
-       else
-               h->prefixlen = -1; /* host address */
-       return (h);
-}
-
-struct address *
-host_v6(const char *s)
+host_ip(const char *s)
 {
        struct addrinfo          hints, *res;
-       struct sockaddr_in6     *sa_in6;
        struct address          *h = NULL;
 
        memset(&hints, 0, sizeof(hints));
-       hints.ai_family = AF_INET6;
-       hints.ai_socktype = SOCK_DGRAM; /* dummy */
+       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 (memcmp(&sa_in6->sin6_addr, &in6addr_any,
-                   sizeof(sa_in6->sin6_addr)) == 0)
-                       h->prefixlen = 0; /* any address */
-               else
-                       h->prefixlen = -1; /* host address */
+               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);
        }
-
-       return (h);
+       return (h); 
 }
 
 int
@@ -1908,15 +1872,13 @@ host_dns(const char *s, struct addressli
 {
        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);
 
        memset(&hints, 0, 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);
@@ -1951,17 +1913,7 @@ host_dns(const char *s, struct addressli
                h->ss.ss_family = res->ai_family;
                h->prefixlen = -1; /* host address */
 
-               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++;
@@ -2049,34 +2001,49 @@ int
 host(const char *s, struct addresslist *al, int max,
     struct portrange *port, const char *ifname, int ipproto)
 {
-       struct address *h;
+       struct address          *h;
+       struct sockaddr_in      *sin;
+       struct sockaddr_in6     *sin6;
 
-       h = host_v4(s);
+       if ((h = host_ip(s)) == NULL)
+               return (host_dns(s, al, max, port, ifname, ipproto));
 
-       /* IPv6 address? */
-       if (h == NULL)
-               h = host_v6(s);
+       switch (h->ss.ss_family) {
+       case AF_INET:
+               sin = (struct sockaddr_in *)&h->ss;
+               if (sin->sin_addr.s_addr == INADDR_ANY)
+                       h->prefixlen = 0; /* any address */
+               else
+                       h->prefixlen = -1; /* host address */
+               break;
+       case AF_INET6:
+               sin6 = (struct sockaddr_in6 *)&h->ss;
+               if (memcmp(&sin6->sin6_addr, &in6addr_any,
+                   sizeof(sin6->sin6_addr)) == 0)
+                       h->prefixlen = 0; /* any address */
+               else
+                       h->prefixlen = -1; /* host address */
+               break;
+       default:
+               fatal("unsupported AF");
+       }
 
-       if (h != NULL) {
-               if (port != NULL)
-                       memcpy(&h->port, 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 (port != NULL)
+               memcpy(&h->port, 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);
 }
 
 struct server *

Reply via email to