• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * API functions for name resolving
4  *
5  * @defgroup netdbapi NETDB API
6  * @ingroup socket
7  */
8 
9 /*
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Simon Goldschmidt
35  *
36  */
37 
38 #include "lwip/netdb.h"
39 
40 #if LWIP_DNS && LWIP_SOCKET
41 
42 #include "lwip/err.h"
43 #include "lwip/mem.h"
44 #include "lwip/memp.h"
45 #include "lwip/ip_addr.h"
46 #include "lwip/api.h"
47 #include "lwip/dns.h"
48 
49 #include <string.h> /* memset */
50 #include <stdlib.h> /* atoi */
51 
52 /** helper struct for gethostbyname_r to access the char* buffer */
53 struct gethostbyname_r_helper {
54   ip_addr_t *addr_list[2];
55   ip_addr_t addr;
56   char *aliases;
57 };
58 
59 /** h_errno is exported in netdb.h for access by applications. */
60 #if LWIP_DNS_API_DECLARE_H_ERRNO
61 int h_errno;
62 #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
63 
64 /** define "hostent" variables storage: 0 if we use a static (but unprotected)
65  * set of variables for lwip_gethostbyname, 1 if we use a local storage */
66 #ifndef LWIP_DNS_API_HOSTENT_STORAGE
67 #define LWIP_DNS_API_HOSTENT_STORAGE 0
68 #endif
69 
70 /** define "hostent" variables storage */
71 #if LWIP_DNS_API_HOSTENT_STORAGE
72 #define HOSTENT_STORAGE
73 #else
74 #define HOSTENT_STORAGE static
75 #endif /* LWIP_DNS_API_STATIC_HOSTENT */
76 
77 /**
78  * Returns an entry containing addresses of address family AF_INET
79  * for the host with name name.
80  * Due to dns_gethostbyname limitations, only one address is returned.
81  *
82  * @param name the hostname to resolve
83  * @return an entry containing addresses of address family AF_INET
84  *         for the host with name name
85  */
86 struct hostent *
lwip_gethostbyname(const char * name)87 lwip_gethostbyname(const char *name)
88 {
89   err_t err;
90   ip_addr_t addr;
91 
92   /* buffer variables for lwip_gethostbyname() */
93   HOSTENT_STORAGE struct hostent s_hostent;
94   HOSTENT_STORAGE char *s_aliases;
95   HOSTENT_STORAGE ip_addr_t s_hostent_addr;
96   HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
97   HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
98 
99   /* query host IP address */
100   err = netconn_gethostbyname(name, &addr);
101   if (err != ERR_OK) {
102     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
103     h_errno = HOST_NOT_FOUND;
104     return NULL;
105   }
106 
107   /* fill hostent */
108   s_hostent_addr = addr;
109   s_phostent_addr[0] = &s_hostent_addr;
110   s_phostent_addr[1] = NULL;
111   strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
112   s_hostname[DNS_MAX_NAME_LENGTH] = 0;
113   s_hostent.h_name = s_hostname;
114   s_aliases = NULL;
115   s_hostent.h_aliases = &s_aliases;
116   s_hostent.h_addrtype = AF_INET;
117   s_hostent.h_length = sizeof(ip_addr_t);
118   s_hostent.h_addr_list = (char **)&s_phostent_addr;
119 
120 #if DNS_DEBUG
121   /* dump hostent */
122   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name           == %s\n", s_hostent.h_name));
123   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases        == %p\n", (void *)s_hostent.h_aliases));
124   /* h_aliases are always empty */
125   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype       == %d\n", s_hostent.h_addrtype));
126   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length         == %d\n", s_hostent.h_length));
127   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list      == %p\n", (void *)s_hostent.h_addr_list));
128   if (s_hostent.h_addr_list != NULL) {
129     u8_t idx;
130     for (idx = 0; s_hostent.h_addr_list[idx]; idx++) {
131       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa(s_phostent_addr[idx])));
132     }
133   }
134 #endif /* DNS_DEBUG */
135 
136 #if LWIP_DNS_API_HOSTENT_STORAGE
137   /* this function should return the "per-thread" hostent after copy from s_hostent */
138   return sys_thread_hostent(&s_hostent);
139 #else
140   return &s_hostent;
141 #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
142 }
143 
144 /**
145  * Thread-safe variant of lwip_gethostbyname: instead of using a static
146  * buffer, this function takes buffer and errno pointers as arguments
147  * and uses these for the result.
148  *
149  * @param name the hostname to resolve
150  * @param ret pre-allocated struct where to store the result
151  * @param buf pre-allocated buffer where to store additional data
152  * @param buflen the size of buf
153  * @param result pointer to a hostent pointer that is set to ret on success
154  *               and set to zero on error
155  * @param h_errnop pointer to an int where to store errors (instead of modifying
156  *                 the global h_errno)
157  * @return 0 on success, non-zero on error, additional error information
158  *         is stored in *h_errnop instead of h_errno to be thread-safe
159  */
160 int
lwip_gethostbyname_r(const char * name,struct hostent * ret,char * buf,size_t buflen,struct hostent ** result,int * h_errnop)161 lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
162                      size_t buflen, struct hostent **result, int *h_errnop)
163 {
164   err_t err;
165   struct gethostbyname_r_helper *h;
166   char *hostname;
167   size_t namelen;
168   int lh_errno;
169 
170   if (h_errnop == NULL) {
171     /* ensure h_errnop is never NULL */
172     h_errnop = &lh_errno;
173   }
174 
175   if (result == NULL) {
176     /* not all arguments given */
177     *h_errnop = EINVAL;
178     return -1;
179   }
180   /* first thing to do: set *result to nothing */
181   *result = NULL;
182   if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
183     /* not all arguments given */
184     *h_errnop = EINVAL;
185     return -1;
186   }
187 
188   namelen = strlen(name);
189   if (buflen < (sizeof(struct gethostbyname_r_helper) + LWIP_MEM_ALIGN_BUFFER(namelen + 1))) {
190     /* buf can't hold the data needed + a copy of name */
191     *h_errnop = ERANGE;
192     return -1;
193   }
194 
195   h = (struct gethostbyname_r_helper *)LWIP_MEM_ALIGN(buf);
196   hostname = ((char *)h) + sizeof(struct gethostbyname_r_helper);
197 
198   /* query host IP address */
199   err = netconn_gethostbyname(name, &h->addr);
200   if (err != ERR_OK) {
201     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
202     *h_errnop = HOST_NOT_FOUND;
203     return -1;
204   }
205 
206   /* copy the hostname into buf */
207   MEMCPY(hostname, name, namelen);
208   hostname[namelen] = 0;
209 
210   /* fill hostent */
211   h->addr_list[0] = &h->addr;
212   h->addr_list[1] = NULL;
213   h->aliases = NULL;
214   ret->h_name = hostname;
215   ret->h_aliases = &h->aliases;
216   ret->h_addrtype = AF_INET;
217   ret->h_length = sizeof(ip_addr_t);
218   ret->h_addr_list = (char **)&h->addr_list;
219 
220   /* set result != NULL */
221   *result = ret;
222 
223   /* return success */
224   return 0;
225 }
226 
227 /**
228  * Frees one or more addrinfo structures returned by getaddrinfo(), along with
229  * any additional storage associated with those structures. If the ai_next field
230  * of the structure is not null, the entire list of structures is freed.
231  *
232  * @param ai struct addrinfo to free
233  */
234 void
lwip_freeaddrinfo(struct addrinfo * ai)235 lwip_freeaddrinfo(struct addrinfo *ai)
236 {
237   struct addrinfo *next;
238 
239   while (ai != NULL) {
240     next = ai->ai_next;
241     memp_free(MEMP_NETDB, ai);
242     ai = next;
243   }
244 }
245 
246 /**
247  * Translates the name of a service location (for example, a host name) and/or
248  * a service name and returns a set of socket addresses and associated
249  * information to be used in creating a socket with which to address the
250  * specified service.
251  * Memory for the result is allocated internally and must be freed by calling
252  * lwip_freeaddrinfo()!
253  *
254  * Due to a limitation in dns_gethostbyname, only the first address of a
255  * host is returned.
256  * Also, service names are not supported (only port numbers)!
257  *
258  * @param nodename descriptive name or address string of the host
259  *                 (may be NULL -> local address)
260  * @param servname port number as string of NULL
261  * @param hints structure containing input values that set socktype and protocol
262  * @param res pointer to a pointer where to store the result (set to NULL on failure)
263  * @return 0 on success, non-zero on failure
264  *
265  * @todo: implement AI_V4MAPPED, AI_ADDRCONFIG
266  */
267 int
lwip_getaddrinfo(const char * nodename,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)268 lwip_getaddrinfo(const char *nodename, const char *servname,
269                  const struct addrinfo *hints, struct addrinfo **res)
270 {
271   err_t err;
272   ip_addr_t addr;
273   struct addrinfo *ai;
274   struct sockaddr_storage *sa = NULL;
275   int port_nr = 0;
276   size_t total_size;
277   size_t namelen = 0;
278   int ai_family;
279 
280   if (res == NULL) {
281     return EAI_FAIL;
282   }
283   *res = NULL;
284   if ((nodename == NULL) && (servname == NULL)) {
285     return EAI_NONAME;
286   }
287 
288   if (hints != NULL) {
289     ai_family = hints->ai_family;
290     if ((ai_family != AF_UNSPEC)
291 #if LWIP_IPV4
292         && (ai_family != AF_INET)
293 #endif /* LWIP_IPV4 */
294 #if LWIP_IPV6
295         && (ai_family != AF_INET6)
296 #endif /* LWIP_IPV6 */
297        ) {
298       return EAI_FAMILY;
299     }
300   } else {
301     ai_family = AF_UNSPEC;
302   }
303 
304   if (servname != NULL) {
305     /* service name specified: convert to port number
306      * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
307     port_nr = atoi(servname);
308     if (port_nr == 0 && (servname[0] != '0')) {
309       /* atoi failed - service was not numeric */
310       return EAI_SERVICE;
311     }
312     if ((port_nr < 0) || (port_nr > 0xffff)) {
313       return EAI_SERVICE;
314     }
315   }
316 
317   if (nodename != NULL) {
318     /* service location specified, try to resolve */
319     if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
320       /* no DNS lookup, just parse for an address string */
321       if (!ipaddr_aton(nodename, &addr)) {
322         return EAI_NONAME;
323       }
324 #if LWIP_IPV4 && LWIP_IPV6
325       if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
326           (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
327         return EAI_NONAME;
328       }
329 #endif /* LWIP_IPV4 && LWIP_IPV6 */
330     } else {
331 #if LWIP_IPV4 && LWIP_IPV6
332       /* AF_UNSPEC: prefer IPv4 */
333       u8_t type = NETCONN_DNS_IPV4_IPV6;
334       if (ai_family == AF_INET) {
335         type = NETCONN_DNS_IPV4;
336       } else if (ai_family == AF_INET6) {
337         type = NETCONN_DNS_IPV6;
338       }
339 #endif /* LWIP_IPV4 && LWIP_IPV6 */
340       err = netconn_gethostbyname_addrtype(nodename, &addr, type);
341       if (err != ERR_OK) {
342         return EAI_FAIL;
343       }
344     }
345   } else {
346     /* service location specified, use loopback address */
347     if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
348       ip_addr_set_any_val(ai_family == AF_INET6, addr);
349     } else {
350       ip_addr_set_loopback_val(ai_family == AF_INET6, addr);
351     }
352   }
353 
354   total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
355   if (nodename != NULL) {
356     namelen = strlen(nodename);
357     if (namelen > DNS_MAX_NAME_LENGTH) {
358       /* invalid name length */
359       return EAI_FAIL;
360     }
361     LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
362     total_size += namelen + 1;
363   }
364   /* If this fails, please report to lwip-devel! :-) */
365   LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
366               total_size <= NETDB_ELEM_SIZE);
367   ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
368   if (ai == NULL) {
369     return EAI_MEMORY;
370   }
371   memset(ai, 0, total_size);
372   /* cast through void* to get rid of alignment warnings */
373   sa = (struct sockaddr_storage *)(void *)((u8_t *)ai + sizeof(struct addrinfo));
374   if (IP_IS_V6_VAL(addr)) {
375 #if LWIP_IPV6
376     struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
377     /* set up sockaddr */
378     inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
379     sa6->sin6_family = AF_INET6;
380     sa6->sin6_len = sizeof(struct sockaddr_in6);
381     sa6->sin6_port = lwip_htons((u16_t)port_nr);
382     sa6->sin6_scope_id = ip6_addr_zone(ip_2_ip6(&addr));
383     ai->ai_family = AF_INET6;
384     ai->ai_addrlen = sizeof(struct sockaddr_in6);
385 #endif /* LWIP_IPV6 */
386   } else {
387 #if LWIP_IPV4
388     struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
389     /* set up sockaddr */
390     inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
391     sa4->sin_family = AF_INET;
392     sa4->sin_len = sizeof(struct sockaddr_in);
393     sa4->sin_port = lwip_htons((u16_t)port_nr);
394     ai->ai_family = AF_INET;
395     ai->ai_addrlen = sizeof(struct sockaddr_in);
396 #endif /* LWIP_IPV4 */
397   }
398 
399   /* set up addrinfo */
400   if (hints != NULL) {
401     /* copy socktype & protocol from hints if specified */
402     ai->ai_socktype = hints->ai_socktype;
403     ai->ai_protocol = hints->ai_protocol;
404   }
405   if (nodename != NULL) {
406     /* copy nodename to canonname if specified */
407     ai->ai_canonname = ((char *)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
408     MEMCPY(ai->ai_canonname, nodename, namelen);
409     ai->ai_canonname[namelen] = 0;
410   }
411   ai->ai_addr = (struct sockaddr *)sa;
412 
413   *res = ai;
414 
415   return 0;
416 }
417 
418 #endif /* LWIP_DNS && LWIP_SOCKET */
419