blob: 8bd0aac3852d566a408d79b38004047f97699ce9 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
2/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
Bernie Innocenti55864192018-08-30 04:05:20 +090033#include <arpa/inet.h>
34#include <arpa/nameser.h>
35#include <assert.h>
36#include <ctype.h>
37#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090038#include <fcntl.h>
39#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090040#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090041#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090042#include <stdbool.h>
43#include <stddef.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090047#include <sys/param.h>
48#include <sys/socket.h>
49#include <sys/stat.h>
50#include <sys/types.h>
51#include <sys/un.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090052#include <syslog.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090053#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090054
Bernie Innocenti189eb502018-10-01 23:10:18 +090055#include "netd_resolv/resolv.h"
56#include "resolv_cache.h"
57#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090058
Bernie Innocenti55864192018-08-30 04:05:20 +090059#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +090060
Bernie Innocenti93a31342018-12-12 00:43:02 +090061const char in_addrany[] = {0, 0, 0, 0};
62const char in_loopback[] = {127, 0, 0, 1};
63const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
64const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +090065
Bernie Innocenti93a31342018-12-12 00:43:02 +090066const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090067 int a_af;
68 int a_addrlen;
69 int a_socklen;
70 int a_off;
71 const char* a_addrany;
72 const char* a_loopback;
73 int a_scoped;
74} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090075 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
76 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
78 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
79 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +090080};
81
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090082struct Explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090083 int e_af;
84 int e_socktype;
85 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090086 int e_wild;
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090087#define WILD_AF(ex) ((ex).e_wild & 0x01)
88#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
89#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +090090};
91
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090092const Explore explore_options[] = {
Ken Chen3270cf52018-11-07 01:20:48 +080093 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
94 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
95 {PF_INET6, SOCK_RAW, ANY, 0x05},
96 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
97 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
98 {PF_INET, SOCK_RAW, ANY, 0x05},
99 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
100 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
101 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900102};
103
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900104#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900105#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900106
107typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900108 HEADER hdr;
109 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900110} querybuf;
111
112struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113 struct res_target* next;
114 const char* name; /* domain name */
115 int qclass, qtype; /* class and type of query */
116 u_char* answer; /* buffer to put answer */
117 int anslen; /* size of answer buffer */
118 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900119};
120
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121static int str2number(const char*);
122static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
123 const struct android_net_context*);
124static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
125static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
126 const char*);
127static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
128 struct addrinfo**);
129static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
130static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
131static int get_portmatch(const struct addrinfo*, const char*);
132static int get_port(const struct addrinfo*, const char*, int);
133static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900134static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900135
Hungming Chend57ade02018-12-25 15:47:47 +0800136static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
137 int* herrno);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900138static int dns_getaddrinfo(const char* name, const addrinfo* pai,
139 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900140static void _sethtent(FILE**);
141static void _endhtent(FILE**);
142static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900143static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900144static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900145
Hungming Chen7f0d3292018-12-27 18:33:19 +0800146static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
147static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800148static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800149 int* herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900150
Bernie Innocenti93a31342018-12-12 00:43:02 +0900151const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 "Success",
153 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
154 "Temporary failure in name resolution", /* EAI_AGAIN */
155 "Invalid value for ai_flags", /* EAI_BADFLAGS */
156 "Non-recoverable failure in name resolution", /* EAI_FAIL */
157 "ai_family not supported", /* EAI_FAMILY */
158 "Memory allocation failure", /* EAI_MEMORY */
159 "No address associated with hostname", /* EAI_NODATA */
160 "hostname nor servname provided, or not known", /* EAI_NONAME */
161 "servname not supported for ai_socktype", /* EAI_SERVICE */
162 "ai_socktype not supported", /* EAI_SOCKTYPE */
163 "System error returned in errno", /* EAI_SYSTEM */
164 "Invalid value for hints", /* EAI_BADHINTS */
165 "Resolved protocol is unknown", /* EAI_PROTOCOL */
166 "Argument buffer overflow", /* EAI_OVERFLOW */
167 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900168};
169
170/* XXX macros that make external reference is BAD. */
171
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172#define GET_AI(ai, afd, addr) \
173 do { \
174 /* external reference: pai, error, and label free */ \
175 (ai) = get_ai(pai, (afd), (addr)); \
176 if ((ai) == NULL) { \
177 error = EAI_MEMORY; \
178 goto free; \
179 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900180 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900181
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182#define GET_PORT(ai, serv) \
183 do { \
184 /* external reference: error and label free */ \
185 error = get_port((ai), (serv), 0); \
186 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900187 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900190 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
191#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900192
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900193const char* gai_strerror(int ecode) {
194 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
195 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900196}
197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900199 while (ai) {
200 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900202 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203 free(ai);
204 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900205 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900206}
207
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208static int str2number(const char* p) {
209 char* ep;
210 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900213
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214 if (*p == '\0') return -1;
215 ep = NULL;
216 errno = 0;
217 v = strtoul(p, &ep, 10);
218 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
219 return v;
220 else
221 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900222}
223
224/*
225 * The following functions determine whether IPv4 or IPv6 connectivity is
226 * available in order to implement AI_ADDRCONFIG.
227 *
228 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
229 * available, but whether addresses of the specified family are "configured
230 * on the local system". However, bionic doesn't currently support getifaddrs,
231 * so checking for connectivity is the next best thing.
232 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233static int _have_ipv6(unsigned mark, uid_t uid) {
234 static const struct sockaddr_in6 sin6_test = {
235 .sin6_family = AF_INET6,
236 .sin6_addr.s6_addr = {// 2000::
237 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800238 sockaddr_union addr = {.sin6 = sin6_test};
239 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900240}
241
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242static int _have_ipv4(unsigned mark, uid_t uid) {
243 static const struct sockaddr_in sin_test = {
244 .sin_family = AF_INET,
245 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
246 };
nuccachene172a4e2018-10-23 17:10:58 +0800247 sockaddr_union addr = {.sin = sin_test};
248 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900249}
250
Bernie Innocentic165ce82018-10-16 23:35:28 +0900251// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
252// NOTE: also called by resolv_set_nameservers_for_net().
253int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
254 addrinfo** result) {
255 hints.ai_flags = AI_NUMERICHOST;
256 const android_net_context netcontext = {
257 .app_netid = NETID_UNSET,
258 .app_mark = MARK_UNSET,
259 .dns_netid = NETID_UNSET,
260 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900261 .uid = NET_CONTEXT_INVALID_UID,
262 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900263 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900264}
265
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
267 const struct addrinfo* hints,
268 const struct android_net_context* netcontext,
269 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800270 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900271 struct addrinfo* cur;
272 int error = 0;
273 struct addrinfo ai;
274 struct addrinfo ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900275
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 /* hostname is allowed to be NULL */
277 /* servname is allowed to be NULL */
278 /* hints is allowed to be NULL */
279 assert(res != NULL);
280 assert(netcontext != NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900281 cur = &sentinel;
Bernie Innocenti2319a772019-02-20 17:50:38 +0900282
283 ai.ai_flags = 0;
284 ai.ai_family = PF_UNSPEC;
285 ai.ai_socktype = ANY;
286 ai.ai_protocol = ANY;
287 ai.ai_addrlen = 0;
288 ai.ai_canonname = nullptr;
289 ai.ai_addr = nullptr;
290 ai.ai_next = nullptr;
291
Ken Chen3270cf52018-11-07 01:20:48 +0800292 do {
293 if (hostname == NULL && servname == NULL) {
294 error = EAI_NONAME;
295 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900296 }
Ken Chen3270cf52018-11-07 01:20:48 +0800297 if (hints) {
298 /* error check for hints */
299 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
300 error = EAI_BADHINTS;
301 break;
302 }
303 if (hints->ai_flags & ~AI_MASK) {
304 error = EAI_BADFLAGS;
305 break;
306 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900307
Ken Chen3270cf52018-11-07 01:20:48 +0800308 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
309 hints->ai_family == PF_INET6)) {
310 error = EAI_FAMILY;
311 break;
312 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900313
314 ai = *hints;
Ken Chen3270cf52018-11-07 01:20:48 +0800315
316 /*
317 * if both socktype/protocol are specified, check if they
318 * are meaningful combination.
319 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900320 if (ai.ai_socktype != ANY && ai.ai_protocol != ANY) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900321 for (const Explore& ex : explore_options) {
322 if (ai.ai_family != ex.e_af) continue;
323 if (ex.e_socktype == ANY) continue;
324 if (ex.e_protocol == ANY) continue;
325 if (ai.ai_socktype == ex.e_socktype && ai.ai_protocol != ex.e_protocol) {
Ken Chen3270cf52018-11-07 01:20:48 +0800326 error = EAI_BADHINTS;
327 break;
328 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900329 }
Ken Chen3270cf52018-11-07 01:20:48 +0800330 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 }
332 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900333
Ken Chen3270cf52018-11-07 01:20:48 +0800334 /*
335 * check for special cases. (1) numeric servname is disallowed if
336 * socktype/protocol are left unspecified. (2) servname is disallowed
337 * for raw and other inet{,6} sockets.
338 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900339 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
340 ai0 = ai; // backup ai
Bernie Innocenti55864192018-08-30 04:05:20 +0900341
Bernie Innocenti2319a772019-02-20 17:50:38 +0900342 if (ai.ai_family == PF_UNSPEC) {
343 ai.ai_family = PF_INET6;
Ken Chen3270cf52018-11-07 01:20:48 +0800344 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900345 error = get_portmatch(&ai, servname);
Ken Chen3270cf52018-11-07 01:20:48 +0800346 if (error) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900347
Bernie Innocenti2319a772019-02-20 17:50:38 +0900348 ai = ai0; // restore ai
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900349 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900350
Bernie Innocenti2319a772019-02-20 17:50:38 +0900351 ai0 = ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900352
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900353 // NULL hostname, or numeric hostname
354 for (const Explore& ex : explore_options) {
Bernie Innocenti2319a772019-02-20 17:50:38 +0900355 ai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900356
Ken Chen3270cf52018-11-07 01:20:48 +0800357 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900358 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900359
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900360 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
361 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
362 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900363
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900364 if (ai.ai_family == PF_UNSPEC) ai.ai_family = ex.e_af;
365 if (ai.ai_socktype == ANY && ex.e_socktype != ANY) ai.ai_socktype = ex.e_socktype;
366 if (ai.ai_protocol == ANY && ex.e_protocol != ANY) ai.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800367
368 if (hostname == NULL)
Bernie Innocenti2319a772019-02-20 17:50:38 +0900369 error = explore_null(&ai, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800370 else
Bernie Innocenti2319a772019-02-20 17:50:38 +0900371 error = explore_numeric_scope(&ai, hostname, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800372
373 if (error) break;
374
375 while (cur->ai_next) cur = cur->ai_next;
376 }
377 if (error) break;
378
379 /*
380 * XXX
381 * If numeric representation of AF1 can be interpreted as FQDN
382 * representation of AF2, we need to think again about the code below.
383 */
384 if (sentinel.ai_next) break;
385
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900386 if (hostname == nullptr) {
Ken Chen3270cf52018-11-07 01:20:48 +0800387 error = EAI_NODATA;
388 break;
389 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900390 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chen3270cf52018-11-07 01:20:48 +0800391 error = EAI_NONAME;
392 break;
393 }
394
395 /*
396 * hostname as alphabetical name.
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900397 * We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
Ken Chen3270cf52018-11-07 01:20:48 +0800398 */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900399 for (const Explore& ex : explore_options) {
Bernie Innocenti2319a772019-02-20 17:50:38 +0900400 ai = ai0;
Ken Chen3270cf52018-11-07 01:20:48 +0800401
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900402 // Require exact match for family field
403 if (ai.ai_family != ex.e_af) continue;
Ken Chen3270cf52018-11-07 01:20:48 +0800404
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900405 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800406 continue;
407 }
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900408 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800409 continue;
410 }
411
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900412 if (ai.ai_socktype == ANY && ex.e_socktype != ANY) ai.ai_socktype = ex.e_socktype;
413 if (ai.ai_protocol == ANY && ex.e_protocol != ANY) ai.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800414
Bernie Innocenti2319a772019-02-20 17:50:38 +0900415 error = explore_fqdn(&ai, hostname, servname, &cur->ai_next, netcontext);
Ken Chen3270cf52018-11-07 01:20:48 +0800416
417 while (cur->ai_next) cur = cur->ai_next;
418 }
419
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900420 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800421 error = 0;
422 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900423 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800424 }
425 } while (0);
426
427 if (error) {
428 freeaddrinfo(sentinel.ai_next);
429 *res = NULL;
430 } else {
431 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900433 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900434}
435
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900436// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900437static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
438 struct addrinfo** res, const struct android_net_context* netcontext) {
439 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900441
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900442 assert(pai != NULL);
443 /* hostname may be NULL */
444 /* servname may be NULL */
445 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900446
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900447 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900448
Bernie Innocenti948f6572018-09-12 21:32:42 +0900449 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900451
Bernie Innocenti948f6572018-09-12 21:32:42 +0900452 if (!files_getaddrinfo(hostname, pai, &result)) {
453 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900455 if (!error) {
456 struct addrinfo* cur;
457 for (cur = result; cur; cur = cur->ai_next) {
458 GET_PORT(cur, servname);
459 /* canonname should be filled already */
460 }
461 *res = result;
462 return 0;
463 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900464
465free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900466 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900467 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900468}
469
470/*
471 * hostname == NULL.
472 * passive socket -> anyaddr (0.0.0.0 or ::)
473 * non-passive socket -> localhost (127.0.0.1 or ::1)
474 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
476 int s;
477 const struct afd* afd;
478 struct addrinfo* cur;
479 struct addrinfo sentinel;
480 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900481
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900482 assert(pai != NULL);
483 /* servname may be NULL */
484 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900485
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900486 *res = NULL;
487 sentinel.ai_next = NULL;
488 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 /*
491 * filter out AFs that are not supported by the kernel
492 * XXX errno?
493 */
494 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
495 if (s < 0) {
496 if (errno != EMFILE) return 0;
497 } else
498 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900499
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 /*
501 * if the servname does not match socktype/protocol, ignore it.
502 */
503 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900504
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900505 afd = find_afd(pai->ai_family);
506 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900507
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900508 if (pai->ai_flags & AI_PASSIVE) {
509 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 GET_PORT(cur->ai_next, servname);
511 } else {
512 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 GET_PORT(cur->ai_next, servname);
514 }
515 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900516
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900517 *res = sentinel.ai_next;
518 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900519
520free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900521 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900522 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900523}
524
525/*
526 * numeric hostname
527 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900528static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
529 struct addrinfo** res, const char* canonname) {
530 const struct afd* afd;
531 struct addrinfo* cur;
532 struct addrinfo sentinel;
533 int error;
534 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900535
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900536 assert(pai != NULL);
537 /* hostname may be 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 * if the servname does not match socktype/protocol, ignore it.
547 */
548 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900549
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900550 afd = find_afd(pai->ai_family);
551 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900552
Ken Chen15c805a2018-10-17 00:19:59 +0800553 if (inet_pton(afd->a_af, hostname, pton) == 1) {
554 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
555 GET_AI(cur->ai_next, afd, pton);
556 GET_PORT(cur->ai_next, servname);
557 if ((pai->ai_flags & AI_CANONNAME)) {
558 /*
559 * Set the numeric address itself as
560 * the canonical name, based on a
561 * clarification in rfc2553bis-03.
562 */
Ken Chen3270cf52018-11-07 01:20:48 +0800563 error = get_canonname(pai, cur->ai_next, canonname);
564 if (error != 0) {
565 freeaddrinfo(sentinel.ai_next);
566 return error;
567 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 }
Ken Chen15c805a2018-10-17 00:19:59 +0800569 while (cur->ai_next) cur = cur->ai_next;
570 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800571 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900572 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900574 *res = sentinel.ai_next;
575 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900576
577free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900578 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900579 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900580}
581
582/*
583 * numeric hostname with scope
584 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900585static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
586 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900587 const struct afd* afd;
588 struct addrinfo* cur;
589 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900590 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900592
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900593 assert(pai != NULL);
594 /* hostname may be NULL */
595 /* servname may be NULL */
596 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900597
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900598 /*
599 * if the servname does not match socktype/protocol, ignore it.
600 */
601 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900602
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900603 afd = find_afd(pai->ai_family);
604 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900607
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 cp = strchr(hostname, SCOPE_DELIMITER);
609 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900611 /*
612 * Handle special case of <scoped_address><delimiter><scope id>
613 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900614 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 if (hostname2 == NULL) return EAI_MEMORY;
616 /* terminate at the delimiter */
617 hostname2[cp - hostname] = '\0';
618 addr = hostname2;
619 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900620
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900621 error = explore_numeric(pai, addr, servname, res, hostname);
622 if (error == 0) {
623 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900624
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 for (cur = *res; cur; cur = cur->ai_next) {
626 if (cur->ai_family != AF_INET6) continue;
627 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
628 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
629 free(hostname2);
630 return (EAI_NODATA); /* XXX: is return OK? */
631 }
632 sin6->sin6_scope_id = scopeid;
633 }
634 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900635
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900636 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900637
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900638 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900639}
640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
642 assert(pai != NULL);
643 assert(ai != NULL);
644 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900645
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900646 if ((pai->ai_flags & AI_CANONNAME) != 0) {
647 ai->ai_canonname = strdup(str);
648 if (ai->ai_canonname == NULL) return EAI_MEMORY;
649 }
650 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900651}
652
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
654 const char* addr) {
655 char* p;
656 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900658 assert(pai != NULL);
659 assert(afd != NULL);
660 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900661
nuccachene21023a2018-09-11 11:13:44 +0800662 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900665 memcpy(ai, pai, sizeof(struct addrinfo));
666 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800667 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900670 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
671 p = (char*) (void*) (ai->ai_addr);
672 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
673 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900674}
675
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900676static int get_portmatch(const struct addrinfo* ai, const char* servname) {
677 assert(ai != NULL);
678 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900681}
682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
684 const char* proto;
685 struct servent* sp;
686 int port;
687 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 assert(ai != NULL);
690 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 if (servname == NULL) return 0;
693 switch (ai->ai_family) {
694 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900696 break;
697 default:
698 return 0;
699 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701 switch (ai->ai_socktype) {
702 case SOCK_RAW:
703 return EAI_SERVICE;
704 case SOCK_DGRAM:
705 case SOCK_STREAM:
706 allownumeric = 1;
707 break;
708 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900710 break;
711 default:
712 return EAI_SOCKTYPE;
713 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715 port = str2number(servname);
716 if (port >= 0) {
717 if (!allownumeric) return EAI_SERVICE;
718 if (port < 0 || port > 65535) return EAI_SERVICE;
719 port = htons(port);
720 } else {
721 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900723 switch (ai->ai_socktype) {
724 case SOCK_DGRAM:
725 proto = "udp";
726 break;
727 case SOCK_STREAM:
728 proto = "tcp";
729 break;
730 default:
731 proto = NULL;
732 break;
733 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
736 port = sp->s_port;
737 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900739 if (!matchonly) {
740 switch (ai->ai_family) {
741 case AF_INET:
742 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
743 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 case AF_INET6:
745 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
746 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 }
748 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900749
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900751}
752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753static const struct afd* find_afd(int af) {
754 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 if (af == PF_UNSPEC) return NULL;
757 for (afd = afdl; afd->a_af; afd++) {
758 if (afd->a_af == af) return afd;
759 }
760 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900761}
762
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900763// Convert a string to a scope identifier.
764static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765 u_long lscopeid;
766 struct in6_addr* a6;
767 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900768
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900769 assert(scope != NULL);
770 assert(sin6 != NULL);
771 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900774
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900775 /* empty scopeid portion is invalid */
776 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900778 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
779 /*
780 * We currently assume a one-to-one mapping between links
781 * and interfaces, so we simply use interface indices for
782 * like-local scopes.
783 */
784 *scopeid = if_nametoindex(scope);
785 if (*scopeid == 0) goto trynumeric;
786 return 0;
787 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900788
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900789 /* still unclear about literal, allow numeric only - placeholder */
790 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
791 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
792 goto trynumeric;
793 else
794 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900795
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900796 /* try to convert to a numeric id as a last resort */
797trynumeric:
798 errno = 0;
799 lscopeid = strtoul(scope, &ep, 10);
800 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
801 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
802 return 0;
803 else
804 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900805}
Bernie Innocenti55864192018-08-30 04:05:20 +0900806
807/* code duplicate with gethnamaddr.c */
808
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900809static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900810
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811#define BOUNDED_INCR(x) \
812 do { \
813 BOUNDS_CHECK(cp, x); \
814 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900815 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900816
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900817#define BOUNDS_CHECK(ptr, count) \
818 do { \
819 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800820 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900821 return NULL; \
822 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900823 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800826 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800827 struct addrinfo sentinel = {};
828 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 struct addrinfo ai;
830 const struct afd* afd;
831 char* canonname;
832 const HEADER* hp;
833 const u_char* cp;
834 int n;
835 const u_char* eom;
836 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900837 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900838 int haveanswer, had_error;
839 char tbuf[MAXDNAME];
840 int (*name_ok)(const char*);
841 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900842
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900843 assert(answer != NULL);
844 assert(qname != NULL);
845 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900846
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900848
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849 canonname = NULL;
850 eom = answer->buf + anslen;
851 switch (qtype) {
852 case T_A:
853 case T_AAAA:
854 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
855 name_ok = res_hnok;
856 break;
857 default:
858 return NULL; /* XXX should be abort(); */
859 }
860 /*
861 * find first satisfactory answer
862 */
863 hp = &answer->hdr;
864 ancount = ntohs(hp->ancount);
865 qdcount = ntohs(hp->qdcount);
866 bp = hostbuf;
867 ep = hostbuf + sizeof hostbuf;
868 cp = answer->buf;
869 BOUNDED_INCR(HFIXEDSZ);
870 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800871 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900872 return (NULL);
873 }
874 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
875 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800876 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900877 return (NULL);
878 }
879 BOUNDED_INCR(n + QFIXEDSZ);
880 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
881 /* res_send() has already verified that the query name is the
882 * same as the one we sent; this just gets the expanded name
883 * (i.e., with the succeeding search-domain tacked on).
884 */
885 n = strlen(bp) + 1; /* for the \0 */
886 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800887 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900888 return (NULL);
889 }
890 canonname = bp;
891 bp += n;
892 /* The qname can be abbreviated, but h_name is now absolute. */
893 qname = canonname;
894 }
895 haveanswer = 0;
896 had_error = 0;
897 while (ancount-- > 0 && cp < eom && !had_error) {
898 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
899 if ((n < 0) || !(*name_ok)(bp)) {
900 had_error++;
901 continue;
902 }
903 cp += n; /* name */
904 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900905 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900907 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900908 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900909 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 cp += INT16SZ; /* len */
911 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900912 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913 /* XXX - debug? syslog? */
914 cp += n;
915 continue; /* XXX - had_error++ ? */
916 }
917 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
918 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
919 if ((n < 0) || !(*name_ok)(tbuf)) {
920 had_error++;
921 continue;
922 }
923 cp += n;
924 /* Get canonical name. */
925 n = strlen(tbuf) + 1; /* for the \0 */
926 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
927 had_error++;
928 continue;
929 }
930 strlcpy(bp, tbuf, (size_t)(ep - bp));
931 canonname = bp;
932 bp += n;
933 continue;
934 }
935 if (qtype == T_ANY) {
936 if (!(type == T_A || type == T_AAAA)) {
937 cp += n;
938 continue;
939 }
940 } else if (type != qtype) {
941 if (type != T_KEY && type != T_SIG)
942 syslog(LOG_NOTICE | LOG_AUTH,
943 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
944 p_class(C_IN), p_type(qtype), p_type(type));
945 cp += n;
946 continue; /* XXX - had_error++ ? */
947 }
948 switch (type) {
949 case T_A:
950 case T_AAAA:
951 if (strcasecmp(canonname, bp) != 0) {
952 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
953 cp += n;
954 continue; /* XXX - had_error++ ? */
955 }
956 if (type == T_A && n != INADDRSZ) {
957 cp += n;
958 continue;
959 }
960 if (type == T_AAAA && n != IN6ADDRSZ) {
961 cp += n;
962 continue;
963 }
964 if (type == T_AAAA) {
965 struct in6_addr in6;
966 memcpy(&in6, cp, IN6ADDRSZ);
967 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
968 cp += n;
969 continue;
970 }
971 }
972 if (!haveanswer) {
973 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900974
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900975 canonname = bp;
976 nn = strlen(bp) + 1; /* for the \0 */
977 bp += nn;
978 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900979
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900980 /* don't overwrite pai */
981 ai = *pai;
982 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
983 afd = find_afd(ai.ai_family);
984 if (afd == NULL) {
985 cp += n;
986 continue;
987 }
988 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
989 if (cur->ai_next == NULL) had_error++;
990 while (cur && cur->ai_next) cur = cur->ai_next;
991 cp += n;
992 break;
993 default:
994 abort();
995 }
996 if (!had_error) haveanswer++;
997 }
998 if (haveanswer) {
999 if (!canonname)
1000 (void) get_canonname(pai, sentinel.ai_next, qname);
1001 else
1002 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +08001003 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001004 return sentinel.ai_next;
1005 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001006
Hungming Chend57ade02018-12-25 15:47:47 +08001007 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001008 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001009}
1010
1011struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001012 struct addrinfo* ai;
1013 int has_src_addr;
1014 sockaddr_union src_addr;
1015 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001016};
1017
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001018static int _get_scope(const struct sockaddr* addr) {
1019 if (addr->sa_family == AF_INET6) {
1020 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1021 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1022 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1023 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1024 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1025 /*
1026 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1027 * link-local scope.
1028 */
1029 return IPV6_ADDR_SCOPE_LINKLOCAL;
1030 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1031 return IPV6_ADDR_SCOPE_SITELOCAL;
1032 } else {
1033 return IPV6_ADDR_SCOPE_GLOBAL;
1034 }
1035 } else if (addr->sa_family == AF_INET) {
1036 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1037 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001038
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001039 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1040 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1041 return IPV6_ADDR_SCOPE_LINKLOCAL;
1042 } else {
1043 /*
1044 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1045 * and shared addresses (100.64.0.0/10), are assigned global scope.
1046 */
1047 return IPV6_ADDR_SCOPE_GLOBAL;
1048 }
1049 } else {
1050 /*
1051 * This should never happen.
1052 * Return a scope with low priority as a last resort.
1053 */
1054 return IPV6_ADDR_SCOPE_NODELOCAL;
1055 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001056}
1057
1058/* These macros are modelled after the ones in <netinet/in6.h>. */
1059
1060/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001061#define IN6_IS_ADDR_TEREDO(a) \
1062 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001063
1064/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001065#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001066
1067/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001068#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001069
1070/*
1071 * Get the label for a given IPv4/IPv6 address.
1072 * RFC 6724, section 2.1.
1073 */
1074
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001075static int _get_label(const struct sockaddr* addr) {
1076 if (addr->sa_family == AF_INET) {
1077 return 4;
1078 } else if (addr->sa_family == AF_INET6) {
1079 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1080 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1081 return 0;
1082 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1083 return 4;
1084 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1085 return 2;
1086 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1087 return 5;
1088 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1089 return 13;
1090 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1091 return 3;
1092 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1093 return 11;
1094 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1095 return 12;
1096 } else {
1097 /* All other IPv6 addresses, including global unicast addresses. */
1098 return 1;
1099 }
1100 } else {
1101 /*
1102 * This should never happen.
1103 * Return a semi-random label as a last resort.
1104 */
1105 return 1;
1106 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001107}
1108
1109/*
1110 * Get the precedence for a given IPv4/IPv6 address.
1111 * RFC 6724, section 2.1.
1112 */
1113
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001114static int _get_precedence(const struct sockaddr* addr) {
1115 if (addr->sa_family == AF_INET) {
1116 return 35;
1117 } else if (addr->sa_family == AF_INET6) {
1118 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1119 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1120 return 50;
1121 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1122 return 35;
1123 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1124 return 30;
1125 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1126 return 5;
1127 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1128 return 3;
1129 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1130 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1131 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1132 return 1;
1133 } else {
1134 /* All other IPv6 addresses, including global unicast addresses. */
1135 return 40;
1136 }
1137 } else {
1138 return 1;
1139 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001140}
1141
1142/*
1143 * Find number of matching initial bits between the two addresses a1 and a2.
1144 */
1145
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001146static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1147 const char* p1 = (const char*) a1;
1148 const char* p2 = (const char*) a2;
1149 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001150
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001151 for (i = 0; i < sizeof(*a1); ++i) {
1152 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001153
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001154 if (p1[i] == p2[i]) {
1155 continue;
1156 }
1157 x = p1[i] ^ p2[i];
1158 for (j = 0; j < CHAR_BIT; ++j) {
1159 if (x & (1 << (CHAR_BIT - 1))) {
1160 return i * CHAR_BIT + j;
1161 }
1162 x <<= 1;
1163 }
1164 }
1165 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001166}
1167
1168/*
1169 * Compare two source/destination address pairs.
1170 * RFC 6724, section 6.
1171 */
1172
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001173static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1174 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1175 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1176 int scope_src1, scope_dst1, scope_match1;
1177 int scope_src2, scope_dst2, scope_match2;
1178 int label_src1, label_dst1, label_match1;
1179 int label_src2, label_dst2, label_match2;
1180 int precedence1, precedence2;
1181 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001183 /* Rule 1: Avoid unusable destinations. */
1184 if (a1->has_src_addr != a2->has_src_addr) {
1185 return a2->has_src_addr - a1->has_src_addr;
1186 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001187
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001188 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001189 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001190 scope_dst1 = _get_scope(a1->ai->ai_addr);
1191 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001192
nuccachene172a4e2018-10-23 17:10:58 +08001193 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001194 scope_dst2 = _get_scope(a2->ai->ai_addr);
1195 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001196
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001197 if (scope_match1 != scope_match2) {
1198 return scope_match2 - scope_match1;
1199 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001201 /*
1202 * Rule 3: Avoid deprecated addresses.
1203 * TODO(sesse): We don't currently have a good way of finding this.
1204 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001205
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001206 /*
1207 * Rule 4: Prefer home addresses.
1208 * TODO(sesse): We don't currently have a good way of finding this.
1209 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001210
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001211 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001212 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001213 label_dst1 = _get_label(a1->ai->ai_addr);
1214 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001215
nuccachene172a4e2018-10-23 17:10:58 +08001216 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001217 label_dst2 = _get_label(a2->ai->ai_addr);
1218 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001220 if (label_match1 != label_match2) {
1221 return label_match2 - label_match1;
1222 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001223
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001224 /* Rule 6: Prefer higher precedence. */
1225 precedence1 = _get_precedence(a1->ai->ai_addr);
1226 precedence2 = _get_precedence(a2->ai->ai_addr);
1227 if (precedence1 != precedence2) {
1228 return precedence2 - precedence1;
1229 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001230
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001231 /*
1232 * Rule 7: Prefer native transport.
1233 * TODO(sesse): We don't currently have a good way of finding this.
1234 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001235
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001236 /* Rule 8: Prefer smaller scope. */
1237 if (scope_dst1 != scope_dst2) {
1238 return scope_dst1 - scope_dst2;
1239 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001240
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001241 /*
1242 * Rule 9: Use longest matching prefix.
1243 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1244 * to work very well directly applied to IPv4. (glibc uses information from
1245 * the routing table for a custom IPv4 implementation here.)
1246 */
1247 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1248 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001249 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001250 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001251 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001252 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1253 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1254 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1255 if (prefixlen1 != prefixlen2) {
1256 return prefixlen2 - prefixlen1;
1257 }
1258 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001259
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001260 /*
1261 * Rule 10: Leave the order unchanged.
1262 * We need this since qsort() is not necessarily stable.
1263 */
1264 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001265}
1266
1267/*
1268 * Find the source address that will be used if trying to connect to the given
1269 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1270 *
1271 * Returns 1 if a source address was found, 0 if the address is unreachable,
1272 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1273 * undefined.
1274 */
1275
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001276static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1277 uid_t uid) {
1278 int sock;
1279 int ret;
1280 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001281
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001282 switch (addr->sa_family) {
1283 case AF_INET:
1284 len = sizeof(struct sockaddr_in);
1285 break;
1286 case AF_INET6:
1287 len = sizeof(struct sockaddr_in6);
1288 break;
1289 default:
1290 /* No known usable source address for non-INET families. */
1291 return 0;
1292 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001293
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001294 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1295 if (sock == -1) {
1296 if (errno == EAFNOSUPPORT) {
1297 return 0;
1298 } else {
1299 return -1;
1300 }
1301 }
1302 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1303 close(sock);
1304 return 0;
1305 }
1306 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1307 close(sock);
1308 return 0;
1309 }
1310 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001311 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001312 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001313
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001314 if (ret == -1) {
1315 close(sock);
1316 return 0;
1317 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001318
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001319 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1320 close(sock);
1321 return -1;
1322 }
1323 close(sock);
1324 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001325}
1326
1327/*
1328 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1329 * Will leave the list unchanged if an error occurs.
1330 */
1331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001332static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1333 struct addrinfo* cur;
1334 int nelem = 0, i;
1335 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001336
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001337 cur = list_sentinel->ai_next;
1338 while (cur) {
1339 ++nelem;
1340 cur = cur->ai_next;
1341 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001342
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001343 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1344 if (elems == NULL) {
1345 goto error;
1346 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001347
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001348 /*
1349 * Convert the linked list to an array that also contains the candidate
1350 * source address for each destination address.
1351 */
1352 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1353 int has_src_addr;
1354 assert(cur != NULL);
1355 elems[i].ai = cur;
1356 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001357
nuccachene172a4e2018-10-23 17:10:58 +08001358 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001359 if (has_src_addr == -1) {
1360 goto error;
1361 }
1362 elems[i].has_src_addr = has_src_addr;
1363 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001364
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001365 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1366 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001367
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001368 list_sentinel->ai_next = elems[0].ai;
1369 for (i = 0; i < nelem - 1; ++i) {
1370 elems[i].ai->ai_next = elems[i + 1].ai;
1371 }
1372 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001373
1374error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001375 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001376}
1377
Bernie Innocenti948f6572018-09-12 21:32:42 +09001378static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1379 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001380 struct addrinfo *ai, *cur;
1381 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001382 struct res_target q, q2;
1383 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001384
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 memset(&q, 0, sizeof(q));
1386 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001387 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001388
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001389 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001390 if (buf == NULL) {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001391 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001392 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001393 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001394 if (buf2 == NULL) {
1395 free(buf);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001396 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001397 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001400 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001401 /* prefer IPv6 */
1402 q.name = name;
1403 q.qclass = C_IN;
1404 q.answer = buf->buf;
1405 q.anslen = sizeof(buf->buf);
1406 int query_ipv6 = 1, query_ipv4 = 1;
1407 if (pai->ai_flags & AI_ADDRCONFIG) {
1408 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1409 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1410 }
1411 if (query_ipv6) {
1412 q.qtype = T_AAAA;
1413 if (query_ipv4) {
1414 q.next = &q2;
1415 q2.name = name;
1416 q2.qclass = C_IN;
1417 q2.qtype = T_A;
1418 q2.answer = buf2->buf;
1419 q2.anslen = sizeof(buf2->buf);
1420 }
1421 } else if (query_ipv4) {
1422 q.qtype = T_A;
1423 } else {
1424 free(buf);
1425 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001426 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001427 }
1428 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001429 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001430 case AF_INET:
1431 q.name = name;
1432 q.qclass = C_IN;
1433 q.qtype = T_A;
1434 q.answer = buf->buf;
1435 q.anslen = sizeof(buf->buf);
1436 break;
1437 case AF_INET6:
1438 q.name = name;
1439 q.qclass = C_IN;
1440 q.qtype = T_AAAA;
1441 q.answer = buf->buf;
1442 q.anslen = sizeof(buf->buf);
1443 break;
1444 default:
1445 free(buf);
1446 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001447 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001448 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001449
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001450 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001451 if (res == NULL) {
1452 free(buf);
1453 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001454 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001455 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001456
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001457 /* this just sets our netid val in the thread private data so we don't have to
1458 * modify the api's all the way down to res_send.c's res_nsend. We could
1459 * fully populate the thread private data here, but if we get down there
1460 * and have a cache hit that would be wasted, so we do the rest there on miss
1461 */
1462 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001463
Hungming Chend57ade02018-12-25 15:47:47 +08001464 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +08001465 if (res_searchN(name, &q, res, &herrno) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001466 free(buf);
1467 free(buf2);
Hungming Chen7f0d3292018-12-27 18:33:19 +08001468 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
1469 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001470 }
Hungming Chend57ade02018-12-25 15:47:47 +08001471 ai = getanswer(buf, q.n, q.name, q.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001472 if (ai) {
1473 cur->ai_next = ai;
1474 while (cur && cur->ai_next) cur = cur->ai_next;
1475 }
1476 if (q.next) {
Hungming Chend57ade02018-12-25 15:47:47 +08001477 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001478 if (ai) cur->ai_next = ai;
1479 }
1480 free(buf);
1481 free(buf2);
1482 if (sentinel.ai_next == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001483 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001484 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001485
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001486 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001487
Bernie Innocenti948f6572018-09-12 21:32:42 +09001488 *rv = sentinel.ai_next;
1489 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001490}
1491
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001492static void _sethtent(FILE** hostf) {
1493 if (!*hostf)
1494 *hostf = fopen(_PATH_HOSTS, "re");
1495 else
1496 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001497}
1498
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001499static void _endhtent(FILE** hostf) {
1500 if (*hostf) {
1501 (void) fclose(*hostf);
1502 *hostf = NULL;
1503 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001504}
1505
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001506static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1507 char* p;
1508 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001509 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001510 int error;
1511 const char* addr;
1512 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001513
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001514 assert(name != NULL);
1515 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001516
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001517 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1518again:
1519 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1520 if (*p == '#') goto again;
1521 if (!(cp = strpbrk(p, "#\n"))) goto again;
1522 *cp = '\0';
1523 if (!(cp = strpbrk(p, " \t"))) goto again;
1524 *cp++ = '\0';
1525 addr = p;
1526 /* if this is not something we're looking for, skip it. */
1527 cname = NULL;
1528 while (cp && *cp) {
1529 if (*cp == ' ' || *cp == '\t') {
1530 cp++;
1531 continue;
1532 }
1533 if (!cname) cname = cp;
1534 tname = cp;
1535 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1536 // fprintf(stderr, "\ttname = '%s'", tname);
1537 if (strcasecmp(name, tname) == 0) goto found;
1538 }
1539 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001540
1541found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001542 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001543 if (error) goto again;
1544 for (res = res0; res; res = res->ai_next) {
1545 /* cover it up */
1546 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001547
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001548 if (pai->ai_flags & AI_CANONNAME) {
1549 if (get_canonname(pai, res, cname) != 0) {
1550 freeaddrinfo(res0);
1551 goto again;
1552 }
1553 }
1554 }
1555 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001556}
1557
Bernie Innocenti948f6572018-09-12 21:32:42 +09001558static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001559 struct addrinfo sentinel = {};
1560 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001561 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001562
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001563 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001564
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001565 _sethtent(&hostf);
1566 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1567 cur->ai_next = p;
1568 while (cur && cur->ai_next) cur = cur->ai_next;
1569 }
1570 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001571
Bernie Innocenti948f6572018-09-12 21:32:42 +09001572 *res = sentinel.ai_next;
1573 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001574}
1575
1576/* resolver logic */
1577
1578/*
1579 * Formulate a normal query, send, and await answer.
1580 * Returned answer is placed in supplied buffer "answer".
1581 * Perform preliminary check of answer, returning success only
1582 * if no error is indicated and the answer count is nonzero.
1583 * Return the size of the response on success, -1 on error.
Hungming Chend57ade02018-12-25 15:47:47 +08001584 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001585 *
1586 * Caller must parse answer and determine whether it answers the question.
1587 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001588static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001589 u_char buf[MAXPACKET];
1590 HEADER* hp;
1591 int n;
1592 struct res_target* t;
1593 int rcode;
1594 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001596 assert(name != NULL);
1597 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001598
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001599 rcode = NOERROR;
1600 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001601
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001602 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001603 u_char* answer;
1604 int anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 hp = (HEADER*) (void*) t->answer;
Ken Chenbfd32022019-01-02 14:59:38 +08001607 bool retried = false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001608 again:
1609 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001611 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001612 int cl = t->qclass;
1613 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001614 answer = t->answer;
1615 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001616#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001617 if (res->options & RES_DEBUG) printf(";; res_queryN(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001618#endif
1619
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001620 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Ken Chenbfd32022019-01-02 14:59:38 +08001621 if (n > 0 && (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && !retried)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001622 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001623 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001624#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001625 if (res->options & RES_DEBUG) printf(";; res_queryN: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001626#endif
Hungming Chend57ade02018-12-25 15:47:47 +08001627 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001628 return n;
1629 }
Mike Yu69615f62018-11-06 15:42:36 +08001630
Luke Huang952d0942018-12-26 16:53:03 +08001631 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001632 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001633 // Record rcode from DNS response header only if no timeout.
1634 // Keep rcode timeout for reporting later if any.
1635 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001636 /* if the query choked with EDNS0, retry without EDNS0 */
1637 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
Ken Chenbfd32022019-01-02 14:59:38 +08001638 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001639#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001640 if (res->options & RES_DEBUG) printf(";; res_queryN: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001641#endif
Ken Chenbfd32022019-01-02 14:59:38 +08001642 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001643 goto again;
1644 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001645#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001646 if (res->options & RES_DEBUG)
1647 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001648#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649 continue;
1650 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001651
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001652 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001654 t->n = n;
1655 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001656
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001657 if (ancount == 0) {
1658 switch (rcode) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001659 // Not defined in RFC.
1660 case RCODE_TIMEOUT:
1661 // DNS metrics monitors DNS query timeout.
1662 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1663 break;
1664 // Defined in RFC 1035 section 4.1.1.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001665 case NXDOMAIN:
Hungming Chend57ade02018-12-25 15:47:47 +08001666 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001667 break;
1668 case SERVFAIL:
Hungming Chend57ade02018-12-25 15:47:47 +08001669 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001670 break;
1671 case NOERROR:
Hungming Chend57ade02018-12-25 15:47:47 +08001672 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001673 break;
1674 case FORMERR:
1675 case NOTIMP:
1676 case REFUSED:
1677 default:
Hungming Chend57ade02018-12-25 15:47:47 +08001678 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001679 break;
1680 }
1681 return -1;
1682 }
1683 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001684}
1685
1686/*
1687 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1688 * Return the size of the response on success, -1 on error.
1689 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chend57ade02018-12-25 15:47:47 +08001690 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001691 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001692static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001693 const char *cp, *const *domain;
1694 HEADER* hp;
1695 u_int dots;
1696 int trailing_dot, ret, saved_herrno;
1697 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001698
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001699 assert(name != NULL);
1700 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001701
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001702 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001704 errno = 0;
Hungming Chend57ade02018-12-25 15:47:47 +08001705 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001706 dots = 0;
1707 for (cp = name; *cp; cp++) dots += (*cp == '.');
1708 trailing_dot = 0;
1709 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001710
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001711 /*
1712 * If there are dots in the name already, let's just give it a try
1713 * 'as is'. The threshold can be set with the "ndots" option.
1714 */
1715 saved_herrno = -1;
1716 if (dots >= res->ndots) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001717 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001718 if (ret > 0) return (ret);
Hungming Chend57ade02018-12-25 15:47:47 +08001719 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001720 tried_as_is++;
1721 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 /*
1724 * We do at least one level of search if
1725 * - there is no dot and RES_DEFNAME is set, or
1726 * - there is at least one dot, there is no trailing dot,
1727 * and RES_DNSRCH is set.
1728 */
1729 if ((!dots && (res->options & RES_DEFNAMES)) ||
1730 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1731 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001733 /* Unfortunately we need to set stuff up before
1734 * the domain stuff is tried. Will have a better
1735 * fix after thread pools are used.
1736 */
1737 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001739 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001740 ret = res_querydomainN(name, *domain, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001741 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001743 /*
1744 * If no server present, give up.
1745 * If name isn't found in this domain,
1746 * keep trying higher domains in the search list
1747 * (if that's enabled).
1748 * On a NO_DATA error, keep trying, otherwise
1749 * a wildcard entry of another type could keep us
1750 * from finding this entry higher in the domain.
1751 * If we get some other error (negative answer or
1752 * server failure), then stop searching up,
1753 * but try the input name below in case it's
1754 * fully-qualified.
1755 */
1756 if (errno == ECONNREFUSED) {
Hungming Chend57ade02018-12-25 15:47:47 +08001757 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001758 return -1;
1759 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001760
Hungming Chend57ade02018-12-25 15:47:47 +08001761 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001762 case NO_DATA:
1763 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001764 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001765 case HOST_NOT_FOUND:
1766 /* keep trying */
1767 break;
1768 case TRY_AGAIN:
1769 if (hp->rcode == SERVFAIL) {
1770 /* try next search element, if any */
1771 got_servfail++;
1772 break;
1773 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001774 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001775 default:
1776 /* anything else implies that we're done */
1777 done++;
1778 }
1779 /*
1780 * if we got here for some reason other than DNSRCH,
1781 * we only wanted one iteration of the loop, so stop.
1782 */
1783 if (!(res->options & RES_DNSRCH)) done++;
1784 }
1785 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001786
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001787 /*
1788 * if we have not already tried the name "as is", do that now.
1789 * note that we do this regardless of how many dots were in the
1790 * name or whether it ends with a dot.
1791 */
1792 if (!tried_as_is) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001793 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001794 if (ret > 0) return ret;
1795 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001796
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001797 /*
1798 * if we got here, we didn't satisfy the search.
1799 * if we did an initial full query, return that query's h_errno
1800 * (note that we wouldn't be here if that query had succeeded).
1801 * else if we ever got a nodata, send that back as the reason.
1802 * else send back meaningless h_errno, that being the one from
1803 * the last DNSRCH we did.
1804 */
1805 if (saved_herrno != -1)
Hungming Chend57ade02018-12-25 15:47:47 +08001806 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001807 else if (got_nodata)
Hungming Chend57ade02018-12-25 15:47:47 +08001808 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001809 else if (got_servfail)
Hungming Chend57ade02018-12-25 15:47:47 +08001810 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001811 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001812}
1813
1814/*
1815 * Perform a call on res_query on the concatenation of name and domain,
1816 * removing a trailing dot from name if domain is NULL.
1817 */
Mike Yu69615f62018-11-06 15:42:36 +08001818static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +08001819 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001820 char nbuf[MAXDNAME];
1821 const char* longname = nbuf;
1822 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001824 assert(name != NULL);
1825 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001826
1827#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001828 if (res->options & RES_DEBUG)
1829 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001830#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001831 if (domain == NULL) {
1832 /*
1833 * Check for trailing '.';
1834 * copy without '.' if present.
1835 */
1836 n = strlen(name);
1837 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001838 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001839 return -1;
1840 }
1841 if (n > 0 && name[--n] == '.') {
1842 strncpy(nbuf, name, n);
1843 nbuf[n] = '\0';
1844 } else
1845 longname = name;
1846 } else {
1847 n = strlen(name);
1848 d = strlen(domain);
1849 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001850 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001851 return -1;
1852 }
1853 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1854 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001855 return res_queryN(longname, target, res, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001856}