Step 1 in /etc/resolv.conf support

This commit is contained in:
Gregory Nutt
2016-01-13 15:06:44 -06:00
parent 530975a9b5
commit 8e9f884eb7
9 changed files with 46 additions and 246 deletions
+17 -1
View File
@@ -533,6 +533,22 @@ config NETDB_BUFSIZE
endif # NETDB_HOSTFILE
menuconfig NETDB_RESOLVCONF
bool "DNS server file support"
default n
depends on FS_READABLE
select LIBC_NETDB
---help---
Enable DNS server look ups in resolver file.
if NETDB_RESOLVCONF
config NETDB_RESOLVCONF_PATH
string "Path to host configuration file"
default "/etc/resolv.conf"
endif # NETDB_RESOLVCONF
config NETDB_DNSCLIENT
bool "DNS Name resolution"
default n
@@ -615,7 +631,7 @@ config NETDB_DNSSERVER_IPv4ADDR
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().
10.0.0.0.1. This may be changed via dns_add_nameserver().
if NETDB_DNSSERVER_IPv6
+1 -1
View File
@@ -372,7 +372,7 @@ static int inet_ipv6_pton(FAR const char *src, FAR void *dest)
* af - The af argument specifies the family of the address. This can be
* AF_INET or AF_INET6.
* src - The src argument points to the string being passed in.
* dest - The dest argument points to a numstr into which the function stores
* dest - The dest argument points to memory into which the function stores
* the numeric address; this must be large enough to hold the numeric
* address (32 bits for AF_INET, 128 bits for AF_INET6).
*
+1 -1
View File
@@ -48,7 +48,7 @@ endif
# Add DNS lookup support
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
CSRCS += lib_dnsclient.c lib_getdnsaddr.c
CSRCS += lib_dnsclient.c lib_dnsaddserver.c lib_dnsforeach.c
endif
# Add the net directory to the build
+10 -1
View File
@@ -74,6 +74,15 @@
# define CONFIG_NETDB_DNSCLIENT_LIFESEC 3600
#endif
#define DNS_MAX_LINE 80
#define NETDB_DNS_KEYWORD "nameserver"
/****************************************************************************
* Public Types
****************************************************************************/
typedef CODE int (*dns_callback_t)(FAR void *arg, int af, FAR void *addr);
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@@ -156,7 +165,7 @@ int dns_find_answer(FAR const char *hostname, FAR struct sockaddr *addr,
FAR socklen_t *addrlen);
#endif
#undef EXTERN
o#undef EXTERN
#if defined(__cplusplus)
}
#endif
+5 -76
View File
@@ -5,7 +5,7 @@
* The uIP DNS resolver functions are used to lookup a hostname and
* map it to a numerical IP address.
*
* Copyright (C) 2007, 2009, 2012, 2014-2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2012, 2014-2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Based heavily on portions of uIP:
@@ -227,8 +227,8 @@ static bool dns_initialize(void)
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));
ret = dns_add_nameserver((FAR struct sockaddr *)&addr4,
sizeof(struct sockaddr_in));
if (ret < 0)
{
return false;
@@ -244,8 +244,8 @@ static bool dns_initialize(void)
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));
ret = dns_add_nameserver((FAR struct sockaddr *)&addr6,
sizeof(struct sockaddr_in6));
if (ret < 0)
{
return false;
@@ -877,77 +877,6 @@ int dns_query(int sd, FAR const char *hostname, FAR struct sockaddr *addr,
return -ETIMEDOUT;
}
/****************************************************************************
* Name: dns_setserver
*
* Description:
* Configure which DNS server to use for queries
*
****************************************************************************/
int dns_setserver(FAR const struct sockaddr *addr, socklen_t addrlen)
{
FAR uint16_t *pport;
size_t copylen;
DEBUGASSERT(addr != NULL);
/* Copy the new server IP address into our private global data structure */
#ifdef CONFIG_NET_IPv4
/* Check for an IPv4 address */
if (addr->sa_family == AF_INET)
{
/* Set up for the IPv4 address copy */
copylen = sizeof(struct sockaddr_in);
pport = &g_dns_server.ipv4.sin_port;
}
else
#endif
#ifdef CONFIG_NET_IPv6
/* Check for an IPv6 address */
if (addr->sa_family == AF_INET6)
{
/* Set up for the IPv6 address copy */
copylen = sizeof(struct sockaddr_in6);
pport = &g_dns_server.ipv6.sin6_port;
}
else
#endif
{
nvdbg("ERROR: Unsupported family: %d\n", addr->sa_family);
return -ENOSYS;
}
/* Copy the IP address */
if (addrlen < copylen)
{
nvdbg("ERROR: Invalid addrlen %ld for family %d\n",
(long)addrlen, addr->sa_family);
return -EINVAL;
}
memcpy(&g_dns_server.addr, addr, copylen);
/* A port number of zero means to use the default DNS server port number */
if (*pport == 0)
{
*pport = HTONS(DNS_DEFAULT_PORT);
}
/* We now have a valid DNS address */
g_dns_address = true;
return OK;
}
/****************************************************************************
* Name: dns_getserver
*
-98
View File
@@ -1,98 +0,0 @@
/****************************************************************************
* libc/netdb/lib_dnsgetaddr.c
*
* Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <netinet/in.h>
#include <nuttx/net/dns.h>
#include <apps/netutils/netlib.h>
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: dns_getaddr
*
* Description:
* Get the DNS server IPv4 address
*
* Parameters:
* ipaddr The location to return the IPv4 address
*
* Return:
* Zero (OK) is returned on success; A negated errno value is returned
* on failure.
*
****************************************************************************/
int dns_getaddr(FAR struct in_addr *inaddr)
{
struct sockaddr_in addr;
socklen_t addrlen;
int ret = -EINVAL;
if (inaddr)
{
addrlen = sizeof(struct sockaddr_in);
ret = dns_getserver((FAR struct sockaddr *)&addr, &addrlen);
if (ret >= 0)
{
/* Sanity check */
DEBUGASSERT(addr.sin_family == AF_INET &&
addrlen == sizeof(struct sockaddr_in));
memcpy(inaddr, &addr.sin_addr, sizeof(struct in_addr));
}
}
return ret;
}
#endif /* CONFIG_NET_IPv4 && CONFIG_NETDB_DNSCLIENT */