2016-01-13 23:02:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* libc/netdb/lib_dnsforeach.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 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>
|
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
#include <stdio.h>
|
2016-01-15 00:55:53 +01:00
|
|
|
#include <stdlib.h>
|
2016-01-14 15:32:20 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
#include <nuttx/net/dns.h>
|
|
|
|
|
|
|
|
#include "netdb/lib_dns.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF
|
2016-01-13 23:02:23 +01:00
|
|
|
static FAR char *skip_spaces(FAR char *ptr)
|
|
|
|
{
|
|
|
|
while (isspace(*ptr)) ptr++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FAR char *find_spaces(FAR char *ptr)
|
|
|
|
{
|
2016-01-14 15:32:20 +01:00
|
|
|
while (*ptr && !isspace(*ptr)) ptr++;
|
2016-01-13 23:02:23 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
2016-01-14 15:32:20 +01:00
|
|
|
#endif
|
2016-01-13 23:02:23 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_foreach_nameserver
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse each nameserver entry in the resolv.conf file and perform the
|
|
|
|
* the provided callback.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF
|
|
|
|
|
|
|
|
int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg)
|
|
|
|
{
|
|
|
|
union dns_server_u u;
|
|
|
|
FAR FILE *stream;
|
|
|
|
char line[DNS_MAX_LINE];
|
2016-01-14 22:12:42 +01:00
|
|
|
FAR char *addrstr;
|
2016-01-13 23:02:23 +01:00
|
|
|
FAR char *ptr;
|
2016-01-15 00:55:53 +01:00
|
|
|
uint16_t port;
|
2016-01-13 23:02:23 +01:00
|
|
|
int keylen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Open the resolver configuration file */
|
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "rb");
|
2016-01-13 23:02:23 +01:00
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
ndbg("ERROR: Failed to open %s: %d\n",
|
2016-01-14 15:32:20 +01:00
|
|
|
CONFIG_NETDB_RESOLVCONF_PATH, errcode);
|
2016-01-13 23:02:23 +01:00
|
|
|
DEBUGASSERT(errcode > 0);
|
|
|
|
return -errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
keylen = strlen(NETDB_DNS_KEYWORD);
|
|
|
|
while (fgets(line, DNS_MAX_LINE, stream) != NULL)
|
|
|
|
{
|
|
|
|
ptr = skip_spaces(line);
|
|
|
|
if (strncmp(ptr, NETDB_DNS_KEYWORD, keylen) == 0)
|
|
|
|
{
|
2016-01-14 15:32:20 +01:00
|
|
|
/* Skip over the 'nameserver' keyword */
|
|
|
|
|
|
|
|
ptr = find_spaces(ptr);
|
2016-01-14 22:12:42 +01:00
|
|
|
addrstr = skip_spaces(ptr);
|
|
|
|
if (*addrstr == '\0')
|
2016-01-14 15:32:20 +01:00
|
|
|
{
|
|
|
|
ndbg("ERROR: Missing address in %s record\n",
|
|
|
|
CONFIG_NETDB_RESOLVCONF_PATH);
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-13 23:02:23 +01:00
|
|
|
|
2016-01-14 22:12:42 +01:00
|
|
|
/* Make sure that the address string is NUL terminated and
|
|
|
|
* not followed by garbage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ptr = find_spaces(addrstr);
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2016-01-15 00:55:53 +01:00
|
|
|
/* Convert the address string to a binary representation. */
|
|
|
|
|
|
|
|
port = HTONS(DNS_DEFAULT_PORT);
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
|
|
|
|
/* The OpenBSD version supports a [host]:port syntax. When a
|
2016-01-13 23:02:23 +01:00
|
|
|
* non-standard port is specified the host address must be
|
|
|
|
* enclosed in square brackets. For example:
|
|
|
|
*
|
|
|
|
* nameserver [10.0.0.1]:5353
|
|
|
|
* nameserver [::1]:5353
|
|
|
|
*/
|
|
|
|
|
2016-01-15 00:55:53 +01:00
|
|
|
if (*addrstr == '[')
|
|
|
|
{
|
|
|
|
/* Make sure that ther is a right bracket */
|
|
|
|
|
|
|
|
ptr = strchr(addrstr, ']');
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
ndbg("ERROR: Missing right bracket after %s\n", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace the right bracket with a NULL terminator */
|
|
|
|
|
|
|
|
addrstr++;
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
|
|
|
/* Get the port number following the right bracket */
|
|
|
|
|
|
|
|
if (*ptr == ':')
|
|
|
|
{
|
|
|
|
FAR char *portstr;
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
/* Isolate the port string */
|
|
|
|
|
|
|
|
portstr = ptr;
|
|
|
|
ptr = find_spaces(addrstr);
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
/* Get the port number */
|
|
|
|
|
|
|
|
tmp = atoi(portstr);
|
|
|
|
if (tmp != 0)
|
|
|
|
{
|
|
|
|
port = htons(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NETDB_RESOLVCONF_NONSTDPORT */
|
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
/* Try to convert the IPv4 address */
|
|
|
|
|
2016-01-14 22:12:42 +01:00
|
|
|
ret = inet_pton(AF_INET, addrstr, &u.ipv4.sin_addr);
|
2016-01-13 23:02:23 +01:00
|
|
|
|
|
|
|
/* The inet_pton() function returns 1 if the conversion succeeds */
|
|
|
|
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
u.ipv4.sin_family = AF_INET;
|
2016-01-15 00:55:53 +01:00
|
|
|
u.ipv4.sin_port = port;
|
2016-01-14 15:32:20 +01:00
|
|
|
ret = callback(arg, (FAR struct sockaddr *)&u.ipv4,
|
|
|
|
sizeof(struct sockaddr_in));
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
{
|
|
|
|
/* Try to convert the IPv6 address */
|
|
|
|
|
2016-01-14 22:12:42 +01:00
|
|
|
ret = inet_pton(AF_INET6, addrstr, &u.ipv6.sin6_addr);
|
2016-01-13 23:02:23 +01:00
|
|
|
|
|
|
|
/* The inet_pton() function returns 1 if the conversion
|
|
|
|
* succeeds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
u.ipv6.sin6_family = AF_INET6;
|
2016-01-15 00:55:53 +01:00
|
|
|
u.ipv6.sin6_port = port;
|
2016-01-14 15:32:20 +01:00
|
|
|
ret = callback(arg, (FAR struct sockaddr *)&u.ipv6,
|
|
|
|
sizeof(struct sockaddr_in6));
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-01-14 22:12:42 +01:00
|
|
|
ndbg("ERROR: Unrecognized address: %s\n", addrstr)
|
2016-01-13 23:02:23 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
fclose(stream);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(stream);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_NETDB_RESOLVCONF */
|
|
|
|
|
|
|
|
int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
if (g_dns_address)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
/* Check for an IPv4 address */
|
|
|
|
|
|
|
|
if (g_dns_server.addr.sa_family == AF_INET)
|
|
|
|
{
|
|
|
|
/* Perform the callback */
|
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
ret = callback(arg, (FAR struct sockaddr *)&g_dns_server.ipv4,
|
|
|
|
sizeof(struct sockaddr_in));
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
/* Check for an IPv6 address */
|
|
|
|
|
|
|
|
if (g_dns_server.addr.sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
/* Perform the callback */
|
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
ret = callback(arg, (FAR struct sockaddr *)&g_dns_server.ipv6,
|
|
|
|
sizeof(struct sockaddr_in6));
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
nvdbg("ERROR: Unsupported family: %d\n",
|
|
|
|
g_dns_server.addr.sa_family);
|
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NETDB_RESOLVCONF */
|
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|