blob: a9550b0d8687fce0aec8f55c3ccaa0f03410a3ca [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $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 Chen5471dca2019-04-15 15:25:35 +080033#define LOG_TAG "resolv"
Bernie Innocenti3952ccc2019-03-03 19:39:53 +090034
Bernie Innocentie71a28a2019-05-29 00:42:35 +090035#include "getaddrinfo.h"
36
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090037#include <arpa/inet.h>
38#include <arpa/nameser.h>
39#include <assert.h>
40#include <ctype.h>
41#include <errno.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090042#include <fcntl.h>
43#include <net/if.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090044#include <netdb.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090045#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090046#include <stdbool.h>
47#include <stddef.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090048#include <stdlib.h>
49#include <string.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090050#include <sys/param.h>
51#include <sys/socket.h>
52#include <sys/stat.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090053#include <sys/un.h>
Bernie Innocentiac18b122018-10-01 23:10:18 +090054#include <unistd.h>
Bernie Innocentiafaacf72018-08-30 07:34:37 +090055
chenbruceacb832c2019-02-20 19:45:50 +080056#include <android-base/logging.h>
57
Bernie Innocentiac18b122018-10-01 23:10:18 +090058#include "netd_resolv/resolv.h"
Bernie Innocenti08487112019-10-11 21:14:13 +090059#include "res_init.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090060#include "resolv_cache.h"
61#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090062
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090063#define ANY 0
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090064
lifr94981782019-05-17 21:15:19 +080065using android::net::NetworkDnsEventReported;
66
Bernie Innocenti4e374b62018-12-12 00:43:02 +090067const char in_addrany[] = {0, 0, 0, 0};
68const char in_loopback[] = {127, 0, 0, 1};
69const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
70const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090071
Bernie Innocenti4e374b62018-12-12 00:43:02 +090072const struct afd {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090073 int a_af;
74 int a_addrlen;
75 int a_socklen;
76 int a_off;
77 const char* a_addrany;
78 const char* a_loopback;
79 int a_scoped;
80} afdl[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090081 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
82 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090083 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
84 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
85 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090086};
87
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090088struct Explore {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090089 int e_af;
90 int e_socktype;
91 int e_protocol;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090092 int e_wild;
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090093#define WILD_AF(ex) ((ex).e_wild & 0x01)
94#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
95#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090096};
97
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090098const Explore explore_options[] = {
Ken Chene0d73c92018-11-07 01:20:48 +080099 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
100 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
101 {PF_INET6, SOCK_RAW, ANY, 0x05},
102 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
103 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
104 {PF_INET, SOCK_RAW, ANY, 0x05},
105 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
106 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
107 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900108};
109
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900110#define PTON_MAX 16
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900111#define MAXPACKET (8 * 1024)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900112
113typedef union {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900114 HEADER hdr;
chenbrucec51f1212019-09-12 16:59:33 +0800115 uint8_t buf[MAXPACKET];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900116} querybuf;
117
118struct res_target {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900119 struct res_target* next;
120 const char* name; /* domain name */
121 int qclass, qtype; /* class and type of query */
chenbrucec51f1212019-09-12 16:59:33 +0800122 uint8_t* answer; /* buffer to put answer */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900123 int anslen; /* size of answer buffer */
124 int n; /* result length */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900125};
126
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900127static int str2number(const char*);
128static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
lifr94981782019-05-17 21:15:19 +0800129 const struct android_net_context*, NetworkDnsEventReported* event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900130static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
131static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
132 const char*);
133static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
134 struct addrinfo**);
135static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
136static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
137static int get_portmatch(const struct addrinfo*, const char*);
138static int get_port(const struct addrinfo*, const char*, int);
139static const struct afd* find_afd(int);
chenbrucec51f1212019-09-12 16:59:33 +0800140static int ip6_str2scopeid(const char*, struct sockaddr_in6*, uint32_t*);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900141
Hungming Chendd4bfb92018-12-25 15:47:47 +0800142static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
143 int* herrno);
Bernie Innocenti455c6232018-09-12 21:32:42 +0900144static int dns_getaddrinfo(const char* name, const addrinfo* pai,
lifr94981782019-05-17 21:15:19 +0800145 const android_net_context* netcontext, addrinfo** rv,
146 NetworkDnsEventReported* event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900147static void _sethtent(FILE**);
148static void _endhtent(FILE**);
149static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti455c6232018-09-12 21:32:42 +0900150static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900151static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900152
Hungming Chen947aab02018-12-27 18:33:19 +0800153static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
154static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yubfb1b342018-11-06 15:42:36 +0800155static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen947aab02018-12-27 18:33:19 +0800156 int* herrno);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900157
Bernie Innocenti4e374b62018-12-12 00:43:02 +0900158const char* const ai_errlist[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900159 "Success",
160 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
161 "Temporary failure in name resolution", /* EAI_AGAIN */
162 "Invalid value for ai_flags", /* EAI_BADFLAGS */
163 "Non-recoverable failure in name resolution", /* EAI_FAIL */
164 "ai_family not supported", /* EAI_FAMILY */
165 "Memory allocation failure", /* EAI_MEMORY */
166 "No address associated with hostname", /* EAI_NODATA */
167 "hostname nor servname provided, or not known", /* EAI_NONAME */
168 "servname not supported for ai_socktype", /* EAI_SERVICE */
169 "ai_socktype not supported", /* EAI_SOCKTYPE */
170 "System error returned in errno", /* EAI_SYSTEM */
171 "Invalid value for hints", /* EAI_BADHINTS */
172 "Resolved protocol is unknown", /* EAI_PROTOCOL */
173 "Argument buffer overflow", /* EAI_OVERFLOW */
174 "Unknown error", /* EAI_MAX */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900175};
176
177/* XXX macros that make external reference is BAD. */
178
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900179#define GET_AI(ai, afd, addr) \
180 do { \
181 /* external reference: pai, error, and label free */ \
182 (ai) = get_ai(pai, (afd), (addr)); \
183 if ((ai) == NULL) { \
184 error = EAI_MEMORY; \
185 goto free; \
186 } \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900187 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900188
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900189#define GET_PORT(ai, serv) \
190 do { \
191 /* external reference: error and label free */ \
192 error = get_port((ai), (serv), 0); \
193 if (error != 0) goto free; \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900194 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900195
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900196#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900197 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
198#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900199
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900200const char* gai_strerror(int ecode) {
201 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
202 return ai_errlist[ecode];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900203}
204
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900205void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900206 while (ai) {
207 struct addrinfo* next = ai->ai_next;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900208 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900209 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900210 free(ai);
211 ai = next;
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900212 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900213}
214
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900215static int str2number(const char* p) {
216 char* ep;
217 unsigned long v;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900218
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900219 assert(p != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900220
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900221 if (*p == '\0') return -1;
222 ep = NULL;
223 errno = 0;
224 v = strtoul(p, &ep, 10);
225 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
226 return v;
227 else
228 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900229}
230
231/*
232 * The following functions determine whether IPv4 or IPv6 connectivity is
233 * available in order to implement AI_ADDRCONFIG.
234 *
235 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
236 * available, but whether addresses of the specified family are "configured
237 * on the local system". However, bionic doesn't currently support getifaddrs,
238 * so checking for connectivity is the next best thing.
239 */
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900240static int have_ipv6(unsigned mark, uid_t uid) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900241 static const struct sockaddr_in6 sin6_test = {
242 .sin6_family = AF_INET6,
243 .sin6_addr.s6_addr = {// 2000::
244 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachenb980f2f2018-10-23 17:10:58 +0800245 sockaddr_union addr = {.sin6 = sin6_test};
246 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900247}
248
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900249static int have_ipv4(unsigned mark, uid_t uid) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900250 static const struct sockaddr_in sin_test = {
251 .sin_family = AF_INET,
252 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
253 };
nuccachenb980f2f2018-10-23 17:10:58 +0800254 sockaddr_union addr = {.sin = sin_test};
255 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900256}
257
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900258// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
Mike Yuc7649d12019-05-22 15:28:07 +0800259// NOTE: also called by resolv_set_nameservers().
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900260int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
261 addrinfo** result) {
262 hints.ai_flags = AI_NUMERICHOST;
263 const android_net_context netcontext = {
264 .app_netid = NETID_UNSET,
265 .app_mark = MARK_UNSET,
266 .dns_netid = NETID_UNSET,
267 .dns_mark = MARK_UNSET,
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900268 .uid = NET_CONTEXT_INVALID_UID,
Praveen Moongalam Thyagarajan8ab18ba2019-09-04 14:46:50 -0700269 .pid = NET_CONTEXT_INVALID_PID,
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900270 };
lifr94981782019-05-17 21:15:19 +0800271 NetworkDnsEventReported event;
272 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result,
273 &event);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900274}
275
Luke Huang69e67182019-06-17 17:06:41 +0800276namespace {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900277
Luke Huang69e67182019-06-17 17:06:41 +0800278int validateHints(const addrinfo* _Nonnull hints) {
279 if (!hints) return EAI_BADHINTS;
280
281 // error check for hints
282 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
283 return EAI_BADHINTS;
284 }
285 if (hints->ai_flags & ~AI_MASK) {
286 return EAI_BADFLAGS;
287 }
288 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
289 hints->ai_family == PF_INET6)) {
290 return EAI_FAMILY;
291 }
292
Luke Huangd8ac4752019-06-18 17:05:47 +0800293 // Socket types which are not in explore_options.
294 switch (hints->ai_socktype) {
295 case SOCK_RAW:
296 case SOCK_DGRAM:
297 case SOCK_STREAM:
298 case ANY:
299 break;
300 default:
301 return EAI_SOCKTYPE;
302 }
303
Luke Huang69e67182019-06-17 17:06:41 +0800304 if (hints->ai_socktype == ANY || hints->ai_protocol == ANY) return 0;
305
306 // if both socktype/protocol are specified, check if they are meaningful combination.
307 for (const Explore& ex : explore_options) {
308 if (hints->ai_family != ex.e_af) continue;
309 if (ex.e_socktype == ANY) continue;
310 if (ex.e_protocol == ANY) continue;
311 if (hints->ai_socktype == ex.e_socktype && hints->ai_protocol != ex.e_protocol) {
312 return EAI_BADHINTS;
313 }
314 }
315
316 return 0;
317}
318
319} // namespace
320
321int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
322 const addrinfo* hints, const android_net_context* netcontext,
lifr94981782019-05-17 21:15:19 +0800323 addrinfo** res, NetworkDnsEventReported* event) {
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900324 // hostname is allowed to be nullptr
325 // servname is allowed to be nullptr
326 // hints is allowed to be nullptr
327 assert(res != nullptr);
328 assert(netcontext != nullptr);
lifr94981782019-05-17 21:15:19 +0800329 assert(event != nullptr);
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900330
Luke Huang69e67182019-06-17 17:06:41 +0800331 addrinfo sentinel = {};
332 addrinfo* cur = &sentinel;
333 int error = 0;
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900334
Ken Chene0d73c92018-11-07 01:20:48 +0800335 do {
Luke Huang69e67182019-06-17 17:06:41 +0800336 if (hostname == nullptr && servname == nullptr) {
Ken Chene0d73c92018-11-07 01:20:48 +0800337 error = EAI_NONAME;
338 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900339 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900340
Luke Huang69e67182019-06-17 17:06:41 +0800341 if (hints && (error = validateHints(hints))) break;
342 addrinfo ai = hints ? *hints : addrinfo{};
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900343
Luke Huang69e67182019-06-17 17:06:41 +0800344 // Check for special cases:
345 // (1) numeric servname is disallowed if socktype/protocol are left unspecified.
346 // (2) servname is disallowed for raw and other inet{,6} sockets.
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900347 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
Luke Huang69e67182019-06-17 17:06:41 +0800348 addrinfo tmp = ai;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900349 if (tmp.ai_family == PF_UNSPEC) {
350 tmp.ai_family = PF_INET6;
Ken Chene0d73c92018-11-07 01:20:48 +0800351 }
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900352 error = get_portmatch(&tmp, servname);
Ken Chene0d73c92018-11-07 01:20:48 +0800353 if (error) break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900354 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900355
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900356 // NULL hostname, or numeric hostname
357 for (const Explore& ex : explore_options) {
Ken Chene0d73c92018-11-07 01:20:48 +0800358 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900359 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900360
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900361 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
362 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
363 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900364
Luke Huang69e67182019-06-17 17:06:41 +0800365 addrinfo tmp = ai;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900366 if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
367 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
368 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chene0d73c92018-11-07 01:20:48 +0800369
Ken Chenffc224a2019-03-19 17:41:28 +0800370 LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900371 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900372 if (hostname == nullptr)
373 error = explore_null(&tmp, servname, &cur->ai_next);
Ken Chene0d73c92018-11-07 01:20:48 +0800374 else
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900375 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
Ken Chene0d73c92018-11-07 01:20:48 +0800376
377 if (error) break;
378
379 while (cur->ai_next) cur = cur->ai_next;
380 }
381 if (error) break;
382
Luke Huang69e67182019-06-17 17:06:41 +0800383 // If numeric representation of AF1 can be interpreted as FQDN
384 // representation of AF2, we need to think again about the code below.
Ken Chene0d73c92018-11-07 01:20:48 +0800385 if (sentinel.ai_next) break;
386
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900387 if (hostname == nullptr) {
Ken Chene0d73c92018-11-07 01:20:48 +0800388 error = EAI_NODATA;
389 break;
390 }
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900391 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chene0d73c92018-11-07 01:20:48 +0800392 error = EAI_NONAME;
393 break;
394 }
395
lifr94981782019-05-17 21:15:19 +0800396 return resolv_getaddrinfo(hostname, servname, hints, netcontext, res, event);
Ken Chene0d73c92018-11-07 01:20:48 +0800397 } while (0);
398
399 if (error) {
400 freeaddrinfo(sentinel.ai_next);
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900401 *res = nullptr;
Ken Chene0d73c92018-11-07 01:20:48 +0800402 } else {
403 *res = sentinel.ai_next;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900404 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900405 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900406}
407
Luke Huang69e67182019-06-17 17:06:41 +0800408int resolv_getaddrinfo(const char* _Nonnull hostname, const char* servname, const addrinfo* hints,
lifr94981782019-05-17 21:15:19 +0800409 const android_net_context* _Nonnull netcontext, addrinfo** _Nonnull res,
410 NetworkDnsEventReported* _Nonnull event) {
Luke Huang69e67182019-06-17 17:06:41 +0800411 if (hostname == nullptr && servname == nullptr) return EAI_NONAME;
412 if (hostname == nullptr) return EAI_NODATA;
413
Luke Huang69e67182019-06-17 17:06:41 +0800414 // servname is allowed to be nullptr
415 // hints is allowed to be nullptr
416 assert(res != nullptr);
417 assert(netcontext != nullptr);
lifr94981782019-05-17 21:15:19 +0800418 assert(event != nullptr);
Luke Huang69e67182019-06-17 17:06:41 +0800419
420 int error = EAI_FAIL;
421 if (hints && (error = validateHints(hints))) {
422 *res = nullptr;
423 return error;
424 }
425
426 addrinfo ai = hints ? *hints : addrinfo{};
427 addrinfo sentinel = {};
428 addrinfo* cur = &sentinel;
429 // hostname as alphanumeric name.
430 // We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
431 for (const Explore& ex : explore_options) {
432 // Require exact match for family field
433 if (ai.ai_family != ex.e_af) continue;
434
435 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
436
437 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
438
439 addrinfo tmp = ai;
440 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
441 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
442
443 LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family
444 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
lifr94981782019-05-17 21:15:19 +0800445 error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext, event);
Luke Huang69e67182019-06-17 17:06:41 +0800446
447 while (cur->ai_next) cur = cur->ai_next;
448 }
449
Luke Huangd8ac4752019-06-18 17:05:47 +0800450 // Propagate the last error from explore_fqdn(), but only when *all* attempts failed.
Luke Huang69e67182019-06-17 17:06:41 +0800451 if ((*res = sentinel.ai_next)) return 0;
452
Luke Huangd8ac4752019-06-18 17:05:47 +0800453 // TODO: consider removing freeaddrinfo.
Luke Huang69e67182019-06-17 17:06:41 +0800454 freeaddrinfo(sentinel.ai_next);
455 *res = nullptr;
Luke Huangd8ac4752019-06-18 17:05:47 +0800456 return (error == 0) ? EAI_FAIL : error;
Luke Huang69e67182019-06-17 17:06:41 +0800457}
458
Bernie Innocenti9c575932018-09-07 21:10:25 +0900459// FQDN hostname, DNS lookup
Luke Huang69e67182019-06-17 17:06:41 +0800460static int explore_fqdn(const addrinfo* pai, const char* hostname, const char* servname,
lifr94981782019-05-17 21:15:19 +0800461 addrinfo** res, const android_net_context* netcontext,
462 NetworkDnsEventReported* event) {
Luke Huang69e67182019-06-17 17:06:41 +0800463 assert(pai != nullptr);
464 // hostname may be nullptr
465 // servname may be nullptr
466 assert(res != nullptr);
467
468 addrinfo* result = nullptr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900469 int error = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900470
Luke Huang69e67182019-06-17 17:06:41 +0800471 // If the servname does not match socktype/protocol, return error code.
472 if ((error = get_portmatch(pai, servname))) return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900473
Bernie Innocenti455c6232018-09-12 21:32:42 +0900474 if (!files_getaddrinfo(hostname, pai, &result)) {
lifr94981782019-05-17 21:15:19 +0800475 error = dns_getaddrinfo(hostname, pai, netcontext, &result, event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900476 }
Luke Huang69e67182019-06-17 17:06:41 +0800477 if (error) {
478 freeaddrinfo(result);
479 return error;
Bernie Innocenti455c6232018-09-12 21:32:42 +0900480 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900481
Luke Huang69e67182019-06-17 17:06:41 +0800482 for (addrinfo* cur = result; cur; cur = cur->ai_next) {
483 // canonname should be filled already
484 if ((error = get_port(cur, servname, 0))) {
485 freeaddrinfo(result);
486 return error;
487 }
488 }
489 *res = result;
490 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900491}
492
493/*
494 * hostname == NULL.
495 * passive socket -> anyaddr (0.0.0.0 or ::)
496 * non-passive socket -> localhost (127.0.0.1 or ::1)
497 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900498static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
499 int s;
500 const struct afd* afd;
501 struct addrinfo* cur;
502 struct addrinfo sentinel;
503 int error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900504
Ken Chenffc224a2019-03-19 17:41:28 +0800505 LOG(DEBUG) << __func__;
506
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900507 assert(pai != NULL);
508 /* servname may be NULL */
509 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900510
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900511 *res = NULL;
512 sentinel.ai_next = NULL;
513 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900514
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900515 /*
516 * filter out AFs that are not supported by the kernel
517 * XXX errno?
518 */
519 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
520 if (s < 0) {
521 if (errno != EMFILE) return 0;
522 } else
523 close(s);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900524
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900525 /*
526 * if the servname does not match socktype/protocol, ignore it.
527 */
528 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900529
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900530 afd = find_afd(pai->ai_family);
531 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900532
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900533 if (pai->ai_flags & AI_PASSIVE) {
534 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900535 GET_PORT(cur->ai_next, servname);
536 } else {
537 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900538 GET_PORT(cur->ai_next, servname);
539 }
540 cur = cur->ai_next;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900541
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900542 *res = sentinel.ai_next;
543 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900544
545free:
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900546 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900547 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900548}
549
550/*
551 * numeric hostname
552 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900553static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
554 struct addrinfo** res, const char* canonname) {
555 const struct afd* afd;
556 struct addrinfo* cur;
557 struct addrinfo sentinel;
558 int error;
559 char pton[PTON_MAX];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900560
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900561 assert(pai != NULL);
562 /* hostname may be NULL */
563 /* servname may be NULL */
564 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900565
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900566 *res = NULL;
567 sentinel.ai_next = NULL;
568 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900569
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900570 /*
571 * if the servname does not match socktype/protocol, ignore it.
572 */
573 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900574
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900575 afd = find_afd(pai->ai_family);
576 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900577
Ken Chena4b33022018-10-17 00:19:59 +0800578 if (inet_pton(afd->a_af, hostname, pton) == 1) {
579 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
580 GET_AI(cur->ai_next, afd, pton);
581 GET_PORT(cur->ai_next, servname);
582 if ((pai->ai_flags & AI_CANONNAME)) {
583 /*
584 * Set the numeric address itself as
585 * the canonical name, based on a
586 * clarification in rfc2553bis-03.
587 */
Ken Chene0d73c92018-11-07 01:20:48 +0800588 error = get_canonname(pai, cur->ai_next, canonname);
589 if (error != 0) {
590 freeaddrinfo(sentinel.ai_next);
591 return error;
592 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900593 }
Ken Chena4b33022018-10-17 00:19:59 +0800594 while (cur->ai_next) cur = cur->ai_next;
595 } else
Ken Chene0d73c92018-11-07 01:20:48 +0800596 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900597 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900598
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900599 *res = sentinel.ai_next;
600 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900601
602free:
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900603 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900604 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900605}
606
607/*
608 * numeric hostname with scope
609 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900610static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
611 const char* servname, struct addrinfo** res) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900612 const struct afd* afd;
613 struct addrinfo* cur;
614 int error;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900615 const char *cp, *scope, *addr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900616 struct sockaddr_in6* sin6;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900617
Ken Chenffc224a2019-03-19 17:41:28 +0800618 LOG(DEBUG) << __func__;
619
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900620 assert(pai != NULL);
621 /* hostname may be NULL */
622 /* servname may be NULL */
623 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900624
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900625 /*
626 * if the servname does not match socktype/protocol, ignore it.
627 */
628 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900629
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900630 afd = find_afd(pai->ai_family);
631 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900632
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900633 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900634
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900635 cp = strchr(hostname, SCOPE_DELIMITER);
636 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900637
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900638 /*
639 * Handle special case of <scoped_address><delimiter><scope id>
640 */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900641 char* hostname2 = strdup(hostname);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900642 if (hostname2 == NULL) return EAI_MEMORY;
643 /* terminate at the delimiter */
644 hostname2[cp - hostname] = '\0';
645 addr = hostname2;
646 scope = cp + 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900647
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900648 error = explore_numeric(pai, addr, servname, res, hostname);
649 if (error == 0) {
chenbrucec51f1212019-09-12 16:59:33 +0800650 uint32_t scopeid;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900651
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900652 for (cur = *res; cur; cur = cur->ai_next) {
653 if (cur->ai_family != AF_INET6) continue;
654 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
655 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
656 free(hostname2);
657 return (EAI_NODATA); /* XXX: is return OK? */
658 }
659 sin6->sin6_scope_id = scopeid;
660 }
661 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900662
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900663 free(hostname2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900664
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900665 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900666}
667
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900668static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
669 assert(pai != NULL);
670 assert(ai != NULL);
671 assert(str != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900672
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900673 if ((pai->ai_flags & AI_CANONNAME) != 0) {
674 ai->ai_canonname = strdup(str);
675 if (ai->ai_canonname == NULL) return EAI_MEMORY;
676 }
677 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900678}
679
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900680static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
681 const char* addr) {
682 char* p;
683 struct addrinfo* ai;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900684
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900685 assert(pai != NULL);
686 assert(afd != NULL);
687 assert(addr != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900688
nuccachena49c0ba2018-09-11 11:13:44 +0800689 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900690 if (ai == NULL) return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900691
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900692 memcpy(ai, pai, sizeof(struct addrinfo));
693 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachena49c0ba2018-09-11 11:13:44 +0800694 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900695
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900696 ai->ai_addrlen = afd->a_socklen;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900697 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
698 p = (char*) (void*) (ai->ai_addr);
699 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
700 return ai;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900701}
702
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900703static int get_portmatch(const struct addrinfo* ai, const char* servname) {
704 assert(ai != NULL);
705 /* servname may be NULL */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900706
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900707 return get_port(ai, servname, 1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900708}
709
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900710static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
711 const char* proto;
712 struct servent* sp;
713 int port;
714 int allownumeric;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900715
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900716 assert(ai != NULL);
717 /* servname may be NULL */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900718
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900719 if (servname == NULL) return 0;
720 switch (ai->ai_family) {
721 case AF_INET:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900722 case AF_INET6:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900723 break;
724 default:
725 return 0;
726 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900727
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900728 switch (ai->ai_socktype) {
729 case SOCK_RAW:
730 return EAI_SERVICE;
731 case SOCK_DGRAM:
732 case SOCK_STREAM:
733 allownumeric = 1;
734 break;
735 case ANY:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900736 allownumeric = 1;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900737 break;
738 default:
739 return EAI_SOCKTYPE;
740 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900741
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900742 port = str2number(servname);
743 if (port >= 0) {
744 if (!allownumeric) return EAI_SERVICE;
745 if (port < 0 || port > 65535) return EAI_SERVICE;
746 port = htons(port);
747 } else {
748 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900749
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900750 switch (ai->ai_socktype) {
751 case SOCK_DGRAM:
752 proto = "udp";
753 break;
754 case SOCK_STREAM:
755 proto = "tcp";
756 break;
757 default:
758 proto = NULL;
759 break;
760 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900761
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900762 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
763 port = sp->s_port;
764 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900765
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900766 if (!matchonly) {
767 switch (ai->ai_family) {
768 case AF_INET:
769 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
770 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900771 case AF_INET6:
772 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
773 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900774 }
775 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900776
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900777 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900778}
779
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900780static const struct afd* find_afd(int af) {
781 const struct afd* afd;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900782
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900783 if (af == PF_UNSPEC) return NULL;
784 for (afd = afdl; afd->a_af; afd++) {
785 if (afd->a_af == af) return afd;
786 }
787 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900788}
789
Bernie Innocenti9c575932018-09-07 21:10:25 +0900790// Convert a string to a scope identifier.
chenbrucec51f1212019-09-12 16:59:33 +0800791static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, uint32_t* scopeid) {
792 uint64_t lscopeid;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900793 struct in6_addr* a6;
794 char* ep;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900795
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900796 assert(scope != NULL);
797 assert(sin6 != NULL);
798 assert(scopeid != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900799
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900800 a6 = &sin6->sin6_addr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900801
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900802 /* empty scopeid portion is invalid */
803 if (*scope == '\0') return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900804
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900805 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
806 /*
807 * We currently assume a one-to-one mapping between links
808 * and interfaces, so we simply use interface indices for
809 * like-local scopes.
810 */
811 *scopeid = if_nametoindex(scope);
Bernie Innocenti63abf5b2019-06-11 21:46:51 +0900812 if (*scopeid != 0) return 0;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900813 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900814
Bernie Innocenti63abf5b2019-06-11 21:46:51 +0900815 // try to convert to a numeric id as a last resort
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900816 errno = 0;
817 lscopeid = strtoul(scope, &ep, 10);
chenbrucec51f1212019-09-12 16:59:33 +0800818 *scopeid = (uint32_t)(lscopeid & 0xffffffffUL);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900819 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
820 return 0;
821 else
822 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900823}
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900824
825/* code duplicate with gethnamaddr.c */
826
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900827#define BOUNDED_INCR(x) \
828 do { \
829 BOUNDS_CHECK(cp, x); \
830 cp += (x); \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900831 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900832
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900833#define BOUNDS_CHECK(ptr, count) \
834 do { \
835 if (eom - (ptr) < (count)) { \
Hungming Chendd4bfb92018-12-25 15:47:47 +0800836 *herrno = NO_RECOVERY; \
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900837 return NULL; \
838 } \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900839 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900840
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900841static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chendd4bfb92018-12-25 15:47:47 +0800842 const struct addrinfo* pai, int* herrno) {
Ken Chene0d73c92018-11-07 01:20:48 +0800843 struct addrinfo sentinel = {};
844 struct addrinfo *cur;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900845 struct addrinfo ai;
846 const struct afd* afd;
847 char* canonname;
848 const HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +0800849 const uint8_t* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900850 int n;
chenbrucec51f1212019-09-12 16:59:33 +0800851 const uint8_t* eom;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900852 char *bp, *ep;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900853 int type, ancount, qdcount;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900854 int haveanswer, had_error;
855 char tbuf[MAXDNAME];
856 int (*name_ok)(const char*);
857 char hostbuf[8 * 1024];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900858
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900859 assert(answer != NULL);
860 assert(qname != NULL);
861 assert(pai != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900862
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900863 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900864
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900865 canonname = NULL;
866 eom = answer->buf + anslen;
867 switch (qtype) {
868 case T_A:
869 case T_AAAA:
870 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
871 name_ok = res_hnok;
872 break;
873 default:
874 return NULL; /* XXX should be abort(); */
875 }
876 /*
877 * find first satisfactory answer
878 */
879 hp = &answer->hdr;
880 ancount = ntohs(hp->ancount);
881 qdcount = ntohs(hp->qdcount);
882 bp = hostbuf;
883 ep = hostbuf + sizeof hostbuf;
884 cp = answer->buf;
885 BOUNDED_INCR(HFIXEDSZ);
886 if (qdcount != 1) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800887 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900888 return (NULL);
889 }
890 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
891 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800892 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900893 return (NULL);
894 }
895 BOUNDED_INCR(n + QFIXEDSZ);
896 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
897 /* res_send() has already verified that the query name is the
898 * same as the one we sent; this just gets the expanded name
899 * (i.e., with the succeeding search-domain tacked on).
900 */
901 n = strlen(bp) + 1; /* for the \0 */
902 if (n >= MAXHOSTNAMELEN) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800903 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900904 return (NULL);
905 }
906 canonname = bp;
907 bp += n;
908 /* The qname can be abbreviated, but h_name is now absolute. */
909 qname = canonname;
910 }
911 haveanswer = 0;
912 had_error = 0;
913 while (ancount-- > 0 && cp < eom && !had_error) {
914 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
915 if ((n < 0) || !(*name_ok)(bp)) {
916 had_error++;
917 continue;
918 }
919 cp += n; /* name */
920 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce0d470422019-03-28 18:44:37 +0800921 type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900922 cp += INT16SZ; /* type */
chenbruce0d470422019-03-28 18:44:37 +0800923 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900924 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce0d470422019-03-28 18:44:37 +0800925 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900926 cp += INT16SZ; /* len */
927 BOUNDS_CHECK(cp, n);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900928 if (cl != C_IN) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900929 /* XXX - debug? syslog? */
930 cp += n;
931 continue; /* XXX - had_error++ ? */
932 }
933 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
934 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
935 if ((n < 0) || !(*name_ok)(tbuf)) {
936 had_error++;
937 continue;
938 }
939 cp += n;
940 /* Get canonical name. */
941 n = strlen(tbuf) + 1; /* for the \0 */
942 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
943 had_error++;
944 continue;
945 }
946 strlcpy(bp, tbuf, (size_t)(ep - bp));
947 canonname = bp;
948 bp += n;
949 continue;
950 }
951 if (qtype == T_ANY) {
952 if (!(type == T_A || type == T_AAAA)) {
953 cp += n;
954 continue;
955 }
956 } else if (type != qtype) {
957 if (type != T_KEY && type != T_SIG)
Ken Chenffc224a2019-03-19 17:41:28 +0800958 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
959 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900960 cp += n;
961 continue; /* XXX - had_error++ ? */
962 }
963 switch (type) {
964 case T_A:
965 case T_AAAA:
966 if (strcasecmp(canonname, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800967 LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp
968 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900969 cp += n;
970 continue; /* XXX - had_error++ ? */
971 }
972 if (type == T_A && n != INADDRSZ) {
973 cp += n;
974 continue;
975 }
976 if (type == T_AAAA && n != IN6ADDRSZ) {
977 cp += n;
978 continue;
979 }
980 if (type == T_AAAA) {
981 struct in6_addr in6;
982 memcpy(&in6, cp, IN6ADDRSZ);
983 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
984 cp += n;
985 continue;
986 }
987 }
988 if (!haveanswer) {
989 int nn;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900990
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900991 canonname = bp;
992 nn = strlen(bp) + 1; /* for the \0 */
993 bp += nn;
994 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900995
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900996 /* don't overwrite pai */
997 ai = *pai;
998 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
999 afd = find_afd(ai.ai_family);
1000 if (afd == NULL) {
1001 cp += n;
1002 continue;
1003 }
1004 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1005 if (cur->ai_next == NULL) had_error++;
1006 while (cur && cur->ai_next) cur = cur->ai_next;
1007 cp += n;
1008 break;
1009 default:
1010 abort();
1011 }
1012 if (!had_error) haveanswer++;
1013 }
1014 if (haveanswer) {
1015 if (!canonname)
1016 (void) get_canonname(pai, sentinel.ai_next, qname);
1017 else
1018 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chendd4bfb92018-12-25 15:47:47 +08001019 *herrno = NETDB_SUCCESS;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001020 return sentinel.ai_next;
1021 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001022
Hungming Chendd4bfb92018-12-25 15:47:47 +08001023 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001024 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001025}
1026
1027struct addrinfo_sort_elem {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001028 struct addrinfo* ai;
1029 int has_src_addr;
1030 sockaddr_union src_addr;
1031 int original_order;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001032};
1033
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001034static int _get_scope(const struct sockaddr* addr) {
1035 if (addr->sa_family == AF_INET6) {
1036 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1037 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1038 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1039 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1040 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1041 /*
1042 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1043 * link-local scope.
1044 */
1045 return IPV6_ADDR_SCOPE_LINKLOCAL;
1046 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1047 return IPV6_ADDR_SCOPE_SITELOCAL;
1048 } else {
1049 return IPV6_ADDR_SCOPE_GLOBAL;
1050 }
1051 } else if (addr->sa_family == AF_INET) {
1052 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1053 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001054
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001055 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1056 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1057 return IPV6_ADDR_SCOPE_LINKLOCAL;
1058 } else {
1059 /*
1060 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1061 * and shared addresses (100.64.0.0/10), are assigned global scope.
1062 */
1063 return IPV6_ADDR_SCOPE_GLOBAL;
1064 }
1065 } else {
1066 /*
1067 * This should never happen.
1068 * Return a scope with low priority as a last resort.
1069 */
1070 return IPV6_ADDR_SCOPE_NODELOCAL;
1071 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001072}
1073
1074/* These macros are modelled after the ones in <netinet/in6.h>. */
1075
1076/* RFC 4380, section 2.6 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001077#define IN6_IS_ADDR_TEREDO(a) \
1078 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001079
1080/* RFC 3056, section 2. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001081#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001082
1083/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001084#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001085
1086/*
1087 * Get the label for a given IPv4/IPv6 address.
1088 * RFC 6724, section 2.1.
1089 */
1090
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001091static int _get_label(const struct sockaddr* addr) {
1092 if (addr->sa_family == AF_INET) {
1093 return 4;
1094 } else if (addr->sa_family == AF_INET6) {
1095 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1096 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1097 return 0;
1098 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1099 return 4;
1100 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1101 return 2;
1102 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1103 return 5;
1104 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1105 return 13;
1106 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1107 return 3;
1108 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1109 return 11;
1110 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1111 return 12;
1112 } else {
1113 /* All other IPv6 addresses, including global unicast addresses. */
1114 return 1;
1115 }
1116 } else {
1117 /*
1118 * This should never happen.
1119 * Return a semi-random label as a last resort.
1120 */
1121 return 1;
1122 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001123}
1124
1125/*
1126 * Get the precedence for a given IPv4/IPv6 address.
1127 * RFC 6724, section 2.1.
1128 */
1129
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001130static int _get_precedence(const struct sockaddr* addr) {
1131 if (addr->sa_family == AF_INET) {
1132 return 35;
1133 } else if (addr->sa_family == AF_INET6) {
1134 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1135 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1136 return 50;
1137 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1138 return 35;
1139 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1140 return 30;
1141 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1142 return 5;
1143 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1144 return 3;
1145 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1146 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1147 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1148 return 1;
1149 } else {
1150 /* All other IPv6 addresses, including global unicast addresses. */
1151 return 40;
1152 }
1153 } else {
1154 return 1;
1155 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001156}
1157
1158/*
1159 * Find number of matching initial bits between the two addresses a1 and a2.
1160 */
1161
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001162static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1163 const char* p1 = (const char*) a1;
1164 const char* p2 = (const char*) a2;
1165 unsigned i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001166
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001167 for (i = 0; i < sizeof(*a1); ++i) {
1168 int x, j;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001169
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001170 if (p1[i] == p2[i]) {
1171 continue;
1172 }
1173 x = p1[i] ^ p2[i];
1174 for (j = 0; j < CHAR_BIT; ++j) {
1175 if (x & (1 << (CHAR_BIT - 1))) {
1176 return i * CHAR_BIT + j;
1177 }
1178 x <<= 1;
1179 }
1180 }
1181 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001182}
1183
1184/*
1185 * Compare two source/destination address pairs.
1186 * RFC 6724, section 6.
1187 */
1188
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001189static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1190 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1191 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1192 int scope_src1, scope_dst1, scope_match1;
1193 int scope_src2, scope_dst2, scope_match2;
1194 int label_src1, label_dst1, label_match1;
1195 int label_src2, label_dst2, label_match2;
1196 int precedence1, precedence2;
1197 int prefixlen1, prefixlen2;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001198
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001199 /* Rule 1: Avoid unusable destinations. */
1200 if (a1->has_src_addr != a2->has_src_addr) {
1201 return a2->has_src_addr - a1->has_src_addr;
1202 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001203
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001204 /* Rule 2: Prefer matching scope. */
nuccachenb980f2f2018-10-23 17:10:58 +08001205 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001206 scope_dst1 = _get_scope(a1->ai->ai_addr);
1207 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001208
nuccachenb980f2f2018-10-23 17:10:58 +08001209 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001210 scope_dst2 = _get_scope(a2->ai->ai_addr);
1211 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001212
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001213 if (scope_match1 != scope_match2) {
1214 return scope_match2 - scope_match1;
1215 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001216
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001217 /*
1218 * Rule 3: Avoid deprecated addresses.
1219 * TODO(sesse): We don't currently have a good way of finding this.
1220 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001221
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001222 /*
1223 * Rule 4: Prefer home addresses.
1224 * TODO(sesse): We don't currently have a good way of finding this.
1225 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001226
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001227 /* Rule 5: Prefer matching label. */
nuccachenb980f2f2018-10-23 17:10:58 +08001228 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001229 label_dst1 = _get_label(a1->ai->ai_addr);
1230 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001231
nuccachenb980f2f2018-10-23 17:10:58 +08001232 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001233 label_dst2 = _get_label(a2->ai->ai_addr);
1234 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001235
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001236 if (label_match1 != label_match2) {
1237 return label_match2 - label_match1;
1238 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001239
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001240 /* Rule 6: Prefer higher precedence. */
1241 precedence1 = _get_precedence(a1->ai->ai_addr);
1242 precedence2 = _get_precedence(a2->ai->ai_addr);
1243 if (precedence1 != precedence2) {
1244 return precedence2 - precedence1;
1245 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001246
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001247 /*
1248 * Rule 7: Prefer native transport.
1249 * TODO(sesse): We don't currently have a good way of finding this.
1250 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001251
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001252 /* Rule 8: Prefer smaller scope. */
1253 if (scope_dst1 != scope_dst2) {
1254 return scope_dst1 - scope_dst2;
1255 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001256
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001257 /*
1258 * Rule 9: Use longest matching prefix.
1259 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1260 * to work very well directly applied to IPv4. (glibc uses information from
1261 * the routing table for a custom IPv4 implementation here.)
1262 */
1263 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1264 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachenb980f2f2018-10-23 17:10:58 +08001265 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001266 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachenb980f2f2018-10-23 17:10:58 +08001267 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001268 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1269 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1270 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1271 if (prefixlen1 != prefixlen2) {
1272 return prefixlen2 - prefixlen1;
1273 }
1274 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001275
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001276 /*
1277 * Rule 10: Leave the order unchanged.
1278 * We need this since qsort() is not necessarily stable.
1279 */
1280 return a1->original_order - a2->original_order;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001281}
1282
1283/*
1284 * Find the source address that will be used if trying to connect to the given
1285 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1286 *
1287 * Returns 1 if a source address was found, 0 if the address is unreachable,
1288 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1289 * undefined.
1290 */
1291
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001292static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1293 uid_t uid) {
1294 int sock;
1295 int ret;
1296 socklen_t len;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001297
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001298 switch (addr->sa_family) {
1299 case AF_INET:
1300 len = sizeof(struct sockaddr_in);
1301 break;
1302 case AF_INET6:
1303 len = sizeof(struct sockaddr_in6);
1304 break;
1305 default:
1306 /* No known usable source address for non-INET families. */
1307 return 0;
1308 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001309
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001310 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1311 if (sock == -1) {
1312 if (errno == EAFNOSUPPORT) {
1313 return 0;
1314 } else {
1315 return -1;
1316 }
1317 }
1318 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1319 close(sock);
1320 return 0;
1321 }
1322 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1323 close(sock);
1324 return 0;
1325 }
1326 do {
Bernie Innocentiafaacf72018-08-30 07:34:37 +09001327 ret = connect(sock, addr, len);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001328 } while (ret == -1 && errno == EINTR);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001329
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001330 if (ret == -1) {
1331 close(sock);
1332 return 0;
1333 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001334
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001335 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1336 close(sock);
1337 return -1;
1338 }
1339 close(sock);
1340 return 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001341}
1342
1343/*
1344 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1345 * Will leave the list unchanged if an error occurs.
1346 */
1347
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001348static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1349 struct addrinfo* cur;
1350 int nelem = 0, i;
1351 struct addrinfo_sort_elem* elems;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001352
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001353 cur = list_sentinel->ai_next;
1354 while (cur) {
1355 ++nelem;
1356 cur = cur->ai_next;
1357 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001358
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001359 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1360 if (elems == NULL) {
1361 goto error;
1362 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001363
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001364 /*
1365 * Convert the linked list to an array that also contains the candidate
1366 * source address for each destination address.
1367 */
1368 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1369 int has_src_addr;
1370 assert(cur != NULL);
1371 elems[i].ai = cur;
1372 elems[i].original_order = i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001373
nuccachenb980f2f2018-10-23 17:10:58 +08001374 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001375 if (has_src_addr == -1) {
1376 goto error;
1377 }
1378 elems[i].has_src_addr = has_src_addr;
1379 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001380
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001381 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1382 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001383
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001384 list_sentinel->ai_next = elems[0].ai;
1385 for (i = 0; i < nelem - 1; ++i) {
1386 elems[i].ai->ai_next = elems[i + 1].ai;
1387 }
1388 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001389
1390error:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001391 free(elems);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001392}
1393
Bernie Innocenti455c6232018-09-12 21:32:42 +09001394static int dns_getaddrinfo(const char* name, const addrinfo* pai,
lifr94981782019-05-17 21:15:19 +08001395 const android_net_context* netcontext, addrinfo** rv,
1396 NetworkDnsEventReported* event) {
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001397 res_target q = {};
1398 res_target q2 = {};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001399
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001400 auto buf = std::make_unique<querybuf>();
1401 auto buf2 = std::make_unique<querybuf>();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001402
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001403 switch (pai->ai_family) {
Bernie Innocenti9c575932018-09-07 21:10:25 +09001404 case AF_UNSPEC: {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001405 /* prefer IPv6 */
1406 q.name = name;
1407 q.qclass = C_IN;
1408 q.answer = buf->buf;
1409 q.anslen = sizeof(buf->buf);
1410 int query_ipv6 = 1, query_ipv4 = 1;
1411 if (pai->ai_flags & AI_ADDRCONFIG) {
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001412 query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid);
1413 query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001414 }
1415 if (query_ipv6) {
1416 q.qtype = T_AAAA;
1417 if (query_ipv4) {
1418 q.next = &q2;
1419 q2.name = name;
1420 q2.qclass = C_IN;
1421 q2.qtype = T_A;
1422 q2.answer = buf2->buf;
1423 q2.anslen = sizeof(buf2->buf);
1424 }
1425 } else if (query_ipv4) {
1426 q.qtype = T_A;
1427 } else {
Bernie Innocenti455c6232018-09-12 21:32:42 +09001428 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001429 }
1430 break;
Bernie Innocenti9c575932018-09-07 21:10:25 +09001431 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001432 case AF_INET:
1433 q.name = name;
1434 q.qclass = C_IN;
1435 q.qtype = T_A;
1436 q.answer = buf->buf;
1437 q.anslen = sizeof(buf->buf);
1438 break;
1439 case AF_INET6:
1440 q.name = name;
1441 q.qclass = C_IN;
1442 q.qtype = T_AAAA;
1443 q.answer = buf->buf;
1444 q.anslen = sizeof(buf->buf);
1445 break;
1446 default:
Bernie Innocenti455c6232018-09-12 21:32:42 +09001447 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001448 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001449
Bernie Innocenti08487112019-10-11 21:14:13 +09001450 ResState res;
1451 res_init(&res, netcontext, event);
Mike Yubfb1b342018-11-06 15:42:36 +08001452
Hungming Chena6914a62019-01-19 15:07:04 +08001453 int he;
Bernie Innocenti08487112019-10-11 21:14:13 +09001454 if (res_searchN(name, &q, &res, &he) < 0) {
Hungming Chena6914a62019-01-19 15:07:04 +08001455 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
1456 // Note that res_searchN() doesn't set the pair NETDB_INTERNAL and errno.
1457 // See also herrnoToAiErrno().
1458 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001459 }
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001460
1461 addrinfo sentinel = {};
1462 addrinfo* cur = &sentinel;
Hungming Chena6914a62019-01-19 15:07:04 +08001463 addrinfo* ai = getanswer(buf.get(), q.n, q.name, q.qtype, pai, &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001464 if (ai) {
1465 cur->ai_next = ai;
1466 while (cur && cur->ai_next) cur = cur->ai_next;
1467 }
1468 if (q.next) {
Hungming Chena6914a62019-01-19 15:07:04 +08001469 ai = getanswer(buf2.get(), q2.n, q2.name, q2.qtype, pai, &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001470 if (ai) cur->ai_next = ai;
1471 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001472 if (sentinel.ai_next == NULL) {
Hungming Chena6914a62019-01-19 15:07:04 +08001473 // Note that getanswer() doesn't set the pair NETDB_INTERNAL and errno.
1474 // See also herrnoToAiErrno().
1475 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001476 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001477
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001478 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001479
Bernie Innocenti455c6232018-09-12 21:32:42 +09001480 *rv = sentinel.ai_next;
1481 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001482}
1483
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001484static void _sethtent(FILE** hostf) {
1485 if (!*hostf)
1486 *hostf = fopen(_PATH_HOSTS, "re");
1487 else
1488 rewind(*hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001489}
1490
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001491static void _endhtent(FILE** hostf) {
1492 if (*hostf) {
1493 (void) fclose(*hostf);
1494 *hostf = NULL;
1495 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001496}
1497
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001498static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1499 char* p;
1500 char *cp, *tname, *cname;
Bernie Innocentie2bc46f2018-10-16 23:35:28 +09001501 struct addrinfo *res0, *res;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001502 int error;
1503 const char* addr;
1504 char hostbuf[8 * 1024];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001505
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001506 assert(name != NULL);
1507 assert(pai != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001508
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001509 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1510again:
1511 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1512 if (*p == '#') goto again;
1513 if (!(cp = strpbrk(p, "#\n"))) goto again;
1514 *cp = '\0';
1515 if (!(cp = strpbrk(p, " \t"))) goto again;
1516 *cp++ = '\0';
1517 addr = p;
1518 /* if this is not something we're looking for, skip it. */
1519 cname = NULL;
1520 while (cp && *cp) {
1521 if (*cp == ' ' || *cp == '\t') {
1522 cp++;
1523 continue;
1524 }
1525 if (!cname) cname = cp;
1526 tname = cp;
1527 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001528 if (strcasecmp(name, tname) == 0) goto found;
1529 }
1530 goto again;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001531
1532found:
Bernie Innocentie2bc46f2018-10-16 23:35:28 +09001533 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001534 if (error) goto again;
1535 for (res = res0; res; res = res->ai_next) {
1536 /* cover it up */
1537 res->ai_flags = pai->ai_flags;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001538
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001539 if (pai->ai_flags & AI_CANONNAME) {
1540 if (get_canonname(pai, res, cname) != 0) {
1541 freeaddrinfo(res0);
1542 goto again;
1543 }
1544 }
1545 }
1546 return res0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001547}
1548
Bernie Innocenti455c6232018-09-12 21:32:42 +09001549static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chene0d73c92018-11-07 01:20:48 +08001550 struct addrinfo sentinel = {};
1551 struct addrinfo *p, *cur;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001552 FILE* hostf = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001553
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001554 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001555
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001556 _sethtent(&hostf);
1557 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1558 cur->ai_next = p;
1559 while (cur && cur->ai_next) cur = cur->ai_next;
1560 }
1561 _endhtent(&hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001562
Bernie Innocenti455c6232018-09-12 21:32:42 +09001563 *res = sentinel.ai_next;
1564 return sentinel.ai_next != NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001565}
1566
1567/* resolver logic */
1568
1569/*
1570 * Formulate a normal query, send, and await answer.
1571 * Returned answer is placed in supplied buffer "answer".
1572 * Perform preliminary check of answer, returning success only
1573 * if no error is indicated and the answer count is nonzero.
1574 * Return the size of the response on success, -1 on error.
Hungming Chendd4bfb92018-12-25 15:47:47 +08001575 * Error number is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001576 *
1577 * Caller must parse answer and determine whether it answers the question.
1578 */
Hungming Chen947aab02018-12-27 18:33:19 +08001579static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
chenbrucec51f1212019-09-12 16:59:33 +08001580 uint8_t buf[MAXPACKET];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001581 HEADER* hp;
1582 int n;
1583 struct res_target* t;
1584 int rcode;
1585 int ancount;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001586
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001587 assert(name != NULL);
1588 /* XXX: target may be NULL??? */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001589
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001590 rcode = NOERROR;
1591 ancount = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001592
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001593 for (t = target; t; t = t->next) {
chenbrucec51f1212019-09-12 16:59:33 +08001594 uint8_t* answer;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001595 int anslen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001596
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001597 hp = (HEADER*) (void*) t->answer;
Ken Chen0a015532019-01-02 14:59:38 +08001598 bool retried = false;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001599 again:
1600 hp->rcode = NOERROR; /* default */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001601
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001602 /* make it easier... */
Bernie Innocenti9c575932018-09-07 21:10:25 +09001603 int cl = t->qclass;
1604 int type = t->qtype;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001605 answer = t->answer;
1606 anslen = t->anslen;
chenbruceacb832c2019-02-20 19:45:50 +08001607
Ken Chenffc224a2019-03-19 17:41:28 +08001608 LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001609
Bernie Innocenti08487112019-10-11 21:14:13 +09001610 n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, sizeof(buf),
1611 res->netcontext_flags);
chenbruced8cbb9b2019-06-20 18:25:28 +08001612 if (n > 0 &&
1613 (res->netcontext_flags &
1614 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
1615 !retried) // TODO: remove the retry flag and provide a sufficient test coverage.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001616 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001617 if (n <= 0) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001618 LOG(ERROR) << __func__ << ": res_nmkquery failed";
Hungming Chendd4bfb92018-12-25 15:47:47 +08001619 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001620 return n;
1621 }
Mike Yubfb1b342018-11-06 15:42:36 +08001622
Luke Huangba7bef92018-12-26 16:53:03 +08001623 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001624 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen947aab02018-12-27 18:33:19 +08001625 // Record rcode from DNS response header only if no timeout.
1626 // Keep rcode timeout for reporting later if any.
chenbruced8cbb9b2019-06-20 18:25:28 +08001627 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; // record most recent error
1628 // if the query choked with EDNS0, retry without EDNS0 that when the server
1629 // has no response, resovler won't retry and do nothing. Even fallback to UDP,
1630 // we also has the same symptom if EDNS is enabled.
1631 if ((res->netcontext_flags &
1632 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
Ken Chen0a015532019-01-02 14:59:38 +08001633 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001634 LOG(DEBUG) << __func__ << ": retry without EDNS0";
Ken Chen0a015532019-01-02 14:59:38 +08001635 retried = true;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001636 goto again;
1637 }
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001638 LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001639 continue;
1640 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001641
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001642 ancount += ntohs(hp->ancount);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001643
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001644 t->n = n;
1645 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001646
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001647 if (ancount == 0) {
1648 switch (rcode) {
Hungming Chen947aab02018-12-27 18:33:19 +08001649 // Not defined in RFC.
1650 case RCODE_TIMEOUT:
1651 // DNS metrics monitors DNS query timeout.
1652 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1653 break;
1654 // Defined in RFC 1035 section 4.1.1.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001655 case NXDOMAIN:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001656 *herrno = HOST_NOT_FOUND;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001657 break;
1658 case SERVFAIL:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001659 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001660 break;
1661 case NOERROR:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001662 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001663 break;
1664 case FORMERR:
1665 case NOTIMP:
1666 case REFUSED:
1667 default:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001668 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001669 break;
1670 }
1671 return -1;
1672 }
1673 return ancount;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001674}
1675
1676/*
1677 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1678 * Return the size of the response on success, -1 on error.
1679 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chendd4bfb92018-12-25 15:47:47 +08001680 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001681 */
Hungming Chen947aab02018-12-27 18:33:19 +08001682static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Luke Huang2dac4382019-06-24 13:28:44 +08001683 const char* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001684 HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +08001685 uint32_t dots;
chenbruce018fdb22019-06-12 18:08:04 +08001686 int ret, saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001687 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001688
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001689 assert(name != NULL);
1690 assert(target != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001691
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001692 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001693
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001694 errno = 0;
Hungming Chendd4bfb92018-12-25 15:47:47 +08001695 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001696 dots = 0;
1697 for (cp = name; *cp; cp++) dots += (*cp == '.');
chenbruce018fdb22019-06-12 18:08:04 +08001698 const bool trailing_dot = (cp > name && *--cp == '.') ? true : false;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001699
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001700 /*
1701 * If there are dots in the name already, let's just give it a try
1702 * 'as is'. The threshold can be set with the "ndots" option.
1703 */
1704 saved_herrno = -1;
1705 if (dots >= res->ndots) {
Hungming Chen947aab02018-12-27 18:33:19 +08001706 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001707 if (ret > 0) return (ret);
Hungming Chendd4bfb92018-12-25 15:47:47 +08001708 saved_herrno = *herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001709 tried_as_is++;
1710 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001711
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001712 /*
1713 * We do at least one level of search if
chenbruce018fdb22019-06-12 18:08:04 +08001714 * - there is no dot, or
1715 * - there is at least one dot and there is no trailing dot.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001716 */
chenbruce018fdb22019-06-12 18:08:04 +08001717 if ((!dots) || (dots && !trailing_dot)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001718 int done = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001719
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001720 /* Unfortunately we need to set stuff up before
1721 * the domain stuff is tried. Will have a better
1722 * fix after thread pools are used.
1723 */
1724 _resolv_populate_res_for_net(res);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001725
Luke Huang2dac4382019-06-24 13:28:44 +08001726 for (const auto& domain : res->search_domains) {
1727 ret = res_querydomainN(name, domain.c_str(), target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001728 if (ret > 0) return ret;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001729
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001730 /*
1731 * If no server present, give up.
1732 * If name isn't found in this domain,
1733 * keep trying higher domains in the search list
1734 * (if that's enabled).
1735 * On a NO_DATA error, keep trying, otherwise
1736 * a wildcard entry of another type could keep us
1737 * from finding this entry higher in the domain.
1738 * If we get some other error (negative answer or
1739 * server failure), then stop searching up,
1740 * but try the input name below in case it's
1741 * fully-qualified.
1742 */
1743 if (errno == ECONNREFUSED) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001744 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001745 return -1;
1746 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001747
Hungming Chendd4bfb92018-12-25 15:47:47 +08001748 switch (*herrno) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001749 case NO_DATA:
1750 got_nodata++;
Bernie Innocentif40b3bd2018-10-10 22:30:12 +09001751 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001752 case HOST_NOT_FOUND:
1753 /* keep trying */
1754 break;
1755 case TRY_AGAIN:
1756 if (hp->rcode == SERVFAIL) {
1757 /* try next search element, if any */
1758 got_servfail++;
1759 break;
1760 }
Bernie Innocentif40b3bd2018-10-10 22:30:12 +09001761 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001762 default:
1763 /* anything else implies that we're done */
1764 done++;
1765 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001766 }
1767 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001768
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001769 /*
1770 * if we have not already tried the name "as is", do that now.
1771 * note that we do this regardless of how many dots were in the
1772 * name or whether it ends with a dot.
1773 */
1774 if (!tried_as_is) {
Hungming Chen947aab02018-12-27 18:33:19 +08001775 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001776 if (ret > 0) return ret;
1777 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001778
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001779 /*
1780 * if we got here, we didn't satisfy the search.
1781 * if we did an initial full query, return that query's h_errno
1782 * (note that we wouldn't be here if that query had succeeded).
1783 * else if we ever got a nodata, send that back as the reason.
1784 * else send back meaningless h_errno, that being the one from
1785 * the last DNSRCH we did.
1786 */
1787 if (saved_herrno != -1)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001788 *herrno = saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001789 else if (got_nodata)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001790 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001791 else if (got_servfail)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001792 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001793 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001794}
1795
1796/*
1797 * Perform a call on res_query on the concatenation of name and domain,
1798 * removing a trailing dot from name if domain is NULL.
1799 */
Mike Yubfb1b342018-11-06 15:42:36 +08001800static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen947aab02018-12-27 18:33:19 +08001801 int* herrno) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001802 char nbuf[MAXDNAME];
1803 const char* longname = nbuf;
1804 size_t n, d;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001805
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001806 assert(name != NULL);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001807
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001808 if (domain == NULL) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001809 // Check for trailing '.'; copy without '.' if present.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001810 n = strlen(name);
1811 if (n + 1 > sizeof(nbuf)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001812 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001813 return -1;
1814 }
1815 if (n > 0 && name[--n] == '.') {
1816 strncpy(nbuf, name, n);
1817 nbuf[n] = '\0';
1818 } else
1819 longname = name;
1820 } else {
1821 n = strlen(name);
1822 d = strlen(domain);
1823 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001824 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001825 return -1;
1826 }
1827 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1828 }
Hungming Chen947aab02018-12-27 18:33:19 +08001829 return res_queryN(longname, target, res, herrno);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001830}