Skip to content

Commit

Permalink
dns: Fixed incorrect handling of 0.0.0.0
Browse files Browse the repository at this point in the history
Fixes regression introduced by commit f174681
  • Loading branch information
espressif-abhikroy committed Aug 14, 2024
1 parent e8d0513 commit bced058
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
107 changes: 72 additions & 35 deletions src/api/netdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -105,16 +117,24 @@ lwip_gethostbyname(const char *name)
return NULL;
}

/* fill hostent */
for (i=0; i<DNS_MAX_HOST_IP; i++){
if (!ip_addr_cmp(&addr_zero, &addr[i])) {
s_hostent_addr[i] = addr[i];
s_phostent_addr[i] = &s_hostent_addr[i];
} else {
break;
COUNT_NON_ZERO_IP_ADDRESSES(addr, ipaddr_cnt);

if (ipaddr_cnt == 0) {
/* handle 0.0.0.0 */
s_hostent_addr[0] = addr[0];
s_phostent_addr[0] = &s_hostent_addr[0];
s_phostent_addr[1] = NULL;
} else {
for (i=0; i<ipaddr_cnt; i++){
if (!ip_addr_cmp(&addr_zero, &addr[i])) {
s_hostent_addr[i] = addr[i];
s_phostent_addr[i] = &s_hostent_addr[i];
} else {
break;
}
}
s_phostent_addr[i] = NULL;
}
s_phostent_addr[i] = NULL;
strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
s_hostname[DNS_MAX_NAME_LENGTH] = 0;
s_hostent.h_name = s_hostname;
Expand Down Expand Up @@ -175,6 +195,7 @@ lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
size_t namelen;
ip_addr_t addr_zero={0};
int lh_errno;
u8_t ipaddr_cnt = 0;

if (h_errnop == NULL) {
/* ensure h_errnop is never NULL */
Expand Down Expand Up @@ -217,15 +238,23 @@ lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
MEMCPY(hostname, name, namelen);
hostname[namelen] = 0;

/* fill hostent */
for (i=0; i<DNS_MAX_HOST_IP; i++){
if (!ip_addr_cmp(&addr_zero, &h->addr[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; i<ipaddr_cnt; i++) {
if (!ip_addr_cmp(&addr_zero, &h->addr[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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -471,33 +501,40 @@ lwip_getaddrinfo(const char *nodename, const char *servname,
}
}

for (i=0; i<DNS_MAX_HOST_IP; i++) {
if (!ip_addr_cmp(&addr_zero, &addr[i])) {
ret = create_addrinfo(addr[i], nodename, hints, port_nr, &ai, i);
if (ret != ERR_OK) { /* failure: free the entire list */
while (ai_head) {
ai = ai_head->ai_next;
memp_free(MEMP_NETDB, ai_head);
ai_head = ai;
COUNT_NON_ZERO_IP_ADDRESSES(addr, ipaddr_cnt);

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; i<ipaddr_cnt; i++) {
if (!ip_addr_cmp(&addr_zero, &addr[i])) {
ret = create_addrinfo(addr[i], nodename, hints, port_nr, &ai, i);
if (ret != ERR_OK) { /* failure: free the entire list */
lwip_freeaddrinfo(ai_head);
*res = NULL;
return ret;
}
*res = NULL;
return ret;
}

if (ai != NULL) {
if (ai_head == NULL) {
/* Initialize head */
ai_head = ai;
ai_tail = ai_head;
} else {
ai_tail->ai_next = ai;
if (ai != NULL) {
if (ai_head == NULL) {
/* Initialize head */
ai_head = ai;
} else {
ai_tail->ai_next = ai;
}
ai_tail = ai;
ai_tail->ai_next = NULL;
}
ai_tail->ai_next = NULL;
}
}
*res = ai_head;
}
*res = ai_head;

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bced058

Please sign in to comment.