You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/08/03 15:43:52 UTC

[incubator-nuttx] branch master updated: delay creating DNS bind socket

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/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 1141ee28e9 delay creating DNS bind socket
1141ee28e9 is described below

commit 1141ee28e96f22d8ce230d86c83baa136c416a69
Author: liyi25 <li...@xiaomi.com>
AuthorDate: Mon Aug 1 20:31:19 2022 +0800

    delay creating DNS bind socket
    
    Signed-off-by: liyi <li...@xiaomi.com>
---
 libs/libc/netdb/lib_dns.h               |  5 ++---
 libs/libc/netdb/lib_dnsbind.c           |  4 ++--
 libs/libc/netdb/lib_dnsquery.c          | 23 +++++++++++++++--------
 libs/libc/netdb/lib_gethostentbynamer.c | 18 +-----------------
 4 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/libs/libc/netdb/lib_dns.h b/libs/libc/netdb/lib_dns.h
old mode 100644
new mode 100755
index 3ee8aee2ba..78d335792a
--- a/libs/libc/netdb/lib_dns.h
+++ b/libs/libc/netdb/lib_dns.h
@@ -177,7 +177,7 @@ void dns_semgive(void);
  *
  ****************************************************************************/
 
-int dns_bind(void);
+int dns_bind(sa_family_t family);
 
 /****************************************************************************
  * Name: dns_query
@@ -187,7 +187,6 @@ int dns_bind(void);
  *   return its IP address in 'ipaddr'
  *
  * Input Parameters:
- *   sd       - The socket descriptor previously initialized by dsn_bind().
  *   hostname - The hostname string to be resolved.
  *   addr     - The location to return the IP addresses associated with the
  *     hostname.
@@ -200,7 +199,7 @@ int dns_bind(void);
  *
  ****************************************************************************/
 
-int dns_query(int sd, FAR const char *hostname, FAR union dns_addr_u *addr,
+int dns_query(FAR const char *hostname, FAR union dns_addr_u *addr,
               FAR int *naddr);
 
 /****************************************************************************
diff --git a/libs/libc/netdb/lib_dnsbind.c b/libs/libc/netdb/lib_dnsbind.c
old mode 100644
new mode 100755
index e1380c5cce..3db9b19f3f
--- a/libs/libc/netdb/lib_dnsbind.c
+++ b/libs/libc/netdb/lib_dnsbind.c
@@ -61,7 +61,7 @@
  *
  ****************************************************************************/
 
-int dns_bind(void)
+int dns_bind(sa_family_t family)
 {
   struct timeval tv;
   int sd;
@@ -77,7 +77,7 @@ int dns_bind(void)
 
   /* Create a new socket */
 
-  sd = socket(PF_INET, SOCK_DGRAM, 0);
+  sd = socket(family, SOCK_DGRAM, 0);
   if (sd < 0)
     {
       ret = -get_errno();
diff --git a/libs/libc/netdb/lib_dnsquery.c b/libs/libc/netdb/lib_dnsquery.c
old mode 100644
new mode 100755
index cb3b69d597..9c8040f79c
--- a/libs/libc/netdb/lib_dnsquery.c
+++ b/libs/libc/netdb/lib_dnsquery.c
@@ -50,6 +50,7 @@
 
 #include <string.h>
 #include <time.h>
+#include <unistd.h>
 #include <errno.h>
 #include <debug.h>
 #include <assert.h>
@@ -85,7 +86,6 @@
 
 struct dns_query_s
 {
-  int sd;                         /* DNS server socket */
   int result;                     /* Explanation of the failure */
   FAR const char *hostname;       /* Hostname to lookup */
   FAR union dns_addr_u *addr;     /* Location to return host address */
@@ -583,6 +583,13 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
   int retries;
   int ret;
 
+  int sd = dns_bind(addr->sa_family);
+  if (sd < 0)
+    {
+      query->result = sd;
+      return 0;
+    }
+
   /* Loop while receive timeout errors occur and there are remaining
    * retries.
    */
@@ -592,7 +599,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
 #ifdef CONFIG_NET_IPv6
       /* Send the IPv6 query */
 
-      ret = dns_send_query(query->sd, query->hostname,
+      ret = dns_send_query(sd, query->hostname,
                           (FAR union dns_addr_u *)addr,
                            DNS_RECTYPE_AAAA, &qinfo);
       if (ret < 0)
@@ -604,7 +611,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
         {
           /* Obtain the IPv6 response */
 
-          ret = dns_recv_response(query->sd, &query->addr[next],
+          ret = dns_recv_response(sd, &query->addr[next],
                                   *query->naddr - next, &qinfo);
           if (ret >= 0)
             {
@@ -621,7 +628,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
 #ifdef CONFIG_NET_IPv4
       /* Send the IPv4 query */
 
-      ret = dns_send_query(query->sd, query->hostname,
+      ret = dns_send_query(sd, query->hostname,
                            (FAR union dns_addr_u *)addr,
                            DNS_RECTYPE_A, &qinfo);
       if (ret < 0)
@@ -633,7 +640,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
         {
           /* Obtain the IPv4 response */
 
-          ret = dns_recv_response(query->sd, &query->addr[next],
+          ret = dns_recv_response(sd, &query->addr[next],
                                   *query->naddr - next, &qinfo);
           if (ret >= 0)
             {
@@ -659,6 +666,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
            */
 
           *query->naddr = next;
+          close(sd);
           return 1;
         }
       else if (query->result != -EAGAIN)
@@ -667,6 +675,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
         }
     }
 
+  close(sd);
   return 0;
 }
 
@@ -682,7 +691,6 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
  *   return its IP address in 'ipaddr'
  *
  * Input Parameters:
- *   sd       - The socket descriptor previously initialized by dsn_bind().
  *   hostname - The hostname string to be resolved.
  *   addr     - The location to return the IP addresses associated with the
  *     hostname.
@@ -695,7 +703,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
  *
  ****************************************************************************/
 
-int dns_query(int sd, FAR const char *hostname, FAR union dns_addr_u *addr,
+int dns_query(FAR const char *hostname, FAR union dns_addr_u *addr,
               FAR int *naddr)
 {
   FAR struct dns_query_s query;
@@ -703,7 +711,6 @@ int dns_query(int sd, FAR const char *hostname, FAR union dns_addr_u *addr,
 
   /* Set up the query info structure */
 
-  query.sd       = sd;
   query.result   = -EADDRNOTAVAIL;
   query.hostname = hostname;
   query.addr     = addr;
diff --git a/libs/libc/netdb/lib_gethostentbynamer.c b/libs/libc/netdb/lib_gethostentbynamer.c
old mode 100644
new mode 100755
index 5b96c9d681..b7f6a2fc64
--- a/libs/libc/netdb/lib_gethostentbynamer.c
+++ b/libs/libc/netdb/lib_gethostentbynamer.c
@@ -432,25 +432,9 @@ static int lib_find_answer(FAR const char *name, FAR struct hostent_s *host,
 static int lib_dns_query(FAR const char *hostname,
                          FAR union dns_addr_u *addr, int *naddr)
 {
-  int sd;
-  int ret;
-
-  /* Create and bind a socket to the DNS server */
-
-  sd = dns_bind();
-  if (sd < 0)
-    {
-      return sd;
-    }
-
   /* Perform the query to get the IP address */
 
-  ret = dns_query(sd, hostname, addr, naddr);
-
-  /* Release the socket */
-
-  close(sd);
-  return ret;
+  return dns_query(hostname, addr, naddr);
 }
 #endif /* CONFIG_NETDB_DNSCLIENT */