1ec20fe00SJun-ichiro itojun Hagino /* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */ 2b826397aSJun-ichiro itojun Hagino 37d56d374SYoshinobu Inoue /* 47d56d374SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 57d56d374SYoshinobu Inoue * All rights reserved. 67d56d374SYoshinobu Inoue * 77d56d374SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 87d56d374SYoshinobu Inoue * modification, are permitted provided that the following conditions 97d56d374SYoshinobu Inoue * are met: 107d56d374SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 117d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 127d56d374SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 137d56d374SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 147d56d374SYoshinobu Inoue * documentation and/or other materials provided with the distribution. 157d56d374SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 167d56d374SYoshinobu Inoue * may be used to endorse or promote products derived from this software 177d56d374SYoshinobu Inoue * without specific prior written permission. 187d56d374SYoshinobu Inoue * 197d56d374SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 207d56d374SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 217d56d374SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 227d56d374SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 237d56d374SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 247d56d374SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 257d56d374SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 267d56d374SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 277d56d374SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 287d56d374SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 297d56d374SYoshinobu Inoue * SUCH DAMAGE. 307d56d374SYoshinobu Inoue */ 317d56d374SYoshinobu Inoue 327d56d374SYoshinobu Inoue /* 337d56d374SYoshinobu Inoue * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. 347d56d374SYoshinobu Inoue * 357d56d374SYoshinobu Inoue * Issues to be discussed: 367d56d374SYoshinobu Inoue * - Thread safe-ness must be checked. 377d56d374SYoshinobu Inoue * - Return values. There are nonstandard return values defined and used 387d56d374SYoshinobu Inoue * in the source code. This is because RFC2553 is silent about which error 397d56d374SYoshinobu Inoue * code must be returned for which situation. 40a42af91cSHajimu UMEMOTO * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is 41dd521ef1SHajimu UMEMOTO * invalid. current code - SEGV on freeaddrinfo(NULL) 42dd521ef1SHajimu UMEMOTO * 4300a8a579SYoshinobu Inoue * Note: 4400a8a579SYoshinobu Inoue * - The code filters out AFs that are not supported by the kernel, 4500a8a579SYoshinobu Inoue * when globbing NULL hostname (to loopback, or wildcard). Is it the right 4600a8a579SYoshinobu Inoue * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 4700a8a579SYoshinobu Inoue * in ai_flags? 48a42af91cSHajimu UMEMOTO * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. 49a42af91cSHajimu UMEMOTO * (1) what should we do against numeric hostname (2) what should we do 50a42af91cSHajimu UMEMOTO * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? 51a42af91cSHajimu UMEMOTO * non-loopback address configured? global address configured? 52dd521ef1SHajimu UMEMOTO * 53dd521ef1SHajimu UMEMOTO * OS specific notes for netbsd/openbsd/freebsd4/bsdi4: 54a42af91cSHajimu UMEMOTO * - To avoid search order issue, we have a big amount of code duplicate 55a42af91cSHajimu UMEMOTO * from gethnamaddr.c and some other places. The issues that there's no 56a42af91cSHajimu UMEMOTO * lower layer function to lookup "IPv4 or IPv6" record. Calling 57a42af91cSHajimu UMEMOTO * gethostbyname2 from getaddrinfo will end up in wrong search order, as 58dd521ef1SHajimu UMEMOTO * presented above. 59dd521ef1SHajimu UMEMOTO * 60dd521ef1SHajimu UMEMOTO * OS specific notes for freebsd4: 61dd521ef1SHajimu UMEMOTO * - FreeBSD supported $GAI. The code does not. 62dd521ef1SHajimu UMEMOTO * - FreeBSD allowed classful IPv4 numeric (127.1), the code does not. 637d56d374SYoshinobu Inoue */ 647d56d374SYoshinobu Inoue 65333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 66333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 67333fc21eSDavid E. O'Brien 68d201fe46SDaniel Eischen #include "namespace.h" 697d56d374SYoshinobu Inoue #include <sys/types.h> 707d56d374SYoshinobu Inoue #include <sys/param.h> 717d56d374SYoshinobu Inoue #include <sys/socket.h> 727d56d374SYoshinobu Inoue #include <net/if.h> 737d56d374SYoshinobu Inoue #include <netinet/in.h> 747d56d374SYoshinobu Inoue #include <arpa/inet.h> 757d56d374SYoshinobu Inoue #include <arpa/nameser.h> 768d630135SAlfred Perlstein #include <rpc/rpc.h> 778d630135SAlfred Perlstein #include <rpcsvc/yp_prot.h> 788d630135SAlfred Perlstein #include <rpcsvc/ypclnt.h> 797d56d374SYoshinobu Inoue #include <netdb.h> 80e8baaa70SDaniel Eischen #include <pthread.h> 817d56d374SYoshinobu Inoue #include <resolv.h> 827d56d374SYoshinobu Inoue #include <string.h> 837d56d374SYoshinobu Inoue #include <stdlib.h> 847d56d374SYoshinobu Inoue #include <stddef.h> 857d56d374SYoshinobu Inoue #include <ctype.h> 867d56d374SYoshinobu Inoue #include <unistd.h> 877d56d374SYoshinobu Inoue #include <stdio.h> 88a42af91cSHajimu UMEMOTO #include <errno.h> 8969b0a4b6SJim Pirzyk 9069b0a4b6SJim Pirzyk #include "res_config.h" 9169b0a4b6SJim Pirzyk 92ce6282dbSHajimu UMEMOTO #ifdef DEBUG 93ce6282dbSHajimu UMEMOTO #include <syslog.h> 94ce6282dbSHajimu UMEMOTO #endif 957d56d374SYoshinobu Inoue 96248aee62SJacques Vidrine #include <stdarg.h> 97248aee62SJacques Vidrine #include <nsswitch.h> 98d201fe46SDaniel Eischen #include "un-namespace.h" 992bbd7cf8SJacques Vidrine #include "libc_private.h" 100248aee62SJacques Vidrine 1017d56d374SYoshinobu Inoue #if defined(__KAME__) && defined(INET6) 1027d56d374SYoshinobu Inoue # define FAITH 1037d56d374SYoshinobu Inoue #endif 1047d56d374SYoshinobu Inoue 1057d56d374SYoshinobu Inoue #define SUCCESS 0 1067d56d374SYoshinobu Inoue #define ANY 0 1077d56d374SYoshinobu Inoue #define YES 1 1087d56d374SYoshinobu Inoue #define NO 0 1097d56d374SYoshinobu Inoue 1107d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 }; 111dd521ef1SHajimu UMEMOTO static const char in_loopback[] = { 127, 0, 0, 1 }; 112dd521ef1SHajimu UMEMOTO #ifdef INET6 1137d56d374SYoshinobu Inoue static const char in6_addrany[] = { 1147d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 1157d56d374SYoshinobu Inoue }; 1167d56d374SYoshinobu Inoue static const char in6_loopback[] = { 1177d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 1187d56d374SYoshinobu Inoue }; 119dd521ef1SHajimu UMEMOTO #endif 1207d56d374SYoshinobu Inoue 1217d56d374SYoshinobu Inoue static const struct afd { 1227d56d374SYoshinobu Inoue int a_af; 1237d56d374SYoshinobu Inoue int a_addrlen; 1247d56d374SYoshinobu Inoue int a_socklen; 1257d56d374SYoshinobu Inoue int a_off; 1267d56d374SYoshinobu Inoue const char *a_addrany; 1277d56d374SYoshinobu Inoue const char *a_loopback; 1287d56d374SYoshinobu Inoue int a_scoped; 1297d56d374SYoshinobu Inoue } afdl [] = { 1307d56d374SYoshinobu Inoue #ifdef INET6 1317d56d374SYoshinobu Inoue #define N_INET6 0 1327d56d374SYoshinobu Inoue {PF_INET6, sizeof(struct in6_addr), 1337d56d374SYoshinobu Inoue sizeof(struct sockaddr_in6), 1347d56d374SYoshinobu Inoue offsetof(struct sockaddr_in6, sin6_addr), 1357d56d374SYoshinobu Inoue in6_addrany, in6_loopback, 1}, 1367d56d374SYoshinobu Inoue #define N_INET 1 1377d56d374SYoshinobu Inoue #else 1387d56d374SYoshinobu Inoue #define N_INET 0 1397d56d374SYoshinobu Inoue #endif 1407d56d374SYoshinobu Inoue {PF_INET, sizeof(struct in_addr), 1417d56d374SYoshinobu Inoue sizeof(struct sockaddr_in), 1427d56d374SYoshinobu Inoue offsetof(struct sockaddr_in, sin_addr), 1437d56d374SYoshinobu Inoue in_addrany, in_loopback, 0}, 1447d56d374SYoshinobu Inoue {0, 0, 0, 0, NULL, NULL, 0}, 1457d56d374SYoshinobu Inoue }; 1467d56d374SYoshinobu Inoue 1477d56d374SYoshinobu Inoue struct explore { 148a42af91cSHajimu UMEMOTO int e_af; 1497d56d374SYoshinobu Inoue int e_socktype; 1507d56d374SYoshinobu Inoue int e_protocol; 1517d56d374SYoshinobu Inoue const char *e_protostr; 1527d56d374SYoshinobu Inoue int e_wild; 1537d56d374SYoshinobu Inoue #define WILD_AF(ex) ((ex)->e_wild & 0x01) 1547d56d374SYoshinobu Inoue #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 1557d56d374SYoshinobu Inoue #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 1567d56d374SYoshinobu Inoue }; 1577d56d374SYoshinobu Inoue 1587d56d374SYoshinobu Inoue static const struct explore explore[] = { 159a42af91cSHajimu UMEMOTO #if 0 160a42af91cSHajimu UMEMOTO { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, 161a42af91cSHajimu UMEMOTO #endif 162a42af91cSHajimu UMEMOTO #ifdef INET6 163a42af91cSHajimu UMEMOTO { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 164a42af91cSHajimu UMEMOTO { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 165a42af91cSHajimu UMEMOTO { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, 166a42af91cSHajimu UMEMOTO #endif 167a42af91cSHajimu UMEMOTO { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 168a42af91cSHajimu UMEMOTO { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 169a42af91cSHajimu UMEMOTO { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, 170a42af91cSHajimu UMEMOTO { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 171a42af91cSHajimu UMEMOTO { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 172a42af91cSHajimu UMEMOTO { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, 173a42af91cSHajimu UMEMOTO { -1, 0, 0, NULL, 0 }, 1747d56d374SYoshinobu Inoue }; 1757d56d374SYoshinobu Inoue 1767d56d374SYoshinobu Inoue #ifdef INET6 1777d56d374SYoshinobu Inoue #define PTON_MAX 16 1787d56d374SYoshinobu Inoue #else 1797d56d374SYoshinobu Inoue #define PTON_MAX 4 1807d56d374SYoshinobu Inoue #endif 1817d56d374SYoshinobu Inoue 182248aee62SJacques Vidrine static const ns_src default_dns_files[] = { 183248aee62SJacques Vidrine { NSSRC_FILES, NS_SUCCESS }, 184248aee62SJacques Vidrine { NSSRC_DNS, NS_SUCCESS }, 185248aee62SJacques Vidrine { 0 } 186248aee62SJacques Vidrine }; 187248aee62SJacques Vidrine 1885cd588cbSHajimu UMEMOTO #define MAXPACKET (64*1024) 189a42af91cSHajimu UMEMOTO 190a42af91cSHajimu UMEMOTO typedef union { 191a42af91cSHajimu UMEMOTO HEADER hdr; 192a42af91cSHajimu UMEMOTO u_char buf[MAXPACKET]; 193a42af91cSHajimu UMEMOTO } querybuf; 194a42af91cSHajimu UMEMOTO 195a42af91cSHajimu UMEMOTO struct res_target { 196a42af91cSHajimu UMEMOTO struct res_target *next; 197a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 198b826397aSJun-ichiro itojun Hagino int qclass, qtype; /* class and type of query */ 199a42af91cSHajimu UMEMOTO u_char *answer; /* buffer to put answer */ 200a42af91cSHajimu UMEMOTO int anslen; /* size of answer buffer */ 201a42af91cSHajimu UMEMOTO int n; /* result length */ 202a42af91cSHajimu UMEMOTO }; 203a42af91cSHajimu UMEMOTO 204c05ac53bSDavid E. O'Brien static int str_isnumber(const char *); 2051372519bSDavid E. O'Brien static int explore_fqdn(const struct addrinfo *, const char *, 2061372519bSDavid E. O'Brien const char *, struct addrinfo **); 2071372519bSDavid E. O'Brien static int explore_null(const struct addrinfo *, 2081372519bSDavid E. O'Brien const char *, struct addrinfo **); 2091372519bSDavid E. O'Brien static int explore_numeric(const struct addrinfo *, const char *, 2101372519bSDavid E. O'Brien const char *, struct addrinfo **); 2111372519bSDavid E. O'Brien static int explore_numeric_scope(const struct addrinfo *, const char *, 2121372519bSDavid E. O'Brien const char *, struct addrinfo **); 2131372519bSDavid E. O'Brien static int get_canonname(const struct addrinfo *, 2141372519bSDavid E. O'Brien struct addrinfo *, const char *); 2151372519bSDavid E. O'Brien static struct addrinfo *get_ai(const struct addrinfo *, 2161372519bSDavid E. O'Brien const struct afd *, const char *); 217c05ac53bSDavid E. O'Brien static int get_portmatch(const struct addrinfo *, const char *); 218c05ac53bSDavid E. O'Brien static int get_port(struct addrinfo *, const char *, int); 219c05ac53bSDavid E. O'Brien static const struct afd *find_afd(int); 220c05ac53bSDavid E. O'Brien static int addrconfig(struct addrinfo *); 221a42af91cSHajimu UMEMOTO #ifdef INET6 222a89c1d30SHajimu UMEMOTO static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); 223a42af91cSHajimu UMEMOTO #endif 224a42af91cSHajimu UMEMOTO 2251372519bSDavid E. O'Brien static struct addrinfo *getanswer(const querybuf *, int, const char *, int, 2261372519bSDavid E. O'Brien const struct addrinfo *); 227a89c1d30SHajimu UMEMOTO static int _dns_getaddrinfo(void *, void *, va_list); 228c05ac53bSDavid E. O'Brien static void _sethtent(void); 229c05ac53bSDavid E. O'Brien static void _endhtent(void); 230c05ac53bSDavid E. O'Brien static struct addrinfo *_gethtent(const char *, const struct addrinfo *); 231c05ac53bSDavid E. O'Brien static int _files_getaddrinfo(void *, void *, va_list); 232a42af91cSHajimu UMEMOTO #ifdef YP 233c05ac53bSDavid E. O'Brien static struct addrinfo *_yphostent(char *, const struct addrinfo *); 234c05ac53bSDavid E. O'Brien static int _yp_getaddrinfo(void *, void *, va_list); 235a42af91cSHajimu UMEMOTO #endif 236a42af91cSHajimu UMEMOTO 237c05ac53bSDavid E. O'Brien static int res_queryN(const char *, struct res_target *); 238c05ac53bSDavid E. O'Brien static int res_searchN(const char *, struct res_target *); 2391372519bSDavid E. O'Brien static int res_querydomainN(const char *, const char *, 2401372519bSDavid E. O'Brien struct res_target *); 241a42af91cSHajimu UMEMOTO 242b29ec00bSHajimu UMEMOTO static struct ai_errlist { 243b29ec00bSHajimu UMEMOTO const char *str; 244b29ec00bSHajimu UMEMOTO int code; 245b29ec00bSHajimu UMEMOTO } ai_errlist[] = { 246b29ec00bSHajimu UMEMOTO { "Success", 0, }, 247b29ec00bSHajimu UMEMOTO #ifdef EAI_ADDRFAMILY 248b29ec00bSHajimu UMEMOTO { "Address family for hostname not supported", EAI_ADDRFAMILY, }, 249b29ec00bSHajimu UMEMOTO #endif 250b29ec00bSHajimu UMEMOTO { "Temporary failure in name resolution", EAI_AGAIN, }, 251b29ec00bSHajimu UMEMOTO { "Invalid value for ai_flags", EAI_BADFLAGS, }, 252b29ec00bSHajimu UMEMOTO { "Non-recoverable failure in name resolution", EAI_FAIL, }, 253b29ec00bSHajimu UMEMOTO { "ai_family not supported", EAI_FAMILY, }, 254b29ec00bSHajimu UMEMOTO { "Memory allocation failure", EAI_MEMORY, }, 255b29ec00bSHajimu UMEMOTO #ifdef EAI_NODATA 256b29ec00bSHajimu UMEMOTO { "No address associated with hostname", EAI_NODATA, }, 257b29ec00bSHajimu UMEMOTO #endif 258b29ec00bSHajimu UMEMOTO { "hostname nor servname provided, or not known", EAI_NONAME, }, 259b29ec00bSHajimu UMEMOTO { "servname not supported for ai_socktype", EAI_SERVICE, }, 260b29ec00bSHajimu UMEMOTO { "ai_socktype not supported", EAI_SOCKTYPE, }, 261b29ec00bSHajimu UMEMOTO { "System error returned in errno", EAI_SYSTEM, }, 262b29ec00bSHajimu UMEMOTO { "Invalid value for hints", EAI_BADHINTS, }, 263b29ec00bSHajimu UMEMOTO { "Resolved protocol is unknown", EAI_PROTOCOL, }, 264b29ec00bSHajimu UMEMOTO /* backward compatibility with userland code prior to 2553bis-02 */ 265b29ec00bSHajimu UMEMOTO { "Address family for hostname not supported", 1, }, 266b29ec00bSHajimu UMEMOTO { "No address associated with hostname", 7, }, 267b29ec00bSHajimu UMEMOTO { NULL, -1, }, 2687d56d374SYoshinobu Inoue }; 2697d56d374SYoshinobu Inoue 27071918af6SHajimu UMEMOTO /* 27171918af6SHajimu UMEMOTO * XXX: Our res_*() is not thread-safe. So, we share lock between 27271918af6SHajimu UMEMOTO * getaddrinfo() and getipnodeby*(). Still, we cannot use 27371918af6SHajimu UMEMOTO * getaddrinfo() and getipnodeby*() in conjunction with other 27471918af6SHajimu UMEMOTO * functions which call res_*(). 27571918af6SHajimu UMEMOTO */ 276e8baaa70SDaniel Eischen pthread_mutex_t __getaddrinfo_thread_lock = PTHREAD_MUTEX_INITIALIZER; 27771918af6SHajimu UMEMOTO #define THREAD_LOCK() \ 278e8baaa70SDaniel Eischen if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock); 27971918af6SHajimu UMEMOTO #define THREAD_UNLOCK() \ 280e8baaa70SDaniel Eischen if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock); 28171918af6SHajimu UMEMOTO 2827d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */ 2837d56d374SYoshinobu Inoue 2847d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \ 2857d56d374SYoshinobu Inoue do { \ 2867d56d374SYoshinobu Inoue /* external reference: pai, error, and label free */ \ 2877d56d374SYoshinobu Inoue (ai) = get_ai(pai, (afd), (addr)); \ 2887d56d374SYoshinobu Inoue if ((ai) == NULL) { \ 2897d56d374SYoshinobu Inoue error = EAI_MEMORY; \ 2907d56d374SYoshinobu Inoue goto free; \ 2917d56d374SYoshinobu Inoue } \ 292a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 2937d56d374SYoshinobu Inoue 2947d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \ 2957d56d374SYoshinobu Inoue do { \ 2967d56d374SYoshinobu Inoue /* external reference: error and label free */ \ 2977d56d374SYoshinobu Inoue error = get_port((ai), (serv), 0); \ 2987d56d374SYoshinobu Inoue if (error != 0) \ 2997d56d374SYoshinobu Inoue goto free; \ 300a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3017d56d374SYoshinobu Inoue 3027d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \ 3037d56d374SYoshinobu Inoue do { \ 3047d56d374SYoshinobu Inoue /* external reference: pai, error and label free */ \ 3057d56d374SYoshinobu Inoue error = get_canonname(pai, (ai), (str)); \ 3067d56d374SYoshinobu Inoue if (error != 0) \ 3077d56d374SYoshinobu Inoue goto free; \ 308a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3097d56d374SYoshinobu Inoue 3107d56d374SYoshinobu Inoue #define ERR(err) \ 3117d56d374SYoshinobu Inoue do { \ 3127d56d374SYoshinobu Inoue /* external reference: error, and label bad */ \ 3137d56d374SYoshinobu Inoue error = (err); \ 3147d56d374SYoshinobu Inoue goto bad; \ 315a42af91cSHajimu UMEMOTO /*NOTREACHED*/ \ 316a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3177d56d374SYoshinobu Inoue 3187d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \ 319a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 3207d56d374SYoshinobu Inoue #define MATCH(x, y, w) \ 321a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 3227d56d374SYoshinobu Inoue 3237d56d374SYoshinobu Inoue char * 3247d56d374SYoshinobu Inoue gai_strerror(ecode) 3257d56d374SYoshinobu Inoue int ecode; 3267d56d374SYoshinobu Inoue { 327b29ec00bSHajimu UMEMOTO struct ai_errlist *p; 328b29ec00bSHajimu UMEMOTO 329b29ec00bSHajimu UMEMOTO for (p = ai_errlist; p->str; p++) { 330b29ec00bSHajimu UMEMOTO if (p->code == ecode) 331b29ec00bSHajimu UMEMOTO return (char *)p->str; 332b29ec00bSHajimu UMEMOTO } 3337d56d374SYoshinobu Inoue } 3347d56d374SYoshinobu Inoue 3357d56d374SYoshinobu Inoue void 3367d56d374SYoshinobu Inoue freeaddrinfo(ai) 3377d56d374SYoshinobu Inoue struct addrinfo *ai; 3387d56d374SYoshinobu Inoue { 3397d56d374SYoshinobu Inoue struct addrinfo *next; 3407d56d374SYoshinobu Inoue 3417d56d374SYoshinobu Inoue do { 3427d56d374SYoshinobu Inoue next = ai->ai_next; 3437d56d374SYoshinobu Inoue if (ai->ai_canonname) 3447d56d374SYoshinobu Inoue free(ai->ai_canonname); 3457d56d374SYoshinobu Inoue /* no need to free(ai->ai_addr) */ 3467d56d374SYoshinobu Inoue free(ai); 347a42af91cSHajimu UMEMOTO ai = next; 348a42af91cSHajimu UMEMOTO } while (ai); 3497d56d374SYoshinobu Inoue } 3507d56d374SYoshinobu Inoue 3517d56d374SYoshinobu Inoue static int 3527d56d374SYoshinobu Inoue str_isnumber(p) 3537d56d374SYoshinobu Inoue const char *p; 3547d56d374SYoshinobu Inoue { 355ec20fe00SJun-ichiro itojun Hagino char *ep; 356ec20fe00SJun-ichiro itojun Hagino 357ec20fe00SJun-ichiro itojun Hagino if (*p == '\0') 3587d56d374SYoshinobu Inoue return NO; 359ec20fe00SJun-ichiro itojun Hagino ep = NULL; 360a89c1d30SHajimu UMEMOTO errno = 0; 361ec20fe00SJun-ichiro itojun Hagino (void)strtoul(p, &ep, 10); 362a89c1d30SHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0') 3637d56d374SYoshinobu Inoue return YES; 364ec20fe00SJun-ichiro itojun Hagino else 365ec20fe00SJun-ichiro itojun Hagino return NO; 3667d56d374SYoshinobu Inoue } 3677d56d374SYoshinobu Inoue 3687d56d374SYoshinobu Inoue int 3697d56d374SYoshinobu Inoue getaddrinfo(hostname, servname, hints, res) 3707d56d374SYoshinobu Inoue const char *hostname, *servname; 3717d56d374SYoshinobu Inoue const struct addrinfo *hints; 3727d56d374SYoshinobu Inoue struct addrinfo **res; 3737d56d374SYoshinobu Inoue { 3747d56d374SYoshinobu Inoue struct addrinfo sentinel; 3757d56d374SYoshinobu Inoue struct addrinfo *cur; 3767d56d374SYoshinobu Inoue int error = 0; 3777d56d374SYoshinobu Inoue struct addrinfo ai; 3787d56d374SYoshinobu Inoue struct addrinfo ai0; 3797d56d374SYoshinobu Inoue struct addrinfo *pai; 3807d56d374SYoshinobu Inoue const struct explore *ex; 3817d56d374SYoshinobu Inoue 382a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 3837d56d374SYoshinobu Inoue cur = &sentinel; 3847d56d374SYoshinobu Inoue pai = &ai; 3857d56d374SYoshinobu Inoue pai->ai_flags = 0; 3867d56d374SYoshinobu Inoue pai->ai_family = PF_UNSPEC; 3877d56d374SYoshinobu Inoue pai->ai_socktype = ANY; 3887d56d374SYoshinobu Inoue pai->ai_protocol = ANY; 3897d56d374SYoshinobu Inoue pai->ai_addrlen = 0; 3907d56d374SYoshinobu Inoue pai->ai_canonname = NULL; 3917d56d374SYoshinobu Inoue pai->ai_addr = NULL; 3927d56d374SYoshinobu Inoue pai->ai_next = NULL; 3937d56d374SYoshinobu Inoue 3947d56d374SYoshinobu Inoue if (hostname == NULL && servname == NULL) 3957d56d374SYoshinobu Inoue return EAI_NONAME; 3967d56d374SYoshinobu Inoue if (hints) { 3977d56d374SYoshinobu Inoue /* error check for hints */ 3987d56d374SYoshinobu Inoue if (hints->ai_addrlen || hints->ai_canonname || 3997d56d374SYoshinobu Inoue hints->ai_addr || hints->ai_next) 4007d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); /* xxx */ 4017d56d374SYoshinobu Inoue if (hints->ai_flags & ~AI_MASK) 4027d56d374SYoshinobu Inoue ERR(EAI_BADFLAGS); 4037d56d374SYoshinobu Inoue switch (hints->ai_family) { 4047d56d374SYoshinobu Inoue case PF_UNSPEC: 4057d56d374SYoshinobu Inoue case PF_INET: 4067d56d374SYoshinobu Inoue #ifdef INET6 4077d56d374SYoshinobu Inoue case PF_INET6: 4087d56d374SYoshinobu Inoue #endif 4097d56d374SYoshinobu Inoue break; 4107d56d374SYoshinobu Inoue default: 4117d56d374SYoshinobu Inoue ERR(EAI_FAMILY); 4127d56d374SYoshinobu Inoue } 4137d56d374SYoshinobu Inoue memcpy(pai, hints, sizeof(*pai)); 4147d56d374SYoshinobu Inoue 4157d56d374SYoshinobu Inoue /* 4167d56d374SYoshinobu Inoue * if both socktype/protocol are specified, check if they 4177d56d374SYoshinobu Inoue * are meaningful combination. 4187d56d374SYoshinobu Inoue */ 4197d56d374SYoshinobu Inoue if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 420a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 421a42af91cSHajimu UMEMOTO if (pai->ai_family != ex->e_af) 422a42af91cSHajimu UMEMOTO continue; 4237d56d374SYoshinobu Inoue if (ex->e_socktype == ANY) 4247d56d374SYoshinobu Inoue continue; 4257d56d374SYoshinobu Inoue if (ex->e_protocol == ANY) 4267d56d374SYoshinobu Inoue continue; 427dd521ef1SHajimu UMEMOTO if (pai->ai_socktype == ex->e_socktype && 428dd521ef1SHajimu UMEMOTO pai->ai_protocol != ex->e_protocol) { 4297d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); 4307d56d374SYoshinobu Inoue } 4317d56d374SYoshinobu Inoue } 4327d56d374SYoshinobu Inoue } 433a42af91cSHajimu UMEMOTO } 4347d56d374SYoshinobu Inoue 4357d56d374SYoshinobu Inoue /* 436a42af91cSHajimu UMEMOTO * post-2553: AI_ALL and AI_V4MAPPED are effective only against 437dd521ef1SHajimu UMEMOTO * AF_INET6 query. They need to be ignored if specified in other 438a42af91cSHajimu UMEMOTO * occassions. 439a42af91cSHajimu UMEMOTO */ 440a42af91cSHajimu UMEMOTO switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) { 441a42af91cSHajimu UMEMOTO case AI_V4MAPPED: 442a42af91cSHajimu UMEMOTO case AI_ALL | AI_V4MAPPED: 443a42af91cSHajimu UMEMOTO if (pai->ai_family != AF_INET6) 444a42af91cSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 445a42af91cSHajimu UMEMOTO break; 446a42af91cSHajimu UMEMOTO case AI_ALL: 447a42af91cSHajimu UMEMOTO #if 1 448a42af91cSHajimu UMEMOTO /* illegal */ 449a42af91cSHajimu UMEMOTO ERR(EAI_BADFLAGS); 450a42af91cSHajimu UMEMOTO #else 451a42af91cSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 452a42af91cSHajimu UMEMOTO #endif 453a42af91cSHajimu UMEMOTO break; 454a42af91cSHajimu UMEMOTO } 455a42af91cSHajimu UMEMOTO 456a42af91cSHajimu UMEMOTO /* 457a42af91cSHajimu UMEMOTO * check for special cases. (1) numeric servname is disallowed if 458a42af91cSHajimu UMEMOTO * socktype/protocol are left unspecified. (2) servname is disallowed 459a42af91cSHajimu UMEMOTO * for raw and other inet{,6} sockets. 4607d56d374SYoshinobu Inoue */ 4617d56d374SYoshinobu Inoue if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 462a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4637d56d374SYoshinobu Inoue || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 4647d56d374SYoshinobu Inoue #endif 4657d56d374SYoshinobu Inoue ) { 466a42af91cSHajimu UMEMOTO ai0 = *pai; /* backup *pai */ 4677d56d374SYoshinobu Inoue 468a42af91cSHajimu UMEMOTO if (pai->ai_family == PF_UNSPEC) { 469a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4707d56d374SYoshinobu Inoue pai->ai_family = PF_INET6; 4717d56d374SYoshinobu Inoue #else 4727d56d374SYoshinobu Inoue pai->ai_family = PF_INET; 4737d56d374SYoshinobu Inoue #endif 474a42af91cSHajimu UMEMOTO } 4757d56d374SYoshinobu Inoue error = get_portmatch(pai, servname); 4767d56d374SYoshinobu Inoue if (error) 4777d56d374SYoshinobu Inoue ERR(error); 478a42af91cSHajimu UMEMOTO 479a42af91cSHajimu UMEMOTO *pai = ai0; 4807d56d374SYoshinobu Inoue } 4817d56d374SYoshinobu Inoue 482a42af91cSHajimu UMEMOTO ai0 = *pai; 483a42af91cSHajimu UMEMOTO 4847d56d374SYoshinobu Inoue /* NULL hostname, or numeric hostname */ 485a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 4867d56d374SYoshinobu Inoue *pai = ai0; 4877d56d374SYoshinobu Inoue 488a42af91cSHajimu UMEMOTO /* PF_UNSPEC entries are prepared for DNS queries only */ 489a42af91cSHajimu UMEMOTO if (ex->e_af == PF_UNSPEC) 4907d56d374SYoshinobu Inoue continue; 491a42af91cSHajimu UMEMOTO 492a42af91cSHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 4937d56d374SYoshinobu Inoue continue; 494248aee62SJacques Vidrine if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) 495a42af91cSHajimu UMEMOTO continue; 496248aee62SJacques Vidrine if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) 4977d56d374SYoshinobu Inoue continue; 4987d56d374SYoshinobu Inoue 4997d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 500a42af91cSHajimu UMEMOTO pai->ai_family = ex->e_af; 5017d56d374SYoshinobu Inoue if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 5027d56d374SYoshinobu Inoue pai->ai_socktype = ex->e_socktype; 5037d56d374SYoshinobu Inoue if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 5047d56d374SYoshinobu Inoue pai->ai_protocol = ex->e_protocol; 5057d56d374SYoshinobu Inoue 5067d56d374SYoshinobu Inoue if (hostname == NULL) 507a42af91cSHajimu UMEMOTO error = explore_null(pai, servname, &cur->ai_next); 5087d56d374SYoshinobu Inoue else 509248aee62SJacques Vidrine error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next); 5107d56d374SYoshinobu Inoue 5117d56d374SYoshinobu Inoue if (error) 5127d56d374SYoshinobu Inoue goto free; 5137d56d374SYoshinobu Inoue 5147d56d374SYoshinobu Inoue while (cur && cur->ai_next) 5157d56d374SYoshinobu Inoue cur = cur->ai_next; 5167d56d374SYoshinobu Inoue } 5177d56d374SYoshinobu Inoue 5187d56d374SYoshinobu Inoue /* 5197d56d374SYoshinobu Inoue * XXX 5207d56d374SYoshinobu Inoue * If numreic representation of AF1 can be interpreted as FQDN 5217d56d374SYoshinobu Inoue * representation of AF2, we need to think again about the code below. 5227d56d374SYoshinobu Inoue */ 5237d56d374SYoshinobu Inoue if (sentinel.ai_next) 5247d56d374SYoshinobu Inoue goto good; 5257d56d374SYoshinobu Inoue 5267d56d374SYoshinobu Inoue if (pai->ai_flags & AI_NUMERICHOST) 527b484faa5SPeter Pentchev ERR(EAI_NONAME); 5287d56d374SYoshinobu Inoue if (hostname == NULL) 529b826397aSJun-ichiro itojun Hagino ERR(EAI_NODATA); 5307d56d374SYoshinobu Inoue 531a42af91cSHajimu UMEMOTO if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0)) 532a42af91cSHajimu UMEMOTO ERR(EAI_FAIL); 533a42af91cSHajimu UMEMOTO 534a42af91cSHajimu UMEMOTO /* 535a42af91cSHajimu UMEMOTO * hostname as alphabetical name. 536a42af91cSHajimu UMEMOTO * we would like to prefer AF_INET6 than AF_INET, so we'll make a 537a42af91cSHajimu UMEMOTO * outer loop by AFs. 538a42af91cSHajimu UMEMOTO */ 539a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 5407d56d374SYoshinobu Inoue *pai = ai0; 5417d56d374SYoshinobu Inoue 542a42af91cSHajimu UMEMOTO /* require exact match for family field */ 543a42af91cSHajimu UMEMOTO if (pai->ai_family != ex->e_af) 544a42af91cSHajimu UMEMOTO continue; 545a42af91cSHajimu UMEMOTO 5467d56d374SYoshinobu Inoue if (!MATCH(pai->ai_socktype, ex->e_socktype, 5477d56d374SYoshinobu Inoue WILD_SOCKTYPE(ex))) { 5487d56d374SYoshinobu Inoue continue; 5497d56d374SYoshinobu Inoue } 5507d56d374SYoshinobu Inoue if (!MATCH(pai->ai_protocol, ex->e_protocol, 5517d56d374SYoshinobu Inoue WILD_PROTOCOL(ex))) { 5527d56d374SYoshinobu Inoue continue; 5537d56d374SYoshinobu Inoue } 5547d56d374SYoshinobu Inoue 5557d56d374SYoshinobu Inoue if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 5567d56d374SYoshinobu Inoue pai->ai_socktype = ex->e_socktype; 5577d56d374SYoshinobu Inoue if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 5587d56d374SYoshinobu Inoue pai->ai_protocol = ex->e_protocol; 5597d56d374SYoshinobu Inoue 560248aee62SJacques Vidrine error = explore_fqdn(pai, hostname, servname, 561248aee62SJacques Vidrine &cur->ai_next); 5627d56d374SYoshinobu Inoue 5637d56d374SYoshinobu Inoue while (cur && cur->ai_next) 5647d56d374SYoshinobu Inoue cur = cur->ai_next; 5657d56d374SYoshinobu Inoue } 5667d56d374SYoshinobu Inoue 567a42af91cSHajimu UMEMOTO /* XXX */ 568a42af91cSHajimu UMEMOTO if (sentinel.ai_next) 569a42af91cSHajimu UMEMOTO error = 0; 570a42af91cSHajimu UMEMOTO 571a42af91cSHajimu UMEMOTO if (error) 572a42af91cSHajimu UMEMOTO goto free; 573a42af91cSHajimu UMEMOTO if (error == 0) { 5747d56d374SYoshinobu Inoue if (sentinel.ai_next) { 5757d56d374SYoshinobu Inoue good: 5767d56d374SYoshinobu Inoue *res = sentinel.ai_next; 5777d56d374SYoshinobu Inoue return SUCCESS; 578a42af91cSHajimu UMEMOTO } else 579a42af91cSHajimu UMEMOTO error = EAI_FAIL; 5807d56d374SYoshinobu Inoue } 5817d56d374SYoshinobu Inoue free: 5827d56d374SYoshinobu Inoue bad: 5837d56d374SYoshinobu Inoue if (sentinel.ai_next) 5847d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 5857d56d374SYoshinobu Inoue *res = NULL; 5867d56d374SYoshinobu Inoue return error; 5877d56d374SYoshinobu Inoue } 5887d56d374SYoshinobu Inoue 5897d56d374SYoshinobu Inoue /* 5907d56d374SYoshinobu Inoue * FQDN hostname, DNS lookup 5917d56d374SYoshinobu Inoue */ 5927d56d374SYoshinobu Inoue static int 5937d56d374SYoshinobu Inoue explore_fqdn(pai, hostname, servname, res) 5947d56d374SYoshinobu Inoue const struct addrinfo *pai; 5957d56d374SYoshinobu Inoue const char *hostname; 5967d56d374SYoshinobu Inoue const char *servname; 5977d56d374SYoshinobu Inoue struct addrinfo **res; 5987d56d374SYoshinobu Inoue { 599a42af91cSHajimu UMEMOTO struct addrinfo *result; 600a42af91cSHajimu UMEMOTO struct addrinfo *cur; 601248aee62SJacques Vidrine int error = 0; 602248aee62SJacques Vidrine static const ns_dtab dtab[] = { 603248aee62SJacques Vidrine NS_FILES_CB(_files_getaddrinfo, NULL) 604248aee62SJacques Vidrine { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ 605248aee62SJacques Vidrine NS_NIS_CB(_yp_getaddrinfo, NULL) 606248aee62SJacques Vidrine { 0 } 607248aee62SJacques Vidrine }; 6087d56d374SYoshinobu Inoue 609a42af91cSHajimu UMEMOTO result = NULL; 6107d56d374SYoshinobu Inoue 61171918af6SHajimu UMEMOTO THREAD_LOCK(); 61271918af6SHajimu UMEMOTO 6137d56d374SYoshinobu Inoue /* 6147d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 6157d56d374SYoshinobu Inoue */ 61671918af6SHajimu UMEMOTO if (get_portmatch(pai, servname) != 0) { 61771918af6SHajimu UMEMOTO THREAD_UNLOCK(); 6187d56d374SYoshinobu Inoue return 0; 61971918af6SHajimu UMEMOTO } 6207d56d374SYoshinobu Inoue 62120e0e084SJacques Vidrine switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", 622248aee62SJacques Vidrine default_dns_files, hostname, pai)) { 623248aee62SJacques Vidrine case NS_TRYAGAIN: 624248aee62SJacques Vidrine error = EAI_AGAIN; 625248aee62SJacques Vidrine goto free; 626248aee62SJacques Vidrine case NS_UNAVAIL: 627248aee62SJacques Vidrine error = EAI_FAIL; 628248aee62SJacques Vidrine goto free; 629248aee62SJacques Vidrine case NS_NOTFOUND: 630248aee62SJacques Vidrine error = EAI_NODATA; 631248aee62SJacques Vidrine goto free; 632248aee62SJacques Vidrine case NS_SUCCESS: 633248aee62SJacques Vidrine error = 0; 634a42af91cSHajimu UMEMOTO for (cur = result; cur; cur = cur->ai_next) { 635a42af91cSHajimu UMEMOTO GET_PORT(cur, servname); 636a42af91cSHajimu UMEMOTO /* canonname should be filled already */ 6377d56d374SYoshinobu Inoue } 638248aee62SJacques Vidrine break; 639a42af91cSHajimu UMEMOTO } 64071918af6SHajimu UMEMOTO THREAD_UNLOCK(); 6417d56d374SYoshinobu Inoue 642248aee62SJacques Vidrine *res = result; 643248aee62SJacques Vidrine 644248aee62SJacques Vidrine return 0; 645248aee62SJacques Vidrine 6467d56d374SYoshinobu Inoue free: 64771918af6SHajimu UMEMOTO THREAD_UNLOCK(); 648a42af91cSHajimu UMEMOTO if (result) 649a42af91cSHajimu UMEMOTO freeaddrinfo(result); 6507d56d374SYoshinobu Inoue return error; 6517d56d374SYoshinobu Inoue } 6527d56d374SYoshinobu Inoue 6537d56d374SYoshinobu Inoue /* 6547d56d374SYoshinobu Inoue * hostname == NULL. 6557d56d374SYoshinobu Inoue * passive socket -> anyaddr (0.0.0.0 or ::) 6567d56d374SYoshinobu Inoue * non-passive socket -> localhost (127.0.0.1 or ::1) 6577d56d374SYoshinobu Inoue */ 6587d56d374SYoshinobu Inoue static int 659a42af91cSHajimu UMEMOTO explore_null(pai, servname, res) 6607d56d374SYoshinobu Inoue const struct addrinfo *pai; 6617d56d374SYoshinobu Inoue const char *servname; 6627d56d374SYoshinobu Inoue struct addrinfo **res; 6637d56d374SYoshinobu Inoue { 6647d56d374SYoshinobu Inoue int s; 6657d56d374SYoshinobu Inoue const struct afd *afd; 6667d56d374SYoshinobu Inoue struct addrinfo *cur; 6677d56d374SYoshinobu Inoue struct addrinfo sentinel; 6687d56d374SYoshinobu Inoue int error; 6697d56d374SYoshinobu Inoue 6707d56d374SYoshinobu Inoue *res = NULL; 6717d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 6727d56d374SYoshinobu Inoue cur = &sentinel; 6737d56d374SYoshinobu Inoue 6747d56d374SYoshinobu Inoue /* 6757d56d374SYoshinobu Inoue * filter out AFs that are not supported by the kernel 6767d56d374SYoshinobu Inoue * XXX errno? 6777d56d374SYoshinobu Inoue */ 678d201fe46SDaniel Eischen s = _socket(pai->ai_family, SOCK_DGRAM, 0); 679a42af91cSHajimu UMEMOTO if (s < 0) { 680a42af91cSHajimu UMEMOTO if (errno != EMFILE) 6817d56d374SYoshinobu Inoue return 0; 682a42af91cSHajimu UMEMOTO } else 6839233c4d9SJason Evans _close(s); 684a42af91cSHajimu UMEMOTO 685a42af91cSHajimu UMEMOTO /* 686a42af91cSHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 687a42af91cSHajimu UMEMOTO */ 688a42af91cSHajimu UMEMOTO if (get_portmatch(pai, servname) != 0) 689a42af91cSHajimu UMEMOTO return 0; 690a42af91cSHajimu UMEMOTO 6917d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 6927d56d374SYoshinobu Inoue if (afd == NULL) 6937d56d374SYoshinobu Inoue return 0; 6947d56d374SYoshinobu Inoue 695a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_PASSIVE) { 696a42af91cSHajimu UMEMOTO GET_AI(cur->ai_next, afd, afd->a_addrany); 6977d56d374SYoshinobu Inoue /* xxx meaningless? 6987d56d374SYoshinobu Inoue * GET_CANONNAME(cur->ai_next, "anyaddr"); 699a42af91cSHajimu UMEMOTO */ 700a42af91cSHajimu UMEMOTO GET_PORT(cur->ai_next, servname); 701a42af91cSHajimu UMEMOTO } else { 702a42af91cSHajimu UMEMOTO GET_AI(cur->ai_next, afd, afd->a_loopback); 703a42af91cSHajimu UMEMOTO /* xxx meaningless? 7047d56d374SYoshinobu Inoue * GET_CANONNAME(cur->ai_next, "localhost"); 7057d56d374SYoshinobu Inoue */ 7067d56d374SYoshinobu Inoue GET_PORT(cur->ai_next, servname); 707a42af91cSHajimu UMEMOTO } 708a42af91cSHajimu UMEMOTO cur = cur->ai_next; 7097d56d374SYoshinobu Inoue 7107d56d374SYoshinobu Inoue *res = sentinel.ai_next; 7117d56d374SYoshinobu Inoue return 0; 7127d56d374SYoshinobu Inoue 7137d56d374SYoshinobu Inoue free: 7147d56d374SYoshinobu Inoue if (sentinel.ai_next) 7157d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 7167d56d374SYoshinobu Inoue return error; 7177d56d374SYoshinobu Inoue } 7187d56d374SYoshinobu Inoue 7197d56d374SYoshinobu Inoue /* 7207d56d374SYoshinobu Inoue * numeric hostname 7217d56d374SYoshinobu Inoue */ 7227d56d374SYoshinobu Inoue static int 7237d56d374SYoshinobu Inoue explore_numeric(pai, hostname, servname, res) 7247d56d374SYoshinobu Inoue const struct addrinfo *pai; 7257d56d374SYoshinobu Inoue const char *hostname; 7267d56d374SYoshinobu Inoue const char *servname; 7277d56d374SYoshinobu Inoue struct addrinfo **res; 7287d56d374SYoshinobu Inoue { 7297d56d374SYoshinobu Inoue const struct afd *afd; 7307d56d374SYoshinobu Inoue struct addrinfo *cur; 7317d56d374SYoshinobu Inoue struct addrinfo sentinel; 7327d56d374SYoshinobu Inoue int error; 7337d56d374SYoshinobu Inoue char pton[PTON_MAX]; 7347d56d374SYoshinobu Inoue 7357d56d374SYoshinobu Inoue *res = NULL; 7367d56d374SYoshinobu Inoue sentinel.ai_next = NULL; 7377d56d374SYoshinobu Inoue cur = &sentinel; 7387d56d374SYoshinobu Inoue 7397d56d374SYoshinobu Inoue /* 7407d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 7417d56d374SYoshinobu Inoue */ 7427d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 7437d56d374SYoshinobu Inoue return 0; 7447d56d374SYoshinobu Inoue 7457d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 7467d56d374SYoshinobu Inoue if (afd == NULL) 7477d56d374SYoshinobu Inoue return 0; 7487d56d374SYoshinobu Inoue 749b826397aSJun-ichiro itojun Hagino switch (afd->a_af) { 750b826397aSJun-ichiro itojun Hagino #if 1 /*X/Open spec*/ 751b826397aSJun-ichiro itojun Hagino case AF_INET: 752b826397aSJun-ichiro itojun Hagino if (inet_aton(hostname, (struct in_addr *)pton) == 1) { 7537d56d374SYoshinobu Inoue if (pai->ai_family == afd->a_af || 7547d56d374SYoshinobu Inoue pai->ai_family == PF_UNSPEC /*?*/) { 7557d56d374SYoshinobu Inoue GET_AI(cur->ai_next, afd, pton); 7567d56d374SYoshinobu Inoue GET_PORT(cur->ai_next, servname); 7577d56d374SYoshinobu Inoue while (cur && cur->ai_next) 7587d56d374SYoshinobu Inoue cur = cur->ai_next; 7597d56d374SYoshinobu Inoue } else 7607d56d374SYoshinobu Inoue ERR(EAI_FAMILY); /*xxx*/ 7617d56d374SYoshinobu Inoue } 762b826397aSJun-ichiro itojun Hagino break; 763b826397aSJun-ichiro itojun Hagino #endif 764b826397aSJun-ichiro itojun Hagino default: 765b826397aSJun-ichiro itojun Hagino if (inet_pton(afd->a_af, hostname, pton) == 1) { 766b826397aSJun-ichiro itojun Hagino if (pai->ai_family == afd->a_af || 767b826397aSJun-ichiro itojun Hagino pai->ai_family == PF_UNSPEC /*?*/) { 768b826397aSJun-ichiro itojun Hagino GET_AI(cur->ai_next, afd, pton); 769b826397aSJun-ichiro itojun Hagino GET_PORT(cur->ai_next, servname); 770b826397aSJun-ichiro itojun Hagino while (cur && cur->ai_next) 771b826397aSJun-ichiro itojun Hagino cur = cur->ai_next; 772b826397aSJun-ichiro itojun Hagino } else 773dd521ef1SHajimu UMEMOTO ERR(EAI_FAMILY); /* XXX */ 774b826397aSJun-ichiro itojun Hagino } 775b826397aSJun-ichiro itojun Hagino break; 776b826397aSJun-ichiro itojun Hagino } 7777d56d374SYoshinobu Inoue 7787d56d374SYoshinobu Inoue *res = sentinel.ai_next; 7797d56d374SYoshinobu Inoue return 0; 7807d56d374SYoshinobu Inoue 7817d56d374SYoshinobu Inoue free: 7827d56d374SYoshinobu Inoue bad: 7837d56d374SYoshinobu Inoue if (sentinel.ai_next) 7847d56d374SYoshinobu Inoue freeaddrinfo(sentinel.ai_next); 7857d56d374SYoshinobu Inoue return error; 7867d56d374SYoshinobu Inoue } 7877d56d374SYoshinobu Inoue 7887d56d374SYoshinobu Inoue /* 7897d56d374SYoshinobu Inoue * numeric hostname with scope 7907d56d374SYoshinobu Inoue */ 7917d56d374SYoshinobu Inoue static int 7927d56d374SYoshinobu Inoue explore_numeric_scope(pai, hostname, servname, res) 7937d56d374SYoshinobu Inoue const struct addrinfo *pai; 7947d56d374SYoshinobu Inoue const char *hostname; 7957d56d374SYoshinobu Inoue const char *servname; 7967d56d374SYoshinobu Inoue struct addrinfo **res; 7977d56d374SYoshinobu Inoue { 798a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6) 7997d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 8007d56d374SYoshinobu Inoue #else 8017d56d374SYoshinobu Inoue const struct afd *afd; 8027d56d374SYoshinobu Inoue struct addrinfo *cur; 8037d56d374SYoshinobu Inoue int error; 804a42af91cSHajimu UMEMOTO char *cp, *hostname2 = NULL, *scope, *addr; 8057d56d374SYoshinobu Inoue struct sockaddr_in6 *sin6; 8067d56d374SYoshinobu Inoue 8077d56d374SYoshinobu Inoue /* 8087d56d374SYoshinobu Inoue * if the servname does not match socktype/protocol, ignore it. 8097d56d374SYoshinobu Inoue */ 8107d56d374SYoshinobu Inoue if (get_portmatch(pai, servname) != 0) 8117d56d374SYoshinobu Inoue return 0; 8127d56d374SYoshinobu Inoue 8137d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 8147d56d374SYoshinobu Inoue if (afd == NULL) 8157d56d374SYoshinobu Inoue return 0; 816248aee62SJacques Vidrine 8177d56d374SYoshinobu Inoue if (!afd->a_scoped) 8187d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 8197d56d374SYoshinobu Inoue 8207d56d374SYoshinobu Inoue cp = strchr(hostname, SCOPE_DELIMITER); 8217d56d374SYoshinobu Inoue if (cp == NULL) 8227d56d374SYoshinobu Inoue return explore_numeric(pai, hostname, servname, res); 8237d56d374SYoshinobu Inoue 8247d56d374SYoshinobu Inoue /* 8257d56d374SYoshinobu Inoue * Handle special case of <scoped_address><delimiter><scope id> 8267d56d374SYoshinobu Inoue */ 8277d56d374SYoshinobu Inoue hostname2 = strdup(hostname); 8287d56d374SYoshinobu Inoue if (hostname2 == NULL) 8297d56d374SYoshinobu Inoue return EAI_MEMORY; 8307d56d374SYoshinobu Inoue /* terminate at the delimiter */ 8317d56d374SYoshinobu Inoue hostname2[cp - hostname] = '\0'; 832a42af91cSHajimu UMEMOTO addr = hostname2; 833a42af91cSHajimu UMEMOTO scope = cp + 1; 8347d56d374SYoshinobu Inoue 835a42af91cSHajimu UMEMOTO error = explore_numeric(pai, addr, servname, res); 8367d56d374SYoshinobu Inoue if (error == 0) { 837a89c1d30SHajimu UMEMOTO u_int32_t scopeid; 838a42af91cSHajimu UMEMOTO 8397d56d374SYoshinobu Inoue for (cur = *res; cur; cur = cur->ai_next) { 8407d56d374SYoshinobu Inoue if (cur->ai_family != AF_INET6) 8417d56d374SYoshinobu Inoue continue; 842a42af91cSHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; 843a89c1d30SHajimu UMEMOTO if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { 844a42af91cSHajimu UMEMOTO free(hostname2); 845b826397aSJun-ichiro itojun Hagino return(EAI_NODATA); /* XXX: is return OK? */ 846a42af91cSHajimu UMEMOTO } 847a42af91cSHajimu UMEMOTO sin6->sin6_scope_id = scopeid; 8487d56d374SYoshinobu Inoue } 8497d56d374SYoshinobu Inoue } 8507d56d374SYoshinobu Inoue 8517d56d374SYoshinobu Inoue free(hostname2); 8527d56d374SYoshinobu Inoue 8537d56d374SYoshinobu Inoue return error; 8547d56d374SYoshinobu Inoue #endif 8557d56d374SYoshinobu Inoue } 8567d56d374SYoshinobu Inoue 8577d56d374SYoshinobu Inoue static int 8587d56d374SYoshinobu Inoue get_canonname(pai, ai, str) 8597d56d374SYoshinobu Inoue const struct addrinfo *pai; 8607d56d374SYoshinobu Inoue struct addrinfo *ai; 8617d56d374SYoshinobu Inoue const char *str; 8627d56d374SYoshinobu Inoue { 8637d56d374SYoshinobu Inoue if ((pai->ai_flags & AI_CANONNAME) != 0) { 8647d56d374SYoshinobu Inoue ai->ai_canonname = (char *)malloc(strlen(str) + 1); 8657d56d374SYoshinobu Inoue if (ai->ai_canonname == NULL) 8667d56d374SYoshinobu Inoue return EAI_MEMORY; 867d0509082SJacques Vidrine strlcpy(ai->ai_canonname, str, strlen(str) + 1); 8687d56d374SYoshinobu Inoue } 8697d56d374SYoshinobu Inoue return 0; 8707d56d374SYoshinobu Inoue } 8717d56d374SYoshinobu Inoue 8727d56d374SYoshinobu Inoue static struct addrinfo * 8737d56d374SYoshinobu Inoue get_ai(pai, afd, addr) 8747d56d374SYoshinobu Inoue const struct addrinfo *pai; 8757d56d374SYoshinobu Inoue const struct afd *afd; 8767d56d374SYoshinobu Inoue const char *addr; 8777d56d374SYoshinobu Inoue { 8787d56d374SYoshinobu Inoue char *p; 8797d56d374SYoshinobu Inoue struct addrinfo *ai; 8807d56d374SYoshinobu Inoue #ifdef FAITH 8817d56d374SYoshinobu Inoue struct in6_addr faith_prefix; 8827d56d374SYoshinobu Inoue char *fp_str; 8837d56d374SYoshinobu Inoue int translate = 0; 8847d56d374SYoshinobu Inoue #endif 8857d56d374SYoshinobu Inoue 8867d56d374SYoshinobu Inoue #ifdef FAITH 8877d56d374SYoshinobu Inoue /* 8887d56d374SYoshinobu Inoue * Transfrom an IPv4 addr into a special IPv6 addr format for 8897d56d374SYoshinobu Inoue * IPv6->IPv4 translation gateway. (only TCP is supported now) 8907d56d374SYoshinobu Inoue * 8917d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 8927d56d374SYoshinobu Inoue * | faith prefix part (12 bytes) | embedded | 8937d56d374SYoshinobu Inoue * | | IPv4 addr part (4 bytes) 8947d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 8957d56d374SYoshinobu Inoue * 8967d56d374SYoshinobu Inoue * faith prefix part is specified as ascii IPv6 addr format 8977d56d374SYoshinobu Inoue * in environmental variable GAI. 8987d56d374SYoshinobu Inoue * For FAITH to work correctly, routing to faith prefix must be 8997d56d374SYoshinobu Inoue * setup toward a machine where a FAITH daemon operates. 9007d56d374SYoshinobu Inoue * Also, the machine must enable some mechanizm 9017d56d374SYoshinobu Inoue * (e.g. faith interface hack) to divert those packet with 9027d56d374SYoshinobu Inoue * faith prefixed destination addr to user-land FAITH daemon. 9037d56d374SYoshinobu Inoue */ 9047d56d374SYoshinobu Inoue fp_str = getenv("GAI"); 9057d56d374SYoshinobu Inoue if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 9067d56d374SYoshinobu Inoue afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 9077d56d374SYoshinobu Inoue u_int32_t v4a; 9087d56d374SYoshinobu Inoue u_int8_t v4a_top; 9097d56d374SYoshinobu Inoue 9107d56d374SYoshinobu Inoue memcpy(&v4a, addr, sizeof v4a); 9117d56d374SYoshinobu Inoue v4a_top = v4a >> IN_CLASSA_NSHIFT; 9127d56d374SYoshinobu Inoue if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 9137d56d374SYoshinobu Inoue v4a_top != 0 && v4a != IN_LOOPBACKNET) { 9147d56d374SYoshinobu Inoue afd = &afdl[N_INET6]; 9157d56d374SYoshinobu Inoue memcpy(&faith_prefix.s6_addr[12], addr, 9167d56d374SYoshinobu Inoue sizeof(struct in_addr)); 9177d56d374SYoshinobu Inoue translate = 1; 9187d56d374SYoshinobu Inoue } 9197d56d374SYoshinobu Inoue } 9207d56d374SYoshinobu Inoue #endif 9217d56d374SYoshinobu Inoue 9227d56d374SYoshinobu Inoue ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 9237d56d374SYoshinobu Inoue + (afd->a_socklen)); 9247d56d374SYoshinobu Inoue if (ai == NULL) 9257d56d374SYoshinobu Inoue return NULL; 9267d56d374SYoshinobu Inoue 9277d56d374SYoshinobu Inoue memcpy(ai, pai, sizeof(struct addrinfo)); 928a42af91cSHajimu UMEMOTO ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 929a42af91cSHajimu UMEMOTO memset(ai->ai_addr, 0, (size_t)afd->a_socklen); 9307d56d374SYoshinobu Inoue ai->ai_addr->sa_len = afd->a_socklen; 9317d56d374SYoshinobu Inoue ai->ai_addrlen = afd->a_socklen; 9327d56d374SYoshinobu Inoue ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 933248aee62SJacques Vidrine p = (char *)(void *)(ai->ai_addr); 9347d56d374SYoshinobu Inoue #ifdef FAITH 9357d56d374SYoshinobu Inoue if (translate == 1) 936248aee62SJacques Vidrine memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen); 9377d56d374SYoshinobu Inoue else 9387d56d374SYoshinobu Inoue #endif 939248aee62SJacques Vidrine memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); 9407d56d374SYoshinobu Inoue return ai; 9417d56d374SYoshinobu Inoue } 9427d56d374SYoshinobu Inoue 9437d56d374SYoshinobu Inoue static int 9447d56d374SYoshinobu Inoue get_portmatch(ai, servname) 9457d56d374SYoshinobu Inoue const struct addrinfo *ai; 9467d56d374SYoshinobu Inoue const char *servname; 9477d56d374SYoshinobu Inoue { 948a42af91cSHajimu UMEMOTO 9497d56d374SYoshinobu Inoue /* get_port does not touch first argument. when matchonly == 1. */ 950a42af91cSHajimu UMEMOTO /* LINTED const cast */ 9517d56d374SYoshinobu Inoue return get_port((struct addrinfo *)ai, servname, 1); 9527d56d374SYoshinobu Inoue } 9537d56d374SYoshinobu Inoue 9547d56d374SYoshinobu Inoue static int 9557d56d374SYoshinobu Inoue get_port(ai, servname, matchonly) 9567d56d374SYoshinobu Inoue struct addrinfo *ai; 9577d56d374SYoshinobu Inoue const char *servname; 9587d56d374SYoshinobu Inoue int matchonly; 9597d56d374SYoshinobu Inoue { 9607d56d374SYoshinobu Inoue const char *proto; 9617d56d374SYoshinobu Inoue struct servent *sp; 9627d56d374SYoshinobu Inoue int port; 9637d56d374SYoshinobu Inoue int allownumeric; 9647d56d374SYoshinobu Inoue 9657d56d374SYoshinobu Inoue if (servname == NULL) 9667d56d374SYoshinobu Inoue return 0; 967a42af91cSHajimu UMEMOTO switch (ai->ai_family) { 968a42af91cSHajimu UMEMOTO case AF_INET: 969a42af91cSHajimu UMEMOTO #ifdef AF_INET6 970a42af91cSHajimu UMEMOTO case AF_INET6: 9717d56d374SYoshinobu Inoue #endif 972a42af91cSHajimu UMEMOTO break; 973a42af91cSHajimu UMEMOTO default: 9747d56d374SYoshinobu Inoue return 0; 975a42af91cSHajimu UMEMOTO } 9767d56d374SYoshinobu Inoue 9777d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 9787d56d374SYoshinobu Inoue case SOCK_RAW: 9797d56d374SYoshinobu Inoue return EAI_SERVICE; 9807d56d374SYoshinobu Inoue case SOCK_DGRAM: 9817d56d374SYoshinobu Inoue case SOCK_STREAM: 9827d56d374SYoshinobu Inoue allownumeric = 1; 9837d56d374SYoshinobu Inoue break; 9847d56d374SYoshinobu Inoue case ANY: 9857d56d374SYoshinobu Inoue allownumeric = 0; 9867d56d374SYoshinobu Inoue break; 9877d56d374SYoshinobu Inoue default: 9887d56d374SYoshinobu Inoue return EAI_SOCKTYPE; 9897d56d374SYoshinobu Inoue } 9907d56d374SYoshinobu Inoue 9917d56d374SYoshinobu Inoue if (str_isnumber(servname)) { 9927d56d374SYoshinobu Inoue if (!allownumeric) 9937d56d374SYoshinobu Inoue return EAI_SERVICE; 994dd521ef1SHajimu UMEMOTO port = atoi(servname); 9957d56d374SYoshinobu Inoue if (port < 0 || port > 65535) 9967d56d374SYoshinobu Inoue return EAI_SERVICE; 997dd521ef1SHajimu UMEMOTO port = htons(port); 9987d56d374SYoshinobu Inoue } else { 9997d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 10007d56d374SYoshinobu Inoue case SOCK_DGRAM: 10017d56d374SYoshinobu Inoue proto = "udp"; 10027d56d374SYoshinobu Inoue break; 10037d56d374SYoshinobu Inoue case SOCK_STREAM: 10047d56d374SYoshinobu Inoue proto = "tcp"; 10057d56d374SYoshinobu Inoue break; 10067d56d374SYoshinobu Inoue default: 10077d56d374SYoshinobu Inoue proto = NULL; 10087d56d374SYoshinobu Inoue break; 10097d56d374SYoshinobu Inoue } 10107d56d374SYoshinobu Inoue 10117d56d374SYoshinobu Inoue if ((sp = getservbyname(servname, proto)) == NULL) 10127d56d374SYoshinobu Inoue return EAI_SERVICE; 10137d56d374SYoshinobu Inoue port = sp->s_port; 10147d56d374SYoshinobu Inoue } 10157d56d374SYoshinobu Inoue 10167d56d374SYoshinobu Inoue if (!matchonly) { 10177d56d374SYoshinobu Inoue switch (ai->ai_family) { 10187d56d374SYoshinobu Inoue case AF_INET: 1019a42af91cSHajimu UMEMOTO ((struct sockaddr_in *)(void *) 1020a42af91cSHajimu UMEMOTO ai->ai_addr)->sin_port = port; 10217d56d374SYoshinobu Inoue break; 10227d56d374SYoshinobu Inoue #ifdef INET6 10237d56d374SYoshinobu Inoue case AF_INET6: 1024a42af91cSHajimu UMEMOTO ((struct sockaddr_in6 *)(void *) 1025a42af91cSHajimu UMEMOTO ai->ai_addr)->sin6_port = port; 10267d56d374SYoshinobu Inoue break; 10277d56d374SYoshinobu Inoue #endif 10287d56d374SYoshinobu Inoue } 10297d56d374SYoshinobu Inoue } 10307d56d374SYoshinobu Inoue 10317d56d374SYoshinobu Inoue return 0; 10327d56d374SYoshinobu Inoue } 10337d56d374SYoshinobu Inoue 10347d56d374SYoshinobu Inoue static const struct afd * 10357d56d374SYoshinobu Inoue find_afd(af) 10367d56d374SYoshinobu Inoue int af; 10377d56d374SYoshinobu Inoue { 10387d56d374SYoshinobu Inoue const struct afd *afd; 10397d56d374SYoshinobu Inoue 10407d56d374SYoshinobu Inoue if (af == PF_UNSPEC) 10417d56d374SYoshinobu Inoue return NULL; 10427d56d374SYoshinobu Inoue for (afd = afdl; afd->a_af; afd++) { 10437d56d374SYoshinobu Inoue if (afd->a_af == af) 10447d56d374SYoshinobu Inoue return afd; 10457d56d374SYoshinobu Inoue } 10467d56d374SYoshinobu Inoue return NULL; 10477d56d374SYoshinobu Inoue } 1048a42af91cSHajimu UMEMOTO 1049a42af91cSHajimu UMEMOTO /* 1050a42af91cSHajimu UMEMOTO * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend 1051a42af91cSHajimu UMEMOTO * will take care of it. 1052a42af91cSHajimu UMEMOTO * the semantics of AI_ADDRCONFIG is not defined well. we are not sure 1053a42af91cSHajimu UMEMOTO * if the code is right or not. 1054a42af91cSHajimu UMEMOTO * 1055a42af91cSHajimu UMEMOTO * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with 1056a42af91cSHajimu UMEMOTO * _dns_getaddrinfo. 1057a42af91cSHajimu UMEMOTO */ 1058a42af91cSHajimu UMEMOTO static int 1059a42af91cSHajimu UMEMOTO addrconfig(pai) 1060a42af91cSHajimu UMEMOTO struct addrinfo *pai; 1061a42af91cSHajimu UMEMOTO { 1062a42af91cSHajimu UMEMOTO int s, af; 1063a42af91cSHajimu UMEMOTO 1064a42af91cSHajimu UMEMOTO /* 1065a42af91cSHajimu UMEMOTO * TODO: 1066a42af91cSHajimu UMEMOTO * Note that implementation dependent test for address 1067a42af91cSHajimu UMEMOTO * configuration should be done everytime called 1068a42af91cSHajimu UMEMOTO * (or apropriate interval), 1069a42af91cSHajimu UMEMOTO * because addresses will be dynamically assigned or deleted. 1070a42af91cSHajimu UMEMOTO */ 1071a42af91cSHajimu UMEMOTO af = pai->ai_family; 1072a42af91cSHajimu UMEMOTO if (af == AF_UNSPEC) { 1073d201fe46SDaniel Eischen if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1074a42af91cSHajimu UMEMOTO af = AF_INET; 1075a42af91cSHajimu UMEMOTO else { 107661e4108cSJason Evans _close(s); 1077d201fe46SDaniel Eischen if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1078a42af91cSHajimu UMEMOTO af = AF_INET6; 1079a42af91cSHajimu UMEMOTO else 108061e4108cSJason Evans _close(s); 1081a42af91cSHajimu UMEMOTO } 1082a42af91cSHajimu UMEMOTO } 1083a42af91cSHajimu UMEMOTO if (af != AF_UNSPEC) { 1084d201fe46SDaniel Eischen if ((s = _socket(af, SOCK_DGRAM, 0)) < 0) 1085a42af91cSHajimu UMEMOTO return 0; 108661e4108cSJason Evans _close(s); 1087a42af91cSHajimu UMEMOTO } 1088a42af91cSHajimu UMEMOTO pai->ai_family = af; 1089a42af91cSHajimu UMEMOTO return 1; 1090a42af91cSHajimu UMEMOTO } 1091a42af91cSHajimu UMEMOTO 1092a42af91cSHajimu UMEMOTO #ifdef INET6 1093a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */ 1094a42af91cSHajimu UMEMOTO static int 1095a89c1d30SHajimu UMEMOTO ip6_str2scopeid(scope, sin6, scopeid) 1096a42af91cSHajimu UMEMOTO char *scope; 1097a42af91cSHajimu UMEMOTO struct sockaddr_in6 *sin6; 1098a89c1d30SHajimu UMEMOTO u_int32_t *scopeid; 1099a42af91cSHajimu UMEMOTO { 1100a89c1d30SHajimu UMEMOTO u_long lscopeid; 1101a89c1d30SHajimu UMEMOTO struct in6_addr *a6; 1102a42af91cSHajimu UMEMOTO char *ep; 1103a42af91cSHajimu UMEMOTO 1104a89c1d30SHajimu UMEMOTO a6 = &sin6->sin6_addr; 1105a89c1d30SHajimu UMEMOTO 1106ec20fe00SJun-ichiro itojun Hagino /* empty scopeid portion is invalid */ 1107ec20fe00SJun-ichiro itojun Hagino if (*scope == '\0') 1108ec20fe00SJun-ichiro itojun Hagino return -1; 1109ec20fe00SJun-ichiro itojun Hagino 1110a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { 1111a42af91cSHajimu UMEMOTO /* 1112a42af91cSHajimu UMEMOTO * We currently assume a one-to-one mapping between links 1113a42af91cSHajimu UMEMOTO * and interfaces, so we simply use interface indices for 1114a42af91cSHajimu UMEMOTO * like-local scopes. 1115a42af91cSHajimu UMEMOTO */ 1116a89c1d30SHajimu UMEMOTO *scopeid = if_nametoindex(scope); 1117a89c1d30SHajimu UMEMOTO if (*scopeid == 0) 1118a42af91cSHajimu UMEMOTO goto trynumeric; 1119a89c1d30SHajimu UMEMOTO return 0; 1120a42af91cSHajimu UMEMOTO } 1121a42af91cSHajimu UMEMOTO 1122a42af91cSHajimu UMEMOTO /* still unclear about literal, allow numeric only - placeholder */ 1123a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) 1124a42af91cSHajimu UMEMOTO goto trynumeric; 1125a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) 1126a42af91cSHajimu UMEMOTO goto trynumeric; 1127a42af91cSHajimu UMEMOTO else 1128a42af91cSHajimu UMEMOTO goto trynumeric; /* global */ 1129a42af91cSHajimu UMEMOTO 1130a42af91cSHajimu UMEMOTO /* try to convert to a numeric id as a last resort */ 1131a42af91cSHajimu UMEMOTO trynumeric: 1132a89c1d30SHajimu UMEMOTO errno = 0; 1133a89c1d30SHajimu UMEMOTO lscopeid = strtoul(scope, &ep, 10); 1134a89c1d30SHajimu UMEMOTO *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); 1135a89c1d30SHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) 1136a89c1d30SHajimu UMEMOTO return 0; 1137a42af91cSHajimu UMEMOTO else 1138a42af91cSHajimu UMEMOTO return -1; 1139a42af91cSHajimu UMEMOTO } 1140a42af91cSHajimu UMEMOTO #endif 1141a42af91cSHajimu UMEMOTO 114269b0a4b6SJim Pirzyk #ifdef RESOLVSORT 114369b0a4b6SJim Pirzyk struct addr_ptr { 114469b0a4b6SJim Pirzyk struct addrinfo *ai; 114569b0a4b6SJim Pirzyk int aval; 114669b0a4b6SJim Pirzyk }; 114769b0a4b6SJim Pirzyk 114869b0a4b6SJim Pirzyk static int 114969b0a4b6SJim Pirzyk addr4sort(struct addrinfo *sentinel) 115069b0a4b6SJim Pirzyk { 115169b0a4b6SJim Pirzyk struct addrinfo *ai; 115269b0a4b6SJim Pirzyk struct addr_ptr *addrs, addr; 115369b0a4b6SJim Pirzyk struct sockaddr_in *sin; 115469b0a4b6SJim Pirzyk int naddrs, i, j; 115569b0a4b6SJim Pirzyk int needsort = 0; 115669b0a4b6SJim Pirzyk 115769b0a4b6SJim Pirzyk if (!sentinel) 115869b0a4b6SJim Pirzyk return -1; 115969b0a4b6SJim Pirzyk naddrs = 0; 116069b0a4b6SJim Pirzyk for (ai = sentinel->ai_next; ai; ai = ai->ai_next) 116169b0a4b6SJim Pirzyk naddrs++; 116269b0a4b6SJim Pirzyk if (naddrs < 2) 116369b0a4b6SJim Pirzyk return 0; /* We don't need sorting. */ 116469b0a4b6SJim Pirzyk if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL) 116569b0a4b6SJim Pirzyk return -1; 116669b0a4b6SJim Pirzyk i = 0; 116769b0a4b6SJim Pirzyk for (ai = sentinel->ai_next; ai; ai = ai->ai_next) { 116869b0a4b6SJim Pirzyk sin = (struct sockaddr_in *)ai->ai_addr; 116969b0a4b6SJim Pirzyk for (j = 0; (unsigned)j < _res.nsort; j++) { 117069b0a4b6SJim Pirzyk if (_res.sort_list[j].addr.s_addr == 117169b0a4b6SJim Pirzyk (sin->sin_addr.s_addr & _res.sort_list[j].mask)) 117269b0a4b6SJim Pirzyk break; 117369b0a4b6SJim Pirzyk } 117469b0a4b6SJim Pirzyk addrs[i].ai = ai; 117569b0a4b6SJim Pirzyk addrs[i].aval = j; 117669b0a4b6SJim Pirzyk if (needsort == 0 && i > 0 && j < addrs[i - 1].aval) 117769b0a4b6SJim Pirzyk needsort = i; 117869b0a4b6SJim Pirzyk i++; 117969b0a4b6SJim Pirzyk } 118069b0a4b6SJim Pirzyk if (!needsort) { 118169b0a4b6SJim Pirzyk free(addrs); 118269b0a4b6SJim Pirzyk return 0; 118369b0a4b6SJim Pirzyk } 118469b0a4b6SJim Pirzyk 118569b0a4b6SJim Pirzyk while (needsort < naddrs) { 118669b0a4b6SJim Pirzyk for (j = needsort - 1; j >= 0; j--) { 118769b0a4b6SJim Pirzyk if (addrs[j].aval > addrs[j+1].aval) { 118869b0a4b6SJim Pirzyk addr = addrs[j]; 118969b0a4b6SJim Pirzyk addrs[j] = addrs[j + 1]; 119069b0a4b6SJim Pirzyk addrs[j + 1] = addr; 119169b0a4b6SJim Pirzyk } else 119269b0a4b6SJim Pirzyk break; 119369b0a4b6SJim Pirzyk } 119469b0a4b6SJim Pirzyk needsort++; 119569b0a4b6SJim Pirzyk } 119669b0a4b6SJim Pirzyk 119769b0a4b6SJim Pirzyk ai = sentinel; 119869b0a4b6SJim Pirzyk for (i = 0; i < naddrs; ++i) { 119969b0a4b6SJim Pirzyk ai->ai_next = addrs[i].ai; 120069b0a4b6SJim Pirzyk ai = ai->ai_next; 120169b0a4b6SJim Pirzyk } 120269b0a4b6SJim Pirzyk ai->ai_next = NULL; 120369b0a4b6SJim Pirzyk free(addrs); 120469b0a4b6SJim Pirzyk return 0; 120569b0a4b6SJim Pirzyk } 120669b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/ 120769b0a4b6SJim Pirzyk 1208a42af91cSHajimu UMEMOTO #ifdef DEBUG 1209a42af91cSHajimu UMEMOTO static const char AskedForGot[] = 1210a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 1211a42af91cSHajimu UMEMOTO #endif 1212248aee62SJacques Vidrine static FILE *hostf = NULL; 1213a42af91cSHajimu UMEMOTO 1214a42af91cSHajimu UMEMOTO static struct addrinfo * 1215a42af91cSHajimu UMEMOTO getanswer(answer, anslen, qname, qtype, pai) 1216a42af91cSHajimu UMEMOTO const querybuf *answer; 1217a42af91cSHajimu UMEMOTO int anslen; 1218a42af91cSHajimu UMEMOTO const char *qname; 1219a42af91cSHajimu UMEMOTO int qtype; 1220a42af91cSHajimu UMEMOTO const struct addrinfo *pai; 1221a42af91cSHajimu UMEMOTO { 1222a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1223a42af91cSHajimu UMEMOTO struct addrinfo ai; 1224a42af91cSHajimu UMEMOTO const struct afd *afd; 1225a42af91cSHajimu UMEMOTO char *canonname; 1226a42af91cSHajimu UMEMOTO const HEADER *hp; 1227a42af91cSHajimu UMEMOTO const u_char *cp; 1228a42af91cSHajimu UMEMOTO int n; 1229a42af91cSHajimu UMEMOTO const u_char *eom; 1230dd521ef1SHajimu UMEMOTO char *bp, *ep; 1231dd521ef1SHajimu UMEMOTO int type, class, ancount, qdcount; 1232a42af91cSHajimu UMEMOTO int haveanswer, had_error; 1233a42af91cSHajimu UMEMOTO char tbuf[MAXDNAME]; 1234c05ac53bSDavid E. O'Brien int (*name_ok)(const char *); 1235a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 1236a42af91cSHajimu UMEMOTO 1237a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 1238a42af91cSHajimu UMEMOTO cur = &sentinel; 1239a42af91cSHajimu UMEMOTO 1240a42af91cSHajimu UMEMOTO canonname = NULL; 1241a42af91cSHajimu UMEMOTO eom = answer->buf + anslen; 1242a42af91cSHajimu UMEMOTO switch (qtype) { 1243a42af91cSHajimu UMEMOTO case T_A: 1244a42af91cSHajimu UMEMOTO case T_AAAA: 1245a42af91cSHajimu UMEMOTO case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ 1246a42af91cSHajimu UMEMOTO name_ok = res_hnok; 1247a42af91cSHajimu UMEMOTO break; 1248a42af91cSHajimu UMEMOTO default: 1249a42af91cSHajimu UMEMOTO return (NULL); /* XXX should be abort(); */ 1250a42af91cSHajimu UMEMOTO } 1251a42af91cSHajimu UMEMOTO /* 1252a42af91cSHajimu UMEMOTO * find first satisfactory answer 1253a42af91cSHajimu UMEMOTO */ 1254a42af91cSHajimu UMEMOTO hp = &answer->hdr; 1255a42af91cSHajimu UMEMOTO ancount = ntohs(hp->ancount); 1256a42af91cSHajimu UMEMOTO qdcount = ntohs(hp->qdcount); 1257a42af91cSHajimu UMEMOTO bp = hostbuf; 1258dd521ef1SHajimu UMEMOTO ep = hostbuf + sizeof hostbuf; 1259a42af91cSHajimu UMEMOTO cp = answer->buf + HFIXEDSZ; 1260a42af91cSHajimu UMEMOTO if (qdcount != 1) { 1261a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1262a42af91cSHajimu UMEMOTO return (NULL); 1263a42af91cSHajimu UMEMOTO } 1264dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1265a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 1266a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1267a42af91cSHajimu UMEMOTO return (NULL); 1268a42af91cSHajimu UMEMOTO } 1269a42af91cSHajimu UMEMOTO cp += n + QFIXEDSZ; 1270a42af91cSHajimu UMEMOTO if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 1271a42af91cSHajimu UMEMOTO /* res_send() has already verified that the query name is the 1272a42af91cSHajimu UMEMOTO * same as the one we sent; this just gets the expanded name 1273a42af91cSHajimu UMEMOTO * (i.e., with the succeeding search-domain tacked on). 1274a42af91cSHajimu UMEMOTO */ 1275a42af91cSHajimu UMEMOTO n = strlen(bp) + 1; /* for the \0 */ 1276a42af91cSHajimu UMEMOTO if (n >= MAXHOSTNAMELEN) { 1277a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1278a42af91cSHajimu UMEMOTO return (NULL); 1279a42af91cSHajimu UMEMOTO } 1280a42af91cSHajimu UMEMOTO canonname = bp; 1281a42af91cSHajimu UMEMOTO bp += n; 1282a42af91cSHajimu UMEMOTO /* The qname can be abbreviated, but h_name is now absolute. */ 1283a42af91cSHajimu UMEMOTO qname = canonname; 1284a42af91cSHajimu UMEMOTO } 1285a42af91cSHajimu UMEMOTO haveanswer = 0; 1286a42af91cSHajimu UMEMOTO had_error = 0; 1287a42af91cSHajimu UMEMOTO while (ancount-- > 0 && cp < eom && !had_error) { 1288dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1289a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 1290a42af91cSHajimu UMEMOTO had_error++; 1291a42af91cSHajimu UMEMOTO continue; 1292a42af91cSHajimu UMEMOTO } 1293a42af91cSHajimu UMEMOTO cp += n; /* name */ 1294a42af91cSHajimu UMEMOTO type = _getshort(cp); 1295a42af91cSHajimu UMEMOTO cp += INT16SZ; /* type */ 1296a42af91cSHajimu UMEMOTO class = _getshort(cp); 1297a42af91cSHajimu UMEMOTO cp += INT16SZ + INT32SZ; /* class, TTL */ 1298a42af91cSHajimu UMEMOTO n = _getshort(cp); 1299a42af91cSHajimu UMEMOTO cp += INT16SZ; /* len */ 1300a42af91cSHajimu UMEMOTO if (class != C_IN) { 1301a42af91cSHajimu UMEMOTO /* XXX - debug? syslog? */ 1302a42af91cSHajimu UMEMOTO cp += n; 1303a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1304a42af91cSHajimu UMEMOTO } 1305a42af91cSHajimu UMEMOTO if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 1306a42af91cSHajimu UMEMOTO type == T_CNAME) { 1307a42af91cSHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1308a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(tbuf)) { 1309a42af91cSHajimu UMEMOTO had_error++; 1310a42af91cSHajimu UMEMOTO continue; 1311a42af91cSHajimu UMEMOTO } 1312a42af91cSHajimu UMEMOTO cp += n; 1313a42af91cSHajimu UMEMOTO /* Get canonical name. */ 1314a42af91cSHajimu UMEMOTO n = strlen(tbuf) + 1; /* for the \0 */ 1315dd521ef1SHajimu UMEMOTO if (n > ep - bp || n >= MAXHOSTNAMELEN) { 1316a42af91cSHajimu UMEMOTO had_error++; 1317a42af91cSHajimu UMEMOTO continue; 1318a42af91cSHajimu UMEMOTO } 1319d0509082SJacques Vidrine strlcpy(bp, tbuf, ep - bp); 1320a42af91cSHajimu UMEMOTO canonname = bp; 1321a42af91cSHajimu UMEMOTO bp += n; 1322a42af91cSHajimu UMEMOTO continue; 1323a42af91cSHajimu UMEMOTO } 1324a42af91cSHajimu UMEMOTO if (qtype == T_ANY) { 1325a42af91cSHajimu UMEMOTO if (!(type == T_A || type == T_AAAA)) { 1326a42af91cSHajimu UMEMOTO cp += n; 1327a42af91cSHajimu UMEMOTO continue; 1328a42af91cSHajimu UMEMOTO } 1329a42af91cSHajimu UMEMOTO } else if (type != qtype) { 1330a42af91cSHajimu UMEMOTO #ifdef DEBUG 1331a42af91cSHajimu UMEMOTO if (type != T_KEY && type != T_SIG) 1332a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 1333a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 1334a42af91cSHajimu UMEMOTO qname, p_class(C_IN), p_type(qtype), 1335a42af91cSHajimu UMEMOTO p_type(type)); 1336a42af91cSHajimu UMEMOTO #endif 1337a42af91cSHajimu UMEMOTO cp += n; 1338a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1339a42af91cSHajimu UMEMOTO } 1340a42af91cSHajimu UMEMOTO switch (type) { 1341a42af91cSHajimu UMEMOTO case T_A: 1342a42af91cSHajimu UMEMOTO case T_AAAA: 1343a42af91cSHajimu UMEMOTO if (strcasecmp(canonname, bp) != 0) { 1344a42af91cSHajimu UMEMOTO #ifdef DEBUG 1345a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 1346a42af91cSHajimu UMEMOTO AskedForGot, canonname, bp); 1347a42af91cSHajimu UMEMOTO #endif 1348a42af91cSHajimu UMEMOTO cp += n; 1349a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1350a42af91cSHajimu UMEMOTO } 1351a42af91cSHajimu UMEMOTO if (type == T_A && n != INADDRSZ) { 1352a42af91cSHajimu UMEMOTO cp += n; 1353a42af91cSHajimu UMEMOTO continue; 1354a42af91cSHajimu UMEMOTO } 1355a42af91cSHajimu UMEMOTO if (type == T_AAAA && n != IN6ADDRSZ) { 1356a42af91cSHajimu UMEMOTO cp += n; 1357a42af91cSHajimu UMEMOTO continue; 1358a42af91cSHajimu UMEMOTO } 1359a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 1360a42af91cSHajimu UMEMOTO if (type == T_AAAA) { 1361a42af91cSHajimu UMEMOTO struct in6_addr in6; 1362a42af91cSHajimu UMEMOTO memcpy(&in6, cp, sizeof(in6)); 1363a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&in6)) { 1364a42af91cSHajimu UMEMOTO cp += n; 1365a42af91cSHajimu UMEMOTO continue; 1366a42af91cSHajimu UMEMOTO } 1367a42af91cSHajimu UMEMOTO } 1368a42af91cSHajimu UMEMOTO #endif 1369a42af91cSHajimu UMEMOTO if (!haveanswer) { 1370a42af91cSHajimu UMEMOTO int nn; 1371a42af91cSHajimu UMEMOTO 1372a42af91cSHajimu UMEMOTO canonname = bp; 1373a42af91cSHajimu UMEMOTO nn = strlen(bp) + 1; /* for the \0 */ 1374a42af91cSHajimu UMEMOTO bp += nn; 1375a42af91cSHajimu UMEMOTO } 1376a42af91cSHajimu UMEMOTO 1377a42af91cSHajimu UMEMOTO /* don't overwrite pai */ 1378a42af91cSHajimu UMEMOTO ai = *pai; 1379a42af91cSHajimu UMEMOTO ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; 1380a42af91cSHajimu UMEMOTO afd = find_afd(ai.ai_family); 1381a42af91cSHajimu UMEMOTO if (afd == NULL) { 1382a42af91cSHajimu UMEMOTO cp += n; 1383a42af91cSHajimu UMEMOTO continue; 1384a42af91cSHajimu UMEMOTO } 1385a42af91cSHajimu UMEMOTO cur->ai_next = get_ai(&ai, afd, (const char *)cp); 1386a42af91cSHajimu UMEMOTO if (cur->ai_next == NULL) 1387a42af91cSHajimu UMEMOTO had_error++; 1388a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1389a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1390a42af91cSHajimu UMEMOTO cp += n; 1391a42af91cSHajimu UMEMOTO break; 1392a42af91cSHajimu UMEMOTO default: 1393a42af91cSHajimu UMEMOTO abort(); 1394a42af91cSHajimu UMEMOTO } 1395a42af91cSHajimu UMEMOTO if (!had_error) 1396a42af91cSHajimu UMEMOTO haveanswer++; 1397a42af91cSHajimu UMEMOTO } 1398a42af91cSHajimu UMEMOTO if (haveanswer) { 139969b0a4b6SJim Pirzyk #if defined(RESOLVSORT) 140069b0a4b6SJim Pirzyk /* 140169b0a4b6SJim Pirzyk * We support only IPv4 address for backward 140269b0a4b6SJim Pirzyk * compatibility against gethostbyname(3). 140369b0a4b6SJim Pirzyk */ 140469b0a4b6SJim Pirzyk if (_res.nsort && qtype == T_A) { 140569b0a4b6SJim Pirzyk if (addr4sort(&sentinel) < 0) { 140669b0a4b6SJim Pirzyk freeaddrinfo(sentinel.ai_next); 140769b0a4b6SJim Pirzyk h_errno = NO_RECOVERY; 140869b0a4b6SJim Pirzyk return NULL; 140969b0a4b6SJim Pirzyk } 141069b0a4b6SJim Pirzyk } 141169b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/ 1412a42af91cSHajimu UMEMOTO if (!canonname) 1413a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, qname); 1414a42af91cSHajimu UMEMOTO else 1415a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, canonname); 1416a42af91cSHajimu UMEMOTO h_errno = NETDB_SUCCESS; 1417a42af91cSHajimu UMEMOTO return sentinel.ai_next; 1418a42af91cSHajimu UMEMOTO } 1419a42af91cSHajimu UMEMOTO 1420a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1421a42af91cSHajimu UMEMOTO return NULL; 1422a42af91cSHajimu UMEMOTO } 1423a42af91cSHajimu UMEMOTO 1424a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 1425a42af91cSHajimu UMEMOTO static int 1426248aee62SJacques Vidrine _dns_getaddrinfo(rv, cb_data, ap) 1427248aee62SJacques Vidrine void *rv; 1428248aee62SJacques Vidrine void *cb_data; 1429248aee62SJacques Vidrine va_list ap; 1430a42af91cSHajimu UMEMOTO { 1431a42af91cSHajimu UMEMOTO struct addrinfo *ai; 14325cd588cbSHajimu UMEMOTO querybuf *buf, *buf2; 1433a42af91cSHajimu UMEMOTO const char *name; 1434248aee62SJacques Vidrine const struct addrinfo *pai; 1435a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1436a42af91cSHajimu UMEMOTO struct res_target q, q2; 1437a42af91cSHajimu UMEMOTO 1438248aee62SJacques Vidrine name = va_arg(ap, char *); 1439248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *); 1440248aee62SJacques Vidrine 1441a42af91cSHajimu UMEMOTO memset(&q, 0, sizeof(q2)); 1442a42af91cSHajimu UMEMOTO memset(&q2, 0, sizeof(q2)); 1443a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 1444a42af91cSHajimu UMEMOTO cur = &sentinel; 1445a42af91cSHajimu UMEMOTO 14465cd588cbSHajimu UMEMOTO buf = malloc(sizeof(*buf)); 14475cd588cbSHajimu UMEMOTO if (!buf) { 14485cd588cbSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 14495cd588cbSHajimu UMEMOTO return NS_NOTFOUND; 14505cd588cbSHajimu UMEMOTO } 14515cd588cbSHajimu UMEMOTO buf2 = malloc(sizeof(*buf2)); 14525cd588cbSHajimu UMEMOTO if (!buf2) { 14535cd588cbSHajimu UMEMOTO free(buf); 14545cd588cbSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 14555cd588cbSHajimu UMEMOTO return NS_NOTFOUND; 14565cd588cbSHajimu UMEMOTO } 14575cd588cbSHajimu UMEMOTO 1458a42af91cSHajimu UMEMOTO switch (pai->ai_family) { 1459a42af91cSHajimu UMEMOTO case AF_UNSPEC: 1460a42af91cSHajimu UMEMOTO /* prefer IPv6 */ 1461dd521ef1SHajimu UMEMOTO q.name = name; 1462b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 1463b826397aSJun-ichiro itojun Hagino q.qtype = T_AAAA; 14645cd588cbSHajimu UMEMOTO q.answer = buf->buf; 14655cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 1466a42af91cSHajimu UMEMOTO q.next = &q2; 1467a89c1d30SHajimu UMEMOTO q2.name = name; 1468b826397aSJun-ichiro itojun Hagino q2.qclass = C_IN; 1469b826397aSJun-ichiro itojun Hagino q2.qtype = T_A; 14705cd588cbSHajimu UMEMOTO q2.answer = buf2->buf; 14715cd588cbSHajimu UMEMOTO q2.anslen = sizeof(buf2->buf); 1472a42af91cSHajimu UMEMOTO break; 1473a42af91cSHajimu UMEMOTO case AF_INET: 1474dd521ef1SHajimu UMEMOTO q.name = name; 1475b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 1476b826397aSJun-ichiro itojun Hagino q.qtype = T_A; 14775cd588cbSHajimu UMEMOTO q.answer = buf->buf; 14785cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 1479a42af91cSHajimu UMEMOTO break; 1480a42af91cSHajimu UMEMOTO case AF_INET6: 1481dd521ef1SHajimu UMEMOTO q.name = name; 1482b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 1483b826397aSJun-ichiro itojun Hagino q.qtype = T_AAAA; 14845cd588cbSHajimu UMEMOTO q.answer = buf->buf; 14855cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 1486a42af91cSHajimu UMEMOTO break; 1487a42af91cSHajimu UMEMOTO default: 14885cd588cbSHajimu UMEMOTO free(buf); 14895cd588cbSHajimu UMEMOTO free(buf2); 1490248aee62SJacques Vidrine return NS_UNAVAIL; 1491a42af91cSHajimu UMEMOTO } 14925cd588cbSHajimu UMEMOTO if (res_searchN(name, &q) < 0) { 14935cd588cbSHajimu UMEMOTO free(buf); 14945cd588cbSHajimu UMEMOTO free(buf2); 1495248aee62SJacques Vidrine return NS_NOTFOUND; 14965cd588cbSHajimu UMEMOTO } 14975cd588cbSHajimu UMEMOTO ai = getanswer(buf, q.n, q.name, q.qtype, pai); 1498a42af91cSHajimu UMEMOTO if (ai) { 1499a42af91cSHajimu UMEMOTO cur->ai_next = ai; 1500a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1501a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1502a42af91cSHajimu UMEMOTO } 1503a42af91cSHajimu UMEMOTO if (q.next) { 15045cd588cbSHajimu UMEMOTO ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); 1505a42af91cSHajimu UMEMOTO if (ai) 1506a42af91cSHajimu UMEMOTO cur->ai_next = ai; 1507a42af91cSHajimu UMEMOTO } 15085cd588cbSHajimu UMEMOTO free(buf); 15095cd588cbSHajimu UMEMOTO free(buf2); 1510a42af91cSHajimu UMEMOTO if (sentinel.ai_next == NULL) 1511a42af91cSHajimu UMEMOTO switch (h_errno) { 1512a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 1513248aee62SJacques Vidrine return NS_NOTFOUND; 1514a42af91cSHajimu UMEMOTO case TRY_AGAIN: 1515248aee62SJacques Vidrine return NS_TRYAGAIN; 1516a42af91cSHajimu UMEMOTO default: 1517248aee62SJacques Vidrine return NS_UNAVAIL; 1518a42af91cSHajimu UMEMOTO } 1519248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 1520248aee62SJacques Vidrine return NS_SUCCESS; 1521248aee62SJacques Vidrine } 1522248aee62SJacques Vidrine 1523248aee62SJacques Vidrine static void 1524248aee62SJacques Vidrine _sethtent() 1525248aee62SJacques Vidrine { 1526248aee62SJacques Vidrine if (!hostf) 1527248aee62SJacques Vidrine hostf = fopen(_PATH_HOSTS, "r" ); 1528248aee62SJacques Vidrine else 1529248aee62SJacques Vidrine rewind(hostf); 1530248aee62SJacques Vidrine } 1531248aee62SJacques Vidrine 1532248aee62SJacques Vidrine static void 1533248aee62SJacques Vidrine _endhtent() 1534248aee62SJacques Vidrine { 1535248aee62SJacques Vidrine if (hostf) { 1536248aee62SJacques Vidrine (void) fclose(hostf); 1537248aee62SJacques Vidrine hostf = NULL; 1538248aee62SJacques Vidrine } 1539a42af91cSHajimu UMEMOTO } 1540a42af91cSHajimu UMEMOTO 1541a42af91cSHajimu UMEMOTO static struct addrinfo * 1542248aee62SJacques Vidrine _gethtent(name, pai) 1543a42af91cSHajimu UMEMOTO const char *name; 1544a42af91cSHajimu UMEMOTO const struct addrinfo *pai; 1545a42af91cSHajimu UMEMOTO { 1546a42af91cSHajimu UMEMOTO char *p; 1547a42af91cSHajimu UMEMOTO char *cp, *tname, *cname; 1548a42af91cSHajimu UMEMOTO struct addrinfo hints, *res0, *res; 1549a42af91cSHajimu UMEMOTO int error; 1550a42af91cSHajimu UMEMOTO const char *addr; 1551a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 1552a42af91cSHajimu UMEMOTO 1553248aee62SJacques Vidrine if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) 1554248aee62SJacques Vidrine return (NULL); 1555a42af91cSHajimu UMEMOTO again: 1556a42af91cSHajimu UMEMOTO if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) 1557a42af91cSHajimu UMEMOTO return (NULL); 1558a42af91cSHajimu UMEMOTO if (*p == '#') 1559a42af91cSHajimu UMEMOTO goto again; 1560a42af91cSHajimu UMEMOTO if (!(cp = strpbrk(p, "#\n"))) 1561a42af91cSHajimu UMEMOTO goto again; 1562a42af91cSHajimu UMEMOTO *cp = '\0'; 1563a42af91cSHajimu UMEMOTO if (!(cp = strpbrk(p, " \t"))) 1564a42af91cSHajimu UMEMOTO goto again; 1565a42af91cSHajimu UMEMOTO *cp++ = '\0'; 1566a42af91cSHajimu UMEMOTO addr = p; 1567a42af91cSHajimu UMEMOTO cname = NULL; 1568a42af91cSHajimu UMEMOTO /* if this is not something we're looking for, skip it. */ 1569a42af91cSHajimu UMEMOTO while (cp && *cp) { 1570a42af91cSHajimu UMEMOTO if (*cp == ' ' || *cp == '\t') { 1571a42af91cSHajimu UMEMOTO cp++; 1572a42af91cSHajimu UMEMOTO continue; 1573a42af91cSHajimu UMEMOTO } 1574a42af91cSHajimu UMEMOTO tname = cp; 1575a42af91cSHajimu UMEMOTO if (cname == NULL) 1576a42af91cSHajimu UMEMOTO cname = cp; 1577a42af91cSHajimu UMEMOTO if ((cp = strpbrk(cp, " \t")) != NULL) 1578a42af91cSHajimu UMEMOTO *cp++ = '\0'; 1579a42af91cSHajimu UMEMOTO if (strcasecmp(name, tname) == 0) 1580a42af91cSHajimu UMEMOTO goto found; 1581a42af91cSHajimu UMEMOTO } 1582a42af91cSHajimu UMEMOTO goto again; 1583a42af91cSHajimu UMEMOTO 1584a42af91cSHajimu UMEMOTO found: 1585dd521ef1SHajimu UMEMOTO /* we should not glob socktype/protocol here */ 1586dd521ef1SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 1587dd521ef1SHajimu UMEMOTO hints.ai_family = pai->ai_family; 1588dd521ef1SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; 1589dd521ef1SHajimu UMEMOTO hints.ai_protocol = 0; 1590a42af91cSHajimu UMEMOTO hints.ai_flags = AI_NUMERICHOST; 1591dd521ef1SHajimu UMEMOTO error = getaddrinfo(addr, "0", &hints, &res0); 1592a42af91cSHajimu UMEMOTO if (error) 1593a42af91cSHajimu UMEMOTO goto again; 1594a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 1595a42af91cSHajimu UMEMOTO /* XXX should check all items in the chain */ 1596a42af91cSHajimu UMEMOTO if (res0->ai_family == AF_INET6 && 1597a42af91cSHajimu UMEMOTO IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) { 1598a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 1599a42af91cSHajimu UMEMOTO goto again; 1600a42af91cSHajimu UMEMOTO } 1601a42af91cSHajimu UMEMOTO #endif 1602a42af91cSHajimu UMEMOTO for (res = res0; res; res = res->ai_next) { 1603a42af91cSHajimu UMEMOTO /* cover it up */ 1604a42af91cSHajimu UMEMOTO res->ai_flags = pai->ai_flags; 1605dd521ef1SHajimu UMEMOTO res->ai_socktype = pai->ai_socktype; 1606dd521ef1SHajimu UMEMOTO res->ai_protocol = pai->ai_protocol; 1607a42af91cSHajimu UMEMOTO 1608a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_CANONNAME) { 1609a42af91cSHajimu UMEMOTO if (get_canonname(pai, res, cname) != 0) { 1610a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 1611a42af91cSHajimu UMEMOTO goto again; 1612a42af91cSHajimu UMEMOTO } 1613a42af91cSHajimu UMEMOTO } 1614a42af91cSHajimu UMEMOTO } 1615a42af91cSHajimu UMEMOTO return res0; 1616a42af91cSHajimu UMEMOTO } 1617a42af91cSHajimu UMEMOTO 1618a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 1619a42af91cSHajimu UMEMOTO static int 1620248aee62SJacques Vidrine _files_getaddrinfo(rv, cb_data, ap) 1621248aee62SJacques Vidrine void *rv; 1622248aee62SJacques Vidrine void *cb_data; 1623248aee62SJacques Vidrine va_list ap; 1624a42af91cSHajimu UMEMOTO { 1625248aee62SJacques Vidrine const char *name; 1626248aee62SJacques Vidrine const struct addrinfo *pai; 1627a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1628a42af91cSHajimu UMEMOTO struct addrinfo *p; 1629a42af91cSHajimu UMEMOTO 1630248aee62SJacques Vidrine name = va_arg(ap, char *); 1631248aee62SJacques Vidrine pai = va_arg(ap, struct addrinfo *); 1632248aee62SJacques Vidrine 1633248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 1634a42af91cSHajimu UMEMOTO cur = &sentinel; 1635a42af91cSHajimu UMEMOTO 1636248aee62SJacques Vidrine _sethtent(); 1637248aee62SJacques Vidrine while ((p = _gethtent(name, pai)) != NULL) { 1638a42af91cSHajimu UMEMOTO cur->ai_next = p; 1639a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1640a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1641a42af91cSHajimu UMEMOTO } 1642248aee62SJacques Vidrine _endhtent(); 1643a42af91cSHajimu UMEMOTO 1644248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 1645248aee62SJacques Vidrine if (sentinel.ai_next == NULL) 1646248aee62SJacques Vidrine return NS_NOTFOUND; 1647248aee62SJacques Vidrine return NS_SUCCESS; 1648a42af91cSHajimu UMEMOTO } 1649a42af91cSHajimu UMEMOTO 1650a42af91cSHajimu UMEMOTO #ifdef YP 1651248aee62SJacques Vidrine static char *__ypdomain; 1652a42af91cSHajimu UMEMOTO 1653248aee62SJacques Vidrine /*ARGSUSED*/ 1654248aee62SJacques Vidrine static struct addrinfo * 1655248aee62SJacques Vidrine _yphostent(line, pai) 1656248aee62SJacques Vidrine char *line; 1657248aee62SJacques Vidrine const struct addrinfo *pai; 1658248aee62SJacques Vidrine { 1659248aee62SJacques Vidrine struct addrinfo sentinel, *cur; 1660248aee62SJacques Vidrine struct addrinfo hints, *res, *res0; 1661248aee62SJacques Vidrine int error; 1662248aee62SJacques Vidrine char *p = line; 1663248aee62SJacques Vidrine const char *addr, *canonname; 1664248aee62SJacques Vidrine char *nextline; 1665248aee62SJacques Vidrine char *cp; 1666248aee62SJacques Vidrine 1667248aee62SJacques Vidrine addr = canonname = NULL; 1668248aee62SJacques Vidrine 1669248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 1670a42af91cSHajimu UMEMOTO cur = &sentinel; 1671a42af91cSHajimu UMEMOTO 1672248aee62SJacques Vidrine nextline: 1673248aee62SJacques Vidrine /* terminate line */ 1674248aee62SJacques Vidrine cp = strchr(p, '\n'); 1675248aee62SJacques Vidrine if (cp) { 1676248aee62SJacques Vidrine *cp++ = '\0'; 1677248aee62SJacques Vidrine nextline = cp; 1678248aee62SJacques Vidrine } else 1679248aee62SJacques Vidrine nextline = NULL; 1680a42af91cSHajimu UMEMOTO 1681248aee62SJacques Vidrine cp = strpbrk(p, " \t"); 1682248aee62SJacques Vidrine if (cp == NULL) { 1683248aee62SJacques Vidrine if (canonname == NULL) 1684248aee62SJacques Vidrine return (NULL); 1685248aee62SJacques Vidrine else 1686248aee62SJacques Vidrine goto done; 1687a42af91cSHajimu UMEMOTO } 1688248aee62SJacques Vidrine *cp++ = '\0'; 1689a42af91cSHajimu UMEMOTO 1690248aee62SJacques Vidrine addr = p; 1691a42af91cSHajimu UMEMOTO 1692248aee62SJacques Vidrine while (cp && *cp) { 1693248aee62SJacques Vidrine if (*cp == ' ' || *cp == '\t') { 1694248aee62SJacques Vidrine cp++; 1695a42af91cSHajimu UMEMOTO continue; 1696248aee62SJacques Vidrine } 1697248aee62SJacques Vidrine if (!canonname) 1698248aee62SJacques Vidrine canonname = cp; 1699248aee62SJacques Vidrine if ((cp = strpbrk(cp, " \t")) != NULL) 1700248aee62SJacques Vidrine *cp++ = '\0'; 1701a42af91cSHajimu UMEMOTO } 1702a42af91cSHajimu UMEMOTO 1703248aee62SJacques Vidrine hints = *pai; 1704248aee62SJacques Vidrine hints.ai_flags = AI_NUMERICHOST; 1705248aee62SJacques Vidrine error = getaddrinfo(addr, NULL, &hints, &res0); 1706248aee62SJacques Vidrine if (error == 0) { 1707248aee62SJacques Vidrine for (res = res0; res; res = res->ai_next) { 1708248aee62SJacques Vidrine /* cover it up */ 1709248aee62SJacques Vidrine res->ai_flags = pai->ai_flags; 1710248aee62SJacques Vidrine 1711248aee62SJacques Vidrine if (pai->ai_flags & AI_CANONNAME) 1712248aee62SJacques Vidrine (void)get_canonname(pai, res, canonname); 1713248aee62SJacques Vidrine } 1714248aee62SJacques Vidrine } else 1715248aee62SJacques Vidrine res0 = NULL; 1716248aee62SJacques Vidrine if (res0) { 1717248aee62SJacques Vidrine cur->ai_next = res0; 1718a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1719a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1720a42af91cSHajimu UMEMOTO } 1721a42af91cSHajimu UMEMOTO 1722248aee62SJacques Vidrine if (nextline) { 1723248aee62SJacques Vidrine p = nextline; 1724248aee62SJacques Vidrine goto nextline; 1725248aee62SJacques Vidrine } 1726a42af91cSHajimu UMEMOTO 1727248aee62SJacques Vidrine done: 1728248aee62SJacques Vidrine return sentinel.ai_next; 1729248aee62SJacques Vidrine } 1730248aee62SJacques Vidrine 1731248aee62SJacques Vidrine /*ARGSUSED*/ 1732248aee62SJacques Vidrine static int 1733248aee62SJacques Vidrine _yp_getaddrinfo(rv, cb_data, ap) 1734248aee62SJacques Vidrine void *rv; 1735248aee62SJacques Vidrine void *cb_data; 1736248aee62SJacques Vidrine va_list ap; 1737248aee62SJacques Vidrine { 1738248aee62SJacques Vidrine struct addrinfo sentinel, *cur; 1739248aee62SJacques Vidrine struct addrinfo *ai = NULL; 1740248aee62SJacques Vidrine static char *__ypcurrent; 1741248aee62SJacques Vidrine int __ypcurrentlen, r; 1742248aee62SJacques Vidrine const char *name; 1743248aee62SJacques Vidrine const struct addrinfo *pai; 1744248aee62SJacques Vidrine 1745248aee62SJacques Vidrine name = va_arg(ap, char *); 1746248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *); 1747248aee62SJacques Vidrine 1748248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 1749248aee62SJacques Vidrine cur = &sentinel; 1750248aee62SJacques Vidrine 1751248aee62SJacques Vidrine if (!__ypdomain) { 1752248aee62SJacques Vidrine if (_yp_check(&__ypdomain) == 0) 1753248aee62SJacques Vidrine return NS_UNAVAIL; 1754248aee62SJacques Vidrine } 1755248aee62SJacques Vidrine if (__ypcurrent) 1756248aee62SJacques Vidrine free(__ypcurrent); 1757248aee62SJacques Vidrine __ypcurrent = NULL; 1758248aee62SJacques Vidrine 1759248aee62SJacques Vidrine /* hosts.byname is only for IPv4 (Solaris8) */ 1760248aee62SJacques Vidrine if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { 1761248aee62SJacques Vidrine r = yp_match(__ypdomain, "hosts.byname", name, 1762248aee62SJacques Vidrine (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1763248aee62SJacques Vidrine if (r == 0) { 1764248aee62SJacques Vidrine struct addrinfo ai4; 1765248aee62SJacques Vidrine 1766248aee62SJacques Vidrine ai4 = *pai; 1767248aee62SJacques Vidrine ai4.ai_family = AF_INET; 1768248aee62SJacques Vidrine ai = _yphostent(__ypcurrent, &ai4); 1769248aee62SJacques Vidrine if (ai) { 1770248aee62SJacques Vidrine cur->ai_next = ai; 1771248aee62SJacques Vidrine while (cur && cur->ai_next) 1772248aee62SJacques Vidrine cur = cur->ai_next; 1773248aee62SJacques Vidrine } 1774248aee62SJacques Vidrine } 1775248aee62SJacques Vidrine } 1776248aee62SJacques Vidrine 1777248aee62SJacques Vidrine /* ipnodes.byname can hold both IPv4/v6 */ 1778248aee62SJacques Vidrine r = yp_match(__ypdomain, "ipnodes.byname", name, 1779248aee62SJacques Vidrine (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1780248aee62SJacques Vidrine if (r == 0) { 1781248aee62SJacques Vidrine ai = _yphostent(__ypcurrent, pai); 1782248aee62SJacques Vidrine if (ai) { 1783248aee62SJacques Vidrine cur->ai_next = ai; 1784248aee62SJacques Vidrine while (cur && cur->ai_next) 1785248aee62SJacques Vidrine cur = cur->ai_next; 1786248aee62SJacques Vidrine } 1787248aee62SJacques Vidrine } 1788248aee62SJacques Vidrine 1789248aee62SJacques Vidrine if (sentinel.ai_next == NULL) { 1790248aee62SJacques Vidrine h_errno = HOST_NOT_FOUND; 1791248aee62SJacques Vidrine return NS_NOTFOUND; 1792248aee62SJacques Vidrine } 1793248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 1794248aee62SJacques Vidrine return NS_SUCCESS; 1795a42af91cSHajimu UMEMOTO } 1796a42af91cSHajimu UMEMOTO #endif 1797a42af91cSHajimu UMEMOTO 1798a42af91cSHajimu UMEMOTO /* resolver logic */ 1799a42af91cSHajimu UMEMOTO 1800c05ac53bSDavid E. O'Brien extern const char *__hostalias(const char *); 1801a42af91cSHajimu UMEMOTO extern int h_errno; 1802a42af91cSHajimu UMEMOTO 1803a42af91cSHajimu UMEMOTO /* 1804a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and await answer. 1805a42af91cSHajimu UMEMOTO * Returned answer is placed in supplied buffer "answer". 1806a42af91cSHajimu UMEMOTO * Perform preliminary check of answer, returning success only 1807a42af91cSHajimu UMEMOTO * if no error is indicated and the answer count is nonzero. 1808a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 1809a42af91cSHajimu UMEMOTO * Error number is left in h_errno. 1810a42af91cSHajimu UMEMOTO * 1811a42af91cSHajimu UMEMOTO * Caller must parse answer and determine whether it answers the question. 1812a42af91cSHajimu UMEMOTO */ 1813a42af91cSHajimu UMEMOTO static int 1814a42af91cSHajimu UMEMOTO res_queryN(name, target) 1815a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 1816a42af91cSHajimu UMEMOTO struct res_target *target; 1817a42af91cSHajimu UMEMOTO { 18185cd588cbSHajimu UMEMOTO u_char *buf; 1819a42af91cSHajimu UMEMOTO HEADER *hp; 1820a42af91cSHajimu UMEMOTO int n; 1821a42af91cSHajimu UMEMOTO struct res_target *t; 1822a42af91cSHajimu UMEMOTO int rcode; 1823a42af91cSHajimu UMEMOTO int ancount; 1824a42af91cSHajimu UMEMOTO 1825a42af91cSHajimu UMEMOTO rcode = NOERROR; 1826a42af91cSHajimu UMEMOTO ancount = 0; 1827a42af91cSHajimu UMEMOTO 1828a42af91cSHajimu UMEMOTO if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1829a42af91cSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 1830a42af91cSHajimu UMEMOTO return (-1); 1831a42af91cSHajimu UMEMOTO } 1832a42af91cSHajimu UMEMOTO 18335cd588cbSHajimu UMEMOTO buf = malloc(MAXPACKET); 18345cd588cbSHajimu UMEMOTO if (!buf) { 18355cd588cbSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 18365cd588cbSHajimu UMEMOTO return -1; 18375cd588cbSHajimu UMEMOTO } 18385cd588cbSHajimu UMEMOTO 1839a42af91cSHajimu UMEMOTO for (t = target; t; t = t->next) { 1840a42af91cSHajimu UMEMOTO int class, type; 1841a42af91cSHajimu UMEMOTO u_char *answer; 1842a42af91cSHajimu UMEMOTO int anslen; 1843a42af91cSHajimu UMEMOTO 1844a42af91cSHajimu UMEMOTO hp = (HEADER *)(void *)t->answer; 1845a42af91cSHajimu UMEMOTO hp->rcode = NOERROR; /* default */ 1846a42af91cSHajimu UMEMOTO 1847a42af91cSHajimu UMEMOTO /* make it easier... */ 1848b826397aSJun-ichiro itojun Hagino class = t->qclass; 1849b826397aSJun-ichiro itojun Hagino type = t->qtype; 1850a42af91cSHajimu UMEMOTO answer = t->answer; 1851a42af91cSHajimu UMEMOTO anslen = t->anslen; 1852a42af91cSHajimu UMEMOTO #ifdef DEBUG 1853a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1854a42af91cSHajimu UMEMOTO printf(";; res_query(%s, %d, %d)\n", name, class, type); 1855a42af91cSHajimu UMEMOTO #endif 1856a42af91cSHajimu UMEMOTO 1857a42af91cSHajimu UMEMOTO n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, 18585cd588cbSHajimu UMEMOTO buf, MAXPACKET); 1859ce6282dbSHajimu UMEMOTO if (n > 0 && (_res.options & RES_USE_EDNS0) != 0) 18605cd588cbSHajimu UMEMOTO n = res_opt(n, buf, MAXPACKET, anslen); 1861a42af91cSHajimu UMEMOTO if (n <= 0) { 1862a42af91cSHajimu UMEMOTO #ifdef DEBUG 1863a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1864a42af91cSHajimu UMEMOTO printf(";; res_query: mkquery failed\n"); 1865a42af91cSHajimu UMEMOTO #endif 18665cd588cbSHajimu UMEMOTO free(buf); 1867a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1868a42af91cSHajimu UMEMOTO return (n); 1869a42af91cSHajimu UMEMOTO } 1870a42af91cSHajimu UMEMOTO n = res_send(buf, n, answer, anslen); 1871a42af91cSHajimu UMEMOTO #if 0 1872a42af91cSHajimu UMEMOTO if (n < 0) { 1873a42af91cSHajimu UMEMOTO #ifdef DEBUG 1874a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1875a42af91cSHajimu UMEMOTO printf(";; res_query: send error\n"); 1876a42af91cSHajimu UMEMOTO #endif 18775cd588cbSHajimu UMEMOTO free(buf); 1878a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 1879a42af91cSHajimu UMEMOTO return (n); 1880a42af91cSHajimu UMEMOTO } 1881a42af91cSHajimu UMEMOTO #endif 1882a42af91cSHajimu UMEMOTO 188354384cf3SJacques Vidrine if (n < 0 || n > anslen) 188454384cf3SJacques Vidrine hp->rcode = FORMERR; /* XXX not very informative */ 188554384cf3SJacques Vidrine if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 1886a42af91cSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */ 1887a42af91cSHajimu UMEMOTO #ifdef DEBUG 1888a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 1889dd521ef1SHajimu UMEMOTO printf(";; rcode = %u, ancount=%u\n", hp->rcode, 1890a42af91cSHajimu UMEMOTO ntohs(hp->ancount)); 1891a42af91cSHajimu UMEMOTO #endif 1892a42af91cSHajimu UMEMOTO continue; 1893a42af91cSHajimu UMEMOTO } 1894a42af91cSHajimu UMEMOTO 1895a42af91cSHajimu UMEMOTO ancount += ntohs(hp->ancount); 1896a42af91cSHajimu UMEMOTO 1897a42af91cSHajimu UMEMOTO t->n = n; 1898a42af91cSHajimu UMEMOTO } 1899a42af91cSHajimu UMEMOTO 19005cd588cbSHajimu UMEMOTO free(buf); 19015cd588cbSHajimu UMEMOTO 1902a42af91cSHajimu UMEMOTO if (ancount == 0) { 1903a42af91cSHajimu UMEMOTO switch (rcode) { 1904a42af91cSHajimu UMEMOTO case NXDOMAIN: 1905a42af91cSHajimu UMEMOTO h_errno = HOST_NOT_FOUND; 1906a42af91cSHajimu UMEMOTO break; 1907a42af91cSHajimu UMEMOTO case SERVFAIL: 1908a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 1909a42af91cSHajimu UMEMOTO break; 1910a42af91cSHajimu UMEMOTO case NOERROR: 1911a42af91cSHajimu UMEMOTO h_errno = NO_DATA; 1912a42af91cSHajimu UMEMOTO break; 1913a42af91cSHajimu UMEMOTO case FORMERR: 1914a42af91cSHajimu UMEMOTO case NOTIMP: 1915a42af91cSHajimu UMEMOTO case REFUSED: 1916a42af91cSHajimu UMEMOTO default: 1917a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 1918a42af91cSHajimu UMEMOTO break; 1919a42af91cSHajimu UMEMOTO } 1920a42af91cSHajimu UMEMOTO return (-1); 1921a42af91cSHajimu UMEMOTO } 1922a42af91cSHajimu UMEMOTO return (ancount); 1923a42af91cSHajimu UMEMOTO } 1924a42af91cSHajimu UMEMOTO 1925a42af91cSHajimu UMEMOTO /* 1926a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and retrieve answer in supplied buffer. 1927a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 1928a42af91cSHajimu UMEMOTO * If enabled, implement search rules until answer or unrecoverable failure 1929a42af91cSHajimu UMEMOTO * is detected. Error code, if any, is left in h_errno. 1930a42af91cSHajimu UMEMOTO */ 1931a42af91cSHajimu UMEMOTO static int 1932a42af91cSHajimu UMEMOTO res_searchN(name, target) 1933a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 1934a42af91cSHajimu UMEMOTO struct res_target *target; 1935a42af91cSHajimu UMEMOTO { 1936a42af91cSHajimu UMEMOTO const char *cp, * const *domain; 1937a42af91cSHajimu UMEMOTO HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ 1938a42af91cSHajimu UMEMOTO u_int dots; 1939a42af91cSHajimu UMEMOTO int trailing_dot, ret, saved_herrno; 1940a42af91cSHajimu UMEMOTO int got_nodata = 0, got_servfail = 0, tried_as_is = 0; 1941a42af91cSHajimu UMEMOTO 1942a42af91cSHajimu UMEMOTO if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1943a42af91cSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 1944a42af91cSHajimu UMEMOTO return (-1); 1945a42af91cSHajimu UMEMOTO } 1946a42af91cSHajimu UMEMOTO 1947a42af91cSHajimu UMEMOTO errno = 0; 1948a42af91cSHajimu UMEMOTO h_errno = HOST_NOT_FOUND; /* default, if we never query */ 1949a42af91cSHajimu UMEMOTO dots = 0; 1950a42af91cSHajimu UMEMOTO for (cp = name; *cp; cp++) 1951a42af91cSHajimu UMEMOTO dots += (*cp == '.'); 1952a42af91cSHajimu UMEMOTO trailing_dot = 0; 1953a42af91cSHajimu UMEMOTO if (cp > name && *--cp == '.') 1954a42af91cSHajimu UMEMOTO trailing_dot++; 1955a42af91cSHajimu UMEMOTO 1956a42af91cSHajimu UMEMOTO /* 1957a42af91cSHajimu UMEMOTO * if there aren't any dots, it could be a user-level alias 1958a42af91cSHajimu UMEMOTO */ 1959a42af91cSHajimu UMEMOTO if (!dots && (cp = __hostalias(name)) != NULL) 1960a42af91cSHajimu UMEMOTO return (res_queryN(cp, target)); 1961a42af91cSHajimu UMEMOTO 1962a42af91cSHajimu UMEMOTO /* 1963a42af91cSHajimu UMEMOTO * If there are dots in the name already, let's just give it a try 1964a42af91cSHajimu UMEMOTO * 'as is'. The threshold can be set with the "ndots" option. 1965a42af91cSHajimu UMEMOTO */ 1966a42af91cSHajimu UMEMOTO saved_herrno = -1; 1967a42af91cSHajimu UMEMOTO if (dots >= _res.ndots) { 1968a42af91cSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target); 1969a42af91cSHajimu UMEMOTO if (ret > 0) 1970a42af91cSHajimu UMEMOTO return (ret); 1971a42af91cSHajimu UMEMOTO saved_herrno = h_errno; 1972a42af91cSHajimu UMEMOTO tried_as_is++; 1973a42af91cSHajimu UMEMOTO } 1974a42af91cSHajimu UMEMOTO 1975a42af91cSHajimu UMEMOTO /* 1976a42af91cSHajimu UMEMOTO * We do at least one level of search if 1977a42af91cSHajimu UMEMOTO * - there is no dot and RES_DEFNAME is set, or 1978a42af91cSHajimu UMEMOTO * - there is at least one dot, there is no trailing dot, 1979a42af91cSHajimu UMEMOTO * and RES_DNSRCH is set. 1980a42af91cSHajimu UMEMOTO */ 1981a42af91cSHajimu UMEMOTO if ((!dots && (_res.options & RES_DEFNAMES)) || 1982a42af91cSHajimu UMEMOTO (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { 1983a42af91cSHajimu UMEMOTO int done = 0; 1984a42af91cSHajimu UMEMOTO 1985a42af91cSHajimu UMEMOTO for (domain = (const char * const *)_res.dnsrch; 1986a42af91cSHajimu UMEMOTO *domain && !done; 1987a42af91cSHajimu UMEMOTO domain++) { 1988a42af91cSHajimu UMEMOTO 1989a42af91cSHajimu UMEMOTO ret = res_querydomainN(name, *domain, target); 1990a42af91cSHajimu UMEMOTO if (ret > 0) 1991a42af91cSHajimu UMEMOTO return (ret); 1992a42af91cSHajimu UMEMOTO 1993a42af91cSHajimu UMEMOTO /* 1994a42af91cSHajimu UMEMOTO * If no server present, give up. 1995a42af91cSHajimu UMEMOTO * If name isn't found in this domain, 1996a42af91cSHajimu UMEMOTO * keep trying higher domains in the search list 1997a42af91cSHajimu UMEMOTO * (if that's enabled). 1998a42af91cSHajimu UMEMOTO * On a NO_DATA error, keep trying, otherwise 1999a42af91cSHajimu UMEMOTO * a wildcard entry of another type could keep us 2000a42af91cSHajimu UMEMOTO * from finding this entry higher in the domain. 2001a42af91cSHajimu UMEMOTO * If we get some other error (negative answer or 2002a42af91cSHajimu UMEMOTO * server failure), then stop searching up, 2003a42af91cSHajimu UMEMOTO * but try the input name below in case it's 2004a42af91cSHajimu UMEMOTO * fully-qualified. 2005a42af91cSHajimu UMEMOTO */ 2006a42af91cSHajimu UMEMOTO if (errno == ECONNREFUSED) { 2007a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 2008a42af91cSHajimu UMEMOTO return (-1); 2009a42af91cSHajimu UMEMOTO } 2010a42af91cSHajimu UMEMOTO 2011a42af91cSHajimu UMEMOTO switch (h_errno) { 2012a42af91cSHajimu UMEMOTO case NO_DATA: 2013a42af91cSHajimu UMEMOTO got_nodata++; 2014a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 2015a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 2016a42af91cSHajimu UMEMOTO /* keep trying */ 2017a42af91cSHajimu UMEMOTO break; 2018a42af91cSHajimu UMEMOTO case TRY_AGAIN: 2019a42af91cSHajimu UMEMOTO if (hp->rcode == SERVFAIL) { 2020a42af91cSHajimu UMEMOTO /* try next search element, if any */ 2021a42af91cSHajimu UMEMOTO got_servfail++; 2022a42af91cSHajimu UMEMOTO break; 2023a42af91cSHajimu UMEMOTO } 2024a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 2025a42af91cSHajimu UMEMOTO default: 2026a42af91cSHajimu UMEMOTO /* anything else implies that we're done */ 2027a42af91cSHajimu UMEMOTO done++; 2028a42af91cSHajimu UMEMOTO } 2029a42af91cSHajimu UMEMOTO /* 2030a42af91cSHajimu UMEMOTO * if we got here for some reason other than DNSRCH, 2031a42af91cSHajimu UMEMOTO * we only wanted one iteration of the loop, so stop. 2032a42af91cSHajimu UMEMOTO */ 2033a42af91cSHajimu UMEMOTO if (!(_res.options & RES_DNSRCH)) 2034a42af91cSHajimu UMEMOTO done++; 2035a42af91cSHajimu UMEMOTO } 2036a42af91cSHajimu UMEMOTO } 2037a42af91cSHajimu UMEMOTO 2038a42af91cSHajimu UMEMOTO /* 2039a42af91cSHajimu UMEMOTO * if we have not already tried the name "as is", do that now. 2040a42af91cSHajimu UMEMOTO * note that we do this regardless of how many dots were in the 2041a42af91cSHajimu UMEMOTO * name or whether it ends with a dot. 2042a42af91cSHajimu UMEMOTO */ 2043a42af91cSHajimu UMEMOTO if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) { 2044a42af91cSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target); 2045a42af91cSHajimu UMEMOTO if (ret > 0) 2046a42af91cSHajimu UMEMOTO return (ret); 2047a42af91cSHajimu UMEMOTO } 2048a42af91cSHajimu UMEMOTO 2049a42af91cSHajimu UMEMOTO /* 2050a42af91cSHajimu UMEMOTO * if we got here, we didn't satisfy the search. 2051a42af91cSHajimu UMEMOTO * if we did an initial full query, return that query's h_errno 2052a42af91cSHajimu UMEMOTO * (note that we wouldn't be here if that query had succeeded). 2053a42af91cSHajimu UMEMOTO * else if we ever got a nodata, send that back as the reason. 2054a42af91cSHajimu UMEMOTO * else send back meaningless h_errno, that being the one from 2055a42af91cSHajimu UMEMOTO * the last DNSRCH we did. 2056a42af91cSHajimu UMEMOTO */ 2057a42af91cSHajimu UMEMOTO if (saved_herrno != -1) 2058a42af91cSHajimu UMEMOTO h_errno = saved_herrno; 2059a42af91cSHajimu UMEMOTO else if (got_nodata) 2060a42af91cSHajimu UMEMOTO h_errno = NO_DATA; 2061a42af91cSHajimu UMEMOTO else if (got_servfail) 2062a42af91cSHajimu UMEMOTO h_errno = TRY_AGAIN; 2063a42af91cSHajimu UMEMOTO return (-1); 2064a42af91cSHajimu UMEMOTO } 2065a42af91cSHajimu UMEMOTO 2066a42af91cSHajimu UMEMOTO /* 2067a42af91cSHajimu UMEMOTO * Perform a call on res_query on the concatenation of name and domain, 2068a42af91cSHajimu UMEMOTO * removing a trailing dot from name if domain is NULL. 2069a42af91cSHajimu UMEMOTO */ 2070a42af91cSHajimu UMEMOTO static int 2071a42af91cSHajimu UMEMOTO res_querydomainN(name, domain, target) 2072a42af91cSHajimu UMEMOTO const char *name, *domain; 2073a42af91cSHajimu UMEMOTO struct res_target *target; 2074a42af91cSHajimu UMEMOTO { 2075a42af91cSHajimu UMEMOTO char nbuf[MAXDNAME]; 2076a42af91cSHajimu UMEMOTO const char *longname = nbuf; 2077a42af91cSHajimu UMEMOTO size_t n, d; 2078a42af91cSHajimu UMEMOTO 2079a42af91cSHajimu UMEMOTO if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 2080a42af91cSHajimu UMEMOTO h_errno = NETDB_INTERNAL; 2081a42af91cSHajimu UMEMOTO return (-1); 2082a42af91cSHajimu UMEMOTO } 2083a42af91cSHajimu UMEMOTO #ifdef DEBUG 2084a42af91cSHajimu UMEMOTO if (_res.options & RES_DEBUG) 2085a42af91cSHajimu UMEMOTO printf(";; res_querydomain(%s, %s)\n", 2086a42af91cSHajimu UMEMOTO name, domain?domain:"<Nil>"); 2087a42af91cSHajimu UMEMOTO #endif 2088a42af91cSHajimu UMEMOTO if (domain == NULL) { 2089a42af91cSHajimu UMEMOTO /* 2090a42af91cSHajimu UMEMOTO * Check for trailing '.'; 2091a42af91cSHajimu UMEMOTO * copy without '.' if present. 2092a42af91cSHajimu UMEMOTO */ 2093a42af91cSHajimu UMEMOTO n = strlen(name); 2094a42af91cSHajimu UMEMOTO if (n >= MAXDNAME) { 2095a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 2096a42af91cSHajimu UMEMOTO return (-1); 2097a42af91cSHajimu UMEMOTO } 2098a42af91cSHajimu UMEMOTO if (n > 0 && name[--n] == '.') { 2099a42af91cSHajimu UMEMOTO strncpy(nbuf, name, n); 2100a42af91cSHajimu UMEMOTO nbuf[n] = '\0'; 2101a42af91cSHajimu UMEMOTO } else 2102a42af91cSHajimu UMEMOTO longname = name; 2103a42af91cSHajimu UMEMOTO } else { 2104a42af91cSHajimu UMEMOTO n = strlen(name); 2105a42af91cSHajimu UMEMOTO d = strlen(domain); 2106a42af91cSHajimu UMEMOTO if (n + d + 1 >= MAXDNAME) { 2107a42af91cSHajimu UMEMOTO h_errno = NO_RECOVERY; 2108a42af91cSHajimu UMEMOTO return (-1); 2109a42af91cSHajimu UMEMOTO } 2110dd521ef1SHajimu UMEMOTO snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); 2111a42af91cSHajimu UMEMOTO } 2112a42af91cSHajimu UMEMOTO return (res_queryN(longname, target)); 2113a42af91cSHajimu UMEMOTO } 2114