1bf838688SHajimu UMEMOTO /* $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 * - Return values. There are nonstandard return values defined and used 377d56d374SYoshinobu Inoue * in the source code. This is because RFC2553 is silent about which error 387d56d374SYoshinobu Inoue * code must be returned for which situation. 39a42af91cSHajimu UMEMOTO * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is 40dd521ef1SHajimu UMEMOTO * invalid. current code - SEGV on freeaddrinfo(NULL) 41dd521ef1SHajimu UMEMOTO * 4200a8a579SYoshinobu Inoue * Note: 4300a8a579SYoshinobu Inoue * - The code filters out AFs that are not supported by the kernel, 4400a8a579SYoshinobu Inoue * when globbing NULL hostname (to loopback, or wildcard). Is it the right 4500a8a579SYoshinobu Inoue * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 4600a8a579SYoshinobu Inoue * in ai_flags? 47a42af91cSHajimu UMEMOTO * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. 48a42af91cSHajimu UMEMOTO * (1) what should we do against numeric hostname (2) what should we do 49a42af91cSHajimu UMEMOTO * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? 50a42af91cSHajimu UMEMOTO * non-loopback address configured? global address configured? 51dd521ef1SHajimu UMEMOTO * 52dd521ef1SHajimu UMEMOTO * OS specific notes for freebsd4: 53dd521ef1SHajimu UMEMOTO * - FreeBSD supported $GAI. The code does not. 547d56d374SYoshinobu Inoue */ 557d56d374SYoshinobu Inoue 56333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 57333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 58333fc21eSDavid E. O'Brien 59d201fe46SDaniel Eischen #include "namespace.h" 607d56d374SYoshinobu Inoue #include <sys/types.h> 617d56d374SYoshinobu Inoue #include <sys/param.h> 627d56d374SYoshinobu Inoue #include <sys/socket.h> 637d56d374SYoshinobu Inoue #include <net/if.h> 647d56d374SYoshinobu Inoue #include <netinet/in.h> 65*6f63a114SHajimu UMEMOTO #include <net/if_types.h> 66*6f63a114SHajimu UMEMOTO #include <ifaddrs.h> 674c6867a8SHajimu UMEMOTO #include <sys/queue.h> 684c6867a8SHajimu UMEMOTO #ifdef INET6 694c6867a8SHajimu UMEMOTO #include <net/if_var.h> 704c6867a8SHajimu UMEMOTO #include <sys/sysctl.h> 714d489f47SHajimu UMEMOTO #include <sys/ioctl.h> 72*6f63a114SHajimu UMEMOTO #include <netinet6/in6_var.h> 73*6f63a114SHajimu UMEMOTO #include <netinet6/nd6.h> 744c6867a8SHajimu UMEMOTO #endif 757d56d374SYoshinobu Inoue #include <arpa/inet.h> 767d56d374SYoshinobu Inoue #include <arpa/nameser.h> 77bf838688SHajimu UMEMOTO #include <rpc/rpc.h> 78bf838688SHajimu UMEMOTO #include <rpcsvc/yp_prot.h> 79bf838688SHajimu UMEMOTO #include <rpcsvc/ypclnt.h> 807d56d374SYoshinobu Inoue #include <netdb.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" 10006a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 10106a99fe3SHajimu UMEMOTO #include "nscache.h" 10206a99fe3SHajimu UMEMOTO #endif 103248aee62SJacques Vidrine 1047d56d374SYoshinobu Inoue #if defined(__KAME__) && defined(INET6) 1057d56d374SYoshinobu Inoue # define FAITH 1067d56d374SYoshinobu Inoue #endif 1077d56d374SYoshinobu Inoue 1087d56d374SYoshinobu Inoue #define ANY 0 1097d56d374SYoshinobu Inoue #define YES 1 1107d56d374SYoshinobu Inoue #define NO 0 1117d56d374SYoshinobu Inoue 1127d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 }; 113dd521ef1SHajimu UMEMOTO static const char in_loopback[] = { 127, 0, 0, 1 }; 114dd521ef1SHajimu UMEMOTO #ifdef INET6 1157d56d374SYoshinobu Inoue static const char in6_addrany[] = { 1167d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 1177d56d374SYoshinobu Inoue }; 1187d56d374SYoshinobu Inoue static const char in6_loopback[] = { 1197d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 1207d56d374SYoshinobu Inoue }; 121dd521ef1SHajimu UMEMOTO #endif 1227d56d374SYoshinobu Inoue 1234c6867a8SHajimu UMEMOTO struct policyqueue { 1244c6867a8SHajimu UMEMOTO TAILQ_ENTRY(policyqueue) pc_entry; 1254c6867a8SHajimu UMEMOTO #ifdef INET6 1264c6867a8SHajimu UMEMOTO struct in6_addrpolicy pc_policy; 1274c6867a8SHajimu UMEMOTO #endif 1284c6867a8SHajimu UMEMOTO }; 1294c6867a8SHajimu UMEMOTO TAILQ_HEAD(policyhead, policyqueue); 1304c6867a8SHajimu UMEMOTO 1317d56d374SYoshinobu Inoue static const struct afd { 1327d56d374SYoshinobu Inoue int a_af; 1337d56d374SYoshinobu Inoue int a_addrlen; 134036ae3ddSHajimu UMEMOTO socklen_t a_socklen; 1357d56d374SYoshinobu Inoue int a_off; 1367d56d374SYoshinobu Inoue const char *a_addrany; 1377d56d374SYoshinobu Inoue const char *a_loopback; 1387d56d374SYoshinobu Inoue int a_scoped; 1397d56d374SYoshinobu Inoue } afdl [] = { 1407d56d374SYoshinobu Inoue #ifdef INET6 1417d56d374SYoshinobu Inoue #define N_INET6 0 1427d56d374SYoshinobu Inoue {PF_INET6, sizeof(struct in6_addr), 1437d56d374SYoshinobu Inoue sizeof(struct sockaddr_in6), 1447d56d374SYoshinobu Inoue offsetof(struct sockaddr_in6, sin6_addr), 1457d56d374SYoshinobu Inoue in6_addrany, in6_loopback, 1}, 1467d56d374SYoshinobu Inoue #define N_INET 1 1477d56d374SYoshinobu Inoue #else 1487d56d374SYoshinobu Inoue #define N_INET 0 1497d56d374SYoshinobu Inoue #endif 1507d56d374SYoshinobu Inoue {PF_INET, sizeof(struct in_addr), 1517d56d374SYoshinobu Inoue sizeof(struct sockaddr_in), 1527d56d374SYoshinobu Inoue offsetof(struct sockaddr_in, sin_addr), 1537d56d374SYoshinobu Inoue in_addrany, in_loopback, 0}, 1547d56d374SYoshinobu Inoue {0, 0, 0, 0, NULL, NULL, 0}, 1557d56d374SYoshinobu Inoue }; 1567d56d374SYoshinobu Inoue 1577d56d374SYoshinobu Inoue struct explore { 158a42af91cSHajimu UMEMOTO int e_af; 1597d56d374SYoshinobu Inoue int e_socktype; 1607d56d374SYoshinobu Inoue int e_protocol; 1617d56d374SYoshinobu Inoue int e_wild; 1627d56d374SYoshinobu Inoue #define WILD_AF(ex) ((ex)->e_wild & 0x01) 1637d56d374SYoshinobu Inoue #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 1647d56d374SYoshinobu Inoue #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 1657d56d374SYoshinobu Inoue }; 1667d56d374SYoshinobu Inoue 1677d56d374SYoshinobu Inoue static const struct explore explore[] = { 168a42af91cSHajimu UMEMOTO #if 0 1692bd56eacSJilles Tjoelker { PF_LOCAL, ANY, ANY, 0x01 }, 170a42af91cSHajimu UMEMOTO #endif 171a42af91cSHajimu UMEMOTO #ifdef INET6 1722bd56eacSJilles Tjoelker { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07 }, 1732bd56eacSJilles Tjoelker { PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07 }, 1742bd56eacSJilles Tjoelker { PF_INET6, SOCK_STREAM, IPPROTO_SCTP, 0x03 }, 1752bd56eacSJilles Tjoelker { PF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, 0x07 }, 176e06e816fSKevin Lo { PF_INET6, SOCK_DGRAM, IPPROTO_UDPLITE, 0x03 }, 1772bd56eacSJilles Tjoelker { PF_INET6, SOCK_RAW, ANY, 0x05 }, 178a42af91cSHajimu UMEMOTO #endif 1792bd56eacSJilles Tjoelker { PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07 }, 1802bd56eacSJilles Tjoelker { PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07 }, 1812bd56eacSJilles Tjoelker { PF_INET, SOCK_STREAM, IPPROTO_SCTP, 0x03 }, 1822bd56eacSJilles Tjoelker { PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP, 0x07 }, 183e06e816fSKevin Lo { PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE, 0x03 }, 1842bd56eacSJilles Tjoelker { PF_INET, SOCK_RAW, ANY, 0x05 }, 1852bd56eacSJilles Tjoelker { -1, 0, 0, 0 }, 1867d56d374SYoshinobu Inoue }; 1877d56d374SYoshinobu Inoue 1887d56d374SYoshinobu Inoue #ifdef INET6 1897d56d374SYoshinobu Inoue #define PTON_MAX 16 1907d56d374SYoshinobu Inoue #else 1917d56d374SYoshinobu Inoue #define PTON_MAX 4 1927d56d374SYoshinobu Inoue #endif 1937d56d374SYoshinobu Inoue 1944c6867a8SHajimu UMEMOTO #define AIO_SRCFLAG_DEPRECATED 0x1 1954c6867a8SHajimu UMEMOTO 1964c6867a8SHajimu UMEMOTO struct ai_order { 1974c6867a8SHajimu UMEMOTO union { 1984c6867a8SHajimu UMEMOTO struct sockaddr_storage aiou_ss; 1994c6867a8SHajimu UMEMOTO struct sockaddr aiou_sa; 2004c6867a8SHajimu UMEMOTO } aio_src_un; 2014c6867a8SHajimu UMEMOTO #define aio_srcsa aio_src_un.aiou_sa 2024c6867a8SHajimu UMEMOTO u_int32_t aio_srcflag; 2034c6867a8SHajimu UMEMOTO int aio_srcscope; 2044c6867a8SHajimu UMEMOTO int aio_dstscope; 2054c6867a8SHajimu UMEMOTO struct policyqueue *aio_srcpolicy; 2064c6867a8SHajimu UMEMOTO struct policyqueue *aio_dstpolicy; 2074c6867a8SHajimu UMEMOTO struct addrinfo *aio_ai; 2084c6867a8SHajimu UMEMOTO int aio_matchlen; 2094c6867a8SHajimu UMEMOTO }; 2104c6867a8SHajimu UMEMOTO 211248aee62SJacques Vidrine static const ns_src default_dns_files[] = { 212248aee62SJacques Vidrine { NSSRC_FILES, NS_SUCCESS }, 213248aee62SJacques Vidrine { NSSRC_DNS, NS_SUCCESS }, 214248aee62SJacques Vidrine { 0 } 215248aee62SJacques Vidrine }; 216248aee62SJacques Vidrine 217a42af91cSHajimu UMEMOTO struct res_target { 218a42af91cSHajimu UMEMOTO struct res_target *next; 219a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 220b826397aSJun-ichiro itojun Hagino int qclass, qtype; /* class and type of query */ 221a42af91cSHajimu UMEMOTO u_char *answer; /* buffer to put answer */ 222a42af91cSHajimu UMEMOTO int anslen; /* size of answer buffer */ 223a42af91cSHajimu UMEMOTO int n; /* result length */ 224a42af91cSHajimu UMEMOTO }; 225a42af91cSHajimu UMEMOTO 226688a5c3eSHajimu UMEMOTO #define MAXPACKET (64*1024) 227688a5c3eSHajimu UMEMOTO 228688a5c3eSHajimu UMEMOTO typedef union { 229688a5c3eSHajimu UMEMOTO HEADER hdr; 230688a5c3eSHajimu UMEMOTO u_char buf[MAXPACKET]; 231688a5c3eSHajimu UMEMOTO } querybuf; 232688a5c3eSHajimu UMEMOTO 233e24e9d99SHajimu UMEMOTO static int str2number(const char *, int *); 234bc421492SHajimu UMEMOTO static int explore_copy(const struct addrinfo *, const struct addrinfo *, 235bc421492SHajimu UMEMOTO struct addrinfo **); 2361372519bSDavid E. O'Brien static int explore_null(const struct addrinfo *, 2371372519bSDavid E. O'Brien const char *, struct addrinfo **); 2381372519bSDavid E. O'Brien static int explore_numeric(const struct addrinfo *, const char *, 2398755b918SHajimu UMEMOTO const char *, struct addrinfo **, const char *); 2401372519bSDavid E. O'Brien static int explore_numeric_scope(const struct addrinfo *, const char *, 2411372519bSDavid E. O'Brien const char *, struct addrinfo **); 2421372519bSDavid E. O'Brien static int get_canonname(const struct addrinfo *, 2431372519bSDavid E. O'Brien struct addrinfo *, const char *); 2441372519bSDavid E. O'Brien static struct addrinfo *get_ai(const struct addrinfo *, 2451372519bSDavid E. O'Brien const struct afd *, const char *); 246bc421492SHajimu UMEMOTO static struct addrinfo *copy_ai(const struct addrinfo *); 247c05ac53bSDavid E. O'Brien static int get_portmatch(const struct addrinfo *, const char *); 248c05ac53bSDavid E. O'Brien static int get_port(struct addrinfo *, const char *, int); 249c05ac53bSDavid E. O'Brien static const struct afd *find_afd(int); 250bf838688SHajimu UMEMOTO static int addrconfig(struct addrinfo *); 251*6f63a114SHajimu UMEMOTO #ifdef INET6 252*6f63a114SHajimu UMEMOTO static int is_ifdisabled(char *); 253*6f63a114SHajimu UMEMOTO #endif 2544d489f47SHajimu UMEMOTO static void set_source(struct ai_order *, struct policyhead *); 2554c6867a8SHajimu UMEMOTO static int comp_dst(const void *, const void *); 256a42af91cSHajimu UMEMOTO #ifdef INET6 257a89c1d30SHajimu UMEMOTO static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); 258a42af91cSHajimu UMEMOTO #endif 2594c6867a8SHajimu UMEMOTO static int gai_addr2scopetype(struct sockaddr *); 260a42af91cSHajimu UMEMOTO 261688a5c3eSHajimu UMEMOTO static int explore_fqdn(const struct addrinfo *, const char *, 262688a5c3eSHajimu UMEMOTO const char *, struct addrinfo **); 263688a5c3eSHajimu UMEMOTO 2644c6867a8SHajimu UMEMOTO static int reorder(struct addrinfo *); 2654c6867a8SHajimu UMEMOTO static int get_addrselectpolicy(struct policyhead *); 2664c6867a8SHajimu UMEMOTO static void free_addrselectpolicy(struct policyhead *); 2674c6867a8SHajimu UMEMOTO static struct policyqueue *match_addrselectpolicy(struct sockaddr *, 2684c6867a8SHajimu UMEMOTO struct policyhead *); 2694d489f47SHajimu UMEMOTO static int matchlen(struct sockaddr *, struct sockaddr *); 2704c6867a8SHajimu UMEMOTO 2711372519bSDavid E. O'Brien static struct addrinfo *getanswer(const querybuf *, int, const char *, int, 2725342d17fSHajimu UMEMOTO const struct addrinfo *, res_state); 273688a5c3eSHajimu UMEMOTO #if defined(RESOLVSORT) 2745342d17fSHajimu UMEMOTO static int addr4sort(struct addrinfo *, res_state); 275688a5c3eSHajimu UMEMOTO #endif 276a89c1d30SHajimu UMEMOTO static int _dns_getaddrinfo(void *, void *, va_list); 2779def31dcSHajimu UMEMOTO static void _sethtent(FILE **); 2789def31dcSHajimu UMEMOTO static void _endhtent(FILE **); 2799def31dcSHajimu UMEMOTO static struct addrinfo *_gethtent(FILE **, const char *, 2809def31dcSHajimu UMEMOTO const struct addrinfo *); 281c05ac53bSDavid E. O'Brien static int _files_getaddrinfo(void *, void *, va_list); 282a42af91cSHajimu UMEMOTO #ifdef YP 283c05ac53bSDavid E. O'Brien static struct addrinfo *_yphostent(char *, const struct addrinfo *); 284c05ac53bSDavid E. O'Brien static int _yp_getaddrinfo(void *, void *, va_list); 285a42af91cSHajimu UMEMOTO #endif 28606a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 28706a99fe3SHajimu UMEMOTO static int addrinfo_id_func(char *, size_t *, va_list, void *); 28806a99fe3SHajimu UMEMOTO static int addrinfo_marshal_func(char *, size_t *, void *, va_list, void *); 28906a99fe3SHajimu UMEMOTO static int addrinfo_unmarshal_func(char *, size_t, void *, va_list, void *); 29006a99fe3SHajimu UMEMOTO #endif 291a42af91cSHajimu UMEMOTO 2925342d17fSHajimu UMEMOTO static int res_queryN(const char *, struct res_target *, res_state); 2935342d17fSHajimu UMEMOTO static int res_searchN(const char *, struct res_target *, res_state); 2941372519bSDavid E. O'Brien static int res_querydomainN(const char *, const char *, 2955342d17fSHajimu UMEMOTO struct res_target *, res_state); 296a42af91cSHajimu UMEMOTO 2977d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */ 2987d56d374SYoshinobu Inoue 2997d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \ 3007d56d374SYoshinobu Inoue do { \ 3017d56d374SYoshinobu Inoue /* external reference: pai, error, and label free */ \ 3027d56d374SYoshinobu Inoue (ai) = get_ai(pai, (afd), (addr)); \ 3037d56d374SYoshinobu Inoue if ((ai) == NULL) { \ 3047d56d374SYoshinobu Inoue error = EAI_MEMORY; \ 3057d56d374SYoshinobu Inoue goto free; \ 3067d56d374SYoshinobu Inoue } \ 307a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3087d56d374SYoshinobu Inoue 3097d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \ 3107d56d374SYoshinobu Inoue do { \ 3117d56d374SYoshinobu Inoue /* external reference: error and label free */ \ 3127d56d374SYoshinobu Inoue error = get_port((ai), (serv), 0); \ 3137d56d374SYoshinobu Inoue if (error != 0) \ 3147d56d374SYoshinobu Inoue goto free; \ 315a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3167d56d374SYoshinobu Inoue 3177d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \ 3187d56d374SYoshinobu Inoue do { \ 3197d56d374SYoshinobu Inoue /* external reference: pai, error and label free */ \ 3207d56d374SYoshinobu Inoue error = get_canonname(pai, (ai), (str)); \ 3217d56d374SYoshinobu Inoue if (error != 0) \ 3227d56d374SYoshinobu Inoue goto free; \ 323a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3247d56d374SYoshinobu Inoue 3257d56d374SYoshinobu Inoue #define ERR(err) \ 3267d56d374SYoshinobu Inoue do { \ 3277d56d374SYoshinobu Inoue /* external reference: error, and label bad */ \ 3287d56d374SYoshinobu Inoue error = (err); \ 3297d56d374SYoshinobu Inoue goto bad; \ 330a42af91cSHajimu UMEMOTO /*NOTREACHED*/ \ 331a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3327d56d374SYoshinobu Inoue 3337d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \ 334a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 3357d56d374SYoshinobu Inoue #define MATCH(x, y, w) \ 336a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 3377d56d374SYoshinobu Inoue 3387d56d374SYoshinobu Inoue void 33990d1d883SHajimu UMEMOTO freeaddrinfo(struct addrinfo *ai) 3407d56d374SYoshinobu Inoue { 3417d56d374SYoshinobu Inoue struct addrinfo *next; 3427d56d374SYoshinobu Inoue 3437d56d374SYoshinobu Inoue do { 3447d56d374SYoshinobu Inoue next = ai->ai_next; 3457d56d374SYoshinobu Inoue if (ai->ai_canonname) 3467d56d374SYoshinobu Inoue free(ai->ai_canonname); 3477d56d374SYoshinobu Inoue /* no need to free(ai->ai_addr) */ 3487d56d374SYoshinobu Inoue free(ai); 349a42af91cSHajimu UMEMOTO ai = next; 350a42af91cSHajimu UMEMOTO } while (ai); 3517d56d374SYoshinobu Inoue } 3527d56d374SYoshinobu Inoue 3537d56d374SYoshinobu Inoue static int 354e24e9d99SHajimu UMEMOTO str2number(const char *p, int *portp) 3557d56d374SYoshinobu Inoue { 356ec20fe00SJun-ichiro itojun Hagino char *ep; 357c224435eSHajimu UMEMOTO unsigned long v; 358ec20fe00SJun-ichiro itojun Hagino 359ec20fe00SJun-ichiro itojun Hagino if (*p == '\0') 360c224435eSHajimu UMEMOTO return -1; 361ec20fe00SJun-ichiro itojun Hagino ep = NULL; 362a89c1d30SHajimu UMEMOTO errno = 0; 363c224435eSHajimu UMEMOTO v = strtoul(p, &ep, 10); 364e24e9d99SHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) { 365e24e9d99SHajimu UMEMOTO *portp = v; 366e24e9d99SHajimu UMEMOTO return 0; 367e24e9d99SHajimu UMEMOTO } else 368c224435eSHajimu UMEMOTO return -1; 3697d56d374SYoshinobu Inoue } 3707d56d374SYoshinobu Inoue 3717d56d374SYoshinobu Inoue int 37290d1d883SHajimu UMEMOTO getaddrinfo(const char *hostname, const char *servname, 37390d1d883SHajimu UMEMOTO const struct addrinfo *hints, struct addrinfo **res) 3747d56d374SYoshinobu Inoue { 3757d56d374SYoshinobu Inoue struct addrinfo sentinel; 3767d56d374SYoshinobu Inoue struct addrinfo *cur; 3777d56d374SYoshinobu Inoue int error = 0; 378bc421492SHajimu UMEMOTO struct addrinfo ai, ai0, *afai; 3797d56d374SYoshinobu Inoue struct addrinfo *pai; 380bc421492SHajimu UMEMOTO const struct afd *afd; 3817d56d374SYoshinobu Inoue const struct explore *ex; 382bc421492SHajimu UMEMOTO struct addrinfo *afailist[sizeof(afdl)/sizeof(afdl[0])]; 383bc421492SHajimu UMEMOTO struct addrinfo *afai_unspec; 384bc421492SHajimu UMEMOTO int found; 3854c6867a8SHajimu UMEMOTO int numeric = 0; 3867d56d374SYoshinobu Inoue 387bc421492SHajimu UMEMOTO /* ensure we return NULL on errors */ 388bc421492SHajimu UMEMOTO *res = NULL; 389bc421492SHajimu UMEMOTO 390bc421492SHajimu UMEMOTO memset(&ai, 0, sizeof(ai)); 391bc421492SHajimu UMEMOTO 392bc421492SHajimu UMEMOTO memset(afailist, 0, sizeof(afailist)); 393bc421492SHajimu UMEMOTO afai_unspec = NULL; 394bc421492SHajimu UMEMOTO 395a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 3967d56d374SYoshinobu Inoue cur = &sentinel; 3977d56d374SYoshinobu Inoue pai = &ai; 3987d56d374SYoshinobu Inoue pai->ai_flags = 0; 3997d56d374SYoshinobu Inoue pai->ai_family = PF_UNSPEC; 4007d56d374SYoshinobu Inoue pai->ai_socktype = ANY; 4017d56d374SYoshinobu Inoue pai->ai_protocol = ANY; 4027d56d374SYoshinobu Inoue pai->ai_addrlen = 0; 4037d56d374SYoshinobu Inoue pai->ai_canonname = NULL; 4047d56d374SYoshinobu Inoue pai->ai_addr = NULL; 4057d56d374SYoshinobu Inoue pai->ai_next = NULL; 4067d56d374SYoshinobu Inoue 4077d56d374SYoshinobu Inoue if (hostname == NULL && servname == NULL) 4087d56d374SYoshinobu Inoue return EAI_NONAME; 4097d56d374SYoshinobu Inoue if (hints) { 4107d56d374SYoshinobu Inoue /* error check for hints */ 4117d56d374SYoshinobu Inoue if (hints->ai_addrlen || hints->ai_canonname || 4127d56d374SYoshinobu Inoue hints->ai_addr || hints->ai_next) 4137d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); /* xxx */ 4147d56d374SYoshinobu Inoue if (hints->ai_flags & ~AI_MASK) 4157d56d374SYoshinobu Inoue ERR(EAI_BADFLAGS); 4167d56d374SYoshinobu Inoue switch (hints->ai_family) { 4177d56d374SYoshinobu Inoue case PF_UNSPEC: 4187d56d374SYoshinobu Inoue case PF_INET: 4197d56d374SYoshinobu Inoue #ifdef INET6 4207d56d374SYoshinobu Inoue case PF_INET6: 4217d56d374SYoshinobu Inoue #endif 4227d56d374SYoshinobu Inoue break; 4237d56d374SYoshinobu Inoue default: 4247d56d374SYoshinobu Inoue ERR(EAI_FAMILY); 4257d56d374SYoshinobu Inoue } 4267d56d374SYoshinobu Inoue memcpy(pai, hints, sizeof(*pai)); 4277d56d374SYoshinobu Inoue 4287d56d374SYoshinobu Inoue /* 4297d56d374SYoshinobu Inoue * if both socktype/protocol are specified, check if they 4307d56d374SYoshinobu Inoue * are meaningful combination. 4317d56d374SYoshinobu Inoue */ 4327d56d374SYoshinobu Inoue if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 433a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 434bc421492SHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, ex->e_af, 435bc421492SHajimu UMEMOTO WILD_AF(ex))) 436a42af91cSHajimu UMEMOTO continue; 437bc421492SHajimu UMEMOTO if (!MATCH(pai->ai_socktype, ex->e_socktype, 438bc421492SHajimu UMEMOTO WILD_SOCKTYPE(ex))) 4397d56d374SYoshinobu Inoue continue; 440bc421492SHajimu UMEMOTO if (!MATCH(pai->ai_protocol, ex->e_protocol, 441bc421492SHajimu UMEMOTO WILD_PROTOCOL(ex))) 4427d56d374SYoshinobu Inoue continue; 443bc421492SHajimu UMEMOTO 444bc421492SHajimu UMEMOTO /* matched */ 4454d756307SHajimu UMEMOTO break; 4464d756307SHajimu UMEMOTO } 4474d756307SHajimu UMEMOTO 4484d756307SHajimu UMEMOTO if (ex->e_af < 0) 4497d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); 4507d56d374SYoshinobu Inoue } 4517d56d374SYoshinobu Inoue } 4527d56d374SYoshinobu Inoue 4537d56d374SYoshinobu Inoue /* 454a42af91cSHajimu UMEMOTO * check for special cases. (1) numeric servname is disallowed if 455a42af91cSHajimu UMEMOTO * socktype/protocol are left unspecified. (2) servname is disallowed 456a42af91cSHajimu UMEMOTO * for raw and other inet{,6} sockets. 4577d56d374SYoshinobu Inoue */ 4587d56d374SYoshinobu Inoue if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 459a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4607d56d374SYoshinobu Inoue || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 4617d56d374SYoshinobu Inoue #endif 4627d56d374SYoshinobu Inoue ) { 463a42af91cSHajimu UMEMOTO ai0 = *pai; /* backup *pai */ 4647d56d374SYoshinobu Inoue 465a42af91cSHajimu UMEMOTO if (pai->ai_family == PF_UNSPEC) { 466a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4677d56d374SYoshinobu Inoue pai->ai_family = PF_INET6; 4687d56d374SYoshinobu Inoue #else 4697d56d374SYoshinobu Inoue pai->ai_family = PF_INET; 4707d56d374SYoshinobu Inoue #endif 471a42af91cSHajimu UMEMOTO } 4727d56d374SYoshinobu Inoue error = get_portmatch(pai, servname); 4737d56d374SYoshinobu Inoue if (error) 474f61ac9d9SDimitry Andric goto bad; 475a42af91cSHajimu UMEMOTO 476a42af91cSHajimu UMEMOTO *pai = ai0; 4777d56d374SYoshinobu Inoue } 4787d56d374SYoshinobu Inoue 479a42af91cSHajimu UMEMOTO ai0 = *pai; 480a42af91cSHajimu UMEMOTO 481bc421492SHajimu UMEMOTO /* 482bc421492SHajimu UMEMOTO * NULL hostname, or numeric hostname. 483bc421492SHajimu UMEMOTO * If numeric representation of AF1 can be interpreted as FQDN 484bc421492SHajimu UMEMOTO * representation of AF2, we need to think again about the code below. 485bc421492SHajimu UMEMOTO */ 486bc421492SHajimu UMEMOTO found = 0; 487bc421492SHajimu UMEMOTO for (afd = afdl; afd->a_af; afd++) { 4887d56d374SYoshinobu Inoue *pai = ai0; 4897d56d374SYoshinobu Inoue 490bc421492SHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1)) 4917d56d374SYoshinobu Inoue continue; 4927d56d374SYoshinobu Inoue 4937d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 494bc421492SHajimu UMEMOTO pai->ai_family = afd->a_af; 4957d56d374SYoshinobu Inoue 496bc421492SHajimu UMEMOTO if (hostname == NULL) { 497bc421492SHajimu UMEMOTO error = explore_null(pai, servname, 498bc421492SHajimu UMEMOTO &afailist[afd - afdl]); 4997d56d374SYoshinobu Inoue 500bf838688SHajimu UMEMOTO /* 501bc421492SHajimu UMEMOTO * Errors from explore_null should be unexpected and 502bc421492SHajimu UMEMOTO * be caught to avoid returning an incomplete result. 503bf838688SHajimu UMEMOTO */ 504bc421492SHajimu UMEMOTO if (error != 0) 505bc421492SHajimu UMEMOTO goto bad; 506bc421492SHajimu UMEMOTO } else { 507bc421492SHajimu UMEMOTO error = explore_numeric_scope(pai, hostname, servname, 508bc421492SHajimu UMEMOTO &afailist[afd - afdl]); 509bc421492SHajimu UMEMOTO 510bc421492SHajimu UMEMOTO /* 511bc421492SHajimu UMEMOTO * explore_numeric_scope returns an error for address 512bc421492SHajimu UMEMOTO * families that do not match that of hostname. 513bc421492SHajimu UMEMOTO * Thus we should not catch the error at this moment. 514bc421492SHajimu UMEMOTO */ 515bc421492SHajimu UMEMOTO } 516bc421492SHajimu UMEMOTO 517bc421492SHajimu UMEMOTO if (!error && afailist[afd - afdl]) 518bc421492SHajimu UMEMOTO found++; 519bc421492SHajimu UMEMOTO } 520bc421492SHajimu UMEMOTO if (found) { 5214c6867a8SHajimu UMEMOTO numeric = 1; 522bc421492SHajimu UMEMOTO goto globcopy; 5234c6867a8SHajimu UMEMOTO } 524bf838688SHajimu UMEMOTO 525bf838688SHajimu UMEMOTO if (hostname == NULL) 526bf838688SHajimu UMEMOTO ERR(EAI_NONAME); /* used to be EAI_NODATA */ 527bf838688SHajimu UMEMOTO if (pai->ai_flags & AI_NUMERICHOST) 528bf838688SHajimu UMEMOTO ERR(EAI_NONAME); 529bf838688SHajimu UMEMOTO 530bf838688SHajimu UMEMOTO if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0)) 531bf838688SHajimu UMEMOTO ERR(EAI_FAIL); 532a42af91cSHajimu UMEMOTO 533f95d4633SHajimu UMEMOTO /* 534bf838688SHajimu UMEMOTO * hostname as alphabetical name. 535f95d4633SHajimu UMEMOTO */ 536bc421492SHajimu UMEMOTO *pai = ai0; 537bc421492SHajimu UMEMOTO error = explore_fqdn(pai, hostname, servname, &afai_unspec); 538bc421492SHajimu UMEMOTO 539bc421492SHajimu UMEMOTO globcopy: 540bf838688SHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 541bf838688SHajimu UMEMOTO *pai = ai0; 542f95d4633SHajimu UMEMOTO 543bc421492SHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 544f95d4633SHajimu UMEMOTO continue; 545bf838688SHajimu UMEMOTO if (!MATCH(pai->ai_socktype, ex->e_socktype, 546bc421492SHajimu UMEMOTO WILD_SOCKTYPE(ex))) 547bf838688SHajimu UMEMOTO continue; 548bf838688SHajimu UMEMOTO if (!MATCH(pai->ai_protocol, ex->e_protocol, 549bc421492SHajimu UMEMOTO WILD_PROTOCOL(ex))) 550bf838688SHajimu UMEMOTO continue; 551f95d4633SHajimu UMEMOTO 552bc421492SHajimu UMEMOTO if (pai->ai_family == PF_UNSPEC) 553bc421492SHajimu UMEMOTO pai->ai_family = ex->e_af; 554bf838688SHajimu UMEMOTO if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 555bf838688SHajimu UMEMOTO pai->ai_socktype = ex->e_socktype; 556bf838688SHajimu UMEMOTO if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 557bf838688SHajimu UMEMOTO pai->ai_protocol = ex->e_protocol; 558bf838688SHajimu UMEMOTO 559bc421492SHajimu UMEMOTO /* 560bc421492SHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 561bc421492SHajimu UMEMOTO */ 562bc421492SHajimu UMEMOTO if (get_portmatch(pai, servname) != 0) 563bc421492SHajimu UMEMOTO continue; 564bc421492SHajimu UMEMOTO 565bc421492SHajimu UMEMOTO if (afai_unspec) 566bc421492SHajimu UMEMOTO afai = afai_unspec; 567bc421492SHajimu UMEMOTO else { 568bc421492SHajimu UMEMOTO if ((afd = find_afd(pai->ai_family)) == NULL) 569bc421492SHajimu UMEMOTO continue; 570bc421492SHajimu UMEMOTO /* XXX assumes that afd points inside afdl[] */ 571bc421492SHajimu UMEMOTO afai = afailist[afd - afdl]; 572bc421492SHajimu UMEMOTO } 573bc421492SHajimu UMEMOTO if (!afai) 574bc421492SHajimu UMEMOTO continue; 575bc421492SHajimu UMEMOTO 576bc421492SHajimu UMEMOTO error = explore_copy(pai, afai, &cur->ai_next); 577bc421492SHajimu UMEMOTO if (error != 0) 578bc421492SHajimu UMEMOTO goto bad; 579bf838688SHajimu UMEMOTO 580bf838688SHajimu UMEMOTO while (cur && cur->ai_next) 581f95d4633SHajimu UMEMOTO cur = cur->ai_next; 582f95d4633SHajimu UMEMOTO } 583f95d4633SHajimu UMEMOTO 5844c6867a8SHajimu UMEMOTO /* 5854c6867a8SHajimu UMEMOTO * ensure we return either: 5864c6867a8SHajimu UMEMOTO * - error == 0, non-NULL *res 5874c6867a8SHajimu UMEMOTO * - error != 0, NULL *res 5884c6867a8SHajimu UMEMOTO */ 589bf838688SHajimu UMEMOTO if (error == 0) { 590bf838688SHajimu UMEMOTO if (sentinel.ai_next) { 5914c6867a8SHajimu UMEMOTO /* 5924c6867a8SHajimu UMEMOTO * If the returned entry is for an active connection, 5934c6867a8SHajimu UMEMOTO * and the given name is not numeric, reorder the 5944c6867a8SHajimu UMEMOTO * list, so that the application would try the list 595e47c82edSJINMEI Tatuya * in the most efficient order. Since the head entry 596e47c82edSJINMEI Tatuya * of the original list may contain ai_canonname and 597e47c82edSJINMEI Tatuya * that entry may be moved elsewhere in the new list, 598e47c82edSJINMEI Tatuya * we keep the pointer and will restore it in the new 599e47c82edSJINMEI Tatuya * head entry. (Note that RFC3493 requires the head 600e47c82edSJINMEI Tatuya * entry store it when requested by the caller). 6014c6867a8SHajimu UMEMOTO */ 6024c6867a8SHajimu UMEMOTO if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) { 603e47c82edSJINMEI Tatuya if (!numeric) { 604e47c82edSJINMEI Tatuya char *canonname; 605e47c82edSJINMEI Tatuya 606e47c82edSJINMEI Tatuya canonname = 607e47c82edSJINMEI Tatuya sentinel.ai_next->ai_canonname; 608e47c82edSJINMEI Tatuya sentinel.ai_next->ai_canonname = NULL; 6094c6867a8SHajimu UMEMOTO (void)reorder(&sentinel); 610e47c82edSJINMEI Tatuya if (sentinel.ai_next->ai_canonname == 611e47c82edSJINMEI Tatuya NULL) { 612e47c82edSJINMEI Tatuya sentinel.ai_next->ai_canonname 613e47c82edSJINMEI Tatuya = canonname; 614e47c82edSJINMEI Tatuya } else if (canonname != NULL) 615e47c82edSJINMEI Tatuya free(canonname); 616e47c82edSJINMEI Tatuya } 6174c6867a8SHajimu UMEMOTO } 618bf838688SHajimu UMEMOTO *res = sentinel.ai_next; 619bf838688SHajimu UMEMOTO } else 620bf838688SHajimu UMEMOTO error = EAI_FAIL; 621bf838688SHajimu UMEMOTO } 622bc421492SHajimu UMEMOTO 623bf838688SHajimu UMEMOTO bad: 624bc421492SHajimu UMEMOTO if (afai_unspec) 625bc421492SHajimu UMEMOTO freeaddrinfo(afai_unspec); 626bc421492SHajimu UMEMOTO for (afd = afdl; afd->a_af; afd++) { 627bc421492SHajimu UMEMOTO if (afailist[afd - afdl]) 628bc421492SHajimu UMEMOTO freeaddrinfo(afailist[afd - afdl]); 629bc421492SHajimu UMEMOTO } 630bc421492SHajimu UMEMOTO if (!*res) 631bf838688SHajimu UMEMOTO if (sentinel.ai_next) 632f95d4633SHajimu UMEMOTO freeaddrinfo(sentinel.ai_next); 633bc421492SHajimu UMEMOTO 634bc421492SHajimu UMEMOTO return (error); 6357d56d374SYoshinobu Inoue } 6367d56d374SYoshinobu Inoue 6374c6867a8SHajimu UMEMOTO static int 63890d1d883SHajimu UMEMOTO reorder(struct addrinfo *sentinel) 6394c6867a8SHajimu UMEMOTO { 6404c6867a8SHajimu UMEMOTO struct addrinfo *ai, **aip; 6414c6867a8SHajimu UMEMOTO struct ai_order *aio; 6424c6867a8SHajimu UMEMOTO int i, n; 6434c6867a8SHajimu UMEMOTO struct policyhead policyhead; 6444c6867a8SHajimu UMEMOTO 6454c6867a8SHajimu UMEMOTO /* count the number of addrinfo elements for sorting. */ 6464c6867a8SHajimu UMEMOTO for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++) 6474c6867a8SHajimu UMEMOTO ; 6484c6867a8SHajimu UMEMOTO 6494c6867a8SHajimu UMEMOTO /* 6504c6867a8SHajimu UMEMOTO * If the number is small enough, we can skip the reordering process. 6514c6867a8SHajimu UMEMOTO */ 6524c6867a8SHajimu UMEMOTO if (n <= 1) 6534c6867a8SHajimu UMEMOTO return(n); 6544c6867a8SHajimu UMEMOTO 6554c6867a8SHajimu UMEMOTO /* allocate a temporary array for sort and initialization of it. */ 6564c6867a8SHajimu UMEMOTO if ((aio = malloc(sizeof(*aio) * n)) == NULL) 6574c6867a8SHajimu UMEMOTO return(n); /* give up reordering */ 6584c6867a8SHajimu UMEMOTO memset(aio, 0, sizeof(*aio) * n); 6594c6867a8SHajimu UMEMOTO 6604c6867a8SHajimu UMEMOTO /* retrieve address selection policy from the kernel */ 6614c6867a8SHajimu UMEMOTO TAILQ_INIT(&policyhead); 6624c6867a8SHajimu UMEMOTO if (!get_addrselectpolicy(&policyhead)) { 6634c6867a8SHajimu UMEMOTO /* no policy is installed into kernel, we don't sort. */ 6644c6867a8SHajimu UMEMOTO free(aio); 6654c6867a8SHajimu UMEMOTO return (n); 6664c6867a8SHajimu UMEMOTO } 6674c6867a8SHajimu UMEMOTO 6684c6867a8SHajimu UMEMOTO for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) { 6694c6867a8SHajimu UMEMOTO aio[i].aio_ai = ai; 6704c6867a8SHajimu UMEMOTO aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr); 6714c6867a8SHajimu UMEMOTO aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr, 6724c6867a8SHajimu UMEMOTO &policyhead); 6734d489f47SHajimu UMEMOTO set_source(&aio[i], &policyhead); 6744c6867a8SHajimu UMEMOTO } 6754c6867a8SHajimu UMEMOTO 6764c6867a8SHajimu UMEMOTO /* perform sorting. */ 6774c6867a8SHajimu UMEMOTO qsort(aio, n, sizeof(*aio), comp_dst); 6784c6867a8SHajimu UMEMOTO 6794c6867a8SHajimu UMEMOTO /* reorder the addrinfo chain. */ 6804c6867a8SHajimu UMEMOTO for (i = 0, aip = &sentinel->ai_next; i < n; i++) { 6814c6867a8SHajimu UMEMOTO *aip = aio[i].aio_ai; 6824c6867a8SHajimu UMEMOTO aip = &aio[i].aio_ai->ai_next; 6834c6867a8SHajimu UMEMOTO } 6844c6867a8SHajimu UMEMOTO *aip = NULL; 6854c6867a8SHajimu UMEMOTO 6864c6867a8SHajimu UMEMOTO /* cleanup and return */ 6874c6867a8SHajimu UMEMOTO free(aio); 6884c6867a8SHajimu UMEMOTO free_addrselectpolicy(&policyhead); 6894c6867a8SHajimu UMEMOTO return(n); 6904c6867a8SHajimu UMEMOTO } 6914c6867a8SHajimu UMEMOTO 6924c6867a8SHajimu UMEMOTO static int 69390d1d883SHajimu UMEMOTO get_addrselectpolicy(struct policyhead *head) 6944c6867a8SHajimu UMEMOTO { 6954c6867a8SHajimu UMEMOTO #ifdef INET6 6964c6867a8SHajimu UMEMOTO int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY }; 6974c6867a8SHajimu UMEMOTO size_t l; 6984c6867a8SHajimu UMEMOTO char *buf; 6994c6867a8SHajimu UMEMOTO struct in6_addrpolicy *pol, *ep; 7004c6867a8SHajimu UMEMOTO 7014c6867a8SHajimu UMEMOTO if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) 7024c6867a8SHajimu UMEMOTO return (0); 7030c19b5deSMaksim Yevmenkin if (l == 0) 7040c19b5deSMaksim Yevmenkin return (0); 7054c6867a8SHajimu UMEMOTO if ((buf = malloc(l)) == NULL) 7064c6867a8SHajimu UMEMOTO return (0); 7074c6867a8SHajimu UMEMOTO if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 7084c6867a8SHajimu UMEMOTO free(buf); 7094c6867a8SHajimu UMEMOTO return (0); 7104c6867a8SHajimu UMEMOTO } 7114c6867a8SHajimu UMEMOTO 7124c6867a8SHajimu UMEMOTO ep = (struct in6_addrpolicy *)(buf + l); 7134c6867a8SHajimu UMEMOTO for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) { 7144c6867a8SHajimu UMEMOTO struct policyqueue *new; 7154c6867a8SHajimu UMEMOTO 7164c6867a8SHajimu UMEMOTO if ((new = malloc(sizeof(*new))) == NULL) { 7174c6867a8SHajimu UMEMOTO free_addrselectpolicy(head); /* make the list empty */ 7184c6867a8SHajimu UMEMOTO break; 7194c6867a8SHajimu UMEMOTO } 7204c6867a8SHajimu UMEMOTO new->pc_policy = *pol; 7214c6867a8SHajimu UMEMOTO TAILQ_INSERT_TAIL(head, new, pc_entry); 7224c6867a8SHajimu UMEMOTO } 7234c6867a8SHajimu UMEMOTO 7244c6867a8SHajimu UMEMOTO free(buf); 7254c6867a8SHajimu UMEMOTO return (1); 7264c6867a8SHajimu UMEMOTO #else 7274c6867a8SHajimu UMEMOTO return (0); 7284c6867a8SHajimu UMEMOTO #endif 7294c6867a8SHajimu UMEMOTO } 7304c6867a8SHajimu UMEMOTO 7314c6867a8SHajimu UMEMOTO static void 73290d1d883SHajimu UMEMOTO free_addrselectpolicy(struct policyhead *head) 7334c6867a8SHajimu UMEMOTO { 7344c6867a8SHajimu UMEMOTO struct policyqueue *ent, *nent; 7354c6867a8SHajimu UMEMOTO 7364c6867a8SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = nent) { 7374c6867a8SHajimu UMEMOTO nent = TAILQ_NEXT(ent, pc_entry); 7384c6867a8SHajimu UMEMOTO TAILQ_REMOVE(head, ent, pc_entry); 7394c6867a8SHajimu UMEMOTO free(ent); 7404c6867a8SHajimu UMEMOTO } 7414c6867a8SHajimu UMEMOTO } 7424c6867a8SHajimu UMEMOTO 7434c6867a8SHajimu UMEMOTO static struct policyqueue * 74490d1d883SHajimu UMEMOTO match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head) 7454c6867a8SHajimu UMEMOTO { 7464c6867a8SHajimu UMEMOTO #ifdef INET6 7474c6867a8SHajimu UMEMOTO struct policyqueue *ent, *bestent = NULL; 7484c6867a8SHajimu UMEMOTO struct in6_addrpolicy *pol; 7494c6867a8SHajimu UMEMOTO int matchlen, bestmatchlen = -1; 7504c6867a8SHajimu UMEMOTO u_char *mp, *ep, *k, *p, m; 7514c6867a8SHajimu UMEMOTO struct sockaddr_in6 key; 7524c6867a8SHajimu UMEMOTO 7534c6867a8SHajimu UMEMOTO switch(addr->sa_family) { 7544c6867a8SHajimu UMEMOTO case AF_INET6: 7554c6867a8SHajimu UMEMOTO key = *(struct sockaddr_in6 *)addr; 7564c6867a8SHajimu UMEMOTO break; 7574c6867a8SHajimu UMEMOTO case AF_INET: 7584c6867a8SHajimu UMEMOTO /* convert the address into IPv4-mapped IPv6 address. */ 7594c6867a8SHajimu UMEMOTO memset(&key, 0, sizeof(key)); 7604c6867a8SHajimu UMEMOTO key.sin6_family = AF_INET6; 7614c6867a8SHajimu UMEMOTO key.sin6_len = sizeof(key); 7624c6867a8SHajimu UMEMOTO key.sin6_addr.s6_addr[10] = 0xff; 7634c6867a8SHajimu UMEMOTO key.sin6_addr.s6_addr[11] = 0xff; 7644c6867a8SHajimu UMEMOTO memcpy(&key.sin6_addr.s6_addr[12], 7654c6867a8SHajimu UMEMOTO &((struct sockaddr_in *)addr)->sin_addr, 4); 7664c6867a8SHajimu UMEMOTO break; 7674c6867a8SHajimu UMEMOTO default: 7684c6867a8SHajimu UMEMOTO return(NULL); 7694c6867a8SHajimu UMEMOTO } 7704c6867a8SHajimu UMEMOTO 7714c6867a8SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) { 7724c6867a8SHajimu UMEMOTO pol = &ent->pc_policy; 7734c6867a8SHajimu UMEMOTO matchlen = 0; 7744c6867a8SHajimu UMEMOTO 7754c6867a8SHajimu UMEMOTO mp = (u_char *)&pol->addrmask.sin6_addr; 7764c6867a8SHajimu UMEMOTO ep = mp + 16; /* XXX: scope field? */ 7774c6867a8SHajimu UMEMOTO k = (u_char *)&key.sin6_addr; 7784c6867a8SHajimu UMEMOTO p = (u_char *)&pol->addr.sin6_addr; 7794c6867a8SHajimu UMEMOTO for (; mp < ep && *mp; mp++, k++, p++) { 7804c6867a8SHajimu UMEMOTO m = *mp; 7814c6867a8SHajimu UMEMOTO if ((*k & m) != *p) 7824c6867a8SHajimu UMEMOTO goto next; /* not match */ 7834c6867a8SHajimu UMEMOTO if (m == 0xff) /* short cut for a typical case */ 7844c6867a8SHajimu UMEMOTO matchlen += 8; 7854c6867a8SHajimu UMEMOTO else { 7864c6867a8SHajimu UMEMOTO while (m >= 0x80) { 7874c6867a8SHajimu UMEMOTO matchlen++; 7884c6867a8SHajimu UMEMOTO m <<= 1; 7894c6867a8SHajimu UMEMOTO } 7904c6867a8SHajimu UMEMOTO } 7914c6867a8SHajimu UMEMOTO } 7924c6867a8SHajimu UMEMOTO 7934c6867a8SHajimu UMEMOTO /* matched. check if this is better than the current best. */ 7944c6867a8SHajimu UMEMOTO if (matchlen > bestmatchlen) { 7954c6867a8SHajimu UMEMOTO bestent = ent; 7964c6867a8SHajimu UMEMOTO bestmatchlen = matchlen; 7974c6867a8SHajimu UMEMOTO } 7984c6867a8SHajimu UMEMOTO 7994c6867a8SHajimu UMEMOTO next: 8004c6867a8SHajimu UMEMOTO continue; 8014c6867a8SHajimu UMEMOTO } 8024c6867a8SHajimu UMEMOTO 8034c6867a8SHajimu UMEMOTO return(bestent); 8044c6867a8SHajimu UMEMOTO #else 8054c6867a8SHajimu UMEMOTO return(NULL); 8064c6867a8SHajimu UMEMOTO #endif 8074c6867a8SHajimu UMEMOTO 8084c6867a8SHajimu UMEMOTO } 8094c6867a8SHajimu UMEMOTO 8104d489f47SHajimu UMEMOTO static void 81190d1d883SHajimu UMEMOTO set_source(struct ai_order *aio, struct policyhead *ph) 8124d489f47SHajimu UMEMOTO { 8134d489f47SHajimu UMEMOTO struct addrinfo ai = *aio->aio_ai; 8144d489f47SHajimu UMEMOTO struct sockaddr_storage ss; 815943db5a2SHajimu UMEMOTO socklen_t srclen; 816943db5a2SHajimu UMEMOTO int s; 8174d489f47SHajimu UMEMOTO 8184d489f47SHajimu UMEMOTO /* set unspec ("no source is available"), just in case */ 8194d489f47SHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC; 8204d489f47SHajimu UMEMOTO aio->aio_srcscope = -1; 8214d489f47SHajimu UMEMOTO 8224d489f47SHajimu UMEMOTO switch(ai.ai_family) { 8234d489f47SHajimu UMEMOTO case AF_INET: 8244d489f47SHajimu UMEMOTO #ifdef INET6 8254d489f47SHajimu UMEMOTO case AF_INET6: 8264d489f47SHajimu UMEMOTO #endif 8274d489f47SHajimu UMEMOTO break; 8284d489f47SHajimu UMEMOTO default: /* ignore unsupported AFs explicitly */ 8294d489f47SHajimu UMEMOTO return; 8304d489f47SHajimu UMEMOTO } 8314d489f47SHajimu UMEMOTO 8324d489f47SHajimu UMEMOTO /* XXX: make a dummy addrinfo to call connect() */ 8334d489f47SHajimu UMEMOTO ai.ai_socktype = SOCK_DGRAM; 8344d489f47SHajimu UMEMOTO ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */ 8354d489f47SHajimu UMEMOTO ai.ai_next = NULL; 8364d489f47SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 8374d489f47SHajimu UMEMOTO memcpy(&ss, ai.ai_addr, ai.ai_addrlen); 8384d489f47SHajimu UMEMOTO ai.ai_addr = (struct sockaddr *)&ss; 8394d489f47SHajimu UMEMOTO get_port(&ai, "1", 0); 8404d489f47SHajimu UMEMOTO 8414d489f47SHajimu UMEMOTO /* open a socket to get the source address for the given dst */ 84272531978SJilles Tjoelker if ((s = _socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC, 84372531978SJilles Tjoelker ai.ai_protocol)) < 0) 8444d489f47SHajimu UMEMOTO return; /* give up */ 8454d489f47SHajimu UMEMOTO if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0) 8464d489f47SHajimu UMEMOTO goto cleanup; 8474d489f47SHajimu UMEMOTO srclen = ai.ai_addrlen; 8484d489f47SHajimu UMEMOTO if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) { 8494d489f47SHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC; 8504d489f47SHajimu UMEMOTO goto cleanup; 8514d489f47SHajimu UMEMOTO } 8524d489f47SHajimu UMEMOTO aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa); 8534d489f47SHajimu UMEMOTO aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph); 8544d489f47SHajimu UMEMOTO aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr); 8554d489f47SHajimu UMEMOTO #ifdef INET6 8564d489f47SHajimu UMEMOTO if (ai.ai_family == AF_INET6) { 8574d489f47SHajimu UMEMOTO struct in6_ifreq ifr6; 8584d489f47SHajimu UMEMOTO u_int32_t flags6; 8594d489f47SHajimu UMEMOTO 8604d489f47SHajimu UMEMOTO memset(&ifr6, 0, sizeof(ifr6)); 8614d489f47SHajimu UMEMOTO memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen); 8624d489f47SHajimu UMEMOTO if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) { 8634d489f47SHajimu UMEMOTO flags6 = ifr6.ifr_ifru.ifru_flags6; 8644d489f47SHajimu UMEMOTO if ((flags6 & IN6_IFF_DEPRECATED)) 8654d489f47SHajimu UMEMOTO aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED; 8664d489f47SHajimu UMEMOTO } 8674d489f47SHajimu UMEMOTO } 8684d489f47SHajimu UMEMOTO #endif 8694d489f47SHajimu UMEMOTO 8704d489f47SHajimu UMEMOTO cleanup: 8714d489f47SHajimu UMEMOTO _close(s); 8724d489f47SHajimu UMEMOTO return; 8734d489f47SHajimu UMEMOTO } 8744d489f47SHajimu UMEMOTO 8754d489f47SHajimu UMEMOTO static int 87690d1d883SHajimu UMEMOTO matchlen(struct sockaddr *src, struct sockaddr *dst) 8774d489f47SHajimu UMEMOTO { 8784d489f47SHajimu UMEMOTO int match = 0; 8794d489f47SHajimu UMEMOTO u_char *s, *d; 8804d489f47SHajimu UMEMOTO u_char *lim, r; 8814d489f47SHajimu UMEMOTO int addrlen; 8824d489f47SHajimu UMEMOTO 8834d489f47SHajimu UMEMOTO switch (src->sa_family) { 8844d489f47SHajimu UMEMOTO #ifdef INET6 8854d489f47SHajimu UMEMOTO case AF_INET6: 8864d489f47SHajimu UMEMOTO s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 8874d489f47SHajimu UMEMOTO d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 8884d489f47SHajimu UMEMOTO addrlen = sizeof(struct in6_addr); 8894d489f47SHajimu UMEMOTO lim = s + addrlen; 8904d489f47SHajimu UMEMOTO break; 8914d489f47SHajimu UMEMOTO #endif 8924d489f47SHajimu UMEMOTO case AF_INET: 893fd57e549SGeorge V. Neville-Neil s = (u_char *)&((struct sockaddr_in *)src)->sin_addr; 894fd57e549SGeorge V. Neville-Neil d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr; 8954d489f47SHajimu UMEMOTO addrlen = sizeof(struct in_addr); 8964d489f47SHajimu UMEMOTO lim = s + addrlen; 8974d489f47SHajimu UMEMOTO break; 8984d489f47SHajimu UMEMOTO default: 8994d489f47SHajimu UMEMOTO return(0); 9004d489f47SHajimu UMEMOTO } 9014d489f47SHajimu UMEMOTO 9024d489f47SHajimu UMEMOTO while (s < lim) 9034d489f47SHajimu UMEMOTO if ((r = (*d++ ^ *s++)) != 0) { 9044d489f47SHajimu UMEMOTO while (r < addrlen * 8) { 9054d489f47SHajimu UMEMOTO match++; 9064d489f47SHajimu UMEMOTO r <<= 1; 9074d489f47SHajimu UMEMOTO } 9084d489f47SHajimu UMEMOTO break; 9094d489f47SHajimu UMEMOTO } else 9104d489f47SHajimu UMEMOTO match += 8; 9114d489f47SHajimu UMEMOTO return(match); 9124d489f47SHajimu UMEMOTO } 9134d489f47SHajimu UMEMOTO 9144c6867a8SHajimu UMEMOTO static int 91590d1d883SHajimu UMEMOTO comp_dst(const void *arg1, const void *arg2) 9164c6867a8SHajimu UMEMOTO { 9174c6867a8SHajimu UMEMOTO const struct ai_order *dst1 = arg1, *dst2 = arg2; 9184c6867a8SHajimu UMEMOTO 9194c6867a8SHajimu UMEMOTO /* 9204c6867a8SHajimu UMEMOTO * Rule 1: Avoid unusable destinations. 9214c6867a8SHajimu UMEMOTO * XXX: we currently do not consider if an appropriate route exists. 9224c6867a8SHajimu UMEMOTO */ 9234c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 9244c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family == AF_UNSPEC) { 9254c6867a8SHajimu UMEMOTO return(-1); 9264c6867a8SHajimu UMEMOTO } 9274c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family == AF_UNSPEC && 9284c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) { 9294c6867a8SHajimu UMEMOTO return(1); 9304c6867a8SHajimu UMEMOTO } 9314c6867a8SHajimu UMEMOTO 9324c6867a8SHajimu UMEMOTO /* Rule 2: Prefer matching scope. */ 9334c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope == dst1->aio_srcscope && 9344c6867a8SHajimu UMEMOTO dst2->aio_dstscope != dst2->aio_srcscope) { 9354c6867a8SHajimu UMEMOTO return(-1); 9364c6867a8SHajimu UMEMOTO } 9374c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope != dst1->aio_srcscope && 9384c6867a8SHajimu UMEMOTO dst2->aio_dstscope == dst2->aio_srcscope) { 9394c6867a8SHajimu UMEMOTO return(1); 9404c6867a8SHajimu UMEMOTO } 9414c6867a8SHajimu UMEMOTO 9424c6867a8SHajimu UMEMOTO /* Rule 3: Avoid deprecated addresses. */ 9434c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 9444c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) { 9454c6867a8SHajimu UMEMOTO if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 9464c6867a8SHajimu UMEMOTO (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 9474c6867a8SHajimu UMEMOTO return(-1); 9484c6867a8SHajimu UMEMOTO } 9494c6867a8SHajimu UMEMOTO if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 9504c6867a8SHajimu UMEMOTO !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 9514c6867a8SHajimu UMEMOTO return(1); 9524c6867a8SHajimu UMEMOTO } 9534c6867a8SHajimu UMEMOTO } 9544c6867a8SHajimu UMEMOTO 9554c6867a8SHajimu UMEMOTO /* Rule 4: Prefer home addresses. */ 9564c6867a8SHajimu UMEMOTO /* XXX: not implemented yet */ 9574c6867a8SHajimu UMEMOTO 9584c6867a8SHajimu UMEMOTO /* Rule 5: Prefer matching label. */ 9594c6867a8SHajimu UMEMOTO #ifdef INET6 9604c6867a8SHajimu UMEMOTO if (dst1->aio_srcpolicy && dst1->aio_dstpolicy && 9614c6867a8SHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label == 9624c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label && 9634c6867a8SHajimu UMEMOTO (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL || 9644c6867a8SHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label != 9654c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label)) { 9664c6867a8SHajimu UMEMOTO return(-1); 9674c6867a8SHajimu UMEMOTO } 9684c6867a8SHajimu UMEMOTO if (dst2->aio_srcpolicy && dst2->aio_dstpolicy && 9694c6867a8SHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label == 9704c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label && 9714c6867a8SHajimu UMEMOTO (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL || 9724c6867a8SHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label != 9734c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label)) { 9744c6867a8SHajimu UMEMOTO return(1); 9754c6867a8SHajimu UMEMOTO } 9764c6867a8SHajimu UMEMOTO #endif 9774c6867a8SHajimu UMEMOTO 9784c6867a8SHajimu UMEMOTO /* Rule 6: Prefer higher precedence. */ 9794c6867a8SHajimu UMEMOTO #ifdef INET6 9804c6867a8SHajimu UMEMOTO if (dst1->aio_dstpolicy && 9814c6867a8SHajimu UMEMOTO (dst2->aio_dstpolicy == NULL || 9824c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced > 9834c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced)) { 9844c6867a8SHajimu UMEMOTO return(-1); 9854c6867a8SHajimu UMEMOTO } 9864c6867a8SHajimu UMEMOTO if (dst2->aio_dstpolicy && 9874c6867a8SHajimu UMEMOTO (dst1->aio_dstpolicy == NULL || 9884c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced > 9894c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced)) { 9904c6867a8SHajimu UMEMOTO return(1); 9914c6867a8SHajimu UMEMOTO } 9924c6867a8SHajimu UMEMOTO #endif 9934c6867a8SHajimu UMEMOTO 9944c6867a8SHajimu UMEMOTO /* Rule 7: Prefer native transport. */ 9954c6867a8SHajimu UMEMOTO /* XXX: not implemented yet */ 9964c6867a8SHajimu UMEMOTO 9974c6867a8SHajimu UMEMOTO /* Rule 8: Prefer smaller scope. */ 9984c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope >= 0 && 9994c6867a8SHajimu UMEMOTO dst1->aio_dstscope < dst2->aio_dstscope) { 10004c6867a8SHajimu UMEMOTO return(-1); 10014c6867a8SHajimu UMEMOTO } 10024c6867a8SHajimu UMEMOTO if (dst2->aio_dstscope >= 0 && 10034c6867a8SHajimu UMEMOTO dst2->aio_dstscope < dst1->aio_dstscope) { 10044c6867a8SHajimu UMEMOTO return(1); 10054c6867a8SHajimu UMEMOTO } 10064c6867a8SHajimu UMEMOTO 10074c6867a8SHajimu UMEMOTO /* 10084c6867a8SHajimu UMEMOTO * Rule 9: Use longest matching prefix. 10094c6867a8SHajimu UMEMOTO * We compare the match length in a same AF only. 10104c6867a8SHajimu UMEMOTO */ 10114c6867a8SHajimu UMEMOTO if (dst1->aio_ai->ai_addr->sa_family == 10124c6867a8SHajimu UMEMOTO dst2->aio_ai->ai_addr->sa_family) { 10134c6867a8SHajimu UMEMOTO if (dst1->aio_matchlen > dst2->aio_matchlen) { 10144c6867a8SHajimu UMEMOTO return(-1); 10154c6867a8SHajimu UMEMOTO } 10164c6867a8SHajimu UMEMOTO if (dst1->aio_matchlen < dst2->aio_matchlen) { 10174c6867a8SHajimu UMEMOTO return(1); 10184c6867a8SHajimu UMEMOTO } 10194c6867a8SHajimu UMEMOTO } 10204c6867a8SHajimu UMEMOTO 10214c6867a8SHajimu UMEMOTO /* Rule 10: Otherwise, leave the order unchanged. */ 10224c6867a8SHajimu UMEMOTO return(-1); 10234c6867a8SHajimu UMEMOTO } 10244c6867a8SHajimu UMEMOTO 10254c6867a8SHajimu UMEMOTO /* 10264c6867a8SHajimu UMEMOTO * Copy from scope.c. 10274c6867a8SHajimu UMEMOTO * XXX: we should standardize the functions and link them as standard 10284c6867a8SHajimu UMEMOTO * library. 10294c6867a8SHajimu UMEMOTO */ 10304c6867a8SHajimu UMEMOTO static int 103190d1d883SHajimu UMEMOTO gai_addr2scopetype(struct sockaddr *sa) 10324c6867a8SHajimu UMEMOTO { 10334c6867a8SHajimu UMEMOTO #ifdef INET6 10344c6867a8SHajimu UMEMOTO struct sockaddr_in6 *sa6; 10354c6867a8SHajimu UMEMOTO #endif 10364c6867a8SHajimu UMEMOTO struct sockaddr_in *sa4; 10374c6867a8SHajimu UMEMOTO 10384c6867a8SHajimu UMEMOTO switch(sa->sa_family) { 10394c6867a8SHajimu UMEMOTO #ifdef INET6 10404c6867a8SHajimu UMEMOTO case AF_INET6: 10414c6867a8SHajimu UMEMOTO sa6 = (struct sockaddr_in6 *)sa; 10424c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 10434c6867a8SHajimu UMEMOTO /* just use the scope field of the multicast address */ 10444c6867a8SHajimu UMEMOTO return(sa6->sin6_addr.s6_addr[2] & 0x0f); 10454c6867a8SHajimu UMEMOTO } 10464c6867a8SHajimu UMEMOTO /* 10474c6867a8SHajimu UMEMOTO * Unicast addresses: map scope type to corresponding scope 10484c6867a8SHajimu UMEMOTO * value defined for multcast addresses. 10494c6867a8SHajimu UMEMOTO * XXX: hardcoded scope type values are bad... 10504c6867a8SHajimu UMEMOTO */ 10514c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) 10524c6867a8SHajimu UMEMOTO return(1); /* node local scope */ 10534c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) 10544c6867a8SHajimu UMEMOTO return(2); /* link-local scope */ 10554c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr)) 10564c6867a8SHajimu UMEMOTO return(5); /* site-local scope */ 10574c6867a8SHajimu UMEMOTO return(14); /* global scope */ 10584c6867a8SHajimu UMEMOTO break; 10594c6867a8SHajimu UMEMOTO #endif 10604c6867a8SHajimu UMEMOTO case AF_INET: 10614c6867a8SHajimu UMEMOTO /* 10624c6867a8SHajimu UMEMOTO * IPv4 pseudo scoping according to RFC 3484. 10634c6867a8SHajimu UMEMOTO */ 10644c6867a8SHajimu UMEMOTO sa4 = (struct sockaddr_in *)sa; 10654c6867a8SHajimu UMEMOTO /* IPv4 autoconfiguration addresses have link-local scope. */ 10664c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 169 && 10674c6867a8SHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 254) 10684c6867a8SHajimu UMEMOTO return(2); 10694c6867a8SHajimu UMEMOTO /* Private addresses have site-local scope. */ 10704c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 10 || 10714c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 172 && 10724c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) || 10734c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 192 && 10744c6867a8SHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 168)) 1075251b48a1SHajimu UMEMOTO return(14); /* XXX: It should be 5 unless NAT */ 10764c6867a8SHajimu UMEMOTO /* Loopback addresses have link-local scope. */ 10774c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 127) 10784c6867a8SHajimu UMEMOTO return(2); 10794c6867a8SHajimu UMEMOTO return(14); 10804c6867a8SHajimu UMEMOTO break; 10814c6867a8SHajimu UMEMOTO default: 10824c6867a8SHajimu UMEMOTO errno = EAFNOSUPPORT; /* is this a good error? */ 10834c6867a8SHajimu UMEMOTO return(-1); 10844c6867a8SHajimu UMEMOTO } 10854c6867a8SHajimu UMEMOTO } 10864c6867a8SHajimu UMEMOTO 1087bc421492SHajimu UMEMOTO static int 1088bc421492SHajimu UMEMOTO explore_copy(const struct addrinfo *pai, const struct addrinfo *src0, 1089bc421492SHajimu UMEMOTO struct addrinfo **res) 1090bc421492SHajimu UMEMOTO { 1091bc421492SHajimu UMEMOTO int error; 1092bc421492SHajimu UMEMOTO struct addrinfo sentinel, *cur; 1093bc421492SHajimu UMEMOTO const struct addrinfo *src; 1094bc421492SHajimu UMEMOTO 1095bc421492SHajimu UMEMOTO error = 0; 1096bc421492SHajimu UMEMOTO sentinel.ai_next = NULL; 1097bc421492SHajimu UMEMOTO cur = &sentinel; 1098bc421492SHajimu UMEMOTO 1099bc421492SHajimu UMEMOTO for (src = src0; src != NULL; src = src->ai_next) { 1100bc421492SHajimu UMEMOTO if (src->ai_family != pai->ai_family) 1101bc421492SHajimu UMEMOTO continue; 1102bc421492SHajimu UMEMOTO 1103bc421492SHajimu UMEMOTO cur->ai_next = copy_ai(src); 1104bc421492SHajimu UMEMOTO if (!cur->ai_next) { 1105bc421492SHajimu UMEMOTO error = EAI_MEMORY; 1106bc421492SHajimu UMEMOTO goto fail; 1107bc421492SHajimu UMEMOTO } 1108bc421492SHajimu UMEMOTO 1109bc421492SHajimu UMEMOTO cur->ai_next->ai_socktype = pai->ai_socktype; 1110bc421492SHajimu UMEMOTO cur->ai_next->ai_protocol = pai->ai_protocol; 1111bc421492SHajimu UMEMOTO cur = cur->ai_next; 1112bc421492SHajimu UMEMOTO } 1113bc421492SHajimu UMEMOTO 1114bc421492SHajimu UMEMOTO *res = sentinel.ai_next; 1115bc421492SHajimu UMEMOTO return 0; 1116bc421492SHajimu UMEMOTO 1117bc421492SHajimu UMEMOTO fail: 1118bc421492SHajimu UMEMOTO freeaddrinfo(sentinel.ai_next); 1119bc421492SHajimu UMEMOTO return error; 1120bc421492SHajimu UMEMOTO } 1121bc421492SHajimu UMEMOTO 11227d56d374SYoshinobu Inoue /* 11237d56d374SYoshinobu Inoue * hostname == NULL. 11247d56d374SYoshinobu Inoue * passive socket -> anyaddr (0.0.0.0 or ::) 11257d56d374SYoshinobu Inoue * non-passive socket -> localhost (127.0.0.1 or ::1) 11267d56d374SYoshinobu Inoue */ 11277d56d374SYoshinobu Inoue static int 112890d1d883SHajimu UMEMOTO explore_null(const struct addrinfo *pai, const char *servname, 112990d1d883SHajimu UMEMOTO struct addrinfo **res) 11307d56d374SYoshinobu Inoue { 1131bf838688SHajimu UMEMOTO int s; 11327d56d374SYoshinobu Inoue const struct afd *afd; 11330aceddceSHajimu UMEMOTO struct addrinfo *ai; 11347d56d374SYoshinobu Inoue int error; 11357d56d374SYoshinobu Inoue 11367d56d374SYoshinobu Inoue *res = NULL; 11370aceddceSHajimu UMEMOTO ai = NULL; 11387d56d374SYoshinobu Inoue 11397d56d374SYoshinobu Inoue /* 1140bf838688SHajimu UMEMOTO * filter out AFs that are not supported by the kernel 1141bf838688SHajimu UMEMOTO * XXX errno? 1142bf838688SHajimu UMEMOTO */ 114372531978SJilles Tjoelker s = _socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); 1144bf838688SHajimu UMEMOTO if (s < 0) { 1145bf838688SHajimu UMEMOTO if (errno != EMFILE) 1146bf838688SHajimu UMEMOTO return 0; 1147bf838688SHajimu UMEMOTO } else 1148bf838688SHajimu UMEMOTO _close(s); 1149bf838688SHajimu UMEMOTO 11507d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 11517d56d374SYoshinobu Inoue if (afd == NULL) 11527d56d374SYoshinobu Inoue return 0; 11537d56d374SYoshinobu Inoue 1154a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_PASSIVE) { 11550aceddceSHajimu UMEMOTO GET_AI(ai, afd, afd->a_addrany); 11560aceddceSHajimu UMEMOTO GET_PORT(ai, servname); 1157a42af91cSHajimu UMEMOTO } else { 11580aceddceSHajimu UMEMOTO GET_AI(ai, afd, afd->a_loopback); 11590aceddceSHajimu UMEMOTO GET_PORT(ai, servname); 1160a42af91cSHajimu UMEMOTO } 11617d56d374SYoshinobu Inoue 11620aceddceSHajimu UMEMOTO *res = ai; 11637d56d374SYoshinobu Inoue return 0; 11647d56d374SYoshinobu Inoue 11657d56d374SYoshinobu Inoue free: 11660aceddceSHajimu UMEMOTO if (ai != NULL) 11670aceddceSHajimu UMEMOTO freeaddrinfo(ai); 11687d56d374SYoshinobu Inoue return error; 11697d56d374SYoshinobu Inoue } 11707d56d374SYoshinobu Inoue 11717d56d374SYoshinobu Inoue /* 11727d56d374SYoshinobu Inoue * numeric hostname 11737d56d374SYoshinobu Inoue */ 11747d56d374SYoshinobu Inoue static int 117590d1d883SHajimu UMEMOTO explore_numeric(const struct addrinfo *pai, const char *hostname, 117690d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res, const char *canonname) 11777d56d374SYoshinobu Inoue { 11787d56d374SYoshinobu Inoue const struct afd *afd; 11790aceddceSHajimu UMEMOTO struct addrinfo *ai; 11807d56d374SYoshinobu Inoue int error; 11817d56d374SYoshinobu Inoue char pton[PTON_MAX]; 11827d56d374SYoshinobu Inoue 11837d56d374SYoshinobu Inoue *res = NULL; 11840aceddceSHajimu UMEMOTO ai = NULL; 11857d56d374SYoshinobu Inoue 11867d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 11877d56d374SYoshinobu Inoue if (afd == NULL) 11887d56d374SYoshinobu Inoue return 0; 11897d56d374SYoshinobu Inoue 1190b826397aSJun-ichiro itojun Hagino switch (afd->a_af) { 1191b826397aSJun-ichiro itojun Hagino case AF_INET: 11920cf41987SHajimu UMEMOTO /* 11930cf41987SHajimu UMEMOTO * RFC3493 requires getaddrinfo() to accept AF_INET formats 11940cf41987SHajimu UMEMOTO * that are accepted by inet_addr() and its family. The 11950cf41987SHajimu UMEMOTO * accepted forms includes the "classful" one, which inet_pton 11960cf41987SHajimu UMEMOTO * does not accept. So we need to separate the case for 11970cf41987SHajimu UMEMOTO * AF_INET. 11980cf41987SHajimu UMEMOTO */ 1199da83bf18SHajimu UMEMOTO if (inet_aton(hostname, (struct in_addr *)pton) != 1) 1200da83bf18SHajimu UMEMOTO return 0; 1201b826397aSJun-ichiro itojun Hagino break; 1202b826397aSJun-ichiro itojun Hagino default: 1203da83bf18SHajimu UMEMOTO if (inet_pton(afd->a_af, hostname, pton) != 1) 1204da83bf18SHajimu UMEMOTO return 0; 1205da83bf18SHajimu UMEMOTO break; 1206da83bf18SHajimu UMEMOTO } 1207da83bf18SHajimu UMEMOTO 1208da83bf18SHajimu UMEMOTO if (pai->ai_family == afd->a_af) { 12090aceddceSHajimu UMEMOTO GET_AI(ai, afd, pton); 12100aceddceSHajimu UMEMOTO GET_PORT(ai, servname); 12118755b918SHajimu UMEMOTO if ((pai->ai_flags & AI_CANONNAME)) { 12128755b918SHajimu UMEMOTO /* 1213da83bf18SHajimu UMEMOTO * Set the numeric address itself as the canonical 1214da83bf18SHajimu UMEMOTO * name, based on a clarification in RFC3493. 12158755b918SHajimu UMEMOTO */ 12160aceddceSHajimu UMEMOTO GET_CANONNAME(ai, canonname); 12178755b918SHajimu UMEMOTO } 1218da83bf18SHajimu UMEMOTO } else { 1219da83bf18SHajimu UMEMOTO /* 1220da83bf18SHajimu UMEMOTO * XXX: This should not happen since we already matched the AF 1221da83bf18SHajimu UMEMOTO * by find_afd. 1222da83bf18SHajimu UMEMOTO */ 1223da83bf18SHajimu UMEMOTO ERR(EAI_FAMILY); 1224b826397aSJun-ichiro itojun Hagino } 12257d56d374SYoshinobu Inoue 12260aceddceSHajimu UMEMOTO *res = ai; 12277d56d374SYoshinobu Inoue return 0; 12287d56d374SYoshinobu Inoue 12297d56d374SYoshinobu Inoue free: 12307d56d374SYoshinobu Inoue bad: 12310aceddceSHajimu UMEMOTO if (ai != NULL) 12320aceddceSHajimu UMEMOTO freeaddrinfo(ai); 12337d56d374SYoshinobu Inoue return error; 12347d56d374SYoshinobu Inoue } 12357d56d374SYoshinobu Inoue 12367d56d374SYoshinobu Inoue /* 12377d56d374SYoshinobu Inoue * numeric hostname with scope 12387d56d374SYoshinobu Inoue */ 12397d56d374SYoshinobu Inoue static int 124090d1d883SHajimu UMEMOTO explore_numeric_scope(const struct addrinfo *pai, const char *hostname, 124190d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res) 12427d56d374SYoshinobu Inoue { 1243a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6) 12448755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname); 12457d56d374SYoshinobu Inoue #else 12467d56d374SYoshinobu Inoue const struct afd *afd; 12477d56d374SYoshinobu Inoue struct addrinfo *cur; 12487d56d374SYoshinobu Inoue int error; 1249a42af91cSHajimu UMEMOTO char *cp, *hostname2 = NULL, *scope, *addr; 12507d56d374SYoshinobu Inoue struct sockaddr_in6 *sin6; 12517d56d374SYoshinobu Inoue 12527d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 12537d56d374SYoshinobu Inoue if (afd == NULL) 12547d56d374SYoshinobu Inoue return 0; 1255248aee62SJacques Vidrine 12567d56d374SYoshinobu Inoue if (!afd->a_scoped) 12578755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname); 12587d56d374SYoshinobu Inoue 12597d56d374SYoshinobu Inoue cp = strchr(hostname, SCOPE_DELIMITER); 12607d56d374SYoshinobu Inoue if (cp == NULL) 12618755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname); 12627d56d374SYoshinobu Inoue 12637d56d374SYoshinobu Inoue /* 12647d56d374SYoshinobu Inoue * Handle special case of <scoped_address><delimiter><scope id> 12657d56d374SYoshinobu Inoue */ 12667d56d374SYoshinobu Inoue hostname2 = strdup(hostname); 12677d56d374SYoshinobu Inoue if (hostname2 == NULL) 12687d56d374SYoshinobu Inoue return EAI_MEMORY; 12697d56d374SYoshinobu Inoue /* terminate at the delimiter */ 12707d56d374SYoshinobu Inoue hostname2[cp - hostname] = '\0'; 1271a42af91cSHajimu UMEMOTO addr = hostname2; 1272a42af91cSHajimu UMEMOTO scope = cp + 1; 12737d56d374SYoshinobu Inoue 12748755b918SHajimu UMEMOTO error = explore_numeric(pai, addr, servname, res, hostname); 12757d56d374SYoshinobu Inoue if (error == 0) { 1276a89c1d30SHajimu UMEMOTO u_int32_t scopeid; 1277a42af91cSHajimu UMEMOTO 12787d56d374SYoshinobu Inoue for (cur = *res; cur; cur = cur->ai_next) { 12797d56d374SYoshinobu Inoue if (cur->ai_family != AF_INET6) 12807d56d374SYoshinobu Inoue continue; 1281a42af91cSHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; 1282a89c1d30SHajimu UMEMOTO if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { 1283a42af91cSHajimu UMEMOTO free(hostname2); 1284bc421492SHajimu UMEMOTO freeaddrinfo(*res); 1285bc421492SHajimu UMEMOTO *res = NULL; 128637b3e941SHajimu UMEMOTO return(EAI_NONAME); /* XXX: is return OK? */ 1287a42af91cSHajimu UMEMOTO } 1288a42af91cSHajimu UMEMOTO sin6->sin6_scope_id = scopeid; 12897d56d374SYoshinobu Inoue } 12907d56d374SYoshinobu Inoue } 12917d56d374SYoshinobu Inoue 12927d56d374SYoshinobu Inoue free(hostname2); 12937d56d374SYoshinobu Inoue 1294bc421492SHajimu UMEMOTO if (error && *res) { 1295bc421492SHajimu UMEMOTO freeaddrinfo(*res); 1296bc421492SHajimu UMEMOTO *res = NULL; 1297bc421492SHajimu UMEMOTO } 12987d56d374SYoshinobu Inoue return error; 12997d56d374SYoshinobu Inoue #endif 13007d56d374SYoshinobu Inoue } 13017d56d374SYoshinobu Inoue 13027d56d374SYoshinobu Inoue static int 130390d1d883SHajimu UMEMOTO get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str) 13047d56d374SYoshinobu Inoue { 13057d56d374SYoshinobu Inoue if ((pai->ai_flags & AI_CANONNAME) != 0) { 13068743b270SHajimu UMEMOTO ai->ai_canonname = strdup(str); 13077d56d374SYoshinobu Inoue if (ai->ai_canonname == NULL) 13087d56d374SYoshinobu Inoue return EAI_MEMORY; 13097d56d374SYoshinobu Inoue } 13107d56d374SYoshinobu Inoue return 0; 13117d56d374SYoshinobu Inoue } 13127d56d374SYoshinobu Inoue 13137d56d374SYoshinobu Inoue static struct addrinfo * 131490d1d883SHajimu UMEMOTO get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr) 13157d56d374SYoshinobu Inoue { 13167d56d374SYoshinobu Inoue char *p; 13177d56d374SYoshinobu Inoue struct addrinfo *ai; 13187d56d374SYoshinobu Inoue #ifdef FAITH 13197d56d374SYoshinobu Inoue struct in6_addr faith_prefix; 13207d56d374SYoshinobu Inoue char *fp_str; 13217d56d374SYoshinobu Inoue int translate = 0; 13227d56d374SYoshinobu Inoue #endif 13237d56d374SYoshinobu Inoue 13247d56d374SYoshinobu Inoue #ifdef FAITH 13257d56d374SYoshinobu Inoue /* 13267d56d374SYoshinobu Inoue * Transfrom an IPv4 addr into a special IPv6 addr format for 13277d56d374SYoshinobu Inoue * IPv6->IPv4 translation gateway. (only TCP is supported now) 13287d56d374SYoshinobu Inoue * 13297d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 13307d56d374SYoshinobu Inoue * | faith prefix part (12 bytes) | embedded | 13317d56d374SYoshinobu Inoue * | | IPv4 addr part (4 bytes) 13327d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 13337d56d374SYoshinobu Inoue * 13347d56d374SYoshinobu Inoue * faith prefix part is specified as ascii IPv6 addr format 13357d56d374SYoshinobu Inoue * in environmental variable GAI. 13367d56d374SYoshinobu Inoue * For FAITH to work correctly, routing to faith prefix must be 13377d56d374SYoshinobu Inoue * setup toward a machine where a FAITH daemon operates. 13387d56d374SYoshinobu Inoue * Also, the machine must enable some mechanizm 13397d56d374SYoshinobu Inoue * (e.g. faith interface hack) to divert those packet with 13407d56d374SYoshinobu Inoue * faith prefixed destination addr to user-land FAITH daemon. 13417d56d374SYoshinobu Inoue */ 13427d56d374SYoshinobu Inoue fp_str = getenv("GAI"); 13437d56d374SYoshinobu Inoue if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 13447d56d374SYoshinobu Inoue afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 13457d56d374SYoshinobu Inoue u_int32_t v4a; 13467d56d374SYoshinobu Inoue u_int8_t v4a_top; 13477d56d374SYoshinobu Inoue 13487d56d374SYoshinobu Inoue memcpy(&v4a, addr, sizeof v4a); 13497d56d374SYoshinobu Inoue v4a_top = v4a >> IN_CLASSA_NSHIFT; 13507d56d374SYoshinobu Inoue if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 13517d56d374SYoshinobu Inoue v4a_top != 0 && v4a != IN_LOOPBACKNET) { 13527d56d374SYoshinobu Inoue afd = &afdl[N_INET6]; 13537d56d374SYoshinobu Inoue memcpy(&faith_prefix.s6_addr[12], addr, 13547d56d374SYoshinobu Inoue sizeof(struct in_addr)); 13557d56d374SYoshinobu Inoue translate = 1; 13567d56d374SYoshinobu Inoue } 13577d56d374SYoshinobu Inoue } 13587d56d374SYoshinobu Inoue #endif 13597d56d374SYoshinobu Inoue 13607d56d374SYoshinobu Inoue ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 13617d56d374SYoshinobu Inoue + (afd->a_socklen)); 13627d56d374SYoshinobu Inoue if (ai == NULL) 13637d56d374SYoshinobu Inoue return NULL; 13647d56d374SYoshinobu Inoue 13657d56d374SYoshinobu Inoue memcpy(ai, pai, sizeof(struct addrinfo)); 1366a42af91cSHajimu UMEMOTO ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 1367a42af91cSHajimu UMEMOTO memset(ai->ai_addr, 0, (size_t)afd->a_socklen); 13687d56d374SYoshinobu Inoue ai->ai_addr->sa_len = afd->a_socklen; 13697d56d374SYoshinobu Inoue ai->ai_addrlen = afd->a_socklen; 13707d56d374SYoshinobu Inoue ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 1371248aee62SJacques Vidrine p = (char *)(void *)(ai->ai_addr); 13727d56d374SYoshinobu Inoue #ifdef FAITH 13737d56d374SYoshinobu Inoue if (translate == 1) 1374248aee62SJacques Vidrine memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen); 13757d56d374SYoshinobu Inoue else 13767d56d374SYoshinobu Inoue #endif 1377248aee62SJacques Vidrine memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); 13787d56d374SYoshinobu Inoue return ai; 13797d56d374SYoshinobu Inoue } 13807d56d374SYoshinobu Inoue 1381bc421492SHajimu UMEMOTO /* XXX need to malloc() the same way we do from other functions! */ 1382bc421492SHajimu UMEMOTO static struct addrinfo * 1383bc421492SHajimu UMEMOTO copy_ai(const struct addrinfo *pai) 1384bc421492SHajimu UMEMOTO { 1385bc421492SHajimu UMEMOTO struct addrinfo *ai; 1386bc421492SHajimu UMEMOTO size_t l; 1387bc421492SHajimu UMEMOTO 1388bc421492SHajimu UMEMOTO l = sizeof(*ai) + pai->ai_addrlen; 1389bc421492SHajimu UMEMOTO if ((ai = (struct addrinfo *)malloc(l)) == NULL) 1390bc421492SHajimu UMEMOTO return NULL; 1391bc421492SHajimu UMEMOTO memset(ai, 0, l); 1392bc421492SHajimu UMEMOTO memcpy(ai, pai, sizeof(*ai)); 1393bc421492SHajimu UMEMOTO ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 1394bc421492SHajimu UMEMOTO memcpy(ai->ai_addr, pai->ai_addr, pai->ai_addrlen); 1395bc421492SHajimu UMEMOTO 1396bc421492SHajimu UMEMOTO if (pai->ai_canonname) { 1397bc421492SHajimu UMEMOTO l = strlen(pai->ai_canonname) + 1; 1398bc421492SHajimu UMEMOTO if ((ai->ai_canonname = malloc(l)) == NULL) { 1399bc421492SHajimu UMEMOTO free(ai); 1400bc421492SHajimu UMEMOTO return NULL; 1401bc421492SHajimu UMEMOTO } 1402bc421492SHajimu UMEMOTO strlcpy(ai->ai_canonname, pai->ai_canonname, l); 1403bc421492SHajimu UMEMOTO } else { 1404bc421492SHajimu UMEMOTO /* just to make sure */ 1405bc421492SHajimu UMEMOTO ai->ai_canonname = NULL; 1406bc421492SHajimu UMEMOTO } 1407bc421492SHajimu UMEMOTO 1408bc421492SHajimu UMEMOTO ai->ai_next = NULL; 1409bc421492SHajimu UMEMOTO 1410bc421492SHajimu UMEMOTO return ai; 1411bc421492SHajimu UMEMOTO } 1412bc421492SHajimu UMEMOTO 14137d56d374SYoshinobu Inoue static int 141490d1d883SHajimu UMEMOTO get_portmatch(const struct addrinfo *ai, const char *servname) 14157d56d374SYoshinobu Inoue { 1416a42af91cSHajimu UMEMOTO 1417c224435eSHajimu UMEMOTO /* get_port does not touch first argument when matchonly == 1. */ 1418a42af91cSHajimu UMEMOTO /* LINTED const cast */ 14197d56d374SYoshinobu Inoue return get_port((struct addrinfo *)ai, servname, 1); 14207d56d374SYoshinobu Inoue } 14217d56d374SYoshinobu Inoue 14227d56d374SYoshinobu Inoue static int 142390d1d883SHajimu UMEMOTO get_port(struct addrinfo *ai, const char *servname, int matchonly) 14247d56d374SYoshinobu Inoue { 14257d56d374SYoshinobu Inoue const char *proto; 14267d56d374SYoshinobu Inoue struct servent *sp; 1427e24e9d99SHajimu UMEMOTO int port, error; 14287d56d374SYoshinobu Inoue int allownumeric; 14297d56d374SYoshinobu Inoue 14307d56d374SYoshinobu Inoue if (servname == NULL) 14317d56d374SYoshinobu Inoue return 0; 1432a42af91cSHajimu UMEMOTO switch (ai->ai_family) { 1433a42af91cSHajimu UMEMOTO case AF_INET: 1434a42af91cSHajimu UMEMOTO #ifdef AF_INET6 1435a42af91cSHajimu UMEMOTO case AF_INET6: 14367d56d374SYoshinobu Inoue #endif 1437a42af91cSHajimu UMEMOTO break; 1438a42af91cSHajimu UMEMOTO default: 14397d56d374SYoshinobu Inoue return 0; 1440a42af91cSHajimu UMEMOTO } 14417d56d374SYoshinobu Inoue 14427d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 14437d56d374SYoshinobu Inoue case SOCK_RAW: 14447d56d374SYoshinobu Inoue return EAI_SERVICE; 14457d56d374SYoshinobu Inoue case SOCK_DGRAM: 14467d56d374SYoshinobu Inoue case SOCK_STREAM: 14474d756307SHajimu UMEMOTO case SOCK_SEQPACKET: 14487d56d374SYoshinobu Inoue allownumeric = 1; 14497d56d374SYoshinobu Inoue break; 14507d56d374SYoshinobu Inoue case ANY: 1451d53978e3SHajimu UMEMOTO switch (ai->ai_family) { 1452d53978e3SHajimu UMEMOTO case AF_INET: 1453d53978e3SHajimu UMEMOTO #ifdef AF_INET6 1454d53978e3SHajimu UMEMOTO case AF_INET6: 1455d53978e3SHajimu UMEMOTO #endif 1456d53978e3SHajimu UMEMOTO allownumeric = 1; 1457d53978e3SHajimu UMEMOTO break; 1458d53978e3SHajimu UMEMOTO default: 14597d56d374SYoshinobu Inoue allownumeric = 0; 14607d56d374SYoshinobu Inoue break; 1461d53978e3SHajimu UMEMOTO } 1462d53978e3SHajimu UMEMOTO break; 14637d56d374SYoshinobu Inoue default: 14647d56d374SYoshinobu Inoue return EAI_SOCKTYPE; 14657d56d374SYoshinobu Inoue } 14667d56d374SYoshinobu Inoue 1467e24e9d99SHajimu UMEMOTO error = str2number(servname, &port); 1468e24e9d99SHajimu UMEMOTO if (error == 0) { 14697d56d374SYoshinobu Inoue if (!allownumeric) 14707d56d374SYoshinobu Inoue return EAI_SERVICE; 14717d56d374SYoshinobu Inoue if (port < 0 || port > 65535) 14727d56d374SYoshinobu Inoue return EAI_SERVICE; 1473dd521ef1SHajimu UMEMOTO port = htons(port); 14747d56d374SYoshinobu Inoue } else { 1475c224435eSHajimu UMEMOTO if (ai->ai_flags & AI_NUMERICSERV) 1476c224435eSHajimu UMEMOTO return EAI_NONAME; 14774d756307SHajimu UMEMOTO 14784d756307SHajimu UMEMOTO switch (ai->ai_protocol) { 14794d756307SHajimu UMEMOTO case IPPROTO_UDP: 14807d56d374SYoshinobu Inoue proto = "udp"; 14817d56d374SYoshinobu Inoue break; 14824d756307SHajimu UMEMOTO case IPPROTO_TCP: 14837d56d374SYoshinobu Inoue proto = "tcp"; 14847d56d374SYoshinobu Inoue break; 14854d756307SHajimu UMEMOTO case IPPROTO_SCTP: 14864d756307SHajimu UMEMOTO proto = "sctp"; 14874d756307SHajimu UMEMOTO break; 1488e06e816fSKevin Lo case IPPROTO_UDPLITE: 1489e06e816fSKevin Lo proto = "udplite"; 1490e06e816fSKevin Lo break; 14917d56d374SYoshinobu Inoue default: 14927d56d374SYoshinobu Inoue proto = NULL; 14937d56d374SYoshinobu Inoue break; 14947d56d374SYoshinobu Inoue } 14957d56d374SYoshinobu Inoue 149662e8b17dSHajimu UMEMOTO if ((sp = getservbyname(servname, proto)) == NULL) 14977d56d374SYoshinobu Inoue return EAI_SERVICE; 14987d56d374SYoshinobu Inoue port = sp->s_port; 14997d56d374SYoshinobu Inoue } 15007d56d374SYoshinobu Inoue 15017d56d374SYoshinobu Inoue if (!matchonly) { 15027d56d374SYoshinobu Inoue switch (ai->ai_family) { 15037d56d374SYoshinobu Inoue case AF_INET: 1504a42af91cSHajimu UMEMOTO ((struct sockaddr_in *)(void *) 1505a42af91cSHajimu UMEMOTO ai->ai_addr)->sin_port = port; 15067d56d374SYoshinobu Inoue break; 15077d56d374SYoshinobu Inoue #ifdef INET6 15087d56d374SYoshinobu Inoue case AF_INET6: 1509a42af91cSHajimu UMEMOTO ((struct sockaddr_in6 *)(void *) 1510a42af91cSHajimu UMEMOTO ai->ai_addr)->sin6_port = port; 15117d56d374SYoshinobu Inoue break; 15127d56d374SYoshinobu Inoue #endif 15137d56d374SYoshinobu Inoue } 15147d56d374SYoshinobu Inoue } 15157d56d374SYoshinobu Inoue 15167d56d374SYoshinobu Inoue return 0; 15177d56d374SYoshinobu Inoue } 15187d56d374SYoshinobu Inoue 15197d56d374SYoshinobu Inoue static const struct afd * 152090d1d883SHajimu UMEMOTO find_afd(int af) 15217d56d374SYoshinobu Inoue { 15227d56d374SYoshinobu Inoue const struct afd *afd; 15237d56d374SYoshinobu Inoue 15247d56d374SYoshinobu Inoue if (af == PF_UNSPEC) 15257d56d374SYoshinobu Inoue return NULL; 15267d56d374SYoshinobu Inoue for (afd = afdl; afd->a_af; afd++) { 15277d56d374SYoshinobu Inoue if (afd->a_af == af) 15287d56d374SYoshinobu Inoue return afd; 15297d56d374SYoshinobu Inoue } 15307d56d374SYoshinobu Inoue return NULL; 15317d56d374SYoshinobu Inoue } 1532a42af91cSHajimu UMEMOTO 1533a42af91cSHajimu UMEMOTO /* 1534*6f63a114SHajimu UMEMOTO * post-2553: AI_ADDRCONFIG check. Determines which address families are 1535*6f63a114SHajimu UMEMOTO * configured on the local system and correlates with pai->ai_family value. 1536*6f63a114SHajimu UMEMOTO * If an address family is not configured on the system, it will not be 1537*6f63a114SHajimu UMEMOTO * queried for. For this purpose, loopback addresses are not considered 1538*6f63a114SHajimu UMEMOTO * configured addresses. 1539bf838688SHajimu UMEMOTO * 1540bf838688SHajimu UMEMOTO * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with 1541bf838688SHajimu UMEMOTO * _dns_getaddrinfo. 1542a42af91cSHajimu UMEMOTO */ 1543a42af91cSHajimu UMEMOTO static int 154490d1d883SHajimu UMEMOTO addrconfig(struct addrinfo *pai) 1545a42af91cSHajimu UMEMOTO { 1546*6f63a114SHajimu UMEMOTO struct ifaddrs *ifaddrs, *ifa; 1547*6f63a114SHajimu UMEMOTO int seen_inet = 0, seen_inet6 = 0; 1548a42af91cSHajimu UMEMOTO 1549*6f63a114SHajimu UMEMOTO if (getifaddrs(&ifaddrs) != 0) 1550a42af91cSHajimu UMEMOTO return 0; 1551*6f63a114SHajimu UMEMOTO 1552*6f63a114SHajimu UMEMOTO for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { 1553*6f63a114SHajimu UMEMOTO if (ifa->ifa_addr == NULL || (ifa->ifa_flags & IFF_UP) == 0) 1554*6f63a114SHajimu UMEMOTO continue; 1555*6f63a114SHajimu UMEMOTO if ((ifa->ifa_flags & IFT_LOOP) != 0) 1556*6f63a114SHajimu UMEMOTO continue; 1557*6f63a114SHajimu UMEMOTO switch (ifa->ifa_addr->sa_family) { 1558*6f63a114SHajimu UMEMOTO case AF_INET: 1559*6f63a114SHajimu UMEMOTO seen_inet = 1; 1560*6f63a114SHajimu UMEMOTO break; 1561*6f63a114SHajimu UMEMOTO #ifdef INET6 1562*6f63a114SHajimu UMEMOTO case AF_INET6: 1563*6f63a114SHajimu UMEMOTO if (!seen_inet6 && !is_ifdisabled(ifa->ifa_name)) 1564*6f63a114SHajimu UMEMOTO seen_inet6 = 1; 1565*6f63a114SHajimu UMEMOTO break; 1566*6f63a114SHajimu UMEMOTO #endif 1567bf838688SHajimu UMEMOTO } 1568*6f63a114SHajimu UMEMOTO } 1569*6f63a114SHajimu UMEMOTO freeifaddrs(ifaddrs); 1570*6f63a114SHajimu UMEMOTO 1571*6f63a114SHajimu UMEMOTO switch(pai->ai_family) { 1572*6f63a114SHajimu UMEMOTO case AF_INET6: 1573*6f63a114SHajimu UMEMOTO return seen_inet6; 1574*6f63a114SHajimu UMEMOTO case AF_INET: 1575*6f63a114SHajimu UMEMOTO return seen_inet; 1576*6f63a114SHajimu UMEMOTO case AF_UNSPEC: 1577*6f63a114SHajimu UMEMOTO if (seen_inet == seen_inet6) 1578*6f63a114SHajimu UMEMOTO return seen_inet; 1579*6f63a114SHajimu UMEMOTO pai->ai_family = seen_inet ? AF_INET : AF_INET6; 1580*6f63a114SHajimu UMEMOTO return 1; 1581*6f63a114SHajimu UMEMOTO } 1582a42af91cSHajimu UMEMOTO return 1; 1583a42af91cSHajimu UMEMOTO } 1584a42af91cSHajimu UMEMOTO 1585a42af91cSHajimu UMEMOTO #ifdef INET6 1586*6f63a114SHajimu UMEMOTO static int 1587*6f63a114SHajimu UMEMOTO is_ifdisabled(char *name) 1588*6f63a114SHajimu UMEMOTO { 1589*6f63a114SHajimu UMEMOTO struct in6_ndireq nd; 1590*6f63a114SHajimu UMEMOTO int fd; 1591*6f63a114SHajimu UMEMOTO 1592*6f63a114SHajimu UMEMOTO if ((fd = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1593*6f63a114SHajimu UMEMOTO return -1; 1594*6f63a114SHajimu UMEMOTO memset(&nd, 0, sizeof(nd)); 1595*6f63a114SHajimu UMEMOTO strlcpy(nd.ifname, name, sizeof(nd.ifname)); 1596*6f63a114SHajimu UMEMOTO if (_ioctl(fd, SIOCGIFINFO_IN6, &nd) < 0) { 1597*6f63a114SHajimu UMEMOTO _close(fd); 1598*6f63a114SHajimu UMEMOTO return -1; 1599*6f63a114SHajimu UMEMOTO } 1600*6f63a114SHajimu UMEMOTO _close(fd); 1601*6f63a114SHajimu UMEMOTO return ((nd.ndi.flags & ND6_IFF_IFDISABLED) != 0); 1602*6f63a114SHajimu UMEMOTO } 1603*6f63a114SHajimu UMEMOTO 1604a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */ 1605a42af91cSHajimu UMEMOTO static int 160690d1d883SHajimu UMEMOTO ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid) 1607a42af91cSHajimu UMEMOTO { 1608a89c1d30SHajimu UMEMOTO u_long lscopeid; 1609bf838688SHajimu UMEMOTO struct in6_addr *a6; 1610a42af91cSHajimu UMEMOTO char *ep; 1611a42af91cSHajimu UMEMOTO 1612bf838688SHajimu UMEMOTO a6 = &sin6->sin6_addr; 1613bf838688SHajimu UMEMOTO 1614ec20fe00SJun-ichiro itojun Hagino /* empty scopeid portion is invalid */ 1615ec20fe00SJun-ichiro itojun Hagino if (*scope == '\0') 1616ec20fe00SJun-ichiro itojun Hagino return -1; 1617ec20fe00SJun-ichiro itojun Hagino 1618324930d7SHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) || 1619324930d7SHajimu UMEMOTO IN6_IS_ADDR_MC_NODELOCAL(a6)) { 1620a42af91cSHajimu UMEMOTO /* 1621a42af91cSHajimu UMEMOTO * We currently assume a one-to-one mapping between links 1622a42af91cSHajimu UMEMOTO * and interfaces, so we simply use interface indices for 1623a42af91cSHajimu UMEMOTO * like-local scopes. 1624a42af91cSHajimu UMEMOTO */ 1625a89c1d30SHajimu UMEMOTO *scopeid = if_nametoindex(scope); 1626a89c1d30SHajimu UMEMOTO if (*scopeid == 0) 1627a42af91cSHajimu UMEMOTO goto trynumeric; 1628a89c1d30SHajimu UMEMOTO return 0; 1629a42af91cSHajimu UMEMOTO } 1630a42af91cSHajimu UMEMOTO 1631a42af91cSHajimu UMEMOTO /* still unclear about literal, allow numeric only - placeholder */ 1632a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) 1633a42af91cSHajimu UMEMOTO goto trynumeric; 1634a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) 1635a42af91cSHajimu UMEMOTO goto trynumeric; 1636a42af91cSHajimu UMEMOTO else 1637a42af91cSHajimu UMEMOTO goto trynumeric; /* global */ 1638a42af91cSHajimu UMEMOTO 1639a42af91cSHajimu UMEMOTO /* try to convert to a numeric id as a last resort */ 1640a42af91cSHajimu UMEMOTO trynumeric: 1641a89c1d30SHajimu UMEMOTO errno = 0; 1642a89c1d30SHajimu UMEMOTO lscopeid = strtoul(scope, &ep, 10); 1643a89c1d30SHajimu UMEMOTO *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); 1644a89c1d30SHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) 1645a89c1d30SHajimu UMEMOTO return 0; 1646a42af91cSHajimu UMEMOTO else 1647a42af91cSHajimu UMEMOTO return -1; 1648a42af91cSHajimu UMEMOTO } 1649a42af91cSHajimu UMEMOTO #endif 1650a42af91cSHajimu UMEMOTO 165106a99fe3SHajimu UMEMOTO 165206a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 165306a99fe3SHajimu UMEMOTO static int 165406a99fe3SHajimu UMEMOTO addrinfo_id_func(char *buffer, size_t *buffer_size, va_list ap, 165506a99fe3SHajimu UMEMOTO void *cache_mdata) 165606a99fe3SHajimu UMEMOTO { 165706a99fe3SHajimu UMEMOTO res_state statp; 165806a99fe3SHajimu UMEMOTO u_long res_options; 165906a99fe3SHajimu UMEMOTO 166006a99fe3SHajimu UMEMOTO const int op_id = 0; /* identifies the getaddrinfo for the cache */ 166106a99fe3SHajimu UMEMOTO char *hostname; 166206a99fe3SHajimu UMEMOTO struct addrinfo *hints; 166306a99fe3SHajimu UMEMOTO 166406a99fe3SHajimu UMEMOTO char *p; 166506a99fe3SHajimu UMEMOTO int ai_flags, ai_family, ai_socktype, ai_protocol; 166606a99fe3SHajimu UMEMOTO size_t desired_size, size; 166706a99fe3SHajimu UMEMOTO 166806a99fe3SHajimu UMEMOTO statp = __res_state(); 166906a99fe3SHajimu UMEMOTO res_options = statp->options & (RES_RECURSE | RES_DEFNAMES | 167006a99fe3SHajimu UMEMOTO RES_DNSRCH | RES_NOALIASES | RES_USE_INET6); 167106a99fe3SHajimu UMEMOTO 167206a99fe3SHajimu UMEMOTO hostname = va_arg(ap, char *); 167306a99fe3SHajimu UMEMOTO hints = va_arg(ap, struct addrinfo *); 167406a99fe3SHajimu UMEMOTO 167506a99fe3SHajimu UMEMOTO desired_size = sizeof(res_options) + sizeof(int) + sizeof(int) * 4; 167606a99fe3SHajimu UMEMOTO if (hostname != NULL) { 167706a99fe3SHajimu UMEMOTO size = strlen(hostname); 167806a99fe3SHajimu UMEMOTO desired_size += size + 1; 167906a99fe3SHajimu UMEMOTO } else 168006a99fe3SHajimu UMEMOTO size = 0; 168106a99fe3SHajimu UMEMOTO 168206a99fe3SHajimu UMEMOTO if (desired_size > *buffer_size) { 168306a99fe3SHajimu UMEMOTO *buffer_size = desired_size; 168406a99fe3SHajimu UMEMOTO return (NS_RETURN); 168506a99fe3SHajimu UMEMOTO } 168606a99fe3SHajimu UMEMOTO 168706a99fe3SHajimu UMEMOTO if (hints == NULL) 168806a99fe3SHajimu UMEMOTO ai_flags = ai_family = ai_socktype = ai_protocol = 0; 168906a99fe3SHajimu UMEMOTO else { 169006a99fe3SHajimu UMEMOTO ai_flags = hints->ai_flags; 169106a99fe3SHajimu UMEMOTO ai_family = hints->ai_family; 169206a99fe3SHajimu UMEMOTO ai_socktype = hints->ai_socktype; 169306a99fe3SHajimu UMEMOTO ai_protocol = hints->ai_protocol; 169406a99fe3SHajimu UMEMOTO } 169506a99fe3SHajimu UMEMOTO 169606a99fe3SHajimu UMEMOTO p = buffer; 169706a99fe3SHajimu UMEMOTO memcpy(p, &res_options, sizeof(res_options)); 169806a99fe3SHajimu UMEMOTO p += sizeof(res_options); 169906a99fe3SHajimu UMEMOTO 170006a99fe3SHajimu UMEMOTO memcpy(p, &op_id, sizeof(int)); 170106a99fe3SHajimu UMEMOTO p += sizeof(int); 170206a99fe3SHajimu UMEMOTO 170306a99fe3SHajimu UMEMOTO memcpy(p, &ai_flags, sizeof(int)); 170406a99fe3SHajimu UMEMOTO p += sizeof(int); 170506a99fe3SHajimu UMEMOTO 170606a99fe3SHajimu UMEMOTO memcpy(p, &ai_family, sizeof(int)); 170706a99fe3SHajimu UMEMOTO p += sizeof(int); 170806a99fe3SHajimu UMEMOTO 170906a99fe3SHajimu UMEMOTO memcpy(p, &ai_socktype, sizeof(int)); 171006a99fe3SHajimu UMEMOTO p += sizeof(int); 171106a99fe3SHajimu UMEMOTO 171206a99fe3SHajimu UMEMOTO memcpy(p, &ai_protocol, sizeof(int)); 171306a99fe3SHajimu UMEMOTO p += sizeof(int); 171406a99fe3SHajimu UMEMOTO 171506a99fe3SHajimu UMEMOTO if (hostname != NULL) 171606a99fe3SHajimu UMEMOTO memcpy(p, hostname, size); 171706a99fe3SHajimu UMEMOTO 171806a99fe3SHajimu UMEMOTO *buffer_size = desired_size; 171906a99fe3SHajimu UMEMOTO return (NS_SUCCESS); 172006a99fe3SHajimu UMEMOTO } 172106a99fe3SHajimu UMEMOTO 172206a99fe3SHajimu UMEMOTO static int 172306a99fe3SHajimu UMEMOTO addrinfo_marshal_func(char *buffer, size_t *buffer_size, void *retval, 172406a99fe3SHajimu UMEMOTO va_list ap, void *cache_mdata) 172506a99fe3SHajimu UMEMOTO { 172606a99fe3SHajimu UMEMOTO struct addrinfo *ai, *cai; 172706a99fe3SHajimu UMEMOTO char *p; 172806a99fe3SHajimu UMEMOTO size_t desired_size, size, ai_size; 172906a99fe3SHajimu UMEMOTO 173006a99fe3SHajimu UMEMOTO ai = *((struct addrinfo **)retval); 173106a99fe3SHajimu UMEMOTO 173206a99fe3SHajimu UMEMOTO desired_size = sizeof(size_t); 173306a99fe3SHajimu UMEMOTO ai_size = 0; 173406a99fe3SHajimu UMEMOTO for (cai = ai; cai != NULL; cai = cai->ai_next) { 173506a99fe3SHajimu UMEMOTO desired_size += sizeof(struct addrinfo) + cai->ai_addrlen; 173606a99fe3SHajimu UMEMOTO if (cai->ai_canonname != NULL) 173706a99fe3SHajimu UMEMOTO desired_size += sizeof(size_t) + 173806a99fe3SHajimu UMEMOTO strlen(cai->ai_canonname); 173906a99fe3SHajimu UMEMOTO ++ai_size; 174006a99fe3SHajimu UMEMOTO } 174106a99fe3SHajimu UMEMOTO 174206a99fe3SHajimu UMEMOTO if (desired_size > *buffer_size) { 174306a99fe3SHajimu UMEMOTO /* this assignment is here for future use */ 174406a99fe3SHajimu UMEMOTO errno = ERANGE; 174506a99fe3SHajimu UMEMOTO *buffer_size = desired_size; 174606a99fe3SHajimu UMEMOTO return (NS_RETURN); 174706a99fe3SHajimu UMEMOTO } 174806a99fe3SHajimu UMEMOTO 174906a99fe3SHajimu UMEMOTO memset(buffer, 0, desired_size); 175006a99fe3SHajimu UMEMOTO p = buffer; 175106a99fe3SHajimu UMEMOTO 175206a99fe3SHajimu UMEMOTO memcpy(p, &ai_size, sizeof(size_t)); 175306a99fe3SHajimu UMEMOTO p += sizeof(size_t); 175406a99fe3SHajimu UMEMOTO for (cai = ai; cai != NULL; cai = cai->ai_next) { 175506a99fe3SHajimu UMEMOTO memcpy(p, cai, sizeof(struct addrinfo)); 175606a99fe3SHajimu UMEMOTO p += sizeof(struct addrinfo); 175706a99fe3SHajimu UMEMOTO 175806a99fe3SHajimu UMEMOTO memcpy(p, cai->ai_addr, cai->ai_addrlen); 175906a99fe3SHajimu UMEMOTO p += cai->ai_addrlen; 176006a99fe3SHajimu UMEMOTO 176106a99fe3SHajimu UMEMOTO if (cai->ai_canonname != NULL) { 176206a99fe3SHajimu UMEMOTO size = strlen(cai->ai_canonname); 176306a99fe3SHajimu UMEMOTO memcpy(p, &size, sizeof(size_t)); 176406a99fe3SHajimu UMEMOTO p += sizeof(size_t); 176506a99fe3SHajimu UMEMOTO 176606a99fe3SHajimu UMEMOTO memcpy(p, cai->ai_canonname, size); 176706a99fe3SHajimu UMEMOTO p += size; 176806a99fe3SHajimu UMEMOTO } 176906a99fe3SHajimu UMEMOTO } 177006a99fe3SHajimu UMEMOTO 177106a99fe3SHajimu UMEMOTO return (NS_SUCCESS); 177206a99fe3SHajimu UMEMOTO } 177306a99fe3SHajimu UMEMOTO 177406a99fe3SHajimu UMEMOTO static int 177506a99fe3SHajimu UMEMOTO addrinfo_unmarshal_func(char *buffer, size_t buffer_size, void *retval, 177606a99fe3SHajimu UMEMOTO va_list ap, void *cache_mdata) 177706a99fe3SHajimu UMEMOTO { 177806a99fe3SHajimu UMEMOTO struct addrinfo new_ai, *result, *sentinel, *lasts; 177906a99fe3SHajimu UMEMOTO 178006a99fe3SHajimu UMEMOTO char *p; 178106a99fe3SHajimu UMEMOTO size_t ai_size, ai_i, size; 178206a99fe3SHajimu UMEMOTO 178306a99fe3SHajimu UMEMOTO p = buffer; 178406a99fe3SHajimu UMEMOTO memcpy(&ai_size, p, sizeof(size_t)); 178506a99fe3SHajimu UMEMOTO p += sizeof(size_t); 178606a99fe3SHajimu UMEMOTO 178706a99fe3SHajimu UMEMOTO result = NULL; 178806a99fe3SHajimu UMEMOTO lasts = NULL; 178906a99fe3SHajimu UMEMOTO for (ai_i = 0; ai_i < ai_size; ++ai_i) { 179006a99fe3SHajimu UMEMOTO memcpy(&new_ai, p, sizeof(struct addrinfo)); 179106a99fe3SHajimu UMEMOTO p += sizeof(struct addrinfo); 179206a99fe3SHajimu UMEMOTO size = new_ai.ai_addrlen + sizeof(struct addrinfo) + 179306a99fe3SHajimu UMEMOTO _ALIGNBYTES; 179406a99fe3SHajimu UMEMOTO 179506a99fe3SHajimu UMEMOTO sentinel = (struct addrinfo *)malloc(size); 179606a99fe3SHajimu UMEMOTO memset(sentinel, 0, size); 179706a99fe3SHajimu UMEMOTO 179806a99fe3SHajimu UMEMOTO memcpy(sentinel, &new_ai, sizeof(struct addrinfo)); 179906a99fe3SHajimu UMEMOTO sentinel->ai_addr = (struct sockaddr *)_ALIGN((char *)sentinel + 180006a99fe3SHajimu UMEMOTO sizeof(struct addrinfo)); 180106a99fe3SHajimu UMEMOTO 180206a99fe3SHajimu UMEMOTO memcpy(sentinel->ai_addr, p, new_ai.ai_addrlen); 180306a99fe3SHajimu UMEMOTO p += new_ai.ai_addrlen; 180406a99fe3SHajimu UMEMOTO 180506a99fe3SHajimu UMEMOTO if (new_ai.ai_canonname != NULL) { 180606a99fe3SHajimu UMEMOTO memcpy(&size, p, sizeof(size_t)); 180706a99fe3SHajimu UMEMOTO p += sizeof(size_t); 180806a99fe3SHajimu UMEMOTO 180906a99fe3SHajimu UMEMOTO sentinel->ai_canonname = (char *)malloc(size + 1); 181006a99fe3SHajimu UMEMOTO memset(sentinel->ai_canonname, 0, size + 1); 181106a99fe3SHajimu UMEMOTO 181206a99fe3SHajimu UMEMOTO memcpy(sentinel->ai_canonname, p, size); 181306a99fe3SHajimu UMEMOTO p += size; 181406a99fe3SHajimu UMEMOTO } 181506a99fe3SHajimu UMEMOTO 181606a99fe3SHajimu UMEMOTO if (result == NULL) { 181706a99fe3SHajimu UMEMOTO result = sentinel; 181806a99fe3SHajimu UMEMOTO lasts = sentinel; 181906a99fe3SHajimu UMEMOTO } else { 182006a99fe3SHajimu UMEMOTO lasts->ai_next = sentinel; 182106a99fe3SHajimu UMEMOTO lasts = sentinel; 182206a99fe3SHajimu UMEMOTO } 182306a99fe3SHajimu UMEMOTO } 182406a99fe3SHajimu UMEMOTO 182506a99fe3SHajimu UMEMOTO *((struct addrinfo **)retval) = result; 182606a99fe3SHajimu UMEMOTO return (NS_SUCCESS); 182706a99fe3SHajimu UMEMOTO } 182806a99fe3SHajimu UMEMOTO #endif /* NS_CACHING */ 182906a99fe3SHajimu UMEMOTO 1830688a5c3eSHajimu UMEMOTO /* 1831688a5c3eSHajimu UMEMOTO * FQDN hostname, DNS lookup 1832688a5c3eSHajimu UMEMOTO */ 1833688a5c3eSHajimu UMEMOTO static int 183490d1d883SHajimu UMEMOTO explore_fqdn(const struct addrinfo *pai, const char *hostname, 183590d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res) 1836688a5c3eSHajimu UMEMOTO { 1837688a5c3eSHajimu UMEMOTO struct addrinfo *result; 1838688a5c3eSHajimu UMEMOTO struct addrinfo *cur; 1839688a5c3eSHajimu UMEMOTO int error = 0; 184006a99fe3SHajimu UMEMOTO 184106a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 184206a99fe3SHajimu UMEMOTO static const nss_cache_info cache_info = 184306a99fe3SHajimu UMEMOTO NS_COMMON_CACHE_INFO_INITIALIZER( 184406a99fe3SHajimu UMEMOTO hosts, NULL, addrinfo_id_func, addrinfo_marshal_func, 184506a99fe3SHajimu UMEMOTO addrinfo_unmarshal_func); 184606a99fe3SHajimu UMEMOTO #endif 1847688a5c3eSHajimu UMEMOTO static const ns_dtab dtab[] = { 1848688a5c3eSHajimu UMEMOTO NS_FILES_CB(_files_getaddrinfo, NULL) 1849688a5c3eSHajimu UMEMOTO { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ 1850688a5c3eSHajimu UMEMOTO NS_NIS_CB(_yp_getaddrinfo, NULL) 185106a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 185206a99fe3SHajimu UMEMOTO NS_CACHE_CB(&cache_info) 185306a99fe3SHajimu UMEMOTO #endif 1854688a5c3eSHajimu UMEMOTO { 0 } 185569b0a4b6SJim Pirzyk }; 185669b0a4b6SJim Pirzyk 1857688a5c3eSHajimu UMEMOTO result = NULL; 185869b0a4b6SJim Pirzyk 1859688a5c3eSHajimu UMEMOTO /* 1860688a5c3eSHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 1861688a5c3eSHajimu UMEMOTO */ 186233dee819SBrian Feldman if (get_portmatch(pai, servname) != 0) 186369b0a4b6SJim Pirzyk return 0; 186469b0a4b6SJim Pirzyk 1865688a5c3eSHajimu UMEMOTO switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", 1866688a5c3eSHajimu UMEMOTO default_dns_files, hostname, pai)) { 1867688a5c3eSHajimu UMEMOTO case NS_TRYAGAIN: 1868688a5c3eSHajimu UMEMOTO error = EAI_AGAIN; 1869688a5c3eSHajimu UMEMOTO goto free; 1870688a5c3eSHajimu UMEMOTO case NS_UNAVAIL: 1871688a5c3eSHajimu UMEMOTO error = EAI_FAIL; 1872688a5c3eSHajimu UMEMOTO goto free; 1873688a5c3eSHajimu UMEMOTO case NS_NOTFOUND: 1874688a5c3eSHajimu UMEMOTO error = EAI_NONAME; 1875688a5c3eSHajimu UMEMOTO goto free; 1876688a5c3eSHajimu UMEMOTO case NS_SUCCESS: 1877688a5c3eSHajimu UMEMOTO error = 0; 1878688a5c3eSHajimu UMEMOTO for (cur = result; cur; cur = cur->ai_next) { 1879688a5c3eSHajimu UMEMOTO GET_PORT(cur, servname); 1880688a5c3eSHajimu UMEMOTO /* canonname should be filled already */ 1881688a5c3eSHajimu UMEMOTO } 188269b0a4b6SJim Pirzyk break; 188369b0a4b6SJim Pirzyk } 188469b0a4b6SJim Pirzyk 1885688a5c3eSHajimu UMEMOTO *res = result; 1886688a5c3eSHajimu UMEMOTO 188769b0a4b6SJim Pirzyk return 0; 1888688a5c3eSHajimu UMEMOTO 1889688a5c3eSHajimu UMEMOTO free: 1890688a5c3eSHajimu UMEMOTO if (result) 1891688a5c3eSHajimu UMEMOTO freeaddrinfo(result); 1892688a5c3eSHajimu UMEMOTO return error; 189369b0a4b6SJim Pirzyk } 189469b0a4b6SJim Pirzyk 1895a42af91cSHajimu UMEMOTO #ifdef DEBUG 1896a42af91cSHajimu UMEMOTO static const char AskedForGot[] = 1897a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 1898a42af91cSHajimu UMEMOTO #endif 1899a42af91cSHajimu UMEMOTO 1900a42af91cSHajimu UMEMOTO static struct addrinfo * 190190d1d883SHajimu UMEMOTO getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, 190290d1d883SHajimu UMEMOTO const struct addrinfo *pai, res_state res) 1903a42af91cSHajimu UMEMOTO { 1904a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1905a42af91cSHajimu UMEMOTO struct addrinfo ai; 1906a42af91cSHajimu UMEMOTO const struct afd *afd; 1907a42af91cSHajimu UMEMOTO char *canonname; 1908a42af91cSHajimu UMEMOTO const HEADER *hp; 1909a42af91cSHajimu UMEMOTO const u_char *cp; 1910a42af91cSHajimu UMEMOTO int n; 1911a42af91cSHajimu UMEMOTO const u_char *eom; 1912dd521ef1SHajimu UMEMOTO char *bp, *ep; 1913dd521ef1SHajimu UMEMOTO int type, class, ancount, qdcount; 1914a42af91cSHajimu UMEMOTO int haveanswer, had_error; 1915a42af91cSHajimu UMEMOTO char tbuf[MAXDNAME]; 1916c05ac53bSDavid E. O'Brien int (*name_ok)(const char *); 1917a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 1918a42af91cSHajimu UMEMOTO 1919a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 1920a42af91cSHajimu UMEMOTO cur = &sentinel; 1921a42af91cSHajimu UMEMOTO 1922a42af91cSHajimu UMEMOTO canonname = NULL; 1923a42af91cSHajimu UMEMOTO eom = answer->buf + anslen; 1924a42af91cSHajimu UMEMOTO switch (qtype) { 1925a42af91cSHajimu UMEMOTO case T_A: 1926a42af91cSHajimu UMEMOTO case T_AAAA: 1927a42af91cSHajimu UMEMOTO case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ 1928a42af91cSHajimu UMEMOTO name_ok = res_hnok; 1929a42af91cSHajimu UMEMOTO break; 1930a42af91cSHajimu UMEMOTO default: 1931a42af91cSHajimu UMEMOTO return (NULL); /* XXX should be abort(); */ 1932a42af91cSHajimu UMEMOTO } 1933a42af91cSHajimu UMEMOTO /* 1934a42af91cSHajimu UMEMOTO * find first satisfactory answer 1935a42af91cSHajimu UMEMOTO */ 1936a42af91cSHajimu UMEMOTO hp = &answer->hdr; 1937a42af91cSHajimu UMEMOTO ancount = ntohs(hp->ancount); 1938a42af91cSHajimu UMEMOTO qdcount = ntohs(hp->qdcount); 1939a42af91cSHajimu UMEMOTO bp = hostbuf; 1940dd521ef1SHajimu UMEMOTO ep = hostbuf + sizeof hostbuf; 1941a42af91cSHajimu UMEMOTO cp = answer->buf + HFIXEDSZ; 1942a42af91cSHajimu UMEMOTO if (qdcount != 1) { 19435342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 1944a42af91cSHajimu UMEMOTO return (NULL); 1945a42af91cSHajimu UMEMOTO } 1946dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1947a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 19485342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 1949a42af91cSHajimu UMEMOTO return (NULL); 1950a42af91cSHajimu UMEMOTO } 1951a42af91cSHajimu UMEMOTO cp += n + QFIXEDSZ; 1952a42af91cSHajimu UMEMOTO if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 1953a42af91cSHajimu UMEMOTO /* res_send() has already verified that the query name is the 1954a42af91cSHajimu UMEMOTO * same as the one we sent; this just gets the expanded name 1955a42af91cSHajimu UMEMOTO * (i.e., with the succeeding search-domain tacked on). 1956a42af91cSHajimu UMEMOTO */ 1957a42af91cSHajimu UMEMOTO n = strlen(bp) + 1; /* for the \0 */ 1958a42af91cSHajimu UMEMOTO if (n >= MAXHOSTNAMELEN) { 19595342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 1960a42af91cSHajimu UMEMOTO return (NULL); 1961a42af91cSHajimu UMEMOTO } 1962a42af91cSHajimu UMEMOTO canonname = bp; 1963a42af91cSHajimu UMEMOTO bp += n; 1964a42af91cSHajimu UMEMOTO /* The qname can be abbreviated, but h_name is now absolute. */ 1965a42af91cSHajimu UMEMOTO qname = canonname; 1966a42af91cSHajimu UMEMOTO } 1967a42af91cSHajimu UMEMOTO haveanswer = 0; 1968a42af91cSHajimu UMEMOTO had_error = 0; 1969a42af91cSHajimu UMEMOTO while (ancount-- > 0 && cp < eom && !had_error) { 1970dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1971a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 1972a42af91cSHajimu UMEMOTO had_error++; 1973a42af91cSHajimu UMEMOTO continue; 1974a42af91cSHajimu UMEMOTO } 1975a42af91cSHajimu UMEMOTO cp += n; /* name */ 1976a42af91cSHajimu UMEMOTO type = _getshort(cp); 1977a42af91cSHajimu UMEMOTO cp += INT16SZ; /* type */ 1978a42af91cSHajimu UMEMOTO class = _getshort(cp); 1979a42af91cSHajimu UMEMOTO cp += INT16SZ + INT32SZ; /* class, TTL */ 1980a42af91cSHajimu UMEMOTO n = _getshort(cp); 1981a42af91cSHajimu UMEMOTO cp += INT16SZ; /* len */ 1982a42af91cSHajimu UMEMOTO if (class != C_IN) { 1983a42af91cSHajimu UMEMOTO /* XXX - debug? syslog? */ 1984a42af91cSHajimu UMEMOTO cp += n; 1985a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1986a42af91cSHajimu UMEMOTO } 1987a42af91cSHajimu UMEMOTO if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 1988a42af91cSHajimu UMEMOTO type == T_CNAME) { 1989a42af91cSHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1990a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(tbuf)) { 1991a42af91cSHajimu UMEMOTO had_error++; 1992a42af91cSHajimu UMEMOTO continue; 1993a42af91cSHajimu UMEMOTO } 1994a42af91cSHajimu UMEMOTO cp += n; 1995a42af91cSHajimu UMEMOTO /* Get canonical name. */ 1996a42af91cSHajimu UMEMOTO n = strlen(tbuf) + 1; /* for the \0 */ 1997dd521ef1SHajimu UMEMOTO if (n > ep - bp || n >= MAXHOSTNAMELEN) { 1998a42af91cSHajimu UMEMOTO had_error++; 1999a42af91cSHajimu UMEMOTO continue; 2000a42af91cSHajimu UMEMOTO } 2001d0509082SJacques Vidrine strlcpy(bp, tbuf, ep - bp); 2002a42af91cSHajimu UMEMOTO canonname = bp; 2003a42af91cSHajimu UMEMOTO bp += n; 2004a42af91cSHajimu UMEMOTO continue; 2005a42af91cSHajimu UMEMOTO } 2006a42af91cSHajimu UMEMOTO if (qtype == T_ANY) { 2007a42af91cSHajimu UMEMOTO if (!(type == T_A || type == T_AAAA)) { 2008a42af91cSHajimu UMEMOTO cp += n; 2009a42af91cSHajimu UMEMOTO continue; 2010a42af91cSHajimu UMEMOTO } 2011a42af91cSHajimu UMEMOTO } else if (type != qtype) { 2012a42af91cSHajimu UMEMOTO #ifdef DEBUG 2013aba831deSHajimu UMEMOTO if (type != T_KEY && type != T_SIG && 2014aba831deSHajimu UMEMOTO type != ns_t_dname) 2015a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 2016a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 2017a42af91cSHajimu UMEMOTO qname, p_class(C_IN), p_type(qtype), 2018a42af91cSHajimu UMEMOTO p_type(type)); 2019a42af91cSHajimu UMEMOTO #endif 2020a42af91cSHajimu UMEMOTO cp += n; 2021a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 2022a42af91cSHajimu UMEMOTO } 2023a42af91cSHajimu UMEMOTO switch (type) { 2024a42af91cSHajimu UMEMOTO case T_A: 2025a42af91cSHajimu UMEMOTO case T_AAAA: 2026a42af91cSHajimu UMEMOTO if (strcasecmp(canonname, bp) != 0) { 2027a42af91cSHajimu UMEMOTO #ifdef DEBUG 2028a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 2029a42af91cSHajimu UMEMOTO AskedForGot, canonname, bp); 2030a42af91cSHajimu UMEMOTO #endif 2031a42af91cSHajimu UMEMOTO cp += n; 2032a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 2033a42af91cSHajimu UMEMOTO } 2034a42af91cSHajimu UMEMOTO if (type == T_A && n != INADDRSZ) { 2035a42af91cSHajimu UMEMOTO cp += n; 2036a42af91cSHajimu UMEMOTO continue; 2037a42af91cSHajimu UMEMOTO } 2038a42af91cSHajimu UMEMOTO if (type == T_AAAA && n != IN6ADDRSZ) { 2039a42af91cSHajimu UMEMOTO cp += n; 2040a42af91cSHajimu UMEMOTO continue; 2041a42af91cSHajimu UMEMOTO } 2042a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 2043a42af91cSHajimu UMEMOTO if (type == T_AAAA) { 2044a42af91cSHajimu UMEMOTO struct in6_addr in6; 2045a42af91cSHajimu UMEMOTO memcpy(&in6, cp, sizeof(in6)); 2046a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&in6)) { 2047a42af91cSHajimu UMEMOTO cp += n; 2048a42af91cSHajimu UMEMOTO continue; 2049a42af91cSHajimu UMEMOTO } 2050a42af91cSHajimu UMEMOTO } 2051a42af91cSHajimu UMEMOTO #endif 2052a42af91cSHajimu UMEMOTO if (!haveanswer) { 2053a42af91cSHajimu UMEMOTO int nn; 2054a42af91cSHajimu UMEMOTO 2055a42af91cSHajimu UMEMOTO canonname = bp; 2056a42af91cSHajimu UMEMOTO nn = strlen(bp) + 1; /* for the \0 */ 2057a42af91cSHajimu UMEMOTO bp += nn; 2058a42af91cSHajimu UMEMOTO } 2059a42af91cSHajimu UMEMOTO 2060a42af91cSHajimu UMEMOTO /* don't overwrite pai */ 2061a42af91cSHajimu UMEMOTO ai = *pai; 2062a42af91cSHajimu UMEMOTO ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; 2063a42af91cSHajimu UMEMOTO afd = find_afd(ai.ai_family); 2064a42af91cSHajimu UMEMOTO if (afd == NULL) { 2065a42af91cSHajimu UMEMOTO cp += n; 2066a42af91cSHajimu UMEMOTO continue; 2067a42af91cSHajimu UMEMOTO } 2068a42af91cSHajimu UMEMOTO cur->ai_next = get_ai(&ai, afd, (const char *)cp); 2069a42af91cSHajimu UMEMOTO if (cur->ai_next == NULL) 2070a42af91cSHajimu UMEMOTO had_error++; 2071a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 2072a42af91cSHajimu UMEMOTO cur = cur->ai_next; 2073a42af91cSHajimu UMEMOTO cp += n; 2074a42af91cSHajimu UMEMOTO break; 2075a42af91cSHajimu UMEMOTO default: 2076a42af91cSHajimu UMEMOTO abort(); 2077a42af91cSHajimu UMEMOTO } 2078a42af91cSHajimu UMEMOTO if (!had_error) 2079a42af91cSHajimu UMEMOTO haveanswer++; 2080a42af91cSHajimu UMEMOTO } 2081a42af91cSHajimu UMEMOTO if (haveanswer) { 208269b0a4b6SJim Pirzyk #if defined(RESOLVSORT) 208369b0a4b6SJim Pirzyk /* 208469b0a4b6SJim Pirzyk * We support only IPv4 address for backward 208569b0a4b6SJim Pirzyk * compatibility against gethostbyname(3). 208669b0a4b6SJim Pirzyk */ 20875342d17fSHajimu UMEMOTO if (res->nsort && qtype == T_A) { 20885342d17fSHajimu UMEMOTO if (addr4sort(&sentinel, res) < 0) { 208969b0a4b6SJim Pirzyk freeaddrinfo(sentinel.ai_next); 20905342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 209169b0a4b6SJim Pirzyk return NULL; 209269b0a4b6SJim Pirzyk } 209369b0a4b6SJim Pirzyk } 209469b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/ 2095a42af91cSHajimu UMEMOTO if (!canonname) 2096a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, qname); 2097a42af91cSHajimu UMEMOTO else 2098a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, canonname); 20995342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_SUCCESS); 2100a42af91cSHajimu UMEMOTO return sentinel.ai_next; 2101a42af91cSHajimu UMEMOTO } 2102a42af91cSHajimu UMEMOTO 21035342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2104a42af91cSHajimu UMEMOTO return NULL; 2105a42af91cSHajimu UMEMOTO } 2106a42af91cSHajimu UMEMOTO 2107688a5c3eSHajimu UMEMOTO #ifdef RESOLVSORT 2108688a5c3eSHajimu UMEMOTO struct addr_ptr { 2109688a5c3eSHajimu UMEMOTO struct addrinfo *ai; 2110688a5c3eSHajimu UMEMOTO int aval; 2111688a5c3eSHajimu UMEMOTO }; 2112688a5c3eSHajimu UMEMOTO 2113688a5c3eSHajimu UMEMOTO static int 21145342d17fSHajimu UMEMOTO addr4sort(struct addrinfo *sentinel, res_state res) 2115688a5c3eSHajimu UMEMOTO { 2116688a5c3eSHajimu UMEMOTO struct addrinfo *ai; 2117688a5c3eSHajimu UMEMOTO struct addr_ptr *addrs, addr; 2118688a5c3eSHajimu UMEMOTO struct sockaddr_in *sin; 2119688a5c3eSHajimu UMEMOTO int naddrs, i, j; 2120688a5c3eSHajimu UMEMOTO int needsort = 0; 2121688a5c3eSHajimu UMEMOTO 2122688a5c3eSHajimu UMEMOTO if (!sentinel) 2123688a5c3eSHajimu UMEMOTO return -1; 2124688a5c3eSHajimu UMEMOTO naddrs = 0; 2125688a5c3eSHajimu UMEMOTO for (ai = sentinel->ai_next; ai; ai = ai->ai_next) 2126688a5c3eSHajimu UMEMOTO naddrs++; 2127688a5c3eSHajimu UMEMOTO if (naddrs < 2) 2128688a5c3eSHajimu UMEMOTO return 0; /* We don't need sorting. */ 2129688a5c3eSHajimu UMEMOTO if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL) 2130688a5c3eSHajimu UMEMOTO return -1; 2131688a5c3eSHajimu UMEMOTO i = 0; 2132688a5c3eSHajimu UMEMOTO for (ai = sentinel->ai_next; ai; ai = ai->ai_next) { 2133688a5c3eSHajimu UMEMOTO sin = (struct sockaddr_in *)ai->ai_addr; 21345342d17fSHajimu UMEMOTO for (j = 0; (unsigned)j < res->nsort; j++) { 21355342d17fSHajimu UMEMOTO if (res->sort_list[j].addr.s_addr == 21365342d17fSHajimu UMEMOTO (sin->sin_addr.s_addr & res->sort_list[j].mask)) 2137688a5c3eSHajimu UMEMOTO break; 2138688a5c3eSHajimu UMEMOTO } 2139688a5c3eSHajimu UMEMOTO addrs[i].ai = ai; 2140688a5c3eSHajimu UMEMOTO addrs[i].aval = j; 2141688a5c3eSHajimu UMEMOTO if (needsort == 0 && i > 0 && j < addrs[i - 1].aval) 2142688a5c3eSHajimu UMEMOTO needsort = i; 2143688a5c3eSHajimu UMEMOTO i++; 2144688a5c3eSHajimu UMEMOTO } 2145688a5c3eSHajimu UMEMOTO if (!needsort) { 2146688a5c3eSHajimu UMEMOTO free(addrs); 2147688a5c3eSHajimu UMEMOTO return 0; 2148688a5c3eSHajimu UMEMOTO } 2149688a5c3eSHajimu UMEMOTO 2150688a5c3eSHajimu UMEMOTO while (needsort < naddrs) { 2151688a5c3eSHajimu UMEMOTO for (j = needsort - 1; j >= 0; j--) { 2152688a5c3eSHajimu UMEMOTO if (addrs[j].aval > addrs[j+1].aval) { 2153688a5c3eSHajimu UMEMOTO addr = addrs[j]; 2154688a5c3eSHajimu UMEMOTO addrs[j] = addrs[j + 1]; 2155688a5c3eSHajimu UMEMOTO addrs[j + 1] = addr; 2156688a5c3eSHajimu UMEMOTO } else 2157688a5c3eSHajimu UMEMOTO break; 2158688a5c3eSHajimu UMEMOTO } 2159688a5c3eSHajimu UMEMOTO needsort++; 2160688a5c3eSHajimu UMEMOTO } 2161688a5c3eSHajimu UMEMOTO 2162688a5c3eSHajimu UMEMOTO ai = sentinel; 2163688a5c3eSHajimu UMEMOTO for (i = 0; i < naddrs; ++i) { 2164688a5c3eSHajimu UMEMOTO ai->ai_next = addrs[i].ai; 2165688a5c3eSHajimu UMEMOTO ai = ai->ai_next; 2166688a5c3eSHajimu UMEMOTO } 2167688a5c3eSHajimu UMEMOTO ai->ai_next = NULL; 2168688a5c3eSHajimu UMEMOTO free(addrs); 2169688a5c3eSHajimu UMEMOTO return 0; 2170688a5c3eSHajimu UMEMOTO } 2171688a5c3eSHajimu UMEMOTO #endif /*RESOLVSORT*/ 2172688a5c3eSHajimu UMEMOTO 2173a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 2174a42af91cSHajimu UMEMOTO static int 217590d1d883SHajimu UMEMOTO _dns_getaddrinfo(void *rv, void *cb_data, va_list ap) 2176a42af91cSHajimu UMEMOTO { 2177a42af91cSHajimu UMEMOTO struct addrinfo *ai; 21785cd588cbSHajimu UMEMOTO querybuf *buf, *buf2; 2179a214cd1aSHajimu UMEMOTO const char *hostname; 2180248aee62SJacques Vidrine const struct addrinfo *pai; 2181a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 2182a42af91cSHajimu UMEMOTO struct res_target q, q2; 21835342d17fSHajimu UMEMOTO res_state res; 2184a42af91cSHajimu UMEMOTO 2185a214cd1aSHajimu UMEMOTO hostname = va_arg(ap, char *); 2186248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *); 2187248aee62SJacques Vidrine 2188f1fa5b4fSXin LI memset(&q, 0, sizeof(q)); 2189a42af91cSHajimu UMEMOTO memset(&q2, 0, sizeof(q2)); 2190a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 2191a42af91cSHajimu UMEMOTO cur = &sentinel; 2192a42af91cSHajimu UMEMOTO 21935cd588cbSHajimu UMEMOTO buf = malloc(sizeof(*buf)); 21945cd588cbSHajimu UMEMOTO if (!buf) { 21955342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 21965cd588cbSHajimu UMEMOTO return NS_NOTFOUND; 21975cd588cbSHajimu UMEMOTO } 21985cd588cbSHajimu UMEMOTO buf2 = malloc(sizeof(*buf2)); 21995cd588cbSHajimu UMEMOTO if (!buf2) { 22005cd588cbSHajimu UMEMOTO free(buf); 22015342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 22025cd588cbSHajimu UMEMOTO return NS_NOTFOUND; 22035cd588cbSHajimu UMEMOTO } 22045cd588cbSHajimu UMEMOTO 2205a42af91cSHajimu UMEMOTO switch (pai->ai_family) { 2206a42af91cSHajimu UMEMOTO case AF_UNSPEC: 2207a214cd1aSHajimu UMEMOTO q.name = hostname; 2208b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 2209d0d6a2c7SHajimu UMEMOTO q.qtype = T_A; 22105cd588cbSHajimu UMEMOTO q.answer = buf->buf; 22115cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 2212a42af91cSHajimu UMEMOTO q.next = &q2; 2213a214cd1aSHajimu UMEMOTO q2.name = hostname; 2214b826397aSJun-ichiro itojun Hagino q2.qclass = C_IN; 2215d0d6a2c7SHajimu UMEMOTO q2.qtype = T_AAAA; 22165cd588cbSHajimu UMEMOTO q2.answer = buf2->buf; 22175cd588cbSHajimu UMEMOTO q2.anslen = sizeof(buf2->buf); 2218a42af91cSHajimu UMEMOTO break; 2219a42af91cSHajimu UMEMOTO case AF_INET: 2220a214cd1aSHajimu UMEMOTO q.name = hostname; 2221b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 2222b826397aSJun-ichiro itojun Hagino q.qtype = T_A; 22235cd588cbSHajimu UMEMOTO q.answer = buf->buf; 22245cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 2225a42af91cSHajimu UMEMOTO break; 2226a42af91cSHajimu UMEMOTO case AF_INET6: 2227a214cd1aSHajimu UMEMOTO q.name = hostname; 2228b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 2229b826397aSJun-ichiro itojun Hagino q.qtype = T_AAAA; 22305cd588cbSHajimu UMEMOTO q.answer = buf->buf; 22315cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 2232a42af91cSHajimu UMEMOTO break; 2233a42af91cSHajimu UMEMOTO default: 22345cd588cbSHajimu UMEMOTO free(buf); 22355cd588cbSHajimu UMEMOTO free(buf2); 2236248aee62SJacques Vidrine return NS_UNAVAIL; 2237a42af91cSHajimu UMEMOTO } 22385342d17fSHajimu UMEMOTO 22395342d17fSHajimu UMEMOTO res = __res_state(); 22405342d17fSHajimu UMEMOTO if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) { 22415342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 22425342d17fSHajimu UMEMOTO free(buf); 22435342d17fSHajimu UMEMOTO free(buf2); 22445342d17fSHajimu UMEMOTO return NS_NOTFOUND; 22455342d17fSHajimu UMEMOTO } 22465342d17fSHajimu UMEMOTO 22475342d17fSHajimu UMEMOTO if (res_searchN(hostname, &q, res) < 0) { 22485cd588cbSHajimu UMEMOTO free(buf); 22495cd588cbSHajimu UMEMOTO free(buf2); 2250248aee62SJacques Vidrine return NS_NOTFOUND; 22515cd588cbSHajimu UMEMOTO } 2252d0d6a2c7SHajimu UMEMOTO /* prefer IPv6 */ 2253d0d6a2c7SHajimu UMEMOTO if (q.next) { 22545342d17fSHajimu UMEMOTO ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res); 2255a42af91cSHajimu UMEMOTO if (ai) { 2256a42af91cSHajimu UMEMOTO cur->ai_next = ai; 2257a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 2258a42af91cSHajimu UMEMOTO cur = cur->ai_next; 2259a42af91cSHajimu UMEMOTO } 2260d0d6a2c7SHajimu UMEMOTO } 22615342d17fSHajimu UMEMOTO ai = getanswer(buf, q.n, q.name, q.qtype, pai, res); 2262a42af91cSHajimu UMEMOTO if (ai) 2263a42af91cSHajimu UMEMOTO cur->ai_next = ai; 22645cd588cbSHajimu UMEMOTO free(buf); 22655cd588cbSHajimu UMEMOTO free(buf2); 2266a42af91cSHajimu UMEMOTO if (sentinel.ai_next == NULL) 22675342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 2268a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 2269248aee62SJacques Vidrine return NS_NOTFOUND; 2270a42af91cSHajimu UMEMOTO case TRY_AGAIN: 2271248aee62SJacques Vidrine return NS_TRYAGAIN; 2272a42af91cSHajimu UMEMOTO default: 2273248aee62SJacques Vidrine return NS_UNAVAIL; 2274a42af91cSHajimu UMEMOTO } 2275248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 2276248aee62SJacques Vidrine return NS_SUCCESS; 2277248aee62SJacques Vidrine } 2278248aee62SJacques Vidrine 2279248aee62SJacques Vidrine static void 22809def31dcSHajimu UMEMOTO _sethtent(FILE **hostf) 2281248aee62SJacques Vidrine { 22829def31dcSHajimu UMEMOTO if (!*hostf) 2283a93705b0SJilles Tjoelker *hostf = fopen(_PATH_HOSTS, "re"); 2284248aee62SJacques Vidrine else 22859def31dcSHajimu UMEMOTO rewind(*hostf); 2286248aee62SJacques Vidrine } 2287248aee62SJacques Vidrine 2288248aee62SJacques Vidrine static void 22899def31dcSHajimu UMEMOTO _endhtent(FILE **hostf) 2290248aee62SJacques Vidrine { 22919def31dcSHajimu UMEMOTO if (*hostf) { 22929def31dcSHajimu UMEMOTO (void) fclose(*hostf); 22939def31dcSHajimu UMEMOTO *hostf = NULL; 2294248aee62SJacques Vidrine } 2295a42af91cSHajimu UMEMOTO } 2296a42af91cSHajimu UMEMOTO 2297a42af91cSHajimu UMEMOTO static struct addrinfo * 22989def31dcSHajimu UMEMOTO _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai) 2299a42af91cSHajimu UMEMOTO { 2300a42af91cSHajimu UMEMOTO char *p; 2301a42af91cSHajimu UMEMOTO char *cp, *tname, *cname; 2302a42af91cSHajimu UMEMOTO struct addrinfo hints, *res0, *res; 2303a42af91cSHajimu UMEMOTO int error; 2304a42af91cSHajimu UMEMOTO const char *addr; 2305a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 2306a42af91cSHajimu UMEMOTO 2307a93705b0SJilles Tjoelker if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) 2308248aee62SJacques Vidrine return (NULL); 2309a42af91cSHajimu UMEMOTO again: 23109def31dcSHajimu UMEMOTO if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) 2311a42af91cSHajimu UMEMOTO return (NULL); 2312a42af91cSHajimu UMEMOTO if (*p == '#') 2313a42af91cSHajimu UMEMOTO goto again; 23147b6cc404SMaxim Sobolev cp = strpbrk(p, "#\n"); 23157b6cc404SMaxim Sobolev if (cp != NULL) 2316a42af91cSHajimu UMEMOTO *cp = '\0'; 2317a42af91cSHajimu UMEMOTO if (!(cp = strpbrk(p, " \t"))) 2318a42af91cSHajimu UMEMOTO goto again; 2319a42af91cSHajimu UMEMOTO *cp++ = '\0'; 2320a42af91cSHajimu UMEMOTO addr = p; 2321a42af91cSHajimu UMEMOTO cname = NULL; 2322a42af91cSHajimu UMEMOTO /* if this is not something we're looking for, skip it. */ 2323a42af91cSHajimu UMEMOTO while (cp && *cp) { 2324a42af91cSHajimu UMEMOTO if (*cp == ' ' || *cp == '\t') { 2325a42af91cSHajimu UMEMOTO cp++; 2326a42af91cSHajimu UMEMOTO continue; 2327a42af91cSHajimu UMEMOTO } 2328a42af91cSHajimu UMEMOTO tname = cp; 2329a42af91cSHajimu UMEMOTO if (cname == NULL) 2330a42af91cSHajimu UMEMOTO cname = cp; 2331a42af91cSHajimu UMEMOTO if ((cp = strpbrk(cp, " \t")) != NULL) 2332a42af91cSHajimu UMEMOTO *cp++ = '\0'; 2333a42af91cSHajimu UMEMOTO if (strcasecmp(name, tname) == 0) 2334a42af91cSHajimu UMEMOTO goto found; 2335a42af91cSHajimu UMEMOTO } 2336a42af91cSHajimu UMEMOTO goto again; 2337a42af91cSHajimu UMEMOTO 2338a42af91cSHajimu UMEMOTO found: 2339dd521ef1SHajimu UMEMOTO /* we should not glob socktype/protocol here */ 2340dd521ef1SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 2341dd521ef1SHajimu UMEMOTO hints.ai_family = pai->ai_family; 2342dd521ef1SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; 2343dd521ef1SHajimu UMEMOTO hints.ai_protocol = 0; 2344a42af91cSHajimu UMEMOTO hints.ai_flags = AI_NUMERICHOST; 2345dd521ef1SHajimu UMEMOTO error = getaddrinfo(addr, "0", &hints, &res0); 2346a42af91cSHajimu UMEMOTO if (error) 2347a42af91cSHajimu UMEMOTO goto again; 2348a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 2349a42af91cSHajimu UMEMOTO /* XXX should check all items in the chain */ 2350a42af91cSHajimu UMEMOTO if (res0->ai_family == AF_INET6 && 2351a42af91cSHajimu UMEMOTO IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) { 2352a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 2353a42af91cSHajimu UMEMOTO goto again; 2354a42af91cSHajimu UMEMOTO } 2355a42af91cSHajimu UMEMOTO #endif 2356a42af91cSHajimu UMEMOTO for (res = res0; res; res = res->ai_next) { 2357a42af91cSHajimu UMEMOTO /* cover it up */ 2358a42af91cSHajimu UMEMOTO res->ai_flags = pai->ai_flags; 2359dd521ef1SHajimu UMEMOTO res->ai_socktype = pai->ai_socktype; 2360dd521ef1SHajimu UMEMOTO res->ai_protocol = pai->ai_protocol; 2361a42af91cSHajimu UMEMOTO 2362a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_CANONNAME) { 2363a42af91cSHajimu UMEMOTO if (get_canonname(pai, res, cname) != 0) { 2364a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 2365a42af91cSHajimu UMEMOTO goto again; 2366a42af91cSHajimu UMEMOTO } 2367a42af91cSHajimu UMEMOTO } 2368a42af91cSHajimu UMEMOTO } 2369a42af91cSHajimu UMEMOTO return res0; 2370a42af91cSHajimu UMEMOTO } 2371a42af91cSHajimu UMEMOTO 2372a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 2373a42af91cSHajimu UMEMOTO static int 237490d1d883SHajimu UMEMOTO _files_getaddrinfo(void *rv, void *cb_data, va_list ap) 2375a42af91cSHajimu UMEMOTO { 2376248aee62SJacques Vidrine const char *name; 2377248aee62SJacques Vidrine const struct addrinfo *pai; 2378a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 2379a42af91cSHajimu UMEMOTO struct addrinfo *p; 23809def31dcSHajimu UMEMOTO FILE *hostf = NULL; 2381a42af91cSHajimu UMEMOTO 2382248aee62SJacques Vidrine name = va_arg(ap, char *); 2383248aee62SJacques Vidrine pai = va_arg(ap, struct addrinfo *); 2384248aee62SJacques Vidrine 2385248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 2386a42af91cSHajimu UMEMOTO cur = &sentinel; 2387a42af91cSHajimu UMEMOTO 23889def31dcSHajimu UMEMOTO _sethtent(&hostf); 23899def31dcSHajimu UMEMOTO while ((p = _gethtent(&hostf, name, pai)) != NULL) { 2390a42af91cSHajimu UMEMOTO cur->ai_next = p; 2391a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 2392a42af91cSHajimu UMEMOTO cur = cur->ai_next; 2393a42af91cSHajimu UMEMOTO } 23949def31dcSHajimu UMEMOTO _endhtent(&hostf); 2395a42af91cSHajimu UMEMOTO 2396248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 2397248aee62SJacques Vidrine if (sentinel.ai_next == NULL) 2398248aee62SJacques Vidrine return NS_NOTFOUND; 2399248aee62SJacques Vidrine return NS_SUCCESS; 2400a42af91cSHajimu UMEMOTO } 2401a42af91cSHajimu UMEMOTO 2402a42af91cSHajimu UMEMOTO #ifdef YP 2403248aee62SJacques Vidrine /*ARGSUSED*/ 2404248aee62SJacques Vidrine static struct addrinfo * 240590d1d883SHajimu UMEMOTO _yphostent(char *line, const struct addrinfo *pai) 2406248aee62SJacques Vidrine { 2407248aee62SJacques Vidrine struct addrinfo sentinel, *cur; 2408248aee62SJacques Vidrine struct addrinfo hints, *res, *res0; 2409248aee62SJacques Vidrine int error; 2410248aee62SJacques Vidrine char *p = line; 2411248aee62SJacques Vidrine const char *addr, *canonname; 2412248aee62SJacques Vidrine char *nextline; 2413248aee62SJacques Vidrine char *cp; 2414248aee62SJacques Vidrine 2415248aee62SJacques Vidrine addr = canonname = NULL; 2416248aee62SJacques Vidrine 2417248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 2418a42af91cSHajimu UMEMOTO cur = &sentinel; 2419a42af91cSHajimu UMEMOTO 2420248aee62SJacques Vidrine nextline: 2421248aee62SJacques Vidrine /* terminate line */ 2422248aee62SJacques Vidrine cp = strchr(p, '\n'); 2423248aee62SJacques Vidrine if (cp) { 2424248aee62SJacques Vidrine *cp++ = '\0'; 2425248aee62SJacques Vidrine nextline = cp; 2426248aee62SJacques Vidrine } else 2427248aee62SJacques Vidrine nextline = NULL; 2428a42af91cSHajimu UMEMOTO 2429248aee62SJacques Vidrine cp = strpbrk(p, " \t"); 2430248aee62SJacques Vidrine if (cp == NULL) { 2431248aee62SJacques Vidrine if (canonname == NULL) 2432248aee62SJacques Vidrine return (NULL); 2433248aee62SJacques Vidrine else 2434248aee62SJacques Vidrine goto done; 2435a42af91cSHajimu UMEMOTO } 2436248aee62SJacques Vidrine *cp++ = '\0'; 2437a42af91cSHajimu UMEMOTO 2438248aee62SJacques Vidrine addr = p; 2439a42af91cSHajimu UMEMOTO 2440248aee62SJacques Vidrine while (cp && *cp) { 2441248aee62SJacques Vidrine if (*cp == ' ' || *cp == '\t') { 2442248aee62SJacques Vidrine cp++; 2443a42af91cSHajimu UMEMOTO continue; 2444248aee62SJacques Vidrine } 2445248aee62SJacques Vidrine if (!canonname) 2446248aee62SJacques Vidrine canonname = cp; 2447248aee62SJacques Vidrine if ((cp = strpbrk(cp, " \t")) != NULL) 2448248aee62SJacques Vidrine *cp++ = '\0'; 2449a42af91cSHajimu UMEMOTO } 2450a42af91cSHajimu UMEMOTO 2451bf838688SHajimu UMEMOTO hints = *pai; 2452248aee62SJacques Vidrine hints.ai_flags = AI_NUMERICHOST; 2453bf838688SHajimu UMEMOTO error = getaddrinfo(addr, NULL, &hints, &res0); 2454248aee62SJacques Vidrine if (error == 0) { 2455248aee62SJacques Vidrine for (res = res0; res; res = res->ai_next) { 2456248aee62SJacques Vidrine /* cover it up */ 2457248aee62SJacques Vidrine res->ai_flags = pai->ai_flags; 2458248aee62SJacques Vidrine 2459248aee62SJacques Vidrine if (pai->ai_flags & AI_CANONNAME) 2460248aee62SJacques Vidrine (void)get_canonname(pai, res, canonname); 2461248aee62SJacques Vidrine } 2462248aee62SJacques Vidrine } else 2463248aee62SJacques Vidrine res0 = NULL; 2464248aee62SJacques Vidrine if (res0) { 2465248aee62SJacques Vidrine cur->ai_next = res0; 2466a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 2467a42af91cSHajimu UMEMOTO cur = cur->ai_next; 2468a42af91cSHajimu UMEMOTO } 2469a42af91cSHajimu UMEMOTO 2470248aee62SJacques Vidrine if (nextline) { 2471248aee62SJacques Vidrine p = nextline; 2472248aee62SJacques Vidrine goto nextline; 2473248aee62SJacques Vidrine } 2474a42af91cSHajimu UMEMOTO 2475248aee62SJacques Vidrine done: 2476248aee62SJacques Vidrine return sentinel.ai_next; 2477248aee62SJacques Vidrine } 2478248aee62SJacques Vidrine 2479248aee62SJacques Vidrine /*ARGSUSED*/ 2480248aee62SJacques Vidrine static int 248190d1d883SHajimu UMEMOTO _yp_getaddrinfo(void *rv, void *cb_data, va_list ap) 2482248aee62SJacques Vidrine { 2483248aee62SJacques Vidrine struct addrinfo sentinel, *cur; 2484248aee62SJacques Vidrine struct addrinfo *ai = NULL; 2485d7057edbSHajimu UMEMOTO char *ypbuf; 2486d7057edbSHajimu UMEMOTO int ypbuflen, r; 2487248aee62SJacques Vidrine const char *name; 2488248aee62SJacques Vidrine const struct addrinfo *pai; 2489d7057edbSHajimu UMEMOTO char *ypdomain; 2490d7057edbSHajimu UMEMOTO 2491d7057edbSHajimu UMEMOTO if (_yp_check(&ypdomain) == 0) 2492d7057edbSHajimu UMEMOTO return NS_UNAVAIL; 2493248aee62SJacques Vidrine 2494248aee62SJacques Vidrine name = va_arg(ap, char *); 2495248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *); 2496248aee62SJacques Vidrine 2497248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 2498248aee62SJacques Vidrine cur = &sentinel; 2499248aee62SJacques Vidrine 2500248aee62SJacques Vidrine /* hosts.byname is only for IPv4 (Solaris8) */ 2501248aee62SJacques Vidrine if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { 2502d7057edbSHajimu UMEMOTO r = yp_match(ypdomain, "hosts.byname", name, 2503d7057edbSHajimu UMEMOTO (int)strlen(name), &ypbuf, &ypbuflen); 2504248aee62SJacques Vidrine if (r == 0) { 2505248aee62SJacques Vidrine struct addrinfo ai4; 2506248aee62SJacques Vidrine 2507248aee62SJacques Vidrine ai4 = *pai; 2508248aee62SJacques Vidrine ai4.ai_family = AF_INET; 2509d7057edbSHajimu UMEMOTO ai = _yphostent(ypbuf, &ai4); 2510248aee62SJacques Vidrine if (ai) { 2511248aee62SJacques Vidrine cur->ai_next = ai; 2512248aee62SJacques Vidrine while (cur && cur->ai_next) 2513248aee62SJacques Vidrine cur = cur->ai_next; 2514248aee62SJacques Vidrine } 2515d7057edbSHajimu UMEMOTO free(ypbuf); 2516248aee62SJacques Vidrine } 2517f8997ca2SHajimu UMEMOTO } 2518248aee62SJacques Vidrine 2519248aee62SJacques Vidrine /* ipnodes.byname can hold both IPv4/v6 */ 2520d7057edbSHajimu UMEMOTO r = yp_match(ypdomain, "ipnodes.byname", name, 2521d7057edbSHajimu UMEMOTO (int)strlen(name), &ypbuf, &ypbuflen); 2522248aee62SJacques Vidrine if (r == 0) { 2523d7057edbSHajimu UMEMOTO ai = _yphostent(ypbuf, pai); 2524d7057edbSHajimu UMEMOTO if (ai) 2525248aee62SJacques Vidrine cur->ai_next = ai; 2526d7057edbSHajimu UMEMOTO free(ypbuf); 2527248aee62SJacques Vidrine } 2528248aee62SJacques Vidrine 2529248aee62SJacques Vidrine if (sentinel.ai_next == NULL) { 25305342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND); 2531248aee62SJacques Vidrine return NS_NOTFOUND; 2532248aee62SJacques Vidrine } 2533248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 2534248aee62SJacques Vidrine return NS_SUCCESS; 2535a42af91cSHajimu UMEMOTO } 2536a42af91cSHajimu UMEMOTO #endif 2537a42af91cSHajimu UMEMOTO 2538a42af91cSHajimu UMEMOTO /* resolver logic */ 2539a42af91cSHajimu UMEMOTO 2540a42af91cSHajimu UMEMOTO /* 2541a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and await answer. 2542a42af91cSHajimu UMEMOTO * Returned answer is placed in supplied buffer "answer". 2543a42af91cSHajimu UMEMOTO * Perform preliminary check of answer, returning success only 2544a42af91cSHajimu UMEMOTO * if no error is indicated and the answer count is nonzero. 2545a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 2546a42af91cSHajimu UMEMOTO * Error number is left in h_errno. 2547a42af91cSHajimu UMEMOTO * 2548a42af91cSHajimu UMEMOTO * Caller must parse answer and determine whether it answers the question. 2549a42af91cSHajimu UMEMOTO */ 2550a42af91cSHajimu UMEMOTO static int 255190d1d883SHajimu UMEMOTO res_queryN(const char *name, struct res_target *target, res_state res) 2552a42af91cSHajimu UMEMOTO { 25535cd588cbSHajimu UMEMOTO u_char *buf; 2554a42af91cSHajimu UMEMOTO HEADER *hp; 2555a42af91cSHajimu UMEMOTO int n; 25561259a17aSHajimu UMEMOTO u_int oflags; 2557a42af91cSHajimu UMEMOTO struct res_target *t; 2558a42af91cSHajimu UMEMOTO int rcode; 2559a42af91cSHajimu UMEMOTO int ancount; 2560a42af91cSHajimu UMEMOTO 2561a42af91cSHajimu UMEMOTO rcode = NOERROR; 2562a42af91cSHajimu UMEMOTO ancount = 0; 2563a42af91cSHajimu UMEMOTO 25645cd588cbSHajimu UMEMOTO buf = malloc(MAXPACKET); 25655cd588cbSHajimu UMEMOTO if (!buf) { 25665342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 25675cd588cbSHajimu UMEMOTO return -1; 25685cd588cbSHajimu UMEMOTO } 25695cd588cbSHajimu UMEMOTO 2570a42af91cSHajimu UMEMOTO for (t = target; t; t = t->next) { 2571a42af91cSHajimu UMEMOTO int class, type; 2572a42af91cSHajimu UMEMOTO u_char *answer; 2573a42af91cSHajimu UMEMOTO int anslen; 2574a42af91cSHajimu UMEMOTO 2575a42af91cSHajimu UMEMOTO hp = (HEADER *)(void *)t->answer; 2576a42af91cSHajimu UMEMOTO 2577a42af91cSHajimu UMEMOTO /* make it easier... */ 2578b826397aSJun-ichiro itojun Hagino class = t->qclass; 2579b826397aSJun-ichiro itojun Hagino type = t->qtype; 2580a42af91cSHajimu UMEMOTO answer = t->answer; 2581a42af91cSHajimu UMEMOTO anslen = t->anslen; 25821259a17aSHajimu UMEMOTO 25831259a17aSHajimu UMEMOTO oflags = res->_flags; 25841259a17aSHajimu UMEMOTO 25851259a17aSHajimu UMEMOTO again: 25861259a17aSHajimu UMEMOTO hp->rcode = NOERROR; /* default */ 25871259a17aSHajimu UMEMOTO 2588a42af91cSHajimu UMEMOTO #ifdef DEBUG 25895342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2590a42af91cSHajimu UMEMOTO printf(";; res_query(%s, %d, %d)\n", name, class, type); 2591a42af91cSHajimu UMEMOTO #endif 2592a42af91cSHajimu UMEMOTO 25935342d17fSHajimu UMEMOTO n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, 25945cd588cbSHajimu UMEMOTO buf, MAXPACKET); 25951259a17aSHajimu UMEMOTO if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 && 25961259a17aSHajimu UMEMOTO (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U) 25975342d17fSHajimu UMEMOTO n = res_nopt(res, n, buf, MAXPACKET, anslen); 2598a42af91cSHajimu UMEMOTO if (n <= 0) { 2599a42af91cSHajimu UMEMOTO #ifdef DEBUG 26005342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2601a42af91cSHajimu UMEMOTO printf(";; res_query: mkquery failed\n"); 2602a42af91cSHajimu UMEMOTO #endif 26035cd588cbSHajimu UMEMOTO free(buf); 26045342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2605a42af91cSHajimu UMEMOTO return (n); 2606a42af91cSHajimu UMEMOTO } 26075342d17fSHajimu UMEMOTO n = res_nsend(res, buf, n, answer, anslen); 2608a42af91cSHajimu UMEMOTO if (n < 0) { 26091259a17aSHajimu UMEMOTO /* 26101259a17aSHajimu UMEMOTO * if the query choked with EDNS0, retry 26111259a17aSHajimu UMEMOTO * without EDNS0 26121259a17aSHajimu UMEMOTO */ 26131259a17aSHajimu UMEMOTO if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) 26141259a17aSHajimu UMEMOTO != 0U && 26151259a17aSHajimu UMEMOTO ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) { 26161259a17aSHajimu UMEMOTO res->_flags |= RES_F_EDNS0ERR; 26171259a17aSHajimu UMEMOTO if (res->options & RES_DEBUG) 26181259a17aSHajimu UMEMOTO printf(";; res_nquery: retry without EDNS0\n"); 26191259a17aSHajimu UMEMOTO goto again; 26201259a17aSHajimu UMEMOTO } 26211259a17aSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */ 2622a42af91cSHajimu UMEMOTO #ifdef DEBUG 26235342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2624a42af91cSHajimu UMEMOTO printf(";; res_query: send error\n"); 2625a42af91cSHajimu UMEMOTO #endif 26261259a17aSHajimu UMEMOTO continue; 2627a42af91cSHajimu UMEMOTO } 2628a42af91cSHajimu UMEMOTO 2629d05ad2abSHajimu UMEMOTO if (n > anslen) 263054384cf3SJacques Vidrine hp->rcode = FORMERR; /* XXX not very informative */ 26311259a17aSHajimu UMEMOTO if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 2632a42af91cSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */ 2633a42af91cSHajimu UMEMOTO #ifdef DEBUG 26345342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2635dd521ef1SHajimu UMEMOTO printf(";; rcode = %u, ancount=%u\n", hp->rcode, 2636a42af91cSHajimu UMEMOTO ntohs(hp->ancount)); 2637a42af91cSHajimu UMEMOTO #endif 2638a42af91cSHajimu UMEMOTO continue; 2639a42af91cSHajimu UMEMOTO } 2640a42af91cSHajimu UMEMOTO 2641a42af91cSHajimu UMEMOTO ancount += ntohs(hp->ancount); 2642a42af91cSHajimu UMEMOTO 2643a42af91cSHajimu UMEMOTO t->n = n; 2644a42af91cSHajimu UMEMOTO } 2645a42af91cSHajimu UMEMOTO 26465cd588cbSHajimu UMEMOTO free(buf); 26475cd588cbSHajimu UMEMOTO 2648a42af91cSHajimu UMEMOTO if (ancount == 0) { 2649a42af91cSHajimu UMEMOTO switch (rcode) { 2650a42af91cSHajimu UMEMOTO case NXDOMAIN: 26515342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, HOST_NOT_FOUND); 2652a42af91cSHajimu UMEMOTO break; 2653a42af91cSHajimu UMEMOTO case SERVFAIL: 26545342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 2655a42af91cSHajimu UMEMOTO break; 2656a42af91cSHajimu UMEMOTO case NOERROR: 26575342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_DATA); 2658a42af91cSHajimu UMEMOTO break; 2659a42af91cSHajimu UMEMOTO case FORMERR: 2660a42af91cSHajimu UMEMOTO case NOTIMP: 2661a42af91cSHajimu UMEMOTO case REFUSED: 2662a42af91cSHajimu UMEMOTO default: 26635342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2664a42af91cSHajimu UMEMOTO break; 2665a42af91cSHajimu UMEMOTO } 2666a42af91cSHajimu UMEMOTO return (-1); 2667a42af91cSHajimu UMEMOTO } 2668a42af91cSHajimu UMEMOTO return (ancount); 2669a42af91cSHajimu UMEMOTO } 2670a42af91cSHajimu UMEMOTO 2671a42af91cSHajimu UMEMOTO /* 2672a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and retrieve answer in supplied buffer. 2673a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 2674a42af91cSHajimu UMEMOTO * If enabled, implement search rules until answer or unrecoverable failure 2675a42af91cSHajimu UMEMOTO * is detected. Error code, if any, is left in h_errno. 2676a42af91cSHajimu UMEMOTO */ 2677a42af91cSHajimu UMEMOTO static int 267890d1d883SHajimu UMEMOTO res_searchN(const char *name, struct res_target *target, res_state res) 2679a42af91cSHajimu UMEMOTO { 2680a42af91cSHajimu UMEMOTO const char *cp, * const *domain; 2681a42af91cSHajimu UMEMOTO HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ 2682a42af91cSHajimu UMEMOTO u_int dots; 2683a42af91cSHajimu UMEMOTO int trailing_dot, ret, saved_herrno; 26845ecccefbSHajimu UMEMOTO int got_nodata = 0, got_servfail = 0, root_on_list = 0; 26855ecccefbSHajimu UMEMOTO int tried_as_is = 0; 26865ecccefbSHajimu UMEMOTO int searched = 0; 2687159d2a98SHajimu UMEMOTO char abuf[MAXDNAME]; 2688a42af91cSHajimu UMEMOTO 2689a42af91cSHajimu UMEMOTO errno = 0; 26905342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */ 2691a42af91cSHajimu UMEMOTO dots = 0; 2692a42af91cSHajimu UMEMOTO for (cp = name; *cp; cp++) 2693a42af91cSHajimu UMEMOTO dots += (*cp == '.'); 2694a42af91cSHajimu UMEMOTO trailing_dot = 0; 2695a42af91cSHajimu UMEMOTO if (cp > name && *--cp == '.') 2696a42af91cSHajimu UMEMOTO trailing_dot++; 2697a42af91cSHajimu UMEMOTO 2698a42af91cSHajimu UMEMOTO /* 2699a42af91cSHajimu UMEMOTO * if there aren't any dots, it could be a user-level alias 2700a42af91cSHajimu UMEMOTO */ 27015342d17fSHajimu UMEMOTO if (!dots && 27025342d17fSHajimu UMEMOTO (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL) 27035342d17fSHajimu UMEMOTO return (res_queryN(cp, target, res)); 2704a42af91cSHajimu UMEMOTO 2705a42af91cSHajimu UMEMOTO /* 27065ecccefbSHajimu UMEMOTO * If there are enough dots in the name, let's just give it a 27075ecccefbSHajimu UMEMOTO * try 'as is'. The threshold can be set with the "ndots" option. 27085ecccefbSHajimu UMEMOTO * Also, query 'as is', if there is a trailing dot in the name. 2709a42af91cSHajimu UMEMOTO */ 2710a42af91cSHajimu UMEMOTO saved_herrno = -1; 27115342d17fSHajimu UMEMOTO if (dots >= res->ndots || trailing_dot) { 27125342d17fSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target, res); 27135ecccefbSHajimu UMEMOTO if (ret > 0 || trailing_dot) 2714a42af91cSHajimu UMEMOTO return (ret); 27153a0b3b67SHajimu UMEMOTO if (errno == ECONNREFUSED) { 27165342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 27173a0b3b67SHajimu UMEMOTO return (-1); 27183a0b3b67SHajimu UMEMOTO } 27195342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 27203a0b3b67SHajimu UMEMOTO case NO_DATA: 27213a0b3b67SHajimu UMEMOTO case HOST_NOT_FOUND: 27223a0b3b67SHajimu UMEMOTO break; 2723aebc6ec2SHajimu UMEMOTO case TRY_AGAIN: 2724aebc6ec2SHajimu UMEMOTO if (hp->rcode == SERVFAIL) 2725aebc6ec2SHajimu UMEMOTO break; 2726aebc6ec2SHajimu UMEMOTO /* FALLTHROUGH */ 27273a0b3b67SHajimu UMEMOTO default: 27283a0b3b67SHajimu UMEMOTO return (-1); 27293a0b3b67SHajimu UMEMOTO } 27305342d17fSHajimu UMEMOTO saved_herrno = res->res_h_errno; 2731a42af91cSHajimu UMEMOTO tried_as_is++; 2732a42af91cSHajimu UMEMOTO } 2733a42af91cSHajimu UMEMOTO 2734a42af91cSHajimu UMEMOTO /* 2735a42af91cSHajimu UMEMOTO * We do at least one level of search if 2736a42af91cSHajimu UMEMOTO * - there is no dot and RES_DEFNAME is set, or 2737a42af91cSHajimu UMEMOTO * - there is at least one dot, there is no trailing dot, 2738a42af91cSHajimu UMEMOTO * and RES_DNSRCH is set. 2739a42af91cSHajimu UMEMOTO */ 27405342d17fSHajimu UMEMOTO if ((!dots && (res->options & RES_DEFNAMES)) || 27415342d17fSHajimu UMEMOTO (dots && !trailing_dot && (res->options & RES_DNSRCH))) { 2742a42af91cSHajimu UMEMOTO int done = 0; 2743a42af91cSHajimu UMEMOTO 27445342d17fSHajimu UMEMOTO for (domain = (const char * const *)res->dnsrch; 2745a42af91cSHajimu UMEMOTO *domain && !done; 2746a42af91cSHajimu UMEMOTO domain++) { 27475ecccefbSHajimu UMEMOTO searched = 1; 27485ecccefbSHajimu UMEMOTO 27495ecccefbSHajimu UMEMOTO if (domain[0][0] == '\0' || 27505ecccefbSHajimu UMEMOTO (domain[0][0] == '.' && domain[0][1] == '\0')) 27515ecccefbSHajimu UMEMOTO root_on_list++; 27525ecccefbSHajimu UMEMOTO 27535ecccefbSHajimu UMEMOTO if (root_on_list && tried_as_is) 27545ecccefbSHajimu UMEMOTO continue; 2755a42af91cSHajimu UMEMOTO 27565342d17fSHajimu UMEMOTO ret = res_querydomainN(name, *domain, target, res); 2757a42af91cSHajimu UMEMOTO if (ret > 0) 2758a42af91cSHajimu UMEMOTO return (ret); 2759a42af91cSHajimu UMEMOTO 2760a42af91cSHajimu UMEMOTO /* 2761a42af91cSHajimu UMEMOTO * If no server present, give up. 2762a42af91cSHajimu UMEMOTO * If name isn't found in this domain, 2763a42af91cSHajimu UMEMOTO * keep trying higher domains in the search list 2764a42af91cSHajimu UMEMOTO * (if that's enabled). 2765a42af91cSHajimu UMEMOTO * On a NO_DATA error, keep trying, otherwise 2766a42af91cSHajimu UMEMOTO * a wildcard entry of another type could keep us 2767a42af91cSHajimu UMEMOTO * from finding this entry higher in the domain. 2768a42af91cSHajimu UMEMOTO * If we get some other error (negative answer or 2769a42af91cSHajimu UMEMOTO * server failure), then stop searching up, 2770a42af91cSHajimu UMEMOTO * but try the input name below in case it's 2771a42af91cSHajimu UMEMOTO * fully-qualified. 2772a42af91cSHajimu UMEMOTO */ 2773a42af91cSHajimu UMEMOTO if (errno == ECONNREFUSED) { 27745342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 2775a42af91cSHajimu UMEMOTO return (-1); 2776a42af91cSHajimu UMEMOTO } 2777a42af91cSHajimu UMEMOTO 27785342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 2779a42af91cSHajimu UMEMOTO case NO_DATA: 2780a42af91cSHajimu UMEMOTO got_nodata++; 2781a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 2782a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 2783a42af91cSHajimu UMEMOTO /* keep trying */ 2784a42af91cSHajimu UMEMOTO break; 2785a42af91cSHajimu UMEMOTO case TRY_AGAIN: 2786aebc6ec2SHajimu UMEMOTO got_servfail++; 2787a42af91cSHajimu UMEMOTO if (hp->rcode == SERVFAIL) { 2788a42af91cSHajimu UMEMOTO /* try next search element, if any */ 2789a42af91cSHajimu UMEMOTO break; 2790a42af91cSHajimu UMEMOTO } 2791a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 2792a42af91cSHajimu UMEMOTO default: 2793a42af91cSHajimu UMEMOTO /* anything else implies that we're done */ 2794a42af91cSHajimu UMEMOTO done++; 2795a42af91cSHajimu UMEMOTO } 2796a42af91cSHajimu UMEMOTO /* 2797a42af91cSHajimu UMEMOTO * if we got here for some reason other than DNSRCH, 2798a42af91cSHajimu UMEMOTO * we only wanted one iteration of the loop, so stop. 2799a42af91cSHajimu UMEMOTO */ 28005342d17fSHajimu UMEMOTO if (!(res->options & RES_DNSRCH)) 2801a42af91cSHajimu UMEMOTO done++; 2802a42af91cSHajimu UMEMOTO } 2803a42af91cSHajimu UMEMOTO } 2804a42af91cSHajimu UMEMOTO 28055342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 28063a0b3b67SHajimu UMEMOTO case NO_DATA: 28073a0b3b67SHajimu UMEMOTO case HOST_NOT_FOUND: 28083a0b3b67SHajimu UMEMOTO break; 2809aebc6ec2SHajimu UMEMOTO case TRY_AGAIN: 2810aebc6ec2SHajimu UMEMOTO if (hp->rcode == SERVFAIL) 2811aebc6ec2SHajimu UMEMOTO break; 2812aebc6ec2SHajimu UMEMOTO /* FALLTHROUGH */ 28133a0b3b67SHajimu UMEMOTO default: 28143a0b3b67SHajimu UMEMOTO goto giveup; 28153a0b3b67SHajimu UMEMOTO } 28163a0b3b67SHajimu UMEMOTO 2817a42af91cSHajimu UMEMOTO /* 28185ecccefbSHajimu UMEMOTO * If the query has not already been tried as is then try it 28195ecccefbSHajimu UMEMOTO * unless RES_NOTLDQUERY is set and there were no dots. 2820a42af91cSHajimu UMEMOTO */ 28215342d17fSHajimu UMEMOTO if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) && 28225ecccefbSHajimu UMEMOTO !(tried_as_is || root_on_list)) { 28235342d17fSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target, res); 2824a42af91cSHajimu UMEMOTO if (ret > 0) 2825a42af91cSHajimu UMEMOTO return (ret); 2826a42af91cSHajimu UMEMOTO } 2827a42af91cSHajimu UMEMOTO 2828a42af91cSHajimu UMEMOTO /* 2829a42af91cSHajimu UMEMOTO * if we got here, we didn't satisfy the search. 2830a42af91cSHajimu UMEMOTO * if we did an initial full query, return that query's h_errno 2831a42af91cSHajimu UMEMOTO * (note that we wouldn't be here if that query had succeeded). 2832a42af91cSHajimu UMEMOTO * else if we ever got a nodata, send that back as the reason. 2833a42af91cSHajimu UMEMOTO * else send back meaningless h_errno, that being the one from 2834a42af91cSHajimu UMEMOTO * the last DNSRCH we did. 2835a42af91cSHajimu UMEMOTO */ 28363a0b3b67SHajimu UMEMOTO giveup: 2837a42af91cSHajimu UMEMOTO if (saved_herrno != -1) 28385342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, saved_herrno); 2839a42af91cSHajimu UMEMOTO else if (got_nodata) 28405342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_DATA); 2841a42af91cSHajimu UMEMOTO else if (got_servfail) 28425342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 2843a42af91cSHajimu UMEMOTO return (-1); 2844a42af91cSHajimu UMEMOTO } 2845a42af91cSHajimu UMEMOTO 2846a42af91cSHajimu UMEMOTO /* 2847a42af91cSHajimu UMEMOTO * Perform a call on res_query on the concatenation of name and domain, 2848a42af91cSHajimu UMEMOTO * removing a trailing dot from name if domain is NULL. 2849a42af91cSHajimu UMEMOTO */ 2850a42af91cSHajimu UMEMOTO static int 285190d1d883SHajimu UMEMOTO res_querydomainN(const char *name, const char *domain, 285290d1d883SHajimu UMEMOTO struct res_target *target, res_state res) 2853a42af91cSHajimu UMEMOTO { 2854a42af91cSHajimu UMEMOTO char nbuf[MAXDNAME]; 2855a42af91cSHajimu UMEMOTO const char *longname = nbuf; 2856a42af91cSHajimu UMEMOTO size_t n, d; 2857a42af91cSHajimu UMEMOTO 2858a42af91cSHajimu UMEMOTO #ifdef DEBUG 28595342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2860a42af91cSHajimu UMEMOTO printf(";; res_querydomain(%s, %s)\n", 2861a42af91cSHajimu UMEMOTO name, domain?domain:"<Nil>"); 2862a42af91cSHajimu UMEMOTO #endif 2863a42af91cSHajimu UMEMOTO if (domain == NULL) { 2864a42af91cSHajimu UMEMOTO /* 2865a42af91cSHajimu UMEMOTO * Check for trailing '.'; 2866a42af91cSHajimu UMEMOTO * copy without '.' if present. 2867a42af91cSHajimu UMEMOTO */ 2868a42af91cSHajimu UMEMOTO n = strlen(name); 2869a42af91cSHajimu UMEMOTO if (n >= MAXDNAME) { 28705342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2871a42af91cSHajimu UMEMOTO return (-1); 2872a42af91cSHajimu UMEMOTO } 2873a42af91cSHajimu UMEMOTO if (n > 0 && name[--n] == '.') { 2874a42af91cSHajimu UMEMOTO strncpy(nbuf, name, n); 2875a42af91cSHajimu UMEMOTO nbuf[n] = '\0'; 2876a42af91cSHajimu UMEMOTO } else 2877a42af91cSHajimu UMEMOTO longname = name; 2878a42af91cSHajimu UMEMOTO } else { 2879a42af91cSHajimu UMEMOTO n = strlen(name); 2880a42af91cSHajimu UMEMOTO d = strlen(domain); 2881a42af91cSHajimu UMEMOTO if (n + d + 1 >= MAXDNAME) { 28825342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2883a42af91cSHajimu UMEMOTO return (-1); 2884a42af91cSHajimu UMEMOTO } 2885dd521ef1SHajimu UMEMOTO snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); 2886a42af91cSHajimu UMEMOTO } 28875342d17fSHajimu UMEMOTO return (res_queryN(longname, target, res)); 2888a42af91cSHajimu UMEMOTO } 2889