2015-07-08 21:40:52 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/netdb/lib_gethostbynamer.c
|
2015-07-08 21:40:52 +02:00
|
|
|
*
|
2018-07-25 14:32:50 +02:00
|
|
|
* Copyright (C) 2015, 2018 Gregory Nutt. All rights reserved.
|
2015-07-08 21:40:52 +02:00
|
|
|
* 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/types.h>
|
2015-07-12 19:56:53 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2015-07-08 21:40:52 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2015-07-12 21:45:52 +02:00
|
|
|
#include <assert.h>
|
2015-07-08 21:40:52 +02:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2015-07-12 17:19:40 +02:00
|
|
|
#include <nuttx/net/dns.h>
|
2015-08-24 19:29:54 +02:00
|
|
|
#include <nuttx/net/loopback.h>
|
2015-07-10 21:30:09 +02:00
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2015-07-12 17:19:40 +02:00
|
|
|
#include "netdb/lib_dns.h"
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
2015-07-08 21:40:52 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is the layout of the caller provided memory area */
|
|
|
|
|
|
|
|
struct hostent_info_s
|
|
|
|
{
|
2020-03-30 09:45:08 +02:00
|
|
|
int hi_addrtypes[CONFIG_NETDB_MAX_IPADDR + 1];
|
|
|
|
int hi_lengths[CONFIG_NETDB_MAX_IPADDR + 1];
|
2020-03-29 17:15:18 +02:00
|
|
|
FAR char *hi_addrlist[CONFIG_NETDB_MAX_IPADDR + 1];
|
2020-03-30 09:45:08 +02:00
|
|
|
char hi_data[1];
|
2015-07-08 21:40:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_numeric_address
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the name is a numeric IP address. In this case, simply copy
|
|
|
|
* name into the h_name field and its struct in_addr equivalent into the
|
|
|
|
* h_addr_list[0] field of the returned hostent structure.
|
|
|
|
*
|
2015-08-24 18:08:26 +02:00
|
|
|
* Input Parameters:
|
2020-03-30 05:28:13 +02:00
|
|
|
* name - The name of the host to find.
|
2015-07-08 21:40:52 +02:00
|
|
|
* host - Caller provided location to return the host data.
|
|
|
|
* buf - Caller provided buffer to hold string data associated with the
|
|
|
|
* host data.
|
|
|
|
* buflen - The size of the caller-provided buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (0) is returned if the name is an numeric IP address.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 14:49:28 +02:00
|
|
|
static int lib_numeric_address(FAR const char *name,
|
|
|
|
FAR struct hostent *host,
|
2015-07-08 21:40:52 +02:00
|
|
|
FAR char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct hostent_info_s *info;
|
|
|
|
FAR char *ptr;
|
2015-07-12 19:56:53 +02:00
|
|
|
socklen_t addrlen;
|
2015-07-08 21:40:52 +02:00
|
|
|
int namelen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that we have a buffer big enough to get started (it still may not
|
|
|
|
* be big enough).
|
|
|
|
*/
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
if (buflen <= sizeof(struct hostent_info_s))
|
2020-03-30 14:49:28 +02:00
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
2015-07-08 21:40:52 +02:00
|
|
|
|
|
|
|
info = (FAR struct hostent_info_s *)buf;
|
|
|
|
ptr = info->hi_data;
|
|
|
|
buflen -= (sizeof(struct hostent_info_s) - 1);
|
|
|
|
|
|
|
|
memset(host, 0, sizeof(struct hostent));
|
|
|
|
memset(info, 0, sizeof(struct hostent_info_s));
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
host->h_addrtypes = info->hi_addrtypes;
|
|
|
|
host->h_lengths = info->hi_lengths;
|
|
|
|
host->h_addr_list = info->hi_addrlist;
|
|
|
|
|
2015-07-17 16:24:21 +02:00
|
|
|
/* If the address contains a colon, then it might be a numeric IPv6
|
|
|
|
* address
|
|
|
|
*/
|
2015-07-08 21:40:52 +02:00
|
|
|
|
|
|
|
if (strchr(name, ':') != NULL)
|
|
|
|
{
|
|
|
|
/* Make sure that space remains to hold the IPv6 address */
|
|
|
|
|
|
|
|
addrlen = sizeof(struct in6_addr);
|
|
|
|
if (buflen < addrlen)
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
2015-07-08 21:42:36 +02:00
|
|
|
|
2015-07-08 21:40:52 +02:00
|
|
|
ret = inet_pton(AF_INET6, name, ptr);
|
2015-07-17 16:24:21 +02:00
|
|
|
|
|
|
|
/* The inet_pton() function returns 1 if the conversion succeeds. It
|
|
|
|
* will return 0 if the input is not a valid IP address string, or -1
|
|
|
|
* if the address family argument is unsupported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 1)
|
2015-07-08 21:40:52 +02:00
|
|
|
{
|
|
|
|
/* Conversion failed. Must not be a IPv6 address */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-30 05:28:13 +02:00
|
|
|
host->h_addrtype = AF_INET6;
|
2015-07-08 21:40:52 +02:00
|
|
|
}
|
2015-07-17 16:24:21 +02:00
|
|
|
|
|
|
|
/* If the address contains a colon, then it might be a numeric IPv6
|
|
|
|
* address.
|
|
|
|
*/
|
2015-07-08 21:40:52 +02:00
|
|
|
|
|
|
|
else if (strchr(name, '.') != NULL)
|
|
|
|
{
|
|
|
|
/* Make sure that space remains to hold the IPv4 address */
|
|
|
|
|
|
|
|
addrlen = sizeof(struct in_addr);
|
|
|
|
if (buflen < addrlen)
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET, name, ptr);
|
2015-07-17 16:24:21 +02:00
|
|
|
|
|
|
|
/* The inet_pton() function returns 1 if the conversion succeeds. It
|
|
|
|
* will return 0 if the input is not a valid IP address string, or -1
|
|
|
|
* if the address family argument is unsupported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 1)
|
2015-07-08 21:40:52 +02:00
|
|
|
{
|
|
|
|
/* Conversion failed. Must not be an IPv4 address */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-30 05:28:13 +02:00
|
|
|
host->h_addrtype = AF_INET;
|
2015-07-08 21:40:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No colon? No period? Can't be a numeric address */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
host->h_addr = ptr;
|
|
|
|
host->h_length = addrlen;
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
ptr += addrlen;
|
|
|
|
buflen -= addrlen;
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2017-06-12 14:47:43 +02:00
|
|
|
/* And copy name */
|
2015-07-08 21:40:52 +02:00
|
|
|
|
|
|
|
namelen = strlen(name);
|
2017-06-12 14:47:43 +02:00
|
|
|
if ((namelen + 1) > buflen)
|
2015-07-08 21:40:52 +02:00
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(ptr, name, buflen);
|
2017-05-25 02:22:34 +02:00
|
|
|
|
|
|
|
/* Set the address to h_name */
|
|
|
|
|
|
|
|
host->h_name = ptr;
|
2015-07-08 21:40:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-24 18:08:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_localhost
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the name is the reserved name for the local loopback device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-03-30 05:28:13 +02:00
|
|
|
* name - The name of the host to find.
|
2015-08-24 18:08:26 +02:00
|
|
|
* host - Caller provided location to return the host data.
|
|
|
|
* buf - Caller provided buffer to hold string data associated with the
|
|
|
|
* host data.
|
|
|
|
* buflen - The size of the caller-provided buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-03-30 05:28:13 +02:00
|
|
|
* Zero (0) is returned if the name is the loopback device.
|
2015-08-24 18:08:26 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-24 21:49:12 +02:00
|
|
|
#ifdef CONFIG_NET_LOOPBACK
|
2015-08-24 18:08:26 +02:00
|
|
|
static int lib_localhost(FAR const char *name, FAR struct hostent *host,
|
|
|
|
FAR char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct hostent_info_s *info;
|
2015-08-24 19:29:54 +02:00
|
|
|
FAR char *dest;
|
2015-08-24 18:08:26 +02:00
|
|
|
int namelen;
|
2020-03-30 09:45:08 +02:00
|
|
|
int i = 0;
|
2015-08-24 18:08:26 +02:00
|
|
|
|
2020-03-29 15:43:44 +02:00
|
|
|
if (strcmp(name, g_lo_hostname) == 0)
|
2015-08-24 18:08:26 +02:00
|
|
|
{
|
|
|
|
/* Yes.. it is the localhost */
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
/* Make sure that space remains to hold the hostent structure */
|
2015-08-24 18:08:26 +02:00
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
if (buflen <= sizeof(struct hostent_info_s))
|
2015-08-24 18:08:26 +02:00
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
info = (FAR struct hostent_info_s *)buf;
|
|
|
|
dest = info->hi_data;
|
|
|
|
buflen -= (sizeof(struct hostent_info_s) - 1);
|
2015-08-24 18:08:26 +02:00
|
|
|
|
|
|
|
memset(host, 0, sizeof(struct hostent));
|
|
|
|
memset(info, 0, sizeof(struct hostent_info_s));
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
host->h_addrtypes = info->hi_addrtypes;
|
|
|
|
host->h_lengths = info->hi_lengths;
|
|
|
|
host->h_addr_list = info->hi_addrlist;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
/* Save the IPv4 address */
|
|
|
|
|
|
|
|
info->hi_addrtypes[i] = AF_INET;
|
|
|
|
info->hi_lengths[i] = sizeof(struct in_addr);
|
|
|
|
info->hi_addrlist[i] = (FAR char *)&g_lo_ipv4addr;
|
|
|
|
i++;
|
|
|
|
#endif
|
2015-08-24 18:08:26 +02:00
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
/* Save the IPv6 address */
|
|
|
|
|
|
|
|
info->hi_addrtypes[i] = AF_INET6;
|
|
|
|
info->hi_lengths[i] = sizeof(struct in6_addr);
|
|
|
|
info->hi_addrlist[i] = (FAR char *)&g_lo_ipv6addr;
|
|
|
|
i++;
|
|
|
|
#endif
|
2015-08-24 18:08:26 +02:00
|
|
|
|
2017-06-12 14:47:43 +02:00
|
|
|
/* And copy name */
|
2015-08-24 18:08:26 +02:00
|
|
|
|
|
|
|
namelen = strlen(name);
|
2017-06-12 14:47:43 +02:00
|
|
|
if ((namelen + 1) > buflen)
|
2015-08-24 18:08:26 +02:00
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2015-08-24 19:29:54 +02:00
|
|
|
strncpy(dest, name, buflen);
|
2017-05-25 02:22:34 +02:00
|
|
|
|
|
|
|
/* Set the address to h_name */
|
|
|
|
|
|
|
|
host->h_name = dest;
|
2015-08-24 18:08:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-13 18:41:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_find_answer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if we previously resolved this hostname and if that resolved
|
|
|
|
* address is already available in the DNS cache.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - The name of the host to find.
|
|
|
|
* host - Caller provided location to return the host data.
|
|
|
|
* buf - Caller provided buffer to hold string data associated with the
|
|
|
|
* host data.
|
|
|
|
* buflen - The size of the caller-provided buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (0) is returned if the DNS lookup was successful.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
2018-11-14 13:30:49 +01:00
|
|
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
2015-07-13 18:41:32 +02:00
|
|
|
static int lib_find_answer(FAR const char *name, FAR struct hostent *host,
|
|
|
|
FAR char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct hostent_info_s *info;
|
|
|
|
FAR char *ptr;
|
2015-08-22 02:33:03 +02:00
|
|
|
FAR void *addrdata;
|
2015-07-13 18:41:32 +02:00
|
|
|
socklen_t addrlen;
|
2018-11-23 14:09:47 +01:00
|
|
|
int naddr;
|
2015-07-13 18:41:32 +02:00
|
|
|
int addrtype;
|
|
|
|
int namelen;
|
|
|
|
int ret;
|
2018-11-23 14:09:47 +01:00
|
|
|
int i;
|
2015-07-13 18:41:32 +02:00
|
|
|
|
|
|
|
/* Verify that we have a buffer big enough to get started (it still may not
|
|
|
|
* be big enough).
|
|
|
|
*/
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
if (buflen <= sizeof(struct hostent_info_s))
|
2020-03-30 14:49:28 +02:00
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
2015-07-13 18:41:32 +02:00
|
|
|
|
|
|
|
/* Initialize buffers */
|
|
|
|
|
|
|
|
info = (FAR struct hostent_info_s *)buf;
|
|
|
|
ptr = info->hi_data;
|
|
|
|
buflen -= (sizeof(struct hostent_info_s) - 1);
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
/* Verify again that there is space for at least one address. */
|
|
|
|
|
|
|
|
if (buflen < sizeof(union dns_addr_u))
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:41:32 +02:00
|
|
|
memset(host, 0, sizeof(struct hostent));
|
|
|
|
memset(info, 0, sizeof(struct hostent_info_s));
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
host->h_addrtypes = info->hi_addrtypes;
|
|
|
|
host->h_lengths = info->hi_lengths;
|
|
|
|
host->h_addr_list = info->hi_addrlist;
|
|
|
|
|
2015-07-13 18:41:32 +02:00
|
|
|
/* Try to get the host address using the DNS name server */
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
naddr = buflen / sizeof(union dns_addr_u);
|
|
|
|
ret = dns_find_answer(name, (FAR union dns_addr_u *)ptr, &naddr);
|
2015-07-13 18:41:32 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* No, nothing found in the cache */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-29 17:15:18 +02:00
|
|
|
DEBUGASSERT(naddr <= CONFIG_NETDB_MAX_IPADDR);
|
2015-07-13 18:41:32 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
/* Get the address type. */
|
|
|
|
|
|
|
|
for (i = 0; i < naddr; i++)
|
|
|
|
{
|
2015-07-13 18:41:32 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2018-11-23 14:09:47 +01:00
|
|
|
if (((FAR struct sockaddr_in *)ptr)->sin_family == AF_INET)
|
2015-07-13 18:41:32 +02:00
|
|
|
#endif
|
2018-11-23 14:09:47 +01:00
|
|
|
{
|
2020-03-30 05:28:13 +02:00
|
|
|
addrlen = sizeof(struct in_addr);
|
2018-11-23 14:09:47 +01:00
|
|
|
addrtype = AF_INET;
|
|
|
|
addrdata = &((FAR struct sockaddr_in *)ptr)->sin_addr;
|
|
|
|
}
|
2015-07-13 18:41:32 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2018-11-23 14:09:47 +01:00
|
|
|
else
|
2015-07-13 18:41:32 +02:00
|
|
|
#endif
|
2018-11-23 14:09:47 +01:00
|
|
|
{
|
2020-03-30 05:28:13 +02:00
|
|
|
addrlen = sizeof(struct in6_addr);
|
2018-11-23 14:09:47 +01:00
|
|
|
addrtype = AF_INET6;
|
|
|
|
addrdata = &((FAR struct sockaddr_in6 *)ptr)->sin6_addr;
|
|
|
|
}
|
2015-07-13 18:41:32 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
info->hi_addrtypes[i] = addrtype;
|
|
|
|
info->hi_lengths[i] = addrlen;
|
|
|
|
info->hi_addrlist[i] = addrdata;
|
2015-07-13 18:41:32 +02:00
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
ptr += sizeof(union dns_addr_u);
|
|
|
|
buflen -= sizeof(union dns_addr_u);
|
2018-11-23 14:09:47 +01:00
|
|
|
}
|
|
|
|
|
2017-06-12 14:47:43 +02:00
|
|
|
/* And copy name */
|
2015-07-13 18:41:32 +02:00
|
|
|
|
|
|
|
namelen = strlen(name);
|
2017-06-12 14:47:43 +02:00
|
|
|
if ((namelen + 1) > buflen)
|
2015-07-13 18:41:32 +02:00
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(ptr, name, buflen);
|
2017-05-25 02:22:34 +02:00
|
|
|
|
|
|
|
/* Set the address to h_name */
|
|
|
|
|
|
|
|
host->h_name = ptr;
|
2015-07-13 18:41:32 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2018-11-14 13:30:49 +01:00
|
|
|
#endif
|
2015-07-13 18:41:32 +02:00
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|
|
|
|
|
2015-07-12 17:19:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_dns_query
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2015-07-12 17:19:40 +02:00
|
|
|
* Combines the operations of dns_bind(), dns_query(), and dns_free() to
|
|
|
|
* obtain the IP address ('ipaddr') associated with the 'hostname' in one
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-07-19 22:45:34 +02:00
|
|
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
2015-07-12 21:45:52 +02:00
|
|
|
static int lib_dns_query(FAR const char *hostname,
|
2018-11-23 14:09:47 +01:00
|
|
|
FAR union dns_addr_u *addr, int *naddr)
|
2015-07-12 17:19:40 +02:00
|
|
|
{
|
|
|
|
int sd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create and bind a socket to the DNS server */
|
|
|
|
|
|
|
|
sd = dns_bind();
|
|
|
|
if (sd < 0)
|
|
|
|
{
|
2015-07-20 00:35:25 +02:00
|
|
|
return sd;
|
2015-07-12 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the query to get the IP address */
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
ret = dns_query(sd, hostname, addr, naddr);
|
2015-07-12 17:19:40 +02:00
|
|
|
|
|
|
|
/* Release the socket */
|
|
|
|
|
|
|
|
close(sd);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-07-19 22:45:34 +02:00
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|
2015-07-12 17:19:40 +02:00
|
|
|
|
2015-07-08 21:40:52 +02:00
|
|
|
/****************************************************************************
|
2015-07-10 21:30:09 +02:00
|
|
|
* Name: lib_dns_lookup
|
2015-07-08 21:40:52 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-07-10 21:30:09 +02:00
|
|
|
* Try to look-up the host name from the DNS server
|
2015-07-08 21:40:52 +02:00
|
|
|
*
|
2015-07-12 21:45:52 +02:00
|
|
|
* Input Parameters:
|
2015-07-08 21:40:52 +02:00
|
|
|
* name - The name of the host to find.
|
|
|
|
* host - Caller provided location to return the host data.
|
|
|
|
* buf - Caller provided buffer to hold string data associated with the
|
|
|
|
* host data.
|
|
|
|
* buflen - The size of the caller-provided buffer
|
2015-07-08 21:42:36 +02:00
|
|
|
*
|
2015-07-08 21:40:52 +02:00
|
|
|
* Returned Value:
|
2015-07-10 21:30:09 +02:00
|
|
|
* Zero (0) is returned if the DNS lookup was successful.
|
2015-07-08 21:40:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
|
|
|
static int lib_dns_lookup(FAR const char *name, FAR struct hostent *host,
|
|
|
|
FAR char *buf, size_t buflen)
|
2015-07-08 21:40:52 +02:00
|
|
|
{
|
2015-07-10 21:30:09 +02:00
|
|
|
FAR struct hostent_info_s *info;
|
|
|
|
FAR char *ptr;
|
2015-08-22 02:33:03 +02:00
|
|
|
FAR void *addrdata;
|
2015-07-12 19:56:53 +02:00
|
|
|
socklen_t addrlen;
|
2018-11-23 14:09:47 +01:00
|
|
|
int naddr;
|
2015-07-10 21:30:09 +02:00
|
|
|
int addrtype;
|
|
|
|
int namelen;
|
|
|
|
int ret;
|
2018-11-23 14:09:47 +01:00
|
|
|
int i;
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
/* Verify that we have a buffer big enough to get started (it still may not
|
|
|
|
* be big enough).
|
|
|
|
*/
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
if (buflen <= sizeof(struct hostent_info_s))
|
2020-03-30 14:49:28 +02:00
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
/* Initialize buffers */
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
info = (FAR struct hostent_info_s *)buf;
|
|
|
|
ptr = info->hi_data;
|
|
|
|
buflen -= (sizeof(struct hostent_info_s) - 1);
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
/* Verify again that there is space for at least one address. */
|
|
|
|
|
|
|
|
if (buflen < sizeof(union dns_addr_u))
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
memset(host, 0, sizeof(struct hostent));
|
|
|
|
memset(info, 0, sizeof(struct hostent_info_s));
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
host->h_addrtypes = info->hi_addrtypes;
|
|
|
|
host->h_lengths = info->hi_lengths;
|
|
|
|
host->h_addr_list = info->hi_addrlist;
|
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
/* Try to get the host address using the DNS name server */
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
naddr = buflen / sizeof(union dns_addr_u);
|
|
|
|
ret = lib_dns_query(name, (FAR union dns_addr_u *)ptr, &naddr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-07-10 21:30:09 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
/* We can read more than maximum, limit here. */
|
2015-07-10 21:30:09 +02:00
|
|
|
|
2020-03-29 17:15:18 +02:00
|
|
|
naddr = MIN(naddr, CONFIG_NETDB_MAX_IPADDR);
|
2015-07-12 21:45:52 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
for (i = 0; i < naddr; i++)
|
|
|
|
{
|
2015-07-12 21:45:52 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (((FAR struct sockaddr_in *)ptr)->sin_family == AF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
2020-03-30 05:28:13 +02:00
|
|
|
addrlen = sizeof(struct in_addr);
|
2015-07-12 21:45:52 +02:00
|
|
|
addrtype = AF_INET;
|
2015-08-22 02:33:03 +02:00
|
|
|
addrdata = &((FAR struct sockaddr_in *)ptr)->sin_addr;
|
2015-07-12 21:45:52 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-03-30 05:28:13 +02:00
|
|
|
addrlen = sizeof(struct in6_addr);
|
2015-07-12 21:45:52 +02:00
|
|
|
addrtype = AF_INET6;
|
2016-02-25 20:59:42 +01:00
|
|
|
addrdata = &((FAR struct sockaddr_in6 *)ptr)->sin6_addr;
|
2015-07-12 21:45:52 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
info->hi_addrtypes[i] = addrtype;
|
|
|
|
info->hi_lengths[i] = addrlen;
|
|
|
|
info->hi_addrlist[i] = addrdata;
|
2015-07-10 21:30:09 +02:00
|
|
|
|
2020-03-30 09:45:08 +02:00
|
|
|
ptr += sizeof(union dns_addr_u);
|
|
|
|
buflen -= sizeof(union dns_addr_u);
|
2018-11-23 14:09:47 +01:00
|
|
|
}
|
2015-07-10 21:30:09 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
/* And copy name */
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
if ((namelen + 1) > buflen)
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
strncpy(ptr, name, buflen);
|
2017-05-25 02:22:34 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
/* Set the address to h_name */
|
2017-05-25 02:22:34 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
host->h_name = ptr;
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
return OK;
|
2015-07-10 21:30:09 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_hostfile_lookup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Try to look-up the host name from the network host file
|
|
|
|
*
|
2015-08-24 18:08:26 +02:00
|
|
|
* Input Parameters:
|
2015-07-10 21:30:09 +02:00
|
|
|
* name - The name of the host to find.
|
|
|
|
* host - Caller provided location to return the host data.
|
|
|
|
* buf - Caller provided buffer to hold string data associated with the
|
|
|
|
* host data.
|
|
|
|
* buflen - The size of the caller-provided buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-03-30 05:28:13 +02:00
|
|
|
* Zero (0) is returned if the host file lookup was successful.
|
2015-07-10 21:30:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_HOSTFILE
|
2020-03-30 14:49:28 +02:00
|
|
|
static int lib_hostfile_lookup(FAR const char *name,
|
|
|
|
FAR struct hostent *host,
|
|
|
|
FAR char *buf, size_t buflen,
|
|
|
|
FAR int *h_errnop)
|
2015-07-10 21:30:09 +02:00
|
|
|
{
|
|
|
|
FAR FILE *stream;
|
|
|
|
int herrnocode;
|
|
|
|
int nread;
|
2015-07-08 21:40:52 +02:00
|
|
|
|
|
|
|
/* Search the hosts file for a match */
|
|
|
|
|
|
|
|
stream = fopen(CONFIG_NETDB_HOSTCONF_PATH, "r");
|
|
|
|
if (stream == NULL)
|
|
|
|
{
|
2020-03-28 18:08:10 +01:00
|
|
|
int errcode = -errno;
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to open the hosts file %s: %d\n",
|
2015-07-08 21:40:52 +02:00
|
|
|
CONFIG_NETDB_HOSTCONF_PATH, errcode);
|
|
|
|
UNUSED(errcode);
|
|
|
|
|
|
|
|
herrnocode = NO_RECOVERY;
|
|
|
|
goto errorout_with_herrnocode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop reading entries from the hosts file until a match is found or
|
|
|
|
* until we hit the end-of-file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Read the next entry from the hosts file */
|
|
|
|
|
|
|
|
nread = lib_parse_hostfile(stream, host, buf, buflen);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
/* Possible errors:
|
|
|
|
* ERANGE - Buffer not big enough
|
|
|
|
* ESPIPE - End of file (or possibly a read error).
|
|
|
|
* EAGAIN - Error parsing the line (E.g., missing hostname)
|
|
|
|
*/
|
|
|
|
|
2015-07-08 22:33:23 +02:00
|
|
|
if (nread == -ESPIPE)
|
2015-07-08 21:40:52 +02:00
|
|
|
{
|
|
|
|
nread = 0;
|
|
|
|
}
|
2015-07-08 22:33:23 +02:00
|
|
|
else if (nread != -EAGAIN)
|
2015-07-08 21:40:52 +02:00
|
|
|
{
|
|
|
|
herrnocode = NO_RECOVERY;
|
|
|
|
goto errorout_with_stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nread > 0)
|
|
|
|
{
|
|
|
|
/* We successfully read the entry */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
ninfo("Comparing %s to %s\n", name, host->h_name);
|
2015-07-09 17:14:42 +02:00
|
|
|
|
|
|
|
/* Check for a host name match */
|
|
|
|
|
2015-07-08 21:40:52 +02:00
|
|
|
if (strcmp(name, host->h_name) == 0)
|
|
|
|
{
|
|
|
|
/* We have a match */
|
|
|
|
|
|
|
|
fclose(stream);
|
|
|
|
return OK;
|
|
|
|
}
|
2015-07-09 17:14:42 +02:00
|
|
|
|
|
|
|
/* For a match with any host alias */
|
|
|
|
|
|
|
|
if (host->h_aliases != NULL)
|
|
|
|
{
|
|
|
|
FAR char **alias;
|
|
|
|
|
|
|
|
for (alias = host->h_aliases; *alias != NULL; alias++)
|
|
|
|
{
|
|
|
|
/* Check for a host alias match */
|
|
|
|
|
|
|
|
if (strcmp(name, *alias) == 0)
|
|
|
|
{
|
|
|
|
/* We have a match */
|
|
|
|
|
|
|
|
fclose(stream);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-08 21:40:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nread != 0);
|
|
|
|
|
|
|
|
/* We get here when the end of the hosts file is encountered without
|
|
|
|
* finding the hostname.
|
|
|
|
*/
|
|
|
|
|
|
|
|
herrnocode = HOST_NOT_FOUND;
|
|
|
|
|
|
|
|
errorout_with_stream:
|
|
|
|
fclose(stream);
|
|
|
|
|
|
|
|
errorout_with_herrnocode:
|
|
|
|
if (h_errnop)
|
|
|
|
{
|
|
|
|
*h_errnop = herrnocode;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
return ERROR;
|
2015-07-08 21:40:52 +02:00
|
|
|
}
|
2015-07-10 20:11:40 +02:00
|
|
|
#endif /* CONFIG_NETDB_HOSTFILE */
|
2015-07-10 21:30:09 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-06-08 15:07:21 +02:00
|
|
|
* Name: gethostbyname_r
|
2015-07-10 21:30:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The gethostbyname_r() function returns a structure of type hostent for
|
|
|
|
* the given host name. Here name is either a hostname, or an IPv4 address
|
|
|
|
* in standard dot notation (as for inet_addr(3)), or an IPv6 address in
|
|
|
|
* colon (and possibly dot) notation.
|
|
|
|
*
|
|
|
|
* If name is an IPv4 or IPv6 address, no lookup is performed and
|
|
|
|
* gethostbyname_r() simply copies name into the h_name field
|
|
|
|
* and its struct in_addr equivalent into the h_addr_list[0] field of the
|
|
|
|
* returned hostent structure.
|
|
|
|
*
|
2020-03-30 14:49:28 +02:00
|
|
|
* gethostname_r() is *not* POSIX but is similar to a Glibc extension and
|
|
|
|
* is used internally by NuttX to implement the POSIX gethostname().
|
2015-07-10 21:30:09 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - The name of the host to find.
|
|
|
|
* host - Caller provided location to return the host data.
|
|
|
|
* buf - Caller provided buffer to hold string data associated with the
|
|
|
|
* host data.
|
|
|
|
* buflen - The size of the caller-provided buffer
|
|
|
|
* h_errnop - There h_errno value returned in the event of a failure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success, -1 (ERROR) is returned on a failure
|
|
|
|
* with the returned h_errno value provided the reason for the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int gethostbyname_r(FAR const char *name, FAR struct hostent *host,
|
|
|
|
FAR char *buf, size_t buflen, int *h_errnop)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(name != NULL && host != NULL && buf != NULL);
|
|
|
|
|
|
|
|
/* Make sure that the h_errno has a non-error code */
|
|
|
|
|
|
|
|
if (h_errnop)
|
|
|
|
{
|
|
|
|
*h_errnop = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a numeric hostname */
|
|
|
|
|
|
|
|
if (lib_numeric_address(name, host, buf, buflen) == 0)
|
|
|
|
{
|
|
|
|
/* Yes.. we are done */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-08-24 21:49:12 +02:00
|
|
|
#ifdef CONFIG_NET_LOOPBACK
|
2015-08-24 18:08:26 +02:00
|
|
|
/* Check for the local loopback host name */
|
|
|
|
|
|
|
|
if (lib_localhost(name, host, buf, buflen) == 0)
|
|
|
|
{
|
|
|
|
/* Yes.. we are done */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
/* Try to find the name in the HOSTALIASES environment variable */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
2015-07-13 18:41:32 +02:00
|
|
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
|
|
|
/* Check if we already have this hostname mapping cached */
|
|
|
|
|
2020-03-30 05:28:13 +02:00
|
|
|
if (lib_find_answer(name, host, buf, buflen) >= 0)
|
2015-07-13 18:41:32 +02:00
|
|
|
{
|
|
|
|
/* Found the address mapping in the cache */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
/* Try to get the host address using the DNS name server */
|
|
|
|
|
2020-03-30 05:28:13 +02:00
|
|
|
if (lib_dns_lookup(name, host, buf, buflen) >= 0)
|
2015-07-10 21:30:09 +02:00
|
|
|
{
|
|
|
|
/* Successful DNS lookup! */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDB_HOSTFILE
|
|
|
|
/* Search the hosts file for a match */
|
|
|
|
|
|
|
|
return lib_hostfile_lookup(name, host, buf, buflen, h_errnop);
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* The host file file is not supported. The host name mapping was not
|
|
|
|
* found from any lookup heuristic
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (h_errnop)
|
|
|
|
{
|
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
return ERROR;
|
2015-07-10 21:30:09 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIBC_NETDB */
|