]> The Tcpdump Group git mirrors - tcpdump/blob - missing/getaddrinfo.c
- enable build outside of the tree
[tcpdump] / missing / getaddrinfo.c
1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * 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. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
32 *
33 * Issues to be discussed:
34 * - Thread safe-ness must be checked.
35 * - Return values. There are nonstandard return values defined and used
36 * in the source code. This is because RFC2553 is silent about which error
37 * code must be returned for which situation.
38 * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
39 */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #if 0
48 #include <sys/sysctl.h>
49 #endif
50 #include <sys/socket.h>
51 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <arpa/nameser.h>
55 #include <netdb.h>
56 #include <resolv.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #include <stddef.h>
60 #include <ctype.h>
61 #include <unistd.h>
62 #include <stdio.h>
63
64 #ifndef HAVE_PORTABLE_PROTOTYPE
65 #include "cdecl_ext.h"
66 #endif
67
68 #if 0
69 #ifndef HAVE_U_INT32_T
70 #include "bittypes.h"
71 #endif
72 #endif
73
74 #ifndef HAVE_SOCKADDR_STORAGE
75 #include "sockstorage.h"
76 #endif
77
78 #ifndef HAVE_ADDRINFO
79 #include "addrinfo.h"
80 #endif
81
82 #if defined(__KAME__) && defined(INET6)
83 # define FAITH
84 #endif
85
86 #define SUCCESS 0
87 #define ANY 0
88 #define YES 1
89 #define NO 0
90
91 #ifdef FAITH
92 static int translate = NO;
93 static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
94 #endif
95
96 static const char in_addrany[] = { 0, 0, 0, 0 };
97 static const char in6_addrany[] = {
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
99 };
100 static const char in_loopback[] = { 127, 0, 0, 1 };
101 static const char in6_loopback[] = {
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
103 };
104
105 struct sockinet {
106 u_char si_len;
107 u_char si_family;
108 u_short si_port;
109 u_int32_t si_scope_id;
110 };
111
112 static const struct afd {
113 int a_af;
114 int a_addrlen;
115 int a_socklen;
116 int a_off;
117 const char *a_addrany;
118 const char *a_loopback;
119 int a_scoped;
120 } afdl [] = {
121 #ifdef INET6
122 {PF_INET6, sizeof(struct in6_addr),
123 sizeof(struct sockaddr_in6),
124 offsetof(struct sockaddr_in6, sin6_addr),
125 in6_addrany, in6_loopback, 1},
126 #endif
127 {PF_INET, sizeof(struct in_addr),
128 sizeof(struct sockaddr_in),
129 offsetof(struct sockaddr_in, sin_addr),
130 in_addrany, in_loopback, 0},
131 {0, 0, 0, 0, NULL, NULL, 0},
132 };
133
134 struct explore {
135 int e_af;
136 int e_socktype;
137 int e_protocol;
138 const char *e_protostr;
139 int e_wild;
140 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
141 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
142 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
143 };
144
145 static const struct explore explore[] = {
146 #if 0
147 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
148 #endif
149 #ifdef INET6
150 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
151 { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
152 { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
153 #endif
154 { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
155 { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
156 { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
157 { -1, 0, 0, NULL, 0 },
158 };
159
160 #ifdef INET6
161 #define PTON_MAX 16
162 #else
163 #define PTON_MAX 4
164 #endif
165
166
167 static int str_isnumber __P((const char *));
168 static int explore_fqdn __P((const struct addrinfo *, const char *,
169 const char *, struct addrinfo **));
170 static int explore_null __P((const struct addrinfo *, const char *,
171 const char *, struct addrinfo **));
172 static int explore_numeric __P((const struct addrinfo *, const char *,
173 const char *, struct addrinfo **));
174 static int explore_numeric_scope __P((const struct addrinfo *, const char *,
175 const char *, struct addrinfo **));
176 static int get_name __P((const char *, const struct afd *, struct addrinfo **,
177 char *, const struct addrinfo *, const char *));
178 static int get_canonname __P((const struct addrinfo *,
179 struct addrinfo *, const char *));
180 static struct addrinfo *get_ai __P((const struct addrinfo *,
181 const struct afd *, const char *));
182 static int get_portmatch __P((const struct addrinfo *, const char *));
183 static int get_port __P((struct addrinfo *, const char *, int));
184 static const struct afd *find_afd __P((int));
185
186 static char *ai_errlist[] = {
187 "Success",
188 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
189 "Temporary failure in name resolution", /* EAI_AGAIN */
190 "Invalid value for ai_flags", /* EAI_BADFLAGS */
191 "Non-recoverable failure in name resolution", /* EAI_FAIL */
192 "ai_family not supported", /* EAI_FAMILY */
193 "Memory allocation failure", /* EAI_MEMORY */
194 "No address associated with hostname", /* EAI_NODATA */
195 "hostname nor servname provided, or not known", /* EAI_NONAME */
196 "servname not supported for ai_socktype", /* EAI_SERVICE */
197 "ai_socktype not supported", /* EAI_SOCKTYPE */
198 "System error returned in errno", /* EAI_SYSTEM */
199 "Invalid value for hints", /* EAI_BADHINTS */
200 "Resolved protocol is unknown", /* EAI_PROTOCOL */
201 "Unknown error", /* EAI_MAX */
202 };
203
204 /* XXX macros that make external reference is BAD. */
205
206 #define GET_AI(ai, afd, addr) \
207 do { \
208 /* external reference: pai, error, and label free */ \
209 (ai) = get_ai(pai, (afd), (addr)); \
210 if ((ai) == NULL) { \
211 error = EAI_MEMORY; \
212 goto free; \
213 } \
214 } while (0)
215
216 #define GET_PORT(ai, serv) \
217 do { \
218 /* external reference: error and label free */ \
219 error = get_port((ai), (serv), 0); \
220 if (error != 0) \
221 goto free; \
222 } while (0)
223
224 #define GET_CANONNAME(ai, str) \
225 do { \
226 /* external reference: pai, error and label free */ \
227 error = get_canonname(pai, (ai), (str)); \
228 if (error != 0) \
229 goto free; \
230 } while (0)
231
232 #define ERR(err) \
233 do { \
234 /* external reference: error, and label bad */ \
235 error = (err); \
236 goto bad; \
237 } while (0)
238
239 #define MATCH_FAMILY(x, y, w) \
240 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
241 #define MATCH(x, y, w) \
242 ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
243
244 char *
245 gai_strerror(ecode)
246 int ecode;
247 {
248 if (ecode < 0 || ecode > EAI_MAX)
249 ecode = EAI_MAX;
250 return ai_errlist[ecode];
251 }
252
253 void
254 freeaddrinfo(ai)
255 struct addrinfo *ai;
256 {
257 struct addrinfo *next;
258
259 do {
260 next = ai->ai_next;
261 if (ai->ai_canonname)
262 free(ai->ai_canonname);
263 /* no need to free(ai->ai_addr) */
264 free(ai);
265 } while ((ai = next) != NULL);
266 }
267
268 static int
269 str_isnumber(p)
270 const char *p;
271 {
272 char *q = (char *)p;
273 while (*q) {
274 if (! isdigit(*q))
275 return NO;
276 q++;
277 }
278 return YES;
279 }
280
281 int
282 getaddrinfo(hostname, servname, hints, res)
283 const char *hostname, *servname;
284 const struct addrinfo *hints;
285 struct addrinfo **res;
286 {
287 struct addrinfo sentinel;
288 struct addrinfo *cur;
289 int error = 0;
290 struct addrinfo ai;
291 struct addrinfo ai0;
292 struct addrinfo *pai;
293 const struct afd *afd;
294 const struct explore *ex;
295
296 #ifdef FAITH
297 static int firsttime = 1;
298
299 if (firsttime) {
300 /* translator hack */
301 char *q = getenv("GAI");
302 if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
303 translate = YES;
304 firsttime = 0;
305 }
306 #endif
307
308 sentinel.ai_next = NULL;
309 cur = &sentinel;
310 pai = &ai;
311 pai->ai_flags = 0;
312 pai->ai_family = PF_UNSPEC;
313 pai->ai_socktype = ANY;
314 pai->ai_protocol = ANY;
315 pai->ai_addrlen = 0;
316 pai->ai_canonname = NULL;
317 pai->ai_addr = NULL;
318 pai->ai_next = NULL;
319
320 if (hostname == NULL && servname == NULL)
321 return EAI_NONAME;
322 if (hints) {
323 /* error check for hints */
324 if (hints->ai_addrlen || hints->ai_canonname ||
325 hints->ai_addr || hints->ai_next)
326 ERR(EAI_BADHINTS); /* xxx */
327 if (hints->ai_flags & ~AI_MASK)
328 ERR(EAI_BADFLAGS);
329 switch (hints->ai_family) {
330 case PF_UNSPEC:
331 case PF_INET:
332 #ifdef INET6
333 case PF_INET6:
334 #endif
335 break;
336 default:
337 ERR(EAI_FAMILY);
338 }
339 memcpy(pai, hints, sizeof(*pai));
340
341 /*
342 * if both socktype/protocol are specified, check if they
343 * are meaningful combination.
344 */
345 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
346 for (ex = explore; ex->e_af >= 0; ex++) {
347 if (pai->ai_family != ex->e_af)
348 continue;
349 if (ex->e_socktype == ANY)
350 continue;
351 if (ex->e_protocol == ANY)
352 continue;
353 if (pai->ai_socktype == ex->e_socktype
354 && pai->ai_protocol != ex->e_protocol) {
355 ERR(EAI_BADHINTS);
356 }
357 }
358 }
359 }
360
361 /*
362 * check for special cases. (1) numeric servname is disallowed if
363 * socktype/protocol are left unspecified. (2) servname is disallowed
364 * for raw and other inet{,6} sockets.
365 */
366 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
367 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)) {
368 ai0 = *pai;
369
370 if (pai->ai_family == PF_UNSPEC)
371 pai->ai_family = PF_INET6;
372 error = get_portmatch(pai, servname);
373 if (error)
374 ERR(error);
375
376 *pai = ai0;
377 }
378
379 ai0 = *pai;
380
381 /* NULL hostname, or numeric hostname */
382 for (ex = explore; ex->e_af >= 0; ex++) {
383 *pai = ai0;
384
385 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
386 continue;
387 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
388 continue;
389 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
390 continue;
391
392 if (pai->ai_family == PF_UNSPEC)
393 pai->ai_family = ex->e_af;
394 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
395 pai->ai_socktype = ex->e_socktype;
396 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
397 pai->ai_protocol = ex->e_protocol;
398
399 if (hostname == NULL)
400 error = explore_null(pai, hostname, servname, &cur->ai_next);
401 else
402 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
403
404 if (error)
405 goto free;
406
407 while (cur && cur->ai_next)
408 cur = cur->ai_next;
409 }
410
411 /*
412 * XXX
413 * If numreic representation of AF1 can be interpreted as FQDN
414 * representation of AF2, we need to think again about the code below.
415 */
416 if (sentinel.ai_next)
417 goto good;
418
419 if (pai->ai_flags & AI_NUMERICHOST)
420 ERR(EAI_NONAME);
421 if (hostname == NULL)
422 ERR(EAI_NONAME);
423
424 /*
425 * hostname as alphabetical name.
426 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
427 * outer loop by AFs.
428 */
429 for (afd = afdl; afd->a_af; afd++) {
430 *pai = ai0;
431
432 if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
433 continue;
434
435 for (ex = explore; ex->e_af >= 0; ex++) {
436 *pai = ai0;
437
438 if (pai->ai_family == PF_UNSPEC)
439 pai->ai_family = afd->a_af;
440
441 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
442 continue;
443 if (!MATCH(pai->ai_socktype, ex->e_socktype,
444 WILD_SOCKTYPE(ex))) {
445 continue;
446 }
447 if (!MATCH(pai->ai_protocol, ex->e_protocol,
448 WILD_PROTOCOL(ex))) {
449 continue;
450 }
451
452 if (pai->ai_family == PF_UNSPEC)
453 pai->ai_family = ex->e_af;
454 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
455 pai->ai_socktype = ex->e_socktype;
456 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
457 pai->ai_protocol = ex->e_protocol;
458
459 error = explore_fqdn(pai, hostname, servname,
460 &cur->ai_next);
461
462 while (cur && cur->ai_next)
463 cur = cur->ai_next;
464 }
465 }
466
467 /* XXX */
468 if (sentinel.ai_next)
469 error = 0;
470
471 if (error)
472 goto free;
473 if (error == 0) {
474 if (sentinel.ai_next) {
475 good:
476 *res = sentinel.ai_next;
477 return SUCCESS;
478 } else
479 error = EAI_FAIL;
480 }
481 free:
482 bad:
483 if (sentinel.ai_next)
484 freeaddrinfo(sentinel.ai_next);
485 *res = NULL;
486 return error;
487 }
488
489 /*
490 * FQDN hostname, DNS lookup
491 */
492 static int
493 explore_fqdn(pai, hostname, servname, res)
494 const struct addrinfo *pai;
495 const char *hostname;
496 const char *servname;
497 struct addrinfo **res;
498 {
499 int s;
500 struct hostent *hp;
501 int h_error;
502 int af;
503 char *ap;
504 struct addrinfo sentinel, *cur;
505 int i;
506 const struct afd *afd;
507 int error;
508
509 *res = NULL;
510 sentinel.ai_next = NULL;
511 cur = &sentinel;
512
513 /*
514 * filter out AFs that are not supported by the kernel
515 * XXX errno?
516 */
517 s = socket(pai->ai_family, SOCK_DGRAM, 0);
518 if (s < 0)
519 return 0;
520 close(s);
521
522 /*
523 * if the servname does not match socktype/protocol, ignore it.
524 */
525 if (get_portmatch(pai, servname) != 0)
526 return 0;
527
528 afd = find_afd(pai->ai_family);
529
530 #ifdef USE_GETIPNODEBY
531 hp = getipnodebyname(hostname, pai->ai_family, AI_ADDRCONFIG, &h_error);
532 #else
533 hp = gethostbyname2(hostname, pai->ai_family);
534 h_error = h_errno;
535 #endif
536
537 if (hp == NULL) {
538 switch (h_error) {
539 case HOST_NOT_FOUND:
540 case NO_DATA:
541 error = EAI_NODATA;
542 break;
543 case TRY_AGAIN:
544 error = EAI_AGAIN;
545 break;
546 case NO_RECOVERY:
547 case NETDB_INTERNAL:
548 default:
549 error = EAI_FAIL;
550 break;
551 }
552 } else if ((hp->h_name == NULL) || (hp->h_name[0] == 0)
553 || (hp->h_addr_list[0] == NULL)) {
554 #ifdef USE_GETIPNODEBY
555 freehostent(hp);
556 #endif
557 hp = NULL;
558 error = EAI_FAIL;
559 }
560
561 if (hp == NULL)
562 goto free;
563
564 for (i = 0; hp->h_addr_list[i] != NULL; i++) {
565 af = hp->h_addrtype;
566 ap = hp->h_addr_list[i];
567 #ifdef INET6
568 if (af == AF_INET6
569 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
570 af = AF_INET;
571 ap = ap + sizeof(struct in6_addr)
572 - sizeof(struct in_addr);
573 }
574 #endif
575
576 if (af != pai->ai_family)
577 continue;
578
579 if ((pai->ai_flags & AI_CANONNAME) == 0) {
580 GET_AI(cur->ai_next, afd, ap);
581 GET_PORT(cur->ai_next, servname);
582 } else {
583 /*
584 * if AI_CANONNAME and if reverse lookup
585 * fail, return ai anyway to pacify
586 * calling application.
587 *
588 * XXX getaddrinfo() is a name->address
589 * translation function, and it looks
590 * strange that we do addr->name
591 * translation here.
592 */
593 get_name(ap, afd, &cur->ai_next,
594 ap, pai, servname);
595 }
596
597 while (cur && cur->ai_next)
598 cur = cur->ai_next;
599 }
600
601 *res = sentinel.ai_next;
602 return 0;
603
604 free:
605 #ifdef USE_GETIPNODEBY
606 if (hp)
607 freehostent(hp);
608 #endif
609 if (sentinel.ai_next)
610 freeaddrinfo(sentinel.ai_next);
611 return error;
612 }
613
614 /*
615 * hostname == NULL.
616 * passive socket -> anyaddr (0.0.0.0 or ::)
617 * non-passive socket -> localhost (127.0.0.1 or ::1)
618 */
619 static int
620 explore_null(pai, hostname, servname, res)
621 const struct addrinfo *pai;
622 const char *hostname;
623 const char *servname;
624 struct addrinfo **res;
625 {
626 int s;
627 const struct afd *afd;
628 struct addrinfo *cur;
629 struct addrinfo sentinel;
630 int error;
631
632 *res = NULL;
633 sentinel.ai_next = NULL;
634 cur = &sentinel;
635
636 /*
637 * filter out AFs that are not supported by the kernel
638 * XXX errno?
639 */
640 s = socket(pai->ai_family, SOCK_DGRAM, 0);
641 if (s < 0)
642 return 0;
643 close(s);
644
645 /*
646 * if the servname does not match socktype/protocol, ignore it.
647 */
648 if (get_portmatch(pai, servname) != 0)
649 return 0;
650
651 afd = find_afd(pai->ai_family);
652
653 if (pai->ai_flags & AI_PASSIVE) {
654 GET_AI(cur->ai_next, afd, afd->a_addrany);
655 /* xxx meaningless?
656 * GET_CANONNAME(cur->ai_next, "anyaddr");
657 */
658 GET_PORT(cur->ai_next, servname);
659 } else {
660 GET_AI(cur->ai_next, afd, afd->a_loopback);
661 /* xxx meaningless?
662 * GET_CANONNAME(cur->ai_next, "localhost");
663 */
664 GET_PORT(cur->ai_next, servname);
665 }
666 cur = cur->ai_next;
667
668 *res = sentinel.ai_next;
669 return 0;
670
671 free:
672 if (sentinel.ai_next)
673 freeaddrinfo(sentinel.ai_next);
674 return error;
675 }
676
677 /*
678 * numeric hostname
679 */
680 static int
681 explore_numeric(pai, hostname, servname, res)
682 const struct addrinfo *pai;
683 const char *hostname;
684 const char *servname;
685 struct addrinfo **res;
686 {
687 const struct afd *afd;
688 struct addrinfo *cur;
689 struct addrinfo sentinel;
690 int error;
691 char pton[PTON_MAX];
692 int flags;
693
694 *res = NULL;
695 sentinel.ai_next = NULL;
696 cur = &sentinel;
697
698 /*
699 * if the servname does not match socktype/protocol, ignore it.
700 */
701 if (get_portmatch(pai, servname) != 0)
702 return 0;
703
704 afd = find_afd(pai->ai_family);
705 flags = pai->ai_flags;
706
707 if (inet_pton(afd->a_af, hostname, pton) == 1) {
708 u_int32_t v4a;
709 #ifdef INET6
710 u_char pfx;
711 #endif
712
713 switch (afd->a_af) {
714 case AF_INET:
715 v4a = (u_int32_t)ntohl(((struct in_addr *)pton)->s_addr);
716 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
717 flags &= ~AI_CANONNAME;
718 v4a >>= IN_CLASSA_NSHIFT;
719 if (v4a == 0 || v4a == IN_LOOPBACKNET)
720 flags &= ~AI_CANONNAME;
721 break;
722 #ifdef INET6
723 case AF_INET6:
724 pfx = ((struct in6_addr *)pton)->s6_addr[0];
725 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
726 flags &= ~AI_CANONNAME;
727 break;
728 #endif
729 }
730
731 if (pai->ai_family == afd->a_af ||
732 pai->ai_family == PF_UNSPEC /*?*/) {
733 if ((flags & AI_CANONNAME) == 0) {
734 GET_AI(cur->ai_next, afd, pton);
735 GET_PORT(cur->ai_next, servname);
736 } else {
737 /*
738 * if AI_CANONNAME and if reverse lookup
739 * fail, return ai anyway to pacify
740 * calling application.
741 *
742 * XXX getaddrinfo() is a name->address
743 * translation function, and it looks
744 * strange that we do addr->name
745 * translation here.
746 */
747 get_name(pton, afd, &cur->ai_next,
748 pton, pai, servname);
749 }
750 while (cur && cur->ai_next)
751 cur = cur->ai_next;
752 } else
753 ERR(EAI_FAMILY); /*xxx*/
754 }
755
756 *res = sentinel.ai_next;
757 return 0;
758
759 free:
760 bad:
761 if (sentinel.ai_next)
762 freeaddrinfo(sentinel.ai_next);
763 return error;
764 }
765
766 /*
767 * numeric hostname with scope
768 */
769 static int
770 explore_numeric_scope(pai, hostname, servname, res)
771 const struct addrinfo *pai;
772 const char *hostname;
773 const char *servname;
774 struct addrinfo **res;
775 {
776 #ifndef SCOPE_DELIMITER
777 return explore_numeric(pai, hostname, servname, res);
778 #else
779 const struct afd *afd;
780 struct addrinfo *cur;
781 int error;
782 char *cp, *hostname2 = NULL;
783 int scope;
784 struct sockaddr_in6 *sin6;
785
786 /*
787 * if the servname does not match socktype/protocol, ignore it.
788 */
789 if (get_portmatch(pai, servname) != 0)
790 return 0;
791
792 afd = find_afd(pai->ai_family);
793 if (!afd->a_scoped)
794 return explore_numeric(pai, hostname, servname, res);
795
796 cp = strchr(hostname, SCOPE_DELIMITER);
797 if (cp == NULL)
798 return explore_numeric(pai, hostname, servname, res);
799
800 /*
801 * Handle special case of <scoped_address><delimiter><scope id>
802 */
803 hostname2 = strdup(hostname);
804 if (hostname2 == NULL)
805 return EAI_MEMORY;
806 /* terminate at the delimiter */
807 hostname2[cp - hostname] = '\0';
808
809 cp++;
810 switch (pai->ai_family) {
811 #ifdef INET6
812 case AF_INET6:
813 scope = if_nametoindex(cp);
814 break;
815 #endif
816 }
817
818 error = explore_numeric(pai, hostname2, servname, res);
819 if (error == 0) {
820 for (cur = *res; cur; cur = cur->ai_next) {
821 if (cur->ai_family != AF_INET6)
822 continue;
823 sin6 = (struct sockaddr_in6 *)cur->ai_addr;
824 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
825 IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
826 sin6->sin6_scope_id = scope;
827 }
828 }
829
830 free(hostname2);
831
832 return error;
833 #endif
834 }
835
836 static int
837 get_name(addr, afd, res, numaddr, pai, servname)
838 const char *addr;
839 const struct afd *afd;
840 struct addrinfo **res;
841 char *numaddr;
842 const struct addrinfo *pai;
843 const char *servname;
844 {
845 struct hostent *hp;
846 struct addrinfo *cur;
847 int error = 0;
848 #ifdef USE_GETIPNODEBY
849 int h_error;
850
851 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
852 #else
853 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
854 #endif
855 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
856 GET_AI(cur, afd, hp->h_addr_list[0]);
857 GET_PORT(cur, servname);
858 GET_CANONNAME(cur, hp->h_name);
859 } else {
860 GET_AI(cur, afd, numaddr);
861 GET_PORT(cur, servname);
862 }
863
864 #ifdef USE_GETIPNODEBY
865 if (hp)
866 freehostent(hp);
867 #endif
868 *res = cur;
869 return SUCCESS;
870 free:
871 if (cur)
872 freeaddrinfo(cur);
873 #ifdef USE_GETIPNODEBY
874 if (hp)
875 freehostent(hp);
876 #endif
877 /* bad: */
878 *res = NULL;
879 return error;
880 }
881
882 static int
883 get_canonname(pai, ai, str)
884 const struct addrinfo *pai;
885 struct addrinfo *ai;
886 const char *str;
887 {
888 if ((pai->ai_flags & AI_CANONNAME) != 0) {
889 ai->ai_canonname = (char *)malloc(strlen(str) + 1);
890 if (ai->ai_canonname == NULL)
891 return EAI_MEMORY;
892 strcpy(ai->ai_canonname, str);
893 }
894 return 0;
895 }
896
897 static struct addrinfo *
898 get_ai(pai, afd, addr)
899 const struct addrinfo *pai;
900 const struct afd *afd;
901 const char *addr;
902 {
903 char *p;
904 struct addrinfo *ai;
905
906 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
907 + (afd->a_socklen));
908 if (ai == NULL)
909 return NULL;
910
911 memcpy(ai, pai, sizeof(struct addrinfo));
912 ai->ai_addr = (struct sockaddr *)(ai + 1);
913 memset(ai->ai_addr, 0, afd->a_socklen);
914 #ifdef HAVE_SOCKADDR_SA_LEN
915 ai->ai_addr->sa_len = afd->a_socklen;
916 #endif
917 ai->ai_addrlen = afd->a_socklen;
918 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
919 p = (char *)(ai->ai_addr);
920 memcpy(p + afd->a_off, addr, afd->a_addrlen);
921 return ai;
922 }
923
924 static int
925 get_portmatch(ai, servname)
926 const struct addrinfo *ai;
927 const char *servname;
928 {
929 /* get_port does not touch first argument. when matchonly == 1. */
930 return get_port((struct addrinfo *)ai, servname, 1);
931 }
932
933 static int
934 get_port(ai, servname, matchonly)
935 struct addrinfo *ai;
936 const char *servname;
937 int matchonly;
938 {
939 const char *proto;
940 struct servent *sp;
941 int port;
942 int allownumeric;
943
944 if (servname == NULL)
945 return 0;
946 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
947 return 0;
948
949 switch (ai->ai_socktype) {
950 case SOCK_RAW:
951 return EAI_SERVICE;
952 case SOCK_DGRAM:
953 case SOCK_STREAM:
954 allownumeric = 1;
955 break;
956 case ANY:
957 allownumeric = 0;
958 break;
959 default:
960 return EAI_SOCKTYPE;
961 }
962
963 if (str_isnumber(servname)) {
964 if (!allownumeric)
965 return EAI_SERVICE;
966 port = htons(atoi(servname));
967 if (port < 0 || port > 65535)
968 return EAI_SERVICE;
969 } else {
970 switch (ai->ai_socktype) {
971 case SOCK_DGRAM:
972 proto = "udp";
973 break;
974 case SOCK_STREAM:
975 proto = "tcp";
976 break;
977 default:
978 proto = NULL;
979 break;
980 }
981
982 if ((sp = getservbyname(servname, proto)) == NULL)
983 return EAI_SERVICE;
984 port = sp->s_port;
985 }
986
987 if (!matchonly) {
988 switch (ai->ai_family) {
989 case AF_INET:
990 ((struct sockaddr_in *)ai->ai_addr)->sin_port = port;
991 break;
992 #ifdef INET6
993 case AF_INET6:
994 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = port;
995 break;
996 #endif
997 }
998 }
999
1000 return 0;
1001 }
1002
1003 static const struct afd *
1004 find_afd(af)
1005 int af;
1006 {
1007 const struct afd *afd;
1008
1009 if (af == PF_UNSPEC)
1010 return NULL;
1011 for (afd = afdl; afd->a_af; afd++) {
1012 if (afd->a_af == af)
1013 return afd;
1014 }
1015 return NULL;
1016 }