blob: 84aa278248ef1d77f3f88146eb2f91de86cbef25 [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 Chend57ade02018-12-25 15:47:47 +0800147// TODO: Consider that refactor res_queryN, res_searchN and res_querydomainN to return one error
148// code but two error codes.
149static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error,
150 int* herrno);
151static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error,
152 int* herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800153static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chend57ade02018-12-25 15:47:47 +0800154 int* ai_error, int* herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900155
Bernie Innocenti93a31342018-12-12 00:43:02 +0900156const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157 "Success",
158 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
159 "Temporary failure in name resolution", /* EAI_AGAIN */
160 "Invalid value for ai_flags", /* EAI_BADFLAGS */
161 "Non-recoverable failure in name resolution", /* EAI_FAIL */
162 "ai_family not supported", /* EAI_FAMILY */
163 "Memory allocation failure", /* EAI_MEMORY */
164 "No address associated with hostname", /* EAI_NODATA */
165 "hostname nor servname provided, or not known", /* EAI_NONAME */
166 "servname not supported for ai_socktype", /* EAI_SERVICE */
167 "ai_socktype not supported", /* EAI_SOCKTYPE */
168 "System error returned in errno", /* EAI_SYSTEM */
169 "Invalid value for hints", /* EAI_BADHINTS */
170 "Resolved protocol is unknown", /* EAI_PROTOCOL */
171 "Argument buffer overflow", /* EAI_OVERFLOW */
172 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900173};
174
175/* XXX macros that make external reference is BAD. */
176
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900177#define GET_AI(ai, afd, addr) \
178 do { \
179 /* external reference: pai, error, and label free */ \
180 (ai) = get_ai(pai, (afd), (addr)); \
181 if ((ai) == NULL) { \
182 error = EAI_MEMORY; \
183 goto free; \
184 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900185 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187#define GET_PORT(ai, serv) \
188 do { \
189 /* external reference: error and label free */ \
190 error = get_port((ai), (serv), 0); \
191 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900192 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900195 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
196#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198const char* gai_strerror(int ecode) {
199 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
200 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900201}
202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900204 while (ai) {
205 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900207 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208 free(ai);
209 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900210 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900211}
212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213static int str2number(const char* p) {
214 char* ep;
215 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900218
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900219 if (*p == '\0') return -1;
220 ep = NULL;
221 errno = 0;
222 v = strtoul(p, &ep, 10);
223 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
224 return v;
225 else
226 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900227}
228
229/*
230 * The following functions determine whether IPv4 or IPv6 connectivity is
231 * available in order to implement AI_ADDRCONFIG.
232 *
233 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
234 * available, but whether addresses of the specified family are "configured
235 * on the local system". However, bionic doesn't currently support getifaddrs,
236 * so checking for connectivity is the next best thing.
237 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900238static int _have_ipv6(unsigned mark, uid_t uid) {
239 static const struct sockaddr_in6 sin6_test = {
240 .sin6_family = AF_INET6,
241 .sin6_addr.s6_addr = {// 2000::
242 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800243 sockaddr_union addr = {.sin6 = sin6_test};
244 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900245}
246
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247static int _have_ipv4(unsigned mark, uid_t uid) {
248 static const struct sockaddr_in sin_test = {
249 .sin_family = AF_INET,
250 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
251 };
nuccachene172a4e2018-10-23 17:10:58 +0800252 sockaddr_union addr = {.sin = sin_test};
253 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900254}
255
Bernie Innocentic165ce82018-10-16 23:35:28 +0900256// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
257// NOTE: also called by resolv_set_nameservers_for_net().
258int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
259 addrinfo** result) {
260 hints.ai_flags = AI_NUMERICHOST;
261 const android_net_context netcontext = {
262 .app_netid = NETID_UNSET,
263 .app_mark = MARK_UNSET,
264 .dns_netid = NETID_UNSET,
265 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266 .uid = NET_CONTEXT_INVALID_UID,
267 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900268 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900269}
270
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900271int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
272 const struct addrinfo* hints,
273 const struct android_net_context* netcontext,
274 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800275 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 struct addrinfo* cur;
277 int error = 0;
278 struct addrinfo ai;
279 struct addrinfo ai0;
280 struct addrinfo* pai;
281 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900282
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900283 /* hostname is allowed to be NULL */
284 /* servname is allowed to be NULL */
285 /* hints is allowed to be NULL */
286 assert(res != NULL);
287 assert(netcontext != NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900288 cur = &sentinel;
289 pai = &ai;
290 pai->ai_flags = 0;
291 pai->ai_family = PF_UNSPEC;
292 pai->ai_socktype = ANY;
293 pai->ai_protocol = ANY;
294 pai->ai_addrlen = 0;
295 pai->ai_canonname = NULL;
296 pai->ai_addr = NULL;
297 pai->ai_next = NULL;
Ken Chen3270cf52018-11-07 01:20:48 +0800298 do {
299 if (hostname == NULL && servname == NULL) {
300 error = EAI_NONAME;
301 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900302 }
Ken Chen3270cf52018-11-07 01:20:48 +0800303 if (hints) {
304 /* error check for hints */
305 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
306 error = EAI_BADHINTS;
307 break;
308 }
309 if (hints->ai_flags & ~AI_MASK) {
310 error = EAI_BADFLAGS;
311 break;
312 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900313
Ken Chen3270cf52018-11-07 01:20:48 +0800314 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
315 hints->ai_family == PF_INET6)) {
316 error = EAI_FAMILY;
317 break;
318 }
319 *pai = *hints;
320
321 /*
322 * if both socktype/protocol are specified, check if they
323 * are meaningful combination.
324 */
325 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
326 for (ex = explore_options; ex->e_af >= 0; ex++) {
327 if (pai->ai_family != ex->e_af) continue;
328 if (ex->e_socktype == ANY) continue;
329 if (ex->e_protocol == ANY) continue;
330 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
331 error = EAI_BADHINTS;
332 break;
333 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900334 }
Ken Chen3270cf52018-11-07 01:20:48 +0800335 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900336 }
337 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900338
Ken Chen3270cf52018-11-07 01:20:48 +0800339 /*
340 * check for special cases. (1) numeric servname is disallowed if
341 * socktype/protocol are left unspecified. (2) servname is disallowed
342 * for raw and other inet{,6} sockets.
343 */
344 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
345 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
346 ) {
347 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900348
Ken Chen3270cf52018-11-07 01:20:48 +0800349 if (pai->ai_family == PF_UNSPEC) {
350 pai->ai_family = PF_INET6;
351 }
352 error = get_portmatch(pai, servname);
353 if (error) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900354
Ken Chen3270cf52018-11-07 01:20:48 +0800355 *pai = ai0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900356 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900357
Ken Chen3270cf52018-11-07 01:20:48 +0800358 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900359
Ken Chen3270cf52018-11-07 01:20:48 +0800360 /* NULL hostname, or numeric hostname */
361 for (ex = explore_options; ex->e_af >= 0; ex++) {
362 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900363
Ken Chen3270cf52018-11-07 01:20:48 +0800364 /* PF_UNSPEC entries are prepared for DNS queries only */
365 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900366
Ken Chen3270cf52018-11-07 01:20:48 +0800367 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
368 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
369 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900370
Ken Chen3270cf52018-11-07 01:20:48 +0800371 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
372 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
373 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
374
375 if (hostname == NULL)
376 error = explore_null(pai, servname, &cur->ai_next);
377 else
378 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
379
380 if (error) break;
381
382 while (cur->ai_next) cur = cur->ai_next;
383 }
384 if (error) break;
385
386 /*
387 * XXX
388 * If numeric representation of AF1 can be interpreted as FQDN
389 * representation of AF2, we need to think again about the code below.
390 */
391 if (sentinel.ai_next) break;
392
393 if (hostname == NULL) {
394 error = EAI_NODATA;
395 break;
396 }
397 if (pai->ai_flags & AI_NUMERICHOST) {
398 error = EAI_NONAME;
399 break;
400 }
401
402 /*
403 * hostname as alphabetical name.
404 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
405 * outer loop by AFs.
406 */
407 for (ex = explore_options; ex->e_af >= 0; ex++) {
408 *pai = ai0;
409
410 /* require exact match for family field */
411 if (pai->ai_family != ex->e_af) continue;
412
413 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
414 continue;
415 }
416 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
417 continue;
418 }
419
420 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
421 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
422
423 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
424
425 while (cur->ai_next) cur = cur->ai_next;
426 }
427
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900428 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800429 error = 0;
430 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900431 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800432 }
433 } while (0);
434
435 if (error) {
436 freeaddrinfo(sentinel.ai_next);
437 *res = NULL;
438 } else {
439 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900442}
443
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900444// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
446 struct addrinfo** res, const struct android_net_context* netcontext) {
447 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900448 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900449
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 assert(pai != NULL);
451 /* hostname may be NULL */
452 /* servname may be NULL */
453 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900454
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900455 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456
Bernie Innocenti948f6572018-09-12 21:32:42 +0900457 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900458 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900459
Bernie Innocenti948f6572018-09-12 21:32:42 +0900460 if (!files_getaddrinfo(hostname, pai, &result)) {
461 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900462 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900463 if (!error) {
464 struct addrinfo* cur;
465 for (cur = result; cur; cur = cur->ai_next) {
466 GET_PORT(cur, servname);
467 /* canonname should be filled already */
468 }
469 *res = result;
470 return 0;
471 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900472
473free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900474 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900476}
477
478/*
479 * hostname == NULL.
480 * passive socket -> anyaddr (0.0.0.0 or ::)
481 * non-passive socket -> localhost (127.0.0.1 or ::1)
482 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
484 int s;
485 const struct afd* afd;
486 struct addrinfo* cur;
487 struct addrinfo sentinel;
488 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 assert(pai != NULL);
491 /* servname may be NULL */
492 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900493
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 *res = NULL;
495 sentinel.ai_next = NULL;
496 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900497
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 /*
499 * filter out AFs that are not supported by the kernel
500 * XXX errno?
501 */
502 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
503 if (s < 0) {
504 if (errno != EMFILE) return 0;
505 } else
506 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900507
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900508 /*
509 * if the servname does not match socktype/protocol, ignore it.
510 */
511 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900512
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 afd = find_afd(pai->ai_family);
514 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900515
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516 if (pai->ai_flags & AI_PASSIVE) {
517 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900518 GET_PORT(cur->ai_next, servname);
519 } else {
520 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521 GET_PORT(cur->ai_next, servname);
522 }
523 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900524
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525 *res = sentinel.ai_next;
526 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900527
528free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900529 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900530 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900531}
532
533/*
534 * numeric hostname
535 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900536static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
537 struct addrinfo** res, const char* canonname) {
538 const struct afd* afd;
539 struct addrinfo* cur;
540 struct addrinfo sentinel;
541 int error;
542 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 assert(pai != NULL);
545 /* hostname may be NULL */
546 /* servname may be NULL */
547 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900548
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 *res = NULL;
550 sentinel.ai_next = NULL;
551 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900552
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553 /*
554 * if the servname does not match socktype/protocol, ignore it.
555 */
556 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900557
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900558 afd = find_afd(pai->ai_family);
559 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900560
Ken Chen15c805a2018-10-17 00:19:59 +0800561 if (inet_pton(afd->a_af, hostname, pton) == 1) {
562 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
563 GET_AI(cur->ai_next, afd, pton);
564 GET_PORT(cur->ai_next, servname);
565 if ((pai->ai_flags & AI_CANONNAME)) {
566 /*
567 * Set the numeric address itself as
568 * the canonical name, based on a
569 * clarification in rfc2553bis-03.
570 */
Ken Chen3270cf52018-11-07 01:20:48 +0800571 error = get_canonname(pai, cur->ai_next, canonname);
572 if (error != 0) {
573 freeaddrinfo(sentinel.ai_next);
574 return error;
575 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900576 }
Ken Chen15c805a2018-10-17 00:19:59 +0800577 while (cur->ai_next) cur = cur->ai_next;
578 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800579 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900580 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900581
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900582 *res = sentinel.ai_next;
583 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900584
585free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900586 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900587 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900588}
589
590/*
591 * numeric hostname with scope
592 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900593static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
594 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900595 const struct afd* afd;
596 struct addrinfo* cur;
597 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900598 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900599 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 assert(pai != NULL);
602 /* hostname may be NULL */
603 /* servname may be NULL */
604 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 /*
607 * if the servname does not match socktype/protocol, ignore it.
608 */
609 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900611 afd = find_afd(pai->ai_family);
612 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900613
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900614 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900615
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900616 cp = strchr(hostname, SCOPE_DELIMITER);
617 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900618
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 /*
620 * Handle special case of <scoped_address><delimiter><scope id>
621 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900622 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 if (hostname2 == NULL) return EAI_MEMORY;
624 /* terminate at the delimiter */
625 hostname2[cp - hostname] = '\0';
626 addr = hostname2;
627 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900628
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629 error = explore_numeric(pai, addr, servname, res, hostname);
630 if (error == 0) {
631 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900632
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633 for (cur = *res; cur; cur = cur->ai_next) {
634 if (cur->ai_family != AF_INET6) continue;
635 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
636 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
637 free(hostname2);
638 return (EAI_NODATA); /* XXX: is return OK? */
639 }
640 sin6->sin6_scope_id = scopeid;
641 }
642 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900643
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900644 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900645
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900646 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900647}
648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
650 assert(pai != NULL);
651 assert(ai != NULL);
652 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900654 if ((pai->ai_flags & AI_CANONNAME) != 0) {
655 ai->ai_canonname = strdup(str);
656 if (ai->ai_canonname == NULL) return EAI_MEMORY;
657 }
658 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900659}
660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
662 const char* addr) {
663 char* p;
664 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900665
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900666 assert(pai != NULL);
667 assert(afd != NULL);
668 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900669
nuccachene21023a2018-09-11 11:13:44 +0800670 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900671 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 memcpy(ai, pai, sizeof(struct addrinfo));
674 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800675 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900678 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
679 p = (char*) (void*) (ai->ai_addr);
680 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
681 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900682}
683
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900684static int get_portmatch(const struct addrinfo* ai, const char* servname) {
685 assert(ai != NULL);
686 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900687
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900688 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900689}
690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
692 const char* proto;
693 struct servent* sp;
694 int port;
695 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697 assert(ai != NULL);
698 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900699
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 if (servname == NULL) return 0;
701 switch (ai->ai_family) {
702 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900704 break;
705 default:
706 return 0;
707 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900708
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709 switch (ai->ai_socktype) {
710 case SOCK_RAW:
711 return EAI_SERVICE;
712 case SOCK_DGRAM:
713 case SOCK_STREAM:
714 allownumeric = 1;
715 break;
716 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900717 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900718 break;
719 default:
720 return EAI_SOCKTYPE;
721 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900723 port = str2number(servname);
724 if (port >= 0) {
725 if (!allownumeric) return EAI_SERVICE;
726 if (port < 0 || port > 65535) return EAI_SERVICE;
727 port = htons(port);
728 } else {
729 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731 switch (ai->ai_socktype) {
732 case SOCK_DGRAM:
733 proto = "udp";
734 break;
735 case SOCK_STREAM:
736 proto = "tcp";
737 break;
738 default:
739 proto = NULL;
740 break;
741 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900743 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
744 port = sp->s_port;
745 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900746
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 if (!matchonly) {
748 switch (ai->ai_family) {
749 case AF_INET:
750 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
751 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 case AF_INET6:
753 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
754 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900755 }
756 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900759}
760
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900761static const struct afd* find_afd(int af) {
762 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900763
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 if (af == PF_UNSPEC) return NULL;
765 for (afd = afdl; afd->a_af; afd++) {
766 if (afd->a_af == af) return afd;
767 }
768 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900769}
770
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900771// Convert a string to a scope identifier.
772static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 u_long lscopeid;
774 struct in6_addr* a6;
775 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 assert(scope != NULL);
778 assert(sin6 != NULL);
779 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900782
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900783 /* empty scopeid portion is invalid */
784 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900785
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900786 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
787 /*
788 * We currently assume a one-to-one mapping between links
789 * and interfaces, so we simply use interface indices for
790 * like-local scopes.
791 */
792 *scopeid = if_nametoindex(scope);
793 if (*scopeid == 0) goto trynumeric;
794 return 0;
795 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900796
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900797 /* still unclear about literal, allow numeric only - placeholder */
798 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
799 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
800 goto trynumeric;
801 else
802 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900803
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900804 /* try to convert to a numeric id as a last resort */
805trynumeric:
806 errno = 0;
807 lscopeid = strtoul(scope, &ep, 10);
808 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
809 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
810 return 0;
811 else
812 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900813}
Bernie Innocenti55864192018-08-30 04:05:20 +0900814
815/* code duplicate with gethnamaddr.c */
816
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900817static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819#define BOUNDED_INCR(x) \
820 do { \
821 BOUNDS_CHECK(cp, x); \
822 cp += (x); \
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 +0900825#define BOUNDS_CHECK(ptr, count) \
826 do { \
827 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800828 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 return NULL; \
830 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900831 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900832
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900833static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800834 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800835 struct addrinfo sentinel = {};
836 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900837 struct addrinfo ai;
838 const struct afd* afd;
839 char* canonname;
840 const HEADER* hp;
841 const u_char* cp;
842 int n;
843 const u_char* eom;
844 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900845 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 int haveanswer, had_error;
847 char tbuf[MAXDNAME];
848 int (*name_ok)(const char*);
849 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900850
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 assert(answer != NULL);
852 assert(qname != NULL);
853 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900854
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900855 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900856
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900857 canonname = NULL;
858 eom = answer->buf + anslen;
859 switch (qtype) {
860 case T_A:
861 case T_AAAA:
862 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
863 name_ok = res_hnok;
864 break;
865 default:
866 return NULL; /* XXX should be abort(); */
867 }
868 /*
869 * find first satisfactory answer
870 */
871 hp = &answer->hdr;
872 ancount = ntohs(hp->ancount);
873 qdcount = ntohs(hp->qdcount);
874 bp = hostbuf;
875 ep = hostbuf + sizeof hostbuf;
876 cp = answer->buf;
877 BOUNDED_INCR(HFIXEDSZ);
878 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800879 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900880 return (NULL);
881 }
882 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
883 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800884 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900885 return (NULL);
886 }
887 BOUNDED_INCR(n + QFIXEDSZ);
888 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
889 /* res_send() has already verified that the query name is the
890 * same as the one we sent; this just gets the expanded name
891 * (i.e., with the succeeding search-domain tacked on).
892 */
893 n = strlen(bp) + 1; /* for the \0 */
894 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800895 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900896 return (NULL);
897 }
898 canonname = bp;
899 bp += n;
900 /* The qname can be abbreviated, but h_name is now absolute. */
901 qname = canonname;
902 }
903 haveanswer = 0;
904 had_error = 0;
905 while (ancount-- > 0 && cp < eom && !had_error) {
906 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
907 if ((n < 0) || !(*name_ok)(bp)) {
908 had_error++;
909 continue;
910 }
911 cp += n; /* name */
912 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900913 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900915 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900916 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900917 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918 cp += INT16SZ; /* len */
919 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900920 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900921 /* XXX - debug? syslog? */
922 cp += n;
923 continue; /* XXX - had_error++ ? */
924 }
925 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
926 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
927 if ((n < 0) || !(*name_ok)(tbuf)) {
928 had_error++;
929 continue;
930 }
931 cp += n;
932 /* Get canonical name. */
933 n = strlen(tbuf) + 1; /* for the \0 */
934 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
935 had_error++;
936 continue;
937 }
938 strlcpy(bp, tbuf, (size_t)(ep - bp));
939 canonname = bp;
940 bp += n;
941 continue;
942 }
943 if (qtype == T_ANY) {
944 if (!(type == T_A || type == T_AAAA)) {
945 cp += n;
946 continue;
947 }
948 } else if (type != qtype) {
949 if (type != T_KEY && type != T_SIG)
950 syslog(LOG_NOTICE | LOG_AUTH,
951 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
952 p_class(C_IN), p_type(qtype), p_type(type));
953 cp += n;
954 continue; /* XXX - had_error++ ? */
955 }
956 switch (type) {
957 case T_A:
958 case T_AAAA:
959 if (strcasecmp(canonname, bp) != 0) {
960 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
961 cp += n;
962 continue; /* XXX - had_error++ ? */
963 }
964 if (type == T_A && n != INADDRSZ) {
965 cp += n;
966 continue;
967 }
968 if (type == T_AAAA && n != IN6ADDRSZ) {
969 cp += n;
970 continue;
971 }
972 if (type == T_AAAA) {
973 struct in6_addr in6;
974 memcpy(&in6, cp, IN6ADDRSZ);
975 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
976 cp += n;
977 continue;
978 }
979 }
980 if (!haveanswer) {
981 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900982
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900983 canonname = bp;
984 nn = strlen(bp) + 1; /* for the \0 */
985 bp += nn;
986 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900987
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900988 /* don't overwrite pai */
989 ai = *pai;
990 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
991 afd = find_afd(ai.ai_family);
992 if (afd == NULL) {
993 cp += n;
994 continue;
995 }
996 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
997 if (cur->ai_next == NULL) had_error++;
998 while (cur && cur->ai_next) cur = cur->ai_next;
999 cp += n;
1000 break;
1001 default:
1002 abort();
1003 }
1004 if (!had_error) haveanswer++;
1005 }
1006 if (haveanswer) {
1007 if (!canonname)
1008 (void) get_canonname(pai, sentinel.ai_next, qname);
1009 else
1010 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +08001011 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001012 return sentinel.ai_next;
1013 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001014
Hungming Chend57ade02018-12-25 15:47:47 +08001015 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001016 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001017}
1018
1019struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001020 struct addrinfo* ai;
1021 int has_src_addr;
1022 sockaddr_union src_addr;
1023 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001024};
1025
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001026static int _get_scope(const struct sockaddr* addr) {
1027 if (addr->sa_family == AF_INET6) {
1028 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1029 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1030 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1031 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1032 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1033 /*
1034 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1035 * link-local scope.
1036 */
1037 return IPV6_ADDR_SCOPE_LINKLOCAL;
1038 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1039 return IPV6_ADDR_SCOPE_SITELOCAL;
1040 } else {
1041 return IPV6_ADDR_SCOPE_GLOBAL;
1042 }
1043 } else if (addr->sa_family == AF_INET) {
1044 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1045 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001046
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001047 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1048 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1049 return IPV6_ADDR_SCOPE_LINKLOCAL;
1050 } else {
1051 /*
1052 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1053 * and shared addresses (100.64.0.0/10), are assigned global scope.
1054 */
1055 return IPV6_ADDR_SCOPE_GLOBAL;
1056 }
1057 } else {
1058 /*
1059 * This should never happen.
1060 * Return a scope with low priority as a last resort.
1061 */
1062 return IPV6_ADDR_SCOPE_NODELOCAL;
1063 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001064}
1065
1066/* These macros are modelled after the ones in <netinet/in6.h>. */
1067
1068/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001069#define IN6_IS_ADDR_TEREDO(a) \
1070 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001071
1072/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001073#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001074
1075/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001076#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001077
1078/*
1079 * Get the label for a given IPv4/IPv6 address.
1080 * RFC 6724, section 2.1.
1081 */
1082
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001083static int _get_label(const struct sockaddr* addr) {
1084 if (addr->sa_family == AF_INET) {
1085 return 4;
1086 } else if (addr->sa_family == AF_INET6) {
1087 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1088 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1089 return 0;
1090 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1091 return 4;
1092 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1093 return 2;
1094 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1095 return 5;
1096 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1097 return 13;
1098 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1099 return 3;
1100 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1101 return 11;
1102 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1103 return 12;
1104 } else {
1105 /* All other IPv6 addresses, including global unicast addresses. */
1106 return 1;
1107 }
1108 } else {
1109 /*
1110 * This should never happen.
1111 * Return a semi-random label as a last resort.
1112 */
1113 return 1;
1114 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001115}
1116
1117/*
1118 * Get the precedence for a given IPv4/IPv6 address.
1119 * RFC 6724, section 2.1.
1120 */
1121
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001122static int _get_precedence(const struct sockaddr* addr) {
1123 if (addr->sa_family == AF_INET) {
1124 return 35;
1125 } else if (addr->sa_family == AF_INET6) {
1126 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1127 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1128 return 50;
1129 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1130 return 35;
1131 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1132 return 30;
1133 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1134 return 5;
1135 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1136 return 3;
1137 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1138 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1139 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1140 return 1;
1141 } else {
1142 /* All other IPv6 addresses, including global unicast addresses. */
1143 return 40;
1144 }
1145 } else {
1146 return 1;
1147 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001148}
1149
1150/*
1151 * Find number of matching initial bits between the two addresses a1 and a2.
1152 */
1153
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001154static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1155 const char* p1 = (const char*) a1;
1156 const char* p2 = (const char*) a2;
1157 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001158
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001159 for (i = 0; i < sizeof(*a1); ++i) {
1160 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001161
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001162 if (p1[i] == p2[i]) {
1163 continue;
1164 }
1165 x = p1[i] ^ p2[i];
1166 for (j = 0; j < CHAR_BIT; ++j) {
1167 if (x & (1 << (CHAR_BIT - 1))) {
1168 return i * CHAR_BIT + j;
1169 }
1170 x <<= 1;
1171 }
1172 }
1173 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001174}
1175
1176/*
1177 * Compare two source/destination address pairs.
1178 * RFC 6724, section 6.
1179 */
1180
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001181static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1182 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1183 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1184 int scope_src1, scope_dst1, scope_match1;
1185 int scope_src2, scope_dst2, scope_match2;
1186 int label_src1, label_dst1, label_match1;
1187 int label_src2, label_dst2, label_match2;
1188 int precedence1, precedence2;
1189 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001191 /* Rule 1: Avoid unusable destinations. */
1192 if (a1->has_src_addr != a2->has_src_addr) {
1193 return a2->has_src_addr - a1->has_src_addr;
1194 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001195
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001196 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001197 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001198 scope_dst1 = _get_scope(a1->ai->ai_addr);
1199 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001200
nuccachene172a4e2018-10-23 17:10:58 +08001201 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001202 scope_dst2 = _get_scope(a2->ai->ai_addr);
1203 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001204
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001205 if (scope_match1 != scope_match2) {
1206 return scope_match2 - scope_match1;
1207 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001209 /*
1210 * Rule 3: Avoid deprecated 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 /*
1215 * Rule 4: Prefer home addresses.
1216 * TODO(sesse): We don't currently have a good way of finding this.
1217 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001218
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001219 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001220 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001221 label_dst1 = _get_label(a1->ai->ai_addr);
1222 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001223
nuccachene172a4e2018-10-23 17:10:58 +08001224 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001225 label_dst2 = _get_label(a2->ai->ai_addr);
1226 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001228 if (label_match1 != label_match2) {
1229 return label_match2 - label_match1;
1230 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001232 /* Rule 6: Prefer higher precedence. */
1233 precedence1 = _get_precedence(a1->ai->ai_addr);
1234 precedence2 = _get_precedence(a2->ai->ai_addr);
1235 if (precedence1 != precedence2) {
1236 return precedence2 - precedence1;
1237 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001238
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001239 /*
1240 * Rule 7: Prefer native transport.
1241 * TODO(sesse): We don't currently have a good way of finding this.
1242 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001243
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001244 /* Rule 8: Prefer smaller scope. */
1245 if (scope_dst1 != scope_dst2) {
1246 return scope_dst1 - scope_dst2;
1247 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001248
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001249 /*
1250 * Rule 9: Use longest matching prefix.
1251 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1252 * to work very well directly applied to IPv4. (glibc uses information from
1253 * the routing table for a custom IPv4 implementation here.)
1254 */
1255 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1256 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001257 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001258 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001259 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001260 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1261 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1262 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1263 if (prefixlen1 != prefixlen2) {
1264 return prefixlen2 - prefixlen1;
1265 }
1266 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001267
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001268 /*
1269 * Rule 10: Leave the order unchanged.
1270 * We need this since qsort() is not necessarily stable.
1271 */
1272 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001273}
1274
1275/*
1276 * Find the source address that will be used if trying to connect to the given
1277 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1278 *
1279 * Returns 1 if a source address was found, 0 if the address is unreachable,
1280 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1281 * undefined.
1282 */
1283
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001284static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1285 uid_t uid) {
1286 int sock;
1287 int ret;
1288 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001289
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001290 switch (addr->sa_family) {
1291 case AF_INET:
1292 len = sizeof(struct sockaddr_in);
1293 break;
1294 case AF_INET6:
1295 len = sizeof(struct sockaddr_in6);
1296 break;
1297 default:
1298 /* No known usable source address for non-INET families. */
1299 return 0;
1300 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001301
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001302 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1303 if (sock == -1) {
1304 if (errno == EAFNOSUPPORT) {
1305 return 0;
1306 } else {
1307 return -1;
1308 }
1309 }
1310 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1311 close(sock);
1312 return 0;
1313 }
1314 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1315 close(sock);
1316 return 0;
1317 }
1318 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001319 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001320 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001321
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001322 if (ret == -1) {
1323 close(sock);
1324 return 0;
1325 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001326
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001327 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1328 close(sock);
1329 return -1;
1330 }
1331 close(sock);
1332 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001333}
1334
1335/*
1336 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1337 * Will leave the list unchanged if an error occurs.
1338 */
1339
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001340static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1341 struct addrinfo* cur;
1342 int nelem = 0, i;
1343 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001344
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001345 cur = list_sentinel->ai_next;
1346 while (cur) {
1347 ++nelem;
1348 cur = cur->ai_next;
1349 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001350
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001351 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1352 if (elems == NULL) {
1353 goto error;
1354 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001355
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001356 /*
1357 * Convert the linked list to an array that also contains the candidate
1358 * source address for each destination address.
1359 */
1360 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1361 int has_src_addr;
1362 assert(cur != NULL);
1363 elems[i].ai = cur;
1364 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001365
nuccachene172a4e2018-10-23 17:10:58 +08001366 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001367 if (has_src_addr == -1) {
1368 goto error;
1369 }
1370 elems[i].has_src_addr = has_src_addr;
1371 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001372
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001373 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1374 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001375
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001376 list_sentinel->ai_next = elems[0].ai;
1377 for (i = 0; i < nelem - 1; ++i) {
1378 elems[i].ai->ai_next = elems[i + 1].ai;
1379 }
1380 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001381
1382error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001383 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001384}
1385
Bernie Innocenti948f6572018-09-12 21:32:42 +09001386static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1387 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001388 struct addrinfo *ai, *cur;
1389 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001390 struct res_target q, q2;
1391 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001392
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001393 memset(&q, 0, sizeof(q));
1394 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001395 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001396
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001397 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001398 if (buf == NULL) {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001399 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001400 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001401 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001402 if (buf2 == NULL) {
1403 free(buf);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001404 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001405 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001406
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001407 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001408 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001409 /* prefer IPv6 */
1410 q.name = name;
1411 q.qclass = C_IN;
1412 q.answer = buf->buf;
1413 q.anslen = sizeof(buf->buf);
1414 int query_ipv6 = 1, query_ipv4 = 1;
1415 if (pai->ai_flags & AI_ADDRCONFIG) {
1416 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1417 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1418 }
1419 if (query_ipv6) {
1420 q.qtype = T_AAAA;
1421 if (query_ipv4) {
1422 q.next = &q2;
1423 q2.name = name;
1424 q2.qclass = C_IN;
1425 q2.qtype = T_A;
1426 q2.answer = buf2->buf;
1427 q2.anslen = sizeof(buf2->buf);
1428 }
1429 } else if (query_ipv4) {
1430 q.qtype = T_A;
1431 } else {
1432 free(buf);
1433 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001434 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001435 }
1436 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001437 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001438 case AF_INET:
1439 q.name = name;
1440 q.qclass = C_IN;
1441 q.qtype = T_A;
1442 q.answer = buf->buf;
1443 q.anslen = sizeof(buf->buf);
1444 break;
1445 case AF_INET6:
1446 q.name = name;
1447 q.qclass = C_IN;
1448 q.qtype = T_AAAA;
1449 q.answer = buf->buf;
1450 q.anslen = sizeof(buf->buf);
1451 break;
1452 default:
1453 free(buf);
1454 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001455 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001456 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001457
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001458 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001459 if (res == NULL) {
1460 free(buf);
1461 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001462 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001463 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001464
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001465 /* this just sets our netid val in the thread private data so we don't have to
1466 * modify the api's all the way down to res_send.c's res_nsend. We could
1467 * fully populate the thread private data here, but if we get down there
1468 * and have a cache hit that would be wasted, so we do the rest there on miss
1469 */
1470 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001471
1472 // Pass ai_error to catch more detailed errors rather than EAI_NODATA.
1473 int ai_error = EAI_NODATA;
Hungming Chend57ade02018-12-25 15:47:47 +08001474 int herrno = NETDB_INTERNAL;
1475 if (res_searchN(name, &q, res, &ai_error, &herrno) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001476 free(buf);
1477 free(buf2);
Mike Yu69615f62018-11-06 15:42:36 +08001478 return ai_error; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001479 }
Hungming Chend57ade02018-12-25 15:47:47 +08001480 ai = getanswer(buf, q.n, q.name, q.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001481 if (ai) {
1482 cur->ai_next = ai;
1483 while (cur && cur->ai_next) cur = cur->ai_next;
1484 }
1485 if (q.next) {
Hungming Chend57ade02018-12-25 15:47:47 +08001486 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001487 if (ai) cur->ai_next = ai;
1488 }
1489 free(buf);
1490 free(buf2);
1491 if (sentinel.ai_next == NULL) {
Hungming Chend57ade02018-12-25 15:47:47 +08001492 return herrnoToAiError(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001493 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001494
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001495 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001496
Bernie Innocenti948f6572018-09-12 21:32:42 +09001497 *rv = sentinel.ai_next;
1498 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001499}
1500
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001501static void _sethtent(FILE** hostf) {
1502 if (!*hostf)
1503 *hostf = fopen(_PATH_HOSTS, "re");
1504 else
1505 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001506}
1507
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001508static void _endhtent(FILE** hostf) {
1509 if (*hostf) {
1510 (void) fclose(*hostf);
1511 *hostf = NULL;
1512 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001513}
1514
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001515static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1516 char* p;
1517 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001518 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001519 int error;
1520 const char* addr;
1521 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001522
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001523 assert(name != NULL);
1524 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001525
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001526 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1527again:
1528 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1529 if (*p == '#') goto again;
1530 if (!(cp = strpbrk(p, "#\n"))) goto again;
1531 *cp = '\0';
1532 if (!(cp = strpbrk(p, " \t"))) goto again;
1533 *cp++ = '\0';
1534 addr = p;
1535 /* if this is not something we're looking for, skip it. */
1536 cname = NULL;
1537 while (cp && *cp) {
1538 if (*cp == ' ' || *cp == '\t') {
1539 cp++;
1540 continue;
1541 }
1542 if (!cname) cname = cp;
1543 tname = cp;
1544 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1545 // fprintf(stderr, "\ttname = '%s'", tname);
1546 if (strcasecmp(name, tname) == 0) goto found;
1547 }
1548 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001549
1550found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001551 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001552 if (error) goto again;
1553 for (res = res0; res; res = res->ai_next) {
1554 /* cover it up */
1555 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001556
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001557 if (pai->ai_flags & AI_CANONNAME) {
1558 if (get_canonname(pai, res, cname) != 0) {
1559 freeaddrinfo(res0);
1560 goto again;
1561 }
1562 }
1563 }
1564 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001565}
1566
Bernie Innocenti948f6572018-09-12 21:32:42 +09001567static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001568 struct addrinfo sentinel = {};
1569 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001570 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001571
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001572 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001574 _sethtent(&hostf);
1575 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1576 cur->ai_next = p;
1577 while (cur && cur->ai_next) cur = cur->ai_next;
1578 }
1579 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001580
Bernie Innocenti948f6572018-09-12 21:32:42 +09001581 *res = sentinel.ai_next;
1582 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001583}
1584
1585/* resolver logic */
1586
1587/*
1588 * Formulate a normal query, send, and await answer.
1589 * Returned answer is placed in supplied buffer "answer".
1590 * Perform preliminary check of answer, returning success only
1591 * if no error is indicated and the answer count is nonzero.
1592 * Return the size of the response on success, -1 on error.
Hungming Chend57ade02018-12-25 15:47:47 +08001593 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001594 *
1595 * Caller must parse answer and determine whether it answers the question.
1596 */
Hungming Chend57ade02018-12-25 15:47:47 +08001597static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error,
1598 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001599 u_char buf[MAXPACKET];
1600 HEADER* hp;
1601 int n;
1602 struct res_target* t;
1603 int rcode;
1604 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 assert(name != NULL);
1607 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001609 rcode = NOERROR;
1610 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001611
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001612 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001613 u_char* answer;
1614 int anslen;
1615 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001616
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001617 hp = (HEADER*) (void*) t->answer;
1618 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001620 again:
1621 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001622
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001623 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001624 int cl = t->qclass;
1625 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001626 answer = t->answer;
1627 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001628#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001629 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001630#endif
1631
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001632 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001633 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1634 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1635 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001636 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001637#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001638 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001639#endif
Hungming Chend57ade02018-12-25 15:47:47 +08001640 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001641 return n;
1642 }
Mike Yu69615f62018-11-06 15:42:36 +08001643
1644 n = res_nsend(res, buf, n, answer, anslen, &rcode);
1645 *ai_error = rcodeToAiError(rcode);
Bernie Innocenti55864192018-08-30 04:05:20 +09001646
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001647 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1648 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649 /* if the query choked with EDNS0, retry without EDNS0 */
1650 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1651 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1652 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001653#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001654 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001655#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001656 goto again;
1657 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001658#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001659 if (res->options & RES_DEBUG)
1660 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001661#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001662 continue;
1663 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001665 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001666
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001667 t->n = n;
1668 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001669
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001670 if (ancount == 0) {
1671 switch (rcode) {
1672 case NXDOMAIN:
Hungming Chend57ade02018-12-25 15:47:47 +08001673 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001674 break;
1675 case SERVFAIL:
Hungming Chend57ade02018-12-25 15:47:47 +08001676 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001677 break;
1678 case NOERROR:
Hungming Chend57ade02018-12-25 15:47:47 +08001679 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001680 break;
1681 case FORMERR:
1682 case NOTIMP:
1683 case REFUSED:
1684 default:
Hungming Chend57ade02018-12-25 15:47:47 +08001685 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001686 break;
1687 }
1688 return -1;
1689 }
1690 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001691}
1692
1693/*
1694 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1695 * Return the size of the response on success, -1 on error.
1696 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chend57ade02018-12-25 15:47:47 +08001697 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001698 */
Hungming Chend57ade02018-12-25 15:47:47 +08001699static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error,
1700 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001701 const char *cp, *const *domain;
1702 HEADER* hp;
1703 u_int dots;
1704 int trailing_dot, ret, saved_herrno;
1705 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001706
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001707 assert(name != NULL);
1708 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001709
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001710 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001711
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001712 errno = 0;
Hungming Chend57ade02018-12-25 15:47:47 +08001713 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001714 dots = 0;
1715 for (cp = name; *cp; cp++) dots += (*cp == '.');
1716 trailing_dot = 0;
1717 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001718
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001719 /*
1720 * If there are dots in the name already, let's just give it a try
1721 * 'as is'. The threshold can be set with the "ndots" option.
1722 */
1723 saved_herrno = -1;
1724 if (dots >= res->ndots) {
Hungming Chend57ade02018-12-25 15:47:47 +08001725 ret = res_querydomainN(name, NULL, target, res, ai_error, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001726 if (ret > 0) return (ret);
Hungming Chend57ade02018-12-25 15:47:47 +08001727 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001728 tried_as_is++;
1729 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001731 /*
1732 * We do at least one level of search if
1733 * - there is no dot and RES_DEFNAME is set, or
1734 * - there is at least one dot, there is no trailing dot,
1735 * and RES_DNSRCH is set.
1736 */
1737 if ((!dots && (res->options & RES_DEFNAMES)) ||
1738 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1739 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001740
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001741 /* Unfortunately we need to set stuff up before
1742 * the domain stuff is tried. Will have a better
1743 * fix after thread pools are used.
1744 */
1745 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001746
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001747 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Hungming Chend57ade02018-12-25 15:47:47 +08001748 ret = res_querydomainN(name, *domain, target, res, ai_error, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001749 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001750
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001751 /*
1752 * If no server present, give up.
1753 * If name isn't found in this domain,
1754 * keep trying higher domains in the search list
1755 * (if that's enabled).
1756 * On a NO_DATA error, keep trying, otherwise
1757 * a wildcard entry of another type could keep us
1758 * from finding this entry higher in the domain.
1759 * If we get some other error (negative answer or
1760 * server failure), then stop searching up,
1761 * but try the input name below in case it's
1762 * fully-qualified.
1763 */
1764 if (errno == ECONNREFUSED) {
Hungming Chend57ade02018-12-25 15:47:47 +08001765 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001766 return -1;
1767 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001768
Hungming Chend57ade02018-12-25 15:47:47 +08001769 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001770 case NO_DATA:
1771 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001772 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001773 case HOST_NOT_FOUND:
1774 /* keep trying */
1775 break;
1776 case TRY_AGAIN:
1777 if (hp->rcode == SERVFAIL) {
1778 /* try next search element, if any */
1779 got_servfail++;
1780 break;
1781 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001782 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001783 default:
1784 /* anything else implies that we're done */
1785 done++;
1786 }
1787 /*
1788 * if we got here for some reason other than DNSRCH,
1789 * we only wanted one iteration of the loop, so stop.
1790 */
1791 if (!(res->options & RES_DNSRCH)) done++;
1792 }
1793 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001794
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001795 /*
1796 * if we have not already tried the name "as is", do that now.
1797 * note that we do this regardless of how many dots were in the
1798 * name or whether it ends with a dot.
1799 */
1800 if (!tried_as_is) {
Hungming Chend57ade02018-12-25 15:47:47 +08001801 ret = res_querydomainN(name, NULL, target, res, ai_error, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001802 if (ret > 0) return ret;
1803 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001805 /*
1806 * if we got here, we didn't satisfy the search.
1807 * if we did an initial full query, return that query's h_errno
1808 * (note that we wouldn't be here if that query had succeeded).
1809 * else if we ever got a nodata, send that back as the reason.
1810 * else send back meaningless h_errno, that being the one from
1811 * the last DNSRCH we did.
1812 */
1813 if (saved_herrno != -1)
Hungming Chend57ade02018-12-25 15:47:47 +08001814 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001815 else if (got_nodata)
Hungming Chend57ade02018-12-25 15:47:47 +08001816 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001817 else if (got_servfail)
Hungming Chend57ade02018-12-25 15:47:47 +08001818 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001819 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001820}
1821
1822/*
1823 * Perform a call on res_query on the concatenation of name and domain,
1824 * removing a trailing dot from name if domain is NULL.
1825 */
Mike Yu69615f62018-11-06 15:42:36 +08001826static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chend57ade02018-12-25 15:47:47 +08001827 int* ai_error, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001828 char nbuf[MAXDNAME];
1829 const char* longname = nbuf;
1830 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001831
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001832 assert(name != NULL);
1833 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001834
1835#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001836 if (res->options & RES_DEBUG)
1837 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001838#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001839 if (domain == NULL) {
1840 /*
1841 * Check for trailing '.';
1842 * copy without '.' if present.
1843 */
1844 n = strlen(name);
1845 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001846 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001847 return -1;
1848 }
1849 if (n > 0 && name[--n] == '.') {
1850 strncpy(nbuf, name, n);
1851 nbuf[n] = '\0';
1852 } else
1853 longname = name;
1854 } else {
1855 n = strlen(name);
1856 d = strlen(domain);
1857 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001858 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001859 return -1;
1860 }
1861 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1862 }
Hungming Chend57ade02018-12-25 15:47:47 +08001863 return res_queryN(longname, target, res, ai_error, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001864}