blob: 420725106baf3978fbdb80ac6ca00051e3afd6fd [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"
59#include "resolv_cache.h"
60#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090061
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090062#define ANY 0
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090063
lifr94981782019-05-17 21:15:19 +080064using android::net::NetworkDnsEventReported;
65
Bernie Innocenti4e374b62018-12-12 00:43:02 +090066const char in_addrany[] = {0, 0, 0, 0};
67const char in_loopback[] = {127, 0, 0, 1};
68const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
69const 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 +090070
Bernie Innocenti4e374b62018-12-12 00:43:02 +090071const struct afd {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090072 int a_af;
73 int a_addrlen;
74 int a_socklen;
75 int a_off;
76 const char* a_addrany;
77 const char* a_loopback;
78 int a_scoped;
79} afdl[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090080 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
81 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090082 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
83 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
84 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090085};
86
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090087struct Explore {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090088 int e_af;
89 int e_socktype;
90 int e_protocol;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090091 int e_wild;
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090092#define WILD_AF(ex) ((ex).e_wild & 0x01)
93#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
94#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090095};
96
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090097const Explore explore_options[] = {
Ken Chene0d73c92018-11-07 01:20:48 +080098 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
99 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
100 {PF_INET6, SOCK_RAW, ANY, 0x05},
101 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
102 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
103 {PF_INET, SOCK_RAW, ANY, 0x05},
104 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
105 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
106 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900107};
108
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900109#define PTON_MAX 16
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900110#define MAXPACKET (8 * 1024)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900111
112typedef union {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900113 HEADER hdr;
chenbrucec51f1212019-09-12 16:59:33 +0800114 uint8_t buf[MAXPACKET];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900115} querybuf;
116
117struct res_target {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900118 struct res_target* next;
119 const char* name; /* domain name */
120 int qclass, qtype; /* class and type of query */
chenbrucec51f1212019-09-12 16:59:33 +0800121 uint8_t* answer; /* buffer to put answer */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900122 int anslen; /* size of answer buffer */
123 int n; /* result length */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900124};
125
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900126static int str2number(const char*);
127static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
lifr94981782019-05-17 21:15:19 +0800128 const struct android_net_context*, NetworkDnsEventReported* event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900129static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
130static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
131 const char*);
132static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
133 struct addrinfo**);
134static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
135static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
136static int get_portmatch(const struct addrinfo*, const char*);
137static int get_port(const struct addrinfo*, const char*, int);
138static const struct afd* find_afd(int);
chenbrucec51f1212019-09-12 16:59:33 +0800139static int ip6_str2scopeid(const char*, struct sockaddr_in6*, uint32_t*);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900140
Hungming Chendd4bfb92018-12-25 15:47:47 +0800141static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
142 int* herrno);
Bernie Innocenti455c6232018-09-12 21:32:42 +0900143static int dns_getaddrinfo(const char* name, const addrinfo* pai,
lifr94981782019-05-17 21:15:19 +0800144 const android_net_context* netcontext, addrinfo** rv,
145 NetworkDnsEventReported* event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900146static void _sethtent(FILE**);
147static void _endhtent(FILE**);
148static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti455c6232018-09-12 21:32:42 +0900149static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900150static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900151
Hungming Chen947aab02018-12-27 18:33:19 +0800152static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
153static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yubfb1b342018-11-06 15:42:36 +0800154static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen947aab02018-12-27 18:33:19 +0800155 int* herrno);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900156
Bernie Innocenti4e374b62018-12-12 00:43:02 +0900157const char* const ai_errlist[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900158 "Success",
159 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
160 "Temporary failure in name resolution", /* EAI_AGAIN */
161 "Invalid value for ai_flags", /* EAI_BADFLAGS */
162 "Non-recoverable failure in name resolution", /* EAI_FAIL */
163 "ai_family not supported", /* EAI_FAMILY */
164 "Memory allocation failure", /* EAI_MEMORY */
165 "No address associated with hostname", /* EAI_NODATA */
166 "hostname nor servname provided, or not known", /* EAI_NONAME */
167 "servname not supported for ai_socktype", /* EAI_SERVICE */
168 "ai_socktype not supported", /* EAI_SOCKTYPE */
169 "System error returned in errno", /* EAI_SYSTEM */
170 "Invalid value for hints", /* EAI_BADHINTS */
171 "Resolved protocol is unknown", /* EAI_PROTOCOL */
172 "Argument buffer overflow", /* EAI_OVERFLOW */
173 "Unknown error", /* EAI_MAX */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900174};
175
176/* XXX macros that make external reference is BAD. */
177
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900178#define GET_AI(ai, afd, addr) \
179 do { \
180 /* external reference: pai, error, and label free */ \
181 (ai) = get_ai(pai, (afd), (addr)); \
182 if ((ai) == NULL) { \
183 error = EAI_MEMORY; \
184 goto free; \
185 } \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900186 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900187
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900188#define GET_PORT(ai, serv) \
189 do { \
190 /* external reference: error and label free */ \
191 error = get_port((ai), (serv), 0); \
192 if (error != 0) goto free; \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900193 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900194
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900195#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900196 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
197#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900198
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900199const char* gai_strerror(int ecode) {
200 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
201 return ai_errlist[ecode];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900202}
203
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900204void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900205 while (ai) {
206 struct addrinfo* next = ai->ai_next;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900207 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900208 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900209 free(ai);
210 ai = next;
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900211 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900212}
213
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900214static int str2number(const char* p) {
215 char* ep;
216 unsigned long v;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900217
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900218 assert(p != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900219
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900220 if (*p == '\0') return -1;
221 ep = NULL;
222 errno = 0;
223 v = strtoul(p, &ep, 10);
224 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
225 return v;
226 else
227 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900228}
229
230/*
231 * The following functions determine whether IPv4 or IPv6 connectivity is
232 * available in order to implement AI_ADDRCONFIG.
233 *
234 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
235 * available, but whether addresses of the specified family are "configured
236 * on the local system". However, bionic doesn't currently support getifaddrs,
237 * so checking for connectivity is the next best thing.
238 */
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900239static int have_ipv6(unsigned mark, uid_t uid) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900240 static const struct sockaddr_in6 sin6_test = {
241 .sin6_family = AF_INET6,
242 .sin6_addr.s6_addr = {// 2000::
243 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachenb980f2f2018-10-23 17:10:58 +0800244 sockaddr_union addr = {.sin6 = sin6_test};
245 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900246}
247
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900248static int have_ipv4(unsigned mark, uid_t uid) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900249 static const struct sockaddr_in sin_test = {
250 .sin_family = AF_INET,
251 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
252 };
nuccachenb980f2f2018-10-23 17:10:58 +0800253 sockaddr_union addr = {.sin = sin_test};
254 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900255}
256
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900257// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
Mike Yuc7649d12019-05-22 15:28:07 +0800258// NOTE: also called by resolv_set_nameservers().
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900259int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
260 addrinfo** result) {
261 hints.ai_flags = AI_NUMERICHOST;
262 const android_net_context netcontext = {
263 .app_netid = NETID_UNSET,
264 .app_mark = MARK_UNSET,
265 .dns_netid = NETID_UNSET,
266 .dns_mark = MARK_UNSET,
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900267 .uid = NET_CONTEXT_INVALID_UID,
268 };
lifr94981782019-05-17 21:15:19 +0800269 NetworkDnsEventReported event;
270 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result,
271 &event);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900272}
273
Luke Huang69e67182019-06-17 17:06:41 +0800274namespace {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900275
Luke Huang69e67182019-06-17 17:06:41 +0800276int validateHints(const addrinfo* _Nonnull hints) {
277 if (!hints) return EAI_BADHINTS;
278
279 // error check for hints
280 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
281 return EAI_BADHINTS;
282 }
283 if (hints->ai_flags & ~AI_MASK) {
284 return EAI_BADFLAGS;
285 }
286 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
287 hints->ai_family == PF_INET6)) {
288 return EAI_FAMILY;
289 }
290
Luke Huangd8ac4752019-06-18 17:05:47 +0800291 // Socket types which are not in explore_options.
292 switch (hints->ai_socktype) {
293 case SOCK_RAW:
294 case SOCK_DGRAM:
295 case SOCK_STREAM:
296 case ANY:
297 break;
298 default:
299 return EAI_SOCKTYPE;
300 }
301
Luke Huang69e67182019-06-17 17:06:41 +0800302 if (hints->ai_socktype == ANY || hints->ai_protocol == ANY) return 0;
303
304 // if both socktype/protocol are specified, check if they are meaningful combination.
305 for (const Explore& ex : explore_options) {
306 if (hints->ai_family != ex.e_af) continue;
307 if (ex.e_socktype == ANY) continue;
308 if (ex.e_protocol == ANY) continue;
309 if (hints->ai_socktype == ex.e_socktype && hints->ai_protocol != ex.e_protocol) {
310 return EAI_BADHINTS;
311 }
312 }
313
314 return 0;
315}
316
317} // namespace
318
319int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
320 const addrinfo* hints, const android_net_context* netcontext,
lifr94981782019-05-17 21:15:19 +0800321 addrinfo** res, NetworkDnsEventReported* event) {
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900322 // hostname is allowed to be nullptr
323 // servname is allowed to be nullptr
324 // hints is allowed to be nullptr
325 assert(res != nullptr);
326 assert(netcontext != nullptr);
lifr94981782019-05-17 21:15:19 +0800327 assert(event != nullptr);
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900328
Luke Huang69e67182019-06-17 17:06:41 +0800329 addrinfo sentinel = {};
330 addrinfo* cur = &sentinel;
331 int error = 0;
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900332
Ken Chene0d73c92018-11-07 01:20:48 +0800333 do {
Luke Huang69e67182019-06-17 17:06:41 +0800334 if (hostname == nullptr && servname == nullptr) {
Ken Chene0d73c92018-11-07 01:20:48 +0800335 error = EAI_NONAME;
336 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900337 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900338
Luke Huang69e67182019-06-17 17:06:41 +0800339 if (hints && (error = validateHints(hints))) break;
340 addrinfo ai = hints ? *hints : addrinfo{};
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900341
Luke Huang69e67182019-06-17 17:06:41 +0800342 // Check for special cases:
343 // (1) numeric servname is disallowed if socktype/protocol are left unspecified.
344 // (2) servname is disallowed for raw and other inet{,6} sockets.
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900345 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
Luke Huang69e67182019-06-17 17:06:41 +0800346 addrinfo tmp = ai;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900347 if (tmp.ai_family == PF_UNSPEC) {
348 tmp.ai_family = PF_INET6;
Ken Chene0d73c92018-11-07 01:20:48 +0800349 }
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900350 error = get_portmatch(&tmp, servname);
Ken Chene0d73c92018-11-07 01:20:48 +0800351 if (error) break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900352 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900353
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900354 // NULL hostname, or numeric hostname
355 for (const Explore& ex : explore_options) {
Ken Chene0d73c92018-11-07 01:20:48 +0800356 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900357 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900358
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900359 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
360 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
361 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900362
Luke Huang69e67182019-06-17 17:06:41 +0800363 addrinfo tmp = ai;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900364 if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
365 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
366 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chene0d73c92018-11-07 01:20:48 +0800367
Ken Chenffc224a2019-03-19 17:41:28 +0800368 LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900369 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900370 if (hostname == nullptr)
371 error = explore_null(&tmp, servname, &cur->ai_next);
Ken Chene0d73c92018-11-07 01:20:48 +0800372 else
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900373 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
Ken Chene0d73c92018-11-07 01:20:48 +0800374
375 if (error) break;
376
377 while (cur->ai_next) cur = cur->ai_next;
378 }
379 if (error) break;
380
Luke Huang69e67182019-06-17 17:06:41 +0800381 // If numeric representation of AF1 can be interpreted as FQDN
382 // representation of AF2, we need to think again about the code below.
Ken Chene0d73c92018-11-07 01:20:48 +0800383 if (sentinel.ai_next) break;
384
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900385 if (hostname == nullptr) {
Ken Chene0d73c92018-11-07 01:20:48 +0800386 error = EAI_NODATA;
387 break;
388 }
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900389 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chene0d73c92018-11-07 01:20:48 +0800390 error = EAI_NONAME;
391 break;
392 }
393
lifr94981782019-05-17 21:15:19 +0800394 return resolv_getaddrinfo(hostname, servname, hints, netcontext, res, event);
Ken Chene0d73c92018-11-07 01:20:48 +0800395 } while (0);
396
397 if (error) {
398 freeaddrinfo(sentinel.ai_next);
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900399 *res = nullptr;
Ken Chene0d73c92018-11-07 01:20:48 +0800400 } else {
401 *res = sentinel.ai_next;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900402 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900403 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900404}
405
Luke Huang69e67182019-06-17 17:06:41 +0800406int resolv_getaddrinfo(const char* _Nonnull hostname, const char* servname, const addrinfo* hints,
lifr94981782019-05-17 21:15:19 +0800407 const android_net_context* _Nonnull netcontext, addrinfo** _Nonnull res,
408 NetworkDnsEventReported* _Nonnull event) {
Luke Huang69e67182019-06-17 17:06:41 +0800409 if (hostname == nullptr && servname == nullptr) return EAI_NONAME;
410 if (hostname == nullptr) return EAI_NODATA;
411
Luke Huang69e67182019-06-17 17:06:41 +0800412 // servname is allowed to be nullptr
413 // hints is allowed to be nullptr
414 assert(res != nullptr);
415 assert(netcontext != nullptr);
lifr94981782019-05-17 21:15:19 +0800416 assert(event != nullptr);
Luke Huang69e67182019-06-17 17:06:41 +0800417
418 int error = EAI_FAIL;
419 if (hints && (error = validateHints(hints))) {
420 *res = nullptr;
421 return error;
422 }
423
424 addrinfo ai = hints ? *hints : addrinfo{};
425 addrinfo sentinel = {};
426 addrinfo* cur = &sentinel;
427 // hostname as alphanumeric name.
428 // We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
429 for (const Explore& ex : explore_options) {
430 // Require exact match for family field
431 if (ai.ai_family != ex.e_af) continue;
432
433 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
434
435 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
436
437 addrinfo tmp = ai;
438 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
439 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
440
441 LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family
442 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
lifr94981782019-05-17 21:15:19 +0800443 error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext, event);
Luke Huang69e67182019-06-17 17:06:41 +0800444
445 while (cur->ai_next) cur = cur->ai_next;
446 }
447
Luke Huangd8ac4752019-06-18 17:05:47 +0800448 // Propagate the last error from explore_fqdn(), but only when *all* attempts failed.
Luke Huang69e67182019-06-17 17:06:41 +0800449 if ((*res = sentinel.ai_next)) return 0;
450
Luke Huangd8ac4752019-06-18 17:05:47 +0800451 // TODO: consider removing freeaddrinfo.
Luke Huang69e67182019-06-17 17:06:41 +0800452 freeaddrinfo(sentinel.ai_next);
453 *res = nullptr;
Luke Huangd8ac4752019-06-18 17:05:47 +0800454 return (error == 0) ? EAI_FAIL : error;
Luke Huang69e67182019-06-17 17:06:41 +0800455}
456
Bernie Innocenti9c575932018-09-07 21:10:25 +0900457// FQDN hostname, DNS lookup
Luke Huang69e67182019-06-17 17:06:41 +0800458static int explore_fqdn(const addrinfo* pai, const char* hostname, const char* servname,
lifr94981782019-05-17 21:15:19 +0800459 addrinfo** res, const android_net_context* netcontext,
460 NetworkDnsEventReported* event) {
Luke Huang69e67182019-06-17 17:06:41 +0800461 assert(pai != nullptr);
462 // hostname may be nullptr
463 // servname may be nullptr
464 assert(res != nullptr);
465
466 addrinfo* result = nullptr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900467 int error = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900468
Luke Huang69e67182019-06-17 17:06:41 +0800469 // If the servname does not match socktype/protocol, return error code.
470 if ((error = get_portmatch(pai, servname))) return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900471
Bernie Innocenti455c6232018-09-12 21:32:42 +0900472 if (!files_getaddrinfo(hostname, pai, &result)) {
lifr94981782019-05-17 21:15:19 +0800473 error = dns_getaddrinfo(hostname, pai, netcontext, &result, event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900474 }
Luke Huang69e67182019-06-17 17:06:41 +0800475 if (error) {
476 freeaddrinfo(result);
477 return error;
Bernie Innocenti455c6232018-09-12 21:32:42 +0900478 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900479
Luke Huang69e67182019-06-17 17:06:41 +0800480 for (addrinfo* cur = result; cur; cur = cur->ai_next) {
481 // canonname should be filled already
482 if ((error = get_port(cur, servname, 0))) {
483 freeaddrinfo(result);
484 return error;
485 }
486 }
487 *res = result;
488 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900489}
490
491/*
492 * hostname == NULL.
493 * passive socket -> anyaddr (0.0.0.0 or ::)
494 * non-passive socket -> localhost (127.0.0.1 or ::1)
495 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900496static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
497 int s;
498 const struct afd* afd;
499 struct addrinfo* cur;
500 struct addrinfo sentinel;
501 int error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900502
Ken Chenffc224a2019-03-19 17:41:28 +0800503 LOG(DEBUG) << __func__;
504
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900505 assert(pai != NULL);
506 /* servname may be NULL */
507 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900508
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900509 *res = NULL;
510 sentinel.ai_next = NULL;
511 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900512
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900513 /*
514 * filter out AFs that are not supported by the kernel
515 * XXX errno?
516 */
517 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
518 if (s < 0) {
519 if (errno != EMFILE) return 0;
520 } else
521 close(s);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900522
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900523 /*
524 * if the servname does not match socktype/protocol, ignore it.
525 */
526 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900527
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900528 afd = find_afd(pai->ai_family);
529 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900530
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900531 if (pai->ai_flags & AI_PASSIVE) {
532 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900533 GET_PORT(cur->ai_next, servname);
534 } else {
535 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900536 GET_PORT(cur->ai_next, servname);
537 }
538 cur = cur->ai_next;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900539
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900540 *res = sentinel.ai_next;
541 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900542
543free:
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900544 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900545 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900546}
547
548/*
549 * numeric hostname
550 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900551static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
552 struct addrinfo** res, const char* canonname) {
553 const struct afd* afd;
554 struct addrinfo* cur;
555 struct addrinfo sentinel;
556 int error;
557 char pton[PTON_MAX];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900558
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900559 assert(pai != NULL);
560 /* hostname may be NULL */
561 /* servname may be NULL */
562 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900563
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900564 *res = NULL;
565 sentinel.ai_next = NULL;
566 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900567
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900568 /*
569 * if the servname does not match socktype/protocol, ignore it.
570 */
571 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900572
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900573 afd = find_afd(pai->ai_family);
574 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900575
Ken Chena4b33022018-10-17 00:19:59 +0800576 if (inet_pton(afd->a_af, hostname, pton) == 1) {
577 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
578 GET_AI(cur->ai_next, afd, pton);
579 GET_PORT(cur->ai_next, servname);
580 if ((pai->ai_flags & AI_CANONNAME)) {
581 /*
582 * Set the numeric address itself as
583 * the canonical name, based on a
584 * clarification in rfc2553bis-03.
585 */
Ken Chene0d73c92018-11-07 01:20:48 +0800586 error = get_canonname(pai, cur->ai_next, canonname);
587 if (error != 0) {
588 freeaddrinfo(sentinel.ai_next);
589 return error;
590 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900591 }
Ken Chena4b33022018-10-17 00:19:59 +0800592 while (cur->ai_next) cur = cur->ai_next;
593 } else
Ken Chene0d73c92018-11-07 01:20:48 +0800594 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900595 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900596
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900597 *res = sentinel.ai_next;
598 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900599
600free:
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900601 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900602 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900603}
604
605/*
606 * numeric hostname with scope
607 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900608static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
609 const char* servname, struct addrinfo** res) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900610 const struct afd* afd;
611 struct addrinfo* cur;
612 int error;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900613 const char *cp, *scope, *addr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900614 struct sockaddr_in6* sin6;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900615
Ken Chenffc224a2019-03-19 17:41:28 +0800616 LOG(DEBUG) << __func__;
617
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900618 assert(pai != NULL);
619 /* hostname may be NULL */
620 /* servname may be NULL */
621 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900622
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900623 /*
624 * if the servname does not match socktype/protocol, ignore it.
625 */
626 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900627
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900628 afd = find_afd(pai->ai_family);
629 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900630
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900631 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900632
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900633 cp = strchr(hostname, SCOPE_DELIMITER);
634 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900635
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900636 /*
637 * Handle special case of <scoped_address><delimiter><scope id>
638 */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900639 char* hostname2 = strdup(hostname);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900640 if (hostname2 == NULL) return EAI_MEMORY;
641 /* terminate at the delimiter */
642 hostname2[cp - hostname] = '\0';
643 addr = hostname2;
644 scope = cp + 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900645
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900646 error = explore_numeric(pai, addr, servname, res, hostname);
647 if (error == 0) {
chenbrucec51f1212019-09-12 16:59:33 +0800648 uint32_t scopeid;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900649
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900650 for (cur = *res; cur; cur = cur->ai_next) {
651 if (cur->ai_family != AF_INET6) continue;
652 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
653 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
654 free(hostname2);
655 return (EAI_NODATA); /* XXX: is return OK? */
656 }
657 sin6->sin6_scope_id = scopeid;
658 }
659 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900660
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900661 free(hostname2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900662
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900663 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900664}
665
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900666static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
667 assert(pai != NULL);
668 assert(ai != NULL);
669 assert(str != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900670
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900671 if ((pai->ai_flags & AI_CANONNAME) != 0) {
672 ai->ai_canonname = strdup(str);
673 if (ai->ai_canonname == NULL) return EAI_MEMORY;
674 }
675 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900676}
677
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900678static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
679 const char* addr) {
680 char* p;
681 struct addrinfo* ai;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900682
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900683 assert(pai != NULL);
684 assert(afd != NULL);
685 assert(addr != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900686
nuccachena49c0ba2018-09-11 11:13:44 +0800687 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900688 if (ai == NULL) return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900689
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900690 memcpy(ai, pai, sizeof(struct addrinfo));
691 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachena49c0ba2018-09-11 11:13:44 +0800692 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900693
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900694 ai->ai_addrlen = afd->a_socklen;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900695 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
696 p = (char*) (void*) (ai->ai_addr);
697 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
698 return ai;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900699}
700
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900701static int get_portmatch(const struct addrinfo* ai, const char* servname) {
702 assert(ai != NULL);
703 /* servname may be NULL */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900704
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900705 return get_port(ai, servname, 1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900706}
707
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900708static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
709 const char* proto;
710 struct servent* sp;
711 int port;
712 int allownumeric;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900713
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900714 assert(ai != NULL);
715 /* servname may be NULL */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900716
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900717 if (servname == NULL) return 0;
718 switch (ai->ai_family) {
719 case AF_INET:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900720 case AF_INET6:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900721 break;
722 default:
723 return 0;
724 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900725
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900726 switch (ai->ai_socktype) {
727 case SOCK_RAW:
728 return EAI_SERVICE;
729 case SOCK_DGRAM:
730 case SOCK_STREAM:
731 allownumeric = 1;
732 break;
733 case ANY:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900734 allownumeric = 1;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900735 break;
736 default:
737 return EAI_SOCKTYPE;
738 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900739
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900740 port = str2number(servname);
741 if (port >= 0) {
742 if (!allownumeric) return EAI_SERVICE;
743 if (port < 0 || port > 65535) return EAI_SERVICE;
744 port = htons(port);
745 } else {
746 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900747
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900748 switch (ai->ai_socktype) {
749 case SOCK_DGRAM:
750 proto = "udp";
751 break;
752 case SOCK_STREAM:
753 proto = "tcp";
754 break;
755 default:
756 proto = NULL;
757 break;
758 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900759
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900760 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
761 port = sp->s_port;
762 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900763
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900764 if (!matchonly) {
765 switch (ai->ai_family) {
766 case AF_INET:
767 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
768 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900769 case AF_INET6:
770 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
771 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900772 }
773 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900774
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900775 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900776}
777
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900778static const struct afd* find_afd(int af) {
779 const struct afd* afd;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900780
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900781 if (af == PF_UNSPEC) return NULL;
782 for (afd = afdl; afd->a_af; afd++) {
783 if (afd->a_af == af) return afd;
784 }
785 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900786}
787
Bernie Innocenti9c575932018-09-07 21:10:25 +0900788// Convert a string to a scope identifier.
chenbrucec51f1212019-09-12 16:59:33 +0800789static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, uint32_t* scopeid) {
790 uint64_t lscopeid;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900791 struct in6_addr* a6;
792 char* ep;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900793
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900794 assert(scope != NULL);
795 assert(sin6 != NULL);
796 assert(scopeid != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900797
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900798 a6 = &sin6->sin6_addr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900799
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900800 /* empty scopeid portion is invalid */
801 if (*scope == '\0') return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900802
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900803 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
804 /*
805 * We currently assume a one-to-one mapping between links
806 * and interfaces, so we simply use interface indices for
807 * like-local scopes.
808 */
809 *scopeid = if_nametoindex(scope);
Bernie Innocenti63abf5b2019-06-11 21:46:51 +0900810 if (*scopeid != 0) return 0;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900811 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900812
Bernie Innocenti63abf5b2019-06-11 21:46:51 +0900813 // try to convert to a numeric id as a last resort
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900814 errno = 0;
815 lscopeid = strtoul(scope, &ep, 10);
chenbrucec51f1212019-09-12 16:59:33 +0800816 *scopeid = (uint32_t)(lscopeid & 0xffffffffUL);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900817 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
818 return 0;
819 else
820 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900821}
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900822
823/* code duplicate with gethnamaddr.c */
824
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900825#define BOUNDED_INCR(x) \
826 do { \
827 BOUNDS_CHECK(cp, x); \
828 cp += (x); \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900829 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900830
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900831#define BOUNDS_CHECK(ptr, count) \
832 do { \
833 if (eom - (ptr) < (count)) { \
Hungming Chendd4bfb92018-12-25 15:47:47 +0800834 *herrno = NO_RECOVERY; \
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900835 return NULL; \
836 } \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900837 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900838
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900839static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chendd4bfb92018-12-25 15:47:47 +0800840 const struct addrinfo* pai, int* herrno) {
Ken Chene0d73c92018-11-07 01:20:48 +0800841 struct addrinfo sentinel = {};
842 struct addrinfo *cur;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900843 struct addrinfo ai;
844 const struct afd* afd;
845 char* canonname;
846 const HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +0800847 const uint8_t* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900848 int n;
chenbrucec51f1212019-09-12 16:59:33 +0800849 const uint8_t* eom;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900850 char *bp, *ep;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900851 int type, ancount, qdcount;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900852 int haveanswer, had_error;
853 char tbuf[MAXDNAME];
854 int (*name_ok)(const char*);
855 char hostbuf[8 * 1024];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900856
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900857 assert(answer != NULL);
858 assert(qname != NULL);
859 assert(pai != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900860
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900861 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900862
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900863 canonname = NULL;
864 eom = answer->buf + anslen;
865 switch (qtype) {
866 case T_A:
867 case T_AAAA:
868 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
869 name_ok = res_hnok;
870 break;
871 default:
872 return NULL; /* XXX should be abort(); */
873 }
874 /*
875 * find first satisfactory answer
876 */
877 hp = &answer->hdr;
878 ancount = ntohs(hp->ancount);
879 qdcount = ntohs(hp->qdcount);
880 bp = hostbuf;
881 ep = hostbuf + sizeof hostbuf;
882 cp = answer->buf;
883 BOUNDED_INCR(HFIXEDSZ);
884 if (qdcount != 1) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800885 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900886 return (NULL);
887 }
888 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
889 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800890 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900891 return (NULL);
892 }
893 BOUNDED_INCR(n + QFIXEDSZ);
894 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
895 /* res_send() has already verified that the query name is the
896 * same as the one we sent; this just gets the expanded name
897 * (i.e., with the succeeding search-domain tacked on).
898 */
899 n = strlen(bp) + 1; /* for the \0 */
900 if (n >= MAXHOSTNAMELEN) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800901 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900902 return (NULL);
903 }
904 canonname = bp;
905 bp += n;
906 /* The qname can be abbreviated, but h_name is now absolute. */
907 qname = canonname;
908 }
909 haveanswer = 0;
910 had_error = 0;
911 while (ancount-- > 0 && cp < eom && !had_error) {
912 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
913 if ((n < 0) || !(*name_ok)(bp)) {
914 had_error++;
915 continue;
916 }
917 cp += n; /* name */
918 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce0d470422019-03-28 18:44:37 +0800919 type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900920 cp += INT16SZ; /* type */
chenbruce0d470422019-03-28 18:44:37 +0800921 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900922 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce0d470422019-03-28 18:44:37 +0800923 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900924 cp += INT16SZ; /* len */
925 BOUNDS_CHECK(cp, n);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900926 if (cl != C_IN) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900927 /* XXX - debug? syslog? */
928 cp += n;
929 continue; /* XXX - had_error++ ? */
930 }
931 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
932 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
933 if ((n < 0) || !(*name_ok)(tbuf)) {
934 had_error++;
935 continue;
936 }
937 cp += n;
938 /* Get canonical name. */
939 n = strlen(tbuf) + 1; /* for the \0 */
940 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
941 had_error++;
942 continue;
943 }
944 strlcpy(bp, tbuf, (size_t)(ep - bp));
945 canonname = bp;
946 bp += n;
947 continue;
948 }
949 if (qtype == T_ANY) {
950 if (!(type == T_A || type == T_AAAA)) {
951 cp += n;
952 continue;
953 }
954 } else if (type != qtype) {
955 if (type != T_KEY && type != T_SIG)
Ken Chenffc224a2019-03-19 17:41:28 +0800956 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
957 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900958 cp += n;
959 continue; /* XXX - had_error++ ? */
960 }
961 switch (type) {
962 case T_A:
963 case T_AAAA:
964 if (strcasecmp(canonname, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800965 LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp
966 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900967 cp += n;
968 continue; /* XXX - had_error++ ? */
969 }
970 if (type == T_A && n != INADDRSZ) {
971 cp += n;
972 continue;
973 }
974 if (type == T_AAAA && n != IN6ADDRSZ) {
975 cp += n;
976 continue;
977 }
978 if (type == T_AAAA) {
979 struct in6_addr in6;
980 memcpy(&in6, cp, IN6ADDRSZ);
981 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
982 cp += n;
983 continue;
984 }
985 }
986 if (!haveanswer) {
987 int nn;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900988
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900989 canonname = bp;
990 nn = strlen(bp) + 1; /* for the \0 */
991 bp += nn;
992 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900993
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900994 /* don't overwrite pai */
995 ai = *pai;
996 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
997 afd = find_afd(ai.ai_family);
998 if (afd == NULL) {
999 cp += n;
1000 continue;
1001 }
1002 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1003 if (cur->ai_next == NULL) had_error++;
1004 while (cur && cur->ai_next) cur = cur->ai_next;
1005 cp += n;
1006 break;
1007 default:
1008 abort();
1009 }
1010 if (!had_error) haveanswer++;
1011 }
1012 if (haveanswer) {
1013 if (!canonname)
1014 (void) get_canonname(pai, sentinel.ai_next, qname);
1015 else
1016 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chendd4bfb92018-12-25 15:47:47 +08001017 *herrno = NETDB_SUCCESS;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001018 return sentinel.ai_next;
1019 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001020
Hungming Chendd4bfb92018-12-25 15:47:47 +08001021 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001022 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001023}
1024
1025struct addrinfo_sort_elem {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001026 struct addrinfo* ai;
1027 int has_src_addr;
1028 sockaddr_union src_addr;
1029 int original_order;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001030};
1031
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001032static int _get_scope(const struct sockaddr* addr) {
1033 if (addr->sa_family == AF_INET6) {
1034 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1035 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1036 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1037 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1038 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1039 /*
1040 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1041 * link-local scope.
1042 */
1043 return IPV6_ADDR_SCOPE_LINKLOCAL;
1044 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1045 return IPV6_ADDR_SCOPE_SITELOCAL;
1046 } else {
1047 return IPV6_ADDR_SCOPE_GLOBAL;
1048 }
1049 } else if (addr->sa_family == AF_INET) {
1050 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1051 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001052
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001053 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1054 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1055 return IPV6_ADDR_SCOPE_LINKLOCAL;
1056 } else {
1057 /*
1058 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1059 * and shared addresses (100.64.0.0/10), are assigned global scope.
1060 */
1061 return IPV6_ADDR_SCOPE_GLOBAL;
1062 }
1063 } else {
1064 /*
1065 * This should never happen.
1066 * Return a scope with low priority as a last resort.
1067 */
1068 return IPV6_ADDR_SCOPE_NODELOCAL;
1069 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001070}
1071
1072/* These macros are modelled after the ones in <netinet/in6.h>. */
1073
1074/* RFC 4380, section 2.6 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001075#define IN6_IS_ADDR_TEREDO(a) \
1076 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001077
1078/* RFC 3056, section 2. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001079#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001080
1081/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001082#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001083
1084/*
1085 * Get the label for a given IPv4/IPv6 address.
1086 * RFC 6724, section 2.1.
1087 */
1088
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001089static int _get_label(const struct sockaddr* addr) {
1090 if (addr->sa_family == AF_INET) {
1091 return 4;
1092 } else if (addr->sa_family == AF_INET6) {
1093 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1094 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1095 return 0;
1096 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1097 return 4;
1098 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1099 return 2;
1100 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1101 return 5;
1102 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1103 return 13;
1104 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1105 return 3;
1106 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1107 return 11;
1108 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1109 return 12;
1110 } else {
1111 /* All other IPv6 addresses, including global unicast addresses. */
1112 return 1;
1113 }
1114 } else {
1115 /*
1116 * This should never happen.
1117 * Return a semi-random label as a last resort.
1118 */
1119 return 1;
1120 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001121}
1122
1123/*
1124 * Get the precedence for a given IPv4/IPv6 address.
1125 * RFC 6724, section 2.1.
1126 */
1127
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001128static int _get_precedence(const struct sockaddr* addr) {
1129 if (addr->sa_family == AF_INET) {
1130 return 35;
1131 } else if (addr->sa_family == AF_INET6) {
1132 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1133 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1134 return 50;
1135 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1136 return 35;
1137 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1138 return 30;
1139 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1140 return 5;
1141 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1142 return 3;
1143 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1144 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1145 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1146 return 1;
1147 } else {
1148 /* All other IPv6 addresses, including global unicast addresses. */
1149 return 40;
1150 }
1151 } else {
1152 return 1;
1153 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001154}
1155
1156/*
1157 * Find number of matching initial bits between the two addresses a1 and a2.
1158 */
1159
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001160static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1161 const char* p1 = (const char*) a1;
1162 const char* p2 = (const char*) a2;
1163 unsigned i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001164
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001165 for (i = 0; i < sizeof(*a1); ++i) {
1166 int x, j;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001167
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001168 if (p1[i] == p2[i]) {
1169 continue;
1170 }
1171 x = p1[i] ^ p2[i];
1172 for (j = 0; j < CHAR_BIT; ++j) {
1173 if (x & (1 << (CHAR_BIT - 1))) {
1174 return i * CHAR_BIT + j;
1175 }
1176 x <<= 1;
1177 }
1178 }
1179 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001180}
1181
1182/*
1183 * Compare two source/destination address pairs.
1184 * RFC 6724, section 6.
1185 */
1186
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001187static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1188 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1189 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1190 int scope_src1, scope_dst1, scope_match1;
1191 int scope_src2, scope_dst2, scope_match2;
1192 int label_src1, label_dst1, label_match1;
1193 int label_src2, label_dst2, label_match2;
1194 int precedence1, precedence2;
1195 int prefixlen1, prefixlen2;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001196
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001197 /* Rule 1: Avoid unusable destinations. */
1198 if (a1->has_src_addr != a2->has_src_addr) {
1199 return a2->has_src_addr - a1->has_src_addr;
1200 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001201
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001202 /* Rule 2: Prefer matching scope. */
nuccachenb980f2f2018-10-23 17:10:58 +08001203 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001204 scope_dst1 = _get_scope(a1->ai->ai_addr);
1205 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001206
nuccachenb980f2f2018-10-23 17:10:58 +08001207 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001208 scope_dst2 = _get_scope(a2->ai->ai_addr);
1209 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001210
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001211 if (scope_match1 != scope_match2) {
1212 return scope_match2 - scope_match1;
1213 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001214
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001215 /*
1216 * Rule 3: Avoid deprecated addresses.
1217 * TODO(sesse): We don't currently have a good way of finding this.
1218 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001219
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001220 /*
1221 * Rule 4: Prefer home addresses.
1222 * TODO(sesse): We don't currently have a good way of finding this.
1223 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001224
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001225 /* Rule 5: Prefer matching label. */
nuccachenb980f2f2018-10-23 17:10:58 +08001226 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001227 label_dst1 = _get_label(a1->ai->ai_addr);
1228 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001229
nuccachenb980f2f2018-10-23 17:10:58 +08001230 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001231 label_dst2 = _get_label(a2->ai->ai_addr);
1232 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001233
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001234 if (label_match1 != label_match2) {
1235 return label_match2 - label_match1;
1236 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001237
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001238 /* Rule 6: Prefer higher precedence. */
1239 precedence1 = _get_precedence(a1->ai->ai_addr);
1240 precedence2 = _get_precedence(a2->ai->ai_addr);
1241 if (precedence1 != precedence2) {
1242 return precedence2 - precedence1;
1243 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001244
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001245 /*
1246 * Rule 7: Prefer native transport.
1247 * TODO(sesse): We don't currently have a good way of finding this.
1248 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001249
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001250 /* Rule 8: Prefer smaller scope. */
1251 if (scope_dst1 != scope_dst2) {
1252 return scope_dst1 - scope_dst2;
1253 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001254
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001255 /*
1256 * Rule 9: Use longest matching prefix.
1257 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1258 * to work very well directly applied to IPv4. (glibc uses information from
1259 * the routing table for a custom IPv4 implementation here.)
1260 */
1261 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1262 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachenb980f2f2018-10-23 17:10:58 +08001263 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001264 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachenb980f2f2018-10-23 17:10:58 +08001265 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001266 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1267 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1268 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1269 if (prefixlen1 != prefixlen2) {
1270 return prefixlen2 - prefixlen1;
1271 }
1272 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001273
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001274 /*
1275 * Rule 10: Leave the order unchanged.
1276 * We need this since qsort() is not necessarily stable.
1277 */
1278 return a1->original_order - a2->original_order;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001279}
1280
1281/*
1282 * Find the source address that will be used if trying to connect to the given
1283 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1284 *
1285 * Returns 1 if a source address was found, 0 if the address is unreachable,
1286 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1287 * undefined.
1288 */
1289
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001290static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1291 uid_t uid) {
1292 int sock;
1293 int ret;
1294 socklen_t len;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001295
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001296 switch (addr->sa_family) {
1297 case AF_INET:
1298 len = sizeof(struct sockaddr_in);
1299 break;
1300 case AF_INET6:
1301 len = sizeof(struct sockaddr_in6);
1302 break;
1303 default:
1304 /* No known usable source address for non-INET families. */
1305 return 0;
1306 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001307
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001308 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1309 if (sock == -1) {
1310 if (errno == EAFNOSUPPORT) {
1311 return 0;
1312 } else {
1313 return -1;
1314 }
1315 }
1316 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1317 close(sock);
1318 return 0;
1319 }
1320 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1321 close(sock);
1322 return 0;
1323 }
1324 do {
Bernie Innocentiafaacf72018-08-30 07:34:37 +09001325 ret = connect(sock, addr, len);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001326 } while (ret == -1 && errno == EINTR);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001327
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001328 if (ret == -1) {
1329 close(sock);
1330 return 0;
1331 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001332
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001333 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1334 close(sock);
1335 return -1;
1336 }
1337 close(sock);
1338 return 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001339}
1340
1341/*
1342 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1343 * Will leave the list unchanged if an error occurs.
1344 */
1345
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001346static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1347 struct addrinfo* cur;
1348 int nelem = 0, i;
1349 struct addrinfo_sort_elem* elems;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001350
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001351 cur = list_sentinel->ai_next;
1352 while (cur) {
1353 ++nelem;
1354 cur = cur->ai_next;
1355 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001356
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001357 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1358 if (elems == NULL) {
1359 goto error;
1360 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001361
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001362 /*
1363 * Convert the linked list to an array that also contains the candidate
1364 * source address for each destination address.
1365 */
1366 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1367 int has_src_addr;
1368 assert(cur != NULL);
1369 elems[i].ai = cur;
1370 elems[i].original_order = i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001371
nuccachenb980f2f2018-10-23 17:10:58 +08001372 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001373 if (has_src_addr == -1) {
1374 goto error;
1375 }
1376 elems[i].has_src_addr = has_src_addr;
1377 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001378
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001379 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1380 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001381
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001382 list_sentinel->ai_next = elems[0].ai;
1383 for (i = 0; i < nelem - 1; ++i) {
1384 elems[i].ai->ai_next = elems[i + 1].ai;
1385 }
1386 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001387
1388error:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001389 free(elems);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001390}
1391
Bernie Innocenti455c6232018-09-12 21:32:42 +09001392static int dns_getaddrinfo(const char* name, const addrinfo* pai,
lifr94981782019-05-17 21:15:19 +08001393 const android_net_context* netcontext, addrinfo** rv,
1394 NetworkDnsEventReported* event) {
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001395 res_target q = {};
1396 res_target q2 = {};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001397
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001398 auto buf = std::make_unique<querybuf>();
1399 auto buf2 = std::make_unique<querybuf>();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001400
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001401 switch (pai->ai_family) {
Bernie Innocenti9c575932018-09-07 21:10:25 +09001402 case AF_UNSPEC: {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001403 /* prefer IPv6 */
1404 q.name = name;
1405 q.qclass = C_IN;
1406 q.answer = buf->buf;
1407 q.anslen = sizeof(buf->buf);
1408 int query_ipv6 = 1, query_ipv4 = 1;
1409 if (pai->ai_flags & AI_ADDRCONFIG) {
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001410 query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid);
1411 query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001412 }
1413 if (query_ipv6) {
1414 q.qtype = T_AAAA;
1415 if (query_ipv4) {
1416 q.next = &q2;
1417 q2.name = name;
1418 q2.qclass = C_IN;
1419 q2.qtype = T_A;
1420 q2.answer = buf2->buf;
1421 q2.anslen = sizeof(buf2->buf);
1422 }
1423 } else if (query_ipv4) {
1424 q.qtype = T_A;
1425 } else {
Bernie Innocenti455c6232018-09-12 21:32:42 +09001426 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001427 }
1428 break;
Bernie Innocenti9c575932018-09-07 21:10:25 +09001429 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001430 case AF_INET:
1431 q.name = name;
1432 q.qclass = C_IN;
1433 q.qtype = T_A;
1434 q.answer = buf->buf;
1435 q.anslen = sizeof(buf->buf);
1436 break;
1437 case AF_INET6:
1438 q.name = name;
1439 q.qclass = C_IN;
1440 q.qtype = T_AAAA;
1441 q.answer = buf->buf;
1442 q.anslen = sizeof(buf->buf);
1443 break;
1444 default:
Bernie Innocenti455c6232018-09-12 21:32:42 +09001445 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001446 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001447
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001448 res_state res = res_get_state();
1449 if (!res) return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001450
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001451 /* this just sets our netid val in the thread private data so we don't have to
1452 * modify the api's all the way down to res_send.c's res_nsend. We could
1453 * fully populate the thread private data here, but if we get down there
1454 * and have a cache hit that would be wasted, so we do the rest there on miss
1455 */
lifr94981782019-05-17 21:15:19 +08001456 res_setnetcontext(res, netcontext, event);
Mike Yubfb1b342018-11-06 15:42:36 +08001457
Hungming Chena6914a62019-01-19 15:07:04 +08001458 int he;
1459 if (res_searchN(name, &q, res, &he) < 0) {
1460 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
1461 // Note that res_searchN() doesn't set the pair NETDB_INTERNAL and errno.
1462 // See also herrnoToAiErrno().
1463 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001464 }
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001465
1466 addrinfo sentinel = {};
1467 addrinfo* cur = &sentinel;
Hungming Chena6914a62019-01-19 15:07:04 +08001468 addrinfo* ai = getanswer(buf.get(), q.n, q.name, q.qtype, pai, &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001469 if (ai) {
1470 cur->ai_next = ai;
1471 while (cur && cur->ai_next) cur = cur->ai_next;
1472 }
1473 if (q.next) {
Hungming Chena6914a62019-01-19 15:07:04 +08001474 ai = getanswer(buf2.get(), q2.n, q2.name, q2.qtype, pai, &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001475 if (ai) cur->ai_next = ai;
1476 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001477 if (sentinel.ai_next == NULL) {
Hungming Chena6914a62019-01-19 15:07:04 +08001478 // Note that getanswer() doesn't set the pair NETDB_INTERNAL and errno.
1479 // See also herrnoToAiErrno().
1480 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001481 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001482
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001483 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001484
Bernie Innocenti455c6232018-09-12 21:32:42 +09001485 *rv = sentinel.ai_next;
1486 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001487}
1488
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001489static void _sethtent(FILE** hostf) {
1490 if (!*hostf)
1491 *hostf = fopen(_PATH_HOSTS, "re");
1492 else
1493 rewind(*hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001494}
1495
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001496static void _endhtent(FILE** hostf) {
1497 if (*hostf) {
1498 (void) fclose(*hostf);
1499 *hostf = NULL;
1500 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001501}
1502
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001503static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1504 char* p;
1505 char *cp, *tname, *cname;
Bernie Innocentie2bc46f2018-10-16 23:35:28 +09001506 struct addrinfo *res0, *res;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001507 int error;
1508 const char* addr;
1509 char hostbuf[8 * 1024];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001510
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001511 assert(name != NULL);
1512 assert(pai != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001513
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001514 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1515again:
1516 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1517 if (*p == '#') goto again;
1518 if (!(cp = strpbrk(p, "#\n"))) goto again;
1519 *cp = '\0';
1520 if (!(cp = strpbrk(p, " \t"))) goto again;
1521 *cp++ = '\0';
1522 addr = p;
1523 /* if this is not something we're looking for, skip it. */
1524 cname = NULL;
1525 while (cp && *cp) {
1526 if (*cp == ' ' || *cp == '\t') {
1527 cp++;
1528 continue;
1529 }
1530 if (!cname) cname = cp;
1531 tname = cp;
1532 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001533 if (strcasecmp(name, tname) == 0) goto found;
1534 }
1535 goto again;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001536
1537found:
Bernie Innocentie2bc46f2018-10-16 23:35:28 +09001538 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001539 if (error) goto again;
1540 for (res = res0; res; res = res->ai_next) {
1541 /* cover it up */
1542 res->ai_flags = pai->ai_flags;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001543
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001544 if (pai->ai_flags & AI_CANONNAME) {
1545 if (get_canonname(pai, res, cname) != 0) {
1546 freeaddrinfo(res0);
1547 goto again;
1548 }
1549 }
1550 }
1551 return res0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001552}
1553
Bernie Innocenti455c6232018-09-12 21:32:42 +09001554static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chene0d73c92018-11-07 01:20:48 +08001555 struct addrinfo sentinel = {};
1556 struct addrinfo *p, *cur;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001557 FILE* hostf = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001558
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001559 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001560
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001561 _sethtent(&hostf);
1562 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1563 cur->ai_next = p;
1564 while (cur && cur->ai_next) cur = cur->ai_next;
1565 }
1566 _endhtent(&hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001567
Bernie Innocenti455c6232018-09-12 21:32:42 +09001568 *res = sentinel.ai_next;
1569 return sentinel.ai_next != NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001570}
1571
1572/* resolver logic */
1573
1574/*
1575 * Formulate a normal query, send, and await answer.
1576 * Returned answer is placed in supplied buffer "answer".
1577 * Perform preliminary check of answer, returning success only
1578 * if no error is indicated and the answer count is nonzero.
1579 * Return the size of the response on success, -1 on error.
Hungming Chendd4bfb92018-12-25 15:47:47 +08001580 * Error number is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001581 *
1582 * Caller must parse answer and determine whether it answers the question.
1583 */
Hungming Chen947aab02018-12-27 18:33:19 +08001584static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
chenbrucec51f1212019-09-12 16:59:33 +08001585 uint8_t buf[MAXPACKET];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001586 HEADER* hp;
1587 int n;
1588 struct res_target* t;
1589 int rcode;
1590 int ancount;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001591
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001592 assert(name != NULL);
1593 /* XXX: target may be NULL??? */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001594
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001595 rcode = NOERROR;
1596 ancount = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001597
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001598 for (t = target; t; t = t->next) {
chenbrucec51f1212019-09-12 16:59:33 +08001599 uint8_t* answer;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001600 int anslen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001601
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001602 hp = (HEADER*) (void*) t->answer;
Ken Chen0a015532019-01-02 14:59:38 +08001603 bool retried = false;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001604 again:
1605 hp->rcode = NOERROR; /* default */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001606
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001607 /* make it easier... */
Bernie Innocenti9c575932018-09-07 21:10:25 +09001608 int cl = t->qclass;
1609 int type = t->qtype;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001610 answer = t->answer;
1611 anslen = t->anslen;
chenbruceacb832c2019-02-20 19:45:50 +08001612
Ken Chenffc224a2019-03-19 17:41:28 +08001613 LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001614
Bernie Innocenti9c575932018-09-07 21:10:25 +09001615 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
chenbruced8cbb9b2019-06-20 18:25:28 +08001616 if (n > 0 &&
1617 (res->netcontext_flags &
1618 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
1619 !retried) // TODO: remove the retry flag and provide a sufficient test coverage.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001620 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001621 if (n <= 0) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001622 LOG(ERROR) << __func__ << ": res_nmkquery failed";
Hungming Chendd4bfb92018-12-25 15:47:47 +08001623 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001624 return n;
1625 }
Mike Yubfb1b342018-11-06 15:42:36 +08001626
Luke Huangba7bef92018-12-26 16:53:03 +08001627 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001628 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen947aab02018-12-27 18:33:19 +08001629 // Record rcode from DNS response header only if no timeout.
1630 // Keep rcode timeout for reporting later if any.
chenbruced8cbb9b2019-06-20 18:25:28 +08001631 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; // record most recent error
1632 // if the query choked with EDNS0, retry without EDNS0 that when the server
1633 // has no response, resovler won't retry and do nothing. Even fallback to UDP,
1634 // we also has the same symptom if EDNS is enabled.
1635 if ((res->netcontext_flags &
1636 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
Ken Chen0a015532019-01-02 14:59:38 +08001637 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001638 LOG(DEBUG) << __func__ << ": retry without EDNS0";
Ken Chen0a015532019-01-02 14:59:38 +08001639 retried = true;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001640 goto again;
1641 }
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001642 LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001643 continue;
1644 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001645
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001646 ancount += ntohs(hp->ancount);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001647
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001648 t->n = n;
1649 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001650
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001651 if (ancount == 0) {
1652 switch (rcode) {
Hungming Chen947aab02018-12-27 18:33:19 +08001653 // Not defined in RFC.
1654 case RCODE_TIMEOUT:
1655 // DNS metrics monitors DNS query timeout.
1656 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1657 break;
1658 // Defined in RFC 1035 section 4.1.1.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001659 case NXDOMAIN:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001660 *herrno = HOST_NOT_FOUND;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001661 break;
1662 case SERVFAIL:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001663 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001664 break;
1665 case NOERROR:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001666 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001667 break;
1668 case FORMERR:
1669 case NOTIMP:
1670 case REFUSED:
1671 default:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001672 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001673 break;
1674 }
1675 return -1;
1676 }
1677 return ancount;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001678}
1679
1680/*
1681 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1682 * Return the size of the response on success, -1 on error.
1683 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chendd4bfb92018-12-25 15:47:47 +08001684 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001685 */
Hungming Chen947aab02018-12-27 18:33:19 +08001686static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Luke Huang2dac4382019-06-24 13:28:44 +08001687 const char* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001688 HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +08001689 uint32_t dots;
chenbruce018fdb22019-06-12 18:08:04 +08001690 int ret, saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001691 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001692
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001693 assert(name != NULL);
1694 assert(target != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001695
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001696 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001697
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001698 errno = 0;
Hungming Chendd4bfb92018-12-25 15:47:47 +08001699 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001700 dots = 0;
1701 for (cp = name; *cp; cp++) dots += (*cp == '.');
chenbruce018fdb22019-06-12 18:08:04 +08001702 const bool trailing_dot = (cp > name && *--cp == '.') ? true : false;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001703
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001704 /*
1705 * If there are dots in the name already, let's just give it a try
1706 * 'as is'. The threshold can be set with the "ndots" option.
1707 */
1708 saved_herrno = -1;
1709 if (dots >= res->ndots) {
Hungming Chen947aab02018-12-27 18:33:19 +08001710 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001711 if (ret > 0) return (ret);
Hungming Chendd4bfb92018-12-25 15:47:47 +08001712 saved_herrno = *herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001713 tried_as_is++;
1714 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001715
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001716 /*
1717 * We do at least one level of search if
chenbruce018fdb22019-06-12 18:08:04 +08001718 * - there is no dot, or
1719 * - there is at least one dot and there is no trailing dot.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001720 */
chenbruce018fdb22019-06-12 18:08:04 +08001721 if ((!dots) || (dots && !trailing_dot)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001722 int done = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001723
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001724 /* Unfortunately we need to set stuff up before
1725 * the domain stuff is tried. Will have a better
1726 * fix after thread pools are used.
1727 */
1728 _resolv_populate_res_for_net(res);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001729
Luke Huang2dac4382019-06-24 13:28:44 +08001730 for (const auto& domain : res->search_domains) {
1731 ret = res_querydomainN(name, domain.c_str(), target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001732 if (ret > 0) return ret;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001733
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001734 /*
1735 * If no server present, give up.
1736 * If name isn't found in this domain,
1737 * keep trying higher domains in the search list
1738 * (if that's enabled).
1739 * On a NO_DATA error, keep trying, otherwise
1740 * a wildcard entry of another type could keep us
1741 * from finding this entry higher in the domain.
1742 * If we get some other error (negative answer or
1743 * server failure), then stop searching up,
1744 * but try the input name below in case it's
1745 * fully-qualified.
1746 */
1747 if (errno == ECONNREFUSED) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001748 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001749 return -1;
1750 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001751
Hungming Chendd4bfb92018-12-25 15:47:47 +08001752 switch (*herrno) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001753 case NO_DATA:
1754 got_nodata++;
Bernie Innocentif40b3bd2018-10-10 22:30:12 +09001755 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001756 case HOST_NOT_FOUND:
1757 /* keep trying */
1758 break;
1759 case TRY_AGAIN:
1760 if (hp->rcode == SERVFAIL) {
1761 /* try next search element, if any */
1762 got_servfail++;
1763 break;
1764 }
Bernie Innocentif40b3bd2018-10-10 22:30:12 +09001765 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001766 default:
1767 /* anything else implies that we're done */
1768 done++;
1769 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001770 }
1771 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001772
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001773 /*
1774 * if we have not already tried the name "as is", do that now.
1775 * note that we do this regardless of how many dots were in the
1776 * name or whether it ends with a dot.
1777 */
1778 if (!tried_as_is) {
Hungming Chen947aab02018-12-27 18:33:19 +08001779 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001780 if (ret > 0) return ret;
1781 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001782
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001783 /*
1784 * if we got here, we didn't satisfy the search.
1785 * if we did an initial full query, return that query's h_errno
1786 * (note that we wouldn't be here if that query had succeeded).
1787 * else if we ever got a nodata, send that back as the reason.
1788 * else send back meaningless h_errno, that being the one from
1789 * the last DNSRCH we did.
1790 */
1791 if (saved_herrno != -1)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001792 *herrno = saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001793 else if (got_nodata)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001794 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001795 else if (got_servfail)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001796 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001797 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001798}
1799
1800/*
1801 * Perform a call on res_query on the concatenation of name and domain,
1802 * removing a trailing dot from name if domain is NULL.
1803 */
Mike Yubfb1b342018-11-06 15:42:36 +08001804static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen947aab02018-12-27 18:33:19 +08001805 int* herrno) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001806 char nbuf[MAXDNAME];
1807 const char* longname = nbuf;
1808 size_t n, d;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001809
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001810 assert(name != NULL);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001811
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001812 if (domain == NULL) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001813 // Check for trailing '.'; copy without '.' if present.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001814 n = strlen(name);
1815 if (n + 1 > sizeof(nbuf)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001816 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001817 return -1;
1818 }
1819 if (n > 0 && name[--n] == '.') {
1820 strncpy(nbuf, name, n);
1821 nbuf[n] = '\0';
1822 } else
1823 longname = name;
1824 } else {
1825 n = strlen(name);
1826 d = strlen(domain);
1827 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001828 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001829 return -1;
1830 }
1831 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1832 }
Hungming Chen947aab02018-12-27 18:33:19 +08001833 return res_queryN(longname, target, res, herrno);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001834}