ReactOS 0.4.16-dev-927-g467dec4
netdb.c
Go to the documentation of this file.
1
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
53struct gethostbyname_r_helper {
54 ip_addr_t *addr_list[2];
56 char *aliases;
57};
58
60#if LWIP_DNS_API_DECLARE_H_ERRNO
61int h_errno;
62#endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
63
71#ifndef LWIP_DNS_API_HOSTENT_STORAGE
72#define LWIP_DNS_API_HOSTENT_STORAGE 0
73#endif
74
75/* define "hostent" variables storage */
76#if LWIP_DNS_API_HOSTENT_STORAGE
77#define HOSTENT_STORAGE
78#else
79#define HOSTENT_STORAGE static
80#endif /* LWIP_DNS_API_STATIC_HOSTENT */
81
91struct hostent *
92lwip_gethostbyname(const char *name)
93{
94 err_t err;
96
97 /* buffer variables for lwip_gethostbyname() */
98 HOSTENT_STORAGE struct hostent s_hostent;
99 HOSTENT_STORAGE char *s_aliases;
100 HOSTENT_STORAGE ip_addr_t s_hostent_addr;
101 HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
102 HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
103
104 /* query host IP address */
105 err = netconn_gethostbyname(name, &addr);
106 if (err != ERR_OK) {
107 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
109 return NULL;
110 }
111
112 /* fill hostent */
113 s_hostent_addr = addr;
114 s_phostent_addr[0] = &s_hostent_addr;
115 s_phostent_addr[1] = NULL;
116 strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
117 s_hostname[DNS_MAX_NAME_LENGTH] = 0;
118 s_hostent.h_name = s_hostname;
119 s_aliases = NULL;
120 s_hostent.h_aliases = &s_aliases;
121 s_hostent.h_addrtype = AF_INET;
122 s_hostent.h_length = sizeof(ip_addr_t);
123 s_hostent.h_addr_list = (char **)&s_phostent_addr;
124
125#if DNS_DEBUG
126 /* dump hostent */
127 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
128 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", (void *)s_hostent.h_aliases));
129 /* h_aliases are always empty */
130 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
131 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
132 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", (void *)s_hostent.h_addr_list));
133 if (s_hostent.h_addr_list != NULL) {
134 u8_t idx;
135 for (idx = 0; s_hostent.h_addr_list[idx]; idx++) {
136 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa(s_phostent_addr[idx])));
137 }
138 }
139#endif /* DNS_DEBUG */
140
141#if LWIP_DNS_API_HOSTENT_STORAGE
142 /* this function should return the "per-thread" hostent after copy from s_hostent */
143 return sys_thread_hostent(&s_hostent);
144#else
145 return &s_hostent;
146#endif /* LWIP_DNS_API_HOSTENT_STORAGE */
147}
148
165int
166lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
167 size_t buflen, struct hostent **result, int *h_errnop)
168{
169 err_t err;
170 struct gethostbyname_r_helper *h;
171 char *hostname;
172 size_t namelen;
173 int lh_errno;
174
175 if (h_errnop == NULL) {
176 /* ensure h_errnop is never NULL */
177 h_errnop = &lh_errno;
178 }
179
180 if (result == NULL) {
181 /* not all arguments given */
182 *h_errnop = EINVAL;
183 return -1;
184 }
185 /* first thing to do: set *result to nothing */
186 *result = NULL;
187 if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
188 /* not all arguments given */
189 *h_errnop = EINVAL;
190 return -1;
191 }
192
194 if (buflen < (sizeof(struct gethostbyname_r_helper) + LWIP_MEM_ALIGN_BUFFER(namelen + 1))) {
195 /* buf can't hold the data needed + a copy of name */
196 *h_errnop = ERANGE;
197 return -1;
198 }
199
200 h = (struct gethostbyname_r_helper *)LWIP_MEM_ALIGN(buf);
201 hostname = ((char *)h) + sizeof(struct gethostbyname_r_helper);
202
203 /* query host IP address */
204 err = netconn_gethostbyname(name, &h->addr);
205 if (err != ERR_OK) {
206 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
207 *h_errnop = HOST_NOT_FOUND;
208 return -1;
209 }
210
211 /* copy the hostname into buf */
213 hostname[namelen] = 0;
214
215 /* fill hostent */
216 h->addr_list[0] = &h->addr;
217 h->addr_list[1] = NULL;
218 h->aliases = NULL;
219 ret->h_name = hostname;
220 ret->h_aliases = &h->aliases;
221 ret->h_addrtype = AF_INET;
222 ret->h_length = sizeof(ip_addr_t);
223 ret->h_addr_list = (char **)&h->addr_list;
224
225 /* set result != NULL */
226 *result = ret;
227
228 /* return success */
229 return 0;
230}
231
239void
240lwip_freeaddrinfo(struct addrinfo *ai)
241{
242 struct addrinfo *next;
243
244 while (ai != NULL) {
245 next = ai->ai_next;
246 memp_free(MEMP_NETDB, ai);
247 ai = next;
248 }
249}
250
272int
273lwip_getaddrinfo(const char *nodename, const char *servname,
274 const struct addrinfo *hints, struct addrinfo **res)
275{
276 err_t err;
278 struct addrinfo *ai;
279 struct sockaddr_storage *sa = NULL;
280 int port_nr = 0;
281 size_t total_size;
282 size_t namelen = 0;
283 int ai_family;
284
285 if (res == NULL) {
286 return EAI_FAIL;
287 }
288 *res = NULL;
289 if ((nodename == NULL) && (servname == NULL)) {
290 return EAI_NONAME;
291 }
292
293 if (hints != NULL) {
294 ai_family = hints->ai_family;
295 if ((ai_family != AF_UNSPEC)
296#if LWIP_IPV4
297 && (ai_family != AF_INET)
298#endif /* LWIP_IPV4 */
299#if LWIP_IPV6
300 && (ai_family != AF_INET6)
301#endif /* LWIP_IPV6 */
302 ) {
303 return EAI_FAMILY;
304 }
305 } else {
306 ai_family = AF_UNSPEC;
307 }
308
309 if (servname != NULL) {
310 /* service name specified: convert to port number
311 * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
312 port_nr = atoi(servname);
313 if (port_nr == 0 && (servname[0] != '0')) {
314 /* atoi failed - service was not numeric */
315 return EAI_SERVICE;
316 }
317 if ((port_nr < 0) || (port_nr > 0xffff)) {
318 return EAI_SERVICE;
319 }
320 }
321
322 if (nodename != NULL) {
323 /* service location specified, try to resolve */
324 if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
325 /* no DNS lookup, just parse for an address string */
326 if (!ipaddr_aton(nodename, &addr)) {
327 return EAI_NONAME;
328 }
329#if LWIP_IPV4 && LWIP_IPV6
330 if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
331 (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
332 return EAI_NONAME;
333 }
334#endif /* LWIP_IPV4 && LWIP_IPV6 */
335 } else {
336#if LWIP_IPV4 && LWIP_IPV6
337 /* AF_UNSPEC: prefer IPv4 */
338 u8_t type = NETCONN_DNS_IPV4_IPV6;
339 if (ai_family == AF_INET) {
340 type = NETCONN_DNS_IPV4;
341 } else if (ai_family == AF_INET6) {
342 type = NETCONN_DNS_IPV6;
343 }
344#endif /* LWIP_IPV4 && LWIP_IPV6 */
345 err = netconn_gethostbyname_addrtype(nodename, &addr, type);
346 if (err != ERR_OK) {
347 return EAI_FAIL;
348 }
349 }
350 } else {
351 /* service location specified, use loopback address */
352 if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
353 ip_addr_set_any_val(ai_family == AF_INET6, addr);
354 } else {
356 }
357 }
358
359 total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
360 if (nodename != NULL) {
361 namelen = strlen(nodename);
363 /* invalid name length */
364 return EAI_FAIL;
365 }
366 LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
367 total_size += namelen + 1;
368 }
369 /* If this fails, please report to lwip-devel! :-) */
370 LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
371 total_size <= NETDB_ELEM_SIZE);
372 ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
373 if (ai == NULL) {
374 return EAI_MEMORY;
375 }
376 memset(ai, 0, total_size);
377 /* cast through void* to get rid of alignment warnings */
378 sa = (struct sockaddr_storage *)(void *)((u8_t *)ai + sizeof(struct addrinfo));
379 if (IP_IS_V6_VAL(addr)) {
380#if LWIP_IPV6
381 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
382 /* set up sockaddr */
383 inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
384 sa6->sin6_family = AF_INET6;
385 sa6->sin6_len = sizeof(struct sockaddr_in6);
386 sa6->sin6_port = lwip_htons((u16_t)port_nr);
387 sa6->sin6_scope_id = ip6_addr_zone(ip_2_ip6(&addr));
388 ai->ai_family = AF_INET6;
389#endif /* LWIP_IPV6 */
390 } else {
391#if LWIP_IPV4
392 struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
393 /* set up sockaddr */
394 inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
395 sa4->sin_family = AF_INET;
396 sa4->sin_len = sizeof(struct sockaddr_in);
397 sa4->sin_port = lwip_htons((u16_t)port_nr);
398 ai->ai_family = AF_INET;
399#endif /* LWIP_IPV4 */
400 }
401
402 /* set up addrinfo */
403 if (hints != NULL) {
404 /* copy socktype & protocol from hints if specified */
405 ai->ai_socktype = hints->ai_socktype;
406 ai->ai_protocol = hints->ai_protocol;
407 }
408 if (nodename != NULL) {
409 /* copy nodename to canonname if specified */
410 ai->ai_canonname = ((char *)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
411 MEMCPY(ai->ai_canonname, nodename, namelen);
412 ai->ai_canonname[namelen] = 0;
413 }
414 ai->ai_addrlen = sizeof(struct sockaddr_storage);
415 ai->ai_addr = (struct sockaddr *)sa;
416
417 *res = ai;
418
419 return 0;
420}
421
422#endif /* LWIP_DNS && LWIP_SOCKET */
#define EINVAL
Definition: acclib.h:90
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define ERANGE
Definition: acclib.h:92
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
char * hostname
Definition: ftp.c:88
#define lwip_htons(x)
Definition: def.h:86
#define NULL
Definition: types.h:112
unsigned int idx
Definition: utils.c:41
#define AF_INET
Definition: tcpip.h:117
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:158
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint res
Definition: glext.h:9613
GLint namelen
Definition: glext.h:7232
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum const GLvoid * addr
Definition: glext.h:9621
GLuint64EXT * result
Definition: glext.h:11304
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
#define LWIP_MEM_ALIGN_BUFFER(size)
Definition: arch.h:287
uint8_t u8_t
Definition: arch.h:125
uint16_t u16_t
Definition: arch.h:127
#define LWIP_MEM_ALIGN(addr)
Definition: arch.h:294
s8_t err_t
Definition: err.h:96
@ ERR_OK
Definition: err.h:55
#define DNS_DEBUG
Definition: opt.h:3547
#define DNS_MAX_NAME_LENGTH
Definition: opt.h:1145
#define LWIP_IPV4
Definition: opt.h:734
#define LWIP_IPV6
Definition: opt.h:2440
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
#define ip_2_ip6(ipaddr)
Definition: ip_addr.h:356
#define IP_IS_V6_VAL(ipaddr)
Definition: ip_addr.h:348
ip6_addr_t ip_addr_t
Definition: ip_addr.h:344
#define ip_addr_set_any_val(is_ipv6, ipaddr)
Definition: ip_addr.h:289
#define ipaddr_aton(cp, addr)
Definition: ip_addr.h:387
#define IP_IS_V4_VAL(ipaddr)
Definition: ip_addr.h:347
#define ip_addr_set_loopback_val(is_ipv6, ipaddr)
Definition: ip_addr.h:290
#define ipaddr_ntoa(ipaddr)
Definition: ip_addr.h:385
#define MEMCPY(DST, SRC, BYTES)
Definition: macros.h:231
void * memp_malloc(memp_t type)
Definition: memp.c:337
void memp_free(memp_t type, void *mem)
Definition: memp.c:420
static unsigned __int64 next
Definition: rand_nt.c:6
#define err(...)
strncpy
Definition: string.h:335
#define memset(x, y, z)
Definition: compat.h:39
namespace GUID const ADDRINFOEXW * hints
Definition: sock.c:80
static const ADDRINFOW PADDRINFOW *static const WCHAR * servname
Definition: sock.c:79
size_t ai_addrlen
Definition: ws2def.h:669
struct sockaddr * ai_addr
Definition: ws2def.h:671
char * ai_canonname
Definition: ws2def.h:670
int ai_socktype
Definition: ws2def.h:667
int ai_protocol
Definition: ws2def.h:668
struct addrinfo * ai_next
Definition: ws2def.h:672
int ai_family
Definition: ws2def.h:666
Definition: name.c:39
ULONG sin6_scope_id
Definition: ws2ipdef.h:184
ADDRESS_FAMILY sin6_family
Definition: ws2ipdef.h:179
IN6_ADDR sin6_addr
Definition: ws2ipdef.h:182
USHORT sin6_port
Definition: ws2ipdef.h:180
struct in_addr sin_addr
Definition: winsock.h:512
short sin_family
Definition: winsock.h:510
u_short sin_port
Definition: winsock.h:511
int ret
#define AF_INET6
Definition: winsock.h:369
#define HOST_NOT_FOUND
Definition: winsock.h:226
#define AF_UNSPEC
Definition: winsock.h:344
#define h_errno
Definition: winsock.h:225
#define AI_NUMERICHOST
Definition: ws2def.h:295
#define AI_PASSIVE
Definition: ws2def.h:293
#define EAI_NONAME
Definition: ws2tcpip.h:38
#define EAI_FAMILY
Definition: ws2tcpip.h:34
#define EAI_MEMORY
Definition: ws2tcpip.h:35
#define EAI_SERVICE
Definition: ws2tcpip.h:39
#define EAI_FAIL
Definition: ws2tcpip.h:33
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList