2015-07-08 21:40:52 +02:00
|
|
|
/****************************************************************************
|
2015-07-10 20:11:40 +02:00
|
|
|
* libc/netdb/lib_gethostbynamer.c
|
2015-07-08 21:40:52 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2015 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>
|
|
|
|
|
|
|
|
#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-07-10 21:30:09 +02:00
|
|
|
|
2015-07-08 21:40:52 +02:00
|
|
|
#include "lib_internal.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
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is the layout of the caller provided memory area */
|
|
|
|
|
|
|
|
struct hostent_info_s
|
|
|
|
{
|
|
|
|
FAR char *hi_addrlist[2];
|
|
|
|
char hi_data[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Input paramters:
|
|
|
|
* stream - File stream of the opened hosts file with the file pointer
|
|
|
|
* positioned at the beginning of the next host entry.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lib_numeric_address(FAR const char *name, FAR struct hostent *host,
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (buflen <= sizeof(struct hostent_info_s))
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
host->h_addrtype = AF_INET6;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
host->h_addrtype = AF_INET;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No colon? No period? Can't be a numeric address */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->hi_addrlist[0] = ptr;
|
|
|
|
host->h_addr_list = info->hi_addrlist;
|
|
|
|
host->h_length = addrlen;
|
|
|
|
|
|
|
|
ptr += addrlen;
|
|
|
|
buflen -= addrlen;
|
|
|
|
|
|
|
|
/* And copy name */
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
if (addrlen + namelen + 1 > buflen)
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(ptr, name, buflen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int addrtype;
|
|
|
|
int namelen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that we have a buffer big enough to get started (it still may not
|
|
|
|
* be big enough).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (buflen <= sizeof(struct hostent_info_s))
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize buffers */
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
/* Try to get the host address using the DNS name server */
|
|
|
|
|
|
|
|
addrlen = buflen;
|
|
|
|
ret = dns_find_answer(name, (FAR struct sockaddr *)ptr, &addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* No, nothing found in the cache */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the address type; verify the address size. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (((FAR struct sockaddr_in *)ptr)->sin_family == AF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(addrlen == sizeof(struct sockaddr_in));
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
addrtype = AF_INET;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(addrlen == sizeof(struct sockaddr_in6));
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
addrtype = AF_INET6;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Yes.. Return the address that we obtained from the DNS cache. */
|
|
|
|
|
|
|
|
info->hi_addrlist[0] = ptr;
|
|
|
|
host->h_addr_list = info->hi_addrlist;
|
|
|
|
host->h_addrtype = addrtype;
|
|
|
|
host->h_length = addrlen;
|
|
|
|
|
|
|
|
ptr += addrlen;
|
|
|
|
buflen -= addrlen;
|
|
|
|
|
|
|
|
/* And copy the host name */
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
if (addrlen + namelen + 1 > buflen)
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(ptr, name, buflen);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|
|
|
|
|
2015-07-12 17:19:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_dns_query
|
|
|
|
*
|
|
|
|
* Descriptions:
|
|
|
|
* 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,
|
|
|
|
FAR struct sockaddr *addr, socklen_t *addrlen)
|
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 */
|
|
|
|
|
2015-07-12 21:45:52 +02:00
|
|
|
ret = dns_query(sd, hostname, addr, addrlen);
|
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-07-12 19:56:53 +02:00
|
|
|
socklen_t addrlen;
|
2015-07-10 21:30:09 +02:00
|
|
|
int addrtype;
|
|
|
|
int namelen;
|
|
|
|
int ret;
|
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-07-10 21:30:09 +02:00
|
|
|
if (buflen <= sizeof(struct hostent_info_s))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
memset(host, 0, sizeof(struct hostent));
|
|
|
|
memset(info, 0, sizeof(struct hostent_info_s));
|
|
|
|
|
|
|
|
/* Try to get the host address using the DNS name server */
|
|
|
|
|
2015-07-12 21:45:52 +02:00
|
|
|
addrlen = buflen;
|
|
|
|
ret = lib_dns_query(name, (FAR struct sockaddr *)ptr, &addrlen);
|
2015-07-10 21:30:09 +02:00
|
|
|
|
|
|
|
/* Was the DNS lookup successful? */
|
|
|
|
|
|
|
|
if (ret >= 0)
|
2015-07-08 21:40:52 +02:00
|
|
|
{
|
2015-07-12 21:45:52 +02:00
|
|
|
/* Get the address type; verify the address size. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (((FAR struct sockaddr_in *)ptr)->sin_family == AF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(addrlen == sizeof(struct sockaddr_in));
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
addrtype = AF_INET;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(addrlen == sizeof(struct sockaddr_in6));
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
addrtype = AF_INET6;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
/* Yes.. Return the address that we obtained from the DNS name server. */
|
|
|
|
|
|
|
|
info->hi_addrlist[0] = ptr;
|
|
|
|
host->h_addr_list = info->hi_addrlist;
|
|
|
|
host->h_addrtype = addrtype;
|
|
|
|
host->h_length = addrlen;
|
|
|
|
|
|
|
|
ptr += addrlen;
|
|
|
|
buflen -= addrlen;
|
|
|
|
|
|
|
|
/* And copy name */
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
if (addrlen + namelen + 1 > buflen)
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
2015-07-08 21:40:52 +02:00
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
strncpy(ptr, name, buflen);
|
2015-07-08 21:40:52 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:30:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
*
|
|
|
|
* Input paramters:
|
|
|
|
* 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_HOSTFILE
|
|
|
|
static int lib_hostfile_lookup(FAR const char *name, FAR struct hostent *host,
|
|
|
|
FAR char *buf, size_t buflen, int *h_errnop)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
|
|
|
ndbg("ERROR: Failed to open the hosts file %s: %d\n",
|
|
|
|
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 */
|
|
|
|
|
|
|
|
nvdbg("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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
2015-07-10 20:11:40 +02:00
|
|
|
#endif /* CONFIG_NETDB_HOSTFILE */
|
2015-07-10 21:30:09 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gethostname_r
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* gethostname_r() is *not* POSIX but is similar to a Glibc extension and is
|
|
|
|
* used internally by NuttX to implement the POSIX gethostname().
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
{
|
2015-07-10 22:53:30 +02:00
|
|
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
2015-07-10 21:30:09 +02:00
|
|
|
int ret;
|
2015-07-10 22:53:30 +02:00
|
|
|
#endif
|
2015-07-10 21:30:09 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to find the name in the HOSTALIASES environment variable */
|
|
|
|
/* REVISIT: Not implemented */
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
ret = lib_find_answer(name, host, buf, buflen);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
ret = lib_dns_lookup(name, host, buf, buflen);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIBC_NETDB */
|