blob: c22958861dbb753c254838aa7df58f88b3b28c13 [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
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190static int res_queryN(const char*, struct res_target*, res_state);
191static int res_searchN(const char*, struct res_target*, res_state);
192static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194static const char* const ai_errlist[] = {
195 "Success",
196 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
197 "Temporary failure in name resolution", /* EAI_AGAIN */
198 "Invalid value for ai_flags", /* EAI_BADFLAGS */
199 "Non-recoverable failure in name resolution", /* EAI_FAIL */
200 "ai_family not supported", /* EAI_FAMILY */
201 "Memory allocation failure", /* EAI_MEMORY */
202 "No address associated with hostname", /* EAI_NODATA */
203 "hostname nor servname provided, or not known", /* EAI_NONAME */
204 "servname not supported for ai_socktype", /* EAI_SERVICE */
205 "ai_socktype not supported", /* EAI_SOCKTYPE */
206 "System error returned in errno", /* EAI_SYSTEM */
207 "Invalid value for hints", /* EAI_BADHINTS */
208 "Resolved protocol is unknown", /* EAI_PROTOCOL */
209 "Argument buffer overflow", /* EAI_OVERFLOW */
210 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900211};
212
213/* XXX macros that make external reference is BAD. */
214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215#define GET_AI(ai, afd, addr) \
216 do { \
217 /* external reference: pai, error, and label free */ \
218 (ai) = get_ai(pai, (afd), (addr)); \
219 if ((ai) == NULL) { \
220 error = EAI_MEMORY; \
221 goto free; \
222 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900223 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900224
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900225#define GET_PORT(ai, serv) \
226 do { \
227 /* external reference: error and label free */ \
228 error = get_port((ai), (serv), 0); \
229 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900230 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900233 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
234#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900235
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900236const char* gai_strerror(int ecode) {
237 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
238 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900239}
240
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900241void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900242 while (ai) {
243 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900244 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900245 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900246 free(ai);
247 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900248 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900249}
250
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900251static int str2number(const char* p) {
252 char* ep;
253 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900254
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900255 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900257 if (*p == '\0') return -1;
258 ep = NULL;
259 errno = 0;
260 v = strtoul(p, &ep, 10);
261 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
262 return v;
263 else
264 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900265}
266
267/*
268 * The following functions determine whether IPv4 or IPv6 connectivity is
269 * available in order to implement AI_ADDRCONFIG.
270 *
271 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
272 * available, but whether addresses of the specified family are "configured
273 * on the local system". However, bionic doesn't currently support getifaddrs,
274 * so checking for connectivity is the next best thing.
275 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276static int _have_ipv6(unsigned mark, uid_t uid) {
277 static const struct sockaddr_in6 sin6_test = {
278 .sin6_family = AF_INET6,
279 .sin6_addr.s6_addr = {// 2000::
280 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800281 sockaddr_union addr = {.sin6 = sin6_test};
282 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900283}
284
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900285static int _have_ipv4(unsigned mark, uid_t uid) {
286 static const struct sockaddr_in sin_test = {
287 .sin_family = AF_INET,
288 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
289 };
nuccachene172a4e2018-10-23 17:10:58 +0800290 sockaddr_union addr = {.sin = sin_test};
291 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900292}
293
294bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900295 int32_t tmp;
296 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
297 return false;
298 }
299 *result = ntohl(tmp);
300 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900301}
302
Bernie Innocentic165ce82018-10-16 23:35:28 +0900303// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
304// NOTE: also called by resolv_set_nameservers_for_net().
305int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
306 addrinfo** result) {
307 hints.ai_flags = AI_NUMERICHOST;
308 const android_net_context netcontext = {
309 .app_netid = NETID_UNSET,
310 .app_mark = MARK_UNSET,
311 .dns_netid = NETID_UNSET,
312 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900313 .uid = NET_CONTEXT_INVALID_UID,
314 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900315 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900316}
317
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900318int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
319 const struct addrinfo* hints,
320 const struct android_net_context* netcontext,
321 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800322 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900323 struct addrinfo* cur;
324 int error = 0;
325 struct addrinfo ai;
326 struct addrinfo ai0;
327 struct addrinfo* pai;
328 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900329
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900330 /* hostname is allowed to be NULL */
331 /* servname is allowed to be NULL */
332 /* hints is allowed to be NULL */
333 assert(res != NULL);
334 assert(netcontext != NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900335 cur = &sentinel;
336 pai = &ai;
337 pai->ai_flags = 0;
338 pai->ai_family = PF_UNSPEC;
339 pai->ai_socktype = ANY;
340 pai->ai_protocol = ANY;
341 pai->ai_addrlen = 0;
342 pai->ai_canonname = NULL;
343 pai->ai_addr = NULL;
344 pai->ai_next = NULL;
Ken Chen3270cf52018-11-07 01:20:48 +0800345 do {
346 if (hostname == NULL && servname == NULL) {
347 error = EAI_NONAME;
348 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900349 }
Ken Chen3270cf52018-11-07 01:20:48 +0800350 if (hints) {
351 /* error check for hints */
352 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
353 error = EAI_BADHINTS;
354 break;
355 }
356 if (hints->ai_flags & ~AI_MASK) {
357 error = EAI_BADFLAGS;
358 break;
359 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900360
Ken Chen3270cf52018-11-07 01:20:48 +0800361 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
362 hints->ai_family == PF_INET6)) {
363 error = EAI_FAMILY;
364 break;
365 }
366 *pai = *hints;
367
368 /*
369 * if both socktype/protocol are specified, check if they
370 * are meaningful combination.
371 */
372 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
373 for (ex = explore_options; ex->e_af >= 0; ex++) {
374 if (pai->ai_family != ex->e_af) continue;
375 if (ex->e_socktype == ANY) continue;
376 if (ex->e_protocol == ANY) continue;
377 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
378 error = EAI_BADHINTS;
379 break;
380 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900381 }
Ken Chen3270cf52018-11-07 01:20:48 +0800382 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900383 }
384 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900385
Ken Chen3270cf52018-11-07 01:20:48 +0800386 /*
387 * check for special cases. (1) numeric servname is disallowed if
388 * socktype/protocol are left unspecified. (2) servname is disallowed
389 * for raw and other inet{,6} sockets.
390 */
391 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
392 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
393 ) {
394 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900395
Ken Chen3270cf52018-11-07 01:20:48 +0800396 if (pai->ai_family == PF_UNSPEC) {
397 pai->ai_family = PF_INET6;
398 }
399 error = get_portmatch(pai, servname);
400 if (error) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900401
Ken Chen3270cf52018-11-07 01:20:48 +0800402 *pai = ai0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900403 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900404
Ken Chen3270cf52018-11-07 01:20:48 +0800405 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900406
Ken Chen3270cf52018-11-07 01:20:48 +0800407 /* NULL hostname, or numeric hostname */
408 for (ex = explore_options; ex->e_af >= 0; ex++) {
409 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900410
Ken Chen3270cf52018-11-07 01:20:48 +0800411 /* PF_UNSPEC entries are prepared for DNS queries only */
412 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900413
Ken Chen3270cf52018-11-07 01:20:48 +0800414 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
415 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
416 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900417
Ken Chen3270cf52018-11-07 01:20:48 +0800418 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
419 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
420 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
421
422 if (hostname == NULL)
423 error = explore_null(pai, servname, &cur->ai_next);
424 else
425 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
426
427 if (error) break;
428
429 while (cur->ai_next) cur = cur->ai_next;
430 }
431 if (error) break;
432
433 /*
434 * XXX
435 * If numeric representation of AF1 can be interpreted as FQDN
436 * representation of AF2, we need to think again about the code below.
437 */
438 if (sentinel.ai_next) break;
439
440 if (hostname == NULL) {
441 error = EAI_NODATA;
442 break;
443 }
444 if (pai->ai_flags & AI_NUMERICHOST) {
445 error = EAI_NONAME;
446 break;
447 }
448
449 /*
450 * hostname as alphabetical name.
451 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
452 * outer loop by AFs.
453 */
454 for (ex = explore_options; ex->e_af >= 0; ex++) {
455 *pai = ai0;
456
457 /* require exact match for family field */
458 if (pai->ai_family != ex->e_af) continue;
459
460 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
461 continue;
462 }
463 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
464 continue;
465 }
466
467 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
468 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
469
470 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
471
472 while (cur->ai_next) cur = cur->ai_next;
473 }
474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800476 error = 0;
477 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900478 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800479 }
480 } while (0);
481
482 if (error) {
483 freeaddrinfo(sentinel.ai_next);
484 *res = NULL;
485 } else {
486 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900487 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489}
490
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900491// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
493 struct addrinfo** res, const struct android_net_context* netcontext) {
494 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900496
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900497 assert(pai != NULL);
498 /* hostname may be NULL */
499 /* servname may be NULL */
500 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900501
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900502 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900503
Bernie Innocenti948f6572018-09-12 21:32:42 +0900504 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900505 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900506
Bernie Innocenti948f6572018-09-12 21:32:42 +0900507 if (!files_getaddrinfo(hostname, pai, &result)) {
508 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900510 if (!error) {
511 struct addrinfo* cur;
512 for (cur = result; cur; cur = cur->ai_next) {
513 GET_PORT(cur, servname);
514 /* canonname should be filled already */
515 }
516 *res = result;
517 return 0;
518 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900519
520free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900521 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900522 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900523}
524
525/*
526 * hostname == NULL.
527 * passive socket -> anyaddr (0.0.0.0 or ::)
528 * non-passive socket -> localhost (127.0.0.1 or ::1)
529 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900530static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
531 int s;
532 const struct afd* afd;
533 struct addrinfo* cur;
534 struct addrinfo sentinel;
535 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900536
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900537 assert(pai != NULL);
538 /* servname may be NULL */
539 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900540
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541 *res = NULL;
542 sentinel.ai_next = NULL;
543 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900544
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900545 /*
546 * filter out AFs that are not supported by the kernel
547 * XXX errno?
548 */
549 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
550 if (s < 0) {
551 if (errno != EMFILE) return 0;
552 } else
553 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900555 /*
556 * if the servname does not match socktype/protocol, ignore it.
557 */
558 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900559
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900560 afd = find_afd(pai->ai_family);
561 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900562
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900563 if (pai->ai_flags & AI_PASSIVE) {
564 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565 GET_PORT(cur->ai_next, servname);
566 } else {
567 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 GET_PORT(cur->ai_next, servname);
569 }
570 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900571
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900572 *res = sentinel.ai_next;
573 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900574
575free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900576 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900577 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900578}
579
580/*
581 * numeric hostname
582 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900583static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
584 struct addrinfo** res, const char* canonname) {
585 const struct afd* afd;
586 struct addrinfo* cur;
587 struct addrinfo sentinel;
588 int error;
589 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900590
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 assert(pai != NULL);
592 /* hostname may be NULL */
593 /* servname may be NULL */
594 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 *res = NULL;
597 sentinel.ai_next = NULL;
598 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900599
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900600 /*
601 * if the servname does not match socktype/protocol, ignore it.
602 */
603 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 afd = find_afd(pai->ai_family);
606 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900607
Ken Chen15c805a2018-10-17 00:19:59 +0800608 if (inet_pton(afd->a_af, hostname, pton) == 1) {
609 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
610 GET_AI(cur->ai_next, afd, pton);
611 GET_PORT(cur->ai_next, servname);
612 if ((pai->ai_flags & AI_CANONNAME)) {
613 /*
614 * Set the numeric address itself as
615 * the canonical name, based on a
616 * clarification in rfc2553bis-03.
617 */
Ken Chen3270cf52018-11-07 01:20:48 +0800618 error = get_canonname(pai, cur->ai_next, canonname);
619 if (error != 0) {
620 freeaddrinfo(sentinel.ai_next);
621 return error;
622 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 }
Ken Chen15c805a2018-10-17 00:19:59 +0800624 while (cur->ai_next) cur = cur->ai_next;
625 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800626 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900627 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900628
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629 *res = sentinel.ai_next;
630 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900631
632free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900633 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900634 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900635}
636
637/*
638 * numeric hostname with scope
639 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900640static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
641 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900642 const struct afd* afd;
643 struct addrinfo* cur;
644 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900645 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900646 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900647
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 assert(pai != NULL);
649 /* hostname may be NULL */
650 /* servname may be NULL */
651 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900652
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653 /*
654 * if the servname does not match socktype/protocol, ignore it.
655 */
656 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900658 afd = find_afd(pai->ai_family);
659 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900662
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 cp = strchr(hostname, SCOPE_DELIMITER);
664 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900665
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900666 /*
667 * Handle special case of <scoped_address><delimiter><scope id>
668 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900669 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900670 if (hostname2 == NULL) return EAI_MEMORY;
671 /* terminate at the delimiter */
672 hostname2[cp - hostname] = '\0';
673 addr = hostname2;
674 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900675
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900676 error = explore_numeric(pai, addr, servname, res, hostname);
677 if (error == 0) {
678 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680 for (cur = *res; cur; cur = cur->ai_next) {
681 if (cur->ai_family != AF_INET6) continue;
682 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
683 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
684 free(hostname2);
685 return (EAI_NODATA); /* XXX: is return OK? */
686 }
687 sin6->sin6_scope_id = scopeid;
688 }
689 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900693 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900694}
695
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900696static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
697 assert(pai != NULL);
698 assert(ai != NULL);
699 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701 if ((pai->ai_flags & AI_CANONNAME) != 0) {
702 ai->ai_canonname = strdup(str);
703 if (ai->ai_canonname == NULL) return EAI_MEMORY;
704 }
705 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900706}
707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
709 const char* addr) {
710 char* p;
711 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 assert(pai != NULL);
714 assert(afd != NULL);
715 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900716
nuccachene21023a2018-09-11 11:13:44 +0800717 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900718 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900719
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900720 memcpy(ai, pai, sizeof(struct addrinfo));
721 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800722 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900723
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900724 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
726 p = (char*) (void*) (ai->ai_addr);
727 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
728 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900729}
730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731static int get_portmatch(const struct addrinfo* ai, const char* servname) {
732 assert(ai != NULL);
733 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900736}
737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
739 const char* proto;
740 struct servent* sp;
741 int port;
742 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900743
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 assert(ai != NULL);
745 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900746
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 if (servname == NULL) return 0;
748 switch (ai->ai_family) {
749 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751 break;
752 default:
753 return 0;
754 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 switch (ai->ai_socktype) {
757 case SOCK_RAW:
758 return EAI_SERVICE;
759 case SOCK_DGRAM:
760 case SOCK_STREAM:
761 allownumeric = 1;
762 break;
763 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765 break;
766 default:
767 return EAI_SOCKTYPE;
768 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900769
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900770 port = str2number(servname);
771 if (port >= 0) {
772 if (!allownumeric) return EAI_SERVICE;
773 if (port < 0 || port > 65535) return EAI_SERVICE;
774 port = htons(port);
775 } else {
776 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900778 switch (ai->ai_socktype) {
779 case SOCK_DGRAM:
780 proto = "udp";
781 break;
782 case SOCK_STREAM:
783 proto = "tcp";
784 break;
785 default:
786 proto = NULL;
787 break;
788 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
791 port = sp->s_port;
792 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900793
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900794 if (!matchonly) {
795 switch (ai->ai_family) {
796 case AF_INET:
797 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
798 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 case AF_INET6:
800 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
801 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900802 }
803 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900805 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900806}
807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900808static const struct afd* find_afd(int af) {
809 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900810
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811 if (af == PF_UNSPEC) return NULL;
812 for (afd = afdl; afd->a_af; afd++) {
813 if (afd->a_af == af) return afd;
814 }
815 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900816}
817
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900818// Convert a string to a scope identifier.
819static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820 u_long lscopeid;
821 struct in6_addr* a6;
822 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 assert(scope != NULL);
825 assert(sin6 != NULL);
826 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900829
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900830 /* empty scopeid portion is invalid */
831 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900832
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900833 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
834 /*
835 * We currently assume a one-to-one mapping between links
836 * and interfaces, so we simply use interface indices for
837 * like-local scopes.
838 */
839 *scopeid = if_nametoindex(scope);
840 if (*scopeid == 0) goto trynumeric;
841 return 0;
842 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900843
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900844 /* still unclear about literal, allow numeric only - placeholder */
845 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
846 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
847 goto trynumeric;
848 else
849 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900850
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 /* try to convert to a numeric id as a last resort */
852trynumeric:
853 errno = 0;
854 lscopeid = strtoul(scope, &ep, 10);
855 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
856 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
857 return 0;
858 else
859 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900860}
Bernie Innocenti55864192018-08-30 04:05:20 +0900861
862/* code duplicate with gethnamaddr.c */
863
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900864static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900865
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866#define BOUNDED_INCR(x) \
867 do { \
868 BOUNDS_CHECK(cp, x); \
869 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900870 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900871
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900872#define BOUNDS_CHECK(ptr, count) \
873 do { \
874 if (eom - (ptr) < (count)) { \
875 h_errno = NO_RECOVERY; \
876 return NULL; \
877 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900878 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900879
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900880static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
881 const struct addrinfo* pai) {
Ken Chen3270cf52018-11-07 01:20:48 +0800882 struct addrinfo sentinel = {};
883 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900884 struct addrinfo ai;
885 const struct afd* afd;
886 char* canonname;
887 const HEADER* hp;
888 const u_char* cp;
889 int n;
890 const u_char* eom;
891 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900892 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900893 int haveanswer, had_error;
894 char tbuf[MAXDNAME];
895 int (*name_ok)(const char*);
896 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900897
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900898 assert(answer != NULL);
899 assert(qname != NULL);
900 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900901
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900902 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900903
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900904 canonname = NULL;
905 eom = answer->buf + anslen;
906 switch (qtype) {
907 case T_A:
908 case T_AAAA:
909 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
910 name_ok = res_hnok;
911 break;
912 default:
913 return NULL; /* XXX should be abort(); */
914 }
915 /*
916 * find first satisfactory answer
917 */
918 hp = &answer->hdr;
919 ancount = ntohs(hp->ancount);
920 qdcount = ntohs(hp->qdcount);
921 bp = hostbuf;
922 ep = hostbuf + sizeof hostbuf;
923 cp = answer->buf;
924 BOUNDED_INCR(HFIXEDSZ);
925 if (qdcount != 1) {
926 h_errno = NO_RECOVERY;
927 return (NULL);
928 }
929 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
930 if ((n < 0) || !(*name_ok)(bp)) {
931 h_errno = NO_RECOVERY;
932 return (NULL);
933 }
934 BOUNDED_INCR(n + QFIXEDSZ);
935 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
936 /* res_send() has already verified that the query name is the
937 * same as the one we sent; this just gets the expanded name
938 * (i.e., with the succeeding search-domain tacked on).
939 */
940 n = strlen(bp) + 1; /* for the \0 */
941 if (n >= MAXHOSTNAMELEN) {
942 h_errno = NO_RECOVERY;
943 return (NULL);
944 }
945 canonname = bp;
946 bp += n;
947 /* The qname can be abbreviated, but h_name is now absolute. */
948 qname = canonname;
949 }
950 haveanswer = 0;
951 had_error = 0;
952 while (ancount-- > 0 && cp < eom && !had_error) {
953 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
954 if ((n < 0) || !(*name_ok)(bp)) {
955 had_error++;
956 continue;
957 }
958 cp += n; /* name */
959 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900960 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900961 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900962 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900963 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900964 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900965 cp += INT16SZ; /* len */
966 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900967 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900968 /* XXX - debug? syslog? */
969 cp += n;
970 continue; /* XXX - had_error++ ? */
971 }
972 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
973 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
974 if ((n < 0) || !(*name_ok)(tbuf)) {
975 had_error++;
976 continue;
977 }
978 cp += n;
979 /* Get canonical name. */
980 n = strlen(tbuf) + 1; /* for the \0 */
981 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
982 had_error++;
983 continue;
984 }
985 strlcpy(bp, tbuf, (size_t)(ep - bp));
986 canonname = bp;
987 bp += n;
988 continue;
989 }
990 if (qtype == T_ANY) {
991 if (!(type == T_A || type == T_AAAA)) {
992 cp += n;
993 continue;
994 }
995 } else if (type != qtype) {
996 if (type != T_KEY && type != T_SIG)
997 syslog(LOG_NOTICE | LOG_AUTH,
998 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
999 p_class(C_IN), p_type(qtype), p_type(type));
1000 cp += n;
1001 continue; /* XXX - had_error++ ? */
1002 }
1003 switch (type) {
1004 case T_A:
1005 case T_AAAA:
1006 if (strcasecmp(canonname, bp) != 0) {
1007 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1008 cp += n;
1009 continue; /* XXX - had_error++ ? */
1010 }
1011 if (type == T_A && n != INADDRSZ) {
1012 cp += n;
1013 continue;
1014 }
1015 if (type == T_AAAA && n != IN6ADDRSZ) {
1016 cp += n;
1017 continue;
1018 }
1019 if (type == T_AAAA) {
1020 struct in6_addr in6;
1021 memcpy(&in6, cp, IN6ADDRSZ);
1022 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1023 cp += n;
1024 continue;
1025 }
1026 }
1027 if (!haveanswer) {
1028 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001029
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001030 canonname = bp;
1031 nn = strlen(bp) + 1; /* for the \0 */
1032 bp += nn;
1033 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001034
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001035 /* don't overwrite pai */
1036 ai = *pai;
1037 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1038 afd = find_afd(ai.ai_family);
1039 if (afd == NULL) {
1040 cp += n;
1041 continue;
1042 }
1043 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1044 if (cur->ai_next == NULL) had_error++;
1045 while (cur && cur->ai_next) cur = cur->ai_next;
1046 cp += n;
1047 break;
1048 default:
1049 abort();
1050 }
1051 if (!had_error) haveanswer++;
1052 }
1053 if (haveanswer) {
1054 if (!canonname)
1055 (void) get_canonname(pai, sentinel.ai_next, qname);
1056 else
1057 (void) get_canonname(pai, sentinel.ai_next, canonname);
1058 h_errno = NETDB_SUCCESS;
1059 return sentinel.ai_next;
1060 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001061
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001062 h_errno = NO_RECOVERY;
1063 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001064}
1065
1066struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001067 struct addrinfo* ai;
1068 int has_src_addr;
1069 sockaddr_union src_addr;
1070 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001071};
1072
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001073static int _get_scope(const struct sockaddr* addr) {
1074 if (addr->sa_family == AF_INET6) {
1075 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1076 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1077 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1078 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1079 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1080 /*
1081 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1082 * link-local scope.
1083 */
1084 return IPV6_ADDR_SCOPE_LINKLOCAL;
1085 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1086 return IPV6_ADDR_SCOPE_SITELOCAL;
1087 } else {
1088 return IPV6_ADDR_SCOPE_GLOBAL;
1089 }
1090 } else if (addr->sa_family == AF_INET) {
1091 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1092 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001093
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001094 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1095 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1096 return IPV6_ADDR_SCOPE_LINKLOCAL;
1097 } else {
1098 /*
1099 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1100 * and shared addresses (100.64.0.0/10), are assigned global scope.
1101 */
1102 return IPV6_ADDR_SCOPE_GLOBAL;
1103 }
1104 } else {
1105 /*
1106 * This should never happen.
1107 * Return a scope with low priority as a last resort.
1108 */
1109 return IPV6_ADDR_SCOPE_NODELOCAL;
1110 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001111}
1112
1113/* These macros are modelled after the ones in <netinet/in6.h>. */
1114
1115/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001116#define IN6_IS_ADDR_TEREDO(a) \
1117 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001118
1119/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001120#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001121
1122/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001123#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001124
1125/*
1126 * Get the label for a given IPv4/IPv6 address.
1127 * RFC 6724, section 2.1.
1128 */
1129
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001130static int _get_label(const struct sockaddr* addr) {
1131 if (addr->sa_family == AF_INET) {
1132 return 4;
1133 } else if (addr->sa_family == AF_INET6) {
1134 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1135 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1136 return 0;
1137 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1138 return 4;
1139 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1140 return 2;
1141 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1142 return 5;
1143 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1144 return 13;
1145 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1146 return 3;
1147 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1148 return 11;
1149 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1150 return 12;
1151 } else {
1152 /* All other IPv6 addresses, including global unicast addresses. */
1153 return 1;
1154 }
1155 } else {
1156 /*
1157 * This should never happen.
1158 * Return a semi-random label as a last resort.
1159 */
1160 return 1;
1161 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001162}
1163
1164/*
1165 * Get the precedence for a given IPv4/IPv6 address.
1166 * RFC 6724, section 2.1.
1167 */
1168
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001169static int _get_precedence(const struct sockaddr* addr) {
1170 if (addr->sa_family == AF_INET) {
1171 return 35;
1172 } else if (addr->sa_family == AF_INET6) {
1173 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1174 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1175 return 50;
1176 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1177 return 35;
1178 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1179 return 30;
1180 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1181 return 5;
1182 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1183 return 3;
1184 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1185 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1186 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1187 return 1;
1188 } else {
1189 /* All other IPv6 addresses, including global unicast addresses. */
1190 return 40;
1191 }
1192 } else {
1193 return 1;
1194 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001195}
1196
1197/*
1198 * Find number of matching initial bits between the two addresses a1 and a2.
1199 */
1200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001201static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1202 const char* p1 = (const char*) a1;
1203 const char* p2 = (const char*) a2;
1204 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001205
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001206 for (i = 0; i < sizeof(*a1); ++i) {
1207 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001209 if (p1[i] == p2[i]) {
1210 continue;
1211 }
1212 x = p1[i] ^ p2[i];
1213 for (j = 0; j < CHAR_BIT; ++j) {
1214 if (x & (1 << (CHAR_BIT - 1))) {
1215 return i * CHAR_BIT + j;
1216 }
1217 x <<= 1;
1218 }
1219 }
1220 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001221}
1222
1223/*
1224 * Compare two source/destination address pairs.
1225 * RFC 6724, section 6.
1226 */
1227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001228static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1229 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1230 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1231 int scope_src1, scope_dst1, scope_match1;
1232 int scope_src2, scope_dst2, scope_match2;
1233 int label_src1, label_dst1, label_match1;
1234 int label_src2, label_dst2, label_match2;
1235 int precedence1, precedence2;
1236 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001237
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001238 /* Rule 1: Avoid unusable destinations. */
1239 if (a1->has_src_addr != a2->has_src_addr) {
1240 return a2->has_src_addr - a1->has_src_addr;
1241 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001242
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001243 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001244 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001245 scope_dst1 = _get_scope(a1->ai->ai_addr);
1246 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001247
nuccachene172a4e2018-10-23 17:10:58 +08001248 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001249 scope_dst2 = _get_scope(a2->ai->ai_addr);
1250 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001252 if (scope_match1 != scope_match2) {
1253 return scope_match2 - scope_match1;
1254 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001255
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001256 /*
1257 * Rule 3: Avoid deprecated addresses.
1258 * TODO(sesse): We don't currently have a good way of finding this.
1259 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001260
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001261 /*
1262 * Rule 4: Prefer home addresses.
1263 * TODO(sesse): We don't currently have a good way of finding this.
1264 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001265
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001266 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001267 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001268 label_dst1 = _get_label(a1->ai->ai_addr);
1269 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001270
nuccachene172a4e2018-10-23 17:10:58 +08001271 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001272 label_dst2 = _get_label(a2->ai->ai_addr);
1273 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001274
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001275 if (label_match1 != label_match2) {
1276 return label_match2 - label_match1;
1277 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001278
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001279 /* Rule 6: Prefer higher precedence. */
1280 precedence1 = _get_precedence(a1->ai->ai_addr);
1281 precedence2 = _get_precedence(a2->ai->ai_addr);
1282 if (precedence1 != precedence2) {
1283 return precedence2 - precedence1;
1284 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001286 /*
1287 * Rule 7: Prefer native transport.
1288 * TODO(sesse): We don't currently have a good way of finding this.
1289 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001290
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001291 /* Rule 8: Prefer smaller scope. */
1292 if (scope_dst1 != scope_dst2) {
1293 return scope_dst1 - scope_dst2;
1294 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001295
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001296 /*
1297 * Rule 9: Use longest matching prefix.
1298 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1299 * to work very well directly applied to IPv4. (glibc uses information from
1300 * the routing table for a custom IPv4 implementation here.)
1301 */
1302 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1303 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001304 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001305 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001306 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001307 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1308 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1309 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1310 if (prefixlen1 != prefixlen2) {
1311 return prefixlen2 - prefixlen1;
1312 }
1313 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001314
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001315 /*
1316 * Rule 10: Leave the order unchanged.
1317 * We need this since qsort() is not necessarily stable.
1318 */
1319 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001320}
1321
1322/*
1323 * Find the source address that will be used if trying to connect to the given
1324 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1325 *
1326 * Returns 1 if a source address was found, 0 if the address is unreachable,
1327 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1328 * undefined.
1329 */
1330
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001331static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1332 uid_t uid) {
1333 int sock;
1334 int ret;
1335 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001336
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001337 switch (addr->sa_family) {
1338 case AF_INET:
1339 len = sizeof(struct sockaddr_in);
1340 break;
1341 case AF_INET6:
1342 len = sizeof(struct sockaddr_in6);
1343 break;
1344 default:
1345 /* No known usable source address for non-INET families. */
1346 return 0;
1347 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001348
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001349 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1350 if (sock == -1) {
1351 if (errno == EAFNOSUPPORT) {
1352 return 0;
1353 } else {
1354 return -1;
1355 }
1356 }
1357 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1358 close(sock);
1359 return 0;
1360 }
1361 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1362 close(sock);
1363 return 0;
1364 }
1365 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001366 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001367 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001368
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001369 if (ret == -1) {
1370 close(sock);
1371 return 0;
1372 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001373
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001374 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1375 close(sock);
1376 return -1;
1377 }
1378 close(sock);
1379 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001380}
1381
1382/*
1383 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1384 * Will leave the list unchanged if an error occurs.
1385 */
1386
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001387static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1388 struct addrinfo* cur;
1389 int nelem = 0, i;
1390 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001391
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001392 cur = list_sentinel->ai_next;
1393 while (cur) {
1394 ++nelem;
1395 cur = cur->ai_next;
1396 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001397
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001398 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1399 if (elems == NULL) {
1400 goto error;
1401 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001402
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001403 /*
1404 * Convert the linked list to an array that also contains the candidate
1405 * source address for each destination address.
1406 */
1407 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1408 int has_src_addr;
1409 assert(cur != NULL);
1410 elems[i].ai = cur;
1411 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001412
nuccachene172a4e2018-10-23 17:10:58 +08001413 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001414 if (has_src_addr == -1) {
1415 goto error;
1416 }
1417 elems[i].has_src_addr = has_src_addr;
1418 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001419
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001420 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1421 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001422
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001423 list_sentinel->ai_next = elems[0].ai;
1424 for (i = 0; i < nelem - 1; ++i) {
1425 elems[i].ai->ai_next = elems[i + 1].ai;
1426 }
1427 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001428
1429error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001430 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001431}
1432
Bernie Innocenti948f6572018-09-12 21:32:42 +09001433static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1434 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001435 struct addrinfo *ai, *cur;
1436 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001437 struct res_target q, q2;
1438 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001439
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001440 memset(&q, 0, sizeof(q));
1441 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001442 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001443
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001444 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001445 if (buf == NULL) {
1446 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001447 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001448 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001449 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001450 if (buf2 == NULL) {
1451 free(buf);
1452 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001453 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001454 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001455
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001456 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001457 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001458 /* prefer IPv6 */
1459 q.name = name;
1460 q.qclass = C_IN;
1461 q.answer = buf->buf;
1462 q.anslen = sizeof(buf->buf);
1463 int query_ipv6 = 1, query_ipv4 = 1;
1464 if (pai->ai_flags & AI_ADDRCONFIG) {
1465 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1466 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1467 }
1468 if (query_ipv6) {
1469 q.qtype = T_AAAA;
1470 if (query_ipv4) {
1471 q.next = &q2;
1472 q2.name = name;
1473 q2.qclass = C_IN;
1474 q2.qtype = T_A;
1475 q2.answer = buf2->buf;
1476 q2.anslen = sizeof(buf2->buf);
1477 }
1478 } else if (query_ipv4) {
1479 q.qtype = T_A;
1480 } else {
1481 free(buf);
1482 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001483 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001484 }
1485 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001486 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001487 case AF_INET:
1488 q.name = name;
1489 q.qclass = C_IN;
1490 q.qtype = T_A;
1491 q.answer = buf->buf;
1492 q.anslen = sizeof(buf->buf);
1493 break;
1494 case AF_INET6:
1495 q.name = name;
1496 q.qclass = C_IN;
1497 q.qtype = T_AAAA;
1498 q.answer = buf->buf;
1499 q.anslen = sizeof(buf->buf);
1500 break;
1501 default:
1502 free(buf);
1503 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001504 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001505 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001506
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001507 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001508 if (res == NULL) {
1509 free(buf);
1510 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001511 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001512 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001513
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001514 /* this just sets our netid val in the thread private data so we don't have to
1515 * modify the api's all the way down to res_send.c's res_nsend. We could
1516 * fully populate the thread private data here, but if we get down there
1517 * and have a cache hit that would be wasted, so we do the rest there on miss
1518 */
1519 res_setnetcontext(res, netcontext);
1520 if (res_searchN(name, &q, res) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001521 free(buf);
1522 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001523 return EAI_NODATA; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001524 }
1525 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1526 if (ai) {
1527 cur->ai_next = ai;
1528 while (cur && cur->ai_next) cur = cur->ai_next;
1529 }
1530 if (q.next) {
1531 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1532 if (ai) cur->ai_next = ai;
1533 }
1534 free(buf);
1535 free(buf2);
1536 if (sentinel.ai_next == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001537 switch (h_errno) {
1538 case HOST_NOT_FOUND:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001539 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001540 case TRY_AGAIN:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001541 return EAI_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001542 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001543 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001544 }
1545 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001546
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001547 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001548
Bernie Innocenti948f6572018-09-12 21:32:42 +09001549 *rv = sentinel.ai_next;
1550 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001551}
1552
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001553static void _sethtent(FILE** hostf) {
1554 if (!*hostf)
1555 *hostf = fopen(_PATH_HOSTS, "re");
1556 else
1557 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001558}
1559
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001560static void _endhtent(FILE** hostf) {
1561 if (*hostf) {
1562 (void) fclose(*hostf);
1563 *hostf = NULL;
1564 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001565}
1566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001567static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1568 char* p;
1569 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001570 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001571 int error;
1572 const char* addr;
1573 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001574
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001575 assert(name != NULL);
1576 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001577
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001578 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1579again:
1580 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1581 if (*p == '#') goto again;
1582 if (!(cp = strpbrk(p, "#\n"))) goto again;
1583 *cp = '\0';
1584 if (!(cp = strpbrk(p, " \t"))) goto again;
1585 *cp++ = '\0';
1586 addr = p;
1587 /* if this is not something we're looking for, skip it. */
1588 cname = NULL;
1589 while (cp && *cp) {
1590 if (*cp == ' ' || *cp == '\t') {
1591 cp++;
1592 continue;
1593 }
1594 if (!cname) cname = cp;
1595 tname = cp;
1596 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1597 // fprintf(stderr, "\ttname = '%s'", tname);
1598 if (strcasecmp(name, tname) == 0) goto found;
1599 }
1600 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001601
1602found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001603 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001604 if (error) goto again;
1605 for (res = res0; res; res = res->ai_next) {
1606 /* cover it up */
1607 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001609 if (pai->ai_flags & AI_CANONNAME) {
1610 if (get_canonname(pai, res, cname) != 0) {
1611 freeaddrinfo(res0);
1612 goto again;
1613 }
1614 }
1615 }
1616 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001617}
1618
Bernie Innocenti948f6572018-09-12 21:32:42 +09001619static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001620 struct addrinfo sentinel = {};
1621 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001622 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001623
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001624 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001625
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001626 _sethtent(&hostf);
1627 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1628 cur->ai_next = p;
1629 while (cur && cur->ai_next) cur = cur->ai_next;
1630 }
1631 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001632
Bernie Innocenti948f6572018-09-12 21:32:42 +09001633 *res = sentinel.ai_next;
1634 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001635}
1636
1637/* resolver logic */
1638
1639/*
1640 * Formulate a normal query, send, and await answer.
1641 * Returned answer is placed in supplied buffer "answer".
1642 * Perform preliminary check of answer, returning success only
1643 * if no error is indicated and the answer count is nonzero.
1644 * Return the size of the response on success, -1 on error.
1645 * Error number is left in h_errno.
1646 *
1647 * Caller must parse answer and determine whether it answers the question.
1648 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1650 res_state res) {
1651 u_char buf[MAXPACKET];
1652 HEADER* hp;
1653 int n;
1654 struct res_target* t;
1655 int rcode;
1656 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001658 assert(name != NULL);
1659 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001661 rcode = NOERROR;
1662 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001663
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001664 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001665 u_char* answer;
1666 int anslen;
1667 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001669 hp = (HEADER*) (void*) t->answer;
1670 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001671
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001672 again:
1673 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001674
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001675 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001676 int cl = t->qclass;
1677 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001678 answer = t->answer;
1679 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001680#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001681 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001682#endif
1683
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001684 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001685 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1686 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1687 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001688 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001689#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001690 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001691#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001692 h_errno = NO_RECOVERY;
1693 return n;
1694 }
1695 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001697 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1698 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001699 /* if the query choked with EDNS0, retry without EDNS0 */
1700 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1701 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1702 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001703#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001704 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001705#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001706 goto again;
1707 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001708#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001709 if (res->options & RES_DEBUG)
1710 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001711#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001712 continue;
1713 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001715 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001716
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001717 t->n = n;
1718 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001719
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001720 if (ancount == 0) {
1721 switch (rcode) {
1722 case NXDOMAIN:
1723 h_errno = HOST_NOT_FOUND;
1724 break;
1725 case SERVFAIL:
1726 h_errno = TRY_AGAIN;
1727 break;
1728 case NOERROR:
1729 h_errno = NO_DATA;
1730 break;
1731 case FORMERR:
1732 case NOTIMP:
1733 case REFUSED:
1734 default:
1735 h_errno = NO_RECOVERY;
1736 break;
1737 }
1738 return -1;
1739 }
1740 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001741}
1742
1743/*
1744 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1745 * Return the size of the response on success, -1 on error.
1746 * If enabled, implement search rules until answer or unrecoverable failure
1747 * is detected. Error code, if any, is left in h_errno.
1748 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001749static int res_searchN(const char* name, struct res_target* target, res_state res) {
1750 const char *cp, *const *domain;
1751 HEADER* hp;
1752 u_int dots;
1753 int trailing_dot, ret, saved_herrno;
1754 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001756 assert(name != NULL);
1757 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001758
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001759 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001760
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001761 errno = 0;
1762 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1763 dots = 0;
1764 for (cp = name; *cp; cp++) dots += (*cp == '.');
1765 trailing_dot = 0;
1766 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001768 /*
1769 * If there are dots in the name already, let's just give it a try
1770 * 'as is'. The threshold can be set with the "ndots" option.
1771 */
1772 saved_herrno = -1;
1773 if (dots >= res->ndots) {
1774 ret = res_querydomainN(name, NULL, target, res);
1775 if (ret > 0) return (ret);
1776 saved_herrno = h_errno;
1777 tried_as_is++;
1778 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001780 /*
1781 * We do at least one level of search if
1782 * - there is no dot and RES_DEFNAME is set, or
1783 * - there is at least one dot, there is no trailing dot,
1784 * and RES_DNSRCH is set.
1785 */
1786 if ((!dots && (res->options & RES_DEFNAMES)) ||
1787 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1788 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001790 /* Unfortunately we need to set stuff up before
1791 * the domain stuff is tried. Will have a better
1792 * fix after thread pools are used.
1793 */
1794 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001795
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001796 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1797 ret = res_querydomainN(name, *domain, target, res);
1798 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001799
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001800 /*
1801 * If no server present, give up.
1802 * If name isn't found in this domain,
1803 * keep trying higher domains in the search list
1804 * (if that's enabled).
1805 * On a NO_DATA error, keep trying, otherwise
1806 * a wildcard entry of another type could keep us
1807 * from finding this entry higher in the domain.
1808 * If we get some other error (negative answer or
1809 * server failure), then stop searching up,
1810 * but try the input name below in case it's
1811 * fully-qualified.
1812 */
1813 if (errno == ECONNREFUSED) {
1814 h_errno = TRY_AGAIN;
1815 return -1;
1816 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001818 switch (h_errno) {
1819 case NO_DATA:
1820 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001821 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001822 case HOST_NOT_FOUND:
1823 /* keep trying */
1824 break;
1825 case TRY_AGAIN:
1826 if (hp->rcode == SERVFAIL) {
1827 /* try next search element, if any */
1828 got_servfail++;
1829 break;
1830 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001831 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001832 default:
1833 /* anything else implies that we're done */
1834 done++;
1835 }
1836 /*
1837 * if we got here for some reason other than DNSRCH,
1838 * we only wanted one iteration of the loop, so stop.
1839 */
1840 if (!(res->options & RES_DNSRCH)) done++;
1841 }
1842 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001843
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001844 /*
1845 * if we have not already tried the name "as is", do that now.
1846 * note that we do this regardless of how many dots were in the
1847 * name or whether it ends with a dot.
1848 */
1849 if (!tried_as_is) {
1850 ret = res_querydomainN(name, NULL, target, res);
1851 if (ret > 0) return ret;
1852 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001853
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001854 /*
1855 * if we got here, we didn't satisfy the search.
1856 * if we did an initial full query, return that query's h_errno
1857 * (note that we wouldn't be here if that query had succeeded).
1858 * else if we ever got a nodata, send that back as the reason.
1859 * else send back meaningless h_errno, that being the one from
1860 * the last DNSRCH we did.
1861 */
1862 if (saved_herrno != -1)
1863 h_errno = saved_herrno;
1864 else if (got_nodata)
1865 h_errno = NO_DATA;
1866 else if (got_servfail)
1867 h_errno = TRY_AGAIN;
1868 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001869}
1870
1871/*
1872 * Perform a call on res_query on the concatenation of name and domain,
1873 * removing a trailing dot from name if domain is NULL.
1874 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001875static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1876 res_state res) {
1877 char nbuf[MAXDNAME];
1878 const char* longname = nbuf;
1879 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001880
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001881 assert(name != NULL);
1882 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001883
1884#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001885 if (res->options & RES_DEBUG)
1886 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001887#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001888 if (domain == NULL) {
1889 /*
1890 * Check for trailing '.';
1891 * copy without '.' if present.
1892 */
1893 n = strlen(name);
1894 if (n + 1 > sizeof(nbuf)) {
1895 h_errno = NO_RECOVERY;
1896 return -1;
1897 }
1898 if (n > 0 && name[--n] == '.') {
1899 strncpy(nbuf, name, n);
1900 nbuf[n] = '\0';
1901 } else
1902 longname = name;
1903 } else {
1904 n = strlen(name);
1905 d = strlen(domain);
1906 if (n + 1 + d + 1 > sizeof(nbuf)) {
1907 h_errno = NO_RECOVERY;
1908 return -1;
1909 }
1910 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1911 }
1912 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001913}