The Pedigree Project 0.1
netdb.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
28/*
29 * Redistribution and use in source and binary forms, with or without modification,
30 * are permitted provided that the following conditions are met:
31 *
32 * 1. Redistributions of source code must retain the above copyright notice,
33 * this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright notice,
35 * this list of conditions and the following disclaimer in the documentation
36 * and/or other materials provided with the distribution.
37 * 3. The name of the author may not be used to endorse or promote products
38 * derived from this software without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
43 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
45 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
48 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
49 * OF SUCH DAMAGE.
50 *
51 * This file is part of the lwIP TCP/IP stack.
52 *
53 * Author: Simon Goldschmidt
54 *
55 */
56
57#include "lwip/netdb.h"
58
59#if LWIP_DNS && LWIP_SOCKET
60
61#include "lwip/err.h"
62#include "lwip/mem.h"
63#include "lwip/memp.h"
64#include "lwip/ip_addr.h"
65#include "lwip/api.h"
66#include "lwip/dns.h"
67
68#include <string.h> /* memset */
69#include <stdlib.h> /* strtoul */
70
72struct gethostbyname_r_helper {
73 ip_addr_t *addr_list[2];
74 ip_addr_t addr;
75 char *aliases;
76};
77
79#if LWIP_DNS_API_DECLARE_H_ERRNO
80int h_errno;
81#endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
82
85#ifndef LWIP_DNS_API_HOSTENT_STORAGE
86#define LWIP_DNS_API_HOSTENT_STORAGE 0
87#endif
88
90#if LWIP_DNS_API_HOSTENT_STORAGE
91#define HOSTENT_STORAGE
92#else
93#define HOSTENT_STORAGE static
94#endif /* LWIP_DNS_API_STATIC_HOSTENT */
95
105struct hostent*
106lwip_gethostbyname(const char *name)
107{
108 err_t err;
109 ip_addr_t addr;
110
111 /* buffer variables for lwip_gethostbyname() */
112 HOSTENT_STORAGE struct hostent s_hostent;
113 HOSTENT_STORAGE char *s_aliases;
114 HOSTENT_STORAGE ip_addr_t s_hostent_addr;
115 HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
116 HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
117
118 /* query host IP address */
119 err = netconn_gethostbyname(name, &addr);
120 if (err != ERR_OK) {
121 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
122 h_errno = HOST_NOT_FOUND;
123 return NULL;
124 }
125
126 /* fill hostent */
127 s_hostent_addr = addr;
128 s_phostent_addr[0] = &s_hostent_addr;
129 s_phostent_addr[1] = NULL;
130 strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
131 s_hostname[DNS_MAX_NAME_LENGTH] = 0;
132 s_hostent.h_name = s_hostname;
133 s_aliases = NULL;
134 s_hostent.h_aliases = &s_aliases;
135 s_hostent.h_addrtype = AF_INET;
136 s_hostent.h_length = sizeof(ip_addr_t);
137 s_hostent.h_addr_list = (char**)&s_phostent_addr;
138
139#if DNS_DEBUG
140 /* dump hostent */
141 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
142 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", (void*)s_hostent.h_aliases));
143 /* h_aliases are always empty */
144 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
145 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
146 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", (void*)s_hostent.h_addr_list));
147 if (s_hostent.h_addr_list != NULL) {
148 u8_t idx;
149 for (idx=0; s_hostent.h_addr_list[idx]; idx++) {
150 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i] == %p\n", idx, s_hostent.h_addr_list[idx]));
151 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t*)s_hostent.h_addr_list[idx])));
152 }
153 }
154#endif /* DNS_DEBUG */
155
156#if LWIP_DNS_API_HOSTENT_STORAGE
157 /* this function should return the "per-thread" hostent after copy from s_hostent */
158 return sys_thread_hostent(&s_hostent);
159#else
160 return &s_hostent;
161#endif /* LWIP_DNS_API_HOSTENT_STORAGE */
162}
163
180int
181lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
182 size_t buflen, struct hostent **result, int *h_errnop)
183{
184 err_t err;
185 struct gethostbyname_r_helper *h;
186 char *hostname;
187 size_t namelen;
188 int lh_errno;
189
190 if (h_errnop == NULL) {
191 /* ensure h_errnop is never NULL */
192 h_errnop = &lh_errno;
193 }
194
195 if (result == NULL) {
196 /* not all arguments given */
197 *h_errnop = EINVAL;
198 return -1;
199 }
200 /* first thing to do: set *result to nothing */
201 *result = NULL;
202 if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
203 /* not all arguments given */
204 *h_errnop = EINVAL;
205 return -1;
206 }
207
208 namelen = strlen(name);
209 if (buflen < (sizeof(struct gethostbyname_r_helper) + namelen + 1 + (MEM_ALIGNMENT - 1))) {
210 /* buf can't hold the data needed + a copy of name */
211 *h_errnop = ERANGE;
212 return -1;
213 }
214
215 h = (struct gethostbyname_r_helper*)LWIP_MEM_ALIGN(buf);
216 hostname = ((char*)h) + sizeof(struct gethostbyname_r_helper);
217
218 /* query host IP address */
219 err = netconn_gethostbyname(name, &h->addr);
220 if (err != ERR_OK) {
221 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
222 *h_errnop = HOST_NOT_FOUND;
223 return -1;
224 }
225
226 /* copy the hostname into buf */
227 MEMCPY(hostname, name, namelen);
228 hostname[namelen] = 0;
229
230 /* fill hostent */
231 h->addr_list[0] = &h->addr;
232 h->addr_list[1] = NULL;
233 h->aliases = NULL;
234 ret->h_name = hostname;
235 ret->h_aliases = &h->aliases;
236 ret->h_addrtype = AF_INET;
237 ret->h_length = sizeof(ip_addr_t);
238 ret->h_addr_list = (char**)&h->addr_list;
239
240 /* set result != NULL */
241 *result = ret;
242
243 /* return success */
244 return 0;
245}
246
254void
255lwip_freeaddrinfo(struct addrinfo *ai)
256{
257 struct addrinfo *next;
258
259 while (ai != NULL) {
260 next = ai->ai_next;
261 memp_free(MEMP_NETDB, ai);
262 ai = next;
263 }
264}
265
287int
288lwip_getaddrinfo(const char *nodename, const char *servname,
289 const struct addrinfo *hints, struct addrinfo **res)
290{
291 err_t err;
292 ip_addr_t addr;
293 struct addrinfo *ai;
294 struct sockaddr_storage *sa = NULL;
295 int port_nr = 0;
296 size_t total_size;
297 size_t namelen = 0;
298 int ai_family;
299
300 if (res == NULL) {
301 return EAI_FAIL;
302 }
303 *res = NULL;
304 if ((nodename == NULL) && (servname == NULL)) {
305 return EAI_NONAME;
306 }
307
308 if (hints != NULL) {
309 ai_family = hints->ai_family;
310 if ((ai_family != AF_UNSPEC)
311#if LWIP_IPV4
312 && (ai_family != AF_INET)
313#endif /* LWIP_IPV4 */
314#if LWIP_IPV6
315 && (ai_family != AF_INET6)
316#endif /* LWIP_IPV6 */
317 ) {
318 return EAI_FAMILY;
319 }
320 } else {
321 ai_family = AF_UNSPEC;
322 }
323
324 if (servname != NULL) {
325 /* service name specified: convert to port number
326 * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
327 char *end;
328 unsigned long parsed_port = strtoul(servname, &end, 10);
329 if ((end == servname) || (*end != '\0') || (parsed_port == 0) || (parsed_port > 0xffff)) {
330 return EAI_SERVICE;
331 }
332 port_nr = (int)parsed_port;
333 }
334
335 if (nodename != NULL) {
336 /* service location specified, try to resolve */
337 if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
338 /* no DNS lookup, just parse for an address string */
339 if (!ipaddr_aton(nodename, &addr)) {
340 return EAI_NONAME;
341 }
342#if LWIP_IPV4 && LWIP_IPV6
343 if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
344 (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
345 return EAI_NONAME;
346 }
347#endif /* LWIP_IPV4 && LWIP_IPV6 */
348 } else {
349#if LWIP_IPV4 && LWIP_IPV6
350 /* AF_UNSPEC: prefer IPv4 */
351 u8_t type = NETCONN_DNS_IPV4_IPV6;
352 if (ai_family == AF_INET) {
353 type = NETCONN_DNS_IPV4;
354 } else if (ai_family == AF_INET6) {
355 type = NETCONN_DNS_IPV6;
356 }
357#endif /* LWIP_IPV4 && LWIP_IPV6 */
358 err = netconn_gethostbyname_addrtype(nodename, &addr, type);
359 if (err != ERR_OK) {
360 return EAI_FAIL;
361 }
362 }
363 } else {
364 /* service location specified, use loopback address */
365 if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
366 ip_addr_set_any(ai_family == AF_INET6, &addr);
367 } else {
368 ip_addr_set_loopback(ai_family == AF_INET6, &addr);
369 }
370 }
371
372 total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
373 if (nodename != NULL) {
374 namelen = strlen(nodename);
375 if (namelen > DNS_MAX_NAME_LENGTH) {
376 /* invalid name length */
377 return EAI_FAIL;
378 }
379 LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
380 total_size += namelen + 1;
381 }
382 /* If this fails, please report to lwip-devel! :-) */
383 LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
384 total_size <= NETDB_ELEM_SIZE);
385 ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
386 if (ai == NULL) {
387 return EAI_MEMORY;
388 }
389 memset(ai, 0, total_size);
390 /* cast through void* to get rid of alignment warnings */
391 sa = (struct sockaddr_storage *)(void*)((u8_t*)ai + sizeof(struct addrinfo));
392 if (IP_IS_V6_VAL(addr)) {
393#if LWIP_IPV6
394 struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)sa;
395 /* set up sockaddr */
396 inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
397 sa6->sin6_family = AF_INET6;
398 sa6->sin6_len = sizeof(struct sockaddr_in6);
399 sa6->sin6_port = lwip_htons((u16_t)port_nr);
400 ai->ai_family = AF_INET6;
401#endif /* LWIP_IPV6 */
402 } else {
403#if LWIP_IPV4
404 struct sockaddr_in *sa4 = (struct sockaddr_in*)sa;
405 /* set up sockaddr */
406 inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
407 sa4->sin_family = AF_INET;
408 sa4->sin_len = sizeof(struct sockaddr_in);
409 sa4->sin_port = lwip_htons((u16_t)port_nr);
410 ai->ai_family = AF_INET;
411#endif /* LWIP_IPV4 */
412 }
413
414 /* set up addrinfo */
415 if (hints != NULL) {
416 /* copy socktype & protocol from hints if specified */
417 ai->ai_socktype = hints->ai_socktype;
418 ai->ai_protocol = hints->ai_protocol;
419 }
420 if (nodename != NULL) {
421 /* copy nodename to canonname if specified */
422 ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
423 MEMCPY(ai->ai_canonname, nodename, namelen);
424 ai->ai_canonname[namelen] = 0;
425 }
426 ai->ai_addrlen = sizeof(struct sockaddr_storage);
427 ai->ai_addr = (struct sockaddr*)sa;
428
429 *res = ai;
430
431 return 0;
432}
433
434#endif /* LWIP_DNS && LWIP_SOCKET */
#define LWIP_DEBUGF(debug, message)
Definition debug.h:183
#define LWIP_MEM_ALIGN(addr)
Definition arch.h:248
s8_t err_t
Definition err.h:76
@ ERR_OK
Definition err.h:82
#define DNS_DEBUG
Definition opt.h:2861
#define DNS_MAX_NAME_LENGTH
Definition opt.h:1040
#define LWIP_IPV4
Definition opt.h:659
void * memp_malloc(memp_t type)
Definition memp.c:404
void memp_free(memp_t type, void *mem)
Definition memp.c:488