From daf39c03bdd92aaa21b9d928e1d2ab9c6160e2f0 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Thu, 11 Aug 2022 10:58:32 +0800 Subject: [PATCH] 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 --- 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 78d335792a1de..a964445966004 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 cc4e0b5933636..475160078a635 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 3db9b19f3f041..491cea43a6a05 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 51795168a77f4..a50b559098da3 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 *