blob: f1aff7a54aa93c47302d8f697ebe811db6406007 [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>
91#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090092#include <sys/param.h>
93#include <sys/socket.h>
94#include <sys/stat.h>
95#include <sys/types.h>
96#include <sys/un.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090097#include <syslog.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090098#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090099
Bernie Innocenti189eb502018-10-01 23:10:18 +0900100#include "netd_resolv/resolv.h"
101#include "resolv_cache.h"
102#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900103
Bernie Innocenti55864192018-08-30 04:05:20 +0900104#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900105
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106static const char in_addrany[] = {0, 0, 0, 0};
107static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900108static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
109static 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 +0900110
Bernie Innocenti55864192018-08-30 04:05:20 +0900111static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900112 int a_af;
113 int a_addrlen;
114 int a_socklen;
115 int a_off;
116 const char* a_addrany;
117 const char* a_loopback;
118 int a_scoped;
119} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
121 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
123 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
124 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900125};
126
127struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900128 int e_af;
129 int e_socktype;
130 int e_protocol;
131 const char* e_protostr;
132 int e_wild;
133#define WILD_AF(ex) ((ex)->e_wild & 0x01)
134#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
135#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900136};
137
138static const struct explore explore[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
140 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
141 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
143 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
144 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
145 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
146 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
147 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
148 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900149};
150
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900153
154typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 HEADER hdr;
156 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900157} querybuf;
158
159struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 struct res_target* next;
161 const char* name; /* domain name */
162 int qclass, qtype; /* class and type of query */
163 u_char* answer; /* buffer to put answer */
164 int anslen; /* size of answer buffer */
165 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900166};
167
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900168static int str2number(const char*);
169static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
170 const struct android_net_context*);
171static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
172static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
173 const char*);
174static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
175 struct addrinfo**);
176static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
177static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
178static int get_portmatch(const struct addrinfo*, const char*);
179static int get_port(const struct addrinfo*, const char*, int);
180static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900181static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900184static int dns_getaddrinfo(const char* name, const addrinfo* pai,
185 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900186static void _sethtent(FILE**);
187static void _endhtent(FILE**);
188static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900189static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192static int res_queryN(const char*, struct res_target*, res_state);
193static int res_searchN(const char*, struct res_target*, res_state);
194static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900195
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196static const char* const ai_errlist[] = {
197 "Success",
198 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
199 "Temporary failure in name resolution", /* EAI_AGAIN */
200 "Invalid value for ai_flags", /* EAI_BADFLAGS */
201 "Non-recoverable failure in name resolution", /* EAI_FAIL */
202 "ai_family not supported", /* EAI_FAMILY */
203 "Memory allocation failure", /* EAI_MEMORY */
204 "No address associated with hostname", /* EAI_NODATA */
205 "hostname nor servname provided, or not known", /* EAI_NONAME */
206 "servname not supported for ai_socktype", /* EAI_SERVICE */
207 "ai_socktype not supported", /* EAI_SOCKTYPE */
208 "System error returned in errno", /* EAI_SYSTEM */
209 "Invalid value for hints", /* EAI_BADHINTS */
210 "Resolved protocol is unknown", /* EAI_PROTOCOL */
211 "Argument buffer overflow", /* EAI_OVERFLOW */
212 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900213};
214
215/* XXX macros that make external reference is BAD. */
216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217#define GET_AI(ai, afd, addr) \
218 do { \
219 /* external reference: pai, error, and label free */ \
220 (ai) = get_ai(pai, (afd), (addr)); \
221 if ((ai) == NULL) { \
222 error = EAI_MEMORY; \
223 goto free; \
224 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900225 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900226
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900227#define GET_PORT(ai, serv) \
228 do { \
229 /* external reference: error and label free */ \
230 error = get_port((ai), (serv), 0); \
231 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900232 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234#define GET_CANONNAME(ai, str) \
235 do { \
236 /* external reference: pai, error and label free */ \
237 error = get_canonname(pai, (ai), (str)); \
238 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900239 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900240
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900241#define ERR(err) \
242 do { \
243 /* external reference: error, and label bad */ \
244 error = (err); \
245 goto bad; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900246 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900247
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900248#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900249 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
250#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900252const char* gai_strerror(int ecode) {
253 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
254 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900255}
256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900257void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900258 while (ai) {
259 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900260 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900261 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900262 free(ai);
263 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900264 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900265}
266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267static int str2number(const char* p) {
268 char* ep;
269 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900270
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900271 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900272
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900273 if (*p == '\0') return -1;
274 ep = NULL;
275 errno = 0;
276 v = strtoul(p, &ep, 10);
277 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
278 return v;
279 else
280 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900281}
282
283/*
284 * The following functions determine whether IPv4 or IPv6 connectivity is
285 * available in order to implement AI_ADDRCONFIG.
286 *
287 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
288 * available, but whether addresses of the specified family are "configured
289 * on the local system". However, bionic doesn't currently support getifaddrs,
290 * so checking for connectivity is the next best thing.
291 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900292static int _have_ipv6(unsigned mark, uid_t uid) {
293 static const struct sockaddr_in6 sin6_test = {
294 .sin6_family = AF_INET6,
295 .sin6_addr.s6_addr = {// 2000::
296 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800297 sockaddr_union addr = {.sin6 = sin6_test};
298 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900299}
300
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900301static int _have_ipv4(unsigned mark, uid_t uid) {
302 static const struct sockaddr_in sin_test = {
303 .sin_family = AF_INET,
304 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
305 };
nuccachene172a4e2018-10-23 17:10:58 +0800306 sockaddr_union addr = {.sin = sin_test};
307 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900308}
309
310bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900311 int32_t tmp;
312 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
313 return false;
314 }
315 *result = ntohl(tmp);
316 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900317}
318
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900319int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
320 struct addrinfo** res) {
321 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900322}
323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900324int android_getaddrinfofornet(const char* hostname, const char* servname,
325 const struct addrinfo* hints, unsigned netid, unsigned mark,
326 struct addrinfo** res) {
327 struct android_net_context netcontext = {
328 .app_netid = netid,
329 .app_mark = mark,
330 .dns_netid = netid,
331 .dns_mark = mark,
332 .uid = NET_CONTEXT_INVALID_UID,
333 };
334 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900335}
336
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900337int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
338 const struct addrinfo* hints,
339 const struct android_net_context* netcontext,
340 struct addrinfo** res) {
341 struct addrinfo sentinel;
342 struct addrinfo* cur;
343 int error = 0;
344 struct addrinfo ai;
345 struct addrinfo ai0;
346 struct addrinfo* pai;
347 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900348
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900349 /* hostname is allowed to be NULL */
350 /* servname is allowed to be NULL */
351 /* hints is allowed to be NULL */
352 assert(res != NULL);
353 assert(netcontext != NULL);
354 memset(&sentinel, 0, sizeof(sentinel));
355 cur = &sentinel;
356 pai = &ai;
357 pai->ai_flags = 0;
358 pai->ai_family = PF_UNSPEC;
359 pai->ai_socktype = ANY;
360 pai->ai_protocol = ANY;
361 pai->ai_addrlen = 0;
362 pai->ai_canonname = NULL;
363 pai->ai_addr = NULL;
364 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900365
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900366 if (hostname == NULL && servname == NULL) return EAI_NONAME;
367 if (hints) {
368 /* error check for hints */
369 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
370 ERR(EAI_BADHINTS); /* xxx */
371 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
372 switch (hints->ai_family) {
373 case PF_UNSPEC:
374 case PF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900375 case PF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900376 break;
377 default:
378 ERR(EAI_FAMILY);
379 }
380 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900381
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900382 /*
383 * if both socktype/protocol are specified, check if they
384 * are meaningful combination.
385 */
386 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
387 for (ex = explore; ex->e_af >= 0; ex++) {
388 if (pai->ai_family != ex->e_af) continue;
389 if (ex->e_socktype == ANY) continue;
390 if (ex->e_protocol == ANY) continue;
391 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
392 ERR(EAI_BADHINTS);
393 }
394 }
395 }
396 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900397
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900398 /*
399 * check for special cases. (1) numeric servname is disallowed if
400 * socktype/protocol are left unspecified. (2) servname is disallowed
401 * for raw and other inet{,6} sockets.
402 */
403 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900404 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900405 ) {
406 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900407
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900408 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900409 pai->ai_family = PF_INET6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900410 }
411 error = get_portmatch(pai, servname);
412 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900413
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900414 *pai = ai0;
415 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900416
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900417 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 /* NULL hostname, or numeric hostname */
420 for (ex = explore; ex->e_af >= 0; ex++) {
421 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900422
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900423 /* PF_UNSPEC entries are prepared for DNS queries only */
424 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900425
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900426 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
427 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
428 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900429
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900430 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
431 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
432 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900433
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900434 if (hostname == NULL)
435 error = explore_null(pai, servname, &cur->ai_next);
436 else
437 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900438
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900439 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900440
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 while (cur->ai_next) cur = cur->ai_next;
442 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900443
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900444 /*
445 * XXX
446 * If numeric representation of AF1 can be interpreted as FQDN
447 * representation of AF2, we need to think again about the code below.
448 */
449 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900450
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900451 if (hostname == NULL) ERR(EAI_NODATA);
452 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900453
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 /*
455 * hostname as alphabetical name.
456 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
457 * outer loop by AFs.
458 */
459 for (ex = explore; ex->e_af >= 0; ex++) {
460 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900461
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900462 /* require exact match for family field */
463 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900464
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900465 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
466 continue;
467 }
468 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
469 continue;
470 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900471
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
473 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900476
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477 while (cur && cur->ai_next) cur = cur->ai_next;
478 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900479
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 /* XXX */
481 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 if (error) goto free;
484 if (error == 0) {
485 if (sentinel.ai_next) {
486 good:
487 *res = sentinel.ai_next;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900488 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 } else
490 error = EAI_FAIL;
491 }
492free:
493bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900494 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 *res = NULL;
496 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900497}
498
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900499// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
501 struct addrinfo** res, const struct android_net_context* netcontext) {
502 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900504
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900505 assert(pai != NULL);
506 /* hostname may be NULL */
507 /* servname may be NULL */
508 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900509
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900511
Bernie Innocenti948f6572018-09-12 21:32:42 +0900512 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900514
Bernie Innocenti948f6572018-09-12 21:32:42 +0900515 if (!files_getaddrinfo(hostname, pai, &result)) {
516 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900517 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900518 if (!error) {
519 struct addrinfo* cur;
520 for (cur = result; cur; cur = cur->ai_next) {
521 GET_PORT(cur, servname);
522 /* canonname should be filled already */
523 }
524 *res = result;
525 return 0;
526 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900527
528free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900529 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900530 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900531}
532
533/*
534 * hostname == NULL.
535 * passive socket -> anyaddr (0.0.0.0 or ::)
536 * non-passive socket -> localhost (127.0.0.1 or ::1)
537 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
539 int s;
540 const struct afd* afd;
541 struct addrinfo* cur;
542 struct addrinfo sentinel;
543 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900544
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900545 assert(pai != NULL);
546 /* servname may be NULL */
547 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900548
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 *res = NULL;
550 sentinel.ai_next = NULL;
551 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900552
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553 /*
554 * filter out AFs that are not supported by the kernel
555 * XXX errno?
556 */
557 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
558 if (s < 0) {
559 if (errno != EMFILE) return 0;
560 } else
561 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900562
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900563 /*
564 * if the servname does not match socktype/protocol, ignore it.
565 */
566 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 afd = find_afd(pai->ai_family);
569 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900570
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 if (pai->ai_flags & AI_PASSIVE) {
572 GET_AI(cur->ai_next, afd, afd->a_addrany);
573 /* xxx meaningless?
574 * GET_CANONNAME(cur->ai_next, "anyaddr");
575 */
576 GET_PORT(cur->ai_next, servname);
577 } else {
578 GET_AI(cur->ai_next, afd, afd->a_loopback);
579 /* xxx meaningless?
580 * GET_CANONNAME(cur->ai_next, "localhost");
581 */
582 GET_PORT(cur->ai_next, servname);
583 }
584 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900585
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900586 *res = sentinel.ai_next;
587 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900588
589free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900590 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900592}
593
594/*
595 * numeric hostname
596 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
598 struct addrinfo** res, const char* canonname) {
599 const struct afd* afd;
600 struct addrinfo* cur;
601 struct addrinfo sentinel;
602 int error;
603 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 assert(pai != NULL);
606 /* hostname may be NULL */
607 /* servname may be NULL */
608 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900609
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 *res = NULL;
611 sentinel.ai_next = NULL;
612 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900613
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900614 /*
615 * if the servname does not match socktype/protocol, ignore it.
616 */
617 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900618
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 afd = find_afd(pai->ai_family);
620 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900621
Ken Chen15c805a2018-10-17 00:19:59 +0800622 if (inet_pton(afd->a_af, hostname, pton) == 1) {
623 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
624 GET_AI(cur->ai_next, afd, pton);
625 GET_PORT(cur->ai_next, servname);
626 if ((pai->ai_flags & AI_CANONNAME)) {
627 /*
628 * Set the numeric address itself as
629 * the canonical name, based on a
630 * clarification in rfc2553bis-03.
631 */
632 GET_CANONNAME(cur->ai_next, canonname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633 }
Ken Chen15c805a2018-10-17 00:19:59 +0800634 while (cur->ai_next) cur = cur->ai_next;
635 } else
636 ERR(EAI_FAMILY); /*xxx*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900637 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 *res = sentinel.ai_next;
640 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900641
642free:
643bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900644 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900645 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900646}
647
648/*
649 * numeric hostname with scope
650 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
652 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653 const struct afd* afd;
654 struct addrinfo* cur;
655 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900656 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900657 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900658
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659 assert(pai != NULL);
660 /* hostname may be NULL */
661 /* servname may be NULL */
662 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900663
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664 /*
665 * if the servname does not match socktype/protocol, ignore it.
666 */
667 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669 afd = find_afd(pai->ai_family);
670 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900671
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900672 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900673
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900674 cp = strchr(hostname, SCOPE_DELIMITER);
675 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 /*
678 * Handle special case of <scoped_address><delimiter><scope id>
679 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900680 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900681 if (hostname2 == NULL) return EAI_MEMORY;
682 /* terminate at the delimiter */
683 hostname2[cp - hostname] = '\0';
684 addr = hostname2;
685 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900686
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687 error = explore_numeric(pai, addr, servname, res, hostname);
688 if (error == 0) {
689 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 for (cur = *res; cur; cur = cur->ai_next) {
692 if (cur->ai_family != AF_INET6) continue;
693 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
694 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
695 free(hostname2);
696 return (EAI_NODATA); /* XXX: is return OK? */
697 }
698 sin6->sin6_scope_id = scopeid;
699 }
700 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900701
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900702 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900704 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900705}
706
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900707static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
708 assert(pai != NULL);
709 assert(ai != NULL);
710 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900711
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900712 if ((pai->ai_flags & AI_CANONNAME) != 0) {
713 ai->ai_canonname = strdup(str);
714 if (ai->ai_canonname == NULL) return EAI_MEMORY;
715 }
716 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900717}
718
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
720 const char* addr) {
721 char* p;
722 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900723
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900724 assert(pai != NULL);
725 assert(afd != NULL);
726 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900727
nuccachene21023a2018-09-11 11:13:44 +0800728 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900729 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731 memcpy(ai, pai, sizeof(struct addrinfo));
732 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800733 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900736 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
737 p = (char*) (void*) (ai->ai_addr);
738 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
739 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900740}
741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742static int get_portmatch(const struct addrinfo* ai, const char* servname) {
743 assert(ai != NULL);
744 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900745
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900746 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900747}
748
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900749static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
750 const char* proto;
751 struct servent* sp;
752 int port;
753 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900754
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900755 assert(ai != NULL);
756 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 if (servname == NULL) return 0;
759 switch (ai->ai_family) {
760 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900761 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 break;
763 default:
764 return 0;
765 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900766
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900767 switch (ai->ai_socktype) {
768 case SOCK_RAW:
769 return EAI_SERVICE;
770 case SOCK_DGRAM:
771 case SOCK_STREAM:
772 allownumeric = 1;
773 break;
774 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900775 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 break;
777 default:
778 return EAI_SOCKTYPE;
779 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 port = str2number(servname);
782 if (port >= 0) {
783 if (!allownumeric) return EAI_SERVICE;
784 if (port < 0 || port > 65535) return EAI_SERVICE;
785 port = htons(port);
786 } else {
787 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900788
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900789 switch (ai->ai_socktype) {
790 case SOCK_DGRAM:
791 proto = "udp";
792 break;
793 case SOCK_STREAM:
794 proto = "tcp";
795 break;
796 default:
797 proto = NULL;
798 break;
799 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
802 port = sp->s_port;
803 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900805 if (!matchonly) {
806 switch (ai->ai_family) {
807 case AF_INET:
808 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
809 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900810 case AF_INET6:
811 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
812 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900813 }
814 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900815
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900816 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817}
818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819static const struct afd* find_afd(int af) {
820 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822 if (af == PF_UNSPEC) return NULL;
823 for (afd = afdl; afd->a_af; afd++) {
824 if (afd->a_af == af) return afd;
825 }
826 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900827}
828
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900829// Convert a string to a scope identifier.
830static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 u_long lscopeid;
832 struct in6_addr* a6;
833 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835 assert(scope != NULL);
836 assert(sin6 != NULL);
837 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900838
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900840
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 /* empty scopeid portion is invalid */
842 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900843
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900844 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
845 /*
846 * We currently assume a one-to-one mapping between links
847 * and interfaces, so we simply use interface indices for
848 * like-local scopes.
849 */
850 *scopeid = if_nametoindex(scope);
851 if (*scopeid == 0) goto trynumeric;
852 return 0;
853 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900854
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900855 /* still unclear about literal, allow numeric only - placeholder */
856 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
857 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
858 goto trynumeric;
859 else
860 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900861
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900862 /* try to convert to a numeric id as a last resort */
863trynumeric:
864 errno = 0;
865 lscopeid = strtoul(scope, &ep, 10);
866 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
867 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
868 return 0;
869 else
870 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900871}
Bernie Innocenti55864192018-08-30 04:05:20 +0900872
873/* code duplicate with gethnamaddr.c */
874
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900876
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900877#define BOUNDED_INCR(x) \
878 do { \
879 BOUNDS_CHECK(cp, x); \
880 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900881 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900882
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900883#define BOUNDS_CHECK(ptr, count) \
884 do { \
885 if (eom - (ptr) < (count)) { \
886 h_errno = NO_RECOVERY; \
887 return NULL; \
888 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900889 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900890
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900891static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
892 const struct addrinfo* pai) {
893 struct addrinfo sentinel, *cur;
894 struct addrinfo ai;
895 const struct afd* afd;
896 char* canonname;
897 const HEADER* hp;
898 const u_char* cp;
899 int n;
900 const u_char* eom;
901 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900902 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 int haveanswer, had_error;
904 char tbuf[MAXDNAME];
905 int (*name_ok)(const char*);
906 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900907
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900908 assert(answer != NULL);
909 assert(qname != NULL);
910 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900911
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912 memset(&sentinel, 0, sizeof(sentinel));
913 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900914
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900915 canonname = NULL;
916 eom = answer->buf + anslen;
917 switch (qtype) {
918 case T_A:
919 case T_AAAA:
920 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
921 name_ok = res_hnok;
922 break;
923 default:
924 return NULL; /* XXX should be abort(); */
925 }
926 /*
927 * find first satisfactory answer
928 */
929 hp = &answer->hdr;
930 ancount = ntohs(hp->ancount);
931 qdcount = ntohs(hp->qdcount);
932 bp = hostbuf;
933 ep = hostbuf + sizeof hostbuf;
934 cp = answer->buf;
935 BOUNDED_INCR(HFIXEDSZ);
936 if (qdcount != 1) {
937 h_errno = NO_RECOVERY;
938 return (NULL);
939 }
940 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
941 if ((n < 0) || !(*name_ok)(bp)) {
942 h_errno = NO_RECOVERY;
943 return (NULL);
944 }
945 BOUNDED_INCR(n + QFIXEDSZ);
946 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
947 /* res_send() has already verified that the query name is the
948 * same as the one we sent; this just gets the expanded name
949 * (i.e., with the succeeding search-domain tacked on).
950 */
951 n = strlen(bp) + 1; /* for the \0 */
952 if (n >= MAXHOSTNAMELEN) {
953 h_errno = NO_RECOVERY;
954 return (NULL);
955 }
956 canonname = bp;
957 bp += n;
958 /* The qname can be abbreviated, but h_name is now absolute. */
959 qname = canonname;
960 }
961 haveanswer = 0;
962 had_error = 0;
963 while (ancount-- > 0 && cp < eom && !had_error) {
964 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
965 if ((n < 0) || !(*name_ok)(bp)) {
966 had_error++;
967 continue;
968 }
969 cp += n; /* name */
970 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900971 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900972 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900973 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900974 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900975 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900976 cp += INT16SZ; /* len */
977 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900978 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900979 /* XXX - debug? syslog? */
980 cp += n;
981 continue; /* XXX - had_error++ ? */
982 }
983 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
984 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
985 if ((n < 0) || !(*name_ok)(tbuf)) {
986 had_error++;
987 continue;
988 }
989 cp += n;
990 /* Get canonical name. */
991 n = strlen(tbuf) + 1; /* for the \0 */
992 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
993 had_error++;
994 continue;
995 }
996 strlcpy(bp, tbuf, (size_t)(ep - bp));
997 canonname = bp;
998 bp += n;
999 continue;
1000 }
1001 if (qtype == T_ANY) {
1002 if (!(type == T_A || type == T_AAAA)) {
1003 cp += n;
1004 continue;
1005 }
1006 } else if (type != qtype) {
1007 if (type != T_KEY && type != T_SIG)
1008 syslog(LOG_NOTICE | LOG_AUTH,
1009 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1010 p_class(C_IN), p_type(qtype), p_type(type));
1011 cp += n;
1012 continue; /* XXX - had_error++ ? */
1013 }
1014 switch (type) {
1015 case T_A:
1016 case T_AAAA:
1017 if (strcasecmp(canonname, bp) != 0) {
1018 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1019 cp += n;
1020 continue; /* XXX - had_error++ ? */
1021 }
1022 if (type == T_A && n != INADDRSZ) {
1023 cp += n;
1024 continue;
1025 }
1026 if (type == T_AAAA && n != IN6ADDRSZ) {
1027 cp += n;
1028 continue;
1029 }
1030 if (type == T_AAAA) {
1031 struct in6_addr in6;
1032 memcpy(&in6, cp, IN6ADDRSZ);
1033 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1034 cp += n;
1035 continue;
1036 }
1037 }
1038 if (!haveanswer) {
1039 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001040
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001041 canonname = bp;
1042 nn = strlen(bp) + 1; /* for the \0 */
1043 bp += nn;
1044 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001045
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001046 /* don't overwrite pai */
1047 ai = *pai;
1048 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1049 afd = find_afd(ai.ai_family);
1050 if (afd == NULL) {
1051 cp += n;
1052 continue;
1053 }
1054 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1055 if (cur->ai_next == NULL) had_error++;
1056 while (cur && cur->ai_next) cur = cur->ai_next;
1057 cp += n;
1058 break;
1059 default:
1060 abort();
1061 }
1062 if (!had_error) haveanswer++;
1063 }
1064 if (haveanswer) {
1065 if (!canonname)
1066 (void) get_canonname(pai, sentinel.ai_next, qname);
1067 else
1068 (void) get_canonname(pai, sentinel.ai_next, canonname);
1069 h_errno = NETDB_SUCCESS;
1070 return sentinel.ai_next;
1071 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001072
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001073 h_errno = NO_RECOVERY;
1074 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001075}
1076
1077struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001078 struct addrinfo* ai;
1079 int has_src_addr;
1080 sockaddr_union src_addr;
1081 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001082};
1083
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001084static int _get_scope(const struct sockaddr* addr) {
1085 if (addr->sa_family == AF_INET6) {
1086 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1087 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1088 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1089 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1090 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1091 /*
1092 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1093 * link-local scope.
1094 */
1095 return IPV6_ADDR_SCOPE_LINKLOCAL;
1096 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1097 return IPV6_ADDR_SCOPE_SITELOCAL;
1098 } else {
1099 return IPV6_ADDR_SCOPE_GLOBAL;
1100 }
1101 } else if (addr->sa_family == AF_INET) {
1102 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1103 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001104
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001105 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1106 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1107 return IPV6_ADDR_SCOPE_LINKLOCAL;
1108 } else {
1109 /*
1110 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1111 * and shared addresses (100.64.0.0/10), are assigned global scope.
1112 */
1113 return IPV6_ADDR_SCOPE_GLOBAL;
1114 }
1115 } else {
1116 /*
1117 * This should never happen.
1118 * Return a scope with low priority as a last resort.
1119 */
1120 return IPV6_ADDR_SCOPE_NODELOCAL;
1121 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001122}
1123
1124/* These macros are modelled after the ones in <netinet/in6.h>. */
1125
1126/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001127#define IN6_IS_ADDR_TEREDO(a) \
1128 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001129
1130/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001131#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001132
1133/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001134#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001135
1136/*
1137 * Get the label for a given IPv4/IPv6 address.
1138 * RFC 6724, section 2.1.
1139 */
1140
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001141static int _get_label(const struct sockaddr* addr) {
1142 if (addr->sa_family == AF_INET) {
1143 return 4;
1144 } else if (addr->sa_family == AF_INET6) {
1145 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1146 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1147 return 0;
1148 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1149 return 4;
1150 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1151 return 2;
1152 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1153 return 5;
1154 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1155 return 13;
1156 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1157 return 3;
1158 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1159 return 11;
1160 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1161 return 12;
1162 } else {
1163 /* All other IPv6 addresses, including global unicast addresses. */
1164 return 1;
1165 }
1166 } else {
1167 /*
1168 * This should never happen.
1169 * Return a semi-random label as a last resort.
1170 */
1171 return 1;
1172 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001173}
1174
1175/*
1176 * Get the precedence for a given IPv4/IPv6 address.
1177 * RFC 6724, section 2.1.
1178 */
1179
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001180static int _get_precedence(const struct sockaddr* addr) {
1181 if (addr->sa_family == AF_INET) {
1182 return 35;
1183 } else if (addr->sa_family == AF_INET6) {
1184 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1185 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1186 return 50;
1187 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1188 return 35;
1189 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1190 return 30;
1191 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1192 return 5;
1193 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1194 return 3;
1195 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1196 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1197 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1198 return 1;
1199 } else {
1200 /* All other IPv6 addresses, including global unicast addresses. */
1201 return 40;
1202 }
1203 } else {
1204 return 1;
1205 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001206}
1207
1208/*
1209 * Find number of matching initial bits between the two addresses a1 and a2.
1210 */
1211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001212static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1213 const char* p1 = (const char*) a1;
1214 const char* p2 = (const char*) a2;
1215 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001217 for (i = 0; i < sizeof(*a1); ++i) {
1218 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001220 if (p1[i] == p2[i]) {
1221 continue;
1222 }
1223 x = p1[i] ^ p2[i];
1224 for (j = 0; j < CHAR_BIT; ++j) {
1225 if (x & (1 << (CHAR_BIT - 1))) {
1226 return i * CHAR_BIT + j;
1227 }
1228 x <<= 1;
1229 }
1230 }
1231 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001232}
1233
1234/*
1235 * Compare two source/destination address pairs.
1236 * RFC 6724, section 6.
1237 */
1238
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001239static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1240 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1241 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1242 int scope_src1, scope_dst1, scope_match1;
1243 int scope_src2, scope_dst2, scope_match2;
1244 int label_src1, label_dst1, label_match1;
1245 int label_src2, label_dst2, label_match2;
1246 int precedence1, precedence2;
1247 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001248
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001249 /* Rule 1: Avoid unusable destinations. */
1250 if (a1->has_src_addr != a2->has_src_addr) {
1251 return a2->has_src_addr - a1->has_src_addr;
1252 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001254 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001255 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001256 scope_dst1 = _get_scope(a1->ai->ai_addr);
1257 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001258
nuccachene172a4e2018-10-23 17:10:58 +08001259 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001260 scope_dst2 = _get_scope(a2->ai->ai_addr);
1261 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001262
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001263 if (scope_match1 != scope_match2) {
1264 return scope_match2 - scope_match1;
1265 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001267 /*
1268 * Rule 3: Avoid deprecated addresses.
1269 * TODO(sesse): We don't currently have a good way of finding this.
1270 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001271
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001272 /*
1273 * Rule 4: Prefer home addresses.
1274 * TODO(sesse): We don't currently have a good way of finding this.
1275 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001276
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001277 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001278 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001279 label_dst1 = _get_label(a1->ai->ai_addr);
1280 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001281
nuccachene172a4e2018-10-23 17:10:58 +08001282 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001283 label_dst2 = _get_label(a2->ai->ai_addr);
1284 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001286 if (label_match1 != label_match2) {
1287 return label_match2 - label_match1;
1288 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001289
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001290 /* Rule 6: Prefer higher precedence. */
1291 precedence1 = _get_precedence(a1->ai->ai_addr);
1292 precedence2 = _get_precedence(a2->ai->ai_addr);
1293 if (precedence1 != precedence2) {
1294 return precedence2 - precedence1;
1295 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001296
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001297 /*
1298 * Rule 7: Prefer native transport.
1299 * TODO(sesse): We don't currently have a good way of finding this.
1300 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001301
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001302 /* Rule 8: Prefer smaller scope. */
1303 if (scope_dst1 != scope_dst2) {
1304 return scope_dst1 - scope_dst2;
1305 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001306
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001307 /*
1308 * Rule 9: Use longest matching prefix.
1309 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1310 * to work very well directly applied to IPv4. (glibc uses information from
1311 * the routing table for a custom IPv4 implementation here.)
1312 */
1313 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1314 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001315 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001316 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001317 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001318 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1319 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1320 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1321 if (prefixlen1 != prefixlen2) {
1322 return prefixlen2 - prefixlen1;
1323 }
1324 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001325
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001326 /*
1327 * Rule 10: Leave the order unchanged.
1328 * We need this since qsort() is not necessarily stable.
1329 */
1330 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001331}
1332
1333/*
1334 * Find the source address that will be used if trying to connect to the given
1335 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1336 *
1337 * Returns 1 if a source address was found, 0 if the address is unreachable,
1338 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1339 * undefined.
1340 */
1341
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001342static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1343 uid_t uid) {
1344 int sock;
1345 int ret;
1346 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001347
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001348 switch (addr->sa_family) {
1349 case AF_INET:
1350 len = sizeof(struct sockaddr_in);
1351 break;
1352 case AF_INET6:
1353 len = sizeof(struct sockaddr_in6);
1354 break;
1355 default:
1356 /* No known usable source address for non-INET families. */
1357 return 0;
1358 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001359
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001360 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1361 if (sock == -1) {
1362 if (errno == EAFNOSUPPORT) {
1363 return 0;
1364 } else {
1365 return -1;
1366 }
1367 }
1368 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1369 close(sock);
1370 return 0;
1371 }
1372 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1373 close(sock);
1374 return 0;
1375 }
1376 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001377 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001378 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001379
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001380 if (ret == -1) {
1381 close(sock);
1382 return 0;
1383 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001384
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1386 close(sock);
1387 return -1;
1388 }
1389 close(sock);
1390 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001391}
1392
1393/*
1394 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1395 * Will leave the list unchanged if an error occurs.
1396 */
1397
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001398static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1399 struct addrinfo* cur;
1400 int nelem = 0, i;
1401 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001402
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001403 cur = list_sentinel->ai_next;
1404 while (cur) {
1405 ++nelem;
1406 cur = cur->ai_next;
1407 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001408
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001409 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1410 if (elems == NULL) {
1411 goto error;
1412 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001413
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001414 /*
1415 * Convert the linked list to an array that also contains the candidate
1416 * source address for each destination address.
1417 */
1418 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1419 int has_src_addr;
1420 assert(cur != NULL);
1421 elems[i].ai = cur;
1422 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001423
nuccachene172a4e2018-10-23 17:10:58 +08001424 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001425 if (has_src_addr == -1) {
1426 goto error;
1427 }
1428 elems[i].has_src_addr = has_src_addr;
1429 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001430
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001431 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1432 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001433
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001434 list_sentinel->ai_next = elems[0].ai;
1435 for (i = 0; i < nelem - 1; ++i) {
1436 elems[i].ai->ai_next = elems[i + 1].ai;
1437 }
1438 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001439
1440error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001441 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001442}
1443
Bernie Innocenti948f6572018-09-12 21:32:42 +09001444static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1445 const android_net_context* netcontext, addrinfo** rv) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001446 struct addrinfo* ai;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001447 struct addrinfo sentinel, *cur;
1448 struct res_target q, q2;
1449 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001450
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001451 memset(&q, 0, sizeof(q));
1452 memset(&q2, 0, sizeof(q2));
1453 memset(&sentinel, 0, sizeof(sentinel));
1454 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001455
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001456 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001457 if (buf == NULL) {
1458 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001459 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001460 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001461 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001462 if (buf2 == NULL) {
1463 free(buf);
1464 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001465 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001466 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001467
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001468 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001469 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001470 /* prefer IPv6 */
1471 q.name = name;
1472 q.qclass = C_IN;
1473 q.answer = buf->buf;
1474 q.anslen = sizeof(buf->buf);
1475 int query_ipv6 = 1, query_ipv4 = 1;
1476 if (pai->ai_flags & AI_ADDRCONFIG) {
1477 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1478 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1479 }
1480 if (query_ipv6) {
1481 q.qtype = T_AAAA;
1482 if (query_ipv4) {
1483 q.next = &q2;
1484 q2.name = name;
1485 q2.qclass = C_IN;
1486 q2.qtype = T_A;
1487 q2.answer = buf2->buf;
1488 q2.anslen = sizeof(buf2->buf);
1489 }
1490 } else if (query_ipv4) {
1491 q.qtype = T_A;
1492 } else {
1493 free(buf);
1494 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001495 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001496 }
1497 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001498 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001499 case AF_INET:
1500 q.name = name;
1501 q.qclass = C_IN;
1502 q.qtype = T_A;
1503 q.answer = buf->buf;
1504 q.anslen = sizeof(buf->buf);
1505 break;
1506 case AF_INET6:
1507 q.name = name;
1508 q.qclass = C_IN;
1509 q.qtype = T_AAAA;
1510 q.answer = buf->buf;
1511 q.anslen = sizeof(buf->buf);
1512 break;
1513 default:
1514 free(buf);
1515 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001516 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001517 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001518
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001519 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001520 if (res == NULL) {
1521 free(buf);
1522 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001523 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001524 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001525
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001526 /* this just sets our netid val in the thread private data so we don't have to
1527 * modify the api's all the way down to res_send.c's res_nsend. We could
1528 * fully populate the thread private data here, but if we get down there
1529 * and have a cache hit that would be wasted, so we do the rest there on miss
1530 */
1531 res_setnetcontext(res, netcontext);
1532 if (res_searchN(name, &q, res) < 0) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001533 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001534 free(buf);
1535 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001536 return EAI_NODATA; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001537 }
1538 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1539 if (ai) {
1540 cur->ai_next = ai;
1541 while (cur && cur->ai_next) cur = cur->ai_next;
1542 }
1543 if (q.next) {
1544 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1545 if (ai) cur->ai_next = ai;
1546 }
1547 free(buf);
1548 free(buf2);
1549 if (sentinel.ai_next == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001550 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001551 switch (h_errno) {
1552 case HOST_NOT_FOUND:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001553 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001554 case TRY_AGAIN:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001555 return EAI_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001556 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001557 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001558 }
1559 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001560
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001561 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001562
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001563 res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001564
Bernie Innocenti948f6572018-09-12 21:32:42 +09001565 *rv = sentinel.ai_next;
1566 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001567}
1568
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001569static void _sethtent(FILE** hostf) {
1570 if (!*hostf)
1571 *hostf = fopen(_PATH_HOSTS, "re");
1572 else
1573 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001574}
1575
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001576static void _endhtent(FILE** hostf) {
1577 if (*hostf) {
1578 (void) fclose(*hostf);
1579 *hostf = NULL;
1580 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001581}
1582
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001583static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1584 char* p;
1585 char *cp, *tname, *cname;
1586 struct addrinfo hints, *res0, *res;
1587 int error;
1588 const char* addr;
1589 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001590
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001591 assert(name != NULL);
1592 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001593
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001594 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1595again:
1596 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1597 if (*p == '#') goto again;
1598 if (!(cp = strpbrk(p, "#\n"))) goto again;
1599 *cp = '\0';
1600 if (!(cp = strpbrk(p, " \t"))) goto again;
1601 *cp++ = '\0';
1602 addr = p;
1603 /* if this is not something we're looking for, skip it. */
1604 cname = NULL;
1605 while (cp && *cp) {
1606 if (*cp == ' ' || *cp == '\t') {
1607 cp++;
1608 continue;
1609 }
1610 if (!cname) cname = cp;
1611 tname = cp;
1612 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1613 // fprintf(stderr, "\ttname = '%s'", tname);
1614 if (strcasecmp(name, tname) == 0) goto found;
1615 }
1616 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001617
1618found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001619 hints = *pai;
1620 hints.ai_flags = AI_NUMERICHOST;
1621 error = getaddrinfo(addr, NULL, &hints, &res0);
1622 if (error) goto again;
1623 for (res = res0; res; res = res->ai_next) {
1624 /* cover it up */
1625 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001626
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001627 if (pai->ai_flags & AI_CANONNAME) {
1628 if (get_canonname(pai, res, cname) != 0) {
1629 freeaddrinfo(res0);
1630 goto again;
1631 }
1632 }
1633 }
1634 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001635}
1636
Bernie Innocenti948f6572018-09-12 21:32:42 +09001637static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001638 struct addrinfo sentinel, *cur;
1639 struct addrinfo* p;
1640 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001641
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001642 memset(&sentinel, 0, sizeof(sentinel));
1643 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001644
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001645 _sethtent(&hostf);
1646 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1647 cur->ai_next = p;
1648 while (cur && cur->ai_next) cur = cur->ai_next;
1649 }
1650 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001651
Bernie Innocenti948f6572018-09-12 21:32:42 +09001652 *res = sentinel.ai_next;
1653 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001654}
1655
1656/* resolver logic */
1657
1658/*
1659 * Formulate a normal query, send, and await answer.
1660 * Returned answer is placed in supplied buffer "answer".
1661 * Perform preliminary check of answer, returning success only
1662 * if no error is indicated and the answer count is nonzero.
1663 * Return the size of the response on success, -1 on error.
1664 * Error number is left in h_errno.
1665 *
1666 * Caller must parse answer and determine whether it answers the question.
1667 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001668static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1669 res_state res) {
1670 u_char buf[MAXPACKET];
1671 HEADER* hp;
1672 int n;
1673 struct res_target* t;
1674 int rcode;
1675 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001677 assert(name != NULL);
1678 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001680 rcode = NOERROR;
1681 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001683 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001684 u_char* answer;
1685 int anslen;
1686 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001687
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001688 hp = (HEADER*) (void*) t->answer;
1689 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001691 again:
1692 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001694 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001695 int cl = t->qclass;
1696 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001697 answer = t->answer;
1698 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001699#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001700 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001701#endif
1702
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001703 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001704 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1705 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1706 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001707 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001708#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001709 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001710#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001711 h_errno = NO_RECOVERY;
1712 return n;
1713 }
1714 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001715
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001716 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1717 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001718 /* if the query choked with EDNS0, retry without EDNS0 */
1719 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1720 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1721 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001722#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001724#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001725 goto again;
1726 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001727#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001728 if (res->options & RES_DEBUG)
1729 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001730#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001731 continue;
1732 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001733
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001734 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001735
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001736 t->n = n;
1737 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001739 if (ancount == 0) {
1740 switch (rcode) {
1741 case NXDOMAIN:
1742 h_errno = HOST_NOT_FOUND;
1743 break;
1744 case SERVFAIL:
1745 h_errno = TRY_AGAIN;
1746 break;
1747 case NOERROR:
1748 h_errno = NO_DATA;
1749 break;
1750 case FORMERR:
1751 case NOTIMP:
1752 case REFUSED:
1753 default:
1754 h_errno = NO_RECOVERY;
1755 break;
1756 }
1757 return -1;
1758 }
1759 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001760}
1761
1762/*
1763 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1764 * Return the size of the response on success, -1 on error.
1765 * If enabled, implement search rules until answer or unrecoverable failure
1766 * is detected. Error code, if any, is left in h_errno.
1767 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001768static int res_searchN(const char* name, struct res_target* target, res_state res) {
1769 const char *cp, *const *domain;
1770 HEADER* hp;
1771 u_int dots;
1772 int trailing_dot, ret, saved_herrno;
1773 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001774
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001775 assert(name != NULL);
1776 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001778 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001780 errno = 0;
1781 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1782 dots = 0;
1783 for (cp = name; *cp; cp++) dots += (*cp == '.');
1784 trailing_dot = 0;
1785 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001786
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001787 /*
1788 * If there are dots in the name already, let's just give it a try
1789 * 'as is'. The threshold can be set with the "ndots" option.
1790 */
1791 saved_herrno = -1;
1792 if (dots >= res->ndots) {
1793 ret = res_querydomainN(name, NULL, target, res);
1794 if (ret > 0) return (ret);
1795 saved_herrno = h_errno;
1796 tried_as_is++;
1797 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001798
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001799 /*
1800 * We do at least one level of search if
1801 * - there is no dot and RES_DEFNAME is set, or
1802 * - there is at least one dot, there is no trailing dot,
1803 * and RES_DNSRCH is set.
1804 */
1805 if ((!dots && (res->options & RES_DEFNAMES)) ||
1806 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1807 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001808
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001809 /* Unfortunately we need to set stuff up before
1810 * the domain stuff is tried. Will have a better
1811 * fix after thread pools are used.
1812 */
1813 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001815 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1816 ret = res_querydomainN(name, *domain, target, res);
1817 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001819 /*
1820 * If no server present, give up.
1821 * If name isn't found in this domain,
1822 * keep trying higher domains in the search list
1823 * (if that's enabled).
1824 * On a NO_DATA error, keep trying, otherwise
1825 * a wildcard entry of another type could keep us
1826 * from finding this entry higher in the domain.
1827 * If we get some other error (negative answer or
1828 * server failure), then stop searching up,
1829 * but try the input name below in case it's
1830 * fully-qualified.
1831 */
1832 if (errno == ECONNREFUSED) {
1833 h_errno = TRY_AGAIN;
1834 return -1;
1835 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001836
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001837 switch (h_errno) {
1838 case NO_DATA:
1839 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001840 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001841 case HOST_NOT_FOUND:
1842 /* keep trying */
1843 break;
1844 case TRY_AGAIN:
1845 if (hp->rcode == SERVFAIL) {
1846 /* try next search element, if any */
1847 got_servfail++;
1848 break;
1849 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001850 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001851 default:
1852 /* anything else implies that we're done */
1853 done++;
1854 }
1855 /*
1856 * if we got here for some reason other than DNSRCH,
1857 * we only wanted one iteration of the loop, so stop.
1858 */
1859 if (!(res->options & RES_DNSRCH)) done++;
1860 }
1861 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001862
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001863 /*
1864 * if we have not already tried the name "as is", do that now.
1865 * note that we do this regardless of how many dots were in the
1866 * name or whether it ends with a dot.
1867 */
1868 if (!tried_as_is) {
1869 ret = res_querydomainN(name, NULL, target, res);
1870 if (ret > 0) return ret;
1871 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001873 /*
1874 * if we got here, we didn't satisfy the search.
1875 * if we did an initial full query, return that query's h_errno
1876 * (note that we wouldn't be here if that query had succeeded).
1877 * else if we ever got a nodata, send that back as the reason.
1878 * else send back meaningless h_errno, that being the one from
1879 * the last DNSRCH we did.
1880 */
1881 if (saved_herrno != -1)
1882 h_errno = saved_herrno;
1883 else if (got_nodata)
1884 h_errno = NO_DATA;
1885 else if (got_servfail)
1886 h_errno = TRY_AGAIN;
1887 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001888}
1889
1890/*
1891 * Perform a call on res_query on the concatenation of name and domain,
1892 * removing a trailing dot from name if domain is NULL.
1893 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001894static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1895 res_state res) {
1896 char nbuf[MAXDNAME];
1897 const char* longname = nbuf;
1898 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001899
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001900 assert(name != NULL);
1901 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001902
1903#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001904 if (res->options & RES_DEBUG)
1905 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001906#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001907 if (domain == NULL) {
1908 /*
1909 * Check for trailing '.';
1910 * copy without '.' if present.
1911 */
1912 n = strlen(name);
1913 if (n + 1 > sizeof(nbuf)) {
1914 h_errno = NO_RECOVERY;
1915 return -1;
1916 }
1917 if (n > 0 && name[--n] == '.') {
1918 strncpy(nbuf, name, n);
1919 nbuf[n] = '\0';
1920 } else
1921 longname = name;
1922 } else {
1923 n = strlen(name);
1924 d = strlen(domain);
1925 if (n + 1 + d + 1 > sizeof(nbuf)) {
1926 h_errno = NO_RECOVERY;
1927 return -1;
1928 }
1929 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1930 }
1931 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001932}