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 | 0a0870d | 2020-02-12 20:41:10 +0800 | [diff] [blame] | 56 | #include <future> |
| 57 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 58 | #include <android-base/logging.h> |
| 59 | |
Luke Huang | f40df9c | 2020-04-21 08:51:48 +0800 | [diff] [blame^] | 60 | #include "Experiments.h" |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 61 | #include "netd_resolv/resolv.h" |
Bernie Innocenti | 10a9028 | 2020-01-23 23:28:00 +0900 | [diff] [blame] | 62 | #include "res_comp.h" |
| 63 | #include "res_debug.h" |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 64 | #include "res_init.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 | |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 154 | static int res_queryN(const char* name, res_target* target, res_state res, int* herrno); |
| 155 | static int res_searchN(const char* name, res_target* target, res_state res, int* herrno); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 156 | static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state 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: |
| 734 | allownumeric = 1; |
| 735 | break; |
| 736 | case ANY: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 737 | allownumeric = 1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 738 | break; |
| 739 | default: |
| 740 | return EAI_SOCKTYPE; |
| 741 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 742 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 743 | port = str2number(servname); |
| 744 | if (port >= 0) { |
| 745 | if (!allownumeric) return EAI_SERVICE; |
| 746 | if (port < 0 || port > 65535) return EAI_SERVICE; |
| 747 | port = htons(port); |
| 748 | } else { |
| 749 | if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 750 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 751 | switch (ai->ai_socktype) { |
| 752 | case SOCK_DGRAM: |
| 753 | proto = "udp"; |
| 754 | break; |
| 755 | case SOCK_STREAM: |
| 756 | proto = "tcp"; |
| 757 | break; |
| 758 | default: |
| 759 | proto = NULL; |
| 760 | break; |
| 761 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 762 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 763 | if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE; |
| 764 | port = sp->s_port; |
| 765 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 766 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 767 | if (!matchonly) { |
| 768 | switch (ai->ai_family) { |
| 769 | case AF_INET: |
| 770 | ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port; |
| 771 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 772 | case AF_INET6: |
| 773 | ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port; |
| 774 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 775 | } |
| 776 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 777 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 778 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 779 | } |
| 780 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 781 | static const struct afd* find_afd(int af) { |
| 782 | const struct afd* afd; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 783 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 784 | if (af == PF_UNSPEC) return NULL; |
| 785 | for (afd = afdl; afd->a_af; afd++) { |
| 786 | if (afd->a_af == af) return afd; |
| 787 | } |
| 788 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 789 | } |
| 790 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 791 | // Convert a string to a scope identifier. |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 792 | static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, uint32_t* scopeid) { |
| 793 | uint64_t lscopeid; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 794 | struct in6_addr* a6; |
| 795 | char* ep; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 796 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 797 | assert(scope != NULL); |
| 798 | assert(sin6 != NULL); |
| 799 | assert(scopeid != NULL); |
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 | a6 = &sin6->sin6_addr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 802 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 803 | /* empty scopeid portion is invalid */ |
| 804 | if (*scope == '\0') return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 805 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 806 | if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { |
| 807 | /* |
| 808 | * We currently assume a one-to-one mapping between links |
| 809 | * and interfaces, so we simply use interface indices for |
| 810 | * like-local scopes. |
| 811 | */ |
| 812 | *scopeid = if_nametoindex(scope); |
Bernie Innocenti | 63abf5b | 2019-06-11 21:46:51 +0900 | [diff] [blame] | 813 | if (*scopeid != 0) return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 814 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 815 | |
Bernie Innocenti | 63abf5b | 2019-06-11 21:46:51 +0900 | [diff] [blame] | 816 | // try to convert to a numeric id as a last resort |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 817 | errno = 0; |
| 818 | lscopeid = strtoul(scope, &ep, 10); |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 819 | *scopeid = (uint32_t)(lscopeid & 0xffffffffUL); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 820 | if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) |
| 821 | return 0; |
| 822 | else |
| 823 | return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 824 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 825 | |
| 826 | /* code duplicate with gethnamaddr.c */ |
| 827 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 828 | #define BOUNDED_INCR(x) \ |
| 829 | do { \ |
| 830 | BOUNDS_CHECK(cp, x); \ |
| 831 | cp += (x); \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 832 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 833 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 834 | #define BOUNDS_CHECK(ptr, count) \ |
| 835 | do { \ |
| 836 | if (eom - (ptr) < (count)) { \ |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 837 | *herrno = NO_RECOVERY; \ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 838 | return NULL; \ |
| 839 | } \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 840 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 841 | |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 842 | static struct addrinfo* getanswer(const std::vector<uint8_t>& answer, int anslen, const char* qname, |
| 843 | int qtype, const struct addrinfo* pai, int* herrno) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 844 | struct addrinfo sentinel = {}; |
| 845 | struct addrinfo *cur; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 846 | struct addrinfo ai; |
| 847 | const struct afd* afd; |
| 848 | char* canonname; |
| 849 | const HEADER* hp; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 850 | const uint8_t* cp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 851 | int n; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 852 | const uint8_t* eom; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 853 | char *bp, *ep; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 854 | int type, ancount, qdcount; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 855 | int haveanswer, had_error; |
| 856 | char tbuf[MAXDNAME]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 857 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 858 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 859 | assert(qname != NULL); |
| 860 | assert(pai != NULL); |
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 | cur = &sentinel; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 863 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 864 | canonname = NULL; |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 865 | eom = answer.data() + anslen; |
Bernie Innocenti | 10a9028 | 2020-01-23 23:28:00 +0900 | [diff] [blame] | 866 | |
| 867 | bool (*name_ok)(const char* dn); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 868 | switch (qtype) { |
| 869 | case T_A: |
| 870 | case T_AAAA: |
| 871 | case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ |
| 872 | name_ok = res_hnok; |
| 873 | break; |
| 874 | default: |
| 875 | return NULL; /* XXX should be abort(); */ |
| 876 | } |
| 877 | /* |
| 878 | * find first satisfactory answer |
| 879 | */ |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 880 | hp = reinterpret_cast<const HEADER*>(answer.data()); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 881 | ancount = ntohs(hp->ancount); |
| 882 | qdcount = ntohs(hp->qdcount); |
| 883 | bp = hostbuf; |
| 884 | ep = hostbuf + sizeof hostbuf; |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 885 | cp = answer.data(); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 886 | BOUNDED_INCR(HFIXEDSZ); |
| 887 | if (qdcount != 1) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 888 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 889 | return (NULL); |
| 890 | } |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 891 | n = dn_expand(answer.data(), eom, cp, bp, ep - bp); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 892 | if ((n < 0) || !(*name_ok)(bp)) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 893 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 894 | return (NULL); |
| 895 | } |
| 896 | BOUNDED_INCR(n + QFIXEDSZ); |
| 897 | if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { |
| 898 | /* res_send() has already verified that the query name is the |
| 899 | * same as the one we sent; this just gets the expanded name |
| 900 | * (i.e., with the succeeding search-domain tacked on). |
| 901 | */ |
| 902 | n = strlen(bp) + 1; /* for the \0 */ |
| 903 | if (n >= MAXHOSTNAMELEN) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 904 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 905 | return (NULL); |
| 906 | } |
| 907 | canonname = bp; |
| 908 | bp += n; |
| 909 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 910 | qname = canonname; |
| 911 | } |
| 912 | haveanswer = 0; |
| 913 | had_error = 0; |
| 914 | while (ancount-- > 0 && cp < eom && !had_error) { |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 915 | n = dn_expand(answer.data(), eom, cp, bp, ep - bp); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 916 | if ((n < 0) || !(*name_ok)(bp)) { |
| 917 | had_error++; |
| 918 | continue; |
| 919 | } |
| 920 | cp += n; /* name */ |
| 921 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 922 | type = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 923 | cp += INT16SZ; /* type */ |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 924 | int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 925 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 926 | n = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 927 | cp += INT16SZ; /* len */ |
| 928 | BOUNDS_CHECK(cp, n); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 929 | if (cl != C_IN) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 930 | /* XXX - debug? syslog? */ |
| 931 | cp += n; |
| 932 | continue; /* XXX - had_error++ ? */ |
| 933 | } |
| 934 | 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] | 935 | n = dn_expand(answer.data(), eom, cp, tbuf, sizeof tbuf); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 936 | if ((n < 0) || !(*name_ok)(tbuf)) { |
| 937 | had_error++; |
| 938 | continue; |
| 939 | } |
| 940 | cp += n; |
| 941 | /* Get canonical name. */ |
| 942 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 943 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 944 | had_error++; |
| 945 | continue; |
| 946 | } |
| 947 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 948 | canonname = bp; |
| 949 | bp += n; |
| 950 | continue; |
| 951 | } |
| 952 | if (qtype == T_ANY) { |
| 953 | if (!(type == T_A || type == T_AAAA)) { |
| 954 | cp += n; |
| 955 | continue; |
| 956 | } |
| 957 | } else if (type != qtype) { |
| 958 | if (type != T_KEY && type != T_SIG) |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 959 | LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " " |
| 960 | << p_type(qtype) << "\", got type \"" << p_type(type) << "\""; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 961 | cp += n; |
| 962 | continue; /* XXX - had_error++ ? */ |
| 963 | } |
| 964 | switch (type) { |
| 965 | case T_A: |
| 966 | case T_AAAA: |
| 967 | if (strcasecmp(canonname, bp) != 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 968 | LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp |
| 969 | << "\""; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 970 | cp += n; |
| 971 | continue; /* XXX - had_error++ ? */ |
| 972 | } |
| 973 | if (type == T_A && n != INADDRSZ) { |
| 974 | cp += n; |
| 975 | continue; |
| 976 | } |
| 977 | if (type == T_AAAA && n != IN6ADDRSZ) { |
| 978 | cp += n; |
| 979 | continue; |
| 980 | } |
| 981 | if (type == T_AAAA) { |
| 982 | struct in6_addr in6; |
| 983 | memcpy(&in6, cp, IN6ADDRSZ); |
| 984 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 985 | cp += n; |
| 986 | continue; |
| 987 | } |
| 988 | } |
| 989 | if (!haveanswer) { |
| 990 | int nn; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 991 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 992 | canonname = bp; |
| 993 | nn = strlen(bp) + 1; /* for the \0 */ |
| 994 | bp += nn; |
| 995 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 996 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 997 | /* don't overwrite pai */ |
| 998 | ai = *pai; |
| 999 | ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; |
| 1000 | afd = find_afd(ai.ai_family); |
| 1001 | if (afd == NULL) { |
| 1002 | cp += n; |
| 1003 | continue; |
| 1004 | } |
| 1005 | cur->ai_next = get_ai(&ai, afd, (const char*) cp); |
| 1006 | if (cur->ai_next == NULL) had_error++; |
| 1007 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1008 | cp += n; |
| 1009 | break; |
| 1010 | default: |
| 1011 | abort(); |
| 1012 | } |
| 1013 | if (!had_error) haveanswer++; |
| 1014 | } |
| 1015 | if (haveanswer) { |
| 1016 | if (!canonname) |
| 1017 | (void) get_canonname(pai, sentinel.ai_next, qname); |
| 1018 | else |
| 1019 | (void) get_canonname(pai, sentinel.ai_next, canonname); |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1020 | *herrno = NETDB_SUCCESS; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1021 | return sentinel.ai_next; |
| 1022 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1023 | |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1024 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1025 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | struct addrinfo_sort_elem { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1029 | struct addrinfo* ai; |
| 1030 | int has_src_addr; |
| 1031 | sockaddr_union src_addr; |
| 1032 | int original_order; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1033 | }; |
| 1034 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1035 | static int _get_scope(const struct sockaddr* addr) { |
| 1036 | if (addr->sa_family == AF_INET6) { |
| 1037 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1038 | if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) { |
| 1039 | return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr); |
| 1040 | } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) || |
| 1041 | IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) { |
| 1042 | /* |
| 1043 | * RFC 4291 section 2.5.3 says loopback is to be treated as having |
| 1044 | * link-local scope. |
| 1045 | */ |
| 1046 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1047 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1048 | return IPV6_ADDR_SCOPE_SITELOCAL; |
| 1049 | } else { |
| 1050 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1051 | } |
| 1052 | } else if (addr->sa_family == AF_INET) { |
| 1053 | const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr; |
| 1054 | unsigned long int na = ntohl(addr4->sin_addr.s_addr); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1055 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1056 | if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */ |
| 1057 | (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */ |
| 1058 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1059 | } else { |
| 1060 | /* |
| 1061 | * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses |
| 1062 | * and shared addresses (100.64.0.0/10), are assigned global scope. |
| 1063 | */ |
| 1064 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1065 | } |
| 1066 | } else { |
| 1067 | /* |
| 1068 | * This should never happen. |
| 1069 | * Return a scope with low priority as a last resort. |
| 1070 | */ |
| 1071 | return IPV6_ADDR_SCOPE_NODELOCAL; |
| 1072 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /* These macros are modelled after the ones in <netinet/in6.h>. */ |
| 1076 | |
| 1077 | /* RFC 4380, section 2.6 */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1078 | #define IN6_IS_ADDR_TEREDO(a) \ |
| 1079 | ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000))) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1080 | |
| 1081 | /* RFC 3056, section 2. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1082 | #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] | 1083 | |
| 1084 | /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1085 | #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] | 1086 | |
| 1087 | /* |
| 1088 | * Get the label for a given IPv4/IPv6 address. |
| 1089 | * RFC 6724, section 2.1. |
| 1090 | */ |
| 1091 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1092 | static int _get_label(const struct sockaddr* addr) { |
| 1093 | if (addr->sa_family == AF_INET) { |
| 1094 | return 4; |
| 1095 | } else if (addr->sa_family == AF_INET6) { |
| 1096 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1097 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1098 | return 0; |
| 1099 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1100 | return 4; |
| 1101 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1102 | return 2; |
| 1103 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1104 | return 5; |
| 1105 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1106 | return 13; |
| 1107 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) { |
| 1108 | return 3; |
| 1109 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1110 | return 11; |
| 1111 | } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1112 | return 12; |
| 1113 | } else { |
| 1114 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1115 | return 1; |
| 1116 | } |
| 1117 | } else { |
| 1118 | /* |
| 1119 | * This should never happen. |
| 1120 | * Return a semi-random label as a last resort. |
| 1121 | */ |
| 1122 | return 1; |
| 1123 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * Get the precedence for a given IPv4/IPv6 address. |
| 1128 | * RFC 6724, section 2.1. |
| 1129 | */ |
| 1130 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1131 | static int _get_precedence(const struct sockaddr* addr) { |
| 1132 | if (addr->sa_family == AF_INET) { |
| 1133 | return 35; |
| 1134 | } else if (addr->sa_family == AF_INET6) { |
| 1135 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1136 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1137 | return 50; |
| 1138 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1139 | return 35; |
| 1140 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1141 | return 30; |
| 1142 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1143 | return 5; |
| 1144 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1145 | return 3; |
| 1146 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) || |
| 1147 | IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) || |
| 1148 | IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1149 | return 1; |
| 1150 | } else { |
| 1151 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1152 | return 40; |
| 1153 | } |
| 1154 | } else { |
| 1155 | return 1; |
| 1156 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Find number of matching initial bits between the two addresses a1 and a2. |
| 1161 | */ |
| 1162 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1163 | static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) { |
| 1164 | const char* p1 = (const char*) a1; |
| 1165 | const char* p2 = (const char*) a2; |
| 1166 | unsigned i; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1167 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1168 | for (i = 0; i < sizeof(*a1); ++i) { |
| 1169 | int x, j; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1170 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1171 | if (p1[i] == p2[i]) { |
| 1172 | continue; |
| 1173 | } |
| 1174 | x = p1[i] ^ p2[i]; |
| 1175 | for (j = 0; j < CHAR_BIT; ++j) { |
| 1176 | if (x & (1 << (CHAR_BIT - 1))) { |
| 1177 | return i * CHAR_BIT + j; |
| 1178 | } |
| 1179 | x <<= 1; |
| 1180 | } |
| 1181 | } |
| 1182 | return sizeof(*a1) * CHAR_BIT; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Compare two source/destination address pairs. |
| 1187 | * RFC 6724, section 6. |
| 1188 | */ |
| 1189 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1190 | static int _rfc6724_compare(const void* ptr1, const void* ptr2) { |
| 1191 | const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1; |
| 1192 | const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2; |
| 1193 | int scope_src1, scope_dst1, scope_match1; |
| 1194 | int scope_src2, scope_dst2, scope_match2; |
| 1195 | int label_src1, label_dst1, label_match1; |
| 1196 | int label_src2, label_dst2, label_match2; |
| 1197 | int precedence1, precedence2; |
| 1198 | int prefixlen1, prefixlen2; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1199 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1200 | /* Rule 1: Avoid unusable destinations. */ |
| 1201 | if (a1->has_src_addr != a2->has_src_addr) { |
| 1202 | return a2->has_src_addr - a1->has_src_addr; |
| 1203 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1204 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1205 | /* Rule 2: Prefer matching scope. */ |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1206 | scope_src1 = _get_scope(&a1->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1207 | scope_dst1 = _get_scope(a1->ai->ai_addr); |
| 1208 | scope_match1 = (scope_src1 == scope_dst1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1209 | |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1210 | scope_src2 = _get_scope(&a2->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1211 | scope_dst2 = _get_scope(a2->ai->ai_addr); |
| 1212 | scope_match2 = (scope_src2 == scope_dst2); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1213 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1214 | if (scope_match1 != scope_match2) { |
| 1215 | return scope_match2 - scope_match1; |
| 1216 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1217 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1218 | /* |
| 1219 | * Rule 3: Avoid deprecated addresses. |
| 1220 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1221 | */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1222 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1223 | /* |
| 1224 | * Rule 4: Prefer home addresses. |
| 1225 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1226 | */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1227 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1228 | /* Rule 5: Prefer matching label. */ |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1229 | label_src1 = _get_label(&a1->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1230 | label_dst1 = _get_label(a1->ai->ai_addr); |
| 1231 | label_match1 = (label_src1 == label_dst1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1232 | |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1233 | label_src2 = _get_label(&a2->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1234 | label_dst2 = _get_label(a2->ai->ai_addr); |
| 1235 | label_match2 = (label_src2 == label_dst2); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1236 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1237 | if (label_match1 != label_match2) { |
| 1238 | return label_match2 - label_match1; |
| 1239 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1240 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1241 | /* Rule 6: Prefer higher precedence. */ |
| 1242 | precedence1 = _get_precedence(a1->ai->ai_addr); |
| 1243 | precedence2 = _get_precedence(a2->ai->ai_addr); |
| 1244 | if (precedence1 != precedence2) { |
| 1245 | return precedence2 - precedence1; |
| 1246 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1247 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1248 | /* |
| 1249 | * Rule 7: Prefer native transport. |
| 1250 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1251 | */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1252 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1253 | /* Rule 8: Prefer smaller scope. */ |
| 1254 | if (scope_dst1 != scope_dst2) { |
| 1255 | return scope_dst1 - scope_dst2; |
| 1256 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1257 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1258 | /* |
| 1259 | * Rule 9: Use longest matching prefix. |
| 1260 | * We implement this for IPv6 only, as the rules in RFC 6724 don't seem |
| 1261 | * to work very well directly applied to IPv4. (glibc uses information from |
| 1262 | * the routing table for a custom IPv4 implementation here.) |
| 1263 | */ |
| 1264 | if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr && |
| 1265 | a2->ai->ai_addr->sa_family == AF_INET6) { |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1266 | const struct sockaddr_in6* a1_src = &a1->src_addr.sin6; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1267 | 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] | 1268 | const struct sockaddr_in6* a2_src = &a2->src_addr.sin6; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1269 | const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr; |
| 1270 | prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr); |
| 1271 | prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr); |
| 1272 | if (prefixlen1 != prefixlen2) { |
| 1273 | return prefixlen2 - prefixlen1; |
| 1274 | } |
| 1275 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1276 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1277 | /* |
| 1278 | * Rule 10: Leave the order unchanged. |
| 1279 | * We need this since qsort() is not necessarily stable. |
| 1280 | */ |
| 1281 | return a1->original_order - a2->original_order; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Find the source address that will be used if trying to connect to the given |
| 1286 | * address. src_addr must be large enough to hold a struct sockaddr_in6. |
| 1287 | * |
| 1288 | * Returns 1 if a source address was found, 0 if the address is unreachable, |
| 1289 | * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are |
| 1290 | * undefined. |
| 1291 | */ |
| 1292 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1293 | static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark, |
| 1294 | uid_t uid) { |
| 1295 | int sock; |
| 1296 | int ret; |
| 1297 | socklen_t len; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1298 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1299 | switch (addr->sa_family) { |
| 1300 | case AF_INET: |
| 1301 | len = sizeof(struct sockaddr_in); |
| 1302 | break; |
| 1303 | case AF_INET6: |
| 1304 | len = sizeof(struct sockaddr_in6); |
| 1305 | break; |
| 1306 | default: |
| 1307 | /* No known usable source address for non-INET families. */ |
| 1308 | return 0; |
| 1309 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1310 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1311 | sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); |
| 1312 | if (sock == -1) { |
| 1313 | if (errno == EAFNOSUPPORT) { |
| 1314 | return 0; |
| 1315 | } else { |
| 1316 | return -1; |
| 1317 | } |
| 1318 | } |
| 1319 | if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) { |
| 1320 | close(sock); |
| 1321 | return 0; |
| 1322 | } |
| 1323 | if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) { |
| 1324 | close(sock); |
| 1325 | return 0; |
| 1326 | } |
| 1327 | do { |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 1328 | ret = connect(sock, addr, len); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1329 | } while (ret == -1 && errno == EINTR); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1330 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1331 | if (ret == -1) { |
| 1332 | close(sock); |
| 1333 | return 0; |
| 1334 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1335 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1336 | if (src_addr && getsockname(sock, src_addr, &len) == -1) { |
| 1337 | close(sock); |
| 1338 | return -1; |
| 1339 | } |
| 1340 | close(sock); |
| 1341 | return 1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Sort the linked list starting at sentinel->ai_next in RFC6724 order. |
| 1346 | * Will leave the list unchanged if an error occurs. |
| 1347 | */ |
| 1348 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1349 | static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) { |
| 1350 | struct addrinfo* cur; |
| 1351 | int nelem = 0, i; |
| 1352 | struct addrinfo_sort_elem* elems; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1353 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1354 | cur = list_sentinel->ai_next; |
| 1355 | while (cur) { |
| 1356 | ++nelem; |
| 1357 | cur = cur->ai_next; |
| 1358 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1359 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1360 | elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem)); |
| 1361 | if (elems == NULL) { |
| 1362 | goto error; |
| 1363 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1364 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1365 | /* |
| 1366 | * Convert the linked list to an array that also contains the candidate |
| 1367 | * source address for each destination address. |
| 1368 | */ |
| 1369 | for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) { |
| 1370 | int has_src_addr; |
| 1371 | assert(cur != NULL); |
| 1372 | elems[i].ai = cur; |
| 1373 | elems[i].original_order = i; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1374 | |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1375 | 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] | 1376 | if (has_src_addr == -1) { |
| 1377 | goto error; |
| 1378 | } |
| 1379 | elems[i].has_src_addr = has_src_addr; |
| 1380 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1381 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1382 | /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */ |
| 1383 | qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1384 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1385 | list_sentinel->ai_next = elems[0].ai; |
| 1386 | for (i = 0; i < nelem - 1; ++i) { |
| 1387 | elems[i].ai->ai_next = elems[i + 1].ai; |
| 1388 | } |
| 1389 | elems[nelem - 1].ai->ai_next = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1390 | |
| 1391 | error: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1392 | free(elems); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1393 | } |
| 1394 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1395 | static int dns_getaddrinfo(const char* name, const addrinfo* pai, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 1396 | const android_net_context* netcontext, addrinfo** rv, |
| 1397 | NetworkDnsEventReported* event) { |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 1398 | res_target q = {}; |
| 1399 | res_target q2 = {}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1400 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1401 | switch (pai->ai_family) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1402 | case AF_UNSPEC: { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1403 | /* prefer IPv6 */ |
| 1404 | q.name = name; |
| 1405 | q.qclass = C_IN; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1406 | int query_ipv6 = 1, query_ipv4 = 1; |
| 1407 | if (pai->ai_flags & AI_ADDRCONFIG) { |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 1408 | query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid); |
| 1409 | query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1410 | } |
| 1411 | if (query_ipv6) { |
| 1412 | q.qtype = T_AAAA; |
| 1413 | if (query_ipv4) { |
| 1414 | q.next = &q2; |
| 1415 | q2.name = name; |
| 1416 | q2.qclass = C_IN; |
| 1417 | q2.qtype = T_A; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1418 | } |
| 1419 | } else if (query_ipv4) { |
| 1420 | q.qtype = T_A; |
| 1421 | } else { |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1422 | return EAI_NODATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1423 | } |
| 1424 | break; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1425 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1426 | case AF_INET: |
| 1427 | q.name = name; |
| 1428 | q.qclass = C_IN; |
| 1429 | q.qtype = T_A; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1430 | break; |
| 1431 | case AF_INET6: |
| 1432 | q.name = name; |
| 1433 | q.qclass = C_IN; |
| 1434 | q.qtype = T_AAAA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1435 | break; |
| 1436 | default: |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1437 | return EAI_FAMILY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1438 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1439 | |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1440 | ResState res; |
| 1441 | res_init(&res, netcontext, event); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1442 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1443 | int he; |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1444 | if (res_searchN(name, &q, &res, &he) < 0) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1445 | // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA. |
| 1446 | // Note that res_searchN() doesn't set the pair NETDB_INTERNAL and errno. |
| 1447 | // See also herrnoToAiErrno(). |
| 1448 | return herrnoToAiErrno(he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1449 | } |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 1450 | |
| 1451 | addrinfo sentinel = {}; |
| 1452 | addrinfo* cur = &sentinel; |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 1453 | 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] | 1454 | if (ai) { |
| 1455 | cur->ai_next = ai; |
| 1456 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1457 | } |
| 1458 | if (q.next) { |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 1459 | ai = getanswer(q2.answer, q2.n, q2.name, q2.qtype, pai, &he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1460 | if (ai) cur->ai_next = ai; |
| 1461 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1462 | if (sentinel.ai_next == NULL) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1463 | // Note that getanswer() doesn't set the pair NETDB_INTERNAL and errno. |
| 1464 | // See also herrnoToAiErrno(). |
| 1465 | return herrnoToAiErrno(he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1466 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1467 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1468 | _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1469 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1470 | *rv = sentinel.ai_next; |
| 1471 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1472 | } |
| 1473 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1474 | static void _sethtent(FILE** hostf) { |
| 1475 | if (!*hostf) |
| 1476 | *hostf = fopen(_PATH_HOSTS, "re"); |
| 1477 | else |
| 1478 | rewind(*hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1479 | } |
| 1480 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1481 | static void _endhtent(FILE** hostf) { |
| 1482 | if (*hostf) { |
| 1483 | (void) fclose(*hostf); |
| 1484 | *hostf = NULL; |
| 1485 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1486 | } |
| 1487 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1488 | static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) { |
| 1489 | char* p; |
| 1490 | char *cp, *tname, *cname; |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1491 | struct addrinfo *res0, *res; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1492 | int error; |
| 1493 | const char* addr; |
| 1494 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1495 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1496 | assert(name != NULL); |
| 1497 | assert(pai != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1498 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1499 | if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL); |
| 1500 | again: |
| 1501 | if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL); |
| 1502 | if (*p == '#') goto again; |
| 1503 | if (!(cp = strpbrk(p, "#\n"))) goto again; |
| 1504 | *cp = '\0'; |
| 1505 | if (!(cp = strpbrk(p, " \t"))) goto again; |
| 1506 | *cp++ = '\0'; |
| 1507 | addr = p; |
| 1508 | /* if this is not something we're looking for, skip it. */ |
| 1509 | cname = NULL; |
| 1510 | while (cp && *cp) { |
| 1511 | if (*cp == ' ' || *cp == '\t') { |
| 1512 | cp++; |
| 1513 | continue; |
| 1514 | } |
| 1515 | if (!cname) cname = cp; |
| 1516 | tname = cp; |
| 1517 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1518 | if (strcasecmp(name, tname) == 0) goto found; |
| 1519 | } |
| 1520 | goto again; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1521 | |
| 1522 | found: |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1523 | error = getaddrinfo_numeric(addr, nullptr, *pai, &res0); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1524 | if (error) goto again; |
| 1525 | for (res = res0; res; res = res->ai_next) { |
| 1526 | /* cover it up */ |
| 1527 | res->ai_flags = pai->ai_flags; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1528 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1529 | if (pai->ai_flags & AI_CANONNAME) { |
| 1530 | if (get_canonname(pai, res, cname) != 0) { |
| 1531 | freeaddrinfo(res0); |
| 1532 | goto again; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | return res0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1537 | } |
| 1538 | |
chenbruce | fd837fa | 2019-10-29 18:35:36 +0800 | [diff] [blame] | 1539 | static struct addrinfo* getCustomHosts(const size_t netid, const char* _Nonnull name, |
| 1540 | const struct addrinfo* _Nonnull pai) { |
| 1541 | struct addrinfo sentinel = {}; |
| 1542 | struct addrinfo *res0, *res; |
| 1543 | res = &sentinel; |
| 1544 | std::vector<std::string> hosts = getCustomizedTableByName(netid, name); |
| 1545 | for (const std::string& host : hosts) { |
| 1546 | int error = getaddrinfo_numeric(host.c_str(), nullptr, *pai, &res0); |
| 1547 | if (!error && res0 != nullptr) { |
| 1548 | res->ai_next = res0; |
| 1549 | res = res0; |
| 1550 | res0 = nullptr; |
| 1551 | } |
| 1552 | } |
| 1553 | return sentinel.ai_next; |
| 1554 | } |
| 1555 | |
| 1556 | static bool files_getaddrinfo(const size_t netid, const char* name, const addrinfo* pai, |
| 1557 | addrinfo** res) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 1558 | struct addrinfo sentinel = {}; |
| 1559 | struct addrinfo *p, *cur; |
chenbruce | fd837fa | 2019-10-29 18:35:36 +0800 | [diff] [blame] | 1560 | FILE* hostf = nullptr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1561 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1562 | cur = &sentinel; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1563 | _sethtent(&hostf); |
chenbruce | fd837fa | 2019-10-29 18:35:36 +0800 | [diff] [blame] | 1564 | while ((p = _gethtent(&hostf, name, pai)) != nullptr) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1565 | cur->ai_next = p; |
| 1566 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1567 | } |
| 1568 | _endhtent(&hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1569 | |
chenbruce | fd837fa | 2019-10-29 18:35:36 +0800 | [diff] [blame] | 1570 | if ((p = getCustomHosts(netid, name, pai)) != nullptr) { |
| 1571 | cur->ai_next = p; |
| 1572 | } |
| 1573 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1574 | *res = sentinel.ai_next; |
chenbruce | fd837fa | 2019-10-29 18:35:36 +0800 | [diff] [blame] | 1575 | return sentinel.ai_next != nullptr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /* resolver logic */ |
| 1579 | |
Luke Huang | 0a0870d | 2020-02-12 20:41:10 +0800 | [diff] [blame] | 1580 | namespace { |
| 1581 | |
| 1582 | constexpr int SLEEP_TIME_MS = 2; |
| 1583 | |
| 1584 | int getHerrnoFromRcode(int rcode) { |
| 1585 | switch (rcode) { |
| 1586 | // Not defined in RFC. |
| 1587 | case RCODE_TIMEOUT: |
| 1588 | // DNS metrics monitors DNS query timeout. |
| 1589 | return NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno. |
| 1590 | // Defined in RFC 1035 section 4.1.1. |
| 1591 | case NXDOMAIN: |
| 1592 | return HOST_NOT_FOUND; |
| 1593 | case SERVFAIL: |
| 1594 | return TRY_AGAIN; |
| 1595 | case NOERROR: |
| 1596 | return NO_DATA; |
| 1597 | case FORMERR: |
| 1598 | case NOTIMP: |
| 1599 | case REFUSED: |
| 1600 | default: |
| 1601 | return NO_RECOVERY; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | struct QueryResult { |
| 1606 | int ancount; |
| 1607 | int rcode; |
| 1608 | int herrno; |
| 1609 | NetworkDnsEventReported event; |
| 1610 | }; |
| 1611 | |
| 1612 | QueryResult doQuery(const char* name, res_target* t, res_state res) { |
| 1613 | HEADER* hp = (HEADER*)(void*)t->answer.data(); |
| 1614 | |
| 1615 | hp->rcode = NOERROR; // default |
| 1616 | |
| 1617 | const int cl = t->qclass; |
| 1618 | const int type = t->qtype; |
| 1619 | const int anslen = t->answer.size(); |
| 1620 | |
| 1621 | LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")"; |
| 1622 | |
| 1623 | uint8_t buf[MAXPACKET]; |
| 1624 | |
| 1625 | int n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, sizeof(buf), |
| 1626 | res->netcontext_flags); |
| 1627 | |
| 1628 | if (n > 0 && |
| 1629 | (res->netcontext_flags & (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS))) { |
| 1630 | n = res_nopt(res, n, buf, sizeof(buf), anslen); |
| 1631 | } |
| 1632 | |
| 1633 | NetworkDnsEventReported event; |
| 1634 | if (n <= 0) { |
| 1635 | LOG(ERROR) << __func__ << ": res_nmkquery failed"; |
| 1636 | return {0, -1, NO_RECOVERY, event}; |
| 1637 | return { |
| 1638 | .ancount = 0, |
| 1639 | .rcode = -1, |
| 1640 | .herrno = NO_RECOVERY, |
| 1641 | .event = event, |
| 1642 | }; |
| 1643 | } |
| 1644 | |
| 1645 | ResState res_temp = fromResState(*res, &event); |
| 1646 | |
| 1647 | int rcode = NOERROR; |
| 1648 | n = res_nsend(&res_temp, buf, n, t->answer.data(), anslen, &rcode, 0); |
| 1649 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
| 1650 | // if the query choked with EDNS0, retry without EDNS0 |
| 1651 | if ((res_temp.netcontext_flags & |
| 1652 | (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) && |
| 1653 | (res_temp._flags & RES_F_EDNS0ERR)) { |
| 1654 | LOG(DEBUG) << __func__ << ": retry without EDNS0"; |
| 1655 | n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, |
| 1656 | sizeof(buf), res->netcontext_flags); |
| 1657 | n = res_nsend(&res_temp, buf, n, t->answer.data(), anslen, &rcode, 0); |
| 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, |
| 1667 | .event = event, |
| 1668 | }; |
| 1669 | } |
| 1670 | |
| 1671 | } // namespace |
| 1672 | |
| 1673 | static int res_queryN_parallel(const char* name, res_target* target, res_state res, int* herrno) { |
| 1674 | std::vector<std::future<QueryResult>> results; |
| 1675 | results.reserve(2); |
| 1676 | for (res_target* t = target; t; t = t->next) { |
| 1677 | results.emplace_back(std::async(std::launch::async, doQuery, name, t, res)); |
| 1678 | // Avoiding gateways drop packets if queries are sent too close together |
Luke Huang | f40df9c | 2020-04-21 08:51:48 +0800 | [diff] [blame^] | 1679 | int sleepTime = android::net::Experiments::getInstance()->getFlag( |
| 1680 | "parallel_lookup_sleep_time", SLEEP_TIME_MS); |
| 1681 | if (sleepTime > 1000) sleepTime = 1000; |
| 1682 | if (t->next) usleep(sleepTime * 1000); |
Luke Huang | 0a0870d | 2020-02-12 20:41:10 +0800 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | int ancount = 0; |
| 1686 | int rcode = 0; |
| 1687 | |
| 1688 | for (auto& f : results) { |
| 1689 | const QueryResult& r = f.get(); |
| 1690 | if (r.herrno == NO_RECOVERY) { |
| 1691 | *herrno = r.herrno; |
| 1692 | return -1; |
| 1693 | } |
| 1694 | res->event->MergeFrom(r.event); |
| 1695 | ancount += r.ancount; |
| 1696 | rcode = r.rcode; |
| 1697 | } |
| 1698 | |
| 1699 | if (ancount == 0) { |
| 1700 | *herrno = getHerrnoFromRcode(rcode); |
| 1701 | return -1; |
| 1702 | } |
| 1703 | |
| 1704 | return ancount; |
| 1705 | } |
| 1706 | |
| 1707 | static int res_queryN_wrapper(const char* name, res_target* target, res_state res, int* herrno) { |
Luke Huang | f40df9c | 2020-04-21 08:51:48 +0800 | [diff] [blame^] | 1708 | const bool parallel_lookup = |
| 1709 | android::net::Experiments::getInstance()->getFlag("parallel_lookup", 0); |
Luke Huang | 0a0870d | 2020-02-12 20:41:10 +0800 | [diff] [blame] | 1710 | if (parallel_lookup) return res_queryN_parallel(name, target, res, herrno); |
| 1711 | |
| 1712 | return res_queryN(name, target, res, herrno); |
| 1713 | } |
| 1714 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1715 | /* |
| 1716 | * Formulate a normal query, send, and await answer. |
| 1717 | * Returned answer is placed in supplied buffer "answer". |
| 1718 | * Perform preliminary check of answer, returning success only |
| 1719 | * if no error is indicated and the answer count is nonzero. |
| 1720 | * Return the size of the response on success, -1 on error. |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1721 | * Error number is left in *herrno. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1722 | * |
| 1723 | * Caller must parse answer and determine whether it answers the question. |
| 1724 | */ |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1725 | static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 1726 | uint8_t buf[MAXPACKET]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1727 | int n; |
| 1728 | struct res_target* t; |
| 1729 | int rcode; |
| 1730 | int ancount; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1731 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1732 | assert(name != NULL); |
| 1733 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1734 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1735 | rcode = NOERROR; |
| 1736 | ancount = 0; |
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 | for (t = target; t; t = t->next) { |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 1739 | HEADER* hp = (HEADER*)(void*)t->answer.data(); |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 1740 | bool retried = false; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1741 | again: |
| 1742 | hp->rcode = NOERROR; /* default */ |
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 | /* make it easier... */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1745 | int cl = t->qclass; |
| 1746 | int type = t->qtype; |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 1747 | const int anslen = t->answer.size(); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1748 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1749 | LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1750 | |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1751 | n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, sizeof(buf), |
| 1752 | res->netcontext_flags); |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 1753 | if (n > 0 && |
| 1754 | (res->netcontext_flags & |
| 1755 | (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) && |
| 1756 | !retried) // TODO: remove the retry flag and provide a sufficient test coverage. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1757 | n = res_nopt(res, n, buf, sizeof(buf), anslen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1758 | if (n <= 0) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1759 | LOG(ERROR) << __func__ << ": res_nmkquery failed"; |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1760 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1761 | return n; |
| 1762 | } |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1763 | |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 1764 | n = res_nsend(res, buf, n, t->answer.data(), anslen, &rcode, 0); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1765 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1766 | // Record rcode from DNS response header only if no timeout. |
| 1767 | // Keep rcode timeout for reporting later if any. |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 1768 | if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; // record most recent error |
| 1769 | // if the query choked with EDNS0, retry without EDNS0 that when the server |
| 1770 | // has no response, resovler won't retry and do nothing. Even fallback to UDP, |
| 1771 | // we also has the same symptom if EDNS is enabled. |
| 1772 | if ((res->netcontext_flags & |
| 1773 | (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) && |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 1774 | (res->_flags & RES_F_EDNS0ERR) && !retried) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1775 | LOG(DEBUG) << __func__ << ": retry without EDNS0"; |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 1776 | retried = true; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1777 | goto again; |
| 1778 | } |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1779 | LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1780 | continue; |
| 1781 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1782 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1783 | ancount += ntohs(hp->ancount); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1784 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1785 | t->n = n; |
| 1786 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1787 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1788 | if (ancount == 0) { |
Luke Huang | 0a0870d | 2020-02-12 20:41:10 +0800 | [diff] [blame] | 1789 | *herrno = getHerrnoFromRcode(rcode); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1790 | return -1; |
| 1791 | } |
| 1792 | return ancount; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | /* |
| 1796 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 1797 | * Return the size of the response on success, -1 on error. |
| 1798 | * If enabled, implement search rules until answer or unrecoverable failure |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1799 | * is detected. Error code, if any, is left in *herrno. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1800 | */ |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1801 | static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) { |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1802 | const char* cp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1803 | HEADER* hp; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 1804 | uint32_t dots; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1805 | int ret, saved_herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1806 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1807 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1808 | assert(name != NULL); |
| 1809 | assert(target != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1810 | |
Luke Huang | c98fd80 | 2019-10-15 16:36:36 +0900 | [diff] [blame] | 1811 | hp = (HEADER*)(void*)target->answer.data(); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1812 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1813 | errno = 0; |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1814 | *herrno = HOST_NOT_FOUND; /* default, if we never query */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1815 | dots = 0; |
| 1816 | for (cp = name; *cp; cp++) dots += (*cp == '.'); |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1817 | const bool trailing_dot = (cp > name && *--cp == '.') ? true : false; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1818 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1819 | /* |
| 1820 | * If there are dots in the name already, let's just give it a try |
| 1821 | * 'as is'. The threshold can be set with the "ndots" option. |
| 1822 | */ |
| 1823 | saved_herrno = -1; |
| 1824 | if (dots >= res->ndots) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1825 | ret = res_querydomainN(name, NULL, target, res, herrno); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1826 | if (ret > 0) return (ret); |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1827 | saved_herrno = *herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1828 | tried_as_is++; |
| 1829 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1830 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1831 | /* |
| 1832 | * We do at least one level of search if |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1833 | * - there is no dot, or |
| 1834 | * - there is at least one dot and there is no trailing dot. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1835 | */ |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1836 | if ((!dots) || (dots && !trailing_dot)) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1837 | int done = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1838 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1839 | /* Unfortunately we need to set stuff up before |
| 1840 | * the domain stuff is tried. Will have a better |
| 1841 | * fix after thread pools are used. |
| 1842 | */ |
Mike Yu | 021c3e1 | 2019-10-08 17:29:34 +0800 | [diff] [blame] | 1843 | resolv_populate_res_for_net(res); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1844 | |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1845 | for (const auto& domain : res->search_domains) { |
| 1846 | ret = res_querydomainN(name, domain.c_str(), target, res, herrno); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1847 | if (ret > 0) return ret; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1848 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1849 | /* |
| 1850 | * If no server present, give up. |
| 1851 | * If name isn't found in this domain, |
| 1852 | * keep trying higher domains in the search list |
| 1853 | * (if that's enabled). |
| 1854 | * On a NO_DATA error, keep trying, otherwise |
| 1855 | * a wildcard entry of another type could keep us |
| 1856 | * from finding this entry higher in the domain. |
| 1857 | * If we get some other error (negative answer or |
| 1858 | * server failure), then stop searching up, |
| 1859 | * but try the input name below in case it's |
| 1860 | * fully-qualified. |
| 1861 | */ |
| 1862 | if (errno == ECONNREFUSED) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1863 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1864 | return -1; |
| 1865 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1866 | |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1867 | switch (*herrno) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1868 | case NO_DATA: |
| 1869 | got_nodata++; |
Bernie Innocenti | f40b3bd | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 1870 | [[fallthrough]]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1871 | case HOST_NOT_FOUND: |
| 1872 | /* keep trying */ |
| 1873 | break; |
| 1874 | case TRY_AGAIN: |
| 1875 | if (hp->rcode == SERVFAIL) { |
| 1876 | /* try next search element, if any */ |
| 1877 | got_servfail++; |
| 1878 | break; |
| 1879 | } |
Bernie Innocenti | f40b3bd | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 1880 | [[fallthrough]]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1881 | default: |
| 1882 | /* anything else implies that we're done */ |
| 1883 | done++; |
| 1884 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1885 | } |
| 1886 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1887 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1888 | /* |
| 1889 | * if we have not already tried the name "as is", do that now. |
| 1890 | * note that we do this regardless of how many dots were in the |
| 1891 | * name or whether it ends with a dot. |
| 1892 | */ |
| 1893 | if (!tried_as_is) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1894 | ret = res_querydomainN(name, NULL, target, res, herrno); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1895 | if (ret > 0) return ret; |
| 1896 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1897 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1898 | /* |
| 1899 | * if we got here, we didn't satisfy the search. |
| 1900 | * if we did an initial full query, return that query's h_errno |
| 1901 | * (note that we wouldn't be here if that query had succeeded). |
| 1902 | * else if we ever got a nodata, send that back as the reason. |
| 1903 | * else send back meaningless h_errno, that being the one from |
| 1904 | * the last DNSRCH we did. |
| 1905 | */ |
| 1906 | if (saved_herrno != -1) |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1907 | *herrno = saved_herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1908 | else if (got_nodata) |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1909 | *herrno = NO_DATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1910 | else if (got_servfail) |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1911 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1912 | return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1913 | } |
| 1914 | |
Luke Huang | 0a0870d | 2020-02-12 20:41:10 +0800 | [diff] [blame] | 1915 | // Perform a call on res_query on the concatenation of name and domain, |
| 1916 | // removing a trailing dot from name if domain is NULL. |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1917 | static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res, |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1918 | int* herrno) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1919 | char nbuf[MAXDNAME]; |
| 1920 | const char* longname = nbuf; |
| 1921 | size_t n, d; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1922 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1923 | assert(name != NULL); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1924 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1925 | if (domain == NULL) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1926 | // Check for trailing '.'; copy without '.' if present. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1927 | n = strlen(name); |
| 1928 | if (n + 1 > sizeof(nbuf)) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1929 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1930 | return -1; |
| 1931 | } |
| 1932 | if (n > 0 && name[--n] == '.') { |
| 1933 | strncpy(nbuf, name, n); |
| 1934 | nbuf[n] = '\0'; |
| 1935 | } else |
| 1936 | longname = name; |
| 1937 | } else { |
| 1938 | n = strlen(name); |
| 1939 | d = strlen(domain); |
| 1940 | if (n + 1 + d + 1 > sizeof(nbuf)) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1941 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1942 | return -1; |
| 1943 | } |
| 1944 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 1945 | } |
Luke Huang | 0a0870d | 2020-02-12 20:41:10 +0800 | [diff] [blame] | 1946 | return res_queryN_wrapper(longname, target, res, herrno); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1947 | } |