blob: b2429aa0648cc962e6c176f59204650468801209 [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 Innocenti55864192018-08-30 04:05:20 +090035#include <arpa/inet.h>
36#include <arpa/nameser.h>
37#include <assert.h>
38#include <ctype.h>
39#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090040#include <fcntl.h>
41#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090042#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090043#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090044#include <stdbool.h>
45#include <stddef.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090049#include <sys/param.h>
50#include <sys/socket.h>
51#include <sys/stat.h>
52#include <sys/types.h>
53#include <sys/un.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090054#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090055
chenbruce16adee42019-02-20 19:45:50 +080056#include <android-base/logging.h>
57
Bernie Innocenti189eb502018-10-01 23:10:18 +090058#include "netd_resolv/resolv.h"
59#include "resolv_cache.h"
60#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090061
Bernie Innocenti55864192018-08-30 04:05:20 +090062#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +090063
Bernie Innocenti93a31342018-12-12 00:43:02 +090064const char in_addrany[] = {0, 0, 0, 0};
65const char in_loopback[] = {127, 0, 0, 1};
66const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
67const 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 +090068
Bernie Innocenti93a31342018-12-12 00:43:02 +090069const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090070 int a_af;
71 int a_addrlen;
72 int a_socklen;
73 int a_off;
74 const char* a_addrany;
75 const char* a_loopback;
76 int a_scoped;
77} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
79 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090080 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
81 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
82 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +090083};
84
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090085struct Explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090086 int e_af;
87 int e_socktype;
88 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090089 int e_wild;
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090090#define WILD_AF(ex) ((ex).e_wild & 0x01)
91#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
92#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +090093};
94
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090095const Explore explore_options[] = {
Ken Chen3270cf52018-11-07 01:20:48 +080096 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
97 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
98 {PF_INET6, SOCK_RAW, ANY, 0x05},
99 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
100 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
101 {PF_INET, SOCK_RAW, ANY, 0x05},
102 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
103 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
104 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900105};
106
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900107#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900108#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900109
110typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 HEADER hdr;
112 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900113} querybuf;
114
115struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116 struct res_target* next;
117 const char* name; /* domain name */
118 int qclass, qtype; /* class and type of query */
119 u_char* answer; /* buffer to put answer */
120 int anslen; /* size of answer buffer */
121 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900122};
123
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124static int str2number(const char*);
125static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
126 const struct android_net_context*);
127static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
128static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
129 const char*);
130static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
131 struct addrinfo**);
132static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
133static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
134static int get_portmatch(const struct addrinfo*, const char*);
135static int get_port(const struct addrinfo*, const char*, int);
136static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900137static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900138
Hungming Chend57ade02018-12-25 15:47:47 +0800139static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
140 int* herrno);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900141static int dns_getaddrinfo(const char* name, const addrinfo* pai,
142 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900143static void _sethtent(FILE**);
144static void _endhtent(FILE**);
145static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900146static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900148
Hungming Chen7f0d3292018-12-27 18:33:19 +0800149static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
150static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800151static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800152 int* herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900153
Bernie Innocenti93a31342018-12-12 00:43:02 +0900154const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 "Success",
156 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
157 "Temporary failure in name resolution", /* EAI_AGAIN */
158 "Invalid value for ai_flags", /* EAI_BADFLAGS */
159 "Non-recoverable failure in name resolution", /* EAI_FAIL */
160 "ai_family not supported", /* EAI_FAMILY */
161 "Memory allocation failure", /* EAI_MEMORY */
162 "No address associated with hostname", /* EAI_NODATA */
163 "hostname nor servname provided, or not known", /* EAI_NONAME */
164 "servname not supported for ai_socktype", /* EAI_SERVICE */
165 "ai_socktype not supported", /* EAI_SOCKTYPE */
166 "System error returned in errno", /* EAI_SYSTEM */
167 "Invalid value for hints", /* EAI_BADHINTS */
168 "Resolved protocol is unknown", /* EAI_PROTOCOL */
169 "Argument buffer overflow", /* EAI_OVERFLOW */
170 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900171};
172
173/* XXX macros that make external reference is BAD. */
174
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900175#define GET_AI(ai, afd, addr) \
176 do { \
177 /* external reference: pai, error, and label free */ \
178 (ai) = get_ai(pai, (afd), (addr)); \
179 if ((ai) == NULL) { \
180 error = EAI_MEMORY; \
181 goto free; \
182 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900183 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900184
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185#define GET_PORT(ai, serv) \
186 do { \
187 /* external reference: error and label free */ \
188 error = get_port((ai), (serv), 0); \
189 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900190 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900193 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
194#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900195
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196const char* gai_strerror(int ecode) {
197 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
198 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900199}
200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900202 while (ai) {
203 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900205 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206 free(ai);
207 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900208 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900209}
210
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211static int str2number(const char* p) {
212 char* ep;
213 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217 if (*p == '\0') return -1;
218 ep = NULL;
219 errno = 0;
220 v = strtoul(p, &ep, 10);
221 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
222 return v;
223 else
224 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900225}
226
227/*
228 * The following functions determine whether IPv4 or IPv6 connectivity is
229 * available in order to implement AI_ADDRCONFIG.
230 *
231 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
232 * available, but whether addresses of the specified family are "configured
233 * on the local system". However, bionic doesn't currently support getifaddrs,
234 * so checking for connectivity is the next best thing.
235 */
Bernie Innocenti43b97d92019-03-05 15:45:03 +0900236static int have_ipv6(unsigned mark, uid_t uid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237 static const struct sockaddr_in6 sin6_test = {
238 .sin6_family = AF_INET6,
239 .sin6_addr.s6_addr = {// 2000::
240 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800241 sockaddr_union addr = {.sin6 = sin6_test};
242 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900243}
244
Bernie Innocenti43b97d92019-03-05 15:45:03 +0900245static int have_ipv4(unsigned mark, uid_t uid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900246 static const struct sockaddr_in sin_test = {
247 .sin_family = AF_INET,
248 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
249 };
nuccachene172a4e2018-10-23 17:10:58 +0800250 sockaddr_union addr = {.sin = sin_test};
251 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900252}
253
Bernie Innocentic165ce82018-10-16 23:35:28 +0900254// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
255// NOTE: also called by resolv_set_nameservers_for_net().
256int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
257 addrinfo** result) {
258 hints.ai_flags = AI_NUMERICHOST;
259 const android_net_context netcontext = {
260 .app_netid = NETID_UNSET,
261 .app_mark = MARK_UNSET,
262 .dns_netid = NETID_UNSET,
263 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900264 .uid = NET_CONTEXT_INVALID_UID,
265 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900266 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900267}
268
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900269int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
270 const struct addrinfo* hints,
271 const struct android_net_context* netcontext,
272 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800273 struct addrinfo sentinel = {};
Bernie Innocentib47552e2019-02-20 18:39:35 +0900274 struct addrinfo* cur = &sentinel;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900275 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900276
Bernie Innocentib47552e2019-02-20 18:39:35 +0900277 // hostname is allowed to be nullptr
278 // servname is allowed to be nullptr
279 // hints is allowed to be nullptr
280 assert(res != nullptr);
281 assert(netcontext != nullptr);
Bernie Innocenti2319a772019-02-20 17:50:38 +0900282
Bernie Innocentib47552e2019-02-20 18:39:35 +0900283 struct addrinfo ai = {
284 .ai_flags = 0,
285 .ai_family = PF_UNSPEC,
286 .ai_socktype = ANY,
287 .ai_protocol = ANY,
288 .ai_addrlen = 0,
289 .ai_canonname = nullptr,
290 .ai_addr = nullptr,
291 .ai_next = nullptr,
292 };
Bernie Innocenti2319a772019-02-20 17:50:38 +0900293
Ken Chen3270cf52018-11-07 01:20:48 +0800294 do {
295 if (hostname == NULL && servname == NULL) {
296 error = EAI_NONAME;
297 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900298 }
Ken Chen3270cf52018-11-07 01:20:48 +0800299 if (hints) {
300 /* error check for hints */
301 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
302 error = EAI_BADHINTS;
303 break;
304 }
305 if (hints->ai_flags & ~AI_MASK) {
306 error = EAI_BADFLAGS;
307 break;
308 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900309
Ken Chen3270cf52018-11-07 01:20:48 +0800310 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
311 hints->ai_family == PF_INET6)) {
312 error = EAI_FAMILY;
313 break;
314 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900315
316 ai = *hints;
Ken Chen3270cf52018-11-07 01:20:48 +0800317
318 /*
319 * if both socktype/protocol are specified, check if they
320 * are meaningful combination.
321 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900322 if (ai.ai_socktype != ANY && ai.ai_protocol != ANY) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900323 for (const Explore& ex : explore_options) {
324 if (ai.ai_family != ex.e_af) continue;
325 if (ex.e_socktype == ANY) continue;
326 if (ex.e_protocol == ANY) continue;
327 if (ai.ai_socktype == ex.e_socktype && ai.ai_protocol != ex.e_protocol) {
Ken Chen3270cf52018-11-07 01:20:48 +0800328 error = EAI_BADHINTS;
329 break;
330 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 }
Ken Chen3270cf52018-11-07 01:20:48 +0800332 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900333 }
334 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900335
Ken Chen3270cf52018-11-07 01:20:48 +0800336 /*
Bernie Innocentib47552e2019-02-20 18:39:35 +0900337 * Check for special cases:
338 * (1) numeric servname is disallowed if socktype/protocol are left unspecified.
339 * (2) servname is disallowed for raw and other inet{,6} sockets.
Ken Chen3270cf52018-11-07 01:20:48 +0800340 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900341 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
Bernie Innocentib47552e2019-02-20 18:39:35 +0900342 struct addrinfo tmp = ai;
343 if (tmp.ai_family == PF_UNSPEC) {
344 tmp.ai_family = PF_INET6;
Ken Chen3270cf52018-11-07 01:20:48 +0800345 }
Bernie Innocentib47552e2019-02-20 18:39:35 +0900346 error = get_portmatch(&tmp, servname);
Ken Chen3270cf52018-11-07 01:20:48 +0800347 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900348 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900349
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900350 // NULL hostname, or numeric hostname
351 for (const Explore& ex : explore_options) {
Ken Chen3270cf52018-11-07 01:20:48 +0800352 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900353 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900354
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900355 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
356 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
357 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900358
Bernie Innocentib47552e2019-02-20 18:39:35 +0900359 struct addrinfo tmp = ai;
360 if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
361 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
362 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800363
Ken Chenbab50142019-03-19 17:41:28 +0800364 LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family
Bernie Innocentid017e972019-03-03 19:39:53 +0900365 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocentib47552e2019-02-20 18:39:35 +0900366 if (hostname == nullptr)
367 error = explore_null(&tmp, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800368 else
Bernie Innocentib47552e2019-02-20 18:39:35 +0900369 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800370
371 if (error) break;
372
373 while (cur->ai_next) cur = cur->ai_next;
374 }
375 if (error) break;
376
377 /*
378 * XXX
379 * If numeric representation of AF1 can be interpreted as FQDN
380 * representation of AF2, we need to think again about the code below.
381 */
382 if (sentinel.ai_next) break;
383
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900384 if (hostname == nullptr) {
Ken Chen3270cf52018-11-07 01:20:48 +0800385 error = EAI_NODATA;
386 break;
387 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900388 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chen3270cf52018-11-07 01:20:48 +0800389 error = EAI_NONAME;
390 break;
391 }
392
393 /*
394 * hostname as alphabetical name.
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900395 * 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 +0800396 */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900397 for (const Explore& ex : explore_options) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900398 // Require exact match for family field
399 if (ai.ai_family != ex.e_af) continue;
Ken Chen3270cf52018-11-07 01:20:48 +0800400
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900401 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800402 continue;
403 }
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900404 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800405 continue;
406 }
407
Bernie Innocentib47552e2019-02-20 18:39:35 +0900408 struct addrinfo tmp = ai;
409 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
410 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800411
Ken Chenbab50142019-03-19 17:41:28 +0800412 LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family
Bernie Innocentid017e972019-03-03 19:39:53 +0900413 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocentib47552e2019-02-20 18:39:35 +0900414 error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext);
Ken Chen3270cf52018-11-07 01:20:48 +0800415
416 while (cur->ai_next) cur = cur->ai_next;
417 }
418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800420 error = 0;
421 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900422 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800423 }
424 } while (0);
425
426 if (error) {
427 freeaddrinfo(sentinel.ai_next);
Bernie Innocenti43b97d92019-03-05 15:45:03 +0900428 *res = nullptr;
Ken Chen3270cf52018-11-07 01:20:48 +0800429 } else {
430 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900431 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900433}
434
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900435// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900436static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
437 struct addrinfo** res, const struct android_net_context* netcontext) {
438 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900439 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900440
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 assert(pai != NULL);
442 /* hostname may be NULL */
443 /* servname may be NULL */
444 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900445
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900446 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900447
Bernie Innocenti948f6572018-09-12 21:32:42 +0900448 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900450
Bernie Innocenti948f6572018-09-12 21:32:42 +0900451 if (!files_getaddrinfo(hostname, pai, &result)) {
452 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900454 if (!error) {
455 struct addrinfo* cur;
456 for (cur = result; cur; cur = cur->ai_next) {
457 GET_PORT(cur, servname);
458 /* canonname should be filled already */
459 }
460 *res = result;
461 return 0;
462 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900463
464free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900465 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900467}
468
469/*
470 * hostname == NULL.
471 * passive socket -> anyaddr (0.0.0.0 or ::)
472 * non-passive socket -> localhost (127.0.0.1 or ::1)
473 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900474static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
475 int s;
476 const struct afd* afd;
477 struct addrinfo* cur;
478 struct addrinfo sentinel;
479 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900480
Ken Chenbab50142019-03-19 17:41:28 +0800481 LOG(DEBUG) << __func__;
482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 assert(pai != NULL);
484 /* servname may be NULL */
485 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900486
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900487 *res = NULL;
488 sentinel.ai_next = NULL;
489 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900490
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900491 /*
492 * filter out AFs that are not supported by the kernel
493 * XXX errno?
494 */
495 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
496 if (s < 0) {
497 if (errno != EMFILE) return 0;
498 } else
499 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900500
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900501 /*
502 * if the servname does not match socktype/protocol, ignore it.
503 */
504 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900505
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506 afd = find_afd(pai->ai_family);
507 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900508
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509 if (pai->ai_flags & AI_PASSIVE) {
510 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 GET_PORT(cur->ai_next, servname);
512 } else {
513 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 GET_PORT(cur->ai_next, servname);
515 }
516 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900517
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900518 *res = sentinel.ai_next;
519 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900520
521free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900522 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900523 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900524}
525
526/*
527 * numeric hostname
528 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900529static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
530 struct addrinfo** res, const char* canonname) {
531 const struct afd* afd;
532 struct addrinfo* cur;
533 struct addrinfo sentinel;
534 int error;
535 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900536
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900537 assert(pai != NULL);
538 /* hostname may be NULL */
539 /* servname may be NULL */
540 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900541
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900542 *res = NULL;
543 sentinel.ai_next = NULL;
544 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 /*
547 * if the servname does not match socktype/protocol, ignore it.
548 */
549 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900550
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551 afd = find_afd(pai->ai_family);
552 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900553
Ken Chen15c805a2018-10-17 00:19:59 +0800554 if (inet_pton(afd->a_af, hostname, pton) == 1) {
555 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
556 GET_AI(cur->ai_next, afd, pton);
557 GET_PORT(cur->ai_next, servname);
558 if ((pai->ai_flags & AI_CANONNAME)) {
559 /*
560 * Set the numeric address itself as
561 * the canonical name, based on a
562 * clarification in rfc2553bis-03.
563 */
Ken Chen3270cf52018-11-07 01:20:48 +0800564 error = get_canonname(pai, cur->ai_next, canonname);
565 if (error != 0) {
566 freeaddrinfo(sentinel.ai_next);
567 return error;
568 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900569 }
Ken Chen15c805a2018-10-17 00:19:59 +0800570 while (cur->ai_next) cur = cur->ai_next;
571 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800572 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900574
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900575 *res = sentinel.ai_next;
576 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900577
578free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900579 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900580 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900581}
582
583/*
584 * numeric hostname with scope
585 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900586static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
587 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900588 const struct afd* afd;
589 struct addrinfo* cur;
590 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900591 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900592 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900593
Ken Chenbab50142019-03-19 17:41:28 +0800594 LOG(DEBUG) << __func__;
595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 assert(pai != NULL);
597 /* hostname may be NULL */
598 /* servname may be NULL */
599 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 /*
602 * if the servname does not match socktype/protocol, ignore it.
603 */
604 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 afd = find_afd(pai->ai_family);
607 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900609 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900611 cp = strchr(hostname, SCOPE_DELIMITER);
612 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900613
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900614 /*
615 * Handle special case of <scoped_address><delimiter><scope id>
616 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900617 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618 if (hostname2 == NULL) return EAI_MEMORY;
619 /* terminate at the delimiter */
620 hostname2[cp - hostname] = '\0';
621 addr = hostname2;
622 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900623
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624 error = explore_numeric(pai, addr, servname, res, hostname);
625 if (error == 0) {
626 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628 for (cur = *res; cur; cur = cur->ai_next) {
629 if (cur->ai_family != AF_INET6) continue;
630 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
631 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
632 free(hostname2);
633 return (EAI_NODATA); /* XXX: is return OK? */
634 }
635 sin6->sin6_scope_id = scopeid;
636 }
637 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900642}
643
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900644static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
645 assert(pai != NULL);
646 assert(ai != NULL);
647 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 if ((pai->ai_flags & AI_CANONNAME) != 0) {
650 ai->ai_canonname = strdup(str);
651 if (ai->ai_canonname == NULL) return EAI_MEMORY;
652 }
653 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900654}
655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
657 const char* addr) {
658 char* p;
659 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661 assert(pai != NULL);
662 assert(afd != NULL);
663 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900664
nuccachene21023a2018-09-11 11:13:44 +0800665 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900666 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900667
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900668 memcpy(ai, pai, sizeof(struct addrinfo));
669 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800670 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900671
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900672 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
674 p = (char*) (void*) (ai->ai_addr);
675 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
676 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900677}
678
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679static int get_portmatch(const struct addrinfo* ai, const char* servname) {
680 assert(ai != NULL);
681 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900684}
685
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900686static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
687 const char* proto;
688 struct servent* sp;
689 int port;
690 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 assert(ai != NULL);
693 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695 if (servname == NULL) return 0;
696 switch (ai->ai_family) {
697 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900699 break;
700 default:
701 return 0;
702 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900704 switch (ai->ai_socktype) {
705 case SOCK_RAW:
706 return EAI_SERVICE;
707 case SOCK_DGRAM:
708 case SOCK_STREAM:
709 allownumeric = 1;
710 break;
711 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900712 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 break;
714 default:
715 return EAI_SOCKTYPE;
716 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900718 port = str2number(servname);
719 if (port >= 0) {
720 if (!allownumeric) return EAI_SERVICE;
721 if (port < 0 || port > 65535) return EAI_SERVICE;
722 port = htons(port);
723 } else {
724 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900725
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900726 switch (ai->ai_socktype) {
727 case SOCK_DGRAM:
728 proto = "udp";
729 break;
730 case SOCK_STREAM:
731 proto = "tcp";
732 break;
733 default:
734 proto = NULL;
735 break;
736 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
739 port = sp->s_port;
740 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 if (!matchonly) {
743 switch (ai->ai_family) {
744 case AF_INET:
745 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
746 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 case AF_INET6:
748 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
749 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 }
751 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900754}
755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756static const struct afd* find_afd(int af) {
757 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900758
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900759 if (af == PF_UNSPEC) return NULL;
760 for (afd = afdl; afd->a_af; afd++) {
761 if (afd->a_af == af) return afd;
762 }
763 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900764}
765
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900766// Convert a string to a scope identifier.
767static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 u_long lscopeid;
769 struct in6_addr* a6;
770 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900772 assert(scope != NULL);
773 assert(sin6 != NULL);
774 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900778 /* empty scopeid portion is invalid */
779 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
782 /*
783 * We currently assume a one-to-one mapping between links
784 * and interfaces, so we simply use interface indices for
785 * like-local scopes.
786 */
787 *scopeid = if_nametoindex(scope);
788 if (*scopeid == 0) goto trynumeric;
789 return 0;
790 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900791
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900792 /* still unclear about literal, allow numeric only - placeholder */
793 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
794 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
795 goto trynumeric;
796 else
797 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900798
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 /* try to convert to a numeric id as a last resort */
800trynumeric:
801 errno = 0;
802 lscopeid = strtoul(scope, &ep, 10);
803 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
804 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
805 return 0;
806 else
807 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900808}
Bernie Innocenti55864192018-08-30 04:05:20 +0900809
810/* code duplicate with gethnamaddr.c */
811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812#define BOUNDED_INCR(x) \
813 do { \
814 BOUNDS_CHECK(cp, x); \
815 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900816 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900818#define BOUNDS_CHECK(ptr, count) \
819 do { \
820 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800821 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822 return NULL; \
823 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900824 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900825
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900826static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800827 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800828 struct addrinfo sentinel = {};
829 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900830 struct addrinfo ai;
831 const struct afd* afd;
832 char* canonname;
833 const HEADER* hp;
834 const u_char* cp;
835 int n;
836 const u_char* eom;
837 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900838 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 int haveanswer, had_error;
840 char tbuf[MAXDNAME];
841 int (*name_ok)(const char*);
842 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900843
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900844 assert(answer != NULL);
845 assert(qname != NULL);
846 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900847
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900849
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900850 canonname = NULL;
851 eom = answer->buf + anslen;
852 switch (qtype) {
853 case T_A:
854 case T_AAAA:
855 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
856 name_ok = res_hnok;
857 break;
858 default:
859 return NULL; /* XXX should be abort(); */
860 }
861 /*
862 * find first satisfactory answer
863 */
864 hp = &answer->hdr;
865 ancount = ntohs(hp->ancount);
866 qdcount = ntohs(hp->qdcount);
867 bp = hostbuf;
868 ep = hostbuf + sizeof hostbuf;
869 cp = answer->buf;
870 BOUNDED_INCR(HFIXEDSZ);
871 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800872 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873 return (NULL);
874 }
875 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
876 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800877 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900878 return (NULL);
879 }
880 BOUNDED_INCR(n + QFIXEDSZ);
881 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
882 /* res_send() has already verified that the query name is the
883 * same as the one we sent; this just gets the expanded name
884 * (i.e., with the succeeding search-domain tacked on).
885 */
886 n = strlen(bp) + 1; /* for the \0 */
887 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800888 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900889 return (NULL);
890 }
891 canonname = bp;
892 bp += n;
893 /* The qname can be abbreviated, but h_name is now absolute. */
894 qname = canonname;
895 }
896 haveanswer = 0;
897 had_error = 0;
898 while (ancount-- > 0 && cp < eom && !had_error) {
899 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
900 if ((n < 0) || !(*name_ok)(bp)) {
901 had_error++;
902 continue;
903 }
904 cp += n; /* name */
905 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900906 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900907 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900908 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900909 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900910 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900911 cp += INT16SZ; /* len */
912 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900913 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 /* XXX - debug? syslog? */
915 cp += n;
916 continue; /* XXX - had_error++ ? */
917 }
918 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
919 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
920 if ((n < 0) || !(*name_ok)(tbuf)) {
921 had_error++;
922 continue;
923 }
924 cp += n;
925 /* Get canonical name. */
926 n = strlen(tbuf) + 1; /* for the \0 */
927 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
928 had_error++;
929 continue;
930 }
931 strlcpy(bp, tbuf, (size_t)(ep - bp));
932 canonname = bp;
933 bp += n;
934 continue;
935 }
936 if (qtype == T_ANY) {
937 if (!(type == T_A || type == T_AAAA)) {
938 cp += n;
939 continue;
940 }
941 } else if (type != qtype) {
942 if (type != T_KEY && type != T_SIG)
Ken Chenbab50142019-03-19 17:41:28 +0800943 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
944 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900945 cp += n;
946 continue; /* XXX - had_error++ ? */
947 }
948 switch (type) {
949 case T_A:
950 case T_AAAA:
951 if (strcasecmp(canonname, bp) != 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800952 LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp
953 << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900954 cp += n;
955 continue; /* XXX - had_error++ ? */
956 }
957 if (type == T_A && n != INADDRSZ) {
958 cp += n;
959 continue;
960 }
961 if (type == T_AAAA && n != IN6ADDRSZ) {
962 cp += n;
963 continue;
964 }
965 if (type == T_AAAA) {
966 struct in6_addr in6;
967 memcpy(&in6, cp, IN6ADDRSZ);
968 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
969 cp += n;
970 continue;
971 }
972 }
973 if (!haveanswer) {
974 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900975
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900976 canonname = bp;
977 nn = strlen(bp) + 1; /* for the \0 */
978 bp += nn;
979 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900980
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900981 /* don't overwrite pai */
982 ai = *pai;
983 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
984 afd = find_afd(ai.ai_family);
985 if (afd == NULL) {
986 cp += n;
987 continue;
988 }
989 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
990 if (cur->ai_next == NULL) had_error++;
991 while (cur && cur->ai_next) cur = cur->ai_next;
992 cp += n;
993 break;
994 default:
995 abort();
996 }
997 if (!had_error) haveanswer++;
998 }
999 if (haveanswer) {
1000 if (!canonname)
1001 (void) get_canonname(pai, sentinel.ai_next, qname);
1002 else
1003 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +08001004 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001005 return sentinel.ai_next;
1006 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001007
Hungming Chend57ade02018-12-25 15:47:47 +08001008 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001009 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001010}
1011
1012struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001013 struct addrinfo* ai;
1014 int has_src_addr;
1015 sockaddr_union src_addr;
1016 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001017};
1018
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001019static int _get_scope(const struct sockaddr* addr) {
1020 if (addr->sa_family == AF_INET6) {
1021 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1022 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1023 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1024 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1025 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1026 /*
1027 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1028 * link-local scope.
1029 */
1030 return IPV6_ADDR_SCOPE_LINKLOCAL;
1031 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1032 return IPV6_ADDR_SCOPE_SITELOCAL;
1033 } else {
1034 return IPV6_ADDR_SCOPE_GLOBAL;
1035 }
1036 } else if (addr->sa_family == AF_INET) {
1037 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1038 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001039
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001040 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1041 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1042 return IPV6_ADDR_SCOPE_LINKLOCAL;
1043 } else {
1044 /*
1045 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1046 * and shared addresses (100.64.0.0/10), are assigned global scope.
1047 */
1048 return IPV6_ADDR_SCOPE_GLOBAL;
1049 }
1050 } else {
1051 /*
1052 * This should never happen.
1053 * Return a scope with low priority as a last resort.
1054 */
1055 return IPV6_ADDR_SCOPE_NODELOCAL;
1056 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001057}
1058
1059/* These macros are modelled after the ones in <netinet/in6.h>. */
1060
1061/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001062#define IN6_IS_ADDR_TEREDO(a) \
1063 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001064
1065/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001066#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001067
1068/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001069#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001070
1071/*
1072 * Get the label for a given IPv4/IPv6 address.
1073 * RFC 6724, section 2.1.
1074 */
1075
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001076static int _get_label(const struct sockaddr* addr) {
1077 if (addr->sa_family == AF_INET) {
1078 return 4;
1079 } else if (addr->sa_family == AF_INET6) {
1080 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1081 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1082 return 0;
1083 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1084 return 4;
1085 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1086 return 2;
1087 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1088 return 5;
1089 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1090 return 13;
1091 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1092 return 3;
1093 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1094 return 11;
1095 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1096 return 12;
1097 } else {
1098 /* All other IPv6 addresses, including global unicast addresses. */
1099 return 1;
1100 }
1101 } else {
1102 /*
1103 * This should never happen.
1104 * Return a semi-random label as a last resort.
1105 */
1106 return 1;
1107 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001108}
1109
1110/*
1111 * Get the precedence for a given IPv4/IPv6 address.
1112 * RFC 6724, section 2.1.
1113 */
1114
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001115static int _get_precedence(const struct sockaddr* addr) {
1116 if (addr->sa_family == AF_INET) {
1117 return 35;
1118 } else if (addr->sa_family == AF_INET6) {
1119 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1120 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1121 return 50;
1122 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1123 return 35;
1124 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1125 return 30;
1126 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1127 return 5;
1128 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1129 return 3;
1130 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1131 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1132 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1133 return 1;
1134 } else {
1135 /* All other IPv6 addresses, including global unicast addresses. */
1136 return 40;
1137 }
1138 } else {
1139 return 1;
1140 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001141}
1142
1143/*
1144 * Find number of matching initial bits between the two addresses a1 and a2.
1145 */
1146
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001147static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1148 const char* p1 = (const char*) a1;
1149 const char* p2 = (const char*) a2;
1150 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001151
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001152 for (i = 0; i < sizeof(*a1); ++i) {
1153 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001154
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001155 if (p1[i] == p2[i]) {
1156 continue;
1157 }
1158 x = p1[i] ^ p2[i];
1159 for (j = 0; j < CHAR_BIT; ++j) {
1160 if (x & (1 << (CHAR_BIT - 1))) {
1161 return i * CHAR_BIT + j;
1162 }
1163 x <<= 1;
1164 }
1165 }
1166 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001167}
1168
1169/*
1170 * Compare two source/destination address pairs.
1171 * RFC 6724, section 6.
1172 */
1173
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001174static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1175 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1176 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1177 int scope_src1, scope_dst1, scope_match1;
1178 int scope_src2, scope_dst2, scope_match2;
1179 int label_src1, label_dst1, label_match1;
1180 int label_src2, label_dst2, label_match2;
1181 int precedence1, precedence2;
1182 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001183
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001184 /* Rule 1: Avoid unusable destinations. */
1185 if (a1->has_src_addr != a2->has_src_addr) {
1186 return a2->has_src_addr - a1->has_src_addr;
1187 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001189 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001190 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001191 scope_dst1 = _get_scope(a1->ai->ai_addr);
1192 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001193
nuccachene172a4e2018-10-23 17:10:58 +08001194 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001195 scope_dst2 = _get_scope(a2->ai->ai_addr);
1196 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001198 if (scope_match1 != scope_match2) {
1199 return scope_match2 - scope_match1;
1200 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001201
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001202 /*
1203 * Rule 3: Avoid deprecated addresses.
1204 * TODO(sesse): We don't currently have a good way of finding this.
1205 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001206
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001207 /*
1208 * Rule 4: Prefer home addresses.
1209 * TODO(sesse): We don't currently have a good way of finding this.
1210 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001212 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001213 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001214 label_dst1 = _get_label(a1->ai->ai_addr);
1215 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001216
nuccachene172a4e2018-10-23 17:10:58 +08001217 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001218 label_dst2 = _get_label(a2->ai->ai_addr);
1219 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001220
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001221 if (label_match1 != label_match2) {
1222 return label_match2 - label_match1;
1223 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001224
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001225 /* Rule 6: Prefer higher precedence. */
1226 precedence1 = _get_precedence(a1->ai->ai_addr);
1227 precedence2 = _get_precedence(a2->ai->ai_addr);
1228 if (precedence1 != precedence2) {
1229 return precedence2 - precedence1;
1230 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001232 /*
1233 * Rule 7: Prefer native transport.
1234 * TODO(sesse): We don't currently have a good way of finding this.
1235 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001236
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001237 /* Rule 8: Prefer smaller scope. */
1238 if (scope_dst1 != scope_dst2) {
1239 return scope_dst1 - scope_dst2;
1240 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001241
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001242 /*
1243 * Rule 9: Use longest matching prefix.
1244 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1245 * to work very well directly applied to IPv4. (glibc uses information from
1246 * the routing table for a custom IPv4 implementation here.)
1247 */
1248 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1249 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001250 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001251 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001252 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001253 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1254 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1255 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1256 if (prefixlen1 != prefixlen2) {
1257 return prefixlen2 - prefixlen1;
1258 }
1259 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001260
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001261 /*
1262 * Rule 10: Leave the order unchanged.
1263 * We need this since qsort() is not necessarily stable.
1264 */
1265 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001266}
1267
1268/*
1269 * Find the source address that will be used if trying to connect to the given
1270 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1271 *
1272 * Returns 1 if a source address was found, 0 if the address is unreachable,
1273 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1274 * undefined.
1275 */
1276
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001277static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1278 uid_t uid) {
1279 int sock;
1280 int ret;
1281 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001282
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001283 switch (addr->sa_family) {
1284 case AF_INET:
1285 len = sizeof(struct sockaddr_in);
1286 break;
1287 case AF_INET6:
1288 len = sizeof(struct sockaddr_in6);
1289 break;
1290 default:
1291 /* No known usable source address for non-INET families. */
1292 return 0;
1293 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001294
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001295 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1296 if (sock == -1) {
1297 if (errno == EAFNOSUPPORT) {
1298 return 0;
1299 } else {
1300 return -1;
1301 }
1302 }
1303 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1304 close(sock);
1305 return 0;
1306 }
1307 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1308 close(sock);
1309 return 0;
1310 }
1311 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001312 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001313 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001314
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001315 if (ret == -1) {
1316 close(sock);
1317 return 0;
1318 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001319
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001320 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1321 close(sock);
1322 return -1;
1323 }
1324 close(sock);
1325 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001326}
1327
1328/*
1329 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1330 * Will leave the list unchanged if an error occurs.
1331 */
1332
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001333static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1334 struct addrinfo* cur;
1335 int nelem = 0, i;
1336 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001337
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001338 cur = list_sentinel->ai_next;
1339 while (cur) {
1340 ++nelem;
1341 cur = cur->ai_next;
1342 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001343
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001344 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1345 if (elems == NULL) {
1346 goto error;
1347 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001348
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001349 /*
1350 * Convert the linked list to an array that also contains the candidate
1351 * source address for each destination address.
1352 */
1353 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1354 int has_src_addr;
1355 assert(cur != NULL);
1356 elems[i].ai = cur;
1357 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001358
nuccachene172a4e2018-10-23 17:10:58 +08001359 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001360 if (has_src_addr == -1) {
1361 goto error;
1362 }
1363 elems[i].has_src_addr = has_src_addr;
1364 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001365
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001366 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1367 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001368
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001369 list_sentinel->ai_next = elems[0].ai;
1370 for (i = 0; i < nelem - 1; ++i) {
1371 elems[i].ai->ai_next = elems[i + 1].ai;
1372 }
1373 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001374
1375error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001376 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001377}
1378
Bernie Innocenti948f6572018-09-12 21:32:42 +09001379static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1380 const android_net_context* netcontext, addrinfo** rv) {
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001381 res_target q = {};
1382 res_target q2 = {};
Bernie Innocenti55864192018-08-30 04:05:20 +09001383
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001384 auto buf = std::make_unique<querybuf>();
1385 auto buf2 = std::make_unique<querybuf>();
Bernie Innocenti55864192018-08-30 04:05:20 +09001386
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001387 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001388 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001389 /* prefer IPv6 */
1390 q.name = name;
1391 q.qclass = C_IN;
1392 q.answer = buf->buf;
1393 q.anslen = sizeof(buf->buf);
1394 int query_ipv6 = 1, query_ipv4 = 1;
1395 if (pai->ai_flags & AI_ADDRCONFIG) {
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001396 query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid);
1397 query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001398 }
1399 if (query_ipv6) {
1400 q.qtype = T_AAAA;
1401 if (query_ipv4) {
1402 q.next = &q2;
1403 q2.name = name;
1404 q2.qclass = C_IN;
1405 q2.qtype = T_A;
1406 q2.answer = buf2->buf;
1407 q2.anslen = sizeof(buf2->buf);
1408 }
1409 } else if (query_ipv4) {
1410 q.qtype = T_A;
1411 } else {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001412 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001413 }
1414 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001415 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001416 case AF_INET:
1417 q.name = name;
1418 q.qclass = C_IN;
1419 q.qtype = T_A;
1420 q.answer = buf->buf;
1421 q.anslen = sizeof(buf->buf);
1422 break;
1423 case AF_INET6:
1424 q.name = name;
1425 q.qclass = C_IN;
1426 q.qtype = T_AAAA;
1427 q.answer = buf->buf;
1428 q.anslen = sizeof(buf->buf);
1429 break;
1430 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001431 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001432 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001433
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001434 res_state res = res_get_state();
1435 if (!res) return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +09001436
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001437 /* this just sets our netid val in the thread private data so we don't have to
1438 * modify the api's all the way down to res_send.c's res_nsend. We could
1439 * fully populate the thread private data here, but if we get down there
1440 * and have a cache hit that would be wasted, so we do the rest there on miss
1441 */
1442 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001443
Hungming Chend57ade02018-12-25 15:47:47 +08001444 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +08001445 if (res_searchN(name, &q, res, &herrno) < 0) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001446 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
1447 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001448 }
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001449
1450 addrinfo sentinel = {};
1451 addrinfo* cur = &sentinel;
1452 addrinfo* ai = getanswer(buf.get(), q.n, q.name, q.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001453 if (ai) {
1454 cur->ai_next = ai;
1455 while (cur && cur->ai_next) cur = cur->ai_next;
1456 }
1457 if (q.next) {
Bernie Innocenti43b97d92019-03-05 15:45:03 +09001458 ai = getanswer(buf2.get(), q2.n, q2.name, q2.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001459 if (ai) cur->ai_next = ai;
1460 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001461 if (sentinel.ai_next == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001462 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001463 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001464
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001465 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001466
Bernie Innocenti948f6572018-09-12 21:32:42 +09001467 *rv = sentinel.ai_next;
1468 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001469}
1470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001471static void _sethtent(FILE** hostf) {
1472 if (!*hostf)
1473 *hostf = fopen(_PATH_HOSTS, "re");
1474 else
1475 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001476}
1477
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001478static void _endhtent(FILE** hostf) {
1479 if (*hostf) {
1480 (void) fclose(*hostf);
1481 *hostf = NULL;
1482 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001483}
1484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001485static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1486 char* p;
1487 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001488 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001489 int error;
1490 const char* addr;
1491 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001493 assert(name != NULL);
1494 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001495
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001496 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1497again:
1498 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1499 if (*p == '#') goto again;
1500 if (!(cp = strpbrk(p, "#\n"))) goto again;
1501 *cp = '\0';
1502 if (!(cp = strpbrk(p, " \t"))) goto again;
1503 *cp++ = '\0';
1504 addr = p;
1505 /* if this is not something we're looking for, skip it. */
1506 cname = NULL;
1507 while (cp && *cp) {
1508 if (*cp == ' ' || *cp == '\t') {
1509 cp++;
1510 continue;
1511 }
1512 if (!cname) cname = cp;
1513 tname = cp;
1514 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001515 if (strcasecmp(name, tname) == 0) goto found;
1516 }
1517 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001518
1519found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001520 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001521 if (error) goto again;
1522 for (res = res0; res; res = res->ai_next) {
1523 /* cover it up */
1524 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001525
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001526 if (pai->ai_flags & AI_CANONNAME) {
1527 if (get_canonname(pai, res, cname) != 0) {
1528 freeaddrinfo(res0);
1529 goto again;
1530 }
1531 }
1532 }
1533 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001534}
1535
Bernie Innocenti948f6572018-09-12 21:32:42 +09001536static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001537 struct addrinfo sentinel = {};
1538 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001539 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001540
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001541 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001542
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001543 _sethtent(&hostf);
1544 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1545 cur->ai_next = p;
1546 while (cur && cur->ai_next) cur = cur->ai_next;
1547 }
1548 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001549
Bernie Innocenti948f6572018-09-12 21:32:42 +09001550 *res = sentinel.ai_next;
1551 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001552}
1553
1554/* resolver logic */
1555
1556/*
1557 * Formulate a normal query, send, and await answer.
1558 * Returned answer is placed in supplied buffer "answer".
1559 * Perform preliminary check of answer, returning success only
1560 * if no error is indicated and the answer count is nonzero.
1561 * Return the size of the response on success, -1 on error.
Hungming Chend57ade02018-12-25 15:47:47 +08001562 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001563 *
1564 * Caller must parse answer and determine whether it answers the question.
1565 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001566static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001567 u_char buf[MAXPACKET];
1568 HEADER* hp;
1569 int n;
1570 struct res_target* t;
1571 int rcode;
1572 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001574 assert(name != NULL);
1575 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001576
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001577 rcode = NOERROR;
1578 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001579
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001580 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001581 u_char* answer;
1582 int anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001583
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001584 hp = (HEADER*) (void*) t->answer;
Ken Chenbfd32022019-01-02 14:59:38 +08001585 bool retried = false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001586 again:
1587 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001588
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001589 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001590 int cl = t->qclass;
1591 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001592 answer = t->answer;
1593 anslen = t->anslen;
chenbruce16adee42019-02-20 19:45:50 +08001594
Ken Chenbab50142019-03-19 17:41:28 +08001595 LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
Bernie Innocenti55864192018-08-30 04:05:20 +09001596
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001597 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Ken Chenbfd32022019-01-02 14:59:38 +08001598 if (n > 0 && (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && !retried)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001599 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001600 if (n <= 0) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001601 LOG(ERROR) << __func__ << ": res_nmkquery failed";
Hungming Chend57ade02018-12-25 15:47:47 +08001602 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001603 return n;
1604 }
Mike Yu69615f62018-11-06 15:42:36 +08001605
Luke Huang952d0942018-12-26 16:53:03 +08001606 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001607 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001608 // Record rcode from DNS response header only if no timeout.
1609 // Keep rcode timeout for reporting later if any.
1610 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001611 /* if the query choked with EDNS0, retry without EDNS0 */
1612 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
Ken Chenbfd32022019-01-02 14:59:38 +08001613 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001614 LOG(DEBUG) << __func__ << ": retry without EDNS0";
Ken Chenbfd32022019-01-02 14:59:38 +08001615 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001616 goto again;
1617 }
Bernie Innocentid017e972019-03-03 19:39:53 +09001618 LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001619 continue;
1620 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001621
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001622 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001623
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001624 t->n = n;
1625 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001626
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001627 if (ancount == 0) {
1628 switch (rcode) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001629 // Not defined in RFC.
1630 case RCODE_TIMEOUT:
1631 // DNS metrics monitors DNS query timeout.
1632 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1633 break;
1634 // Defined in RFC 1035 section 4.1.1.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001635 case NXDOMAIN:
Hungming Chend57ade02018-12-25 15:47:47 +08001636 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001637 break;
1638 case SERVFAIL:
Hungming Chend57ade02018-12-25 15:47:47 +08001639 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001640 break;
1641 case NOERROR:
Hungming Chend57ade02018-12-25 15:47:47 +08001642 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001643 break;
1644 case FORMERR:
1645 case NOTIMP:
1646 case REFUSED:
1647 default:
Hungming Chend57ade02018-12-25 15:47:47 +08001648 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649 break;
1650 }
1651 return -1;
1652 }
1653 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001654}
1655
1656/*
1657 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1658 * Return the size of the response on success, -1 on error.
1659 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chend57ade02018-12-25 15:47:47 +08001660 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001661 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001662static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001663 const char *cp, *const *domain;
1664 HEADER* hp;
1665 u_int dots;
1666 int trailing_dot, ret, saved_herrno;
1667 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001669 assert(name != NULL);
1670 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001671
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001672 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001673
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001674 errno = 0;
Hungming Chend57ade02018-12-25 15:47:47 +08001675 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001676 dots = 0;
1677 for (cp = name; *cp; cp++) dots += (*cp == '.');
1678 trailing_dot = 0;
1679 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001680
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001681 /*
1682 * If there are dots in the name already, let's just give it a try
1683 * 'as is'. The threshold can be set with the "ndots" option.
1684 */
1685 saved_herrno = -1;
1686 if (dots >= res->ndots) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001687 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001688 if (ret > 0) return (ret);
Hungming Chend57ade02018-12-25 15:47:47 +08001689 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001690 tried_as_is++;
1691 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001693 /*
1694 * We do at least one level of search if
1695 * - there is no dot and RES_DEFNAME is set, or
1696 * - there is at least one dot, there is no trailing dot,
1697 * and RES_DNSRCH is set.
1698 */
1699 if ((!dots && (res->options & RES_DEFNAMES)) ||
1700 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1701 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001703 /* Unfortunately we need to set stuff up before
1704 * the domain stuff is tried. Will have a better
1705 * fix after thread pools are used.
1706 */
1707 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001708
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001709 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001710 ret = res_querydomainN(name, *domain, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001711 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001713 /*
1714 * If no server present, give up.
1715 * If name isn't found in this domain,
1716 * keep trying higher domains in the search list
1717 * (if that's enabled).
1718 * On a NO_DATA error, keep trying, otherwise
1719 * a wildcard entry of another type could keep us
1720 * from finding this entry higher in the domain.
1721 * If we get some other error (negative answer or
1722 * server failure), then stop searching up,
1723 * but try the input name below in case it's
1724 * fully-qualified.
1725 */
1726 if (errno == ECONNREFUSED) {
Hungming Chend57ade02018-12-25 15:47:47 +08001727 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001728 return -1;
1729 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001730
Hungming Chend57ade02018-12-25 15:47:47 +08001731 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001732 case NO_DATA:
1733 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001734 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001735 case HOST_NOT_FOUND:
1736 /* keep trying */
1737 break;
1738 case TRY_AGAIN:
1739 if (hp->rcode == SERVFAIL) {
1740 /* try next search element, if any */
1741 got_servfail++;
1742 break;
1743 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001744 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001745 default:
1746 /* anything else implies that we're done */
1747 done++;
1748 }
1749 /*
1750 * if we got here for some reason other than DNSRCH,
1751 * we only wanted one iteration of the loop, so stop.
1752 */
1753 if (!(res->options & RES_DNSRCH)) done++;
1754 }
1755 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001756
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001757 /*
1758 * if we have not already tried the name "as is", do that now.
1759 * note that we do this regardless of how many dots were in the
1760 * name or whether it ends with a dot.
1761 */
1762 if (!tried_as_is) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001763 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001764 if (ret > 0) return ret;
1765 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001766
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001767 /*
1768 * if we got here, we didn't satisfy the search.
1769 * if we did an initial full query, return that query's h_errno
1770 * (note that we wouldn't be here if that query had succeeded).
1771 * else if we ever got a nodata, send that back as the reason.
1772 * else send back meaningless h_errno, that being the one from
1773 * the last DNSRCH we did.
1774 */
1775 if (saved_herrno != -1)
Hungming Chend57ade02018-12-25 15:47:47 +08001776 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001777 else if (got_nodata)
Hungming Chend57ade02018-12-25 15:47:47 +08001778 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001779 else if (got_servfail)
Hungming Chend57ade02018-12-25 15:47:47 +08001780 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001781 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001782}
1783
1784/*
1785 * Perform a call on res_query on the concatenation of name and domain,
1786 * removing a trailing dot from name if domain is NULL.
1787 */
Mike Yu69615f62018-11-06 15:42:36 +08001788static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +08001789 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001790 char nbuf[MAXDNAME];
1791 const char* longname = nbuf;
1792 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001793
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001794 assert(name != NULL);
Bernie Innocentid017e972019-03-03 19:39:53 +09001795
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001796 if (domain == NULL) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001797 // Check for trailing '.'; copy without '.' if present.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001798 n = strlen(name);
1799 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001800 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001801 return -1;
1802 }
1803 if (n > 0 && name[--n] == '.') {
1804 strncpy(nbuf, name, n);
1805 nbuf[n] = '\0';
1806 } else
1807 longname = name;
1808 } else {
1809 n = strlen(name);
1810 d = strlen(domain);
1811 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001812 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001813 return -1;
1814 }
1815 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1816 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001817 return res_queryN(longname, target, res, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001818}