Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */ |
| 2 | /* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the project nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Issues to be discussed: |
| 35 | * - Thread safe-ness must be checked. |
| 36 | * - Return values. There are nonstandard return values defined and used |
| 37 | * in the source code. This is because RFC2553 is silent about which error |
| 38 | * code must be returned for which situation. |
| 39 | * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 |
| 40 | * says to use inet_aton() to convert IPv4 numeric to binary (alows |
| 41 | * classful form as a result). |
| 42 | * current code - disallow classful form for IPv4 (due to use of inet_pton). |
| 43 | * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is |
| 44 | * invalid. |
| 45 | * current code - SEGV on freeaddrinfo(NULL) |
| 46 | * Note: |
| 47 | * - We use getipnodebyname() just for thread-safeness. There's no intent |
| 48 | * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to |
| 49 | * getipnodebyname(). |
| 50 | * - The code filters out AFs that are not supported by the kernel, |
| 51 | * when globbing NULL hostname (to loopback, or wildcard). Is it the right |
| 52 | * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG |
| 53 | * in ai_flags? |
| 54 | * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. |
| 55 | * (1) what should we do against numeric hostname (2) what should we do |
| 56 | * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? |
| 57 | * non-loopback address configured? global address configured? |
| 58 | * - To avoid search order issue, we have a big amount of code duplicate |
| 59 | * from gethnamaddr.c and some other places. The issues that there's no |
| 60 | * lower layer function to lookup "IPv4 or IPv6" record. Calling |
| 61 | * gethostbyname2 from getaddrinfo will end up in wrong search order, as |
| 62 | * follows: |
| 63 | * - The code makes use of following calls when asked to resolver with |
| 64 | * ai_family = PF_UNSPEC: |
| 65 | * getipnodebyname(host, AF_INET6); |
| 66 | * getipnodebyname(host, AF_INET); |
| 67 | * This will result in the following queries if the node is configure to |
| 68 | * prefer /etc/hosts than DNS: |
| 69 | * lookup /etc/hosts for IPv6 address |
| 70 | * lookup DNS for IPv6 address |
| 71 | * lookup /etc/hosts for IPv4 address |
| 72 | * lookup DNS for IPv4 address |
| 73 | * which may not meet people's requirement. |
| 74 | * The right thing to happen is to have underlying layer which does |
| 75 | * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. |
| 76 | * This would result in a bit of code duplicate with _dns_ghbyname() and |
| 77 | * friends. |
| 78 | */ |
| 79 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 80 | #include <arpa/inet.h> |
| 81 | #include <arpa/nameser.h> |
| 82 | #include <assert.h> |
| 83 | #include <ctype.h> |
| 84 | #include <errno.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 85 | #include <fcntl.h> |
| 86 | #include <net/if.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 87 | #include <netdb.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 88 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 89 | #include <stdbool.h> |
| 90 | #include <stddef.h> |
| 91 | #include <stdio.h> |
| 92 | #include <stdlib.h> |
| 93 | #include <string.h> |
| 94 | #include <strings.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 95 | #include <sys/param.h> |
| 96 | #include <sys/socket.h> |
| 97 | #include <sys/stat.h> |
| 98 | #include <sys/types.h> |
| 99 | #include <sys/un.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 100 | #include <unistd.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 101 | #include "resolv_cache.h" |
| 102 | #include "resolv_netid.h" |
| 103 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 104 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 105 | #include <stdarg.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 106 | #include <syslog.h> |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame^] | 107 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 108 | #include "nsswitch.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 109 | |
| 110 | typedef union sockaddr_union { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 111 | struct sockaddr generic; |
| 112 | struct sockaddr_in in; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 113 | struct sockaddr_in6 in6; |
| 114 | } sockaddr_union; |
| 115 | |
| 116 | #define SUCCESS 0 |
| 117 | #define ANY 0 |
| 118 | #define YES 1 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | #define NO 0 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 120 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 121 | static const char in_addrany[] = {0, 0, 0, 0}; |
| 122 | static const char in_loopback[] = {127, 0, 0, 1}; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 123 | #ifdef INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 124 | static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 125 | static const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 126 | #endif |
| 127 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 128 | static const struct afd { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 129 | int a_af; |
| 130 | int a_addrlen; |
| 131 | int a_socklen; |
| 132 | int a_off; |
| 133 | const char* a_addrany; |
| 134 | const char* a_loopback; |
| 135 | int a_scoped; |
| 136 | } afdl[] = { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 137 | #ifdef INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 138 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), |
| 139 | offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1}, |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 140 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 141 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), |
| 142 | offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0}, |
| 143 | {0, 0, 0, 0, NULL, NULL, 0}, |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | struct explore { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | int e_af; |
| 148 | int e_socktype; |
| 149 | int e_protocol; |
| 150 | const char* e_protostr; |
| 151 | int e_wild; |
| 152 | #define WILD_AF(ex) ((ex)->e_wild & 0x01) |
| 153 | #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) |
| 154 | #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | static const struct explore explore[] = { |
| 158 | #if 0 |
| 159 | { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, |
| 160 | #endif |
| 161 | #ifdef INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 162 | {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07}, |
| 163 | {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07}, |
| 164 | {PF_INET6, SOCK_RAW, ANY, NULL, 0x05}, |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 165 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 166 | {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07}, |
| 167 | {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07}, |
| 168 | {PF_INET, SOCK_RAW, ANY, NULL, 0x05}, |
| 169 | {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07}, |
| 170 | {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07}, |
| 171 | {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05}, |
| 172 | {-1, 0, 0, NULL, 0}, |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | #ifdef INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 176 | #define PTON_MAX 16 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 177 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 178 | #define PTON_MAX 4 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 179 | #endif |
| 180 | |
| 181 | static const ns_src default_dns_files[] = { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame^] | 182 | {NSSRC_FILES, NS_SUCCESS}, |
| 183 | {NSSRC_DNS, NS_SUCCESS}, |
| 184 | {0, 0} |
| 185 | }; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 186 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 187 | #define MAXPACKET (8 * 1024) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 188 | |
| 189 | typedef union { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 190 | HEADER hdr; |
| 191 | u_char buf[MAXPACKET]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 192 | } querybuf; |
| 193 | |
| 194 | struct res_target { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 195 | struct res_target* next; |
| 196 | const char* name; /* domain name */ |
| 197 | int qclass, qtype; /* class and type of query */ |
| 198 | u_char* answer; /* buffer to put answer */ |
| 199 | int anslen; /* size of answer buffer */ |
| 200 | int n; /* result length */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 201 | }; |
| 202 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 203 | static int str2number(const char*); |
| 204 | static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**, |
| 205 | const struct android_net_context*); |
| 206 | static int explore_null(const struct addrinfo*, const char*, struct addrinfo**); |
| 207 | static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**, |
| 208 | const char*); |
| 209 | static int explore_numeric_scope(const struct addrinfo*, const char*, const char*, |
| 210 | struct addrinfo**); |
| 211 | static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*); |
| 212 | static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*); |
| 213 | static int get_portmatch(const struct addrinfo*, const char*); |
| 214 | static int get_port(const struct addrinfo*, const char*, int); |
| 215 | static const struct afd* find_afd(int); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 216 | #ifdef INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 217 | static int ip6_str2scopeid(char*, struct sockaddr_in6*, u_int32_t*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 218 | #endif |
| 219 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 220 | static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*); |
| 221 | static int _dns_getaddrinfo(void*, void*, va_list); |
| 222 | static void _sethtent(FILE**); |
| 223 | static void _endhtent(FILE**); |
| 224 | static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*); |
| 225 | static int _files_getaddrinfo(void*, void*, va_list); |
| 226 | static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 227 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 228 | static int res_queryN(const char*, struct res_target*, res_state); |
| 229 | static int res_searchN(const char*, struct res_target*, res_state); |
| 230 | static int res_querydomainN(const char*, const char*, struct res_target*, res_state); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 231 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 232 | static const char* const ai_errlist[] = { |
| 233 | "Success", |
| 234 | "Address family for hostname not supported", /* EAI_ADDRFAMILY */ |
| 235 | "Temporary failure in name resolution", /* EAI_AGAIN */ |
| 236 | "Invalid value for ai_flags", /* EAI_BADFLAGS */ |
| 237 | "Non-recoverable failure in name resolution", /* EAI_FAIL */ |
| 238 | "ai_family not supported", /* EAI_FAMILY */ |
| 239 | "Memory allocation failure", /* EAI_MEMORY */ |
| 240 | "No address associated with hostname", /* EAI_NODATA */ |
| 241 | "hostname nor servname provided, or not known", /* EAI_NONAME */ |
| 242 | "servname not supported for ai_socktype", /* EAI_SERVICE */ |
| 243 | "ai_socktype not supported", /* EAI_SOCKTYPE */ |
| 244 | "System error returned in errno", /* EAI_SYSTEM */ |
| 245 | "Invalid value for hints", /* EAI_BADHINTS */ |
| 246 | "Resolved protocol is unknown", /* EAI_PROTOCOL */ |
| 247 | "Argument buffer overflow", /* EAI_OVERFLOW */ |
| 248 | "Unknown error", /* EAI_MAX */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | /* XXX macros that make external reference is BAD. */ |
| 252 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 253 | #define GET_AI(ai, afd, addr) \ |
| 254 | do { \ |
| 255 | /* external reference: pai, error, and label free */ \ |
| 256 | (ai) = get_ai(pai, (afd), (addr)); \ |
| 257 | if ((ai) == NULL) { \ |
| 258 | error = EAI_MEMORY; \ |
| 259 | goto free; \ |
| 260 | } \ |
| 261 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 262 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 263 | #define GET_PORT(ai, serv) \ |
| 264 | do { \ |
| 265 | /* external reference: error and label free */ \ |
| 266 | error = get_port((ai), (serv), 0); \ |
| 267 | if (error != 0) goto free; \ |
| 268 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 269 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 270 | #define GET_CANONNAME(ai, str) \ |
| 271 | do { \ |
| 272 | /* external reference: pai, error and label free */ \ |
| 273 | error = get_canonname(pai, (ai), (str)); \ |
| 274 | if (error != 0) goto free; \ |
| 275 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 276 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 277 | #define ERR(err) \ |
| 278 | do { \ |
| 279 | /* external reference: error, and label bad */ \ |
| 280 | error = (err); \ |
| 281 | goto bad; \ |
| 282 | /*NOTREACHED*/ \ |
| 283 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 284 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 285 | #define MATCH_FAMILY(x, y, w) \ |
| 286 | ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) |
| 287 | #define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY))) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 288 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 289 | const char* gai_strerror(int ecode) { |
| 290 | if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX; |
| 291 | return ai_errlist[ecode]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 292 | } |
| 293 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 294 | void freeaddrinfo(struct addrinfo* ai) { |
| 295 | struct addrinfo* next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 296 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 297 | if (ai == NULL) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 298 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 299 | do { |
| 300 | next = ai->ai_next; |
| 301 | if (ai->ai_canonname) free(ai->ai_canonname); |
| 302 | /* no need to free(ai->ai_addr) */ |
| 303 | free(ai); |
| 304 | ai = next; |
| 305 | } while (ai); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 306 | } |
| 307 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 308 | static int str2number(const char* p) { |
| 309 | char* ep; |
| 310 | unsigned long v; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 311 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 312 | assert(p != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 313 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 314 | if (*p == '\0') return -1; |
| 315 | ep = NULL; |
| 316 | errno = 0; |
| 317 | v = strtoul(p, &ep, 10); |
| 318 | if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) |
| 319 | return v; |
| 320 | else |
| 321 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * The following functions determine whether IPv4 or IPv6 connectivity is |
| 326 | * available in order to implement AI_ADDRCONFIG. |
| 327 | * |
| 328 | * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is |
| 329 | * available, but whether addresses of the specified family are "configured |
| 330 | * on the local system". However, bionic doesn't currently support getifaddrs, |
| 331 | * so checking for connectivity is the next best thing. |
| 332 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 333 | static int _have_ipv6(unsigned mark, uid_t uid) { |
| 334 | static const struct sockaddr_in6 sin6_test = { |
| 335 | .sin6_family = AF_INET6, |
| 336 | .sin6_addr.s6_addr = {// 2000:: |
| 337 | 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; |
| 338 | sockaddr_union addr = {.in6 = sin6_test}; |
| 339 | return _find_src_addr(&addr.generic, NULL, mark, uid) == 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 340 | } |
| 341 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 342 | static int _have_ipv4(unsigned mark, uid_t uid) { |
| 343 | static const struct sockaddr_in sin_test = { |
| 344 | .sin_family = AF_INET, |
| 345 | .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8 |
| 346 | }; |
| 347 | sockaddr_union addr = {.in = sin_test}; |
| 348 | return _find_src_addr(&addr.generic, NULL, mark, uid) == 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | bool readBE32(FILE* fp, int32_t* result) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 352 | int32_t tmp; |
| 353 | if (fread(&tmp, sizeof(tmp), 1, fp) != 1) { |
| 354 | return false; |
| 355 | } |
| 356 | *result = ntohl(tmp); |
| 357 | return true; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 358 | } |
| 359 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 360 | int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints, |
| 361 | struct addrinfo** res) { |
| 362 | return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 363 | } |
| 364 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 365 | int android_getaddrinfofornet(const char* hostname, const char* servname, |
| 366 | const struct addrinfo* hints, unsigned netid, unsigned mark, |
| 367 | struct addrinfo** res) { |
| 368 | struct android_net_context netcontext = { |
| 369 | .app_netid = netid, |
| 370 | .app_mark = mark, |
| 371 | .dns_netid = netid, |
| 372 | .dns_mark = mark, |
| 373 | .uid = NET_CONTEXT_INVALID_UID, |
| 374 | }; |
| 375 | return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 376 | } |
| 377 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 378 | int android_getaddrinfofornetcontext(const char* hostname, const char* servname, |
| 379 | const struct addrinfo* hints, |
| 380 | const struct android_net_context* netcontext, |
| 381 | struct addrinfo** res) { |
| 382 | struct addrinfo sentinel; |
| 383 | struct addrinfo* cur; |
| 384 | int error = 0; |
| 385 | struct addrinfo ai; |
| 386 | struct addrinfo ai0; |
| 387 | struct addrinfo* pai; |
| 388 | const struct explore* ex; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 389 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 390 | /* hostname is allowed to be NULL */ |
| 391 | /* servname is allowed to be NULL */ |
| 392 | /* hints is allowed to be NULL */ |
| 393 | assert(res != NULL); |
| 394 | assert(netcontext != NULL); |
| 395 | memset(&sentinel, 0, sizeof(sentinel)); |
| 396 | cur = &sentinel; |
| 397 | pai = &ai; |
| 398 | pai->ai_flags = 0; |
| 399 | pai->ai_family = PF_UNSPEC; |
| 400 | pai->ai_socktype = ANY; |
| 401 | pai->ai_protocol = ANY; |
| 402 | pai->ai_addrlen = 0; |
| 403 | pai->ai_canonname = NULL; |
| 404 | pai->ai_addr = NULL; |
| 405 | pai->ai_next = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 406 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 407 | if (hostname == NULL && servname == NULL) return EAI_NONAME; |
| 408 | if (hints) { |
| 409 | /* error check for hints */ |
| 410 | if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) |
| 411 | ERR(EAI_BADHINTS); /* xxx */ |
| 412 | if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS); |
| 413 | switch (hints->ai_family) { |
| 414 | case PF_UNSPEC: |
| 415 | case PF_INET: |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 416 | #ifdef INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 417 | case PF_INET6: |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 418 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 419 | break; |
| 420 | default: |
| 421 | ERR(EAI_FAMILY); |
| 422 | } |
| 423 | memcpy(pai, hints, sizeof(*pai)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 424 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 425 | /* |
| 426 | * if both socktype/protocol are specified, check if they |
| 427 | * are meaningful combination. |
| 428 | */ |
| 429 | if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { |
| 430 | for (ex = explore; ex->e_af >= 0; ex++) { |
| 431 | if (pai->ai_family != ex->e_af) continue; |
| 432 | if (ex->e_socktype == ANY) continue; |
| 433 | if (ex->e_protocol == ANY) continue; |
| 434 | if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) { |
| 435 | ERR(EAI_BADHINTS); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 440 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 441 | /* |
| 442 | * check for special cases. (1) numeric servname is disallowed if |
| 443 | * socktype/protocol are left unspecified. (2) servname is disallowed |
| 444 | * for raw and other inet{,6} sockets. |
| 445 | */ |
| 446 | if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 447 | #ifdef PF_INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 448 | || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 449 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 450 | ) { |
| 451 | ai0 = *pai; /* backup *pai */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 452 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 453 | if (pai->ai_family == PF_UNSPEC) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 454 | #ifdef PF_INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 455 | pai->ai_family = PF_INET6; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 456 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 457 | pai->ai_family = PF_INET; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 458 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 459 | } |
| 460 | error = get_portmatch(pai, servname); |
| 461 | if (error) ERR(error); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 462 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 463 | *pai = ai0; |
| 464 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 465 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 466 | ai0 = *pai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 467 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 468 | /* NULL hostname, or numeric hostname */ |
| 469 | for (ex = explore; ex->e_af >= 0; ex++) { |
| 470 | *pai = ai0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 471 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 472 | /* PF_UNSPEC entries are prepared for DNS queries only */ |
| 473 | if (ex->e_af == PF_UNSPEC) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 474 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 475 | if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue; |
| 476 | if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue; |
| 477 | if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 478 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 479 | if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af; |
| 480 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype; |
| 481 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 482 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 483 | if (hostname == NULL) |
| 484 | error = explore_null(pai, servname, &cur->ai_next); |
| 485 | else |
| 486 | error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 487 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 488 | if (error) goto free; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 489 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 490 | while (cur->ai_next) cur = cur->ai_next; |
| 491 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 492 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 493 | /* |
| 494 | * XXX |
| 495 | * If numeric representation of AF1 can be interpreted as FQDN |
| 496 | * representation of AF2, we need to think again about the code below. |
| 497 | */ |
| 498 | if (sentinel.ai_next) goto good; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 499 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 500 | if (hostname == NULL) ERR(EAI_NODATA); |
| 501 | if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 502 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 503 | /* |
| 504 | * hostname as alphabetical name. |
| 505 | * we would like to prefer AF_INET6 than AF_INET, so we'll make a |
| 506 | * outer loop by AFs. |
| 507 | */ |
| 508 | for (ex = explore; ex->e_af >= 0; ex++) { |
| 509 | *pai = ai0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 510 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 511 | /* require exact match for family field */ |
| 512 | if (pai->ai_family != ex->e_af) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 513 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 514 | if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) { |
| 515 | continue; |
| 516 | } |
| 517 | if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) { |
| 518 | continue; |
| 519 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 520 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 521 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype; |
| 522 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 523 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 524 | error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 525 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 526 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 527 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 528 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 529 | /* XXX */ |
| 530 | if (sentinel.ai_next) error = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 531 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 532 | if (error) goto free; |
| 533 | if (error == 0) { |
| 534 | if (sentinel.ai_next) { |
| 535 | good: |
| 536 | *res = sentinel.ai_next; |
| 537 | return SUCCESS; |
| 538 | } else |
| 539 | error = EAI_FAIL; |
| 540 | } |
| 541 | free: |
| 542 | bad: |
| 543 | if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next); |
| 544 | *res = NULL; |
| 545 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* |
| 549 | * FQDN hostname, DNS lookup |
| 550 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 551 | static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname, |
| 552 | struct addrinfo** res, const struct android_net_context* netcontext) { |
| 553 | struct addrinfo* result; |
| 554 | struct addrinfo* cur; |
| 555 | int error = 0; |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame^] | 556 | static const ns_dtab dtab[] = { |
| 557 | {NSSRC_FILES, _files_getaddrinfo, NULL}, |
| 558 | {NSSRC_DNS, _dns_getaddrinfo, NULL}, |
| 559 | {0, 0, 0} |
| 560 | }; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 561 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 562 | assert(pai != NULL); |
| 563 | /* hostname may be NULL */ |
| 564 | /* servname may be NULL */ |
| 565 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 566 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 567 | result = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 568 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 569 | /* |
| 570 | * if the servname does not match socktype/protocol, ignore it. |
| 571 | */ |
| 572 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 573 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 574 | switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai, |
| 575 | netcontext)) { |
| 576 | case NS_TRYAGAIN: |
| 577 | error = EAI_AGAIN; |
| 578 | goto free; |
| 579 | case NS_UNAVAIL: |
| 580 | error = EAI_FAIL; |
| 581 | goto free; |
| 582 | case NS_NOTFOUND: |
| 583 | error = EAI_NODATA; |
| 584 | goto free; |
| 585 | case NS_SUCCESS: |
| 586 | error = 0; |
| 587 | for (cur = result; cur; cur = cur->ai_next) { |
| 588 | GET_PORT(cur, servname); |
| 589 | /* canonname should be filled already */ |
| 590 | } |
| 591 | break; |
| 592 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 593 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 594 | *res = result; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 595 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 596 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 597 | |
| 598 | free: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 599 | if (result) freeaddrinfo(result); |
| 600 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* |
| 604 | * hostname == NULL. |
| 605 | * passive socket -> anyaddr (0.0.0.0 or ::) |
| 606 | * non-passive socket -> localhost (127.0.0.1 or ::1) |
| 607 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 608 | static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) { |
| 609 | int s; |
| 610 | const struct afd* afd; |
| 611 | struct addrinfo* cur; |
| 612 | struct addrinfo sentinel; |
| 613 | int error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 614 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 615 | assert(pai != NULL); |
| 616 | /* servname may be NULL */ |
| 617 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 618 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 619 | *res = NULL; |
| 620 | sentinel.ai_next = NULL; |
| 621 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 622 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 623 | /* |
| 624 | * filter out AFs that are not supported by the kernel |
| 625 | * XXX errno? |
| 626 | */ |
| 627 | s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 628 | if (s < 0) { |
| 629 | if (errno != EMFILE) return 0; |
| 630 | } else |
| 631 | close(s); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 632 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 633 | /* |
| 634 | * if the servname does not match socktype/protocol, ignore it. |
| 635 | */ |
| 636 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 637 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 638 | afd = find_afd(pai->ai_family); |
| 639 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 640 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 641 | if (pai->ai_flags & AI_PASSIVE) { |
| 642 | GET_AI(cur->ai_next, afd, afd->a_addrany); |
| 643 | /* xxx meaningless? |
| 644 | * GET_CANONNAME(cur->ai_next, "anyaddr"); |
| 645 | */ |
| 646 | GET_PORT(cur->ai_next, servname); |
| 647 | } else { |
| 648 | GET_AI(cur->ai_next, afd, afd->a_loopback); |
| 649 | /* xxx meaningless? |
| 650 | * GET_CANONNAME(cur->ai_next, "localhost"); |
| 651 | */ |
| 652 | GET_PORT(cur->ai_next, servname); |
| 653 | } |
| 654 | cur = cur->ai_next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 655 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 656 | *res = sentinel.ai_next; |
| 657 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 658 | |
| 659 | free: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 660 | if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next); |
| 661 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * numeric hostname |
| 666 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 667 | static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname, |
| 668 | struct addrinfo** res, const char* canonname) { |
| 669 | const struct afd* afd; |
| 670 | struct addrinfo* cur; |
| 671 | struct addrinfo sentinel; |
| 672 | int error; |
| 673 | char pton[PTON_MAX]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 674 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 675 | assert(pai != NULL); |
| 676 | /* hostname may be NULL */ |
| 677 | /* servname may be NULL */ |
| 678 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 679 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 680 | *res = NULL; |
| 681 | sentinel.ai_next = NULL; |
| 682 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 683 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 684 | /* |
| 685 | * if the servname does not match socktype/protocol, ignore it. |
| 686 | */ |
| 687 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 688 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 689 | afd = find_afd(pai->ai_family); |
| 690 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 691 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 692 | switch (afd->a_af) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 693 | #if 0 /*X/Open spec*/ |
| 694 | case AF_INET: |
| 695 | if (inet_aton(hostname, (struct in_addr *)pton) == 1) { |
| 696 | if (pai->ai_family == afd->a_af || |
| 697 | pai->ai_family == PF_UNSPEC /*?*/) { |
| 698 | GET_AI(cur->ai_next, afd, pton); |
| 699 | GET_PORT(cur->ai_next, servname); |
| 700 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 701 | /* |
| 702 | * Set the numeric address itself as |
| 703 | * the canonical name, based on a |
| 704 | * clarification in rfc2553bis-03. |
| 705 | */ |
| 706 | GET_CANONNAME(cur->ai_next, canonname); |
| 707 | } |
| 708 | while (cur && cur->ai_next) |
| 709 | cur = cur->ai_next; |
| 710 | } else |
| 711 | ERR(EAI_FAMILY); /*xxx*/ |
| 712 | } |
| 713 | break; |
| 714 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 715 | default: |
| 716 | if (inet_pton(afd->a_af, hostname, pton) == 1) { |
| 717 | if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) { |
| 718 | GET_AI(cur->ai_next, afd, pton); |
| 719 | GET_PORT(cur->ai_next, servname); |
| 720 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 721 | /* |
| 722 | * Set the numeric address itself as |
| 723 | * the canonical name, based on a |
| 724 | * clarification in rfc2553bis-03. |
| 725 | */ |
| 726 | GET_CANONNAME(cur->ai_next, canonname); |
| 727 | } |
| 728 | while (cur->ai_next) cur = cur->ai_next; |
| 729 | } else |
| 730 | ERR(EAI_FAMILY); /*xxx*/ |
| 731 | } |
| 732 | break; |
| 733 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 734 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 735 | *res = sentinel.ai_next; |
| 736 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 737 | |
| 738 | free: |
| 739 | bad: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 740 | if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next); |
| 741 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /* |
| 745 | * numeric hostname with scope |
| 746 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 747 | static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname, |
| 748 | const char* servname, struct addrinfo** res) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 749 | #if !defined(SCOPE_DELIMITER) || !defined(INET6) |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 750 | return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 751 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 752 | const struct afd* afd; |
| 753 | struct addrinfo* cur; |
| 754 | int error; |
| 755 | char *cp, *hostname2 = NULL, *scope, *addr; |
| 756 | struct sockaddr_in6* sin6; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 757 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 758 | assert(pai != NULL); |
| 759 | /* hostname may be NULL */ |
| 760 | /* servname may be NULL */ |
| 761 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 762 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 763 | /* |
| 764 | * if the servname does not match socktype/protocol, ignore it. |
| 765 | */ |
| 766 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 767 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 768 | afd = find_afd(pai->ai_family); |
| 769 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 770 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 771 | if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 772 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 773 | cp = strchr(hostname, SCOPE_DELIMITER); |
| 774 | if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 775 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 776 | /* |
| 777 | * Handle special case of <scoped_address><delimiter><scope id> |
| 778 | */ |
| 779 | hostname2 = strdup(hostname); |
| 780 | if (hostname2 == NULL) return EAI_MEMORY; |
| 781 | /* terminate at the delimiter */ |
| 782 | hostname2[cp - hostname] = '\0'; |
| 783 | addr = hostname2; |
| 784 | scope = cp + 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 785 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 786 | error = explore_numeric(pai, addr, servname, res, hostname); |
| 787 | if (error == 0) { |
| 788 | u_int32_t scopeid; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 789 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 790 | for (cur = *res; cur; cur = cur->ai_next) { |
| 791 | if (cur->ai_family != AF_INET6) continue; |
| 792 | sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr; |
| 793 | if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { |
| 794 | free(hostname2); |
| 795 | return (EAI_NODATA); /* XXX: is return OK? */ |
| 796 | } |
| 797 | sin6->sin6_scope_id = scopeid; |
| 798 | } |
| 799 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 800 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 801 | free(hostname2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 802 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 803 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 804 | #endif |
| 805 | } |
| 806 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 807 | static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) { |
| 808 | assert(pai != NULL); |
| 809 | assert(ai != NULL); |
| 810 | assert(str != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 811 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 812 | if ((pai->ai_flags & AI_CANONNAME) != 0) { |
| 813 | ai->ai_canonname = strdup(str); |
| 814 | if (ai->ai_canonname == NULL) return EAI_MEMORY; |
| 815 | } |
| 816 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 817 | } |
| 818 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 819 | static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd, |
| 820 | const char* addr) { |
| 821 | char* p; |
| 822 | struct addrinfo* ai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 823 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 824 | assert(pai != NULL); |
| 825 | assert(afd != NULL); |
| 826 | assert(addr != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 827 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 828 | ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen)); |
| 829 | if (ai == NULL) return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 830 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 831 | memcpy(ai, pai, sizeof(struct addrinfo)); |
| 832 | ai->ai_addr = (struct sockaddr*) (void*) (ai + 1); |
| 833 | memset(ai->ai_addr, 0, (size_t) afd->a_socklen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 834 | |
| 835 | #ifdef HAVE_SA_LEN |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 836 | ai->ai_addr->sa_len = afd->a_socklen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 837 | #endif |
| 838 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 839 | ai->ai_addrlen = afd->a_socklen; |
| 840 | #if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__) |
| 841 | ai->__ai_pad0 = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 842 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 843 | ai->ai_addr->sa_family = ai->ai_family = afd->a_af; |
| 844 | p = (char*) (void*) (ai->ai_addr); |
| 845 | memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen); |
| 846 | return ai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 847 | } |
| 848 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 849 | static int get_portmatch(const struct addrinfo* ai, const char* servname) { |
| 850 | assert(ai != NULL); |
| 851 | /* servname may be NULL */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 852 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 853 | return get_port(ai, servname, 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 854 | } |
| 855 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 856 | static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) { |
| 857 | const char* proto; |
| 858 | struct servent* sp; |
| 859 | int port; |
| 860 | int allownumeric; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 861 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 862 | assert(ai != NULL); |
| 863 | /* servname may be NULL */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 864 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 865 | if (servname == NULL) return 0; |
| 866 | switch (ai->ai_family) { |
| 867 | case AF_INET: |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 868 | #ifdef AF_INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 869 | case AF_INET6: |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 870 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 871 | break; |
| 872 | default: |
| 873 | return 0; |
| 874 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 875 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 876 | switch (ai->ai_socktype) { |
| 877 | case SOCK_RAW: |
| 878 | return EAI_SERVICE; |
| 879 | case SOCK_DGRAM: |
| 880 | case SOCK_STREAM: |
| 881 | allownumeric = 1; |
| 882 | break; |
| 883 | case ANY: |
| 884 | #if 1 /* ANDROID-SPECIFIC CHANGE TO MATCH GLIBC */ |
| 885 | allownumeric = 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 886 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 887 | allownumeric = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 888 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 889 | break; |
| 890 | default: |
| 891 | return EAI_SOCKTYPE; |
| 892 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 893 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 894 | port = str2number(servname); |
| 895 | if (port >= 0) { |
| 896 | if (!allownumeric) return EAI_SERVICE; |
| 897 | if (port < 0 || port > 65535) return EAI_SERVICE; |
| 898 | port = htons(port); |
| 899 | } else { |
| 900 | if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 901 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 902 | switch (ai->ai_socktype) { |
| 903 | case SOCK_DGRAM: |
| 904 | proto = "udp"; |
| 905 | break; |
| 906 | case SOCK_STREAM: |
| 907 | proto = "tcp"; |
| 908 | break; |
| 909 | default: |
| 910 | proto = NULL; |
| 911 | break; |
| 912 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 913 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 914 | if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE; |
| 915 | port = sp->s_port; |
| 916 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 917 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 918 | if (!matchonly) { |
| 919 | switch (ai->ai_family) { |
| 920 | case AF_INET: |
| 921 | ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port; |
| 922 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 923 | #ifdef INET6 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 924 | case AF_INET6: |
| 925 | ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port; |
| 926 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 927 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 928 | } |
| 929 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 930 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 931 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 932 | } |
| 933 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 934 | static const struct afd* find_afd(int af) { |
| 935 | const struct afd* afd; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 936 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 937 | if (af == PF_UNSPEC) return NULL; |
| 938 | for (afd = afdl; afd->a_af; afd++) { |
| 939 | if (afd->a_af == af) return afd; |
| 940 | } |
| 941 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | #ifdef INET6 |
| 945 | /* convert a string to a scope identifier. XXX: IPv6 specific */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 946 | static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) { |
| 947 | u_long lscopeid; |
| 948 | struct in6_addr* a6; |
| 949 | char* ep; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 950 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 951 | assert(scope != NULL); |
| 952 | assert(sin6 != NULL); |
| 953 | assert(scopeid != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 954 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 955 | a6 = &sin6->sin6_addr; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 956 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 957 | /* empty scopeid portion is invalid */ |
| 958 | if (*scope == '\0') return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 959 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 960 | if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { |
| 961 | /* |
| 962 | * We currently assume a one-to-one mapping between links |
| 963 | * and interfaces, so we simply use interface indices for |
| 964 | * like-local scopes. |
| 965 | */ |
| 966 | *scopeid = if_nametoindex(scope); |
| 967 | if (*scopeid == 0) goto trynumeric; |
| 968 | return 0; |
| 969 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 970 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 971 | /* still unclear about literal, allow numeric only - placeholder */ |
| 972 | if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric; |
| 973 | if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) |
| 974 | goto trynumeric; |
| 975 | else |
| 976 | goto trynumeric; /* global */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 977 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 978 | /* try to convert to a numeric id as a last resort */ |
| 979 | trynumeric: |
| 980 | errno = 0; |
| 981 | lscopeid = strtoul(scope, &ep, 10); |
| 982 | *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); |
| 983 | if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) |
| 984 | return 0; |
| 985 | else |
| 986 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 987 | } |
| 988 | #endif |
| 989 | |
| 990 | /* code duplicate with gethnamaddr.c */ |
| 991 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 992 | static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 993 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 994 | #define BOUNDED_INCR(x) \ |
| 995 | do { \ |
| 996 | BOUNDS_CHECK(cp, x); \ |
| 997 | cp += (x); \ |
| 998 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 999 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1000 | #define BOUNDS_CHECK(ptr, count) \ |
| 1001 | do { \ |
| 1002 | if (eom - (ptr) < (count)) { \ |
| 1003 | h_errno = NO_RECOVERY; \ |
| 1004 | return NULL; \ |
| 1005 | } \ |
| 1006 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1007 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1008 | static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype, |
| 1009 | const struct addrinfo* pai) { |
| 1010 | struct addrinfo sentinel, *cur; |
| 1011 | struct addrinfo ai; |
| 1012 | const struct afd* afd; |
| 1013 | char* canonname; |
| 1014 | const HEADER* hp; |
| 1015 | const u_char* cp; |
| 1016 | int n; |
| 1017 | const u_char* eom; |
| 1018 | char *bp, *ep; |
| 1019 | int type, class, ancount, qdcount; |
| 1020 | int haveanswer, had_error; |
| 1021 | char tbuf[MAXDNAME]; |
| 1022 | int (*name_ok)(const char*); |
| 1023 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1024 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1025 | assert(answer != NULL); |
| 1026 | assert(qname != NULL); |
| 1027 | assert(pai != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1028 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1029 | memset(&sentinel, 0, sizeof(sentinel)); |
| 1030 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1031 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1032 | canonname = NULL; |
| 1033 | eom = answer->buf + anslen; |
| 1034 | switch (qtype) { |
| 1035 | case T_A: |
| 1036 | case T_AAAA: |
| 1037 | case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ |
| 1038 | name_ok = res_hnok; |
| 1039 | break; |
| 1040 | default: |
| 1041 | return NULL; /* XXX should be abort(); */ |
| 1042 | } |
| 1043 | /* |
| 1044 | * find first satisfactory answer |
| 1045 | */ |
| 1046 | hp = &answer->hdr; |
| 1047 | ancount = ntohs(hp->ancount); |
| 1048 | qdcount = ntohs(hp->qdcount); |
| 1049 | bp = hostbuf; |
| 1050 | ep = hostbuf + sizeof hostbuf; |
| 1051 | cp = answer->buf; |
| 1052 | BOUNDED_INCR(HFIXEDSZ); |
| 1053 | if (qdcount != 1) { |
| 1054 | h_errno = NO_RECOVERY; |
| 1055 | return (NULL); |
| 1056 | } |
| 1057 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 1058 | if ((n < 0) || !(*name_ok)(bp)) { |
| 1059 | h_errno = NO_RECOVERY; |
| 1060 | return (NULL); |
| 1061 | } |
| 1062 | BOUNDED_INCR(n + QFIXEDSZ); |
| 1063 | if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { |
| 1064 | /* res_send() has already verified that the query name is the |
| 1065 | * same as the one we sent; this just gets the expanded name |
| 1066 | * (i.e., with the succeeding search-domain tacked on). |
| 1067 | */ |
| 1068 | n = strlen(bp) + 1; /* for the \0 */ |
| 1069 | if (n >= MAXHOSTNAMELEN) { |
| 1070 | h_errno = NO_RECOVERY; |
| 1071 | return (NULL); |
| 1072 | } |
| 1073 | canonname = bp; |
| 1074 | bp += n; |
| 1075 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 1076 | qname = canonname; |
| 1077 | } |
| 1078 | haveanswer = 0; |
| 1079 | had_error = 0; |
| 1080 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 1081 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 1082 | if ((n < 0) || !(*name_ok)(bp)) { |
| 1083 | had_error++; |
| 1084 | continue; |
| 1085 | } |
| 1086 | cp += n; /* name */ |
| 1087 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
| 1088 | type = _getshort(cp); |
| 1089 | cp += INT16SZ; /* type */ |
| 1090 | class = _getshort(cp); |
| 1091 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
| 1092 | n = _getshort(cp); |
| 1093 | cp += INT16SZ; /* len */ |
| 1094 | BOUNDS_CHECK(cp, n); |
| 1095 | if (class != C_IN) { |
| 1096 | /* XXX - debug? syslog? */ |
| 1097 | cp += n; |
| 1098 | continue; /* XXX - had_error++ ? */ |
| 1099 | } |
| 1100 | if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) { |
| 1101 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 1102 | if ((n < 0) || !(*name_ok)(tbuf)) { |
| 1103 | had_error++; |
| 1104 | continue; |
| 1105 | } |
| 1106 | cp += n; |
| 1107 | /* Get canonical name. */ |
| 1108 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 1109 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 1110 | had_error++; |
| 1111 | continue; |
| 1112 | } |
| 1113 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 1114 | canonname = bp; |
| 1115 | bp += n; |
| 1116 | continue; |
| 1117 | } |
| 1118 | if (qtype == T_ANY) { |
| 1119 | if (!(type == T_A || type == T_AAAA)) { |
| 1120 | cp += n; |
| 1121 | continue; |
| 1122 | } |
| 1123 | } else if (type != qtype) { |
| 1124 | if (type != T_KEY && type != T_SIG) |
| 1125 | syslog(LOG_NOTICE | LOG_AUTH, |
| 1126 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname, |
| 1127 | p_class(C_IN), p_type(qtype), p_type(type)); |
| 1128 | cp += n; |
| 1129 | continue; /* XXX - had_error++ ? */ |
| 1130 | } |
| 1131 | switch (type) { |
| 1132 | case T_A: |
| 1133 | case T_AAAA: |
| 1134 | if (strcasecmp(canonname, bp) != 0) { |
| 1135 | syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp); |
| 1136 | cp += n; |
| 1137 | continue; /* XXX - had_error++ ? */ |
| 1138 | } |
| 1139 | if (type == T_A && n != INADDRSZ) { |
| 1140 | cp += n; |
| 1141 | continue; |
| 1142 | } |
| 1143 | if (type == T_AAAA && n != IN6ADDRSZ) { |
| 1144 | cp += n; |
| 1145 | continue; |
| 1146 | } |
| 1147 | if (type == T_AAAA) { |
| 1148 | struct in6_addr in6; |
| 1149 | memcpy(&in6, cp, IN6ADDRSZ); |
| 1150 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 1151 | cp += n; |
| 1152 | continue; |
| 1153 | } |
| 1154 | } |
| 1155 | if (!haveanswer) { |
| 1156 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1157 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1158 | canonname = bp; |
| 1159 | nn = strlen(bp) + 1; /* for the \0 */ |
| 1160 | bp += nn; |
| 1161 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1162 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1163 | /* don't overwrite pai */ |
| 1164 | ai = *pai; |
| 1165 | ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; |
| 1166 | afd = find_afd(ai.ai_family); |
| 1167 | if (afd == NULL) { |
| 1168 | cp += n; |
| 1169 | continue; |
| 1170 | } |
| 1171 | cur->ai_next = get_ai(&ai, afd, (const char*) cp); |
| 1172 | if (cur->ai_next == NULL) had_error++; |
| 1173 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1174 | cp += n; |
| 1175 | break; |
| 1176 | default: |
| 1177 | abort(); |
| 1178 | } |
| 1179 | if (!had_error) haveanswer++; |
| 1180 | } |
| 1181 | if (haveanswer) { |
| 1182 | if (!canonname) |
| 1183 | (void) get_canonname(pai, sentinel.ai_next, qname); |
| 1184 | else |
| 1185 | (void) get_canonname(pai, sentinel.ai_next, canonname); |
| 1186 | h_errno = NETDB_SUCCESS; |
| 1187 | return sentinel.ai_next; |
| 1188 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1189 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1190 | h_errno = NO_RECOVERY; |
| 1191 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | struct addrinfo_sort_elem { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1195 | struct addrinfo* ai; |
| 1196 | int has_src_addr; |
| 1197 | sockaddr_union src_addr; |
| 1198 | int original_order; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1199 | }; |
| 1200 | |
| 1201 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1202 | static int _get_scope(const struct sockaddr* addr) { |
| 1203 | if (addr->sa_family == AF_INET6) { |
| 1204 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1205 | if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) { |
| 1206 | return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr); |
| 1207 | } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) || |
| 1208 | IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) { |
| 1209 | /* |
| 1210 | * RFC 4291 section 2.5.3 says loopback is to be treated as having |
| 1211 | * link-local scope. |
| 1212 | */ |
| 1213 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1214 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1215 | return IPV6_ADDR_SCOPE_SITELOCAL; |
| 1216 | } else { |
| 1217 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1218 | } |
| 1219 | } else if (addr->sa_family == AF_INET) { |
| 1220 | const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr; |
| 1221 | unsigned long int na = ntohl(addr4->sin_addr.s_addr); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1222 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1223 | if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */ |
| 1224 | (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */ |
| 1225 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1226 | } else { |
| 1227 | /* |
| 1228 | * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses |
| 1229 | * and shared addresses (100.64.0.0/10), are assigned global scope. |
| 1230 | */ |
| 1231 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1232 | } |
| 1233 | } else { |
| 1234 | /* |
| 1235 | * This should never happen. |
| 1236 | * Return a scope with low priority as a last resort. |
| 1237 | */ |
| 1238 | return IPV6_ADDR_SCOPE_NODELOCAL; |
| 1239 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /* These macros are modelled after the ones in <netinet/in6.h>. */ |
| 1243 | |
| 1244 | /* RFC 4380, section 2.6 */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1245 | #define IN6_IS_ADDR_TEREDO(a) \ |
| 1246 | ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000))) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1247 | |
| 1248 | /* RFC 3056, section 2. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1249 | #define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02)) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1250 | |
| 1251 | /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1252 | #define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe)) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1253 | |
| 1254 | /* |
| 1255 | * Get the label for a given IPv4/IPv6 address. |
| 1256 | * RFC 6724, section 2.1. |
| 1257 | */ |
| 1258 | |
| 1259 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1260 | static int _get_label(const struct sockaddr* addr) { |
| 1261 | if (addr->sa_family == AF_INET) { |
| 1262 | return 4; |
| 1263 | } else if (addr->sa_family == AF_INET6) { |
| 1264 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1265 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1266 | return 0; |
| 1267 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1268 | return 4; |
| 1269 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1270 | return 2; |
| 1271 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1272 | return 5; |
| 1273 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1274 | return 13; |
| 1275 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) { |
| 1276 | return 3; |
| 1277 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1278 | return 11; |
| 1279 | } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1280 | return 12; |
| 1281 | } else { |
| 1282 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1283 | return 1; |
| 1284 | } |
| 1285 | } else { |
| 1286 | /* |
| 1287 | * This should never happen. |
| 1288 | * Return a semi-random label as a last resort. |
| 1289 | */ |
| 1290 | return 1; |
| 1291 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Get the precedence for a given IPv4/IPv6 address. |
| 1296 | * RFC 6724, section 2.1. |
| 1297 | */ |
| 1298 | |
| 1299 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1300 | static int _get_precedence(const struct sockaddr* addr) { |
| 1301 | if (addr->sa_family == AF_INET) { |
| 1302 | return 35; |
| 1303 | } else if (addr->sa_family == AF_INET6) { |
| 1304 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1305 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1306 | return 50; |
| 1307 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1308 | return 35; |
| 1309 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1310 | return 30; |
| 1311 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1312 | return 5; |
| 1313 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1314 | return 3; |
| 1315 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) || |
| 1316 | IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) || |
| 1317 | IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1318 | return 1; |
| 1319 | } else { |
| 1320 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1321 | return 40; |
| 1322 | } |
| 1323 | } else { |
| 1324 | return 1; |
| 1325 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Find number of matching initial bits between the two addresses a1 and a2. |
| 1330 | */ |
| 1331 | |
| 1332 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1333 | static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) { |
| 1334 | const char* p1 = (const char*) a1; |
| 1335 | const char* p2 = (const char*) a2; |
| 1336 | unsigned i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1337 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1338 | for (i = 0; i < sizeof(*a1); ++i) { |
| 1339 | int x, j; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1340 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1341 | if (p1[i] == p2[i]) { |
| 1342 | continue; |
| 1343 | } |
| 1344 | x = p1[i] ^ p2[i]; |
| 1345 | for (j = 0; j < CHAR_BIT; ++j) { |
| 1346 | if (x & (1 << (CHAR_BIT - 1))) { |
| 1347 | return i * CHAR_BIT + j; |
| 1348 | } |
| 1349 | x <<= 1; |
| 1350 | } |
| 1351 | } |
| 1352 | return sizeof(*a1) * CHAR_BIT; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * Compare two source/destination address pairs. |
| 1357 | * RFC 6724, section 6. |
| 1358 | */ |
| 1359 | |
| 1360 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1361 | static int _rfc6724_compare(const void* ptr1, const void* ptr2) { |
| 1362 | const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1; |
| 1363 | const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2; |
| 1364 | int scope_src1, scope_dst1, scope_match1; |
| 1365 | int scope_src2, scope_dst2, scope_match2; |
| 1366 | int label_src1, label_dst1, label_match1; |
| 1367 | int label_src2, label_dst2, label_match2; |
| 1368 | int precedence1, precedence2; |
| 1369 | int prefixlen1, prefixlen2; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1370 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1371 | /* Rule 1: Avoid unusable destinations. */ |
| 1372 | if (a1->has_src_addr != a2->has_src_addr) { |
| 1373 | return a2->has_src_addr - a1->has_src_addr; |
| 1374 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1375 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1376 | /* Rule 2: Prefer matching scope. */ |
| 1377 | scope_src1 = _get_scope(&a1->src_addr.generic); |
| 1378 | scope_dst1 = _get_scope(a1->ai->ai_addr); |
| 1379 | scope_match1 = (scope_src1 == scope_dst1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1380 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1381 | scope_src2 = _get_scope(&a2->src_addr.generic); |
| 1382 | scope_dst2 = _get_scope(a2->ai->ai_addr); |
| 1383 | scope_match2 = (scope_src2 == scope_dst2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1384 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1385 | if (scope_match1 != scope_match2) { |
| 1386 | return scope_match2 - scope_match1; |
| 1387 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1388 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1389 | /* |
| 1390 | * Rule 3: Avoid deprecated addresses. |
| 1391 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1392 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1393 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1394 | /* |
| 1395 | * Rule 4: Prefer home addresses. |
| 1396 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1397 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1398 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1399 | /* Rule 5: Prefer matching label. */ |
| 1400 | label_src1 = _get_label(&a1->src_addr.generic); |
| 1401 | label_dst1 = _get_label(a1->ai->ai_addr); |
| 1402 | label_match1 = (label_src1 == label_dst1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1403 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1404 | label_src2 = _get_label(&a2->src_addr.generic); |
| 1405 | label_dst2 = _get_label(a2->ai->ai_addr); |
| 1406 | label_match2 = (label_src2 == label_dst2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1407 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1408 | if (label_match1 != label_match2) { |
| 1409 | return label_match2 - label_match1; |
| 1410 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1411 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1412 | /* Rule 6: Prefer higher precedence. */ |
| 1413 | precedence1 = _get_precedence(a1->ai->ai_addr); |
| 1414 | precedence2 = _get_precedence(a2->ai->ai_addr); |
| 1415 | if (precedence1 != precedence2) { |
| 1416 | return precedence2 - precedence1; |
| 1417 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1418 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1419 | /* |
| 1420 | * Rule 7: Prefer native transport. |
| 1421 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1422 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1423 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1424 | /* Rule 8: Prefer smaller scope. */ |
| 1425 | if (scope_dst1 != scope_dst2) { |
| 1426 | return scope_dst1 - scope_dst2; |
| 1427 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1428 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1429 | /* |
| 1430 | * Rule 9: Use longest matching prefix. |
| 1431 | * We implement this for IPv6 only, as the rules in RFC 6724 don't seem |
| 1432 | * to work very well directly applied to IPv4. (glibc uses information from |
| 1433 | * the routing table for a custom IPv4 implementation here.) |
| 1434 | */ |
| 1435 | if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr && |
| 1436 | a2->ai->ai_addr->sa_family == AF_INET6) { |
| 1437 | const struct sockaddr_in6* a1_src = &a1->src_addr.in6; |
| 1438 | const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr; |
| 1439 | const struct sockaddr_in6* a2_src = &a2->src_addr.in6; |
| 1440 | const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr; |
| 1441 | prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr); |
| 1442 | prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr); |
| 1443 | if (prefixlen1 != prefixlen2) { |
| 1444 | return prefixlen2 - prefixlen1; |
| 1445 | } |
| 1446 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1447 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1448 | /* |
| 1449 | * Rule 10: Leave the order unchanged. |
| 1450 | * We need this since qsort() is not necessarily stable. |
| 1451 | */ |
| 1452 | return a1->original_order - a2->original_order; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | * Find the source address that will be used if trying to connect to the given |
| 1457 | * address. src_addr must be large enough to hold a struct sockaddr_in6. |
| 1458 | * |
| 1459 | * Returns 1 if a source address was found, 0 if the address is unreachable, |
| 1460 | * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are |
| 1461 | * undefined. |
| 1462 | */ |
| 1463 | |
| 1464 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1465 | static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark, |
| 1466 | uid_t uid) { |
| 1467 | int sock; |
| 1468 | int ret; |
| 1469 | socklen_t len; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1470 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1471 | switch (addr->sa_family) { |
| 1472 | case AF_INET: |
| 1473 | len = sizeof(struct sockaddr_in); |
| 1474 | break; |
| 1475 | case AF_INET6: |
| 1476 | len = sizeof(struct sockaddr_in6); |
| 1477 | break; |
| 1478 | default: |
| 1479 | /* No known usable source address for non-INET families. */ |
| 1480 | return 0; |
| 1481 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1482 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1483 | sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); |
| 1484 | if (sock == -1) { |
| 1485 | if (errno == EAFNOSUPPORT) { |
| 1486 | return 0; |
| 1487 | } else { |
| 1488 | return -1; |
| 1489 | } |
| 1490 | } |
| 1491 | if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) { |
| 1492 | close(sock); |
| 1493 | return 0; |
| 1494 | } |
| 1495 | if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) { |
| 1496 | close(sock); |
| 1497 | return 0; |
| 1498 | } |
| 1499 | do { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame^] | 1500 | ret = connect(sock, addr, len); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1501 | } while (ret == -1 && errno == EINTR); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1502 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1503 | if (ret == -1) { |
| 1504 | close(sock); |
| 1505 | return 0; |
| 1506 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1507 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1508 | if (src_addr && getsockname(sock, src_addr, &len) == -1) { |
| 1509 | close(sock); |
| 1510 | return -1; |
| 1511 | } |
| 1512 | close(sock); |
| 1513 | return 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /* |
| 1517 | * Sort the linked list starting at sentinel->ai_next in RFC6724 order. |
| 1518 | * Will leave the list unchanged if an error occurs. |
| 1519 | */ |
| 1520 | |
| 1521 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1522 | static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) { |
| 1523 | struct addrinfo* cur; |
| 1524 | int nelem = 0, i; |
| 1525 | struct addrinfo_sort_elem* elems; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1526 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1527 | cur = list_sentinel->ai_next; |
| 1528 | while (cur) { |
| 1529 | ++nelem; |
| 1530 | cur = cur->ai_next; |
| 1531 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1532 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1533 | elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem)); |
| 1534 | if (elems == NULL) { |
| 1535 | goto error; |
| 1536 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1537 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1538 | /* |
| 1539 | * Convert the linked list to an array that also contains the candidate |
| 1540 | * source address for each destination address. |
| 1541 | */ |
| 1542 | for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) { |
| 1543 | int has_src_addr; |
| 1544 | assert(cur != NULL); |
| 1545 | elems[i].ai = cur; |
| 1546 | elems[i].original_order = i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1547 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1548 | has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid); |
| 1549 | if (has_src_addr == -1) { |
| 1550 | goto error; |
| 1551 | } |
| 1552 | elems[i].has_src_addr = has_src_addr; |
| 1553 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1554 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1555 | /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */ |
| 1556 | qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1557 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1558 | list_sentinel->ai_next = elems[0].ai; |
| 1559 | for (i = 0; i < nelem - 1; ++i) { |
| 1560 | elems[i].ai->ai_next = elems[i + 1].ai; |
| 1561 | } |
| 1562 | elems[nelem - 1].ai->ai_next = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1563 | |
| 1564 | error: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1565 | free(elems); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1569 | static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) { |
| 1570 | struct addrinfo* ai; |
| 1571 | querybuf *buf, *buf2; |
| 1572 | const char* name; |
| 1573 | const struct addrinfo* pai; |
| 1574 | struct addrinfo sentinel, *cur; |
| 1575 | struct res_target q, q2; |
| 1576 | res_state res; |
| 1577 | const struct android_net_context* netcontext; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1578 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1579 | name = va_arg(ap, char*); |
| 1580 | pai = va_arg(ap, const struct addrinfo*); |
| 1581 | netcontext = va_arg(ap, const struct android_net_context*); |
| 1582 | // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1583 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1584 | memset(&q, 0, sizeof(q)); |
| 1585 | memset(&q2, 0, sizeof(q2)); |
| 1586 | memset(&sentinel, 0, sizeof(sentinel)); |
| 1587 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1588 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1589 | buf = malloc(sizeof(*buf)); |
| 1590 | if (buf == NULL) { |
| 1591 | h_errno = NETDB_INTERNAL; |
| 1592 | return NS_NOTFOUND; |
| 1593 | } |
| 1594 | buf2 = malloc(sizeof(*buf2)); |
| 1595 | if (buf2 == NULL) { |
| 1596 | free(buf); |
| 1597 | h_errno = NETDB_INTERNAL; |
| 1598 | return NS_NOTFOUND; |
| 1599 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1600 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1601 | switch (pai->ai_family) { |
| 1602 | case AF_UNSPEC: |
| 1603 | /* prefer IPv6 */ |
| 1604 | q.name = name; |
| 1605 | q.qclass = C_IN; |
| 1606 | q.answer = buf->buf; |
| 1607 | q.anslen = sizeof(buf->buf); |
| 1608 | int query_ipv6 = 1, query_ipv4 = 1; |
| 1609 | if (pai->ai_flags & AI_ADDRCONFIG) { |
| 1610 | query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid); |
| 1611 | query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid); |
| 1612 | } |
| 1613 | if (query_ipv6) { |
| 1614 | q.qtype = T_AAAA; |
| 1615 | if (query_ipv4) { |
| 1616 | q.next = &q2; |
| 1617 | q2.name = name; |
| 1618 | q2.qclass = C_IN; |
| 1619 | q2.qtype = T_A; |
| 1620 | q2.answer = buf2->buf; |
| 1621 | q2.anslen = sizeof(buf2->buf); |
| 1622 | } |
| 1623 | } else if (query_ipv4) { |
| 1624 | q.qtype = T_A; |
| 1625 | } else { |
| 1626 | free(buf); |
| 1627 | free(buf2); |
| 1628 | return NS_NOTFOUND; |
| 1629 | } |
| 1630 | break; |
| 1631 | case AF_INET: |
| 1632 | q.name = name; |
| 1633 | q.qclass = C_IN; |
| 1634 | q.qtype = T_A; |
| 1635 | q.answer = buf->buf; |
| 1636 | q.anslen = sizeof(buf->buf); |
| 1637 | break; |
| 1638 | case AF_INET6: |
| 1639 | q.name = name; |
| 1640 | q.qclass = C_IN; |
| 1641 | q.qtype = T_AAAA; |
| 1642 | q.answer = buf->buf; |
| 1643 | q.anslen = sizeof(buf->buf); |
| 1644 | break; |
| 1645 | default: |
| 1646 | free(buf); |
| 1647 | free(buf2); |
| 1648 | return NS_UNAVAIL; |
| 1649 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1650 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1651 | res = __res_get_state(); |
| 1652 | if (res == NULL) { |
| 1653 | free(buf); |
| 1654 | free(buf2); |
| 1655 | return NS_NOTFOUND; |
| 1656 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1657 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1658 | /* this just sets our netid val in the thread private data so we don't have to |
| 1659 | * modify the api's all the way down to res_send.c's res_nsend. We could |
| 1660 | * fully populate the thread private data here, but if we get down there |
| 1661 | * and have a cache hit that would be wasted, so we do the rest there on miss |
| 1662 | */ |
| 1663 | res_setnetcontext(res, netcontext); |
| 1664 | if (res_searchN(name, &q, res) < 0) { |
| 1665 | __res_put_state(res); |
| 1666 | free(buf); |
| 1667 | free(buf2); |
| 1668 | return NS_NOTFOUND; |
| 1669 | } |
| 1670 | ai = getanswer(buf, q.n, q.name, q.qtype, pai); |
| 1671 | if (ai) { |
| 1672 | cur->ai_next = ai; |
| 1673 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1674 | } |
| 1675 | if (q.next) { |
| 1676 | ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); |
| 1677 | if (ai) cur->ai_next = ai; |
| 1678 | } |
| 1679 | free(buf); |
| 1680 | free(buf2); |
| 1681 | if (sentinel.ai_next == NULL) { |
| 1682 | __res_put_state(res); |
| 1683 | switch (h_errno) { |
| 1684 | case HOST_NOT_FOUND: |
| 1685 | return NS_NOTFOUND; |
| 1686 | case TRY_AGAIN: |
| 1687 | return NS_TRYAGAIN; |
| 1688 | default: |
| 1689 | return NS_UNAVAIL; |
| 1690 | } |
| 1691 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1692 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1693 | _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1694 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1695 | __res_put_state(res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1696 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1697 | *((struct addrinfo**) rv) = sentinel.ai_next; |
| 1698 | return NS_SUCCESS; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1699 | } |
| 1700 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1701 | static void _sethtent(FILE** hostf) { |
| 1702 | if (!*hostf) |
| 1703 | *hostf = fopen(_PATH_HOSTS, "re"); |
| 1704 | else |
| 1705 | rewind(*hostf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1706 | } |
| 1707 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1708 | static void _endhtent(FILE** hostf) { |
| 1709 | if (*hostf) { |
| 1710 | (void) fclose(*hostf); |
| 1711 | *hostf = NULL; |
| 1712 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1713 | } |
| 1714 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1715 | static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) { |
| 1716 | char* p; |
| 1717 | char *cp, *tname, *cname; |
| 1718 | struct addrinfo hints, *res0, *res; |
| 1719 | int error; |
| 1720 | const char* addr; |
| 1721 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1722 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1723 | // fprintf(stderr, "_gethtent() name = '%s'\n", name); |
| 1724 | assert(name != NULL); |
| 1725 | assert(pai != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1726 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1727 | if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL); |
| 1728 | again: |
| 1729 | if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL); |
| 1730 | if (*p == '#') goto again; |
| 1731 | if (!(cp = strpbrk(p, "#\n"))) goto again; |
| 1732 | *cp = '\0'; |
| 1733 | if (!(cp = strpbrk(p, " \t"))) goto again; |
| 1734 | *cp++ = '\0'; |
| 1735 | addr = p; |
| 1736 | /* if this is not something we're looking for, skip it. */ |
| 1737 | cname = NULL; |
| 1738 | while (cp && *cp) { |
| 1739 | if (*cp == ' ' || *cp == '\t') { |
| 1740 | cp++; |
| 1741 | continue; |
| 1742 | } |
| 1743 | if (!cname) cname = cp; |
| 1744 | tname = cp; |
| 1745 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
| 1746 | // fprintf(stderr, "\ttname = '%s'", tname); |
| 1747 | if (strcasecmp(name, tname) == 0) goto found; |
| 1748 | } |
| 1749 | goto again; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1750 | |
| 1751 | found: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1752 | hints = *pai; |
| 1753 | hints.ai_flags = AI_NUMERICHOST; |
| 1754 | error = getaddrinfo(addr, NULL, &hints, &res0); |
| 1755 | if (error) goto again; |
| 1756 | for (res = res0; res; res = res->ai_next) { |
| 1757 | /* cover it up */ |
| 1758 | res->ai_flags = pai->ai_flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1759 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1760 | if (pai->ai_flags & AI_CANONNAME) { |
| 1761 | if (get_canonname(pai, res, cname) != 0) { |
| 1762 | freeaddrinfo(res0); |
| 1763 | goto again; |
| 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | return res0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /*ARGSUSED*/ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1771 | static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) { |
| 1772 | const char* name; |
| 1773 | const struct addrinfo* pai; |
| 1774 | struct addrinfo sentinel, *cur; |
| 1775 | struct addrinfo* p; |
| 1776 | FILE* hostf = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1777 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1778 | name = va_arg(ap, char*); |
| 1779 | pai = va_arg(ap, struct addrinfo*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1780 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1781 | // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name); |
| 1782 | memset(&sentinel, 0, sizeof(sentinel)); |
| 1783 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1784 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1785 | _sethtent(&hostf); |
| 1786 | while ((p = _gethtent(&hostf, name, pai)) != NULL) { |
| 1787 | cur->ai_next = p; |
| 1788 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1789 | } |
| 1790 | _endhtent(&hostf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1791 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1792 | *((struct addrinfo**) rv) = sentinel.ai_next; |
| 1793 | if (sentinel.ai_next == NULL) return NS_NOTFOUND; |
| 1794 | return NS_SUCCESS; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /* resolver logic */ |
| 1798 | |
| 1799 | /* |
| 1800 | * Formulate a normal query, send, and await answer. |
| 1801 | * Returned answer is placed in supplied buffer "answer". |
| 1802 | * Perform preliminary check of answer, returning success only |
| 1803 | * if no error is indicated and the answer count is nonzero. |
| 1804 | * Return the size of the response on success, -1 on error. |
| 1805 | * Error number is left in h_errno. |
| 1806 | * |
| 1807 | * Caller must parse answer and determine whether it answers the question. |
| 1808 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1809 | static int res_queryN(const char* name, /* domain name */ struct res_target* target, |
| 1810 | res_state res) { |
| 1811 | u_char buf[MAXPACKET]; |
| 1812 | HEADER* hp; |
| 1813 | int n; |
| 1814 | struct res_target* t; |
| 1815 | int rcode; |
| 1816 | int ancount; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1817 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1818 | assert(name != NULL); |
| 1819 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1820 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1821 | rcode = NOERROR; |
| 1822 | ancount = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1823 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1824 | for (t = target; t; t = t->next) { |
| 1825 | int class, type; |
| 1826 | u_char* answer; |
| 1827 | int anslen; |
| 1828 | u_int oflags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1829 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1830 | hp = (HEADER*) (void*) t->answer; |
| 1831 | oflags = res->_flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1832 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1833 | again: |
| 1834 | hp->rcode = NOERROR; /* default */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1835 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1836 | /* make it easier... */ |
| 1837 | class = t->qclass; |
| 1838 | type = t->qtype; |
| 1839 | answer = t->answer; |
| 1840 | anslen = t->anslen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1841 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1842 | if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1843 | #endif |
| 1844 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1845 | n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1846 | #ifdef RES_USE_EDNS0 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1847 | if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 && |
| 1848 | (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0) |
| 1849 | n = res_nopt(res, n, buf, sizeof(buf), anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1850 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1851 | if (n <= 0) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1852 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1853 | if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1854 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1855 | h_errno = NO_RECOVERY; |
| 1856 | return n; |
| 1857 | } |
| 1858 | n = res_nsend(res, buf, n, answer, anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1859 | #if 0 |
| 1860 | if (n < 0) { |
| 1861 | #ifdef DEBUG |
| 1862 | if (res->options & RES_DEBUG) |
| 1863 | printf(";; res_query: send error\n"); |
| 1864 | #endif |
| 1865 | h_errno = TRY_AGAIN; |
| 1866 | return n; |
| 1867 | } |
| 1868 | #endif |
| 1869 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1870 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
| 1871 | rcode = hp->rcode; /* record most recent error */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1872 | #ifdef RES_USE_EDNS0 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1873 | /* if the query choked with EDNS0, retry without EDNS0 */ |
| 1874 | if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && |
| 1875 | ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) { |
| 1876 | res->_flags |= RES_F_EDNS0ERR; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1877 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1878 | if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1879 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1880 | goto again; |
| 1881 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1882 | #endif |
| 1883 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1884 | if (res->options & RES_DEBUG) |
| 1885 | printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1886 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1887 | continue; |
| 1888 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1889 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1890 | ancount += ntohs(hp->ancount); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1891 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1892 | t->n = n; |
| 1893 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1894 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1895 | if (ancount == 0) { |
| 1896 | switch (rcode) { |
| 1897 | case NXDOMAIN: |
| 1898 | h_errno = HOST_NOT_FOUND; |
| 1899 | break; |
| 1900 | case SERVFAIL: |
| 1901 | h_errno = TRY_AGAIN; |
| 1902 | break; |
| 1903 | case NOERROR: |
| 1904 | h_errno = NO_DATA; |
| 1905 | break; |
| 1906 | case FORMERR: |
| 1907 | case NOTIMP: |
| 1908 | case REFUSED: |
| 1909 | default: |
| 1910 | h_errno = NO_RECOVERY; |
| 1911 | break; |
| 1912 | } |
| 1913 | return -1; |
| 1914 | } |
| 1915 | return ancount; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | /* |
| 1919 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 1920 | * Return the size of the response on success, -1 on error. |
| 1921 | * If enabled, implement search rules until answer or unrecoverable failure |
| 1922 | * is detected. Error code, if any, is left in h_errno. |
| 1923 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1924 | static int res_searchN(const char* name, struct res_target* target, res_state res) { |
| 1925 | const char *cp, *const *domain; |
| 1926 | HEADER* hp; |
| 1927 | u_int dots; |
| 1928 | int trailing_dot, ret, saved_herrno; |
| 1929 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1930 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1931 | assert(name != NULL); |
| 1932 | assert(target != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1933 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1934 | hp = (HEADER*) (void*) target->answer; /*XXX*/ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1935 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1936 | errno = 0; |
| 1937 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ |
| 1938 | dots = 0; |
| 1939 | for (cp = name; *cp; cp++) dots += (*cp == '.'); |
| 1940 | trailing_dot = 0; |
| 1941 | if (cp > name && *--cp == '.') trailing_dot++; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1942 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1943 | // fprintf(stderr, "res_searchN() name = '%s'\n", name); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1944 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1945 | /* |
| 1946 | * if there aren't any dots, it could be a user-level alias |
| 1947 | */ |
| 1948 | if (!dots && (cp = __hostalias(name)) != NULL) { |
| 1949 | ret = res_queryN(cp, target, res); |
| 1950 | return ret; |
| 1951 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1952 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1953 | /* |
| 1954 | * If there are dots in the name already, let's just give it a try |
| 1955 | * 'as is'. The threshold can be set with the "ndots" option. |
| 1956 | */ |
| 1957 | saved_herrno = -1; |
| 1958 | if (dots >= res->ndots) { |
| 1959 | ret = res_querydomainN(name, NULL, target, res); |
| 1960 | if (ret > 0) return (ret); |
| 1961 | saved_herrno = h_errno; |
| 1962 | tried_as_is++; |
| 1963 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1964 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1965 | /* |
| 1966 | * We do at least one level of search if |
| 1967 | * - there is no dot and RES_DEFNAME is set, or |
| 1968 | * - there is at least one dot, there is no trailing dot, |
| 1969 | * and RES_DNSRCH is set. |
| 1970 | */ |
| 1971 | if ((!dots && (res->options & RES_DEFNAMES)) || |
| 1972 | (dots && !trailing_dot && (res->options & RES_DNSRCH))) { |
| 1973 | int done = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1974 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1975 | /* Unfortunately we need to set stuff up before |
| 1976 | * the domain stuff is tried. Will have a better |
| 1977 | * fix after thread pools are used. |
| 1978 | */ |
| 1979 | _resolv_populate_res_for_net(res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1980 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1981 | for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) { |
| 1982 | ret = res_querydomainN(name, *domain, target, res); |
| 1983 | if (ret > 0) return ret; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1984 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1985 | /* |
| 1986 | * If no server present, give up. |
| 1987 | * If name isn't found in this domain, |
| 1988 | * keep trying higher domains in the search list |
| 1989 | * (if that's enabled). |
| 1990 | * On a NO_DATA error, keep trying, otherwise |
| 1991 | * a wildcard entry of another type could keep us |
| 1992 | * from finding this entry higher in the domain. |
| 1993 | * If we get some other error (negative answer or |
| 1994 | * server failure), then stop searching up, |
| 1995 | * but try the input name below in case it's |
| 1996 | * fully-qualified. |
| 1997 | */ |
| 1998 | if (errno == ECONNREFUSED) { |
| 1999 | h_errno = TRY_AGAIN; |
| 2000 | return -1; |
| 2001 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2002 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2003 | switch (h_errno) { |
| 2004 | case NO_DATA: |
| 2005 | got_nodata++; |
| 2006 | /* FALLTHROUGH */ |
| 2007 | case HOST_NOT_FOUND: |
| 2008 | /* keep trying */ |
| 2009 | break; |
| 2010 | case TRY_AGAIN: |
| 2011 | if (hp->rcode == SERVFAIL) { |
| 2012 | /* try next search element, if any */ |
| 2013 | got_servfail++; |
| 2014 | break; |
| 2015 | } |
| 2016 | /* FALLTHROUGH */ |
| 2017 | default: |
| 2018 | /* anything else implies that we're done */ |
| 2019 | done++; |
| 2020 | } |
| 2021 | /* |
| 2022 | * if we got here for some reason other than DNSRCH, |
| 2023 | * we only wanted one iteration of the loop, so stop. |
| 2024 | */ |
| 2025 | if (!(res->options & RES_DNSRCH)) done++; |
| 2026 | } |
| 2027 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2028 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2029 | /* |
| 2030 | * if we have not already tried the name "as is", do that now. |
| 2031 | * note that we do this regardless of how many dots were in the |
| 2032 | * name or whether it ends with a dot. |
| 2033 | */ |
| 2034 | if (!tried_as_is) { |
| 2035 | ret = res_querydomainN(name, NULL, target, res); |
| 2036 | if (ret > 0) return ret; |
| 2037 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2038 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2039 | /* |
| 2040 | * if we got here, we didn't satisfy the search. |
| 2041 | * if we did an initial full query, return that query's h_errno |
| 2042 | * (note that we wouldn't be here if that query had succeeded). |
| 2043 | * else if we ever got a nodata, send that back as the reason. |
| 2044 | * else send back meaningless h_errno, that being the one from |
| 2045 | * the last DNSRCH we did. |
| 2046 | */ |
| 2047 | if (saved_herrno != -1) |
| 2048 | h_errno = saved_herrno; |
| 2049 | else if (got_nodata) |
| 2050 | h_errno = NO_DATA; |
| 2051 | else if (got_servfail) |
| 2052 | h_errno = TRY_AGAIN; |
| 2053 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * Perform a call on res_query on the concatenation of name and domain, |
| 2058 | * removing a trailing dot from name if domain is NULL. |
| 2059 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2060 | static int res_querydomainN(const char* name, const char* domain, struct res_target* target, |
| 2061 | res_state res) { |
| 2062 | char nbuf[MAXDNAME]; |
| 2063 | const char* longname = nbuf; |
| 2064 | size_t n, d; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2065 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2066 | assert(name != NULL); |
| 2067 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2068 | |
| 2069 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2070 | if (res->options & RES_DEBUG) |
| 2071 | printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2072 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2073 | if (domain == NULL) { |
| 2074 | /* |
| 2075 | * Check for trailing '.'; |
| 2076 | * copy without '.' if present. |
| 2077 | */ |
| 2078 | n = strlen(name); |
| 2079 | if (n + 1 > sizeof(nbuf)) { |
| 2080 | h_errno = NO_RECOVERY; |
| 2081 | return -1; |
| 2082 | } |
| 2083 | if (n > 0 && name[--n] == '.') { |
| 2084 | strncpy(nbuf, name, n); |
| 2085 | nbuf[n] = '\0'; |
| 2086 | } else |
| 2087 | longname = name; |
| 2088 | } else { |
| 2089 | n = strlen(name); |
| 2090 | d = strlen(domain); |
| 2091 | if (n + 1 + d + 1 > sizeof(nbuf)) { |
| 2092 | h_errno = NO_RECOVERY; |
| 2093 | return -1; |
| 2094 | } |
| 2095 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 2096 | } |
| 2097 | return res_queryN(longname, target, res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2098 | } |