blob: bb9ef0717adf7fcfdc2e846a2c03ab1af403f397 [file] [log] [blame]
Bernie Innocenti55864192018-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
Bernie Innocentid017e972019-03-03 19:39:53 +090033#define LOG_TAG "getaddrinfo"
34
Bernie Innocenti9fa30802019-05-29 10:51:38 -070035#include "getaddrinfo.h"
36
Bernie Innocenti55864192018-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 Innocentif12d5bb2018-08-31 14:09:46 +090042#include <fcntl.h>
43#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090044#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090045#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090046#include <stdbool.h>
47#include <stddef.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090051#include <sys/param.h>
52#include <sys/socket.h>
53#include <sys/stat.h>
54#include <sys/types.h>
55#include <sys/un.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090056#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090057
chenbruce16adee42019-02-20 19:45:50 +080058#include <android-base/logging.h>
59
Bernie Innocenti189eb502018-10-01 23:10:18 +090060#include "netd_resolv/resolv.h"
61#include "resolv_cache.h"
62#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090063
Bernie Innocenti55864192018-08-30 04:05:20 +090064#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +090065
Frank Li08ce7b82019-06-27 10:11:52 -070066using android::net::NetworkDnsEventReported;
67
Bernie Innocenti93a31342018-12-12 00:43:02 +090068const char in_addrany[] = {0, 0, 0, 0};
69const char in_loopback[] = {127, 0, 0, 1};
70const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
71const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +090072
Bernie Innocenti93a31342018-12-12 00:43:02 +090073const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090074 int a_af;
75 int a_addrlen;
76 int a_socklen;
77 int a_off;
78 const char* a_addrany;
79 const char* a_loopback;
80 int a_scoped;
81} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
83 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090084 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
85 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
86 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +090087};
88
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090089struct Explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090090 int e_af;
91 int e_socktype;
92 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090093 int e_wild;
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090094#define WILD_AF(ex) ((ex).e_wild & 0x01)
95#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
96#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +090097};
98
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090099const Explore explore_options[] = {
Ken Chen3270cf52018-11-07 01:20:48 +0800100 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
101 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
102 {PF_INET6, SOCK_RAW, ANY, 0x05},
103 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
104 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
105 {PF_INET, SOCK_RAW, ANY, 0x05},
106 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
107 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
108 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900109};
110
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900112#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900113
114typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900115 HEADER hdr;
116 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900117} querybuf;
118
119struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 struct res_target* next;
121 const char* name; /* domain name */
122 int qclass, qtype; /* class and type of query */
123 u_char* answer; /* buffer to put answer */
124 int anslen; /* size of answer buffer */
125 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900126};
127
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900128static int str2number(const char*);
129static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
Frank Li08ce7b82019-06-27 10:11:52 -0700130 const struct android_net_context*, NetworkDnsEventReported* event);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900131static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
132static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
133 const char*);
134static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
135 struct addrinfo**);
136static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
137static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
138static int get_portmatch(const struct addrinfo*, const char*);
139static int get_port(const struct addrinfo*, const char*, int);
140static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900141static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900142
Hungming Chend57ade02018-12-25 15:47:47 +0800143static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
144 int* herrno);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900145static int dns_getaddrinfo(const char* name, const addrinfo* pai,
Frank Li08ce7b82019-06-27 10:11:52 -0700146 const android_net_context* netcontext, addrinfo** rv,
147 NetworkDnsEventReported* event);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900148static void _sethtent(FILE**);
149static void _endhtent(FILE**);
150static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900151static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900153
Hungming Chen7f0d3292018-12-27 18:33:19 +0800154static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
155static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800156static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800157 int* herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900158
Bernie Innocenti93a31342018-12-12 00:43:02 +0900159const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 "Success",
161 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
162 "Temporary failure in name resolution", /* EAI_AGAIN */
163 "Invalid value for ai_flags", /* EAI_BADFLAGS */
164 "Non-recoverable failure in name resolution", /* EAI_FAIL */
165 "ai_family not supported", /* EAI_FAMILY */
166 "Memory allocation failure", /* EAI_MEMORY */
167 "No address associated with hostname", /* EAI_NODATA */
168 "hostname nor servname provided, or not known", /* EAI_NONAME */
169 "servname not supported for ai_socktype", /* EAI_SERVICE */
170 "ai_socktype not supported", /* EAI_SOCKTYPE */
171 "System error returned in errno", /* EAI_SYSTEM */
172 "Invalid value for hints", /* EAI_BADHINTS */
173 "Resolved protocol is unknown", /* EAI_PROTOCOL */
174 "Argument buffer overflow", /* EAI_OVERFLOW */
175 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900176};
177
178/* XXX macros that make external reference is BAD. */
179
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900180#define GET_AI(ai, afd, addr) \
181 do { \
182 /* external reference: pai, error, and label free */ \
183 (ai) = get_ai(pai, (afd), (addr)); \
184 if ((ai) == NULL) { \
185 error = EAI_MEMORY; \
186 goto free; \
187 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900188 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900189
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190#define GET_PORT(ai, serv) \
191 do { \
192 /* external reference: error and label free */ \
193 error = get_port((ai), (serv), 0); \
194 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900195 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900196
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900198 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
199#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201const char* gai_strerror(int ecode) {
202 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
203 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900204}
205
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900207 while (ai) {
208 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900209 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900210 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211 free(ai);
212 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900213 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900214}
215
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900216static int str2number(const char* p) {
217 char* ep;
218 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900221
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900222 if (*p == '\0') return -1;
223 ep = NULL;
224 errno = 0;
225 v = strtoul(p, &ep, 10);
226 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
227 return v;
228 else
229 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900230}
231
232/*
233 * The following functions determine whether IPv4 or IPv6 connectivity is
234 * available in order to implement AI_ADDRCONFIG.
235 *
236 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
237 * available, but whether addresses of the specified family are "configured
238 * on the local system". However, bionic doesn't currently support getifaddrs,
239 * so checking for connectivity is the next best thing.
240 */
Bernie Innocenti43b97d92019-03-05 15:45:03 +0900241static int have_ipv6(unsigned mark, uid_t uid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242 static const struct sockaddr_in6 sin6_test = {
243 .sin6_family = AF_INET6,
244 .sin6_addr.s6_addr = {// 2000::
245 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800246 sockaddr_union addr = {.sin6 = sin6_test};
247 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900248}
249
Bernie Innocenti43b97d92019-03-05 15:45:03 +0900250static int have_ipv4(unsigned mark, uid_t uid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900251 static const struct sockaddr_in sin_test = {
252 .sin_family = AF_INET,
253 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
254 };
nuccachene172a4e2018-10-23 17:10:58 +0800255 sockaddr_union addr = {.sin = sin_test};
256 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900257}
258
Bernie Innocentic165ce82018-10-16 23:35:28 +0900259// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
260// NOTE: also called by resolv_set_nameservers_for_net().
261int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
262 addrinfo** result) {
263 hints.ai_flags = AI_NUMERICHOST;
264 const android_net_context netcontext = {
265 .app_netid = NETID_UNSET,
266 .app_mark = MARK_UNSET,
267 .dns_netid = NETID_UNSET,
268 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900269 .uid = NET_CONTEXT_INVALID_UID,
270 };
Frank Li08ce7b82019-06-27 10:11:52 -0700271 NetworkDnsEventReported event;
272 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result,
273 &event);
Bernie Innocenti55864192018-08-30 04:05:20 +0900274}
275
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
277 const struct addrinfo* hints,
278 const struct android_net_context* netcontext,
Frank Li08ce7b82019-06-27 10:11:52 -0700279 struct addrinfo** res, NetworkDnsEventReported* event) {
Ken Chen3270cf52018-11-07 01:20:48 +0800280 struct addrinfo sentinel = {};
Bernie Innocentib47552e2019-02-20 18:39:35 +0900281 struct addrinfo* cur = &sentinel;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900282 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900283
Bernie Innocentib47552e2019-02-20 18:39:35 +0900284 // hostname is allowed to be nullptr
285 // servname is allowed to be nullptr
286 // hints is allowed to be nullptr
287 assert(res != nullptr);
288 assert(netcontext != nullptr);
Frank Li08ce7b82019-06-27 10:11:52 -0700289 assert(event != nullptr);
Bernie Innocenti2319a772019-02-20 17:50:38 +0900290
Bernie Innocentib47552e2019-02-20 18:39:35 +0900291 struct addrinfo ai = {
292 .ai_flags = 0,
293 .ai_family = PF_UNSPEC,
294 .ai_socktype = ANY,
295 .ai_protocol = ANY,
296 .ai_addrlen = 0,
297 .ai_canonname = nullptr,
298 .ai_addr = nullptr,
299 .ai_next = nullptr,
300 };
Bernie Innocenti2319a772019-02-20 17:50:38 +0900301
Ken Chen3270cf52018-11-07 01:20:48 +0800302 do {
303 if (hostname == NULL && servname == NULL) {
304 error = EAI_NONAME;
305 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900306 }
Ken Chen3270cf52018-11-07 01:20:48 +0800307 if (hints) {
308 /* error check for hints */
309 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
310 error = EAI_BADHINTS;
311 break;
312 }
313 if (hints->ai_flags & ~AI_MASK) {
314 error = EAI_BADFLAGS;
315 break;
316 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900317
Ken Chen3270cf52018-11-07 01:20:48 +0800318 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
319 hints->ai_family == PF_INET6)) {
320 error = EAI_FAMILY;
321 break;
322 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900323
324 ai = *hints;
Ken Chen3270cf52018-11-07 01:20:48 +0800325
326 /*
327 * if both socktype/protocol are specified, check if they
328 * are meaningful combination.
329 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900330 if (ai.ai_socktype != ANY && ai.ai_protocol != ANY) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900331 for (const Explore& ex : explore_options) {
332 if (ai.ai_family != ex.e_af) continue;
333 if (ex.e_socktype == ANY) continue;
334 if (ex.e_protocol == ANY) continue;
335 if (ai.ai_socktype == ex.e_socktype && ai.ai_protocol != ex.e_protocol) {
Ken Chen3270cf52018-11-07 01:20:48 +0800336 error = EAI_BADHINTS;
337 break;
338 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900339 }
Ken Chen3270cf52018-11-07 01:20:48 +0800340 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900341 }
342 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900343
Ken Chen3270cf52018-11-07 01:20:48 +0800344 /*
Bernie Innocentib47552e2019-02-20 18:39:35 +0900345 * Check for special cases:
346 * (1) numeric servname is disallowed if socktype/protocol are left unspecified.
347 * (2) servname is disallowed for raw and other inet{,6} sockets.
Ken Chen3270cf52018-11-07 01:20:48 +0800348 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900349 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
Bernie Innocentib47552e2019-02-20 18:39:35 +0900350 struct addrinfo tmp = ai;
351 if (tmp.ai_family == PF_UNSPEC) {
352 tmp.ai_family = PF_INET6;
Ken Chen3270cf52018-11-07 01:20:48 +0800353 }
Bernie Innocentib47552e2019-02-20 18:39:35 +0900354 error = get_portmatch(&tmp, servname);
Ken Chen3270cf52018-11-07 01:20:48 +0800355 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900356 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900357
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900358 // NULL hostname, or numeric hostname
359 for (const Explore& ex : explore_options) {
Ken Chen3270cf52018-11-07 01:20:48 +0800360 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900361 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900362
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900363 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
364 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
365 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900366
Bernie Innocentib47552e2019-02-20 18:39:35 +0900367 struct addrinfo tmp = ai;
368 if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
369 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
370 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800371
Ken Chenbab50142019-03-19 17:41:28 +0800372 LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family
Bernie Innocentid017e972019-03-03 19:39:53 +0900373 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocentib47552e2019-02-20 18:39:35 +0900374 if (hostname == nullptr)
375 error = explore_null(&tmp, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800376 else
Bernie Innocentib47552e2019-02-20 18:39:35 +0900377 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800378
379 if (error) break;
380
381 while (cur->ai_next) cur = cur->ai_next;
382 }
383 if (error) break;
384
385 /*
386 * XXX
387 * If numeric representation of AF1 can be interpreted as FQDN
388 * representation of AF2, we need to think again about the code below.
389 */
390 if (sentinel.ai_next) break;
391
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900392 if (hostname == nullptr) {
Ken Chen3270cf52018-11-07 01:20:48 +0800393 error = EAI_NODATA;
394 break;
395 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900396 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chen3270cf52018-11-07 01:20:48 +0800397 error = EAI_NONAME;
398 break;
399 }
400
401 /*
402 * hostname as alphabetical name.
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900403 * We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
Ken Chen3270cf52018-11-07 01:20:48 +0800404 */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900405 for (const Explore& ex : explore_options) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900406 // Require exact match for family field
407 if (ai.ai_family != ex.e_af) continue;
Ken Chen3270cf52018-11-07 01:20:48 +0800408
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900409 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800410 continue;
411 }
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900412 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800413 continue;
414 }
415
Bernie Innocentib47552e2019-02-20 18:39:35 +0900416 struct addrinfo tmp = ai;
417 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
418 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800419
Ken Chenbab50142019-03-19 17:41:28 +0800420 LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family
Bernie Innocentid017e972019-03-03 19:39:53 +0900421 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Frank Li08ce7b82019-06-27 10:11:52 -0700422 error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext, event);
Ken Chen3270cf52018-11-07 01:20:48 +0800423
424 while (cur->ai_next) cur = cur->ai_next;
425 }
426
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900427 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800428 error = 0;
429 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900430 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800431 }
432 } while (0);
433
434 if (error) {
435 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti43b97d92019-03-05 15:45:03 +0900436 *res = nullptr;
Ken Chen3270cf52018-11-07 01:20:48 +0800437 } else {
438 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900439 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900441}
442
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900443// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900444static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
Frank Li08ce7b82019-06-27 10:11:52 -0700445 struct addrinfo** res, const struct android_net_context* netcontext,
446 NetworkDnsEventReported* event) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900447 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900448 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900449
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 assert(pai != NULL);
451 /* hostname may be NULL */
452 /* servname may be NULL */
453 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900454
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900455 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456
Bernie Innocenti948f6572018-09-12 21:32:42 +0900457 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900458 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900459
Bernie Innocenti948f6572018-09-12 21:32:42 +0900460 if (!files_getaddrinfo(hostname, pai, &result)) {
Frank Li08ce7b82019-06-27 10:11:52 -0700461 error = dns_getaddrinfo(hostname, pai, netcontext, &result, event);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900462 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900463 if (!error) {
464 struct addrinfo* cur;
465 for (cur = result; cur; cur = cur->ai_next) {
466 GET_PORT(cur, servname);
467 /* canonname should be filled already */
468 }
469 *res = result;
470 return 0;
471 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900472
473free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900474 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900476}
477
478/*
479 * hostname == NULL.
480 * passive socket -> anyaddr (0.0.0.0 or ::)
481 * non-passive socket -> localhost (127.0.0.1 or ::1)
482 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
484 int s;
485 const struct afd* afd;
486 struct addrinfo* cur;
487 struct addrinfo sentinel;
488 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Ken Chenbab50142019-03-19 17:41:28 +0800490 LOG(DEBUG) << __func__;
491
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492 assert(pai != NULL);
493 /* servname may be NULL */
494 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900495
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900496 *res = NULL;
497 sentinel.ai_next = NULL;
498 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900499
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 /*
501 * filter out AFs that are not supported by the kernel
502 * XXX errno?
503 */
504 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
505 if (s < 0) {
506 if (errno != EMFILE) return 0;
507 } else
508 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900509
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 /*
511 * if the servname does not match socktype/protocol, ignore it.
512 */
513 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900514
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900515 afd = find_afd(pai->ai_family);
516 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900517
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900518 if (pai->ai_flags & AI_PASSIVE) {
519 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900520 GET_PORT(cur->ai_next, servname);
521 } else {
522 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900523 GET_PORT(cur->ai_next, servname);
524 }
525 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900526
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900527 *res = sentinel.ai_next;
528 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900529
530free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900531 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900532 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900533}
534
535/*
536 * numeric hostname
537 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
539 struct addrinfo** res, const char* canonname) {
540 const struct afd* afd;
541 struct addrinfo* cur;
542 struct addrinfo sentinel;
543 int error;
544 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 assert(pai != NULL);
547 /* hostname may be NULL */
548 /* servname may be NULL */
549 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900550
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551 *res = NULL;
552 sentinel.ai_next = NULL;
553 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900555 /*
556 * if the servname does not match socktype/protocol, ignore it.
557 */
558 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900559
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900560 afd = find_afd(pai->ai_family);
561 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900562
Ken Chen15c805a2018-10-17 00:19:59 +0800563 if (inet_pton(afd->a_af, hostname, pton) == 1) {
564 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
565 GET_AI(cur->ai_next, afd, pton);
566 GET_PORT(cur->ai_next, servname);
567 if ((pai->ai_flags & AI_CANONNAME)) {
568 /*
569 * Set the numeric address itself as
570 * the canonical name, based on a
571 * clarification in rfc2553bis-03.
572 */
Ken Chen3270cf52018-11-07 01:20:48 +0800573 error = get_canonname(pai, cur->ai_next, canonname);
574 if (error != 0) {
575 freeaddrinfo(sentinel.ai_next);
576 return error;
577 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 }
Ken Chen15c805a2018-10-17 00:19:59 +0800579 while (cur->ai_next) cur = cur->ai_next;
580 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800581 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900582 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900583
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900584 *res = sentinel.ai_next;
585 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900586
587free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900588 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900589 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900590}
591
592/*
593 * numeric hostname with scope
594 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900595static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
596 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 const struct afd* afd;
598 struct addrinfo* cur;
599 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900600 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900602
Ken Chenbab50142019-03-19 17:41:28 +0800603 LOG(DEBUG) << __func__;
604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 assert(pai != NULL);
606 /* hostname may be NULL */
607 /* servname may be NULL */
608 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900609
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 /*
611 * if the servname does not match socktype/protocol, ignore it.
612 */
613 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 afd = find_afd(pai->ai_family);
616 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900617
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900620 cp = strchr(hostname, SCOPE_DELIMITER);
621 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900622
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 /*
624 * Handle special case of <scoped_address><delimiter><scope id>
625 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900626 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900627 if (hostname2 == NULL) return EAI_MEMORY;
628 /* terminate at the delimiter */
629 hostname2[cp - hostname] = '\0';
630 addr = hostname2;
631 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900632
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633 error = explore_numeric(pai, addr, servname, res, hostname);
634 if (error == 0) {
635 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900636
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900637 for (cur = *res; cur; cur = cur->ai_next) {
638 if (cur->ai_family != AF_INET6) continue;
639 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
640 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
641 free(hostname2);
642 return (EAI_NODATA); /* XXX: is return OK? */
643 }
644 sin6->sin6_scope_id = scopeid;
645 }
646 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900647
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900649
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900650 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900651}
652
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
654 assert(pai != NULL);
655 assert(ai != NULL);
656 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900658 if ((pai->ai_flags & AI_CANONNAME) != 0) {
659 ai->ai_canonname = strdup(str);
660 if (ai->ai_canonname == NULL) return EAI_MEMORY;
661 }
662 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900663}
664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900665static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
666 const char* addr) {
667 char* p;
668 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900669
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900670 assert(pai != NULL);
671 assert(afd != NULL);
672 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900673
nuccachene21023a2018-09-11 11:13:44 +0800674 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900675 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 memcpy(ai, pai, sizeof(struct addrinfo));
678 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800679 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900680
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900681 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900682 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
683 p = (char*) (void*) (ai->ai_addr);
684 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
685 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900686}
687
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900688static int get_portmatch(const struct addrinfo* ai, const char* servname) {
689 assert(ai != NULL);
690 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900693}
694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
696 const char* proto;
697 struct servent* sp;
698 int port;
699 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701 assert(ai != NULL);
702 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900704 if (servname == NULL) return 0;
705 switch (ai->ai_family) {
706 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900707 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708 break;
709 default:
710 return 0;
711 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 switch (ai->ai_socktype) {
714 case SOCK_RAW:
715 return EAI_SERVICE;
716 case SOCK_DGRAM:
717 case SOCK_STREAM:
718 allownumeric = 1;
719 break;
720 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900722 break;
723 default:
724 return EAI_SOCKTYPE;
725 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900727 port = str2number(servname);
728 if (port >= 0) {
729 if (!allownumeric) return EAI_SERVICE;
730 if (port < 0 || port > 65535) return EAI_SERVICE;
731 port = htons(port);
732 } else {
733 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 switch (ai->ai_socktype) {
736 case SOCK_DGRAM:
737 proto = "udp";
738 break;
739 case SOCK_STREAM:
740 proto = "tcp";
741 break;
742 default:
743 proto = NULL;
744 break;
745 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900746
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
748 port = sp->s_port;
749 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900750
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751 if (!matchonly) {
752 switch (ai->ai_family) {
753 case AF_INET:
754 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
755 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 case AF_INET6:
757 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
758 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900759 }
760 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900763}
764
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765static const struct afd* find_afd(int af) {
766 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 if (af == PF_UNSPEC) return NULL;
769 for (afd = afdl; afd->a_af; afd++) {
770 if (afd->a_af == af) return afd;
771 }
772 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900773}
774
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900775// Convert a string to a scope identifier.
776static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 u_long lscopeid;
778 struct in6_addr* a6;
779 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 assert(scope != NULL);
782 assert(sin6 != NULL);
783 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900784
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900786
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900787 /* empty scopeid portion is invalid */
788 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
791 /*
792 * We currently assume a one-to-one mapping between links
793 * and interfaces, so we simply use interface indices for
794 * like-local scopes.
795 */
796 *scopeid = if_nametoindex(scope);
797 if (*scopeid == 0) goto trynumeric;
798 return 0;
799 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 /* still unclear about literal, allow numeric only - placeholder */
802 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
803 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
804 goto trynumeric;
805 else
806 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900808 /* try to convert to a numeric id as a last resort */
809trynumeric:
810 errno = 0;
811 lscopeid = strtoul(scope, &ep, 10);
812 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
813 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
814 return 0;
815 else
816 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817}
Bernie Innocenti55864192018-08-30 04:05:20 +0900818
819/* code duplicate with gethnamaddr.c */
820
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900821#define BOUNDED_INCR(x) \
822 do { \
823 BOUNDS_CHECK(cp, x); \
824 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900825 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900826
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900827#define BOUNDS_CHECK(ptr, count) \
828 do { \
829 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800830 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 return NULL; \
832 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900833 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800836 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800837 struct addrinfo sentinel = {};
838 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 struct addrinfo ai;
840 const struct afd* afd;
841 char* canonname;
842 const HEADER* hp;
843 const u_char* cp;
844 int n;
845 const u_char* eom;
846 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900847 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848 int haveanswer, had_error;
849 char tbuf[MAXDNAME];
850 int (*name_ok)(const char*);
851 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 assert(answer != NULL);
854 assert(qname != NULL);
855 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900856
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900857 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900858
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900859 canonname = NULL;
860 eom = answer->buf + anslen;
861 switch (qtype) {
862 case T_A:
863 case T_AAAA:
864 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
865 name_ok = res_hnok;
866 break;
867 default:
868 return NULL; /* XXX should be abort(); */
869 }
870 /*
871 * find first satisfactory answer
872 */
873 hp = &answer->hdr;
874 ancount = ntohs(hp->ancount);
875 qdcount = ntohs(hp->qdcount);
876 bp = hostbuf;
877 ep = hostbuf + sizeof hostbuf;
878 cp = answer->buf;
879 BOUNDED_INCR(HFIXEDSZ);
880 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800881 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900882 return (NULL);
883 }
884 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
885 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800886 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900887 return (NULL);
888 }
889 BOUNDED_INCR(n + QFIXEDSZ);
890 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
891 /* res_send() has already verified that the query name is the
892 * same as the one we sent; this just gets the expanded name
893 * (i.e., with the succeeding search-domain tacked on).
894 */
895 n = strlen(bp) + 1; /* for the \0 */
896 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800897 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900898 return (NULL);
899 }
900 canonname = bp;
901 bp += n;
902 /* The qname can be abbreviated, but h_name is now absolute. */
903 qname = canonname;
904 }
905 haveanswer = 0;
906 had_error = 0;
907 while (ancount-- > 0 && cp < eom && !had_error) {
908 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
909 if ((n < 0) || !(*name_ok)(bp)) {
910 had_error++;
911 continue;
912 }
913 cp += n; /* name */
914 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce86a50bb2019-03-28 18:44:37 +0800915 type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900916 cp += INT16SZ; /* type */
chenbruce86a50bb2019-03-28 18:44:37 +0800917 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce86a50bb2019-03-28 18:44:37 +0800919 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900920 cp += INT16SZ; /* len */
921 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900922 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900923 /* XXX - debug? syslog? */
924 cp += n;
925 continue; /* XXX - had_error++ ? */
926 }
927 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
928 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
929 if ((n < 0) || !(*name_ok)(tbuf)) {
930 had_error++;
931 continue;
932 }
933 cp += n;
934 /* Get canonical name. */
935 n = strlen(tbuf) + 1; /* for the \0 */
936 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
937 had_error++;
938 continue;
939 }
940 strlcpy(bp, tbuf, (size_t)(ep - bp));
941 canonname = bp;
942 bp += n;
943 continue;
944 }
945 if (qtype == T_ANY) {
946 if (!(type == T_A || type == T_AAAA)) {
947 cp += n;
948 continue;
949 }
950 } else if (type != qtype) {
951 if (type != T_KEY && type != T_SIG)
Ken Chenbab50142019-03-19 17:41:28 +0800952 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
953 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900954 cp += n;
955 continue; /* XXX - had_error++ ? */
956 }
957 switch (type) {
958 case T_A:
959 case T_AAAA:
960 if (strcasecmp(canonname, bp) != 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800961 LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp
962 << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900963 cp += n;
964 continue; /* XXX - had_error++ ? */
965 }
966 if (type == T_A && n != INADDRSZ) {
967 cp += n;
968 continue;
969 }
970 if (type == T_AAAA && n != IN6ADDRSZ) {
971 cp += n;
972 continue;
973 }
974 if (type == T_AAAA) {
975 struct in6_addr in6;
976 memcpy(&in6, cp, IN6ADDRSZ);
977 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
978 cp += n;
979 continue;
980 }
981 }
982 if (!haveanswer) {
983 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900984
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 canonname = bp;
986 nn = strlen(bp) + 1; /* for the \0 */
987 bp += nn;
988 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900989
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900990 /* don't overwrite pai */
991 ai = *pai;
992 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
993 afd = find_afd(ai.ai_family);
994 if (afd == NULL) {
995 cp += n;
996 continue;
997 }
998 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
999 if (cur->ai_next == NULL) had_error++;
1000 while (cur && cur->ai_next) cur = cur->ai_next;
1001 cp += n;
1002 break;
1003 default:
1004 abort();
1005 }
1006 if (!had_error) haveanswer++;
1007 }
1008 if (haveanswer) {
1009 if (!canonname)
1010 (void) get_canonname(pai, sentinel.ai_next, qname);
1011 else
1012 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +08001013 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001014 return sentinel.ai_next;
1015 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001016
Hungming Chend57ade02018-12-25 15:47:47 +08001017 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001018 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001019}
1020
1021struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001022 struct addrinfo* ai;
1023 int has_src_addr;
1024 sockaddr_union src_addr;
1025 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001026};
1027
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001028static int _get_scope(const struct sockaddr* addr) {
1029 if (addr->sa_family == AF_INET6) {
1030 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1031 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1032 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1033 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1034 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1035 /*
1036 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1037 * link-local scope.
1038 */
1039 return IPV6_ADDR_SCOPE_LINKLOCAL;
1040 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1041 return IPV6_ADDR_SCOPE_SITELOCAL;
1042 } else {
1043 return IPV6_ADDR_SCOPE_GLOBAL;
1044 }
1045 } else if (addr->sa_family == AF_INET) {
1046 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1047 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001048
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001049 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1050 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1051 return IPV6_ADDR_SCOPE_LINKLOCAL;
1052 } else {
1053 /*
1054 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1055 * and shared addresses (100.64.0.0/10), are assigned global scope.
1056 */
1057 return IPV6_ADDR_SCOPE_GLOBAL;
1058 }
1059 } else {
1060 /*
1061 * This should never happen.
1062 * Return a scope with low priority as a last resort.
1063 */
1064 return IPV6_ADDR_SCOPE_NODELOCAL;
1065 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001066}
1067
1068/* These macros are modelled after the ones in <netinet/in6.h>. */
1069
1070/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001071#define IN6_IS_ADDR_TEREDO(a) \
1072 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001073
1074/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001075#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001076
1077/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001078#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001079
1080/*
1081 * Get the label for a given IPv4/IPv6 address.
1082 * RFC 6724, section 2.1.
1083 */
1084
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001085static int _get_label(const struct sockaddr* addr) {
1086 if (addr->sa_family == AF_INET) {
1087 return 4;
1088 } else if (addr->sa_family == AF_INET6) {
1089 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1090 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1091 return 0;
1092 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1093 return 4;
1094 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1095 return 2;
1096 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1097 return 5;
1098 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1099 return 13;
1100 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1101 return 3;
1102 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1103 return 11;
1104 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1105 return 12;
1106 } else {
1107 /* All other IPv6 addresses, including global unicast addresses. */
1108 return 1;
1109 }
1110 } else {
1111 /*
1112 * This should never happen.
1113 * Return a semi-random label as a last resort.
1114 */
1115 return 1;
1116 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001117}
1118
1119/*
1120 * Get the precedence for a given IPv4/IPv6 address.
1121 * RFC 6724, section 2.1.
1122 */
1123
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001124static int _get_precedence(const struct sockaddr* addr) {
1125 if (addr->sa_family == AF_INET) {
1126 return 35;
1127 } else if (addr->sa_family == AF_INET6) {
1128 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1129 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1130 return 50;
1131 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1132 return 35;
1133 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1134 return 30;
1135 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1136 return 5;
1137 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1138 return 3;
1139 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1140 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1141 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1142 return 1;
1143 } else {
1144 /* All other IPv6 addresses, including global unicast addresses. */
1145 return 40;
1146 }
1147 } else {
1148 return 1;
1149 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001150}
1151
1152/*
1153 * Find number of matching initial bits between the two addresses a1 and a2.
1154 */
1155
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001156static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1157 const char* p1 = (const char*) a1;
1158 const char* p2 = (const char*) a2;
1159 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001160
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001161 for (i = 0; i < sizeof(*a1); ++i) {
1162 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001163
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001164 if (p1[i] == p2[i]) {
1165 continue;
1166 }
1167 x = p1[i] ^ p2[i];
1168 for (j = 0; j < CHAR_BIT; ++j) {
1169 if (x & (1 << (CHAR_BIT - 1))) {
1170 return i * CHAR_BIT + j;
1171 }
1172 x <<= 1;
1173 }
1174 }
1175 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001176}
1177
1178/*
1179 * Compare two source/destination address pairs.
1180 * RFC 6724, section 6.
1181 */
1182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001183static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1184 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1185 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1186 int scope_src1, scope_dst1, scope_match1;
1187 int scope_src2, scope_dst2, scope_match2;
1188 int label_src1, label_dst1, label_match1;
1189 int label_src2, label_dst2, label_match2;
1190 int precedence1, precedence2;
1191 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001192
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001193 /* Rule 1: Avoid unusable destinations. */
1194 if (a1->has_src_addr != a2->has_src_addr) {
1195 return a2->has_src_addr - a1->has_src_addr;
1196 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001198 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001199 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001200 scope_dst1 = _get_scope(a1->ai->ai_addr);
1201 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001202
nuccachene172a4e2018-10-23 17:10:58 +08001203 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001204 scope_dst2 = _get_scope(a2->ai->ai_addr);
1205 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001206
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001207 if (scope_match1 != scope_match2) {
1208 return scope_match2 - scope_match1;
1209 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001210
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001211 /*
1212 * Rule 3: Avoid deprecated addresses.
1213 * TODO(sesse): We don't currently have a good way of finding this.
1214 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001215
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001216 /*
1217 * Rule 4: Prefer home addresses.
1218 * TODO(sesse): We don't currently have a good way of finding this.
1219 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001220
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001221 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001222 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001223 label_dst1 = _get_label(a1->ai->ai_addr);
1224 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001225
nuccachene172a4e2018-10-23 17:10:58 +08001226 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001227 label_dst2 = _get_label(a2->ai->ai_addr);
1228 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001229
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001230 if (label_match1 != label_match2) {
1231 return label_match2 - label_match1;
1232 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001234 /* Rule 6: Prefer higher precedence. */
1235 precedence1 = _get_precedence(a1->ai->ai_addr);
1236 precedence2 = _get_precedence(a2->ai->ai_addr);
1237 if (precedence1 != precedence2) {
1238 return precedence2 - precedence1;
1239 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001240
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001241 /*
1242 * Rule 7: Prefer native transport.
1243 * TODO(sesse): We don't currently have a good way of finding this.
1244 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001245
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001246 /* Rule 8: Prefer smaller scope. */
1247 if (scope_dst1 != scope_dst2) {
1248 return scope_dst1 - scope_dst2;
1249 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001250
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001251 /*
1252 * Rule 9: Use longest matching prefix.
1253 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1254 * to work very well directly applied to IPv4. (glibc uses information from
1255 * the routing table for a custom IPv4 implementation here.)
1256 */
1257 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1258 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001259 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001260 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001261 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001262 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1263 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1264 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1265 if (prefixlen1 != prefixlen2) {
1266 return prefixlen2 - prefixlen1;
1267 }
1268 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001269
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001270 /*
1271 * Rule 10: Leave the order unchanged.
1272 * We need this since qsort() is not necessarily stable.
1273 */
1274 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001275}
1276
1277/*
1278 * Find the source address that will be used if trying to connect to the given
1279 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1280 *
1281 * Returns 1 if a source address was found, 0 if the address is unreachable,
1282 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1283 * undefined.
1284 */
1285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001286static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1287 uid_t uid) {
1288 int sock;
1289 int ret;
1290 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001291
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001292 switch (addr->sa_family) {
1293 case AF_INET:
1294 len = sizeof(struct sockaddr_in);
1295 break;
1296 case AF_INET6:
1297 len = sizeof(struct sockaddr_in6);
1298 break;
1299 default:
1300 /* No known usable source address for non-INET families. */
1301 return 0;
1302 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001303
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001304 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1305 if (sock == -1) {
1306 if (errno == EAFNOSUPPORT) {
1307 return 0;
1308 } else {
1309 return -1;
1310 }
1311 }
1312 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1313 close(sock);
1314 return 0;
1315 }
1316 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1317 close(sock);
1318 return 0;
1319 }
1320 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001321 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001322 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001324 if (ret == -1) {
1325 close(sock);
1326 return 0;
1327 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001328
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001329 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1330 close(sock);
1331 return -1;
1332 }
1333 close(sock);
1334 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001335}
1336
1337/*
1338 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1339 * Will leave the list unchanged if an error occurs.
1340 */
1341
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001342static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1343 struct addrinfo* cur;
1344 int nelem = 0, i;
1345 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001346
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001347 cur = list_sentinel->ai_next;
1348 while (cur) {
1349 ++nelem;
1350 cur = cur->ai_next;
1351 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001352
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001353 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1354 if (elems == NULL) {
1355 goto error;
1356 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001357
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001358 /*
1359 * Convert the linked list to an array that also contains the candidate
1360 * source address for each destination address.
1361 */
1362 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1363 int has_src_addr;
1364 assert(cur != NULL);
1365 elems[i].ai = cur;
1366 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001367
nuccachene172a4e2018-10-23 17:10:58 +08001368 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001369 if (has_src_addr == -1) {
1370 goto error;
1371 }
1372 elems[i].has_src_addr = has_src_addr;
1373 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001374
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001375 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1376 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001377
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001378 list_sentinel->ai_next = elems[0].ai;
1379 for (i = 0; i < nelem - 1; ++i) {
1380 elems[i].ai->ai_next = elems[i + 1].ai;
1381 }
1382 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001383
1384error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001386}
1387
Bernie Innocenti948f6572018-09-12 21:32:42 +09001388static int dns_getaddrinfo(const char* name, const addrinfo* pai,
Frank Li08ce7b82019-06-27 10:11:52 -07001389 const android_net_context* netcontext, addrinfo** rv,
1390 NetworkDnsEventReported* event) {
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001391 res_target q = {};
1392 res_target q2 = {};
Bernie Innocenti55864192018-08-30 04:05:20 +09001393
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001394 auto buf = std::make_unique<querybuf>();
1395 auto buf2 = std::make_unique<querybuf>();
Bernie Innocenti55864192018-08-30 04:05:20 +09001396
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001397 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001398 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 /* prefer IPv6 */
1400 q.name = name;
1401 q.qclass = C_IN;
1402 q.answer = buf->buf;
1403 q.anslen = sizeof(buf->buf);
1404 int query_ipv6 = 1, query_ipv4 = 1;
1405 if (pai->ai_flags & AI_ADDRCONFIG) {
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001406 query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid);
1407 query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001408 }
1409 if (query_ipv6) {
1410 q.qtype = T_AAAA;
1411 if (query_ipv4) {
1412 q.next = &q2;
1413 q2.name = name;
1414 q2.qclass = C_IN;
1415 q2.qtype = T_A;
1416 q2.answer = buf2->buf;
1417 q2.anslen = sizeof(buf2->buf);
1418 }
1419 } else if (query_ipv4) {
1420 q.qtype = T_A;
1421 } else {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001422 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001423 }
1424 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001425 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001426 case AF_INET:
1427 q.name = name;
1428 q.qclass = C_IN;
1429 q.qtype = T_A;
1430 q.answer = buf->buf;
1431 q.anslen = sizeof(buf->buf);
1432 break;
1433 case AF_INET6:
1434 q.name = name;
1435 q.qclass = C_IN;
1436 q.qtype = T_AAAA;
1437 q.answer = buf->buf;
1438 q.anslen = sizeof(buf->buf);
1439 break;
1440 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001441 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001442 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001443
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001444 res_state res = res_get_state();
1445 if (!res) return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +09001446
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001447 /* this just sets our netid val in the thread private data so we don't have to
1448 * modify the api's all the way down to res_send.c's res_nsend. We could
1449 * fully populate the thread private data here, but if we get down there
1450 * and have a cache hit that would be wasted, so we do the rest there on miss
1451 */
Frank Li08ce7b82019-06-27 10:11:52 -07001452 res_setnetcontext(res, netcontext, event);
Mike Yu69615f62018-11-06 15:42:36 +08001453
Hungming Chen213ce272019-01-19 15:07:04 +08001454 int he;
1455 if (res_searchN(name, &q, res, &he) < 0) {
1456 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
1457 // Note that res_searchN() doesn't set the pair NETDB_INTERNAL and errno.
1458 // See also herrnoToAiErrno().
1459 return herrnoToAiErrno(he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001460 }
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001461
1462 addrinfo sentinel = {};
1463 addrinfo* cur = &sentinel;
Hungming Chen213ce272019-01-19 15:07:04 +08001464 addrinfo* ai = getanswer(buf.get(), q.n, q.name, q.qtype, pai, &he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001465 if (ai) {
1466 cur->ai_next = ai;
1467 while (cur && cur->ai_next) cur = cur->ai_next;
1468 }
1469 if (q.next) {
Hungming Chen213ce272019-01-19 15:07:04 +08001470 ai = getanswer(buf2.get(), q2.n, q2.name, q2.qtype, pai, &he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001471 if (ai) cur->ai_next = ai;
1472 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001473 if (sentinel.ai_next == NULL) {
Hungming Chen213ce272019-01-19 15:07:04 +08001474 // Note that getanswer() doesn't set the pair NETDB_INTERNAL and errno.
1475 // See also herrnoToAiErrno().
1476 return herrnoToAiErrno(he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001477 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001478
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001479 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001480
Bernie Innocenti948f6572018-09-12 21:32:42 +09001481 *rv = sentinel.ai_next;
1482 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001483}
1484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001485static void _sethtent(FILE** hostf) {
1486 if (!*hostf)
1487 *hostf = fopen(_PATH_HOSTS, "re");
1488 else
1489 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001490}
1491
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001492static void _endhtent(FILE** hostf) {
1493 if (*hostf) {
1494 (void) fclose(*hostf);
1495 *hostf = NULL;
1496 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001497}
1498
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001499static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1500 char* p;
1501 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001502 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001503 int error;
1504 const char* addr;
1505 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001506
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001507 assert(name != NULL);
1508 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001509
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001510 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1511again:
1512 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1513 if (*p == '#') goto again;
1514 if (!(cp = strpbrk(p, "#\n"))) goto again;
1515 *cp = '\0';
1516 if (!(cp = strpbrk(p, " \t"))) goto again;
1517 *cp++ = '\0';
1518 addr = p;
1519 /* if this is not something we're looking for, skip it. */
1520 cname = NULL;
1521 while (cp && *cp) {
1522 if (*cp == ' ' || *cp == '\t') {
1523 cp++;
1524 continue;
1525 }
1526 if (!cname) cname = cp;
1527 tname = cp;
1528 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001529 if (strcasecmp(name, tname) == 0) goto found;
1530 }
1531 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001532
1533found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001534 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001535 if (error) goto again;
1536 for (res = res0; res; res = res->ai_next) {
1537 /* cover it up */
1538 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001539
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001540 if (pai->ai_flags & AI_CANONNAME) {
1541 if (get_canonname(pai, res, cname) != 0) {
1542 freeaddrinfo(res0);
1543 goto again;
1544 }
1545 }
1546 }
1547 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001548}
1549
Bernie Innocenti948f6572018-09-12 21:32:42 +09001550static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001551 struct addrinfo sentinel = {};
1552 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001553 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001555 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001556
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001557 _sethtent(&hostf);
1558 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1559 cur->ai_next = p;
1560 while (cur && cur->ai_next) cur = cur->ai_next;
1561 }
1562 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001563
Bernie Innocenti948f6572018-09-12 21:32:42 +09001564 *res = sentinel.ai_next;
1565 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001566}
1567
1568/* resolver logic */
1569
1570/*
1571 * Formulate a normal query, send, and await answer.
1572 * Returned answer is placed in supplied buffer "answer".
1573 * Perform preliminary check of answer, returning success only
1574 * if no error is indicated and the answer count is nonzero.
1575 * Return the size of the response on success, -1 on error.
Hungming Chend57ade02018-12-25 15:47:47 +08001576 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001577 *
1578 * Caller must parse answer and determine whether it answers the question.
1579 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001580static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001581 u_char buf[MAXPACKET];
1582 HEADER* hp;
1583 int n;
1584 struct res_target* t;
1585 int rcode;
1586 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001587
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001588 assert(name != NULL);
1589 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001590
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001591 rcode = NOERROR;
1592 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001593
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001594 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001595 u_char* answer;
1596 int anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001597
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001598 hp = (HEADER*) (void*) t->answer;
Ken Chenbfd32022019-01-02 14:59:38 +08001599 bool retried = false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001600 again:
1601 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001602
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001603 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001604 int cl = t->qclass;
1605 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 answer = t->answer;
1607 anslen = t->anslen;
chenbruce16adee42019-02-20 19:45:50 +08001608
Ken Chenbab50142019-03-19 17:41:28 +08001609 LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
Bernie Innocenti55864192018-08-30 04:05:20 +09001610
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001611 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Ken Chenbfd32022019-01-02 14:59:38 +08001612 if (n > 0 && (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && !retried)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001613 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001614 if (n <= 0) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001615 LOG(ERROR) << __func__ << ": res_nmkquery failed";
Hungming Chend57ade02018-12-25 15:47:47 +08001616 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001617 return n;
1618 }
Mike Yu69615f62018-11-06 15:42:36 +08001619
Luke Huang952d0942018-12-26 16:53:03 +08001620 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001621 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001622 // Record rcode from DNS response header only if no timeout.
1623 // Keep rcode timeout for reporting later if any.
1624 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001625 /* if the query choked with EDNS0, retry without EDNS0 */
1626 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
Ken Chenbfd32022019-01-02 14:59:38 +08001627 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001628 LOG(DEBUG) << __func__ << ": retry without EDNS0";
Ken Chenbfd32022019-01-02 14:59:38 +08001629 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001630 goto again;
1631 }
Bernie Innocentid017e972019-03-03 19:39:53 +09001632 LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001633 continue;
1634 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001635
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001636 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001637
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001638 t->n = n;
1639 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001641 if (ancount == 0) {
1642 switch (rcode) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001643 // Not defined in RFC.
1644 case RCODE_TIMEOUT:
1645 // DNS metrics monitors DNS query timeout.
1646 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1647 break;
1648 // Defined in RFC 1035 section 4.1.1.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649 case NXDOMAIN:
Hungming Chend57ade02018-12-25 15:47:47 +08001650 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001651 break;
1652 case SERVFAIL:
Hungming Chend57ade02018-12-25 15:47:47 +08001653 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001654 break;
1655 case NOERROR:
Hungming Chend57ade02018-12-25 15:47:47 +08001656 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001657 break;
1658 case FORMERR:
1659 case NOTIMP:
1660 case REFUSED:
1661 default:
Hungming Chend57ade02018-12-25 15:47:47 +08001662 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001663 break;
1664 }
1665 return -1;
1666 }
1667 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001668}
1669
1670/*
1671 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1672 * Return the size of the response on success, -1 on error.
1673 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chend57ade02018-12-25 15:47:47 +08001674 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001675 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001676static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001677 const char *cp, *const *domain;
1678 HEADER* hp;
1679 u_int dots;
1680 int trailing_dot, ret, saved_herrno;
1681 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001683 assert(name != NULL);
1684 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001685
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001686 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001687
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001688 errno = 0;
Hungming Chend57ade02018-12-25 15:47:47 +08001689 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001690 dots = 0;
1691 for (cp = name; *cp; cp++) dots += (*cp == '.');
1692 trailing_dot = 0;
1693 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001695 /*
1696 * If there are dots in the name already, let's just give it a try
1697 * 'as is'. The threshold can be set with the "ndots" option.
1698 */
1699 saved_herrno = -1;
1700 if (dots >= res->ndots) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001701 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001702 if (ret > 0) return (ret);
Hungming Chend57ade02018-12-25 15:47:47 +08001703 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001704 tried_as_is++;
1705 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001706
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001707 /*
1708 * We do at least one level of search if
1709 * - there is no dot and RES_DEFNAME is set, or
1710 * - there is at least one dot, there is no trailing dot,
1711 * and RES_DNSRCH is set.
1712 */
1713 if ((!dots && (res->options & RES_DEFNAMES)) ||
1714 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1715 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001716
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001717 /* Unfortunately we need to set stuff up before
1718 * the domain stuff is tried. Will have a better
1719 * fix after thread pools are used.
1720 */
1721 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001724 ret = res_querydomainN(name, *domain, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001725 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001727 /*
1728 * If no server present, give up.
1729 * If name isn't found in this domain,
1730 * keep trying higher domains in the search list
1731 * (if that's enabled).
1732 * On a NO_DATA error, keep trying, otherwise
1733 * a wildcard entry of another type could keep us
1734 * from finding this entry higher in the domain.
1735 * If we get some other error (negative answer or
1736 * server failure), then stop searching up,
1737 * but try the input name below in case it's
1738 * fully-qualified.
1739 */
1740 if (errno == ECONNREFUSED) {
Hungming Chend57ade02018-12-25 15:47:47 +08001741 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001742 return -1;
1743 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001744
Hungming Chend57ade02018-12-25 15:47:47 +08001745 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001746 case NO_DATA:
1747 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001748 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001749 case HOST_NOT_FOUND:
1750 /* keep trying */
1751 break;
1752 case TRY_AGAIN:
1753 if (hp->rcode == SERVFAIL) {
1754 /* try next search element, if any */
1755 got_servfail++;
1756 break;
1757 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001758 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001759 default:
1760 /* anything else implies that we're done */
1761 done++;
1762 }
1763 /*
1764 * if we got here for some reason other than DNSRCH,
1765 * we only wanted one iteration of the loop, so stop.
1766 */
1767 if (!(res->options & RES_DNSRCH)) done++;
1768 }
1769 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001770
Bernie Innocentif12d5bb2018-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 Chen7f0d3292018-12-27 18:33:19 +08001777 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001778 if (ret > 0) return ret;
1779 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001780
Bernie Innocentif12d5bb2018-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 Chend57ade02018-12-25 15:47:47 +08001790 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001791 else if (got_nodata)
Hungming Chend57ade02018-12-25 15:47:47 +08001792 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001793 else if (got_servfail)
Hungming Chend57ade02018-12-25 15:47:47 +08001794 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001795 return -1;
Bernie Innocenti55864192018-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 Yu69615f62018-11-06 15:42:36 +08001802static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +08001803 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001804 char nbuf[MAXDNAME];
1805 const char* longname = nbuf;
1806 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001808 assert(name != NULL);
Bernie Innocentid017e972019-03-03 19:39:53 +09001809
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001810 if (domain == NULL) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001811 // Check for trailing '.'; copy without '.' if present.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001812 n = strlen(name);
1813 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001814 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-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 Chend57ade02018-12-25 15:47:47 +08001826 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001827 return -1;
1828 }
1829 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1830 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001831 return res_queryN(longname, target, res, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001832}