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 * - Thread safe-ness must be checked. 377d56d374SYoshinobu Inoue * - Return values. There are nonstandard return values defined and used 387d56d374SYoshinobu Inoue * in the source code. This is because RFC2553 is silent about which error 397d56d374SYoshinobu Inoue * code must be returned for which situation. 40a42af91cSHajimu UMEMOTO * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is 41dd521ef1SHajimu UMEMOTO * invalid. current code - SEGV on freeaddrinfo(NULL) 42dd521ef1SHajimu UMEMOTO * 4300a8a579SYoshinobu Inoue * Note: 4400a8a579SYoshinobu Inoue * - The code filters out AFs that are not supported by the kernel, 4500a8a579SYoshinobu Inoue * when globbing NULL hostname (to loopback, or wildcard). Is it the right 4600a8a579SYoshinobu Inoue * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 4700a8a579SYoshinobu Inoue * in ai_flags? 48a42af91cSHajimu UMEMOTO * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. 49a42af91cSHajimu UMEMOTO * (1) what should we do against numeric hostname (2) what should we do 50a42af91cSHajimu UMEMOTO * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? 51a42af91cSHajimu UMEMOTO * non-loopback address configured? global address configured? 52dd521ef1SHajimu UMEMOTO * 53dd521ef1SHajimu UMEMOTO * OS specific notes for netbsd/openbsd/freebsd4/bsdi4: 54a42af91cSHajimu UMEMOTO * - To avoid search order issue, we have a big amount of code duplicate 55a42af91cSHajimu UMEMOTO * from gethnamaddr.c and some other places. The issues that there's no 56a42af91cSHajimu UMEMOTO * lower layer function to lookup "IPv4 or IPv6" record. Calling 57a42af91cSHajimu UMEMOTO * gethostbyname2 from getaddrinfo will end up in wrong search order, as 58dd521ef1SHajimu UMEMOTO * presented above. 59dd521ef1SHajimu UMEMOTO * 60dd521ef1SHajimu UMEMOTO * OS specific notes for freebsd4: 61dd521ef1SHajimu UMEMOTO * - FreeBSD supported $GAI. The code does not. 627d56d374SYoshinobu Inoue */ 637d56d374SYoshinobu Inoue 64333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 65333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 66333fc21eSDavid E. O'Brien 67d201fe46SDaniel Eischen #include "namespace.h" 687d56d374SYoshinobu Inoue #include <sys/types.h> 697d56d374SYoshinobu Inoue #include <sys/param.h> 707d56d374SYoshinobu Inoue #include <sys/socket.h> 717d56d374SYoshinobu Inoue #include <net/if.h> 727d56d374SYoshinobu Inoue #include <netinet/in.h> 734c6867a8SHajimu UMEMOTO #include <sys/queue.h> 744c6867a8SHajimu UMEMOTO #ifdef INET6 754c6867a8SHajimu UMEMOTO #include <net/if_var.h> 764c6867a8SHajimu UMEMOTO #include <sys/sysctl.h> 774d489f47SHajimu UMEMOTO #include <sys/ioctl.h> 784c6867a8SHajimu UMEMOTO #include <netinet6/in6_var.h> /* XXX */ 794c6867a8SHajimu UMEMOTO #endif 807d56d374SYoshinobu Inoue #include <arpa/inet.h> 817d56d374SYoshinobu Inoue #include <arpa/nameser.h> 82bf838688SHajimu UMEMOTO #include <rpc/rpc.h> 83bf838688SHajimu UMEMOTO #include <rpcsvc/yp_prot.h> 84bf838688SHajimu UMEMOTO #include <rpcsvc/ypclnt.h> 857d56d374SYoshinobu Inoue #include <netdb.h> 867d56d374SYoshinobu Inoue #include <resolv.h> 877d56d374SYoshinobu Inoue #include <string.h> 887d56d374SYoshinobu Inoue #include <stdlib.h> 897d56d374SYoshinobu Inoue #include <stddef.h> 907d56d374SYoshinobu Inoue #include <ctype.h> 917d56d374SYoshinobu Inoue #include <unistd.h> 927d56d374SYoshinobu Inoue #include <stdio.h> 93a42af91cSHajimu UMEMOTO #include <errno.h> 9469b0a4b6SJim Pirzyk 9569b0a4b6SJim Pirzyk #include "res_config.h" 9669b0a4b6SJim Pirzyk 97ce6282dbSHajimu UMEMOTO #ifdef DEBUG 98ce6282dbSHajimu UMEMOTO #include <syslog.h> 99ce6282dbSHajimu UMEMOTO #endif 1007d56d374SYoshinobu Inoue 101248aee62SJacques Vidrine #include <stdarg.h> 102248aee62SJacques Vidrine #include <nsswitch.h> 103d201fe46SDaniel Eischen #include "un-namespace.h" 1042bbd7cf8SJacques Vidrine #include "libc_private.h" 10506a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 10606a99fe3SHajimu UMEMOTO #include "nscache.h" 10706a99fe3SHajimu UMEMOTO #endif 108248aee62SJacques Vidrine 1097d56d374SYoshinobu Inoue #if defined(__KAME__) && defined(INET6) 1107d56d374SYoshinobu Inoue # define FAITH 1117d56d374SYoshinobu Inoue #endif 1127d56d374SYoshinobu Inoue 1137d56d374SYoshinobu Inoue #define SUCCESS 0 1147d56d374SYoshinobu Inoue #define ANY 0 1157d56d374SYoshinobu Inoue #define YES 1 1167d56d374SYoshinobu Inoue #define NO 0 1177d56d374SYoshinobu Inoue 1187d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 }; 119dd521ef1SHajimu UMEMOTO static const char in_loopback[] = { 127, 0, 0, 1 }; 120dd521ef1SHajimu UMEMOTO #ifdef INET6 1217d56d374SYoshinobu Inoue static const char in6_addrany[] = { 1227d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 1237d56d374SYoshinobu Inoue }; 1247d56d374SYoshinobu Inoue static const char in6_loopback[] = { 1257d56d374SYoshinobu Inoue 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 1267d56d374SYoshinobu Inoue }; 127dd521ef1SHajimu UMEMOTO #endif 1287d56d374SYoshinobu Inoue 1294c6867a8SHajimu UMEMOTO struct policyqueue { 1304c6867a8SHajimu UMEMOTO TAILQ_ENTRY(policyqueue) pc_entry; 1314c6867a8SHajimu UMEMOTO #ifdef INET6 1324c6867a8SHajimu UMEMOTO struct in6_addrpolicy pc_policy; 1334c6867a8SHajimu UMEMOTO #endif 1344c6867a8SHajimu UMEMOTO }; 1354c6867a8SHajimu UMEMOTO TAILQ_HEAD(policyhead, policyqueue); 1364c6867a8SHajimu UMEMOTO 1377d56d374SYoshinobu Inoue static const struct afd { 1387d56d374SYoshinobu Inoue int a_af; 1397d56d374SYoshinobu Inoue int a_addrlen; 140036ae3ddSHajimu UMEMOTO socklen_t a_socklen; 1417d56d374SYoshinobu Inoue int a_off; 1427d56d374SYoshinobu Inoue const char *a_addrany; 1437d56d374SYoshinobu Inoue const char *a_loopback; 1447d56d374SYoshinobu Inoue int a_scoped; 1457d56d374SYoshinobu Inoue } afdl [] = { 1467d56d374SYoshinobu Inoue #ifdef INET6 1477d56d374SYoshinobu Inoue #define N_INET6 0 1487d56d374SYoshinobu Inoue {PF_INET6, sizeof(struct in6_addr), 1497d56d374SYoshinobu Inoue sizeof(struct sockaddr_in6), 1507d56d374SYoshinobu Inoue offsetof(struct sockaddr_in6, sin6_addr), 1517d56d374SYoshinobu Inoue in6_addrany, in6_loopback, 1}, 1527d56d374SYoshinobu Inoue #define N_INET 1 1537d56d374SYoshinobu Inoue #else 1547d56d374SYoshinobu Inoue #define N_INET 0 1557d56d374SYoshinobu Inoue #endif 1567d56d374SYoshinobu Inoue {PF_INET, sizeof(struct in_addr), 1577d56d374SYoshinobu Inoue sizeof(struct sockaddr_in), 1587d56d374SYoshinobu Inoue offsetof(struct sockaddr_in, sin_addr), 1597d56d374SYoshinobu Inoue in_addrany, in_loopback, 0}, 1607d56d374SYoshinobu Inoue {0, 0, 0, 0, NULL, NULL, 0}, 1617d56d374SYoshinobu Inoue }; 1627d56d374SYoshinobu Inoue 1637d56d374SYoshinobu Inoue struct explore { 164a42af91cSHajimu UMEMOTO int e_af; 1657d56d374SYoshinobu Inoue int e_socktype; 1667d56d374SYoshinobu Inoue int e_protocol; 1677d56d374SYoshinobu Inoue const char *e_protostr; 1687d56d374SYoshinobu Inoue int e_wild; 1697d56d374SYoshinobu Inoue #define WILD_AF(ex) ((ex)->e_wild & 0x01) 1707d56d374SYoshinobu Inoue #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 1717d56d374SYoshinobu Inoue #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 1727d56d374SYoshinobu Inoue }; 1737d56d374SYoshinobu Inoue 1747d56d374SYoshinobu Inoue static const struct explore explore[] = { 175a42af91cSHajimu UMEMOTO #if 0 176bf838688SHajimu UMEMOTO { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, 177a42af91cSHajimu UMEMOTO #endif 178a42af91cSHajimu UMEMOTO #ifdef INET6 179bf838688SHajimu UMEMOTO { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 180bf838688SHajimu UMEMOTO { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 181bf838688SHajimu UMEMOTO { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, 182a42af91cSHajimu UMEMOTO #endif 183bf838688SHajimu UMEMOTO { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 184bf838688SHajimu UMEMOTO { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 185bf838688SHajimu UMEMOTO { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, 186bf838688SHajimu UMEMOTO { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 187bf838688SHajimu UMEMOTO { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 188bf838688SHajimu UMEMOTO { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, 189a42af91cSHajimu UMEMOTO { -1, 0, 0, NULL, 0 }, 1907d56d374SYoshinobu Inoue }; 1917d56d374SYoshinobu Inoue 1927d56d374SYoshinobu Inoue #ifdef INET6 1937d56d374SYoshinobu Inoue #define PTON_MAX 16 1947d56d374SYoshinobu Inoue #else 1957d56d374SYoshinobu Inoue #define PTON_MAX 4 1967d56d374SYoshinobu Inoue #endif 1977d56d374SYoshinobu Inoue 1984c6867a8SHajimu UMEMOTO #define AIO_SRCFLAG_DEPRECATED 0x1 1994c6867a8SHajimu UMEMOTO 2004c6867a8SHajimu UMEMOTO struct ai_order { 2014c6867a8SHajimu UMEMOTO union { 2024c6867a8SHajimu UMEMOTO struct sockaddr_storage aiou_ss; 2034c6867a8SHajimu UMEMOTO struct sockaddr aiou_sa; 2044c6867a8SHajimu UMEMOTO } aio_src_un; 2054c6867a8SHajimu UMEMOTO #define aio_srcsa aio_src_un.aiou_sa 2064c6867a8SHajimu UMEMOTO u_int32_t aio_srcflag; 2074c6867a8SHajimu UMEMOTO int aio_srcscope; 2084c6867a8SHajimu UMEMOTO int aio_dstscope; 2094c6867a8SHajimu UMEMOTO struct policyqueue *aio_srcpolicy; 2104c6867a8SHajimu UMEMOTO struct policyqueue *aio_dstpolicy; 2114c6867a8SHajimu UMEMOTO struct addrinfo *aio_ai; 2124c6867a8SHajimu UMEMOTO int aio_matchlen; 2134c6867a8SHajimu UMEMOTO }; 2144c6867a8SHajimu UMEMOTO 215248aee62SJacques Vidrine static const ns_src default_dns_files[] = { 216248aee62SJacques Vidrine { NSSRC_FILES, NS_SUCCESS }, 217248aee62SJacques Vidrine { NSSRC_DNS, NS_SUCCESS }, 218248aee62SJacques Vidrine { 0 } 219248aee62SJacques Vidrine }; 220248aee62SJacques Vidrine 221a42af91cSHajimu UMEMOTO struct res_target { 222a42af91cSHajimu UMEMOTO struct res_target *next; 223a42af91cSHajimu UMEMOTO const char *name; /* domain name */ 224b826397aSJun-ichiro itojun Hagino int qclass, qtype; /* class and type of query */ 225a42af91cSHajimu UMEMOTO u_char *answer; /* buffer to put answer */ 226a42af91cSHajimu UMEMOTO int anslen; /* size of answer buffer */ 227a42af91cSHajimu UMEMOTO int n; /* result length */ 228a42af91cSHajimu UMEMOTO }; 229a42af91cSHajimu UMEMOTO 230688a5c3eSHajimu UMEMOTO #define MAXPACKET (64*1024) 231688a5c3eSHajimu UMEMOTO 232688a5c3eSHajimu UMEMOTO typedef union { 233688a5c3eSHajimu UMEMOTO HEADER hdr; 234688a5c3eSHajimu UMEMOTO u_char buf[MAXPACKET]; 235688a5c3eSHajimu UMEMOTO } querybuf; 236688a5c3eSHajimu UMEMOTO 237c224435eSHajimu UMEMOTO static int str2number(const char *); 2381372519bSDavid E. O'Brien static int explore_null(const struct addrinfo *, 2391372519bSDavid E. O'Brien const char *, struct addrinfo **); 2401372519bSDavid E. O'Brien static int explore_numeric(const struct addrinfo *, const char *, 2418755b918SHajimu UMEMOTO const char *, struct addrinfo **, const char *); 2421372519bSDavid E. O'Brien static int explore_numeric_scope(const struct addrinfo *, const char *, 2431372519bSDavid E. O'Brien const char *, struct addrinfo **); 2441372519bSDavid E. O'Brien static int get_canonname(const struct addrinfo *, 2451372519bSDavid E. O'Brien struct addrinfo *, const char *); 2461372519bSDavid E. O'Brien static struct addrinfo *get_ai(const struct addrinfo *, 2471372519bSDavid E. O'Brien const struct afd *, const char *); 248c05ac53bSDavid E. O'Brien static int get_portmatch(const struct addrinfo *, const char *); 249c05ac53bSDavid E. O'Brien static int get_port(struct addrinfo *, const char *, int); 250c05ac53bSDavid E. O'Brien static const struct afd *find_afd(int); 251bf838688SHajimu UMEMOTO static int addrconfig(struct addrinfo *); 2524d489f47SHajimu UMEMOTO static void set_source(struct ai_order *, struct policyhead *); 2534c6867a8SHajimu UMEMOTO static int comp_dst(const void *, const void *); 254a42af91cSHajimu UMEMOTO #ifdef INET6 255a89c1d30SHajimu UMEMOTO static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); 256a42af91cSHajimu UMEMOTO #endif 2574c6867a8SHajimu UMEMOTO static int gai_addr2scopetype(struct sockaddr *); 258a42af91cSHajimu UMEMOTO 259688a5c3eSHajimu UMEMOTO static int explore_fqdn(const struct addrinfo *, const char *, 260688a5c3eSHajimu UMEMOTO const char *, struct addrinfo **); 261688a5c3eSHajimu UMEMOTO 2624c6867a8SHajimu UMEMOTO static int reorder(struct addrinfo *); 2634c6867a8SHajimu UMEMOTO static int get_addrselectpolicy(struct policyhead *); 2644c6867a8SHajimu UMEMOTO static void free_addrselectpolicy(struct policyhead *); 2654c6867a8SHajimu UMEMOTO static struct policyqueue *match_addrselectpolicy(struct sockaddr *, 2664c6867a8SHajimu UMEMOTO struct policyhead *); 2674d489f47SHajimu UMEMOTO static int matchlen(struct sockaddr *, struct sockaddr *); 2684c6867a8SHajimu UMEMOTO 2691372519bSDavid E. O'Brien static struct addrinfo *getanswer(const querybuf *, int, const char *, int, 2705342d17fSHajimu UMEMOTO const struct addrinfo *, res_state); 271688a5c3eSHajimu UMEMOTO #if defined(RESOLVSORT) 2725342d17fSHajimu UMEMOTO static int addr4sort(struct addrinfo *, res_state); 273688a5c3eSHajimu UMEMOTO #endif 274a89c1d30SHajimu UMEMOTO static int _dns_getaddrinfo(void *, void *, va_list); 2759def31dcSHajimu UMEMOTO static void _sethtent(FILE **); 2769def31dcSHajimu UMEMOTO static void _endhtent(FILE **); 2779def31dcSHajimu UMEMOTO static struct addrinfo *_gethtent(FILE **, const char *, 2789def31dcSHajimu UMEMOTO const struct addrinfo *); 279c05ac53bSDavid E. O'Brien static int _files_getaddrinfo(void *, void *, va_list); 280a42af91cSHajimu UMEMOTO #ifdef YP 281c05ac53bSDavid E. O'Brien static struct addrinfo *_yphostent(char *, const struct addrinfo *); 282c05ac53bSDavid E. O'Brien static int _yp_getaddrinfo(void *, void *, va_list); 283a42af91cSHajimu UMEMOTO #endif 28406a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 28506a99fe3SHajimu UMEMOTO static int addrinfo_id_func(char *, size_t *, va_list, void *); 28606a99fe3SHajimu UMEMOTO static int addrinfo_marshal_func(char *, size_t *, void *, va_list, void *); 28706a99fe3SHajimu UMEMOTO static int addrinfo_unmarshal_func(char *, size_t, void *, va_list, void *); 28806a99fe3SHajimu UMEMOTO #endif 289a42af91cSHajimu UMEMOTO 2905342d17fSHajimu UMEMOTO static int res_queryN(const char *, struct res_target *, res_state); 2915342d17fSHajimu UMEMOTO static int res_searchN(const char *, struct res_target *, res_state); 2921372519bSDavid E. O'Brien static int res_querydomainN(const char *, const char *, 2935342d17fSHajimu UMEMOTO struct res_target *, res_state); 294a42af91cSHajimu UMEMOTO 2957d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */ 2967d56d374SYoshinobu Inoue 2977d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \ 2987d56d374SYoshinobu Inoue do { \ 2997d56d374SYoshinobu Inoue /* external reference: pai, error, and label free */ \ 3007d56d374SYoshinobu Inoue (ai) = get_ai(pai, (afd), (addr)); \ 3017d56d374SYoshinobu Inoue if ((ai) == NULL) { \ 3027d56d374SYoshinobu Inoue error = EAI_MEMORY; \ 3037d56d374SYoshinobu Inoue goto free; \ 3047d56d374SYoshinobu Inoue } \ 305a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3067d56d374SYoshinobu Inoue 3077d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \ 3087d56d374SYoshinobu Inoue do { \ 3097d56d374SYoshinobu Inoue /* external reference: error and label free */ \ 3107d56d374SYoshinobu Inoue error = get_port((ai), (serv), 0); \ 3117d56d374SYoshinobu Inoue if (error != 0) \ 3127d56d374SYoshinobu Inoue goto free; \ 313a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3147d56d374SYoshinobu Inoue 3157d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \ 3167d56d374SYoshinobu Inoue do { \ 3177d56d374SYoshinobu Inoue /* external reference: pai, error and label free */ \ 3187d56d374SYoshinobu Inoue error = get_canonname(pai, (ai), (str)); \ 3197d56d374SYoshinobu Inoue if (error != 0) \ 3207d56d374SYoshinobu Inoue goto free; \ 321a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3227d56d374SYoshinobu Inoue 3237d56d374SYoshinobu Inoue #define ERR(err) \ 3247d56d374SYoshinobu Inoue do { \ 3257d56d374SYoshinobu Inoue /* external reference: error, and label bad */ \ 3267d56d374SYoshinobu Inoue error = (err); \ 3277d56d374SYoshinobu Inoue goto bad; \ 328a42af91cSHajimu UMEMOTO /*NOTREACHED*/ \ 329a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0) 3307d56d374SYoshinobu Inoue 3317d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \ 332a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 3337d56d374SYoshinobu Inoue #define MATCH(x, y, w) \ 334a42af91cSHajimu UMEMOTO ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 3357d56d374SYoshinobu Inoue 3367d56d374SYoshinobu Inoue void 33790d1d883SHajimu UMEMOTO freeaddrinfo(struct addrinfo *ai) 3387d56d374SYoshinobu Inoue { 3397d56d374SYoshinobu Inoue struct addrinfo *next; 3407d56d374SYoshinobu Inoue 3417d56d374SYoshinobu Inoue do { 3427d56d374SYoshinobu Inoue next = ai->ai_next; 3437d56d374SYoshinobu Inoue if (ai->ai_canonname) 3447d56d374SYoshinobu Inoue free(ai->ai_canonname); 3457d56d374SYoshinobu Inoue /* no need to free(ai->ai_addr) */ 3467d56d374SYoshinobu Inoue free(ai); 347a42af91cSHajimu UMEMOTO ai = next; 348a42af91cSHajimu UMEMOTO } while (ai); 3497d56d374SYoshinobu Inoue } 3507d56d374SYoshinobu Inoue 3517d56d374SYoshinobu Inoue static int 35290d1d883SHajimu UMEMOTO str2number(const char *p) 3537d56d374SYoshinobu Inoue { 354ec20fe00SJun-ichiro itojun Hagino char *ep; 355c224435eSHajimu UMEMOTO unsigned long v; 356ec20fe00SJun-ichiro itojun Hagino 357ec20fe00SJun-ichiro itojun Hagino if (*p == '\0') 358c224435eSHajimu UMEMOTO return -1; 359ec20fe00SJun-ichiro itojun Hagino ep = NULL; 360a89c1d30SHajimu UMEMOTO errno = 0; 361c224435eSHajimu UMEMOTO v = strtoul(p, &ep, 10); 362c224435eSHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) 363c224435eSHajimu UMEMOTO return v; 364ec20fe00SJun-ichiro itojun Hagino else 365c224435eSHajimu UMEMOTO return -1; 3667d56d374SYoshinobu Inoue } 3677d56d374SYoshinobu Inoue 3687d56d374SYoshinobu Inoue int 36990d1d883SHajimu UMEMOTO getaddrinfo(const char *hostname, const char *servname, 37090d1d883SHajimu UMEMOTO const struct addrinfo *hints, struct addrinfo **res) 3717d56d374SYoshinobu Inoue { 3727d56d374SYoshinobu Inoue struct addrinfo sentinel; 3737d56d374SYoshinobu Inoue struct addrinfo *cur; 3747d56d374SYoshinobu Inoue int error = 0; 375bf838688SHajimu UMEMOTO struct addrinfo ai; 376bf838688SHajimu UMEMOTO struct addrinfo ai0; 3777d56d374SYoshinobu Inoue struct addrinfo *pai; 3787d56d374SYoshinobu Inoue const struct explore *ex; 3794c6867a8SHajimu UMEMOTO int numeric = 0; 3807d56d374SYoshinobu Inoue 381a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 3827d56d374SYoshinobu Inoue cur = &sentinel; 3837d56d374SYoshinobu Inoue pai = &ai; 3847d56d374SYoshinobu Inoue pai->ai_flags = 0; 3857d56d374SYoshinobu Inoue pai->ai_family = PF_UNSPEC; 3867d56d374SYoshinobu Inoue pai->ai_socktype = ANY; 3877d56d374SYoshinobu Inoue pai->ai_protocol = ANY; 3887d56d374SYoshinobu Inoue pai->ai_addrlen = 0; 3897d56d374SYoshinobu Inoue pai->ai_canonname = NULL; 3907d56d374SYoshinobu Inoue pai->ai_addr = NULL; 3917d56d374SYoshinobu Inoue pai->ai_next = NULL; 3927d56d374SYoshinobu Inoue 3937d56d374SYoshinobu Inoue if (hostname == NULL && servname == NULL) 3947d56d374SYoshinobu Inoue return EAI_NONAME; 3957d56d374SYoshinobu Inoue if (hints) { 3967d56d374SYoshinobu Inoue /* error check for hints */ 3977d56d374SYoshinobu Inoue if (hints->ai_addrlen || hints->ai_canonname || 3987d56d374SYoshinobu Inoue hints->ai_addr || hints->ai_next) 3997d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); /* xxx */ 4007d56d374SYoshinobu Inoue if (hints->ai_flags & ~AI_MASK) 4017d56d374SYoshinobu Inoue ERR(EAI_BADFLAGS); 4027d56d374SYoshinobu Inoue switch (hints->ai_family) { 4037d56d374SYoshinobu Inoue case PF_UNSPEC: 4047d56d374SYoshinobu Inoue case PF_INET: 4057d56d374SYoshinobu Inoue #ifdef INET6 4067d56d374SYoshinobu Inoue case PF_INET6: 4077d56d374SYoshinobu Inoue #endif 4087d56d374SYoshinobu Inoue break; 4097d56d374SYoshinobu Inoue default: 4107d56d374SYoshinobu Inoue ERR(EAI_FAMILY); 4117d56d374SYoshinobu Inoue } 4127d56d374SYoshinobu Inoue memcpy(pai, hints, sizeof(*pai)); 4137d56d374SYoshinobu Inoue 4147d56d374SYoshinobu Inoue /* 4157d56d374SYoshinobu Inoue * if both socktype/protocol are specified, check if they 4167d56d374SYoshinobu Inoue * are meaningful combination. 4177d56d374SYoshinobu Inoue */ 4187d56d374SYoshinobu Inoue if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 419a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 420bf838688SHajimu UMEMOTO if (pai->ai_family != ex->e_af) 421a42af91cSHajimu UMEMOTO continue; 422bf838688SHajimu UMEMOTO if (ex->e_socktype == ANY) 4237d56d374SYoshinobu Inoue continue; 424bf838688SHajimu UMEMOTO if (ex->e_protocol == ANY) 4257d56d374SYoshinobu Inoue continue; 426bf838688SHajimu UMEMOTO if (pai->ai_socktype == ex->e_socktype && 427bf838688SHajimu UMEMOTO pai->ai_protocol != ex->e_protocol) { 4287d56d374SYoshinobu Inoue ERR(EAI_BADHINTS); 4297d56d374SYoshinobu Inoue } 4307d56d374SYoshinobu Inoue } 4317d56d374SYoshinobu Inoue } 432bf838688SHajimu UMEMOTO } 4337d56d374SYoshinobu Inoue 4347d56d374SYoshinobu Inoue /* 435a42af91cSHajimu UMEMOTO * post-2553: AI_ALL and AI_V4MAPPED are effective only against 436dd521ef1SHajimu UMEMOTO * AF_INET6 query. They need to be ignored if specified in other 437a42af91cSHajimu UMEMOTO * occassions. 438a42af91cSHajimu UMEMOTO */ 439a42af91cSHajimu UMEMOTO switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) { 440a42af91cSHajimu UMEMOTO case AI_V4MAPPED: 441a42af91cSHajimu UMEMOTO case AI_ALL | AI_V4MAPPED: 442a42af91cSHajimu UMEMOTO if (pai->ai_family != AF_INET6) 443a42af91cSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 444a42af91cSHajimu UMEMOTO break; 445a42af91cSHajimu UMEMOTO case AI_ALL: 446a42af91cSHajimu UMEMOTO #if 1 447a42af91cSHajimu UMEMOTO /* illegal */ 448a42af91cSHajimu UMEMOTO ERR(EAI_BADFLAGS); 449a42af91cSHajimu UMEMOTO #else 450a42af91cSHajimu UMEMOTO pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 451a42af91cSHajimu UMEMOTO #endif 452a42af91cSHajimu UMEMOTO break; 453a42af91cSHajimu UMEMOTO } 454a42af91cSHajimu UMEMOTO 455a42af91cSHajimu UMEMOTO /* 456a42af91cSHajimu UMEMOTO * check for special cases. (1) numeric servname is disallowed if 457a42af91cSHajimu UMEMOTO * socktype/protocol are left unspecified. (2) servname is disallowed 458a42af91cSHajimu UMEMOTO * for raw and other inet{,6} sockets. 4597d56d374SYoshinobu Inoue */ 4607d56d374SYoshinobu Inoue if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 461a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4627d56d374SYoshinobu Inoue || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 4637d56d374SYoshinobu Inoue #endif 4647d56d374SYoshinobu Inoue ) { 465a42af91cSHajimu UMEMOTO ai0 = *pai; /* backup *pai */ 4667d56d374SYoshinobu Inoue 467a42af91cSHajimu UMEMOTO if (pai->ai_family == PF_UNSPEC) { 468a42af91cSHajimu UMEMOTO #ifdef PF_INET6 4697d56d374SYoshinobu Inoue pai->ai_family = PF_INET6; 4707d56d374SYoshinobu Inoue #else 4717d56d374SYoshinobu Inoue pai->ai_family = PF_INET; 4727d56d374SYoshinobu Inoue #endif 473a42af91cSHajimu UMEMOTO } 4747d56d374SYoshinobu Inoue error = get_portmatch(pai, servname); 4757d56d374SYoshinobu Inoue if (error) 4767d56d374SYoshinobu Inoue ERR(error); 477a42af91cSHajimu UMEMOTO 478a42af91cSHajimu UMEMOTO *pai = ai0; 4797d56d374SYoshinobu Inoue } 4807d56d374SYoshinobu Inoue 481a42af91cSHajimu UMEMOTO ai0 = *pai; 482a42af91cSHajimu UMEMOTO 483bf838688SHajimu UMEMOTO /* NULL hostname, or numeric hostname */ 484a42af91cSHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 4857d56d374SYoshinobu Inoue *pai = ai0; 4867d56d374SYoshinobu Inoue 487bf838688SHajimu UMEMOTO /* PF_UNSPEC entries are prepared for DNS queries only */ 488bf838688SHajimu UMEMOTO if (ex->e_af == PF_UNSPEC) 489bf838688SHajimu UMEMOTO continue; 490a42af91cSHajimu UMEMOTO 491a42af91cSHajimu UMEMOTO if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 4927d56d374SYoshinobu Inoue continue; 493248aee62SJacques Vidrine if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) 494a42af91cSHajimu UMEMOTO continue; 495248aee62SJacques Vidrine if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) 4967d56d374SYoshinobu Inoue continue; 4977d56d374SYoshinobu Inoue 4987d56d374SYoshinobu Inoue if (pai->ai_family == PF_UNSPEC) 499a42af91cSHajimu UMEMOTO pai->ai_family = ex->e_af; 5007d56d374SYoshinobu Inoue if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 5017d56d374SYoshinobu Inoue pai->ai_socktype = ex->e_socktype; 5027d56d374SYoshinobu Inoue if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 5037d56d374SYoshinobu Inoue pai->ai_protocol = ex->e_protocol; 5047d56d374SYoshinobu Inoue 505bf838688SHajimu UMEMOTO if (hostname == NULL) 506bf838688SHajimu UMEMOTO error = explore_null(pai, servname, &cur->ai_next); 507bf838688SHajimu UMEMOTO else 5088755b918SHajimu UMEMOTO error = explore_numeric_scope(pai, hostname, servname, 5098755b918SHajimu UMEMOTO &cur->ai_next); 5107d56d374SYoshinobu Inoue 511bf838688SHajimu UMEMOTO if (error) 512bf838688SHajimu UMEMOTO goto free; 5137d56d374SYoshinobu Inoue 5147d56d374SYoshinobu Inoue while (cur && cur->ai_next) 5157d56d374SYoshinobu Inoue cur = cur->ai_next; 5167d56d374SYoshinobu Inoue } 5177d56d374SYoshinobu Inoue 518bf838688SHajimu UMEMOTO /* 519bf838688SHajimu UMEMOTO * XXX 520bf838688SHajimu UMEMOTO * If numreic representation of AF1 can be interpreted as FQDN 521bf838688SHajimu UMEMOTO * representation of AF2, we need to think again about the code below. 522bf838688SHajimu UMEMOTO */ 5234c6867a8SHajimu UMEMOTO if (sentinel.ai_next) { 5244c6867a8SHajimu UMEMOTO numeric = 1; 525bf838688SHajimu UMEMOTO goto good; 5264c6867a8SHajimu UMEMOTO } 527bf838688SHajimu UMEMOTO 528bf838688SHajimu UMEMOTO if (hostname == NULL) 529bf838688SHajimu UMEMOTO ERR(EAI_NONAME); /* used to be EAI_NODATA */ 530bf838688SHajimu UMEMOTO if (pai->ai_flags & AI_NUMERICHOST) 531bf838688SHajimu UMEMOTO ERR(EAI_NONAME); 532bf838688SHajimu UMEMOTO 533bf838688SHajimu UMEMOTO if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0)) 534bf838688SHajimu UMEMOTO ERR(EAI_FAIL); 535a42af91cSHajimu UMEMOTO 536f95d4633SHajimu UMEMOTO /* 537bf838688SHajimu UMEMOTO * hostname as alphabetical name. 538bf838688SHajimu UMEMOTO * we would like to prefer AF_INET6 than AF_INET, so we'll make a 539bf838688SHajimu UMEMOTO * outer loop by AFs. 540f95d4633SHajimu UMEMOTO */ 541bf838688SHajimu UMEMOTO for (ex = explore; ex->e_af >= 0; ex++) { 542bf838688SHajimu UMEMOTO *pai = ai0; 543f95d4633SHajimu UMEMOTO 544bf838688SHajimu UMEMOTO /* require exact match for family field */ 545bf838688SHajimu UMEMOTO if (pai->ai_family != ex->e_af) 546f95d4633SHajimu UMEMOTO continue; 547f95d4633SHajimu UMEMOTO 548bf838688SHajimu UMEMOTO if (!MATCH(pai->ai_socktype, ex->e_socktype, 549bf838688SHajimu UMEMOTO WILD_SOCKTYPE(ex))) { 550bf838688SHajimu UMEMOTO continue; 551bf838688SHajimu UMEMOTO } 552bf838688SHajimu UMEMOTO if (!MATCH(pai->ai_protocol, ex->e_protocol, 553bf838688SHajimu UMEMOTO WILD_PROTOCOL(ex))) { 554bf838688SHajimu UMEMOTO continue; 555f95d4633SHajimu UMEMOTO } 556f95d4633SHajimu UMEMOTO 557bf838688SHajimu UMEMOTO if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 558bf838688SHajimu UMEMOTO pai->ai_socktype = ex->e_socktype; 559bf838688SHajimu UMEMOTO if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 560bf838688SHajimu UMEMOTO pai->ai_protocol = ex->e_protocol; 561bf838688SHajimu UMEMOTO 562bf838688SHajimu UMEMOTO error = explore_fqdn(pai, hostname, servname, 563bf838688SHajimu UMEMOTO &cur->ai_next); 564bf838688SHajimu UMEMOTO 565bf838688SHajimu UMEMOTO while (cur && cur->ai_next) 566f95d4633SHajimu UMEMOTO cur = cur->ai_next; 567f95d4633SHajimu UMEMOTO } 568f95d4633SHajimu UMEMOTO 5694c6867a8SHajimu UMEMOTO /* XXX inhibit errors if we have the result */ 570bf838688SHajimu UMEMOTO if (sentinel.ai_next) 571bf838688SHajimu UMEMOTO error = 0; 572f95d4633SHajimu UMEMOTO 5734c6867a8SHajimu UMEMOTO good: 5744c6867a8SHajimu UMEMOTO /* 5754c6867a8SHajimu UMEMOTO * ensure we return either: 5764c6867a8SHajimu UMEMOTO * - error == 0, non-NULL *res 5774c6867a8SHajimu UMEMOTO * - error != 0, NULL *res 5784c6867a8SHajimu UMEMOTO */ 579bf838688SHajimu UMEMOTO if (error == 0) { 580bf838688SHajimu UMEMOTO if (sentinel.ai_next) { 5814c6867a8SHajimu UMEMOTO /* 5824c6867a8SHajimu UMEMOTO * If the returned entry is for an active connection, 5834c6867a8SHajimu UMEMOTO * and the given name is not numeric, reorder the 5844c6867a8SHajimu UMEMOTO * list, so that the application would try the list 5854c6867a8SHajimu UMEMOTO * in the most efficient order. 5864c6867a8SHajimu UMEMOTO */ 5874c6867a8SHajimu UMEMOTO if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) { 5884c6867a8SHajimu UMEMOTO if (!numeric) 5894c6867a8SHajimu UMEMOTO (void)reorder(&sentinel); 5904c6867a8SHajimu UMEMOTO } 591bf838688SHajimu UMEMOTO *res = sentinel.ai_next; 592bf838688SHajimu UMEMOTO return SUCCESS; 593bf838688SHajimu UMEMOTO } else 594bf838688SHajimu UMEMOTO error = EAI_FAIL; 595bf838688SHajimu UMEMOTO } 596bf838688SHajimu UMEMOTO free: 597bf838688SHajimu UMEMOTO bad: 598bf838688SHajimu UMEMOTO if (sentinel.ai_next) 599f95d4633SHajimu UMEMOTO freeaddrinfo(sentinel.ai_next); 600bf838688SHajimu UMEMOTO *res = NULL; 6017d56d374SYoshinobu Inoue return error; 6027d56d374SYoshinobu Inoue } 6037d56d374SYoshinobu Inoue 6044c6867a8SHajimu UMEMOTO static int 60590d1d883SHajimu UMEMOTO reorder(struct addrinfo *sentinel) 6064c6867a8SHajimu UMEMOTO { 6074c6867a8SHajimu UMEMOTO struct addrinfo *ai, **aip; 6084c6867a8SHajimu UMEMOTO struct ai_order *aio; 6094c6867a8SHajimu UMEMOTO int i, n; 6104c6867a8SHajimu UMEMOTO struct policyhead policyhead; 6114c6867a8SHajimu UMEMOTO 6124c6867a8SHajimu UMEMOTO /* count the number of addrinfo elements for sorting. */ 6134c6867a8SHajimu UMEMOTO for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++) 6144c6867a8SHajimu UMEMOTO ; 6154c6867a8SHajimu UMEMOTO 6164c6867a8SHajimu UMEMOTO /* 6174c6867a8SHajimu UMEMOTO * If the number is small enough, we can skip the reordering process. 6184c6867a8SHajimu UMEMOTO */ 6194c6867a8SHajimu UMEMOTO if (n <= 1) 6204c6867a8SHajimu UMEMOTO return(n); 6214c6867a8SHajimu UMEMOTO 6224c6867a8SHajimu UMEMOTO /* allocate a temporary array for sort and initialization of it. */ 6234c6867a8SHajimu UMEMOTO if ((aio = malloc(sizeof(*aio) * n)) == NULL) 6244c6867a8SHajimu UMEMOTO return(n); /* give up reordering */ 6254c6867a8SHajimu UMEMOTO memset(aio, 0, sizeof(*aio) * n); 6264c6867a8SHajimu UMEMOTO 6274c6867a8SHajimu UMEMOTO /* retrieve address selection policy from the kernel */ 6284c6867a8SHajimu UMEMOTO TAILQ_INIT(&policyhead); 6294c6867a8SHajimu UMEMOTO if (!get_addrselectpolicy(&policyhead)) { 6304c6867a8SHajimu UMEMOTO /* no policy is installed into kernel, we don't sort. */ 6314c6867a8SHajimu UMEMOTO free(aio); 6324c6867a8SHajimu UMEMOTO return (n); 6334c6867a8SHajimu UMEMOTO } 6344c6867a8SHajimu UMEMOTO 6354c6867a8SHajimu UMEMOTO for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) { 6364c6867a8SHajimu UMEMOTO aio[i].aio_ai = ai; 6374c6867a8SHajimu UMEMOTO aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr); 6384c6867a8SHajimu UMEMOTO aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr, 6394c6867a8SHajimu UMEMOTO &policyhead); 6404d489f47SHajimu UMEMOTO set_source(&aio[i], &policyhead); 6414c6867a8SHajimu UMEMOTO } 6424c6867a8SHajimu UMEMOTO 6434c6867a8SHajimu UMEMOTO /* perform sorting. */ 6444c6867a8SHajimu UMEMOTO qsort(aio, n, sizeof(*aio), comp_dst); 6454c6867a8SHajimu UMEMOTO 6464c6867a8SHajimu UMEMOTO /* reorder the addrinfo chain. */ 6474c6867a8SHajimu UMEMOTO for (i = 0, aip = &sentinel->ai_next; i < n; i++) { 6484c6867a8SHajimu UMEMOTO *aip = aio[i].aio_ai; 6494c6867a8SHajimu UMEMOTO aip = &aio[i].aio_ai->ai_next; 6504c6867a8SHajimu UMEMOTO } 6514c6867a8SHajimu UMEMOTO *aip = NULL; 6524c6867a8SHajimu UMEMOTO 6534c6867a8SHajimu UMEMOTO /* cleanup and return */ 6544c6867a8SHajimu UMEMOTO free(aio); 6554c6867a8SHajimu UMEMOTO free_addrselectpolicy(&policyhead); 6564c6867a8SHajimu UMEMOTO return(n); 6574c6867a8SHajimu UMEMOTO } 6584c6867a8SHajimu UMEMOTO 6594c6867a8SHajimu UMEMOTO static int 66090d1d883SHajimu UMEMOTO get_addrselectpolicy(struct policyhead *head) 6614c6867a8SHajimu UMEMOTO { 6624c6867a8SHajimu UMEMOTO #ifdef INET6 6634c6867a8SHajimu UMEMOTO int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY }; 6644c6867a8SHajimu UMEMOTO size_t l; 6654c6867a8SHajimu UMEMOTO char *buf; 6664c6867a8SHajimu UMEMOTO struct in6_addrpolicy *pol, *ep; 6674c6867a8SHajimu UMEMOTO 6684c6867a8SHajimu UMEMOTO if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) 6694c6867a8SHajimu UMEMOTO return (0); 6704c6867a8SHajimu UMEMOTO if ((buf = malloc(l)) == NULL) 6714c6867a8SHajimu UMEMOTO return (0); 6724c6867a8SHajimu UMEMOTO if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 6734c6867a8SHajimu UMEMOTO free(buf); 6744c6867a8SHajimu UMEMOTO return (0); 6754c6867a8SHajimu UMEMOTO } 6764c6867a8SHajimu UMEMOTO 6774c6867a8SHajimu UMEMOTO ep = (struct in6_addrpolicy *)(buf + l); 6784c6867a8SHajimu UMEMOTO for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) { 6794c6867a8SHajimu UMEMOTO struct policyqueue *new; 6804c6867a8SHajimu UMEMOTO 6814c6867a8SHajimu UMEMOTO if ((new = malloc(sizeof(*new))) == NULL) { 6824c6867a8SHajimu UMEMOTO free_addrselectpolicy(head); /* make the list empty */ 6834c6867a8SHajimu UMEMOTO break; 6844c6867a8SHajimu UMEMOTO } 6854c6867a8SHajimu UMEMOTO new->pc_policy = *pol; 6864c6867a8SHajimu UMEMOTO TAILQ_INSERT_TAIL(head, new, pc_entry); 6874c6867a8SHajimu UMEMOTO } 6884c6867a8SHajimu UMEMOTO 6894c6867a8SHajimu UMEMOTO free(buf); 6904c6867a8SHajimu UMEMOTO return (1); 6914c6867a8SHajimu UMEMOTO #else 6924c6867a8SHajimu UMEMOTO return (0); 6934c6867a8SHajimu UMEMOTO #endif 6944c6867a8SHajimu UMEMOTO } 6954c6867a8SHajimu UMEMOTO 6964c6867a8SHajimu UMEMOTO static void 69790d1d883SHajimu UMEMOTO free_addrselectpolicy(struct policyhead *head) 6984c6867a8SHajimu UMEMOTO { 6994c6867a8SHajimu UMEMOTO struct policyqueue *ent, *nent; 7004c6867a8SHajimu UMEMOTO 7014c6867a8SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = nent) { 7024c6867a8SHajimu UMEMOTO nent = TAILQ_NEXT(ent, pc_entry); 7034c6867a8SHajimu UMEMOTO TAILQ_REMOVE(head, ent, pc_entry); 7044c6867a8SHajimu UMEMOTO free(ent); 7054c6867a8SHajimu UMEMOTO } 7064c6867a8SHajimu UMEMOTO } 7074c6867a8SHajimu UMEMOTO 7084c6867a8SHajimu UMEMOTO static struct policyqueue * 70990d1d883SHajimu UMEMOTO match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head) 7104c6867a8SHajimu UMEMOTO { 7114c6867a8SHajimu UMEMOTO #ifdef INET6 7124c6867a8SHajimu UMEMOTO struct policyqueue *ent, *bestent = NULL; 7134c6867a8SHajimu UMEMOTO struct in6_addrpolicy *pol; 7144c6867a8SHajimu UMEMOTO int matchlen, bestmatchlen = -1; 7154c6867a8SHajimu UMEMOTO u_char *mp, *ep, *k, *p, m; 7164c6867a8SHajimu UMEMOTO struct sockaddr_in6 key; 7174c6867a8SHajimu UMEMOTO 7184c6867a8SHajimu UMEMOTO switch(addr->sa_family) { 7194c6867a8SHajimu UMEMOTO case AF_INET6: 7204c6867a8SHajimu UMEMOTO key = *(struct sockaddr_in6 *)addr; 7214c6867a8SHajimu UMEMOTO break; 7224c6867a8SHajimu UMEMOTO case AF_INET: 7234c6867a8SHajimu UMEMOTO /* convert the address into IPv4-mapped IPv6 address. */ 7244c6867a8SHajimu UMEMOTO memset(&key, 0, sizeof(key)); 7254c6867a8SHajimu UMEMOTO key.sin6_family = AF_INET6; 7264c6867a8SHajimu UMEMOTO key.sin6_len = sizeof(key); 7274c6867a8SHajimu UMEMOTO key.sin6_addr.s6_addr[10] = 0xff; 7284c6867a8SHajimu UMEMOTO key.sin6_addr.s6_addr[11] = 0xff; 7294c6867a8SHajimu UMEMOTO memcpy(&key.sin6_addr.s6_addr[12], 7304c6867a8SHajimu UMEMOTO &((struct sockaddr_in *)addr)->sin_addr, 4); 7314c6867a8SHajimu UMEMOTO break; 7324c6867a8SHajimu UMEMOTO default: 7334c6867a8SHajimu UMEMOTO return(NULL); 7344c6867a8SHajimu UMEMOTO } 7354c6867a8SHajimu UMEMOTO 7364c6867a8SHajimu UMEMOTO for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) { 7374c6867a8SHajimu UMEMOTO pol = &ent->pc_policy; 7384c6867a8SHajimu UMEMOTO matchlen = 0; 7394c6867a8SHajimu UMEMOTO 7404c6867a8SHajimu UMEMOTO mp = (u_char *)&pol->addrmask.sin6_addr; 7414c6867a8SHajimu UMEMOTO ep = mp + 16; /* XXX: scope field? */ 7424c6867a8SHajimu UMEMOTO k = (u_char *)&key.sin6_addr; 7434c6867a8SHajimu UMEMOTO p = (u_char *)&pol->addr.sin6_addr; 7444c6867a8SHajimu UMEMOTO for (; mp < ep && *mp; mp++, k++, p++) { 7454c6867a8SHajimu UMEMOTO m = *mp; 7464c6867a8SHajimu UMEMOTO if ((*k & m) != *p) 7474c6867a8SHajimu UMEMOTO goto next; /* not match */ 7484c6867a8SHajimu UMEMOTO if (m == 0xff) /* short cut for a typical case */ 7494c6867a8SHajimu UMEMOTO matchlen += 8; 7504c6867a8SHajimu UMEMOTO else { 7514c6867a8SHajimu UMEMOTO while (m >= 0x80) { 7524c6867a8SHajimu UMEMOTO matchlen++; 7534c6867a8SHajimu UMEMOTO m <<= 1; 7544c6867a8SHajimu UMEMOTO } 7554c6867a8SHajimu UMEMOTO } 7564c6867a8SHajimu UMEMOTO } 7574c6867a8SHajimu UMEMOTO 7584c6867a8SHajimu UMEMOTO /* matched. check if this is better than the current best. */ 7594c6867a8SHajimu UMEMOTO if (matchlen > bestmatchlen) { 7604c6867a8SHajimu UMEMOTO bestent = ent; 7614c6867a8SHajimu UMEMOTO bestmatchlen = matchlen; 7624c6867a8SHajimu UMEMOTO } 7634c6867a8SHajimu UMEMOTO 7644c6867a8SHajimu UMEMOTO next: 7654c6867a8SHajimu UMEMOTO continue; 7664c6867a8SHajimu UMEMOTO } 7674c6867a8SHajimu UMEMOTO 7684c6867a8SHajimu UMEMOTO return(bestent); 7694c6867a8SHajimu UMEMOTO #else 7704c6867a8SHajimu UMEMOTO return(NULL); 7714c6867a8SHajimu UMEMOTO #endif 7724c6867a8SHajimu UMEMOTO 7734c6867a8SHajimu UMEMOTO } 7744c6867a8SHajimu UMEMOTO 7754d489f47SHajimu UMEMOTO static void 77690d1d883SHajimu UMEMOTO set_source(struct ai_order *aio, struct policyhead *ph) 7774d489f47SHajimu UMEMOTO { 7784d489f47SHajimu UMEMOTO struct addrinfo ai = *aio->aio_ai; 7794d489f47SHajimu UMEMOTO struct sockaddr_storage ss; 780943db5a2SHajimu UMEMOTO socklen_t srclen; 781943db5a2SHajimu UMEMOTO int s; 7824d489f47SHajimu UMEMOTO 7834d489f47SHajimu UMEMOTO /* set unspec ("no source is available"), just in case */ 7844d489f47SHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC; 7854d489f47SHajimu UMEMOTO aio->aio_srcscope = -1; 7864d489f47SHajimu UMEMOTO 7874d489f47SHajimu UMEMOTO switch(ai.ai_family) { 7884d489f47SHajimu UMEMOTO case AF_INET: 7894d489f47SHajimu UMEMOTO #ifdef INET6 7904d489f47SHajimu UMEMOTO case AF_INET6: 7914d489f47SHajimu UMEMOTO #endif 7924d489f47SHajimu UMEMOTO break; 7934d489f47SHajimu UMEMOTO default: /* ignore unsupported AFs explicitly */ 7944d489f47SHajimu UMEMOTO return; 7954d489f47SHajimu UMEMOTO } 7964d489f47SHajimu UMEMOTO 7974d489f47SHajimu UMEMOTO /* XXX: make a dummy addrinfo to call connect() */ 7984d489f47SHajimu UMEMOTO ai.ai_socktype = SOCK_DGRAM; 7994d489f47SHajimu UMEMOTO ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */ 8004d489f47SHajimu UMEMOTO ai.ai_next = NULL; 8014d489f47SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 8024d489f47SHajimu UMEMOTO memcpy(&ss, ai.ai_addr, ai.ai_addrlen); 8034d489f47SHajimu UMEMOTO ai.ai_addr = (struct sockaddr *)&ss; 8044d489f47SHajimu UMEMOTO get_port(&ai, "1", 0); 8054d489f47SHajimu UMEMOTO 8064d489f47SHajimu UMEMOTO /* open a socket to get the source address for the given dst */ 8074d489f47SHajimu UMEMOTO if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0) 8084d489f47SHajimu UMEMOTO return; /* give up */ 8094d489f47SHajimu UMEMOTO if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0) 8104d489f47SHajimu UMEMOTO goto cleanup; 8114d489f47SHajimu UMEMOTO srclen = ai.ai_addrlen; 8124d489f47SHajimu UMEMOTO if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) { 8134d489f47SHajimu UMEMOTO aio->aio_srcsa.sa_family = AF_UNSPEC; 8144d489f47SHajimu UMEMOTO goto cleanup; 8154d489f47SHajimu UMEMOTO } 8164d489f47SHajimu UMEMOTO aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa); 8174d489f47SHajimu UMEMOTO aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph); 8184d489f47SHajimu UMEMOTO aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr); 8194d489f47SHajimu UMEMOTO #ifdef INET6 8204d489f47SHajimu UMEMOTO if (ai.ai_family == AF_INET6) { 8214d489f47SHajimu UMEMOTO struct in6_ifreq ifr6; 8224d489f47SHajimu UMEMOTO u_int32_t flags6; 8234d489f47SHajimu UMEMOTO 8244d489f47SHajimu UMEMOTO /* XXX: interface name should not be hardcoded */ 8254d489f47SHajimu UMEMOTO strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name)); 8264d489f47SHajimu UMEMOTO memset(&ifr6, 0, sizeof(ifr6)); 8274d489f47SHajimu UMEMOTO memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen); 8284d489f47SHajimu UMEMOTO if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) { 8294d489f47SHajimu UMEMOTO flags6 = ifr6.ifr_ifru.ifru_flags6; 8304d489f47SHajimu UMEMOTO if ((flags6 & IN6_IFF_DEPRECATED)) 8314d489f47SHajimu UMEMOTO aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED; 8324d489f47SHajimu UMEMOTO } 8334d489f47SHajimu UMEMOTO } 8344d489f47SHajimu UMEMOTO #endif 8354d489f47SHajimu UMEMOTO 8364d489f47SHajimu UMEMOTO cleanup: 8374d489f47SHajimu UMEMOTO _close(s); 8384d489f47SHajimu UMEMOTO return; 8394d489f47SHajimu UMEMOTO } 8404d489f47SHajimu UMEMOTO 8414d489f47SHajimu UMEMOTO static int 84290d1d883SHajimu UMEMOTO matchlen(struct sockaddr *src, struct sockaddr *dst) 8434d489f47SHajimu UMEMOTO { 8444d489f47SHajimu UMEMOTO int match = 0; 8454d489f47SHajimu UMEMOTO u_char *s, *d; 8464d489f47SHajimu UMEMOTO u_char *lim, r; 8474d489f47SHajimu UMEMOTO int addrlen; 8484d489f47SHajimu UMEMOTO 8494d489f47SHajimu UMEMOTO switch (src->sa_family) { 8504d489f47SHajimu UMEMOTO #ifdef INET6 8514d489f47SHajimu UMEMOTO case AF_INET6: 8524d489f47SHajimu UMEMOTO s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 8534d489f47SHajimu UMEMOTO d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 8544d489f47SHajimu UMEMOTO addrlen = sizeof(struct in6_addr); 8554d489f47SHajimu UMEMOTO lim = s + addrlen; 8564d489f47SHajimu UMEMOTO break; 8574d489f47SHajimu UMEMOTO #endif 8584d489f47SHajimu UMEMOTO case AF_INET: 859fd57e549SGeorge V. Neville-Neil s = (u_char *)&((struct sockaddr_in *)src)->sin_addr; 860fd57e549SGeorge V. Neville-Neil d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr; 8614d489f47SHajimu UMEMOTO addrlen = sizeof(struct in_addr); 8624d489f47SHajimu UMEMOTO lim = s + addrlen; 8634d489f47SHajimu UMEMOTO break; 8644d489f47SHajimu UMEMOTO default: 8654d489f47SHajimu UMEMOTO return(0); 8664d489f47SHajimu UMEMOTO } 8674d489f47SHajimu UMEMOTO 8684d489f47SHajimu UMEMOTO while (s < lim) 8694d489f47SHajimu UMEMOTO if ((r = (*d++ ^ *s++)) != 0) { 8704d489f47SHajimu UMEMOTO while (r < addrlen * 8) { 8714d489f47SHajimu UMEMOTO match++; 8724d489f47SHajimu UMEMOTO r <<= 1; 8734d489f47SHajimu UMEMOTO } 8744d489f47SHajimu UMEMOTO break; 8754d489f47SHajimu UMEMOTO } else 8764d489f47SHajimu UMEMOTO match += 8; 8774d489f47SHajimu UMEMOTO return(match); 8784d489f47SHajimu UMEMOTO } 8794d489f47SHajimu UMEMOTO 8804c6867a8SHajimu UMEMOTO static int 88190d1d883SHajimu UMEMOTO comp_dst(const void *arg1, const void *arg2) 8824c6867a8SHajimu UMEMOTO { 8834c6867a8SHajimu UMEMOTO const struct ai_order *dst1 = arg1, *dst2 = arg2; 8844c6867a8SHajimu UMEMOTO 8854c6867a8SHajimu UMEMOTO /* 8864c6867a8SHajimu UMEMOTO * Rule 1: Avoid unusable destinations. 8874c6867a8SHajimu UMEMOTO * XXX: we currently do not consider if an appropriate route exists. 8884c6867a8SHajimu UMEMOTO */ 8894c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 8904c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family == AF_UNSPEC) { 8914c6867a8SHajimu UMEMOTO return(-1); 8924c6867a8SHajimu UMEMOTO } 8934c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family == AF_UNSPEC && 8944c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) { 8954c6867a8SHajimu UMEMOTO return(1); 8964c6867a8SHajimu UMEMOTO } 8974c6867a8SHajimu UMEMOTO 8984c6867a8SHajimu UMEMOTO /* Rule 2: Prefer matching scope. */ 8994c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope == dst1->aio_srcscope && 9004c6867a8SHajimu UMEMOTO dst2->aio_dstscope != dst2->aio_srcscope) { 9014c6867a8SHajimu UMEMOTO return(-1); 9024c6867a8SHajimu UMEMOTO } 9034c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope != dst1->aio_srcscope && 9044c6867a8SHajimu UMEMOTO dst2->aio_dstscope == dst2->aio_srcscope) { 9054c6867a8SHajimu UMEMOTO return(1); 9064c6867a8SHajimu UMEMOTO } 9074c6867a8SHajimu UMEMOTO 9084c6867a8SHajimu UMEMOTO /* Rule 3: Avoid deprecated addresses. */ 9094c6867a8SHajimu UMEMOTO if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 9104c6867a8SHajimu UMEMOTO dst2->aio_srcsa.sa_family != AF_UNSPEC) { 9114c6867a8SHajimu UMEMOTO if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 9124c6867a8SHajimu UMEMOTO (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 9134c6867a8SHajimu UMEMOTO return(-1); 9144c6867a8SHajimu UMEMOTO } 9154c6867a8SHajimu UMEMOTO if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 9164c6867a8SHajimu UMEMOTO !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 9174c6867a8SHajimu UMEMOTO return(1); 9184c6867a8SHajimu UMEMOTO } 9194c6867a8SHajimu UMEMOTO } 9204c6867a8SHajimu UMEMOTO 9214c6867a8SHajimu UMEMOTO /* Rule 4: Prefer home addresses. */ 9224c6867a8SHajimu UMEMOTO /* XXX: not implemented yet */ 9234c6867a8SHajimu UMEMOTO 9244c6867a8SHajimu UMEMOTO /* Rule 5: Prefer matching label. */ 9254c6867a8SHajimu UMEMOTO #ifdef INET6 9264c6867a8SHajimu UMEMOTO if (dst1->aio_srcpolicy && dst1->aio_dstpolicy && 9274c6867a8SHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label == 9284c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label && 9294c6867a8SHajimu UMEMOTO (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL || 9304c6867a8SHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label != 9314c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label)) { 9324c6867a8SHajimu UMEMOTO return(-1); 9334c6867a8SHajimu UMEMOTO } 9344c6867a8SHajimu UMEMOTO if (dst2->aio_srcpolicy && dst2->aio_dstpolicy && 9354c6867a8SHajimu UMEMOTO dst2->aio_srcpolicy->pc_policy.label == 9364c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.label && 9374c6867a8SHajimu UMEMOTO (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL || 9384c6867a8SHajimu UMEMOTO dst1->aio_srcpolicy->pc_policy.label != 9394c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.label)) { 9404c6867a8SHajimu UMEMOTO return(1); 9414c6867a8SHajimu UMEMOTO } 9424c6867a8SHajimu UMEMOTO #endif 9434c6867a8SHajimu UMEMOTO 9444c6867a8SHajimu UMEMOTO /* Rule 6: Prefer higher precedence. */ 9454c6867a8SHajimu UMEMOTO #ifdef INET6 9464c6867a8SHajimu UMEMOTO if (dst1->aio_dstpolicy && 9474c6867a8SHajimu UMEMOTO (dst2->aio_dstpolicy == NULL || 9484c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced > 9494c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced)) { 9504c6867a8SHajimu UMEMOTO return(-1); 9514c6867a8SHajimu UMEMOTO } 9524c6867a8SHajimu UMEMOTO if (dst2->aio_dstpolicy && 9534c6867a8SHajimu UMEMOTO (dst1->aio_dstpolicy == NULL || 9544c6867a8SHajimu UMEMOTO dst2->aio_dstpolicy->pc_policy.preced > 9554c6867a8SHajimu UMEMOTO dst1->aio_dstpolicy->pc_policy.preced)) { 9564c6867a8SHajimu UMEMOTO return(1); 9574c6867a8SHajimu UMEMOTO } 9584c6867a8SHajimu UMEMOTO #endif 9594c6867a8SHajimu UMEMOTO 9604c6867a8SHajimu UMEMOTO /* Rule 7: Prefer native transport. */ 9614c6867a8SHajimu UMEMOTO /* XXX: not implemented yet */ 9624c6867a8SHajimu UMEMOTO 9634c6867a8SHajimu UMEMOTO /* Rule 8: Prefer smaller scope. */ 9644c6867a8SHajimu UMEMOTO if (dst1->aio_dstscope >= 0 && 9654c6867a8SHajimu UMEMOTO dst1->aio_dstscope < dst2->aio_dstscope) { 9664c6867a8SHajimu UMEMOTO return(-1); 9674c6867a8SHajimu UMEMOTO } 9684c6867a8SHajimu UMEMOTO if (dst2->aio_dstscope >= 0 && 9694c6867a8SHajimu UMEMOTO dst2->aio_dstscope < dst1->aio_dstscope) { 9704c6867a8SHajimu UMEMOTO return(1); 9714c6867a8SHajimu UMEMOTO } 9724c6867a8SHajimu UMEMOTO 9734c6867a8SHajimu UMEMOTO /* 9744c6867a8SHajimu UMEMOTO * Rule 9: Use longest matching prefix. 9754c6867a8SHajimu UMEMOTO * We compare the match length in a same AF only. 9764c6867a8SHajimu UMEMOTO */ 9774c6867a8SHajimu UMEMOTO if (dst1->aio_ai->ai_addr->sa_family == 9784c6867a8SHajimu UMEMOTO dst2->aio_ai->ai_addr->sa_family) { 9794c6867a8SHajimu UMEMOTO if (dst1->aio_matchlen > dst2->aio_matchlen) { 9804c6867a8SHajimu UMEMOTO return(-1); 9814c6867a8SHajimu UMEMOTO } 9824c6867a8SHajimu UMEMOTO if (dst1->aio_matchlen < dst2->aio_matchlen) { 9834c6867a8SHajimu UMEMOTO return(1); 9844c6867a8SHajimu UMEMOTO } 9854c6867a8SHajimu UMEMOTO } 9864c6867a8SHajimu UMEMOTO 9874c6867a8SHajimu UMEMOTO /* Rule 10: Otherwise, leave the order unchanged. */ 9884c6867a8SHajimu UMEMOTO return(-1); 9894c6867a8SHajimu UMEMOTO } 9904c6867a8SHajimu UMEMOTO 9914c6867a8SHajimu UMEMOTO /* 9924c6867a8SHajimu UMEMOTO * Copy from scope.c. 9934c6867a8SHajimu UMEMOTO * XXX: we should standardize the functions and link them as standard 9944c6867a8SHajimu UMEMOTO * library. 9954c6867a8SHajimu UMEMOTO */ 9964c6867a8SHajimu UMEMOTO static int 99790d1d883SHajimu UMEMOTO gai_addr2scopetype(struct sockaddr *sa) 9984c6867a8SHajimu UMEMOTO { 9994c6867a8SHajimu UMEMOTO #ifdef INET6 10004c6867a8SHajimu UMEMOTO struct sockaddr_in6 *sa6; 10014c6867a8SHajimu UMEMOTO #endif 10024c6867a8SHajimu UMEMOTO struct sockaddr_in *sa4; 10034c6867a8SHajimu UMEMOTO 10044c6867a8SHajimu UMEMOTO switch(sa->sa_family) { 10054c6867a8SHajimu UMEMOTO #ifdef INET6 10064c6867a8SHajimu UMEMOTO case AF_INET6: 10074c6867a8SHajimu UMEMOTO sa6 = (struct sockaddr_in6 *)sa; 10084c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 10094c6867a8SHajimu UMEMOTO /* just use the scope field of the multicast address */ 10104c6867a8SHajimu UMEMOTO return(sa6->sin6_addr.s6_addr[2] & 0x0f); 10114c6867a8SHajimu UMEMOTO } 10124c6867a8SHajimu UMEMOTO /* 10134c6867a8SHajimu UMEMOTO * Unicast addresses: map scope type to corresponding scope 10144c6867a8SHajimu UMEMOTO * value defined for multcast addresses. 10154c6867a8SHajimu UMEMOTO * XXX: hardcoded scope type values are bad... 10164c6867a8SHajimu UMEMOTO */ 10174c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) 10184c6867a8SHajimu UMEMOTO return(1); /* node local scope */ 10194c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) 10204c6867a8SHajimu UMEMOTO return(2); /* link-local scope */ 10214c6867a8SHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr)) 10224c6867a8SHajimu UMEMOTO return(5); /* site-local scope */ 10234c6867a8SHajimu UMEMOTO return(14); /* global scope */ 10244c6867a8SHajimu UMEMOTO break; 10254c6867a8SHajimu UMEMOTO #endif 10264c6867a8SHajimu UMEMOTO case AF_INET: 10274c6867a8SHajimu UMEMOTO /* 10284c6867a8SHajimu UMEMOTO * IPv4 pseudo scoping according to RFC 3484. 10294c6867a8SHajimu UMEMOTO */ 10304c6867a8SHajimu UMEMOTO sa4 = (struct sockaddr_in *)sa; 10314c6867a8SHajimu UMEMOTO /* IPv4 autoconfiguration addresses have link-local scope. */ 10324c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 169 && 10334c6867a8SHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 254) 10344c6867a8SHajimu UMEMOTO return(2); 10354c6867a8SHajimu UMEMOTO /* Private addresses have site-local scope. */ 10364c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 10 || 10374c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 172 && 10384c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) || 10394c6867a8SHajimu UMEMOTO (((u_char *)&sa4->sin_addr)[0] == 192 && 10404c6867a8SHajimu UMEMOTO ((u_char *)&sa4->sin_addr)[1] == 168)) 1041251b48a1SHajimu UMEMOTO return(14); /* XXX: It should be 5 unless NAT */ 10424c6867a8SHajimu UMEMOTO /* Loopback addresses have link-local scope. */ 10434c6867a8SHajimu UMEMOTO if (((u_char *)&sa4->sin_addr)[0] == 127) 10444c6867a8SHajimu UMEMOTO return(2); 10454c6867a8SHajimu UMEMOTO return(14); 10464c6867a8SHajimu UMEMOTO break; 10474c6867a8SHajimu UMEMOTO default: 10484c6867a8SHajimu UMEMOTO errno = EAFNOSUPPORT; /* is this a good error? */ 10494c6867a8SHajimu UMEMOTO return(-1); 10504c6867a8SHajimu UMEMOTO } 10514c6867a8SHajimu UMEMOTO } 10524c6867a8SHajimu UMEMOTO 10537d56d374SYoshinobu Inoue /* 10547d56d374SYoshinobu Inoue * hostname == NULL. 10557d56d374SYoshinobu Inoue * passive socket -> anyaddr (0.0.0.0 or ::) 10567d56d374SYoshinobu Inoue * non-passive socket -> localhost (127.0.0.1 or ::1) 10577d56d374SYoshinobu Inoue */ 10587d56d374SYoshinobu Inoue static int 105990d1d883SHajimu UMEMOTO explore_null(const struct addrinfo *pai, const char *servname, 106090d1d883SHajimu UMEMOTO struct addrinfo **res) 10617d56d374SYoshinobu Inoue { 1062bf838688SHajimu UMEMOTO int s; 10637d56d374SYoshinobu Inoue const struct afd *afd; 10640aceddceSHajimu UMEMOTO struct addrinfo *ai; 10657d56d374SYoshinobu Inoue int error; 10667d56d374SYoshinobu Inoue 10677d56d374SYoshinobu Inoue *res = NULL; 10680aceddceSHajimu UMEMOTO ai = NULL; 10697d56d374SYoshinobu Inoue 10707d56d374SYoshinobu Inoue /* 1071bf838688SHajimu UMEMOTO * filter out AFs that are not supported by the kernel 1072bf838688SHajimu UMEMOTO * XXX errno? 1073bf838688SHajimu UMEMOTO */ 1074bf838688SHajimu UMEMOTO s = _socket(pai->ai_family, SOCK_DGRAM, 0); 1075bf838688SHajimu UMEMOTO if (s < 0) { 1076bf838688SHajimu UMEMOTO if (errno != EMFILE) 1077bf838688SHajimu UMEMOTO return 0; 1078bf838688SHajimu UMEMOTO } else 1079bf838688SHajimu UMEMOTO _close(s); 1080bf838688SHajimu UMEMOTO 1081bf838688SHajimu UMEMOTO /* 1082a42af91cSHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 1083a42af91cSHajimu UMEMOTO */ 1084a42af91cSHajimu UMEMOTO if (get_portmatch(pai, servname) != 0) 1085a42af91cSHajimu UMEMOTO return 0; 1086a42af91cSHajimu UMEMOTO 10877d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 10887d56d374SYoshinobu Inoue if (afd == NULL) 10897d56d374SYoshinobu Inoue return 0; 10907d56d374SYoshinobu Inoue 1091a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_PASSIVE) { 10920aceddceSHajimu UMEMOTO GET_AI(ai, afd, afd->a_addrany); 10930aceddceSHajimu UMEMOTO GET_PORT(ai, servname); 1094a42af91cSHajimu UMEMOTO } else { 10950aceddceSHajimu UMEMOTO GET_AI(ai, afd, afd->a_loopback); 10960aceddceSHajimu UMEMOTO GET_PORT(ai, servname); 1097a42af91cSHajimu UMEMOTO } 10987d56d374SYoshinobu Inoue 10990aceddceSHajimu UMEMOTO *res = ai; 11007d56d374SYoshinobu Inoue return 0; 11017d56d374SYoshinobu Inoue 11027d56d374SYoshinobu Inoue free: 11030aceddceSHajimu UMEMOTO if (ai != NULL) 11040aceddceSHajimu UMEMOTO freeaddrinfo(ai); 11057d56d374SYoshinobu Inoue return error; 11067d56d374SYoshinobu Inoue } 11077d56d374SYoshinobu Inoue 11087d56d374SYoshinobu Inoue /* 11097d56d374SYoshinobu Inoue * numeric hostname 11107d56d374SYoshinobu Inoue */ 11117d56d374SYoshinobu Inoue static int 111290d1d883SHajimu UMEMOTO explore_numeric(const struct addrinfo *pai, const char *hostname, 111390d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res, const char *canonname) 11147d56d374SYoshinobu Inoue { 11157d56d374SYoshinobu Inoue const struct afd *afd; 11160aceddceSHajimu UMEMOTO struct addrinfo *ai; 11177d56d374SYoshinobu Inoue int error; 11187d56d374SYoshinobu Inoue char pton[PTON_MAX]; 11197d56d374SYoshinobu Inoue 11207d56d374SYoshinobu Inoue *res = NULL; 11210aceddceSHajimu UMEMOTO ai = NULL; 11227d56d374SYoshinobu Inoue 1123bf838688SHajimu UMEMOTO /* 1124bf838688SHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 1125bf838688SHajimu UMEMOTO */ 1126bf838688SHajimu UMEMOTO if (get_portmatch(pai, servname) != 0) 1127bf838688SHajimu UMEMOTO return 0; 1128bf838688SHajimu UMEMOTO 11297d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 11307d56d374SYoshinobu Inoue if (afd == NULL) 11317d56d374SYoshinobu Inoue return 0; 11327d56d374SYoshinobu Inoue 1133b826397aSJun-ichiro itojun Hagino switch (afd->a_af) { 1134b826397aSJun-ichiro itojun Hagino case AF_INET: 11350cf41987SHajimu UMEMOTO /* 11360cf41987SHajimu UMEMOTO * RFC3493 requires getaddrinfo() to accept AF_INET formats 11370cf41987SHajimu UMEMOTO * that are accepted by inet_addr() and its family. The 11380cf41987SHajimu UMEMOTO * accepted forms includes the "classful" one, which inet_pton 11390cf41987SHajimu UMEMOTO * does not accept. So we need to separate the case for 11400cf41987SHajimu UMEMOTO * AF_INET. 11410cf41987SHajimu UMEMOTO */ 1142b826397aSJun-ichiro itojun Hagino if (inet_aton(hostname, (struct in_addr *)pton) == 1) { 11437d56d374SYoshinobu Inoue if (pai->ai_family == afd->a_af || 11447d56d374SYoshinobu Inoue pai->ai_family == PF_UNSPEC /*?*/) { 11450aceddceSHajimu UMEMOTO GET_AI(ai, afd, pton); 11460aceddceSHajimu UMEMOTO GET_PORT(ai, servname); 11478755b918SHajimu UMEMOTO if ((pai->ai_flags & AI_CANONNAME)) { 11488755b918SHajimu UMEMOTO /* 11498755b918SHajimu UMEMOTO * Set the numeric address itself as 11508755b918SHajimu UMEMOTO * the canonical name, based on a 11518755b918SHajimu UMEMOTO * clarification in rfc3493. 11528755b918SHajimu UMEMOTO */ 11530aceddceSHajimu UMEMOTO GET_CANONNAME(ai, canonname); 11548755b918SHajimu UMEMOTO } 11557d56d374SYoshinobu Inoue } else 11567d56d374SYoshinobu Inoue ERR(EAI_FAMILY); /*xxx*/ 11577d56d374SYoshinobu Inoue } 1158b826397aSJun-ichiro itojun Hagino break; 1159b826397aSJun-ichiro itojun Hagino default: 1160b826397aSJun-ichiro itojun Hagino if (inet_pton(afd->a_af, hostname, pton) == 1) { 1161b826397aSJun-ichiro itojun Hagino if (pai->ai_family == afd->a_af || 1162b826397aSJun-ichiro itojun Hagino pai->ai_family == PF_UNSPEC /*?*/) { 11630aceddceSHajimu UMEMOTO GET_AI(ai, afd, pton); 11640aceddceSHajimu UMEMOTO GET_PORT(ai, servname); 11658755b918SHajimu UMEMOTO if ((pai->ai_flags & AI_CANONNAME)) { 11668755b918SHajimu UMEMOTO /* 11678755b918SHajimu UMEMOTO * Set the numeric address itself as 11688755b918SHajimu UMEMOTO * the canonical name, based on a 11698755b918SHajimu UMEMOTO * clarification in rfc3493. 11708755b918SHajimu UMEMOTO */ 11710aceddceSHajimu UMEMOTO GET_CANONNAME(ai, canonname); 11728755b918SHajimu UMEMOTO } 1173b826397aSJun-ichiro itojun Hagino } else 1174dd521ef1SHajimu UMEMOTO ERR(EAI_FAMILY); /* XXX */ 1175b826397aSJun-ichiro itojun Hagino } 1176b826397aSJun-ichiro itojun Hagino break; 1177b826397aSJun-ichiro itojun Hagino } 11787d56d374SYoshinobu Inoue 11790aceddceSHajimu UMEMOTO *res = ai; 11807d56d374SYoshinobu Inoue return 0; 11817d56d374SYoshinobu Inoue 11827d56d374SYoshinobu Inoue free: 11837d56d374SYoshinobu Inoue bad: 11840aceddceSHajimu UMEMOTO if (ai != NULL) 11850aceddceSHajimu UMEMOTO freeaddrinfo(ai); 11867d56d374SYoshinobu Inoue return error; 11877d56d374SYoshinobu Inoue } 11887d56d374SYoshinobu Inoue 11897d56d374SYoshinobu Inoue /* 11907d56d374SYoshinobu Inoue * numeric hostname with scope 11917d56d374SYoshinobu Inoue */ 11927d56d374SYoshinobu Inoue static int 119390d1d883SHajimu UMEMOTO explore_numeric_scope(const struct addrinfo *pai, const char *hostname, 119490d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res) 11957d56d374SYoshinobu Inoue { 1196a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6) 11978755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname); 11987d56d374SYoshinobu Inoue #else 11997d56d374SYoshinobu Inoue const struct afd *afd; 12007d56d374SYoshinobu Inoue struct addrinfo *cur; 12017d56d374SYoshinobu Inoue int error; 1202a42af91cSHajimu UMEMOTO char *cp, *hostname2 = NULL, *scope, *addr; 12037d56d374SYoshinobu Inoue struct sockaddr_in6 *sin6; 12047d56d374SYoshinobu Inoue 1205bf838688SHajimu UMEMOTO /* 1206bf838688SHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 1207bf838688SHajimu UMEMOTO */ 1208bf838688SHajimu UMEMOTO if (get_portmatch(pai, servname) != 0) 1209bf838688SHajimu UMEMOTO return 0; 1210bf838688SHajimu UMEMOTO 12117d56d374SYoshinobu Inoue afd = find_afd(pai->ai_family); 12127d56d374SYoshinobu Inoue if (afd == NULL) 12137d56d374SYoshinobu Inoue return 0; 1214248aee62SJacques Vidrine 12157d56d374SYoshinobu Inoue if (!afd->a_scoped) 12168755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname); 12177d56d374SYoshinobu Inoue 12187d56d374SYoshinobu Inoue cp = strchr(hostname, SCOPE_DELIMITER); 12197d56d374SYoshinobu Inoue if (cp == NULL) 12208755b918SHajimu UMEMOTO return explore_numeric(pai, hostname, servname, res, hostname); 12217d56d374SYoshinobu Inoue 12227d56d374SYoshinobu Inoue /* 12237d56d374SYoshinobu Inoue * Handle special case of <scoped_address><delimiter><scope id> 12247d56d374SYoshinobu Inoue */ 12257d56d374SYoshinobu Inoue hostname2 = strdup(hostname); 12267d56d374SYoshinobu Inoue if (hostname2 == NULL) 12277d56d374SYoshinobu Inoue return EAI_MEMORY; 12287d56d374SYoshinobu Inoue /* terminate at the delimiter */ 12297d56d374SYoshinobu Inoue hostname2[cp - hostname] = '\0'; 1230a42af91cSHajimu UMEMOTO addr = hostname2; 1231a42af91cSHajimu UMEMOTO scope = cp + 1; 12327d56d374SYoshinobu Inoue 12338755b918SHajimu UMEMOTO error = explore_numeric(pai, addr, servname, res, hostname); 12347d56d374SYoshinobu Inoue if (error == 0) { 1235a89c1d30SHajimu UMEMOTO u_int32_t scopeid; 1236a42af91cSHajimu UMEMOTO 12377d56d374SYoshinobu Inoue for (cur = *res; cur; cur = cur->ai_next) { 12387d56d374SYoshinobu Inoue if (cur->ai_family != AF_INET6) 12397d56d374SYoshinobu Inoue continue; 1240a42af91cSHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; 1241a89c1d30SHajimu UMEMOTO if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { 1242a42af91cSHajimu UMEMOTO free(hostname2); 124337b3e941SHajimu UMEMOTO return(EAI_NONAME); /* XXX: is return OK? */ 1244a42af91cSHajimu UMEMOTO } 1245a42af91cSHajimu UMEMOTO sin6->sin6_scope_id = scopeid; 12467d56d374SYoshinobu Inoue } 12477d56d374SYoshinobu Inoue } 12487d56d374SYoshinobu Inoue 12497d56d374SYoshinobu Inoue free(hostname2); 12507d56d374SYoshinobu Inoue 12517d56d374SYoshinobu Inoue return error; 12527d56d374SYoshinobu Inoue #endif 12537d56d374SYoshinobu Inoue } 12547d56d374SYoshinobu Inoue 12557d56d374SYoshinobu Inoue static int 125690d1d883SHajimu UMEMOTO get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str) 12577d56d374SYoshinobu Inoue { 12587d56d374SYoshinobu Inoue if ((pai->ai_flags & AI_CANONNAME) != 0) { 12598743b270SHajimu UMEMOTO ai->ai_canonname = strdup(str); 12607d56d374SYoshinobu Inoue if (ai->ai_canonname == NULL) 12617d56d374SYoshinobu Inoue return EAI_MEMORY; 12627d56d374SYoshinobu Inoue } 12637d56d374SYoshinobu Inoue return 0; 12647d56d374SYoshinobu Inoue } 12657d56d374SYoshinobu Inoue 12667d56d374SYoshinobu Inoue static struct addrinfo * 126790d1d883SHajimu UMEMOTO get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr) 12687d56d374SYoshinobu Inoue { 12697d56d374SYoshinobu Inoue char *p; 12707d56d374SYoshinobu Inoue struct addrinfo *ai; 12717d56d374SYoshinobu Inoue #ifdef FAITH 12727d56d374SYoshinobu Inoue struct in6_addr faith_prefix; 12737d56d374SYoshinobu Inoue char *fp_str; 12747d56d374SYoshinobu Inoue int translate = 0; 12757d56d374SYoshinobu Inoue #endif 12767d56d374SYoshinobu Inoue 12777d56d374SYoshinobu Inoue #ifdef FAITH 12787d56d374SYoshinobu Inoue /* 12797d56d374SYoshinobu Inoue * Transfrom an IPv4 addr into a special IPv6 addr format for 12807d56d374SYoshinobu Inoue * IPv6->IPv4 translation gateway. (only TCP is supported now) 12817d56d374SYoshinobu Inoue * 12827d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 12837d56d374SYoshinobu Inoue * | faith prefix part (12 bytes) | embedded | 12847d56d374SYoshinobu Inoue * | | IPv4 addr part (4 bytes) 12857d56d374SYoshinobu Inoue * +-----------------------------------+------------+ 12867d56d374SYoshinobu Inoue * 12877d56d374SYoshinobu Inoue * faith prefix part is specified as ascii IPv6 addr format 12887d56d374SYoshinobu Inoue * in environmental variable GAI. 12897d56d374SYoshinobu Inoue * For FAITH to work correctly, routing to faith prefix must be 12907d56d374SYoshinobu Inoue * setup toward a machine where a FAITH daemon operates. 12917d56d374SYoshinobu Inoue * Also, the machine must enable some mechanizm 12927d56d374SYoshinobu Inoue * (e.g. faith interface hack) to divert those packet with 12937d56d374SYoshinobu Inoue * faith prefixed destination addr to user-land FAITH daemon. 12947d56d374SYoshinobu Inoue */ 12957d56d374SYoshinobu Inoue fp_str = getenv("GAI"); 12967d56d374SYoshinobu Inoue if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 12977d56d374SYoshinobu Inoue afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 12987d56d374SYoshinobu Inoue u_int32_t v4a; 12997d56d374SYoshinobu Inoue u_int8_t v4a_top; 13007d56d374SYoshinobu Inoue 13017d56d374SYoshinobu Inoue memcpy(&v4a, addr, sizeof v4a); 13027d56d374SYoshinobu Inoue v4a_top = v4a >> IN_CLASSA_NSHIFT; 13037d56d374SYoshinobu Inoue if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 13047d56d374SYoshinobu Inoue v4a_top != 0 && v4a != IN_LOOPBACKNET) { 13057d56d374SYoshinobu Inoue afd = &afdl[N_INET6]; 13067d56d374SYoshinobu Inoue memcpy(&faith_prefix.s6_addr[12], addr, 13077d56d374SYoshinobu Inoue sizeof(struct in_addr)); 13087d56d374SYoshinobu Inoue translate = 1; 13097d56d374SYoshinobu Inoue } 13107d56d374SYoshinobu Inoue } 13117d56d374SYoshinobu Inoue #endif 13127d56d374SYoshinobu Inoue 13137d56d374SYoshinobu Inoue ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 13147d56d374SYoshinobu Inoue + (afd->a_socklen)); 13157d56d374SYoshinobu Inoue if (ai == NULL) 13167d56d374SYoshinobu Inoue return NULL; 13177d56d374SYoshinobu Inoue 13187d56d374SYoshinobu Inoue memcpy(ai, pai, sizeof(struct addrinfo)); 1319a42af91cSHajimu UMEMOTO ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 1320a42af91cSHajimu UMEMOTO memset(ai->ai_addr, 0, (size_t)afd->a_socklen); 13217d56d374SYoshinobu Inoue ai->ai_addr->sa_len = afd->a_socklen; 13227d56d374SYoshinobu Inoue ai->ai_addrlen = afd->a_socklen; 13237d56d374SYoshinobu Inoue ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 1324248aee62SJacques Vidrine p = (char *)(void *)(ai->ai_addr); 13257d56d374SYoshinobu Inoue #ifdef FAITH 13267d56d374SYoshinobu Inoue if (translate == 1) 1327248aee62SJacques Vidrine memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen); 13287d56d374SYoshinobu Inoue else 13297d56d374SYoshinobu Inoue #endif 1330248aee62SJacques Vidrine memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); 13317d56d374SYoshinobu Inoue return ai; 13327d56d374SYoshinobu Inoue } 13337d56d374SYoshinobu Inoue 13347d56d374SYoshinobu Inoue static int 133590d1d883SHajimu UMEMOTO get_portmatch(const struct addrinfo *ai, const char *servname) 13367d56d374SYoshinobu Inoue { 1337a42af91cSHajimu UMEMOTO 1338c224435eSHajimu UMEMOTO /* get_port does not touch first argument when matchonly == 1. */ 1339a42af91cSHajimu UMEMOTO /* LINTED const cast */ 13407d56d374SYoshinobu Inoue return get_port((struct addrinfo *)ai, servname, 1); 13417d56d374SYoshinobu Inoue } 13427d56d374SYoshinobu Inoue 13437d56d374SYoshinobu Inoue static int 134490d1d883SHajimu UMEMOTO get_port(struct addrinfo *ai, const char *servname, int matchonly) 13457d56d374SYoshinobu Inoue { 13467d56d374SYoshinobu Inoue const char *proto; 13477d56d374SYoshinobu Inoue struct servent *sp; 13487d56d374SYoshinobu Inoue int port; 13497d56d374SYoshinobu Inoue int allownumeric; 13507d56d374SYoshinobu Inoue 13517d56d374SYoshinobu Inoue if (servname == NULL) 13527d56d374SYoshinobu Inoue return 0; 1353a42af91cSHajimu UMEMOTO switch (ai->ai_family) { 1354a42af91cSHajimu UMEMOTO case AF_INET: 1355a42af91cSHajimu UMEMOTO #ifdef AF_INET6 1356a42af91cSHajimu UMEMOTO case AF_INET6: 13577d56d374SYoshinobu Inoue #endif 1358a42af91cSHajimu UMEMOTO break; 1359a42af91cSHajimu UMEMOTO default: 13607d56d374SYoshinobu Inoue return 0; 1361a42af91cSHajimu UMEMOTO } 13627d56d374SYoshinobu Inoue 13637d56d374SYoshinobu Inoue switch (ai->ai_socktype) { 13647d56d374SYoshinobu Inoue case SOCK_RAW: 13657d56d374SYoshinobu Inoue return EAI_SERVICE; 13667d56d374SYoshinobu Inoue case SOCK_DGRAM: 13677d56d374SYoshinobu Inoue case SOCK_STREAM: 13687d56d374SYoshinobu Inoue allownumeric = 1; 13697d56d374SYoshinobu Inoue break; 13707d56d374SYoshinobu Inoue case ANY: 13717d56d374SYoshinobu Inoue allownumeric = 0; 13727d56d374SYoshinobu Inoue break; 13737d56d374SYoshinobu Inoue default: 13747d56d374SYoshinobu Inoue return EAI_SOCKTYPE; 13757d56d374SYoshinobu Inoue } 13767d56d374SYoshinobu Inoue 1377c224435eSHajimu UMEMOTO port = str2number(servname); 1378c224435eSHajimu UMEMOTO if (port >= 0) { 13797d56d374SYoshinobu Inoue if (!allownumeric) 13807d56d374SYoshinobu Inoue return EAI_SERVICE; 13817d56d374SYoshinobu Inoue if (port < 0 || port > 65535) 13827d56d374SYoshinobu Inoue return EAI_SERVICE; 1383dd521ef1SHajimu UMEMOTO port = htons(port); 13847d56d374SYoshinobu Inoue } else { 1385c224435eSHajimu UMEMOTO if (ai->ai_flags & AI_NUMERICSERV) 1386c224435eSHajimu UMEMOTO return EAI_NONAME; 1387bf838688SHajimu UMEMOTO switch (ai->ai_socktype) { 1388bf838688SHajimu UMEMOTO case SOCK_DGRAM: 13897d56d374SYoshinobu Inoue proto = "udp"; 13907d56d374SYoshinobu Inoue break; 1391bf838688SHajimu UMEMOTO case SOCK_STREAM: 13927d56d374SYoshinobu Inoue proto = "tcp"; 13937d56d374SYoshinobu Inoue break; 13947d56d374SYoshinobu Inoue default: 13957d56d374SYoshinobu Inoue proto = NULL; 13967d56d374SYoshinobu Inoue break; 13977d56d374SYoshinobu Inoue } 13987d56d374SYoshinobu Inoue 139962e8b17dSHajimu UMEMOTO if ((sp = getservbyname(servname, proto)) == NULL) 14007d56d374SYoshinobu Inoue return EAI_SERVICE; 14017d56d374SYoshinobu Inoue port = sp->s_port; 14027d56d374SYoshinobu Inoue } 14037d56d374SYoshinobu Inoue 14047d56d374SYoshinobu Inoue if (!matchonly) { 14057d56d374SYoshinobu Inoue switch (ai->ai_family) { 14067d56d374SYoshinobu Inoue case AF_INET: 1407a42af91cSHajimu UMEMOTO ((struct sockaddr_in *)(void *) 1408a42af91cSHajimu UMEMOTO ai->ai_addr)->sin_port = port; 14097d56d374SYoshinobu Inoue break; 14107d56d374SYoshinobu Inoue #ifdef INET6 14117d56d374SYoshinobu Inoue case AF_INET6: 1412a42af91cSHajimu UMEMOTO ((struct sockaddr_in6 *)(void *) 1413a42af91cSHajimu UMEMOTO ai->ai_addr)->sin6_port = port; 14147d56d374SYoshinobu Inoue break; 14157d56d374SYoshinobu Inoue #endif 14167d56d374SYoshinobu Inoue } 14177d56d374SYoshinobu Inoue } 14187d56d374SYoshinobu Inoue 14197d56d374SYoshinobu Inoue return 0; 14207d56d374SYoshinobu Inoue } 14217d56d374SYoshinobu Inoue 14227d56d374SYoshinobu Inoue static const struct afd * 142390d1d883SHajimu UMEMOTO find_afd(int af) 14247d56d374SYoshinobu Inoue { 14257d56d374SYoshinobu Inoue const struct afd *afd; 14267d56d374SYoshinobu Inoue 14277d56d374SYoshinobu Inoue if (af == PF_UNSPEC) 14287d56d374SYoshinobu Inoue return NULL; 14297d56d374SYoshinobu Inoue for (afd = afdl; afd->a_af; afd++) { 14307d56d374SYoshinobu Inoue if (afd->a_af == af) 14317d56d374SYoshinobu Inoue return afd; 14327d56d374SYoshinobu Inoue } 14337d56d374SYoshinobu Inoue return NULL; 14347d56d374SYoshinobu Inoue } 1435a42af91cSHajimu UMEMOTO 1436a42af91cSHajimu UMEMOTO /* 1437a42af91cSHajimu UMEMOTO * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend 1438a42af91cSHajimu UMEMOTO * will take care of it. 1439a42af91cSHajimu UMEMOTO * the semantics of AI_ADDRCONFIG is not defined well. we are not sure 1440a42af91cSHajimu UMEMOTO * if the code is right or not. 1441bf838688SHajimu UMEMOTO * 1442bf838688SHajimu UMEMOTO * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with 1443bf838688SHajimu UMEMOTO * _dns_getaddrinfo. 1444a42af91cSHajimu UMEMOTO */ 1445a42af91cSHajimu UMEMOTO static int 144690d1d883SHajimu UMEMOTO addrconfig(struct addrinfo *pai) 1447a42af91cSHajimu UMEMOTO { 1448bf838688SHajimu UMEMOTO int s, af; 1449a42af91cSHajimu UMEMOTO 1450bf838688SHajimu UMEMOTO /* 1451bf838688SHajimu UMEMOTO * TODO: 1452bf838688SHajimu UMEMOTO * Note that implementation dependent test for address 1453bf838688SHajimu UMEMOTO * configuration should be done everytime called 1454bf838688SHajimu UMEMOTO * (or apropriate interval), 1455bf838688SHajimu UMEMOTO * because addresses will be dynamically assigned or deleted. 1456bf838688SHajimu UMEMOTO */ 1457bf838688SHajimu UMEMOTO af = pai->ai_family; 1458bf838688SHajimu UMEMOTO if (af == AF_UNSPEC) { 1459bf838688SHajimu UMEMOTO if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1460bf838688SHajimu UMEMOTO af = AF_INET; 1461bf838688SHajimu UMEMOTO else { 1462bf838688SHajimu UMEMOTO _close(s); 1463bf838688SHajimu UMEMOTO if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1464bf838688SHajimu UMEMOTO af = AF_INET6; 1465bf838688SHajimu UMEMOTO else 1466bf838688SHajimu UMEMOTO _close(s); 1467bf838688SHajimu UMEMOTO } 1468bf838688SHajimu UMEMOTO } 1469bf838688SHajimu UMEMOTO if (af != AF_UNSPEC) { 1470bf838688SHajimu UMEMOTO if ((s = _socket(af, SOCK_DGRAM, 0)) < 0) 1471a42af91cSHajimu UMEMOTO return 0; 1472bf838688SHajimu UMEMOTO _close(s); 1473bf838688SHajimu UMEMOTO } 1474bf838688SHajimu UMEMOTO pai->ai_family = af; 1475a42af91cSHajimu UMEMOTO return 1; 1476a42af91cSHajimu UMEMOTO } 1477a42af91cSHajimu UMEMOTO 1478a42af91cSHajimu UMEMOTO #ifdef INET6 1479a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */ 1480a42af91cSHajimu UMEMOTO static int 148190d1d883SHajimu UMEMOTO ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid) 1482a42af91cSHajimu UMEMOTO { 1483a89c1d30SHajimu UMEMOTO u_long lscopeid; 1484bf838688SHajimu UMEMOTO struct in6_addr *a6; 1485a42af91cSHajimu UMEMOTO char *ep; 1486a42af91cSHajimu UMEMOTO 1487bf838688SHajimu UMEMOTO a6 = &sin6->sin6_addr; 1488bf838688SHajimu UMEMOTO 1489ec20fe00SJun-ichiro itojun Hagino /* empty scopeid portion is invalid */ 1490ec20fe00SJun-ichiro itojun Hagino if (*scope == '\0') 1491ec20fe00SJun-ichiro itojun Hagino return -1; 1492ec20fe00SJun-ichiro itojun Hagino 1493bf838688SHajimu UMEMOTO if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { 1494a42af91cSHajimu UMEMOTO /* 1495a42af91cSHajimu UMEMOTO * We currently assume a one-to-one mapping between links 1496a42af91cSHajimu UMEMOTO * and interfaces, so we simply use interface indices for 1497a42af91cSHajimu UMEMOTO * like-local scopes. 1498a42af91cSHajimu UMEMOTO */ 1499a89c1d30SHajimu UMEMOTO *scopeid = if_nametoindex(scope); 1500a89c1d30SHajimu UMEMOTO if (*scopeid == 0) 1501a42af91cSHajimu UMEMOTO goto trynumeric; 1502a89c1d30SHajimu UMEMOTO return 0; 1503a42af91cSHajimu UMEMOTO } 1504a42af91cSHajimu UMEMOTO 1505a42af91cSHajimu UMEMOTO /* still unclear about literal, allow numeric only - placeholder */ 1506a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) 1507a42af91cSHajimu UMEMOTO goto trynumeric; 1508a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) 1509a42af91cSHajimu UMEMOTO goto trynumeric; 1510a42af91cSHajimu UMEMOTO else 1511a42af91cSHajimu UMEMOTO goto trynumeric; /* global */ 1512a42af91cSHajimu UMEMOTO 1513a42af91cSHajimu UMEMOTO /* try to convert to a numeric id as a last resort */ 1514a42af91cSHajimu UMEMOTO trynumeric: 1515a89c1d30SHajimu UMEMOTO errno = 0; 1516a89c1d30SHajimu UMEMOTO lscopeid = strtoul(scope, &ep, 10); 1517a89c1d30SHajimu UMEMOTO *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); 1518a89c1d30SHajimu UMEMOTO if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) 1519a89c1d30SHajimu UMEMOTO return 0; 1520a42af91cSHajimu UMEMOTO else 1521a42af91cSHajimu UMEMOTO return -1; 1522a42af91cSHajimu UMEMOTO } 1523a42af91cSHajimu UMEMOTO #endif 1524a42af91cSHajimu UMEMOTO 152506a99fe3SHajimu UMEMOTO 152606a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 152706a99fe3SHajimu UMEMOTO static int 152806a99fe3SHajimu UMEMOTO addrinfo_id_func(char *buffer, size_t *buffer_size, va_list ap, 152906a99fe3SHajimu UMEMOTO void *cache_mdata) 153006a99fe3SHajimu UMEMOTO { 153106a99fe3SHajimu UMEMOTO res_state statp; 153206a99fe3SHajimu UMEMOTO u_long res_options; 153306a99fe3SHajimu UMEMOTO 153406a99fe3SHajimu UMEMOTO const int op_id = 0; /* identifies the getaddrinfo for the cache */ 153506a99fe3SHajimu UMEMOTO char *hostname; 153606a99fe3SHajimu UMEMOTO struct addrinfo *hints; 153706a99fe3SHajimu UMEMOTO 153806a99fe3SHajimu UMEMOTO char *p; 153906a99fe3SHajimu UMEMOTO int ai_flags, ai_family, ai_socktype, ai_protocol; 154006a99fe3SHajimu UMEMOTO size_t desired_size, size; 154106a99fe3SHajimu UMEMOTO 154206a99fe3SHajimu UMEMOTO statp = __res_state(); 154306a99fe3SHajimu UMEMOTO res_options = statp->options & (RES_RECURSE | RES_DEFNAMES | 154406a99fe3SHajimu UMEMOTO RES_DNSRCH | RES_NOALIASES | RES_USE_INET6); 154506a99fe3SHajimu UMEMOTO 154606a99fe3SHajimu UMEMOTO hostname = va_arg(ap, char *); 154706a99fe3SHajimu UMEMOTO hints = va_arg(ap, struct addrinfo *); 154806a99fe3SHajimu UMEMOTO 154906a99fe3SHajimu UMEMOTO desired_size = sizeof(res_options) + sizeof(int) + sizeof(int) * 4; 155006a99fe3SHajimu UMEMOTO if (hostname != NULL) { 155106a99fe3SHajimu UMEMOTO size = strlen(hostname); 155206a99fe3SHajimu UMEMOTO desired_size += size + 1; 155306a99fe3SHajimu UMEMOTO } else 155406a99fe3SHajimu UMEMOTO size = 0; 155506a99fe3SHajimu UMEMOTO 155606a99fe3SHajimu UMEMOTO if (desired_size > *buffer_size) { 155706a99fe3SHajimu UMEMOTO *buffer_size = desired_size; 155806a99fe3SHajimu UMEMOTO return (NS_RETURN); 155906a99fe3SHajimu UMEMOTO } 156006a99fe3SHajimu UMEMOTO 156106a99fe3SHajimu UMEMOTO if (hints == NULL) 156206a99fe3SHajimu UMEMOTO ai_flags = ai_family = ai_socktype = ai_protocol = 0; 156306a99fe3SHajimu UMEMOTO else { 156406a99fe3SHajimu UMEMOTO ai_flags = hints->ai_flags; 156506a99fe3SHajimu UMEMOTO ai_family = hints->ai_family; 156606a99fe3SHajimu UMEMOTO ai_socktype = hints->ai_socktype; 156706a99fe3SHajimu UMEMOTO ai_protocol = hints->ai_protocol; 156806a99fe3SHajimu UMEMOTO } 156906a99fe3SHajimu UMEMOTO 157006a99fe3SHajimu UMEMOTO p = buffer; 157106a99fe3SHajimu UMEMOTO memcpy(p, &res_options, sizeof(res_options)); 157206a99fe3SHajimu UMEMOTO p += sizeof(res_options); 157306a99fe3SHajimu UMEMOTO 157406a99fe3SHajimu UMEMOTO memcpy(p, &op_id, sizeof(int)); 157506a99fe3SHajimu UMEMOTO p += sizeof(int); 157606a99fe3SHajimu UMEMOTO 157706a99fe3SHajimu UMEMOTO memcpy(p, &ai_flags, sizeof(int)); 157806a99fe3SHajimu UMEMOTO p += sizeof(int); 157906a99fe3SHajimu UMEMOTO 158006a99fe3SHajimu UMEMOTO memcpy(p, &ai_family, sizeof(int)); 158106a99fe3SHajimu UMEMOTO p += sizeof(int); 158206a99fe3SHajimu UMEMOTO 158306a99fe3SHajimu UMEMOTO memcpy(p, &ai_socktype, sizeof(int)); 158406a99fe3SHajimu UMEMOTO p += sizeof(int); 158506a99fe3SHajimu UMEMOTO 158606a99fe3SHajimu UMEMOTO memcpy(p, &ai_protocol, sizeof(int)); 158706a99fe3SHajimu UMEMOTO p += sizeof(int); 158806a99fe3SHajimu UMEMOTO 158906a99fe3SHajimu UMEMOTO if (hostname != NULL) 159006a99fe3SHajimu UMEMOTO memcpy(p, hostname, size); 159106a99fe3SHajimu UMEMOTO 159206a99fe3SHajimu UMEMOTO *buffer_size = desired_size; 159306a99fe3SHajimu UMEMOTO return (NS_SUCCESS); 159406a99fe3SHajimu UMEMOTO } 159506a99fe3SHajimu UMEMOTO 159606a99fe3SHajimu UMEMOTO static int 159706a99fe3SHajimu UMEMOTO addrinfo_marshal_func(char *buffer, size_t *buffer_size, void *retval, 159806a99fe3SHajimu UMEMOTO va_list ap, void *cache_mdata) 159906a99fe3SHajimu UMEMOTO { 160006a99fe3SHajimu UMEMOTO struct addrinfo *ai, *cai; 160106a99fe3SHajimu UMEMOTO char *p; 160206a99fe3SHajimu UMEMOTO size_t desired_size, size, ai_size; 160306a99fe3SHajimu UMEMOTO 160406a99fe3SHajimu UMEMOTO ai = *((struct addrinfo **)retval); 160506a99fe3SHajimu UMEMOTO 160606a99fe3SHajimu UMEMOTO desired_size = sizeof(size_t); 160706a99fe3SHajimu UMEMOTO ai_size = 0; 160806a99fe3SHajimu UMEMOTO for (cai = ai; cai != NULL; cai = cai->ai_next) { 160906a99fe3SHajimu UMEMOTO desired_size += sizeof(struct addrinfo) + cai->ai_addrlen; 161006a99fe3SHajimu UMEMOTO if (cai->ai_canonname != NULL) 161106a99fe3SHajimu UMEMOTO desired_size += sizeof(size_t) + 161206a99fe3SHajimu UMEMOTO strlen(cai->ai_canonname); 161306a99fe3SHajimu UMEMOTO ++ai_size; 161406a99fe3SHajimu UMEMOTO } 161506a99fe3SHajimu UMEMOTO 161606a99fe3SHajimu UMEMOTO if (desired_size > *buffer_size) { 161706a99fe3SHajimu UMEMOTO /* this assignment is here for future use */ 161806a99fe3SHajimu UMEMOTO errno = ERANGE; 161906a99fe3SHajimu UMEMOTO *buffer_size = desired_size; 162006a99fe3SHajimu UMEMOTO return (NS_RETURN); 162106a99fe3SHajimu UMEMOTO } 162206a99fe3SHajimu UMEMOTO 162306a99fe3SHajimu UMEMOTO memset(buffer, 0, desired_size); 162406a99fe3SHajimu UMEMOTO p = buffer; 162506a99fe3SHajimu UMEMOTO 162606a99fe3SHajimu UMEMOTO memcpy(p, &ai_size, sizeof(size_t)); 162706a99fe3SHajimu UMEMOTO p += sizeof(size_t); 162806a99fe3SHajimu UMEMOTO for (cai = ai; cai != NULL; cai = cai->ai_next) { 162906a99fe3SHajimu UMEMOTO memcpy(p, cai, sizeof(struct addrinfo)); 163006a99fe3SHajimu UMEMOTO p += sizeof(struct addrinfo); 163106a99fe3SHajimu UMEMOTO 163206a99fe3SHajimu UMEMOTO memcpy(p, cai->ai_addr, cai->ai_addrlen); 163306a99fe3SHajimu UMEMOTO p += cai->ai_addrlen; 163406a99fe3SHajimu UMEMOTO 163506a99fe3SHajimu UMEMOTO if (cai->ai_canonname != NULL) { 163606a99fe3SHajimu UMEMOTO size = strlen(cai->ai_canonname); 163706a99fe3SHajimu UMEMOTO memcpy(p, &size, sizeof(size_t)); 163806a99fe3SHajimu UMEMOTO p += sizeof(size_t); 163906a99fe3SHajimu UMEMOTO 164006a99fe3SHajimu UMEMOTO memcpy(p, cai->ai_canonname, size); 164106a99fe3SHajimu UMEMOTO p += size; 164206a99fe3SHajimu UMEMOTO } 164306a99fe3SHajimu UMEMOTO } 164406a99fe3SHajimu UMEMOTO 164506a99fe3SHajimu UMEMOTO return (NS_SUCCESS); 164606a99fe3SHajimu UMEMOTO } 164706a99fe3SHajimu UMEMOTO 164806a99fe3SHajimu UMEMOTO static int 164906a99fe3SHajimu UMEMOTO addrinfo_unmarshal_func(char *buffer, size_t buffer_size, void *retval, 165006a99fe3SHajimu UMEMOTO va_list ap, void *cache_mdata) 165106a99fe3SHajimu UMEMOTO { 165206a99fe3SHajimu UMEMOTO struct addrinfo new_ai, *result, *sentinel, *lasts; 165306a99fe3SHajimu UMEMOTO 165406a99fe3SHajimu UMEMOTO char *p; 165506a99fe3SHajimu UMEMOTO size_t ai_size, ai_i, size; 165606a99fe3SHajimu UMEMOTO 165706a99fe3SHajimu UMEMOTO p = buffer; 165806a99fe3SHajimu UMEMOTO memcpy(&ai_size, p, sizeof(size_t)); 165906a99fe3SHajimu UMEMOTO p += sizeof(size_t); 166006a99fe3SHajimu UMEMOTO 166106a99fe3SHajimu UMEMOTO result = NULL; 166206a99fe3SHajimu UMEMOTO lasts = NULL; 166306a99fe3SHajimu UMEMOTO for (ai_i = 0; ai_i < ai_size; ++ai_i) { 166406a99fe3SHajimu UMEMOTO memcpy(&new_ai, p, sizeof(struct addrinfo)); 166506a99fe3SHajimu UMEMOTO p += sizeof(struct addrinfo); 166606a99fe3SHajimu UMEMOTO size = new_ai.ai_addrlen + sizeof(struct addrinfo) + 166706a99fe3SHajimu UMEMOTO _ALIGNBYTES; 166806a99fe3SHajimu UMEMOTO 166906a99fe3SHajimu UMEMOTO sentinel = (struct addrinfo *)malloc(size); 167006a99fe3SHajimu UMEMOTO memset(sentinel, 0, size); 167106a99fe3SHajimu UMEMOTO 167206a99fe3SHajimu UMEMOTO memcpy(sentinel, &new_ai, sizeof(struct addrinfo)); 167306a99fe3SHajimu UMEMOTO sentinel->ai_addr = (struct sockaddr *)_ALIGN((char *)sentinel + 167406a99fe3SHajimu UMEMOTO sizeof(struct addrinfo)); 167506a99fe3SHajimu UMEMOTO 167606a99fe3SHajimu UMEMOTO memcpy(sentinel->ai_addr, p, new_ai.ai_addrlen); 167706a99fe3SHajimu UMEMOTO p += new_ai.ai_addrlen; 167806a99fe3SHajimu UMEMOTO 167906a99fe3SHajimu UMEMOTO if (new_ai.ai_canonname != NULL) { 168006a99fe3SHajimu UMEMOTO memcpy(&size, p, sizeof(size_t)); 168106a99fe3SHajimu UMEMOTO p += sizeof(size_t); 168206a99fe3SHajimu UMEMOTO 168306a99fe3SHajimu UMEMOTO sentinel->ai_canonname = (char *)malloc(size + 1); 168406a99fe3SHajimu UMEMOTO memset(sentinel->ai_canonname, 0, size + 1); 168506a99fe3SHajimu UMEMOTO 168606a99fe3SHajimu UMEMOTO memcpy(sentinel->ai_canonname, p, size); 168706a99fe3SHajimu UMEMOTO p += size; 168806a99fe3SHajimu UMEMOTO } 168906a99fe3SHajimu UMEMOTO 169006a99fe3SHajimu UMEMOTO if (result == NULL) { 169106a99fe3SHajimu UMEMOTO result = sentinel; 169206a99fe3SHajimu UMEMOTO lasts = sentinel; 169306a99fe3SHajimu UMEMOTO } else { 169406a99fe3SHajimu UMEMOTO lasts->ai_next = sentinel; 169506a99fe3SHajimu UMEMOTO lasts = sentinel; 169606a99fe3SHajimu UMEMOTO } 169706a99fe3SHajimu UMEMOTO } 169806a99fe3SHajimu UMEMOTO 169906a99fe3SHajimu UMEMOTO *((struct addrinfo **)retval) = result; 170006a99fe3SHajimu UMEMOTO return (NS_SUCCESS); 170106a99fe3SHajimu UMEMOTO } 170206a99fe3SHajimu UMEMOTO #endif /* NS_CACHING */ 170306a99fe3SHajimu UMEMOTO 1704688a5c3eSHajimu UMEMOTO /* 1705688a5c3eSHajimu UMEMOTO * FQDN hostname, DNS lookup 1706688a5c3eSHajimu UMEMOTO */ 1707688a5c3eSHajimu UMEMOTO static int 170890d1d883SHajimu UMEMOTO explore_fqdn(const struct addrinfo *pai, const char *hostname, 170990d1d883SHajimu UMEMOTO const char *servname, struct addrinfo **res) 1710688a5c3eSHajimu UMEMOTO { 1711688a5c3eSHajimu UMEMOTO struct addrinfo *result; 1712688a5c3eSHajimu UMEMOTO struct addrinfo *cur; 1713688a5c3eSHajimu UMEMOTO int error = 0; 171406a99fe3SHajimu UMEMOTO 171506a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 171606a99fe3SHajimu UMEMOTO static const nss_cache_info cache_info = 171706a99fe3SHajimu UMEMOTO NS_COMMON_CACHE_INFO_INITIALIZER( 171806a99fe3SHajimu UMEMOTO hosts, NULL, addrinfo_id_func, addrinfo_marshal_func, 171906a99fe3SHajimu UMEMOTO addrinfo_unmarshal_func); 172006a99fe3SHajimu UMEMOTO #endif 1721688a5c3eSHajimu UMEMOTO static const ns_dtab dtab[] = { 1722688a5c3eSHajimu UMEMOTO NS_FILES_CB(_files_getaddrinfo, NULL) 1723688a5c3eSHajimu UMEMOTO { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ 1724688a5c3eSHajimu UMEMOTO NS_NIS_CB(_yp_getaddrinfo, NULL) 172506a99fe3SHajimu UMEMOTO #ifdef NS_CACHING 172606a99fe3SHajimu UMEMOTO NS_CACHE_CB(&cache_info) 172706a99fe3SHajimu UMEMOTO #endif 1728688a5c3eSHajimu UMEMOTO { 0 } 172969b0a4b6SJim Pirzyk }; 173069b0a4b6SJim Pirzyk 1731688a5c3eSHajimu UMEMOTO result = NULL; 173269b0a4b6SJim Pirzyk 1733688a5c3eSHajimu UMEMOTO /* 1734688a5c3eSHajimu UMEMOTO * if the servname does not match socktype/protocol, ignore it. 1735688a5c3eSHajimu UMEMOTO */ 173633dee819SBrian Feldman if (get_portmatch(pai, servname) != 0) 173769b0a4b6SJim Pirzyk return 0; 173869b0a4b6SJim Pirzyk 1739688a5c3eSHajimu UMEMOTO switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", 1740688a5c3eSHajimu UMEMOTO default_dns_files, hostname, pai)) { 1741688a5c3eSHajimu UMEMOTO case NS_TRYAGAIN: 1742688a5c3eSHajimu UMEMOTO error = EAI_AGAIN; 1743688a5c3eSHajimu UMEMOTO goto free; 1744688a5c3eSHajimu UMEMOTO case NS_UNAVAIL: 1745688a5c3eSHajimu UMEMOTO error = EAI_FAIL; 1746688a5c3eSHajimu UMEMOTO goto free; 1747688a5c3eSHajimu UMEMOTO case NS_NOTFOUND: 1748688a5c3eSHajimu UMEMOTO error = EAI_NONAME; 1749688a5c3eSHajimu UMEMOTO goto free; 1750688a5c3eSHajimu UMEMOTO case NS_SUCCESS: 1751688a5c3eSHajimu UMEMOTO error = 0; 1752688a5c3eSHajimu UMEMOTO for (cur = result; cur; cur = cur->ai_next) { 1753688a5c3eSHajimu UMEMOTO GET_PORT(cur, servname); 1754688a5c3eSHajimu UMEMOTO /* canonname should be filled already */ 1755688a5c3eSHajimu UMEMOTO } 175669b0a4b6SJim Pirzyk break; 175769b0a4b6SJim Pirzyk } 175869b0a4b6SJim Pirzyk 1759688a5c3eSHajimu UMEMOTO *res = result; 1760688a5c3eSHajimu UMEMOTO 176169b0a4b6SJim Pirzyk return 0; 1762688a5c3eSHajimu UMEMOTO 1763688a5c3eSHajimu UMEMOTO free: 1764688a5c3eSHajimu UMEMOTO if (result) 1765688a5c3eSHajimu UMEMOTO freeaddrinfo(result); 1766688a5c3eSHajimu UMEMOTO return error; 176769b0a4b6SJim Pirzyk } 176869b0a4b6SJim Pirzyk 1769a42af91cSHajimu UMEMOTO #ifdef DEBUG 1770a42af91cSHajimu UMEMOTO static const char AskedForGot[] = 1771a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 1772a42af91cSHajimu UMEMOTO #endif 1773a42af91cSHajimu UMEMOTO 1774a42af91cSHajimu UMEMOTO static struct addrinfo * 177590d1d883SHajimu UMEMOTO getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, 177690d1d883SHajimu UMEMOTO const struct addrinfo *pai, res_state res) 1777a42af91cSHajimu UMEMOTO { 1778a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 1779a42af91cSHajimu UMEMOTO struct addrinfo ai; 1780a42af91cSHajimu UMEMOTO const struct afd *afd; 1781a42af91cSHajimu UMEMOTO char *canonname; 1782a42af91cSHajimu UMEMOTO const HEADER *hp; 1783a42af91cSHajimu UMEMOTO const u_char *cp; 1784a42af91cSHajimu UMEMOTO int n; 1785a42af91cSHajimu UMEMOTO const u_char *eom; 1786dd521ef1SHajimu UMEMOTO char *bp, *ep; 1787dd521ef1SHajimu UMEMOTO int type, class, ancount, qdcount; 1788a42af91cSHajimu UMEMOTO int haveanswer, had_error; 1789a42af91cSHajimu UMEMOTO char tbuf[MAXDNAME]; 1790c05ac53bSDavid E. O'Brien int (*name_ok)(const char *); 1791a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 1792a42af91cSHajimu UMEMOTO 1793a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 1794a42af91cSHajimu UMEMOTO cur = &sentinel; 1795a42af91cSHajimu UMEMOTO 1796a42af91cSHajimu UMEMOTO canonname = NULL; 1797a42af91cSHajimu UMEMOTO eom = answer->buf + anslen; 1798a42af91cSHajimu UMEMOTO switch (qtype) { 1799a42af91cSHajimu UMEMOTO case T_A: 1800a42af91cSHajimu UMEMOTO case T_AAAA: 1801a42af91cSHajimu UMEMOTO case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ 1802a42af91cSHajimu UMEMOTO name_ok = res_hnok; 1803a42af91cSHajimu UMEMOTO break; 1804a42af91cSHajimu UMEMOTO default: 1805a42af91cSHajimu UMEMOTO return (NULL); /* XXX should be abort(); */ 1806a42af91cSHajimu UMEMOTO } 1807a42af91cSHajimu UMEMOTO /* 1808a42af91cSHajimu UMEMOTO * find first satisfactory answer 1809a42af91cSHajimu UMEMOTO */ 1810a42af91cSHajimu UMEMOTO hp = &answer->hdr; 1811a42af91cSHajimu UMEMOTO ancount = ntohs(hp->ancount); 1812a42af91cSHajimu UMEMOTO qdcount = ntohs(hp->qdcount); 1813a42af91cSHajimu UMEMOTO bp = hostbuf; 1814dd521ef1SHajimu UMEMOTO ep = hostbuf + sizeof hostbuf; 1815a42af91cSHajimu UMEMOTO cp = answer->buf + HFIXEDSZ; 1816a42af91cSHajimu UMEMOTO if (qdcount != 1) { 18175342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 1818a42af91cSHajimu UMEMOTO return (NULL); 1819a42af91cSHajimu UMEMOTO } 1820dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1821a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 18225342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 1823a42af91cSHajimu UMEMOTO return (NULL); 1824a42af91cSHajimu UMEMOTO } 1825a42af91cSHajimu UMEMOTO cp += n + QFIXEDSZ; 1826a42af91cSHajimu UMEMOTO if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 1827a42af91cSHajimu UMEMOTO /* res_send() has already verified that the query name is the 1828a42af91cSHajimu UMEMOTO * same as the one we sent; this just gets the expanded name 1829a42af91cSHajimu UMEMOTO * (i.e., with the succeeding search-domain tacked on). 1830a42af91cSHajimu UMEMOTO */ 1831a42af91cSHajimu UMEMOTO n = strlen(bp) + 1; /* for the \0 */ 1832a42af91cSHajimu UMEMOTO if (n >= MAXHOSTNAMELEN) { 18335342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 1834a42af91cSHajimu UMEMOTO return (NULL); 1835a42af91cSHajimu UMEMOTO } 1836a42af91cSHajimu UMEMOTO canonname = bp; 1837a42af91cSHajimu UMEMOTO bp += n; 1838a42af91cSHajimu UMEMOTO /* The qname can be abbreviated, but h_name is now absolute. */ 1839a42af91cSHajimu UMEMOTO qname = canonname; 1840a42af91cSHajimu UMEMOTO } 1841a42af91cSHajimu UMEMOTO haveanswer = 0; 1842a42af91cSHajimu UMEMOTO had_error = 0; 1843a42af91cSHajimu UMEMOTO while (ancount-- > 0 && cp < eom && !had_error) { 1844dd521ef1SHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1845a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(bp)) { 1846a42af91cSHajimu UMEMOTO had_error++; 1847a42af91cSHajimu UMEMOTO continue; 1848a42af91cSHajimu UMEMOTO } 1849a42af91cSHajimu UMEMOTO cp += n; /* name */ 1850a42af91cSHajimu UMEMOTO type = _getshort(cp); 1851a42af91cSHajimu UMEMOTO cp += INT16SZ; /* type */ 1852a42af91cSHajimu UMEMOTO class = _getshort(cp); 1853a42af91cSHajimu UMEMOTO cp += INT16SZ + INT32SZ; /* class, TTL */ 1854a42af91cSHajimu UMEMOTO n = _getshort(cp); 1855a42af91cSHajimu UMEMOTO cp += INT16SZ; /* len */ 1856a42af91cSHajimu UMEMOTO if (class != C_IN) { 1857a42af91cSHajimu UMEMOTO /* XXX - debug? syslog? */ 1858a42af91cSHajimu UMEMOTO cp += n; 1859a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1860a42af91cSHajimu UMEMOTO } 1861a42af91cSHajimu UMEMOTO if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 1862a42af91cSHajimu UMEMOTO type == T_CNAME) { 1863a42af91cSHajimu UMEMOTO n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1864a42af91cSHajimu UMEMOTO if ((n < 0) || !(*name_ok)(tbuf)) { 1865a42af91cSHajimu UMEMOTO had_error++; 1866a42af91cSHajimu UMEMOTO continue; 1867a42af91cSHajimu UMEMOTO } 1868a42af91cSHajimu UMEMOTO cp += n; 1869a42af91cSHajimu UMEMOTO /* Get canonical name. */ 1870a42af91cSHajimu UMEMOTO n = strlen(tbuf) + 1; /* for the \0 */ 1871dd521ef1SHajimu UMEMOTO if (n > ep - bp || n >= MAXHOSTNAMELEN) { 1872a42af91cSHajimu UMEMOTO had_error++; 1873a42af91cSHajimu UMEMOTO continue; 1874a42af91cSHajimu UMEMOTO } 1875d0509082SJacques Vidrine strlcpy(bp, tbuf, ep - bp); 1876a42af91cSHajimu UMEMOTO canonname = bp; 1877a42af91cSHajimu UMEMOTO bp += n; 1878a42af91cSHajimu UMEMOTO continue; 1879a42af91cSHajimu UMEMOTO } 1880a42af91cSHajimu UMEMOTO if (qtype == T_ANY) { 1881a42af91cSHajimu UMEMOTO if (!(type == T_A || type == T_AAAA)) { 1882a42af91cSHajimu UMEMOTO cp += n; 1883a42af91cSHajimu UMEMOTO continue; 1884a42af91cSHajimu UMEMOTO } 1885a42af91cSHajimu UMEMOTO } else if (type != qtype) { 1886a42af91cSHajimu UMEMOTO #ifdef DEBUG 1887a42af91cSHajimu UMEMOTO if (type != T_KEY && type != T_SIG) 1888a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 1889a42af91cSHajimu UMEMOTO "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 1890a42af91cSHajimu UMEMOTO qname, p_class(C_IN), p_type(qtype), 1891a42af91cSHajimu UMEMOTO p_type(type)); 1892a42af91cSHajimu UMEMOTO #endif 1893a42af91cSHajimu UMEMOTO cp += n; 1894a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1895a42af91cSHajimu UMEMOTO } 1896a42af91cSHajimu UMEMOTO switch (type) { 1897a42af91cSHajimu UMEMOTO case T_A: 1898a42af91cSHajimu UMEMOTO case T_AAAA: 1899a42af91cSHajimu UMEMOTO if (strcasecmp(canonname, bp) != 0) { 1900a42af91cSHajimu UMEMOTO #ifdef DEBUG 1901a42af91cSHajimu UMEMOTO syslog(LOG_NOTICE|LOG_AUTH, 1902a42af91cSHajimu UMEMOTO AskedForGot, canonname, bp); 1903a42af91cSHajimu UMEMOTO #endif 1904a42af91cSHajimu UMEMOTO cp += n; 1905a42af91cSHajimu UMEMOTO continue; /* XXX - had_error++ ? */ 1906a42af91cSHajimu UMEMOTO } 1907a42af91cSHajimu UMEMOTO if (type == T_A && n != INADDRSZ) { 1908a42af91cSHajimu UMEMOTO cp += n; 1909a42af91cSHajimu UMEMOTO continue; 1910a42af91cSHajimu UMEMOTO } 1911a42af91cSHajimu UMEMOTO if (type == T_AAAA && n != IN6ADDRSZ) { 1912a42af91cSHajimu UMEMOTO cp += n; 1913a42af91cSHajimu UMEMOTO continue; 1914a42af91cSHajimu UMEMOTO } 1915a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 1916a42af91cSHajimu UMEMOTO if (type == T_AAAA) { 1917a42af91cSHajimu UMEMOTO struct in6_addr in6; 1918a42af91cSHajimu UMEMOTO memcpy(&in6, cp, sizeof(in6)); 1919a42af91cSHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&in6)) { 1920a42af91cSHajimu UMEMOTO cp += n; 1921a42af91cSHajimu UMEMOTO continue; 1922a42af91cSHajimu UMEMOTO } 1923a42af91cSHajimu UMEMOTO } 1924a42af91cSHajimu UMEMOTO #endif 1925a42af91cSHajimu UMEMOTO if (!haveanswer) { 1926a42af91cSHajimu UMEMOTO int nn; 1927a42af91cSHajimu UMEMOTO 1928a42af91cSHajimu UMEMOTO canonname = bp; 1929a42af91cSHajimu UMEMOTO nn = strlen(bp) + 1; /* for the \0 */ 1930a42af91cSHajimu UMEMOTO bp += nn; 1931a42af91cSHajimu UMEMOTO } 1932a42af91cSHajimu UMEMOTO 1933a42af91cSHajimu UMEMOTO /* don't overwrite pai */ 1934a42af91cSHajimu UMEMOTO ai = *pai; 1935a42af91cSHajimu UMEMOTO ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; 1936a42af91cSHajimu UMEMOTO afd = find_afd(ai.ai_family); 1937a42af91cSHajimu UMEMOTO if (afd == NULL) { 1938a42af91cSHajimu UMEMOTO cp += n; 1939a42af91cSHajimu UMEMOTO continue; 1940a42af91cSHajimu UMEMOTO } 1941a42af91cSHajimu UMEMOTO cur->ai_next = get_ai(&ai, afd, (const char *)cp); 1942a42af91cSHajimu UMEMOTO if (cur->ai_next == NULL) 1943a42af91cSHajimu UMEMOTO had_error++; 1944a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 1945a42af91cSHajimu UMEMOTO cur = cur->ai_next; 1946a42af91cSHajimu UMEMOTO cp += n; 1947a42af91cSHajimu UMEMOTO break; 1948a42af91cSHajimu UMEMOTO default: 1949a42af91cSHajimu UMEMOTO abort(); 1950a42af91cSHajimu UMEMOTO } 1951a42af91cSHajimu UMEMOTO if (!had_error) 1952a42af91cSHajimu UMEMOTO haveanswer++; 1953a42af91cSHajimu UMEMOTO } 1954a42af91cSHajimu UMEMOTO if (haveanswer) { 195569b0a4b6SJim Pirzyk #if defined(RESOLVSORT) 195669b0a4b6SJim Pirzyk /* 195769b0a4b6SJim Pirzyk * We support only IPv4 address for backward 195869b0a4b6SJim Pirzyk * compatibility against gethostbyname(3). 195969b0a4b6SJim Pirzyk */ 19605342d17fSHajimu UMEMOTO if (res->nsort && qtype == T_A) { 19615342d17fSHajimu UMEMOTO if (addr4sort(&sentinel, res) < 0) { 196269b0a4b6SJim Pirzyk freeaddrinfo(sentinel.ai_next); 19635342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 196469b0a4b6SJim Pirzyk return NULL; 196569b0a4b6SJim Pirzyk } 196669b0a4b6SJim Pirzyk } 196769b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/ 1968a42af91cSHajimu UMEMOTO if (!canonname) 1969a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, qname); 1970a42af91cSHajimu UMEMOTO else 1971a42af91cSHajimu UMEMOTO (void)get_canonname(pai, sentinel.ai_next, canonname); 19725342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_SUCCESS); 1973a42af91cSHajimu UMEMOTO return sentinel.ai_next; 1974a42af91cSHajimu UMEMOTO } 1975a42af91cSHajimu UMEMOTO 19765342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 1977a42af91cSHajimu UMEMOTO return NULL; 1978a42af91cSHajimu UMEMOTO } 1979a42af91cSHajimu UMEMOTO 1980688a5c3eSHajimu UMEMOTO #ifdef RESOLVSORT 1981688a5c3eSHajimu UMEMOTO struct addr_ptr { 1982688a5c3eSHajimu UMEMOTO struct addrinfo *ai; 1983688a5c3eSHajimu UMEMOTO int aval; 1984688a5c3eSHajimu UMEMOTO }; 1985688a5c3eSHajimu UMEMOTO 1986688a5c3eSHajimu UMEMOTO static int 19875342d17fSHajimu UMEMOTO addr4sort(struct addrinfo *sentinel, res_state res) 1988688a5c3eSHajimu UMEMOTO { 1989688a5c3eSHajimu UMEMOTO struct addrinfo *ai; 1990688a5c3eSHajimu UMEMOTO struct addr_ptr *addrs, addr; 1991688a5c3eSHajimu UMEMOTO struct sockaddr_in *sin; 1992688a5c3eSHajimu UMEMOTO int naddrs, i, j; 1993688a5c3eSHajimu UMEMOTO int needsort = 0; 1994688a5c3eSHajimu UMEMOTO 1995688a5c3eSHajimu UMEMOTO if (!sentinel) 1996688a5c3eSHajimu UMEMOTO return -1; 1997688a5c3eSHajimu UMEMOTO naddrs = 0; 1998688a5c3eSHajimu UMEMOTO for (ai = sentinel->ai_next; ai; ai = ai->ai_next) 1999688a5c3eSHajimu UMEMOTO naddrs++; 2000688a5c3eSHajimu UMEMOTO if (naddrs < 2) 2001688a5c3eSHajimu UMEMOTO return 0; /* We don't need sorting. */ 2002688a5c3eSHajimu UMEMOTO if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL) 2003688a5c3eSHajimu UMEMOTO return -1; 2004688a5c3eSHajimu UMEMOTO i = 0; 2005688a5c3eSHajimu UMEMOTO for (ai = sentinel->ai_next; ai; ai = ai->ai_next) { 2006688a5c3eSHajimu UMEMOTO sin = (struct sockaddr_in *)ai->ai_addr; 20075342d17fSHajimu UMEMOTO for (j = 0; (unsigned)j < res->nsort; j++) { 20085342d17fSHajimu UMEMOTO if (res->sort_list[j].addr.s_addr == 20095342d17fSHajimu UMEMOTO (sin->sin_addr.s_addr & res->sort_list[j].mask)) 2010688a5c3eSHajimu UMEMOTO break; 2011688a5c3eSHajimu UMEMOTO } 2012688a5c3eSHajimu UMEMOTO addrs[i].ai = ai; 2013688a5c3eSHajimu UMEMOTO addrs[i].aval = j; 2014688a5c3eSHajimu UMEMOTO if (needsort == 0 && i > 0 && j < addrs[i - 1].aval) 2015688a5c3eSHajimu UMEMOTO needsort = i; 2016688a5c3eSHajimu UMEMOTO i++; 2017688a5c3eSHajimu UMEMOTO } 2018688a5c3eSHajimu UMEMOTO if (!needsort) { 2019688a5c3eSHajimu UMEMOTO free(addrs); 2020688a5c3eSHajimu UMEMOTO return 0; 2021688a5c3eSHajimu UMEMOTO } 2022688a5c3eSHajimu UMEMOTO 2023688a5c3eSHajimu UMEMOTO while (needsort < naddrs) { 2024688a5c3eSHajimu UMEMOTO for (j = needsort - 1; j >= 0; j--) { 2025688a5c3eSHajimu UMEMOTO if (addrs[j].aval > addrs[j+1].aval) { 2026688a5c3eSHajimu UMEMOTO addr = addrs[j]; 2027688a5c3eSHajimu UMEMOTO addrs[j] = addrs[j + 1]; 2028688a5c3eSHajimu UMEMOTO addrs[j + 1] = addr; 2029688a5c3eSHajimu UMEMOTO } else 2030688a5c3eSHajimu UMEMOTO break; 2031688a5c3eSHajimu UMEMOTO } 2032688a5c3eSHajimu UMEMOTO needsort++; 2033688a5c3eSHajimu UMEMOTO } 2034688a5c3eSHajimu UMEMOTO 2035688a5c3eSHajimu UMEMOTO ai = sentinel; 2036688a5c3eSHajimu UMEMOTO for (i = 0; i < naddrs; ++i) { 2037688a5c3eSHajimu UMEMOTO ai->ai_next = addrs[i].ai; 2038688a5c3eSHajimu UMEMOTO ai = ai->ai_next; 2039688a5c3eSHajimu UMEMOTO } 2040688a5c3eSHajimu UMEMOTO ai->ai_next = NULL; 2041688a5c3eSHajimu UMEMOTO free(addrs); 2042688a5c3eSHajimu UMEMOTO return 0; 2043688a5c3eSHajimu UMEMOTO } 2044688a5c3eSHajimu UMEMOTO #endif /*RESOLVSORT*/ 2045688a5c3eSHajimu UMEMOTO 2046a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 2047a42af91cSHajimu UMEMOTO static int 204890d1d883SHajimu UMEMOTO _dns_getaddrinfo(void *rv, void *cb_data, va_list ap) 2049a42af91cSHajimu UMEMOTO { 2050a42af91cSHajimu UMEMOTO struct addrinfo *ai; 20515cd588cbSHajimu UMEMOTO querybuf *buf, *buf2; 2052a214cd1aSHajimu UMEMOTO const char *hostname; 2053248aee62SJacques Vidrine const struct addrinfo *pai; 2054a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 2055a42af91cSHajimu UMEMOTO struct res_target q, q2; 20565342d17fSHajimu UMEMOTO res_state res; 2057a42af91cSHajimu UMEMOTO 2058a214cd1aSHajimu UMEMOTO hostname = va_arg(ap, char *); 2059248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *); 2060248aee62SJacques Vidrine 2061f1fa5b4fSXin LI memset(&q, 0, sizeof(q)); 2062a42af91cSHajimu UMEMOTO memset(&q2, 0, sizeof(q2)); 2063a42af91cSHajimu UMEMOTO memset(&sentinel, 0, sizeof(sentinel)); 2064a42af91cSHajimu UMEMOTO cur = &sentinel; 2065a42af91cSHajimu UMEMOTO 20665cd588cbSHajimu UMEMOTO buf = malloc(sizeof(*buf)); 20675cd588cbSHajimu UMEMOTO if (!buf) { 20685342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 20695cd588cbSHajimu UMEMOTO return NS_NOTFOUND; 20705cd588cbSHajimu UMEMOTO } 20715cd588cbSHajimu UMEMOTO buf2 = malloc(sizeof(*buf2)); 20725cd588cbSHajimu UMEMOTO if (!buf2) { 20735cd588cbSHajimu UMEMOTO free(buf); 20745342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 20755cd588cbSHajimu UMEMOTO return NS_NOTFOUND; 20765cd588cbSHajimu UMEMOTO } 20775cd588cbSHajimu UMEMOTO 2078a42af91cSHajimu UMEMOTO switch (pai->ai_family) { 2079a42af91cSHajimu UMEMOTO case AF_UNSPEC: 2080a214cd1aSHajimu UMEMOTO q.name = hostname; 2081b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 2082d0d6a2c7SHajimu UMEMOTO q.qtype = T_A; 20835cd588cbSHajimu UMEMOTO q.answer = buf->buf; 20845cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 2085a42af91cSHajimu UMEMOTO q.next = &q2; 2086a214cd1aSHajimu UMEMOTO q2.name = hostname; 2087b826397aSJun-ichiro itojun Hagino q2.qclass = C_IN; 2088d0d6a2c7SHajimu UMEMOTO q2.qtype = T_AAAA; 20895cd588cbSHajimu UMEMOTO q2.answer = buf2->buf; 20905cd588cbSHajimu UMEMOTO q2.anslen = sizeof(buf2->buf); 2091a42af91cSHajimu UMEMOTO break; 2092a42af91cSHajimu UMEMOTO case AF_INET: 2093a214cd1aSHajimu UMEMOTO q.name = hostname; 2094b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 2095b826397aSJun-ichiro itojun Hagino q.qtype = T_A; 20965cd588cbSHajimu UMEMOTO q.answer = buf->buf; 20975cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 2098a42af91cSHajimu UMEMOTO break; 2099a42af91cSHajimu UMEMOTO case AF_INET6: 2100a214cd1aSHajimu UMEMOTO q.name = hostname; 2101b826397aSJun-ichiro itojun Hagino q.qclass = C_IN; 2102b826397aSJun-ichiro itojun Hagino q.qtype = T_AAAA; 21035cd588cbSHajimu UMEMOTO q.answer = buf->buf; 21045cd588cbSHajimu UMEMOTO q.anslen = sizeof(buf->buf); 2105a42af91cSHajimu UMEMOTO break; 2106a42af91cSHajimu UMEMOTO default: 21075cd588cbSHajimu UMEMOTO free(buf); 21085cd588cbSHajimu UMEMOTO free(buf2); 2109248aee62SJacques Vidrine return NS_UNAVAIL; 2110a42af91cSHajimu UMEMOTO } 21115342d17fSHajimu UMEMOTO 21125342d17fSHajimu UMEMOTO res = __res_state(); 21135342d17fSHajimu UMEMOTO if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) { 21145342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 21155342d17fSHajimu UMEMOTO free(buf); 21165342d17fSHajimu UMEMOTO free(buf2); 21175342d17fSHajimu UMEMOTO return NS_NOTFOUND; 21185342d17fSHajimu UMEMOTO } 21195342d17fSHajimu UMEMOTO 21205342d17fSHajimu UMEMOTO if (res_searchN(hostname, &q, res) < 0) { 21215cd588cbSHajimu UMEMOTO free(buf); 21225cd588cbSHajimu UMEMOTO free(buf2); 2123248aee62SJacques Vidrine return NS_NOTFOUND; 21245cd588cbSHajimu UMEMOTO } 2125d0d6a2c7SHajimu UMEMOTO /* prefer IPv6 */ 2126d0d6a2c7SHajimu UMEMOTO if (q.next) { 21275342d17fSHajimu UMEMOTO ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res); 2128a42af91cSHajimu UMEMOTO if (ai) { 2129a42af91cSHajimu UMEMOTO cur->ai_next = ai; 2130a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 2131a42af91cSHajimu UMEMOTO cur = cur->ai_next; 2132a42af91cSHajimu UMEMOTO } 2133d0d6a2c7SHajimu UMEMOTO } 21345342d17fSHajimu UMEMOTO ai = getanswer(buf, q.n, q.name, q.qtype, pai, res); 2135a42af91cSHajimu UMEMOTO if (ai) 2136a42af91cSHajimu UMEMOTO cur->ai_next = ai; 21375cd588cbSHajimu UMEMOTO free(buf); 21385cd588cbSHajimu UMEMOTO free(buf2); 2139a42af91cSHajimu UMEMOTO if (sentinel.ai_next == NULL) 21405342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 2141a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 2142248aee62SJacques Vidrine return NS_NOTFOUND; 2143a42af91cSHajimu UMEMOTO case TRY_AGAIN: 2144248aee62SJacques Vidrine return NS_TRYAGAIN; 2145a42af91cSHajimu UMEMOTO default: 2146248aee62SJacques Vidrine return NS_UNAVAIL; 2147a42af91cSHajimu UMEMOTO } 2148248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 2149248aee62SJacques Vidrine return NS_SUCCESS; 2150248aee62SJacques Vidrine } 2151248aee62SJacques Vidrine 2152248aee62SJacques Vidrine static void 21539def31dcSHajimu UMEMOTO _sethtent(FILE **hostf) 2154248aee62SJacques Vidrine { 21559def31dcSHajimu UMEMOTO if (!*hostf) 21569def31dcSHajimu UMEMOTO *hostf = fopen(_PATH_HOSTS, "r"); 2157248aee62SJacques Vidrine else 21589def31dcSHajimu UMEMOTO rewind(*hostf); 2159248aee62SJacques Vidrine } 2160248aee62SJacques Vidrine 2161248aee62SJacques Vidrine static void 21629def31dcSHajimu UMEMOTO _endhtent(FILE **hostf) 2163248aee62SJacques Vidrine { 21649def31dcSHajimu UMEMOTO if (*hostf) { 21659def31dcSHajimu UMEMOTO (void) fclose(*hostf); 21669def31dcSHajimu UMEMOTO *hostf = NULL; 2167248aee62SJacques Vidrine } 2168a42af91cSHajimu UMEMOTO } 2169a42af91cSHajimu UMEMOTO 2170a42af91cSHajimu UMEMOTO static struct addrinfo * 21719def31dcSHajimu UMEMOTO _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai) 2172a42af91cSHajimu UMEMOTO { 2173a42af91cSHajimu UMEMOTO char *p; 2174a42af91cSHajimu UMEMOTO char *cp, *tname, *cname; 2175a42af91cSHajimu UMEMOTO struct addrinfo hints, *res0, *res; 2176a42af91cSHajimu UMEMOTO int error; 2177a42af91cSHajimu UMEMOTO const char *addr; 2178a42af91cSHajimu UMEMOTO char hostbuf[8*1024]; 2179a42af91cSHajimu UMEMOTO 21809def31dcSHajimu UMEMOTO if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r"))) 2181248aee62SJacques Vidrine return (NULL); 2182a42af91cSHajimu UMEMOTO again: 21839def31dcSHajimu UMEMOTO if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) 2184a42af91cSHajimu UMEMOTO return (NULL); 2185a42af91cSHajimu UMEMOTO if (*p == '#') 2186a42af91cSHajimu UMEMOTO goto again; 21877b6cc404SMaxim Sobolev cp = strpbrk(p, "#\n"); 21887b6cc404SMaxim Sobolev if (cp != NULL) 2189a42af91cSHajimu UMEMOTO *cp = '\0'; 2190a42af91cSHajimu UMEMOTO if (!(cp = strpbrk(p, " \t"))) 2191a42af91cSHajimu UMEMOTO goto again; 2192a42af91cSHajimu UMEMOTO *cp++ = '\0'; 2193a42af91cSHajimu UMEMOTO addr = p; 2194a42af91cSHajimu UMEMOTO cname = NULL; 2195a42af91cSHajimu UMEMOTO /* if this is not something we're looking for, skip it. */ 2196a42af91cSHajimu UMEMOTO while (cp && *cp) { 2197a42af91cSHajimu UMEMOTO if (*cp == ' ' || *cp == '\t') { 2198a42af91cSHajimu UMEMOTO cp++; 2199a42af91cSHajimu UMEMOTO continue; 2200a42af91cSHajimu UMEMOTO } 2201a42af91cSHajimu UMEMOTO tname = cp; 2202a42af91cSHajimu UMEMOTO if (cname == NULL) 2203a42af91cSHajimu UMEMOTO cname = cp; 2204a42af91cSHajimu UMEMOTO if ((cp = strpbrk(cp, " \t")) != NULL) 2205a42af91cSHajimu UMEMOTO *cp++ = '\0'; 2206a42af91cSHajimu UMEMOTO if (strcasecmp(name, tname) == 0) 2207a42af91cSHajimu UMEMOTO goto found; 2208a42af91cSHajimu UMEMOTO } 2209a42af91cSHajimu UMEMOTO goto again; 2210a42af91cSHajimu UMEMOTO 2211a42af91cSHajimu UMEMOTO found: 2212dd521ef1SHajimu UMEMOTO /* we should not glob socktype/protocol here */ 2213dd521ef1SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 2214dd521ef1SHajimu UMEMOTO hints.ai_family = pai->ai_family; 2215dd521ef1SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; 2216dd521ef1SHajimu UMEMOTO hints.ai_protocol = 0; 2217a42af91cSHajimu UMEMOTO hints.ai_flags = AI_NUMERICHOST; 2218dd521ef1SHajimu UMEMOTO error = getaddrinfo(addr, "0", &hints, &res0); 2219a42af91cSHajimu UMEMOTO if (error) 2220a42af91cSHajimu UMEMOTO goto again; 2221a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED 2222a42af91cSHajimu UMEMOTO /* XXX should check all items in the chain */ 2223a42af91cSHajimu UMEMOTO if (res0->ai_family == AF_INET6 && 2224a42af91cSHajimu UMEMOTO IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) { 2225a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 2226a42af91cSHajimu UMEMOTO goto again; 2227a42af91cSHajimu UMEMOTO } 2228a42af91cSHajimu UMEMOTO #endif 2229a42af91cSHajimu UMEMOTO for (res = res0; res; res = res->ai_next) { 2230a42af91cSHajimu UMEMOTO /* cover it up */ 2231a42af91cSHajimu UMEMOTO res->ai_flags = pai->ai_flags; 2232dd521ef1SHajimu UMEMOTO res->ai_socktype = pai->ai_socktype; 2233dd521ef1SHajimu UMEMOTO res->ai_protocol = pai->ai_protocol; 2234a42af91cSHajimu UMEMOTO 2235a42af91cSHajimu UMEMOTO if (pai->ai_flags & AI_CANONNAME) { 2236a42af91cSHajimu UMEMOTO if (get_canonname(pai, res, cname) != 0) { 2237a42af91cSHajimu UMEMOTO freeaddrinfo(res0); 2238a42af91cSHajimu UMEMOTO goto again; 2239a42af91cSHajimu UMEMOTO } 2240a42af91cSHajimu UMEMOTO } 2241a42af91cSHajimu UMEMOTO } 2242a42af91cSHajimu UMEMOTO return res0; 2243a42af91cSHajimu UMEMOTO } 2244a42af91cSHajimu UMEMOTO 2245a42af91cSHajimu UMEMOTO /*ARGSUSED*/ 2246a42af91cSHajimu UMEMOTO static int 224790d1d883SHajimu UMEMOTO _files_getaddrinfo(void *rv, void *cb_data, va_list ap) 2248a42af91cSHajimu UMEMOTO { 2249248aee62SJacques Vidrine const char *name; 2250248aee62SJacques Vidrine const struct addrinfo *pai; 2251a42af91cSHajimu UMEMOTO struct addrinfo sentinel, *cur; 2252a42af91cSHajimu UMEMOTO struct addrinfo *p; 22539def31dcSHajimu UMEMOTO FILE *hostf = NULL; 2254a42af91cSHajimu UMEMOTO 2255248aee62SJacques Vidrine name = va_arg(ap, char *); 2256248aee62SJacques Vidrine pai = va_arg(ap, struct addrinfo *); 2257248aee62SJacques Vidrine 2258248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 2259a42af91cSHajimu UMEMOTO cur = &sentinel; 2260a42af91cSHajimu UMEMOTO 22619def31dcSHajimu UMEMOTO _sethtent(&hostf); 22629def31dcSHajimu UMEMOTO while ((p = _gethtent(&hostf, name, pai)) != NULL) { 2263a42af91cSHajimu UMEMOTO cur->ai_next = p; 2264a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 2265a42af91cSHajimu UMEMOTO cur = cur->ai_next; 2266a42af91cSHajimu UMEMOTO } 22679def31dcSHajimu UMEMOTO _endhtent(&hostf); 2268a42af91cSHajimu UMEMOTO 2269248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 2270248aee62SJacques Vidrine if (sentinel.ai_next == NULL) 2271248aee62SJacques Vidrine return NS_NOTFOUND; 2272248aee62SJacques Vidrine return NS_SUCCESS; 2273a42af91cSHajimu UMEMOTO } 2274a42af91cSHajimu UMEMOTO 2275a42af91cSHajimu UMEMOTO #ifdef YP 2276248aee62SJacques Vidrine /*ARGSUSED*/ 2277248aee62SJacques Vidrine static struct addrinfo * 227890d1d883SHajimu UMEMOTO _yphostent(char *line, const struct addrinfo *pai) 2279248aee62SJacques Vidrine { 2280248aee62SJacques Vidrine struct addrinfo sentinel, *cur; 2281248aee62SJacques Vidrine struct addrinfo hints, *res, *res0; 2282248aee62SJacques Vidrine int error; 2283248aee62SJacques Vidrine char *p = line; 2284248aee62SJacques Vidrine const char *addr, *canonname; 2285248aee62SJacques Vidrine char *nextline; 2286248aee62SJacques Vidrine char *cp; 2287248aee62SJacques Vidrine 2288248aee62SJacques Vidrine addr = canonname = NULL; 2289248aee62SJacques Vidrine 2290248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 2291a42af91cSHajimu UMEMOTO cur = &sentinel; 2292a42af91cSHajimu UMEMOTO 2293248aee62SJacques Vidrine nextline: 2294248aee62SJacques Vidrine /* terminate line */ 2295248aee62SJacques Vidrine cp = strchr(p, '\n'); 2296248aee62SJacques Vidrine if (cp) { 2297248aee62SJacques Vidrine *cp++ = '\0'; 2298248aee62SJacques Vidrine nextline = cp; 2299248aee62SJacques Vidrine } else 2300248aee62SJacques Vidrine nextline = NULL; 2301a42af91cSHajimu UMEMOTO 2302248aee62SJacques Vidrine cp = strpbrk(p, " \t"); 2303248aee62SJacques Vidrine if (cp == NULL) { 2304248aee62SJacques Vidrine if (canonname == NULL) 2305248aee62SJacques Vidrine return (NULL); 2306248aee62SJacques Vidrine else 2307248aee62SJacques Vidrine goto done; 2308a42af91cSHajimu UMEMOTO } 2309248aee62SJacques Vidrine *cp++ = '\0'; 2310a42af91cSHajimu UMEMOTO 2311248aee62SJacques Vidrine addr = p; 2312a42af91cSHajimu UMEMOTO 2313248aee62SJacques Vidrine while (cp && *cp) { 2314248aee62SJacques Vidrine if (*cp == ' ' || *cp == '\t') { 2315248aee62SJacques Vidrine cp++; 2316a42af91cSHajimu UMEMOTO continue; 2317248aee62SJacques Vidrine } 2318248aee62SJacques Vidrine if (!canonname) 2319248aee62SJacques Vidrine canonname = cp; 2320248aee62SJacques Vidrine if ((cp = strpbrk(cp, " \t")) != NULL) 2321248aee62SJacques Vidrine *cp++ = '\0'; 2322a42af91cSHajimu UMEMOTO } 2323a42af91cSHajimu UMEMOTO 2324bf838688SHajimu UMEMOTO hints = *pai; 2325248aee62SJacques Vidrine hints.ai_flags = AI_NUMERICHOST; 2326bf838688SHajimu UMEMOTO error = getaddrinfo(addr, NULL, &hints, &res0); 2327248aee62SJacques Vidrine if (error == 0) { 2328248aee62SJacques Vidrine for (res = res0; res; res = res->ai_next) { 2329248aee62SJacques Vidrine /* cover it up */ 2330248aee62SJacques Vidrine res->ai_flags = pai->ai_flags; 2331248aee62SJacques Vidrine 2332248aee62SJacques Vidrine if (pai->ai_flags & AI_CANONNAME) 2333248aee62SJacques Vidrine (void)get_canonname(pai, res, canonname); 2334248aee62SJacques Vidrine } 2335248aee62SJacques Vidrine } else 2336248aee62SJacques Vidrine res0 = NULL; 2337248aee62SJacques Vidrine if (res0) { 2338248aee62SJacques Vidrine cur->ai_next = res0; 2339a42af91cSHajimu UMEMOTO while (cur && cur->ai_next) 2340a42af91cSHajimu UMEMOTO cur = cur->ai_next; 2341a42af91cSHajimu UMEMOTO } 2342a42af91cSHajimu UMEMOTO 2343248aee62SJacques Vidrine if (nextline) { 2344248aee62SJacques Vidrine p = nextline; 2345248aee62SJacques Vidrine goto nextline; 2346248aee62SJacques Vidrine } 2347a42af91cSHajimu UMEMOTO 2348248aee62SJacques Vidrine done: 2349248aee62SJacques Vidrine return sentinel.ai_next; 2350248aee62SJacques Vidrine } 2351248aee62SJacques Vidrine 2352248aee62SJacques Vidrine /*ARGSUSED*/ 2353248aee62SJacques Vidrine static int 235490d1d883SHajimu UMEMOTO _yp_getaddrinfo(void *rv, void *cb_data, va_list ap) 2355248aee62SJacques Vidrine { 2356248aee62SJacques Vidrine struct addrinfo sentinel, *cur; 2357248aee62SJacques Vidrine struct addrinfo *ai = NULL; 2358d7057edbSHajimu UMEMOTO char *ypbuf; 2359d7057edbSHajimu UMEMOTO int ypbuflen, r; 2360248aee62SJacques Vidrine const char *name; 2361248aee62SJacques Vidrine const struct addrinfo *pai; 2362d7057edbSHajimu UMEMOTO char *ypdomain; 2363d7057edbSHajimu UMEMOTO 2364d7057edbSHajimu UMEMOTO if (_yp_check(&ypdomain) == 0) 2365d7057edbSHajimu UMEMOTO return NS_UNAVAIL; 2366248aee62SJacques Vidrine 2367248aee62SJacques Vidrine name = va_arg(ap, char *); 2368248aee62SJacques Vidrine pai = va_arg(ap, const struct addrinfo *); 2369248aee62SJacques Vidrine 2370248aee62SJacques Vidrine memset(&sentinel, 0, sizeof(sentinel)); 2371248aee62SJacques Vidrine cur = &sentinel; 2372248aee62SJacques Vidrine 2373248aee62SJacques Vidrine /* hosts.byname is only for IPv4 (Solaris8) */ 2374248aee62SJacques Vidrine if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { 2375d7057edbSHajimu UMEMOTO r = yp_match(ypdomain, "hosts.byname", name, 2376d7057edbSHajimu UMEMOTO (int)strlen(name), &ypbuf, &ypbuflen); 2377248aee62SJacques Vidrine if (r == 0) { 2378248aee62SJacques Vidrine struct addrinfo ai4; 2379248aee62SJacques Vidrine 2380248aee62SJacques Vidrine ai4 = *pai; 2381248aee62SJacques Vidrine ai4.ai_family = AF_INET; 2382d7057edbSHajimu UMEMOTO ai = _yphostent(ypbuf, &ai4); 2383248aee62SJacques Vidrine if (ai) { 2384248aee62SJacques Vidrine cur->ai_next = ai; 2385248aee62SJacques Vidrine while (cur && cur->ai_next) 2386248aee62SJacques Vidrine cur = cur->ai_next; 2387248aee62SJacques Vidrine } 2388d7057edbSHajimu UMEMOTO free(ypbuf); 2389248aee62SJacques Vidrine } 2390f8997ca2SHajimu UMEMOTO } 2391248aee62SJacques Vidrine 2392248aee62SJacques Vidrine /* ipnodes.byname can hold both IPv4/v6 */ 2393d7057edbSHajimu UMEMOTO r = yp_match(ypdomain, "ipnodes.byname", name, 2394d7057edbSHajimu UMEMOTO (int)strlen(name), &ypbuf, &ypbuflen); 2395248aee62SJacques Vidrine if (r == 0) { 2396d7057edbSHajimu UMEMOTO ai = _yphostent(ypbuf, pai); 2397d7057edbSHajimu UMEMOTO if (ai) 2398248aee62SJacques Vidrine cur->ai_next = ai; 2399d7057edbSHajimu UMEMOTO free(ypbuf); 2400248aee62SJacques Vidrine } 2401248aee62SJacques Vidrine 2402248aee62SJacques Vidrine if (sentinel.ai_next == NULL) { 24035342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND); 2404248aee62SJacques Vidrine return NS_NOTFOUND; 2405248aee62SJacques Vidrine } 2406248aee62SJacques Vidrine *((struct addrinfo **)rv) = sentinel.ai_next; 2407248aee62SJacques Vidrine return NS_SUCCESS; 2408a42af91cSHajimu UMEMOTO } 2409a42af91cSHajimu UMEMOTO #endif 2410a42af91cSHajimu UMEMOTO 2411a42af91cSHajimu UMEMOTO /* resolver logic */ 2412a42af91cSHajimu UMEMOTO 2413a42af91cSHajimu UMEMOTO /* 2414a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and await answer. 2415a42af91cSHajimu UMEMOTO * Returned answer is placed in supplied buffer "answer". 2416a42af91cSHajimu UMEMOTO * Perform preliminary check of answer, returning success only 2417a42af91cSHajimu UMEMOTO * if no error is indicated and the answer count is nonzero. 2418a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 2419a42af91cSHajimu UMEMOTO * Error number is left in h_errno. 2420a42af91cSHajimu UMEMOTO * 2421a42af91cSHajimu UMEMOTO * Caller must parse answer and determine whether it answers the question. 2422a42af91cSHajimu UMEMOTO */ 2423a42af91cSHajimu UMEMOTO static int 242490d1d883SHajimu UMEMOTO res_queryN(const char *name, struct res_target *target, res_state res) 2425a42af91cSHajimu UMEMOTO { 24265cd588cbSHajimu UMEMOTO u_char *buf; 2427a42af91cSHajimu UMEMOTO HEADER *hp; 2428a42af91cSHajimu UMEMOTO int n; 24291259a17aSHajimu UMEMOTO u_int oflags; 2430a42af91cSHajimu UMEMOTO struct res_target *t; 2431a42af91cSHajimu UMEMOTO int rcode; 2432a42af91cSHajimu UMEMOTO int ancount; 2433a42af91cSHajimu UMEMOTO 2434a42af91cSHajimu UMEMOTO rcode = NOERROR; 2435a42af91cSHajimu UMEMOTO ancount = 0; 2436a42af91cSHajimu UMEMOTO 24375cd588cbSHajimu UMEMOTO buf = malloc(MAXPACKET); 24385cd588cbSHajimu UMEMOTO if (!buf) { 24395342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NETDB_INTERNAL); 24405cd588cbSHajimu UMEMOTO return -1; 24415cd588cbSHajimu UMEMOTO } 24425cd588cbSHajimu UMEMOTO 2443a42af91cSHajimu UMEMOTO for (t = target; t; t = t->next) { 2444a42af91cSHajimu UMEMOTO int class, type; 2445a42af91cSHajimu UMEMOTO u_char *answer; 2446a42af91cSHajimu UMEMOTO int anslen; 2447a42af91cSHajimu UMEMOTO 2448a42af91cSHajimu UMEMOTO hp = (HEADER *)(void *)t->answer; 2449a42af91cSHajimu UMEMOTO 2450a42af91cSHajimu UMEMOTO /* make it easier... */ 2451b826397aSJun-ichiro itojun Hagino class = t->qclass; 2452b826397aSJun-ichiro itojun Hagino type = t->qtype; 2453a42af91cSHajimu UMEMOTO answer = t->answer; 2454a42af91cSHajimu UMEMOTO anslen = t->anslen; 24551259a17aSHajimu UMEMOTO 24561259a17aSHajimu UMEMOTO oflags = res->_flags; 24571259a17aSHajimu UMEMOTO 24581259a17aSHajimu UMEMOTO again: 24591259a17aSHajimu UMEMOTO hp->rcode = NOERROR; /* default */ 24601259a17aSHajimu UMEMOTO 2461a42af91cSHajimu UMEMOTO #ifdef DEBUG 24625342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2463a42af91cSHajimu UMEMOTO printf(";; res_query(%s, %d, %d)\n", name, class, type); 2464a42af91cSHajimu UMEMOTO #endif 2465a42af91cSHajimu UMEMOTO 24665342d17fSHajimu UMEMOTO n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, 24675cd588cbSHajimu UMEMOTO buf, MAXPACKET); 24681259a17aSHajimu UMEMOTO if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 && 24691259a17aSHajimu UMEMOTO (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U) 24705342d17fSHajimu UMEMOTO n = res_nopt(res, n, buf, MAXPACKET, anslen); 2471a42af91cSHajimu UMEMOTO if (n <= 0) { 2472a42af91cSHajimu UMEMOTO #ifdef DEBUG 24735342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2474a42af91cSHajimu UMEMOTO printf(";; res_query: mkquery failed\n"); 2475a42af91cSHajimu UMEMOTO #endif 24765cd588cbSHajimu UMEMOTO free(buf); 24775342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2478a42af91cSHajimu UMEMOTO return (n); 2479a42af91cSHajimu UMEMOTO } 24805342d17fSHajimu UMEMOTO n = res_nsend(res, buf, n, answer, anslen); 2481a42af91cSHajimu UMEMOTO if (n < 0) { 24821259a17aSHajimu UMEMOTO /* 24831259a17aSHajimu UMEMOTO * if the query choked with EDNS0, retry 24841259a17aSHajimu UMEMOTO * without EDNS0 24851259a17aSHajimu UMEMOTO */ 24861259a17aSHajimu UMEMOTO if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) 24871259a17aSHajimu UMEMOTO != 0U && 24881259a17aSHajimu UMEMOTO ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) { 24891259a17aSHajimu UMEMOTO res->_flags |= RES_F_EDNS0ERR; 24901259a17aSHajimu UMEMOTO if (res->options & RES_DEBUG) 24911259a17aSHajimu UMEMOTO printf(";; res_nquery: retry without EDNS0\n"); 24921259a17aSHajimu UMEMOTO goto again; 24931259a17aSHajimu UMEMOTO } 24941259a17aSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */ 2495a42af91cSHajimu UMEMOTO #ifdef DEBUG 24965342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2497a42af91cSHajimu UMEMOTO printf(";; res_query: send error\n"); 2498a42af91cSHajimu UMEMOTO #endif 24991259a17aSHajimu UMEMOTO continue; 2500a42af91cSHajimu UMEMOTO } 2501a42af91cSHajimu UMEMOTO 2502d05ad2abSHajimu UMEMOTO if (n > anslen) 250354384cf3SJacques Vidrine hp->rcode = FORMERR; /* XXX not very informative */ 25041259a17aSHajimu UMEMOTO if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 2505a42af91cSHajimu UMEMOTO rcode = hp->rcode; /* record most recent error */ 2506a42af91cSHajimu UMEMOTO #ifdef DEBUG 25075342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2508dd521ef1SHajimu UMEMOTO printf(";; rcode = %u, ancount=%u\n", hp->rcode, 2509a42af91cSHajimu UMEMOTO ntohs(hp->ancount)); 2510a42af91cSHajimu UMEMOTO #endif 2511a42af91cSHajimu UMEMOTO continue; 2512a42af91cSHajimu UMEMOTO } 2513a42af91cSHajimu UMEMOTO 2514a42af91cSHajimu UMEMOTO ancount += ntohs(hp->ancount); 2515a42af91cSHajimu UMEMOTO 2516a42af91cSHajimu UMEMOTO t->n = n; 2517a42af91cSHajimu UMEMOTO } 2518a42af91cSHajimu UMEMOTO 25195cd588cbSHajimu UMEMOTO free(buf); 25205cd588cbSHajimu UMEMOTO 2521a42af91cSHajimu UMEMOTO if (ancount == 0) { 2522a42af91cSHajimu UMEMOTO switch (rcode) { 2523a42af91cSHajimu UMEMOTO case NXDOMAIN: 25245342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, HOST_NOT_FOUND); 2525a42af91cSHajimu UMEMOTO break; 2526a42af91cSHajimu UMEMOTO case SERVFAIL: 25275342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 2528a42af91cSHajimu UMEMOTO break; 2529a42af91cSHajimu UMEMOTO case NOERROR: 25305342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_DATA); 2531a42af91cSHajimu UMEMOTO break; 2532a42af91cSHajimu UMEMOTO case FORMERR: 2533a42af91cSHajimu UMEMOTO case NOTIMP: 2534a42af91cSHajimu UMEMOTO case REFUSED: 2535a42af91cSHajimu UMEMOTO default: 25365342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2537a42af91cSHajimu UMEMOTO break; 2538a42af91cSHajimu UMEMOTO } 2539a42af91cSHajimu UMEMOTO return (-1); 2540a42af91cSHajimu UMEMOTO } 2541a42af91cSHajimu UMEMOTO return (ancount); 2542a42af91cSHajimu UMEMOTO } 2543a42af91cSHajimu UMEMOTO 2544a42af91cSHajimu UMEMOTO /* 2545a42af91cSHajimu UMEMOTO * Formulate a normal query, send, and retrieve answer in supplied buffer. 2546a42af91cSHajimu UMEMOTO * Return the size of the response on success, -1 on error. 2547a42af91cSHajimu UMEMOTO * If enabled, implement search rules until answer or unrecoverable failure 2548a42af91cSHajimu UMEMOTO * is detected. Error code, if any, is left in h_errno. 2549a42af91cSHajimu UMEMOTO */ 2550a42af91cSHajimu UMEMOTO static int 255190d1d883SHajimu UMEMOTO res_searchN(const char *name, struct res_target *target, res_state res) 2552a42af91cSHajimu UMEMOTO { 2553a42af91cSHajimu UMEMOTO const char *cp, * const *domain; 2554a42af91cSHajimu UMEMOTO HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ 2555a42af91cSHajimu UMEMOTO u_int dots; 2556a42af91cSHajimu UMEMOTO int trailing_dot, ret, saved_herrno; 25575ecccefbSHajimu UMEMOTO int got_nodata = 0, got_servfail = 0, root_on_list = 0; 25585ecccefbSHajimu UMEMOTO int tried_as_is = 0; 25595ecccefbSHajimu UMEMOTO int searched = 0; 2560159d2a98SHajimu UMEMOTO char abuf[MAXDNAME]; 2561a42af91cSHajimu UMEMOTO 2562a42af91cSHajimu UMEMOTO errno = 0; 25635342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */ 2564a42af91cSHajimu UMEMOTO dots = 0; 2565a42af91cSHajimu UMEMOTO for (cp = name; *cp; cp++) 2566a42af91cSHajimu UMEMOTO dots += (*cp == '.'); 2567a42af91cSHajimu UMEMOTO trailing_dot = 0; 2568a42af91cSHajimu UMEMOTO if (cp > name && *--cp == '.') 2569a42af91cSHajimu UMEMOTO trailing_dot++; 2570a42af91cSHajimu UMEMOTO 2571a42af91cSHajimu UMEMOTO /* 2572a42af91cSHajimu UMEMOTO * if there aren't any dots, it could be a user-level alias 2573a42af91cSHajimu UMEMOTO */ 25745342d17fSHajimu UMEMOTO if (!dots && 25755342d17fSHajimu UMEMOTO (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL) 25765342d17fSHajimu UMEMOTO return (res_queryN(cp, target, res)); 2577a42af91cSHajimu UMEMOTO 2578a42af91cSHajimu UMEMOTO /* 25795ecccefbSHajimu UMEMOTO * If there are enough dots in the name, let's just give it a 25805ecccefbSHajimu UMEMOTO * try 'as is'. The threshold can be set with the "ndots" option. 25815ecccefbSHajimu UMEMOTO * Also, query 'as is', if there is a trailing dot in the name. 2582a42af91cSHajimu UMEMOTO */ 2583a42af91cSHajimu UMEMOTO saved_herrno = -1; 25845342d17fSHajimu UMEMOTO if (dots >= res->ndots || trailing_dot) { 25855342d17fSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target, res); 25865ecccefbSHajimu UMEMOTO if (ret > 0 || trailing_dot) 2587a42af91cSHajimu UMEMOTO return (ret); 25883a0b3b67SHajimu UMEMOTO if (errno == ECONNREFUSED) { 25895342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 25903a0b3b67SHajimu UMEMOTO return (-1); 25913a0b3b67SHajimu UMEMOTO } 25925342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 25933a0b3b67SHajimu UMEMOTO case NO_DATA: 25943a0b3b67SHajimu UMEMOTO case HOST_NOT_FOUND: 25953a0b3b67SHajimu UMEMOTO break; 2596aebc6ec2SHajimu UMEMOTO case TRY_AGAIN: 2597aebc6ec2SHajimu UMEMOTO if (hp->rcode == SERVFAIL) 2598aebc6ec2SHajimu UMEMOTO break; 2599aebc6ec2SHajimu UMEMOTO /* FALLTHROUGH */ 26003a0b3b67SHajimu UMEMOTO default: 26013a0b3b67SHajimu UMEMOTO return (-1); 26023a0b3b67SHajimu UMEMOTO } 26035342d17fSHajimu UMEMOTO saved_herrno = res->res_h_errno; 2604a42af91cSHajimu UMEMOTO tried_as_is++; 2605a42af91cSHajimu UMEMOTO } 2606a42af91cSHajimu UMEMOTO 2607a42af91cSHajimu UMEMOTO /* 2608a42af91cSHajimu UMEMOTO * We do at least one level of search if 2609a42af91cSHajimu UMEMOTO * - there is no dot and RES_DEFNAME is set, or 2610a42af91cSHajimu UMEMOTO * - there is at least one dot, there is no trailing dot, 2611a42af91cSHajimu UMEMOTO * and RES_DNSRCH is set. 2612a42af91cSHajimu UMEMOTO */ 26135342d17fSHajimu UMEMOTO if ((!dots && (res->options & RES_DEFNAMES)) || 26145342d17fSHajimu UMEMOTO (dots && !trailing_dot && (res->options & RES_DNSRCH))) { 2615a42af91cSHajimu UMEMOTO int done = 0; 2616a42af91cSHajimu UMEMOTO 26175342d17fSHajimu UMEMOTO for (domain = (const char * const *)res->dnsrch; 2618a42af91cSHajimu UMEMOTO *domain && !done; 2619a42af91cSHajimu UMEMOTO domain++) { 26205ecccefbSHajimu UMEMOTO searched = 1; 26215ecccefbSHajimu UMEMOTO 26225ecccefbSHajimu UMEMOTO if (domain[0][0] == '\0' || 26235ecccefbSHajimu UMEMOTO (domain[0][0] == '.' && domain[0][1] == '\0')) 26245ecccefbSHajimu UMEMOTO root_on_list++; 26255ecccefbSHajimu UMEMOTO 26265ecccefbSHajimu UMEMOTO if (root_on_list && tried_as_is) 26275ecccefbSHajimu UMEMOTO continue; 2628a42af91cSHajimu UMEMOTO 26295342d17fSHajimu UMEMOTO ret = res_querydomainN(name, *domain, target, res); 2630a42af91cSHajimu UMEMOTO if (ret > 0) 2631a42af91cSHajimu UMEMOTO return (ret); 2632a42af91cSHajimu UMEMOTO 2633a42af91cSHajimu UMEMOTO /* 2634a42af91cSHajimu UMEMOTO * If no server present, give up. 2635a42af91cSHajimu UMEMOTO * If name isn't found in this domain, 2636a42af91cSHajimu UMEMOTO * keep trying higher domains in the search list 2637a42af91cSHajimu UMEMOTO * (if that's enabled). 2638a42af91cSHajimu UMEMOTO * On a NO_DATA error, keep trying, otherwise 2639a42af91cSHajimu UMEMOTO * a wildcard entry of another type could keep us 2640a42af91cSHajimu UMEMOTO * from finding this entry higher in the domain. 2641a42af91cSHajimu UMEMOTO * If we get some other error (negative answer or 2642a42af91cSHajimu UMEMOTO * server failure), then stop searching up, 2643a42af91cSHajimu UMEMOTO * but try the input name below in case it's 2644a42af91cSHajimu UMEMOTO * fully-qualified. 2645a42af91cSHajimu UMEMOTO */ 2646a42af91cSHajimu UMEMOTO if (errno == ECONNREFUSED) { 26475342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 2648a42af91cSHajimu UMEMOTO return (-1); 2649a42af91cSHajimu UMEMOTO } 2650a42af91cSHajimu UMEMOTO 26515342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 2652a42af91cSHajimu UMEMOTO case NO_DATA: 2653a42af91cSHajimu UMEMOTO got_nodata++; 2654a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 2655a42af91cSHajimu UMEMOTO case HOST_NOT_FOUND: 2656a42af91cSHajimu UMEMOTO /* keep trying */ 2657a42af91cSHajimu UMEMOTO break; 2658a42af91cSHajimu UMEMOTO case TRY_AGAIN: 2659aebc6ec2SHajimu UMEMOTO got_servfail++; 2660a42af91cSHajimu UMEMOTO if (hp->rcode == SERVFAIL) { 2661a42af91cSHajimu UMEMOTO /* try next search element, if any */ 2662a42af91cSHajimu UMEMOTO break; 2663a42af91cSHajimu UMEMOTO } 2664a42af91cSHajimu UMEMOTO /* FALLTHROUGH */ 2665a42af91cSHajimu UMEMOTO default: 2666a42af91cSHajimu UMEMOTO /* anything else implies that we're done */ 2667a42af91cSHajimu UMEMOTO done++; 2668a42af91cSHajimu UMEMOTO } 2669a42af91cSHajimu UMEMOTO /* 2670a42af91cSHajimu UMEMOTO * if we got here for some reason other than DNSRCH, 2671a42af91cSHajimu UMEMOTO * we only wanted one iteration of the loop, so stop. 2672a42af91cSHajimu UMEMOTO */ 26735342d17fSHajimu UMEMOTO if (!(res->options & RES_DNSRCH)) 2674a42af91cSHajimu UMEMOTO done++; 2675a42af91cSHajimu UMEMOTO } 2676a42af91cSHajimu UMEMOTO } 2677a42af91cSHajimu UMEMOTO 26785342d17fSHajimu UMEMOTO switch (res->res_h_errno) { 26793a0b3b67SHajimu UMEMOTO case NO_DATA: 26803a0b3b67SHajimu UMEMOTO case HOST_NOT_FOUND: 26813a0b3b67SHajimu UMEMOTO break; 2682aebc6ec2SHajimu UMEMOTO case TRY_AGAIN: 2683aebc6ec2SHajimu UMEMOTO if (hp->rcode == SERVFAIL) 2684aebc6ec2SHajimu UMEMOTO break; 2685aebc6ec2SHajimu UMEMOTO /* FALLTHROUGH */ 26863a0b3b67SHajimu UMEMOTO default: 26873a0b3b67SHajimu UMEMOTO goto giveup; 26883a0b3b67SHajimu UMEMOTO } 26893a0b3b67SHajimu UMEMOTO 2690a42af91cSHajimu UMEMOTO /* 26915ecccefbSHajimu UMEMOTO * If the query has not already been tried as is then try it 26925ecccefbSHajimu UMEMOTO * unless RES_NOTLDQUERY is set and there were no dots. 2693a42af91cSHajimu UMEMOTO */ 26945342d17fSHajimu UMEMOTO if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) && 26955ecccefbSHajimu UMEMOTO !(tried_as_is || root_on_list)) { 26965342d17fSHajimu UMEMOTO ret = res_querydomainN(name, NULL, target, res); 2697a42af91cSHajimu UMEMOTO if (ret > 0) 2698a42af91cSHajimu UMEMOTO return (ret); 2699a42af91cSHajimu UMEMOTO } 2700a42af91cSHajimu UMEMOTO 2701a42af91cSHajimu UMEMOTO /* 2702a42af91cSHajimu UMEMOTO * if we got here, we didn't satisfy the search. 2703a42af91cSHajimu UMEMOTO * if we did an initial full query, return that query's h_errno 2704a42af91cSHajimu UMEMOTO * (note that we wouldn't be here if that query had succeeded). 2705a42af91cSHajimu UMEMOTO * else if we ever got a nodata, send that back as the reason. 2706a42af91cSHajimu UMEMOTO * else send back meaningless h_errno, that being the one from 2707a42af91cSHajimu UMEMOTO * the last DNSRCH we did. 2708a42af91cSHajimu UMEMOTO */ 27093a0b3b67SHajimu UMEMOTO giveup: 2710a42af91cSHajimu UMEMOTO if (saved_herrno != -1) 27115342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, saved_herrno); 2712a42af91cSHajimu UMEMOTO else if (got_nodata) 27135342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_DATA); 2714a42af91cSHajimu UMEMOTO else if (got_servfail) 27155342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, TRY_AGAIN); 2716a42af91cSHajimu UMEMOTO return (-1); 2717a42af91cSHajimu UMEMOTO } 2718a42af91cSHajimu UMEMOTO 2719a42af91cSHajimu UMEMOTO /* 2720a42af91cSHajimu UMEMOTO * Perform a call on res_query on the concatenation of name and domain, 2721a42af91cSHajimu UMEMOTO * removing a trailing dot from name if domain is NULL. 2722a42af91cSHajimu UMEMOTO */ 2723a42af91cSHajimu UMEMOTO static int 272490d1d883SHajimu UMEMOTO res_querydomainN(const char *name, const char *domain, 272590d1d883SHajimu UMEMOTO struct res_target *target, res_state res) 2726a42af91cSHajimu UMEMOTO { 2727a42af91cSHajimu UMEMOTO char nbuf[MAXDNAME]; 2728a42af91cSHajimu UMEMOTO const char *longname = nbuf; 2729a42af91cSHajimu UMEMOTO size_t n, d; 2730a42af91cSHajimu UMEMOTO 2731a42af91cSHajimu UMEMOTO #ifdef DEBUG 27325342d17fSHajimu UMEMOTO if (res->options & RES_DEBUG) 2733a42af91cSHajimu UMEMOTO printf(";; res_querydomain(%s, %s)\n", 2734a42af91cSHajimu UMEMOTO name, domain?domain:"<Nil>"); 2735a42af91cSHajimu UMEMOTO #endif 2736a42af91cSHajimu UMEMOTO if (domain == NULL) { 2737a42af91cSHajimu UMEMOTO /* 2738a42af91cSHajimu UMEMOTO * Check for trailing '.'; 2739a42af91cSHajimu UMEMOTO * copy without '.' if present. 2740a42af91cSHajimu UMEMOTO */ 2741a42af91cSHajimu UMEMOTO n = strlen(name); 2742a42af91cSHajimu UMEMOTO if (n >= MAXDNAME) { 27435342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2744a42af91cSHajimu UMEMOTO return (-1); 2745a42af91cSHajimu UMEMOTO } 2746a42af91cSHajimu UMEMOTO if (n > 0 && name[--n] == '.') { 2747a42af91cSHajimu UMEMOTO strncpy(nbuf, name, n); 2748a42af91cSHajimu UMEMOTO nbuf[n] = '\0'; 2749a42af91cSHajimu UMEMOTO } else 2750a42af91cSHajimu UMEMOTO longname = name; 2751a42af91cSHajimu UMEMOTO } else { 2752a42af91cSHajimu UMEMOTO n = strlen(name); 2753a42af91cSHajimu UMEMOTO d = strlen(domain); 2754a42af91cSHajimu UMEMOTO if (n + d + 1 >= MAXDNAME) { 27555342d17fSHajimu UMEMOTO RES_SET_H_ERRNO(res, NO_RECOVERY); 2756a42af91cSHajimu UMEMOTO return (-1); 2757a42af91cSHajimu UMEMOTO } 2758dd521ef1SHajimu UMEMOTO snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); 2759a42af91cSHajimu UMEMOTO } 27605342d17fSHajimu UMEMOTO return (res_queryN(longname, target, res)); 2761a42af91cSHajimu UMEMOTO } 2762