Skip to content

Commit

Permalink
dns_client: directly initialize g_dns_servers and remove dns_initiali…
Browse files Browse the repository at this point in the history
…ze 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>
(cherry picked from commit daf39c0)
  • Loading branch information
SPRESENSE committed Nov 7, 2022
1 parent 83f83cf commit e6db2fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 107 deletions.
10 changes: 0 additions & 10 deletions libs/libc/netdb/lib_dns.h
Expand Up @@ -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
*
Expand Down
37 changes: 35 additions & 2 deletions libs/libc/netdb/lib_dnsaddserver.c
Expand Up @@ -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

/****************************************************************************
Expand Down
8 changes: 0 additions & 8 deletions libs/libc/netdb/lib_dnsbind.c
Expand Up @@ -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);
Expand Down
87 changes: 0 additions & 87 deletions libs/libc/netdb/lib_dnsinit.c
Expand Up @@ -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
*
Expand Down

0 comments on commit e6db2fe

Please sign in to comment.