blob: 9965ca187c8a8003dc4a1523a210625d1e50dfe6 [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 Innocenti55864192018-08-30 04:05:20 +090033#include <arpa/inet.h>
34#include <arpa/nameser.h>
35#include <assert.h>
36#include <ctype.h>
37#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090038#include <fcntl.h>
39#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090040#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090041#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090042#include <stdbool.h>
43#include <stddef.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090047#include <sys/param.h>
48#include <sys/socket.h>
49#include <sys/stat.h>
50#include <sys/types.h>
51#include <sys/un.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090052#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090053
chenbruce16adee42019-02-20 19:45:50 +080054#include <android-base/logging.h>
55
Bernie Innocenti189eb502018-10-01 23:10:18 +090056#include "netd_resolv/resolv.h"
57#include "resolv_cache.h"
58#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090059
Bernie Innocenti55864192018-08-30 04:05:20 +090060#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +090061
Bernie Innocenti93a31342018-12-12 00:43:02 +090062const char in_addrany[] = {0, 0, 0, 0};
63const char in_loopback[] = {127, 0, 0, 1};
64const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
65const 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 +090066
Bernie Innocenti93a31342018-12-12 00:43:02 +090067const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090068 int a_af;
69 int a_addrlen;
70 int a_socklen;
71 int a_off;
72 const char* a_addrany;
73 const char* a_loopback;
74 int a_scoped;
75} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090076 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
77 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
79 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
80 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +090081};
82
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090083struct Explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090084 int e_af;
85 int e_socktype;
86 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090087 int e_wild;
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090088#define WILD_AF(ex) ((ex).e_wild & 0x01)
89#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
90#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +090091};
92
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090093const Explore explore_options[] = {
Ken Chen3270cf52018-11-07 01:20:48 +080094 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
95 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
96 {PF_INET6, SOCK_RAW, ANY, 0x05},
97 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
98 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
99 {PF_INET, SOCK_RAW, ANY, 0x05},
100 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
101 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
102 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900103};
104
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900105#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900107
108typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900109 HEADER hdr;
110 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900111} querybuf;
112
113struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114 struct res_target* next;
115 const char* name; /* domain name */
116 int qclass, qtype; /* class and type of query */
117 u_char* answer; /* buffer to put answer */
118 int anslen; /* size of answer buffer */
119 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900120};
121
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122static int str2number(const char*);
123static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
124 const struct android_net_context*);
125static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
126static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
127 const char*);
128static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
129 struct addrinfo**);
130static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
131static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
132static int get_portmatch(const struct addrinfo*, const char*);
133static int get_port(const struct addrinfo*, const char*, int);
134static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900135static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
Hungming Chend57ade02018-12-25 15:47:47 +0800137static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
138 int* herrno);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900139static int dns_getaddrinfo(const char* name, const addrinfo* pai,
140 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141static void _sethtent(FILE**);
142static void _endhtent(FILE**);
143static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900144static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900146
Hungming Chen7f0d3292018-12-27 18:33:19 +0800147static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
148static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800149static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800150 int* herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900151
Bernie Innocenti93a31342018-12-12 00:43:02 +0900152const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 "Success",
154 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
155 "Temporary failure in name resolution", /* EAI_AGAIN */
156 "Invalid value for ai_flags", /* EAI_BADFLAGS */
157 "Non-recoverable failure in name resolution", /* EAI_FAIL */
158 "ai_family not supported", /* EAI_FAMILY */
159 "Memory allocation failure", /* EAI_MEMORY */
160 "No address associated with hostname", /* EAI_NODATA */
161 "hostname nor servname provided, or not known", /* EAI_NONAME */
162 "servname not supported for ai_socktype", /* EAI_SERVICE */
163 "ai_socktype not supported", /* EAI_SOCKTYPE */
164 "System error returned in errno", /* EAI_SYSTEM */
165 "Invalid value for hints", /* EAI_BADHINTS */
166 "Resolved protocol is unknown", /* EAI_PROTOCOL */
167 "Argument buffer overflow", /* EAI_OVERFLOW */
168 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900169};
170
171/* XXX macros that make external reference is BAD. */
172
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173#define GET_AI(ai, afd, addr) \
174 do { \
175 /* external reference: pai, error, and label free */ \
176 (ai) = get_ai(pai, (afd), (addr)); \
177 if ((ai) == NULL) { \
178 error = EAI_MEMORY; \
179 goto free; \
180 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900181 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183#define GET_PORT(ai, serv) \
184 do { \
185 /* external reference: error and label free */ \
186 error = get_port((ai), (serv), 0); \
187 if (error != 0) goto free; \
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 MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900191 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
192#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194const char* gai_strerror(int ecode) {
195 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
196 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900197}
198
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900199void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900200 while (ai) {
201 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900203 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 free(ai);
205 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900206 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900207}
208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900209static int str2number(const char* p) {
210 char* ep;
211 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215 if (*p == '\0') return -1;
216 ep = NULL;
217 errno = 0;
218 v = strtoul(p, &ep, 10);
219 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
220 return v;
221 else
222 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900223}
224
225/*
226 * The following functions determine whether IPv4 or IPv6 connectivity is
227 * available in order to implement AI_ADDRCONFIG.
228 *
229 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
230 * available, but whether addresses of the specified family are "configured
231 * on the local system". However, bionic doesn't currently support getifaddrs,
232 * so checking for connectivity is the next best thing.
233 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234static int _have_ipv6(unsigned mark, uid_t uid) {
235 static const struct sockaddr_in6 sin6_test = {
236 .sin6_family = AF_INET6,
237 .sin6_addr.s6_addr = {// 2000::
238 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800239 sockaddr_union addr = {.sin6 = sin6_test};
240 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900241}
242
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900243static int _have_ipv4(unsigned mark, uid_t uid) {
244 static const struct sockaddr_in sin_test = {
245 .sin_family = AF_INET,
246 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
247 };
nuccachene172a4e2018-10-23 17:10:58 +0800248 sockaddr_union addr = {.sin = sin_test};
249 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900250}
251
Bernie Innocentic165ce82018-10-16 23:35:28 +0900252// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
253// NOTE: also called by resolv_set_nameservers_for_net().
254int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
255 addrinfo** result) {
256 hints.ai_flags = AI_NUMERICHOST;
257 const android_net_context netcontext = {
258 .app_netid = NETID_UNSET,
259 .app_mark = MARK_UNSET,
260 .dns_netid = NETID_UNSET,
261 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900262 .uid = NET_CONTEXT_INVALID_UID,
263 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900264 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900265}
266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
268 const struct addrinfo* hints,
269 const struct android_net_context* netcontext,
270 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800271 struct addrinfo sentinel = {};
Bernie Innocentib47552e2019-02-20 18:39:35 +0900272 struct addrinfo* cur = &sentinel;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900273 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900274
Bernie Innocentib47552e2019-02-20 18:39:35 +0900275 // hostname is allowed to be nullptr
276 // servname is allowed to be nullptr
277 // hints is allowed to be nullptr
278 assert(res != nullptr);
279 assert(netcontext != nullptr);
Bernie Innocenti2319a772019-02-20 17:50:38 +0900280
Bernie Innocentib47552e2019-02-20 18:39:35 +0900281 struct addrinfo ai = {
282 .ai_flags = 0,
283 .ai_family = PF_UNSPEC,
284 .ai_socktype = ANY,
285 .ai_protocol = ANY,
286 .ai_addrlen = 0,
287 .ai_canonname = nullptr,
288 .ai_addr = nullptr,
289 .ai_next = nullptr,
290 };
Bernie Innocenti2319a772019-02-20 17:50:38 +0900291
Ken Chen3270cf52018-11-07 01:20:48 +0800292 do {
293 if (hostname == NULL && servname == NULL) {
294 error = EAI_NONAME;
295 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900296 }
Ken Chen3270cf52018-11-07 01:20:48 +0800297 if (hints) {
298 /* error check for hints */
299 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
300 error = EAI_BADHINTS;
301 break;
302 }
303 if (hints->ai_flags & ~AI_MASK) {
304 error = EAI_BADFLAGS;
305 break;
306 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900307
Ken Chen3270cf52018-11-07 01:20:48 +0800308 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
309 hints->ai_family == PF_INET6)) {
310 error = EAI_FAMILY;
311 break;
312 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900313
314 ai = *hints;
Ken Chen3270cf52018-11-07 01:20:48 +0800315
316 /*
317 * if both socktype/protocol are specified, check if they
318 * are meaningful combination.
319 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900320 if (ai.ai_socktype != ANY && ai.ai_protocol != ANY) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900321 for (const Explore& ex : explore_options) {
322 if (ai.ai_family != ex.e_af) continue;
323 if (ex.e_socktype == ANY) continue;
324 if (ex.e_protocol == ANY) continue;
325 if (ai.ai_socktype == ex.e_socktype && ai.ai_protocol != ex.e_protocol) {
Ken Chen3270cf52018-11-07 01:20:48 +0800326 error = EAI_BADHINTS;
327 break;
328 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900329 }
Ken Chen3270cf52018-11-07 01:20:48 +0800330 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 }
332 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900333
Ken Chen3270cf52018-11-07 01:20:48 +0800334 /*
Bernie Innocentib47552e2019-02-20 18:39:35 +0900335 * Check for special cases:
336 * (1) numeric servname is disallowed if socktype/protocol are left unspecified.
337 * (2) servname is disallowed for raw and other inet{,6} sockets.
Ken Chen3270cf52018-11-07 01:20:48 +0800338 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900339 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
Bernie Innocentib47552e2019-02-20 18:39:35 +0900340 struct addrinfo tmp = ai;
341 if (tmp.ai_family == PF_UNSPEC) {
342 tmp.ai_family = PF_INET6;
Ken Chen3270cf52018-11-07 01:20:48 +0800343 }
Bernie Innocentib47552e2019-02-20 18:39:35 +0900344 error = get_portmatch(&tmp, servname);
Ken Chen3270cf52018-11-07 01:20:48 +0800345 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900346 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900347
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900348 // NULL hostname, or numeric hostname
349 for (const Explore& ex : explore_options) {
Ken Chen3270cf52018-11-07 01:20:48 +0800350 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900351 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900352
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900353 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
354 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
355 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900356
Bernie Innocentib47552e2019-02-20 18:39:35 +0900357 struct addrinfo tmp = ai;
358 if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
359 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
360 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800361
Bernie Innocentib47552e2019-02-20 18:39:35 +0900362 if (hostname == nullptr)
363 error = explore_null(&tmp, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800364 else
Bernie Innocentib47552e2019-02-20 18:39:35 +0900365 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800366
367 if (error) break;
368
369 while (cur->ai_next) cur = cur->ai_next;
370 }
371 if (error) break;
372
373 /*
374 * XXX
375 * If numeric representation of AF1 can be interpreted as FQDN
376 * representation of AF2, we need to think again about the code below.
377 */
378 if (sentinel.ai_next) break;
379
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900380 if (hostname == nullptr) {
Ken Chen3270cf52018-11-07 01:20:48 +0800381 error = EAI_NODATA;
382 break;
383 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900384 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chen3270cf52018-11-07 01:20:48 +0800385 error = EAI_NONAME;
386 break;
387 }
388
389 /*
390 * hostname as alphabetical name.
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900391 * 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 +0800392 */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900393 for (const Explore& ex : explore_options) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900394 // Require exact match for family field
395 if (ai.ai_family != ex.e_af) continue;
Ken Chen3270cf52018-11-07 01:20:48 +0800396
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900397 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800398 continue;
399 }
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900400 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800401 continue;
402 }
403
Bernie Innocentib47552e2019-02-20 18:39:35 +0900404 struct addrinfo tmp = ai;
405 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
406 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800407
Bernie Innocentib47552e2019-02-20 18:39:35 +0900408 error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext);
Ken Chen3270cf52018-11-07 01:20:48 +0800409
410 while (cur->ai_next) cur = cur->ai_next;
411 }
412
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900413 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800414 error = 0;
415 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900416 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800417 }
418 } while (0);
419
420 if (error) {
421 freeaddrinfo(sentinel.ai_next);
422 *res = NULL;
423 } else {
424 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900426 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900427}
428
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900429// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900430static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
431 struct addrinfo** res, const struct android_net_context* netcontext) {
432 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900433 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900434
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900435 assert(pai != NULL);
436 /* hostname may be NULL */
437 /* servname may be NULL */
438 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900439
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900441
Bernie Innocenti948f6572018-09-12 21:32:42 +0900442 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900443 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900444
Bernie Innocenti948f6572018-09-12 21:32:42 +0900445 if (!files_getaddrinfo(hostname, pai, &result)) {
446 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900447 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900448 if (!error) {
449 struct addrinfo* cur;
450 for (cur = result; cur; cur = cur->ai_next) {
451 GET_PORT(cur, servname);
452 /* canonname should be filled already */
453 }
454 *res = result;
455 return 0;
456 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900457
458free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900459 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900460 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900461}
462
463/*
464 * hostname == NULL.
465 * passive socket -> anyaddr (0.0.0.0 or ::)
466 * non-passive socket -> localhost (127.0.0.1 or ::1)
467 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900468static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
469 int s;
470 const struct afd* afd;
471 struct addrinfo* cur;
472 struct addrinfo sentinel;
473 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 assert(pai != NULL);
476 /* servname may be NULL */
477 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900478
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 *res = NULL;
480 sentinel.ai_next = NULL;
481 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 /*
484 * filter out AFs that are not supported by the kernel
485 * XXX errno?
486 */
487 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
488 if (s < 0) {
489 if (errno != EMFILE) return 0;
490 } else
491 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 /*
494 * if the servname does not match socktype/protocol, ignore it.
495 */
496 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900497
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 afd = find_afd(pai->ai_family);
499 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900500
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900501 if (pai->ai_flags & AI_PASSIVE) {
502 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 GET_PORT(cur->ai_next, servname);
504 } else {
505 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506 GET_PORT(cur->ai_next, servname);
507 }
508 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900509
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 *res = sentinel.ai_next;
511 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900512
513free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900514 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900515 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900516}
517
518/*
519 * numeric hostname
520 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
522 struct addrinfo** res, const char* canonname) {
523 const struct afd* afd;
524 struct addrinfo* cur;
525 struct addrinfo sentinel;
526 int error;
527 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900528
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900529 assert(pai != NULL);
530 /* hostname may be NULL */
531 /* servname may be NULL */
532 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900533
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900534 *res = NULL;
535 sentinel.ai_next = NULL;
536 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900537
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538 /*
539 * if the servname does not match socktype/protocol, ignore it.
540 */
541 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900542
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900543 afd = find_afd(pai->ai_family);
544 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900545
Ken Chen15c805a2018-10-17 00:19:59 +0800546 if (inet_pton(afd->a_af, hostname, pton) == 1) {
547 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
548 GET_AI(cur->ai_next, afd, pton);
549 GET_PORT(cur->ai_next, servname);
550 if ((pai->ai_flags & AI_CANONNAME)) {
551 /*
552 * Set the numeric address itself as
553 * the canonical name, based on a
554 * clarification in rfc2553bis-03.
555 */
Ken Chen3270cf52018-11-07 01:20:48 +0800556 error = get_canonname(pai, cur->ai_next, canonname);
557 if (error != 0) {
558 freeaddrinfo(sentinel.ai_next);
559 return error;
560 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900561 }
Ken Chen15c805a2018-10-17 00:19:59 +0800562 while (cur->ai_next) cur = cur->ai_next;
563 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800564 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 *res = sentinel.ai_next;
568 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900569
570free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900571 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900572 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900573}
574
575/*
576 * numeric hostname with scope
577 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
579 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900580 const struct afd* afd;
581 struct addrinfo* cur;
582 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900583 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900584 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900585
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900586 assert(pai != NULL);
587 /* hostname may be NULL */
588 /* servname may be NULL */
589 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900590
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 /*
592 * if the servname does not match socktype/protocol, ignore it.
593 */
594 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 afd = find_afd(pai->ai_family);
597 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900598
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900599 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 cp = strchr(hostname, SCOPE_DELIMITER);
602 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900603
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900604 /*
605 * Handle special case of <scoped_address><delimiter><scope id>
606 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900607 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 if (hostname2 == NULL) return EAI_MEMORY;
609 /* terminate at the delimiter */
610 hostname2[cp - hostname] = '\0';
611 addr = hostname2;
612 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900613
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900614 error = explore_numeric(pai, addr, servname, res, hostname);
615 if (error == 0) {
616 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900617
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618 for (cur = *res; cur; cur = cur->ai_next) {
619 if (cur->ai_family != AF_INET6) continue;
620 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
621 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
622 free(hostname2);
623 return (EAI_NODATA); /* XXX: is return OK? */
624 }
625 sin6->sin6_scope_id = scopeid;
626 }
627 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900628
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900630
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900631 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900632}
633
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900634static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
635 assert(pai != NULL);
636 assert(ai != NULL);
637 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 if ((pai->ai_flags & AI_CANONNAME) != 0) {
640 ai->ai_canonname = strdup(str);
641 if (ai->ai_canonname == NULL) return EAI_MEMORY;
642 }
643 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900644}
645
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900646static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
647 const char* addr) {
648 char* p;
649 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900650
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651 assert(pai != NULL);
652 assert(afd != NULL);
653 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900654
nuccachene21023a2018-09-11 11:13:44 +0800655 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900658 memcpy(ai, pai, sizeof(struct addrinfo));
659 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800660 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900661
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900662 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
664 p = (char*) (void*) (ai->ai_addr);
665 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
666 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900667}
668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669static int get_portmatch(const struct addrinfo* ai, const char* servname) {
670 assert(ai != NULL);
671 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900674}
675
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900676static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
677 const char* proto;
678 struct servent* sp;
679 int port;
680 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900681
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900682 assert(ai != NULL);
683 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900684
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900685 if (servname == NULL) return 0;
686 switch (ai->ai_family) {
687 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900688 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 break;
690 default:
691 return 0;
692 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 switch (ai->ai_socktype) {
695 case SOCK_RAW:
696 return EAI_SERVICE;
697 case SOCK_DGRAM:
698 case SOCK_STREAM:
699 allownumeric = 1;
700 break;
701 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900702 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703 break;
704 default:
705 return EAI_SOCKTYPE;
706 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708 port = str2number(servname);
709 if (port >= 0) {
710 if (!allownumeric) return EAI_SERVICE;
711 if (port < 0 || port > 65535) return EAI_SERVICE;
712 port = htons(port);
713 } else {
714 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900715
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900716 switch (ai->ai_socktype) {
717 case SOCK_DGRAM:
718 proto = "udp";
719 break;
720 case SOCK_STREAM:
721 proto = "tcp";
722 break;
723 default:
724 proto = NULL;
725 break;
726 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900728 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
729 port = sp->s_port;
730 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732 if (!matchonly) {
733 switch (ai->ai_family) {
734 case AF_INET:
735 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
736 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900737 case AF_INET6:
738 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
739 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900740 }
741 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900743 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900744}
745
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900746static const struct afd* find_afd(int af) {
747 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900748
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900749 if (af == PF_UNSPEC) return NULL;
750 for (afd = afdl; afd->a_af; afd++) {
751 if (afd->a_af == af) return afd;
752 }
753 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900754}
755
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900756// Convert a string to a scope identifier.
757static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 u_long lscopeid;
759 struct in6_addr* a6;
760 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 assert(scope != NULL);
763 assert(sin6 != NULL);
764 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900765
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900766 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 /* empty scopeid portion is invalid */
769 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
772 /*
773 * We currently assume a one-to-one mapping between links
774 * and interfaces, so we simply use interface indices for
775 * like-local scopes.
776 */
777 *scopeid = if_nametoindex(scope);
778 if (*scopeid == 0) goto trynumeric;
779 return 0;
780 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900781
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900782 /* still unclear about literal, allow numeric only - placeholder */
783 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
784 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
785 goto trynumeric;
786 else
787 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900788
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900789 /* try to convert to a numeric id as a last resort */
790trynumeric:
791 errno = 0;
792 lscopeid = strtoul(scope, &ep, 10);
793 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
794 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
795 return 0;
796 else
797 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900798}
Bernie Innocenti55864192018-08-30 04:05:20 +0900799
800/* code duplicate with gethnamaddr.c */
801
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900802#define BOUNDED_INCR(x) \
803 do { \
804 BOUNDS_CHECK(cp, x); \
805 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900806 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900808#define BOUNDS_CHECK(ptr, count) \
809 do { \
810 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800811 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 return NULL; \
813 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900814 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900815
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900816static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800817 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800818 struct addrinfo sentinel = {};
819 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820 struct addrinfo ai;
821 const struct afd* afd;
822 char* canonname;
823 const HEADER* hp;
824 const u_char* cp;
825 int n;
826 const u_char* eom;
827 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900828 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 int haveanswer, had_error;
830 char tbuf[MAXDNAME];
831 int (*name_ok)(const char*);
832 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900833
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900834 assert(answer != NULL);
835 assert(qname != NULL);
836 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900837
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900838 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 canonname = NULL;
841 eom = answer->buf + anslen;
842 switch (qtype) {
843 case T_A:
844 case T_AAAA:
845 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
846 name_ok = res_hnok;
847 break;
848 default:
849 return NULL; /* XXX should be abort(); */
850 }
851 /*
852 * find first satisfactory answer
853 */
854 hp = &answer->hdr;
855 ancount = ntohs(hp->ancount);
856 qdcount = ntohs(hp->qdcount);
857 bp = hostbuf;
858 ep = hostbuf + sizeof hostbuf;
859 cp = answer->buf;
860 BOUNDED_INCR(HFIXEDSZ);
861 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800862 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900863 return (NULL);
864 }
865 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
866 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800867 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 return (NULL);
869 }
870 BOUNDED_INCR(n + QFIXEDSZ);
871 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
872 /* res_send() has already verified that the query name is the
873 * same as the one we sent; this just gets the expanded name
874 * (i.e., with the succeeding search-domain tacked on).
875 */
876 n = strlen(bp) + 1; /* for the \0 */
877 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800878 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 return (NULL);
880 }
881 canonname = bp;
882 bp += n;
883 /* The qname can be abbreviated, but h_name is now absolute. */
884 qname = canonname;
885 }
886 haveanswer = 0;
887 had_error = 0;
888 while (ancount-- > 0 && cp < eom && !had_error) {
889 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
890 if ((n < 0) || !(*name_ok)(bp)) {
891 had_error++;
892 continue;
893 }
894 cp += n; /* name */
895 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900896 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900897 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900898 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900900 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900901 cp += INT16SZ; /* len */
902 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900903 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900904 /* XXX - debug? syslog? */
905 cp += n;
906 continue; /* XXX - had_error++ ? */
907 }
908 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
909 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
910 if ((n < 0) || !(*name_ok)(tbuf)) {
911 had_error++;
912 continue;
913 }
914 cp += n;
915 /* Get canonical name. */
916 n = strlen(tbuf) + 1; /* for the \0 */
917 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
918 had_error++;
919 continue;
920 }
921 strlcpy(bp, tbuf, (size_t)(ep - bp));
922 canonname = bp;
923 bp += n;
924 continue;
925 }
926 if (qtype == T_ANY) {
927 if (!(type == T_A || type == T_AAAA)) {
928 cp += n;
929 continue;
930 }
931 } else if (type != qtype) {
932 if (type != T_KEY && type != T_SIG)
chenbruce16adee42019-02-20 19:45:50 +0800933 LOG(DEBUG) << __func__ << "(getanswer): asked for \"" << qname << " "
934 << p_class(C_IN) << " " << p_type(qtype) << "\", got type \""
935 << p_type(type) << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900936 cp += n;
937 continue; /* XXX - had_error++ ? */
938 }
939 switch (type) {
940 case T_A:
941 case T_AAAA:
942 if (strcasecmp(canonname, bp) != 0) {
chenbruce16adee42019-02-20 19:45:50 +0800943 LOG(DEBUG) << __func__ << "(getanswer): asked for \"" << canonname
944 << "\", got \"" << bp << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900945 cp += n;
946 continue; /* XXX - had_error++ ? */
947 }
948 if (type == T_A && n != INADDRSZ) {
949 cp += n;
950 continue;
951 }
952 if (type == T_AAAA && n != IN6ADDRSZ) {
953 cp += n;
954 continue;
955 }
956 if (type == T_AAAA) {
957 struct in6_addr in6;
958 memcpy(&in6, cp, IN6ADDRSZ);
959 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
960 cp += n;
961 continue;
962 }
963 }
964 if (!haveanswer) {
965 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900966
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 canonname = bp;
968 nn = strlen(bp) + 1; /* for the \0 */
969 bp += nn;
970 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900971
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900972 /* don't overwrite pai */
973 ai = *pai;
974 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
975 afd = find_afd(ai.ai_family);
976 if (afd == NULL) {
977 cp += n;
978 continue;
979 }
980 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
981 if (cur->ai_next == NULL) had_error++;
982 while (cur && cur->ai_next) cur = cur->ai_next;
983 cp += n;
984 break;
985 default:
986 abort();
987 }
988 if (!had_error) haveanswer++;
989 }
990 if (haveanswer) {
991 if (!canonname)
992 (void) get_canonname(pai, sentinel.ai_next, qname);
993 else
994 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +0800995 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900996 return sentinel.ai_next;
997 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900998
Hungming Chend57ade02018-12-25 15:47:47 +0800999 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001000 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001001}
1002
1003struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001004 struct addrinfo* ai;
1005 int has_src_addr;
1006 sockaddr_union src_addr;
1007 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001008};
1009
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001010static int _get_scope(const struct sockaddr* addr) {
1011 if (addr->sa_family == AF_INET6) {
1012 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1013 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1014 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1015 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1016 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1017 /*
1018 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1019 * link-local scope.
1020 */
1021 return IPV6_ADDR_SCOPE_LINKLOCAL;
1022 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1023 return IPV6_ADDR_SCOPE_SITELOCAL;
1024 } else {
1025 return IPV6_ADDR_SCOPE_GLOBAL;
1026 }
1027 } else if (addr->sa_family == AF_INET) {
1028 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1029 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001030
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001031 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1032 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1033 return IPV6_ADDR_SCOPE_LINKLOCAL;
1034 } else {
1035 /*
1036 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1037 * and shared addresses (100.64.0.0/10), are assigned global scope.
1038 */
1039 return IPV6_ADDR_SCOPE_GLOBAL;
1040 }
1041 } else {
1042 /*
1043 * This should never happen.
1044 * Return a scope with low priority as a last resort.
1045 */
1046 return IPV6_ADDR_SCOPE_NODELOCAL;
1047 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001048}
1049
1050/* These macros are modelled after the ones in <netinet/in6.h>. */
1051
1052/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001053#define IN6_IS_ADDR_TEREDO(a) \
1054 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001055
1056/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001057#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001058
1059/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001060#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001061
1062/*
1063 * Get the label for a given IPv4/IPv6 address.
1064 * RFC 6724, section 2.1.
1065 */
1066
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001067static int _get_label(const struct sockaddr* addr) {
1068 if (addr->sa_family == AF_INET) {
1069 return 4;
1070 } else if (addr->sa_family == AF_INET6) {
1071 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1072 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1073 return 0;
1074 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1075 return 4;
1076 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1077 return 2;
1078 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1079 return 5;
1080 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1081 return 13;
1082 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1083 return 3;
1084 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1085 return 11;
1086 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1087 return 12;
1088 } else {
1089 /* All other IPv6 addresses, including global unicast addresses. */
1090 return 1;
1091 }
1092 } else {
1093 /*
1094 * This should never happen.
1095 * Return a semi-random label as a last resort.
1096 */
1097 return 1;
1098 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001099}
1100
1101/*
1102 * Get the precedence for a given IPv4/IPv6 address.
1103 * RFC 6724, section 2.1.
1104 */
1105
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001106static int _get_precedence(const struct sockaddr* addr) {
1107 if (addr->sa_family == AF_INET) {
1108 return 35;
1109 } else if (addr->sa_family == AF_INET6) {
1110 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1111 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1112 return 50;
1113 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1114 return 35;
1115 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1116 return 30;
1117 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1118 return 5;
1119 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1120 return 3;
1121 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1122 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1123 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1124 return 1;
1125 } else {
1126 /* All other IPv6 addresses, including global unicast addresses. */
1127 return 40;
1128 }
1129 } else {
1130 return 1;
1131 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001132}
1133
1134/*
1135 * Find number of matching initial bits between the two addresses a1 and a2.
1136 */
1137
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001138static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1139 const char* p1 = (const char*) a1;
1140 const char* p2 = (const char*) a2;
1141 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001142
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001143 for (i = 0; i < sizeof(*a1); ++i) {
1144 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001145
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001146 if (p1[i] == p2[i]) {
1147 continue;
1148 }
1149 x = p1[i] ^ p2[i];
1150 for (j = 0; j < CHAR_BIT; ++j) {
1151 if (x & (1 << (CHAR_BIT - 1))) {
1152 return i * CHAR_BIT + j;
1153 }
1154 x <<= 1;
1155 }
1156 }
1157 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001158}
1159
1160/*
1161 * Compare two source/destination address pairs.
1162 * RFC 6724, section 6.
1163 */
1164
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001165static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1166 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1167 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1168 int scope_src1, scope_dst1, scope_match1;
1169 int scope_src2, scope_dst2, scope_match2;
1170 int label_src1, label_dst1, label_match1;
1171 int label_src2, label_dst2, label_match2;
1172 int precedence1, precedence2;
1173 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001174
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001175 /* Rule 1: Avoid unusable destinations. */
1176 if (a1->has_src_addr != a2->has_src_addr) {
1177 return a2->has_src_addr - a1->has_src_addr;
1178 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001179
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001180 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001181 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001182 scope_dst1 = _get_scope(a1->ai->ai_addr);
1183 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001184
nuccachene172a4e2018-10-23 17:10:58 +08001185 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001186 scope_dst2 = _get_scope(a2->ai->ai_addr);
1187 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001189 if (scope_match1 != scope_match2) {
1190 return scope_match2 - scope_match1;
1191 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001192
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001193 /*
1194 * Rule 3: Avoid deprecated addresses.
1195 * TODO(sesse): We don't currently have a good way of finding this.
1196 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001198 /*
1199 * Rule 4: Prefer home addresses.
1200 * TODO(sesse): We don't currently have a good way of finding this.
1201 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001203 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001204 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001205 label_dst1 = _get_label(a1->ai->ai_addr);
1206 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001207
nuccachene172a4e2018-10-23 17:10:58 +08001208 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001209 label_dst2 = _get_label(a2->ai->ai_addr);
1210 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001212 if (label_match1 != label_match2) {
1213 return label_match2 - label_match1;
1214 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001215
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001216 /* Rule 6: Prefer higher precedence. */
1217 precedence1 = _get_precedence(a1->ai->ai_addr);
1218 precedence2 = _get_precedence(a2->ai->ai_addr);
1219 if (precedence1 != precedence2) {
1220 return precedence2 - precedence1;
1221 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001223 /*
1224 * Rule 7: Prefer native transport.
1225 * TODO(sesse): We don't currently have a good way of finding this.
1226 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001228 /* Rule 8: Prefer smaller scope. */
1229 if (scope_dst1 != scope_dst2) {
1230 return scope_dst1 - scope_dst2;
1231 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001232
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001233 /*
1234 * Rule 9: Use longest matching prefix.
1235 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1236 * to work very well directly applied to IPv4. (glibc uses information from
1237 * the routing table for a custom IPv4 implementation here.)
1238 */
1239 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1240 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001241 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001242 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001243 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001244 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1245 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1246 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1247 if (prefixlen1 != prefixlen2) {
1248 return prefixlen2 - prefixlen1;
1249 }
1250 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001252 /*
1253 * Rule 10: Leave the order unchanged.
1254 * We need this since qsort() is not necessarily stable.
1255 */
1256 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001257}
1258
1259/*
1260 * Find the source address that will be used if trying to connect to the given
1261 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1262 *
1263 * Returns 1 if a source address was found, 0 if the address is unreachable,
1264 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1265 * undefined.
1266 */
1267
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001268static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1269 uid_t uid) {
1270 int sock;
1271 int ret;
1272 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001273
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001274 switch (addr->sa_family) {
1275 case AF_INET:
1276 len = sizeof(struct sockaddr_in);
1277 break;
1278 case AF_INET6:
1279 len = sizeof(struct sockaddr_in6);
1280 break;
1281 default:
1282 /* No known usable source address for non-INET families. */
1283 return 0;
1284 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001286 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1287 if (sock == -1) {
1288 if (errno == EAFNOSUPPORT) {
1289 return 0;
1290 } else {
1291 return -1;
1292 }
1293 }
1294 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1295 close(sock);
1296 return 0;
1297 }
1298 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1299 close(sock);
1300 return 0;
1301 }
1302 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001303 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001304 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001305
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001306 if (ret == -1) {
1307 close(sock);
1308 return 0;
1309 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001310
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001311 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1312 close(sock);
1313 return -1;
1314 }
1315 close(sock);
1316 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001317}
1318
1319/*
1320 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1321 * Will leave the list unchanged if an error occurs.
1322 */
1323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001324static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1325 struct addrinfo* cur;
1326 int nelem = 0, i;
1327 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001328
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001329 cur = list_sentinel->ai_next;
1330 while (cur) {
1331 ++nelem;
1332 cur = cur->ai_next;
1333 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001334
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001335 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1336 if (elems == NULL) {
1337 goto error;
1338 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001339
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001340 /*
1341 * Convert the linked list to an array that also contains the candidate
1342 * source address for each destination address.
1343 */
1344 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1345 int has_src_addr;
1346 assert(cur != NULL);
1347 elems[i].ai = cur;
1348 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001349
nuccachene172a4e2018-10-23 17:10:58 +08001350 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001351 if (has_src_addr == -1) {
1352 goto error;
1353 }
1354 elems[i].has_src_addr = has_src_addr;
1355 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001356
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001357 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1358 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001359
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001360 list_sentinel->ai_next = elems[0].ai;
1361 for (i = 0; i < nelem - 1; ++i) {
1362 elems[i].ai->ai_next = elems[i + 1].ai;
1363 }
1364 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001365
1366error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001367 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001368}
1369
Bernie Innocenti948f6572018-09-12 21:32:42 +09001370static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1371 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001372 struct addrinfo *ai, *cur;
1373 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001374 struct res_target q, q2;
1375 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001376
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001377 memset(&q, 0, sizeof(q));
1378 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001379 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001380
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001381 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001382 if (buf == NULL) {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001383 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001384 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001385 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001386 if (buf2 == NULL) {
1387 free(buf);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001388 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001389 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001390
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001391 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001392 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001393 /* prefer IPv6 */
1394 q.name = name;
1395 q.qclass = C_IN;
1396 q.answer = buf->buf;
1397 q.anslen = sizeof(buf->buf);
1398 int query_ipv6 = 1, query_ipv4 = 1;
1399 if (pai->ai_flags & AI_ADDRCONFIG) {
1400 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1401 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1402 }
1403 if (query_ipv6) {
1404 q.qtype = T_AAAA;
1405 if (query_ipv4) {
1406 q.next = &q2;
1407 q2.name = name;
1408 q2.qclass = C_IN;
1409 q2.qtype = T_A;
1410 q2.answer = buf2->buf;
1411 q2.anslen = sizeof(buf2->buf);
1412 }
1413 } else if (query_ipv4) {
1414 q.qtype = T_A;
1415 } else {
1416 free(buf);
1417 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001418 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001419 }
1420 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001421 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001422 case AF_INET:
1423 q.name = name;
1424 q.qclass = C_IN;
1425 q.qtype = T_A;
1426 q.answer = buf->buf;
1427 q.anslen = sizeof(buf->buf);
1428 break;
1429 case AF_INET6:
1430 q.name = name;
1431 q.qclass = C_IN;
1432 q.qtype = T_AAAA;
1433 q.answer = buf->buf;
1434 q.anslen = sizeof(buf->buf);
1435 break;
1436 default:
1437 free(buf);
1438 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001439 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001440 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001441
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001442 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001443 if (res == NULL) {
1444 free(buf);
1445 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001446 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001447 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001448
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001449 /* this just sets our netid val in the thread private data so we don't have to
1450 * modify the api's all the way down to res_send.c's res_nsend. We could
1451 * fully populate the thread private data here, but if we get down there
1452 * and have a cache hit that would be wasted, so we do the rest there on miss
1453 */
1454 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001455
Hungming Chend57ade02018-12-25 15:47:47 +08001456 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +08001457 if (res_searchN(name, &q, res, &herrno) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001458 free(buf);
1459 free(buf2);
Hungming Chen7f0d3292018-12-27 18:33:19 +08001460 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
1461 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001462 }
Hungming Chend57ade02018-12-25 15:47:47 +08001463 ai = getanswer(buf, q.n, q.name, q.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001464 if (ai) {
1465 cur->ai_next = ai;
1466 while (cur && cur->ai_next) cur = cur->ai_next;
1467 }
1468 if (q.next) {
Hungming Chend57ade02018-12-25 15:47:47 +08001469 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001470 if (ai) cur->ai_next = ai;
1471 }
1472 free(buf);
1473 free(buf2);
1474 if (sentinel.ai_next == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001475 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001476 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001477
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001478 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001479
Bernie Innocenti948f6572018-09-12 21:32:42 +09001480 *rv = sentinel.ai_next;
1481 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001482}
1483
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001484static void _sethtent(FILE** hostf) {
1485 if (!*hostf)
1486 *hostf = fopen(_PATH_HOSTS, "re");
1487 else
1488 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001489}
1490
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001491static void _endhtent(FILE** hostf) {
1492 if (*hostf) {
1493 (void) fclose(*hostf);
1494 *hostf = NULL;
1495 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001496}
1497
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001498static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1499 char* p;
1500 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001501 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001502 int error;
1503 const char* addr;
1504 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001505
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001506 assert(name != NULL);
1507 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001508
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001509 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1510again:
1511 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1512 if (*p == '#') goto again;
1513 if (!(cp = strpbrk(p, "#\n"))) goto again;
1514 *cp = '\0';
1515 if (!(cp = strpbrk(p, " \t"))) goto again;
1516 *cp++ = '\0';
1517 addr = p;
1518 /* if this is not something we're looking for, skip it. */
1519 cname = NULL;
1520 while (cp && *cp) {
1521 if (*cp == ' ' || *cp == '\t') {
1522 cp++;
1523 continue;
1524 }
1525 if (!cname) cname = cp;
1526 tname = cp;
1527 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1528 // fprintf(stderr, "\ttname = '%s'", tname);
1529 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
1609 LOG(DEBUG) << ";; res_queryN(" << name << ", " << 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) {
chenbruce16adee42019-02-20 19:45:50 +08001615 LOG(DEBUG) << ";; res_queryN: mkquery 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) {
chenbruce16adee42019-02-20 19:45:50 +08001628 LOG(DEBUG) << ";; res_queryN: retry without EDNS0";
Ken Chenbfd32022019-01-02 14:59:38 +08001629 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001630 goto again;
1631 }
chenbruce16adee42019-02-20 19:45:50 +08001632 LOG(DEBUG) << ";; 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);
1809 /* XXX: target may be NULL??? */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001810 if (domain == NULL) {
chenbruce16adee42019-02-20 19:45:50 +08001811 LOG(DEBUG) << ";; res_querydomain(" << name << ", <Nil>)";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001812 /*
1813 * Check for trailing '.';
1814 * copy without '.' if present.
1815 */
1816 n = strlen(name);
1817 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001818 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001819 return -1;
1820 }
1821 if (n > 0 && name[--n] == '.') {
1822 strncpy(nbuf, name, n);
1823 nbuf[n] = '\0';
1824 } else
1825 longname = name;
1826 } else {
chenbruce16adee42019-02-20 19:45:50 +08001827 LOG(DEBUG) << ";; res_querydomain(" << name << ", " << domain << ")";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001828 n = strlen(name);
1829 d = strlen(domain);
1830 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001831 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001832 return -1;
1833 }
1834 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1835 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001836 return res_queryN(longname, target, res, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001837}