blob: 7369b9bc4e4bbf653268cba3abb524195a442dc8 [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 Innocentif12d5bb2018-08-31 14:09:46 +090052#include <syslog.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090053#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090054
Bernie Innocenti189eb502018-10-01 23:10:18 +090055#include "netd_resolv/resolv.h"
56#include "resolv_cache.h"
57#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090058
Bernie Innocenti55864192018-08-30 04:05:20 +090059#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +090060
Bernie Innocenti93a31342018-12-12 00:43:02 +090061const char in_addrany[] = {0, 0, 0, 0};
62const char in_loopback[] = {127, 0, 0, 1};
63const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
64const 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 +090065
Bernie Innocenti93a31342018-12-12 00:43:02 +090066const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090067 int a_af;
68 int a_addrlen;
69 int a_socklen;
70 int a_off;
71 const char* a_addrany;
72 const char* a_loopback;
73 int a_scoped;
74} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090075 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
76 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
78 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
79 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +090080};
81
82struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090083 int e_af;
84 int e_socktype;
85 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090086 int e_wild;
87#define WILD_AF(ex) ((ex)->e_wild & 0x01)
88#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
89#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +090090};
91
Bernie Innocenti93a31342018-12-12 00:43:02 +090092const struct explore explore_options[] = {
Ken Chen3270cf52018-11-07 01:20:48 +080093 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
94 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
95 {PF_INET6, SOCK_RAW, ANY, 0x05},
96 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
97 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
98 {PF_INET, SOCK_RAW, ANY, 0x05},
99 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
100 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
101 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
102 {-1, 0, 0, 0},
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 Innocentif12d5bb2018-08-31 14:09:46 +0900272 struct addrinfo* cur;
273 int error = 0;
274 struct addrinfo ai;
275 struct addrinfo ai0;
276 struct addrinfo* pai;
277 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900278
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900279 /* hostname is allowed to be NULL */
280 /* servname is allowed to be NULL */
281 /* hints is allowed to be NULL */
282 assert(res != NULL);
283 assert(netcontext != NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900284 cur = &sentinel;
285 pai = &ai;
286 pai->ai_flags = 0;
287 pai->ai_family = PF_UNSPEC;
288 pai->ai_socktype = ANY;
289 pai->ai_protocol = ANY;
290 pai->ai_addrlen = 0;
291 pai->ai_canonname = NULL;
292 pai->ai_addr = NULL;
293 pai->ai_next = NULL;
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 }
315 *pai = *hints;
316
317 /*
318 * if both socktype/protocol are specified, check if they
319 * are meaningful combination.
320 */
321 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
322 for (ex = explore_options; ex->e_af >= 0; ex++) {
323 if (pai->ai_family != ex->e_af) continue;
324 if (ex->e_socktype == ANY) continue;
325 if (ex->e_protocol == ANY) continue;
326 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
327 error = EAI_BADHINTS;
328 break;
329 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900330 }
Ken Chen3270cf52018-11-07 01:20:48 +0800331 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900332 }
333 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900334
Ken Chen3270cf52018-11-07 01:20:48 +0800335 /*
336 * check for special cases. (1) numeric servname is disallowed if
337 * socktype/protocol are left unspecified. (2) servname is disallowed
338 * for raw and other inet{,6} sockets.
339 */
340 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
341 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
342 ) {
343 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900344
Ken Chen3270cf52018-11-07 01:20:48 +0800345 if (pai->ai_family == PF_UNSPEC) {
346 pai->ai_family = PF_INET6;
347 }
348 error = get_portmatch(pai, servname);
349 if (error) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900350
Ken Chen3270cf52018-11-07 01:20:48 +0800351 *pai = ai0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900352 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900353
Ken Chen3270cf52018-11-07 01:20:48 +0800354 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900355
Ken Chen3270cf52018-11-07 01:20:48 +0800356 /* NULL hostname, or numeric hostname */
357 for (ex = explore_options; ex->e_af >= 0; ex++) {
358 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900359
Ken Chen3270cf52018-11-07 01:20:48 +0800360 /* PF_UNSPEC entries are prepared for DNS queries only */
361 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900362
Ken Chen3270cf52018-11-07 01:20:48 +0800363 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
364 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
365 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900366
Ken Chen3270cf52018-11-07 01:20:48 +0800367 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
368 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
369 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
370
371 if (hostname == NULL)
372 error = explore_null(pai, servname, &cur->ai_next);
373 else
374 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
375
376 if (error) break;
377
378 while (cur->ai_next) cur = cur->ai_next;
379 }
380 if (error) break;
381
382 /*
383 * XXX
384 * If numeric representation of AF1 can be interpreted as FQDN
385 * representation of AF2, we need to think again about the code below.
386 */
387 if (sentinel.ai_next) break;
388
389 if (hostname == NULL) {
390 error = EAI_NODATA;
391 break;
392 }
393 if (pai->ai_flags & AI_NUMERICHOST) {
394 error = EAI_NONAME;
395 break;
396 }
397
398 /*
399 * hostname as alphabetical name.
400 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
401 * outer loop by AFs.
402 */
403 for (ex = explore_options; ex->e_af >= 0; ex++) {
404 *pai = ai0;
405
406 /* require exact match for family field */
407 if (pai->ai_family != ex->e_af) continue;
408
409 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
410 continue;
411 }
412 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
413 continue;
414 }
415
416 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
417 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
418
419 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
420
421 while (cur->ai_next) cur = cur->ai_next;
422 }
423
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900424 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800425 error = 0;
426 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900427 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800428 }
429 } while (0);
430
431 if (error) {
432 freeaddrinfo(sentinel.ai_next);
433 *res = NULL;
434 } else {
435 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900436 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900437 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900438}
439
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900440// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
442 struct addrinfo** res, const struct android_net_context* netcontext) {
443 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900444 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900445
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900446 assert(pai != NULL);
447 /* hostname may be NULL */
448 /* servname may be NULL */
449 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900450
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900451 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900452
Bernie Innocenti948f6572018-09-12 21:32:42 +0900453 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900455
Bernie Innocenti948f6572018-09-12 21:32:42 +0900456 if (!files_getaddrinfo(hostname, pai, &result)) {
457 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900458 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900459 if (!error) {
460 struct addrinfo* cur;
461 for (cur = result; cur; cur = cur->ai_next) {
462 GET_PORT(cur, servname);
463 /* canonname should be filled already */
464 }
465 *res = result;
466 return 0;
467 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900468
469free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900470 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900471 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900472}
473
474/*
475 * hostname == NULL.
476 * passive socket -> anyaddr (0.0.0.0 or ::)
477 * non-passive socket -> localhost (127.0.0.1 or ::1)
478 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
480 int s;
481 const struct afd* afd;
482 struct addrinfo* cur;
483 struct addrinfo sentinel;
484 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900485
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900486 assert(pai != NULL);
487 /* servname may be NULL */
488 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 *res = NULL;
491 sentinel.ai_next = NULL;
492 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900493
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 /*
495 * filter out AFs that are not supported by the kernel
496 * XXX errno?
497 */
498 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
499 if (s < 0) {
500 if (errno != EMFILE) return 0;
501 } else
502 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900503
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900504 /*
505 * if the servname does not match socktype/protocol, ignore it.
506 */
507 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900508
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509 afd = find_afd(pai->ai_family);
510 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900511
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900512 if (pai->ai_flags & AI_PASSIVE) {
513 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 GET_PORT(cur->ai_next, servname);
515 } else {
516 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900517 GET_PORT(cur->ai_next, servname);
518 }
519 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900520
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521 *res = sentinel.ai_next;
522 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900523
524free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900525 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900526 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900527}
528
529/*
530 * numeric hostname
531 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900532static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
533 struct addrinfo** res, const char* canonname) {
534 const struct afd* afd;
535 struct addrinfo* cur;
536 struct addrinfo sentinel;
537 int error;
538 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900539
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900540 assert(pai != NULL);
541 /* hostname may be NULL */
542 /* servname may be NULL */
543 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900544
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900545 *res = NULL;
546 sentinel.ai_next = NULL;
547 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900548
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 /*
550 * if the servname does not match socktype/protocol, ignore it.
551 */
552 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900553
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900554 afd = find_afd(pai->ai_family);
555 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900556
Ken Chen15c805a2018-10-17 00:19:59 +0800557 if (inet_pton(afd->a_af, hostname, pton) == 1) {
558 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
559 GET_AI(cur->ai_next, afd, pton);
560 GET_PORT(cur->ai_next, servname);
561 if ((pai->ai_flags & AI_CANONNAME)) {
562 /*
563 * Set the numeric address itself as
564 * the canonical name, based on a
565 * clarification in rfc2553bis-03.
566 */
Ken Chen3270cf52018-11-07 01:20:48 +0800567 error = get_canonname(pai, cur->ai_next, canonname);
568 if (error != 0) {
569 freeaddrinfo(sentinel.ai_next);
570 return error;
571 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900572 }
Ken Chen15c805a2018-10-17 00:19:59 +0800573 while (cur->ai_next) cur = cur->ai_next;
574 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800575 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900576 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900577
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 *res = sentinel.ai_next;
579 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900580
581free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900582 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900583 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900584}
585
586/*
587 * numeric hostname with scope
588 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900589static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
590 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 const struct afd* afd;
592 struct addrinfo* cur;
593 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900594 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900595 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 assert(pai != NULL);
598 /* hostname may be NULL */
599 /* servname may be NULL */
600 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900601
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900602 /*
603 * if the servname does not match socktype/protocol, ignore it.
604 */
605 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900606
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900607 afd = find_afd(pai->ai_family);
608 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900609
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900611
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900612 cp = strchr(hostname, SCOPE_DELIMITER);
613 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 /*
616 * Handle special case of <scoped_address><delimiter><scope id>
617 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900618 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 if (hostname2 == NULL) return EAI_MEMORY;
620 /* terminate at the delimiter */
621 hostname2[cp - hostname] = '\0';
622 addr = hostname2;
623 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900624
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 error = explore_numeric(pai, addr, servname, res, hostname);
626 if (error == 0) {
627 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900628
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629 for (cur = *res; cur; cur = cur->ai_next) {
630 if (cur->ai_family != AF_INET6) continue;
631 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
632 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
633 free(hostname2);
634 return (EAI_NODATA); /* XXX: is return OK? */
635 }
636 sin6->sin6_scope_id = scopeid;
637 }
638 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900639
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900640 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900641
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900642 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900643}
644
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900645static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
646 assert(pai != NULL);
647 assert(ai != NULL);
648 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900649
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900650 if ((pai->ai_flags & AI_CANONNAME) != 0) {
651 ai->ai_canonname = strdup(str);
652 if (ai->ai_canonname == NULL) return EAI_MEMORY;
653 }
654 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900655}
656
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900657static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
658 const char* addr) {
659 char* p;
660 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900661
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900662 assert(pai != NULL);
663 assert(afd != NULL);
664 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900665
nuccachene21023a2018-09-11 11:13:44 +0800666 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669 memcpy(ai, pai, sizeof(struct addrinfo));
670 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800671 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900674 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
675 p = (char*) (void*) (ai->ai_addr);
676 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
677 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900678}
679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680static int get_portmatch(const struct addrinfo* ai, const char* servname) {
681 assert(ai != NULL);
682 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900683
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900684 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900685}
686
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
688 const char* proto;
689 struct servent* sp;
690 int port;
691 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900693 assert(ai != NULL);
694 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900695
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900696 if (servname == NULL) return 0;
697 switch (ai->ai_family) {
698 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900699 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 break;
701 default:
702 return 0;
703 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900704
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900705 switch (ai->ai_socktype) {
706 case SOCK_RAW:
707 return EAI_SERVICE;
708 case SOCK_DGRAM:
709 case SOCK_STREAM:
710 allownumeric = 1;
711 break;
712 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900714 break;
715 default:
716 return EAI_SOCKTYPE;
717 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900718
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719 port = str2number(servname);
720 if (port >= 0) {
721 if (!allownumeric) return EAI_SERVICE;
722 if (port < 0 || port > 65535) return EAI_SERVICE;
723 port = htons(port);
724 } else {
725 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900727 switch (ai->ai_socktype) {
728 case SOCK_DGRAM:
729 proto = "udp";
730 break;
731 case SOCK_STREAM:
732 proto = "tcp";
733 break;
734 default:
735 proto = NULL;
736 break;
737 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900739 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
740 port = sp->s_port;
741 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900743 if (!matchonly) {
744 switch (ai->ai_family) {
745 case AF_INET:
746 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
747 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900748 case AF_INET6:
749 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
750 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751 }
752 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900753
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900754 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900755}
756
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900757static const struct afd* find_afd(int af) {
758 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 if (af == PF_UNSPEC) return NULL;
761 for (afd = afdl; afd->a_af; afd++) {
762 if (afd->a_af == af) return afd;
763 }
764 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900765}
766
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900767// Convert a string to a scope identifier.
768static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900769 u_long lscopeid;
770 struct in6_addr* a6;
771 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 assert(scope != NULL);
774 assert(sin6 != NULL);
775 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900778
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900779 /* empty scopeid portion is invalid */
780 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900781
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900782 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
783 /*
784 * We currently assume a one-to-one mapping between links
785 * and interfaces, so we simply use interface indices for
786 * like-local scopes.
787 */
788 *scopeid = if_nametoindex(scope);
789 if (*scopeid == 0) goto trynumeric;
790 return 0;
791 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900792
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900793 /* still unclear about literal, allow numeric only - placeholder */
794 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
795 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
796 goto trynumeric;
797 else
798 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900799
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900800 /* try to convert to a numeric id as a last resort */
801trynumeric:
802 errno = 0;
803 lscopeid = strtoul(scope, &ep, 10);
804 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
805 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
806 return 0;
807 else
808 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900809}
Bernie Innocenti55864192018-08-30 04:05:20 +0900810
811/* code duplicate with gethnamaddr.c */
812
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900813static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815#define BOUNDED_INCR(x) \
816 do { \
817 BOUNDS_CHECK(cp, x); \
818 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900819 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900820
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900821#define BOUNDS_CHECK(ptr, count) \
822 do { \
823 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800824 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825 return NULL; \
826 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900827 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900828
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800830 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800831 struct addrinfo sentinel = {};
832 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900833 struct addrinfo ai;
834 const struct afd* afd;
835 char* canonname;
836 const HEADER* hp;
837 const u_char* cp;
838 int n;
839 const u_char* eom;
840 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900841 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900842 int haveanswer, had_error;
843 char tbuf[MAXDNAME];
844 int (*name_ok)(const char*);
845 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900846
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 assert(answer != NULL);
848 assert(qname != NULL);
849 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900850
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 canonname = NULL;
854 eom = answer->buf + anslen;
855 switch (qtype) {
856 case T_A:
857 case T_AAAA:
858 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
859 name_ok = res_hnok;
860 break;
861 default:
862 return NULL; /* XXX should be abort(); */
863 }
864 /*
865 * find first satisfactory answer
866 */
867 hp = &answer->hdr;
868 ancount = ntohs(hp->ancount);
869 qdcount = ntohs(hp->qdcount);
870 bp = hostbuf;
871 ep = hostbuf + sizeof hostbuf;
872 cp = answer->buf;
873 BOUNDED_INCR(HFIXEDSZ);
874 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800875 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 return (NULL);
877 }
878 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
879 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800880 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881 return (NULL);
882 }
883 BOUNDED_INCR(n + QFIXEDSZ);
884 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
885 /* res_send() has already verified that the query name is the
886 * same as the one we sent; this just gets the expanded name
887 * (i.e., with the succeeding search-domain tacked on).
888 */
889 n = strlen(bp) + 1; /* for the \0 */
890 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800891 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900892 return (NULL);
893 }
894 canonname = bp;
895 bp += n;
896 /* The qname can be abbreviated, but h_name is now absolute. */
897 qname = canonname;
898 }
899 haveanswer = 0;
900 had_error = 0;
901 while (ancount-- > 0 && cp < eom && !had_error) {
902 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
903 if ((n < 0) || !(*name_ok)(bp)) {
904 had_error++;
905 continue;
906 }
907 cp += n; /* name */
908 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900909 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900911 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900913 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 cp += INT16SZ; /* len */
915 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900916 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900917 /* XXX - debug? syslog? */
918 cp += n;
919 continue; /* XXX - had_error++ ? */
920 }
921 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
922 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
923 if ((n < 0) || !(*name_ok)(tbuf)) {
924 had_error++;
925 continue;
926 }
927 cp += n;
928 /* Get canonical name. */
929 n = strlen(tbuf) + 1; /* for the \0 */
930 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
931 had_error++;
932 continue;
933 }
934 strlcpy(bp, tbuf, (size_t)(ep - bp));
935 canonname = bp;
936 bp += n;
937 continue;
938 }
939 if (qtype == T_ANY) {
940 if (!(type == T_A || type == T_AAAA)) {
941 cp += n;
942 continue;
943 }
944 } else if (type != qtype) {
945 if (type != T_KEY && type != T_SIG)
946 syslog(LOG_NOTICE | LOG_AUTH,
947 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
948 p_class(C_IN), p_type(qtype), p_type(type));
949 cp += n;
950 continue; /* XXX - had_error++ ? */
951 }
952 switch (type) {
953 case T_A:
954 case T_AAAA:
955 if (strcasecmp(canonname, bp) != 0) {
956 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
957 cp += n;
958 continue; /* XXX - had_error++ ? */
959 }
960 if (type == T_A && n != INADDRSZ) {
961 cp += n;
962 continue;
963 }
964 if (type == T_AAAA && n != IN6ADDRSZ) {
965 cp += n;
966 continue;
967 }
968 if (type == T_AAAA) {
969 struct in6_addr in6;
970 memcpy(&in6, cp, IN6ADDRSZ);
971 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
972 cp += n;
973 continue;
974 }
975 }
976 if (!haveanswer) {
977 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900978
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900979 canonname = bp;
980 nn = strlen(bp) + 1; /* for the \0 */
981 bp += nn;
982 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900983
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900984 /* don't overwrite pai */
985 ai = *pai;
986 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
987 afd = find_afd(ai.ai_family);
988 if (afd == NULL) {
989 cp += n;
990 continue;
991 }
992 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
993 if (cur->ai_next == NULL) had_error++;
994 while (cur && cur->ai_next) cur = cur->ai_next;
995 cp += n;
996 break;
997 default:
998 abort();
999 }
1000 if (!had_error) haveanswer++;
1001 }
1002 if (haveanswer) {
1003 if (!canonname)
1004 (void) get_canonname(pai, sentinel.ai_next, qname);
1005 else
1006 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +08001007 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001008 return sentinel.ai_next;
1009 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001010
Hungming Chend57ade02018-12-25 15:47:47 +08001011 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001012 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001013}
1014
1015struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001016 struct addrinfo* ai;
1017 int has_src_addr;
1018 sockaddr_union src_addr;
1019 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001020};
1021
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001022static int _get_scope(const struct sockaddr* addr) {
1023 if (addr->sa_family == AF_INET6) {
1024 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1025 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1026 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1027 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1028 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1029 /*
1030 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1031 * link-local scope.
1032 */
1033 return IPV6_ADDR_SCOPE_LINKLOCAL;
1034 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1035 return IPV6_ADDR_SCOPE_SITELOCAL;
1036 } else {
1037 return IPV6_ADDR_SCOPE_GLOBAL;
1038 }
1039 } else if (addr->sa_family == AF_INET) {
1040 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1041 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001042
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001043 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1044 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1045 return IPV6_ADDR_SCOPE_LINKLOCAL;
1046 } else {
1047 /*
1048 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1049 * and shared addresses (100.64.0.0/10), are assigned global scope.
1050 */
1051 return IPV6_ADDR_SCOPE_GLOBAL;
1052 }
1053 } else {
1054 /*
1055 * This should never happen.
1056 * Return a scope with low priority as a last resort.
1057 */
1058 return IPV6_ADDR_SCOPE_NODELOCAL;
1059 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001060}
1061
1062/* These macros are modelled after the ones in <netinet/in6.h>. */
1063
1064/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001065#define IN6_IS_ADDR_TEREDO(a) \
1066 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001067
1068/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001069#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001070
1071/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001072#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001073
1074/*
1075 * Get the label for a given IPv4/IPv6 address.
1076 * RFC 6724, section 2.1.
1077 */
1078
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001079static int _get_label(const struct sockaddr* addr) {
1080 if (addr->sa_family == AF_INET) {
1081 return 4;
1082 } else if (addr->sa_family == AF_INET6) {
1083 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1084 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1085 return 0;
1086 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1087 return 4;
1088 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1089 return 2;
1090 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1091 return 5;
1092 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1093 return 13;
1094 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1095 return 3;
1096 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1097 return 11;
1098 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1099 return 12;
1100 } else {
1101 /* All other IPv6 addresses, including global unicast addresses. */
1102 return 1;
1103 }
1104 } else {
1105 /*
1106 * This should never happen.
1107 * Return a semi-random label as a last resort.
1108 */
1109 return 1;
1110 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001111}
1112
1113/*
1114 * Get the precedence for a given IPv4/IPv6 address.
1115 * RFC 6724, section 2.1.
1116 */
1117
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001118static int _get_precedence(const struct sockaddr* addr) {
1119 if (addr->sa_family == AF_INET) {
1120 return 35;
1121 } else if (addr->sa_family == AF_INET6) {
1122 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1123 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1124 return 50;
1125 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1126 return 35;
1127 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1128 return 30;
1129 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1130 return 5;
1131 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1132 return 3;
1133 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1134 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1135 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1136 return 1;
1137 } else {
1138 /* All other IPv6 addresses, including global unicast addresses. */
1139 return 40;
1140 }
1141 } else {
1142 return 1;
1143 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001144}
1145
1146/*
1147 * Find number of matching initial bits between the two addresses a1 and a2.
1148 */
1149
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001150static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1151 const char* p1 = (const char*) a1;
1152 const char* p2 = (const char*) a2;
1153 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001154
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001155 for (i = 0; i < sizeof(*a1); ++i) {
1156 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001157
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001158 if (p1[i] == p2[i]) {
1159 continue;
1160 }
1161 x = p1[i] ^ p2[i];
1162 for (j = 0; j < CHAR_BIT; ++j) {
1163 if (x & (1 << (CHAR_BIT - 1))) {
1164 return i * CHAR_BIT + j;
1165 }
1166 x <<= 1;
1167 }
1168 }
1169 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001170}
1171
1172/*
1173 * Compare two source/destination address pairs.
1174 * RFC 6724, section 6.
1175 */
1176
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001177static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1178 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1179 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1180 int scope_src1, scope_dst1, scope_match1;
1181 int scope_src2, scope_dst2, scope_match2;
1182 int label_src1, label_dst1, label_match1;
1183 int label_src2, label_dst2, label_match2;
1184 int precedence1, precedence2;
1185 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001187 /* Rule 1: Avoid unusable destinations. */
1188 if (a1->has_src_addr != a2->has_src_addr) {
1189 return a2->has_src_addr - a1->has_src_addr;
1190 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001192 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001193 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001194 scope_dst1 = _get_scope(a1->ai->ai_addr);
1195 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001196
nuccachene172a4e2018-10-23 17:10:58 +08001197 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001198 scope_dst2 = _get_scope(a2->ai->ai_addr);
1199 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001201 if (scope_match1 != scope_match2) {
1202 return scope_match2 - scope_match1;
1203 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001204
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001205 /*
1206 * Rule 3: Avoid deprecated addresses.
1207 * TODO(sesse): We don't currently have a good way of finding this.
1208 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001209
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001210 /*
1211 * Rule 4: Prefer home addresses.
1212 * TODO(sesse): We don't currently have a good way of finding this.
1213 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001215 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001216 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001217 label_dst1 = _get_label(a1->ai->ai_addr);
1218 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001219
nuccachene172a4e2018-10-23 17:10:58 +08001220 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001221 label_dst2 = _get_label(a2->ai->ai_addr);
1222 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001223
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001224 if (label_match1 != label_match2) {
1225 return label_match2 - label_match1;
1226 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001228 /* Rule 6: Prefer higher precedence. */
1229 precedence1 = _get_precedence(a1->ai->ai_addr);
1230 precedence2 = _get_precedence(a2->ai->ai_addr);
1231 if (precedence1 != precedence2) {
1232 return precedence2 - precedence1;
1233 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001235 /*
1236 * Rule 7: Prefer native transport.
1237 * TODO(sesse): We don't currently have a good way of finding this.
1238 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001239
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001240 /* Rule 8: Prefer smaller scope. */
1241 if (scope_dst1 != scope_dst2) {
1242 return scope_dst1 - scope_dst2;
1243 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001244
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001245 /*
1246 * Rule 9: Use longest matching prefix.
1247 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1248 * to work very well directly applied to IPv4. (glibc uses information from
1249 * the routing table for a custom IPv4 implementation here.)
1250 */
1251 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1252 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001253 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001254 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001255 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001256 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1257 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1258 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1259 if (prefixlen1 != prefixlen2) {
1260 return prefixlen2 - prefixlen1;
1261 }
1262 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001263
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001264 /*
1265 * Rule 10: Leave the order unchanged.
1266 * We need this since qsort() is not necessarily stable.
1267 */
1268 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001269}
1270
1271/*
1272 * Find the source address that will be used if trying to connect to the given
1273 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1274 *
1275 * Returns 1 if a source address was found, 0 if the address is unreachable,
1276 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1277 * undefined.
1278 */
1279
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001280static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1281 uid_t uid) {
1282 int sock;
1283 int ret;
1284 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001286 switch (addr->sa_family) {
1287 case AF_INET:
1288 len = sizeof(struct sockaddr_in);
1289 break;
1290 case AF_INET6:
1291 len = sizeof(struct sockaddr_in6);
1292 break;
1293 default:
1294 /* No known usable source address for non-INET families. */
1295 return 0;
1296 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001297
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001298 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1299 if (sock == -1) {
1300 if (errno == EAFNOSUPPORT) {
1301 return 0;
1302 } else {
1303 return -1;
1304 }
1305 }
1306 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1307 close(sock);
1308 return 0;
1309 }
1310 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1311 close(sock);
1312 return 0;
1313 }
1314 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001315 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001316 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001317
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001318 if (ret == -1) {
1319 close(sock);
1320 return 0;
1321 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001322
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001323 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1324 close(sock);
1325 return -1;
1326 }
1327 close(sock);
1328 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001329}
1330
1331/*
1332 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1333 * Will leave the list unchanged if an error occurs.
1334 */
1335
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001336static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1337 struct addrinfo* cur;
1338 int nelem = 0, i;
1339 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001340
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001341 cur = list_sentinel->ai_next;
1342 while (cur) {
1343 ++nelem;
1344 cur = cur->ai_next;
1345 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001346
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001347 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1348 if (elems == NULL) {
1349 goto error;
1350 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001351
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001352 /*
1353 * Convert the linked list to an array that also contains the candidate
1354 * source address for each destination address.
1355 */
1356 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1357 int has_src_addr;
1358 assert(cur != NULL);
1359 elems[i].ai = cur;
1360 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001361
nuccachene172a4e2018-10-23 17:10:58 +08001362 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001363 if (has_src_addr == -1) {
1364 goto error;
1365 }
1366 elems[i].has_src_addr = has_src_addr;
1367 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001368
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001369 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1370 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001371
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001372 list_sentinel->ai_next = elems[0].ai;
1373 for (i = 0; i < nelem - 1; ++i) {
1374 elems[i].ai->ai_next = elems[i + 1].ai;
1375 }
1376 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001377
1378error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001379 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001380}
1381
Bernie Innocenti948f6572018-09-12 21:32:42 +09001382static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1383 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001384 struct addrinfo *ai, *cur;
1385 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001386 struct res_target q, q2;
1387 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001388
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001389 memset(&q, 0, sizeof(q));
1390 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001391 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001392
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001393 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001394 if (buf == NULL) {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001395 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001396 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001397 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001398 if (buf2 == NULL) {
1399 free(buf);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001400 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001401 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001402
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001403 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001404 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001405 /* prefer IPv6 */
1406 q.name = name;
1407 q.qclass = C_IN;
1408 q.answer = buf->buf;
1409 q.anslen = sizeof(buf->buf);
1410 int query_ipv6 = 1, query_ipv4 = 1;
1411 if (pai->ai_flags & AI_ADDRCONFIG) {
1412 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1413 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1414 }
1415 if (query_ipv6) {
1416 q.qtype = T_AAAA;
1417 if (query_ipv4) {
1418 q.next = &q2;
1419 q2.name = name;
1420 q2.qclass = C_IN;
1421 q2.qtype = T_A;
1422 q2.answer = buf2->buf;
1423 q2.anslen = sizeof(buf2->buf);
1424 }
1425 } else if (query_ipv4) {
1426 q.qtype = T_A;
1427 } else {
1428 free(buf);
1429 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001430 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001431 }
1432 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001433 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001434 case AF_INET:
1435 q.name = name;
1436 q.qclass = C_IN;
1437 q.qtype = T_A;
1438 q.answer = buf->buf;
1439 q.anslen = sizeof(buf->buf);
1440 break;
1441 case AF_INET6:
1442 q.name = name;
1443 q.qclass = C_IN;
1444 q.qtype = T_AAAA;
1445 q.answer = buf->buf;
1446 q.anslen = sizeof(buf->buf);
1447 break;
1448 default:
1449 free(buf);
1450 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001451 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001452 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001453
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001454 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001455 if (res == NULL) {
1456 free(buf);
1457 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001458 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001459 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001460
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001461 /* this just sets our netid val in the thread private data so we don't have to
1462 * modify the api's all the way down to res_send.c's res_nsend. We could
1463 * fully populate the thread private data here, but if we get down there
1464 * and have a cache hit that would be wasted, so we do the rest there on miss
1465 */
1466 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001467
Hungming Chend57ade02018-12-25 15:47:47 +08001468 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +08001469 if (res_searchN(name, &q, res, &herrno) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001470 free(buf);
1471 free(buf2);
Hungming Chen7f0d3292018-12-27 18:33:19 +08001472 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
1473 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001474 }
Hungming Chend57ade02018-12-25 15:47:47 +08001475 ai = getanswer(buf, q.n, q.name, q.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001476 if (ai) {
1477 cur->ai_next = ai;
1478 while (cur && cur->ai_next) cur = cur->ai_next;
1479 }
1480 if (q.next) {
Hungming Chend57ade02018-12-25 15:47:47 +08001481 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001482 if (ai) cur->ai_next = ai;
1483 }
1484 free(buf);
1485 free(buf2);
1486 if (sentinel.ai_next == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001487 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001488 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001490 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001491
Bernie Innocenti948f6572018-09-12 21:32:42 +09001492 *rv = sentinel.ai_next;
1493 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001494}
1495
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001496static void _sethtent(FILE** hostf) {
1497 if (!*hostf)
1498 *hostf = fopen(_PATH_HOSTS, "re");
1499 else
1500 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001501}
1502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001503static void _endhtent(FILE** hostf) {
1504 if (*hostf) {
1505 (void) fclose(*hostf);
1506 *hostf = NULL;
1507 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001508}
1509
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001510static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1511 char* p;
1512 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001513 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001514 int error;
1515 const char* addr;
1516 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001517
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001518 assert(name != NULL);
1519 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001520
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001521 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1522again:
1523 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1524 if (*p == '#') goto again;
1525 if (!(cp = strpbrk(p, "#\n"))) goto again;
1526 *cp = '\0';
1527 if (!(cp = strpbrk(p, " \t"))) goto again;
1528 *cp++ = '\0';
1529 addr = p;
1530 /* if this is not something we're looking for, skip it. */
1531 cname = NULL;
1532 while (cp && *cp) {
1533 if (*cp == ' ' || *cp == '\t') {
1534 cp++;
1535 continue;
1536 }
1537 if (!cname) cname = cp;
1538 tname = cp;
1539 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1540 // fprintf(stderr, "\ttname = '%s'", tname);
1541 if (strcasecmp(name, tname) == 0) goto found;
1542 }
1543 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001544
1545found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001546 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001547 if (error) goto again;
1548 for (res = res0; res; res = res->ai_next) {
1549 /* cover it up */
1550 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001551
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001552 if (pai->ai_flags & AI_CANONNAME) {
1553 if (get_canonname(pai, res, cname) != 0) {
1554 freeaddrinfo(res0);
1555 goto again;
1556 }
1557 }
1558 }
1559 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001560}
1561
Bernie Innocenti948f6572018-09-12 21:32:42 +09001562static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001563 struct addrinfo sentinel = {};
1564 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001565 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001567 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001568
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001569 _sethtent(&hostf);
1570 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1571 cur->ai_next = p;
1572 while (cur && cur->ai_next) cur = cur->ai_next;
1573 }
1574 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001575
Bernie Innocenti948f6572018-09-12 21:32:42 +09001576 *res = sentinel.ai_next;
1577 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001578}
1579
1580/* resolver logic */
1581
1582/*
1583 * Formulate a normal query, send, and await answer.
1584 * Returned answer is placed in supplied buffer "answer".
1585 * Perform preliminary check of answer, returning success only
1586 * if no error is indicated and the answer count is nonzero.
1587 * Return the size of the response on success, -1 on error.
Hungming Chend57ade02018-12-25 15:47:47 +08001588 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001589 *
1590 * Caller must parse answer and determine whether it answers the question.
1591 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001592static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001593 u_char buf[MAXPACKET];
1594 HEADER* hp;
1595 int n;
1596 struct res_target* t;
1597 int rcode;
1598 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001599
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001600 assert(name != NULL);
1601 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001602
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001603 rcode = NOERROR;
1604 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001607 u_char* answer;
1608 int anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001609
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001610 hp = (HEADER*) (void*) t->answer;
Ken Chenbfd32022019-01-02 14:59:38 +08001611 bool retried = false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001612 again:
1613 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001615 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001616 int cl = t->qclass;
1617 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001618 answer = t->answer;
1619 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001620#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001621 if (res->options & RES_DEBUG) printf(";; res_queryN(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001622#endif
1623
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001624 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Ken Chenbfd32022019-01-02 14:59:38 +08001625 if (n > 0 && (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && !retried)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001626 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001627 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001628#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001629 if (res->options & RES_DEBUG) printf(";; res_queryN: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001630#endif
Hungming Chend57ade02018-12-25 15:47:47 +08001631 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001632 return n;
1633 }
Mike Yu69615f62018-11-06 15:42:36 +08001634
Luke Huang952d0942018-12-26 16:53:03 +08001635 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001636 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001637 // Record rcode from DNS response header only if no timeout.
1638 // Keep rcode timeout for reporting later if any.
1639 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001640 /* if the query choked with EDNS0, retry without EDNS0 */
1641 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
Ken Chenbfd32022019-01-02 14:59:38 +08001642 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001643#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001644 if (res->options & RES_DEBUG) printf(";; res_queryN: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001645#endif
Ken Chenbfd32022019-01-02 14:59:38 +08001646 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001647 goto again;
1648 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001649#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001650 if (res->options & RES_DEBUG)
1651 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001652#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001653 continue;
1654 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001656 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001658 t->n = n;
1659 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001661 if (ancount == 0) {
1662 switch (rcode) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001663 // Not defined in RFC.
1664 case RCODE_TIMEOUT:
1665 // DNS metrics monitors DNS query timeout.
1666 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1667 break;
1668 // Defined in RFC 1035 section 4.1.1.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001669 case NXDOMAIN:
Hungming Chend57ade02018-12-25 15:47:47 +08001670 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001671 break;
1672 case SERVFAIL:
Hungming Chend57ade02018-12-25 15:47:47 +08001673 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001674 break;
1675 case NOERROR:
Hungming Chend57ade02018-12-25 15:47:47 +08001676 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001677 break;
1678 case FORMERR:
1679 case NOTIMP:
1680 case REFUSED:
1681 default:
Hungming Chend57ade02018-12-25 15:47:47 +08001682 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001683 break;
1684 }
1685 return -1;
1686 }
1687 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001688}
1689
1690/*
1691 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1692 * Return the size of the response on success, -1 on error.
1693 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chend57ade02018-12-25 15:47:47 +08001694 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001695 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001696static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001697 const char *cp, *const *domain;
1698 HEADER* hp;
1699 u_int dots;
1700 int trailing_dot, ret, saved_herrno;
1701 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001703 assert(name != NULL);
1704 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001705
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001706 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001708 errno = 0;
Hungming Chend57ade02018-12-25 15:47:47 +08001709 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001710 dots = 0;
1711 for (cp = name; *cp; cp++) dots += (*cp == '.');
1712 trailing_dot = 0;
1713 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001715 /*
1716 * If there are dots in the name already, let's just give it a try
1717 * 'as is'. The threshold can be set with the "ndots" option.
1718 */
1719 saved_herrno = -1;
1720 if (dots >= res->ndots) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001721 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001722 if (ret > 0) return (ret);
Hungming Chend57ade02018-12-25 15:47:47 +08001723 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001724 tried_as_is++;
1725 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001727 /*
1728 * We do at least one level of search if
1729 * - there is no dot and RES_DEFNAME is set, or
1730 * - there is at least one dot, there is no trailing dot,
1731 * and RES_DNSRCH is set.
1732 */
1733 if ((!dots && (res->options & RES_DEFNAMES)) ||
1734 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1735 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001737 /* Unfortunately we need to set stuff up before
1738 * the domain stuff is tried. Will have a better
1739 * fix after thread pools are used.
1740 */
1741 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001743 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001744 ret = res_querydomainN(name, *domain, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001745 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001746
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001747 /*
1748 * If no server present, give up.
1749 * If name isn't found in this domain,
1750 * keep trying higher domains in the search list
1751 * (if that's enabled).
1752 * On a NO_DATA error, keep trying, otherwise
1753 * a wildcard entry of another type could keep us
1754 * from finding this entry higher in the domain.
1755 * If we get some other error (negative answer or
1756 * server failure), then stop searching up,
1757 * but try the input name below in case it's
1758 * fully-qualified.
1759 */
1760 if (errno == ECONNREFUSED) {
Hungming Chend57ade02018-12-25 15:47:47 +08001761 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001762 return -1;
1763 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001764
Hungming Chend57ade02018-12-25 15:47:47 +08001765 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001766 case NO_DATA:
1767 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001768 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001769 case HOST_NOT_FOUND:
1770 /* keep trying */
1771 break;
1772 case TRY_AGAIN:
1773 if (hp->rcode == SERVFAIL) {
1774 /* try next search element, if any */
1775 got_servfail++;
1776 break;
1777 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001778 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001779 default:
1780 /* anything else implies that we're done */
1781 done++;
1782 }
1783 /*
1784 * if we got here for some reason other than DNSRCH,
1785 * we only wanted one iteration of the loop, so stop.
1786 */
1787 if (!(res->options & RES_DNSRCH)) done++;
1788 }
1789 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001790
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001791 /*
1792 * if we have not already tried the name "as is", do that now.
1793 * note that we do this regardless of how many dots were in the
1794 * name or whether it ends with a dot.
1795 */
1796 if (!tried_as_is) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001797 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001798 if (ret > 0) return ret;
1799 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001801 /*
1802 * if we got here, we didn't satisfy the search.
1803 * if we did an initial full query, return that query's h_errno
1804 * (note that we wouldn't be here if that query had succeeded).
1805 * else if we ever got a nodata, send that back as the reason.
1806 * else send back meaningless h_errno, that being the one from
1807 * the last DNSRCH we did.
1808 */
1809 if (saved_herrno != -1)
Hungming Chend57ade02018-12-25 15:47:47 +08001810 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001811 else if (got_nodata)
Hungming Chend57ade02018-12-25 15:47:47 +08001812 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001813 else if (got_servfail)
Hungming Chend57ade02018-12-25 15:47:47 +08001814 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001815 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001816}
1817
1818/*
1819 * Perform a call on res_query on the concatenation of name and domain,
1820 * removing a trailing dot from name if domain is NULL.
1821 */
Mike Yu69615f62018-11-06 15:42:36 +08001822static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +08001823 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001824 char nbuf[MAXDNAME];
1825 const char* longname = nbuf;
1826 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001828 assert(name != NULL);
1829 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001830
1831#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001832 if (res->options & RES_DEBUG)
1833 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001834#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001835 if (domain == NULL) {
1836 /*
1837 * Check for trailing '.';
1838 * copy without '.' if present.
1839 */
1840 n = strlen(name);
1841 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001842 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001843 return -1;
1844 }
1845 if (n > 0 && name[--n] == '.') {
1846 strncpy(nbuf, name, n);
1847 nbuf[n] = '\0';
1848 } else
1849 longname = name;
1850 } else {
1851 n = strlen(name);
1852 d = strlen(domain);
1853 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001854 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001855 return -1;
1856 }
1857 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1858 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001859 return res_queryN(longname, target, res, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001860}