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/errno.h"
44 #include "lwip/mem.h"
45 #include "lwip/memp.h"
46 #include "lwip/ip_addr.h"
47 #include "lwip/api.h"
48 #include "lwip/dns.h"
49
50 #include <string.h> /* memset */
51 #include <stdlib.h> /* atoi */
52
53 /** helper struct for gethostbyname_r to access the char* buffer */
54 struct gethostbyname_r_helper {
55 ip_addr_t *addr_list[2];
56 ip_addr_t addr;
57 char *aliases;
58 };
59
60 /** h_errno is exported in netdb.h for access by applications. */
61 #if LWIP_DNS_API_DECLARE_H_ERRNO
62 int h_errno;
63 #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
64
65 /** LWIP_DNS_API_HOSTENT_STORAGE: if set to 0 (default), lwip_gethostbyname()
66 * returns the same global variable for all calls (in all threads).
67 * When set to 1, your port should provide a function
68 * struct hostent* sys_thread_hostent( struct hostent* h);
69 * which have to do a copy of "h" and return a pointer ont the "per-thread"
70 * copy.
71 */
72 #ifndef LWIP_DNS_API_HOSTENT_STORAGE
73 #define LWIP_DNS_API_HOSTENT_STORAGE 0
74 #endif
75
76 /* define "hostent" variables storage */
77 #if LWIP_DNS_API_HOSTENT_STORAGE
78 #define HOSTENT_STORAGE
79 #else
80 #define HOSTENT_STORAGE static
81 #endif /* LWIP_DNS_API_STATIC_HOSTENT */
82
83 /**
84 * Returns an entry containing addresses of address family AF_INET
85 * for the host with name name.
86 * Due to dns_gethostbyname limitations, only one address is returned.
87 *
88 * @param name the hostname to resolve
89 * @return an entry containing addresses of address family AF_INET
90 * for the host with name name
91 */
92 struct hostent *
lwip_gethostbyname(const char * name)93 lwip_gethostbyname(const char *name)
94 {
95 err_t err;
96 ip_addr_t addr;
97
98 /* buffer variables for lwip_gethostbyname() */
99 HOSTENT_STORAGE struct hostent s_hostent;
100 HOSTENT_STORAGE char *s_aliases;
101 HOSTENT_STORAGE ip_addr_t s_hostent_addr;
102 HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
103 HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
104
105 /* query host IP address */
106 err = netconn_gethostbyname(name, &addr);
107 if (err != ERR_OK) {
108 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
109 h_errno = HOST_NOT_FOUND;
110 return NULL;
111 }
112
113 /* fill hostent */
114 s_hostent_addr = addr;
115 s_phostent_addr[0] = &s_hostent_addr;
116 s_phostent_addr[1] = NULL;
117 strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
118 s_hostname[DNS_MAX_NAME_LENGTH] = 0;
119 s_hostent.h_name = s_hostname;
120 s_aliases = NULL;
121 s_hostent.h_aliases = &s_aliases;
122 s_hostent.h_addrtype = AF_INET;
123 s_hostent.h_length = sizeof(ip_addr_t);
124 s_hostent.h_addr_list = (char **)&s_phostent_addr;
125
126 #if DNS_DEBUG
127 /* dump hostent */
128 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
129 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", (void *)s_hostent.h_aliases));
130 /* h_aliases are always empty */
131 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
132 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
133 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", (void *)s_hostent.h_addr_list));
134 if (s_hostent.h_addr_list != NULL) {
135 u8_t idx;
136 for (idx = 0; s_hostent.h_addr_list[idx]; idx++) {
137 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa(s_phostent_addr[idx])));
138 }
139 }
140 #endif /* DNS_DEBUG */
141
142 #if LWIP_DNS_API_HOSTENT_STORAGE
143 /* this function should return the "per-thread" hostent after copy from s_hostent */
144 return sys_thread_hostent(&s_hostent);
145 #else
146 return &s_hostent;
147 #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
148 }
149
150 /**
151 * Thread-safe variant of lwip_gethostbyname: instead of using a static
152 * buffer, this function takes buffer and errno pointers as arguments
153 * and uses these for the result.
154 *
155 * @param name the hostname to resolve
156 * @param ret pre-allocated struct where to store the result
157 * @param buf pre-allocated buffer where to store additional data
158 * @param buflen the size of buf
159 * @param result pointer to a hostent pointer that is set to ret on success
160 * and set to zero on error
161 * @param h_errnop pointer to an int where to store errors (instead of modifying
162 * the global h_errno)
163 * @return 0 on success, non-zero on error, additional error information
164 * is stored in *h_errnop instead of h_errno to be thread-safe
165 */
166 int
lwip_gethostbyname_r(const char * name,struct hostent * ret,char * buf,size_t buflen,struct hostent ** result,int * h_errnop)167 lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
168 size_t buflen, struct hostent **result, int *h_errnop)
169 {
170 err_t err;
171 struct gethostbyname_r_helper *h;
172 char *hostname;
173 size_t namelen;
174 int lh_errno;
175
176 if (h_errnop == NULL) {
177 /* ensure h_errnop is never NULL */
178 h_errnop = &lh_errno;
179 }
180
181 if (result == NULL) {
182 /* not all arguments given */
183 *h_errnop = EINVAL;
184 return -1;
185 }
186 /* first thing to do: set *result to nothing */
187 *result = NULL;
188 if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
189 /* not all arguments given */
190 *h_errnop = EINVAL;
191 return -1;
192 }
193
194 namelen = strlen(name);
195 if (buflen < (sizeof(struct gethostbyname_r_helper) + LWIP_MEM_ALIGN_BUFFER(namelen + 1))) {
196 /* buf can't hold the data needed + a copy of name */
197 *h_errnop = ERANGE;
198 return -1;
199 }
200
201 h = (struct gethostbyname_r_helper *)LWIP_MEM_ALIGN(buf);
202 hostname = ((char *)h) + sizeof(struct gethostbyname_r_helper);
203
204 /* query host IP address */
205 err = netconn_gethostbyname(name, &h->addr);
206 if (err != ERR_OK) {
207 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
208 *h_errnop = HOST_NOT_FOUND;
209 return -1;
210 }
211
212 /* copy the hostname into buf */
213 MEMCPY(hostname, name, namelen);
214 hostname[namelen] = 0;
215
216 /* fill hostent */
217 h->addr_list[0] = &h->addr;
218 h->addr_list[1] = NULL;
219 h->aliases = NULL;
220 ret->h_name = hostname;
221 ret->h_aliases = &h->aliases;
222 ret->h_addrtype = AF_INET;
223 ret->h_length = sizeof(ip_addr_t);
224 ret->h_addr_list = (char **)&h->addr_list;
225
226 /* set result != NULL */
227 *result = ret;
228
229 /* return success */
230 return 0;
231 }
232
233 /**
234 * Frees one or more addrinfo structures returned by getaddrinfo(), along with
235 * any additional storage associated with those structures. If the ai_next field
236 * of the structure is not null, the entire list of structures is freed.
237 *
238 * @param ai struct addrinfo to free
239 */
240 void
lwip_freeaddrinfo(struct addrinfo * ai)241 lwip_freeaddrinfo(struct addrinfo *ai)
242 {
243 struct addrinfo *next;
244
245 while (ai != NULL) {
246 next = ai->ai_next;
247 memp_free(MEMP_NETDB, ai);
248 ai = next;
249 }
250 }
251
252 /**
253 * Translates the name of a service location (for example, a host name) and/or
254 * a service name and returns a set of socket addresses and associated
255 * information to be used in creating a socket with which to address the
256 * specified service.
257 * Memory for the result is allocated internally and must be freed by calling
258 * lwip_freeaddrinfo()!
259 *
260 * Due to a limitation in dns_gethostbyname, only the first address of a
261 * host is returned.
262 * Also, service names are not supported (only port numbers)!
263 *
264 * @param nodename descriptive name or address string of the host
265 * (may be NULL -> local address)
266 * @param servname port number as string of NULL
267 * @param hints structure containing input values that set socktype and protocol
268 * @param res pointer to a pointer where to store the result (set to NULL on failure)
269 * @return 0 on success, non-zero on failure
270 *
271 * @todo: implement AI_V4MAPPED, AI_ADDRCONFIG
272 */
273 int
lwip_getaddrinfo(const char * nodename,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)274 lwip_getaddrinfo(const char *nodename, const char *servname,
275 const struct addrinfo *hints, struct addrinfo **res)
276 {
277 err_t err;
278 ip_addr_t addr;
279 struct addrinfo *ai;
280 struct sockaddr_storage *sa = NULL;
281 int port_nr = 0;
282 size_t total_size;
283 size_t namelen = 0;
284 int ai_family;
285
286 if (res == NULL) {
287 return EAI_FAIL;
288 }
289 *res = NULL;
290 if ((nodename == NULL) && (servname == NULL)) {
291 return EAI_NONAME;
292 }
293
294 if (hints != NULL) {
295 ai_family = hints->ai_family;
296 if ((ai_family != AF_UNSPEC)
297 #if LWIP_IPV4
298 && (ai_family != AF_INET)
299 #endif /* LWIP_IPV4 */
300 #if LWIP_IPV6
301 && (ai_family != AF_INET6)
302 #endif /* LWIP_IPV6 */
303 ) {
304 return EAI_FAMILY;
305 }
306 } else {
307 ai_family = AF_UNSPEC;
308 }
309
310 if (servname != NULL) {
311 /* service name specified: convert to port number
312 * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
313 port_nr = atoi(servname);
314 if (port_nr == 0 && (servname[0] != '0')) {
315 /* atoi failed - service was not numeric */
316 return EAI_SERVICE;
317 }
318 if ((port_nr < 0) || (port_nr > 0xffff)) {
319 return EAI_SERVICE;
320 }
321 }
322
323 if (nodename != NULL) {
324 /* service location specified, try to resolve */
325 if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
326 /* no DNS lookup, just parse for an address string */
327 if (!ipaddr_aton(nodename, &addr)) {
328 return EAI_NONAME;
329 }
330 #if LWIP_IPV4 && LWIP_IPV6
331 if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
332 (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
333 return EAI_NONAME;
334 }
335 #endif /* LWIP_IPV4 && LWIP_IPV6 */
336 } else {
337 #if LWIP_IPV4 && LWIP_IPV6
338 /* AF_UNSPEC: prefer IPv4 */
339 u8_t type = NETCONN_DNS_IPV4_IPV6;
340 if (ai_family == AF_INET) {
341 type = NETCONN_DNS_IPV4;
342 } else if (ai_family == AF_INET6) {
343 type = NETCONN_DNS_IPV6;
344 }
345 #endif /* LWIP_IPV4 && LWIP_IPV6 */
346 err = netconn_gethostbyname_addrtype(nodename, &addr, type);
347 if (err != ERR_OK) {
348 return EAI_FAIL;
349 }
350 }
351 } else {
352 /* service location specified, use loopback address */
353 if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
354 ip_addr_set_any_val(ai_family == AF_INET6, addr);
355 } else {
356 ip_addr_set_loopback_val(ai_family == AF_INET6, addr);
357 }
358 }
359
360 total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
361 if (nodename != NULL) {
362 namelen = strlen(nodename);
363 if (namelen > DNS_MAX_NAME_LENGTH) {
364 /* invalid name length */
365 return EAI_FAIL;
366 }
367 LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
368 total_size += namelen + 1;
369 }
370 /* If this fails, please report to lwip-devel! :-) */
371 LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
372 total_size <= NETDB_ELEM_SIZE);
373 ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
374 if (ai == NULL) {
375 return EAI_MEMORY;
376 }
377 memset(ai, 0, total_size);
378 /* cast through void* to get rid of alignment warnings */
379 sa = (struct sockaddr_storage *)(void *)((u8_t *)ai + sizeof(struct addrinfo));
380 if (IP_IS_V6_VAL(addr)) {
381 #if LWIP_IPV6
382 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
383 /* set up sockaddr */
384 inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
385 sa6->sin6_family = AF_INET6;
386 #if LWIP_SOCKET_HAVE_SA_LEN
387 sa6->sin6_len = sizeof(struct sockaddr_in6);
388 #endif /* LWIP_SOCKET_HAVE_SA_LEN */
389 sa6->sin6_port = lwip_htons((u16_t)port_nr);
390 sa6->sin6_scope_id = ip6_addr_zone(ip_2_ip6(&addr));
391 ai->ai_family = AF_INET6;
392 ai->ai_addrlen = sizeof(struct sockaddr_in6);
393 #endif /* LWIP_IPV6 */
394 } else {
395 #if LWIP_IPV4
396 struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
397 /* set up sockaddr */
398 inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
399 sa4->sin_family = AF_INET;
400 #if LWIP_SOCKET_HAVE_SA_LEN
401 sa4->sin_len = sizeof(struct sockaddr_in);
402 #endif /* LWIP_SOCKET_HAVE_SA_LEN */
403 sa4->sin_port = lwip_htons((u16_t)port_nr);
404 ai->ai_family = AF_INET;
405 ai->ai_addrlen = sizeof(struct sockaddr_in);
406 #endif /* LWIP_IPV4 */
407 }
408
409 /* set up addrinfo */
410 if (hints != NULL) {
411 /* copy socktype & protocol from hints if specified */
412 ai->ai_socktype = hints->ai_socktype;
413 ai->ai_protocol = hints->ai_protocol;
414 }
415 if (nodename != NULL) {
416 /* copy nodename to canonname if specified */
417 ai->ai_canonname = ((char *)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
418 MEMCPY(ai->ai_canonname, nodename, namelen);
419 ai->ai_canonname[namelen] = 0;
420 }
421 ai->ai_addr = (struct sockaddr *)sa;
422
423 *res = ai;
424
425 return 0;
426 }
427
428 #endif /* LWIP_DNS && LWIP_SOCKET */
429