2016-01-13 23:02:23 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/netdb/lib_dnsaddserver.c
|
2016-01-13 23:02:23 +01:00
|
|
|
*
|
2020-03-14 00:41: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
|
|
|
*
|
2020-03-14 00:41:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-13 23:02:23 +01:00
|
|
|
*
|
2020-03-14 00:41: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 <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/ip.h>
|
2016-01-13 23:02:23 +01:00
|
|
|
#include <nuttx/net/dns.h>
|
|
|
|
|
|
|
|
#include "netdb/lib_dns.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETDB_RESOLVCONF
|
2020-04-24 11:21:37 +02:00
|
|
|
/* The DNS server addresses */
|
2016-01-13 23:02:23 +01:00
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
union dns_addr_u g_dns_servers[CONFIG_NETDB_DNSSERVER_NAMESERVERS];
|
|
|
|
uint8_t g_dns_nservers; /* Number of currently configured nameservers */
|
2016-01-13 23:02:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_add_nameserver
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure which DNS server to use for queries
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF
|
|
|
|
int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
FAR FILE *stream;
|
2016-01-15 00:55:53 +01:00
|
|
|
char addrstr[DNS_MAX_ADDRSTR];
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
|
|
|
|
uint16_t port;
|
|
|
|
#endif
|
2016-01-13 23:29:50 +01:00
|
|
|
int ret;
|
2016-01-13 23:02:23 +01:00
|
|
|
|
2020-03-14 00:41:21 +01:00
|
|
|
stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "a+");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
/* Check for an IPv4 address */
|
|
|
|
|
|
|
|
if (addr->sa_family == AF_INET)
|
|
|
|
{
|
2016-01-14 15:32:20 +01:00
|
|
|
if (addrlen < sizeof(struct sockaddr_in))
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
2016-01-13 23:29:50 +01:00
|
|
|
goto errout;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in *in4 = (FAR struct sockaddr_in *)addr;
|
|
|
|
|
2020-03-14 00:41:21 +01:00
|
|
|
if (inet_ntop(AF_INET, &in4->sin_addr, addrstr,
|
|
|
|
DNS_MAX_ADDRSTR) == NULL)
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
ret = -get_errno();
|
2020-03-28 18:08:10 +01:00
|
|
|
nerr("ERROR: inet_ntop failed: %d\n", ret);
|
|
|
|
DEBUGASSERT(ret < 0);
|
2016-01-13 23:29:50 +01:00
|
|
|
goto errout;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
2016-01-15 00:55:53 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
|
|
|
|
/* Get the port number */
|
|
|
|
|
|
|
|
port = ntohs(in4->sin_port);
|
|
|
|
#endif
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
2016-01-13 23:29:50 +01:00
|
|
|
}
|
|
|
|
else
|
2016-01-13 23:02:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2016-01-15 00:55:53 +01:00
|
|
|
/* Check for an IPv6 address */
|
2016-01-13 23:02:23 +01:00
|
|
|
|
|
|
|
if (addr->sa_family == AF_INET6)
|
|
|
|
{
|
2016-01-14 15:32:20 +01:00
|
|
|
if (addrlen < sizeof(struct sockaddr_in6))
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
2016-01-13 23:29:50 +01:00
|
|
|
goto errout;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in6 *in6 = (FAR struct sockaddr_in6 *)addr;
|
|
|
|
|
2020-03-14 00:41:21 +01:00
|
|
|
if (inet_ntop(AF_INET6, &in6->sin6_addr, addrstr,
|
|
|
|
DNS_MAX_ADDRSTR) == NULL)
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
ret = -get_errno();
|
2020-03-28 18:08:10 +01:00
|
|
|
nerr("ERROR: inet_ntop failed: %d\n", ret);
|
|
|
|
DEBUGASSERT(ret < 0);
|
2016-01-13 23:29:50 +01:00
|
|
|
goto errout;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
2016-01-15 00:55:53 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
|
|
|
|
/* Get the port number */
|
|
|
|
|
|
|
|
port = ntohs(in6->sin6_port);
|
|
|
|
#endif
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
2016-01-13 23:29:50 +01:00
|
|
|
}
|
|
|
|
else
|
2016-01-13 23:02:23 +01:00
|
|
|
#endif
|
2016-01-13 23:29:50 +01:00
|
|
|
{
|
2021-01-27 08:43:01 +01:00
|
|
|
nerr("ERROR: Unsupported family: %d\n", addr->sa_family);
|
2016-01-13 23:29:50 +01:00
|
|
|
ret = -ENOSYS;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2016-01-15 00:55:53 +01:00
|
|
|
/* Write the new record to the end of the resolv.conf file. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
|
|
|
|
/* The OpenBSD version supports a [host]:port syntax. When a 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (port != 0 && port != DNS_DEFAULT_PORT)
|
|
|
|
{
|
2020-03-28 18:08:10 +01:00
|
|
|
ret = fprintf(stream, "%s [%s]:%u\n",
|
|
|
|
NETDB_DNS_KEYWORD, addrstr, port);
|
2016-01-15 00:55:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-03-28 18:08:10 +01:00
|
|
|
ret = fprintf(stream, "%s %s\n",
|
|
|
|
NETDB_DNS_KEYWORD, addrstr);
|
2016-01-15 00:55:53 +01:00
|
|
|
}
|
2016-01-13 23:29:50 +01:00
|
|
|
|
2020-03-28 18:08:10 +01:00
|
|
|
if (ret < 0)
|
2016-01-13 23:29:50 +01:00
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
ret = -get_errno();
|
2020-03-28 18:08:10 +01:00
|
|
|
nerr("ERROR: fprintf failed: %d\n", ret);
|
|
|
|
DEBUGASSERT(ret < 0);
|
2016-01-13 23:29:50 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:02:10 +01:00
|
|
|
dns_notify_nameserver(addr, addrlen);
|
2016-01-13 23:29:50 +01:00
|
|
|
ret = OK;
|
2016-01-13 23:02:23 +01:00
|
|
|
|
2016-01-13 23:29:50 +01:00
|
|
|
errout:
|
2016-01-13 23:02:23 +01:00
|
|
|
fclose(stream);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_NETDB_RESOLVCONF */
|
|
|
|
|
|
|
|
int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
FAR uint16_t *pport;
|
|
|
|
size_t copylen;
|
2020-04-24 11:21:37 +02:00
|
|
|
int nservers;
|
|
|
|
int idx;
|
2016-01-13 23:02:23 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(addr != NULL);
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
/* Get the index of the next free nameserver slot. */
|
|
|
|
|
|
|
|
dns_semtake();
|
|
|
|
if (g_dns_nservers == CONFIG_NETDB_DNSSERVER_NAMESERVERS)
|
|
|
|
{
|
|
|
|
idx = 0;
|
|
|
|
nservers = g_dns_nservers;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx = g_dns_nservers;
|
|
|
|
nservers = idx + 1;
|
|
|
|
}
|
2016-01-13 23:02:23 +01:00
|
|
|
|
|
|
|
#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);
|
2020-04-24 11:21:37 +02:00
|
|
|
pport = &g_dns_servers[idx].ipv4.sin_port;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
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);
|
2020-04-24 11:21:37 +02:00
|
|
|
pport = &g_dns_servers[idx].ipv6.sin6_port;
|
2016-01-13 23:02:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
nerr("ERROR: Unsupported family: %d\n", addr->sa_family);
|
2020-04-24 11:21:37 +02:00
|
|
|
dns_semgive();
|
2016-01-13 23:02:23 +01:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the IP address */
|
|
|
|
|
|
|
|
if (addrlen < copylen)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
nerr("ERROR: Invalid addrlen %ld for family %d\n",
|
2016-01-13 23:02:23 +01:00
|
|
|
(long)addrlen, addr->sa_family);
|
2020-04-24 11:21:37 +02:00
|
|
|
dns_semgive();
|
2016-01-13 23:02:23 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
memcpy(&g_dns_servers[idx].addr, addr, copylen);
|
2016-01-13 23:02:23 +01:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
g_dns_nservers = nservers;
|
|
|
|
dns_semgive();
|
2019-03-19 17:02:10 +01:00
|
|
|
dns_notify_nameserver(addr, addrlen);
|
2016-01-13 23:02:23 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
#endif /* CONFIG_NETDB_RESOLVCONF */
|
2016-01-13 23:02:23 +01:00
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|