Make host_*() AF-agnostic like in ntpd/pfctl/relayd

host_dns() stays unchanged because of port setting. Perhaps we could handle it
like in relayd ? (additional function to set port with sockaddr_storage :
relay_socket_af())

Also fix lines longer than 80 chars.

Regress passes, tested with this :

listen on lo0 port 61636
listen on 192.168.70.10 port 6136
listen on 2001:db8:1::10 port 61639 secure
listen on "/tmp/ldapi"

Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/ldapd/parse.y,v
retrieving revision 1.33
diff -u -p -r1.33 parse.y
--- parse.y     7 Sep 2018 07:35:31 -0000       1.33
+++ parse.y     22 Oct 2018 09:27:27 -0000
@@ -75,8 +75,7 @@ void           lungetc(int);
 int             findeol(void);
 
 struct listener *host_unix(const char *path);
-struct listener        *host_v4(const char *, in_port_t);
-struct listener        *host_v6(const char *, in_port_t);
+struct listener        *host_ip(const char *);
 int             host_dns(const char *, const char *,
                    struct listenerlist *, int, in_port_t, u_int8_t);
 int             host(const char *, const char *,
@@ -960,46 +959,24 @@ host_unix(const char *path)
 }
 
 struct listener *
-host_v4(const char *s, in_port_t port)
+host_ip(const char *s)
 {
-       struct in_addr           ina;
-       struct sockaddr_in      *sain;
-       struct listener         *h;
-
-       memset(&ina, 0, sizeof(ina));
-       if (inet_pton(AF_INET, s, &ina) != 1)
-               return (NULL);
-
-       if ((h = calloc(1, sizeof(*h))) == NULL)
-               fatal(NULL);
-       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;
-       sain->sin_port = port;
-
-       return (h);
-}
-
-struct listener *
-host_v6(const char *s, in_port_t port)
-{
-       struct in6_addr          ina6;
-       struct sockaddr_in6     *sin6;
-       struct listener         *h;
-
-       memset(&ina6, 0, sizeof(ina6));
-       if (inet_pton(AF_INET6, s, &ina6) != 1)
-               return (NULL);
-
-       if ((h = calloc(1, sizeof(*h))) == NULL)
-               fatal(NULL);
-       sin6 = (struct sockaddr_in6 *)&h->ss;
-       sin6->sin6_len = sizeof(struct sockaddr_in6);
-       sin6->sin6_family = AF_INET6;
-       sin6->sin6_port = port;
-       memcpy(&sin6->sin6_addr, &ina6, sizeof(ina6));
+       struct addrinfo  hints, *res;
+       struct listener *h = NULL;
 
+       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 (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);
 }
 
@@ -1014,7 +991,7 @@ host_dns(const char *s, const char *cert
        struct listener         *h;
 
        memset(&hints, 0, sizeof(hints));
-       hints.ai_family = PF_UNSPEC;
+       hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
        error = getaddrinfo(s, NULL, &hints, &res0);
        if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
@@ -1038,7 +1015,8 @@ host_dns(const char *s, const char *cert
                h->ssl = NULL;
                h->ssl_cert_name[0] = '\0';
                if (cert != NULL)
-                       (void)strlcpy(h->ssl_cert_name, cert, 
sizeof(h->ssl_cert_name));
+                       (void)strlcpy(h->ssl_cert_name, cert,
+                           sizeof(h->ssl_cert_name));
 
                if (res->ai_family == AF_INET) {
                        sain = (struct sockaddr_in *)&h->ss;
@@ -1069,32 +1047,36 @@ int
 host(const char *s, const char *cert, struct listenerlist *al,
     int max, in_port_t port, u_int8_t flags)
 {
-       struct listener *h;
-
-       /* Unix socket path? */
-       h = host_unix(s);
-
-       /* IPv4 address? */
-       if (h == NULL)
-               h = host_v4(s, port);
-
-       /* IPv6 address? */
-       if (h == NULL)
-               h = host_v6(s, port);
-
-       if (h != NULL) {
-               h->port = port;
-               h->flags |= flags;
-               h->ssl = NULL;
-               h->ssl_cert_name[0] = '\0';
-               if (cert != NULL)
-                       strlcpy(h->ssl_cert_name, cert, 
sizeof(h->ssl_cert_name));
+       struct listener         *h;
+       struct sockaddr_in      *sain;
+       struct sockaddr_in6     *sin6;
 
-               TAILQ_INSERT_HEAD(al, h, entry);
-               return (1);
-       }
+       if ((h = host_unix(s)) == NULL)
+               if ((h = host_ip(s)) == NULL)
+                       return (host_dns(s, cert, al, max, port, flags));
+
+       switch (h->ss.ss_family) {
+       case AF_INET:
+               sain = (struct sockaddr_in *)&h->ss;
+               sain->sin_port = port;
+               break;
+       case AF_INET6:
+               sin6 = (struct sockaddr_in6 *)&h->ss;
+               sin6->sin6_port = port;
+               break;
+       default:
+               break;
+       } 
+
+       h->port = port;
+       h->flags |= flags;
+       h->ssl = NULL;
+       h->ssl_cert_name[0] = '\0';
+       if (cert != NULL)
+               strlcpy(h->ssl_cert_name, cert, sizeof(h->ssl_cert_name));
 
-       return (host_dns(s, cert, al, max, port, flags));
+       TAILQ_INSERT_HEAD(al, h, entry);
+       return (1);
 }
 
 int
@@ -1129,7 +1111,8 @@ interface(const char *s, const char *cer
                        h->ssl = NULL;
                        h->ssl_cert_name[0] = '\0';
                        if (cert != NULL)
-                               (void)strlcpy(h->ssl_cert_name, cert, 
sizeof(h->ssl_cert_name));
+                               (void)strlcpy(h->ssl_cert_name, cert,
+                                   sizeof(h->ssl_cert_name));
 
                        ret = 1;
                        TAILQ_INSERT_HEAD(al, h, entry);
@@ -1150,7 +1133,8 @@ interface(const char *s, const char *cer
                        h->ssl = NULL;
                        h->ssl_cert_name[0] = '\0';
                        if (cert != NULL)
-                               (void)strlcpy(h->ssl_cert_name, cert, 
sizeof(h->ssl_cert_name));
+                               (void)strlcpy(h->ssl_cert_name, cert,
+                                   sizeof(h->ssl_cert_name));
 
                        ret = 1;
                        TAILQ_INSERT_HEAD(al, h, entry);

Reply via email to