From a41f68da84eda8bf86c72a5f45bc718e7bcc81b7 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Wed, 13 Sep 2023 14:33:19 +0800 Subject: [PATCH] dns: fix dns failed when ipv4/6 dual stack enable The ipv6 address filled the cache, and the ipv4 address did not have a place to store it, causing the resolution to fail. so if IPV6 has already filled the buffer, rewrite ipv4 DNS results from half of the buffer. Signed-off-by: zhanghongyu --- libs/libc/netdb/lib_dnsquery.c | 5 +++++ libs/libc/netdb/lib_gethostentbynamer.c | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/libs/libc/netdb/lib_dnsquery.c b/libs/libc/netdb/lib_dnsquery.c index 38d25efb89a..ba414f4c53f 100644 --- a/libs/libc/netdb/lib_dnsquery.c +++ b/libs/libc/netdb/lib_dnsquery.c @@ -712,6 +712,11 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr, { /* Obtain the IPv4 response */ + if (next >= *query->naddr) + { + next = *query->naddr / 2; + } + ret = dns_recv_response(sd, &query->addr[next], *query->naddr - next, &qdata->qinfo, &query->ttl, qdata->buffer); diff --git a/libs/libc/netdb/lib_gethostentbynamer.c b/libs/libc/netdb/lib_gethostentbynamer.c index 27c5dec04fd..6ccd61ef1d2 100644 --- a/libs/libc/netdb/lib_gethostentbynamer.c +++ b/libs/libc/netdb/lib_gethostentbynamer.c @@ -502,16 +502,16 @@ static int lib_dns_lookup(FAR const char *name, FAR struct hostent_s *host, naddr = (buflen - (namelen + 1)) / sizeof(union dns_addr_u); DEBUGASSERT(naddr >= 1); + + /* We can read more than maximum, limit here. */ + + naddr = MIN(naddr, CONFIG_NETDB_MAX_IPADDR); ret = lib_dns_query(name, (FAR union dns_addr_u *)ptr, &naddr); if (ret < 0) { return ret; } - /* We can read more than maximum, limit here. */ - - naddr = MIN(naddr, CONFIG_NETDB_MAX_IPADDR); - for (i = 0; i < naddr; i++) { #ifdef CONFIG_NET_IPv4