blob: 5abd472c598cb2179dacbf2175410b7b9ff8bf37 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
2/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
Bernie Innocenti55864192018-08-30 04:05:20 +090033#include <arpa/inet.h>
34#include <arpa/nameser.h>
35#include <assert.h>
36#include <ctype.h>
37#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090038#include <fcntl.h>
39#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090040#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090041#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090042#include <stdbool.h>
43#include <stddef.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090047#include <sys/param.h>
48#include <sys/socket.h>
49#include <sys/stat.h>
50#include <sys/types.h>
51#include <sys/un.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090052#include <syslog.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090053#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090054
Bernie Innocenti189eb502018-10-01 23:10:18 +090055#include "netd_resolv/resolv.h"
56#include "resolv_cache.h"
57#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090058
Bernie Innocenti55864192018-08-30 04:05:20 +090059#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +090060
Bernie Innocenti93a31342018-12-12 00:43:02 +090061const char in_addrany[] = {0, 0, 0, 0};
62const char in_loopback[] = {127, 0, 0, 1};
63const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
64const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +090065
Bernie Innocenti93a31342018-12-12 00:43:02 +090066const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090067 int a_af;
68 int a_addrlen;
69 int a_socklen;
70 int a_off;
71 const char* a_addrany;
72 const char* a_loopback;
73 int a_scoped;
74} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090075 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
76 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
78 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
79 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +090080};
81
82struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090083 int e_af;
84 int e_socktype;
85 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090086 int e_wild;
87#define WILD_AF(ex) ((ex)->e_wild & 0x01)
88#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
89#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +090090};
91
Bernie Innocenti93a31342018-12-12 00:43:02 +090092const struct explore explore_options[] = {
Ken Chen3270cf52018-11-07 01:20:48 +080093 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
94 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
95 {PF_INET6, SOCK_RAW, ANY, 0x05},
96 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
97 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
98 {PF_INET, SOCK_RAW, ANY, 0x05},
99 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
100 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
101 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
102 {-1, 0, 0, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900103};
104
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900105#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900107
108typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900109 HEADER hdr;
110 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900111} querybuf;
112
113struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114 struct res_target* next;
115 const char* name; /* domain name */
116 int qclass, qtype; /* class and type of query */
117 u_char* answer; /* buffer to put answer */
118 int anslen; /* size of answer buffer */
119 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900120};
121
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122static int str2number(const char*);
123static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
124 const struct android_net_context*);
125static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
126static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
127 const char*);
128static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
129 struct addrinfo**);
130static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
131static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
132static int get_portmatch(const struct addrinfo*, const char*);
133static int get_port(const struct addrinfo*, const char*, int);
134static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900135static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
Hungming Chend57ade02018-12-25 15:47:47 +0800137static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
138 int* herrno);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900139static int dns_getaddrinfo(const char* name, const addrinfo* pai,
140 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141static void _sethtent(FILE**);
142static void _endhtent(FILE**);
143static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900144static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900146
Hungming Chen7f0d3292018-12-27 18:33:19 +0800147static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
148static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800149static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800150 int* herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900151
Bernie Innocenti93a31342018-12-12 00:43:02 +0900152const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 "Success",
154 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
155 "Temporary failure in name resolution", /* EAI_AGAIN */
156 "Invalid value for ai_flags", /* EAI_BADFLAGS */
157 "Non-recoverable failure in name resolution", /* EAI_FAIL */
158 "ai_family not supported", /* EAI_FAMILY */
159 "Memory allocation failure", /* EAI_MEMORY */
160 "No address associated with hostname", /* EAI_NODATA */
161 "hostname nor servname provided, or not known", /* EAI_NONAME */
162 "servname not supported for ai_socktype", /* EAI_SERVICE */
163 "ai_socktype not supported", /* EAI_SOCKTYPE */
164 "System error returned in errno", /* EAI_SYSTEM */
165 "Invalid value for hints", /* EAI_BADHINTS */
166 "Resolved protocol is unknown", /* EAI_PROTOCOL */
167 "Argument buffer overflow", /* EAI_OVERFLOW */
168 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900169};
170
171/* XXX macros that make external reference is BAD. */
172
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173#define GET_AI(ai, afd, addr) \
174 do { \
175 /* external reference: pai, error, and label free */ \
176 (ai) = get_ai(pai, (afd), (addr)); \
177 if ((ai) == NULL) { \
178 error = EAI_MEMORY; \
179 goto free; \
180 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900181 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183#define GET_PORT(ai, serv) \
184 do { \
185 /* external reference: error and label free */ \
186 error = get_port((ai), (serv), 0); \
187 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900188 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900189
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900191 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
192#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194const char* gai_strerror(int ecode) {
195 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
196 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900197}
198
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900199void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900200 while (ai) {
201 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900203 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 free(ai);
205 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900206 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900207}
208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900209static int str2number(const char* p) {
210 char* ep;
211 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215 if (*p == '\0') return -1;
216 ep = NULL;
217 errno = 0;
218 v = strtoul(p, &ep, 10);
219 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
220 return v;
221 else
222 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900223}
224
225/*
226 * The following functions determine whether IPv4 or IPv6 connectivity is
227 * available in order to implement AI_ADDRCONFIG.
228 *
229 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
230 * available, but whether addresses of the specified family are "configured
231 * on the local system". However, bionic doesn't currently support getifaddrs,
232 * so checking for connectivity is the next best thing.
233 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234static int _have_ipv6(unsigned mark, uid_t uid) {
235 static const struct sockaddr_in6 sin6_test = {
236 .sin6_family = AF_INET6,
237 .sin6_addr.s6_addr = {// 2000::
238 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800239 sockaddr_union addr = {.sin6 = sin6_test};
240 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900241}
242
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900243static int _have_ipv4(unsigned mark, uid_t uid) {
244 static const struct sockaddr_in sin_test = {
245 .sin_family = AF_INET,
246 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
247 };
nuccachene172a4e2018-10-23 17:10:58 +0800248 sockaddr_union addr = {.sin = sin_test};
249 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900250}
251
Bernie Innocentic165ce82018-10-16 23:35:28 +0900252// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
253// NOTE: also called by resolv_set_nameservers_for_net().
254int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
255 addrinfo** result) {
256 hints.ai_flags = AI_NUMERICHOST;
257 const android_net_context netcontext = {
258 .app_netid = NETID_UNSET,
259 .app_mark = MARK_UNSET,
260 .dns_netid = NETID_UNSET,
261 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900262 .uid = NET_CONTEXT_INVALID_UID,
263 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900264 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900265}
266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
268 const struct addrinfo* hints,
269 const struct android_net_context* netcontext,
270 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800271 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900272 struct addrinfo* cur;
273 int error = 0;
274 struct addrinfo ai;
275 struct addrinfo ai0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900277
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900278 /* hostname is allowed to be NULL */
279 /* servname is allowed to be NULL */
280 /* hints is allowed to be NULL */
281 assert(res != NULL);
282 assert(netcontext != NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900283 cur = &sentinel;
Bernie Innocenti2319a772019-02-20 17:50:38 +0900284
285 ai.ai_flags = 0;
286 ai.ai_family = PF_UNSPEC;
287 ai.ai_socktype = ANY;
288 ai.ai_protocol = ANY;
289 ai.ai_addrlen = 0;
290 ai.ai_canonname = nullptr;
291 ai.ai_addr = nullptr;
292 ai.ai_next = nullptr;
293
Ken Chen3270cf52018-11-07 01:20:48 +0800294 do {
295 if (hostname == NULL && servname == NULL) {
296 error = EAI_NONAME;
297 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900298 }
Ken Chen3270cf52018-11-07 01:20:48 +0800299 if (hints) {
300 /* error check for hints */
301 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
302 error = EAI_BADHINTS;
303 break;
304 }
305 if (hints->ai_flags & ~AI_MASK) {
306 error = EAI_BADFLAGS;
307 break;
308 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900309
Ken Chen3270cf52018-11-07 01:20:48 +0800310 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
311 hints->ai_family == PF_INET6)) {
312 error = EAI_FAMILY;
313 break;
314 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900315
316 ai = *hints;
Ken Chen3270cf52018-11-07 01:20:48 +0800317
318 /*
319 * if both socktype/protocol are specified, check if they
320 * are meaningful combination.
321 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900322 if (ai.ai_socktype != ANY && ai.ai_protocol != ANY) {
Ken Chen3270cf52018-11-07 01:20:48 +0800323 for (ex = explore_options; ex->e_af >= 0; ex++) {
Bernie Innocenti2319a772019-02-20 17:50:38 +0900324 if (ai.ai_family != ex->e_af) continue;
Ken Chen3270cf52018-11-07 01:20:48 +0800325 if (ex->e_socktype == ANY) continue;
326 if (ex->e_protocol == ANY) continue;
Bernie Innocenti2319a772019-02-20 17:50:38 +0900327 if (ai.ai_socktype == ex->e_socktype && ai.ai_protocol != ex->e_protocol) {
Ken Chen3270cf52018-11-07 01:20:48 +0800328 error = EAI_BADHINTS;
329 break;
330 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 }
Ken Chen3270cf52018-11-07 01:20:48 +0800332 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900333 }
334 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900335
Ken Chen3270cf52018-11-07 01:20:48 +0800336 /*
337 * check for special cases. (1) numeric servname is disallowed if
338 * socktype/protocol are left unspecified. (2) servname is disallowed
339 * for raw and other inet{,6} sockets.
340 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900341 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
342 ai0 = ai; // backup ai
Bernie Innocenti55864192018-08-30 04:05:20 +0900343
Bernie Innocenti2319a772019-02-20 17:50:38 +0900344 if (ai.ai_family == PF_UNSPEC) {
345 ai.ai_family = PF_INET6;
Ken Chen3270cf52018-11-07 01:20:48 +0800346 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900347 error = get_portmatch(&ai, servname);
Ken Chen3270cf52018-11-07 01:20:48 +0800348 if (error) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900349
Bernie Innocenti2319a772019-02-20 17:50:38 +0900350 ai = ai0; // restore ai
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900351 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900352
Bernie Innocenti2319a772019-02-20 17:50:38 +0900353 ai0 = ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900354
Ken Chen3270cf52018-11-07 01:20:48 +0800355 /* NULL hostname, or numeric hostname */
356 for (ex = explore_options; ex->e_af >= 0; ex++) {
Bernie Innocenti2319a772019-02-20 17:50:38 +0900357 ai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900358
Ken Chen3270cf52018-11-07 01:20:48 +0800359 /* PF_UNSPEC entries are prepared for DNS queries only */
360 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900361
Bernie Innocenti2319a772019-02-20 17:50:38 +0900362 if (!MATCH_FAMILY(ai.ai_family, ex->e_af, WILD_AF(ex))) continue;
363 if (!MATCH(ai.ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
364 if (!MATCH(ai.ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900365
Bernie Innocenti2319a772019-02-20 17:50:38 +0900366 if (ai.ai_family == PF_UNSPEC) ai.ai_family = ex->e_af;
367 if (ai.ai_socktype == ANY && ex->e_socktype != ANY) ai.ai_socktype = ex->e_socktype;
368 if (ai.ai_protocol == ANY && ex->e_protocol != ANY) ai.ai_protocol = ex->e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800369
370 if (hostname == NULL)
Bernie Innocenti2319a772019-02-20 17:50:38 +0900371 error = explore_null(&ai, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800372 else
Bernie Innocenti2319a772019-02-20 17:50:38 +0900373 error = explore_numeric_scope(&ai, hostname, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800374
375 if (error) break;
376
377 while (cur->ai_next) cur = cur->ai_next;
378 }
379 if (error) break;
380
381 /*
382 * XXX
383 * If numeric representation of AF1 can be interpreted as FQDN
384 * representation of AF2, we need to think again about the code below.
385 */
386 if (sentinel.ai_next) break;
387
388 if (hostname == NULL) {
389 error = EAI_NODATA;
390 break;
391 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900392 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chen3270cf52018-11-07 01:20:48 +0800393 error = EAI_NONAME;
394 break;
395 }
396
397 /*
398 * hostname as alphabetical name.
399 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
400 * outer loop by AFs.
401 */
402 for (ex = explore_options; ex->e_af >= 0; ex++) {
Bernie Innocenti2319a772019-02-20 17:50:38 +0900403 ai = ai0;
Ken Chen3270cf52018-11-07 01:20:48 +0800404
405 /* require exact match for family field */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900406 if (ai.ai_family != ex->e_af) continue;
Ken Chen3270cf52018-11-07 01:20:48 +0800407
Bernie Innocenti2319a772019-02-20 17:50:38 +0900408 if (!MATCH(ai.ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800409 continue;
410 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900411 if (!MATCH(ai.ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800412 continue;
413 }
414
Bernie Innocenti2319a772019-02-20 17:50:38 +0900415 if (ai.ai_socktype == ANY && ex->e_socktype != ANY) ai.ai_socktype = ex->e_socktype;
416 if (ai.ai_protocol == ANY && ex->e_protocol != ANY) ai.ai_protocol = ex->e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800417
Bernie Innocenti2319a772019-02-20 17:50:38 +0900418 error = explore_fqdn(&ai, hostname, servname, &cur->ai_next, netcontext);
Ken Chen3270cf52018-11-07 01:20:48 +0800419
420 while (cur->ai_next) cur = cur->ai_next;
421 }
422
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900423 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800424 error = 0;
425 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900426 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800427 }
428 } while (0);
429
430 if (error) {
431 freeaddrinfo(sentinel.ai_next);
432 *res = NULL;
433 } else {
434 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900435 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900436 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900437}
438
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900439// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
441 struct addrinfo** res, const struct android_net_context* netcontext) {
442 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900443 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900444
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445 assert(pai != NULL);
446 /* hostname may be NULL */
447 /* servname may be NULL */
448 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900449
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900451
Bernie Innocenti948f6572018-09-12 21:32:42 +0900452 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900454
Bernie Innocenti948f6572018-09-12 21:32:42 +0900455 if (!files_getaddrinfo(hostname, pai, &result)) {
456 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900457 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900458 if (!error) {
459 struct addrinfo* cur;
460 for (cur = result; cur; cur = cur->ai_next) {
461 GET_PORT(cur, servname);
462 /* canonname should be filled already */
463 }
464 *res = result;
465 return 0;
466 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900467
468free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900469 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900470 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900471}
472
473/*
474 * hostname == NULL.
475 * passive socket -> anyaddr (0.0.0.0 or ::)
476 * non-passive socket -> localhost (127.0.0.1 or ::1)
477 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900478static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
479 int s;
480 const struct afd* afd;
481 struct addrinfo* cur;
482 struct addrinfo sentinel;
483 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485 assert(pai != NULL);
486 /* servname may be NULL */
487 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900488
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 *res = NULL;
490 sentinel.ai_next = NULL;
491 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 /*
494 * filter out AFs that are not supported by the kernel
495 * XXX errno?
496 */
497 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
498 if (s < 0) {
499 if (errno != EMFILE) return 0;
500 } else
501 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 /*
504 * if the servname does not match socktype/protocol, ignore it.
505 */
506 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900507
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900508 afd = find_afd(pai->ai_family);
509 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 if (pai->ai_flags & AI_PASSIVE) {
512 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 GET_PORT(cur->ai_next, servname);
514 } else {
515 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516 GET_PORT(cur->ai_next, servname);
517 }
518 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900519
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900520 *res = sentinel.ai_next;
521 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900522
523free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900524 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900526}
527
528/*
529 * numeric hostname
530 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900531static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
532 struct addrinfo** res, const char* canonname) {
533 const struct afd* afd;
534 struct addrinfo* cur;
535 struct addrinfo sentinel;
536 int error;
537 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900538
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900539 assert(pai != NULL);
540 /* hostname may be NULL */
541 /* servname may be NULL */
542 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 *res = NULL;
545 sentinel.ai_next = NULL;
546 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900547
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900548 /*
549 * if the servname does not match socktype/protocol, ignore it.
550 */
551 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900552
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553 afd = find_afd(pai->ai_family);
554 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900555
Ken Chen15c805a2018-10-17 00:19:59 +0800556 if (inet_pton(afd->a_af, hostname, pton) == 1) {
557 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
558 GET_AI(cur->ai_next, afd, pton);
559 GET_PORT(cur->ai_next, servname);
560 if ((pai->ai_flags & AI_CANONNAME)) {
561 /*
562 * Set the numeric address itself as
563 * the canonical name, based on a
564 * clarification in rfc2553bis-03.
565 */
Ken Chen3270cf52018-11-07 01:20:48 +0800566 error = get_canonname(pai, cur->ai_next, canonname);
567 if (error != 0) {
568 freeaddrinfo(sentinel.ai_next);
569 return error;
570 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 }
Ken Chen15c805a2018-10-17 00:19:59 +0800572 while (cur->ai_next) cur = cur->ai_next;
573 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800574 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900575 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900576
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900577 *res = sentinel.ai_next;
578 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900579
580free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900581 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900582 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900583}
584
585/*
586 * numeric hostname with scope
587 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900588static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
589 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900590 const struct afd* afd;
591 struct addrinfo* cur;
592 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900593 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900594 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 assert(pai != NULL);
597 /* hostname may be NULL */
598 /* servname may be NULL */
599 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 /*
602 * if the servname does not match socktype/protocol, ignore it.
603 */
604 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 afd = find_afd(pai->ai_family);
607 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900609 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900611 cp = strchr(hostname, SCOPE_DELIMITER);
612 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900613
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900614 /*
615 * Handle special case of <scoped_address><delimiter><scope id>
616 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900617 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618 if (hostname2 == NULL) return EAI_MEMORY;
619 /* terminate at the delimiter */
620 hostname2[cp - hostname] = '\0';
621 addr = hostname2;
622 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900623
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624 error = explore_numeric(pai, addr, servname, res, hostname);
625 if (error == 0) {
626 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628 for (cur = *res; cur; cur = cur->ai_next) {
629 if (cur->ai_family != AF_INET6) continue;
630 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
631 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
632 free(hostname2);
633 return (EAI_NODATA); /* XXX: is return OK? */
634 }
635 sin6->sin6_scope_id = scopeid;
636 }
637 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900642}
643
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900644static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
645 assert(pai != NULL);
646 assert(ai != NULL);
647 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 if ((pai->ai_flags & AI_CANONNAME) != 0) {
650 ai->ai_canonname = strdup(str);
651 if (ai->ai_canonname == NULL) return EAI_MEMORY;
652 }
653 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900654}
655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
657 const char* addr) {
658 char* p;
659 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661 assert(pai != NULL);
662 assert(afd != NULL);
663 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900664
nuccachene21023a2018-09-11 11:13:44 +0800665 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900666 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900667
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900668 memcpy(ai, pai, sizeof(struct addrinfo));
669 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800670 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900671
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900672 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
674 p = (char*) (void*) (ai->ai_addr);
675 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
676 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900677}
678
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679static int get_portmatch(const struct addrinfo* ai, const char* servname) {
680 assert(ai != NULL);
681 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900684}
685
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900686static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
687 const char* proto;
688 struct servent* sp;
689 int port;
690 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 assert(ai != NULL);
693 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695 if (servname == NULL) return 0;
696 switch (ai->ai_family) {
697 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900699 break;
700 default:
701 return 0;
702 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900704 switch (ai->ai_socktype) {
705 case SOCK_RAW:
706 return EAI_SERVICE;
707 case SOCK_DGRAM:
708 case SOCK_STREAM:
709 allownumeric = 1;
710 break;
711 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900712 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 break;
714 default:
715 return EAI_SOCKTYPE;
716 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900718 port = str2number(servname);
719 if (port >= 0) {
720 if (!allownumeric) return EAI_SERVICE;
721 if (port < 0 || port > 65535) return EAI_SERVICE;
722 port = htons(port);
723 } else {
724 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900725
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900726 switch (ai->ai_socktype) {
727 case SOCK_DGRAM:
728 proto = "udp";
729 break;
730 case SOCK_STREAM:
731 proto = "tcp";
732 break;
733 default:
734 proto = NULL;
735 break;
736 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
739 port = sp->s_port;
740 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 if (!matchonly) {
743 switch (ai->ai_family) {
744 case AF_INET:
745 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
746 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 case AF_INET6:
748 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
749 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 }
751 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900754}
755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756static const struct afd* find_afd(int af) {
757 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900758
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900759 if (af == PF_UNSPEC) return NULL;
760 for (afd = afdl; afd->a_af; afd++) {
761 if (afd->a_af == af) return afd;
762 }
763 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900764}
765
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900766// Convert a string to a scope identifier.
767static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 u_long lscopeid;
769 struct in6_addr* a6;
770 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900772 assert(scope != NULL);
773 assert(sin6 != NULL);
774 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900778 /* empty scopeid portion is invalid */
779 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
782 /*
783 * We currently assume a one-to-one mapping between links
784 * and interfaces, so we simply use interface indices for
785 * like-local scopes.
786 */
787 *scopeid = if_nametoindex(scope);
788 if (*scopeid == 0) goto trynumeric;
789 return 0;
790 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900791
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900792 /* still unclear about literal, allow numeric only - placeholder */
793 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
794 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
795 goto trynumeric;
796 else
797 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900798
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 /* try to convert to a numeric id as a last resort */
800trynumeric:
801 errno = 0;
802 lscopeid = strtoul(scope, &ep, 10);
803 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
804 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
805 return 0;
806 else
807 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900808}
Bernie Innocenti55864192018-08-30 04:05:20 +0900809
810/* code duplicate with gethnamaddr.c */
811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900813
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900814#define BOUNDED_INCR(x) \
815 do { \
816 BOUNDS_CHECK(cp, x); \
817 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900818 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900819
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820#define BOUNDS_CHECK(ptr, count) \
821 do { \
822 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800823 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 return NULL; \
825 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900826 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800829 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800830 struct addrinfo sentinel = {};
831 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900832 struct addrinfo ai;
833 const struct afd* afd;
834 char* canonname;
835 const HEADER* hp;
836 const u_char* cp;
837 int n;
838 const u_char* eom;
839 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900840 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 int haveanswer, had_error;
842 char tbuf[MAXDNAME];
843 int (*name_ok)(const char*);
844 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900845
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 assert(answer != NULL);
847 assert(qname != NULL);
848 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900849
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900850 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900852 canonname = NULL;
853 eom = answer->buf + anslen;
854 switch (qtype) {
855 case T_A:
856 case T_AAAA:
857 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
858 name_ok = res_hnok;
859 break;
860 default:
861 return NULL; /* XXX should be abort(); */
862 }
863 /*
864 * find first satisfactory answer
865 */
866 hp = &answer->hdr;
867 ancount = ntohs(hp->ancount);
868 qdcount = ntohs(hp->qdcount);
869 bp = hostbuf;
870 ep = hostbuf + sizeof hostbuf;
871 cp = answer->buf;
872 BOUNDED_INCR(HFIXEDSZ);
873 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800874 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 return (NULL);
876 }
877 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
878 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800879 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900880 return (NULL);
881 }
882 BOUNDED_INCR(n + QFIXEDSZ);
883 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
884 /* res_send() has already verified that the query name is the
885 * same as the one we sent; this just gets the expanded name
886 * (i.e., with the succeeding search-domain tacked on).
887 */
888 n = strlen(bp) + 1; /* for the \0 */
889 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800890 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900891 return (NULL);
892 }
893 canonname = bp;
894 bp += n;
895 /* The qname can be abbreviated, but h_name is now absolute. */
896 qname = canonname;
897 }
898 haveanswer = 0;
899 had_error = 0;
900 while (ancount-- > 0 && cp < eom && !had_error) {
901 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
902 if ((n < 0) || !(*name_ok)(bp)) {
903 had_error++;
904 continue;
905 }
906 cp += n; /* name */
907 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900908 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900909 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900910 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900911 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900912 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913 cp += INT16SZ; /* len */
914 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900915 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900916 /* XXX - debug? syslog? */
917 cp += n;
918 continue; /* XXX - had_error++ ? */
919 }
920 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
921 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
922 if ((n < 0) || !(*name_ok)(tbuf)) {
923 had_error++;
924 continue;
925 }
926 cp += n;
927 /* Get canonical name. */
928 n = strlen(tbuf) + 1; /* for the \0 */
929 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
930 had_error++;
931 continue;
932 }
933 strlcpy(bp, tbuf, (size_t)(ep - bp));
934 canonname = bp;
935 bp += n;
936 continue;
937 }
938 if (qtype == T_ANY) {
939 if (!(type == T_A || type == T_AAAA)) {
940 cp += n;
941 continue;
942 }
943 } else if (type != qtype) {
944 if (type != T_KEY && type != T_SIG)
945 syslog(LOG_NOTICE | LOG_AUTH,
946 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
947 p_class(C_IN), p_type(qtype), p_type(type));
948 cp += n;
949 continue; /* XXX - had_error++ ? */
950 }
951 switch (type) {
952 case T_A:
953 case T_AAAA:
954 if (strcasecmp(canonname, bp) != 0) {
955 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
956 cp += n;
957 continue; /* XXX - had_error++ ? */
958 }
959 if (type == T_A && n != INADDRSZ) {
960 cp += n;
961 continue;
962 }
963 if (type == T_AAAA && n != IN6ADDRSZ) {
964 cp += n;
965 continue;
966 }
967 if (type == T_AAAA) {
968 struct in6_addr in6;
969 memcpy(&in6, cp, IN6ADDRSZ);
970 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
971 cp += n;
972 continue;
973 }
974 }
975 if (!haveanswer) {
976 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900977
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 canonname = bp;
979 nn = strlen(bp) + 1; /* for the \0 */
980 bp += nn;
981 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900982
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900983 /* don't overwrite pai */
984 ai = *pai;
985 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
986 afd = find_afd(ai.ai_family);
987 if (afd == NULL) {
988 cp += n;
989 continue;
990 }
991 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
992 if (cur->ai_next == NULL) had_error++;
993 while (cur && cur->ai_next) cur = cur->ai_next;
994 cp += n;
995 break;
996 default:
997 abort();
998 }
999 if (!had_error) haveanswer++;
1000 }
1001 if (haveanswer) {
1002 if (!canonname)
1003 (void) get_canonname(pai, sentinel.ai_next, qname);
1004 else
1005 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +08001006 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001007 return sentinel.ai_next;
1008 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001009
Hungming Chend57ade02018-12-25 15:47:47 +08001010 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001011 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001012}
1013
1014struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001015 struct addrinfo* ai;
1016 int has_src_addr;
1017 sockaddr_union src_addr;
1018 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001019};
1020
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001021static int _get_scope(const struct sockaddr* addr) {
1022 if (addr->sa_family == AF_INET6) {
1023 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1024 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1025 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1026 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1027 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1028 /*
1029 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1030 * link-local scope.
1031 */
1032 return IPV6_ADDR_SCOPE_LINKLOCAL;
1033 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1034 return IPV6_ADDR_SCOPE_SITELOCAL;
1035 } else {
1036 return IPV6_ADDR_SCOPE_GLOBAL;
1037 }
1038 } else if (addr->sa_family == AF_INET) {
1039 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1040 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001041
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001042 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1043 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1044 return IPV6_ADDR_SCOPE_LINKLOCAL;
1045 } else {
1046 /*
1047 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1048 * and shared addresses (100.64.0.0/10), are assigned global scope.
1049 */
1050 return IPV6_ADDR_SCOPE_GLOBAL;
1051 }
1052 } else {
1053 /*
1054 * This should never happen.
1055 * Return a scope with low priority as a last resort.
1056 */
1057 return IPV6_ADDR_SCOPE_NODELOCAL;
1058 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001059}
1060
1061/* These macros are modelled after the ones in <netinet/in6.h>. */
1062
1063/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001064#define IN6_IS_ADDR_TEREDO(a) \
1065 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001066
1067/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001068#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001069
1070/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001071#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001072
1073/*
1074 * Get the label for a given IPv4/IPv6 address.
1075 * RFC 6724, section 2.1.
1076 */
1077
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001078static int _get_label(const struct sockaddr* addr) {
1079 if (addr->sa_family == AF_INET) {
1080 return 4;
1081 } else if (addr->sa_family == AF_INET6) {
1082 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1083 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1084 return 0;
1085 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1086 return 4;
1087 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1088 return 2;
1089 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1090 return 5;
1091 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1092 return 13;
1093 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1094 return 3;
1095 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1096 return 11;
1097 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1098 return 12;
1099 } else {
1100 /* All other IPv6 addresses, including global unicast addresses. */
1101 return 1;
1102 }
1103 } else {
1104 /*
1105 * This should never happen.
1106 * Return a semi-random label as a last resort.
1107 */
1108 return 1;
1109 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001110}
1111
1112/*
1113 * Get the precedence for a given IPv4/IPv6 address.
1114 * RFC 6724, section 2.1.
1115 */
1116
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001117static int _get_precedence(const struct sockaddr* addr) {
1118 if (addr->sa_family == AF_INET) {
1119 return 35;
1120 } else if (addr->sa_family == AF_INET6) {
1121 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1122 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1123 return 50;
1124 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1125 return 35;
1126 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1127 return 30;
1128 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1129 return 5;
1130 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1131 return 3;
1132 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1133 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1134 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1135 return 1;
1136 } else {
1137 /* All other IPv6 addresses, including global unicast addresses. */
1138 return 40;
1139 }
1140 } else {
1141 return 1;
1142 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001143}
1144
1145/*
1146 * Find number of matching initial bits between the two addresses a1 and a2.
1147 */
1148
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001149static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1150 const char* p1 = (const char*) a1;
1151 const char* p2 = (const char*) a2;
1152 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001153
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001154 for (i = 0; i < sizeof(*a1); ++i) {
1155 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001156
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001157 if (p1[i] == p2[i]) {
1158 continue;
1159 }
1160 x = p1[i] ^ p2[i];
1161 for (j = 0; j < CHAR_BIT; ++j) {
1162 if (x & (1 << (CHAR_BIT - 1))) {
1163 return i * CHAR_BIT + j;
1164 }
1165 x <<= 1;
1166 }
1167 }
1168 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001169}
1170
1171/*
1172 * Compare two source/destination address pairs.
1173 * RFC 6724, section 6.
1174 */
1175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001176static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1177 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1178 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1179 int scope_src1, scope_dst1, scope_match1;
1180 int scope_src2, scope_dst2, scope_match2;
1181 int label_src1, label_dst1, label_match1;
1182 int label_src2, label_dst2, label_match2;
1183 int precedence1, precedence2;
1184 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001185
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001186 /* Rule 1: Avoid unusable destinations. */
1187 if (a1->has_src_addr != a2->has_src_addr) {
1188 return a2->has_src_addr - a1->has_src_addr;
1189 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001191 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001192 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001193 scope_dst1 = _get_scope(a1->ai->ai_addr);
1194 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001195
nuccachene172a4e2018-10-23 17:10:58 +08001196 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001197 scope_dst2 = _get_scope(a2->ai->ai_addr);
1198 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001199
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001200 if (scope_match1 != scope_match2) {
1201 return scope_match2 - scope_match1;
1202 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001203
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001204 /*
1205 * Rule 3: Avoid deprecated addresses.
1206 * TODO(sesse): We don't currently have a good way of finding this.
1207 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001209 /*
1210 * Rule 4: Prefer home addresses.
1211 * TODO(sesse): We don't currently have a good way of finding this.
1212 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001213
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001214 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001215 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001216 label_dst1 = _get_label(a1->ai->ai_addr);
1217 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001218
nuccachene172a4e2018-10-23 17:10:58 +08001219 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001220 label_dst2 = _get_label(a2->ai->ai_addr);
1221 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001223 if (label_match1 != label_match2) {
1224 return label_match2 - label_match1;
1225 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001226
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001227 /* Rule 6: Prefer higher precedence. */
1228 precedence1 = _get_precedence(a1->ai->ai_addr);
1229 precedence2 = _get_precedence(a2->ai->ai_addr);
1230 if (precedence1 != precedence2) {
1231 return precedence2 - precedence1;
1232 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001234 /*
1235 * Rule 7: Prefer native transport.
1236 * TODO(sesse): We don't currently have a good way of finding this.
1237 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001238
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001239 /* Rule 8: Prefer smaller scope. */
1240 if (scope_dst1 != scope_dst2) {
1241 return scope_dst1 - scope_dst2;
1242 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001243
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001244 /*
1245 * Rule 9: Use longest matching prefix.
1246 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1247 * to work very well directly applied to IPv4. (glibc uses information from
1248 * the routing table for a custom IPv4 implementation here.)
1249 */
1250 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1251 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001252 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001253 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001254 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001255 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1256 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1257 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1258 if (prefixlen1 != prefixlen2) {
1259 return prefixlen2 - prefixlen1;
1260 }
1261 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001262
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001263 /*
1264 * Rule 10: Leave the order unchanged.
1265 * We need this since qsort() is not necessarily stable.
1266 */
1267 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001268}
1269
1270/*
1271 * Find the source address that will be used if trying to connect to the given
1272 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1273 *
1274 * Returns 1 if a source address was found, 0 if the address is unreachable,
1275 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1276 * undefined.
1277 */
1278
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001279static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1280 uid_t uid) {
1281 int sock;
1282 int ret;
1283 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001284
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001285 switch (addr->sa_family) {
1286 case AF_INET:
1287 len = sizeof(struct sockaddr_in);
1288 break;
1289 case AF_INET6:
1290 len = sizeof(struct sockaddr_in6);
1291 break;
1292 default:
1293 /* No known usable source address for non-INET families. */
1294 return 0;
1295 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001296
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001297 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1298 if (sock == -1) {
1299 if (errno == EAFNOSUPPORT) {
1300 return 0;
1301 } else {
1302 return -1;
1303 }
1304 }
1305 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1306 close(sock);
1307 return 0;
1308 }
1309 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1310 close(sock);
1311 return 0;
1312 }
1313 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001314 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001315 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001316
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001317 if (ret == -1) {
1318 close(sock);
1319 return 0;
1320 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001321
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001322 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1323 close(sock);
1324 return -1;
1325 }
1326 close(sock);
1327 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001328}
1329
1330/*
1331 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1332 * Will leave the list unchanged if an error occurs.
1333 */
1334
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001335static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1336 struct addrinfo* cur;
1337 int nelem = 0, i;
1338 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001339
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001340 cur = list_sentinel->ai_next;
1341 while (cur) {
1342 ++nelem;
1343 cur = cur->ai_next;
1344 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001345
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001346 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1347 if (elems == NULL) {
1348 goto error;
1349 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001350
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001351 /*
1352 * Convert the linked list to an array that also contains the candidate
1353 * source address for each destination address.
1354 */
1355 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1356 int has_src_addr;
1357 assert(cur != NULL);
1358 elems[i].ai = cur;
1359 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001360
nuccachene172a4e2018-10-23 17:10:58 +08001361 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001362 if (has_src_addr == -1) {
1363 goto error;
1364 }
1365 elems[i].has_src_addr = has_src_addr;
1366 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001367
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001368 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1369 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001370
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001371 list_sentinel->ai_next = elems[0].ai;
1372 for (i = 0; i < nelem - 1; ++i) {
1373 elems[i].ai->ai_next = elems[i + 1].ai;
1374 }
1375 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001376
1377error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001378 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001379}
1380
Bernie Innocenti948f6572018-09-12 21:32:42 +09001381static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1382 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001383 struct addrinfo *ai, *cur;
1384 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 struct res_target q, q2;
1386 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001387
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001388 memset(&q, 0, sizeof(q));
1389 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001390 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001391
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001392 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001393 if (buf == NULL) {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001394 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001395 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001396 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001397 if (buf2 == NULL) {
1398 free(buf);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001399 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001400 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001401
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001402 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001403 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001404 /* prefer IPv6 */
1405 q.name = name;
1406 q.qclass = C_IN;
1407 q.answer = buf->buf;
1408 q.anslen = sizeof(buf->buf);
1409 int query_ipv6 = 1, query_ipv4 = 1;
1410 if (pai->ai_flags & AI_ADDRCONFIG) {
1411 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1412 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1413 }
1414 if (query_ipv6) {
1415 q.qtype = T_AAAA;
1416 if (query_ipv4) {
1417 q.next = &q2;
1418 q2.name = name;
1419 q2.qclass = C_IN;
1420 q2.qtype = T_A;
1421 q2.answer = buf2->buf;
1422 q2.anslen = sizeof(buf2->buf);
1423 }
1424 } else if (query_ipv4) {
1425 q.qtype = T_A;
1426 } else {
1427 free(buf);
1428 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001429 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001430 }
1431 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001432 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001433 case AF_INET:
1434 q.name = name;
1435 q.qclass = C_IN;
1436 q.qtype = T_A;
1437 q.answer = buf->buf;
1438 q.anslen = sizeof(buf->buf);
1439 break;
1440 case AF_INET6:
1441 q.name = name;
1442 q.qclass = C_IN;
1443 q.qtype = T_AAAA;
1444 q.answer = buf->buf;
1445 q.anslen = sizeof(buf->buf);
1446 break;
1447 default:
1448 free(buf);
1449 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001450 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001451 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001452
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001453 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001454 if (res == NULL) {
1455 free(buf);
1456 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001457 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001458 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001459
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001460 /* this just sets our netid val in the thread private data so we don't have to
1461 * modify the api's all the way down to res_send.c's res_nsend. We could
1462 * fully populate the thread private data here, but if we get down there
1463 * and have a cache hit that would be wasted, so we do the rest there on miss
1464 */
1465 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001466
Hungming Chend57ade02018-12-25 15:47:47 +08001467 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +08001468 if (res_searchN(name, &q, res, &herrno) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001469 free(buf);
1470 free(buf2);
Hungming Chen7f0d3292018-12-27 18:33:19 +08001471 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
1472 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001473 }
Hungming Chend57ade02018-12-25 15:47:47 +08001474 ai = getanswer(buf, q.n, q.name, q.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001475 if (ai) {
1476 cur->ai_next = ai;
1477 while (cur && cur->ai_next) cur = cur->ai_next;
1478 }
1479 if (q.next) {
Hungming Chend57ade02018-12-25 15:47:47 +08001480 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001481 if (ai) cur->ai_next = ai;
1482 }
1483 free(buf);
1484 free(buf2);
1485 if (sentinel.ai_next == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001486 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001487 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001488
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001489 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001490
Bernie Innocenti948f6572018-09-12 21:32:42 +09001491 *rv = sentinel.ai_next;
1492 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001493}
1494
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001495static void _sethtent(FILE** hostf) {
1496 if (!*hostf)
1497 *hostf = fopen(_PATH_HOSTS, "re");
1498 else
1499 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001500}
1501
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001502static void _endhtent(FILE** hostf) {
1503 if (*hostf) {
1504 (void) fclose(*hostf);
1505 *hostf = NULL;
1506 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001507}
1508
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001509static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1510 char* p;
1511 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001512 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001513 int error;
1514 const char* addr;
1515 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001516
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001517 assert(name != NULL);
1518 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001519
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001520 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1521again:
1522 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1523 if (*p == '#') goto again;
1524 if (!(cp = strpbrk(p, "#\n"))) goto again;
1525 *cp = '\0';
1526 if (!(cp = strpbrk(p, " \t"))) goto again;
1527 *cp++ = '\0';
1528 addr = p;
1529 /* if this is not something we're looking for, skip it. */
1530 cname = NULL;
1531 while (cp && *cp) {
1532 if (*cp == ' ' || *cp == '\t') {
1533 cp++;
1534 continue;
1535 }
1536 if (!cname) cname = cp;
1537 tname = cp;
1538 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1539 // fprintf(stderr, "\ttname = '%s'", tname);
1540 if (strcasecmp(name, tname) == 0) goto found;
1541 }
1542 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001543
1544found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001545 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001546 if (error) goto again;
1547 for (res = res0; res; res = res->ai_next) {
1548 /* cover it up */
1549 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001550
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001551 if (pai->ai_flags & AI_CANONNAME) {
1552 if (get_canonname(pai, res, cname) != 0) {
1553 freeaddrinfo(res0);
1554 goto again;
1555 }
1556 }
1557 }
1558 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001559}
1560
Bernie Innocenti948f6572018-09-12 21:32:42 +09001561static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001562 struct addrinfo sentinel = {};
1563 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001564 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001565
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001566 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001568 _sethtent(&hostf);
1569 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1570 cur->ai_next = p;
1571 while (cur && cur->ai_next) cur = cur->ai_next;
1572 }
1573 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001574
Bernie Innocenti948f6572018-09-12 21:32:42 +09001575 *res = sentinel.ai_next;
1576 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001577}
1578
1579/* resolver logic */
1580
1581/*
1582 * Formulate a normal query, send, and await answer.
1583 * Returned answer is placed in supplied buffer "answer".
1584 * Perform preliminary check of answer, returning success only
1585 * if no error is indicated and the answer count is nonzero.
1586 * Return the size of the response on success, -1 on error.
Hungming Chend57ade02018-12-25 15:47:47 +08001587 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001588 *
1589 * Caller must parse answer and determine whether it answers the question.
1590 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001591static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001592 u_char buf[MAXPACKET];
1593 HEADER* hp;
1594 int n;
1595 struct res_target* t;
1596 int rcode;
1597 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001598
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001599 assert(name != NULL);
1600 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001601
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001602 rcode = NOERROR;
1603 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001605 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 u_char* answer;
1607 int anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001609 hp = (HEADER*) (void*) t->answer;
Ken Chenbfd32022019-01-02 14:59:38 +08001610 bool retried = false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001611 again:
1612 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001613
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001614 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001615 int cl = t->qclass;
1616 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001617 answer = t->answer;
1618 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001619#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001620 if (res->options & RES_DEBUG) printf(";; res_queryN(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001621#endif
1622
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001623 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Ken Chenbfd32022019-01-02 14:59:38 +08001624 if (n > 0 && (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && !retried)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001625 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001626 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001627#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001628 if (res->options & RES_DEBUG) printf(";; res_queryN: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001629#endif
Hungming Chend57ade02018-12-25 15:47:47 +08001630 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001631 return n;
1632 }
Mike Yu69615f62018-11-06 15:42:36 +08001633
Luke Huang952d0942018-12-26 16:53:03 +08001634 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001635 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001636 // Record rcode from DNS response header only if no timeout.
1637 // Keep rcode timeout for reporting later if any.
1638 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001639 /* if the query choked with EDNS0, retry without EDNS0 */
1640 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
Ken Chenbfd32022019-01-02 14:59:38 +08001641 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001642#ifdef DEBUG
Ken Chenbfd32022019-01-02 14:59:38 +08001643 if (res->options & RES_DEBUG) printf(";; res_queryN: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001644#endif
Ken Chenbfd32022019-01-02 14:59:38 +08001645 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001646 goto again;
1647 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001648#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649 if (res->options & RES_DEBUG)
1650 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001651#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001652 continue;
1653 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001654
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001655 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001656
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001657 t->n = n;
1658 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001659
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001660 if (ancount == 0) {
1661 switch (rcode) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001662 // Not defined in RFC.
1663 case RCODE_TIMEOUT:
1664 // DNS metrics monitors DNS query timeout.
1665 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1666 break;
1667 // Defined in RFC 1035 section 4.1.1.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001668 case NXDOMAIN:
Hungming Chend57ade02018-12-25 15:47:47 +08001669 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001670 break;
1671 case SERVFAIL:
Hungming Chend57ade02018-12-25 15:47:47 +08001672 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001673 break;
1674 case NOERROR:
Hungming Chend57ade02018-12-25 15:47:47 +08001675 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001676 break;
1677 case FORMERR:
1678 case NOTIMP:
1679 case REFUSED:
1680 default:
Hungming Chend57ade02018-12-25 15:47:47 +08001681 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001682 break;
1683 }
1684 return -1;
1685 }
1686 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001687}
1688
1689/*
1690 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1691 * Return the size of the response on success, -1 on error.
1692 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chend57ade02018-12-25 15:47:47 +08001693 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001694 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001695static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001696 const char *cp, *const *domain;
1697 HEADER* hp;
1698 u_int dots;
1699 int trailing_dot, ret, saved_herrno;
1700 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001701
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001702 assert(name != NULL);
1703 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001704
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001705 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001706
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001707 errno = 0;
Hungming Chend57ade02018-12-25 15:47:47 +08001708 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001709 dots = 0;
1710 for (cp = name; *cp; cp++) dots += (*cp == '.');
1711 trailing_dot = 0;
1712 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001714 /*
1715 * If there are dots in the name already, let's just give it a try
1716 * 'as is'. The threshold can be set with the "ndots" option.
1717 */
1718 saved_herrno = -1;
1719 if (dots >= res->ndots) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001720 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001721 if (ret > 0) return (ret);
Hungming Chend57ade02018-12-25 15:47:47 +08001722 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 tried_as_is++;
1724 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001725
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001726 /*
1727 * We do at least one level of search if
1728 * - there is no dot and RES_DEFNAME is set, or
1729 * - there is at least one dot, there is no trailing dot,
1730 * and RES_DNSRCH is set.
1731 */
1732 if ((!dots && (res->options & RES_DEFNAMES)) ||
1733 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1734 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001735
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001736 /* Unfortunately we need to set stuff up before
1737 * the domain stuff is tried. Will have a better
1738 * fix after thread pools are used.
1739 */
1740 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001742 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001743 ret = res_querydomainN(name, *domain, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001744 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001745
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001746 /*
1747 * If no server present, give up.
1748 * If name isn't found in this domain,
1749 * keep trying higher domains in the search list
1750 * (if that's enabled).
1751 * On a NO_DATA error, keep trying, otherwise
1752 * a wildcard entry of another type could keep us
1753 * from finding this entry higher in the domain.
1754 * If we get some other error (negative answer or
1755 * server failure), then stop searching up,
1756 * but try the input name below in case it's
1757 * fully-qualified.
1758 */
1759 if (errno == ECONNREFUSED) {
Hungming Chend57ade02018-12-25 15:47:47 +08001760 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001761 return -1;
1762 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001763
Hungming Chend57ade02018-12-25 15:47:47 +08001764 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001765 case NO_DATA:
1766 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001767 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001768 case HOST_NOT_FOUND:
1769 /* keep trying */
1770 break;
1771 case TRY_AGAIN:
1772 if (hp->rcode == SERVFAIL) {
1773 /* try next search element, if any */
1774 got_servfail++;
1775 break;
1776 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001777 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001778 default:
1779 /* anything else implies that we're done */
1780 done++;
1781 }
1782 /*
1783 * if we got here for some reason other than DNSRCH,
1784 * we only wanted one iteration of the loop, so stop.
1785 */
1786 if (!(res->options & RES_DNSRCH)) done++;
1787 }
1788 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001790 /*
1791 * if we have not already tried the name "as is", do that now.
1792 * note that we do this regardless of how many dots were in the
1793 * name or whether it ends with a dot.
1794 */
1795 if (!tried_as_is) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001796 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001797 if (ret > 0) return ret;
1798 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001799
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001800 /*
1801 * if we got here, we didn't satisfy the search.
1802 * if we did an initial full query, return that query's h_errno
1803 * (note that we wouldn't be here if that query had succeeded).
1804 * else if we ever got a nodata, send that back as the reason.
1805 * else send back meaningless h_errno, that being the one from
1806 * the last DNSRCH we did.
1807 */
1808 if (saved_herrno != -1)
Hungming Chend57ade02018-12-25 15:47:47 +08001809 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001810 else if (got_nodata)
Hungming Chend57ade02018-12-25 15:47:47 +08001811 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001812 else if (got_servfail)
Hungming Chend57ade02018-12-25 15:47:47 +08001813 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001814 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001815}
1816
1817/*
1818 * Perform a call on res_query on the concatenation of name and domain,
1819 * removing a trailing dot from name if domain is NULL.
1820 */
Mike Yu69615f62018-11-06 15:42:36 +08001821static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +08001822 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001823 char nbuf[MAXDNAME];
1824 const char* longname = nbuf;
1825 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001826
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001827 assert(name != NULL);
1828 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001829
1830#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001831 if (res->options & RES_DEBUG)
1832 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001833#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001834 if (domain == NULL) {
1835 /*
1836 * Check for trailing '.';
1837 * copy without '.' if present.
1838 */
1839 n = strlen(name);
1840 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001841 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001842 return -1;
1843 }
1844 if (n > 0 && name[--n] == '.') {
1845 strncpy(nbuf, name, n);
1846 nbuf[n] = '\0';
1847 } else
1848 longname = name;
1849 } else {
1850 n = strlen(name);
1851 d = strlen(domain);
1852 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001853 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001854 return -1;
1855 }
1856 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1857 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001858 return res_queryN(longname, target, res, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001859}