]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxtables: Replace gethostbyname() with getaddrinfo()
authorArpan Kapoor <rpnkpr@gmail.com>
Thu, 17 Mar 2016 12:57:19 +0000 (18:27 +0530)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sun, 3 Jul 2016 09:07:16 +0000 (11:07 +0200)
Make the function host_to_ipaddr() similar to host_to_ip6addr(),
using getaddrinfo() instead of the obsoleted gethostbyname().

Signed-off-by: Arpan Kapoor <rpnkpr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
libxtables/xtables.c

index 5e23734d4e58d4174d80db23d8e459dd836ad822..921dfe98c69b7a71e31e48df79f4986d7bde7810 100644 (file)
@@ -1353,22 +1353,36 @@ static struct in_addr *network_to_ipaddr(const char *name)
 
 static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
 {
-       struct hostent *host;
        struct in_addr *addr;
+       struct addrinfo hints;
+       struct addrinfo *res, *p;
+       int err;
        unsigned int i;
 
-       *naddr = 0;
-       if ((host = gethostbyname(name)) != NULL) {
-               if (host->h_addrtype != AF_INET ||
-                   host->h_length != sizeof(struct in_addr))
-                       return NULL;
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_flags    = AI_CANONNAME;
+       hints.ai_family   = AF_INET;
+       hints.ai_socktype = SOCK_RAW;
 
-               while (host->h_addr_list[*naddr] != NULL)
+       *naddr = 0;
+       if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
+#ifdef DEBUG
+               fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
+#endif
+               return NULL;
+       } else {
+               for (p = res; p != NULL; p = p->ai_next)
                        ++*naddr;
+#ifdef DEBUG
+               fprintf(stderr, "resolved: len=%d  %s ", res->ai_addrlen,
+                       xtables_ipaddr_to_numeric(&((struct sockaddr_in *)res->ai_addr)->sin_addr));
+#endif
                addr = xtables_calloc(*naddr, sizeof(struct in_addr));
-               for (i = 0; i < *naddr; i++)
-                       memcpy(&addr[i], host->h_addr_list[i],
+               for (i = 0, p = res; p != NULL; p = p->ai_next)
+                       memcpy(&addr[i++],
+                              &((const struct sockaddr_in *)p->ai_addr)->sin_addr,
                               sizeof(struct in_addr));
+               freeaddrinfo(res);
                return addr;
        }