blob: fc311ba2ecc9984469b86a7e1ac20c23a1a65fc7 [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
33/*
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked.
36 * - Return values. There are nonstandard return values defined and used
37 * in the source code. This is because RFC2553 is silent about which error
38 * code must be returned for which situation.
39 * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
40 * says to use inet_aton() to convert IPv4 numeric to binary (alows
41 * classful form as a result).
42 * current code - disallow classful form for IPv4 (due to use of inet_pton).
Bernie Innocenti55864192018-08-30 04:05:20 +090043 * Note:
44 * - We use getipnodebyname() just for thread-safeness. There's no intent
45 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
46 * getipnodebyname().
47 * - The code filters out AFs that are not supported by the kernel,
48 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
49 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
50 * in ai_flags?
51 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
52 * (1) what should we do against numeric hostname (2) what should we do
53 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
54 * non-loopback address configured? global address configured?
55 * - To avoid search order issue, we have a big amount of code duplicate
56 * from gethnamaddr.c and some other places. The issues that there's no
57 * lower layer function to lookup "IPv4 or IPv6" record. Calling
58 * gethostbyname2 from getaddrinfo will end up in wrong search order, as
59 * follows:
60 * - The code makes use of following calls when asked to resolver with
61 * ai_family = PF_UNSPEC:
62 * getipnodebyname(host, AF_INET6);
63 * getipnodebyname(host, AF_INET);
64 * This will result in the following queries if the node is configure to
65 * prefer /etc/hosts than DNS:
66 * lookup /etc/hosts for IPv6 address
67 * lookup DNS for IPv6 address
68 * lookup /etc/hosts for IPv4 address
69 * lookup DNS for IPv4 address
70 * which may not meet people's requirement.
71 * The right thing to happen is to have underlying layer which does
72 * PF_UNSPEC lookup (lookup both) and return chain of addrinfos.
73 * This would result in a bit of code duplicate with _dns_ghbyname() and
74 * friends.
75 */
76
Bernie Innocenti55864192018-08-30 04:05:20 +090077#include <arpa/inet.h>
78#include <arpa/nameser.h>
79#include <assert.h>
80#include <ctype.h>
81#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#include <fcntl.h>
83#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090084#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090086#include <stdbool.h>
87#include <stddef.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090091#include <sys/param.h>
92#include <sys/socket.h>
93#include <sys/stat.h>
94#include <sys/types.h>
95#include <sys/un.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090096#include <syslog.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090097#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090098
Bernie Innocenti189eb502018-10-01 23:10:18 +090099#include "netd_resolv/resolv.h"
100#include "resolv_cache.h"
101#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900102
Bernie Innocenti55864192018-08-30 04:05:20 +0900103#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900104
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900105static const char in_addrany[] = {0, 0, 0, 0};
106static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900107static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
108static const 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 +0900109
Bernie Innocenti55864192018-08-30 04:05:20 +0900110static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 int a_af;
112 int a_addrlen;
113 int a_socklen;
114 int a_off;
115 const char* a_addrany;
116 const char* a_loopback;
117 int a_scoped;
118} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
120 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
122 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
123 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900124};
125
126struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900127 int e_af;
128 int e_socktype;
129 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900130 int e_wild;
131#define WILD_AF(ex) ((ex)->e_wild & 0x01)
132#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
133#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900134};
135
Ken Chen3270cf52018-11-07 01:20:48 +0800136static const struct explore explore_options[] = {
137 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
138 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
139 {PF_INET6, SOCK_RAW, ANY, 0x05},
140 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
141 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
142 {PF_INET, SOCK_RAW, ANY, 0x05},
143 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
144 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
145 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
146 {-1, 0, 0, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900147};
148
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900149#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900151
152typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 HEADER hdr;
154 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900155} querybuf;
156
157struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158 struct res_target* next;
159 const char* name; /* domain name */
160 int qclass, qtype; /* class and type of query */
161 u_char* answer; /* buffer to put answer */
162 int anslen; /* size of answer buffer */
163 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900164};
165
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166static int str2number(const char*);
167static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
168 const struct android_net_context*);
169static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
170static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
171 const char*);
172static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
173 struct addrinfo**);
174static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
175static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
176static int get_portmatch(const struct addrinfo*, const char*);
177static int get_port(const struct addrinfo*, const char*, int);
178static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900179static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900180
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900181static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900182static int dns_getaddrinfo(const char* name, const addrinfo* pai,
183 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900184static void _sethtent(FILE**);
185static void _endhtent(FILE**);
186static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900187static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900188static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900189
Mike Yu69615f62018-11-06 15:42:36 +0800190static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error);
191static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error);
192static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
193 int* ai_error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900194
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195static const char* const ai_errlist[] = {
196 "Success",
197 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
198 "Temporary failure in name resolution", /* EAI_AGAIN */
199 "Invalid value for ai_flags", /* EAI_BADFLAGS */
200 "Non-recoverable failure in name resolution", /* EAI_FAIL */
201 "ai_family not supported", /* EAI_FAMILY */
202 "Memory allocation failure", /* EAI_MEMORY */
203 "No address associated with hostname", /* EAI_NODATA */
204 "hostname nor servname provided, or not known", /* EAI_NONAME */
205 "servname not supported for ai_socktype", /* EAI_SERVICE */
206 "ai_socktype not supported", /* EAI_SOCKTYPE */
207 "System error returned in errno", /* EAI_SYSTEM */
208 "Invalid value for hints", /* EAI_BADHINTS */
209 "Resolved protocol is unknown", /* EAI_PROTOCOL */
210 "Argument buffer overflow", /* EAI_OVERFLOW */
211 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900212};
213
214/* XXX macros that make external reference is BAD. */
215
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900216#define GET_AI(ai, afd, addr) \
217 do { \
218 /* external reference: pai, error, and label free */ \
219 (ai) = get_ai(pai, (afd), (addr)); \
220 if ((ai) == NULL) { \
221 error = EAI_MEMORY; \
222 goto free; \
223 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900224 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900225
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900226#define GET_PORT(ai, serv) \
227 do { \
228 /* external reference: error and label free */ \
229 error = get_port((ai), (serv), 0); \
230 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900231 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900232
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900234 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
235#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900236
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237const char* gai_strerror(int ecode) {
238 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
239 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900240}
241
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900243 while (ai) {
244 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900245 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900246 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247 free(ai);
248 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900249 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900250}
251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900252static int str2number(const char* p) {
253 char* ep;
254 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900255
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900256 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900257
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900258 if (*p == '\0') return -1;
259 ep = NULL;
260 errno = 0;
261 v = strtoul(p, &ep, 10);
262 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
263 return v;
264 else
265 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900266}
267
268/*
269 * The following functions determine whether IPv4 or IPv6 connectivity is
270 * available in order to implement AI_ADDRCONFIG.
271 *
272 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
273 * available, but whether addresses of the specified family are "configured
274 * on the local system". However, bionic doesn't currently support getifaddrs,
275 * so checking for connectivity is the next best thing.
276 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900277static int _have_ipv6(unsigned mark, uid_t uid) {
278 static const struct sockaddr_in6 sin6_test = {
279 .sin6_family = AF_INET6,
280 .sin6_addr.s6_addr = {// 2000::
281 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800282 sockaddr_union addr = {.sin6 = sin6_test};
283 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900284}
285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900286static int _have_ipv4(unsigned mark, uid_t uid) {
287 static const struct sockaddr_in sin_test = {
288 .sin_family = AF_INET,
289 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
290 };
nuccachene172a4e2018-10-23 17:10:58 +0800291 sockaddr_union addr = {.sin = sin_test};
292 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900293}
294
295bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900296 int32_t tmp;
297 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
298 return false;
299 }
300 *result = ntohl(tmp);
301 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900302}
303
Bernie Innocentic165ce82018-10-16 23:35:28 +0900304// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
305// NOTE: also called by resolv_set_nameservers_for_net().
306int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
307 addrinfo** result) {
308 hints.ai_flags = AI_NUMERICHOST;
309 const android_net_context netcontext = {
310 .app_netid = NETID_UNSET,
311 .app_mark = MARK_UNSET,
312 .dns_netid = NETID_UNSET,
313 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900314 .uid = NET_CONTEXT_INVALID_UID,
315 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900316 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900317}
318
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900319int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
320 const struct addrinfo* hints,
321 const struct android_net_context* netcontext,
322 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800323 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900324 struct addrinfo* cur;
325 int error = 0;
326 struct addrinfo ai;
327 struct addrinfo ai0;
328 struct addrinfo* pai;
329 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900330
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 /* hostname is allowed to be NULL */
332 /* servname is allowed to be NULL */
333 /* hints is allowed to be NULL */
334 assert(res != NULL);
335 assert(netcontext != NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900336 cur = &sentinel;
337 pai = &ai;
338 pai->ai_flags = 0;
339 pai->ai_family = PF_UNSPEC;
340 pai->ai_socktype = ANY;
341 pai->ai_protocol = ANY;
342 pai->ai_addrlen = 0;
343 pai->ai_canonname = NULL;
344 pai->ai_addr = NULL;
345 pai->ai_next = NULL;
Ken Chen3270cf52018-11-07 01:20:48 +0800346 do {
347 if (hostname == NULL && servname == NULL) {
348 error = EAI_NONAME;
349 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900350 }
Ken Chen3270cf52018-11-07 01:20:48 +0800351 if (hints) {
352 /* error check for hints */
353 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
354 error = EAI_BADHINTS;
355 break;
356 }
357 if (hints->ai_flags & ~AI_MASK) {
358 error = EAI_BADFLAGS;
359 break;
360 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900361
Ken Chen3270cf52018-11-07 01:20:48 +0800362 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
363 hints->ai_family == PF_INET6)) {
364 error = EAI_FAMILY;
365 break;
366 }
367 *pai = *hints;
368
369 /*
370 * if both socktype/protocol are specified, check if they
371 * are meaningful combination.
372 */
373 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
374 for (ex = explore_options; ex->e_af >= 0; ex++) {
375 if (pai->ai_family != ex->e_af) continue;
376 if (ex->e_socktype == ANY) continue;
377 if (ex->e_protocol == ANY) continue;
378 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
379 error = EAI_BADHINTS;
380 break;
381 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900382 }
Ken Chen3270cf52018-11-07 01:20:48 +0800383 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900384 }
385 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900386
Ken Chen3270cf52018-11-07 01:20:48 +0800387 /*
388 * check for special cases. (1) numeric servname is disallowed if
389 * socktype/protocol are left unspecified. (2) servname is disallowed
390 * for raw and other inet{,6} sockets.
391 */
392 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
393 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
394 ) {
395 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900396
Ken Chen3270cf52018-11-07 01:20:48 +0800397 if (pai->ai_family == PF_UNSPEC) {
398 pai->ai_family = PF_INET6;
399 }
400 error = get_portmatch(pai, servname);
401 if (error) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900402
Ken Chen3270cf52018-11-07 01:20:48 +0800403 *pai = ai0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900404 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900405
Ken Chen3270cf52018-11-07 01:20:48 +0800406 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900407
Ken Chen3270cf52018-11-07 01:20:48 +0800408 /* NULL hostname, or numeric hostname */
409 for (ex = explore_options; ex->e_af >= 0; ex++) {
410 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900411
Ken Chen3270cf52018-11-07 01:20:48 +0800412 /* PF_UNSPEC entries are prepared for DNS queries only */
413 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900414
Ken Chen3270cf52018-11-07 01:20:48 +0800415 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
416 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
417 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900418
Ken Chen3270cf52018-11-07 01:20:48 +0800419 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
420 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
421 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
422
423 if (hostname == NULL)
424 error = explore_null(pai, servname, &cur->ai_next);
425 else
426 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
427
428 if (error) break;
429
430 while (cur->ai_next) cur = cur->ai_next;
431 }
432 if (error) break;
433
434 /*
435 * XXX
436 * If numeric representation of AF1 can be interpreted as FQDN
437 * representation of AF2, we need to think again about the code below.
438 */
439 if (sentinel.ai_next) break;
440
441 if (hostname == NULL) {
442 error = EAI_NODATA;
443 break;
444 }
445 if (pai->ai_flags & AI_NUMERICHOST) {
446 error = EAI_NONAME;
447 break;
448 }
449
450 /*
451 * hostname as alphabetical name.
452 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
453 * outer loop by AFs.
454 */
455 for (ex = explore_options; ex->e_af >= 0; ex++) {
456 *pai = ai0;
457
458 /* require exact match for family field */
459 if (pai->ai_family != ex->e_af) continue;
460
461 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
462 continue;
463 }
464 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
465 continue;
466 }
467
468 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
469 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
470
471 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
472
473 while (cur->ai_next) cur = cur->ai_next;
474 }
475
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900476 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800477 error = 0;
478 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800480 }
481 } while (0);
482
483 if (error) {
484 freeaddrinfo(sentinel.ai_next);
485 *res = NULL;
486 } else {
487 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900490}
491
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900492// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
494 struct addrinfo** res, const struct android_net_context* netcontext) {
495 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900496 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900497
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 assert(pai != NULL);
499 /* hostname may be NULL */
500 /* servname may be NULL */
501 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900504
Bernie Innocenti948f6572018-09-12 21:32:42 +0900505 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900507
Bernie Innocenti948f6572018-09-12 21:32:42 +0900508 if (!files_getaddrinfo(hostname, pai, &result)) {
509 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900511 if (!error) {
512 struct addrinfo* cur;
513 for (cur = result; cur; cur = cur->ai_next) {
514 GET_PORT(cur, servname);
515 /* canonname should be filled already */
516 }
517 *res = result;
518 return 0;
519 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900520
521free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900522 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900523 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900524}
525
526/*
527 * hostname == NULL.
528 * passive socket -> anyaddr (0.0.0.0 or ::)
529 * non-passive socket -> localhost (127.0.0.1 or ::1)
530 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900531static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
532 int s;
533 const struct afd* afd;
534 struct addrinfo* cur;
535 struct addrinfo sentinel;
536 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900537
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538 assert(pai != NULL);
539 /* servname may be NULL */
540 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900541
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900542 *res = NULL;
543 sentinel.ai_next = NULL;
544 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 /*
547 * filter out AFs that are not supported by the kernel
548 * XXX errno?
549 */
550 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
551 if (s < 0) {
552 if (errno != EMFILE) return 0;
553 } else
554 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900555
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900556 /*
557 * if the servname does not match socktype/protocol, ignore it.
558 */
559 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900560
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900561 afd = find_afd(pai->ai_family);
562 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900563
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900564 if (pai->ai_flags & AI_PASSIVE) {
565 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900566 GET_PORT(cur->ai_next, servname);
567 } else {
568 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900569 GET_PORT(cur->ai_next, servname);
570 }
571 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900572
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573 *res = sentinel.ai_next;
574 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900575
576free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900577 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900579}
580
581/*
582 * numeric hostname
583 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900584static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
585 struct addrinfo** res, const char* canonname) {
586 const struct afd* afd;
587 struct addrinfo* cur;
588 struct addrinfo sentinel;
589 int error;
590 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900591
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900592 assert(pai != NULL);
593 /* hostname may be NULL */
594 /* servname may be NULL */
595 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 *res = NULL;
598 sentinel.ai_next = NULL;
599 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 /*
602 * if the servname does not match socktype/protocol, ignore it.
603 */
604 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 afd = find_afd(pai->ai_family);
607 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900608
Ken Chen15c805a2018-10-17 00:19:59 +0800609 if (inet_pton(afd->a_af, hostname, pton) == 1) {
610 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
611 GET_AI(cur->ai_next, afd, pton);
612 GET_PORT(cur->ai_next, servname);
613 if ((pai->ai_flags & AI_CANONNAME)) {
614 /*
615 * Set the numeric address itself as
616 * the canonical name, based on a
617 * clarification in rfc2553bis-03.
618 */
Ken Chen3270cf52018-11-07 01:20:48 +0800619 error = get_canonname(pai, cur->ai_next, canonname);
620 if (error != 0) {
621 freeaddrinfo(sentinel.ai_next);
622 return error;
623 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624 }
Ken Chen15c805a2018-10-17 00:19:59 +0800625 while (cur->ai_next) cur = cur->ai_next;
626 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800627 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900629
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900630 *res = sentinel.ai_next;
631 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900632
633free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900634 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900635 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900636}
637
638/*
639 * numeric hostname with scope
640 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
642 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900643 const struct afd* afd;
644 struct addrinfo* cur;
645 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900646 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900647 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 assert(pai != NULL);
650 /* hostname may be NULL */
651 /* servname may be NULL */
652 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900654 /*
655 * if the servname does not match socktype/protocol, ignore it.
656 */
657 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900658
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659 afd = find_afd(pai->ai_family);
660 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900661
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900662 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900663
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664 cp = strchr(hostname, SCOPE_DELIMITER);
665 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900666
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667 /*
668 * Handle special case of <scoped_address><delimiter><scope id>
669 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900670 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900671 if (hostname2 == NULL) return EAI_MEMORY;
672 /* terminate at the delimiter */
673 hostname2[cp - hostname] = '\0';
674 addr = hostname2;
675 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 error = explore_numeric(pai, addr, servname, res, hostname);
678 if (error == 0) {
679 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900680
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900681 for (cur = *res; cur; cur = cur->ai_next) {
682 if (cur->ai_family != AF_INET6) continue;
683 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
684 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
685 free(hostname2);
686 return (EAI_NODATA); /* XXX: is return OK? */
687 }
688 sin6->sin6_scope_id = scopeid;
689 }
690 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900695}
696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
698 assert(pai != NULL);
699 assert(ai != NULL);
700 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900701
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900702 if ((pai->ai_flags & AI_CANONNAME) != 0) {
703 ai->ai_canonname = strdup(str);
704 if (ai->ai_canonname == NULL) return EAI_MEMORY;
705 }
706 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900707}
708
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
710 const char* addr) {
711 char* p;
712 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900714 assert(pai != NULL);
715 assert(afd != NULL);
716 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900717
nuccachene21023a2018-09-11 11:13:44 +0800718 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900720
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721 memcpy(ai, pai, sizeof(struct addrinfo));
722 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800723 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900726 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
727 p = (char*) (void*) (ai->ai_addr);
728 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
729 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900730}
731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732static int get_portmatch(const struct addrinfo* ai, const char* servname) {
733 assert(ai != NULL);
734 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900735
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900736 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900737}
738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900739static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
740 const char* proto;
741 struct servent* sp;
742 int port;
743 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900745 assert(ai != NULL);
746 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900747
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900748 if (servname == NULL) return 0;
749 switch (ai->ai_family) {
750 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 break;
753 default:
754 return 0;
755 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900756
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900757 switch (ai->ai_socktype) {
758 case SOCK_RAW:
759 return EAI_SERVICE;
760 case SOCK_DGRAM:
761 case SOCK_STREAM:
762 allownumeric = 1;
763 break;
764 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900766 break;
767 default:
768 return EAI_SOCKTYPE;
769 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 port = str2number(servname);
772 if (port >= 0) {
773 if (!allownumeric) return EAI_SERVICE;
774 if (port < 0 || port > 65535) return EAI_SERVICE;
775 port = htons(port);
776 } else {
777 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900778
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900779 switch (ai->ai_socktype) {
780 case SOCK_DGRAM:
781 proto = "udp";
782 break;
783 case SOCK_STREAM:
784 proto = "tcp";
785 break;
786 default:
787 proto = NULL;
788 break;
789 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900790
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900791 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
792 port = sp->s_port;
793 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900794
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900795 if (!matchonly) {
796 switch (ai->ai_family) {
797 case AF_INET:
798 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
799 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900800 case AF_INET6:
801 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
802 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900803 }
804 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900805
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900806 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900807}
808
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900809static const struct afd* find_afd(int af) {
810 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 if (af == PF_UNSPEC) return NULL;
813 for (afd = afdl; afd->a_af; afd++) {
814 if (afd->a_af == af) return afd;
815 }
816 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817}
818
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900819// Convert a string to a scope identifier.
820static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900821 u_long lscopeid;
822 struct in6_addr* a6;
823 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825 assert(scope != NULL);
826 assert(sin6 != NULL);
827 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900828
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 /* empty scopeid portion is invalid */
832 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900833
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900834 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
835 /*
836 * We currently assume a one-to-one mapping between links
837 * and interfaces, so we simply use interface indices for
838 * like-local scopes.
839 */
840 *scopeid = if_nametoindex(scope);
841 if (*scopeid == 0) goto trynumeric;
842 return 0;
843 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900844
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 /* still unclear about literal, allow numeric only - placeholder */
846 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
847 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
848 goto trynumeric;
849 else
850 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900852 /* try to convert to a numeric id as a last resort */
853trynumeric:
854 errno = 0;
855 lscopeid = strtoul(scope, &ep, 10);
856 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
857 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
858 return 0;
859 else
860 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900861}
Bernie Innocenti55864192018-08-30 04:05:20 +0900862
863/* code duplicate with gethnamaddr.c */
864
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900865static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900866
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900867#define BOUNDED_INCR(x) \
868 do { \
869 BOUNDS_CHECK(cp, x); \
870 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900871 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873#define BOUNDS_CHECK(ptr, count) \
874 do { \
875 if (eom - (ptr) < (count)) { \
876 h_errno = NO_RECOVERY; \
877 return NULL; \
878 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900879 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900880
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
882 const struct addrinfo* pai) {
Ken Chen3270cf52018-11-07 01:20:48 +0800883 struct addrinfo sentinel = {};
884 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900885 struct addrinfo ai;
886 const struct afd* afd;
887 char* canonname;
888 const HEADER* hp;
889 const u_char* cp;
890 int n;
891 const u_char* eom;
892 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900893 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900894 int haveanswer, had_error;
895 char tbuf[MAXDNAME];
896 int (*name_ok)(const char*);
897 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900898
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 assert(answer != NULL);
900 assert(qname != NULL);
901 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900902
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900904
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900905 canonname = NULL;
906 eom = answer->buf + anslen;
907 switch (qtype) {
908 case T_A:
909 case T_AAAA:
910 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
911 name_ok = res_hnok;
912 break;
913 default:
914 return NULL; /* XXX should be abort(); */
915 }
916 /*
917 * find first satisfactory answer
918 */
919 hp = &answer->hdr;
920 ancount = ntohs(hp->ancount);
921 qdcount = ntohs(hp->qdcount);
922 bp = hostbuf;
923 ep = hostbuf + sizeof hostbuf;
924 cp = answer->buf;
925 BOUNDED_INCR(HFIXEDSZ);
926 if (qdcount != 1) {
927 h_errno = NO_RECOVERY;
928 return (NULL);
929 }
930 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
931 if ((n < 0) || !(*name_ok)(bp)) {
932 h_errno = NO_RECOVERY;
933 return (NULL);
934 }
935 BOUNDED_INCR(n + QFIXEDSZ);
936 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
937 /* res_send() has already verified that the query name is the
938 * same as the one we sent; this just gets the expanded name
939 * (i.e., with the succeeding search-domain tacked on).
940 */
941 n = strlen(bp) + 1; /* for the \0 */
942 if (n >= MAXHOSTNAMELEN) {
943 h_errno = NO_RECOVERY;
944 return (NULL);
945 }
946 canonname = bp;
947 bp += n;
948 /* The qname can be abbreviated, but h_name is now absolute. */
949 qname = canonname;
950 }
951 haveanswer = 0;
952 had_error = 0;
953 while (ancount-- > 0 && cp < eom && !had_error) {
954 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
955 if ((n < 0) || !(*name_ok)(bp)) {
956 had_error++;
957 continue;
958 }
959 cp += n; /* name */
960 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900961 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900962 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900963 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900964 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900965 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900966 cp += INT16SZ; /* len */
967 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900968 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900969 /* XXX - debug? syslog? */
970 cp += n;
971 continue; /* XXX - had_error++ ? */
972 }
973 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
974 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
975 if ((n < 0) || !(*name_ok)(tbuf)) {
976 had_error++;
977 continue;
978 }
979 cp += n;
980 /* Get canonical name. */
981 n = strlen(tbuf) + 1; /* for the \0 */
982 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
983 had_error++;
984 continue;
985 }
986 strlcpy(bp, tbuf, (size_t)(ep - bp));
987 canonname = bp;
988 bp += n;
989 continue;
990 }
991 if (qtype == T_ANY) {
992 if (!(type == T_A || type == T_AAAA)) {
993 cp += n;
994 continue;
995 }
996 } else if (type != qtype) {
997 if (type != T_KEY && type != T_SIG)
998 syslog(LOG_NOTICE | LOG_AUTH,
999 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1000 p_class(C_IN), p_type(qtype), p_type(type));
1001 cp += n;
1002 continue; /* XXX - had_error++ ? */
1003 }
1004 switch (type) {
1005 case T_A:
1006 case T_AAAA:
1007 if (strcasecmp(canonname, bp) != 0) {
1008 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1009 cp += n;
1010 continue; /* XXX - had_error++ ? */
1011 }
1012 if (type == T_A && n != INADDRSZ) {
1013 cp += n;
1014 continue;
1015 }
1016 if (type == T_AAAA && n != IN6ADDRSZ) {
1017 cp += n;
1018 continue;
1019 }
1020 if (type == T_AAAA) {
1021 struct in6_addr in6;
1022 memcpy(&in6, cp, IN6ADDRSZ);
1023 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1024 cp += n;
1025 continue;
1026 }
1027 }
1028 if (!haveanswer) {
1029 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001030
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001031 canonname = bp;
1032 nn = strlen(bp) + 1; /* for the \0 */
1033 bp += nn;
1034 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001035
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001036 /* don't overwrite pai */
1037 ai = *pai;
1038 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1039 afd = find_afd(ai.ai_family);
1040 if (afd == NULL) {
1041 cp += n;
1042 continue;
1043 }
1044 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1045 if (cur->ai_next == NULL) had_error++;
1046 while (cur && cur->ai_next) cur = cur->ai_next;
1047 cp += n;
1048 break;
1049 default:
1050 abort();
1051 }
1052 if (!had_error) haveanswer++;
1053 }
1054 if (haveanswer) {
1055 if (!canonname)
1056 (void) get_canonname(pai, sentinel.ai_next, qname);
1057 else
1058 (void) get_canonname(pai, sentinel.ai_next, canonname);
1059 h_errno = NETDB_SUCCESS;
1060 return sentinel.ai_next;
1061 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001062
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001063 h_errno = NO_RECOVERY;
1064 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001065}
1066
1067struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001068 struct addrinfo* ai;
1069 int has_src_addr;
1070 sockaddr_union src_addr;
1071 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001072};
1073
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001074static int _get_scope(const struct sockaddr* addr) {
1075 if (addr->sa_family == AF_INET6) {
1076 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1077 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1078 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1079 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1080 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1081 /*
1082 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1083 * link-local scope.
1084 */
1085 return IPV6_ADDR_SCOPE_LINKLOCAL;
1086 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1087 return IPV6_ADDR_SCOPE_SITELOCAL;
1088 } else {
1089 return IPV6_ADDR_SCOPE_GLOBAL;
1090 }
1091 } else if (addr->sa_family == AF_INET) {
1092 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1093 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001094
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001095 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1096 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1097 return IPV6_ADDR_SCOPE_LINKLOCAL;
1098 } else {
1099 /*
1100 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1101 * and shared addresses (100.64.0.0/10), are assigned global scope.
1102 */
1103 return IPV6_ADDR_SCOPE_GLOBAL;
1104 }
1105 } else {
1106 /*
1107 * This should never happen.
1108 * Return a scope with low priority as a last resort.
1109 */
1110 return IPV6_ADDR_SCOPE_NODELOCAL;
1111 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001112}
1113
1114/* These macros are modelled after the ones in <netinet/in6.h>. */
1115
1116/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001117#define IN6_IS_ADDR_TEREDO(a) \
1118 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001119
1120/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001121#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001122
1123/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001124#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001125
1126/*
1127 * Get the label for a given IPv4/IPv6 address.
1128 * RFC 6724, section 2.1.
1129 */
1130
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001131static int _get_label(const struct sockaddr* addr) {
1132 if (addr->sa_family == AF_INET) {
1133 return 4;
1134 } else if (addr->sa_family == AF_INET6) {
1135 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1136 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1137 return 0;
1138 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1139 return 4;
1140 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1141 return 2;
1142 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1143 return 5;
1144 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1145 return 13;
1146 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1147 return 3;
1148 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1149 return 11;
1150 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1151 return 12;
1152 } else {
1153 /* All other IPv6 addresses, including global unicast addresses. */
1154 return 1;
1155 }
1156 } else {
1157 /*
1158 * This should never happen.
1159 * Return a semi-random label as a last resort.
1160 */
1161 return 1;
1162 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001163}
1164
1165/*
1166 * Get the precedence for a given IPv4/IPv6 address.
1167 * RFC 6724, section 2.1.
1168 */
1169
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001170static int _get_precedence(const struct sockaddr* addr) {
1171 if (addr->sa_family == AF_INET) {
1172 return 35;
1173 } else if (addr->sa_family == AF_INET6) {
1174 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1175 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1176 return 50;
1177 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1178 return 35;
1179 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1180 return 30;
1181 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1182 return 5;
1183 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1184 return 3;
1185 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1186 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1187 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1188 return 1;
1189 } else {
1190 /* All other IPv6 addresses, including global unicast addresses. */
1191 return 40;
1192 }
1193 } else {
1194 return 1;
1195 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001196}
1197
1198/*
1199 * Find number of matching initial bits between the two addresses a1 and a2.
1200 */
1201
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001202static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1203 const char* p1 = (const char*) a1;
1204 const char* p2 = (const char*) a2;
1205 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001206
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001207 for (i = 0; i < sizeof(*a1); ++i) {
1208 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001209
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001210 if (p1[i] == p2[i]) {
1211 continue;
1212 }
1213 x = p1[i] ^ p2[i];
1214 for (j = 0; j < CHAR_BIT; ++j) {
1215 if (x & (1 << (CHAR_BIT - 1))) {
1216 return i * CHAR_BIT + j;
1217 }
1218 x <<= 1;
1219 }
1220 }
1221 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001222}
1223
1224/*
1225 * Compare two source/destination address pairs.
1226 * RFC 6724, section 6.
1227 */
1228
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001229static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1230 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1231 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1232 int scope_src1, scope_dst1, scope_match1;
1233 int scope_src2, scope_dst2, scope_match2;
1234 int label_src1, label_dst1, label_match1;
1235 int label_src2, label_dst2, label_match2;
1236 int precedence1, precedence2;
1237 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001238
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001239 /* Rule 1: Avoid unusable destinations. */
1240 if (a1->has_src_addr != a2->has_src_addr) {
1241 return a2->has_src_addr - a1->has_src_addr;
1242 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001243
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001244 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001245 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001246 scope_dst1 = _get_scope(a1->ai->ai_addr);
1247 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001248
nuccachene172a4e2018-10-23 17:10:58 +08001249 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001250 scope_dst2 = _get_scope(a2->ai->ai_addr);
1251 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001252
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001253 if (scope_match1 != scope_match2) {
1254 return scope_match2 - scope_match1;
1255 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001257 /*
1258 * Rule 3: Avoid deprecated addresses.
1259 * TODO(sesse): We don't currently have a good way of finding this.
1260 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001261
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001262 /*
1263 * Rule 4: Prefer home addresses.
1264 * TODO(sesse): We don't currently have a good way of finding this.
1265 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001267 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001268 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001269 label_dst1 = _get_label(a1->ai->ai_addr);
1270 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001271
nuccachene172a4e2018-10-23 17:10:58 +08001272 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001273 label_dst2 = _get_label(a2->ai->ai_addr);
1274 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001275
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001276 if (label_match1 != label_match2) {
1277 return label_match2 - label_match1;
1278 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001279
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001280 /* Rule 6: Prefer higher precedence. */
1281 precedence1 = _get_precedence(a1->ai->ai_addr);
1282 precedence2 = _get_precedence(a2->ai->ai_addr);
1283 if (precedence1 != precedence2) {
1284 return precedence2 - precedence1;
1285 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001286
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001287 /*
1288 * Rule 7: Prefer native transport.
1289 * TODO(sesse): We don't currently have a good way of finding this.
1290 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001291
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001292 /* Rule 8: Prefer smaller scope. */
1293 if (scope_dst1 != scope_dst2) {
1294 return scope_dst1 - scope_dst2;
1295 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001296
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001297 /*
1298 * Rule 9: Use longest matching prefix.
1299 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1300 * to work very well directly applied to IPv4. (glibc uses information from
1301 * the routing table for a custom IPv4 implementation here.)
1302 */
1303 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1304 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001305 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001306 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001307 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001308 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1309 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1310 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1311 if (prefixlen1 != prefixlen2) {
1312 return prefixlen2 - prefixlen1;
1313 }
1314 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001315
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001316 /*
1317 * Rule 10: Leave the order unchanged.
1318 * We need this since qsort() is not necessarily stable.
1319 */
1320 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001321}
1322
1323/*
1324 * Find the source address that will be used if trying to connect to the given
1325 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1326 *
1327 * Returns 1 if a source address was found, 0 if the address is unreachable,
1328 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1329 * undefined.
1330 */
1331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001332static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1333 uid_t uid) {
1334 int sock;
1335 int ret;
1336 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001337
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001338 switch (addr->sa_family) {
1339 case AF_INET:
1340 len = sizeof(struct sockaddr_in);
1341 break;
1342 case AF_INET6:
1343 len = sizeof(struct sockaddr_in6);
1344 break;
1345 default:
1346 /* No known usable source address for non-INET families. */
1347 return 0;
1348 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001349
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001350 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1351 if (sock == -1) {
1352 if (errno == EAFNOSUPPORT) {
1353 return 0;
1354 } else {
1355 return -1;
1356 }
1357 }
1358 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1359 close(sock);
1360 return 0;
1361 }
1362 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1363 close(sock);
1364 return 0;
1365 }
1366 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001367 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001368 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001369
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001370 if (ret == -1) {
1371 close(sock);
1372 return 0;
1373 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001374
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001375 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1376 close(sock);
1377 return -1;
1378 }
1379 close(sock);
1380 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001381}
1382
1383/*
1384 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1385 * Will leave the list unchanged if an error occurs.
1386 */
1387
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001388static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1389 struct addrinfo* cur;
1390 int nelem = 0, i;
1391 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001392
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001393 cur = list_sentinel->ai_next;
1394 while (cur) {
1395 ++nelem;
1396 cur = cur->ai_next;
1397 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1400 if (elems == NULL) {
1401 goto error;
1402 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001403
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001404 /*
1405 * Convert the linked list to an array that also contains the candidate
1406 * source address for each destination address.
1407 */
1408 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1409 int has_src_addr;
1410 assert(cur != NULL);
1411 elems[i].ai = cur;
1412 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001413
nuccachene172a4e2018-10-23 17:10:58 +08001414 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001415 if (has_src_addr == -1) {
1416 goto error;
1417 }
1418 elems[i].has_src_addr = has_src_addr;
1419 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001420
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001421 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1422 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001423
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001424 list_sentinel->ai_next = elems[0].ai;
1425 for (i = 0; i < nelem - 1; ++i) {
1426 elems[i].ai->ai_next = elems[i + 1].ai;
1427 }
1428 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001429
1430error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001431 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001432}
1433
Bernie Innocenti948f6572018-09-12 21:32:42 +09001434static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1435 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001436 struct addrinfo *ai, *cur;
1437 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001438 struct res_target q, q2;
1439 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001440
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001441 memset(&q, 0, sizeof(q));
1442 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001443 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001444
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001445 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001446 if (buf == NULL) {
1447 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001448 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001449 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001450 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001451 if (buf2 == NULL) {
1452 free(buf);
1453 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001454 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001455 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001456
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001457 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001458 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001459 /* prefer IPv6 */
1460 q.name = name;
1461 q.qclass = C_IN;
1462 q.answer = buf->buf;
1463 q.anslen = sizeof(buf->buf);
1464 int query_ipv6 = 1, query_ipv4 = 1;
1465 if (pai->ai_flags & AI_ADDRCONFIG) {
1466 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1467 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1468 }
1469 if (query_ipv6) {
1470 q.qtype = T_AAAA;
1471 if (query_ipv4) {
1472 q.next = &q2;
1473 q2.name = name;
1474 q2.qclass = C_IN;
1475 q2.qtype = T_A;
1476 q2.answer = buf2->buf;
1477 q2.anslen = sizeof(buf2->buf);
1478 }
1479 } else if (query_ipv4) {
1480 q.qtype = T_A;
1481 } else {
1482 free(buf);
1483 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001484 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001485 }
1486 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001487 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001488 case AF_INET:
1489 q.name = name;
1490 q.qclass = C_IN;
1491 q.qtype = T_A;
1492 q.answer = buf->buf;
1493 q.anslen = sizeof(buf->buf);
1494 break;
1495 case AF_INET6:
1496 q.name = name;
1497 q.qclass = C_IN;
1498 q.qtype = T_AAAA;
1499 q.answer = buf->buf;
1500 q.anslen = sizeof(buf->buf);
1501 break;
1502 default:
1503 free(buf);
1504 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001505 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001506 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001507
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001508 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001509 if (res == NULL) {
1510 free(buf);
1511 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001512 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001513 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001514
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001515 /* this just sets our netid val in the thread private data so we don't have to
1516 * modify the api's all the way down to res_send.c's res_nsend. We could
1517 * fully populate the thread private data here, but if we get down there
1518 * and have a cache hit that would be wasted, so we do the rest there on miss
1519 */
1520 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001521
1522 // Pass ai_error to catch more detailed errors rather than EAI_NODATA.
1523 int ai_error = EAI_NODATA;
1524 if (res_searchN(name, &q, res, &ai_error) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001525 free(buf);
1526 free(buf2);
Mike Yu69615f62018-11-06 15:42:36 +08001527 return ai_error; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001528 }
1529 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1530 if (ai) {
1531 cur->ai_next = ai;
1532 while (cur && cur->ai_next) cur = cur->ai_next;
1533 }
1534 if (q.next) {
1535 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1536 if (ai) cur->ai_next = ai;
1537 }
1538 free(buf);
1539 free(buf2);
1540 if (sentinel.ai_next == NULL) {
Mike Yu69615f62018-11-06 15:42:36 +08001541 return herrnoToAiError(h_errno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001542 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001544 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001545
Bernie Innocenti948f6572018-09-12 21:32:42 +09001546 *rv = sentinel.ai_next;
1547 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001548}
1549
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001550static void _sethtent(FILE** hostf) {
1551 if (!*hostf)
1552 *hostf = fopen(_PATH_HOSTS, "re");
1553 else
1554 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001555}
1556
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001557static void _endhtent(FILE** hostf) {
1558 if (*hostf) {
1559 (void) fclose(*hostf);
1560 *hostf = NULL;
1561 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001562}
1563
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001564static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1565 char* p;
1566 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001567 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001568 int error;
1569 const char* addr;
1570 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001571
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001572 assert(name != NULL);
1573 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001574
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001575 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1576again:
1577 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1578 if (*p == '#') goto again;
1579 if (!(cp = strpbrk(p, "#\n"))) goto again;
1580 *cp = '\0';
1581 if (!(cp = strpbrk(p, " \t"))) goto again;
1582 *cp++ = '\0';
1583 addr = p;
1584 /* if this is not something we're looking for, skip it. */
1585 cname = NULL;
1586 while (cp && *cp) {
1587 if (*cp == ' ' || *cp == '\t') {
1588 cp++;
1589 continue;
1590 }
1591 if (!cname) cname = cp;
1592 tname = cp;
1593 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1594 // fprintf(stderr, "\ttname = '%s'", tname);
1595 if (strcasecmp(name, tname) == 0) goto found;
1596 }
1597 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001598
1599found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001600 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001601 if (error) goto again;
1602 for (res = res0; res; res = res->ai_next) {
1603 /* cover it up */
1604 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 if (pai->ai_flags & AI_CANONNAME) {
1607 if (get_canonname(pai, res, cname) != 0) {
1608 freeaddrinfo(res0);
1609 goto again;
1610 }
1611 }
1612 }
1613 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001614}
1615
Bernie Innocenti948f6572018-09-12 21:32:42 +09001616static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001617 struct addrinfo sentinel = {};
1618 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001619 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001620
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001621 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001622
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001623 _sethtent(&hostf);
1624 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1625 cur->ai_next = p;
1626 while (cur && cur->ai_next) cur = cur->ai_next;
1627 }
1628 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001629
Bernie Innocenti948f6572018-09-12 21:32:42 +09001630 *res = sentinel.ai_next;
1631 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001632}
1633
1634/* resolver logic */
1635
1636/*
1637 * Formulate a normal query, send, and await answer.
1638 * Returned answer is placed in supplied buffer "answer".
1639 * Perform preliminary check of answer, returning success only
1640 * if no error is indicated and the answer count is nonzero.
1641 * Return the size of the response on success, -1 on error.
1642 * Error number is left in h_errno.
1643 *
1644 * Caller must parse answer and determine whether it answers the question.
1645 */
Mike Yu69615f62018-11-06 15:42:36 +08001646static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001647 u_char buf[MAXPACKET];
1648 HEADER* hp;
1649 int n;
1650 struct res_target* t;
1651 int rcode;
1652 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001654 assert(name != NULL);
1655 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001656
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001657 rcode = NOERROR;
1658 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001659
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001660 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001661 u_char* answer;
1662 int anslen;
1663 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001665 hp = (HEADER*) (void*) t->answer;
1666 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001667
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001668 again:
1669 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001670
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001671 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001672 int cl = t->qclass;
1673 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001674 answer = t->answer;
1675 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001676#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001677 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001678#endif
1679
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001680 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001681 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1682 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1683 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001684 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001685#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001686 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001687#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001688 h_errno = NO_RECOVERY;
1689 return n;
1690 }
Mike Yu69615f62018-11-06 15:42:36 +08001691
1692 n = res_nsend(res, buf, n, answer, anslen, &rcode);
1693 *ai_error = rcodeToAiError(rcode);
Bernie Innocenti55864192018-08-30 04:05:20 +09001694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001695 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1696 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001697 /* if the query choked with EDNS0, retry without EDNS0 */
1698 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1699 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1700 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001701#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001702 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001703#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001704 goto again;
1705 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001706#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001707 if (res->options & RES_DEBUG)
1708 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001709#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001710 continue;
1711 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001713 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001715 t->n = n;
1716 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001718 if (ancount == 0) {
1719 switch (rcode) {
1720 case NXDOMAIN:
1721 h_errno = HOST_NOT_FOUND;
1722 break;
1723 case SERVFAIL:
1724 h_errno = TRY_AGAIN;
1725 break;
1726 case NOERROR:
1727 h_errno = NO_DATA;
1728 break;
1729 case FORMERR:
1730 case NOTIMP:
1731 case REFUSED:
1732 default:
1733 h_errno = NO_RECOVERY;
1734 break;
1735 }
1736 return -1;
1737 }
1738 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001739}
1740
1741/*
1742 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1743 * Return the size of the response on success, -1 on error.
1744 * If enabled, implement search rules until answer or unrecoverable failure
1745 * is detected. Error code, if any, is left in h_errno.
1746 */
Mike Yu69615f62018-11-06 15:42:36 +08001747static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001748 const char *cp, *const *domain;
1749 HEADER* hp;
1750 u_int dots;
1751 int trailing_dot, ret, saved_herrno;
1752 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001753
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001754 assert(name != NULL);
1755 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001756
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001757 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001758
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001759 errno = 0;
1760 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1761 dots = 0;
1762 for (cp = name; *cp; cp++) dots += (*cp == '.');
1763 trailing_dot = 0;
1764 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001765
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001766 /*
1767 * If there are dots in the name already, let's just give it a try
1768 * 'as is'. The threshold can be set with the "ndots" option.
1769 */
1770 saved_herrno = -1;
1771 if (dots >= res->ndots) {
Mike Yu69615f62018-11-06 15:42:36 +08001772 ret = res_querydomainN(name, NULL, target, res, ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001773 if (ret > 0) return (ret);
1774 saved_herrno = h_errno;
1775 tried_as_is++;
1776 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001778 /*
1779 * We do at least one level of search if
1780 * - there is no dot and RES_DEFNAME is set, or
1781 * - there is at least one dot, there is no trailing dot,
1782 * and RES_DNSRCH is set.
1783 */
1784 if ((!dots && (res->options & RES_DEFNAMES)) ||
1785 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1786 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001787
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001788 /* Unfortunately we need to set stuff up before
1789 * the domain stuff is tried. Will have a better
1790 * fix after thread pools are used.
1791 */
1792 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001793
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001794 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Mike Yu69615f62018-11-06 15:42:36 +08001795 ret = res_querydomainN(name, *domain, target, res, ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001796 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001797
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001798 /*
1799 * If no server present, give up.
1800 * If name isn't found in this domain,
1801 * keep trying higher domains in the search list
1802 * (if that's enabled).
1803 * On a NO_DATA error, keep trying, otherwise
1804 * a wildcard entry of another type could keep us
1805 * from finding this entry higher in the domain.
1806 * If we get some other error (negative answer or
1807 * server failure), then stop searching up,
1808 * but try the input name below in case it's
1809 * fully-qualified.
1810 */
1811 if (errno == ECONNREFUSED) {
1812 h_errno = TRY_AGAIN;
1813 return -1;
1814 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001815
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001816 switch (h_errno) {
1817 case NO_DATA:
1818 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001819 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001820 case HOST_NOT_FOUND:
1821 /* keep trying */
1822 break;
1823 case TRY_AGAIN:
1824 if (hp->rcode == SERVFAIL) {
1825 /* try next search element, if any */
1826 got_servfail++;
1827 break;
1828 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001829 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001830 default:
1831 /* anything else implies that we're done */
1832 done++;
1833 }
1834 /*
1835 * if we got here for some reason other than DNSRCH,
1836 * we only wanted one iteration of the loop, so stop.
1837 */
1838 if (!(res->options & RES_DNSRCH)) done++;
1839 }
1840 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001841
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001842 /*
1843 * if we have not already tried the name "as is", do that now.
1844 * note that we do this regardless of how many dots were in the
1845 * name or whether it ends with a dot.
1846 */
1847 if (!tried_as_is) {
Mike Yu69615f62018-11-06 15:42:36 +08001848 ret = res_querydomainN(name, NULL, target, res, ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001849 if (ret > 0) return ret;
1850 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001852 /*
1853 * if we got here, we didn't satisfy the search.
1854 * if we did an initial full query, return that query's h_errno
1855 * (note that we wouldn't be here if that query had succeeded).
1856 * else if we ever got a nodata, send that back as the reason.
1857 * else send back meaningless h_errno, that being the one from
1858 * the last DNSRCH we did.
1859 */
1860 if (saved_herrno != -1)
1861 h_errno = saved_herrno;
1862 else if (got_nodata)
1863 h_errno = NO_DATA;
1864 else if (got_servfail)
1865 h_errno = TRY_AGAIN;
1866 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001867}
1868
1869/*
1870 * Perform a call on res_query on the concatenation of name and domain,
1871 * removing a trailing dot from name if domain is NULL.
1872 */
Mike Yu69615f62018-11-06 15:42:36 +08001873static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
1874 int* ai_error) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001875 char nbuf[MAXDNAME];
1876 const char* longname = nbuf;
1877 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001879 assert(name != NULL);
1880 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001881
1882#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001883 if (res->options & RES_DEBUG)
1884 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001885#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001886 if (domain == NULL) {
1887 /*
1888 * Check for trailing '.';
1889 * copy without '.' if present.
1890 */
1891 n = strlen(name);
1892 if (n + 1 > sizeof(nbuf)) {
1893 h_errno = NO_RECOVERY;
1894 return -1;
1895 }
1896 if (n > 0 && name[--n] == '.') {
1897 strncpy(nbuf, name, n);
1898 nbuf[n] = '\0';
1899 } else
1900 longname = name;
1901 } else {
1902 n = strlen(name);
1903 d = strlen(domain);
1904 if (n + 1 + d + 1 > sizeof(nbuf)) {
1905 h_errno = NO_RECOVERY;
1906 return -1;
1907 }
1908 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1909 }
Mike Yu69615f62018-11-06 15:42:36 +08001910 return res_queryN(longname, target, res, ai_error);
Bernie Innocenti55864192018-08-30 04:05:20 +09001911}