Lee Kai Xuan / mbed-os

Fork of mbed-os by erkin yucel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_netdb.c Source File

lwip_netdb.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * API functions for name resolving
00004  *
00005  */
00006 
00007 /*
00008  * Redistribution and use in source and binary forms, with or without modification,
00009  * are permitted provided that the following conditions are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright notice,
00012  *    this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright notice,
00014  *    this list of conditions and the following disclaimer in the documentation
00015  *    and/or other materials provided with the distribution.
00016  * 3. The name of the author may not be used to endorse or promote products
00017  *    derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00020  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00021  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00022  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00024  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00027  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00028  * OF SUCH DAMAGE.
00029  *
00030  * This file is part of the lwIP TCP/IP stack.
00031  *
00032  * Author: Simon Goldschmidt
00033  *
00034  */
00035 
00036 
00037 /**
00038  * @defgroup netdbapi NETDB API
00039  * @ingroup socket
00040  */
00041 
00042 #include "lwip/netdb.h"
00043 
00044 #if LWIP_DNS && LWIP_SOCKET
00045 
00046 #include "lwip/err.h"
00047 #include "lwip/mem.h"
00048 #include "lwip/memp.h"
00049 #include "lwip/ip_addr.h"
00050 #include "lwip/api.h"
00051 #include "lwip/dns.h"
00052 
00053 #include <string.h>
00054 #include <stdlib.h>
00055 
00056 /** helper struct for gethostbyname_r to access the char* buffer */
00057 struct gethostbyname_r_helper {
00058   ip_addr_t *addr_list[2];
00059   ip_addr_t addr;
00060   char *aliases;
00061 };
00062 
00063 /** h_errno is exported in netdb.h for access by applications. */
00064 #if LWIP_DNS_API_DECLARE_H_ERRNO
00065 int h_errno;
00066 #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
00067 
00068 /** define "hostent" variables storage: 0 if we use a static (but unprotected)
00069  * set of variables for lwip_gethostbyname, 1 if we use a local storage */
00070 #ifndef LWIP_DNS_API_HOSTENT_STORAGE
00071 #define LWIP_DNS_API_HOSTENT_STORAGE 0
00072 #endif
00073 
00074 /** define "hostent" variables storage */
00075 #if LWIP_DNS_API_HOSTENT_STORAGE
00076 #define HOSTENT_STORAGE
00077 #else
00078 #define HOSTENT_STORAGE static
00079 #endif /* LWIP_DNS_API_STATIC_HOSTENT */
00080 
00081 /**
00082  * Returns an entry containing addresses of address family AF_INET
00083  * for the host with name name.
00084  * Due to dns_gethostbyname limitations, only one address is returned.
00085  *
00086  * @param name the hostname to resolve
00087  * @return an entry containing addresses of address family AF_INET
00088  *         for the host with name name
00089  */
00090 struct hostent*
00091 lwip_gethostbyname(const char *name)
00092 {
00093   err_t err;
00094   ip_addr_t addr;
00095 
00096   /* buffer variables for lwip_gethostbyname() */
00097   HOSTENT_STORAGE struct hostent s_hostent;
00098   HOSTENT_STORAGE char *s_aliases;
00099   HOSTENT_STORAGE ip_addr_t s_hostent_addr;
00100   HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
00101   HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
00102 
00103   /* query host IP address */
00104   err = netconn_gethostbyname(name, &addr);
00105   if (err != ERR_OK) {
00106     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
00107     h_errno = HOST_NOT_FOUND;
00108     return NULL;
00109   }
00110 
00111   /* fill hostent */
00112   s_hostent_addr = addr;
00113   s_phostent_addr[0] = &s_hostent_addr;
00114   s_phostent_addr[1] = NULL;
00115   strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
00116   s_hostname[DNS_MAX_NAME_LENGTH] = 0;
00117   s_hostent.h_name = s_hostname;
00118   s_aliases = NULL;
00119   s_hostent.h_aliases = &s_aliases;
00120   s_hostent.h_addrtype = AF_INET;
00121   s_hostent.h_length = sizeof(ip_addr_t);
00122   s_hostent.h_addr_list = (char**)&s_phostent_addr;
00123 
00124 #if DNS_DEBUG
00125   /* dump hostent */
00126   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name           == %s\n", s_hostent.h_name));
00127   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases        == %p\n", (void*)s_hostent.h_aliases));
00128   /* h_aliases are always empty */
00129   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype       == %d\n", s_hostent.h_addrtype));
00130   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length         == %d\n", s_hostent.h_length));
00131   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list      == %p\n", (void*)s_hostent.h_addr_list));
00132   if (s_hostent.h_addr_list != NULL) {
00133     u8_t idx;
00134     for (idx=0; s_hostent.h_addr_list[idx]; idx++) {
00135       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]   == %p\n", idx, s_hostent.h_addr_list[idx]));
00136       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t*)s_hostent.h_addr_list[idx])));
00137     }
00138   }
00139 #endif /* DNS_DEBUG */
00140 
00141 #if LWIP_DNS_API_HOSTENT_STORAGE
00142   /* this function should return the "per-thread" hostent after copy from s_hostent */
00143   return sys_thread_hostent(&s_hostent);
00144 #else
00145   return &s_hostent;
00146 #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
00147 }
00148 
00149 /**
00150  * Thread-safe variant of lwip_gethostbyname: instead of using a static
00151  * buffer, this function takes buffer and errno pointers as arguments
00152  * and uses these for the result.
00153  *
00154  * @param name the hostname to resolve
00155  * @param ret pre-allocated struct where to store the result
00156  * @param buf pre-allocated buffer where to store additional data
00157  * @param buflen the size of buf
00158  * @param result pointer to a hostent pointer that is set to ret on success
00159  *               and set to zero on error
00160  * @param h_errnop pointer to an int where to store errors (instead of modifying
00161  *                 the global h_errno)
00162  * @return 0 on success, non-zero on error, additional error information
00163  *         is stored in *h_errnop instead of h_errno to be thread-safe
00164  */
00165 int
00166 lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
00167                 size_t buflen, struct hostent **result, int *h_errnop)
00168 {
00169   err_t err;
00170   struct gethostbyname_r_helper *h;
00171   char *hostname;
00172   size_t namelen;
00173   int lh_errno;
00174 
00175   if (h_errnop == NULL) {
00176     /* ensure h_errnop is never NULL */
00177     h_errnop = &lh_errno;
00178   }
00179 
00180   if (result == NULL) {
00181     /* not all arguments given */
00182     *h_errnop = EINVAL;
00183     return -1;
00184   }
00185   /* first thing to do: set *result to nothing */
00186   *result = NULL;
00187   if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
00188     /* not all arguments given */
00189     *h_errnop = EINVAL;
00190     return -1;
00191   }
00192 
00193   namelen = strlen(name);
00194   if (buflen < (sizeof(struct gethostbyname_r_helper) + namelen + 1 + (MEM_ALIGNMENT - 1))) {
00195     /* buf can't hold the data needed + a copy of name */
00196     *h_errnop = ERANGE;
00197     return -1;
00198   }
00199 
00200   h = (struct gethostbyname_r_helper*)LWIP_MEM_ALIGN(buf);
00201   hostname = ((char*)h) + sizeof(struct gethostbyname_r_helper);
00202 
00203   /* query host IP address */
00204   err = netconn_gethostbyname(name, &h->addr);
00205   if (err != ERR_OK) {
00206     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
00207     *h_errnop = HOST_NOT_FOUND;
00208     return -1;
00209   }
00210 
00211   /* copy the hostname into buf */
00212   MEMCPY(hostname, name, namelen);
00213   hostname[namelen] = 0;
00214 
00215   /* fill hostent */
00216   h->addr_list[0] = &h->addr;
00217   h->addr_list[1] = NULL;
00218   h->aliases = NULL;
00219   ret->h_name = hostname;
00220   ret->h_aliases = &h->aliases;
00221   ret->h_addrtype = AF_INET;
00222   ret->h_length = sizeof(ip_addr_t);
00223   ret->h_addr_list = (char**)&h->addr_list;
00224 
00225   /* set result != NULL */
00226   *result = ret;
00227 
00228   /* return success */
00229   return 0;
00230 }
00231 
00232 /**
00233  * Frees one or more addrinfo structures returned by getaddrinfo(), along with
00234  * any additional storage associated with those structures. If the ai_next field
00235  * of the structure is not null, the entire list of structures is freed.
00236  *
00237  * @param ai struct addrinfo to free
00238  */
00239 void
00240 lwip_freeaddrinfo(struct addrinfo *ai)
00241 {
00242   struct addrinfo *next;
00243 
00244   while (ai != NULL) {
00245     next = ai->ai_next;
00246     memp_free(MEMP_NETDB, ai);
00247     ai = next;
00248   }
00249 }
00250 
00251 /**
00252  * Translates the name of a service location (for example, a host name) and/or
00253  * a service name and returns a set of socket addresses and associated
00254  * information to be used in creating a socket with which to address the
00255  * specified service.
00256  * Memory for the result is allocated internally and must be freed by calling
00257  * lwip_freeaddrinfo()!
00258  *
00259  * Due to a limitation in dns_gethostbyname, only the first address of a
00260  * host is returned.
00261  * Also, service names are not supported (only port numbers)!
00262  *
00263  * @param nodename descriptive name or address string of the host
00264  *                 (may be NULL -> local address)
00265  * @param servname port number as string of NULL
00266  * @param hints structure containing input values that set socktype and protocol
00267  * @param res pointer to a pointer where to store the result (set to NULL on failure)
00268  * @return 0 on success, non-zero on failure
00269  *
00270  * @todo: implement AI_V4MAPPED, AI_ADDRCONFIG
00271  */
00272 int
00273 lwip_getaddrinfo(const char *nodename, const char *servname,
00274        const struct addrinfo *hints, struct addrinfo **res)
00275 {
00276   err_t err;
00277   ip_addr_t addr;
00278   struct addrinfo *ai;
00279   struct sockaddr_storage *sa = NULL;
00280   int port_nr = 0;
00281   size_t total_size;
00282   size_t namelen = 0;
00283   int ai_family;
00284 
00285   if (res == NULL) {
00286     return EAI_FAIL;
00287   }
00288   *res = NULL;
00289   if ((nodename == NULL) && (servname == NULL)) {
00290     return EAI_NONAME;
00291   }
00292 
00293   if (hints != NULL) {
00294     ai_family = hints->ai_family;
00295     if ((ai_family != AF_UNSPEC)
00296 #if LWIP_IPV4
00297       && (ai_family != AF_INET)
00298 #endif /* LWIP_IPV4 */
00299 #if LWIP_IPV6
00300       && (ai_family != AF_INET6)
00301 #endif /* LWIP_IPV6 */
00302       ) {
00303       return EAI_FAMILY;
00304     }
00305   } else {
00306     ai_family = AF_UNSPEC;
00307   }
00308 
00309   if (servname != NULL) {
00310     /* service name specified: convert to port number
00311      * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
00312     port_nr = atoi(servname);
00313     if ((port_nr <= 0) || (port_nr > 0xffff)) {
00314       return EAI_SERVICE;
00315     }
00316   }
00317 
00318   if (nodename != NULL) {
00319     /* service location specified, try to resolve */
00320     if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
00321       /* no DNS lookup, just parse for an address string */
00322       if (!ipaddr_aton(nodename, &addr)) {
00323         return EAI_NONAME;
00324       }
00325 #if LWIP_IPV4 && LWIP_IPV6
00326       if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
00327           (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
00328         return EAI_NONAME;
00329       }
00330 #endif /* LWIP_IPV4 && LWIP_IPV6 */
00331     } else {
00332 #if LWIP_IPV4 && LWIP_IPV6
00333       /* AF_UNSPEC: prefer IPv4 */
00334       u8_t type = NETCONN_DNS_IPV4_IPV6;
00335       if (ai_family == AF_INET) {
00336         type = NETCONN_DNS_IPV4;
00337       } else if (ai_family == AF_INET6) {
00338         type = NETCONN_DNS_IPV6;
00339       }
00340 #endif /* LWIP_IPV4 && LWIP_IPV6 */
00341       err = netconn_gethostbyname_addrtype(nodename, &addr, type);
00342       if (err != ERR_OK) {
00343         return EAI_FAIL;
00344       }
00345     }
00346   } else {
00347     /* service location specified, use loopback address */
00348     if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
00349       ip_addr_set_any(ai_family == AF_INET6, &addr);
00350     } else {
00351       ip_addr_set_loopback(ai_family == AF_INET6, &addr);
00352     }
00353   }
00354 
00355   total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
00356   if (nodename != NULL) {
00357     namelen = strlen(nodename);
00358     if (namelen > DNS_MAX_NAME_LENGTH) {
00359       /* invalid name length */
00360       return EAI_FAIL;
00361     }
00362     LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
00363     total_size += namelen + 1;
00364   }
00365   /* If this fails, please report to lwip-devel! :-) */
00366   LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
00367     total_size <= NETDB_ELEM_SIZE);
00368   ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
00369   if (ai == NULL) {
00370     return EAI_MEMORY;
00371   }
00372   memset(ai, 0, total_size);
00373   /* cast through void* to get rid of alignment warnings */
00374   sa = (struct sockaddr_storage *)(void*)((u8_t*)ai + sizeof(struct addrinfo));
00375   if (IP_IS_V6_VAL(addr)) {
00376 #if LWIP_IPV6
00377     struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)sa;
00378     /* set up sockaddr */
00379     inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
00380     sa6->sin6_family = AF_INET6;
00381     sa6->sin6_len = sizeof(struct sockaddr_in6);
00382     sa6->sin6_port = htons((u16_t)port_nr);
00383     ai->ai_family = AF_INET6;
00384 #endif /* LWIP_IPV6 */
00385   } else {
00386 #if LWIP_IPV4
00387     struct sockaddr_in *sa4 = (struct sockaddr_in*)sa;
00388     /* set up sockaddr */
00389     inet_addr_from_ipaddr(&sa4->sin_addr, ip_2_ip4(&addr));
00390     sa4->sin_family = AF_INET;
00391     sa4->sin_len = sizeof(struct sockaddr_in);
00392     sa4->sin_port = htons((u16_t)port_nr);
00393     ai->ai_family = AF_INET;
00394 #endif /* LWIP_IPV4 */
00395   }
00396 
00397   /* set up addrinfo */
00398   if (hints != NULL) {
00399     /* copy socktype & protocol from hints if specified */
00400     ai->ai_socktype = hints->ai_socktype;
00401     ai->ai_protocol = hints->ai_protocol;
00402   }
00403   if (nodename != NULL) {
00404     /* copy nodename to canonname if specified */
00405     ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
00406     MEMCPY(ai->ai_canonname, nodename, namelen);
00407     ai->ai_canonname[namelen] = 0;
00408   }
00409   ai->ai_addrlen = sizeof(struct sockaddr_storage);
00410   ai->ai_addr = (struct sockaddr*)sa;
00411 
00412   *res = ai;
00413 
00414   return 0;
00415 }
00416 
00417 #endif /* LWIP_DNS && LWIP_SOCKET */