2016-01-13 23:02:23 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/netdb/lib_dnsforeach.c
|
2016-01-13 23:02:23 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-01-13 23:02:23 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-13 23:02:23 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-01-13 23:02:23 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
2020-04-24 11:21:37 +02:00
|
|
|
* Traverse each nameserver entry in the resolv.conf file and perform
|
2016-01-13 23:02:23 +01:00
|
|
|
* the provided callback.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF
|
|
|
|
|
|
|
|
int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg)
|
|
|
|
{
|
2018-11-23 14:09:47 +01:00
|
|
|
union dns_addr_u u;
|
2016-01-13 23:02:23 +01:00
|
|
|
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 */
|
|
|
|
|
2020-03-28 16:19:17 +01:00
|
|
|
stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "r");
|
2016-01-13 23:02:23 +01:00
|
|
|
if (stream == NULL)
|
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
ret = -get_errno();
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to open %s: %d\n",
|
2020-03-28 18:08:10 +01:00
|
|
|
CONFIG_NETDB_RESOLVCONF_PATH, ret);
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
return ret;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 06:05:22 +02:00
|
|
|
dns_semtake();
|
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
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
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Missing address in %s record\n",
|
2016-01-14 15:32:20 +01:00
|
|
|
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 == '[')
|
|
|
|
{
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Make sure that there is a right bracket */
|
2016-01-15 00:55:53 +01:00
|
|
|
|
|
|
|
ptr = strchr(addrstr, ']');
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Missing right bracket after %s\n", line);
|
2016-01-15 00:55:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace the right bracket with a NULL terminator */
|
|
|
|
|
|
|
|
addrstr++;
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
|
|
|
/* Get the port number following the right bracket */
|
|
|
|
|
2020-03-28 16:20:44 +01:00
|
|
|
if (*ptr++ == ':')
|
2016-01-15 00:55:53 +01:00
|
|
|
{
|
|
|
|
FAR char *portstr;
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
/* Isolate the port string */
|
|
|
|
|
|
|
|
portstr = ptr;
|
2020-03-28 16:20:44 +01:00
|
|
|
ptr = find_spaces(ptr);
|
2016-01-15 00:55:53 +01:00
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
/* Get the port number */
|
|
|
|
|
|
|
|
tmp = atoi(portstr);
|
|
|
|
if (tmp != 0)
|
|
|
|
{
|
2022-01-18 08:38:00 +01:00
|
|
|
port = HTONS(tmp);
|
2016-01-15 00:55:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
{
|
2021-01-27 08:43:01 +01:00
|
|
|
nerr("ERROR: Unrecognized address: %s\n", addrstr);
|
2016-01-13 23:02:23 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-30 14:49:28 +02:00
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2022-04-01 06:05:22 +02:00
|
|
|
break;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 06:05:22 +02:00
|
|
|
dns_semgive();
|
2016-01-13 23:02:23 +01:00
|
|
|
fclose(stream);
|
2022-04-01 06:05:22 +02:00
|
|
|
return ret;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_NETDB_RESOLVCONF */
|
|
|
|
|
|
|
|
int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg)
|
|
|
|
{
|
2020-04-24 11:21:37 +02:00
|
|
|
FAR struct sockaddr *addr;
|
2016-01-13 23:02:23 +01:00
|
|
|
int ret = OK;
|
2020-04-24 11:21:37 +02:00
|
|
|
int i;
|
2016-01-13 23:02:23 +01:00
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
dns_semtake();
|
|
|
|
for (i = 0; i < g_dns_nservers; i++)
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
/* Check for an IPv4 address */
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
if (g_dns_servers[i].addr.sa_family == AF_INET)
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
2020-04-24 11:21:37 +02:00
|
|
|
struct sockaddr_in copy;
|
|
|
|
|
|
|
|
/* Operate on copy of server address, in case it changes. */
|
|
|
|
|
|
|
|
memcpy(©, &g_dns_servers[i].ipv4, sizeof(struct sockaddr_in));
|
|
|
|
addr = (FAR struct sockaddr *)©
|
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
/* Perform the callback */
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
ret = callback(arg, addr, sizeof(struct sockaddr_in));
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
/* Check for an IPv6 address */
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
if (g_dns_servers[i].addr.sa_family == AF_INET6)
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
2020-04-24 11:21:37 +02:00
|
|
|
struct sockaddr_in6 copy;
|
|
|
|
|
|
|
|
/* Operate on copy of server address, in case it changes. */
|
|
|
|
|
|
|
|
memcpy(©, &g_dns_servers[i].ipv6, sizeof(struct sockaddr_in6));
|
|
|
|
addr = (FAR struct sockaddr *)©
|
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
/* Perform the callback */
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
ret = callback(arg, addr, sizeof(struct sockaddr_in6));
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
nerr("ERROR: Unsupported family: %d\n",
|
2020-04-24 11:21:37 +02:00
|
|
|
g_dns_servers[i].addr.sa_family);
|
2016-01-13 23:02:23 +01:00
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
2020-04-24 11:21:37 +02:00
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
dns_semgive();
|
2016-01-13 23:02:23 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NETDB_RESOLVCONF */
|
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|