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

[incubator-nuttx] branch master updated: dns_client: directly initialize g_dns_servers and remove dns_initialize directly

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

pkarashchenko 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 daf39c03bd dns_client: directly initialize g_dns_servers and remove dns_initialize directly
daf39c03bd is described below

commit daf39c03bdd92aaa21b9d928e1d2ab9c6160e2f0
Author: zhanghongyu <zh...@xiaomi.com>
AuthorDate: Thu Aug 11 10:58:32 2022 +0800

    dns_client: directly initialize g_dns_servers and remove dns_initialize directly
    
    g_dns_servers need init before dns_query, Otherwise sim can not add default
    dns server when use usrsock mode to share host network.
    
    Avoid similar problems in the future, so directly initialize g_dns_servers.
    
    Signed-off-by: zhanghongyu <zh...@xiaomi.com>
---
 libs/libc/netdb/lib_dns.h          | 10 -----
 libs/libc/netdb/lib_dnsaddserver.c | 37 +++++++++++++++-
 libs/libc/netdb/lib_dnsbind.c      |  8 ----
 libs/libc/netdb/lib_dnsinit.c      | 87 --------------------------------------
 4 files changed, 35 insertions(+), 107 deletions(-)

diff --git a/libs/libc/netdb/lib_dns.h b/libs/libc/netdb/lib_dns.h
index 78d335792a..a964445966 100644
--- a/libs/libc/netdb/lib_dns.h
+++ b/libs/libc/netdb/lib_dns.h
@@ -131,16 +131,6 @@ EXTERN uint8_t g_dns_nservers;
  * Public Function Prototypes
  ****************************************************************************/
 
-/****************************************************************************
- * Name: dns_initialize
- *
- * Description:
- *   Make sure that the DNS client has been properly initialized for use.
- *
- ****************************************************************************/
-
-bool dns_initialize(void);
-
 /****************************************************************************
  * Name: dns_semtake
  *
diff --git a/libs/libc/netdb/lib_dnsaddserver.c b/libs/libc/netdb/lib_dnsaddserver.c
index cc4e0b5933..475160078a 100644
--- a/libs/libc/netdb/lib_dnsaddserver.c
+++ b/libs/libc/netdb/lib_dnsaddserver.c
@@ -45,8 +45,41 @@
 #ifndef CONFIG_NETDB_RESOLVCONF
 /* The DNS server addresses */
 
-union dns_addr_u g_dns_servers[CONFIG_NETDB_DNSSERVER_NAMESERVERS];
-uint8_t g_dns_nservers;    /* Number of currently configured nameservers */
+union dns_addr_u g_dns_servers[CONFIG_NETDB_DNSSERVER_NAMESERVERS] =
+  {
+#if defined(CONFIG_NETDB_DNSSERVER_IPv4)
+    {
+      .ipv4.sin_family      = AF_INET,
+      .ipv4.sin_port        = HTONS(DNS_DEFAULT_PORT),
+      .ipv4.sin_addr.s_addr = HTONL(CONFIG_NETDB_DNSSERVER_IPv4ADDR),
+    }
+#elif defined(CONFIG_NETDB_DNSSERVER_IPv6)
+    {
+      .ipv6.sin6_family               = AF_INET6,
+      .ipv6.sin6_port                 = HTONS(DNS_DEFAULT_PORT),
+      .ipv6.sin6_addr.in6_u.u6_addr16 =
+        {
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_1),
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_2),
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_3),
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_4),
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_5),
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_6),
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_7),
+          HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_8)
+        }
+    }
+#endif
+  };
+
+/* Number of currently configured nameservers */
+
+#if defined(CONFIG_NETDB_DNSSERVER_IPv4) || defined(CONFIG_NETDB_DNSSERVER_IPv6)
+uint8_t g_dns_nservers = 1;
+#else
+uint8_t g_dns_nservers;
+#endif
+
 #endif
 
 /****************************************************************************
diff --git a/libs/libc/netdb/lib_dnsbind.c b/libs/libc/netdb/lib_dnsbind.c
index 3db9b19f3f..491cea43a6 100644
--- a/libs/libc/netdb/lib_dnsbind.c
+++ b/libs/libc/netdb/lib_dnsbind.c
@@ -67,14 +67,6 @@ int dns_bind(sa_family_t family)
   int sd;
   int ret;
 
-  /* Has the DNS client been properly initialized? */
-
-  if (!dns_initialize())
-    {
-      nerr("ERROR: DNS client has not been initialized\n");
-      return -EDESTADDRREQ;
-    }
-
   /* Create a new socket */
 
   sd = socket(family, SOCK_DGRAM, 0);
diff --git a/libs/libc/netdb/lib_dnsinit.c b/libs/libc/netdb/lib_dnsinit.c
index 51795168a7..a50b559098 100644
--- a/libs/libc/netdb/lib_dnsinit.c
+++ b/libs/libc/netdb/lib_dnsinit.c
@@ -42,97 +42,10 @@
 
 static rmutex_t g_dns_lock = NXRMUTEX_INITIALIZER;
 
-/****************************************************************************
- * Public Data
- ****************************************************************************/
-
-#if defined(CONFIG_NETDB_DNSSERVER_IPv6) && !defined(CONFIG_NETDB_RESOLVCONF)
-
-/* This is the default IPv6 DNS server address */
-
-static const uint16_t g_ipv6_hostaddr[8] =
-{
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_1),
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_2),
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_3),
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_4),
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_5),
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_6),
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_7),
-  HTONS(CONFIG_NETDB_DNSSERVER_IPv6ADDR_8)
-};
-#endif
-
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: dns_initialize
- *
- * Description:
- *   Make sure that the DNS client has been properly initialized for use.
- *
- ****************************************************************************/
-
-bool dns_initialize(void)
-{
-#ifndef CONFIG_NETDB_RESOLVCONF
-  int nservers;
-
-  dns_semtake();
-  nservers = g_dns_nservers;
-  dns_semgive();
-
-  /* Has at least one DNS server IP address been assigned? */
-
-  if (nservers == 0)
-    {
-#if defined(CONFIG_NETDB_DNSSERVER_IPv4)
-      struct sockaddr_in addr4;
-      int ret;
-
-      /* No, configure the default IPv4 DNS server address */
-
-      addr4.sin_family      = AF_INET;
-      addr4.sin_port        = HTONS(DNS_DEFAULT_PORT);
-      addr4.sin_addr.s_addr = HTONL(CONFIG_NETDB_DNSSERVER_IPv4ADDR);
-
-      ret = dns_add_nameserver((FAR struct sockaddr *)&addr4,
-                               sizeof(struct sockaddr_in));
-      if (ret < 0)
-        {
-          return false;
-        }
-
-#elif defined(CONFIG_NETDB_DNSSERVER_IPv6)
-      struct sockaddr_in6 addr6;
-      int ret;
-
-      /* No, configure the default IPv6 DNS server address */
-
-      addr6.sin6_family = AF_INET6;
-      addr6.sin6_port   = HTONS(DNS_DEFAULT_PORT);
-      memcpy(addr6.sin6_addr.s6_addr, g_ipv6_hostaddr, 16);
-
-      ret = dns_add_nameserver((FAR struct sockaddr *)&addr6,
-                               sizeof(struct sockaddr_in6));
-      if (ret < 0)
-        {
-          return false;
-        }
-
-#else
-      /* Then we are not ready to perform DNS queries */
-
-      return false;
-#endif
-    }
-#endif /* !CONFIG_NETDB_RESOLVCONF */
-
-  return true;
-}
-
 /****************************************************************************
  * Name: dns_semtake
  *