1b826397aSJun-ichiro itojun Hagino /* $FreeBSD$ */ 2ec20fe00SJun-ichiro itojun Hagino /* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */ 3b826397aSJun-ichiro itojun Hagino 47d56d374SYoshinobu Inoue /* 57d56d374SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 67d56d374SYoshinobu Inoue * All rights reserved. 77d56d374SYoshinobu Inoue * 87d56d374SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 97d56d374SYoshinobu Inoue * modification, are permitted provided that the following conditions 107d56d374SYoshinobu Inoue * are met: 117d56d374SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 127d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 137d56d374SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 147d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 157d56d374SYoshinobu Inoue * documentation and/or other materials provided with the distribution. 167d56d374SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 177d56d374SYoshinobu Inoue * may be used to endorse or promote products derived from this software 187d56d374SYoshinobu Inoue * without specific prior written permission. 197d56d374SYoshinobu Inoue * 207d56d374SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 217d56d374SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 227d56d374SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 237d56d374SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 247d56d374SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 257d56d374SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 267d56d374SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 277d56d374SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 287d56d374SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 297d56d374SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 307d56d374SYoshinobu Inoue * SUCH DAMAGE. 317d56d374SYoshinobu Inoue */ 327d56d374SYoshinobu Inoue 337d56d374SYoshinobu Inoue /* 347d56d374SYoshinobu Inoue * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. 357d56d374SYoshinobu Inoue * 367d56d374SYoshinobu Inoue * Issues to be discussed: 377d56d374SYoshinobu Inoue * - Thread safe-ness must be checked. 387d56d374SYoshinobu Inoue * - Return values. There are nonstandard return values defined and used 397d56d374SYoshinobu Inoue * in the source code. This is because RFC2553 is silent about which error 407d56d374SYoshinobu Inoue * code must be returned for which situation. 41a42af91cSHajimu UMEMOTO * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is 42a42af91cSHajimu UMEMOTO * invalid. 43a42af91cSHajimu UMEMOTO * current code - SEGV on freeaddrinfo(NULL) 4400a8a579SYoshinobu Inoue * Note: 4500a8a579SYoshinobu Inoue * - We use getipnodebyname() just for thread-safeness. There's no intent 4600a8a579SYoshinobu Inoue * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to 4700a8a579SYoshinobu Inoue * getipnodebyname(). 4800a8a579SYoshinobu Inoue * - The code filters out AFs that are not supported by the kernel, 4900a8a579SYoshinobu Inoue * when globbing NULL hostname (to loopback, or wildcard). Is it the right 5000a8a579SYoshinobu Inoue * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 5100a8a579SYoshinobu Inoue * in ai_flags? 52a42af91cSHajimu UMEMOTO * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. 53a42af91cSHajimu UMEMOTO * (1) what should we do against numeric hostname (2) what should we do 54a42af91cSHajimu UMEMOTO * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? 55a42af91cSHajimu UMEMOTO * non-loopback address configured? global address configured? 56a42af91cSHajimu UMEMOTO * - To avoid search order issue, we have a big amount of code duplicate 57a42af91cSHajimu UMEMOTO * from gethnamaddr.c and some other places. The issues that there's no 58a42af91cSHajimu UMEMOTO * lower layer function to lookup "IPv4 or IPv6" record. Calling 59a42af91cSHajimu UMEMOTO * gethostbyname2 from getaddrinfo will end up in wrong search order, as 60a42af91cSHajimu UMEMOTO * follows: 61a42af91cSHajimu UMEMOTO * - The code makes use of following calls when asked to resolver with 62a42af91cSHajimu UMEMOTO * ai_family = PF_UNSPEC: 63a42af91cSHajimu UMEMOTO * getipnodebyname(host, AF_INET6); 64a42af91cSHajimu UMEMOTO * getipnodebyname(host, AF_INET); 65a42af91cSHajimu UMEMOTO * This will result in the following queries if the node is configure to 66a42af91cSHajimu UMEMOTO * prefer /etc/hosts than DNS: 67a42af91cSHajimu UMEMOTO * lookup /etc/hosts for IPv6 address 68a42af91cSHajimu UMEMOTO * lookup DNS for IPv6 address 69a42af91cSHajimu UMEMOTO * lookup /etc/hosts for IPv4 address 70a42af91cSHajimu UMEMOTO * lookup DNS for IPv4 address 71a42af91cSHajimu UMEMOTO * which may not meet people's requirement. 72a42af91cSHajimu UMEMOTO * The right thing to happen is to have underlying layer which does 73a42af91cSHajimu UMEMOTO * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. 74a42af91cSHajimu UMEMOTO * This would result in a bit of code duplicate with _dns_ghbyname() and 75a42af91cSHajimu UMEMOTO * friends. 76a42af91cSHajimu UMEMOTO */ 77a42af91cSHajimu UMEMOTO /* 78a42af91cSHajimu UMEMOTO * diffs with other KAME platforms: 79a42af91cSHajimu UMEMOTO * - other KAME platforms already nuked FAITH ($GAI), but as FreeBSD 80a42af91cSHajimu UMEMOTO * 4.0-RELEASE supplies it, we still have the code here. 81a42af91cSHajimu UMEMOTO * - EAI_RESNULL support 82a42af91cSHajimu UMEMOTO * - AI_ADDRCONFIG support is supplied 83a42af91cSHajimu UMEMOTO * - EDNS0 support is not available due to resolver differences 84a42af91cSHajimu UMEMOTO * - some of FreeBSD style (#define tabify and others) 85a42af91cSHajimu UMEMOTO * - AI_ADDRCONFIG is turned on by default. 86a42af91cSHajimu UMEMOTO * - classful IPv4 numeric (127.1) is allowed. 877d56d374SYoshinobu Inoue */ 887d56d374SYoshinobu Inoue 897d56d374SYoshinobu Inoue #include <sys/types.h> 907d56d374SYoshinobu Inoue #include <sys/param.h> 917d56d374SYoshinobu Inoue #include <sys/socket.h> 927d56d374SYoshinobu Inoue #include <net/if.h> 937d56d374SYoshinobu Inoue #include <netinet/in.h> 947d56d374SYoshinobu Inoue #include <arpa/inet.h> 957d56d374SYoshinobu Inoue #include <arpa/nameser.h> 967d56d374SYoshinobu Inoue #include <netdb.h> 977d56d374SYoshinobu Inoue #include <resolv.h> 987d56d374SYoshinobu Inoue #include <string.h> 997d56d374SYoshinobu Inoue #include <stdlib.h> 1007d56d374SYoshinobu Inoue #include <stddef.h> 1017d56d374SYoshinobu Inoue #include <ctype.h> 1027d56d374SYoshinobu Inoue #include <unistd.h> 1037d56d374SYoshinobu Inoue #include <stdio.h> 104a42af91cSHajimu UMEMOTO #include <errno.h> 1057d56d374SYoshinobu Inoue 1067d56d374SYoshinobu Inoue #if defined(__KAME__) && defined(INET6) 1077d56d374SYoshinobu Inoue # define FAITH 1087d56d374SYoshinobu Inoue #endif 1097d56d374SYoshinobu Inoue 1107d56d374SYoshinobu Inoue #define SUCCESS 0 1117d56d374SYoshinobu Inoue #define ANY 0 1127d56d374SYoshinobu Inoue #define YES 1 1137d56d374SYoshinobu Inoue #define NO 0 1147d56d374SYoshinobu Inoue 1157d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 }; 1167d56d374SYoshinobu Inoue static const char in6_addrany[] = { 1177d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 1187d56d374SYoshinobu Inoue }; 1197d56d374SYoshinobu Inoue static const char in_loopback[] = { 127, 0, 0, 1 }; 1207d56d374SYoshinobu Inoue static const char in6_loopback[] = { 1217d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 1227d56d374SYoshinobu Inoue }; 1237d56d374SYoshinobu Inoue 1247d56d374SYoshinobu Inoue static const struct afd { 1257d56d374SYoshinobu Inoue int a_af; 1267d56d374SYoshinobu Inoue int a_addrlen; 1277d56d374SYoshinobu Inoue int a_socklen; 1287d56d374SYoshinobu Inoue int a_off; 1297d56d374SYoshinobu Inoue const char *a_addrany; 1307d56d374SYoshinobu Inoue const char *a_loopback; 1317d56d374SYoshinobu Inoue int a_scoped; 1327d56d374SYoshinobu Inoue } afdl [] = { 1337d56d374SYoshinobu Inoue #ifdef INET6 1347d56d374SYoshinobu Inoue #define N_INET6 0 1357d56d374SYoshinobu Inoue {PF_INET6, sizeof(struct in6_addr), 1367d56d374SYoshinobu Inoue sizeof(struct sockaddr_in6), 1377d56d374SYoshinobu Inoue offsetof(struct sockaddr_in6, sin6_addr), 1387d56d374SYoshinobu Inoue in6_addrany, in6_loopback, 1}, 1397d56d374SYoshinobu Inoue #define N_INET 1 1407d56d374SYoshinobu Inoue #else 1417d56d374SYoshinobu Inoue #define N_INET 0 1427d56d374SYoshinobu Inoue #endif 1437d56d374SYoshinobu Inoue {PF_INET, sizeof(struct in_addr), 1447d56d374SYoshinobu Inoue sizeof(struct sockaddr_in), 1457d56d374SYoshinobu Inoue offsetof(struct sockaddr_in, sin_addr), 1467d56d374SYoshinobu Inoue in_addrany, in_loopback, 0}, 1477d56d374SYoshinobu Inoue {0, 0, 0, 0, NULL, NULL, 0}, 1487d56d374SYoshinobu Inoue }; 1497d56d374SYoshinobu Inoue 1507d56d374SYoshinobu Inoue struct explore { 151a42af91cSHajimu UMEMOTO int e_af; 1527d56d374SYoshinobu Inoue int e_socktype; 1537d56d374SYoshinobu Inoue int e_protocol; 1547d56d374SYoshinobu Inoue const char *e_protostr; 1557d56d374SYoshinobu Inoue int e_wild; 1567d56d374SYoshinobu Inoue #define WILD_AF(ex) ((ex)->e_wild & 0x01) 1577d56d374SYoshinobu Inoue #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 1587d56d374SYoshinobu Inoue #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 1597d56d374SYoshinobu Inoue }; 1607d56d374SYoshinobu Inoue 1617d56d374SYoshinobu Inoue static const struct explore explore[] = { 162a42af91cSHajimu UMEMOTO #if 0 163a42af91cSHajimu UMEMOTO { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, 164a42af91cSHajimu UMEMOTO #endif 165a42af91cSHajimu UMEMOTO #ifdef INET6 166a42af91cSHajimu UMEMOTO { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 167a42af91cSHajimu UMEMOTO { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 168a42af91cSHajimu UMEMOTO { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, 169a42af91cSHajimu UMEMOTO #endif 170a42af91cSHajimu UMEMOTO { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 171a42af91cSHajimu UMEMOTO { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 172a42af91cSHajimu UMEMOTO { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, 173a42af91cSHajimu UMEMOTO { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 174a42af91cSHajimu UMEMOTO { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 175a42af91cSHajimu UMEMOTO { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, 176a42af91cSHajimu UMEMOTO { -1, 0, 0, NULL, 0 }, 1777d56d374SYoshinobu Inoue }; 1787d56d374SYoshinobu Inoue 1797d56d374SYoshinobu Inoue #ifdef INET6 1807d56d374SYoshinobu Inoue #define PTON_MAX 16 1817d56d374SYoshinobu Inoue #else 1827d56d374SYoshinobu Inoue #define PTON_MAX 4 1837d56d374SYoshinobu Inoue #endif 1847d56d374SYoshinobu Inoue 185a42af91cSHajimu UMEMOTO #if PACKETSZ > 1024 186a42af91cSHajimu UMEMOTO #define MAXPACKET PACKETSZ 187a42af91cSHajimu UMEMOTO #else 188a42af91cSHajimu UMEMOTO #define MAXPACKET 1024 189a42af91cSHajimu UMEMOTO #endif 190a42af91cSHajimu UMEMOTO 191a42af91cSHajimu UMEMOTO typedef union { 192a42af91cSHajimu UMEMOTO HEADER hdr; 193a42af91cSHajimu UMEMOTO u_char buf[MAXPACKET]; 194a42af91cSHajimu UMEMOTO } querybuf; 195a42af91cSHajimu UMEMOTO 196a42af91cSHajimu UMEMOTO struct res_target { 197a42af91cSHajimu UMEMOTO struct res_target *next; 198a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 199b826397aSJun-ichiro itojun Hagino int qclass, qtype; /* class and type of query */ 200a42af91cSHajimu UMEMOTO u_char *answer; /* buffer to put answer */ 201a42af91cSHajimu UMEMOTO int anslen; /* size of answer buffer */ 202a42af91cSHajimu UMEMOTO int n; /* result length */ 203a42af91cSHajimu UMEMOTO }; 204a42af91cSHajimu UMEMOTO 2057d56d374SYoshinobu Inoue static int str_isnumber __P((const char *)); 2067d56d374SYoshinobu Inoue static int explore_fqdn __P((const struct addrinfo *, const char *, 2077d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 208a42af91cSHajimu UMEMOTO static int explore_null __P((const struct addrinfo *, 2097d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 2107d56d374SYoshinobu Inoue static int explore_numeric __P((const struct addrinfo *, const char *, 2117d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 2127d56d374SYoshinobu Inoue static int explore_numeric_scope __P((const struct addrinfo *, const char *, 2137d56d374SYoshinobu Inoue const char *, struct addrinfo **)); 2147d56d374SYoshinobu Inoue static int get_canonname __P((const struct addrinfo *, 2157d56d374SYoshinobu Inoue struct addrinfo *, const char *)); 2167d56d374SYoshinobu Inoue static struct addrinfo *get_ai __P((const struct addrinfo *, 2177d56d374SYoshinobu Inoue const struct afd *, const char *)); 2187d56d374SYoshinobu Inoue static int get_portmatch __P((const struct addrinfo *, const char *)); 2197d56d374SYoshinobu Inoue static int get_port __P((struct addrinfo *, const char *, int)); 2207d56d374SYoshinobu Inoue static const struct afd *find_afd __P((int)); 221a42af91cSHajimu UMEMOTO static int addrconfig __P((struct addrinfo *)); 222a42af91cSHajimu UMEMOTO #ifdef INET6 223a42af91cSHajimu UMEMOTO static int ip6_str2scopeid __P((char *, struct sockaddr_in6 *)); 224a42af91cSHajimu UMEMOTO #endif 225a42af91cSHajimu UMEMOTO 226a42af91cSHajimu UMEMOTO static struct addrinfo *getanswer __P((const querybuf *, int, const char *, 227a42af91cSHajimu UMEMOTO int, const struct addrinfo *)); 228a42af91cSHajimu UMEMOTO static int _dns_getaddrinfo __P((const struct addrinfo *, const char *, 229a42af91cSHajimu UMEMOTO struct addrinfo **)); 230a42af91cSHajimu UMEMOTO static struct addrinfo *_gethtent __P((FILE *fp, const char *, 231a42af91cSHajimu UMEMOTO const struct addrinfo *)); 232a42af91cSHajimu UMEMOTO static int _files_getaddrinfo __P((const struct addrinfo *, const char *, 233a42af91cSHajimu UMEMOTO struct addrinfo **)); 234a42af91cSHajimu UMEMOTO #ifdef YP 235a42af91cSHajimu UMEMOTO static int _nis_getaddrinfo __P((const struct addrinfo *, const char *, 236a42af91cSHajimu UMEMOTO struct addrinfo **)); 237a42af91cSHajimu UMEMOTO #endif 238a42af91cSHajimu UMEMOTO 239a42af91cSHajimu UMEMOTO static int res_queryN __P((const char *, struct res_target *)); 240a42af91cSHajimu UMEMOTO static int res_searchN __P((const char *, struct res_target *)); 241a42af91cSHajimu UMEMOTO static int res_querydomainN __P((const char *, const char *, 242a42af91cSHajimu UMEMOTO struct res_target *)); 243a42af91cSHajimu UMEMOTO 2447d56d374SYoshinobu Inoue 2457d56d374SYoshinobu Inoue static char *ai_errlist[] = { 2467d56d374SYoshinobu Inoue "Success", 2477d56d374SYoshinobu Inoue "Address family for hostname not supported", /* EAI_ADDRFAMILY */ 2487d56d374SYoshinobu Inoue "Temporary failure in name resolution", /* EAI_AGAIN */ 2497d56d374SYoshinobu Inoue "Invalid value for ai_flags", /* EAI_BADFLAGS */ 2507d56d374SYoshinobu Inoue "Non-recoverable failure in name resolution", /* EAI_FAIL */ 2517d56d374SYoshinobu Inoue "ai_family not supported", /* EAI_FAMILY */ 2527d56d374SYoshinobu Inoue "Memory allocation failure", /* EAI_MEMORY */ 2537d56d374SYoshinobu Inoue "No address associated with hostname", /* EAI_NODATA */ 2547d56d374SYoshinobu Inoue "hostname nor servname provided, or not known", /* EAI_NONAME */ 2557d56d374SYoshinobu Inoue "servname not supported for ai_socktype", /* EAI_SERVICE */ 2567d56d374SYoshinobu Inoue "ai_socktype not supported", /* EAI_SOCKTYPE */ 2577d56d374SYoshinobu Inoue "System error returned in errno", /* EAI_SYSTEM */ 2587d56d374SYoshinobu Inoue "Invalid value for hints", /* EAI_BADHINTS */ 2597d56d374SYoshinobu Inoue "Resolved protocol is unknown", /* EAI_PROTOCOL */ 260b826397aSJun-ichiro itojun Hagino #ifdef EAI_RESNULL 2617d56d374SYoshinobu Inoue "Argument res is NULL", /* EAI_RESNULL */ 262b826397aSJun-ichiro itojun Hagino #endif 2637d56d374SYoshinobu Inoue "Unknown error", /* EAI_MAX */ 2647d56d374SYoshinobu Inoue }; 2657d56d374SYoshinobu Inoue 266a42af91cSHajimu UMEMOTO /* 267a42af91cSHajimu UMEMOTO * Select order host function. 268a42af91cSHajimu UMEMOTO */ 269a42af91cSHajimu UMEMOTO #define MAXHOSTCONF 4 270a42af91cSHajimu UMEMOTO 271a42af91cSHajimu UMEMOTO #ifndef HOSTCONF 272a42af91cSHajimu UMEMOTO # define HOSTCONF "/etc/host.conf" 273a42af91cSHajimu UMEMOTO #endif /* !HOSTCONF */ 274a42af91cSHajimu UMEMOTO 275a42af91cSHajimu UMEMOTO struct _hostconf { 276a42af91cSHajimu UMEMOTO int (*byname)(const struct addrinfo *, const char *, 277a42af91cSHajimu UMEMOTO struct addrinfo **); 278a42af91cSHajimu UMEMOTO }; 279a42af91cSHajimu UMEMOTO 280a42af91cSHajimu UMEMOTO /* default order */ 281a42af91cSHajimu UMEMOTO static struct _hostconf _hostconf[MAXHOSTCONF] = { 282a42af91cSHajimu UMEMOTO _dns_getaddrinfo, 283a42af91cSHajimu UMEMOTO _files_getaddrinfo, 284a42af91cSHajimu UMEMOTO #ifdef ICMPNL 285a42af91cSHajimu UMEMOTO NULL, 286a42af91cSHajimu UMEMOTO #endif /* ICMPNL */ 287a42af91cSHajimu UMEMOTO }; 288a42af91cSHajimu UMEMOTO 289a42af91cSHajimu UMEMOTO static int _hostconf_init_done; 290a42af91cSHajimu UMEMOTO static void _hostconf_init(void); 291a42af91cSHajimu UMEMOTO 2927d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */ 2937d56d374SYoshinobu Inoue 2947d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \ 2957d56d374SYoshinobu Inoue do { \ 2967d56d374SYoshinobu Inoue /* external reference: pai, error, and label free */ \ 2977d56d374SYoshinobu Inoue (ai) = get_ai(pai, (afd), (addr)); \ 2987d56d374SYoshinobu Inoue if ((ai) == NULL) { \ 2997d56d374SYoshinobu Inoue error = EAI_MEMORY; \ 3007d56d374SYoshinobu Inoue goto free; \ 3017d56d374SYoshinobu Inoue } \ 302a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3037d56d374SYoshinobu Inoue 3047d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \ 3057d56d374SYoshinobu Inoue do { \ 3067d56d374SYoshinobu Inoue /* external reference: error and label free */ \ 3077d56d374SYoshinobu Inoue error = get_port((ai), (serv), 0); \ 3087d56d374SYoshinobu Inoue if (error != 0) \ 3097d56d374SYoshinobu Inoue goto free; \ 310a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3117d56d374SYoshinobu Inoue 3127d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \ 3137d56d374SYoshinobu Inoue do { \ 3147d56d374SYoshinobu Inoue /* external reference: pai, error and label free */ \ 3157d56d374SYoshinobu Inoue error = get_canonname(pai, (ai), (str)); \ 3167d56d374SYoshinobu Inoue if (error != 0) \ 3177d56d374SYoshinobu Inoue goto free; \ 318a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3197d56d374SYoshinobu Inoue 3207d56d374SYoshinobu Inoue #define ERR(err) \ 3217d56d374SYoshinobu Inoue do { \ 3227d56d374SYoshinobu Inoue /* external reference: error, and label bad */ \ 3237d56d374SYoshinobu Inoue error = (err); \ 3247d56d374SYoshinobu Inoue goto bad; \ 325a42af91cSHajimu UMEMOTO /*NOTREACHED*/ \ 326a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3277d56d374SYoshinobu Inoue 3287d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \ 329a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 3307d56d374SYoshinobu Inoue #define MATCH(x, y, w) \ 331a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 3327d56d374SYoshinobu Inoue 3337d56d374SYoshinobu Inoue char * 3347d56d374SYoshinobu Inoue gai_strerror(ecode) 3357d56d374SYoshinobu Inoue int ecode; 3367d56d374SYoshinobu Inoue { 3377d56d374SYoshinobu Inoue if (ecode < 0 || ecode > EAI_MAX) 3387d56d374SYoshinobu Inoue ecode = EAI_MAX; 3397d56d374SYoshinobu Inoue return ai_errlist[ecode]; 3407d56d374SYoshinobu Inoue } 3417d56d374SYoshinobu Inoue 3427d56d374SYoshinobu Inoue void 3437d56d374SYoshinobu Inoue freeaddrinfo(ai) 3447d56d374SYoshinobu Inoue struct addrinfo *ai; 3457d56d374SYoshinobu Inoue { 3467d56d374SYoshinobu Inoue struct addrinfo *next; 3477d56d374SYoshinobu Inoue 3487d56d374SYoshinobu Inoue do { 3497d56d374SYoshinobu Inoue next = ai->ai_next; 3507d56d374SYoshinobu Inoue if (ai->ai_canonname) 3517d56d374SYoshinobu Inoue free(ai->ai_canonname); 3527d56d374SYoshinobu Inoue /* no need to free(ai->ai_addr) */ 3537d56d374SYoshinobu Inoue free(ai); 354a42af91cSHajimu UMEMOTO ai = next; 355a42af91cSHajimu UMEMOTO } while (ai); 3567d56d374SYoshinobu Inoue } 3577d56d374SYoshinobu Inoue 3587d56d374SYoshinobu Inoue static int 3597d56d374SYoshinobu Inoue str_isnumber(p) 3607d56d374SYoshinobu Inoue const char *p; 3617d56d374SYoshinobu Inoue { 362ec20fe00SJun-ichiro itojun Hagino char *ep; 363ec20fe00SJun-ichiro itojun Hagino 364ec20fe00SJun-ichiro itojun Hagino if (*p == '\0') 3657d56d374SYoshinobu Inoue return NO; 366ec20fe00SJun-ichiro itojun Hagino ep = NULL; 367ec20fe00SJun-ichiro itojun Hagino (void)strtoul(p, &ep, 10); 368ec20fe00SJun-ichiro itojun Hagino if (ep && *ep == '\0') 3697d56d374SYoshinobu Inoue return YES; 370ec20fe00SJun-ichiro itojun Hagino else 371ec20fe00SJun-ichiro itojun Hagino return NO; 3727d56d374SYoshinobu Inoue } 3737d56d374SYoshinobu Inoue 3747d56d374SYoshinobu Inoue int 3757d56d374SYoshinobu Inoue getaddrinfo(hostname, servname, hints, res) 3767d56d374SYoshinobu Inoue const char *hostname, *servname; 3777d56d374SYoshinobu Inoue const struct addrinfo *hints; 3787d56d374SYoshinobu Inoue struct addrinfo **res; 3797d56d374SYoshinobu Inoue { 3807d56d374SYoshinobu Inoue struct addrinfo sentinel; 3817d56d374SYoshinobu Inoue struct addrinfo *cur; 3827d56d374SYoshinobu Inoue int error = 0; 3837d56d374SYoshinobu Inoue struct addrinfo ai; 3847d56d374SYoshinobu Inoue struct addrinfo ai0; 3857d56d374SYoshinobu Inoue struct addrinfo *pai; 3867d56d374SYoshinobu Inoue const struct explore *ex; 3877d56d374SYoshinobu Inoue 388a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 3897d56d374SYoshinobu Inoue cur = &sentinel; 3907d56d374SYoshinobu Inoue pai = &ai; 3917d56d374SYoshinobu Inoue pai->ai_flags = 0; 3927d56d374SYoshinobu Inoue pai->ai_family = PF_UNSPEC; 3937d56d374SYoshinobu Inoue pai->ai_socktype = ANY; 3947d56d374SYoshinobu Inoue pai->ai_protocol = ANY; 3957d56d374SYoshinobu Inoue pai->ai_addrlen = 0; 3967d56d374SYoshinobu Inoue pai->ai_canonname = NULL; 3977d56d374SYoshinobu Inoue pai->ai_addr = NULL; 3987d56d374SYoshinobu Inoue pai->ai_next = NULL; 3997d56d374SYoshinobu Inoue 4007d56d374SYoshinobu Inoue if (hostname == NULL && servname == NULL) 4017d56d374SYoshinobu Inoue return EAI_NONAME; 402b826397aSJun-ichiro itojun Hagino #ifdef EAI_RESNULL 4037d56d374SYoshinobu Inoue if (res == NULL) 4047d56d374SYoshinobu Inoue return EAI_RESNULL; /* xxx */ 405b826397aSJun-ichiro itojun Hagino #endif 4067d56d374SYoshinobu Inoue if (hints) { 4077d56d374SYoshinobu Inoue /* error check for hints */ 4087d56d374SYoshinobu Inoue if (hints->ai_addrlen || hints->ai_canonname || 4097d56d374SYoshinobu Inoue hints->ai_addr || hints->ai_next) 4107d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); /* xxx */ 4117d56d374SYoshinobu Inoue if (hints->ai_flags & ~AI_MASK) 4127d56d374SYoshinobu Inoue ERR(EAI_BADFLAGS); 4137d56d374SYoshinobu Inoue switch (hints->ai_family) { 4147d56d374SYoshinobu Inoue case PF_UNSPEC: 4157d56d374SYoshinobu Inoue case PF_INET: 4167d56d374SYoshinobu Inoue #ifdef INET6 4177d56d374SYoshinobu Inoue case PF_INET6: 4187d56d374SYoshinobu Inoue #endif 4197d56d374SYoshinobu Inoue break; 4207d56d374SYoshinobu Inoue default: 4217d56d374SYoshinobu Inoue ERR(EAI_FAMILY); 4227d56d374SYoshinobu Inoue } 4237d56d374SYoshinobu Inoue memcpy(pai, hints, sizeof(*pai)); 4247d56d374SYoshinobu Inoue 4257d56d374SYoshinobu Inoue /* 4267d56d374SYoshinobu Inoue * if both socktype/protocol are specified, check if they 4277d56d374SYoshinobu Inoue * are meaningful combination. 4287d56d374SYoshinobu Inoue */ 4297d56d374SYoshinobu Inoue if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 430a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 431a42af91cSHajimu UMEMOTO if (pai->ai_family != ex->e_af) 432a42af91cSHajimu UMEMOTO continue; 4337d56d374SYoshinobu Inoue if (ex->e_socktype == ANY) 4347d56d374SYoshinobu Inoue continue; 4357d56d374SYoshinobu Inoue if (ex->e_protocol == ANY) 4367d56d374SYoshinobu Inoue continue; 4377d56d374SYoshinobu Inoue if (pai->ai_socktype == ex->e_socktype 438a42af91cSHajimu UMEMOTO && pai->ai_protocol != ex->e_protocol) { 4397d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); 4407d56d374SYoshinobu Inoue } 4417d56d374SYoshinobu Inoue } 4427d56d374SYoshinobu Inoue } 443a42af91cSHajimu UMEMOTO } 4447d56d374SYoshinobu Inoue 4457d56d374SYoshinobu Inoue /* 446a42af91cSHajimu UMEMOTO * post-2553: AI_ALL and AI_V4MAPPED are effective only against 447a42af91cSHajimu UMEMOTO * AF_INET6 query. They needs to be ignored if specified in other 448a42af91cSHajimu UMEMOTO * occassions. 449a42af91cSHajimu UMEMOTO */ 450a42af91cSHajimu UMEMOTO switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) { 451a42af91cSHajimu UMEMOTO case AI_V4MAPPED: 452a42af91cSHajimu UMEMOTO case AI_ALL | AI_V4MAPPED: 453a42af91cSHajimu UMEMOTO if (pai->ai_family != AF_INET6) 454a42af91cSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 455a42af91cSHajimu UMEMOTO break; 456a42af91cSHajimu UMEMOTO case AI_ALL: 457a42af91cSHajimu UMEMOTO #if 1 458a42af91cSHajimu UMEMOTO /* illegal */ 459a42af91cSHajimu UMEMOTO ERR(EAI_BADFLAGS); 460a42af91cSHajimu UMEMOTO #else 461a42af91cSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 462a42af91cSHajimu UMEMOTO #endif 463a42af91cSHajimu UMEMOTO break; 464a42af91cSHajimu UMEMOTO } 465a42af91cSHajimu UMEMOTO 466a42af91cSHajimu UMEMOTO /* 467a42af91cSHajimu UMEMOTO * check for special cases. (1) numeric servname is disallowed if 468a42af91cSHajimu UMEMOTO * socktype/protocol are left unspecified. (2) servname is disallowed 469a42af91cSHajimu UMEMOTO * for raw and other inet{,6} sockets. 4707d56d374SYoshinobu Inoue */ 4717d56d374SYoshinobu Inoue if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 472a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4737d56d374SYoshinobu Inoue || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 4747d56d374SYoshinobu Inoue #endif 4757d56d374SYoshinobu Inoue ) { 476a42af91cSHajimu UMEMOTO ai0 = *pai; /* backup *pai */ 4777d56d374SYoshinobu Inoue 478a42af91cSHajimu UMEMOTO if (pai->ai_family == PF_UNSPEC) { 479a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4807d56d374SYoshinobu Inoue pai->ai_family = PF_INET6; 4817d56d374SYoshinobu Inoue #else 4827d56d374SYoshinobu Inoue pai->ai_family = PF_INET; 4837d56d374SYoshinobu Inoue #endif 484a42af91cSHajimu UMEMOTO } 4857d56d374SYoshinobu Inoue error = get_portmatch(pai, servname); 4867d56d374SYoshinobu Inoue if (error) 4877d56d374SYoshinobu Inoue ERR(error); 488a42af91cSHajimu UMEMOTO 489a42af91cSHajimu UMEMOTO *pai = ai0; 4907d56d374SYoshinobu Inoue } 4917d56d374SYoshinobu Inoue 492a42af91cSHajimu UMEMOTO ai0 = *pai; 493a42af91cSHajimu UMEMOTO 4947d56d374SYoshinobu Inoue /* NULL hostname, or numeric hostname */ 495a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 4967d56d374SYoshinobu Inoue *pai = ai0; 4977d56d374SYoshinobu Inoue 498a42af91cSHajimu UMEMOTO /* PF_UNSPEC entries are prepared for DNS queries only */ 499a42af91cSHajimu UMEMOTO if (ex->e_af == PF_UNSPEC) 5007d56d374SYoshinobu Inoue continue; 501a42af91cSHajimu UMEMOTO 502a42af91cSHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 5037d56d374SYoshinobu Inoue continue; 504a42af91cSHajimu UMEMOTO if (!MATCH(pai->ai_socktype, ex->e_socktype, 505a42af91cSHajimu UMEMOTO WILD_SOCKTYPE(ex))) 506a42af91cSHajimu UMEMOTO continue; 507a42af91cSHajimu UMEMOTO if (!MATCH(pai->ai_protocol, ex->e_protocol, 508a42af91cSHajimu UMEMOTO WILD_PROTOCOL(ex))) 5097d56d374SYoshinobu Inoue continue; 5107d56d374SYoshinobu Inoue 5117d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 512a42af91cSHajimu UMEMOTO pai->ai_family = ex->e_af; 5137d56d374SYoshinobu Inoue if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 5147d56d374SYoshinobu Inoue pai->ai_socktype = ex->e_socktype; 5157d56d374SYoshinobu Inoue if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 5167d56d374SYoshinobu Inoue pai->ai_protocol = ex->e_protocol; 5177d56d374SYoshinobu Inoue 5187d56d374SYoshinobu Inoue if (hostname == NULL) 519a42af91cSHajimu UMEMOTO error = explore_null(pai, servname, &cur->ai_next); 5207d56d374SYoshinobu Inoue else 521a42af91cSHajimu UMEMOTO error = explore_numeric_scope(pai, hostname, servname, 522a42af91cSHajimu UMEMOTO &cur->ai_next); 5237d56d374SYoshinobu Inoue 5247d56d374SYoshinobu Inoue if (error) 5257d56d374SYoshinobu Inoue goto free; 5267d56d374SYoshinobu Inoue 5277d56d374SYoshinobu Inoue while (cur && cur->ai_next) 5287d56d374SYoshinobu Inoue cur = cur->ai_next; 5297d56d374SYoshinobu Inoue } 5307d56d374SYoshinobu Inoue 5317d56d374SYoshinobu Inoue /* 5327d56d374SYoshinobu Inoue * XXX 5337d56d374SYoshinobu Inoue * If numreic representation of AF1 can be interpreted as FQDN 5347d56d374SYoshinobu Inoue * representation of AF2, we need to think again about the code below. 5357d56d374SYoshinobu Inoue */ 5367d56d374SYoshinobu Inoue if (sentinel.ai_next) 5377d56d374SYoshinobu Inoue goto good; 5387d56d374SYoshinobu Inoue 5397d56d374SYoshinobu Inoue if (pai->ai_flags & AI_NUMERICHOST) 540b826397aSJun-ichiro itojun Hagino ERR(EAI_NODATA); 5417d56d374SYoshinobu Inoue if (hostname == NULL) 542b826397aSJun-ichiro itojun Hagino ERR(EAI_NODATA); 5437d56d374SYoshinobu Inoue 544a42af91cSHajimu UMEMOTO #if 1 545a42af91cSHajimu UMEMOTO /* XXX: temporarily, behave as if AI_ADDRCONFIG is specified */ 546a42af91cSHajimu UMEMOTO pai->ai_flags |= AI_ADDRCONFIG; 547a42af91cSHajimu UMEMOTO #endif 548a42af91cSHajimu UMEMOTO if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0)) 549a42af91cSHajimu UMEMOTO ERR(EAI_FAIL); 550a42af91cSHajimu UMEMOTO 551a42af91cSHajimu UMEMOTO /* 552a42af91cSHajimu UMEMOTO * hostname as alphabetical name. 553a42af91cSHajimu UMEMOTO * we would like to prefer AF_INET6 than AF_INET, so we'll make a 554a42af91cSHajimu UMEMOTO * outer loop by AFs. 555a42af91cSHajimu UMEMOTO */ 556a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 5577d56d374SYoshinobu Inoue *pai = ai0; 5587d56d374SYoshinobu Inoue 559a42af91cSHajimu UMEMOTO /* require exact match for family field */ 560a42af91cSHajimu UMEMOTO if (pai->ai_family != ex->e_af) 561a42af91cSHajimu UMEMOTO continue; 562a42af91cSHajimu UMEMOTO 5637d56d374SYoshinobu Inoue if (!MATCH(pai->ai_socktype, ex->e_socktype, 5647d56d374SYoshinobu Inoue WILD_SOCKTYPE(ex))) { 5657d56d374SYoshinobu Inoue continue; 5667d56d374SYoshinobu Inoue } 5677d56d374SYoshinobu Inoue if (!MATCH(pai->ai_protocol, ex->e_protocol, 5687d56d374SYoshinobu Inoue WILD_PROTOCOL(ex))) { 5697d56d374SYoshinobu Inoue continue; 5707d56d374SYoshinobu Inoue } 5717d56d374SYoshinobu Inoue 5727d56d374SYoshinobu Inoue if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 5737d56d374SYoshinobu Inoue pai->ai_socktype = ex->e_socktype; 5747d56d374SYoshinobu Inoue if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 5757d56d374SYoshinobu Inoue pai->ai_protocol = ex->e_protocol; 5767d56d374SYoshinobu Inoue 577a42af91cSHajimu UMEMOTO error = explore_fqdn(pai, hostname, servname, &cur->ai_next); 5787d56d374SYoshinobu Inoue 5797d56d374SYoshinobu Inoue while (cur && cur->ai_next) 5807d56d374SYoshinobu Inoue cur = cur->ai_next; 5817d56d374SYoshinobu Inoue } 5827d56d374SYoshinobu Inoue 583a42af91cSHajimu UMEMOTO /* XXX */ 584a42af91cSHajimu UMEMOTO if (sentinel.ai_next) 585a42af91cSHajimu UMEMOTO error = 0; 586a42af91cSHajimu UMEMOTO 587a42af91cSHajimu UMEMOTO if (error) 588a42af91cSHajimu UMEMOTO goto free; 589a42af91cSHajimu UMEMOTO if (error == 0) { 5907d56d374SYoshinobu Inoue if (sentinel.ai_next) { 5917d56d374SYoshinobu Inoue good: 5927d56d374SYoshinobu Inoue *res = sentinel.ai_next; 5937d56d374SYoshinobu Inoue return SUCCESS; 594a42af91cSHajimu UMEMOTO } else 595a42af91cSHajimu UMEMOTO error = EAI_FAIL; 5967d56d374SYoshinobu Inoue } 5977d56d374SYoshinobu Inoue free: 5987d56d374SYoshinobu Inoue bad: 5997d56d374SYoshinobu Inoue if (sentinel.ai_next) 6007d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 6017d56d374SYoshinobu Inoue *res = NULL; 6027d56d374SYoshinobu Inoue return error; 6037d56d374SYoshinobu Inoue } 6047d56d374SYoshinobu Inoue 605a42af91cSHajimu UMEMOTO static char * 606a42af91cSHajimu UMEMOTO _hgetword(char **pp) 607a42af91cSHajimu UMEMOTO { 608a42af91cSHajimu UMEMOTO char c, *p, *ret; 609a42af91cSHajimu UMEMOTO const char *sp; 610a42af91cSHajimu UMEMOTO static const char sep[] = "# \t\n"; 611a42af91cSHajimu UMEMOTO 612a42af91cSHajimu UMEMOTO ret = NULL; 613a42af91cSHajimu UMEMOTO for (p = *pp; (c = *p) != '\0'; p++) { 614a42af91cSHajimu UMEMOTO for (sp = sep; *sp != '\0'; sp++) { 615a42af91cSHajimu UMEMOTO if (c == *sp) 616a42af91cSHajimu UMEMOTO break; 617a42af91cSHajimu UMEMOTO } 618a42af91cSHajimu UMEMOTO if (c == '#') 619a42af91cSHajimu UMEMOTO p[1] = '\0'; /* ignore rest of line */ 620a42af91cSHajimu UMEMOTO if (ret == NULL) { 621a42af91cSHajimu UMEMOTO if (*sp == '\0') 622a42af91cSHajimu UMEMOTO ret = p; 623a42af91cSHajimu UMEMOTO } else { 624a42af91cSHajimu UMEMOTO if (*sp != '\0') { 625a42af91cSHajimu UMEMOTO *p++ = '\0'; 626a42af91cSHajimu UMEMOTO break; 627a42af91cSHajimu UMEMOTO } 628a42af91cSHajimu UMEMOTO } 629a42af91cSHajimu UMEMOTO } 630a42af91cSHajimu UMEMOTO *pp = p; 631a42af91cSHajimu UMEMOTO if (ret == NULL || *ret == '\0') 632a42af91cSHajimu UMEMOTO return NULL; 633a42af91cSHajimu UMEMOTO return ret; 634a42af91cSHajimu UMEMOTO } 635a42af91cSHajimu UMEMOTO 636a42af91cSHajimu UMEMOTO /* 637a42af91cSHajimu UMEMOTO * Initialize hostconf structure. 638a42af91cSHajimu UMEMOTO */ 639a42af91cSHajimu UMEMOTO 640a42af91cSHajimu UMEMOTO static void 641a42af91cSHajimu UMEMOTO _hostconf_init(void) 642a42af91cSHajimu UMEMOTO { 643a42af91cSHajimu UMEMOTO FILE *fp; 644a42af91cSHajimu UMEMOTO int n; 645a42af91cSHajimu UMEMOTO char *p, *line; 646a42af91cSHajimu UMEMOTO char buf[BUFSIZ]; 647a42af91cSHajimu UMEMOTO 648a42af91cSHajimu UMEMOTO _hostconf_init_done = 1; 649a42af91cSHajimu UMEMOTO n = 0; 650a42af91cSHajimu UMEMOTO p = HOSTCONF; 651a42af91cSHajimu UMEMOTO if ((fp = fopen(p, "r")) == NULL) 652a42af91cSHajimu UMEMOTO return; 653a42af91cSHajimu UMEMOTO while (n < MAXHOSTCONF && fgets(buf, sizeof(buf), fp)) { 654a42af91cSHajimu UMEMOTO line = buf; 655a42af91cSHajimu UMEMOTO if ((p = _hgetword(&line)) == NULL) 656a42af91cSHajimu UMEMOTO continue; 657a42af91cSHajimu UMEMOTO do { 658a42af91cSHajimu UMEMOTO if (strcmp(p, "hosts") == 0 659a42af91cSHajimu UMEMOTO || strcmp(p, "local") == 0 660a42af91cSHajimu UMEMOTO || strcmp(p, "file") == 0 661a42af91cSHajimu UMEMOTO || strcmp(p, "files") == 0) 662a42af91cSHajimu UMEMOTO _hostconf[n++].byname = _files_getaddrinfo; 663a42af91cSHajimu UMEMOTO else if (strcmp(p, "dns") == 0 664a42af91cSHajimu UMEMOTO || strcmp(p, "bind") == 0) 665a42af91cSHajimu UMEMOTO _hostconf[n++].byname = _dns_getaddrinfo; 666a42af91cSHajimu UMEMOTO #ifdef YP 667a42af91cSHajimu UMEMOTO else if (strcmp(p, "nis") == 0) 668a42af91cSHajimu UMEMOTO _hostconf[n++].byname = _nis_getaddrinfo; 669a42af91cSHajimu UMEMOTO #endif 670a42af91cSHajimu UMEMOTO } while ((p = _hgetword(&line)) != NULL); 671a42af91cSHajimu UMEMOTO } 672a42af91cSHajimu UMEMOTO fclose(fp); 673a42af91cSHajimu UMEMOTO if (n < 0) { 674a42af91cSHajimu UMEMOTO /* no keyword found. do not change default configuration */ 675a42af91cSHajimu UMEMOTO return; 676a42af91cSHajimu UMEMOTO } 677a42af91cSHajimu UMEMOTO for (; n < MAXHOSTCONF; n++) 678a42af91cSHajimu UMEMOTO _hostconf[n].byname = NULL; 679a42af91cSHajimu UMEMOTO } 680a42af91cSHajimu UMEMOTO 6817d56d374SYoshinobu Inoue /* 6827d56d374SYoshinobu Inoue * FQDN hostname, DNS lookup 6837d56d374SYoshinobu Inoue */ 6847d56d374SYoshinobu Inoue static int 6857d56d374SYoshinobu Inoue explore_fqdn(pai, hostname, servname, res) 6867d56d374SYoshinobu Inoue const struct addrinfo *pai; 6877d56d374SYoshinobu Inoue const char *hostname; 6887d56d374SYoshinobu Inoue const char *servname; 6897d56d374SYoshinobu Inoue struct addrinfo **res; 6907d56d374SYoshinobu Inoue { 691a42af91cSHajimu UMEMOTO struct addrinfo *result; 692a42af91cSHajimu UMEMOTO struct addrinfo *cur; 693a42af91cSHajimu UMEMOTO int error = 0, i; 6947d56d374SYoshinobu Inoue 695a42af91cSHajimu UMEMOTO result = NULL; 6967d56d374SYoshinobu Inoue *res = NULL; 6977d56d374SYoshinobu Inoue 6987d56d374SYoshinobu Inoue /* 6997d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 7007d56d374SYoshinobu Inoue */ 7017d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 7027d56d374SYoshinobu Inoue return 0; 7037d56d374SYoshinobu Inoue 704a42af91cSHajimu UMEMOTO if (!_hostconf_init_done) 705a42af91cSHajimu UMEMOTO _hostconf_init(); 7067d56d374SYoshinobu Inoue 707a42af91cSHajimu UMEMOTO for (i = 0; i < MAXHOSTCONF; i++) { 708a42af91cSHajimu UMEMOTO if (!_hostconf[i].byname) 709e6f35403SYoshinobu Inoue continue; 710a42af91cSHajimu UMEMOTO error = (*_hostconf[i].byname)(pai, hostname, &result); 711a42af91cSHajimu UMEMOTO if (error != 0) 7127d56d374SYoshinobu Inoue continue; 713a42af91cSHajimu UMEMOTO for (cur = result; cur; cur = cur->ai_next) { 714a42af91cSHajimu UMEMOTO GET_PORT(cur, servname); 715a42af91cSHajimu UMEMOTO /* canonname should be filled already */ 7167d56d374SYoshinobu Inoue } 717a42af91cSHajimu UMEMOTO *res = result; 7187d56d374SYoshinobu Inoue return 0; 719a42af91cSHajimu UMEMOTO } 7207d56d374SYoshinobu Inoue 7217d56d374SYoshinobu Inoue free: 722a42af91cSHajimu UMEMOTO if (result) 723a42af91cSHajimu UMEMOTO freeaddrinfo(result); 7247d56d374SYoshinobu Inoue return error; 7257d56d374SYoshinobu Inoue } 7267d56d374SYoshinobu Inoue 7277d56d374SYoshinobu Inoue /* 7287d56d374SYoshinobu Inoue * hostname == NULL. 7297d56d374SYoshinobu Inoue * passive socket -> anyaddr (0.0.0.0 or ::) 7307d56d374SYoshinobu Inoue * non-passive socket -> localhost (127.0.0.1 or ::1) 7317d56d374SYoshinobu Inoue */ 7327d56d374SYoshinobu Inoue static int 733a42af91cSHajimu UMEMOTO explore_null(pai, servname, res) 7347d56d374SYoshinobu Inoue const struct addrinfo *pai; 7357d56d374SYoshinobu Inoue const char *servname; 7367d56d374SYoshinobu Inoue struct addrinfo **res; 7377d56d374SYoshinobu Inoue { 7387d56d374SYoshinobu Inoue int s; 7397d56d374SYoshinobu Inoue const struct afd *afd; 7407d56d374SYoshinobu Inoue struct addrinfo *cur; 7417d56d374SYoshinobu Inoue struct addrinfo sentinel; 7427d56d374SYoshinobu Inoue int error; 7437d56d374SYoshinobu Inoue 7447d56d374SYoshinobu Inoue *res = NULL; 7457d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 7467d56d374SYoshinobu Inoue cur = &sentinel; 7477d56d374SYoshinobu Inoue 7487d56d374SYoshinobu Inoue /* 7497d56d374SYoshinobu Inoue * filter out AFs that are not supported by the kernel 7507d56d374SYoshinobu Inoue * XXX errno? 7517d56d374SYoshinobu Inoue */ 7527d56d374SYoshinobu Inoue s = socket(pai->ai_family, SOCK_DGRAM, 0); 753a42af91cSHajimu UMEMOTO if (s < 0) { 754a42af91cSHajimu UMEMOTO if (errno != EMFILE) 7557d56d374SYoshinobu Inoue return 0; 756a42af91cSHajimu UMEMOTO } else 7579233c4d9SJason Evans _close(s); 758a42af91cSHajimu UMEMOTO 759a42af91cSHajimu UMEMOTO /* 760a42af91cSHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 761a42af91cSHajimu UMEMOTO */ 762a42af91cSHajimu UMEMOTO if (get_portmatch(pai, servname) != 0) 763a42af91cSHajimu UMEMOTO return 0; 764a42af91cSHajimu UMEMOTO 7657d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 7667d56d374SYoshinobu Inoue if (afd == NULL) 7677d56d374SYoshinobu Inoue return 0; 7687d56d374SYoshinobu Inoue 769a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_PASSIVE) { 770a42af91cSHajimu UMEMOTO GET_AI(cur->ai_next, afd, afd->a_addrany); 7717d56d374SYoshinobu Inoue /* xxx meaningless? 7727d56d374SYoshinobu Inoue * GET_CANONNAME(cur->ai_next, "anyaddr"); 773a42af91cSHajimu UMEMOTO */ 774a42af91cSHajimu UMEMOTO GET_PORT(cur->ai_next, servname); 775a42af91cSHajimu UMEMOTO } else { 776a42af91cSHajimu UMEMOTO GET_AI(cur->ai_next, afd, afd->a_loopback); 777a42af91cSHajimu UMEMOTO /* xxx meaningless? 7787d56d374SYoshinobu Inoue * GET_CANONNAME(cur->ai_next, "localhost"); 7797d56d374SYoshinobu Inoue */ 7807d56d374SYoshinobu Inoue GET_PORT(cur->ai_next, servname); 781a42af91cSHajimu UMEMOTO } 782a42af91cSHajimu UMEMOTO cur = cur->ai_next; 7837d56d374SYoshinobu Inoue 7847d56d374SYoshinobu Inoue *res = sentinel.ai_next; 7857d56d374SYoshinobu Inoue return 0; 7867d56d374SYoshinobu Inoue 7877d56d374SYoshinobu Inoue free: 7887d56d374SYoshinobu Inoue if (sentinel.ai_next) 7897d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 7907d56d374SYoshinobu Inoue return error; 7917d56d374SYoshinobu Inoue } 7927d56d374SYoshinobu Inoue 7937d56d374SYoshinobu Inoue /* 7947d56d374SYoshinobu Inoue * numeric hostname 7957d56d374SYoshinobu Inoue */ 7967d56d374SYoshinobu Inoue static int 7977d56d374SYoshinobu Inoue explore_numeric(pai, hostname, servname, res) 7987d56d374SYoshinobu Inoue const struct addrinfo *pai; 7997d56d374SYoshinobu Inoue const char *hostname; 8007d56d374SYoshinobu Inoue const char *servname; 8017d56d374SYoshinobu Inoue struct addrinfo **res; 8027d56d374SYoshinobu Inoue { 8037d56d374SYoshinobu Inoue const struct afd *afd; 8047d56d374SYoshinobu Inoue struct addrinfo *cur; 8057d56d374SYoshinobu Inoue struct addrinfo sentinel; 8067d56d374SYoshinobu Inoue int error; 8077d56d374SYoshinobu Inoue char pton[PTON_MAX]; 8087d56d374SYoshinobu Inoue 8097d56d374SYoshinobu Inoue *res = NULL; 8107d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 8117d56d374SYoshinobu Inoue cur = &sentinel; 8127d56d374SYoshinobu Inoue 8137d56d374SYoshinobu Inoue /* 8147d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 8157d56d374SYoshinobu Inoue */ 8167d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 8177d56d374SYoshinobu Inoue return 0; 8187d56d374SYoshinobu Inoue 8197d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 8207d56d374SYoshinobu Inoue if (afd == NULL) 8217d56d374SYoshinobu Inoue return 0; 8227d56d374SYoshinobu Inoue 823b826397aSJun-ichiro itojun Hagino switch (afd->a_af) { 824b826397aSJun-ichiro itojun Hagino #if 1 /*X/Open spec*/ 825b826397aSJun-ichiro itojun Hagino case AF_INET: 826b826397aSJun-ichiro itojun Hagino if (inet_aton(hostname, (struct in_addr *)pton) == 1) { 8277d56d374SYoshinobu Inoue if (pai->ai_family == afd->a_af || 8287d56d374SYoshinobu Inoue pai->ai_family == PF_UNSPEC /*?*/) { 8297d56d374SYoshinobu Inoue GET_AI(cur->ai_next, afd, pton); 8307d56d374SYoshinobu Inoue GET_PORT(cur->ai_next, servname); 8317d56d374SYoshinobu Inoue while (cur && cur->ai_next) 8327d56d374SYoshinobu Inoue cur = cur->ai_next; 8337d56d374SYoshinobu Inoue } else 8347d56d374SYoshinobu Inoue ERR(EAI_FAMILY); /*xxx*/ 8357d56d374SYoshinobu Inoue } 836b826397aSJun-ichiro itojun Hagino break; 837b826397aSJun-ichiro itojun Hagino #endif 838b826397aSJun-ichiro itojun Hagino default: 839b826397aSJun-ichiro itojun Hagino if (inet_pton(afd->a_af, hostname, pton) == 1) { 840b826397aSJun-ichiro itojun Hagino if (pai->ai_family == afd->a_af || 841b826397aSJun-ichiro itojun Hagino pai->ai_family == PF_UNSPEC /*?*/) { 842b826397aSJun-ichiro itojun Hagino GET_AI(cur->ai_next, afd, pton); 843b826397aSJun-ichiro itojun Hagino GET_PORT(cur->ai_next, servname); 844b826397aSJun-ichiro itojun Hagino while (cur && cur->ai_next) 845b826397aSJun-ichiro itojun Hagino cur = cur->ai_next; 846b826397aSJun-ichiro itojun Hagino } else 847b826397aSJun-ichiro itojun Hagino ERR(EAI_FAMILY); /*xxx*/ 848b826397aSJun-ichiro itojun Hagino } 849b826397aSJun-ichiro itojun Hagino break; 850b826397aSJun-ichiro itojun Hagino } 8517d56d374SYoshinobu Inoue 8527d56d374SYoshinobu Inoue *res = sentinel.ai_next; 8537d56d374SYoshinobu Inoue return 0; 8547d56d374SYoshinobu Inoue 8557d56d374SYoshinobu Inoue free: 8567d56d374SYoshinobu Inoue bad: 8577d56d374SYoshinobu Inoue if (sentinel.ai_next) 8587d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 8597d56d374SYoshinobu Inoue return error; 8607d56d374SYoshinobu Inoue } 8617d56d374SYoshinobu Inoue 8627d56d374SYoshinobu Inoue /* 8637d56d374SYoshinobu Inoue * numeric hostname with scope 8647d56d374SYoshinobu Inoue */ 8657d56d374SYoshinobu Inoue static int 8667d56d374SYoshinobu Inoue explore_numeric_scope(pai, hostname, servname, res) 8677d56d374SYoshinobu Inoue const struct addrinfo *pai; 8687d56d374SYoshinobu Inoue const char *hostname; 8697d56d374SYoshinobu Inoue const char *servname; 8707d56d374SYoshinobu Inoue struct addrinfo **res; 8717d56d374SYoshinobu Inoue { 872a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6) 8737d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 8747d56d374SYoshinobu Inoue #else 8757d56d374SYoshinobu Inoue const struct afd *afd; 8767d56d374SYoshinobu Inoue struct addrinfo *cur; 8777d56d374SYoshinobu Inoue int error; 878a42af91cSHajimu UMEMOTO char *cp, *hostname2 = NULL, *scope, *addr; 8797d56d374SYoshinobu Inoue struct sockaddr_in6 *sin6; 8807d56d374SYoshinobu Inoue 8817d56d374SYoshinobu Inoue /* 8827d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 8837d56d374SYoshinobu Inoue */ 8847d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 8857d56d374SYoshinobu Inoue return 0; 8867d56d374SYoshinobu Inoue 8877d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 8887d56d374SYoshinobu Inoue if (afd == NULL) 8897d56d374SYoshinobu Inoue return 0; 8907d56d374SYoshinobu Inoue if (!afd->a_scoped) 8917d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 8927d56d374SYoshinobu Inoue 8937d56d374SYoshinobu Inoue cp = strchr(hostname, SCOPE_DELIMITER); 8947d56d374SYoshinobu Inoue if (cp == NULL) 8957d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 8967d56d374SYoshinobu Inoue 8977d56d374SYoshinobu Inoue /* 8987d56d374SYoshinobu Inoue * Handle special case of <scoped_address><delimiter><scope id> 8997d56d374SYoshinobu Inoue */ 9007d56d374SYoshinobu Inoue hostname2 = strdup(hostname); 9017d56d374SYoshinobu Inoue if (hostname2 == NULL) 9027d56d374SYoshinobu Inoue return EAI_MEMORY; 9037d56d374SYoshinobu Inoue /* terminate at the delimiter */ 9047d56d374SYoshinobu Inoue hostname2[cp - hostname] = '\0'; 905a42af91cSHajimu UMEMOTO addr = hostname2; 906a42af91cSHajimu UMEMOTO scope = cp + 1; 9077d56d374SYoshinobu Inoue 908a42af91cSHajimu UMEMOTO error = explore_numeric(pai, addr, servname, res); 9097d56d374SYoshinobu Inoue if (error == 0) { 910a42af91cSHajimu UMEMOTO int scopeid; 911a42af91cSHajimu UMEMOTO 9127d56d374SYoshinobu Inoue for (cur = *res; cur; cur = cur->ai_next) { 9137d56d374SYoshinobu Inoue if (cur->ai_family != AF_INET6) 9147d56d374SYoshinobu Inoue continue; 915a42af91cSHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; 916a42af91cSHajimu UMEMOTO if ((scopeid = ip6_str2scopeid(scope, sin6)) == -1) { 917a42af91cSHajimu UMEMOTO free(hostname2); 918b826397aSJun-ichiro itojun Hagino return(EAI_NODATA); /* XXX: is return OK? */ 919a42af91cSHajimu UMEMOTO } 920a42af91cSHajimu UMEMOTO sin6->sin6_scope_id = scopeid; 9217d56d374SYoshinobu Inoue } 9227d56d374SYoshinobu Inoue } 9237d56d374SYoshinobu Inoue 9247d56d374SYoshinobu Inoue free(hostname2); 9257d56d374SYoshinobu Inoue 9267d56d374SYoshinobu Inoue return error; 9277d56d374SYoshinobu Inoue #endif 9287d56d374SYoshinobu Inoue } 9297d56d374SYoshinobu Inoue 9307d56d374SYoshinobu Inoue static int 9317d56d374SYoshinobu Inoue get_canonname(pai, ai, str) 9327d56d374SYoshinobu Inoue const struct addrinfo *pai; 9337d56d374SYoshinobu Inoue struct addrinfo *ai; 9347d56d374SYoshinobu Inoue const char *str; 9357d56d374SYoshinobu Inoue { 9367d56d374SYoshinobu Inoue if ((pai->ai_flags & AI_CANONNAME) != 0) { 9377d56d374SYoshinobu Inoue ai->ai_canonname = (char *)malloc(strlen(str) + 1); 9387d56d374SYoshinobu Inoue if (ai->ai_canonname == NULL) 9397d56d374SYoshinobu Inoue return EAI_MEMORY; 9407d56d374SYoshinobu Inoue strcpy(ai->ai_canonname, str); 9417d56d374SYoshinobu Inoue } 9427d56d374SYoshinobu Inoue return 0; 9437d56d374SYoshinobu Inoue } 9447d56d374SYoshinobu Inoue 9457d56d374SYoshinobu Inoue static struct addrinfo * 9467d56d374SYoshinobu Inoue get_ai(pai, afd, addr) 9477d56d374SYoshinobu Inoue const struct addrinfo *pai; 9487d56d374SYoshinobu Inoue const struct afd *afd; 9497d56d374SYoshinobu Inoue const char *addr; 9507d56d374SYoshinobu Inoue { 9517d56d374SYoshinobu Inoue char *p; 9527d56d374SYoshinobu Inoue struct addrinfo *ai; 9537d56d374SYoshinobu Inoue #ifdef FAITH 9547d56d374SYoshinobu Inoue struct in6_addr faith_prefix; 9557d56d374SYoshinobu Inoue char *fp_str; 9567d56d374SYoshinobu Inoue int translate = 0; 9577d56d374SYoshinobu Inoue #endif 9587d56d374SYoshinobu Inoue 9597d56d374SYoshinobu Inoue #ifdef FAITH 9607d56d374SYoshinobu Inoue /* 9617d56d374SYoshinobu Inoue * Transfrom an IPv4 addr into a special IPv6 addr format for 9627d56d374SYoshinobu Inoue * IPv6->IPv4 translation gateway. (only TCP is supported now) 9637d56d374SYoshinobu Inoue * 9647d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 9657d56d374SYoshinobu Inoue * | faith prefix part (12 bytes) | embedded | 9667d56d374SYoshinobu Inoue * | | IPv4 addr part (4 bytes) 9677d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 9687d56d374SYoshinobu Inoue * 9697d56d374SYoshinobu Inoue * faith prefix part is specified as ascii IPv6 addr format 9707d56d374SYoshinobu Inoue * in environmental variable GAI. 9717d56d374SYoshinobu Inoue * For FAITH to work correctly, routing to faith prefix must be 9727d56d374SYoshinobu Inoue * setup toward a machine where a FAITH daemon operates. 9737d56d374SYoshinobu Inoue * Also, the machine must enable some mechanizm 9747d56d374SYoshinobu Inoue * (e.g. faith interface hack) to divert those packet with 9757d56d374SYoshinobu Inoue * faith prefixed destination addr to user-land FAITH daemon. 9767d56d374SYoshinobu Inoue */ 9777d56d374SYoshinobu Inoue fp_str = getenv("GAI"); 9787d56d374SYoshinobu Inoue if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 9797d56d374SYoshinobu Inoue afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 9807d56d374SYoshinobu Inoue u_int32_t v4a; 9817d56d374SYoshinobu Inoue u_int8_t v4a_top; 9827d56d374SYoshinobu Inoue 9837d56d374SYoshinobu Inoue memcpy(&v4a, addr, sizeof v4a); 9847d56d374SYoshinobu Inoue v4a_top = v4a >> IN_CLASSA_NSHIFT; 9857d56d374SYoshinobu Inoue if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 9867d56d374SYoshinobu Inoue v4a_top != 0 && v4a != IN_LOOPBACKNET) { 9877d56d374SYoshinobu Inoue afd = &afdl[N_INET6]; 9887d56d374SYoshinobu Inoue memcpy(&faith_prefix.s6_addr[12], addr, 9897d56d374SYoshinobu Inoue sizeof(struct in_addr)); 9907d56d374SYoshinobu Inoue translate = 1; 9917d56d374SYoshinobu Inoue } 9927d56d374SYoshinobu Inoue } 9937d56d374SYoshinobu Inoue #endif 9947d56d374SYoshinobu Inoue 9957d56d374SYoshinobu Inoue ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 9967d56d374SYoshinobu Inoue + (afd->a_socklen)); 9977d56d374SYoshinobu Inoue if (ai == NULL) 9987d56d374SYoshinobu Inoue return NULL; 9997d56d374SYoshinobu Inoue 10007d56d374SYoshinobu Inoue memcpy(ai, pai, sizeof(struct addrinfo)); 1001a42af91cSHajimu UMEMOTO ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 1002a42af91cSHajimu UMEMOTO memset(ai->ai_addr, 0, (size_t)afd->a_socklen); 10037d56d374SYoshinobu Inoue ai->ai_addr->sa_len = afd->a_socklen; 10047d56d374SYoshinobu Inoue ai->ai_addrlen = afd->a_socklen; 10057d56d374SYoshinobu Inoue ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 10067d56d374SYoshinobu Inoue p = (char *)(ai->ai_addr); 10077d56d374SYoshinobu Inoue #ifdef FAITH 10087d56d374SYoshinobu Inoue if (translate == 1) 10097d56d374SYoshinobu Inoue memcpy(p + afd->a_off, &faith_prefix, afd->a_addrlen); 10107d56d374SYoshinobu Inoue else 10117d56d374SYoshinobu Inoue #endif 10127d56d374SYoshinobu Inoue memcpy(p + afd->a_off, addr, afd->a_addrlen); 10137d56d374SYoshinobu Inoue 10147d56d374SYoshinobu Inoue return ai; 10157d56d374SYoshinobu Inoue } 10167d56d374SYoshinobu Inoue 10177d56d374SYoshinobu Inoue static int 10187d56d374SYoshinobu Inoue get_portmatch(ai, servname) 10197d56d374SYoshinobu Inoue const struct addrinfo *ai; 10207d56d374SYoshinobu Inoue const char *servname; 10217d56d374SYoshinobu Inoue { 1022a42af91cSHajimu UMEMOTO 10237d56d374SYoshinobu Inoue /* get_port does not touch first argument. when matchonly == 1. */ 1024a42af91cSHajimu UMEMOTO /* LINTED const cast */ 10257d56d374SYoshinobu Inoue return get_port((struct addrinfo *)ai, servname, 1); 10267d56d374SYoshinobu Inoue } 10277d56d374SYoshinobu Inoue 10287d56d374SYoshinobu Inoue static int 10297d56d374SYoshinobu Inoue get_port(ai, servname, matchonly) 10307d56d374SYoshinobu Inoue struct addrinfo *ai; 10317d56d374SYoshinobu Inoue const char *servname; 10327d56d374SYoshinobu Inoue int matchonly; 10337d56d374SYoshinobu Inoue { 10347d56d374SYoshinobu Inoue const char *proto; 10357d56d374SYoshinobu Inoue struct servent *sp; 10367d56d374SYoshinobu Inoue int port; 10377d56d374SYoshinobu Inoue int allownumeric; 10387d56d374SYoshinobu Inoue 10397d56d374SYoshinobu Inoue if (servname == NULL) 10407d56d374SYoshinobu Inoue return 0; 1041a42af91cSHajimu UMEMOTO switch (ai->ai_family) { 1042a42af91cSHajimu UMEMOTO case AF_INET: 1043a42af91cSHajimu UMEMOTO #ifdef AF_INET6 1044a42af91cSHajimu UMEMOTO case AF_INET6: 10457d56d374SYoshinobu Inoue #endif 1046a42af91cSHajimu UMEMOTO break; 1047a42af91cSHajimu UMEMOTO default: 10487d56d374SYoshinobu Inoue return 0; 1049a42af91cSHajimu UMEMOTO } 10507d56d374SYoshinobu Inoue 10517d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 10527d56d374SYoshinobu Inoue case SOCK_RAW: 10537d56d374SYoshinobu Inoue return EAI_SERVICE; 10547d56d374SYoshinobu Inoue case SOCK_DGRAM: 10557d56d374SYoshinobu Inoue case SOCK_STREAM: 10567d56d374SYoshinobu Inoue allownumeric = 1; 10577d56d374SYoshinobu Inoue break; 10587d56d374SYoshinobu Inoue case ANY: 10597d56d374SYoshinobu Inoue allownumeric = 0; 10607d56d374SYoshinobu Inoue break; 10617d56d374SYoshinobu Inoue default: 10627d56d374SYoshinobu Inoue return EAI_SOCKTYPE; 10637d56d374SYoshinobu Inoue } 10647d56d374SYoshinobu Inoue 10657d56d374SYoshinobu Inoue if (str_isnumber(servname)) { 10667d56d374SYoshinobu Inoue if (!allownumeric) 10677d56d374SYoshinobu Inoue return EAI_SERVICE; 10687d56d374SYoshinobu Inoue port = htons(atoi(servname)); 10697d56d374SYoshinobu Inoue if (port < 0 || port > 65535) 10707d56d374SYoshinobu Inoue return EAI_SERVICE; 10717d56d374SYoshinobu Inoue } else { 10727d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 10737d56d374SYoshinobu Inoue case SOCK_DGRAM: 10747d56d374SYoshinobu Inoue proto = "udp"; 10757d56d374SYoshinobu Inoue break; 10767d56d374SYoshinobu Inoue case SOCK_STREAM: 10777d56d374SYoshinobu Inoue proto = "tcp"; 10787d56d374SYoshinobu Inoue break; 10797d56d374SYoshinobu Inoue default: 10807d56d374SYoshinobu Inoue proto = NULL; 10817d56d374SYoshinobu Inoue break; 10827d56d374SYoshinobu Inoue } 10837d56d374SYoshinobu Inoue 10847d56d374SYoshinobu Inoue if ((sp = getservbyname(servname, proto)) == NULL) 10857d56d374SYoshinobu Inoue return EAI_SERVICE; 10867d56d374SYoshinobu Inoue port = sp->s_port; 10877d56d374SYoshinobu Inoue } 10887d56d374SYoshinobu Inoue 10897d56d374SYoshinobu Inoue if (!matchonly) { 10907d56d374SYoshinobu Inoue switch (ai->ai_family) { 10917d56d374SYoshinobu Inoue case AF_INET: 1092a42af91cSHajimu UMEMOTO ((struct sockaddr_in *)(void *) 1093a42af91cSHajimu UMEMOTO ai->ai_addr)->sin_port = port; 10947d56d374SYoshinobu Inoue break; 10957d56d374SYoshinobu Inoue #ifdef INET6 10967d56d374SYoshinobu Inoue case AF_INET6: 1097a42af91cSHajimu UMEMOTO ((struct sockaddr_in6 *)(void *) 1098a42af91cSHajimu UMEMOTO ai->ai_addr)->sin6_port = port; 10997d56d374SYoshinobu Inoue break; 11007d56d374SYoshinobu Inoue #endif 11017d56d374SYoshinobu Inoue } 11027d56d374SYoshinobu Inoue } 11037d56d374SYoshinobu Inoue 11047d56d374SYoshinobu Inoue return 0; 11057d56d374SYoshinobu Inoue } 11067d56d374SYoshinobu Inoue 11077d56d374SYoshinobu Inoue static const struct afd * 11087d56d374SYoshinobu Inoue find_afd(af) 11097d56d374SYoshinobu Inoue int af; 11107d56d374SYoshinobu Inoue { 11117d56d374SYoshinobu Inoue const struct afd *afd; 11127d56d374SYoshinobu Inoue 11137d56d374SYoshinobu Inoue if (af == PF_UNSPEC) 11147d56d374SYoshinobu Inoue return NULL; 11157d56d374SYoshinobu Inoue for (afd = afdl; afd->a_af; afd++) { 11167d56d374SYoshinobu Inoue if (afd->a_af == af) 11177d56d374SYoshinobu Inoue return afd; 11187d56d374SYoshinobu Inoue } 11197d56d374SYoshinobu Inoue return NULL; 11207d56d374SYoshinobu Inoue } 1121a42af91cSHajimu UMEMOTO 1122a42af91cSHajimu UMEMOTO /* 1123a42af91cSHajimu UMEMOTO * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend 1124a42af91cSHajimu UMEMOTO * will take care of it. 1125a42af91cSHajimu UMEMOTO * the semantics of AI_ADDRCONFIG is not defined well. we are not sure 1126a42af91cSHajimu UMEMOTO * if the code is right or not. 1127a42af91cSHajimu UMEMOTO * 1128a42af91cSHajimu UMEMOTO * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with 1129a42af91cSHajimu UMEMOTO * _dns_getaddrinfo. 1130a42af91cSHajimu UMEMOTO */ 1131a42af91cSHajimu UMEMOTO static int 1132a42af91cSHajimu UMEMOTO addrconfig(pai) 1133a42af91cSHajimu UMEMOTO struct addrinfo *pai; 1134a42af91cSHajimu UMEMOTO { 1135a42af91cSHajimu UMEMOTO int s, af; 1136a42af91cSHajimu UMEMOTO 1137a42af91cSHajimu UMEMOTO /* 1138a42af91cSHajimu UMEMOTO * TODO: 1139a42af91cSHajimu UMEMOTO * Note that implementation dependent test for address 1140a42af91cSHajimu UMEMOTO * configuration should be done everytime called 1141a42af91cSHajimu UMEMOTO * (or apropriate interval), 1142a42af91cSHajimu UMEMOTO * because addresses will be dynamically assigned or deleted. 1143a42af91cSHajimu UMEMOTO */ 1144a42af91cSHajimu UMEMOTO af = pai->ai_family; 1145a42af91cSHajimu UMEMOTO if (af == AF_UNSPEC) { 1146a42af91cSHajimu UMEMOTO if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1147a42af91cSHajimu UMEMOTO af = AF_INET; 1148a42af91cSHajimu UMEMOTO else { 1149a42af91cSHajimu UMEMOTO close(s); 1150a42af91cSHajimu UMEMOTO if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1151a42af91cSHajimu UMEMOTO af = AF_INET6; 1152a42af91cSHajimu UMEMOTO else 1153a42af91cSHajimu UMEMOTO close(s); 1154a42af91cSHajimu UMEMOTO } 1155a42af91cSHajimu UMEMOTO 1156a42af91cSHajimu UMEMOTO } 1157a42af91cSHajimu UMEMOTO if (af != AF_UNSPEC) { 1158a42af91cSHajimu UMEMOTO if ((s = socket(af, SOCK_DGRAM, 0)) < 0) 1159a42af91cSHajimu UMEMOTO return 0; 1160a42af91cSHajimu UMEMOTO close(s); 1161a42af91cSHajimu UMEMOTO } 1162a42af91cSHajimu UMEMOTO pai->ai_family = af; 1163a42af91cSHajimu UMEMOTO return 1; 1164a42af91cSHajimu UMEMOTO } 1165a42af91cSHajimu UMEMOTO 1166a42af91cSHajimu UMEMOTO #ifdef INET6 1167a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */ 1168a42af91cSHajimu UMEMOTO static int 1169a42af91cSHajimu UMEMOTO ip6_str2scopeid(scope, sin6) 1170a42af91cSHajimu UMEMOTO char *scope; 1171a42af91cSHajimu UMEMOTO struct sockaddr_in6 *sin6; 1172a42af91cSHajimu UMEMOTO { 1173a42af91cSHajimu UMEMOTO int scopeid; 1174a42af91cSHajimu UMEMOTO struct in6_addr *a6 = &sin6->sin6_addr; 1175a42af91cSHajimu UMEMOTO char *ep; 1176a42af91cSHajimu UMEMOTO 1177ec20fe00SJun-ichiro itojun Hagino /* empty scopeid portion is invalid */ 1178ec20fe00SJun-ichiro itojun Hagino if (*scope == '\0') 1179ec20fe00SJun-ichiro itojun Hagino return -1; 1180ec20fe00SJun-ichiro itojun Hagino 1181a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { 1182a42af91cSHajimu UMEMOTO /* 1183a42af91cSHajimu UMEMOTO * We currently assume a one-to-one mapping between links 1184a42af91cSHajimu UMEMOTO * and interfaces, so we simply use interface indices for 1185a42af91cSHajimu UMEMOTO * like-local scopes. 1186a42af91cSHajimu UMEMOTO */ 1187a42af91cSHajimu UMEMOTO scopeid = if_nametoindex(scope); 1188a42af91cSHajimu UMEMOTO if (scopeid == 0) 1189a42af91cSHajimu UMEMOTO goto trynumeric; 1190a42af91cSHajimu UMEMOTO return(scopeid); 1191a42af91cSHajimu UMEMOTO } 1192a42af91cSHajimu UMEMOTO 1193a42af91cSHajimu UMEMOTO /* still unclear about literal, allow numeric only - placeholder */ 1194a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) 1195a42af91cSHajimu UMEMOTO goto trynumeric; 1196a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) 1197a42af91cSHajimu UMEMOTO goto trynumeric; 1198a42af91cSHajimu UMEMOTO else 1199a42af91cSHajimu UMEMOTO goto trynumeric; /* global */ 1200a42af91cSHajimu UMEMOTO 1201a42af91cSHajimu UMEMOTO /* try to convert to a numeric id as a last resort */ 1202a42af91cSHajimu UMEMOTO trynumeric: 1203a42af91cSHajimu UMEMOTO scopeid = (int)strtoul(scope, &ep, 10); 1204a42af91cSHajimu UMEMOTO if (*ep == '\0') 1205a42af91cSHajimu UMEMOTO return scopeid; 1206a42af91cSHajimu UMEMOTO else 1207a42af91cSHajimu UMEMOTO return -1; 1208a42af91cSHajimu UMEMOTO } 1209a42af91cSHajimu UMEMOTO #endif 1210a42af91cSHajimu UMEMOTO 1211a42af91cSHajimu UMEMOTO #ifdef DEBUG 1212a42af91cSHajimu UMEMOTO static const char AskedForGot[] = 1213a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 1214a42af91cSHajimu UMEMOTO #endif 1215a42af91cSHajimu UMEMOTO 1216a42af91cSHajimu UMEMOTO static struct addrinfo * 1217a42af91cSHajimu UMEMOTO getanswer(answer, anslen, qname, qtype, pai) 1218a42af91cSHajimu UMEMOTO const querybuf *answer; 1219a42af91cSHajimu UMEMOTO int anslen; 1220a42af91cSHajimu UMEMOTO const char *qname; 1221a42af91cSHajimu UMEMOTO int qtype; 1222a42af91cSHajimu UMEMOTO const struct addrinfo *pai; 1223a42af91cSHajimu UMEMOTO { 1224a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1225a42af91cSHajimu UMEMOTO struct addrinfo ai; 1226a42af91cSHajimu UMEMOTO const struct afd *afd; 1227a42af91cSHajimu UMEMOTO char *canonname; 1228a42af91cSHajimu UMEMOTO const HEADER *hp; 1229a42af91cSHajimu UMEMOTO const u_char *cp; 1230a42af91cSHajimu UMEMOTO int n; 1231a42af91cSHajimu UMEMOTO const u_char *eom; 1232a42af91cSHajimu UMEMOTO char *bp; 1233a42af91cSHajimu UMEMOTO int type, class, buflen, ancount, qdcount; 1234a42af91cSHajimu UMEMOTO int haveanswer, had_error; 1235a42af91cSHajimu UMEMOTO char tbuf[MAXDNAME]; 1236a42af91cSHajimu UMEMOTO int (*name_ok) __P((const char *)); 1237a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 1238a42af91cSHajimu UMEMOTO 1239a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 1240a42af91cSHajimu UMEMOTO cur = &sentinel; 1241a42af91cSHajimu UMEMOTO 1242a42af91cSHajimu UMEMOTO canonname = NULL; 1243a42af91cSHajimu UMEMOTO eom = answer->buf + anslen; 1244a42af91cSHajimu UMEMOTO switch (qtype) { 1245a42af91cSHajimu UMEMOTO case T_A: 1246a42af91cSHajimu UMEMOTO case T_AAAA: 1247a42af91cSHajimu UMEMOTO case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ 1248a42af91cSHajimu UMEMOTO name_ok = res_hnok; 1249a42af91cSHajimu UMEMOTO break; 1250a42af91cSHajimu UMEMOTO default: 1251a42af91cSHajimu UMEMOTO return (NULL); /* XXX should be abort(); */ 1252a42af91cSHajimu UMEMOTO } 1253a42af91cSHajimu UMEMOTO /* 1254a42af91cSHajimu UMEMOTO * find first satisfactory answer 1255a42af91cSHajimu UMEMOTO */ 1256a42af91cSHajimu UMEMOTO hp = &answer->hdr; 1257a42af91cSHajimu UMEMOTO ancount = ntohs(hp->ancount); 1258a42af91cSHajimu UMEMOTO qdcount = ntohs(hp->qdcount); 1259a42af91cSHajimu UMEMOTO bp = hostbuf; 1260a42af91cSHajimu UMEMOTO buflen = sizeof hostbuf; 1261a42af91cSHajimu UMEMOTO cp = answer->buf + HFIXEDSZ; 1262a42af91cSHajimu UMEMOTO if (qdcount != 1) { 1263a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1264a42af91cSHajimu UMEMOTO return (NULL); 1265a42af91cSHajimu UMEMOTO } 1266a42af91cSHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, buflen); 1267a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 1268a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1269a42af91cSHajimu UMEMOTO return (NULL); 1270a42af91cSHajimu UMEMOTO } 1271a42af91cSHajimu UMEMOTO cp += n + QFIXEDSZ; 1272a42af91cSHajimu UMEMOTO if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 1273a42af91cSHajimu UMEMOTO /* res_send() has already verified that the query name is the 1274a42af91cSHajimu UMEMOTO * same as the one we sent; this just gets the expanded name 1275a42af91cSHajimu UMEMOTO * (i.e., with the succeeding search-domain tacked on). 1276a42af91cSHajimu UMEMOTO */ 1277a42af91cSHajimu UMEMOTO n = strlen(bp) + 1; /* for the \0 */ 1278a42af91cSHajimu UMEMOTO if (n >= MAXHOSTNAMELEN) { 1279a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1280a42af91cSHajimu UMEMOTO return (NULL); 1281a42af91cSHajimu UMEMOTO } 1282a42af91cSHajimu UMEMOTO canonname = bp; 1283a42af91cSHajimu UMEMOTO bp += n; 1284a42af91cSHajimu UMEMOTO buflen -= n; 1285a42af91cSHajimu UMEMOTO /* The qname can be abbreviated, but h_name is now absolute. */ 1286a42af91cSHajimu UMEMOTO qname = canonname; 1287a42af91cSHajimu UMEMOTO } 1288a42af91cSHajimu UMEMOTO haveanswer = 0; 1289a42af91cSHajimu UMEMOTO had_error = 0; 1290a42af91cSHajimu UMEMOTO while (ancount-- > 0 && cp < eom && !had_error) { 1291a42af91cSHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, buflen); 1292a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 1293a42af91cSHajimu UMEMOTO had_error++; 1294a42af91cSHajimu UMEMOTO continue; 1295a42af91cSHajimu UMEMOTO } 1296a42af91cSHajimu UMEMOTO cp += n; /* name */ 1297a42af91cSHajimu UMEMOTO type = _getshort(cp); 1298a42af91cSHajimu UMEMOTO cp += INT16SZ; /* type */ 1299a42af91cSHajimu UMEMOTO class = _getshort(cp); 1300a42af91cSHajimu UMEMOTO cp += INT16SZ + INT32SZ; /* class, TTL */ 1301a42af91cSHajimu UMEMOTO n = _getshort(cp); 1302a42af91cSHajimu UMEMOTO cp += INT16SZ; /* len */ 1303a42af91cSHajimu UMEMOTO if (class != C_IN) { 1304a42af91cSHajimu UMEMOTO /* XXX - debug? syslog? */ 1305a42af91cSHajimu UMEMOTO cp += n; 1306a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1307a42af91cSHajimu UMEMOTO } 1308a42af91cSHajimu UMEMOTO if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 1309a42af91cSHajimu UMEMOTO type == T_CNAME) { 1310a42af91cSHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1311a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(tbuf)) { 1312a42af91cSHajimu UMEMOTO had_error++; 1313a42af91cSHajimu UMEMOTO continue; 1314a42af91cSHajimu UMEMOTO } 1315a42af91cSHajimu UMEMOTO cp += n; 1316a42af91cSHajimu UMEMOTO /* Get canonical name. */ 1317a42af91cSHajimu UMEMOTO n = strlen(tbuf) + 1; /* for the \0 */ 1318a42af91cSHajimu UMEMOTO if (n > buflen || n >= MAXHOSTNAMELEN) { 1319a42af91cSHajimu UMEMOTO had_error++; 1320a42af91cSHajimu UMEMOTO continue; 1321a42af91cSHajimu UMEMOTO } 1322a42af91cSHajimu UMEMOTO strcpy(bp, tbuf); 1323a42af91cSHajimu UMEMOTO canonname = bp; 1324a42af91cSHajimu UMEMOTO bp += n; 1325a42af91cSHajimu UMEMOTO buflen -= n; 1326a42af91cSHajimu UMEMOTO continue; 1327a42af91cSHajimu UMEMOTO } 1328a42af91cSHajimu UMEMOTO if (qtype == T_ANY) { 1329a42af91cSHajimu UMEMOTO if (!(type == T_A || type == T_AAAA)) { 1330a42af91cSHajimu UMEMOTO cp += n; 1331a42af91cSHajimu UMEMOTO continue; 1332a42af91cSHajimu UMEMOTO } 1333a42af91cSHajimu UMEMOTO } else if (type != qtype) { 1334a42af91cSHajimu UMEMOTO #ifdef DEBUG 1335a42af91cSHajimu UMEMOTO if (type != T_KEY && type != T_SIG) 1336a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 1337a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 1338a42af91cSHajimu UMEMOTO qname, p_class(C_IN), p_type(qtype), 1339a42af91cSHajimu UMEMOTO p_type(type)); 1340a42af91cSHajimu UMEMOTO #endif 1341a42af91cSHajimu UMEMOTO cp += n; 1342a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1343a42af91cSHajimu UMEMOTO } 1344a42af91cSHajimu UMEMOTO switch (type) { 1345a42af91cSHajimu UMEMOTO case T_A: 1346a42af91cSHajimu UMEMOTO case T_AAAA: 1347a42af91cSHajimu UMEMOTO if (strcasecmp(canonname, bp) != 0) { 1348a42af91cSHajimu UMEMOTO #ifdef DEBUG 1349a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 1350a42af91cSHajimu UMEMOTO AskedForGot, canonname, bp); 1351a42af91cSHajimu UMEMOTO #endif 1352a42af91cSHajimu UMEMOTO cp += n; 1353a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1354a42af91cSHajimu UMEMOTO } 1355a42af91cSHajimu UMEMOTO if (type == T_A && n != INADDRSZ) { 1356a42af91cSHajimu UMEMOTO cp += n; 1357a42af91cSHajimu UMEMOTO continue; 1358a42af91cSHajimu UMEMOTO } 1359a42af91cSHajimu UMEMOTO if (type == T_AAAA && n != IN6ADDRSZ) { 1360a42af91cSHajimu UMEMOTO cp += n; 1361a42af91cSHajimu UMEMOTO continue; 1362a42af91cSHajimu UMEMOTO } 1363a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 1364a42af91cSHajimu UMEMOTO if (type == T_AAAA) { 1365a42af91cSHajimu UMEMOTO struct in6_addr in6; 1366a42af91cSHajimu UMEMOTO memcpy(&in6, cp, sizeof(in6)); 1367a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&in6)) { 1368a42af91cSHajimu UMEMOTO cp += n; 1369a42af91cSHajimu UMEMOTO continue; 1370a42af91cSHajimu UMEMOTO } 1371a42af91cSHajimu UMEMOTO } 1372a42af91cSHajimu UMEMOTO #endif 1373a42af91cSHajimu UMEMOTO if (!haveanswer) { 1374a42af91cSHajimu UMEMOTO int nn; 1375a42af91cSHajimu UMEMOTO 1376a42af91cSHajimu UMEMOTO canonname = bp; 1377a42af91cSHajimu UMEMOTO nn = strlen(bp) + 1; /* for the \0 */ 1378a42af91cSHajimu UMEMOTO bp += nn; 1379a42af91cSHajimu UMEMOTO buflen -= nn; 1380a42af91cSHajimu UMEMOTO } 1381a42af91cSHajimu UMEMOTO 1382a42af91cSHajimu UMEMOTO /* don't overwrite pai */ 1383a42af91cSHajimu UMEMOTO ai = *pai; 1384a42af91cSHajimu UMEMOTO ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; 1385a42af91cSHajimu UMEMOTO afd = find_afd(ai.ai_family); 1386a42af91cSHajimu UMEMOTO if (afd == NULL) { 1387a42af91cSHajimu UMEMOTO cp += n; 1388a42af91cSHajimu UMEMOTO continue; 1389a42af91cSHajimu UMEMOTO } 1390a42af91cSHajimu UMEMOTO cur->ai_next = get_ai(&ai, afd, (const char *)cp); 1391a42af91cSHajimu UMEMOTO if (cur->ai_next == NULL) 1392a42af91cSHajimu UMEMOTO had_error++; 1393a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1394a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1395a42af91cSHajimu UMEMOTO cp += n; 1396a42af91cSHajimu UMEMOTO break; 1397a42af91cSHajimu UMEMOTO default: 1398a42af91cSHajimu UMEMOTO abort(); 1399a42af91cSHajimu UMEMOTO } 1400a42af91cSHajimu UMEMOTO if (!had_error) 1401a42af91cSHajimu UMEMOTO haveanswer++; 1402a42af91cSHajimu UMEMOTO } 1403a42af91cSHajimu UMEMOTO if (haveanswer) { 1404a42af91cSHajimu UMEMOTO if (!canonname) 1405a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, qname); 1406a42af91cSHajimu UMEMOTO else 1407a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, canonname); 1408a42af91cSHajimu UMEMOTO h_errno = NETDB_SUCCESS; 1409a42af91cSHajimu UMEMOTO return sentinel.ai_next; 1410a42af91cSHajimu UMEMOTO } 1411a42af91cSHajimu UMEMOTO 1412a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1413a42af91cSHajimu UMEMOTO return NULL; 1414a42af91cSHajimu UMEMOTO } 1415a42af91cSHajimu UMEMOTO 1416a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 1417a42af91cSHajimu UMEMOTO static int 1418a42af91cSHajimu UMEMOTO _dns_getaddrinfo(pai, hostname, res) 1419a42af91cSHajimu UMEMOTO const struct addrinfo *pai; 1420a42af91cSHajimu UMEMOTO const char *hostname; 1421a42af91cSHajimu UMEMOTO struct addrinfo **res; 1422a42af91cSHajimu UMEMOTO { 1423a42af91cSHajimu UMEMOTO struct addrinfo *ai; 1424a42af91cSHajimu UMEMOTO querybuf buf, buf2; 1425a42af91cSHajimu UMEMOTO const char *name; 1426a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1427a42af91cSHajimu UMEMOTO struct res_target q, q2; 1428a42af91cSHajimu UMEMOTO 1429a42af91cSHajimu UMEMOTO memset(&q, 0, sizeof(q2)); 1430a42af91cSHajimu UMEMOTO memset(&q2, 0, sizeof(q2)); 1431a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 1432a42af91cSHajimu UMEMOTO cur = &sentinel; 1433a42af91cSHajimu UMEMOTO 1434a42af91cSHajimu UMEMOTO switch (pai->ai_family) { 1435a42af91cSHajimu UMEMOTO case AF_UNSPEC: 1436a42af91cSHajimu UMEMOTO /* prefer IPv6 */ 1437b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 1438b826397aSJun-ichiro itojun Hagino q.qtype = T_AAAA; 1439a42af91cSHajimu UMEMOTO q.answer = buf.buf; 1440a42af91cSHajimu UMEMOTO q.anslen = sizeof(buf); 1441a42af91cSHajimu UMEMOTO q.next = &q2; 1442b826397aSJun-ichiro itojun Hagino q2.qclass = C_IN; 1443b826397aSJun-ichiro itojun Hagino q2.qtype = T_A; 1444a42af91cSHajimu UMEMOTO q2.answer = buf2.buf; 1445a42af91cSHajimu UMEMOTO q2.anslen = sizeof(buf2); 1446a42af91cSHajimu UMEMOTO break; 1447a42af91cSHajimu UMEMOTO case AF_INET: 1448b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 1449b826397aSJun-ichiro itojun Hagino q.qtype = T_A; 1450a42af91cSHajimu UMEMOTO q.answer = buf.buf; 1451a42af91cSHajimu UMEMOTO q.anslen = sizeof(buf); 1452a42af91cSHajimu UMEMOTO break; 1453a42af91cSHajimu UMEMOTO case AF_INET6: 1454b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 1455b826397aSJun-ichiro itojun Hagino q.qtype = T_AAAA; 1456a42af91cSHajimu UMEMOTO q.answer = buf.buf; 1457a42af91cSHajimu UMEMOTO q.anslen = sizeof(buf); 1458a42af91cSHajimu UMEMOTO break; 1459a42af91cSHajimu UMEMOTO default: 1460a42af91cSHajimu UMEMOTO return EAI_FAIL; 1461a42af91cSHajimu UMEMOTO } 1462a42af91cSHajimu UMEMOTO if (res_searchN(hostname, &q) < 0) 1463a42af91cSHajimu UMEMOTO return EAI_NODATA; 1464b826397aSJun-ichiro itojun Hagino ai = getanswer(&buf, q.n, q.name, q.qtype, pai); 1465a42af91cSHajimu UMEMOTO if (ai) { 1466a42af91cSHajimu UMEMOTO cur->ai_next = ai; 1467a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1468a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1469a42af91cSHajimu UMEMOTO } 1470a42af91cSHajimu UMEMOTO if (q.next) { 1471b826397aSJun-ichiro itojun Hagino ai = getanswer(&buf2, q2.n, q2.name, q2.qtype, pai); 1472a42af91cSHajimu UMEMOTO if (ai) 1473a42af91cSHajimu UMEMOTO cur->ai_next = ai; 1474a42af91cSHajimu UMEMOTO } 1475a42af91cSHajimu UMEMOTO if (sentinel.ai_next == NULL) 1476a42af91cSHajimu UMEMOTO switch (h_errno) { 1477a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 1478a42af91cSHajimu UMEMOTO return EAI_NODATA; 1479a42af91cSHajimu UMEMOTO case TRY_AGAIN: 1480a42af91cSHajimu UMEMOTO return EAI_AGAIN; 1481a42af91cSHajimu UMEMOTO default: 1482a42af91cSHajimu UMEMOTO return EAI_FAIL; 1483a42af91cSHajimu UMEMOTO } 1484a42af91cSHajimu UMEMOTO *res = sentinel.ai_next; 1485a42af91cSHajimu UMEMOTO return 0; 1486a42af91cSHajimu UMEMOTO } 1487a42af91cSHajimu UMEMOTO 1488a42af91cSHajimu UMEMOTO static struct addrinfo * 1489a42af91cSHajimu UMEMOTO _gethtent(hostf, name, pai) 1490a42af91cSHajimu UMEMOTO FILE *hostf; 1491a42af91cSHajimu UMEMOTO const char *name; 1492a42af91cSHajimu UMEMOTO const struct addrinfo *pai; 1493a42af91cSHajimu UMEMOTO { 1494a42af91cSHajimu UMEMOTO char *p; 1495a42af91cSHajimu UMEMOTO char *cp, *tname, *cname; 1496a42af91cSHajimu UMEMOTO struct addrinfo hints, *res0, *res; 1497a42af91cSHajimu UMEMOTO int error; 1498a42af91cSHajimu UMEMOTO const char *addr; 1499a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 1500a42af91cSHajimu UMEMOTO 1501a42af91cSHajimu UMEMOTO again: 1502a42af91cSHajimu UMEMOTO if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) 1503a42af91cSHajimu UMEMOTO return (NULL); 1504a42af91cSHajimu UMEMOTO if (*p == '#') 1505a42af91cSHajimu UMEMOTO goto again; 1506a42af91cSHajimu UMEMOTO if (!(cp = strpbrk(p, "#\n"))) 1507a42af91cSHajimu UMEMOTO goto again; 1508a42af91cSHajimu UMEMOTO *cp = '\0'; 1509a42af91cSHajimu UMEMOTO if (!(cp = strpbrk(p, " \t"))) 1510a42af91cSHajimu UMEMOTO goto again; 1511a42af91cSHajimu UMEMOTO *cp++ = '\0'; 1512a42af91cSHajimu UMEMOTO addr = p; 1513a42af91cSHajimu UMEMOTO cname = NULL; 1514a42af91cSHajimu UMEMOTO /* if this is not something we're looking for, skip it. */ 1515a42af91cSHajimu UMEMOTO while (cp && *cp) { 1516a42af91cSHajimu UMEMOTO if (*cp == ' ' || *cp == '\t') { 1517a42af91cSHajimu UMEMOTO cp++; 1518a42af91cSHajimu UMEMOTO continue; 1519a42af91cSHajimu UMEMOTO } 1520a42af91cSHajimu UMEMOTO tname = cp; 1521a42af91cSHajimu UMEMOTO if (cname == NULL) 1522a42af91cSHajimu UMEMOTO cname = cp; 1523a42af91cSHajimu UMEMOTO if ((cp = strpbrk(cp, " \t")) != NULL) 1524a42af91cSHajimu UMEMOTO *cp++ = '\0'; 1525a42af91cSHajimu UMEMOTO if (strcasecmp(name, tname) == 0) 1526a42af91cSHajimu UMEMOTO goto found; 1527a42af91cSHajimu UMEMOTO } 1528a42af91cSHajimu UMEMOTO goto again; 1529a42af91cSHajimu UMEMOTO 1530a42af91cSHajimu UMEMOTO found: 1531a42af91cSHajimu UMEMOTO hints = *pai; 1532a42af91cSHajimu UMEMOTO hints.ai_flags = AI_NUMERICHOST; 1533a42af91cSHajimu UMEMOTO error = getaddrinfo(addr, NULL, &hints, &res0); 1534a42af91cSHajimu UMEMOTO if (error) 1535a42af91cSHajimu UMEMOTO goto again; 1536a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 1537a42af91cSHajimu UMEMOTO /* XXX should check all items in the chain */ 1538a42af91cSHajimu UMEMOTO if (res0->ai_family == AF_INET6 && 1539a42af91cSHajimu UMEMOTO IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) { 1540a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 1541a42af91cSHajimu UMEMOTO goto again; 1542a42af91cSHajimu UMEMOTO } 1543a42af91cSHajimu UMEMOTO #endif 1544a42af91cSHajimu UMEMOTO for (res = res0; res; res = res->ai_next) { 1545a42af91cSHajimu UMEMOTO /* cover it up */ 1546a42af91cSHajimu UMEMOTO res->ai_flags = pai->ai_flags; 1547a42af91cSHajimu UMEMOTO 1548a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_CANONNAME) { 1549a42af91cSHajimu UMEMOTO if (get_canonname(pai, res, cname) != 0) { 1550a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 1551a42af91cSHajimu UMEMOTO goto again; 1552a42af91cSHajimu UMEMOTO } 1553a42af91cSHajimu UMEMOTO } 1554a42af91cSHajimu UMEMOTO } 1555a42af91cSHajimu UMEMOTO return res0; 1556a42af91cSHajimu UMEMOTO } 1557a42af91cSHajimu UMEMOTO 1558a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 1559a42af91cSHajimu UMEMOTO static int 1560a42af91cSHajimu UMEMOTO _files_getaddrinfo(pai, hostname, res) 1561a42af91cSHajimu UMEMOTO const struct addrinfo *pai; 1562a42af91cSHajimu UMEMOTO const char *hostname; 1563a42af91cSHajimu UMEMOTO struct addrinfo **res; 1564a42af91cSHajimu UMEMOTO { 1565a42af91cSHajimu UMEMOTO FILE *hostf; 1566a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1567a42af91cSHajimu UMEMOTO struct addrinfo *p; 1568a42af91cSHajimu UMEMOTO 1569a42af91cSHajimu UMEMOTO sentinel.ai_next = NULL; 1570a42af91cSHajimu UMEMOTO cur = &sentinel; 1571a42af91cSHajimu UMEMOTO 1572a42af91cSHajimu UMEMOTO if ((hostf = fopen(_PATH_HOSTS, "r")) == NULL) 1573a42af91cSHajimu UMEMOTO return EAI_FAIL; 1574a42af91cSHajimu UMEMOTO while ((p = _gethtent(hostf, hostname, pai)) != NULL) { 1575a42af91cSHajimu UMEMOTO cur->ai_next = p; 1576a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1577a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1578a42af91cSHajimu UMEMOTO } 1579a42af91cSHajimu UMEMOTO fclose(hostf); 1580a42af91cSHajimu UMEMOTO 1581a42af91cSHajimu UMEMOTO if (!sentinel.ai_next) 1582a42af91cSHajimu UMEMOTO return EAI_NODATA; 1583a42af91cSHajimu UMEMOTO 1584a42af91cSHajimu UMEMOTO *res = sentinel.ai_next; 1585a42af91cSHajimu UMEMOTO return 0; 1586a42af91cSHajimu UMEMOTO } 1587a42af91cSHajimu UMEMOTO 1588a42af91cSHajimu UMEMOTO #ifdef YP 1589a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 1590a42af91cSHajimu UMEMOTO static int 1591a42af91cSHajimu UMEMOTO _nis_getaddrinfo(pai, hostname, res) 1592a42af91cSHajimu UMEMOTO const struct addrinfo *pai; 1593a42af91cSHajimu UMEMOTO const char *hostname; 1594a42af91cSHajimu UMEMOTO struct addrinfo **res; 1595a42af91cSHajimu UMEMOTO { 1596a42af91cSHajimu UMEMOTO struct hostent *hp; 1597a42af91cSHajimu UMEMOTO int h_error; 1598a42af91cSHajimu UMEMOTO int af; 1599a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1600a42af91cSHajimu UMEMOTO int i; 1601a42af91cSHajimu UMEMOTO const struct afd *afd; 1602a42af91cSHajimu UMEMOTO int error; 1603a42af91cSHajimu UMEMOTO 1604a42af91cSHajimu UMEMOTO sentinel.ai_next = NULL; 1605a42af91cSHajimu UMEMOTO cur = &sentinel; 1606a42af91cSHajimu UMEMOTO 1607a42af91cSHajimu UMEMOTO af = (pai->ai_family == AF_UNSPEC) ? AF_INET : pai->ai_family; 1608a42af91cSHajimu UMEMOTO if (af != AF_INET) 1609a42af91cSHajimu UMEMOTO return (EAI_ADDRFAMILY); 1610a42af91cSHajimu UMEMOTO 1611a42af91cSHajimu UMEMOTO if ((hp = _gethostbynisname(hostname, af)) == NULL) { 1612a42af91cSHajimu UMEMOTO switch (errno) { 1613a42af91cSHajimu UMEMOTO /* XXX: should be filled in */ 1614a42af91cSHajimu UMEMOTO default: 1615a42af91cSHajimu UMEMOTO error = EAI_FAIL; 1616a42af91cSHajimu UMEMOTO break; 1617a42af91cSHajimu UMEMOTO } 1618a42af91cSHajimu UMEMOTO } else if (hp->h_name == NULL || 1619a42af91cSHajimu UMEMOTO hp->h_name[0] == 0 || hp->h_addr_list[0] == NULL) { 1620a42af91cSHajimu UMEMOTO hp = NULL; 1621a42af91cSHajimu UMEMOTO error = EAI_FAIL; 1622a42af91cSHajimu UMEMOTO } 1623a42af91cSHajimu UMEMOTO 1624a42af91cSHajimu UMEMOTO if (hp == NULL) 1625a42af91cSHajimu UMEMOTO return error; 1626a42af91cSHajimu UMEMOTO 1627a42af91cSHajimu UMEMOTO for (i = 0; hp->h_addr_list[i] != NULL; i++) { 1628a42af91cSHajimu UMEMOTO if (hp->h_addrtype != af) 1629a42af91cSHajimu UMEMOTO continue; 1630a42af91cSHajimu UMEMOTO 1631a42af91cSHajimu UMEMOTO afd = find_afd(hp->h_addrtype); 1632a42af91cSHajimu UMEMOTO if (afd == NULL) 1633a42af91cSHajimu UMEMOTO continue; 1634a42af91cSHajimu UMEMOTO 1635a42af91cSHajimu UMEMOTO GET_AI(cur->ai_next, afd, hp->h_addr_list[i]); 1636a42af91cSHajimu UMEMOTO if ((pai->ai_flags & AI_CANONNAME) != 0) { 1637a42af91cSHajimu UMEMOTO /* 1638a42af91cSHajimu UMEMOTO * RFC2553 says that ai_canonname will be set only for 1639a42af91cSHajimu UMEMOTO * the first element. we do it for all the elements, 1640a42af91cSHajimu UMEMOTO * just for convenience. 1641a42af91cSHajimu UMEMOTO */ 1642a42af91cSHajimu UMEMOTO GET_CANONNAME(cur->ai_next, hp->h_name); 1643a42af91cSHajimu UMEMOTO } 1644a42af91cSHajimu UMEMOTO 1645a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1646a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1647a42af91cSHajimu UMEMOTO } 1648a42af91cSHajimu UMEMOTO 1649a42af91cSHajimu UMEMOTO *res = sentinel.ai_next; 1650a42af91cSHajimu UMEMOTO return 0; 1651a42af91cSHajimu UMEMOTO 1652a42af91cSHajimu UMEMOTO free: 1653a42af91cSHajimu UMEMOTO if (sentinel.ai_next) 1654a42af91cSHajimu UMEMOTO freeaddrinfo(sentinel.ai_next); 1655a42af91cSHajimu UMEMOTO return error; 1656a42af91cSHajimu UMEMOTO } 1657a42af91cSHajimu UMEMOTO #endif 1658a42af91cSHajimu UMEMOTO 1659a42af91cSHajimu UMEMOTO /* resolver logic */ 1660a42af91cSHajimu UMEMOTO 1661a42af91cSHajimu UMEMOTO extern const char *__hostalias __P((const char *)); 1662a42af91cSHajimu UMEMOTO extern int h_errno; 1663a42af91cSHajimu UMEMOTO 1664a42af91cSHajimu UMEMOTO /* 1665a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and await answer. 1666a42af91cSHajimu UMEMOTO * Returned answer is placed in supplied buffer "answer". 1667a42af91cSHajimu UMEMOTO * Perform preliminary check of answer, returning success only 1668a42af91cSHajimu UMEMOTO * if no error is indicated and the answer count is nonzero. 1669a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 1670a42af91cSHajimu UMEMOTO * Error number is left in h_errno. 1671a42af91cSHajimu UMEMOTO * 1672a42af91cSHajimu UMEMOTO * Caller must parse answer and determine whether it answers the question. 1673a42af91cSHajimu UMEMOTO */ 1674a42af91cSHajimu UMEMOTO static int 1675a42af91cSHajimu UMEMOTO res_queryN(name, target) 1676a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 1677a42af91cSHajimu UMEMOTO struct res_target *target; 1678a42af91cSHajimu UMEMOTO { 1679a42af91cSHajimu UMEMOTO u_char buf[MAXPACKET]; 1680a42af91cSHajimu UMEMOTO HEADER *hp; 1681a42af91cSHajimu UMEMOTO int n; 1682a42af91cSHajimu UMEMOTO struct res_target *t; 1683a42af91cSHajimu UMEMOTO int rcode; 1684a42af91cSHajimu UMEMOTO int ancount; 1685a42af91cSHajimu UMEMOTO 1686a42af91cSHajimu UMEMOTO rcode = NOERROR; 1687a42af91cSHajimu UMEMOTO ancount = 0; 1688a42af91cSHajimu UMEMOTO 1689a42af91cSHajimu UMEMOTO if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1690a42af91cSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 1691a42af91cSHajimu UMEMOTO return (-1); 1692a42af91cSHajimu UMEMOTO } 1693a42af91cSHajimu UMEMOTO 1694a42af91cSHajimu UMEMOTO for (t = target; t; t = t->next) { 1695a42af91cSHajimu UMEMOTO int class, type; 1696a42af91cSHajimu UMEMOTO u_char *answer; 1697a42af91cSHajimu UMEMOTO int anslen; 1698a42af91cSHajimu UMEMOTO 1699a42af91cSHajimu UMEMOTO hp = (HEADER *)(void *)t->answer; 1700a42af91cSHajimu UMEMOTO hp->rcode = NOERROR; /* default */ 1701a42af91cSHajimu UMEMOTO 1702a42af91cSHajimu UMEMOTO /* make it easier... */ 1703b826397aSJun-ichiro itojun Hagino class = t->qclass; 1704b826397aSJun-ichiro itojun Hagino type = t->qtype; 1705a42af91cSHajimu UMEMOTO answer = t->answer; 1706a42af91cSHajimu UMEMOTO anslen = t->anslen; 1707a42af91cSHajimu UMEMOTO #ifdef DEBUG 1708a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1709a42af91cSHajimu UMEMOTO printf(";; res_query(%s, %d, %d)\n", name, class, type); 1710a42af91cSHajimu UMEMOTO #endif 1711a42af91cSHajimu UMEMOTO 1712a42af91cSHajimu UMEMOTO n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, 1713a42af91cSHajimu UMEMOTO buf, sizeof(buf)); 1714a42af91cSHajimu UMEMOTO if (n <= 0) { 1715a42af91cSHajimu UMEMOTO #ifdef DEBUG 1716a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1717a42af91cSHajimu UMEMOTO printf(";; res_query: mkquery failed\n"); 1718a42af91cSHajimu UMEMOTO #endif 1719a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1720a42af91cSHajimu UMEMOTO return (n); 1721a42af91cSHajimu UMEMOTO } 1722a42af91cSHajimu UMEMOTO n = res_send(buf, n, answer, anslen); 1723a42af91cSHajimu UMEMOTO #if 0 1724a42af91cSHajimu UMEMOTO if (n < 0) { 1725a42af91cSHajimu UMEMOTO #ifdef DEBUG 1726a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1727a42af91cSHajimu UMEMOTO printf(";; res_query: send error\n"); 1728a42af91cSHajimu UMEMOTO #endif 1729a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 1730a42af91cSHajimu UMEMOTO return (n); 1731a42af91cSHajimu UMEMOTO } 1732a42af91cSHajimu UMEMOTO #endif 1733a42af91cSHajimu UMEMOTO 1734a42af91cSHajimu UMEMOTO if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 1735a42af91cSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */ 1736a42af91cSHajimu UMEMOTO #ifdef DEBUG 1737a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1738a42af91cSHajimu UMEMOTO printf(";; rcode = %d, ancount=%d\n", hp->rcode, 1739a42af91cSHajimu UMEMOTO ntohs(hp->ancount)); 1740a42af91cSHajimu UMEMOTO #endif 1741a42af91cSHajimu UMEMOTO continue; 1742a42af91cSHajimu UMEMOTO } 1743a42af91cSHajimu UMEMOTO 1744a42af91cSHajimu UMEMOTO ancount += ntohs(hp->ancount); 1745a42af91cSHajimu UMEMOTO 1746a42af91cSHajimu UMEMOTO t->n = n; 1747a42af91cSHajimu UMEMOTO } 1748a42af91cSHajimu UMEMOTO 1749a42af91cSHajimu UMEMOTO if (ancount == 0) { 1750a42af91cSHajimu UMEMOTO switch (rcode) { 1751a42af91cSHajimu UMEMOTO case NXDOMAIN: 1752a42af91cSHajimu UMEMOTO h_errno = HOST_NOT_FOUND; 1753a42af91cSHajimu UMEMOTO break; 1754a42af91cSHajimu UMEMOTO case SERVFAIL: 1755a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 1756a42af91cSHajimu UMEMOTO break; 1757a42af91cSHajimu UMEMOTO case NOERROR: 1758a42af91cSHajimu UMEMOTO h_errno = NO_DATA; 1759a42af91cSHajimu UMEMOTO break; 1760a42af91cSHajimu UMEMOTO case FORMERR: 1761a42af91cSHajimu UMEMOTO case NOTIMP: 1762a42af91cSHajimu UMEMOTO case REFUSED: 1763a42af91cSHajimu UMEMOTO default: 1764a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1765a42af91cSHajimu UMEMOTO break; 1766a42af91cSHajimu UMEMOTO } 1767a42af91cSHajimu UMEMOTO return (-1); 1768a42af91cSHajimu UMEMOTO } 1769a42af91cSHajimu UMEMOTO return (ancount); 1770a42af91cSHajimu UMEMOTO } 1771a42af91cSHajimu UMEMOTO 1772a42af91cSHajimu UMEMOTO /* 1773a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and retrieve answer in supplied buffer. 1774a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 1775a42af91cSHajimu UMEMOTO * If enabled, implement search rules until answer or unrecoverable failure 1776a42af91cSHajimu UMEMOTO * is detected. Error code, if any, is left in h_errno. 1777a42af91cSHajimu UMEMOTO */ 1778a42af91cSHajimu UMEMOTO static int 1779a42af91cSHajimu UMEMOTO res_searchN(name, target) 1780a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 1781a42af91cSHajimu UMEMOTO struct res_target *target; 1782a42af91cSHajimu UMEMOTO { 1783a42af91cSHajimu UMEMOTO const char *cp, * const *domain; 1784a42af91cSHajimu UMEMOTO HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ 1785a42af91cSHajimu UMEMOTO u_int dots; 1786a42af91cSHajimu UMEMOTO int trailing_dot, ret, saved_herrno; 1787a42af91cSHajimu UMEMOTO int got_nodata = 0, got_servfail = 0, tried_as_is = 0; 1788a42af91cSHajimu UMEMOTO 1789a42af91cSHajimu UMEMOTO if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1790a42af91cSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 1791a42af91cSHajimu UMEMOTO return (-1); 1792a42af91cSHajimu UMEMOTO } 1793a42af91cSHajimu UMEMOTO 1794a42af91cSHajimu UMEMOTO errno = 0; 1795a42af91cSHajimu UMEMOTO h_errno = HOST_NOT_FOUND; /* default, if we never query */ 1796a42af91cSHajimu UMEMOTO dots = 0; 1797a42af91cSHajimu UMEMOTO for (cp = name; *cp; cp++) 1798a42af91cSHajimu UMEMOTO dots += (*cp == '.'); 1799a42af91cSHajimu UMEMOTO trailing_dot = 0; 1800a42af91cSHajimu UMEMOTO if (cp > name && *--cp == '.') 1801a42af91cSHajimu UMEMOTO trailing_dot++; 1802a42af91cSHajimu UMEMOTO 1803a42af91cSHajimu UMEMOTO /* 1804a42af91cSHajimu UMEMOTO * if there aren't any dots, it could be a user-level alias 1805a42af91cSHajimu UMEMOTO */ 1806a42af91cSHajimu UMEMOTO if (!dots && (cp = __hostalias(name)) != NULL) 1807a42af91cSHajimu UMEMOTO return (res_queryN(cp, target)); 1808a42af91cSHajimu UMEMOTO 1809a42af91cSHajimu UMEMOTO /* 1810a42af91cSHajimu UMEMOTO * If there are dots in the name already, let's just give it a try 1811a42af91cSHajimu UMEMOTO * 'as is'. The threshold can be set with the "ndots" option. 1812a42af91cSHajimu UMEMOTO */ 1813a42af91cSHajimu UMEMOTO saved_herrno = -1; 1814a42af91cSHajimu UMEMOTO if (dots >= _res.ndots) { 1815a42af91cSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target); 1816a42af91cSHajimu UMEMOTO if (ret > 0) 1817a42af91cSHajimu UMEMOTO return (ret); 1818a42af91cSHajimu UMEMOTO saved_herrno = h_errno; 1819a42af91cSHajimu UMEMOTO tried_as_is++; 1820a42af91cSHajimu UMEMOTO } 1821a42af91cSHajimu UMEMOTO 1822a42af91cSHajimu UMEMOTO /* 1823a42af91cSHajimu UMEMOTO * We do at least one level of search if 1824a42af91cSHajimu UMEMOTO * - there is no dot and RES_DEFNAME is set, or 1825a42af91cSHajimu UMEMOTO * - there is at least one dot, there is no trailing dot, 1826a42af91cSHajimu UMEMOTO * and RES_DNSRCH is set. 1827a42af91cSHajimu UMEMOTO */ 1828a42af91cSHajimu UMEMOTO if ((!dots && (_res.options & RES_DEFNAMES)) || 1829a42af91cSHajimu UMEMOTO (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { 1830a42af91cSHajimu UMEMOTO int done = 0; 1831a42af91cSHajimu UMEMOTO 1832a42af91cSHajimu UMEMOTO for (domain = (const char * const *)_res.dnsrch; 1833a42af91cSHajimu UMEMOTO *domain && !done; 1834a42af91cSHajimu UMEMOTO domain++) { 1835a42af91cSHajimu UMEMOTO 1836a42af91cSHajimu UMEMOTO ret = res_querydomainN(name, *domain, target); 1837a42af91cSHajimu UMEMOTO if (ret > 0) 1838a42af91cSHajimu UMEMOTO return (ret); 1839a42af91cSHajimu UMEMOTO 1840a42af91cSHajimu UMEMOTO /* 1841a42af91cSHajimu UMEMOTO * If no server present, give up. 1842a42af91cSHajimu UMEMOTO * If name isn't found in this domain, 1843a42af91cSHajimu UMEMOTO * keep trying higher domains in the search list 1844a42af91cSHajimu UMEMOTO * (if that's enabled). 1845a42af91cSHajimu UMEMOTO * On a NO_DATA error, keep trying, otherwise 1846a42af91cSHajimu UMEMOTO * a wildcard entry of another type could keep us 1847a42af91cSHajimu UMEMOTO * from finding this entry higher in the domain. 1848a42af91cSHajimu UMEMOTO * If we get some other error (negative answer or 1849a42af91cSHajimu UMEMOTO * server failure), then stop searching up, 1850a42af91cSHajimu UMEMOTO * but try the input name below in case it's 1851a42af91cSHajimu UMEMOTO * fully-qualified. 1852a42af91cSHajimu UMEMOTO */ 1853a42af91cSHajimu UMEMOTO if (errno == ECONNREFUSED) { 1854a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 1855a42af91cSHajimu UMEMOTO return (-1); 1856a42af91cSHajimu UMEMOTO } 1857a42af91cSHajimu UMEMOTO 1858a42af91cSHajimu UMEMOTO switch (h_errno) { 1859a42af91cSHajimu UMEMOTO case NO_DATA: 1860a42af91cSHajimu UMEMOTO got_nodata++; 1861a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 1862a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 1863a42af91cSHajimu UMEMOTO /* keep trying */ 1864a42af91cSHajimu UMEMOTO break; 1865a42af91cSHajimu UMEMOTO case TRY_AGAIN: 1866a42af91cSHajimu UMEMOTO if (hp->rcode == SERVFAIL) { 1867a42af91cSHajimu UMEMOTO /* try next search element, if any */ 1868a42af91cSHajimu UMEMOTO got_servfail++; 1869a42af91cSHajimu UMEMOTO break; 1870a42af91cSHajimu UMEMOTO } 1871a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 1872a42af91cSHajimu UMEMOTO default: 1873a42af91cSHajimu UMEMOTO /* anything else implies that we're done */ 1874a42af91cSHajimu UMEMOTO done++; 1875a42af91cSHajimu UMEMOTO } 1876a42af91cSHajimu UMEMOTO /* 1877a42af91cSHajimu UMEMOTO * if we got here for some reason other than DNSRCH, 1878a42af91cSHajimu UMEMOTO * we only wanted one iteration of the loop, so stop. 1879a42af91cSHajimu UMEMOTO */ 1880a42af91cSHajimu UMEMOTO if (!(_res.options & RES_DNSRCH)) 1881a42af91cSHajimu UMEMOTO done++; 1882a42af91cSHajimu UMEMOTO } 1883a42af91cSHajimu UMEMOTO } 1884a42af91cSHajimu UMEMOTO 1885a42af91cSHajimu UMEMOTO /* 1886a42af91cSHajimu UMEMOTO * if we have not already tried the name "as is", do that now. 1887a42af91cSHajimu UMEMOTO * note that we do this regardless of how many dots were in the 1888a42af91cSHajimu UMEMOTO * name or whether it ends with a dot. 1889a42af91cSHajimu UMEMOTO */ 1890a42af91cSHajimu UMEMOTO if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) { 1891a42af91cSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target); 1892a42af91cSHajimu UMEMOTO if (ret > 0) 1893a42af91cSHajimu UMEMOTO return (ret); 1894a42af91cSHajimu UMEMOTO } 1895a42af91cSHajimu UMEMOTO 1896a42af91cSHajimu UMEMOTO /* 1897a42af91cSHajimu UMEMOTO * if we got here, we didn't satisfy the search. 1898a42af91cSHajimu UMEMOTO * if we did an initial full query, return that query's h_errno 1899a42af91cSHajimu UMEMOTO * (note that we wouldn't be here if that query had succeeded). 1900a42af91cSHajimu UMEMOTO * else if we ever got a nodata, send that back as the reason. 1901a42af91cSHajimu UMEMOTO * else send back meaningless h_errno, that being the one from 1902a42af91cSHajimu UMEMOTO * the last DNSRCH we did. 1903a42af91cSHajimu UMEMOTO */ 1904a42af91cSHajimu UMEMOTO if (saved_herrno != -1) 1905a42af91cSHajimu UMEMOTO h_errno = saved_herrno; 1906a42af91cSHajimu UMEMOTO else if (got_nodata) 1907a42af91cSHajimu UMEMOTO h_errno = NO_DATA; 1908a42af91cSHajimu UMEMOTO else if (got_servfail) 1909a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 1910a42af91cSHajimu UMEMOTO return (-1); 1911a42af91cSHajimu UMEMOTO } 1912a42af91cSHajimu UMEMOTO 1913a42af91cSHajimu UMEMOTO /* 1914a42af91cSHajimu UMEMOTO * Perform a call on res_query on the concatenation of name and domain, 1915a42af91cSHajimu UMEMOTO * removing a trailing dot from name if domain is NULL. 1916a42af91cSHajimu UMEMOTO */ 1917a42af91cSHajimu UMEMOTO static int 1918a42af91cSHajimu UMEMOTO res_querydomainN(name, domain, target) 1919a42af91cSHajimu UMEMOTO const char *name, *domain; 1920a42af91cSHajimu UMEMOTO struct res_target *target; 1921a42af91cSHajimu UMEMOTO { 1922a42af91cSHajimu UMEMOTO char nbuf[MAXDNAME]; 1923a42af91cSHajimu UMEMOTO const char *longname = nbuf; 1924a42af91cSHajimu UMEMOTO size_t n, d; 1925a42af91cSHajimu UMEMOTO 1926a42af91cSHajimu UMEMOTO if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1927a42af91cSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 1928a42af91cSHajimu UMEMOTO return (-1); 1929a42af91cSHajimu UMEMOTO } 1930a42af91cSHajimu UMEMOTO #ifdef DEBUG 1931a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1932a42af91cSHajimu UMEMOTO printf(";; res_querydomain(%s, %s)\n", 1933a42af91cSHajimu UMEMOTO name, domain?domain:"<Nil>"); 1934a42af91cSHajimu UMEMOTO #endif 1935a42af91cSHajimu UMEMOTO if (domain == NULL) { 1936a42af91cSHajimu UMEMOTO /* 1937a42af91cSHajimu UMEMOTO * Check for trailing '.'; 1938a42af91cSHajimu UMEMOTO * copy without '.' if present. 1939a42af91cSHajimu UMEMOTO */ 1940a42af91cSHajimu UMEMOTO n = strlen(name); 1941a42af91cSHajimu UMEMOTO if (n >= MAXDNAME) { 1942a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1943a42af91cSHajimu UMEMOTO return (-1); 1944a42af91cSHajimu UMEMOTO } 1945a42af91cSHajimu UMEMOTO if (n > 0 && name[--n] == '.') { 1946a42af91cSHajimu UMEMOTO strncpy(nbuf, name, n); 1947a42af91cSHajimu UMEMOTO nbuf[n] = '\0'; 1948a42af91cSHajimu UMEMOTO } else 1949a42af91cSHajimu UMEMOTO longname = name; 1950a42af91cSHajimu UMEMOTO } else { 1951a42af91cSHajimu UMEMOTO n = strlen(name); 1952a42af91cSHajimu UMEMOTO d = strlen(domain); 1953a42af91cSHajimu UMEMOTO if (n + d + 1 >= MAXDNAME) { 1954a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1955a42af91cSHajimu UMEMOTO return (-1); 1956a42af91cSHajimu UMEMOTO } 1957a42af91cSHajimu UMEMOTO sprintf(nbuf, "%s.%s", name, domain); 1958a42af91cSHajimu UMEMOTO } 1959a42af91cSHajimu UMEMOTO return (res_queryN(longname, target)); 1960a42af91cSHajimu UMEMOTO } 1961