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