From 3a46457deb9ae0701e7e978ba27f4141302587c0 Mon Sep 17 00:00:00 2001 From: Brad House Date: Wed, 11 Mar 2020 17:14:48 -0400 Subject: [PATCH] replace all usages of inet_addr() with ares_inet_pton() which is more proper (#312) Replace usage of inet_addr() with ares_inet_pton() which is more appropriate and fixes issues with legitimate addresses like 255.255.255.0. IPv6 already used this. Fixes #309 Fix By: Brad House (@bradh352) --- ares__get_hostent.c | 3 +-- ares__readaddrinfo.c | 3 +-- ares_getaddrinfo.c | 5 ++--- ares_gethostbyname.c | 6 +++--- ares_init.c | 6 +++--- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/ares__get_hostent.c b/ares__get_hostent.c index d2f95034..367f3903 100644 --- a/ares__get_hostent.c +++ b/ares__get_hostent.c @@ -138,8 +138,7 @@ int ares__get_hostent(FILE *fp, int family, struct hostent **host) addr.addrV4.s_addr = INADDR_NONE; if ((family == AF_INET) || (family == AF_UNSPEC)) { - addr.addrV4.s_addr = inet_addr(txtaddr); - if (addr.addrV4.s_addr != INADDR_NONE) + if (ares_inet_pton(AF_INET, txtaddr, &addr.addrV4) > 0) { /* Actual network address family and length. */ addr.family = AF_INET; diff --git a/ares__readaddrinfo.c b/ares__readaddrinfo.c index 407ee861..dd3abe2e 100644 --- a/ares__readaddrinfo.c +++ b/ares__readaddrinfo.c @@ -170,8 +170,7 @@ int ares__readaddrinfo(FILE *fp, if ((hints->ai_family == AF_INET) || (hints->ai_family == AF_UNSPEC)) { addr.sa4.sin_port = htons(port); - addr.sa4.sin_addr.s_addr = inet_addr(txtaddr); - if (addr.sa4.sin_addr.s_addr != INADDR_NONE) + if (ares_inet_pton(AF_INET, txtaddr, &addr.sa4.sin_addr) > 0) { node = ares__append_addrinfo_node(&nodes); if(!node) diff --git a/ares_getaddrinfo.c b/ares_getaddrinfo.c index e67903b5..8265e4af 100644 --- a/ares_getaddrinfo.c +++ b/ares_getaddrinfo.c @@ -313,14 +313,13 @@ static int fake_addrinfo(const char *name, memset(&addr, 0, sizeof(addr)); /* if we don't have 3 dots, it is illegal - * (although inet_addr doesn't think so). + * (although inet_pton doesn't think so). */ if (numdots != 3 || !valid) result = 0; else result = - ((addr.sa4.sin_addr.s_addr = inet_addr(name)) == INADDR_NONE ? 0 - : 1); + (ares_inet_pton(AF_INET, name, &addr.sa4.sin_addr) < 1 ? 0 : 1); if (result) { diff --git a/ares_gethostbyname.c b/ares_gethostbyname.c index b847b74a..ecd03e79 100644 --- a/ares_gethostbyname.c +++ b/ares_gethostbyname.c @@ -213,7 +213,7 @@ static void host_callback(void *arg, int status, int timeouts, } if (status == ARES_SUCCESS && host && host->h_addr_list[0] == NULL) { - /* The query returned something but had no A/AAAA record + /* The query returned something but had no A/AAAA record (even after potentially retrying AAAA with A) so we should treat this as an error */ status = ARES_ENODATA; @@ -274,12 +274,12 @@ static int fake_hostent(const char *name, int family, } /* if we don't have 3 dots, it is illegal - * (although inet_addr doesn't think so). + * (although inet_pton doesn't think so). */ if (numdots != 3 || !valid) result = 0; else - result = ((in.s_addr = inet_addr(name)) == INADDR_NONE ? 0 : 1); + result = (ares_inet_pton(AF_INET, name, &in) < 1 ? 0 : 1); if (result) family = AF_INET; diff --git a/ares_init.c b/ares_init.c index a6a138ac..dffa5181 100644 --- a/ares_init.c +++ b/ares_init.c @@ -2297,7 +2297,7 @@ static int set_search(ares_channel channel, const char *str) channel->ndomains = -1; } /* LCOV_EXCL_STOP */ - channel->domains = ares_strsplit(str, ", ", 1, &cnt); + channel->domains = ares_strsplit(str, ", ", 1, &cnt); channel->ndomains = (int)cnt; if (channel->domains == NULL || channel->ndomains == 0) { channel->domains = NULL; @@ -2423,9 +2423,9 @@ static int ip_addr(const char *ipbuf, ares_ssize_t len, struct in_addr *addr) if (len > 15) return -1; - addr->s_addr = inet_addr(ipbuf); - if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0) + if (ares_inet_pton(AF_INET, ipbuf, addr) < 1) return -1; + return 0; }