diff --git a/ChangeLog b/ChangeLog index 9ccc57e55b1..baba8eb3409 100755 --- a/ChangeLog +++ b/ChangeLog @@ -10666,4 +10666,7 @@ * arch/sim/src/up_head.S: Implement board_power_off() for the simulation platform (2015-07-04). * libc/unistd/lib_gethostname.c: Add support for sethostname() (2015-07-05). + * libc/net: Add support for gethostbyname() and gethostbyaddr(). Also + support included for the non-standard gethostbyname_r() and + gethostbyaddr_r() (2015-07-08). diff --git a/Documentation b/Documentation index ab86088d3f2..7443c705201 160000 --- a/Documentation +++ b/Documentation @@ -1 +1 @@ -Subproject commit ab86088d3f24f838bffc759612787b93baf336eb +Subproject commit 7443c70520160a7fa7374d7bdcd23f25af2a498f diff --git a/include/netdb.h b/include/netdb.h index 0274281e407..adc3c1f2fb1 100644 --- a/include/netdb.h +++ b/include/netdb.h @@ -276,10 +276,11 @@ int getaddrinfo(FAR const char *restrict, FAR const char *restrict, FAR const struct addrinfo *restrict, FAR struct addrinfo **restrict); -FAR struct hostent *gethostbyaddr(FAR const void *, socklen_t, int); #endif -FAR struct hostent *gethostbyname(FAR const char *); +FAR struct hostent *gethostbyaddr(FAR const void *addr, socklen_t len, + int type); +FAR struct hostent *gethostbyname(FAR const char *name); #if 0 /* None of these are yet supported */ FAR struct hostent *gethostent(void); @@ -304,6 +305,9 @@ void setservent(int); /* Non-standard interfaces similar to Glibc 2 interfaces */ +int gethostbyaddr_r(FAR const void *addr, socklen_t len, int type, + FAR struct hostent *host, FAR char *buf, + size_t buflen, int *h_errnop); int gethostbyname_r(FAR const char *name, FAR struct hostent *host, FAR char *buf, size_t buflen, int *h_errnop); diff --git a/libc/net/Make.defs b/libc/net/Make.defs index a32799a91cd..42adc65ad49 100644 --- a/libc/net/Make.defs +++ b/libc/net/Make.defs @@ -47,7 +47,8 @@ endif # netdb support ifeq ($(CONFIG_LIB_NETDB),y) -CSRCS += lib_gethostbyname.c lib_gethostbynamer.c lib_parsehostfile.c +CSRCS += lib_netdb.c lib_gethostbyname.c lib_gethostbynamer.c +CSRCS += lib_gethostbyaddr.c lib_gethostbyaddrr.c lib_parsehostfile.c endif # Add the net directory to the build diff --git a/libc/net/lib_gethostbyaddr.c b/libc/net/lib_gethostbyaddr.c new file mode 100644 index 00000000000..6460e718bc6 --- /dev/null +++ b/libc/net/lib_gethostbyaddr.c @@ -0,0 +1,90 @@ +/**************************************************************************** + * libc/net/lib_gethostbyaddr.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include + +#include "lib_internal.h" +#include "net/lib_netdb.h" + +#ifdef CONFIG_LIB_NETDB + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: gethostbyaddr + * + * Description: + * The gethostbyaddr() function returns a structure of type hostent for + * the given host address addr of length len and address type type. Valid + * address types are AF_INET and AF_INET6. The host address argument is a + * pointer to a struct of a type depending on the address type, for example + * a struct in_addr * for address type AF_INET. + * + * Input Parameters: + * addr - The address of the host to find. + * len - The length of the address + * type - The type of the address + * + * Returned Value: + * Upon successful completion, this function will return a pointer to a + * hostent structure if the requested entry was found, and a null pointer + * if the end of the database was reached or the requested entry was not + * found. + * + * Upon unsuccessful completion, gethostbyaddr() will set h_errno to + * indicate the error + * + ****************************************************************************/ + +FAR struct hostent *gethostbyaddr(FAR const void *addr, socklen_t len, int type) +{ + int ret; + + DEBUGASSERT(addr != NULL); + ret = gethostbyaddr_r(addr, len, type, &g_hostent, g_hostbuffer, + CONFIG_NETDB_BUFSIZE, &h_errno); + return ret == 0 ? &g_hostent : NULL; +} + +#endif /* CONFIG_LIB_NETDB */ diff --git a/libc/net/lib_gethostbyaddrr.c b/libc/net/lib_gethostbyaddrr.c new file mode 100644 index 00000000000..26df529f5b1 --- /dev/null +++ b/libc/net/lib_gethostbyaddrr.c @@ -0,0 +1,188 @@ +/**************************************************************************** + * libc/net/lib_gethostbyaddrr.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include + +#include + +#include "lib_internal.h" +#include "net/lib_netdb.h" + +#ifdef CONFIG_LIB_NETDB + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: gethostbyaddr_r + * + * Description: + * The gethostbyaddr_r() function returns a structure of type hostent for + * the given host address addr of length len and address type type. Valid + * address types are AF_INET and AF_INET6. The host address argument is a + * pointer to a struct of a type depending on the address type, for example + * a struct in_addr * for address type AF_INET. + * + * gethostbyaddr_r() is *not* POSIX but is similar to a Glibc extension and is + * used internally by NuttX to implement the POSIX gethostbyaddr(). + * + * Input Parameters: + * addr - The address of the host to find. + * len - The length of the address + * type - The type of the address + * 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 gethostbyaddr_r(FAR const void *addr, socklen_t len, int type, + FAR struct hostent *host, FAR char *buf, + size_t buflen, int *h_errnop) +{ + FAR FILE *stream; + int herrnocode; + int nread; + + DEBUGASSERT(addr != NULL && host != NULL && buf != NULL); + DEBUGASSERT(type == AF_INET || type == AF_INET6); + + /* Make sure that the h_errno has a non-error code */ + + if (h_errnop) + { + *h_errnop = 0; + } + + /* 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) + */ + + if (nread == -ESPIPE) + { + nread = 0; + } + else if (nread != -EAGAIN) + { + herrnocode = NO_RECOVERY; + goto errorout_with_stream; + } + } + else if (nread > 0 && len == host->h_length && type == host->h_addrtype) + { + /* We successfully read the entry and the type and size of the + * address is good. Now compare the addresses: + */ + + FAR char *hostaddr = host->h_addr; + if (hostaddr != NULL) + { + nvdbg("Comparing addresses...\n"); + if (memcmp(addr, hostaddr, len) == 0) + { + /* We have a match */ + + fclose(stream); + return OK; + } + } + } + } + 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; +} + +#endif /* CONFIG_LIB_NETDB */ diff --git a/libc/net/lib_gethostbyname.c b/libc/net/lib_gethostbyname.c index 3c2a276ac98..2d695d6f69d 100644 --- a/libc/net/lib_gethostbyname.c +++ b/libc/net/lib_gethostbyname.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/netdb/lib_gethostbyname.c + * libc/net/lib_gethostbyname.c * * Copyright (C) 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -43,24 +43,10 @@ #include #include "lib_internal.h" +#include "net/lib_netdb.h" #ifdef CONFIG_LIB_NETDB -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#ifndef CONFIG_NETDB_BUFSIZE -# define CONFIG_NETDB_BUFSIZE 128 -#endif - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -static struct hostent g_hostent; -static char g_hostbuffer[CONFIG_NETDB_BUFSIZE]; - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/libc/net/lib_gethostbynamer.c b/libc/net/lib_gethostbynamer.c index cf464785ace..c1fe9ca8ea8 100644 --- a/libc/net/lib_gethostbynamer.c +++ b/libc/net/lib_gethostbynamer.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/netdb/lib_gethostbynamer.c + * libc/net/lib_gethostbynamer.c * * Copyright (C) 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -59,18 +59,6 @@ * Private Type Definitions ****************************************************************************/ -/* This is the maximum number of alternate host names supported by this - * implementation: - */ - -#ifndef CONFIG_NETDB_MAX_ALTNAMES -# define CONFIG_NETDB_MAX_ALTNAMES 4 -#endif - -#ifndef CONFIG_NETDB_HOSTCONF_PATH -# define CONFIG_NETDB_HOSTCONF_PATH "/etc/hosts" -#endif - /* This is the layout of the caller provided memory area */ struct hostent_info_s @@ -298,11 +286,11 @@ int gethostbyname_r(FAR const char *name, FAR struct hostent *host, * EAGAIN - Error parsing the line (E.g., missing hostname) */ - if (ESPIPE) + if (nread == -ESPIPE) { nread = 0; } - else if (ERANGE) + else if (nread != -EAGAIN) { herrnocode = NO_RECOVERY; goto errorout_with_stream; diff --git a/libc/net/lib_netdb.c b/libc/net/lib_netdb.c new file mode 100644 index 00000000000..42e567ca355 --- /dev/null +++ b/libc/net/lib_netdb.c @@ -0,0 +1,59 @@ +/**************************************************************************** + * libc/net/lib_netdb.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#include "net/lib_netdb.h" + +#ifdef CONFIG_LIB_NETDB + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +struct hostent g_hostent; +char g_hostbuffer[CONFIG_NETDB_BUFSIZE]; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#endif /* CONFIG_LIB_NETDB */ diff --git a/libc/net/lib_netdb.h b/libc/net/lib_netdb.h new file mode 100644 index 00000000000..0712ba5687f --- /dev/null +++ b/libc/net/lib_netdb.h @@ -0,0 +1,98 @@ +/**************************************************************************** + * libc/net/lib_netdb.h + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * + ****************************************************************************/ + +#ifndef __LIBC_NET_LIB_NETDB_H +#define __LIBC_NET_LIB_NETDB_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#ifdef CONFIG_LIB_NETDB + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* This is the maximum number of alternate host names supported by this + * implementation: + */ + +#ifndef CONFIG_NETDB_MAX_ALTNAMES +# define CONFIG_NETDB_MAX_ALTNAMES 4 +#endif + +/* This is the path to the system hosts file */ + +#ifndef CONFIG_NETDB_HOSTCONF_PATH +# define CONFIG_NETDB_HOSTCONF_PATH "/etc/hosts" +#endif + +/* Size of the buffer available for host data */ + +#ifndef CONFIG_NETDB_BUFSIZE +# define CONFIG_NETDB_BUFSIZE 128 +#endif + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +EXTERN struct hostent g_hostent; +EXTERN char g_hostbuffer[CONFIG_NETDB_BUFSIZE]; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __INCLUDE_NETDB_H */ +#endif /* __LIBC_NET_LIB_NETDB_H */ diff --git a/libc/net/lib_parsehostfile.c b/libc/net/lib_parsehostfile.c index 4f0d694ccae..dbef59cf94b 100644 --- a/libc/net/lib_parsehostfile.c +++ b/libc/net/lib_parsehostfile.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/netdb/lib_parsehostile.c + * libc/net/lib_parsehostile.c * * Copyright (C) 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -56,14 +56,6 @@ * Pre-processor Definitions ****************************************************************************/ -/* This is the maximum number of alternate host names supported by this - * implementation: - */ - -#ifndef CONFIG_NETDB_MAX_ALTNAMES -# define CONFIG_NETDB_MAX_ALTNAMES 4 -#endif - /* Check if character is any kind of white space (except for newline) */ #define lib_isspace(c) \