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