diff options
author | cmetz <> | 1999-06-23 21:55:29 +0000 |
---|---|---|
committer | cmetz <> | 1999-06-23 21:55:29 +0000 |
commit | bdf7624d67f3bba1a3faed9abf0d2d27291c1cce (patch) | |
tree | 5cb85a83aaa8913b4f148b657a99b1fb4982d71d /src/lib/libc/net/getaddrinfo.c | |
parent | 119cda95bf950cdcba7a16178e7f48fdf84f58d0 (diff) | |
download | openbsd-bdf7624d67f3bba1a3faed9abf0d2d27291c1cce.tar.gz openbsd-bdf7624d67f3bba1a3faed9abf0d2d27291c1cce.tar.bz2 openbsd-bdf7624d67f3bba1a3faed9abf0d2d27291c1cce.zip |
Added some protocol independent interfaces (supposedly IPv6 support APIs, but
ones that are useful for all protocols, not just IPv6).
Diffstat (limited to 'src/lib/libc/net/getaddrinfo.c')
-rw-r--r-- | src/lib/libc/net/getaddrinfo.c | 1127 |
1 files changed, 1127 insertions, 0 deletions
diff --git a/src/lib/libc/net/getaddrinfo.c b/src/lib/libc/net/getaddrinfo.c new file mode 100644 index 0000000000..a572dbcde0 --- /dev/null +++ b/src/lib/libc/net/getaddrinfo.c | |||
@@ -0,0 +1,1127 @@ | |||
1 | /* | ||
2 | * %%% copyright-cmetz-96-bsd | ||
3 | * Copyright (c) 1996-1999, Craig Metz, All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. All advertising materials mentioning features or use of this software | ||
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by Craig Metz and | ||
16 | * by other contributors. | ||
17 | * 4. Neither the name of the author nor the names of contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * | ||
33 | * Redistribution and use in source and binary forms, with or without | ||
34 | * modification, are permitted provided that the following conditions | ||
35 | * are met: | ||
36 | * 1. Redistributions of source code must retain the above copyright | ||
37 | * notice, this list of conditions and the following disclaimer. | ||
38 | * 2. Redistributions in binary form must reproduce the above copyright | ||
39 | * notice, this list of conditions and the following disclaimer in the | ||
40 | * documentation and/or other materials provided with the distribution. | ||
41 | * 3. All advertising materials mentioning features or use of this software | ||
42 | * must display the following acknowledgement: | ||
43 | * This product includes software developed by Craig Metz and | ||
44 | * by other contributors. | ||
45 | * 4. Neither the name of the author nor the names of contributors | ||
46 | * may be used to endorse or promote products derived from this software | ||
47 | * without specific prior written permission. | ||
48 | * | ||
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
59 | * SUCH DAMAGE. | ||
60 | * | ||
61 | */ | ||
62 | |||
63 | /* getaddrinfo() v1.38 */ | ||
64 | |||
65 | /* | ||
66 | I'd like to thank Matti Aarnio for finding some bugs in this code and | ||
67 | sending me patches, as well as everyone else who has contributed to this | ||
68 | code (by reporting bugs, suggesting improvements, and commented on its | ||
69 | behavior and proposed changes). | ||
70 | */ | ||
71 | |||
72 | /* | ||
73 | Someone merged an earlier version of this code into the GNU libc and | ||
74 | added support for getservbyname_r and gethostbyname2_r. The support for | ||
75 | those functions in this version of the code was written using that work | ||
76 | as a reference. I may have improved on it, or I may have broken it. | ||
77 | */ | ||
78 | |||
79 | /* To do what POSIX says, even when it's broken, define: */ | ||
80 | /* #define BROKEN_LIKE_POSIX 1 */ | ||
81 | /* Note: real apps will break if you define this, while nothing other than a | ||
82 | conformance test suite should have a problem with it undefined. */ | ||
83 | |||
84 | /* If your C runtime library provides the POSIX p1003.1g D6.6 bit types | ||
85 | of the form u?int(16|32)_t, define: */ | ||
86 | /* #define HAVE_POSIX1G_TYPES 1 */ | ||
87 | /* Note: this implementation tries to guess what the correct values are for | ||
88 | your compiler+processor combination but might not always get it right. */ | ||
89 | |||
90 | /* To enable debugging support (REQUIRES NRL support library), define: */ | ||
91 | /* #define DEBUG 1 */ | ||
92 | |||
93 | #if FOR_GNULIBC | ||
94 | #define HAVE_POSIX1G_TYPES 1 | ||
95 | #define INET6 1 | ||
96 | #define LOCAL 1 | ||
97 | #define NETDB 1 | ||
98 | #undef RESOLVER | ||
99 | #undef HOSTTABLE | ||
100 | #undef DEBUG | ||
101 | #define HAVE_GETSERVBYNAME_R 1 | ||
102 | #define HAVE_GETHOSTBYNAME2_R 1 | ||
103 | #define getservbyname_r __getservbyname_r | ||
104 | #define gethostbyname2_r __gethostbyname2_r | ||
105 | #endif /* FOR_GNULIBC */ | ||
106 | |||
107 | #ifdef __OpenBSD__ | ||
108 | #define HAVE_POSIX1G_TYPES 1 | ||
109 | #define INET6 1 | ||
110 | #define LOCAL 1 | ||
111 | #define NETDB 1 | ||
112 | #define SALEN 1 | ||
113 | #undef RESOLVER | ||
114 | #undef HOSTTABLE | ||
115 | #undef DEBUG | ||
116 | #undef HAVE_GETSERVBYNAME_R | ||
117 | #undef HAVE_GETHOSTBYNAME2_R | ||
118 | #endif /* __OpenBSD__ */ | ||
119 | |||
120 | #include <sys/types.h> | ||
121 | #include <stdlib.h> | ||
122 | #include <unistd.h> | ||
123 | #include <sys/socket.h> | ||
124 | #include <string.h> | ||
125 | #if LOCAL | ||
126 | #include <stdio.h> | ||
127 | #include <sys/utsname.h> | ||
128 | #include <sys/un.h> | ||
129 | #endif /* LOCAL */ | ||
130 | #include <netinet/in.h> | ||
131 | #include <netdb.h> | ||
132 | #include <errno.h> | ||
133 | #if RESOLVER | ||
134 | #include <arpa/nameser.h> | ||
135 | #include <resolv.h> | ||
136 | #endif /* RESOLVER */ | ||
137 | #if DEBUG | ||
138 | #include <syslog.h> | ||
139 | #endif /* DEBUG */ | ||
140 | |||
141 | #ifndef AF_LOCAL | ||
142 | #define AF_LOCAL AF_UNIX | ||
143 | #endif /* AF_LOCAL */ | ||
144 | #ifndef PF_LOCAL | ||
145 | #define PF_LOCAL PF_UNIX | ||
146 | #endif /* PF_LOCAL */ | ||
147 | #ifndef UNIX_PATH_MAX | ||
148 | #define UNIX_PATH_MAX 108 | ||
149 | #endif /* UNIX_PATH_MAX */ | ||
150 | |||
151 | #if !HAVE_POSIX1G_TYPES | ||
152 | #if (~0UL) == 0xffffffff | ||
153 | #define uint8_t unsigned char | ||
154 | #define int16_t short | ||
155 | #define uint16_t unsigned short | ||
156 | #define int32_t long | ||
157 | #define uint32_t unsigned long | ||
158 | #else /* (~0UL) == 0xffffffff */ | ||
159 | #if (~0UL) == 0xffffffffffffffff | ||
160 | #define uint8_t unsigned char | ||
161 | #define int16_t short | ||
162 | #define uint16_t unsigned short | ||
163 | #define int32_t int | ||
164 | #define uint32_t unsigned int | ||
165 | #else /* (~0UL) == 0xffffffffffffffff */ | ||
166 | #error Neither 32 bit nor 64 bit word size detected. | ||
167 | #error You need to define the bit types manually. | ||
168 | #endif /* (~0UL) == 0xffffffffffffffff */ | ||
169 | #endif /* (~0UL) == 0xffffffff */ | ||
170 | #endif /* !HAVE_POSIX1G_TYPES */ | ||
171 | |||
172 | #if defined(INET6) && !defined(AF_INET6) | ||
173 | #error Without a definition of AF_INET6, this system cannot support IPv6 | ||
174 | #error addresses. | ||
175 | #endif /* defined(INET6) && !defined(AF_INET6) */ | ||
176 | |||
177 | #if INET6 | ||
178 | #ifndef T_AAAA | ||
179 | #define T_AAAA 28 | ||
180 | #endif /* T_AAAA */ | ||
181 | #endif /* INET6 */ | ||
182 | |||
183 | #if DEBUG | ||
184 | #if RESOLVER | ||
185 | #define DEBUG_MESSAGES (_res.options & RES_DEBUG) | ||
186 | #else /* RESOLVER */ | ||
187 | int __getaddrinfo_debug = 0; | ||
188 | #define DEBUG_MESSAGES (__getaddrinfo_debug) | ||
189 | #endif /* RESOLVER */ | ||
190 | #endif /* DEBUG */ | ||
191 | |||
192 | #define GAIH_OKIFUNSPEC 0x0100 | ||
193 | #define GAIH_EAI ~(GAIH_OKIFUNSPEC) | ||
194 | |||
195 | static struct addrinfo nullreq = | ||
196 | { 0, PF_UNSPEC, 0, 0, 0, NULL, NULL, NULL }; | ||
197 | |||
198 | struct gaih_service { | ||
199 | char *name; | ||
200 | int num; | ||
201 | }; | ||
202 | |||
203 | struct gaih_servtuple { | ||
204 | struct gaih_servtuple *next; | ||
205 | int socktype; | ||
206 | int protocol; | ||
207 | int port; | ||
208 | }; | ||
209 | |||
210 | static struct gaih_servtuple nullserv = { | ||
211 | NULL, 0, 0, 0 | ||
212 | }; | ||
213 | |||
214 | struct gaih_addrtuple { | ||
215 | struct gaih_addrtuple *next; | ||
216 | int family; | ||
217 | char addr[16]; | ||
218 | char *cname; | ||
219 | }; | ||
220 | |||
221 | struct gaih_typeproto { | ||
222 | int socktype; | ||
223 | int protocol; | ||
224 | char *name; | ||
225 | }; | ||
226 | |||
227 | #if DEBUG | ||
228 | #define RETURN_ERROR(x) do { \ | ||
229 | if (DEBUG_MESSAGES) \ | ||
230 | fprintf(stderr, "%s:%d: returning %s\n", __FILE__, __LINE__, #x); \ | ||
231 | rval = (x); \ | ||
232 | goto ret; \ | ||
233 | } while(0) | ||
234 | #else /* DEBUG */ | ||
235 | #define RETURN_ERROR(x) do { \ | ||
236 | rval = (x); \ | ||
237 | goto ret; \ | ||
238 | } while(0) | ||
239 | #endif /* DEBUG */ | ||
240 | |||
241 | #if HOSTTABLE | ||
242 | static int hosttable_lookup_addr(const char *name, const struct addrinfo *req, struct gaih_addrtuple **pat) | ||
243 | { | ||
244 | FILE *f; | ||
245 | char buffer[1024]; | ||
246 | char *c, *c2; | ||
247 | int rval = 1; | ||
248 | char *prevcname = NULL; | ||
249 | struct gaih_addrtuple at; | ||
250 | |||
251 | if (!(f = fopen("/etc/hosts", "r"))) | ||
252 | RETURN_ERROR(-EAI_SYSTEM); | ||
253 | |||
254 | while(fgets(buffer, sizeof(buffer), f)) { | ||
255 | if (c = strchr(buffer, '#')) | ||
256 | *c = 0; | ||
257 | |||
258 | c = buffer; | ||
259 | while(*c && !isspace(*c)) c++; | ||
260 | if (!*c) | ||
261 | continue; | ||
262 | |||
263 | *(c++) = 0; | ||
264 | |||
265 | while(*c && isspace(*c)) c++; | ||
266 | if (!*c) | ||
267 | continue; | ||
268 | |||
269 | if (!(c2 = strstr(c, name))) | ||
270 | continue; | ||
271 | |||
272 | if (*(c2 - 1) && !isspace(*(c2 - 1))) | ||
273 | continue; | ||
274 | |||
275 | c2 += strlen(name); | ||
276 | if (*c2 && !isspace(*c2)) | ||
277 | continue; | ||
278 | |||
279 | c2 = c; | ||
280 | while(*c2 && !isspace(*c2)) c2++; | ||
281 | if (!*c2) | ||
282 | continue; | ||
283 | *c2 = 0; | ||
284 | |||
285 | memset(&at, 0, sizeof(struct gaih_addrtuple)); | ||
286 | |||
287 | if (!req->ai_family || (req->ai_family == AF_INET)) | ||
288 | if (inet_pton(AF_INET, buffer, (char *)&at.addr) > 0) { | ||
289 | at.family = AF_INET; | ||
290 | goto build; | ||
291 | }; | ||
292 | |||
293 | #if INET6 | ||
294 | if (!req->ai_family || (req->ai_family == AF_INET6)) | ||
295 | if (inet_pton(AF_INET6, buffer, (char *)&at.addr) > 0) { | ||
296 | at.family = AF_INET6; | ||
297 | goto build; | ||
298 | }; | ||
299 | #endif /* INET6 */ | ||
300 | |||
301 | continue; | ||
302 | |||
303 | build: | ||
304 | if (!(*pat = malloc(sizeof(struct gaih_addrtuple)))) | ||
305 | RETURN_ERROR(-EAI_MEMORY); | ||
306 | |||
307 | memcpy(*pat, &at, sizeof(struct gaih_addrtuple)); | ||
308 | |||
309 | if (req->ai_flags & AI_CANONNAME) | ||
310 | if (prevcname && !strcmp(prevcname, c)) | ||
311 | (*pat)->cname = prevcname; | ||
312 | else | ||
313 | prevcname = (*pat)->cname = strdup(c); | ||
314 | |||
315 | pat = &((*pat)->next); | ||
316 | |||
317 | rval = 0; | ||
318 | }; | ||
319 | |||
320 | ret: | ||
321 | if (f) | ||
322 | fclose(f); | ||
323 | return rval; | ||
324 | }; | ||
325 | #endif /* HOSTTABLE */ | ||
326 | |||
327 | #if NETDB | ||
328 | int netdb_lookup_addr(const char *name, int af, const struct addrinfo *req, struct gaih_addrtuple **pat) | ||
329 | { | ||
330 | int rval, herrno, i; | ||
331 | char *prevcname = NULL; | ||
332 | struct hostent *h; | ||
333 | |||
334 | #if HAVE_GETHOSTBYNAME2_R | ||
335 | void *buf; | ||
336 | int buflen = 1024; | ||
337 | struct hostent th; | ||
338 | int herrno, j; | ||
339 | |||
340 | do { | ||
341 | if (!(buf = malloc(buflen))) | ||
342 | RETURN_ERROR(-EAI_MEMORY); | ||
343 | |||
344 | if (!gethostbyname2_r(name, af, &th, buf, buflen, &h, &herrno)) | ||
345 | break; | ||
346 | |||
347 | free(buf); | ||
348 | buf = NULL; | ||
349 | |||
350 | if ((herrno == NETDB_INTERNAL) && (errno == ERANGE)) { | ||
351 | if (buflen >= 65536) | ||
352 | RETURN_ERROR(-EAI_MEMORY); | ||
353 | |||
354 | buflen = buflen << 1; | ||
355 | continue; | ||
356 | }; | ||
357 | } while(0); | ||
358 | #else /* HAVE_GETHOSTBYNAME2_R */ | ||
359 | h = gethostbyname2(name, af); | ||
360 | herrno = h_errno; | ||
361 | #endif /* HAVE_GETHOSTBYNAME2_R */ | ||
362 | |||
363 | if (!h) { | ||
364 | #if DEBUG | ||
365 | if (DEBUG_MESSAGES) | ||
366 | fprintf(stderr, "getaddrinfo: gethostbyname2 failed, h_errno=%d\n", herrno); | ||
367 | #endif /* DEBUG */ | ||
368 | switch(herrno) { | ||
369 | case NETDB_INTERNAL: | ||
370 | RETURN_ERROR(-EAI_SYSTEM); | ||
371 | case HOST_NOT_FOUND: | ||
372 | RETURN_ERROR(1); | ||
373 | case TRY_AGAIN: | ||
374 | RETURN_ERROR(-EAI_AGAIN); | ||
375 | case NO_RECOVERY: | ||
376 | RETURN_ERROR(-EAI_FAIL); | ||
377 | case NO_DATA: | ||
378 | RETURN_ERROR(1); | ||
379 | default: | ||
380 | RETURN_ERROR(-EAI_FAIL); | ||
381 | }; | ||
382 | }; | ||
383 | |||
384 | for (i = 0; h->h_addr_list[i]; i++) { | ||
385 | while(*pat) | ||
386 | pat = &((*pat)->next); | ||
387 | |||
388 | if (!(*pat = malloc(sizeof(struct gaih_addrtuple)))) | ||
389 | RETURN_ERROR(-EAI_MEMORY); | ||
390 | |||
391 | memset(*pat, 0, sizeof(struct gaih_addrtuple)); | ||
392 | |||
393 | switch((*pat)->family = af) { | ||
394 | case AF_INET: | ||
395 | memcpy((*pat)->addr, h->h_addr_list[i], sizeof(struct in_addr)); | ||
396 | break; | ||
397 | #if INET6 | ||
398 | case AF_INET6: | ||
399 | memcpy((*pat)->addr, h->h_addr_list[i], sizeof(struct in6_addr)); | ||
400 | break; | ||
401 | #endif /* INET6 */ | ||
402 | default: | ||
403 | RETURN_ERROR(-EAI_FAIL); | ||
404 | }; | ||
405 | |||
406 | if (req->ai_flags & AI_CANONNAME) { | ||
407 | if (prevcname && !strcmp(prevcname, h->h_name)) | ||
408 | (*pat)->cname = prevcname; | ||
409 | else | ||
410 | prevcname = (*pat)->cname = strdup(h->h_name); | ||
411 | }; | ||
412 | |||
413 | pat = &((*pat)->next); | ||
414 | } | ||
415 | |||
416 | rval = 0; | ||
417 | |||
418 | ret: | ||
419 | #if HAVE_GETHOSTBYNAME2_R | ||
420 | free(buf); | ||
421 | #endif /* HAVE_GETHOSTBYNAME2_R */ | ||
422 | return rval; | ||
423 | }; | ||
424 | #endif /* NETDB */ | ||
425 | |||
426 | #if RESOLVER | ||
427 | #define RRHEADER_SZ 10 | ||
428 | |||
429 | int resolver_lookup_addr(const char *name, int type, const struct addrinfo *req, struct gaih_addrtuple **pat) | ||
430 | { | ||
431 | int rval; | ||
432 | char answer[PACKETSZ]; | ||
433 | int answerlen; | ||
434 | char dn[MAXDNAME]; | ||
435 | char *prevcname = NULL; | ||
436 | void *p, *ep; | ||
437 | int answers, i, j; | ||
438 | uint16_t rtype, rclass; | ||
439 | |||
440 | if ((answerlen = res_search(name, C_IN, type, answer, sizeof(answer))) < 0) { | ||
441 | #if DEBUG | ||
442 | if (DEBUG_MESSAGES) | ||
443 | fprintf(stderr, "getaddrinfo: res_search failed, h_errno=%d\n", h_errno); | ||
444 | #endif /* DEBUG */ | ||
445 | switch(h_errno) { | ||
446 | case NETDB_INTERNAL: | ||
447 | RETURN_ERROR(-EAI_SYSTEM); | ||
448 | case HOST_NOT_FOUND: | ||
449 | RETURN_ERROR(1); | ||
450 | case TRY_AGAIN: | ||
451 | RETURN_ERROR(-EAI_AGAIN); | ||
452 | case NO_RECOVERY: | ||
453 | RETURN_ERROR(-EAI_FAIL); | ||
454 | case NO_DATA: | ||
455 | RETURN_ERROR(1); | ||
456 | default: | ||
457 | RETURN_ERROR(-EAI_FAIL); | ||
458 | }; | ||
459 | }; | ||
460 | |||
461 | p = answer; | ||
462 | ep = answer + answerlen; | ||
463 | |||
464 | if (answerlen < RRHEADER_SZ) | ||
465 | RETURN_ERROR(-EAI_FAIL); | ||
466 | |||
467 | { | ||
468 | HEADER *h = (HEADER *)p; | ||
469 | if (!h->qr || (h->opcode != QUERY) || (h->qdcount != htons(1)) || | ||
470 | !h->ancount) | ||
471 | RETURN_ERROR(-EAI_FAIL); | ||
472 | answers = ntohs(h->ancount); | ||
473 | }; | ||
474 | p += sizeof(HEADER); | ||
475 | |||
476 | if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0) | ||
477 | RETURN_ERROR(-EAI_FAIL); | ||
478 | p += i; | ||
479 | |||
480 | if (p + 2*sizeof(uint16_t) >= ep) | ||
481 | RETURN_ERROR(-EAI_FAIL); | ||
482 | |||
483 | GETSHORT(rtype, p); | ||
484 | GETSHORT(rclass, p); | ||
485 | |||
486 | if ((rtype != type) || (rclass != C_IN)) | ||
487 | RETURN_ERROR(-EAI_FAIL); | ||
488 | |||
489 | while(answers--) { | ||
490 | if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0) | ||
491 | RETURN_ERROR(-EAI_FAIL); | ||
492 | p += i; | ||
493 | |||
494 | if (p + RRHEADER_SZ >= ep) | ||
495 | RETURN_ERROR(-EAI_FAIL); | ||
496 | |||
497 | GETSHORT(rtype, p); | ||
498 | GETSHORT(rclass, p); | ||
499 | p += sizeof(uint32_t); | ||
500 | if (rclass != C_IN) | ||
501 | RETURN_ERROR(-EAI_FAIL); | ||
502 | GETSHORT(rclass, p); | ||
503 | i = rclass; | ||
504 | |||
505 | if (p + i > ep) | ||
506 | RETURN_ERROR(-EAI_FAIL); | ||
507 | |||
508 | if (rtype == type) { | ||
509 | while(*pat) | ||
510 | pat = &((*pat)->next); | ||
511 | |||
512 | if (!(*pat = malloc(sizeof(struct gaih_addrtuple)))) | ||
513 | RETURN_ERROR(-EAI_MEMORY); | ||
514 | |||
515 | memset(*pat, 0, sizeof(struct gaih_addrtuple)); | ||
516 | |||
517 | switch(type) { | ||
518 | case T_A: | ||
519 | if (i != sizeof(struct in_addr)) | ||
520 | RETURN_ERROR(-EAI_FAIL); | ||
521 | (*pat)->family = AF_INET; | ||
522 | break; | ||
523 | #if INET6 | ||
524 | case T_AAAA: | ||
525 | if (i != sizeof(struct in6_addr)) | ||
526 | RETURN_ERROR(-EAI_FAIL); | ||
527 | (*pat)->family = AF_INET6; | ||
528 | break; | ||
529 | #endif /* INET6 */ | ||
530 | default: | ||
531 | RETURN_ERROR(-EAI_FAIL); | ||
532 | }; | ||
533 | |||
534 | memcpy((*pat)->addr, p, i); | ||
535 | |||
536 | if (req->ai_flags & AI_CANONNAME) | ||
537 | if (prevcname && !strcmp(prevcname, dn)) | ||
538 | (*pat)->cname = prevcname; | ||
539 | else | ||
540 | prevcname = (*pat)->cname = strdup(dn); | ||
541 | }; | ||
542 | p += i; | ||
543 | }; | ||
544 | |||
545 | rval = 0; | ||
546 | |||
547 | ret: | ||
548 | return rval; | ||
549 | }; | ||
550 | #endif /* RESOLVER */ | ||
551 | |||
552 | #if LOCAL | ||
553 | static int gaih_local(const char *name, const struct gaih_service *service, | ||
554 | const struct addrinfo *req, struct addrinfo **pai) | ||
555 | { | ||
556 | int rval; | ||
557 | struct utsname utsname; | ||
558 | |||
559 | if (name || (req->ai_flags & AI_CANONNAME)) | ||
560 | if (uname(&utsname) < 0) | ||
561 | RETURN_ERROR(-EAI_SYSTEM); | ||
562 | if (name) { | ||
563 | if (strcmp(name, "localhost") && strcmp(name, "local") && strcmp(name, "unix") && strcmp(name, utsname.nodename)) | ||
564 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_NONAME); | ||
565 | }; | ||
566 | |||
567 | if (!(*pai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un) + ((req->ai_flags & AI_CANONNAME) ? (strlen(utsname.nodename) + 1): 0)))) | ||
568 | RETURN_ERROR(-EAI_MEMORY); | ||
569 | |||
570 | (*pai)->ai_next = NULL; | ||
571 | (*pai)->ai_flags = req->ai_flags; | ||
572 | (*pai)->ai_family = AF_LOCAL; | ||
573 | (*pai)->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM; | ||
574 | (*pai)->ai_protocol = req->ai_protocol; | ||
575 | (*pai)->ai_addrlen = sizeof(struct sockaddr_un); | ||
576 | (*pai)->ai_addr = (void *)(*pai) + sizeof(struct addrinfo); | ||
577 | #if SALEN | ||
578 | ((struct sockaddr_un *)(*pai)->ai_addr)->sun_len = sizeof(struct sockaddr_un); | ||
579 | #endif /* SALEN */ | ||
580 | ((struct sockaddr_un *)(*pai)->ai_addr)->sun_family = AF_LOCAL; | ||
581 | memset(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, 0, UNIX_PATH_MAX); | ||
582 | if (service) { | ||
583 | char *c; | ||
584 | if (c = strchr(service->name, '/')) { | ||
585 | if (strlen(service->name) >= sizeof(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path)) | ||
586 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE); | ||
587 | strcpy(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, service->name); | ||
588 | } else { | ||
589 | if (strlen(P_tmpdir "/") + 1 + strlen(service->name) >= sizeof(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path)) | ||
590 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE); | ||
591 | strcpy(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, P_tmpdir "/"); | ||
592 | strcat(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, service->name); | ||
593 | }; | ||
594 | } else { | ||
595 | char *c; | ||
596 | if (!(c = tmpnam(NULL))) | ||
597 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SYSTEM); | ||
598 | |||
599 | strncpy((((struct sockaddr_un *)(*pai)->ai_addr)->sun_path), c, sizeof(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path) - 1); | ||
600 | c[sizeof(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path) - 1] = 0; | ||
601 | }; | ||
602 | if (req->ai_flags & AI_CANONNAME) { | ||
603 | strncpy((*pai)->ai_canonname = (char *)(*pai) + sizeof(struct addrinfo) + sizeof(struct sockaddr_un), utsname.nodename, sizeof(utsname.nodename) - 1); | ||
604 | (*pai)->ai_canonname[sizeof(utsname.nodename) - 1] = 0; | ||
605 | } else | ||
606 | (*pai)->ai_canonname = NULL; | ||
607 | |||
608 | rval = 0; | ||
609 | |||
610 | ret: | ||
611 | return rval; | ||
612 | }; | ||
613 | #endif /* LOCAL */ | ||
614 | |||
615 | static struct gaih_typeproto gaih_inet_typeproto[] = { | ||
616 | { 0, 0, NULL }, | ||
617 | { SOCK_STREAM, IPPROTO_TCP, "tcp" }, | ||
618 | { SOCK_DGRAM, IPPROTO_UDP, "udp" }, | ||
619 | { 0, 0, NULL } | ||
620 | }; | ||
621 | |||
622 | static int gaih_inet_serv(char *servicename, struct gaih_typeproto *tp, struct gaih_servtuple **st) | ||
623 | { | ||
624 | int rval; | ||
625 | struct servent *s; | ||
626 | #if HAVE_GETSERVBYNAME_R | ||
627 | int i; | ||
628 | void *buf; | ||
629 | int buflen = 1024; | ||
630 | struct servent ts; | ||
631 | |||
632 | do { | ||
633 | if (!(buf = malloc(buflen))) | ||
634 | RETURN_ERROR(-EAI_MEMORY); | ||
635 | |||
636 | if (!getservbyname_r(servicename, tp->name, &ts, buf, buflen, &s)) | ||
637 | break; | ||
638 | |||
639 | free(buf); | ||
640 | buf = NULL; | ||
641 | |||
642 | if (errno != ERANGE) | ||
643 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE); | ||
644 | |||
645 | if (buflen >= 65536) | ||
646 | RETURN_ERROR(-EAI_MEMORY); | ||
647 | |||
648 | buflen = buflen << 1; | ||
649 | } while(1); | ||
650 | #else /* HAVE_GETSERVBYNAME_R */ | ||
651 | if (!(s = getservbyname(servicename, tp->name))) | ||
652 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE); | ||
653 | #endif /* HAVE_GETSERVBYNAME_R */ | ||
654 | |||
655 | if (!(*st = malloc(sizeof(struct gaih_servtuple)))) | ||
656 | RETURN_ERROR(-EAI_MEMORY); | ||
657 | |||
658 | (*st)->next = NULL; | ||
659 | (*st)->socktype = tp->socktype; | ||
660 | (*st)->protocol = tp->protocol; | ||
661 | (*st)->port = s->s_port; | ||
662 | |||
663 | rval = 0; | ||
664 | |||
665 | ret: | ||
666 | #if HAVE_GETSERVBYNAME_R | ||
667 | if (buf) | ||
668 | free(buf); | ||
669 | #endif /* HAVE_GETSERVBYNAME_R */ | ||
670 | return rval; | ||
671 | } | ||
672 | |||
673 | static int gaih_inet(const char *name, const struct gaih_service *service, | ||
674 | const struct addrinfo *req, struct addrinfo **pai) | ||
675 | { | ||
676 | int rval; | ||
677 | struct hostent *h = NULL; | ||
678 | struct gaih_typeproto *tp = gaih_inet_typeproto; | ||
679 | struct gaih_servtuple *st = &nullserv; | ||
680 | struct gaih_addrtuple *at = NULL; | ||
681 | int i; | ||
682 | |||
683 | if (req->ai_protocol || req->ai_socktype) { | ||
684 | for (tp++; tp->name && | ||
685 | ((req->ai_socktype != tp->socktype) || !req->ai_socktype) && | ||
686 | ((req->ai_protocol != tp->protocol) || !req->ai_protocol); tp++); | ||
687 | if (!tp->name) | ||
688 | if (req->ai_socktype) | ||
689 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SOCKTYPE); | ||
690 | else | ||
691 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE); | ||
692 | } | ||
693 | |||
694 | if (service && (service->num < 0)) { | ||
695 | if (tp->name) { | ||
696 | if (rval = gaih_inet_serv(service->name, tp, &st)) | ||
697 | goto ret; | ||
698 | } else { | ||
699 | struct gaih_servtuple **pst = &st; | ||
700 | for (tp++; tp->name; tp++) { | ||
701 | if (rval = gaih_inet_serv(service->name, tp, pst)) { | ||
702 | if (rval & GAIH_OKIFUNSPEC) | ||
703 | continue; | ||
704 | goto ret; | ||
705 | }; | ||
706 | pst = &((*pst)->next); | ||
707 | }; | ||
708 | if (st == &nullserv) | ||
709 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE); | ||
710 | }; | ||
711 | } else { | ||
712 | if (!(st = malloc(sizeof(struct gaih_servtuple)))) | ||
713 | RETURN_ERROR(-EAI_MEMORY); | ||
714 | |||
715 | st->next = NULL; | ||
716 | st->socktype = tp->socktype; | ||
717 | st->protocol = tp->protocol; | ||
718 | if (service) | ||
719 | st->port = htons(service->num); | ||
720 | else | ||
721 | st->port = 0; | ||
722 | }; | ||
723 | |||
724 | if (!name) { | ||
725 | if (!(at = malloc(sizeof(struct gaih_addrtuple)))) | ||
726 | RETURN_ERROR(-EAI_MEMORY); | ||
727 | |||
728 | memset(at, 0, sizeof(struct gaih_addrtuple)); | ||
729 | |||
730 | #if INET6 | ||
731 | if (req->ai_family) | ||
732 | at->family = req->ai_family; | ||
733 | else { | ||
734 | if (!(at->next = malloc(sizeof(struct gaih_addrtuple)))) | ||
735 | RETURN_ERROR(-EAI_MEMORY); | ||
736 | |||
737 | at->family = AF_INET6; | ||
738 | |||
739 | memset(at->next, 0, sizeof(struct gaih_addrtuple)); | ||
740 | at->next->family = AF_INET; | ||
741 | }; | ||
742 | #else /* INET6 */ | ||
743 | at->family = AF_INET; | ||
744 | #endif /* INET6 */ | ||
745 | |||
746 | goto build; | ||
747 | }; | ||
748 | |||
749 | if (!req->ai_family || (req->ai_family == AF_INET)) { | ||
750 | struct in_addr in_addr; | ||
751 | if (inet_pton(AF_INET, name, &in_addr) > 0) { | ||
752 | if (!(at = malloc(sizeof(struct gaih_addrtuple)))) | ||
753 | RETURN_ERROR(-EAI_MEMORY); | ||
754 | |||
755 | memset(at, 0, sizeof(struct gaih_addrtuple)); | ||
756 | |||
757 | at->family = AF_INET; | ||
758 | memcpy(at->addr, &in_addr, sizeof(struct in_addr)); | ||
759 | goto build; | ||
760 | }; | ||
761 | }; | ||
762 | |||
763 | #if INET6 | ||
764 | if (!req->ai_family || (req->ai_family == AF_INET6)) { | ||
765 | struct in6_addr in6_addr; | ||
766 | if (inet_pton(AF_INET6, name, &in6_addr) > 0) { | ||
767 | if (!(at = malloc(sizeof(struct gaih_addrtuple)))) | ||
768 | RETURN_ERROR(-EAI_MEMORY); | ||
769 | |||
770 | memset(at, 0, sizeof(struct gaih_addrtuple)); | ||
771 | |||
772 | at->family = AF_INET6; | ||
773 | memcpy(at->addr, &in6_addr, sizeof(struct in6_addr)); | ||
774 | goto build; | ||
775 | }; | ||
776 | }; | ||
777 | #endif /* INET6 */ | ||
778 | |||
779 | if (!(req->ai_flags & AI_NUMERICHOST)) { | ||
780 | #if NETDB | ||
781 | #if INET6 | ||
782 | if (!req->ai_family || (req->ai_family == AF_INET6)) | ||
783 | if ((rval = netdb_lookup_addr(name, AF_INET6, req, &at)) < 0) | ||
784 | goto ret; | ||
785 | #endif /* INET6 */ | ||
786 | if (!req->ai_family || (req->ai_family == AF_INET)) | ||
787 | if ((rval = netdb_lookup_addr(name, AF_INET, req, &at)) < 0) | ||
788 | goto ret; | ||
789 | |||
790 | if (!rval) | ||
791 | goto build; | ||
792 | #else /* NETDB */ | ||
793 | #if HOSTTABLE | ||
794 | if ((rval = hosttable_lookup_addr(name, req, &at)) < 0) | ||
795 | goto ret; | ||
796 | |||
797 | if (!rval) | ||
798 | goto build; | ||
799 | #endif /* HOSTTABLE */ | ||
800 | |||
801 | #if RESOLVER | ||
802 | #if INET6 | ||
803 | { | ||
804 | int rval2; | ||
805 | |||
806 | if (!req->ai_family || (req->ai_family == AF_INET6)) | ||
807 | if ((rval2 = resolver_lookup_addr(name, T_AAAA, req, &at)) < 0) { | ||
808 | rval = rval2; | ||
809 | goto ret; | ||
810 | }; | ||
811 | #endif /* INET6 */ | ||
812 | |||
813 | if (!req->ai_family || (req->ai_family == AF_INET)) | ||
814 | if ((rval = resolver_lookup_addr(name, T_A, req, &at)) < 0) | ||
815 | goto ret; | ||
816 | |||
817 | #if INET6 | ||
818 | if (!rval || !rval2) | ||
819 | goto build; | ||
820 | }; | ||
821 | #else /* INET6 */ | ||
822 | if (!rval) | ||
823 | goto build; | ||
824 | #endif /* INET6 */ | ||
825 | #endif /* RESOLVER */ | ||
826 | #endif /* NETDB */ | ||
827 | }; | ||
828 | |||
829 | if (!at) | ||
830 | RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_NONAME); | ||
831 | |||
832 | build: | ||
833 | { | ||
834 | char *prevcname = NULL; | ||
835 | struct gaih_servtuple *st2; | ||
836 | struct gaih_addrtuple *at2 = at; | ||
837 | int j; | ||
838 | |||
839 | while(at2) { | ||
840 | if (req->ai_flags & AI_CANONNAME) { | ||
841 | if (at2->cname) | ||
842 | j = strlen(at2->cname) + 1; | ||
843 | else | ||
844 | if (name) | ||
845 | j = strlen(name) + 1; | ||
846 | else | ||
847 | j = 2; | ||
848 | } else | ||
849 | j = 0; | ||
850 | |||
851 | #if INET6 | ||
852 | if (at2->family == AF_INET6) | ||
853 | i = sizeof(struct sockaddr_in6); | ||
854 | else | ||
855 | #endif /* INET6 */ | ||
856 | i = sizeof(struct sockaddr_in); | ||
857 | |||
858 | st2 = st; | ||
859 | while(st2) { | ||
860 | if (!(*pai = malloc(sizeof(struct addrinfo) + i + j))) | ||
861 | RETURN_ERROR(-EAI_MEMORY); | ||
862 | |||
863 | memset(*pai, 0, sizeof(struct addrinfo) + i + j); | ||
864 | |||
865 | (*pai)->ai_flags = req->ai_flags; | ||
866 | (*pai)->ai_family = at2->family; | ||
867 | (*pai)->ai_socktype = st2->socktype; | ||
868 | (*pai)->ai_protocol = st2->protocol; | ||
869 | (*pai)->ai_addrlen = i; | ||
870 | (*pai)->ai_addr = (void *)(*pai) + sizeof(struct addrinfo); | ||
871 | #if SALEN | ||
872 | ((struct sockaddr_in *)(*pai)->ai_addr)->sin_len = i; | ||
873 | #endif /* SALEN */ | ||
874 | ((struct sockaddr_in *)(*pai)->ai_addr)->sin_family = at2->family; | ||
875 | ((struct sockaddr_in *)(*pai)->ai_addr)->sin_port = st2->port; | ||
876 | |||
877 | #if INET6 | ||
878 | if (at2->family == AF_INET6) | ||
879 | memcpy(&((struct sockaddr_in6 *)(*pai)->ai_addr)->sin6_addr, at2->addr, sizeof(struct in6_addr)); | ||
880 | else | ||
881 | #endif /* INET6 */ | ||
882 | memcpy(&((struct sockaddr_in *)(*pai)->ai_addr)->sin_addr, at2->addr, sizeof(struct in_addr)); | ||
883 | |||
884 | if (j) { | ||
885 | (*pai)->ai_canonname = (void *)(*pai) + sizeof(struct addrinfo) + i; | ||
886 | if (at2->cname) { | ||
887 | strcpy((*pai)->ai_canonname, at2->cname); | ||
888 | if (prevcname != at2->cname) { | ||
889 | if (prevcname) | ||
890 | free(prevcname); | ||
891 | prevcname = at2->cname; | ||
892 | }; | ||
893 | } else | ||
894 | strcpy((*pai)->ai_canonname, name ? name : "*"); | ||
895 | }; | ||
896 | |||
897 | pai = &((*pai)->ai_next); | ||
898 | |||
899 | st2 = st2->next; | ||
900 | }; | ||
901 | at2 = at2->next; | ||
902 | }; | ||
903 | }; | ||
904 | |||
905 | rval = 0; | ||
906 | |||
907 | ret: | ||
908 | if (st != &nullserv) { | ||
909 | struct gaih_servtuple *st2 = st; | ||
910 | while(st) { | ||
911 | st2 = st->next; | ||
912 | free(st); | ||
913 | st = st2; | ||
914 | } | ||
915 | } | ||
916 | if (at) { | ||
917 | struct gaih_addrtuple *at2 = at; | ||
918 | while(at) { | ||
919 | at2 = at->next; | ||
920 | free(at); | ||
921 | at = at2; | ||
922 | } | ||
923 | } | ||
924 | |||
925 | return rval; | ||
926 | } | ||
927 | |||
928 | struct gaih { | ||
929 | int family; | ||
930 | char *name; | ||
931 | int (*gaih)(const char *name, const struct gaih_service *service, | ||
932 | const struct addrinfo *req, struct addrinfo **pai); | ||
933 | }; | ||
934 | |||
935 | static struct gaih gaih[] = { | ||
936 | #if INET6 | ||
937 | { PF_INET6, "inet6", gaih_inet }, | ||
938 | #endif /* INET6 */ | ||
939 | { PF_INET, "inet", gaih_inet }, | ||
940 | #if LOCAL | ||
941 | { PF_LOCAL, "local", gaih_local }, | ||
942 | #endif /* LOCAL */ | ||
943 | { -1, NULL, NULL } | ||
944 | }; | ||
945 | |||
946 | #if DEBUG | ||
947 | static void dump_addrinfo(const struct addrinfo *ai, int follownext) | ||
948 | { | ||
949 | char *c; | ||
950 | |||
951 | loop: | ||
952 | fprintf(stderr, "addrinfo at "); | ||
953 | if (!ai) { | ||
954 | fprintf(stderr, "NULL\n"); | ||
955 | return; | ||
956 | }; | ||
957 | fprintf(stderr, "%08x:\n", (unsigned int)ai); | ||
958 | fprintf(stderr, " flags=%x(", ai->ai_flags); | ||
959 | c = ""; | ||
960 | if (ai->ai_flags & AI_PASSIVE) { | ||
961 | fprintf(stderr, "passive"); | ||
962 | c = " "; | ||
963 | }; | ||
964 | if (ai->ai_flags & AI_CANONNAME) { | ||
965 | fprintf(stderr, "%scanonname", c); | ||
966 | c = " "; | ||
967 | }; | ||
968 | if (ai->ai_flags & AI_NUMERICHOST) { | ||
969 | fprintf(stderr, "%snumerichost", c); | ||
970 | c = " "; | ||
971 | }; | ||
972 | if (ai->ai_flags & AI_EXT) { | ||
973 | fprintf(stderr, "%sext", c); | ||
974 | }; | ||
975 | fprintf(stderr, ")\n"); | ||
976 | fprintf(stderr, " family=%x(%s)\n", ai->ai_family, nrl_afnumtoname(ai->ai_family)); | ||
977 | fprintf(stderr, " socktype=%x(%s)\n", ai->ai_socktype, nrl_socktypenumtoname(ai->ai_socktype)); | ||
978 | fprintf(stderr, " protocol=%x\n", ai->ai_protocol); | ||
979 | fprintf(stderr, " addrlen=%x\n", ai->ai_addrlen); | ||
980 | fprintf(stderr, " addr=%08x", (unsigned int)ai->ai_addr); | ||
981 | if (ai->ai_addr) { | ||
982 | fprintf(stderr, ":\n"); | ||
983 | #if SALEN | ||
984 | fprintf(stderr, " len=%x\n", ai->ai_addr->sa_len); | ||
985 | #endif /* SALEN */ | ||
986 | fprintf(stderr, " family=%x(%s)\n", ai->ai_addr->sa_family, nrl_afnumtoname(ai->ai_addr->sa_family)); | ||
987 | fprintf(stderr, " data="); | ||
988 | |||
989 | #if SALEN | ||
990 | if (ai->ai_addrlen != ai->ai_addr->sa_len) { | ||
991 | fprintf(stderr, " (addrlen != len, skipping)"); | ||
992 | } else | ||
993 | #endif /* SALEN */ | ||
994 | { | ||
995 | uint8_t *p; | ||
996 | int i; | ||
997 | |||
998 | p = (uint8_t *)ai->ai_addr->sa_data; | ||
999 | i = ai->ai_addrlen - ((void *)ai->ai_addr->sa_data - (void *)ai->ai_addr); | ||
1000 | while (i-- > 0) | ||
1001 | fprintf(stderr, "%02x", *(p++)); | ||
1002 | }; | ||
1003 | }; | ||
1004 | fprintf(stderr, "\n canonname=%08x", (unsigned int)ai->ai_canonname); | ||
1005 | if (ai->ai_canonname) | ||
1006 | fprintf(stderr, "(%s)", ai->ai_canonname); | ||
1007 | fprintf(stderr, "\n next=%08x\n", (unsigned int)ai->ai_next); | ||
1008 | |||
1009 | if (follownext && ai->ai_next) { | ||
1010 | ai = ai->ai_next; | ||
1011 | goto loop; | ||
1012 | }; | ||
1013 | }; | ||
1014 | #endif /* DEBUG */ | ||
1015 | |||
1016 | int getaddrinfo(const char *name, const char *service, | ||
1017 | const struct addrinfo *req, struct addrinfo **pai) | ||
1018 | { | ||
1019 | int rval = EAI_SYSTEM; /* XXX */ | ||
1020 | int i, j = 0; | ||
1021 | struct addrinfo *p = NULL, **end; | ||
1022 | struct gaih *g = gaih, *pg = NULL; | ||
1023 | struct gaih_service gaih_service, *pservice; | ||
1024 | |||
1025 | #if DEBUG | ||
1026 | if (DEBUG_MESSAGES) { | ||
1027 | fprintf(stderr, "getaddrinfo(name=%s, service=%s, req=%p, pai=%p)\n req: ", name ? name : "NULL", service ? service : "NULL", req, pai); | ||
1028 | |||
1029 | dump_addrinfo(req, 0); | ||
1030 | }; | ||
1031 | #endif /* DEBUG */ | ||
1032 | |||
1033 | if (name && (name[0] == '*') && !name[1]) | ||
1034 | name = NULL; | ||
1035 | |||
1036 | if (service && (service[0] == '*') && !service[1]) | ||
1037 | service = NULL; | ||
1038 | |||
1039 | #if BROKEN_LIKE_POSIX | ||
1040 | if (!name && !service && !(req.ai_flags & AI_EXT)) | ||
1041 | RETURN_ERROR(EAI_NONAME); | ||
1042 | #endif /* BROKEN_LIKE_POSIX */ | ||
1043 | |||
1044 | if (!req) | ||
1045 | req = &nullreq; | ||
1046 | |||
1047 | if (req->ai_flags & ~(AI_CANONNAME | AI_PASSIVE | AI_NUMERICHOST | AI_EXT)) | ||
1048 | RETURN_ERROR(EAI_BADFLAGS); | ||
1049 | |||
1050 | #if BROKEN_LIKE_POSIX | ||
1051 | if ((req->ai_flags & AI_CANONNAME) && !name && !(req.ai_flags & AI_EXT)) | ||
1052 | RETURN_ERROR(EAI_BADFLAGS); | ||
1053 | #endif /* BROKEN_LIKE_POSIX */ | ||
1054 | |||
1055 | if (service && *service) { | ||
1056 | char *c; | ||
1057 | gaih_service.num = strtoul(gaih_service.name = (void *)service, &c, 10); | ||
1058 | if (*c) { | ||
1059 | gaih_service.num = -1; | ||
1060 | } | ||
1061 | #if BROKEN_LIKE_POSIX | ||
1062 | else | ||
1063 | if (!req->ai_socktype && !(req.ai_flags & AI_EXT)) | ||
1064 | RETURN_ERROR(EAI_SERVICE); | ||
1065 | #endif /* BROKEN_LIKE_POSIX */ | ||
1066 | |||
1067 | pservice = &gaih_service; | ||
1068 | } else | ||
1069 | pservice = NULL; | ||
1070 | |||
1071 | if (pai) | ||
1072 | end = &p; | ||
1073 | else | ||
1074 | end = NULL; | ||
1075 | |||
1076 | while(g->gaih) { | ||
1077 | if ((req->ai_family == g->family) || !req->ai_family) { | ||
1078 | j++; | ||
1079 | if (!((pg && (pg->gaih == g->gaih)))) { | ||
1080 | pg = g; | ||
1081 | if (rval = g->gaih(name, pservice, req, end)) { | ||
1082 | if (!req->ai_family && (rval & GAIH_OKIFUNSPEC)) | ||
1083 | continue; | ||
1084 | |||
1085 | if (p) | ||
1086 | freeaddrinfo(p); | ||
1087 | |||
1088 | rval = -(rval & GAIH_EAI); | ||
1089 | goto ret; | ||
1090 | } | ||
1091 | if (end) | ||
1092 | while(*end) end = &((*end)->ai_next); | ||
1093 | } | ||
1094 | } | ||
1095 | g++; | ||
1096 | } | ||
1097 | |||
1098 | if (!j) | ||
1099 | RETURN_ERROR(EAI_FAMILY); | ||
1100 | |||
1101 | if (p) { | ||
1102 | *pai = p; | ||
1103 | #if DEBUG | ||
1104 | if (DEBUG_MESSAGES) { | ||
1105 | fprintf(stderr, "getaddrinfo: Success. *pai:\n"); | ||
1106 | dump_addrinfo(p, 1); | ||
1107 | }; | ||
1108 | #endif /* DEBUG */ | ||
1109 | rval = 0; | ||
1110 | goto ret; | ||
1111 | } | ||
1112 | |||
1113 | if (!pai && !rval) { | ||
1114 | rval = 0; | ||
1115 | goto ret; | ||
1116 | }; | ||
1117 | |||
1118 | RETURN_ERROR(EAI_NONAME); | ||
1119 | |||
1120 | ret: | ||
1121 | #if DEBUG | ||
1122 | if (DEBUG_MESSAGES) | ||
1123 | fprintf(stderr, "getaddrinfo=%d\n", rval); | ||
1124 | #endif /* DEBUG */ | ||
1125 | return rval; | ||
1126 | } | ||
1127 | |||