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