net: dns: Initialize DNS resolver if DNS servers are set

If the config file contains DNS server addresses, then
configure the DNS resolver to use them.

Change-Id: Ie7f2bdcf7ac4bb7ee0ecf7fb5b7bd2df3379cdc3
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2017-03-20 16:44:05 +02:00
commit 8dc01261ea
4 changed files with 130 additions and 9 deletions

View file

@ -223,6 +223,25 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
void *user_data,
int32_t timeout);
/**
* @brief Get default DNS context.
*
* @detail The system level DNS context uses DNS servers that are
* defined in project config file. If no DNS servers are defined by the
* user, then resolving DNS names using default DNS context will do nothing.
* The configuration options are described in subsys/net/lib/dns/Kconfig file.
*
* @return Default DNS context.
*/
struct dns_resolve_context *dns_resolve_get_default(void);
/**
* @brief Initialize DNS subsystem.
*/
void dns_init_resolver(void);
#else
#define dns_init_resolver(...)
#endif /* CONFIG_DNS_RESOLVER */
#endif /* _DNS_RESOLVE_H */

View file

@ -28,6 +28,7 @@
#include <net/arp.h>
#include <net/nbuf.h>
#include <net/net_core.h>
#include <net/dns_resolve.h>
#include "net_private.h"
#include "net_shell.h"
@ -766,6 +767,8 @@ static inline void l3_init(void)
net_route_init();
dns_init_resolver();
NET_DBG("Network L3 init done");
}

View file

@ -36,19 +36,71 @@ config DNS_RESOLVER_ADDITIONAL_QUERIES
config DNS_RESOLVER_MAX_SERVERS
int "Number of DNS server addresses"
default 1
help
Max number of DNS servers that we can connect to. Normally one
DNS server is enough.
config DNS_NUM_CONCUR_QUERIES
int "Number of simultaneous DNS queries per one DNS context"
range 1 NET_MAX_CONTEXTS
default 1
help
Max number of DNS servers that we can connect to. Normally one
DNS server is enough. Each connection to DNS server will use one
network context.
menuconfig DNS_SERVER_IP_ADDRESSES
bool "Set DNS server IP addresses"
default n
help
Allow DNS IP addresses to be set in config file for
networking applications.
if DNS_SERVER_IP_ADDRESSES
config DNS_SERVER1
string "DNS server 1"
default ""
help
DNS server IP address 1. The address can be either IPv4 or IPv6
address. An optional port number can be given.
Following syntax is supported:
192.0.2.1
192.0.2.1:5353
2001:db8::1
[2001:db8::1]:5353
It is not mandatory to use this Kconfig option at all.
The one calling dns_resolve_init() can use this option or not
to populate the server list. If the DNS server addresses are
set here, then we automatically create default DNS context
for the user.
config DNS_SERVER2
string "DNS server 2"
default ""
help
See help in "DNS server 1" option.
config DNS_SERVER3
string "DNS server 3"
default ""
help
See help in "DNS server 1" option.
config DNS_SERVER4
string "DNS server 4"
default ""
help
See help in "DNS server 1" option.
config DNS_SERVER5
string "DNS server 5"
default ""
help
See help in "DNS server 1" option.
endif # DNS_SERVER_IP_ADDRESSES
config DNS_NUM_CONCUR_QUERIES
int "Number of simultaneous DNS queries per one DNS context"
default 1
help
This defines how many concurrent DNS queries can be generated using
same DNS context. Normally 1 is a good default value. Each query
will use one network context.
same DNS context. Normally 1 is a good default value.
config NET_DEBUG_DNS_RESOLVE
bool "Debug DNS resolver"

View file

@ -69,6 +69,8 @@ NET_BUF_POOL_DEFINE(dns_msg_pool, DNS_RESOLVER_BUF_CTR,
NET_BUF_POOL_DEFINE(dns_qname_pool, DNS_RESOLVER_BUF_CTR, DNS_MAX_NAME_LEN,
0, NULL);
static struct dns_resolve_context dns_default_ctx;
int dns_resolve_init(struct dns_resolve_context *ctx, const char *servers[])
{
#if defined(CONFIG_NET_IPV6)
@ -858,3 +860,48 @@ int dns_resolve_close(struct dns_resolve_context *ctx)
return 0;
}
struct dns_resolve_context *dns_resolve_get_default(void)
{
return &dns_default_ctx;
}
void dns_init_resolver(void)
{
#if defined(CONFIG_DNS_SERVER_IP_ADDRESSES)
static const char *dns_servers[CONFIG_DNS_RESOLVER_MAX_SERVERS + 1];
int count = CONFIG_DNS_RESOLVER_MAX_SERVERS;
int ret;
if (count > 5) {
count = 5;
}
switch (count) {
case 5:
dns_servers[4] = CONFIG_DNS_SERVER5;
/* fallthrough */
case 4:
dns_servers[3] = CONFIG_DNS_SERVER4;
/* fallthrough */
case 3:
dns_servers[2] = CONFIG_DNS_SERVER3;
/* fallthrough */
case 2:
dns_servers[1] = CONFIG_DNS_SERVER2;
/* fallthrough */
case 1:
dns_servers[0] = CONFIG_DNS_SERVER1;
/* fallthrough */
case 0:
break;
}
dns_servers[CONFIG_DNS_RESOLVER_MAX_SERVERS] = NULL;
ret = dns_resolve_init(dns_resolve_get_default(), dns_servers);
if (ret < 0) {
NET_WARN("Cannot initialize DNS resolver (%d)", ret);
}
#endif
}