]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/contrib/src/api/netdb.c
29ccbc58c3f02851b218b9c5fbecddcc6d48ac75
[l4.git] / l4 / pkg / ankh / lib / lwip / contrib / src / api / netdb.c
1 /**
2  * @file
3  * API functions for name resolving
4  *
5  */
6
7 /*
8  * Redistribution and use in source and binary forms, with or without modification, 
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission. 
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the lwIP TCP/IP stack.
31  * 
32  * Author: Simon Goldschmidt
33  *
34  */
35
36 #include "lwip/netdb.h"
37
38 #if LWIP_DNS && LWIP_SOCKET
39
40 #include "lwip/err.h"
41 #include "lwip/mem.h"
42 #include "lwip/memp.h"
43 #include "lwip/ip_addr.h"
44 #include "lwip/api.h"
45 #include "lwip/dns.h"
46
47 #include <string.h>
48 #include <stdlib.h>
49
50 /** helper struct for gethostbyname_r to access the char* buffer */
51 struct gethostbyname_r_helper {
52   ip_addr_t *addrs;
53   ip_addr_t addr;
54   char *aliases;
55 };
56
57 /** h_errno is exported in netdb.h for access by applications. */
58 #if LWIP_DNS_API_DECLARE_H_ERRNO
59 int h_errno;
60 #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
61
62 /** define "hostent" variables storage: 0 if we use a static (but unprotected)
63  * set of variables for lwip_gethostbyname, 1 if we use a local storage */
64 #ifndef LWIP_DNS_API_HOSTENT_STORAGE
65 #define LWIP_DNS_API_HOSTENT_STORAGE 0
66 #endif
67
68 /** define "hostent" variables storage */
69 #if LWIP_DNS_API_HOSTENT_STORAGE
70 #define HOSTENT_STORAGE
71 #else
72 #define HOSTENT_STORAGE static
73 #endif /* LWIP_DNS_API_STATIC_HOSTENT */
74
75 /**
76  * Returns an entry containing addresses of address family AF_INET
77  * for the host with name name.
78  * Due to dns_gethostbyname limitations, only one address is returned.
79  *
80  * @param name the hostname to resolve
81  * @return an entry containing addresses of address family AF_INET
82  *         for the host with name name
83  */
84 struct hostent*
85 lwip_gethostbyname(const char *name)
86 {
87   err_t err;
88   ip_addr_t addr;
89
90   /* buffer variables for lwip_gethostbyname() */
91   HOSTENT_STORAGE struct hostent s_hostent;
92   HOSTENT_STORAGE char *s_aliases;
93   HOSTENT_STORAGE ip_addr_t s_hostent_addr;
94   HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2] = { 0 };
95
96   /* query host IP address */
97   err = netconn_gethostbyname(name, &addr);
98   if (err != ERR_OK) {
99     LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
100     h_errno = HOST_NOT_FOUND;
101     return NULL;
102   }
103
104   /* fill hostent */
105   s_hostent_addr = addr;
106   s_phostent_addr[0] = &s_hostent_addr;
107   s_hostent.h_name = (char*)name;
108   s_hostent.h_aliases = &s_aliases;
109   s_hostent.h_addrtype = AF_INET;
110   s_hostent.h_length = sizeof(ip_addr_t);
111   s_hostent.h_addr_list = (char**)&s_phostent_addr;
112
113 #if DNS_DEBUG
114   /* dump hostent */
115   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name           == %s\n", s_hostent.h_name));
116   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases        == %p\n", s_hostent.h_aliases));
117   if (s_hostent.h_aliases != NULL) {
118     u8_t idx;
119     for ( idx=0; s_hostent.h_aliases[idx]; idx++) {
120       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]->   == %p\n", idx, s_hostent.h_aliases[idx]));
121       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]->   == %s\n",      idx, s_hostent.h_aliases[idx]));
122     }
123   }
124   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype       == %d\n", s_hostent.h_addrtype));
125   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length         == %d\n", s_hostent.h_length));
126   LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list      == %p\n", s_hostent.h_addr_list));
127   if (s_hostent.h_addr_list != NULL) {
128     u8_t idx;
129     for ( idx=0; s_hostent.h_addr_list[idx]; idx++) {
130       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]   == %p\n", idx, s_hostent.h_addr_list[idx]));
131       LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ip_ntoa((ip_addr_t*)s_hostent.h_addr_list[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
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 == 0)) {
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) + namelen + 1 + (MEM_ALIGNMENT - 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 = ENSRNOTFOUND;
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->addrs = &(h->addr);
212   h->aliases = NULL;
213   ret->h_name = (char*)hostname;
214   ret->h_aliases = &(h->aliases);
215   ret->h_addrtype = AF_INET;
216   ret->h_length = sizeof(ip_addr_t);
217   ret->h_addr_list = (char**)&(h->addrs);
218
219   /* set result != NULL */
220   *result = ret;
221
222   /* return success */
223   return 0;
224 }
225
226 /**
227  * Frees one or more addrinfo structures returned by getaddrinfo(), along with
228  * any additional storage associated with those structures. If the ai_next field
229  * of the structure is not null, the entire list of structures is freed.
230  *
231  * @param ai struct addrinfo to free
232  */
233 void
234 lwip_freeaddrinfo(struct addrinfo *ai)
235 {
236   struct addrinfo *next;
237
238   while (ai != NULL) {
239     next = ai->ai_next;
240     memp_free(MEMP_NETDB, ai);
241     ai = next;
242   }
243 }
244
245 /**
246  * Translates the name of a service location (for example, a host name) and/or
247  * a service name and returns a set of socket addresses and associated
248  * information to be used in creating a socket with which to address the
249  * specified service.
250  * Memory for the result is allocated internally and must be freed by calling
251  * lwip_freeaddrinfo()!
252  *
253  * Due to a limitation in dns_gethostbyname, only the first address of a
254  * host is returned.
255  * Also, service names are not supported (only port numbers)!
256  *
257  * @param nodename descriptive name or address string of the host
258  *                 (may be NULL -> local address)
259  * @param servname port number as string of NULL 
260  * @param hints structure containing input values that set socktype and protocol
261  * @param res pointer to a pointer where to store the result (set to NULL on failure)
262  * @return 0 on success, non-zero on failure
263  */
264 int
265 lwip_getaddrinfo(const char *nodename, const char *servname,
266        const struct addrinfo *hints, struct addrinfo **res)
267 {
268   err_t err;
269   ip_addr_t addr;
270   struct addrinfo *ai;
271   struct sockaddr_in *sa = NULL;
272   int port_nr = 0;
273   size_t total_size;
274   size_t namelen = 0;
275
276   if (res == NULL) {
277     return EAI_FAIL;
278   }
279   *res = NULL;
280   if ((nodename == NULL) && (servname == NULL)) {
281     return EAI_NONAME;
282   }
283
284   if (servname != NULL) {
285     /* service name specified: convert to port number
286      * @todo?: currently, only ASCII integers (port numbers) are supported! */
287     port_nr = atoi(servname);
288     if ((port_nr <= 0) || (port_nr > 0xffff)) {
289       return EAI_SERVICE;
290     }
291   }
292
293   if (nodename != NULL) {
294     /* service location specified, try to resolve */
295     err = netconn_gethostbyname(nodename, &addr);
296     if (err != ERR_OK) {
297       return EAI_FAIL;
298     }
299   } else {
300     /* service location specified, use loopback address */
301     ip_addr_set_loopback(&addr);
302   }
303
304   total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_in);
305   if (nodename != NULL) {
306     namelen = strlen(nodename);
307     LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
308     total_size += namelen + 1;
309   }
310   /* If this fails, please report to lwip-devel! :-) */
311   LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
312     total_size <= NETDB_ELEM_SIZE);
313   ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
314   if (ai == NULL) {
315     goto memerr;
316   }
317   memset(ai, 0, total_size);
318   sa = (struct sockaddr_in*)((u8_t*)ai + sizeof(struct addrinfo));
319   /* set up sockaddr */
320   inet_addr_from_ipaddr(&sa->sin_addr, &addr);
321   sa->sin_family = AF_INET;
322   sa->sin_len = sizeof(struct sockaddr_in);
323   sa->sin_port = htons((u16_t)port_nr);
324
325   /* set up addrinfo */
326   ai->ai_family = AF_INET;
327   if (hints != NULL) {
328     /* copy socktype & protocol from hints if specified */
329     ai->ai_socktype = hints->ai_socktype;
330     ai->ai_protocol = hints->ai_protocol;
331   }
332   if (nodename != NULL) {
333     /* copy nodename to canonname if specified */
334     ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
335     MEMCPY(ai->ai_canonname, nodename, namelen);
336     ai->ai_canonname[namelen] = 0;
337   }
338   ai->ai_addrlen = sizeof(struct sockaddr_in);
339   ai->ai_addr = (struct sockaddr*)sa;
340
341   *res = ai;
342
343   return 0;
344 memerr:
345   if (ai != NULL) {
346     memp_free(MEMP_NETDB, ai);
347   }
348   return EAI_MEMORY;
349 }
350
351 #endif /* LWIP_DNS && LWIP_SOCKET */