This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new a41f68da84 dns: fix dns failed when ipv4/6 dual stack enable
a41f68da84 is described below

commit a41f68da84eda8bf86c72a5f45bc718e7bcc81b7
Author: zhanghongyu <[email protected]>
AuthorDate: Wed Sep 13 14:33:19 2023 +0800

    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 <[email protected]>
---
 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 38d25efb89..ba414f4c53 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 27c5dec04f..6ccd61ef1d 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

Reply via email to