This diff merges `host_v4()' and `host_v6()' into the much simpler
`host_addr()'.

Instead of calling inet_pton() and getaddrinfo() while using AF specific
structs for IPv4 and IPv6 respectively, use getaddrinfo() for both in
combination with `struct sockaddr_storage'.

`host_dns()' can adapt the same copy routine.

I tested different ntpd.conf constellations and stepped through gdb to
ensure that values such as family, length and scope id were still
carried over.

Anything missing? Feedback? OK?

Index: config.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/config.c,v
retrieving revision 1.28
diff -u -p -r1.28 config.c
--- config.c    12 Oct 2015 06:50:08 -0000      1.28
+++ config.c    31 Jul 2018 21:58:01 -0000
@@ -29,8 +29,7 @@
 
 #include "ntpd.h"
 
-struct ntp_addr        *host_v4(const char *);
-struct ntp_addr        *host_v6(const char *);
+struct ntp_addr        *host_addr(const char *);
 
 static u_int32_t                maxid = 0;
 static u_int32_t                constraint_maxid = 0;
@@ -44,13 +43,8 @@ host(const char *s, struct ntp_addr **hn
                if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
                        fatal(NULL);
 
-       /* IPv4 address? */
        if (h == NULL)
-               h = host_v4(s);
-
-       /* IPv6 address? */
-       if (h == NULL)
-               h = host_v6(s);
+               h = host_addr(s);
 
        if (h == NULL)
                return;
@@ -59,49 +53,22 @@ host(const char *s, struct ntp_addr **hn
 }
 
 struct ntp_addr        *
-host_v4(const char *s)
-{
-       struct in_addr           ina;
-       struct sockaddr_in      *sa_in;
-       struct ntp_addr         *h;
-
-       memset(&ina, 0, sizeof(struct in_addr));
-       if (inet_pton(AF_INET, s, &ina) != 1)
-               return (NULL);
-
-       if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
-               fatal(NULL);
-       sa_in = (struct sockaddr_in *)&h->ss;
-       sa_in->sin_len = sizeof(struct sockaddr_in);
-       sa_in->sin_family = AF_INET;
-       sa_in->sin_addr.s_addr = ina.s_addr;
-
-       return (h);
-}
-
-struct ntp_addr        *
-host_v6(const char *s)
+host_addr(const char *s)
 {
        struct addrinfo          hints, *res;
-       struct sockaddr_in6     *sa_in6;
        struct ntp_addr         *h = NULL;
 
        memset(&hints, 0, sizeof(hints));
-       hints.ai_family = AF_INET6;
+       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(struct ntp_addr))) == NULL)
-                       fatal(NULL);
-               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(struct ntp_addr))) == NULL)
+                               fatal(NULL);
+                       memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
+               }
                freeaddrinfo(res);
        }
 
@@ -124,12 +91,10 @@ host_dns(const char *s, struct ntp_addr 
 {
        struct addrinfo          hints, *res0, *res;
        int                      error, cnt = 0;
-       struct sockaddr_in      *sa_in;
-       struct sockaddr_in6     *sa_in6;
        struct ntp_addr         *h, *hh = NULL;
 
        memset(&hints, 0, sizeof(hints));
-       hints.ai_family = PF_UNSPEC;
+       hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
        /* ntpd MUST NOT use AI_ADDRCONFIG here */
        error = getaddrinfo(s, NULL, &hints, &res0);
@@ -147,18 +112,7 @@ host_dns(const char *s, struct ntp_addr 
                        continue;
                if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
                        fatal(NULL);
-               h->ss.ss_family = res->ai_family;
-               if (res->ai_family == AF_INET) {
-                       sa_in = (struct sockaddr_in *)&h->ss;
-                       sa_in->sin_len = sizeof(struct sockaddr_in);
-                       sa_in->sin_addr.s_addr = ((struct sockaddr_in *)
-                           res->ai_addr)->sin_addr.s_addr;
-               } else {
-                       sa_in6 = (struct sockaddr_in6 *)&h->ss;
-                       sa_in6->sin6_len = sizeof(struct sockaddr_in6);
-                       memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)
-                           res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
-               }
+               memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
 
                h->next = hh;
                hh = h;
@@ -207,4 +161,3 @@ new_constraint(void)
 
        return (p);
 }
-
===================================================================
Stats: --- 59 lines 1682 chars
Stats: +++ 12 lines 395 chars
Stats: -47 lines
Stats: -1287 chars

Reply via email to