17d56d374SYoshinobu Inoue /* 27d56d374SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 37d56d374SYoshinobu Inoue * All rights reserved. 47d56d374SYoshinobu Inoue * 57d56d374SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 67d56d374SYoshinobu Inoue * modification, are permitted provided that the following conditions 77d56d374SYoshinobu Inoue * are met: 87d56d374SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 97d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 107d56d374SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 117d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 127d56d374SYoshinobu Inoue * documentation and/or other materials provided with the distribution. 137d56d374SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 147d56d374SYoshinobu Inoue * may be used to endorse or promote products derived from this software 157d56d374SYoshinobu Inoue * without specific prior written permission. 167d56d374SYoshinobu Inoue * 177d56d374SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 187d56d374SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 197d56d374SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 207d56d374SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 217d56d374SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 227d56d374SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 237d56d374SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 247d56d374SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 257d56d374SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 267d56d374SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 277d56d374SYoshinobu Inoue * SUCH DAMAGE. 287d56d374SYoshinobu Inoue * 297d56d374SYoshinobu Inoue * $FreeBSD$ 307d56d374SYoshinobu Inoue */ 317d56d374SYoshinobu Inoue 327d56d374SYoshinobu Inoue /* 337d56d374SYoshinobu Inoue * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. 347d56d374SYoshinobu Inoue * 357d56d374SYoshinobu Inoue * Issues to be discussed: 367d56d374SYoshinobu Inoue * - Thread safe-ness must be checked. 377d56d374SYoshinobu Inoue * - Return values. There are nonstandard return values defined and used 387d56d374SYoshinobu Inoue * in the source code. This is because RFC2553 is silent about which error 397d56d374SYoshinobu Inoue * code must be returned for which situation. 4000a8a579SYoshinobu Inoue * Note: 4100a8a579SYoshinobu Inoue * - We use getipnodebyname() just for thread-safeness. There's no intent 4200a8a579SYoshinobu Inoue * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to 4300a8a579SYoshinobu Inoue * getipnodebyname(). 4400a8a579SYoshinobu Inoue * - The code filters out AFs that are not supported by the kernel, 4500a8a579SYoshinobu Inoue * when globbing NULL hostname (to loopback, or wildcard). Is it the right 4600a8a579SYoshinobu Inoue * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 4700a8a579SYoshinobu Inoue * in ai_flags? 487d56d374SYoshinobu Inoue */ 497d56d374SYoshinobu Inoue 507d56d374SYoshinobu Inoue #include <sys/types.h> 517d56d374SYoshinobu Inoue #include <sys/param.h> 527d56d374SYoshinobu Inoue #include <sys/socket.h> 537d56d374SYoshinobu Inoue #include <net/if.h> 547d56d374SYoshinobu Inoue #include <netinet/in.h> 557d56d374SYoshinobu Inoue #include <arpa/inet.h> 567d56d374SYoshinobu Inoue #include <arpa/nameser.h> 577d56d374SYoshinobu Inoue #include <netdb.h> 587d56d374SYoshinobu Inoue #include <resolv.h> 597d56d374SYoshinobu Inoue #include <string.h> 607d56d374SYoshinobu Inoue #include <stdlib.h> 617d56d374SYoshinobu Inoue #include <stddef.h> 627d56d374SYoshinobu Inoue #include <ctype.h> 637d56d374SYoshinobu Inoue #include <unistd.h> 647d56d374SYoshinobu Inoue #include <stdio.h> 657d56d374SYoshinobu Inoue 667d56d374SYoshinobu Inoue #if defined(__KAME__) && defined(INET6) 677d56d374SYoshinobu Inoue # define FAITH 687d56d374SYoshinobu Inoue #endif 697d56d374SYoshinobu Inoue 707d56d374SYoshinobu Inoue #define SUCCESS 0 717d56d374SYoshinobu Inoue #define ANY 0 727d56d374SYoshinobu Inoue #define YES 1 737d56d374SYoshinobu Inoue #define NO 0 747d56d374SYoshinobu Inoue 757d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 }; 767d56d374SYoshinobu Inoue static const char in6_addrany[] = { 777d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 787d56d374SYoshinobu Inoue }; 797d56d374SYoshinobu Inoue static const char in_loopback[] = { 127, 0, 0, 1 }; 807d56d374SYoshinobu Inoue static const char in6_loopback[] = { 817d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 827d56d374SYoshinobu Inoue }; 837d56d374SYoshinobu Inoue 847d56d374SYoshinobu Inoue static const struct afd { 857d56d374SYoshinobu Inoue int a_af; 867d56d374SYoshinobu Inoue int a_addrlen; 877d56d374SYoshinobu Inoue int a_socklen; 887d56d374SYoshinobu Inoue int a_off; 897d56d374SYoshinobu Inoue const char *a_addrany; 907d56d374SYoshinobu Inoue const char *a_loopback; 917d56d374SYoshinobu Inoue int a_scoped; 927d56d374SYoshinobu Inoue } afdl [] = { 937d56d374SYoshinobu Inoue #ifdef INET6 947d56d374SYoshinobu Inoue #define N_INET6 0 957d56d374SYoshinobu Inoue {PF_INET6, sizeof(struct in6_addr), 967d56d374SYoshinobu Inoue sizeof(struct sockaddr_in6), 977d56d374SYoshinobu Inoue offsetof(struct sockaddr_in6, sin6_addr), 987d56d374SYoshinobu Inoue in6_addrany, in6_loopback, 1}, 997d56d374SYoshinobu Inoue #define N_INET 1 1007d56d374SYoshinobu Inoue #else 1017d56d374SYoshinobu Inoue #define N_INET 0 1027d56d374SYoshinobu Inoue #endif 1037d56d374SYoshinobu Inoue {PF_INET, sizeof(struct in_addr), 1047d56d374SYoshinobu Inoue sizeof(struct sockaddr_in), 1057d56d374SYoshinobu Inoue offsetof(struct sockaddr_in, sin_addr), 1067d56d374SYoshinobu Inoue in_addrany, in_loopback, 0}, 1077d56d374SYoshinobu Inoue {0, 0, 0, 0, NULL, NULL, 0}, 1087d56d374SYoshinobu Inoue }; 1097d56d374SYoshinobu Inoue 1107d56d374SYoshinobu Inoue struct explore { 1117d56d374SYoshinobu Inoue int e_af; 1127d56d374SYoshinobu Inoue int e_socktype; 1137d56d374SYoshinobu Inoue int e_protocol; 1147d56d374SYoshinobu Inoue const char *e_protostr; 1157d56d374SYoshinobu Inoue int e_wild; 1167d56d374SYoshinobu Inoue #define WILD_AF(ex) ((ex)->e_wild & 0x01) 1177d56d374SYoshinobu Inoue #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 1187d56d374SYoshinobu Inoue #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 1197d56d374SYoshinobu Inoue }; 1207d56d374SYoshinobu Inoue 1217d56d374SYoshinobu Inoue static const struct explore explore[] = { 1227d56d374SYoshinobu Inoue #ifdef INET6 1237d56d374SYoshinobu Inoue { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 1247d56d374SYoshinobu Inoue { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 1257d56d374SYoshinobu Inoue { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, 1267d56d374SYoshinobu Inoue #endif 1277d56d374SYoshinobu Inoue { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 1287d56d374SYoshinobu Inoue { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 1297d56d374SYoshinobu Inoue { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, 1307d56d374SYoshinobu Inoue { -1, 0, 0, NULL, 0 }, 1317d56d374SYoshinobu Inoue }; 1327d56d374SYoshinobu Inoue 1337d56d374SYoshinobu Inoue #ifdef INET6 1347d56d374SYoshinobu Inoue #define PTON_MAX 16 1357d56d374SYoshinobu Inoue #else 1367d56d374SYoshinobu Inoue #define PTON_MAX 4 1377d56d374SYoshinobu Inoue #endif 1387d56d374SYoshinobu Inoue 1397d56d374SYoshinobu Inoue 1407d56d374SYoshinobu Inoue static int str_isnumber __P((const char *)); 1417d56d374SYoshinobu Inoue static int explore_fqdn __P((const struct addrinfo *, const char *, 1427d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 1437d56d374SYoshinobu Inoue static int explore_null __P((const struct addrinfo *, const char *, 1447d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 1457d56d374SYoshinobu Inoue static int explore_numeric __P((const struct addrinfo *, const char *, 1467d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 1477d56d374SYoshinobu Inoue static int explore_numeric_scope __P((const struct addrinfo *, const char *, 1487d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 1497d56d374SYoshinobu Inoue static int get_canonname __P((const struct addrinfo *, 1507d56d374SYoshinobu Inoue struct addrinfo *, const char *)); 1517d56d374SYoshinobu Inoue static struct addrinfo *get_ai __P((const struct addrinfo *, 1527d56d374SYoshinobu Inoue const struct afd *, const char *)); 1537d56d374SYoshinobu Inoue static int get_portmatch __P((const struct addrinfo *, const char *)); 1547d56d374SYoshinobu Inoue static int get_port __P((struct addrinfo *, const char *, int)); 1557d56d374SYoshinobu Inoue static const struct afd *find_afd __P((int)); 1567d56d374SYoshinobu Inoue 1577d56d374SYoshinobu Inoue static char *ai_errlist[] = { 1587d56d374SYoshinobu Inoue "Success", 1597d56d374SYoshinobu Inoue "Address family for hostname not supported", /* EAI_ADDRFAMILY */ 1607d56d374SYoshinobu Inoue "Temporary failure in name resolution", /* EAI_AGAIN */ 1617d56d374SYoshinobu Inoue "Invalid value for ai_flags", /* EAI_BADFLAGS */ 1627d56d374SYoshinobu Inoue "Non-recoverable failure in name resolution", /* EAI_FAIL */ 1637d56d374SYoshinobu Inoue "ai_family not supported", /* EAI_FAMILY */ 1647d56d374SYoshinobu Inoue "Memory allocation failure", /* EAI_MEMORY */ 1657d56d374SYoshinobu Inoue "No address associated with hostname", /* EAI_NODATA */ 1667d56d374SYoshinobu Inoue "hostname nor servname provided, or not known", /* EAI_NONAME */ 1677d56d374SYoshinobu Inoue "servname not supported for ai_socktype", /* EAI_SERVICE */ 1687d56d374SYoshinobu Inoue "ai_socktype not supported", /* EAI_SOCKTYPE */ 1697d56d374SYoshinobu Inoue "System error returned in errno", /* EAI_SYSTEM */ 1707d56d374SYoshinobu Inoue "Invalid value for hints", /* EAI_BADHINTS */ 1717d56d374SYoshinobu Inoue "Resolved protocol is unknown", /* EAI_PROTOCOL */ 1727d56d374SYoshinobu Inoue "Argument res is NULL", /* EAI_RESNULL */ 1737d56d374SYoshinobu Inoue "Unknown error", /* EAI_MAX */ 1747d56d374SYoshinobu Inoue }; 1757d56d374SYoshinobu Inoue 1767d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */ 1777d56d374SYoshinobu Inoue 1787d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \ 1797d56d374SYoshinobu Inoue do { \ 1807d56d374SYoshinobu Inoue /* external reference: pai, error, and label free */ \ 1817d56d374SYoshinobu Inoue (ai) = get_ai(pai, (afd), (addr)); \ 1827d56d374SYoshinobu Inoue if ((ai) == NULL) { \ 1837d56d374SYoshinobu Inoue error = EAI_MEMORY; \ 1847d56d374SYoshinobu Inoue goto free; \ 1857d56d374SYoshinobu Inoue } \ 1867d56d374SYoshinobu Inoue } while (0) 1877d56d374SYoshinobu Inoue 1887d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \ 1897d56d374SYoshinobu Inoue do { \ 1907d56d374SYoshinobu Inoue /* external reference: error and label free */ \ 1917d56d374SYoshinobu Inoue error = get_port((ai), (serv), 0); \ 1927d56d374SYoshinobu Inoue if (error != 0) \ 1937d56d374SYoshinobu Inoue goto free; \ 1947d56d374SYoshinobu Inoue } while (0) 1957d56d374SYoshinobu Inoue 1967d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \ 1977d56d374SYoshinobu Inoue do { \ 1987d56d374SYoshinobu Inoue /* external reference: pai, error and label free */ \ 1997d56d374SYoshinobu Inoue error = get_canonname(pai, (ai), (str)); \ 2007d56d374SYoshinobu Inoue if (error != 0) \ 2017d56d374SYoshinobu Inoue goto free; \ 2027d56d374SYoshinobu Inoue } while (0) 2037d56d374SYoshinobu Inoue 2047d56d374SYoshinobu Inoue #define ERR(err) \ 2057d56d374SYoshinobu Inoue do { \ 2067d56d374SYoshinobu Inoue /* external reference: error, and label bad */ \ 2077d56d374SYoshinobu Inoue error = (err); \ 2087d56d374SYoshinobu Inoue goto bad; \ 2097d56d374SYoshinobu Inoue } while (0) 2107d56d374SYoshinobu Inoue 2117d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \ 2127d56d374SYoshinobu Inoue ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 2137d56d374SYoshinobu Inoue #define MATCH(x, y, w) \ 2147d56d374SYoshinobu Inoue ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY))) 2157d56d374SYoshinobu Inoue 2167d56d374SYoshinobu Inoue char * 2177d56d374SYoshinobu Inoue gai_strerror(ecode) 2187d56d374SYoshinobu Inoue int ecode; 2197d56d374SYoshinobu Inoue { 2207d56d374SYoshinobu Inoue if (ecode < 0 || ecode > EAI_MAX) 2217d56d374SYoshinobu Inoue ecode = EAI_MAX; 2227d56d374SYoshinobu Inoue return ai_errlist[ecode]; 2237d56d374SYoshinobu Inoue } 2247d56d374SYoshinobu Inoue 2257d56d374SYoshinobu Inoue void 2267d56d374SYoshinobu Inoue freeaddrinfo(ai) 2277d56d374SYoshinobu Inoue struct addrinfo *ai; 2287d56d374SYoshinobu Inoue { 2297d56d374SYoshinobu Inoue struct addrinfo *next; 2307d56d374SYoshinobu Inoue 2317d56d374SYoshinobu Inoue do { 2327d56d374SYoshinobu Inoue next = ai->ai_next; 2337d56d374SYoshinobu Inoue if (ai->ai_canonname) 2347d56d374SYoshinobu Inoue free(ai->ai_canonname); 2357d56d374SYoshinobu Inoue /* no need to free(ai->ai_addr) */ 2367d56d374SYoshinobu Inoue free(ai); 2377d56d374SYoshinobu Inoue } while ((ai = next) != NULL); 2387d56d374SYoshinobu Inoue } 2397d56d374SYoshinobu Inoue 2407d56d374SYoshinobu Inoue static int 2417d56d374SYoshinobu Inoue str_isnumber(p) 2427d56d374SYoshinobu Inoue const char *p; 2437d56d374SYoshinobu Inoue { 2447d56d374SYoshinobu Inoue char *q = (char *)p; 2457d56d374SYoshinobu Inoue while (*q) { 2467d56d374SYoshinobu Inoue if (! isdigit(*q)) 2477d56d374SYoshinobu Inoue return NO; 2487d56d374SYoshinobu Inoue q++; 2497d56d374SYoshinobu Inoue } 2507d56d374SYoshinobu Inoue return YES; 2517d56d374SYoshinobu Inoue } 2527d56d374SYoshinobu Inoue 2537d56d374SYoshinobu Inoue int 2547d56d374SYoshinobu Inoue getaddrinfo(hostname, servname, hints, res) 2557d56d374SYoshinobu Inoue const char *hostname, *servname; 2567d56d374SYoshinobu Inoue const struct addrinfo *hints; 2577d56d374SYoshinobu Inoue struct addrinfo **res; 2587d56d374SYoshinobu Inoue { 2597d56d374SYoshinobu Inoue struct addrinfo sentinel; 2607d56d374SYoshinobu Inoue struct addrinfo *cur; 2617d56d374SYoshinobu Inoue int error = 0; 2627d56d374SYoshinobu Inoue struct addrinfo ai; 2637d56d374SYoshinobu Inoue struct addrinfo ai0; 2647d56d374SYoshinobu Inoue struct addrinfo *pai; 2657d56d374SYoshinobu Inoue const struct afd *afd; 2667d56d374SYoshinobu Inoue const struct explore *ex; 2677d56d374SYoshinobu Inoue 2687d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 2697d56d374SYoshinobu Inoue cur = &sentinel; 2707d56d374SYoshinobu Inoue pai = &ai; 2717d56d374SYoshinobu Inoue pai->ai_flags = 0; 2727d56d374SYoshinobu Inoue pai->ai_family = PF_UNSPEC; 2737d56d374SYoshinobu Inoue pai->ai_socktype = ANY; 2747d56d374SYoshinobu Inoue pai->ai_protocol = ANY; 2757d56d374SYoshinobu Inoue pai->ai_addrlen = 0; 2767d56d374SYoshinobu Inoue pai->ai_canonname = NULL; 2777d56d374SYoshinobu Inoue pai->ai_addr = NULL; 2787d56d374SYoshinobu Inoue pai->ai_next = NULL; 2797d56d374SYoshinobu Inoue 2807d56d374SYoshinobu Inoue if (hostname == NULL && servname == NULL) 2817d56d374SYoshinobu Inoue return EAI_NONAME; 2827d56d374SYoshinobu Inoue if (res == NULL) 2837d56d374SYoshinobu Inoue return EAI_RESNULL; /* xxx */ 2847d56d374SYoshinobu Inoue if (hints) { 2857d56d374SYoshinobu Inoue /* error check for hints */ 2867d56d374SYoshinobu Inoue if (hints->ai_addrlen || hints->ai_canonname || 2877d56d374SYoshinobu Inoue hints->ai_addr || hints->ai_next) 2887d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); /* xxx */ 2897d56d374SYoshinobu Inoue if (hints->ai_flags & ~AI_MASK) 2907d56d374SYoshinobu Inoue ERR(EAI_BADFLAGS); 2917d56d374SYoshinobu Inoue switch (hints->ai_family) { 2927d56d374SYoshinobu Inoue case PF_UNSPEC: 2937d56d374SYoshinobu Inoue case PF_INET: 2947d56d374SYoshinobu Inoue #ifdef INET6 2957d56d374SYoshinobu Inoue case PF_INET6: 2967d56d374SYoshinobu Inoue #endif 2977d56d374SYoshinobu Inoue break; 2987d56d374SYoshinobu Inoue default: 2997d56d374SYoshinobu Inoue ERR(EAI_FAMILY); 3007d56d374SYoshinobu Inoue } 3017d56d374SYoshinobu Inoue memcpy(pai, hints, sizeof(*pai)); 3027d56d374SYoshinobu Inoue 3037d56d374SYoshinobu Inoue /* 3047d56d374SYoshinobu Inoue * if both socktype/protocol are specified, check if they 3057d56d374SYoshinobu Inoue * are meaningful combination. 3067d56d374SYoshinobu Inoue */ 3077d56d374SYoshinobu Inoue if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 3087d56d374SYoshinobu Inoue int matched = 0; 3097d56d374SYoshinobu Inoue 3107d56d374SYoshinobu Inoue for (ex = explore; ex->e_af >= 0; ex++) { 3117d56d374SYoshinobu Inoue if (pai->ai_family != ex->e_af) 3127d56d374SYoshinobu Inoue continue; 3137d56d374SYoshinobu Inoue if (ex->e_socktype == ANY) 3147d56d374SYoshinobu Inoue continue; 3157d56d374SYoshinobu Inoue if (ex->e_protocol == ANY) 3167d56d374SYoshinobu Inoue continue; 3177d56d374SYoshinobu Inoue if (pai->ai_socktype == ex->e_socktype 3187d56d374SYoshinobu Inoue && pai->ai_protocol == ex->e_protocol) 3197d56d374SYoshinobu Inoue matched = 1; 3207d56d374SYoshinobu Inoue else 3217d56d374SYoshinobu Inoue continue; 3227d56d374SYoshinobu Inoue if (matched == 0) 3237d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); 3247d56d374SYoshinobu Inoue } 3257d56d374SYoshinobu Inoue } 3267d56d374SYoshinobu Inoue } 3277d56d374SYoshinobu Inoue 3287d56d374SYoshinobu Inoue /* backup original pai contents */ 3297d56d374SYoshinobu Inoue ai0 = *pai; 3307d56d374SYoshinobu Inoue 3317d56d374SYoshinobu Inoue /* 3327d56d374SYoshinobu Inoue * special cases check for inet and inet6 sockets. 3337d56d374SYoshinobu Inoue * (1) servname is disallowed for raw sockets. 3347d56d374SYoshinobu Inoue * (2) numeric servname is disallowed if socktype/protocol is left 3357d56d374SYoshinobu Inoue * unspecified. 3367d56d374SYoshinobu Inoue */ 3377d56d374SYoshinobu Inoue if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 3387d56d374SYoshinobu Inoue #ifdef INET6 3397d56d374SYoshinobu Inoue || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 3407d56d374SYoshinobu Inoue #endif 3417d56d374SYoshinobu Inoue ) { 3427d56d374SYoshinobu Inoue *pai = ai0; 3437d56d374SYoshinobu Inoue 3447d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 3457d56d374SYoshinobu Inoue #ifdef INET6 3467d56d374SYoshinobu Inoue pai->ai_family = PF_INET6; 3477d56d374SYoshinobu Inoue #else 3487d56d374SYoshinobu Inoue pai->ai_family = PF_INET; 3497d56d374SYoshinobu Inoue #endif 3507d56d374SYoshinobu Inoue error = get_portmatch(pai, servname); 3517d56d374SYoshinobu Inoue if (error) 3527d56d374SYoshinobu Inoue ERR(error); 3537d56d374SYoshinobu Inoue } 3547d56d374SYoshinobu Inoue 3557d56d374SYoshinobu Inoue /* NULL hostname, or numeric hostname */ 3567d56d374SYoshinobu Inoue for (ex = explore; ex->e_af >= 0; ex++) { 3577d56d374SYoshinobu Inoue *pai = ai0; 3587d56d374SYoshinobu Inoue 3597d56d374SYoshinobu Inoue if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 3607d56d374SYoshinobu Inoue continue; 3617d56d374SYoshinobu Inoue if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) 3627d56d374SYoshinobu Inoue continue; 3637d56d374SYoshinobu Inoue if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) 3647d56d374SYoshinobu Inoue continue; 3657d56d374SYoshinobu Inoue 3667d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 3677d56d374SYoshinobu Inoue pai->ai_family = ex->e_af; 3687d56d374SYoshinobu Inoue if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 3697d56d374SYoshinobu Inoue pai->ai_socktype = ex->e_socktype; 3707d56d374SYoshinobu Inoue if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 3717d56d374SYoshinobu Inoue pai->ai_protocol = ex->e_protocol; 3727d56d374SYoshinobu Inoue 3737d56d374SYoshinobu Inoue if (hostname == NULL) 3747d56d374SYoshinobu Inoue error = explore_null(pai, hostname, servname, &cur->ai_next); 3757d56d374SYoshinobu Inoue else 3767d56d374SYoshinobu Inoue error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next); 3777d56d374SYoshinobu Inoue 3787d56d374SYoshinobu Inoue if (error) 3797d56d374SYoshinobu Inoue goto free; 3807d56d374SYoshinobu Inoue 3817d56d374SYoshinobu Inoue while (cur && cur->ai_next) 3827d56d374SYoshinobu Inoue cur = cur->ai_next; 3837d56d374SYoshinobu Inoue } 3847d56d374SYoshinobu Inoue 3857d56d374SYoshinobu Inoue /* 3867d56d374SYoshinobu Inoue * XXX 3877d56d374SYoshinobu Inoue * If numreic representation of AF1 can be interpreted as FQDN 3887d56d374SYoshinobu Inoue * representation of AF2, we need to think again about the code below. 3897d56d374SYoshinobu Inoue */ 3907d56d374SYoshinobu Inoue if (sentinel.ai_next) 3917d56d374SYoshinobu Inoue goto good; 3927d56d374SYoshinobu Inoue 3937d56d374SYoshinobu Inoue if (pai->ai_flags & AI_NUMERICHOST) 3947d56d374SYoshinobu Inoue ERR(EAI_NONAME); 3957d56d374SYoshinobu Inoue if (hostname == NULL) 3967d56d374SYoshinobu Inoue ERR(EAI_NONAME); 3977d56d374SYoshinobu Inoue 3987d56d374SYoshinobu Inoue /* 3997d56d374SYoshinobu Inoue * hostname as alphabetical name. 4007d56d374SYoshinobu Inoue * we would like to prefer AF_INET6 than AF_INET, so we'll make a 4017d56d374SYoshinobu Inoue * outer loop by AFs. 4027d56d374SYoshinobu Inoue */ 4037d56d374SYoshinobu Inoue for (afd = afdl; afd->a_af; afd++) { 4047d56d374SYoshinobu Inoue *pai = ai0; 4057d56d374SYoshinobu Inoue 4067d56d374SYoshinobu Inoue if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1)) 4077d56d374SYoshinobu Inoue continue; 4087d56d374SYoshinobu Inoue 4097d56d374SYoshinobu Inoue for (ex = explore; ex->e_af >= 0; ex++) { 4107d56d374SYoshinobu Inoue *pai = ai0; 4117d56d374SYoshinobu Inoue 4127d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 4137d56d374SYoshinobu Inoue pai->ai_family = afd->a_af; 4147d56d374SYoshinobu Inoue 4157d56d374SYoshinobu Inoue if (!MATCH_FAMILY(pai->ai_family, ex->e_af, 4167d56d374SYoshinobu Inoue WILD_AF(ex))) 4177d56d374SYoshinobu Inoue continue; 4187d56d374SYoshinobu Inoue if (!MATCH(pai->ai_socktype, ex->e_socktype, 4197d56d374SYoshinobu Inoue WILD_SOCKTYPE(ex))) { 4207d56d374SYoshinobu Inoue continue; 4217d56d374SYoshinobu Inoue } 4227d56d374SYoshinobu Inoue if (!MATCH(pai->ai_protocol, ex->e_protocol, 4237d56d374SYoshinobu Inoue WILD_PROTOCOL(ex))) { 4247d56d374SYoshinobu Inoue continue; 4257d56d374SYoshinobu Inoue } 4267d56d374SYoshinobu Inoue 4277d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 4287d56d374SYoshinobu Inoue pai->ai_family = ex->e_af; 4297d56d374SYoshinobu Inoue if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 4307d56d374SYoshinobu Inoue pai->ai_socktype = ex->e_socktype; 4317d56d374SYoshinobu Inoue if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 4327d56d374SYoshinobu Inoue pai->ai_protocol = ex->e_protocol; 4337d56d374SYoshinobu Inoue 4347d56d374SYoshinobu Inoue error = explore_fqdn(pai, hostname, servname, 4357d56d374SYoshinobu Inoue &cur->ai_next); 4367d56d374SYoshinobu Inoue 4377d56d374SYoshinobu Inoue while (cur && cur->ai_next) 4387d56d374SYoshinobu Inoue cur = cur->ai_next; 4397d56d374SYoshinobu Inoue } 4407d56d374SYoshinobu Inoue } 4417d56d374SYoshinobu Inoue 4427d56d374SYoshinobu Inoue /* XXX: if any addrinfo found, SUCCESS return even if (error != 0) */ 4437d56d374SYoshinobu Inoue if (sentinel.ai_next) { 4447d56d374SYoshinobu Inoue good: 4457d56d374SYoshinobu Inoue *res = sentinel.ai_next; 4467d56d374SYoshinobu Inoue return SUCCESS; 4477d56d374SYoshinobu Inoue } 4487d56d374SYoshinobu Inoue /* else, failed */ 4497d56d374SYoshinobu Inoue free: 4507d56d374SYoshinobu Inoue bad: 4517d56d374SYoshinobu Inoue if (error == 0) 4527d56d374SYoshinobu Inoue error = EAI_FAIL; 4537d56d374SYoshinobu Inoue if (sentinel.ai_next) 4547d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 4557d56d374SYoshinobu Inoue *res = NULL; 4567d56d374SYoshinobu Inoue return error; 4577d56d374SYoshinobu Inoue } 4587d56d374SYoshinobu Inoue 4597d56d374SYoshinobu Inoue /* 4607d56d374SYoshinobu Inoue * FQDN hostname, DNS lookup 4617d56d374SYoshinobu Inoue */ 4627d56d374SYoshinobu Inoue static int 4637d56d374SYoshinobu Inoue explore_fqdn(pai, hostname, servname, res) 4647d56d374SYoshinobu Inoue const struct addrinfo *pai; 4657d56d374SYoshinobu Inoue const char *hostname; 4667d56d374SYoshinobu Inoue const char *servname; 4677d56d374SYoshinobu Inoue struct addrinfo **res; 4687d56d374SYoshinobu Inoue { 4697d56d374SYoshinobu Inoue struct hostent *hp; 4707d56d374SYoshinobu Inoue int h_error; 4717d56d374SYoshinobu Inoue int af; 4727d56d374SYoshinobu Inoue char *ap; 4737d56d374SYoshinobu Inoue struct addrinfo sentinel, *cur; 4747d56d374SYoshinobu Inoue int i; 4757d56d374SYoshinobu Inoue const struct afd *afd; 4767d56d374SYoshinobu Inoue int error; 4777d56d374SYoshinobu Inoue 4787d56d374SYoshinobu Inoue *res = NULL; 4797d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 4807d56d374SYoshinobu Inoue cur = &sentinel; 4817d56d374SYoshinobu Inoue 4827d56d374SYoshinobu Inoue /* 4837d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 4847d56d374SYoshinobu Inoue */ 4857d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 4867d56d374SYoshinobu Inoue return 0; 4877d56d374SYoshinobu Inoue 4887d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 4897d56d374SYoshinobu Inoue if (afd == NULL) 4907d56d374SYoshinobu Inoue return 0; 4917d56d374SYoshinobu Inoue 4927d56d374SYoshinobu Inoue hp = getipnodebyname(hostname, pai->ai_family, AI_ADDRCONFIG, 4937d56d374SYoshinobu Inoue &h_error); 4947d56d374SYoshinobu Inoue if (hp == NULL) { 4957d56d374SYoshinobu Inoue switch (h_error) { 4967d56d374SYoshinobu Inoue case HOST_NOT_FOUND: 4977d56d374SYoshinobu Inoue case NO_DATA: 4987d56d374SYoshinobu Inoue error = EAI_NODATA; 4997d56d374SYoshinobu Inoue break; 5007d56d374SYoshinobu Inoue case TRY_AGAIN: 5017d56d374SYoshinobu Inoue error = EAI_AGAIN; 5027d56d374SYoshinobu Inoue break; 5037d56d374SYoshinobu Inoue case NO_RECOVERY: 5047d56d374SYoshinobu Inoue case NETDB_INTERNAL: 5057d56d374SYoshinobu Inoue default: 5067d56d374SYoshinobu Inoue error = EAI_FAIL; 5077d56d374SYoshinobu Inoue break; 5087d56d374SYoshinobu Inoue } 5097d56d374SYoshinobu Inoue } else if ((hp->h_name == NULL) || (hp->h_name[0] == 0) 5107d56d374SYoshinobu Inoue || (hp->h_addr_list[0] == NULL)) { 5117d56d374SYoshinobu Inoue freehostent(hp); 5127d56d374SYoshinobu Inoue hp = NULL; 5137d56d374SYoshinobu Inoue error = EAI_FAIL; 5147d56d374SYoshinobu Inoue } 5157d56d374SYoshinobu Inoue 5167d56d374SYoshinobu Inoue if (hp == NULL) 5177d56d374SYoshinobu Inoue goto free; 5187d56d374SYoshinobu Inoue 5197d56d374SYoshinobu Inoue for (i = 0; hp->h_addr_list[i] != NULL; i++) { 5207d56d374SYoshinobu Inoue af = hp->h_addrtype; 5217d56d374SYoshinobu Inoue ap = hp->h_addr_list[i]; 5227d56d374SYoshinobu Inoue 5237d56d374SYoshinobu Inoue if (af != pai->ai_family) 5247d56d374SYoshinobu Inoue continue; 5257d56d374SYoshinobu Inoue 5267d56d374SYoshinobu Inoue GET_AI(cur->ai_next, afd, ap); 5277d56d374SYoshinobu Inoue GET_PORT(cur->ai_next, servname); 52800a8a579SYoshinobu Inoue if ((pai->ai_flags & AI_CANONNAME) != 0) { 5297d56d374SYoshinobu Inoue /* 53000a8a579SYoshinobu Inoue * RFC2553 says that ai_canonname will be set only for 53100a8a579SYoshinobu Inoue * the first element. we do it for all the elements, 53200a8a579SYoshinobu Inoue * just for convenience. 5337d56d374SYoshinobu Inoue */ 53400a8a579SYoshinobu Inoue GET_CANONNAME(cur->ai_next, hp->h_name); 5357d56d374SYoshinobu Inoue } 5367d56d374SYoshinobu Inoue 5377d56d374SYoshinobu Inoue while (cur && cur->ai_next) 5387d56d374SYoshinobu Inoue cur = cur->ai_next; 5397d56d374SYoshinobu Inoue } 5407d56d374SYoshinobu Inoue 5417d56d374SYoshinobu Inoue *res = sentinel.ai_next; 5427d56d374SYoshinobu Inoue return 0; 5437d56d374SYoshinobu Inoue 5447d56d374SYoshinobu Inoue free: 5457d56d374SYoshinobu Inoue if (hp) 5467d56d374SYoshinobu Inoue freehostent(hp); 5477d56d374SYoshinobu Inoue if (sentinel.ai_next) 5487d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 5497d56d374SYoshinobu Inoue return error; 5507d56d374SYoshinobu Inoue } 5517d56d374SYoshinobu Inoue 5527d56d374SYoshinobu Inoue /* 5537d56d374SYoshinobu Inoue * hostname == NULL. 5547d56d374SYoshinobu Inoue * passive socket -> anyaddr (0.0.0.0 or ::) 5557d56d374SYoshinobu Inoue * non-passive socket -> localhost (127.0.0.1 or ::1) 5567d56d374SYoshinobu Inoue */ 5577d56d374SYoshinobu Inoue static int 5587d56d374SYoshinobu Inoue explore_null(pai, hostname, servname, res) 5597d56d374SYoshinobu Inoue const struct addrinfo *pai; 5607d56d374SYoshinobu Inoue const char *hostname; 5617d56d374SYoshinobu Inoue const char *servname; 5627d56d374SYoshinobu Inoue struct addrinfo **res; 5637d56d374SYoshinobu Inoue { 5647d56d374SYoshinobu Inoue int s; 5657d56d374SYoshinobu Inoue const struct afd *afd; 5667d56d374SYoshinobu Inoue struct addrinfo *cur; 5677d56d374SYoshinobu Inoue struct addrinfo sentinel; 5687d56d374SYoshinobu Inoue int error; 5697d56d374SYoshinobu Inoue 5707d56d374SYoshinobu Inoue *res = NULL; 5717d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 5727d56d374SYoshinobu Inoue cur = &sentinel; 5737d56d374SYoshinobu Inoue 5747d56d374SYoshinobu Inoue /* 5757d56d374SYoshinobu Inoue * filter out AFs that are not supported by the kernel 5767d56d374SYoshinobu Inoue * XXX errno? 5777d56d374SYoshinobu Inoue */ 5787d56d374SYoshinobu Inoue s = socket(pai->ai_family, SOCK_DGRAM, 0); 5797d56d374SYoshinobu Inoue if (s < 0) 5807d56d374SYoshinobu Inoue return 0; 5819233c4d9SJason Evans _close(s); 5827d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 5837d56d374SYoshinobu Inoue if (afd == NULL) 5847d56d374SYoshinobu Inoue return 0; 5857d56d374SYoshinobu Inoue 5867d56d374SYoshinobu Inoue GET_AI(cur->ai_next, afd, 5877d56d374SYoshinobu Inoue (pai->ai_flags & AI_PASSIVE) ? afd->a_addrany : afd->a_loopback 5887d56d374SYoshinobu Inoue ); 5897d56d374SYoshinobu Inoue /* xxx meaningless? 5907d56d374SYoshinobu Inoue * GET_CANONNAME(cur->ai_next, "anyaddr"); 5917d56d374SYoshinobu Inoue * or 5927d56d374SYoshinobu Inoue * GET_CANONNAME(cur->ai_next, "localhost"); 5937d56d374SYoshinobu Inoue */ 5947d56d374SYoshinobu Inoue /* if the servname does not match socktype/protocol, ignored */ 5957d56d374SYoshinobu Inoue GET_PORT(cur->ai_next, servname); 5967d56d374SYoshinobu Inoue 5977d56d374SYoshinobu Inoue *res = sentinel.ai_next; 5987d56d374SYoshinobu Inoue return 0; 5997d56d374SYoshinobu Inoue 6007d56d374SYoshinobu Inoue free: 6017d56d374SYoshinobu Inoue if (sentinel.ai_next) 6027d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 6037d56d374SYoshinobu Inoue return error; 6047d56d374SYoshinobu Inoue } 6057d56d374SYoshinobu Inoue 6067d56d374SYoshinobu Inoue /* 6077d56d374SYoshinobu Inoue * numeric hostname 6087d56d374SYoshinobu Inoue */ 6097d56d374SYoshinobu Inoue static int 6107d56d374SYoshinobu Inoue explore_numeric(pai, hostname, servname, res) 6117d56d374SYoshinobu Inoue const struct addrinfo *pai; 6127d56d374SYoshinobu Inoue const char *hostname; 6137d56d374SYoshinobu Inoue const char *servname; 6147d56d374SYoshinobu Inoue struct addrinfo **res; 6157d56d374SYoshinobu Inoue { 6167d56d374SYoshinobu Inoue const struct afd *afd; 6177d56d374SYoshinobu Inoue struct addrinfo *cur; 6187d56d374SYoshinobu Inoue struct addrinfo sentinel; 6197d56d374SYoshinobu Inoue int error; 6207d56d374SYoshinobu Inoue char pton[PTON_MAX]; 6217d56d374SYoshinobu Inoue int flags; 6227d56d374SYoshinobu Inoue 6237d56d374SYoshinobu Inoue *res = NULL; 6247d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 6257d56d374SYoshinobu Inoue cur = &sentinel; 6267d56d374SYoshinobu Inoue 6277d56d374SYoshinobu Inoue /* 6287d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 6297d56d374SYoshinobu Inoue */ 6307d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 6317d56d374SYoshinobu Inoue return 0; 6327d56d374SYoshinobu Inoue 6337d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 6347d56d374SYoshinobu Inoue if (afd == NULL) 6357d56d374SYoshinobu Inoue return 0; 6367d56d374SYoshinobu Inoue flags = pai->ai_flags; 6377d56d374SYoshinobu Inoue 6387d56d374SYoshinobu Inoue if (inet_pton(afd->a_af, hostname, pton) == 1) { 6397d56d374SYoshinobu Inoue if (pai->ai_family == afd->a_af || 6407d56d374SYoshinobu Inoue pai->ai_family == PF_UNSPEC /*?*/) { 6417d56d374SYoshinobu Inoue GET_AI(cur->ai_next, afd, pton); 6427d56d374SYoshinobu Inoue GET_PORT(cur->ai_next, servname); 6437d56d374SYoshinobu Inoue while (cur && cur->ai_next) 6447d56d374SYoshinobu Inoue cur = cur->ai_next; 6457d56d374SYoshinobu Inoue } else 6467d56d374SYoshinobu Inoue ERR(EAI_FAMILY); /*xxx*/ 6477d56d374SYoshinobu Inoue } 6487d56d374SYoshinobu Inoue 6497d56d374SYoshinobu Inoue *res = sentinel.ai_next; 6507d56d374SYoshinobu Inoue return 0; 6517d56d374SYoshinobu Inoue 6527d56d374SYoshinobu Inoue free: 6537d56d374SYoshinobu Inoue bad: 6547d56d374SYoshinobu Inoue if (sentinel.ai_next) 6557d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 6567d56d374SYoshinobu Inoue return error; 6577d56d374SYoshinobu Inoue } 6587d56d374SYoshinobu Inoue 6597d56d374SYoshinobu Inoue /* 6607d56d374SYoshinobu Inoue * numeric hostname with scope 6617d56d374SYoshinobu Inoue */ 6627d56d374SYoshinobu Inoue static int 6637d56d374SYoshinobu Inoue explore_numeric_scope(pai, hostname, servname, res) 6647d56d374SYoshinobu Inoue const struct addrinfo *pai; 6657d56d374SYoshinobu Inoue const char *hostname; 6667d56d374SYoshinobu Inoue const char *servname; 6677d56d374SYoshinobu Inoue struct addrinfo **res; 6687d56d374SYoshinobu Inoue { 6697d56d374SYoshinobu Inoue #ifndef SCOPE_DELIMITER 6707d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 6717d56d374SYoshinobu Inoue #else 6727d56d374SYoshinobu Inoue const struct afd *afd; 6737d56d374SYoshinobu Inoue struct addrinfo *cur; 6747d56d374SYoshinobu Inoue int error; 6757d56d374SYoshinobu Inoue char *cp, *hostname2 = NULL; 6767d56d374SYoshinobu Inoue int scope; 6777d56d374SYoshinobu Inoue #ifdef INET6 6787d56d374SYoshinobu Inoue struct sockaddr_in6 *sin6; 6797d56d374SYoshinobu Inoue #endif 6807d56d374SYoshinobu Inoue 6817d56d374SYoshinobu Inoue /* 6827d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 6837d56d374SYoshinobu Inoue */ 6847d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 6857d56d374SYoshinobu Inoue return 0; 6867d56d374SYoshinobu Inoue 6877d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 6887d56d374SYoshinobu Inoue if (afd == NULL) 6897d56d374SYoshinobu Inoue return 0; 6907d56d374SYoshinobu Inoue if (!afd->a_scoped) 6917d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 6927d56d374SYoshinobu Inoue 6937d56d374SYoshinobu Inoue cp = strchr(hostname, SCOPE_DELIMITER); 6947d56d374SYoshinobu Inoue if (cp == NULL) 6957d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 6967d56d374SYoshinobu Inoue 6977d56d374SYoshinobu Inoue /* 6987d56d374SYoshinobu Inoue * Handle special case of <scoped_address><delimiter><scope id> 6997d56d374SYoshinobu Inoue */ 7007d56d374SYoshinobu Inoue hostname2 = strdup(hostname); 7017d56d374SYoshinobu Inoue if (hostname2 == NULL) 7027d56d374SYoshinobu Inoue return EAI_MEMORY; 7037d56d374SYoshinobu Inoue /* terminate at the delimiter */ 7047d56d374SYoshinobu Inoue hostname2[cp - hostname] = '\0'; 7057d56d374SYoshinobu Inoue 7067d56d374SYoshinobu Inoue cp++; 7077d56d374SYoshinobu Inoue switch (pai->ai_family) { 7087d56d374SYoshinobu Inoue #ifdef INET6 7097d56d374SYoshinobu Inoue case AF_INET6: 7107d56d374SYoshinobu Inoue scope = if_nametoindex(cp); 7119b59fde4SYoshinobu Inoue if (scope == 0) { 7129b59fde4SYoshinobu Inoue error = EAI_SYSTEM; 7139b59fde4SYoshinobu Inoue goto free; 7149b59fde4SYoshinobu Inoue } 7157d56d374SYoshinobu Inoue break; 7167d56d374SYoshinobu Inoue #endif 7177d56d374SYoshinobu Inoue } 7187d56d374SYoshinobu Inoue 7197d56d374SYoshinobu Inoue error = explore_numeric(pai, hostname2, servname, res); 7207d56d374SYoshinobu Inoue if (error == 0) { 7217d56d374SYoshinobu Inoue for (cur = *res; cur; cur = cur->ai_next) { 7227d56d374SYoshinobu Inoue #ifdef INET6 7237d56d374SYoshinobu Inoue if (cur->ai_family != AF_INET6) 7247d56d374SYoshinobu Inoue continue; 7257d56d374SYoshinobu Inoue sin6 = (struct sockaddr_in6 *)cur->ai_addr; 7267d56d374SYoshinobu Inoue if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) || 7277d56d374SYoshinobu Inoue IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr)) 7287d56d374SYoshinobu Inoue sin6->sin6_scope_id = scope; 7297d56d374SYoshinobu Inoue #endif 7307d56d374SYoshinobu Inoue } 7317d56d374SYoshinobu Inoue } 7327d56d374SYoshinobu Inoue 7339b59fde4SYoshinobu Inoue #ifdef INET6 7349b59fde4SYoshinobu Inoue free: 7359b59fde4SYoshinobu Inoue #endif 7367d56d374SYoshinobu Inoue free(hostname2); 7377d56d374SYoshinobu Inoue 7387d56d374SYoshinobu Inoue return error; 7397d56d374SYoshinobu Inoue #endif 7407d56d374SYoshinobu Inoue } 7417d56d374SYoshinobu Inoue 7427d56d374SYoshinobu Inoue static int 7437d56d374SYoshinobu Inoue get_canonname(pai, ai, str) 7447d56d374SYoshinobu Inoue const struct addrinfo *pai; 7457d56d374SYoshinobu Inoue struct addrinfo *ai; 7467d56d374SYoshinobu Inoue const char *str; 7477d56d374SYoshinobu Inoue { 7487d56d374SYoshinobu Inoue if ((pai->ai_flags & AI_CANONNAME) != 0) { 7497d56d374SYoshinobu Inoue ai->ai_canonname = (char *)malloc(strlen(str) + 1); 7507d56d374SYoshinobu Inoue if (ai->ai_canonname == NULL) 7517d56d374SYoshinobu Inoue return EAI_MEMORY; 7527d56d374SYoshinobu Inoue strcpy(ai->ai_canonname, str); 7537d56d374SYoshinobu Inoue } 7547d56d374SYoshinobu Inoue return 0; 7557d56d374SYoshinobu Inoue } 7567d56d374SYoshinobu Inoue 7577d56d374SYoshinobu Inoue static struct addrinfo * 7587d56d374SYoshinobu Inoue get_ai(pai, afd, addr) 7597d56d374SYoshinobu Inoue const struct addrinfo *pai; 7607d56d374SYoshinobu Inoue const struct afd *afd; 7617d56d374SYoshinobu Inoue const char *addr; 7627d56d374SYoshinobu Inoue { 7637d56d374SYoshinobu Inoue char *p; 7647d56d374SYoshinobu Inoue struct addrinfo *ai; 7657d56d374SYoshinobu Inoue #ifdef FAITH 7667d56d374SYoshinobu Inoue struct in6_addr faith_prefix; 7677d56d374SYoshinobu Inoue char *fp_str; 7687d56d374SYoshinobu Inoue int translate = 0; 7697d56d374SYoshinobu Inoue #endif 7707d56d374SYoshinobu Inoue 7717d56d374SYoshinobu Inoue #ifdef FAITH 7727d56d374SYoshinobu Inoue /* 7737d56d374SYoshinobu Inoue * Transfrom an IPv4 addr into a special IPv6 addr format for 7747d56d374SYoshinobu Inoue * IPv6->IPv4 translation gateway. (only TCP is supported now) 7757d56d374SYoshinobu Inoue * 7767d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 7777d56d374SYoshinobu Inoue * | faith prefix part (12 bytes) | embedded | 7787d56d374SYoshinobu Inoue * | | IPv4 addr part (4 bytes) 7797d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 7807d56d374SYoshinobu Inoue * 7817d56d374SYoshinobu Inoue * faith prefix part is specified as ascii IPv6 addr format 7827d56d374SYoshinobu Inoue * in environmental variable GAI. 7837d56d374SYoshinobu Inoue * For FAITH to work correctly, routing to faith prefix must be 7847d56d374SYoshinobu Inoue * setup toward a machine where a FAITH daemon operates. 7857d56d374SYoshinobu Inoue * Also, the machine must enable some mechanizm 7867d56d374SYoshinobu Inoue * (e.g. faith interface hack) to divert those packet with 7877d56d374SYoshinobu Inoue * faith prefixed destination addr to user-land FAITH daemon. 7887d56d374SYoshinobu Inoue */ 7897d56d374SYoshinobu Inoue fp_str = getenv("GAI"); 7907d56d374SYoshinobu Inoue if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 7917d56d374SYoshinobu Inoue afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 7927d56d374SYoshinobu Inoue u_int32_t v4a; 7937d56d374SYoshinobu Inoue u_int8_t v4a_top; 7947d56d374SYoshinobu Inoue 7957d56d374SYoshinobu Inoue memcpy(&v4a, addr, sizeof v4a); 7967d56d374SYoshinobu Inoue v4a_top = v4a >> IN_CLASSA_NSHIFT; 7977d56d374SYoshinobu Inoue if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 7987d56d374SYoshinobu Inoue v4a_top != 0 && v4a != IN_LOOPBACKNET) { 7997d56d374SYoshinobu Inoue afd = &afdl[N_INET6]; 8007d56d374SYoshinobu Inoue memcpy(&faith_prefix.s6_addr[12], addr, 8017d56d374SYoshinobu Inoue sizeof(struct in_addr)); 8027d56d374SYoshinobu Inoue translate = 1; 8037d56d374SYoshinobu Inoue } 8047d56d374SYoshinobu Inoue } 8057d56d374SYoshinobu Inoue #endif 8067d56d374SYoshinobu Inoue 8077d56d374SYoshinobu Inoue ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 8087d56d374SYoshinobu Inoue + (afd->a_socklen)); 8097d56d374SYoshinobu Inoue if (ai == NULL) 8107d56d374SYoshinobu Inoue return NULL; 8117d56d374SYoshinobu Inoue 8127d56d374SYoshinobu Inoue memcpy(ai, pai, sizeof(struct addrinfo)); 8137d56d374SYoshinobu Inoue ai->ai_addr = (struct sockaddr *)(ai + 1); 8147d56d374SYoshinobu Inoue memset(ai->ai_addr, 0, afd->a_socklen); 8157d56d374SYoshinobu Inoue ai->ai_addr->sa_len = afd->a_socklen; 8167d56d374SYoshinobu Inoue ai->ai_addrlen = afd->a_socklen; 8177d56d374SYoshinobu Inoue ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 8187d56d374SYoshinobu Inoue p = (char *)(ai->ai_addr); 8197d56d374SYoshinobu Inoue #ifdef FAITH 8207d56d374SYoshinobu Inoue if (translate == 1) 8217d56d374SYoshinobu Inoue memcpy(p + afd->a_off, &faith_prefix, afd->a_addrlen); 8227d56d374SYoshinobu Inoue else 8237d56d374SYoshinobu Inoue #endif 8247d56d374SYoshinobu Inoue memcpy(p + afd->a_off, addr, afd->a_addrlen); 8257d56d374SYoshinobu Inoue 8267d56d374SYoshinobu Inoue return ai; 8277d56d374SYoshinobu Inoue } 8287d56d374SYoshinobu Inoue 8297d56d374SYoshinobu Inoue static int 8307d56d374SYoshinobu Inoue get_portmatch(ai, servname) 8317d56d374SYoshinobu Inoue const struct addrinfo *ai; 8327d56d374SYoshinobu Inoue const char *servname; 8337d56d374SYoshinobu Inoue { 8347d56d374SYoshinobu Inoue /* get_port does not touch first argument. when matchonly == 1. */ 8357d56d374SYoshinobu Inoue return get_port((struct addrinfo *)ai, servname, 1); 8367d56d374SYoshinobu Inoue } 8377d56d374SYoshinobu Inoue 8387d56d374SYoshinobu Inoue static int 8397d56d374SYoshinobu Inoue get_port(ai, servname, matchonly) 8407d56d374SYoshinobu Inoue struct addrinfo *ai; 8417d56d374SYoshinobu Inoue const char *servname; 8427d56d374SYoshinobu Inoue int matchonly; 8437d56d374SYoshinobu Inoue { 8447d56d374SYoshinobu Inoue const char *proto; 8457d56d374SYoshinobu Inoue struct servent *sp; 8467d56d374SYoshinobu Inoue int port; 8477d56d374SYoshinobu Inoue int allownumeric; 8487d56d374SYoshinobu Inoue 8497d56d374SYoshinobu Inoue if (servname == NULL) 8507d56d374SYoshinobu Inoue return 0; 8517d56d374SYoshinobu Inoue if (ai->ai_family != AF_INET 8527d56d374SYoshinobu Inoue #ifdef INET6 8537d56d374SYoshinobu Inoue && ai->ai_family != AF_INET6 8547d56d374SYoshinobu Inoue #endif 8557d56d374SYoshinobu Inoue ) 8567d56d374SYoshinobu Inoue return 0; 8577d56d374SYoshinobu Inoue 8587d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 8597d56d374SYoshinobu Inoue case SOCK_RAW: 8607d56d374SYoshinobu Inoue return EAI_SERVICE; 8617d56d374SYoshinobu Inoue case SOCK_DGRAM: 8627d56d374SYoshinobu Inoue case SOCK_STREAM: 8637d56d374SYoshinobu Inoue allownumeric = 1; 8647d56d374SYoshinobu Inoue break; 8657d56d374SYoshinobu Inoue case ANY: 8667d56d374SYoshinobu Inoue allownumeric = 0; 8677d56d374SYoshinobu Inoue break; 8687d56d374SYoshinobu Inoue default: 8697d56d374SYoshinobu Inoue return EAI_SOCKTYPE; 8707d56d374SYoshinobu Inoue } 8717d56d374SYoshinobu Inoue 8727d56d374SYoshinobu Inoue if (str_isnumber(servname)) { 8737d56d374SYoshinobu Inoue if (!allownumeric) 8747d56d374SYoshinobu Inoue return EAI_SERVICE; 8757d56d374SYoshinobu Inoue port = htons(atoi(servname)); 8767d56d374SYoshinobu Inoue if (port < 0 || port > 65535) 8777d56d374SYoshinobu Inoue return EAI_SERVICE; 8787d56d374SYoshinobu Inoue } else { 8797d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 8807d56d374SYoshinobu Inoue case SOCK_DGRAM: 8817d56d374SYoshinobu Inoue proto = "udp"; 8827d56d374SYoshinobu Inoue break; 8837d56d374SYoshinobu Inoue case SOCK_STREAM: 8847d56d374SYoshinobu Inoue proto = "tcp"; 8857d56d374SYoshinobu Inoue break; 8867d56d374SYoshinobu Inoue default: 8877d56d374SYoshinobu Inoue proto = NULL; 8887d56d374SYoshinobu Inoue break; 8897d56d374SYoshinobu Inoue } 8907d56d374SYoshinobu Inoue 8917d56d374SYoshinobu Inoue if ((sp = getservbyname(servname, proto)) == NULL) 8927d56d374SYoshinobu Inoue return EAI_SERVICE; 8937d56d374SYoshinobu Inoue port = sp->s_port; 8947d56d374SYoshinobu Inoue } 8957d56d374SYoshinobu Inoue 8967d56d374SYoshinobu Inoue if (!matchonly) { 8977d56d374SYoshinobu Inoue switch (ai->ai_family) { 8987d56d374SYoshinobu Inoue case AF_INET: 8997d56d374SYoshinobu Inoue ((struct sockaddr_in *)ai->ai_addr)->sin_port = port; 9007d56d374SYoshinobu Inoue break; 9017d56d374SYoshinobu Inoue #ifdef INET6 9027d56d374SYoshinobu Inoue case AF_INET6: 9037d56d374SYoshinobu Inoue ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = port; 9047d56d374SYoshinobu Inoue break; 9057d56d374SYoshinobu Inoue #endif 9067d56d374SYoshinobu Inoue } 9077d56d374SYoshinobu Inoue } 9087d56d374SYoshinobu Inoue 9097d56d374SYoshinobu Inoue return 0; 9107d56d374SYoshinobu Inoue } 9117d56d374SYoshinobu Inoue 9127d56d374SYoshinobu Inoue static const struct afd * 9137d56d374SYoshinobu Inoue find_afd(af) 9147d56d374SYoshinobu Inoue int af; 9157d56d374SYoshinobu Inoue { 9167d56d374SYoshinobu Inoue const struct afd *afd; 9177d56d374SYoshinobu Inoue 9187d56d374SYoshinobu Inoue if (af == PF_UNSPEC) 9197d56d374SYoshinobu Inoue return NULL; 9207d56d374SYoshinobu Inoue for (afd = afdl; afd->a_af; afd++) { 9217d56d374SYoshinobu Inoue if (afd->a_af == af) 9227d56d374SYoshinobu Inoue return afd; 9237d56d374SYoshinobu Inoue } 9247d56d374SYoshinobu Inoue return NULL; 9257d56d374SYoshinobu Inoue } 926