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 <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2022-08-11 10:58:32 +08:00 committed by Petro Karashchenko
parent 6d1646625a
commit daf39c03bd
4 changed files with 35 additions and 107 deletions

View File

@ -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
*

View File

@ -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
/****************************************************************************

View File

@ -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);

View File

@ -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
*