From bced058f737eaabea1aa193f3c365ee78ff555f3 Mon Sep 17 00:00:00 2001 From: Abhik Roy Date: Thu, 13 Jun 2024 19:15:33 +1000 Subject: [PATCH] dns: Fixed incorrect handling of 0.0.0.0 Fixes regression introduced by commit f1746813 --- src/api/netdb.c | 111 ++++++++++++++++++++++++++++++++---------------- src/core/dns.c | 2 +- 2 files changed, 75 insertions(+), 38 deletions(-) diff --git a/src/api/netdb.c b/src/api/netdb.c index e9f48e61..a272acb5 100644 --- a/src/api/netdb.c +++ b/src/api/netdb.c @@ -74,6 +74,17 @@ int h_errno; #define HOSTENT_STORAGE static #endif /* LWIP_DNS_API_STATIC_HOSTENT */ +/* Counts IP addresses in addr array until a zero IP address is encountered */ +#define COUNT_NON_ZERO_IP_ADDRESSES(addr, ipaddr_cnt) \ + do { \ + ipaddr_cnt = 0; \ + for (i = 0; i < DNS_MAX_HOST_IP; i++) { \ + if (!ip_addr_cmp(&addr_zero, &addr[i])) { \ + ipaddr_cnt++; \ + } \ + } \ + } while(0) + /** * Returns an entry containing addresses of address family AF_INET * for the host with name name. @@ -89,6 +100,7 @@ lwip_gethostbyname(const char *name) u8_t i; err_t err; ip_addr_t addr[DNS_MAX_HOST_IP]={0}, addr_zero={0}; + u8_t ipaddr_cnt = 0; /* buffer variables for lwip_gethostbyname() */ HOSTENT_STORAGE struct hostent s_hostent; @@ -105,16 +117,24 @@ lwip_gethostbyname(const char *name) return NULL; } - /* fill hostent */ - for (i=0; iaddr[i])) { - h->addr_list[i] = &h->addr[i]; - } else { - break; + COUNT_NON_ZERO_IP_ADDRESSES(h->addr, ipaddr_cnt); + + if (ipaddr_cnt == 0) { + /* handle 0.0.0.0 */ + h->addr_list[0] = &h->addr[0]; + h->addr_list[1] = NULL; + } else { + for (i=0; iaddr[i])) { + h->addr_list[i] = &h->addr[i]; + } else { + break; + } } + h->addr_list[i] = NULL; } - h->addr_list[i] = NULL; + h->aliases = NULL; ret->h_name = hostname; ret->h_aliases = &h->aliases; @@ -386,6 +415,7 @@ lwip_getaddrinfo(const char *nodename, const char *servname, { err_t err; ip_addr_t addr[DNS_MAX_HOST_IP]={0}, addr_zero={0}; + u8_t ipaddr_cnt = 0; struct addrinfo *ai=NULL, *ai_head=NULL, *ai_tail=NULL; int port_nr = 0; int ai_family; @@ -471,33 +501,40 @@ lwip_getaddrinfo(const char *nodename, const char *servname, } } - for (i=0; iai_next; - memp_free(MEMP_NETDB, ai_head); - ai_head = ai; - } - *res = NULL; - return ret; - } + COUNT_NON_ZERO_IP_ADDRESSES(addr, ipaddr_cnt); - if (ai != NULL) { - if (ai_head == NULL) { - /* Initialize head */ - ai_head = ai; - ai_tail = ai_head; - } else { - ai_tail->ai_next = ai; - ai_tail = ai; + if (ipaddr_cnt == 0) { + /* handle 0.0.0.0 */ + ret = create_addrinfo(addr[0], nodename, hints, port_nr, &ai, 0); + if (ret != ERR_OK) { + *res = NULL; + return ret; + } + *res = ai; + } else { + for (i=0; iai_next = ai; + } + ai_tail = ai; + ai_tail->ai_next = NULL; } - ai_tail->ai_next = NULL; } } + *res = ai_head; } - *res = ai_head; return 0; } diff --git a/src/core/dns.c b/src/core/dns.c index eec4c287..feceb053 100644 --- a/src/core/dns.c +++ b/src/core/dns.c @@ -1118,7 +1118,7 @@ dns_backupserver_available(struct dns_table_entry *pentry) * - retry old pending entries on timeout (also with different servers) * - remove completed entries from the table if their TTL has expired * - * @param i index of the dns_table entry to check + * @param idx index of the dns_table entry to check */ static void dns_check_entry(u8_t idx)