2015-07-12 17:28:03 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/netdb/lib_dns.h
|
2015-07-12 17:28:03 +02:00
|
|
|
* DNS resolver code header file.
|
|
|
|
*
|
2020-03-30 14:49:28 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011-2012, 2014 Gregory Nutt.
|
|
|
|
* All rights reserved.
|
2015-07-12 17:28:03 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Inspired by/based on uIP logic by Adam Dunkels:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2003, Adam Dunkels. All rights reserved.
|
|
|
|
* Author Adam Dunkels <adam@dunkels.com>
|
|
|
|
*
|
|
|
|
* 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. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-06-30 23:12:34 +02:00
|
|
|
#ifndef __LIBS_LIBC_NETDB_LIB_DNS_H
|
|
|
|
#define __LIBS_LIBC_NETDB_LIB_DNS_H
|
2015-07-12 17:28:03 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2015-07-12 21:45:52 +02:00
|
|
|
#include <sys/socket.h>
|
2015-07-12 17:28:03 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/netconfig.h>
|
|
|
|
#include <nuttx/net/dns.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2020-03-30 14:49:28 +02:00
|
|
|
|
|
|
|
/* DNS client configuration *************************************************/
|
2015-07-12 17:28:03 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_NETDB_DNSCLIENT_ENTRIES
|
2016-01-09 22:56:08 +01:00
|
|
|
# define CONFIG_NETDB_DNSCLIENT_ENTRIES 4
|
2015-07-12 17:28:03 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETDB_DNSCLIENT_MAXRESPONSE
|
|
|
|
# define CONFIG_NETDB_DNSCLIENT_MAXRESPONSE 96
|
|
|
|
#endif
|
|
|
|
|
2015-07-13 18:41:32 +02:00
|
|
|
#ifndef CONFIG_NETDB_DNSCLIENT_NAMESIZE
|
|
|
|
# define CONFIG_NETDB_DNSCLIENT_NAMESIZE 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETDB_DNSCLIENT_LIFESEC
|
|
|
|
# define CONFIG_NETDB_DNSCLIENT_LIFESEC 3600
|
|
|
|
#endif
|
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
#ifndef CONFIG_NETDB_RESOLVCONF_PATH
|
|
|
|
# define CONFIG_NETDB_RESOLVCONF_PATH "/etc/resolv.conf"
|
|
|
|
#endif
|
|
|
|
|
2020-04-24 11:21:37 +02:00
|
|
|
#ifndef CONFIG_NETDB_DNSSERVER_NAMESERVERS
|
|
|
|
# define CONFIG_NETDB_DNSSERVER_NAMESERVERS 1
|
|
|
|
#endif
|
|
|
|
|
2016-01-15 00:55:53 +01:00
|
|
|
#define DNS_MAX_ADDRSTR 48
|
|
|
|
#define DNS_MAX_LINE 64
|
2016-01-13 22:06:44 +01:00
|
|
|
#define NETDB_DNS_KEYWORD "nameserver"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Types
|
|
|
|
****************************************************************************/
|
2020-03-30 14:49:28 +02:00
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
/* This describes either an IPv4 or IPv6 address. It is essentially a named
|
|
|
|
* alternative to sockaddr_storage.
|
|
|
|
*/
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
union dns_addr_u
|
2016-01-13 23:02:23 +01:00
|
|
|
{
|
|
|
|
struct sockaddr addr; /* Common address representation */
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct sockaddr_in ipv4; /* IPv4 address */
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct sockaddr_in6 ipv6; /* IPv6 address */
|
|
|
|
#endif
|
|
|
|
};
|
2016-01-13 22:06:44 +01:00
|
|
|
|
2015-07-12 17:28:03 +02:00
|
|
|
/****************************************************************************
|
2016-01-13 23:02:23 +01:00
|
|
|
* Public Data
|
2015-07-12 17:28:03 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#undef EXTERN
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
#define EXTERN extern "C"
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
#define EXTERN extern
|
|
|
|
#endif
|
|
|
|
|
2016-01-13 23:02:23 +01:00
|
|
|
#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
|
|
|
EXTERN union dns_addr_u g_dns_servers[];
|
|
|
|
EXTERN uint8_t g_dns_nservers;
|
2016-01-13 23:02:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-14 17:17:15 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Make sure that the DNS client has been properly initialized for use.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool dns_initialize(void);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_semtake
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-24 11:21:37 +02:00
|
|
|
* Take the DNS semaphore, ignoring errors due to the receipt of signals.
|
2016-01-14 17:17:15 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void dns_semtake(void);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_semgive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the DNS semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void dns_semgive(void);
|
|
|
|
|
2015-07-12 17:28:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the DNS resolver and return a socket bound to the DNS name
|
|
|
|
* server. The name server was previously selected via dns_server().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the bound, non-negative socket descriptor is returned. A
|
|
|
|
* negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dns_bind(void);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_query
|
|
|
|
*
|
|
|
|
* Description:
|
2017-05-11 21:35:56 +02:00
|
|
|
* Using the DNS resolver socket (sd), look up the 'hostname', and
|
2015-07-12 17:28:03 +02:00
|
|
|
* return its IP address in 'ipaddr'
|
|
|
|
*
|
2015-07-13 18:41:32 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* sd - The socket descriptor previously initialized by dsn_bind().
|
|
|
|
* hostname - The hostname string to be resolved.
|
2018-11-23 14:09:47 +01:00
|
|
|
* addr - The location to return the IP addresses associated with the
|
|
|
|
* hostname.
|
|
|
|
* naddr - On entry, the count of addresses backing up the 'addr'
|
|
|
|
* pointer. On return, this location will hold the actual count of
|
|
|
|
* the returned addresses.
|
2015-07-13 18:41:32 +02:00
|
|
|
*
|
2015-07-12 17:28:03 +02:00
|
|
|
* Returned Value:
|
|
|
|
* Returns zero (OK) if the query was successful.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
int dns_query(int sd, FAR const char *hostname, FAR union dns_addr_u *addr,
|
|
|
|
FAR int *naddr);
|
2015-07-12 17:28:03 +02:00
|
|
|
|
2016-01-14 17:17:15 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_save_answer
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-28 18:08:10 +01:00
|
|
|
* Save the last resolved hostname in the DNS cache
|
2016-01-14 17:17:15 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* hostname - The hostname string to be cached.
|
2018-11-23 14:09:47 +01:00
|
|
|
* addr - The IP addresses associated with the hostname.
|
|
|
|
* naddr - The count of the IP addresses.
|
2016-01-14 17:17:15 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
|
|
|
void dns_save_answer(FAR const char *hostname,
|
2018-11-23 14:09:47 +01:00
|
|
|
FAR const union dns_addr_u *addr, int naddr);
|
2016-01-14 17:17:15 +01:00
|
|
|
#endif
|
|
|
|
|
2021-05-24 16:02:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_clear_answer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear the resolved hostname in the DNS cache
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
|
|
|
void dns_clear_answer(void);
|
|
|
|
#endif
|
|
|
|
|
2015-07-13 18:41:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_find_answer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if we already have the resolved hostname address in the cache.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* hostname - The hostname string to be resolved.
|
2018-11-23 14:09:47 +01:00
|
|
|
* addr - The location to return the IP addresses associated with the
|
|
|
|
* hostname.
|
|
|
|
* naddr - On entry, the count of addresses backing up the 'addr'
|
|
|
|
* pointer. On return, this location will hold the actual count of
|
|
|
|
* the returned addresses.
|
2015-07-13 18:41:32 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* If the host name was successfully found in the DNS name resolution
|
|
|
|
* cache, zero (OK) will be returned. Otherwise, some negated errno
|
|
|
|
* value will be returned, typically -ENOENT meaning that the hostname
|
|
|
|
* was not found in the cache.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
2018-11-23 14:09:47 +01:00
|
|
|
int dns_find_answer(FAR const char *hostname, FAR union dns_addr_u *addr,
|
|
|
|
FAR int *naddr);
|
2015-07-13 18:41:32 +02:00
|
|
|
#endif
|
|
|
|
|
2019-03-19 17:02:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_notify_nameserver
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 14:49:28 +02:00
|
|
|
void dns_notify_nameserver(FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen);
|
2019-03-19 17:02:10 +01:00
|
|
|
|
2016-01-14 15:32:20 +01:00
|
|
|
#undef EXTERN
|
2015-07-12 17:28:03 +02:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-06-30 23:12:34 +02:00
|
|
|
#endif /* __LIBS_LIBC_NETDB_LIB_DNS_H */
|