ReactOS 0.4.15-dev-7934-g1dc8d80
netdb.c
Go to the documentation of this file.
1
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
51struct gethostbyname_r_helper {
52 ip_addr_t *addr_list[2];
54 char *aliases;
55};
56
58#if LWIP_DNS_API_DECLARE_H_ERRNO
59int h_errno;
60#endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
61
64#ifndef LWIP_DNS_API_HOSTENT_STORAGE
65#define LWIP_DNS_API_HOSTENT_STORAGE 0
66#endif
67
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
84struct hostent*
85lwip_gethostbyname(const char *name)
86{
87 err_t err;
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];
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));
101 return NULL;
102 }
103
104 /* fill hostent */
105 s_hostent_addr = addr;
106 s_phostent_addr[0] = &s_hostent_addr;
107 s_phostent_addr[1] = NULL;
108 s_hostent.h_name = (char*)name;
109 s_hostent.h_aliases = &s_aliases;
110 s_hostent.h_addrtype = AF_INET;
111 s_hostent.h_length = sizeof(ip_addr_t);
112 s_hostent.h_addr_list = (char**)&s_phostent_addr;
113
114#if DNS_DEBUG
115 /* dump hostent */
116 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
117 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", s_hostent.h_aliases));
118 if (s_hostent.h_aliases != NULL) {
119 u8_t idx;
120 for ( idx=0; s_hostent.h_aliases[idx]; idx++) {
121 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]-> == %p\n", idx, s_hostent.h_aliases[idx]));
122 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]-> == %s\n", idx, s_hostent.h_aliases[idx]));
123 }
124 }
125 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
126 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
127 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", s_hostent.h_addr_list));
128 if (s_hostent.h_addr_list != NULL) {
129 u8_t idx;
130 for ( idx=0; s_hostent.h_addr_list[idx]; idx++) {
131 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i] == %p\n", idx, s_hostent.h_addr_list[idx]));
132 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ip_ntoa((ip_addr_t*)s_hostent.h_addr_list[idx])));
133 }
134 }
135#endif /* DNS_DEBUG */
136
137#if LWIP_DNS_API_HOSTENT_STORAGE
138 /* this function should return the "per-thread" hostent after copy from s_hostent */
139 return sys_thread_hostent(&s_hostent);
140#else
141 return &s_hostent;
142#endif /* LWIP_DNS_API_HOSTENT_STORAGE */
143}
144
161int
162lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
163 size_t buflen, struct hostent **result, int *h_errnop)
164{
165 err_t err;
166 struct gethostbyname_r_helper *h;
167 char *hostname;
168 size_t namelen;
169 int lh_errno;
170
171 if (h_errnop == NULL) {
172 /* ensure h_errnop is never NULL */
173 h_errnop = &lh_errno;
174 }
175
176 if (result == NULL) {
177 /* not all arguments given */
178 *h_errnop = EINVAL;
179 return -1;
180 }
181 /* first thing to do: set *result to nothing */
182 *result = NULL;
183 if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
184 /* not all arguments given */
185 *h_errnop = EINVAL;
186 return -1;
187 }
188
190 if (buflen < (sizeof(struct gethostbyname_r_helper) + namelen + 1 + (MEM_ALIGNMENT - 1))) {
191 /* buf can't hold the data needed + a copy of name */
192 *h_errnop = ERANGE;
193 return -1;
194 }
195
196 h = (struct gethostbyname_r_helper*)LWIP_MEM_ALIGN(buf);
197 hostname = ((char*)h) + sizeof(struct gethostbyname_r_helper);
198
199 /* query host IP address */
200 err = netconn_gethostbyname(name, &h->addr);
201 if (err != ERR_OK) {
202 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
203 *h_errnop = HOST_NOT_FOUND;
204 return -1;
205 }
206
207 /* copy the hostname into buf */
209 hostname[namelen] = 0;
210
211 /* fill hostent */
212 h->addr_list[0] = &h->addr;
213 h->addr_list[1] = NULL;
214 h->aliases = NULL;
215 ret->h_name = hostname;
216 ret->h_aliases = &h->aliases;
217 ret->h_addrtype = AF_INET;
218 ret->h_length = sizeof(ip_addr_t);
219 ret->h_addr_list = (char**)&h->addr_list;
220
221 /* set result != NULL */
222 *result = ret;
223
224 /* return success */
225 return 0;
226}
227
235void
236lwip_freeaddrinfo(struct addrinfo *ai)
237{
238 struct addrinfo *next;
239
240 while (ai != NULL) {
241 next = ai->ai_next;
242 memp_free(MEMP_NETDB, ai);
243 ai = next;
244 }
245}
246
266int
267lwip_getaddrinfo(const char *nodename, const char *servname,
268 const struct addrinfo *hints, struct addrinfo **res)
269{
270 err_t err;
272 struct addrinfo *ai;
273 struct sockaddr_in *sa = NULL;
274 int port_nr = 0;
275 size_t total_size;
276 size_t namelen = 0;
277
278 if (res == NULL) {
279 return EAI_FAIL;
280 }
281 *res = NULL;
282 if ((nodename == NULL) && (servname == NULL)) {
283 return EAI_NONAME;
284 }
285
286 if (servname != NULL) {
287 /* service name specified: convert to port number
288 * @todo?: currently, only ASCII integers (port numbers) are supported! */
289 port_nr = atoi(servname);
290 if ((port_nr <= 0) || (port_nr > 0xffff)) {
291 return EAI_SERVICE;
292 }
293 }
294
295 if (nodename != NULL) {
296 /* service location specified, try to resolve */
297 err = netconn_gethostbyname(nodename, &addr);
298 if (err != ERR_OK) {
299 return EAI_FAIL;
300 }
301 } else {
302 /* service location specified, use loopback address */
304 }
305
306 total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_in);
307 if (nodename != NULL) {
308 namelen = strlen(nodename);
309 LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
310 total_size += namelen + 1;
311 }
312 /* If this fails, please report to lwip-devel! :-) */
313 LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
314 total_size <= NETDB_ELEM_SIZE);
315 ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
316 if (ai == NULL) {
317 goto memerr;
318 }
319 memset(ai, 0, total_size);
320 sa = (struct sockaddr_in*)((u8_t*)ai + sizeof(struct addrinfo));
321 /* set up sockaddr */
322 inet_addr_from_ipaddr(&sa->sin_addr, &addr);
323 sa->sin_family = AF_INET;
324 sa->sin_len = sizeof(struct sockaddr_in);
325 sa->sin_port = htons((u16_t)port_nr);
326
327 /* set up addrinfo */
328 ai->ai_family = AF_INET;
329 if (hints != NULL) {
330 /* copy socktype & protocol from hints if specified */
331 ai->ai_socktype = hints->ai_socktype;
332 ai->ai_protocol = hints->ai_protocol;
333 }
334 if (nodename != NULL) {
335 /* copy nodename to canonname if specified */
336 ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
337 MEMCPY(ai->ai_canonname, nodename, namelen);
338 ai->ai_canonname[namelen] = 0;
339 }
340 ai->ai_addrlen = sizeof(struct sockaddr_in);
341 ai->ai_addr = (struct sockaddr*)sa;
342
343 *res = ai;
344
345 return 0;
346memerr:
347 if (ai != NULL) {
348 memp_free(MEMP_NETDB, ai);
349 }
350 return EAI_MEMORY;
351}
352
353#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 MEM_ALIGNMENT
Definition: d3d9_helpers.c:15
#define NULL
Definition: types.h:112
unsigned int idx
Definition: utils.c:41
unsigned char u8_t
Definition: cc.h:23
unsigned short u16_t
Definition: cc.h:24
#define AF_INET
Definition: tcpip.h:117
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
#define ERR_OK
Definition: err.h:52
s8_t err_t
Definition: err.h:47
u16_t mem_size_t
Definition: mem.h:76
#define LWIP_MEM_ALIGN(addr)
Definition: mem.h:116
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
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
#define inet_addr_from_ipaddr(target_inaddr, source_ipaddr)
Definition: inet.h:92
#define ip_ntoa(ipaddr)
Definition: ip_addr.h:232
#define ip_addr_set_loopback(ipaddr)
Definition: ip_addr.h:172
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
#define MEMCPY(DST, SRC, BYTES)
Definition: macros.h:231
void * memp_malloc(memp_t type)
Definition: memp.c:390
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
#define htons(x)
Definition: module.h:215
#define DNS_DEBUG
Definition: opt.h:2130
static unsigned __int64 next
Definition: rand_nt.c:6
#define err(...)
#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
int ret
#define HOST_NOT_FOUND
Definition: winsock.h:226
#define h_errno
Definition: winsock.h:225
#define EAI_NONAME
Definition: ws2tcpip.h:38
#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