blob: 45c379292510688232a2673738982c8711edc3bb [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
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900137static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900138static int dns_getaddrinfo(const char* name, const addrinfo* pai,
139 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900140static void _sethtent(FILE**);
141static void _endhtent(FILE**);
142static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900143static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900144static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900145
Mike Yu69615f62018-11-06 15:42:36 +0800146static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error);
147static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error);
148static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
149 int* ai_error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900150
Bernie Innocenti93a31342018-12-12 00:43:02 +0900151const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 "Success",
153 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
154 "Temporary failure in name resolution", /* EAI_AGAIN */
155 "Invalid value for ai_flags", /* EAI_BADFLAGS */
156 "Non-recoverable failure in name resolution", /* EAI_FAIL */
157 "ai_family not supported", /* EAI_FAMILY */
158 "Memory allocation failure", /* EAI_MEMORY */
159 "No address associated with hostname", /* EAI_NODATA */
160 "hostname nor servname provided, or not known", /* EAI_NONAME */
161 "servname not supported for ai_socktype", /* EAI_SERVICE */
162 "ai_socktype not supported", /* EAI_SOCKTYPE */
163 "System error returned in errno", /* EAI_SYSTEM */
164 "Invalid value for hints", /* EAI_BADHINTS */
165 "Resolved protocol is unknown", /* EAI_PROTOCOL */
166 "Argument buffer overflow", /* EAI_OVERFLOW */
167 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900168};
169
170/* XXX macros that make external reference is BAD. */
171
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172#define GET_AI(ai, afd, addr) \
173 do { \
174 /* external reference: pai, error, and label free */ \
175 (ai) = get_ai(pai, (afd), (addr)); \
176 if ((ai) == NULL) { \
177 error = EAI_MEMORY; \
178 goto free; \
179 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900180 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900181
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182#define GET_PORT(ai, serv) \
183 do { \
184 /* external reference: error and label free */ \
185 error = get_port((ai), (serv), 0); \
186 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900187 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900190 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
191#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900192
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900193const char* gai_strerror(int ecode) {
194 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
195 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900196}
197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900199 while (ai) {
200 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900202 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203 free(ai);
204 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900205 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900206}
207
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208static int str2number(const char* p) {
209 char* ep;
210 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900213
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214 if (*p == '\0') return -1;
215 ep = NULL;
216 errno = 0;
217 v = strtoul(p, &ep, 10);
218 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
219 return v;
220 else
221 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900222}
223
224/*
225 * The following functions determine whether IPv4 or IPv6 connectivity is
226 * available in order to implement AI_ADDRCONFIG.
227 *
228 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
229 * available, but whether addresses of the specified family are "configured
230 * on the local system". However, bionic doesn't currently support getifaddrs,
231 * so checking for connectivity is the next best thing.
232 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233static int _have_ipv6(unsigned mark, uid_t uid) {
234 static const struct sockaddr_in6 sin6_test = {
235 .sin6_family = AF_INET6,
236 .sin6_addr.s6_addr = {// 2000::
237 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800238 sockaddr_union addr = {.sin6 = sin6_test};
239 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900240}
241
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242static int _have_ipv4(unsigned mark, uid_t uid) {
243 static const struct sockaddr_in sin_test = {
244 .sin_family = AF_INET,
245 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
246 };
nuccachene172a4e2018-10-23 17:10:58 +0800247 sockaddr_union addr = {.sin = sin_test};
248 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900249}
250
Bernie Innocentic165ce82018-10-16 23:35:28 +0900251// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
252// NOTE: also called by resolv_set_nameservers_for_net().
253int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
254 addrinfo** result) {
255 hints.ai_flags = AI_NUMERICHOST;
256 const android_net_context netcontext = {
257 .app_netid = NETID_UNSET,
258 .app_mark = MARK_UNSET,
259 .dns_netid = NETID_UNSET,
260 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900261 .uid = NET_CONTEXT_INVALID_UID,
262 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900263 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900264}
265
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
267 const struct addrinfo* hints,
268 const struct android_net_context* netcontext,
269 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800270 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900271 struct addrinfo* cur;
272 int error = 0;
273 struct addrinfo ai;
274 struct addrinfo ai0;
275 struct addrinfo* pai;
276 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;
284 pai = &ai;
285 pai->ai_flags = 0;
286 pai->ai_family = PF_UNSPEC;
287 pai->ai_socktype = ANY;
288 pai->ai_protocol = ANY;
289 pai->ai_addrlen = 0;
290 pai->ai_canonname = NULL;
291 pai->ai_addr = NULL;
292 pai->ai_next = NULL;
Ken Chen3270cf52018-11-07 01:20:48 +0800293 do {
294 if (hostname == NULL && servname == NULL) {
295 error = EAI_NONAME;
296 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900297 }
Ken Chen3270cf52018-11-07 01:20:48 +0800298 if (hints) {
299 /* error check for hints */
300 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
301 error = EAI_BADHINTS;
302 break;
303 }
304 if (hints->ai_flags & ~AI_MASK) {
305 error = EAI_BADFLAGS;
306 break;
307 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900308
Ken Chen3270cf52018-11-07 01:20:48 +0800309 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
310 hints->ai_family == PF_INET6)) {
311 error = EAI_FAMILY;
312 break;
313 }
314 *pai = *hints;
315
316 /*
317 * if both socktype/protocol are specified, check if they
318 * are meaningful combination.
319 */
320 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
321 for (ex = explore_options; ex->e_af >= 0; ex++) {
322 if (pai->ai_family != ex->e_af) continue;
323 if (ex->e_socktype == ANY) continue;
324 if (ex->e_protocol == ANY) continue;
325 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
326 error = EAI_BADHINTS;
327 break;
328 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900329 }
Ken Chen3270cf52018-11-07 01:20:48 +0800330 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 }
332 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900333
Ken Chen3270cf52018-11-07 01:20:48 +0800334 /*
335 * check for special cases. (1) numeric servname is disallowed if
336 * socktype/protocol are left unspecified. (2) servname is disallowed
337 * for raw and other inet{,6} sockets.
338 */
339 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
340 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
341 ) {
342 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900343
Ken Chen3270cf52018-11-07 01:20:48 +0800344 if (pai->ai_family == PF_UNSPEC) {
345 pai->ai_family = PF_INET6;
346 }
347 error = get_portmatch(pai, servname);
348 if (error) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900349
Ken Chen3270cf52018-11-07 01:20:48 +0800350 *pai = ai0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900351 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900352
Ken Chen3270cf52018-11-07 01:20:48 +0800353 ai0 = *pai;
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++) {
357 *pai = 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
Ken Chen3270cf52018-11-07 01:20:48 +0800362 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
363 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
364 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900365
Ken Chen3270cf52018-11-07 01:20:48 +0800366 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
367 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
368 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
369
370 if (hostname == NULL)
371 error = explore_null(pai, servname, &cur->ai_next);
372 else
373 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
374
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 }
392 if (pai->ai_flags & AI_NUMERICHOST) {
393 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++) {
403 *pai = ai0;
404
405 /* require exact match for family field */
406 if (pai->ai_family != ex->e_af) continue;
407
408 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
409 continue;
410 }
411 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
412 continue;
413 }
414
415 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
416 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
417
418 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
419
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)) { \
823 h_errno = NO_RECOVERY; \
824 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,
829 const struct addrinfo* pai) {
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) {
874 h_errno = NO_RECOVERY;
875 return (NULL);
876 }
877 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
878 if ((n < 0) || !(*name_ok)(bp)) {
879 h_errno = NO_RECOVERY;
880 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) {
890 h_errno = NO_RECOVERY;
891 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);
1006 h_errno = NETDB_SUCCESS;
1007 return sentinel.ai_next;
1008 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001009
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001010 h_errno = NO_RECOVERY;
1011 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) {
1394 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001395 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001396 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001397 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001398 if (buf2 == NULL) {
1399 free(buf);
1400 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001401 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001402 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001403
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001404 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001405 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001406 /* prefer IPv6 */
1407 q.name = name;
1408 q.qclass = C_IN;
1409 q.answer = buf->buf;
1410 q.anslen = sizeof(buf->buf);
1411 int query_ipv6 = 1, query_ipv4 = 1;
1412 if (pai->ai_flags & AI_ADDRCONFIG) {
1413 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1414 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1415 }
1416 if (query_ipv6) {
1417 q.qtype = T_AAAA;
1418 if (query_ipv4) {
1419 q.next = &q2;
1420 q2.name = name;
1421 q2.qclass = C_IN;
1422 q2.qtype = T_A;
1423 q2.answer = buf2->buf;
1424 q2.anslen = sizeof(buf2->buf);
1425 }
1426 } else if (query_ipv4) {
1427 q.qtype = T_A;
1428 } else {
1429 free(buf);
1430 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001431 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001432 }
1433 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001434 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001435 case AF_INET:
1436 q.name = name;
1437 q.qclass = C_IN;
1438 q.qtype = T_A;
1439 q.answer = buf->buf;
1440 q.anslen = sizeof(buf->buf);
1441 break;
1442 case AF_INET6:
1443 q.name = name;
1444 q.qclass = C_IN;
1445 q.qtype = T_AAAA;
1446 q.answer = buf->buf;
1447 q.anslen = sizeof(buf->buf);
1448 break;
1449 default:
1450 free(buf);
1451 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001452 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001453 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001454
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001455 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001456 if (res == NULL) {
1457 free(buf);
1458 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001459 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001460 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001461
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001462 /* this just sets our netid val in the thread private data so we don't have to
1463 * modify the api's all the way down to res_send.c's res_nsend. We could
1464 * fully populate the thread private data here, but if we get down there
1465 * and have a cache hit that would be wasted, so we do the rest there on miss
1466 */
1467 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001468
1469 // Pass ai_error to catch more detailed errors rather than EAI_NODATA.
1470 int ai_error = EAI_NODATA;
1471 if (res_searchN(name, &q, res, &ai_error) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001472 free(buf);
1473 free(buf2);
Mike Yu69615f62018-11-06 15:42:36 +08001474 return ai_error; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001475 }
1476 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1477 if (ai) {
1478 cur->ai_next = ai;
1479 while (cur && cur->ai_next) cur = cur->ai_next;
1480 }
1481 if (q.next) {
1482 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1483 if (ai) cur->ai_next = ai;
1484 }
1485 free(buf);
1486 free(buf2);
1487 if (sentinel.ai_next == NULL) {
Mike Yu69615f62018-11-06 15:42:36 +08001488 return herrnoToAiError(h_errno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001489 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001490
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001491 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001492
Bernie Innocenti948f6572018-09-12 21:32:42 +09001493 *rv = sentinel.ai_next;
1494 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001495}
1496
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001497static void _sethtent(FILE** hostf) {
1498 if (!*hostf)
1499 *hostf = fopen(_PATH_HOSTS, "re");
1500 else
1501 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001502}
1503
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001504static void _endhtent(FILE** hostf) {
1505 if (*hostf) {
1506 (void) fclose(*hostf);
1507 *hostf = NULL;
1508 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001509}
1510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001511static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1512 char* p;
1513 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001514 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001515 int error;
1516 const char* addr;
1517 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001518
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001519 assert(name != NULL);
1520 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001521
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001522 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1523again:
1524 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1525 if (*p == '#') goto again;
1526 if (!(cp = strpbrk(p, "#\n"))) goto again;
1527 *cp = '\0';
1528 if (!(cp = strpbrk(p, " \t"))) goto again;
1529 *cp++ = '\0';
1530 addr = p;
1531 /* if this is not something we're looking for, skip it. */
1532 cname = NULL;
1533 while (cp && *cp) {
1534 if (*cp == ' ' || *cp == '\t') {
1535 cp++;
1536 continue;
1537 }
1538 if (!cname) cname = cp;
1539 tname = cp;
1540 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1541 // fprintf(stderr, "\ttname = '%s'", tname);
1542 if (strcasecmp(name, tname) == 0) goto found;
1543 }
1544 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001545
1546found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001547 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001548 if (error) goto again;
1549 for (res = res0; res; res = res->ai_next) {
1550 /* cover it up */
1551 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001552
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001553 if (pai->ai_flags & AI_CANONNAME) {
1554 if (get_canonname(pai, res, cname) != 0) {
1555 freeaddrinfo(res0);
1556 goto again;
1557 }
1558 }
1559 }
1560 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001561}
1562
Bernie Innocenti948f6572018-09-12 21:32:42 +09001563static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001564 struct addrinfo sentinel = {};
1565 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001566 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001568 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001569
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001570 _sethtent(&hostf);
1571 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1572 cur->ai_next = p;
1573 while (cur && cur->ai_next) cur = cur->ai_next;
1574 }
1575 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001576
Bernie Innocenti948f6572018-09-12 21:32:42 +09001577 *res = sentinel.ai_next;
1578 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001579}
1580
1581/* resolver logic */
1582
1583/*
1584 * Formulate a normal query, send, and await answer.
1585 * Returned answer is placed in supplied buffer "answer".
1586 * Perform preliminary check of answer, returning success only
1587 * if no error is indicated and the answer count is nonzero.
1588 * Return the size of the response on success, -1 on error.
1589 * Error number is left in h_errno.
1590 *
1591 * Caller must parse answer and determine whether it answers the question.
1592 */
Mike Yu69615f62018-11-06 15:42:36 +08001593static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001594 u_char buf[MAXPACKET];
1595 HEADER* hp;
1596 int n;
1597 struct res_target* t;
1598 int rcode;
1599 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001601 assert(name != NULL);
1602 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001603
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001604 rcode = NOERROR;
1605 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001606
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001607 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001608 u_char* answer;
1609 int anslen;
1610 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001611
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001612 hp = (HEADER*) (void*) t->answer;
1613 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001615 again:
1616 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001617
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001618 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001619 int cl = t->qclass;
1620 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001621 answer = t->answer;
1622 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001623#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001624 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001625#endif
1626
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001627 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001628 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1629 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1630 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001631 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001632#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001633 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001634#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001635 h_errno = NO_RECOVERY;
1636 return n;
1637 }
Mike Yu69615f62018-11-06 15:42:36 +08001638
1639 n = res_nsend(res, buf, n, answer, anslen, &rcode);
1640 *ai_error = rcodeToAiError(rcode);
Bernie Innocenti55864192018-08-30 04:05:20 +09001641
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001642 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1643 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001644 /* if the query choked with EDNS0, retry without EDNS0 */
1645 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1646 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1647 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001648#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001650#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001651 goto again;
1652 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001653#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001654 if (res->options & RES_DEBUG)
1655 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001656#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001657 continue;
1658 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001659
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001660 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001661
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001662 t->n = n;
1663 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001665 if (ancount == 0) {
1666 switch (rcode) {
1667 case NXDOMAIN:
1668 h_errno = HOST_NOT_FOUND;
1669 break;
1670 case SERVFAIL:
1671 h_errno = TRY_AGAIN;
1672 break;
1673 case NOERROR:
1674 h_errno = NO_DATA;
1675 break;
1676 case FORMERR:
1677 case NOTIMP:
1678 case REFUSED:
1679 default:
1680 h_errno = NO_RECOVERY;
1681 break;
1682 }
1683 return -1;
1684 }
1685 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001686}
1687
1688/*
1689 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1690 * Return the size of the response on success, -1 on error.
1691 * If enabled, implement search rules until answer or unrecoverable failure
1692 * is detected. Error code, if any, is left in h_errno.
1693 */
Mike Yu69615f62018-11-06 15:42:36 +08001694static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001695 const char *cp, *const *domain;
1696 HEADER* hp;
1697 u_int dots;
1698 int trailing_dot, ret, saved_herrno;
1699 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001701 assert(name != NULL);
1702 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001704 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001705
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001706 errno = 0;
1707 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1708 dots = 0;
1709 for (cp = name; *cp; cp++) dots += (*cp == '.');
1710 trailing_dot = 0;
1711 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001713 /*
1714 * If there are dots in the name already, let's just give it a try
1715 * 'as is'. The threshold can be set with the "ndots" option.
1716 */
1717 saved_herrno = -1;
1718 if (dots >= res->ndots) {
Mike Yu69615f62018-11-06 15:42:36 +08001719 ret = res_querydomainN(name, NULL, target, res, ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001720 if (ret > 0) return (ret);
1721 saved_herrno = h_errno;
1722 tried_as_is++;
1723 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001725 /*
1726 * We do at least one level of search if
1727 * - there is no dot and RES_DEFNAME is set, or
1728 * - there is at least one dot, there is no trailing dot,
1729 * and RES_DNSRCH is set.
1730 */
1731 if ((!dots && (res->options & RES_DEFNAMES)) ||
1732 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1733 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001735 /* Unfortunately we need to set stuff up before
1736 * the domain stuff is tried. Will have a better
1737 * fix after thread pools are used.
1738 */
1739 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001740
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001741 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Mike Yu69615f62018-11-06 15:42:36 +08001742 ret = res_querydomainN(name, *domain, target, res, ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001743 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001745 /*
1746 * If no server present, give up.
1747 * If name isn't found in this domain,
1748 * keep trying higher domains in the search list
1749 * (if that's enabled).
1750 * On a NO_DATA error, keep trying, otherwise
1751 * a wildcard entry of another type could keep us
1752 * from finding this entry higher in the domain.
1753 * If we get some other error (negative answer or
1754 * server failure), then stop searching up,
1755 * but try the input name below in case it's
1756 * fully-qualified.
1757 */
1758 if (errno == ECONNREFUSED) {
1759 h_errno = TRY_AGAIN;
1760 return -1;
1761 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001762
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001763 switch (h_errno) {
1764 case NO_DATA:
1765 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001766 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001767 case HOST_NOT_FOUND:
1768 /* keep trying */
1769 break;
1770 case TRY_AGAIN:
1771 if (hp->rcode == SERVFAIL) {
1772 /* try next search element, if any */
1773 got_servfail++;
1774 break;
1775 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001776 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001777 default:
1778 /* anything else implies that we're done */
1779 done++;
1780 }
1781 /*
1782 * if we got here for some reason other than DNSRCH,
1783 * we only wanted one iteration of the loop, so stop.
1784 */
1785 if (!(res->options & RES_DNSRCH)) done++;
1786 }
1787 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001788
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001789 /*
1790 * if we have not already tried the name "as is", do that now.
1791 * note that we do this regardless of how many dots were in the
1792 * name or whether it ends with a dot.
1793 */
1794 if (!tried_as_is) {
Mike Yu69615f62018-11-06 15:42:36 +08001795 ret = res_querydomainN(name, NULL, target, res, ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001796 if (ret > 0) return ret;
1797 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001798
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001799 /*
1800 * if we got here, we didn't satisfy the search.
1801 * if we did an initial full query, return that query's h_errno
1802 * (note that we wouldn't be here if that query had succeeded).
1803 * else if we ever got a nodata, send that back as the reason.
1804 * else send back meaningless h_errno, that being the one from
1805 * the last DNSRCH we did.
1806 */
1807 if (saved_herrno != -1)
1808 h_errno = saved_herrno;
1809 else if (got_nodata)
1810 h_errno = NO_DATA;
1811 else if (got_servfail)
1812 h_errno = TRY_AGAIN;
1813 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001814}
1815
1816/*
1817 * Perform a call on res_query on the concatenation of name and domain,
1818 * removing a trailing dot from name if domain is NULL.
1819 */
Mike Yu69615f62018-11-06 15:42:36 +08001820static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
1821 int* ai_error) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001822 char nbuf[MAXDNAME];
1823 const char* longname = nbuf;
1824 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001825
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001826 assert(name != NULL);
1827 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001828
1829#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001830 if (res->options & RES_DEBUG)
1831 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001832#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001833 if (domain == NULL) {
1834 /*
1835 * Check for trailing '.';
1836 * copy without '.' if present.
1837 */
1838 n = strlen(name);
1839 if (n + 1 > sizeof(nbuf)) {
1840 h_errno = NO_RECOVERY;
1841 return -1;
1842 }
1843 if (n > 0 && name[--n] == '.') {
1844 strncpy(nbuf, name, n);
1845 nbuf[n] = '\0';
1846 } else
1847 longname = name;
1848 } else {
1849 n = strlen(name);
1850 d = strlen(domain);
1851 if (n + 1 + d + 1 > sizeof(nbuf)) {
1852 h_errno = NO_RECOVERY;
1853 return -1;
1854 }
1855 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1856 }
Mike Yu69615f62018-11-06 15:42:36 +08001857 return res_queryN(longname, target, res, ai_error);
Bernie Innocenti55864192018-08-30 04:05:20 +09001858}