NETDB: Make sure that we have a valid DNS server address before attempting DNS queuries; Provide an optional default DNS server address in the configuration

This commit is contained in:
Gregory Nutt 2015-07-13 08:19:31 -06:00
parent 9a6cf5724e
commit fcaac468b1
4 changed files with 210 additions and 6 deletions

@ -1 +1 @@
Subproject commit 7443c70520160a7fa7374d7bdcd23f25af2a498f
Subproject commit a4500bea3e22eac03299ae2a4d1455c2db09dc42

View File

@ -123,6 +123,10 @@
#define DNS_FLAG2_ERR_NONE 0x00
#define DNS_FLAG2_ERR_NAME 0x03
/* Default DNS server port number */
#define DNS_DEFAULT_PORT 53
/****************************************************************************
* Public Type Definitions
****************************************************************************/

View File

@ -546,9 +546,11 @@ if NETDB_DNSCLIENT
config NETDB_DNSCLIENT_ENTRIES
int "Number of DNS resolver entries"
default 8
default 0 if DEFAULT_SMALL
default 8 if !DEFAULT_SMALL
---help---
Number of DNS resolver entries. Default: 8
Number of cached DNS resolver entries. Default: 8. Zero disables
all cached name resolutions.
config NETDB_DNSCLIENT_MAXRESPONSE
int "Max response size"
@ -558,6 +560,114 @@ config NETDB_DNSCLIENT_MAXRESPONSE
can be received by the DNS resolver. The default is 96 but may
need to be larger on enterprise networks (perhaps 176).
choice
prompt "DNS server address type"
default NETDB_DNSSERVER_NOADDR
config NETDB_DNSSERVER_NOADDR
bool "No default DNS server address"
config NETDB_DNSSERVER_IPv4
bool "IPv4 DNS server address"
depends on NET_IPv4
config NETDB_DNSSERVER_IPv6
bool "IPv6 DNS server address"
depends on NET_IPv6
endchoice # DNS server address type
config NETDB_DNSSERVER_IPv4ADDR
hex "Target IPv4 address"
default 0x0a000001
depends on NETDB_DNSSERVER_IPv4
---help---
Default DNS server IPv4 address in host byte order. Default value
10.0.0.0.1. This may be changed via dns_setserver().
if NETDB_DNSSERVER_IPv6
config NETDB_DNSSERVER_IPv6ADDR_1
hex "[0]"
default 0xfc00
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the first
of the 8-values. The default for all eight values is fc00::1.
config NETDB_DNSSERVER_IPv6ADDR_2
hex "[1]"
default 0x0000
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the second
of the 8-values. The default for all eight values is fc00::1.
config NETDB_DNSSERVER_IPv6ADDR_3
hex "[2]"
default 0x0000
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the third
of the 8-values. The default for all eight values is fc00::1.
config NETDB_DNSSERVER_IPv6ADDR_4
hex "[3]"
default 0x0000
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the fourth
of the 8-values. The default for all eight values is fc00::1.
config NETDB_DNSSERVER_IPv6ADDR_5
hex "[4]"
default 0x0000
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the fifth
of the 8-values. The default for all eight values is fc00::1.
config NETDB_DNSSERVER_IPv6ADDR_6
hex "[5]"
default 0x0000
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the sixth
of the 8-values. The default for all eight values is fc00::1.
config NETDB_DNSSERVER_IPv6ADDR_7
hex "[6]"
default 0x0000
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the seventh
of the 8-values. The default for all eight values is fc00::1.
config NETDB_DNSSERVER_IPv6ADDR_8
hex "[7]"
default 0x0001
range 0x0 0xffff
---help---
This is the default IP address of the DNS server. This is a 16-bit
integer value in host order. Each of the eight values forming the
full IPv6 address must be specified individually. This is the last
of the 8-values. The default for all eight values is fc00::1.
endif # NETDB_DNSSERVER_IPv6
endif # NETDB_DNSCLIENT
comment "Non-standard Library Support"

View File

@ -49,6 +49,7 @@
#include <sys/socket.h>
#include <sys/time.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
@ -94,13 +95,91 @@ union dns_server_u
* Private Data
****************************************************************************/
static uint8_t g_seqno;
static bool g_dns_address; /* We have the address of the DNS server */
static uint8_t g_seqno; /* Sequence number of the next request */
/* The DNS server address */
static union dns_server_u g_dns_server;
#ifdef CONFIG_NETDB_DNSSERVER_IPv6
/* 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.
*
****************************************************************************/
static bool dns_initialize(void)
{
/* Has the DNS server IP address been assigned? */
if (!g_dns_address)
{
#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 = DNS_DEFAULT_PORT;
addr4.sin_addr.s_addr = HTONL(CONFIG_NETDB_DNSSERVER_IPv4ADDR);
ret = dns_setserver((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 = DNS_DEFAULT_PORT;
memcpy(addr6.sin6_addr.s6_addr, g_ipv6_hostaddr, 16);
ret = dns_setserver((FAR struct sockaddr *)&addr6,
sizeof(struct sockaddr_in6));
if (ret < 0)
{
return false;
}
#else
/* No, then we are not ready to perform DNS queries */
return false;
#endif
}
return true;
}
/****************************************************************************
* Name: dns_parse_name
*
@ -144,7 +223,7 @@ static int dns_send_query(int sd, FAR const char *name,
FAR uint8_t *dest;
FAR uint8_t *nptr;
FAR const char *src;
uint8_t seqno = g_seqno++;
uint8_t seqno = g_seqno++; /* REVISIT: Not thread safe */
uint8_t buffer[SEND_BUFFER_SIZE];
socklen_t addrlen;
int errcode;
@ -435,6 +514,14 @@ int dns_bind(void)
int sd;
int ret;
/* Has the DNS client been properly initialized? */
if (!dns_initialize())
{
ndbg("ERROR: DNS client has not been initialized\n");
return -EDESTADDRREQ;
}
/* Create a new socket */
sd = socket(PF_INET, SOCK_DGRAM, 0);
@ -667,9 +754,12 @@ int dns_setserver(FAR const struct sockaddr *addr, socklen_t addrlen)
if (*pport == 0)
{
*pport = HTONS(53);
*pport = HTONS(DNS_DEFAULT_PORT);
}
/* We now have a valid DNS address */
g_dns_address = true;
return OK;
}