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> /* atoi */
70 
72 struct 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
80 int 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 
105 struct hostent*
106 lwip_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 
180 int
181 lwip_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 
254 void
255 lwip_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 
287 int
288 lwip_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  port_nr = atoi(servname);
328  if ((port_nr <= 0) || (port_nr > 0xffff)) {
329  return EAI_SERVICE;
330  }
331  }
332 
333  if (nodename != NULL) {
334  /* service location specified, try to resolve */
335  if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
336  /* no DNS lookup, just parse for an address string */
337  if (!ipaddr_aton(nodename, &addr)) {
338  return EAI_NONAME;
339  }
340 #if LWIP_IPV4 && LWIP_IPV6
341  if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
342  (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
343  return EAI_NONAME;
344  }
345 #endif /* LWIP_IPV4 && LWIP_IPV6 */
346  } else {
347 #if LWIP_IPV4 && LWIP_IPV6
348  /* AF_UNSPEC: prefer IPv4 */
349  u8_t type = NETCONN_DNS_IPV4_IPV6;
350  if (ai_family == AF_INET) {
351  type = NETCONN_DNS_IPV4;
352  } else if (ai_family == AF_INET6) {
353  type = NETCONN_DNS_IPV6;
354  }
355 #endif /* LWIP_IPV4 && LWIP_IPV6 */
356  err = netconn_gethostbyname_addrtype(nodename, &addr, type);
357  if (err != ERR_OK) {
358  return EAI_FAIL;
359  }
360  }
361  } else {
362  /* service location specified, use loopback address */
363  if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
364  ip_addr_set_any(ai_family == AF_INET6, &addr);
365  } else {
366  ip_addr_set_loopback(ai_family == AF_INET6, &addr);
367  }
368  }
369 
370  total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
371  if (nodename != NULL) {
372  namelen = strlen(nodename);
373  if (namelen > DNS_MAX_NAME_LENGTH) {
374  /* invalid name length */
375  return EAI_FAIL;
376  }
377  LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
378  total_size += namelen + 1;
379  }
380  /* If this fails, please report to lwip-devel! :-) */
381  LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
382  total_size <= NETDB_ELEM_SIZE);
383  ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
384  if (ai == NULL) {
385  return EAI_MEMORY;
386  }
387  memset(ai, 0, total_size);
388  /* cast through void* to get rid of alignment warnings */
389  sa = (struct sockaddr_storage *)(void*)((u8_t*)ai + sizeof(struct addrinfo));
390  if (IP_IS_V6_VAL(addr)) {
391 #if LWIP_IPV6
392  struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)sa;
393  /* set up sockaddr */
394  inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
395  sa6->sin6_family = AF_INET6;
396  sa6->sin6_len = sizeof(struct sockaddr_in6);
397  sa6->sin6_port = lwip_htons((u16_t)port_nr);
398  ai->ai_family = AF_INET6;
399 #endif /* LWIP_IPV6 */
400  } else {
401 #if LWIP_IPV4
402  struct sockaddr_in *sa4 = (struct sockaddr_in*)sa;
403  /* set up sockaddr */
404  inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
405  sa4->sin_family = AF_INET;
406  sa4->sin_len = sizeof(struct sockaddr_in);
407  sa4->sin_port = lwip_htons((u16_t)port_nr);
408  ai->ai_family = AF_INET;
409 #endif /* LWIP_IPV4 */
410  }
411 
412  /* set up addrinfo */
413  if (hints != NULL) {
414  /* copy socktype & protocol from hints if specified */
415  ai->ai_socktype = hints->ai_socktype;
416  ai->ai_protocol = hints->ai_protocol;
417  }
418  if (nodename != NULL) {
419  /* copy nodename to canonname if specified */
420  ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
421  MEMCPY(ai->ai_canonname, nodename, namelen);
422  ai->ai_canonname[namelen] = 0;
423  }
424  ai->ai_addrlen = sizeof(struct sockaddr_storage);
425  ai->ai_addr = (struct sockaddr*)sa;
426 
427  *res = ai;
428 
429  return 0;
430 }
431 
432 #endif /* LWIP_DNS && LWIP_SOCKET */
#define LWIP_IPV4
Definition: opt.h:659
#define DNS_DEBUG
Definition: opt.h:2861
void memp_free(memp_t type, void *mem)
Definition: memp.c:488
#define DNS_MAX_NAME_LENGTH
Definition: opt.h:1040
s8_t err_t
Definition: err.h:76
#define LWIP_DEBUGF(debug, message)
#define LWIP_MEM_ALIGN(addr)
Definition: arch.h:248
Definition: err.h:82
void * memp_malloc(memp_t type)
Definition: memp.c:404