Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/api/netdb.c need to be fixed.

0001 /**
0002  * @file
0003  * API functions for name resolving
0004  *
0005  */
0006 
0007 /*
0008  * Redistribution and use in source and binary forms, with or without modification, 
0009  * are permitted provided that the following conditions are met:
0010  *
0011  * 1. Redistributions of source code must retain the above copyright notice,
0012  *    this list of conditions and the following disclaimer.
0013  * 2. Redistributions in binary form must reproduce the above copyright notice,
0014  *    this list of conditions and the following disclaimer in the documentation
0015  *    and/or other materials provided with the distribution.
0016  * 3. The name of the author may not be used to endorse or promote products
0017  *    derived from this software without specific prior written permission. 
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
0020  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
0021  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
0022  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
0023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
0024  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
0025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
0026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
0027  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
0028  * OF SUCH DAMAGE.
0029  *
0030  * This file is part of the lwIP TCP/IP stack.
0031  * 
0032  * Author: Simon Goldschmidt
0033  *
0034  */
0035 
0036 #include "lwip/netdb.h"
0037 
0038 #if LWIP_DNS && LWIP_SOCKET
0039 
0040 #include "lwip/err.h"
0041 #include "lwip/mem.h"
0042 #include "lwip/ip_addr.h"
0043 #include "lwip/api.h"
0044 
0045 #include <string.h>
0046 #include <stdlib.h>
0047 
0048 /** helper struct for gethostbyname_r to access the char* buffer */
0049 struct gethostbyname_r_helper {
0050   struct ip_addr *addrs;
0051   struct ip_addr addr;
0052   char *aliases;
0053 };
0054 
0055 /** h_errno is exported in netdb.h for access by applications. */
0056 #if LWIP_DNS_API_DECLARE_H_ERRNO
0057 int h_errno;
0058 #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
0059 
0060 /** define "hostent" variables storage: 0 if we use a static (but unprotected)
0061  * set of variables for lwip_gethostbyname, 1 if we use a local storage */
0062 #ifndef LWIP_DNS_API_HOSTENT_STORAGE
0063 #define LWIP_DNS_API_HOSTENT_STORAGE 0
0064 #endif
0065 
0066 /** define "hostent" variables storage */
0067 #if LWIP_DNS_API_HOSTENT_STORAGE
0068 #define HOSTENT_STORAGE
0069 #else
0070 #define HOSTENT_STORAGE static
0071 #endif /* LWIP_DNS_API_STATIC_HOSTENT */
0072 
0073 /**
0074  * Returns an entry containing addresses of address family AF_INET
0075  * for the host with name name.
0076  * Due to dns_gethostbyname limitations, only one address is returned.
0077  *
0078  * @param name the hostname to resolve
0079  * @return an entry containing addresses of address family AF_INET
0080  *         for the host with name name
0081  */
0082 struct hostent*
0083 lwip_gethostbyname(const char *name)
0084 {
0085   err_t err;
0086   struct ip_addr addr;
0087 
0088   /* buffer variables for lwip_gethostbyname() */
0089   HOSTENT_STORAGE struct hostent s_hostent;
0090   HOSTENT_STORAGE char *s_aliases;
0091   HOSTENT_STORAGE struct ip_addr s_hostent_addr;
0092   HOSTENT_STORAGE struct ip_addr *s_phostent_addr;
0093 
0094   /* query host IP address */
0095   err = netconn_gethostbyname(name, &addr);
0096   if (err != ERR_OK) {
0097     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
0098     h_errno = HOST_NOT_FOUND;
0099     return NULL;
0100   }
0101 
0102   /* fill hostent */
0103   s_hostent_addr = addr;
0104   s_phostent_addr = &s_hostent_addr;
0105   s_hostent.h_name = (char*)name;
0106   s_hostent.h_aliases = &s_aliases;
0107   s_hostent.h_addrtype = AF_INET;
0108   s_hostent.h_length = sizeof(struct ip_addr);
0109   s_hostent.h_addr_list = (char**)&s_phostent_addr;
0110 
0111 #if DNS_DEBUG
0112   /* dump hostent */
0113   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name           == %s\n", s_hostent.h_name));
0114   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases        == %p\n", s_hostent.h_aliases));
0115   if (s_hostent.h_aliases != NULL) {
0116     u8_t idx;
0117     for ( idx=0; s_hostent.h_aliases[idx]; idx++) {
0118       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]->   == %p\n", idx, s_hostent.h_aliases[idx]));
0119       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]->   == %s\n", idx, s_hostent.h_aliases[idx]));
0120     }
0121   }
0122   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype       == %d\n", s_hostent.h_addrtype));
0123   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length         == %d\n", s_hostent.h_length));
0124   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list      == %p\n", s_hostent.h_addr_list));
0125   if (s_hostent.h_addr_list != NULL) {
0126     u8_t idx;
0127     for ( idx=0; s_hostent.h_addr_list[idx]; idx++) {
0128       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]   == %p\n", idx, s_hostent.h_addr_list[idx]));
0129       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ip_ntoa(s_hostent.h_addr_list[idx])));
0130     }
0131   }
0132 #endif /* DNS_DEBUG */
0133 
0134 #if LWIP_DNS_API_HOSTENT_STORAGE
0135   /* this function should return the "per-thread" hostent after copy from s_hostent */
0136   return sys_thread_hostent(&s_hostent);
0137 #else
0138   return &s_hostent;
0139 #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
0140 }
0141 
0142 /**
0143  * Thread-safe variant of lwip_gethostbyname: instead of using a static
0144  * buffer, this function takes buffer and errno pointers as arguments
0145  * and uses these for the result.
0146  *
0147  * @param name the hostname to resolve
0148  * @param ret pre-allocated struct where to store the result
0149  * @param buf pre-allocated buffer where to store additional data
0150  * @param buflen the size of buf
0151  * @param result pointer to a hostent pointer that is set to ret on success
0152  *               and set to zero on error
0153  * @param h_errnop pointer to an int where to store errors (instead of modifying
0154  *                 the global h_errno)
0155  * @return 0 on success, non-zero on error, additional error information
0156  *         is stored in *h_errnop instead of h_errno to be thread-safe
0157  */
0158 int
0159 lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
0160                 size_t buflen, struct hostent **result, int *h_errnop)
0161 {
0162   err_t err;
0163   struct gethostbyname_r_helper *h;
0164   char *hostname;
0165   size_t namelen;
0166   int lh_errno;
0167 
0168   if (h_errnop == NULL) {
0169     /* ensure h_errnop is never NULL */
0170     h_errnop = &lh_errno;
0171   }
0172 
0173   if (result == NULL) {
0174     /* not all arguments given */
0175     *h_errnop = EINVAL;
0176     return -1;
0177   }
0178   /* first thing to do: set *result to nothing */
0179   *result = NULL;
0180   if ((name == NULL) || (ret == NULL) || (buf == 0)) {
0181     /* not all arguments given */
0182     *h_errnop = EINVAL;
0183     return -1;
0184   }
0185 
0186   namelen = strlen(name);
0187   if (buflen < (sizeof(struct gethostbyname_r_helper) + namelen + 1 + (MEM_ALIGNMENT - 1))) {
0188     /* buf can't hold the data needed + a copy of name */
0189     *h_errnop = ERANGE;
0190     return -1;
0191   }
0192 
0193   h = (struct gethostbyname_r_helper*)LWIP_MEM_ALIGN(buf);
0194   hostname = ((char*)h) + sizeof(struct gethostbyname_r_helper);
0195 
0196   /* query host IP address */
0197   err = netconn_gethostbyname(name, &(h->addr));
0198   if (err != ERR_OK) {
0199     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
0200     *h_errnop = ENSRNOTFOUND;
0201     return -1;
0202   }
0203 
0204   /* copy the hostname into buf */
0205   MEMCPY(hostname, name, namelen);
0206   hostname[namelen] = 0;
0207 
0208   /* fill hostent */
0209   h->addrs = &(h->addr);
0210   h->aliases = NULL;
0211   ret->h_name = (char*)hostname;
0212   ret->h_aliases = &(h->aliases);
0213   ret->h_addrtype = AF_INET;
0214   ret->h_length = sizeof(struct ip_addr);
0215   ret->h_addr_list = (char**)&(h->addrs);
0216 
0217   /* set result != NULL */
0218   *result = ret;
0219 
0220   /* return success */
0221   return 0;
0222 }
0223 
0224 /**
0225  * Frees one or more addrinfo structures returned by getaddrinfo(), along with
0226  * any additional storage associated with those structures. If the ai_next field
0227  * of the structure is not null, the entire list of structures is freed.
0228  *
0229  * @param ai struct addrinfo to free
0230  */
0231 void
0232 lwip_freeaddrinfo(struct addrinfo *ai)
0233 {
0234   struct addrinfo *next;
0235 
0236   while (ai != NULL) {
0237     next = ai->ai_next;
0238     mem_free(ai);
0239     ai = next;
0240   }
0241 }
0242 
0243 /**
0244  * Translates the name of a service location (for example, a host name) and/or
0245  * a service name and returns a set of socket addresses and associated
0246  * information to be used in creating a socket with which to address the
0247  * specified service.
0248  * Memory for the result is allocated internally and must be freed by calling
0249  * lwip_freeaddrinfo()!
0250  *
0251  * Due to a limitation in dns_gethostbyname, only the first address of a
0252  * host is returned.
0253  * Also, service names are not supported (only port numbers)!
0254  *
0255  * @param nodename descriptive name or address string of the host
0256  *                 (may be NULL -> local address)
0257  * @param servname port number as string of NULL 
0258  * @param hints structure containing input values that set socktype and protocol
0259  * @param res pointer to a pointer where to store the result (set to NULL on failure)
0260  * @return 0 on success, non-zero on failure
0261  */
0262 int
0263 lwip_getaddrinfo(const char *nodename, const char *servname,
0264        const struct addrinfo *hints, struct addrinfo **res)
0265 {
0266   err_t err;
0267   struct ip_addr addr;
0268   struct addrinfo *ai;
0269   struct sockaddr_in *sa = NULL;
0270   int port_nr = 0;
0271   size_t total_size;
0272   size_t namelen = 0;
0273 
0274   if (res == NULL) {
0275     return EAI_FAIL;
0276   }
0277   *res = NULL;
0278   if ((nodename == NULL) && (servname == NULL)) {
0279     return EAI_NONAME;
0280   }
0281 
0282   if (servname != NULL) {
0283     /* service name specified: convert to port number
0284      * @todo?: currently, only ASCII integers (port numbers) are supported! */
0285     port_nr = atoi(servname);
0286     if ((port_nr <= 0) || (port_nr > 0xffff)) {
0287       return EAI_SERVICE;
0288     }
0289   }
0290 
0291   if (nodename != NULL) {
0292     /* service location specified, try to resolve */
0293     err = netconn_gethostbyname(nodename, &addr);
0294     if (err != ERR_OK) {
0295       return EAI_FAIL;
0296     }
0297   } else {
0298     /* service location specified, use loopback address */
0299     addr.addr = htonl(INADDR_LOOPBACK);
0300   }
0301 
0302   total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_in);
0303   if (nodename != NULL) {
0304     namelen = strlen(nodename);
0305     LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
0306     total_size += namelen + 1;
0307   }
0308   ai = mem_malloc(total_size);
0309   if (ai == NULL) {
0310     goto memerr;
0311   }
0312   memset(ai, 0, total_size);
0313   sa = (struct sockaddr_in*)((u8_t*)ai + sizeof(struct addrinfo));
0314   /* set up sockaddr */
0315   sa->sin_addr.s_addr = addr.addr;
0316   sa->sin_family = AF_INET;
0317   sa->sin_len = sizeof(struct sockaddr_in);
0318   sa->sin_port = htons(port_nr);
0319 
0320   /* set up addrinfo */
0321   ai->ai_family = AF_INET;
0322   if (hints != NULL) {
0323     /* copy socktype & protocol from hints if specified */
0324     ai->ai_socktype = hints->ai_socktype;
0325     ai->ai_protocol = hints->ai_protocol;
0326   }
0327   if (nodename != NULL) {
0328     /* copy nodename to canonname if specified */
0329     ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
0330     MEMCPY(ai->ai_canonname, nodename, namelen);
0331     ai->ai_canonname[namelen] = 0;
0332   }
0333   ai->ai_addrlen = sizeof(struct sockaddr_in);
0334   ai->ai_addr = (struct sockaddr*)sa;
0335 
0336   *res = ai;
0337 
0338   return 0;
0339 memerr:
0340   if (ai != NULL) {
0341     mem_free(ai);
0342   }
0343   return EAI_MEMORY;
0344 }
0345 
0346 #endif /* LWIP_DNS && LWIP_SOCKET */