blob: 3683920377d7b7345b389a9f066165eb636c8060 [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 Innocenti10a90282020-01-23 23:28:00 +090059#include "res_comp.h"
60#include "res_debug.h"
Bernie Innocenti08487112019-10-11 21:14:13 +090061#include "res_init.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090062#include "resolv_cache.h"
63#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090064
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090065#define ANY 0
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090066
lifr94981782019-05-17 21:15:19 +080067using android::net::NetworkDnsEventReported;
68
Bernie Innocenti4e374b62018-12-12 00:43:02 +090069const char in_addrany[] = {0, 0, 0, 0};
70const char in_loopback[] = {127, 0, 0, 1};
71const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
72const 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 +090073
Bernie Innocenti4e374b62018-12-12 00:43:02 +090074const struct afd {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090075 int a_af;
76 int a_addrlen;
77 int a_socklen;
78 int a_off;
79 const char* a_addrany;
80 const char* a_loopback;
81 int a_scoped;
82} afdl[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090083 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
84 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090085 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
86 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
87 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090088};
89
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090090struct Explore {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090091 int e_af;
92 int e_socktype;
93 int e_protocol;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090094 int e_wild;
Bernie Innocentib0b32bc2019-02-20 18:21:24 +090095#define WILD_AF(ex) ((ex).e_wild & 0x01)
96#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
97#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090098};
99
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900100const Explore explore_options[] = {
Ken Chene0d73c92018-11-07 01:20:48 +0800101 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
102 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
103 {PF_INET6, SOCK_RAW, ANY, 0x05},
104 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
105 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
106 {PF_INET, SOCK_RAW, ANY, 0x05},
107 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
108 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
109 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900110};
111
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900112#define PTON_MAX 16
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900113
114struct res_target {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900115 struct res_target* next;
Luke Huangc98fd802019-10-15 16:36:36 +0900116 const char* name; // domain name
117 int qclass, qtype; // class and type of query
118 std::vector<uint8_t> answer = std::vector<uint8_t>(MAXPACKET, 0); // buffer to put answer
119 int n = 0; // result length
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900120};
121
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900122static int str2number(const char*);
123static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
lifr94981782019-05-17 21:15:19 +0800124 const struct android_net_context*, NetworkDnsEventReported* event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900125static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
126static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
127 const char*);
128static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
129 struct addrinfo**);
130static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
131static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
132static int get_portmatch(const struct addrinfo*, const char*);
133static int get_port(const struct addrinfo*, const char*, int);
134static const struct afd* find_afd(int);
chenbrucec51f1212019-09-12 16:59:33 +0800135static int ip6_str2scopeid(const char*, struct sockaddr_in6*, uint32_t*);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900136
Luke Huangc98fd802019-10-15 16:36:36 +0900137static struct addrinfo* getanswer(const std::vector<uint8_t>&, int, const char*, int,
138 const struct addrinfo*, int* herrno);
Bernie Innocenti455c6232018-09-12 21:32:42 +0900139static int dns_getaddrinfo(const char* name, const addrinfo* pai,
lifr94981782019-05-17 21:15:19 +0800140 const android_net_context* netcontext, addrinfo** rv,
141 NetworkDnsEventReported* event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900142static void _sethtent(FILE**);
143static void _endhtent(FILE**);
144static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
chenbrucefd837fa2019-10-29 18:35:36 +0800145static struct addrinfo* getCustomHosts(const size_t netid, const char*, const struct addrinfo*);
146static bool files_getaddrinfo(const size_t netid, const char* name, const addrinfo* pai,
147 addrinfo** res);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900148static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900149
Hungming Chen947aab02018-12-27 18:33:19 +0800150static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
151static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yubfb1b342018-11-06 15:42:36 +0800152static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen947aab02018-12-27 18:33:19 +0800153 int* herrno);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900154
Bernie Innocenti4e374b62018-12-12 00:43:02 +0900155const char* const ai_errlist[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900156 "Success",
157 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
158 "Temporary failure in name resolution", /* EAI_AGAIN */
159 "Invalid value for ai_flags", /* EAI_BADFLAGS */
160 "Non-recoverable failure in name resolution", /* EAI_FAIL */
161 "ai_family not supported", /* EAI_FAMILY */
162 "Memory allocation failure", /* EAI_MEMORY */
163 "No address associated with hostname", /* EAI_NODATA */
164 "hostname nor servname provided, or not known", /* EAI_NONAME */
165 "servname not supported for ai_socktype", /* EAI_SERVICE */
166 "ai_socktype not supported", /* EAI_SOCKTYPE */
167 "System error returned in errno", /* EAI_SYSTEM */
168 "Invalid value for hints", /* EAI_BADHINTS */
169 "Resolved protocol is unknown", /* EAI_PROTOCOL */
170 "Argument buffer overflow", /* EAI_OVERFLOW */
171 "Unknown error", /* EAI_MAX */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900172};
173
174/* XXX macros that make external reference is BAD. */
175
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900176#define GET_AI(ai, afd, addr) \
177 do { \
178 /* external reference: pai, error, and label free */ \
179 (ai) = get_ai(pai, (afd), (addr)); \
180 if ((ai) == NULL) { \
181 error = EAI_MEMORY; \
182 goto free; \
183 } \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900184 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900185
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900186#define GET_PORT(ai, serv) \
187 do { \
188 /* external reference: error and label free */ \
189 error = get_port((ai), (serv), 0); \
190 if (error != 0) goto free; \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900191 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900192
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900193#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900194 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
195#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900196
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900197const char* gai_strerror(int ecode) {
198 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
199 return ai_errlist[ecode];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900200}
201
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900202void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900203 while (ai) {
204 struct addrinfo* next = ai->ai_next;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900205 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900206 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900207 free(ai);
208 ai = next;
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900209 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900210}
211
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900212static int str2number(const char* p) {
213 char* ep;
214 unsigned long v;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900215
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900216 assert(p != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900217
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900218 if (*p == '\0') return -1;
219 ep = NULL;
220 errno = 0;
221 v = strtoul(p, &ep, 10);
222 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
223 return v;
224 else
225 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900226}
227
228/*
229 * The following functions determine whether IPv4 or IPv6 connectivity is
230 * available in order to implement AI_ADDRCONFIG.
231 *
232 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
233 * available, but whether addresses of the specified family are "configured
234 * on the local system". However, bionic doesn't currently support getifaddrs,
235 * so checking for connectivity is the next best thing.
236 */
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900237static int have_ipv6(unsigned mark, uid_t uid) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900238 static const struct sockaddr_in6 sin6_test = {
239 .sin6_family = AF_INET6,
240 .sin6_addr.s6_addr = {// 2000::
241 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachenb980f2f2018-10-23 17:10:58 +0800242 sockaddr_union addr = {.sin6 = sin6_test};
243 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900244}
245
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900246static int have_ipv4(unsigned mark, uid_t uid) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900247 static const struct sockaddr_in sin_test = {
248 .sin_family = AF_INET,
249 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
250 };
nuccachenb980f2f2018-10-23 17:10:58 +0800251 sockaddr_union addr = {.sin = sin_test};
252 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900253}
254
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900255// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
Mike Yuc7649d12019-05-22 15:28:07 +0800256// NOTE: also called by resolv_set_nameservers().
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900257int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
258 addrinfo** result) {
259 hints.ai_flags = AI_NUMERICHOST;
260 const android_net_context netcontext = {
261 .app_netid = NETID_UNSET,
262 .app_mark = MARK_UNSET,
263 .dns_netid = NETID_UNSET,
264 .dns_mark = MARK_UNSET,
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900265 .uid = NET_CONTEXT_INVALID_UID,
Praveen Moongalam Thyagarajan8ab18ba2019-09-04 14:46:50 -0700266 .pid = NET_CONTEXT_INVALID_PID,
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900267 };
lifr94981782019-05-17 21:15:19 +0800268 NetworkDnsEventReported event;
269 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result,
270 &event);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900271}
272
Luke Huang69e67182019-06-17 17:06:41 +0800273namespace {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900274
Luke Huang69e67182019-06-17 17:06:41 +0800275int validateHints(const addrinfo* _Nonnull hints) {
276 if (!hints) return EAI_BADHINTS;
277
278 // error check for hints
279 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
280 return EAI_BADHINTS;
281 }
282 if (hints->ai_flags & ~AI_MASK) {
283 return EAI_BADFLAGS;
284 }
285 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
286 hints->ai_family == PF_INET6)) {
287 return EAI_FAMILY;
288 }
289
Luke Huangd8ac4752019-06-18 17:05:47 +0800290 // Socket types which are not in explore_options.
291 switch (hints->ai_socktype) {
292 case SOCK_RAW:
293 case SOCK_DGRAM:
294 case SOCK_STREAM:
295 case ANY:
296 break;
297 default:
298 return EAI_SOCKTYPE;
299 }
300
Luke Huang69e67182019-06-17 17:06:41 +0800301 if (hints->ai_socktype == ANY || hints->ai_protocol == ANY) return 0;
302
303 // if both socktype/protocol are specified, check if they are meaningful combination.
304 for (const Explore& ex : explore_options) {
305 if (hints->ai_family != ex.e_af) continue;
306 if (ex.e_socktype == ANY) continue;
307 if (ex.e_protocol == ANY) continue;
308 if (hints->ai_socktype == ex.e_socktype && hints->ai_protocol != ex.e_protocol) {
309 return EAI_BADHINTS;
310 }
311 }
312
313 return 0;
314}
315
316} // namespace
317
318int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
319 const addrinfo* hints, const android_net_context* netcontext,
lifr94981782019-05-17 21:15:19 +0800320 addrinfo** res, NetworkDnsEventReported* event) {
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900321 // hostname is allowed to be nullptr
322 // servname is allowed to be nullptr
323 // hints is allowed to be nullptr
324 assert(res != nullptr);
325 assert(netcontext != nullptr);
lifr94981782019-05-17 21:15:19 +0800326 assert(event != nullptr);
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900327
Luke Huang69e67182019-06-17 17:06:41 +0800328 addrinfo sentinel = {};
329 addrinfo* cur = &sentinel;
330 int error = 0;
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900331
Ken Chene0d73c92018-11-07 01:20:48 +0800332 do {
Luke Huang69e67182019-06-17 17:06:41 +0800333 if (hostname == nullptr && servname == nullptr) {
Ken Chene0d73c92018-11-07 01:20:48 +0800334 error = EAI_NONAME;
335 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900336 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900337
Luke Huang69e67182019-06-17 17:06:41 +0800338 if (hints && (error = validateHints(hints))) break;
339 addrinfo ai = hints ? *hints : addrinfo{};
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900340
Luke Huang69e67182019-06-17 17:06:41 +0800341 // Check for special cases:
342 // (1) numeric servname is disallowed if socktype/protocol are left unspecified.
343 // (2) servname is disallowed for raw and other inet{,6} sockets.
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900344 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
Luke Huang69e67182019-06-17 17:06:41 +0800345 addrinfo tmp = ai;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900346 if (tmp.ai_family == PF_UNSPEC) {
347 tmp.ai_family = PF_INET6;
Ken Chene0d73c92018-11-07 01:20:48 +0800348 }
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900349 error = get_portmatch(&tmp, servname);
Ken Chene0d73c92018-11-07 01:20:48 +0800350 if (error) break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900351 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900352
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900353 // NULL hostname, or numeric hostname
354 for (const Explore& ex : explore_options) {
Ken Chene0d73c92018-11-07 01:20:48 +0800355 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900356 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900357
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900358 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
359 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
360 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900361
Luke Huang69e67182019-06-17 17:06:41 +0800362 addrinfo tmp = ai;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900363 if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
364 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
365 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chene0d73c92018-11-07 01:20:48 +0800366
Ken Chenffc224a2019-03-19 17:41:28 +0800367 LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900368 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900369 if (hostname == nullptr)
370 error = explore_null(&tmp, servname, &cur->ai_next);
Ken Chene0d73c92018-11-07 01:20:48 +0800371 else
Bernie Innocenti0cfe9dc2019-02-20 18:39:35 +0900372 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
Ken Chene0d73c92018-11-07 01:20:48 +0800373
374 if (error) break;
375
376 while (cur->ai_next) cur = cur->ai_next;
377 }
378 if (error) break;
379
Luke Huang69e67182019-06-17 17:06:41 +0800380 // If numeric representation of AF1 can be interpreted as FQDN
381 // representation of AF2, we need to think again about the code below.
Ken Chene0d73c92018-11-07 01:20:48 +0800382 if (sentinel.ai_next) break;
383
Bernie Innocentib0b32bc2019-02-20 18:21:24 +0900384 if (hostname == nullptr) {
Ken Chene0d73c92018-11-07 01:20:48 +0800385 error = EAI_NODATA;
386 break;
387 }
Bernie Innocenti2b1afbe2019-02-20 17:50:38 +0900388 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chene0d73c92018-11-07 01:20:48 +0800389 error = EAI_NONAME;
390 break;
391 }
392
lifr94981782019-05-17 21:15:19 +0800393 return resolv_getaddrinfo(hostname, servname, hints, netcontext, res, event);
Ken Chene0d73c92018-11-07 01:20:48 +0800394 } while (0);
395
396 if (error) {
397 freeaddrinfo(sentinel.ai_next);
Bernie Innocentic50c43d2019-03-05 15:45:03 +0900398 *res = nullptr;
Ken Chene0d73c92018-11-07 01:20:48 +0800399 } else {
400 *res = sentinel.ai_next;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900401 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900402 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900403}
404
Luke Huang69e67182019-06-17 17:06:41 +0800405int resolv_getaddrinfo(const char* _Nonnull hostname, const char* servname, const addrinfo* hints,
lifr94981782019-05-17 21:15:19 +0800406 const android_net_context* _Nonnull netcontext, addrinfo** _Nonnull res,
407 NetworkDnsEventReported* _Nonnull event) {
Luke Huang69e67182019-06-17 17:06:41 +0800408 if (hostname == nullptr && servname == nullptr) return EAI_NONAME;
409 if (hostname == nullptr) return EAI_NODATA;
410
Luke Huang69e67182019-06-17 17:06:41 +0800411 // servname is allowed to be nullptr
412 // hints is allowed to be nullptr
413 assert(res != nullptr);
414 assert(netcontext != nullptr);
lifr94981782019-05-17 21:15:19 +0800415 assert(event != nullptr);
Luke Huang69e67182019-06-17 17:06:41 +0800416
417 int error = EAI_FAIL;
418 if (hints && (error = validateHints(hints))) {
419 *res = nullptr;
420 return error;
421 }
422
423 addrinfo ai = hints ? *hints : addrinfo{};
424 addrinfo sentinel = {};
425 addrinfo* cur = &sentinel;
426 // hostname as alphanumeric name.
427 // We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
428 for (const Explore& ex : explore_options) {
429 // Require exact match for family field
430 if (ai.ai_family != ex.e_af) continue;
431
432 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
433
434 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
435
436 addrinfo tmp = ai;
437 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
438 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
439
440 LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family
441 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
lifr94981782019-05-17 21:15:19 +0800442 error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext, event);
Luke Huang69e67182019-06-17 17:06:41 +0800443
444 while (cur->ai_next) cur = cur->ai_next;
445 }
446
Luke Huangd8ac4752019-06-18 17:05:47 +0800447 // Propagate the last error from explore_fqdn(), but only when *all* attempts failed.
Luke Huang69e67182019-06-17 17:06:41 +0800448 if ((*res = sentinel.ai_next)) return 0;
449
Luke Huangd8ac4752019-06-18 17:05:47 +0800450 // TODO: consider removing freeaddrinfo.
Luke Huang69e67182019-06-17 17:06:41 +0800451 freeaddrinfo(sentinel.ai_next);
452 *res = nullptr;
Luke Huangd8ac4752019-06-18 17:05:47 +0800453 return (error == 0) ? EAI_FAIL : error;
Luke Huang69e67182019-06-17 17:06:41 +0800454}
455
Bernie Innocenti9c575932018-09-07 21:10:25 +0900456// FQDN hostname, DNS lookup
Luke Huang69e67182019-06-17 17:06:41 +0800457static int explore_fqdn(const addrinfo* pai, const char* hostname, const char* servname,
lifr94981782019-05-17 21:15:19 +0800458 addrinfo** res, const android_net_context* netcontext,
459 NetworkDnsEventReported* event) {
Luke Huang69e67182019-06-17 17:06:41 +0800460 assert(pai != nullptr);
461 // hostname may be nullptr
462 // servname may be nullptr
463 assert(res != nullptr);
464
465 addrinfo* result = nullptr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900466 int error = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900467
Luke Huang69e67182019-06-17 17:06:41 +0800468 // If the servname does not match socktype/protocol, return error code.
469 if ((error = get_portmatch(pai, servname))) return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900470
chenbrucefd837fa2019-10-29 18:35:36 +0800471 if (!files_getaddrinfo(netcontext->dns_netid, hostname, pai, &result)) {
lifr94981782019-05-17 21:15:19 +0800472 error = dns_getaddrinfo(hostname, pai, netcontext, &result, event);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900473 }
Luke Huang69e67182019-06-17 17:06:41 +0800474 if (error) {
475 freeaddrinfo(result);
476 return error;
Bernie Innocenti455c6232018-09-12 21:32:42 +0900477 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900478
Luke Huang69e67182019-06-17 17:06:41 +0800479 for (addrinfo* cur = result; cur; cur = cur->ai_next) {
480 // canonname should be filled already
481 if ((error = get_port(cur, servname, 0))) {
482 freeaddrinfo(result);
483 return error;
484 }
485 }
486 *res = result;
487 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900488}
489
490/*
491 * hostname == NULL.
492 * passive socket -> anyaddr (0.0.0.0 or ::)
493 * non-passive socket -> localhost (127.0.0.1 or ::1)
494 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900495static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
496 int s;
497 const struct afd* afd;
498 struct addrinfo* cur;
499 struct addrinfo sentinel;
500 int error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900501
Ken Chenffc224a2019-03-19 17:41:28 +0800502 LOG(DEBUG) << __func__;
503
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900504 assert(pai != NULL);
505 /* servname may be NULL */
506 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900507
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900508 *res = NULL;
509 sentinel.ai_next = NULL;
510 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900511
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900512 /*
513 * filter out AFs that are not supported by the kernel
514 * XXX errno?
515 */
516 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
517 if (s < 0) {
518 if (errno != EMFILE) return 0;
519 } else
520 close(s);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900521
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900522 /*
523 * if the servname does not match socktype/protocol, ignore it.
524 */
525 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900526
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900527 afd = find_afd(pai->ai_family);
528 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900529
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900530 if (pai->ai_flags & AI_PASSIVE) {
531 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900532 GET_PORT(cur->ai_next, servname);
533 } else {
534 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900535 GET_PORT(cur->ai_next, servname);
536 }
537 cur = cur->ai_next;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900538
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900539 *res = sentinel.ai_next;
540 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900541
542free:
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900543 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900544 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900545}
546
547/*
548 * numeric hostname
549 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900550static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
551 struct addrinfo** res, const char* canonname) {
552 const struct afd* afd;
553 struct addrinfo* cur;
554 struct addrinfo sentinel;
555 int error;
556 char pton[PTON_MAX];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900557
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900558 assert(pai != NULL);
559 /* hostname may be NULL */
560 /* servname may be NULL */
561 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900562
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900563 *res = NULL;
564 sentinel.ai_next = NULL;
565 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900566
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900567 /*
568 * if the servname does not match socktype/protocol, ignore it.
569 */
570 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900571
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900572 afd = find_afd(pai->ai_family);
573 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900574
Ken Chena4b33022018-10-17 00:19:59 +0800575 if (inet_pton(afd->a_af, hostname, pton) == 1) {
576 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
577 GET_AI(cur->ai_next, afd, pton);
578 GET_PORT(cur->ai_next, servname);
579 if ((pai->ai_flags & AI_CANONNAME)) {
580 /*
581 * Set the numeric address itself as
582 * the canonical name, based on a
583 * clarification in rfc2553bis-03.
584 */
Ken Chene0d73c92018-11-07 01:20:48 +0800585 error = get_canonname(pai, cur->ai_next, canonname);
586 if (error != 0) {
587 freeaddrinfo(sentinel.ai_next);
588 return error;
589 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900590 }
Ken Chena4b33022018-10-17 00:19:59 +0800591 while (cur->ai_next) cur = cur->ai_next;
592 } else
Ken Chene0d73c92018-11-07 01:20:48 +0800593 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900594 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900595
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900596 *res = sentinel.ai_next;
597 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900598
599free:
Bernie Innocenti18a5ab52018-10-02 12:53:39 +0900600 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900601 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900602}
603
604/*
605 * numeric hostname with scope
606 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900607static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
608 const char* servname, struct addrinfo** res) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900609 const struct afd* afd;
610 struct addrinfo* cur;
611 int error;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900612 const char *cp, *scope, *addr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900613 struct sockaddr_in6* sin6;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900614
Ken Chenffc224a2019-03-19 17:41:28 +0800615 LOG(DEBUG) << __func__;
616
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900617 assert(pai != NULL);
618 /* hostname may be NULL */
619 /* servname may be NULL */
620 assert(res != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900621
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900622 /*
623 * if the servname does not match socktype/protocol, ignore it.
624 */
625 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900626
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900627 afd = find_afd(pai->ai_family);
628 if (afd == NULL) return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900629
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900630 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900631
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900632 cp = strchr(hostname, SCOPE_DELIMITER);
633 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900634
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900635 /*
636 * Handle special case of <scoped_address><delimiter><scope id>
637 */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900638 char* hostname2 = strdup(hostname);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900639 if (hostname2 == NULL) return EAI_MEMORY;
640 /* terminate at the delimiter */
641 hostname2[cp - hostname] = '\0';
642 addr = hostname2;
643 scope = cp + 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900644
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900645 error = explore_numeric(pai, addr, servname, res, hostname);
646 if (error == 0) {
chenbrucec51f1212019-09-12 16:59:33 +0800647 uint32_t scopeid;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900648
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900649 for (cur = *res; cur; cur = cur->ai_next) {
650 if (cur->ai_family != AF_INET6) continue;
651 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
652 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
653 free(hostname2);
654 return (EAI_NODATA); /* XXX: is return OK? */
655 }
656 sin6->sin6_scope_id = scopeid;
657 }
658 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900659
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900660 free(hostname2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900661
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900662 return error;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900663}
664
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900665static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
666 assert(pai != NULL);
667 assert(ai != NULL);
668 assert(str != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900669
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900670 if ((pai->ai_flags & AI_CANONNAME) != 0) {
671 ai->ai_canonname = strdup(str);
672 if (ai->ai_canonname == NULL) return EAI_MEMORY;
673 }
674 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900675}
676
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900677static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
678 const char* addr) {
679 char* p;
680 struct addrinfo* ai;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900681
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900682 assert(pai != NULL);
683 assert(afd != NULL);
684 assert(addr != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900685
nuccachena49c0ba2018-09-11 11:13:44 +0800686 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900687 if (ai == NULL) return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900688
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900689 memcpy(ai, pai, sizeof(struct addrinfo));
690 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachena49c0ba2018-09-11 11:13:44 +0800691 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900692
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900693 ai->ai_addrlen = afd->a_socklen;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900694 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
695 p = (char*) (void*) (ai->ai_addr);
696 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
697 return ai;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900698}
699
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900700static int get_portmatch(const struct addrinfo* ai, const char* servname) {
701 assert(ai != NULL);
702 /* servname may be NULL */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900703
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900704 return get_port(ai, servname, 1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900705}
706
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900707static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
708 const char* proto;
709 struct servent* sp;
710 int port;
711 int allownumeric;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900712
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900713 assert(ai != NULL);
714 /* servname may be NULL */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900715
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900716 if (servname == NULL) return 0;
717 switch (ai->ai_family) {
718 case AF_INET:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900719 case AF_INET6:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900720 break;
721 default:
722 return 0;
723 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900724
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900725 switch (ai->ai_socktype) {
726 case SOCK_RAW:
727 return EAI_SERVICE;
728 case SOCK_DGRAM:
729 case SOCK_STREAM:
730 allownumeric = 1;
731 break;
732 case ANY:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900733 allownumeric = 1;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900734 break;
735 default:
736 return EAI_SOCKTYPE;
737 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900738
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900739 port = str2number(servname);
740 if (port >= 0) {
741 if (!allownumeric) return EAI_SERVICE;
742 if (port < 0 || port > 65535) return EAI_SERVICE;
743 port = htons(port);
744 } else {
745 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900746
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900747 switch (ai->ai_socktype) {
748 case SOCK_DGRAM:
749 proto = "udp";
750 break;
751 case SOCK_STREAM:
752 proto = "tcp";
753 break;
754 default:
755 proto = NULL;
756 break;
757 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900758
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900759 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
760 port = sp->s_port;
761 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900762
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900763 if (!matchonly) {
764 switch (ai->ai_family) {
765 case AF_INET:
766 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
767 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900768 case AF_INET6:
769 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
770 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900771 }
772 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900773
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900774 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900775}
776
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900777static const struct afd* find_afd(int af) {
778 const struct afd* afd;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900779
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900780 if (af == PF_UNSPEC) return NULL;
781 for (afd = afdl; afd->a_af; afd++) {
782 if (afd->a_af == af) return afd;
783 }
784 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900785}
786
Bernie Innocenti9c575932018-09-07 21:10:25 +0900787// Convert a string to a scope identifier.
chenbrucec51f1212019-09-12 16:59:33 +0800788static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, uint32_t* scopeid) {
789 uint64_t lscopeid;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900790 struct in6_addr* a6;
791 char* ep;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900792
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900793 assert(scope != NULL);
794 assert(sin6 != NULL);
795 assert(scopeid != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900796
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900797 a6 = &sin6->sin6_addr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900798
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900799 /* empty scopeid portion is invalid */
800 if (*scope == '\0') return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900801
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900802 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
803 /*
804 * We currently assume a one-to-one mapping between links
805 * and interfaces, so we simply use interface indices for
806 * like-local scopes.
807 */
808 *scopeid = if_nametoindex(scope);
Bernie Innocenti63abf5b2019-06-11 21:46:51 +0900809 if (*scopeid != 0) return 0;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900810 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900811
Bernie Innocenti63abf5b2019-06-11 21:46:51 +0900812 // try to convert to a numeric id as a last resort
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900813 errno = 0;
814 lscopeid = strtoul(scope, &ep, 10);
chenbrucec51f1212019-09-12 16:59:33 +0800815 *scopeid = (uint32_t)(lscopeid & 0xffffffffUL);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900816 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
817 return 0;
818 else
819 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900820}
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900821
822/* code duplicate with gethnamaddr.c */
823
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900824#define BOUNDED_INCR(x) \
825 do { \
826 BOUNDS_CHECK(cp, x); \
827 cp += (x); \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900828 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900829
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900830#define BOUNDS_CHECK(ptr, count) \
831 do { \
832 if (eom - (ptr) < (count)) { \
Hungming Chendd4bfb92018-12-25 15:47:47 +0800833 *herrno = NO_RECOVERY; \
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900834 return NULL; \
835 } \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900836 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900837
Luke Huangc98fd802019-10-15 16:36:36 +0900838static struct addrinfo* getanswer(const std::vector<uint8_t>& answer, int anslen, const char* qname,
839 int qtype, const struct addrinfo* pai, int* herrno) {
Ken Chene0d73c92018-11-07 01:20:48 +0800840 struct addrinfo sentinel = {};
841 struct addrinfo *cur;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900842 struct addrinfo ai;
843 const struct afd* afd;
844 char* canonname;
845 const HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +0800846 const uint8_t* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900847 int n;
chenbrucec51f1212019-09-12 16:59:33 +0800848 const uint8_t* eom;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900849 char *bp, *ep;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900850 int type, ancount, qdcount;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900851 int haveanswer, had_error;
852 char tbuf[MAXDNAME];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900853 char hostbuf[8 * 1024];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900854
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900855 assert(qname != NULL);
856 assert(pai != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900857
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900858 cur = &sentinel;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900859
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900860 canonname = NULL;
Luke Huangc98fd802019-10-15 16:36:36 +0900861 eom = answer.data() + anslen;
Bernie Innocenti10a90282020-01-23 23:28:00 +0900862
863 bool (*name_ok)(const char* dn);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900864 switch (qtype) {
865 case T_A:
866 case T_AAAA:
867 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
868 name_ok = res_hnok;
869 break;
870 default:
871 return NULL; /* XXX should be abort(); */
872 }
873 /*
874 * find first satisfactory answer
875 */
Luke Huangc98fd802019-10-15 16:36:36 +0900876 hp = reinterpret_cast<const HEADER*>(answer.data());
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900877 ancount = ntohs(hp->ancount);
878 qdcount = ntohs(hp->qdcount);
879 bp = hostbuf;
880 ep = hostbuf + sizeof hostbuf;
Luke Huangc98fd802019-10-15 16:36:36 +0900881 cp = answer.data();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900882 BOUNDED_INCR(HFIXEDSZ);
883 if (qdcount != 1) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800884 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900885 return (NULL);
886 }
Luke Huangc98fd802019-10-15 16:36:36 +0900887 n = dn_expand(answer.data(), eom, cp, bp, ep - bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900888 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800889 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900890 return (NULL);
891 }
892 BOUNDED_INCR(n + QFIXEDSZ);
893 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
894 /* res_send() has already verified that the query name is the
895 * same as the one we sent; this just gets the expanded name
896 * (i.e., with the succeeding search-domain tacked on).
897 */
898 n = strlen(bp) + 1; /* for the \0 */
899 if (n >= MAXHOSTNAMELEN) {
Hungming Chendd4bfb92018-12-25 15:47:47 +0800900 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900901 return (NULL);
902 }
903 canonname = bp;
904 bp += n;
905 /* The qname can be abbreviated, but h_name is now absolute. */
906 qname = canonname;
907 }
908 haveanswer = 0;
909 had_error = 0;
910 while (ancount-- > 0 && cp < eom && !had_error) {
Luke Huangc98fd802019-10-15 16:36:36 +0900911 n = dn_expand(answer.data(), eom, cp, bp, ep - bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900912 if ((n < 0) || !(*name_ok)(bp)) {
913 had_error++;
914 continue;
915 }
916 cp += n; /* name */
917 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce0d470422019-03-28 18:44:37 +0800918 type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900919 cp += INT16SZ; /* type */
chenbruce0d470422019-03-28 18:44:37 +0800920 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900921 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce0d470422019-03-28 18:44:37 +0800922 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900923 cp += INT16SZ; /* len */
924 BOUNDS_CHECK(cp, n);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900925 if (cl != C_IN) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900926 /* XXX - debug? syslog? */
927 cp += n;
928 continue; /* XXX - had_error++ ? */
929 }
930 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
Luke Huangc98fd802019-10-15 16:36:36 +0900931 n = dn_expand(answer.data(), eom, cp, tbuf, sizeof tbuf);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900932 if ((n < 0) || !(*name_ok)(tbuf)) {
933 had_error++;
934 continue;
935 }
936 cp += n;
937 /* Get canonical name. */
938 n = strlen(tbuf) + 1; /* for the \0 */
939 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
940 had_error++;
941 continue;
942 }
943 strlcpy(bp, tbuf, (size_t)(ep - bp));
944 canonname = bp;
945 bp += n;
946 continue;
947 }
948 if (qtype == T_ANY) {
949 if (!(type == T_A || type == T_AAAA)) {
950 cp += n;
951 continue;
952 }
953 } else if (type != qtype) {
954 if (type != T_KEY && type != T_SIG)
Ken Chenffc224a2019-03-19 17:41:28 +0800955 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
956 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900957 cp += n;
958 continue; /* XXX - had_error++ ? */
959 }
960 switch (type) {
961 case T_A:
962 case T_AAAA:
963 if (strcasecmp(canonname, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800964 LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp
965 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900966 cp += n;
967 continue; /* XXX - had_error++ ? */
968 }
969 if (type == T_A && n != INADDRSZ) {
970 cp += n;
971 continue;
972 }
973 if (type == T_AAAA && n != IN6ADDRSZ) {
974 cp += n;
975 continue;
976 }
977 if (type == T_AAAA) {
978 struct in6_addr in6;
979 memcpy(&in6, cp, IN6ADDRSZ);
980 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
981 cp += n;
982 continue;
983 }
984 }
985 if (!haveanswer) {
986 int nn;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900987
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900988 canonname = bp;
989 nn = strlen(bp) + 1; /* for the \0 */
990 bp += nn;
991 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900992
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900993 /* don't overwrite pai */
994 ai = *pai;
995 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
996 afd = find_afd(ai.ai_family);
997 if (afd == NULL) {
998 cp += n;
999 continue;
1000 }
1001 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1002 if (cur->ai_next == NULL) had_error++;
1003 while (cur && cur->ai_next) cur = cur->ai_next;
1004 cp += n;
1005 break;
1006 default:
1007 abort();
1008 }
1009 if (!had_error) haveanswer++;
1010 }
1011 if (haveanswer) {
1012 if (!canonname)
1013 (void) get_canonname(pai, sentinel.ai_next, qname);
1014 else
1015 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chendd4bfb92018-12-25 15:47:47 +08001016 *herrno = NETDB_SUCCESS;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001017 return sentinel.ai_next;
1018 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001019
Hungming Chendd4bfb92018-12-25 15:47:47 +08001020 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001021 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001022}
1023
1024struct addrinfo_sort_elem {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001025 struct addrinfo* ai;
1026 int has_src_addr;
1027 sockaddr_union src_addr;
1028 int original_order;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001029};
1030
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001031static int _get_scope(const struct sockaddr* addr) {
1032 if (addr->sa_family == AF_INET6) {
1033 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1034 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1035 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1036 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1037 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1038 /*
1039 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1040 * link-local scope.
1041 */
1042 return IPV6_ADDR_SCOPE_LINKLOCAL;
1043 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1044 return IPV6_ADDR_SCOPE_SITELOCAL;
1045 } else {
1046 return IPV6_ADDR_SCOPE_GLOBAL;
1047 }
1048 } else if (addr->sa_family == AF_INET) {
1049 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1050 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001051
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001052 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1053 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1054 return IPV6_ADDR_SCOPE_LINKLOCAL;
1055 } else {
1056 /*
1057 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1058 * and shared addresses (100.64.0.0/10), are assigned global scope.
1059 */
1060 return IPV6_ADDR_SCOPE_GLOBAL;
1061 }
1062 } else {
1063 /*
1064 * This should never happen.
1065 * Return a scope with low priority as a last resort.
1066 */
1067 return IPV6_ADDR_SCOPE_NODELOCAL;
1068 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001069}
1070
1071/* These macros are modelled after the ones in <netinet/in6.h>. */
1072
1073/* RFC 4380, section 2.6 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001074#define IN6_IS_ADDR_TEREDO(a) \
1075 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001076
1077/* RFC 3056, section 2. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001078#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001079
1080/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001081#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001082
1083/*
1084 * Get the label for a given IPv4/IPv6 address.
1085 * RFC 6724, section 2.1.
1086 */
1087
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001088static int _get_label(const struct sockaddr* addr) {
1089 if (addr->sa_family == AF_INET) {
1090 return 4;
1091 } else if (addr->sa_family == AF_INET6) {
1092 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1093 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1094 return 0;
1095 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1096 return 4;
1097 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1098 return 2;
1099 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1100 return 5;
1101 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1102 return 13;
1103 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1104 return 3;
1105 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1106 return 11;
1107 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1108 return 12;
1109 } else {
1110 /* All other IPv6 addresses, including global unicast addresses. */
1111 return 1;
1112 }
1113 } else {
1114 /*
1115 * This should never happen.
1116 * Return a semi-random label as a last resort.
1117 */
1118 return 1;
1119 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001120}
1121
1122/*
1123 * Get the precedence for a given IPv4/IPv6 address.
1124 * RFC 6724, section 2.1.
1125 */
1126
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001127static int _get_precedence(const struct sockaddr* addr) {
1128 if (addr->sa_family == AF_INET) {
1129 return 35;
1130 } else if (addr->sa_family == AF_INET6) {
1131 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1132 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1133 return 50;
1134 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1135 return 35;
1136 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1137 return 30;
1138 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1139 return 5;
1140 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1141 return 3;
1142 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1143 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1144 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1145 return 1;
1146 } else {
1147 /* All other IPv6 addresses, including global unicast addresses. */
1148 return 40;
1149 }
1150 } else {
1151 return 1;
1152 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001153}
1154
1155/*
1156 * Find number of matching initial bits between the two addresses a1 and a2.
1157 */
1158
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001159static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1160 const char* p1 = (const char*) a1;
1161 const char* p2 = (const char*) a2;
1162 unsigned i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001163
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001164 for (i = 0; i < sizeof(*a1); ++i) {
1165 int x, j;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001166
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001167 if (p1[i] == p2[i]) {
1168 continue;
1169 }
1170 x = p1[i] ^ p2[i];
1171 for (j = 0; j < CHAR_BIT; ++j) {
1172 if (x & (1 << (CHAR_BIT - 1))) {
1173 return i * CHAR_BIT + j;
1174 }
1175 x <<= 1;
1176 }
1177 }
1178 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001179}
1180
1181/*
1182 * Compare two source/destination address pairs.
1183 * RFC 6724, section 6.
1184 */
1185
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001186static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1187 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1188 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1189 int scope_src1, scope_dst1, scope_match1;
1190 int scope_src2, scope_dst2, scope_match2;
1191 int label_src1, label_dst1, label_match1;
1192 int label_src2, label_dst2, label_match2;
1193 int precedence1, precedence2;
1194 int prefixlen1, prefixlen2;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001195
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001196 /* Rule 1: Avoid unusable destinations. */
1197 if (a1->has_src_addr != a2->has_src_addr) {
1198 return a2->has_src_addr - a1->has_src_addr;
1199 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001200
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001201 /* Rule 2: Prefer matching scope. */
nuccachenb980f2f2018-10-23 17:10:58 +08001202 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001203 scope_dst1 = _get_scope(a1->ai->ai_addr);
1204 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001205
nuccachenb980f2f2018-10-23 17:10:58 +08001206 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001207 scope_dst2 = _get_scope(a2->ai->ai_addr);
1208 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001209
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001210 if (scope_match1 != scope_match2) {
1211 return scope_match2 - scope_match1;
1212 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001213
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001214 /*
1215 * Rule 3: Avoid deprecated addresses.
1216 * TODO(sesse): We don't currently have a good way of finding this.
1217 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001218
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001219 /*
1220 * Rule 4: Prefer home addresses.
1221 * TODO(sesse): We don't currently have a good way of finding this.
1222 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001223
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001224 /* Rule 5: Prefer matching label. */
nuccachenb980f2f2018-10-23 17:10:58 +08001225 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001226 label_dst1 = _get_label(a1->ai->ai_addr);
1227 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001228
nuccachenb980f2f2018-10-23 17:10:58 +08001229 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001230 label_dst2 = _get_label(a2->ai->ai_addr);
1231 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001232
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001233 if (label_match1 != label_match2) {
1234 return label_match2 - label_match1;
1235 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001236
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001237 /* Rule 6: Prefer higher precedence. */
1238 precedence1 = _get_precedence(a1->ai->ai_addr);
1239 precedence2 = _get_precedence(a2->ai->ai_addr);
1240 if (precedence1 != precedence2) {
1241 return precedence2 - precedence1;
1242 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001243
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001244 /*
1245 * Rule 7: Prefer native transport.
1246 * TODO(sesse): We don't currently have a good way of finding this.
1247 */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001248
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001249 /* Rule 8: Prefer smaller scope. */
1250 if (scope_dst1 != scope_dst2) {
1251 return scope_dst1 - scope_dst2;
1252 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001253
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001254 /*
1255 * Rule 9: Use longest matching prefix.
1256 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1257 * to work very well directly applied to IPv4. (glibc uses information from
1258 * the routing table for a custom IPv4 implementation here.)
1259 */
1260 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1261 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachenb980f2f2018-10-23 17:10:58 +08001262 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001263 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachenb980f2f2018-10-23 17:10:58 +08001264 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001265 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1266 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1267 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1268 if (prefixlen1 != prefixlen2) {
1269 return prefixlen2 - prefixlen1;
1270 }
1271 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001272
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001273 /*
1274 * Rule 10: Leave the order unchanged.
1275 * We need this since qsort() is not necessarily stable.
1276 */
1277 return a1->original_order - a2->original_order;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001278}
1279
1280/*
1281 * Find the source address that will be used if trying to connect to the given
1282 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1283 *
1284 * Returns 1 if a source address was found, 0 if the address is unreachable,
1285 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1286 * undefined.
1287 */
1288
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001289static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1290 uid_t uid) {
1291 int sock;
1292 int ret;
1293 socklen_t len;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001294
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001295 switch (addr->sa_family) {
1296 case AF_INET:
1297 len = sizeof(struct sockaddr_in);
1298 break;
1299 case AF_INET6:
1300 len = sizeof(struct sockaddr_in6);
1301 break;
1302 default:
1303 /* No known usable source address for non-INET families. */
1304 return 0;
1305 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001306
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001307 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1308 if (sock == -1) {
1309 if (errno == EAFNOSUPPORT) {
1310 return 0;
1311 } else {
1312 return -1;
1313 }
1314 }
1315 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1316 close(sock);
1317 return 0;
1318 }
1319 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1320 close(sock);
1321 return 0;
1322 }
1323 do {
Bernie Innocentiafaacf72018-08-30 07:34:37 +09001324 ret = connect(sock, addr, len);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001325 } while (ret == -1 && errno == EINTR);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001326
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001327 if (ret == -1) {
1328 close(sock);
1329 return 0;
1330 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001331
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001332 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1333 close(sock);
1334 return -1;
1335 }
1336 close(sock);
1337 return 1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001338}
1339
1340/*
1341 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1342 * Will leave the list unchanged if an error occurs.
1343 */
1344
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001345static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1346 struct addrinfo* cur;
1347 int nelem = 0, i;
1348 struct addrinfo_sort_elem* elems;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001349
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001350 cur = list_sentinel->ai_next;
1351 while (cur) {
1352 ++nelem;
1353 cur = cur->ai_next;
1354 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001355
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001356 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1357 if (elems == NULL) {
1358 goto error;
1359 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001360
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001361 /*
1362 * Convert the linked list to an array that also contains the candidate
1363 * source address for each destination address.
1364 */
1365 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1366 int has_src_addr;
1367 assert(cur != NULL);
1368 elems[i].ai = cur;
1369 elems[i].original_order = i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001370
nuccachenb980f2f2018-10-23 17:10:58 +08001371 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001372 if (has_src_addr == -1) {
1373 goto error;
1374 }
1375 elems[i].has_src_addr = has_src_addr;
1376 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001377
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001378 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1379 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001380
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001381 list_sentinel->ai_next = elems[0].ai;
1382 for (i = 0; i < nelem - 1; ++i) {
1383 elems[i].ai->ai_next = elems[i + 1].ai;
1384 }
1385 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001386
1387error:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001388 free(elems);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001389}
1390
Bernie Innocenti455c6232018-09-12 21:32:42 +09001391static int dns_getaddrinfo(const char* name, const addrinfo* pai,
lifr94981782019-05-17 21:15:19 +08001392 const android_net_context* netcontext, addrinfo** rv,
1393 NetworkDnsEventReported* event) {
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001394 res_target q = {};
1395 res_target q2 = {};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001396
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001397 switch (pai->ai_family) {
Bernie Innocenti9c575932018-09-07 21:10:25 +09001398 case AF_UNSPEC: {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001399 /* prefer IPv6 */
1400 q.name = name;
1401 q.qclass = C_IN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001402 int query_ipv6 = 1, query_ipv4 = 1;
1403 if (pai->ai_flags & AI_ADDRCONFIG) {
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001404 query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid);
1405 query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001406 }
1407 if (query_ipv6) {
1408 q.qtype = T_AAAA;
1409 if (query_ipv4) {
1410 q.next = &q2;
1411 q2.name = name;
1412 q2.qclass = C_IN;
1413 q2.qtype = T_A;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001414 }
1415 } else if (query_ipv4) {
1416 q.qtype = T_A;
1417 } else {
Bernie Innocenti455c6232018-09-12 21:32:42 +09001418 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001419 }
1420 break;
Bernie Innocenti9c575932018-09-07 21:10:25 +09001421 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001422 case AF_INET:
1423 q.name = name;
1424 q.qclass = C_IN;
1425 q.qtype = T_A;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001426 break;
1427 case AF_INET6:
1428 q.name = name;
1429 q.qclass = C_IN;
1430 q.qtype = T_AAAA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001431 break;
1432 default:
Bernie Innocenti455c6232018-09-12 21:32:42 +09001433 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001434 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001435
Bernie Innocenti08487112019-10-11 21:14:13 +09001436 ResState res;
1437 res_init(&res, netcontext, event);
Mike Yubfb1b342018-11-06 15:42:36 +08001438
Hungming Chena6914a62019-01-19 15:07:04 +08001439 int he;
Bernie Innocenti08487112019-10-11 21:14:13 +09001440 if (res_searchN(name, &q, &res, &he) < 0) {
Hungming Chena6914a62019-01-19 15:07:04 +08001441 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
1442 // Note that res_searchN() doesn't set the pair NETDB_INTERNAL and errno.
1443 // See also herrnoToAiErrno().
1444 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001445 }
Bernie Innocentic50c43d2019-03-05 15:45:03 +09001446
1447 addrinfo sentinel = {};
1448 addrinfo* cur = &sentinel;
Luke Huangc98fd802019-10-15 16:36:36 +09001449 addrinfo* ai = getanswer(q.answer, q.n, q.name, q.qtype, pai, &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001450 if (ai) {
1451 cur->ai_next = ai;
1452 while (cur && cur->ai_next) cur = cur->ai_next;
1453 }
1454 if (q.next) {
Luke Huangc98fd802019-10-15 16:36:36 +09001455 ai = getanswer(q2.answer, q2.n, q2.name, q2.qtype, pai, &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001456 if (ai) cur->ai_next = ai;
1457 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001458 if (sentinel.ai_next == NULL) {
Hungming Chena6914a62019-01-19 15:07:04 +08001459 // Note that getanswer() doesn't set the pair NETDB_INTERNAL and errno.
1460 // See also herrnoToAiErrno().
1461 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001462 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001463
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001464 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001465
Bernie Innocenti455c6232018-09-12 21:32:42 +09001466 *rv = sentinel.ai_next;
1467 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001468}
1469
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001470static void _sethtent(FILE** hostf) {
1471 if (!*hostf)
1472 *hostf = fopen(_PATH_HOSTS, "re");
1473 else
1474 rewind(*hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001475}
1476
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001477static void _endhtent(FILE** hostf) {
1478 if (*hostf) {
1479 (void) fclose(*hostf);
1480 *hostf = NULL;
1481 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001482}
1483
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001484static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1485 char* p;
1486 char *cp, *tname, *cname;
Bernie Innocentie2bc46f2018-10-16 23:35:28 +09001487 struct addrinfo *res0, *res;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001488 int error;
1489 const char* addr;
1490 char hostbuf[8 * 1024];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001491
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001492 assert(name != NULL);
1493 assert(pai != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001494
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001495 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1496again:
1497 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1498 if (*p == '#') goto again;
1499 if (!(cp = strpbrk(p, "#\n"))) goto again;
1500 *cp = '\0';
1501 if (!(cp = strpbrk(p, " \t"))) goto again;
1502 *cp++ = '\0';
1503 addr = p;
1504 /* if this is not something we're looking for, skip it. */
1505 cname = NULL;
1506 while (cp && *cp) {
1507 if (*cp == ' ' || *cp == '\t') {
1508 cp++;
1509 continue;
1510 }
1511 if (!cname) cname = cp;
1512 tname = cp;
1513 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001514 if (strcasecmp(name, tname) == 0) goto found;
1515 }
1516 goto again;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001517
1518found:
Bernie Innocentie2bc46f2018-10-16 23:35:28 +09001519 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001520 if (error) goto again;
1521 for (res = res0; res; res = res->ai_next) {
1522 /* cover it up */
1523 res->ai_flags = pai->ai_flags;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001524
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001525 if (pai->ai_flags & AI_CANONNAME) {
1526 if (get_canonname(pai, res, cname) != 0) {
1527 freeaddrinfo(res0);
1528 goto again;
1529 }
1530 }
1531 }
1532 return res0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001533}
1534
chenbrucefd837fa2019-10-29 18:35:36 +08001535static struct addrinfo* getCustomHosts(const size_t netid, const char* _Nonnull name,
1536 const struct addrinfo* _Nonnull pai) {
1537 struct addrinfo sentinel = {};
1538 struct addrinfo *res0, *res;
1539 res = &sentinel;
1540 std::vector<std::string> hosts = getCustomizedTableByName(netid, name);
1541 for (const std::string& host : hosts) {
1542 int error = getaddrinfo_numeric(host.c_str(), nullptr, *pai, &res0);
1543 if (!error && res0 != nullptr) {
1544 res->ai_next = res0;
1545 res = res0;
1546 res0 = nullptr;
1547 }
1548 }
1549 return sentinel.ai_next;
1550}
1551
1552static bool files_getaddrinfo(const size_t netid, const char* name, const addrinfo* pai,
1553 addrinfo** res) {
Ken Chene0d73c92018-11-07 01:20:48 +08001554 struct addrinfo sentinel = {};
1555 struct addrinfo *p, *cur;
chenbrucefd837fa2019-10-29 18:35:36 +08001556 FILE* hostf = nullptr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001557
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001558 cur = &sentinel;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001559 _sethtent(&hostf);
chenbrucefd837fa2019-10-29 18:35:36 +08001560 while ((p = _gethtent(&hostf, name, pai)) != nullptr) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001561 cur->ai_next = p;
1562 while (cur && cur->ai_next) cur = cur->ai_next;
1563 }
1564 _endhtent(&hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001565
chenbrucefd837fa2019-10-29 18:35:36 +08001566 if ((p = getCustomHosts(netid, name, pai)) != nullptr) {
1567 cur->ai_next = p;
1568 }
1569
Bernie Innocenti455c6232018-09-12 21:32:42 +09001570 *res = sentinel.ai_next;
chenbrucefd837fa2019-10-29 18:35:36 +08001571 return sentinel.ai_next != nullptr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001572}
1573
1574/* resolver logic */
1575
1576/*
1577 * Formulate a normal query, send, and await answer.
1578 * Returned answer is placed in supplied buffer "answer".
1579 * Perform preliminary check of answer, returning success only
1580 * if no error is indicated and the answer count is nonzero.
1581 * Return the size of the response on success, -1 on error.
Hungming Chendd4bfb92018-12-25 15:47:47 +08001582 * Error number is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001583 *
1584 * Caller must parse answer and determine whether it answers the question.
1585 */
Hungming Chen947aab02018-12-27 18:33:19 +08001586static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
chenbrucec51f1212019-09-12 16:59:33 +08001587 uint8_t buf[MAXPACKET];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001588 int n;
1589 struct res_target* t;
1590 int rcode;
1591 int ancount;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001592
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001593 assert(name != NULL);
1594 /* XXX: target may be NULL??? */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001595
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001596 rcode = NOERROR;
1597 ancount = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001598
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001599 for (t = target; t; t = t->next) {
Luke Huangc98fd802019-10-15 16:36:36 +09001600 HEADER* hp = (HEADER*)(void*)t->answer.data();
Ken Chen0a015532019-01-02 14:59:38 +08001601 bool retried = false;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001602 again:
1603 hp->rcode = NOERROR; /* default */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001604
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001605 /* make it easier... */
Bernie Innocenti9c575932018-09-07 21:10:25 +09001606 int cl = t->qclass;
1607 int type = t->qtype;
Luke Huangc98fd802019-10-15 16:36:36 +09001608 const int anslen = t->answer.size();
chenbruceacb832c2019-02-20 19:45:50 +08001609
Ken Chenffc224a2019-03-19 17:41:28 +08001610 LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001611
Bernie Innocenti08487112019-10-11 21:14:13 +09001612 n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, sizeof(buf),
1613 res->netcontext_flags);
chenbruced8cbb9b2019-06-20 18:25:28 +08001614 if (n > 0 &&
1615 (res->netcontext_flags &
1616 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
1617 !retried) // TODO: remove the retry flag and provide a sufficient test coverage.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001618 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001619 if (n <= 0) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001620 LOG(ERROR) << __func__ << ": res_nmkquery failed";
Hungming Chendd4bfb92018-12-25 15:47:47 +08001621 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001622 return n;
1623 }
Mike Yubfb1b342018-11-06 15:42:36 +08001624
Luke Huangc98fd802019-10-15 16:36:36 +09001625 n = res_nsend(res, buf, n, t->answer.data(), anslen, &rcode, 0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001626 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen947aab02018-12-27 18:33:19 +08001627 // Record rcode from DNS response header only if no timeout.
1628 // Keep rcode timeout for reporting later if any.
chenbruced8cbb9b2019-06-20 18:25:28 +08001629 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; // record most recent error
1630 // if the query choked with EDNS0, retry without EDNS0 that when the server
1631 // has no response, resovler won't retry and do nothing. Even fallback to UDP,
1632 // we also has the same symptom if EDNS is enabled.
1633 if ((res->netcontext_flags &
1634 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
Ken Chen0a015532019-01-02 14:59:38 +08001635 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001636 LOG(DEBUG) << __func__ << ": retry without EDNS0";
Ken Chen0a015532019-01-02 14:59:38 +08001637 retried = true;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001638 goto again;
1639 }
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001640 LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001641 continue;
1642 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001643
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001644 ancount += ntohs(hp->ancount);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001645
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001646 t->n = n;
1647 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001648
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001649 if (ancount == 0) {
1650 switch (rcode) {
Hungming Chen947aab02018-12-27 18:33:19 +08001651 // Not defined in RFC.
1652 case RCODE_TIMEOUT:
1653 // DNS metrics monitors DNS query timeout.
1654 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1655 break;
1656 // Defined in RFC 1035 section 4.1.1.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001657 case NXDOMAIN:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001658 *herrno = HOST_NOT_FOUND;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001659 break;
1660 case SERVFAIL:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001661 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001662 break;
1663 case NOERROR:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001664 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001665 break;
1666 case FORMERR:
1667 case NOTIMP:
1668 case REFUSED:
1669 default:
Hungming Chendd4bfb92018-12-25 15:47:47 +08001670 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001671 break;
1672 }
1673 return -1;
1674 }
1675 return ancount;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001676}
1677
1678/*
1679 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1680 * Return the size of the response on success, -1 on error.
1681 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chendd4bfb92018-12-25 15:47:47 +08001682 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001683 */
Hungming Chen947aab02018-12-27 18:33:19 +08001684static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Luke Huang2dac4382019-06-24 13:28:44 +08001685 const char* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001686 HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +08001687 uint32_t dots;
chenbruce018fdb22019-06-12 18:08:04 +08001688 int ret, saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001689 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001690
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001691 assert(name != NULL);
1692 assert(target != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001693
Luke Huangc98fd802019-10-15 16:36:36 +09001694 hp = (HEADER*)(void*)target->answer.data();
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001695
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001696 errno = 0;
Hungming Chendd4bfb92018-12-25 15:47:47 +08001697 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001698 dots = 0;
1699 for (cp = name; *cp; cp++) dots += (*cp == '.');
chenbruce018fdb22019-06-12 18:08:04 +08001700 const bool trailing_dot = (cp > name && *--cp == '.') ? true : false;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001701
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001702 /*
1703 * If there are dots in the name already, let's just give it a try
1704 * 'as is'. The threshold can be set with the "ndots" option.
1705 */
1706 saved_herrno = -1;
1707 if (dots >= res->ndots) {
Hungming Chen947aab02018-12-27 18:33:19 +08001708 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001709 if (ret > 0) return (ret);
Hungming Chendd4bfb92018-12-25 15:47:47 +08001710 saved_herrno = *herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001711 tried_as_is++;
1712 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001713
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001714 /*
1715 * We do at least one level of search if
chenbruce018fdb22019-06-12 18:08:04 +08001716 * - there is no dot, or
1717 * - there is at least one dot and there is no trailing dot.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001718 */
chenbruce018fdb22019-06-12 18:08:04 +08001719 if ((!dots) || (dots && !trailing_dot)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001720 int done = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001721
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001722 /* Unfortunately we need to set stuff up before
1723 * the domain stuff is tried. Will have a better
1724 * fix after thread pools are used.
1725 */
Mike Yu021c3e12019-10-08 17:29:34 +08001726 resolv_populate_res_for_net(res);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001727
Luke Huang2dac4382019-06-24 13:28:44 +08001728 for (const auto& domain : res->search_domains) {
1729 ret = res_querydomainN(name, domain.c_str(), target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001730 if (ret > 0) return ret;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001731
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001732 /*
1733 * If no server present, give up.
1734 * If name isn't found in this domain,
1735 * keep trying higher domains in the search list
1736 * (if that's enabled).
1737 * On a NO_DATA error, keep trying, otherwise
1738 * a wildcard entry of another type could keep us
1739 * from finding this entry higher in the domain.
1740 * If we get some other error (negative answer or
1741 * server failure), then stop searching up,
1742 * but try the input name below in case it's
1743 * fully-qualified.
1744 */
1745 if (errno == ECONNREFUSED) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001746 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001747 return -1;
1748 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001749
Hungming Chendd4bfb92018-12-25 15:47:47 +08001750 switch (*herrno) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001751 case NO_DATA:
1752 got_nodata++;
Bernie Innocentif40b3bd2018-10-10 22:30:12 +09001753 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001754 case HOST_NOT_FOUND:
1755 /* keep trying */
1756 break;
1757 case TRY_AGAIN:
1758 if (hp->rcode == SERVFAIL) {
1759 /* try next search element, if any */
1760 got_servfail++;
1761 break;
1762 }
Bernie Innocentif40b3bd2018-10-10 22:30:12 +09001763 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001764 default:
1765 /* anything else implies that we're done */
1766 done++;
1767 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001768 }
1769 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001770
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001771 /*
1772 * if we have not already tried the name "as is", do that now.
1773 * note that we do this regardless of how many dots were in the
1774 * name or whether it ends with a dot.
1775 */
1776 if (!tried_as_is) {
Hungming Chen947aab02018-12-27 18:33:19 +08001777 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001778 if (ret > 0) return ret;
1779 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001780
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001781 /*
1782 * if we got here, we didn't satisfy the search.
1783 * if we did an initial full query, return that query's h_errno
1784 * (note that we wouldn't be here if that query had succeeded).
1785 * else if we ever got a nodata, send that back as the reason.
1786 * else send back meaningless h_errno, that being the one from
1787 * the last DNSRCH we did.
1788 */
1789 if (saved_herrno != -1)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001790 *herrno = saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001791 else if (got_nodata)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001792 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001793 else if (got_servfail)
Hungming Chendd4bfb92018-12-25 15:47:47 +08001794 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001795 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001796}
1797
1798/*
1799 * Perform a call on res_query on the concatenation of name and domain,
1800 * removing a trailing dot from name if domain is NULL.
1801 */
Mike Yubfb1b342018-11-06 15:42:36 +08001802static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen947aab02018-12-27 18:33:19 +08001803 int* herrno) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001804 char nbuf[MAXDNAME];
1805 const char* longname = nbuf;
1806 size_t n, d;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001807
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001808 assert(name != NULL);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001809
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001810 if (domain == NULL) {
Bernie Innocenti3952ccc2019-03-03 19:39:53 +09001811 // Check for trailing '.'; copy without '.' if present.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001812 n = strlen(name);
1813 if (n + 1 > sizeof(nbuf)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001814 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001815 return -1;
1816 }
1817 if (n > 0 && name[--n] == '.') {
1818 strncpy(nbuf, name, n);
1819 nbuf[n] = '\0';
1820 } else
1821 longname = name;
1822 } else {
1823 n = strlen(name);
1824 d = strlen(domain);
1825 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chendd4bfb92018-12-25 15:47:47 +08001826 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001827 return -1;
1828 }
1829 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1830 }
Hungming Chen947aab02018-12-27 18:33:19 +08001831 return res_queryN(longname, target, res, herrno);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001832}