blob: 97b5b6ffc9716c07e63062373ba290d584635b91 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
2/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked.
36 * - Return values. There are nonstandard return values defined and used
37 * in the source code. This is because RFC2553 is silent about which error
38 * code must be returned for which situation.
39 * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
40 * says to use inet_aton() to convert IPv4 numeric to binary (alows
41 * classful form as a result).
42 * current code - disallow classful form for IPv4 (due to use of inet_pton).
43 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
44 * invalid.
45 * current code - SEGV on freeaddrinfo(NULL)
46 * Note:
47 * - We use getipnodebyname() just for thread-safeness. There's no intent
48 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
49 * getipnodebyname().
50 * - The code filters out AFs that are not supported by the kernel,
51 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
52 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
53 * in ai_flags?
54 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
55 * (1) what should we do against numeric hostname (2) what should we do
56 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
57 * non-loopback address configured? global address configured?
58 * - To avoid search order issue, we have a big amount of code duplicate
59 * from gethnamaddr.c and some other places. The issues that there's no
60 * lower layer function to lookup "IPv4 or IPv6" record. Calling
61 * gethostbyname2 from getaddrinfo will end up in wrong search order, as
62 * follows:
63 * - The code makes use of following calls when asked to resolver with
64 * ai_family = PF_UNSPEC:
65 * getipnodebyname(host, AF_INET6);
66 * getipnodebyname(host, AF_INET);
67 * This will result in the following queries if the node is configure to
68 * prefer /etc/hosts than DNS:
69 * lookup /etc/hosts for IPv6 address
70 * lookup DNS for IPv6 address
71 * lookup /etc/hosts for IPv4 address
72 * lookup DNS for IPv4 address
73 * which may not meet people's requirement.
74 * The right thing to happen is to have underlying layer which does
75 * PF_UNSPEC lookup (lookup both) and return chain of addrinfos.
76 * This would result in a bit of code duplicate with _dns_ghbyname() and
77 * friends.
78 */
79
Bernie Innocenti55864192018-08-30 04:05:20 +090080#include <arpa/inet.h>
81#include <arpa/nameser.h>
82#include <assert.h>
83#include <ctype.h>
84#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#include <fcntl.h>
86#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090087#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090089#include <stdbool.h>
90#include <stddef.h>
91#include <stdio.h>
92#include <stdlib.h>
93#include <string.h>
94#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090095#include <sys/cdefs.h>
96#include <sys/param.h>
97#include <sys/socket.h>
98#include <sys/stat.h>
99#include <sys/types.h>
100#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900101#include <unistd.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900102#include "NetdClientDispatch.h"
103#include "resolv_cache.h"
104#include "resolv_netid.h"
105#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900106
Bernie Innocenti55864192018-08-30 04:05:20 +0900107#include <stdarg.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900108#include <syslog.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900109#include "nsswitch.h"
110#include "private/bionic_defs.h"
111
112typedef union sockaddr_union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113 struct sockaddr generic;
114 struct sockaddr_in in;
Bernie Innocenti55864192018-08-30 04:05:20 +0900115 struct sockaddr_in6 in6;
116} sockaddr_union;
117
118#define SUCCESS 0
119#define ANY 0
120#define YES 1
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121#define NO 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900122
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900123static const char in_addrany[] = {0, 0, 0, 0};
124static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +0900125#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900126static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
127static const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +0900128#endif
129
130#if defined(__ANDROID__)
131// This should be synchronized to ResponseCode.h
132static const int DnsProxyQueryResult = 222;
133#endif
134
135static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900136 int a_af;
137 int a_addrlen;
138 int a_socklen;
139 int a_off;
140 const char* a_addrany;
141 const char* a_loopback;
142 int a_scoped;
143} afdl[] = {
Bernie Innocenti55864192018-08-30 04:05:20 +0900144#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
146 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocenti55864192018-08-30 04:05:20 +0900147#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900148 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
149 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
150 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900151};
152
153struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154 int e_af;
155 int e_socktype;
156 int e_protocol;
157 const char* e_protostr;
158 int e_wild;
159#define WILD_AF(ex) ((ex)->e_wild & 0x01)
160#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
161#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900162};
163
164static const struct explore explore[] = {
165#if 0
166 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
167#endif
168#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900169 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
170 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
171 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900172#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
174 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
175 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
176 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
177 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
178 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
179 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900180};
181
182#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183#define PTON_MAX 16
Bernie Innocenti55864192018-08-30 04:05:20 +0900184#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185#define PTON_MAX 4
Bernie Innocenti55864192018-08-30 04:05:20 +0900186#endif
187
188static const ns_src default_dns_files[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189 {NSSRC_FILES, NS_SUCCESS}, {NSSRC_DNS, NS_SUCCESS}, {0, 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900192
193typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194 HEADER hdr;
195 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900196} querybuf;
197
198struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900199 struct res_target* next;
200 const char* name; /* domain name */
201 int qclass, qtype; /* class and type of query */
202 u_char* answer; /* buffer to put answer */
203 int anslen; /* size of answer buffer */
204 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900205};
206
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207static int str2number(const char*);
208static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
209 const struct android_net_context*);
210static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
211static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
212 const char*);
213static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
214 struct addrinfo**);
215static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
216static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
217static int get_portmatch(const struct addrinfo*, const char*);
218static int get_port(const struct addrinfo*, const char*, int);
219static const struct afd* find_afd(int);
Bernie Innocenti55864192018-08-30 04:05:20 +0900220#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900221static int ip6_str2scopeid(char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900222#endif
223
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900224static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
225static int _dns_getaddrinfo(void*, void*, va_list);
226static void _sethtent(FILE**);
227static void _endhtent(FILE**);
228static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
229static int _files_getaddrinfo(void*, void*, va_list);
230static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232static int res_queryN(const char*, struct res_target*, res_state);
233static int res_searchN(const char*, struct res_target*, res_state);
234static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900235
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900236static const char* const ai_errlist[] = {
237 "Success",
238 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
239 "Temporary failure in name resolution", /* EAI_AGAIN */
240 "Invalid value for ai_flags", /* EAI_BADFLAGS */
241 "Non-recoverable failure in name resolution", /* EAI_FAIL */
242 "ai_family not supported", /* EAI_FAMILY */
243 "Memory allocation failure", /* EAI_MEMORY */
244 "No address associated with hostname", /* EAI_NODATA */
245 "hostname nor servname provided, or not known", /* EAI_NONAME */
246 "servname not supported for ai_socktype", /* EAI_SERVICE */
247 "ai_socktype not supported", /* EAI_SOCKTYPE */
248 "System error returned in errno", /* EAI_SYSTEM */
249 "Invalid value for hints", /* EAI_BADHINTS */
250 "Resolved protocol is unknown", /* EAI_PROTOCOL */
251 "Argument buffer overflow", /* EAI_OVERFLOW */
252 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900253};
254
255/* XXX macros that make external reference is BAD. */
256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900257#define GET_AI(ai, afd, addr) \
258 do { \
259 /* external reference: pai, error, and label free */ \
260 (ai) = get_ai(pai, (afd), (addr)); \
261 if ((ai) == NULL) { \
262 error = EAI_MEMORY; \
263 goto free; \
264 } \
265 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267#define GET_PORT(ai, serv) \
268 do { \
269 /* external reference: error and label free */ \
270 error = get_port((ai), (serv), 0); \
271 if (error != 0) goto free; \
272 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900273
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900274#define GET_CANONNAME(ai, str) \
275 do { \
276 /* external reference: pai, error and label free */ \
277 error = get_canonname(pai, (ai), (str)); \
278 if (error != 0) goto free; \
279 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900280
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900281#define ERR(err) \
282 do { \
283 /* external reference: error, and label bad */ \
284 error = (err); \
285 goto bad; \
286 /*NOTREACHED*/ \
287 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900288
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900289#define MATCH_FAMILY(x, y, w) \
290 ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
291#define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900292
293__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900294const char* gai_strerror(int ecode) {
295 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
296 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900297}
298
299__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900300void freeaddrinfo(struct addrinfo* ai) {
301 struct addrinfo* next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900302
303#if defined(__BIONIC__)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900304 if (ai == NULL) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900305#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900306 _DIAGASSERT(ai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900307#endif
308
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900309 do {
310 next = ai->ai_next;
311 if (ai->ai_canonname) free(ai->ai_canonname);
312 /* no need to free(ai->ai_addr) */
313 free(ai);
314 ai = next;
315 } while (ai);
Bernie Innocenti55864192018-08-30 04:05:20 +0900316}
317
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900318static int str2number(const char* p) {
319 char* ep;
320 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900321
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900322 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900324 if (*p == '\0') return -1;
325 ep = NULL;
326 errno = 0;
327 v = strtoul(p, &ep, 10);
328 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
329 return v;
330 else
331 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900332}
333
334/*
335 * The following functions determine whether IPv4 or IPv6 connectivity is
336 * available in order to implement AI_ADDRCONFIG.
337 *
338 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
339 * available, but whether addresses of the specified family are "configured
340 * on the local system". However, bionic doesn't currently support getifaddrs,
341 * so checking for connectivity is the next best thing.
342 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900343static int _have_ipv6(unsigned mark, uid_t uid) {
344 static const struct sockaddr_in6 sin6_test = {
345 .sin6_family = AF_INET6,
346 .sin6_addr.s6_addr = {// 2000::
347 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
348 sockaddr_union addr = {.in6 = sin6_test};
349 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900350}
351
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900352static int _have_ipv4(unsigned mark, uid_t uid) {
353 static const struct sockaddr_in sin_test = {
354 .sin_family = AF_INET,
355 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
356 };
357 sockaddr_union addr = {.in = sin_test};
358 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900359}
360
361bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900362 int32_t tmp;
363 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
364 return false;
365 }
366 *result = ntohl(tmp);
367 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900368}
369
370#if defined(__ANDROID__)
371// Returns 0 on success, else returns on error.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900372static int android_getaddrinfo_proxy(const char* hostname, const char* servname,
373 const struct addrinfo* hints, struct addrinfo** res,
374 unsigned netid) {
375 int success = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900376
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900377 // Clear this at start, as we use its non-NULLness later (in the
378 // error path) to decide if we have to free up any memory we
379 // allocated in the process (before failing).
380 *res = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900381
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900382 // Bogus things we can't serialize. Don't use the proxy. These will fail - let them.
383 if ((hostname != NULL && strcspn(hostname, " \n\r\t^'\"") != strlen(hostname)) ||
384 (servname != NULL && strcspn(servname, " \n\r\t^'\"") != strlen(servname))) {
385 return EAI_NODATA;
386 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900387
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900388 FILE* proxy = android_open_proxy();
389 if (proxy == NULL) {
390 return EAI_SYSTEM;
391 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900392
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900393 netid = __netdClientDispatch.netIdForResolv(netid);
Bernie Innocenti55864192018-08-30 04:05:20 +0900394
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900395 // Send the request.
396 if (fprintf(proxy, "getaddrinfo %s %s %d %d %d %d %u", hostname == NULL ? "^" : hostname,
397 servname == NULL ? "^" : servname, hints == NULL ? -1 : hints->ai_flags,
398 hints == NULL ? -1 : hints->ai_family, hints == NULL ? -1 : hints->ai_socktype,
399 hints == NULL ? -1 : hints->ai_protocol, netid) < 0) {
400 goto exit;
401 }
402 // literal NULL byte at end, required by FrameworkListener
403 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
404 goto exit;
405 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900406
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900407 char buf[4];
408 // read result code for gethostbyaddr
409 if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) {
410 goto exit;
411 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900412
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900413 int result_code = (int) strtol(buf, NULL, 10);
414 // verify the code itself
415 if (result_code != DnsProxyQueryResult) {
416 fread(buf, 1, sizeof(buf), proxy);
417 goto exit;
418 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900419
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900420 struct addrinfo* ai = NULL;
421 struct addrinfo** nextres = res;
422 while (1) {
423 int32_t have_more;
424 if (!readBE32(proxy, &have_more)) {
425 break;
426 }
427 if (have_more == 0) {
428 success = 1;
429 break;
430 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900431
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 struct addrinfo* ai = calloc(1, sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
433 if (ai == NULL) {
434 break;
435 }
436 ai->ai_addr = (struct sockaddr*) (ai + 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900437
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900438 // struct addrinfo {
439 // int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
440 // int ai_family; /* PF_xxx */
441 // int ai_socktype; /* SOCK_xxx */
442 // int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
443 // socklen_t ai_addrlen; /* length of ai_addr */
444 // char *ai_canonname; /* canonical name for hostname */
445 // struct sockaddr *ai_addr; /* binary address */
446 // struct addrinfo *ai_next; /* next structure in linked list */
447 // };
Bernie Innocenti55864192018-08-30 04:05:20 +0900448
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449 // Read the struct piece by piece because we might be a 32-bit process
450 // talking to a 64-bit netd.
451 int32_t addr_len;
452 bool success = readBE32(proxy, &ai->ai_flags) && readBE32(proxy, &ai->ai_family) &&
453 readBE32(proxy, &ai->ai_socktype) && readBE32(proxy, &ai->ai_protocol) &&
454 readBE32(proxy, &addr_len);
455 if (!success) {
456 break;
457 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900458
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 // Set ai_addrlen and read the ai_addr data.
460 ai->ai_addrlen = addr_len;
461 if (addr_len != 0) {
462 if ((size_t) addr_len > sizeof(struct sockaddr_storage)) {
463 // Bogus; too big.
464 break;
465 }
466 if (fread(ai->ai_addr, addr_len, 1, proxy) != 1) {
467 break;
468 }
469 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900471 // The string for ai_cannonname.
472 int32_t name_len;
473 if (!readBE32(proxy, &name_len)) {
474 break;
475 }
476 if (name_len != 0) {
477 ai->ai_canonname = (char*) malloc(name_len);
478 if (fread(ai->ai_canonname, name_len, 1, proxy) != 1) {
479 break;
480 }
481 if (ai->ai_canonname[name_len - 1] != '\0') {
482 // The proxy should be returning this
483 // NULL-terminated.
484 break;
485 }
486 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900487
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 *nextres = ai;
489 nextres = &ai->ai_next;
490 ai = NULL;
491 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 if (ai != NULL) {
494 // Clean up partially-built addrinfo that we never ended up
495 // attaching to the response.
496 freeaddrinfo(ai);
497 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900498exit:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900499 if (proxy != NULL) {
500 fclose(proxy);
501 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 if (success) {
504 return 0;
505 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900506
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900507 // Proxy failed;
508 // clean up memory we might've allocated.
509 if (*res) {
510 freeaddrinfo(*res);
511 *res = NULL;
512 }
513 return EAI_NODATA;
Bernie Innocenti55864192018-08-30 04:05:20 +0900514}
515#endif
516
517__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900518int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
519 struct addrinfo** res) {
520 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900521}
522
523__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900524int android_getaddrinfofornet(const char* hostname, const char* servname,
525 const struct addrinfo* hints, unsigned netid, unsigned mark,
526 struct addrinfo** res) {
527 struct android_net_context netcontext = {
528 .app_netid = netid,
529 .app_mark = mark,
530 .dns_netid = netid,
531 .dns_mark = mark,
532 .uid = NET_CONTEXT_INVALID_UID,
533 };
534 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900535}
536
537__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
539 const struct addrinfo* hints,
540 const struct android_net_context* netcontext,
541 struct addrinfo** res) {
542 struct addrinfo sentinel;
543 struct addrinfo* cur;
544 int error = 0;
545 struct addrinfo ai;
546 struct addrinfo ai0;
547 struct addrinfo* pai;
548 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900549
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900550 /* hostname is allowed to be NULL */
551 /* servname is allowed to be NULL */
552 /* hints is allowed to be NULL */
553 assert(res != NULL);
554 assert(netcontext != NULL);
555 memset(&sentinel, 0, sizeof(sentinel));
556 cur = &sentinel;
557 pai = &ai;
558 pai->ai_flags = 0;
559 pai->ai_family = PF_UNSPEC;
560 pai->ai_socktype = ANY;
561 pai->ai_protocol = ANY;
562 pai->ai_addrlen = 0;
563 pai->ai_canonname = NULL;
564 pai->ai_addr = NULL;
565 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 if (hostname == NULL && servname == NULL) return EAI_NONAME;
568 if (hints) {
569 /* error check for hints */
570 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
571 ERR(EAI_BADHINTS); /* xxx */
572 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
573 switch (hints->ai_family) {
574 case PF_UNSPEC:
575 case PF_INET:
Bernie Innocenti55864192018-08-30 04:05:20 +0900576#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900577 case PF_INET6:
Bernie Innocenti55864192018-08-30 04:05:20 +0900578#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900579 break;
580 default:
581 ERR(EAI_FAMILY);
582 }
583 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900584
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900585 /*
586 * if both socktype/protocol are specified, check if they
587 * are meaningful combination.
588 */
589 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
590 for (ex = explore; ex->e_af >= 0; ex++) {
591 if (pai->ai_family != ex->e_af) continue;
592 if (ex->e_socktype == ANY) continue;
593 if (ex->e_protocol == ANY) continue;
594 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
595 ERR(EAI_BADHINTS);
596 }
597 }
598 }
599 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 /*
602 * check for special cases. (1) numeric servname is disallowed if
603 * socktype/protocol are left unspecified. (2) servname is disallowed
604 * for raw and other inet{,6} sockets.
605 */
606 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocenti55864192018-08-30 04:05:20 +0900607#ifdef PF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocenti55864192018-08-30 04:05:20 +0900609#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 ) {
611 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900612
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900613 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900614#ifdef PF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 pai->ai_family = PF_INET6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900616#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900617 pai->ai_family = PF_INET;
Bernie Innocenti55864192018-08-30 04:05:20 +0900618#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 }
620 error = get_portmatch(pai, servname);
621 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900622
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 *pai = ai0;
624 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900625
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900626 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628 /* NULL hostname, or numeric hostname */
629 for (ex = explore; ex->e_af >= 0; ex++) {
630 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900631
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900632 /* PF_UNSPEC entries are prepared for DNS queries only */
633 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900634
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900635 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
636 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
637 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
640 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
641 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900642
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900643 if (hostname == NULL)
644 error = explore_null(pai, servname, &cur->ai_next);
645 else
646 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900647
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900649
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900650 while (cur->ai_next) cur = cur->ai_next;
651 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900652
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653 /*
654 * XXX
655 * If numeric representation of AF1 can be interpreted as FQDN
656 * representation of AF2, we need to think again about the code below.
657 */
658 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900659
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900660 if (hostname == NULL) ERR(EAI_NODATA);
661 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900662
663#if defined(__ANDROID__)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664 int gai_error =
665 android_getaddrinfo_proxy(hostname, servname, hints, res, netcontext->app_netid);
666 if (gai_error != EAI_SYSTEM) {
667 return gai_error;
668 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900669#endif
670
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900671 /*
672 * hostname as alphabetical name.
673 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
674 * outer loop by AFs.
675 */
676 for (ex = explore; ex->e_af >= 0; ex++) {
677 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900678
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679 /* require exact match for family field */
680 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900681
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900682 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
683 continue;
684 }
685 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
686 continue;
687 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
690 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 while (cur && cur->ai_next) cur = cur->ai_next;
695 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697 /* XXX */
698 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900699
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 if (error) goto free;
701 if (error == 0) {
702 if (sentinel.ai_next) {
703 good:
704 *res = sentinel.ai_next;
705 return SUCCESS;
706 } else
707 error = EAI_FAIL;
708 }
709free:
710bad:
711 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
712 *res = NULL;
713 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900714}
715
716/*
717 * FQDN hostname, DNS lookup
718 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
720 struct addrinfo** res, const struct android_net_context* netcontext) {
721 struct addrinfo* result;
722 struct addrinfo* cur;
723 int error = 0;
724 static const ns_dtab dtab[] = {NS_FILES_CB(_files_getaddrinfo, NULL){
725 NSSRC_DNS, _dns_getaddrinfo, NULL}, /* force -DHESIOD */
726 NS_NIS_CB(_yp_getaddrinfo, NULL){0, 0, 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900728 assert(pai != NULL);
729 /* hostname may be NULL */
730 /* servname may be NULL */
731 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900733 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 /*
736 * if the servname does not match socktype/protocol, ignore it.
737 */
738 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900739
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900740 switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai,
741 netcontext)) {
742 case NS_TRYAGAIN:
743 error = EAI_AGAIN;
744 goto free;
745 case NS_UNAVAIL:
746 error = EAI_FAIL;
747 goto free;
748 case NS_NOTFOUND:
749 error = EAI_NODATA;
750 goto free;
751 case NS_SUCCESS:
752 error = 0;
753 for (cur = result; cur; cur = cur->ai_next) {
754 GET_PORT(cur, servname);
755 /* canonname should be filled already */
756 }
757 break;
758 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 *res = result;
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900763
764free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765 if (result) freeaddrinfo(result);
766 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767}
768
769/*
770 * hostname == NULL.
771 * passive socket -> anyaddr (0.0.0.0 or ::)
772 * non-passive socket -> localhost (127.0.0.1 or ::1)
773 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900774static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
775 int s;
776 const struct afd* afd;
777 struct addrinfo* cur;
778 struct addrinfo sentinel;
779 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 assert(pai != NULL);
782 /* servname may be NULL */
783 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900784
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 *res = NULL;
786 sentinel.ai_next = NULL;
787 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900788
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900789 /*
790 * filter out AFs that are not supported by the kernel
791 * XXX errno?
792 */
793 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
794 if (s < 0) {
795 if (errno != EMFILE) return 0;
796 } else
797 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900798
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 /*
800 * if the servname does not match socktype/protocol, ignore it.
801 */
802 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900803
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900804 afd = find_afd(pai->ai_family);
805 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900806
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807 if (pai->ai_flags & AI_PASSIVE) {
808 GET_AI(cur->ai_next, afd, afd->a_addrany);
809 /* xxx meaningless?
810 * GET_CANONNAME(cur->ai_next, "anyaddr");
811 */
812 GET_PORT(cur->ai_next, servname);
813 } else {
814 GET_AI(cur->ai_next, afd, afd->a_loopback);
815 /* xxx meaningless?
816 * GET_CANONNAME(cur->ai_next, "localhost");
817 */
818 GET_PORT(cur->ai_next, servname);
819 }
820 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822 *res = sentinel.ai_next;
823 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
825free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900826 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
827 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900828}
829
830/*
831 * numeric hostname
832 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900833static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
834 struct addrinfo** res, const char* canonname) {
835 const struct afd* afd;
836 struct addrinfo* cur;
837 struct addrinfo sentinel;
838 int error;
839 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900840
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 assert(pai != NULL);
842 /* hostname may be NULL */
843 /* servname may be NULL */
844 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900845
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 *res = NULL;
847 sentinel.ai_next = NULL;
848 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900849
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900850 /*
851 * if the servname does not match socktype/protocol, ignore it.
852 */
853 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900854
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900855 afd = find_afd(pai->ai_family);
856 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900857
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900858 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900859#if 0 /*X/Open spec*/
860 case AF_INET:
861 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
862 if (pai->ai_family == afd->a_af ||
863 pai->ai_family == PF_UNSPEC /*?*/) {
864 GET_AI(cur->ai_next, afd, pton);
865 GET_PORT(cur->ai_next, servname);
866 if ((pai->ai_flags & AI_CANONNAME)) {
867 /*
868 * Set the numeric address itself as
869 * the canonical name, based on a
870 * clarification in rfc2553bis-03.
871 */
872 GET_CANONNAME(cur->ai_next, canonname);
873 }
874 while (cur && cur->ai_next)
875 cur = cur->ai_next;
876 } else
877 ERR(EAI_FAMILY); /*xxx*/
878 }
879 break;
880#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881 default:
882 if (inet_pton(afd->a_af, hostname, pton) == 1) {
883 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
884 GET_AI(cur->ai_next, afd, pton);
885 GET_PORT(cur->ai_next, servname);
886 if ((pai->ai_flags & AI_CANONNAME)) {
887 /*
888 * Set the numeric address itself as
889 * the canonical name, based on a
890 * clarification in rfc2553bis-03.
891 */
892 GET_CANONNAME(cur->ai_next, canonname);
893 }
894 while (cur->ai_next) cur = cur->ai_next;
895 } else
896 ERR(EAI_FAMILY); /*xxx*/
897 }
898 break;
899 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900900
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900901 *res = sentinel.ai_next;
902 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900903
904free:
905bad:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
907 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900908}
909
910/*
911 * numeric hostname with scope
912 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
914 const char* servname, struct addrinfo** res) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900915#if !defined(SCOPE_DELIMITER) || !defined(INET6)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900916 return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900917#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918 const struct afd* afd;
919 struct addrinfo* cur;
920 int error;
921 char *cp, *hostname2 = NULL, *scope, *addr;
922 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900923
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900924 assert(pai != NULL);
925 /* hostname may be NULL */
926 /* servname may be NULL */
927 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900928
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900929 /*
930 * if the servname does not match socktype/protocol, ignore it.
931 */
932 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900933
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934 afd = find_afd(pai->ai_family);
935 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900936
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900937 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900938
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900939 cp = strchr(hostname, SCOPE_DELIMITER);
940 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900941
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900942 /*
943 * Handle special case of <scoped_address><delimiter><scope id>
944 */
945 hostname2 = strdup(hostname);
946 if (hostname2 == NULL) return EAI_MEMORY;
947 /* terminate at the delimiter */
948 hostname2[cp - hostname] = '\0';
949 addr = hostname2;
950 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900951
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900952 error = explore_numeric(pai, addr, servname, res, hostname);
953 if (error == 0) {
954 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900955
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900956 for (cur = *res; cur; cur = cur->ai_next) {
957 if (cur->ai_family != AF_INET6) continue;
958 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
959 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
960 free(hostname2);
961 return (EAI_NODATA); /* XXX: is return OK? */
962 }
963 sin6->sin6_scope_id = scopeid;
964 }
965 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900966
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900968
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900969 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900970#endif
971}
972
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900973static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
974 assert(pai != NULL);
975 assert(ai != NULL);
976 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900977
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 if ((pai->ai_flags & AI_CANONNAME) != 0) {
979 ai->ai_canonname = strdup(str);
980 if (ai->ai_canonname == NULL) return EAI_MEMORY;
981 }
982 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900983}
984
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
986 const char* addr) {
987 char* p;
988 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900989
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900990 assert(pai != NULL);
991 assert(afd != NULL);
992 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900993
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900994 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
995 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900996
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900997 memcpy(ai, pai, sizeof(struct addrinfo));
998 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
999 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001000
1001#ifdef HAVE_SA_LEN
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001002 ai->ai_addr->sa_len = afd->a_socklen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001003#endif
1004
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001005 ai->ai_addrlen = afd->a_socklen;
1006#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
1007 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001008#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001009 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1010 p = (char*) (void*) (ai->ai_addr);
1011 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
1012 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +09001013}
1014
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001015static int get_portmatch(const struct addrinfo* ai, const char* servname) {
1016 assert(ai != NULL);
1017 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +09001018
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001019 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001020}
1021
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001022static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
1023 const char* proto;
1024 struct servent* sp;
1025 int port;
1026 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +09001027
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001028 assert(ai != NULL);
1029 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +09001030
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001031 if (servname == NULL) return 0;
1032 switch (ai->ai_family) {
1033 case AF_INET:
Bernie Innocenti55864192018-08-30 04:05:20 +09001034#ifdef AF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001035 case AF_INET6:
Bernie Innocenti55864192018-08-30 04:05:20 +09001036#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001037 break;
1038 default:
1039 return 0;
1040 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001041
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001042 switch (ai->ai_socktype) {
1043 case SOCK_RAW:
1044 return EAI_SERVICE;
1045 case SOCK_DGRAM:
1046 case SOCK_STREAM:
1047 allownumeric = 1;
1048 break;
1049 case ANY:
1050#if 1 /* ANDROID-SPECIFIC CHANGE TO MATCH GLIBC */
1051 allownumeric = 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001052#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001053 allownumeric = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001054#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001055 break;
1056 default:
1057 return EAI_SOCKTYPE;
1058 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001059
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001060 port = str2number(servname);
1061 if (port >= 0) {
1062 if (!allownumeric) return EAI_SERVICE;
1063 if (port < 0 || port > 65535) return EAI_SERVICE;
1064 port = htons(port);
1065 } else {
1066 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +09001067
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001068 switch (ai->ai_socktype) {
1069 case SOCK_DGRAM:
1070 proto = "udp";
1071 break;
1072 case SOCK_STREAM:
1073 proto = "tcp";
1074 break;
1075 default:
1076 proto = NULL;
1077 break;
1078 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001079
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001080 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
1081 port = sp->s_port;
1082 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001083
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001084 if (!matchonly) {
1085 switch (ai->ai_family) {
1086 case AF_INET:
1087 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
1088 break;
Bernie Innocenti55864192018-08-30 04:05:20 +09001089#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001090 case AF_INET6:
1091 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
1092 break;
Bernie Innocenti55864192018-08-30 04:05:20 +09001093#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001094 }
1095 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001096
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001097 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001098}
1099
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001100static const struct afd* find_afd(int af) {
1101 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +09001102
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001103 if (af == PF_UNSPEC) return NULL;
1104 for (afd = afdl; afd->a_af; afd++) {
1105 if (afd->a_af == af) return afd;
1106 }
1107 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001108}
1109
1110#ifdef INET6
1111/* convert a string to a scope identifier. XXX: IPv6 specific */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001112static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
1113 u_long lscopeid;
1114 struct in6_addr* a6;
1115 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +09001116
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001117 assert(scope != NULL);
1118 assert(sin6 != NULL);
1119 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001120
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001121 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +09001122
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001123 /* empty scopeid portion is invalid */
1124 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001125
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001126 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1127 /*
1128 * We currently assume a one-to-one mapping between links
1129 * and interfaces, so we simply use interface indices for
1130 * like-local scopes.
1131 */
1132 *scopeid = if_nametoindex(scope);
1133 if (*scopeid == 0) goto trynumeric;
1134 return 0;
1135 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001136
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001137 /* still unclear about literal, allow numeric only - placeholder */
1138 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
1139 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1140 goto trynumeric;
1141 else
1142 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +09001143
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001144 /* try to convert to a numeric id as a last resort */
1145trynumeric:
1146 errno = 0;
1147 lscopeid = strtoul(scope, &ep, 10);
1148 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1149 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1150 return 0;
1151 else
1152 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001153}
1154#endif
1155
1156/* code duplicate with gethnamaddr.c */
1157
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001158static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +09001159
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001160#define BOUNDED_INCR(x) \
1161 do { \
1162 BOUNDS_CHECK(cp, x); \
1163 cp += (x); \
1164 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +09001165
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001166#define BOUNDS_CHECK(ptr, count) \
1167 do { \
1168 if (eom - (ptr) < (count)) { \
1169 h_errno = NO_RECOVERY; \
1170 return NULL; \
1171 } \
1172 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +09001173
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001174static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
1175 const struct addrinfo* pai) {
1176 struct addrinfo sentinel, *cur;
1177 struct addrinfo ai;
1178 const struct afd* afd;
1179 char* canonname;
1180 const HEADER* hp;
1181 const u_char* cp;
1182 int n;
1183 const u_char* eom;
1184 char *bp, *ep;
1185 int type, class, ancount, qdcount;
1186 int haveanswer, had_error;
1187 char tbuf[MAXDNAME];
1188 int (*name_ok)(const char*);
1189 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001191 assert(answer != NULL);
1192 assert(qname != NULL);
1193 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001194
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001195 memset(&sentinel, 0, sizeof(sentinel));
1196 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001198 canonname = NULL;
1199 eom = answer->buf + anslen;
1200 switch (qtype) {
1201 case T_A:
1202 case T_AAAA:
1203 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
1204 name_ok = res_hnok;
1205 break;
1206 default:
1207 return NULL; /* XXX should be abort(); */
1208 }
1209 /*
1210 * find first satisfactory answer
1211 */
1212 hp = &answer->hdr;
1213 ancount = ntohs(hp->ancount);
1214 qdcount = ntohs(hp->qdcount);
1215 bp = hostbuf;
1216 ep = hostbuf + sizeof hostbuf;
1217 cp = answer->buf;
1218 BOUNDED_INCR(HFIXEDSZ);
1219 if (qdcount != 1) {
1220 h_errno = NO_RECOVERY;
1221 return (NULL);
1222 }
1223 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1224 if ((n < 0) || !(*name_ok)(bp)) {
1225 h_errno = NO_RECOVERY;
1226 return (NULL);
1227 }
1228 BOUNDED_INCR(n + QFIXEDSZ);
1229 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1230 /* res_send() has already verified that the query name is the
1231 * same as the one we sent; this just gets the expanded name
1232 * (i.e., with the succeeding search-domain tacked on).
1233 */
1234 n = strlen(bp) + 1; /* for the \0 */
1235 if (n >= MAXHOSTNAMELEN) {
1236 h_errno = NO_RECOVERY;
1237 return (NULL);
1238 }
1239 canonname = bp;
1240 bp += n;
1241 /* The qname can be abbreviated, but h_name is now absolute. */
1242 qname = canonname;
1243 }
1244 haveanswer = 0;
1245 had_error = 0;
1246 while (ancount-- > 0 && cp < eom && !had_error) {
1247 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1248 if ((n < 0) || !(*name_ok)(bp)) {
1249 had_error++;
1250 continue;
1251 }
1252 cp += n; /* name */
1253 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1254 type = _getshort(cp);
1255 cp += INT16SZ; /* type */
1256 class = _getshort(cp);
1257 cp += INT16SZ + INT32SZ; /* class, TTL */
1258 n = _getshort(cp);
1259 cp += INT16SZ; /* len */
1260 BOUNDS_CHECK(cp, n);
1261 if (class != C_IN) {
1262 /* XXX - debug? syslog? */
1263 cp += n;
1264 continue; /* XXX - had_error++ ? */
1265 }
1266 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1267 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1268 if ((n < 0) || !(*name_ok)(tbuf)) {
1269 had_error++;
1270 continue;
1271 }
1272 cp += n;
1273 /* Get canonical name. */
1274 n = strlen(tbuf) + 1; /* for the \0 */
1275 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1276 had_error++;
1277 continue;
1278 }
1279 strlcpy(bp, tbuf, (size_t)(ep - bp));
1280 canonname = bp;
1281 bp += n;
1282 continue;
1283 }
1284 if (qtype == T_ANY) {
1285 if (!(type == T_A || type == T_AAAA)) {
1286 cp += n;
1287 continue;
1288 }
1289 } else if (type != qtype) {
1290 if (type != T_KEY && type != T_SIG)
1291 syslog(LOG_NOTICE | LOG_AUTH,
1292 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1293 p_class(C_IN), p_type(qtype), p_type(type));
1294 cp += n;
1295 continue; /* XXX - had_error++ ? */
1296 }
1297 switch (type) {
1298 case T_A:
1299 case T_AAAA:
1300 if (strcasecmp(canonname, bp) != 0) {
1301 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1302 cp += n;
1303 continue; /* XXX - had_error++ ? */
1304 }
1305 if (type == T_A && n != INADDRSZ) {
1306 cp += n;
1307 continue;
1308 }
1309 if (type == T_AAAA && n != IN6ADDRSZ) {
1310 cp += n;
1311 continue;
1312 }
1313 if (type == T_AAAA) {
1314 struct in6_addr in6;
1315 memcpy(&in6, cp, IN6ADDRSZ);
1316 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1317 cp += n;
1318 continue;
1319 }
1320 }
1321 if (!haveanswer) {
1322 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001324 canonname = bp;
1325 nn = strlen(bp) + 1; /* for the \0 */
1326 bp += nn;
1327 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001328
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001329 /* don't overwrite pai */
1330 ai = *pai;
1331 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1332 afd = find_afd(ai.ai_family);
1333 if (afd == NULL) {
1334 cp += n;
1335 continue;
1336 }
1337 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1338 if (cur->ai_next == NULL) had_error++;
1339 while (cur && cur->ai_next) cur = cur->ai_next;
1340 cp += n;
1341 break;
1342 default:
1343 abort();
1344 }
1345 if (!had_error) haveanswer++;
1346 }
1347 if (haveanswer) {
1348 if (!canonname)
1349 (void) get_canonname(pai, sentinel.ai_next, qname);
1350 else
1351 (void) get_canonname(pai, sentinel.ai_next, canonname);
1352 h_errno = NETDB_SUCCESS;
1353 return sentinel.ai_next;
1354 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001355
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001356 h_errno = NO_RECOVERY;
1357 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001358}
1359
1360struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001361 struct addrinfo* ai;
1362 int has_src_addr;
1363 sockaddr_union src_addr;
1364 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001365};
1366
1367/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001368static int _get_scope(const struct sockaddr* addr) {
1369 if (addr->sa_family == AF_INET6) {
1370 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1371 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1372 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1373 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1374 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1375 /*
1376 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1377 * link-local scope.
1378 */
1379 return IPV6_ADDR_SCOPE_LINKLOCAL;
1380 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1381 return IPV6_ADDR_SCOPE_SITELOCAL;
1382 } else {
1383 return IPV6_ADDR_SCOPE_GLOBAL;
1384 }
1385 } else if (addr->sa_family == AF_INET) {
1386 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1387 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001388
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001389 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1390 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1391 return IPV6_ADDR_SCOPE_LINKLOCAL;
1392 } else {
1393 /*
1394 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1395 * and shared addresses (100.64.0.0/10), are assigned global scope.
1396 */
1397 return IPV6_ADDR_SCOPE_GLOBAL;
1398 }
1399 } else {
1400 /*
1401 * This should never happen.
1402 * Return a scope with low priority as a last resort.
1403 */
1404 return IPV6_ADDR_SCOPE_NODELOCAL;
1405 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001406}
1407
1408/* These macros are modelled after the ones in <netinet/in6.h>. */
1409
1410/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001411#define IN6_IS_ADDR_TEREDO(a) \
1412 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001413
1414/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001415#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001416
1417/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001418#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001419
1420/*
1421 * Get the label for a given IPv4/IPv6 address.
1422 * RFC 6724, section 2.1.
1423 */
1424
1425/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001426static int _get_label(const struct sockaddr* addr) {
1427 if (addr->sa_family == AF_INET) {
1428 return 4;
1429 } else if (addr->sa_family == AF_INET6) {
1430 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1431 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1432 return 0;
1433 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1434 return 4;
1435 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1436 return 2;
1437 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1438 return 5;
1439 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1440 return 13;
1441 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1442 return 3;
1443 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1444 return 11;
1445 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1446 return 12;
1447 } else {
1448 /* All other IPv6 addresses, including global unicast addresses. */
1449 return 1;
1450 }
1451 } else {
1452 /*
1453 * This should never happen.
1454 * Return a semi-random label as a last resort.
1455 */
1456 return 1;
1457 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001458}
1459
1460/*
1461 * Get the precedence for a given IPv4/IPv6 address.
1462 * RFC 6724, section 2.1.
1463 */
1464
1465/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001466static int _get_precedence(const struct sockaddr* addr) {
1467 if (addr->sa_family == AF_INET) {
1468 return 35;
1469 } else if (addr->sa_family == AF_INET6) {
1470 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1471 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1472 return 50;
1473 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1474 return 35;
1475 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1476 return 30;
1477 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1478 return 5;
1479 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1480 return 3;
1481 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1482 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1483 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1484 return 1;
1485 } else {
1486 /* All other IPv6 addresses, including global unicast addresses. */
1487 return 40;
1488 }
1489 } else {
1490 return 1;
1491 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001492}
1493
1494/*
1495 * Find number of matching initial bits between the two addresses a1 and a2.
1496 */
1497
1498/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001499static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1500 const char* p1 = (const char*) a1;
1501 const char* p2 = (const char*) a2;
1502 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001503
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001504 for (i = 0; i < sizeof(*a1); ++i) {
1505 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001506
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001507 if (p1[i] == p2[i]) {
1508 continue;
1509 }
1510 x = p1[i] ^ p2[i];
1511 for (j = 0; j < CHAR_BIT; ++j) {
1512 if (x & (1 << (CHAR_BIT - 1))) {
1513 return i * CHAR_BIT + j;
1514 }
1515 x <<= 1;
1516 }
1517 }
1518 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001519}
1520
1521/*
1522 * Compare two source/destination address pairs.
1523 * RFC 6724, section 6.
1524 */
1525
1526/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001527static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1528 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1529 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1530 int scope_src1, scope_dst1, scope_match1;
1531 int scope_src2, scope_dst2, scope_match2;
1532 int label_src1, label_dst1, label_match1;
1533 int label_src2, label_dst2, label_match2;
1534 int precedence1, precedence2;
1535 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001536
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001537 /* Rule 1: Avoid unusable destinations. */
1538 if (a1->has_src_addr != a2->has_src_addr) {
1539 return a2->has_src_addr - a1->has_src_addr;
1540 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001541
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001542 /* Rule 2: Prefer matching scope. */
1543 scope_src1 = _get_scope(&a1->src_addr.generic);
1544 scope_dst1 = _get_scope(a1->ai->ai_addr);
1545 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001546
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001547 scope_src2 = _get_scope(&a2->src_addr.generic);
1548 scope_dst2 = _get_scope(a2->ai->ai_addr);
1549 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001550
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001551 if (scope_match1 != scope_match2) {
1552 return scope_match2 - scope_match1;
1553 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001555 /*
1556 * Rule 3: Avoid deprecated addresses.
1557 * TODO(sesse): We don't currently have a good way of finding this.
1558 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001559
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001560 /*
1561 * Rule 4: Prefer home addresses.
1562 * TODO(sesse): We don't currently have a good way of finding this.
1563 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001564
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001565 /* Rule 5: Prefer matching label. */
1566 label_src1 = _get_label(&a1->src_addr.generic);
1567 label_dst1 = _get_label(a1->ai->ai_addr);
1568 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001569
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001570 label_src2 = _get_label(&a2->src_addr.generic);
1571 label_dst2 = _get_label(a2->ai->ai_addr);
1572 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001574 if (label_match1 != label_match2) {
1575 return label_match2 - label_match1;
1576 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001577
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001578 /* Rule 6: Prefer higher precedence. */
1579 precedence1 = _get_precedence(a1->ai->ai_addr);
1580 precedence2 = _get_precedence(a2->ai->ai_addr);
1581 if (precedence1 != precedence2) {
1582 return precedence2 - precedence1;
1583 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001584
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001585 /*
1586 * Rule 7: Prefer native transport.
1587 * TODO(sesse): We don't currently have a good way of finding this.
1588 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001589
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001590 /* Rule 8: Prefer smaller scope. */
1591 if (scope_dst1 != scope_dst2) {
1592 return scope_dst1 - scope_dst2;
1593 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001594
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001595 /*
1596 * Rule 9: Use longest matching prefix.
1597 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1598 * to work very well directly applied to IPv4. (glibc uses information from
1599 * the routing table for a custom IPv4 implementation here.)
1600 */
1601 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1602 a2->ai->ai_addr->sa_family == AF_INET6) {
1603 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1604 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1605 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1606 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1607 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1608 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1609 if (prefixlen1 != prefixlen2) {
1610 return prefixlen2 - prefixlen1;
1611 }
1612 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001613
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001614 /*
1615 * Rule 10: Leave the order unchanged.
1616 * We need this since qsort() is not necessarily stable.
1617 */
1618 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001619}
1620
1621/*
1622 * Find the source address that will be used if trying to connect to the given
1623 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1624 *
1625 * Returns 1 if a source address was found, 0 if the address is unreachable,
1626 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1627 * undefined.
1628 */
1629
1630/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001631static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1632 uid_t uid) {
1633 int sock;
1634 int ret;
1635 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001636
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001637 switch (addr->sa_family) {
1638 case AF_INET:
1639 len = sizeof(struct sockaddr_in);
1640 break;
1641 case AF_INET6:
1642 len = sizeof(struct sockaddr_in6);
1643 break;
1644 default:
1645 /* No known usable source address for non-INET families. */
1646 return 0;
1647 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1650 if (sock == -1) {
1651 if (errno == EAFNOSUPPORT) {
1652 return 0;
1653 } else {
1654 return -1;
1655 }
1656 }
1657 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1658 close(sock);
1659 return 0;
1660 }
1661 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1662 close(sock);
1663 return 0;
1664 }
1665 do {
1666 ret = __connect(sock, addr, len);
1667 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001669 if (ret == -1) {
1670 close(sock);
1671 return 0;
1672 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001673
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001674 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1675 close(sock);
1676 return -1;
1677 }
1678 close(sock);
1679 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001680}
1681
1682/*
1683 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1684 * Will leave the list unchanged if an error occurs.
1685 */
1686
1687/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001688static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1689 struct addrinfo* cur;
1690 int nelem = 0, i;
1691 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001693 cur = list_sentinel->ai_next;
1694 while (cur) {
1695 ++nelem;
1696 cur = cur->ai_next;
1697 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001698
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001699 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1700 if (elems == NULL) {
1701 goto error;
1702 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001704 /*
1705 * Convert the linked list to an array that also contains the candidate
1706 * source address for each destination address.
1707 */
1708 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1709 int has_src_addr;
1710 assert(cur != NULL);
1711 elems[i].ai = cur;
1712 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001714 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1715 if (has_src_addr == -1) {
1716 goto error;
1717 }
1718 elems[i].has_src_addr = has_src_addr;
1719 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001720
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001721 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1722 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001723
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001724 list_sentinel->ai_next = elems[0].ai;
1725 for (i = 0; i < nelem - 1; ++i) {
1726 elems[i].ai->ai_next = elems[i + 1].ai;
1727 }
1728 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001729
1730error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001731 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001732}
1733
1734/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001735static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1736 struct addrinfo* ai;
1737 querybuf *buf, *buf2;
1738 const char* name;
1739 const struct addrinfo* pai;
1740 struct addrinfo sentinel, *cur;
1741 struct res_target q, q2;
1742 res_state res;
1743 const struct android_net_context* netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001745 name = va_arg(ap, char*);
1746 pai = va_arg(ap, const struct addrinfo*);
1747 netcontext = va_arg(ap, const struct android_net_context*);
1748 // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001749
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001750 memset(&q, 0, sizeof(q));
1751 memset(&q2, 0, sizeof(q2));
1752 memset(&sentinel, 0, sizeof(sentinel));
1753 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001754
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001755 buf = malloc(sizeof(*buf));
1756 if (buf == NULL) {
1757 h_errno = NETDB_INTERNAL;
1758 return NS_NOTFOUND;
1759 }
1760 buf2 = malloc(sizeof(*buf2));
1761 if (buf2 == NULL) {
1762 free(buf);
1763 h_errno = NETDB_INTERNAL;
1764 return NS_NOTFOUND;
1765 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001766
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001767 switch (pai->ai_family) {
1768 case AF_UNSPEC:
1769 /* prefer IPv6 */
1770 q.name = name;
1771 q.qclass = C_IN;
1772 q.answer = buf->buf;
1773 q.anslen = sizeof(buf->buf);
1774 int query_ipv6 = 1, query_ipv4 = 1;
1775 if (pai->ai_flags & AI_ADDRCONFIG) {
1776 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1777 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1778 }
1779 if (query_ipv6) {
1780 q.qtype = T_AAAA;
1781 if (query_ipv4) {
1782 q.next = &q2;
1783 q2.name = name;
1784 q2.qclass = C_IN;
1785 q2.qtype = T_A;
1786 q2.answer = buf2->buf;
1787 q2.anslen = sizeof(buf2->buf);
1788 }
1789 } else if (query_ipv4) {
1790 q.qtype = T_A;
1791 } else {
1792 free(buf);
1793 free(buf2);
1794 return NS_NOTFOUND;
1795 }
1796 break;
1797 case AF_INET:
1798 q.name = name;
1799 q.qclass = C_IN;
1800 q.qtype = T_A;
1801 q.answer = buf->buf;
1802 q.anslen = sizeof(buf->buf);
1803 break;
1804 case AF_INET6:
1805 q.name = name;
1806 q.qclass = C_IN;
1807 q.qtype = T_AAAA;
1808 q.answer = buf->buf;
1809 q.anslen = sizeof(buf->buf);
1810 break;
1811 default:
1812 free(buf);
1813 free(buf2);
1814 return NS_UNAVAIL;
1815 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001816
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001817 res = __res_get_state();
1818 if (res == NULL) {
1819 free(buf);
1820 free(buf2);
1821 return NS_NOTFOUND;
1822 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001824 /* this just sets our netid val in the thread private data so we don't have to
1825 * modify the api's all the way down to res_send.c's res_nsend. We could
1826 * fully populate the thread private data here, but if we get down there
1827 * and have a cache hit that would be wasted, so we do the rest there on miss
1828 */
1829 res_setnetcontext(res, netcontext);
1830 if (res_searchN(name, &q, res) < 0) {
1831 __res_put_state(res);
1832 free(buf);
1833 free(buf2);
1834 return NS_NOTFOUND;
1835 }
1836 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1837 if (ai) {
1838 cur->ai_next = ai;
1839 while (cur && cur->ai_next) cur = cur->ai_next;
1840 }
1841 if (q.next) {
1842 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1843 if (ai) cur->ai_next = ai;
1844 }
1845 free(buf);
1846 free(buf2);
1847 if (sentinel.ai_next == NULL) {
1848 __res_put_state(res);
1849 switch (h_errno) {
1850 case HOST_NOT_FOUND:
1851 return NS_NOTFOUND;
1852 case TRY_AGAIN:
1853 return NS_TRYAGAIN;
1854 default:
1855 return NS_UNAVAIL;
1856 }
1857 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001858
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001859 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001860
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001861 __res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001862
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001863 *((struct addrinfo**) rv) = sentinel.ai_next;
1864 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001865}
1866
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001867static void _sethtent(FILE** hostf) {
1868 if (!*hostf)
1869 *hostf = fopen(_PATH_HOSTS, "re");
1870 else
1871 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001872}
1873
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001874static void _endhtent(FILE** hostf) {
1875 if (*hostf) {
1876 (void) fclose(*hostf);
1877 *hostf = NULL;
1878 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001879}
1880
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001881static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1882 char* p;
1883 char *cp, *tname, *cname;
1884 struct addrinfo hints, *res0, *res;
1885 int error;
1886 const char* addr;
1887 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001888
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001889 // fprintf(stderr, "_gethtent() name = '%s'\n", name);
1890 assert(name != NULL);
1891 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001892
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001893 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1894again:
1895 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1896 if (*p == '#') goto again;
1897 if (!(cp = strpbrk(p, "#\n"))) goto again;
1898 *cp = '\0';
1899 if (!(cp = strpbrk(p, " \t"))) goto again;
1900 *cp++ = '\0';
1901 addr = p;
1902 /* if this is not something we're looking for, skip it. */
1903 cname = NULL;
1904 while (cp && *cp) {
1905 if (*cp == ' ' || *cp == '\t') {
1906 cp++;
1907 continue;
1908 }
1909 if (!cname) cname = cp;
1910 tname = cp;
1911 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1912 // fprintf(stderr, "\ttname = '%s'", tname);
1913 if (strcasecmp(name, tname) == 0) goto found;
1914 }
1915 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001916
1917found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001918 hints = *pai;
1919 hints.ai_flags = AI_NUMERICHOST;
1920 error = getaddrinfo(addr, NULL, &hints, &res0);
1921 if (error) goto again;
1922 for (res = res0; res; res = res->ai_next) {
1923 /* cover it up */
1924 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001925
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001926 if (pai->ai_flags & AI_CANONNAME) {
1927 if (get_canonname(pai, res, cname) != 0) {
1928 freeaddrinfo(res0);
1929 goto again;
1930 }
1931 }
1932 }
1933 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001934}
1935
1936/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001937static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1938 const char* name;
1939 const struct addrinfo* pai;
1940 struct addrinfo sentinel, *cur;
1941 struct addrinfo* p;
1942 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001943
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001944 name = va_arg(ap, char*);
1945 pai = va_arg(ap, struct addrinfo*);
Bernie Innocenti55864192018-08-30 04:05:20 +09001946
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001947 // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
1948 memset(&sentinel, 0, sizeof(sentinel));
1949 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001950
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001951 _sethtent(&hostf);
1952 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1953 cur->ai_next = p;
1954 while (cur && cur->ai_next) cur = cur->ai_next;
1955 }
1956 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001957
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001958 *((struct addrinfo**) rv) = sentinel.ai_next;
1959 if (sentinel.ai_next == NULL) return NS_NOTFOUND;
1960 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001961}
1962
1963/* resolver logic */
1964
1965/*
1966 * Formulate a normal query, send, and await answer.
1967 * Returned answer is placed in supplied buffer "answer".
1968 * Perform preliminary check of answer, returning success only
1969 * if no error is indicated and the answer count is nonzero.
1970 * Return the size of the response on success, -1 on error.
1971 * Error number is left in h_errno.
1972 *
1973 * Caller must parse answer and determine whether it answers the question.
1974 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001975static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1976 res_state res) {
1977 u_char buf[MAXPACKET];
1978 HEADER* hp;
1979 int n;
1980 struct res_target* t;
1981 int rcode;
1982 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001983
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001984 assert(name != NULL);
1985 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001986
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001987 rcode = NOERROR;
1988 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001989
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001990 for (t = target; t; t = t->next) {
1991 int class, type;
1992 u_char* answer;
1993 int anslen;
1994 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001995
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001996 hp = (HEADER*) (void*) t->answer;
1997 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001998
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001999 again:
2000 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09002001
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002002 /* make it easier... */
2003 class = t->qclass;
2004 type = t->qtype;
2005 answer = t->answer;
2006 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09002007#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002008 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09002009#endif
2010
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002011 n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocenti55864192018-08-30 04:05:20 +09002012#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002013 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
2014 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
2015 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09002016#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002017 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09002018#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002019 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09002020#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002021 h_errno = NO_RECOVERY;
2022 return n;
2023 }
2024 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09002025#if 0
2026 if (n < 0) {
2027#ifdef DEBUG
2028 if (res->options & RES_DEBUG)
2029 printf(";; res_query: send error\n");
2030#endif
2031 h_errno = TRY_AGAIN;
2032 return n;
2033 }
2034#endif
2035
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002036 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2037 rcode = hp->rcode; /* record most recent error */
Bernie Innocenti55864192018-08-30 04:05:20 +09002038#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002039 /* if the query choked with EDNS0, retry without EDNS0 */
2040 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
2041 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
2042 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09002043#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002044 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09002045#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002046 goto again;
2047 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002048#endif
2049#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002050 if (res->options & RES_DEBUG)
2051 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09002052#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002053 continue;
2054 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002055
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002056 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09002057
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002058 t->n = n;
2059 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002060
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002061 if (ancount == 0) {
2062 switch (rcode) {
2063 case NXDOMAIN:
2064 h_errno = HOST_NOT_FOUND;
2065 break;
2066 case SERVFAIL:
2067 h_errno = TRY_AGAIN;
2068 break;
2069 case NOERROR:
2070 h_errno = NO_DATA;
2071 break;
2072 case FORMERR:
2073 case NOTIMP:
2074 case REFUSED:
2075 default:
2076 h_errno = NO_RECOVERY;
2077 break;
2078 }
2079 return -1;
2080 }
2081 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09002082}
2083
2084/*
2085 * Formulate a normal query, send, and retrieve answer in supplied buffer.
2086 * Return the size of the response on success, -1 on error.
2087 * If enabled, implement search rules until answer or unrecoverable failure
2088 * is detected. Error code, if any, is left in h_errno.
2089 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002090static int res_searchN(const char* name, struct res_target* target, res_state res) {
2091 const char *cp, *const *domain;
2092 HEADER* hp;
2093 u_int dots;
2094 int trailing_dot, ret, saved_herrno;
2095 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09002096
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002097 assert(name != NULL);
2098 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09002099
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002100 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09002101
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002102 errno = 0;
2103 h_errno = HOST_NOT_FOUND; /* default, if we never query */
2104 dots = 0;
2105 for (cp = name; *cp; cp++) dots += (*cp == '.');
2106 trailing_dot = 0;
2107 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09002108
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002109 // fprintf(stderr, "res_searchN() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09002110
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002111 /*
2112 * if there aren't any dots, it could be a user-level alias
2113 */
2114 if (!dots && (cp = __hostalias(name)) != NULL) {
2115 ret = res_queryN(cp, target, res);
2116 return ret;
2117 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002119 /*
2120 * If there are dots in the name already, let's just give it a try
2121 * 'as is'. The threshold can be set with the "ndots" option.
2122 */
2123 saved_herrno = -1;
2124 if (dots >= res->ndots) {
2125 ret = res_querydomainN(name, NULL, target, res);
2126 if (ret > 0) return (ret);
2127 saved_herrno = h_errno;
2128 tried_as_is++;
2129 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002130
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002131 /*
2132 * We do at least one level of search if
2133 * - there is no dot and RES_DEFNAME is set, or
2134 * - there is at least one dot, there is no trailing dot,
2135 * and RES_DNSRCH is set.
2136 */
2137 if ((!dots && (res->options & RES_DEFNAMES)) ||
2138 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2139 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09002140
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002141 /* Unfortunately we need to set stuff up before
2142 * the domain stuff is tried. Will have a better
2143 * fix after thread pools are used.
2144 */
2145 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09002146
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002147 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
2148 ret = res_querydomainN(name, *domain, target, res);
2149 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09002150
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002151 /*
2152 * If no server present, give up.
2153 * If name isn't found in this domain,
2154 * keep trying higher domains in the search list
2155 * (if that's enabled).
2156 * On a NO_DATA error, keep trying, otherwise
2157 * a wildcard entry of another type could keep us
2158 * from finding this entry higher in the domain.
2159 * If we get some other error (negative answer or
2160 * server failure), then stop searching up,
2161 * but try the input name below in case it's
2162 * fully-qualified.
2163 */
2164 if (errno == ECONNREFUSED) {
2165 h_errno = TRY_AGAIN;
2166 return -1;
2167 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002168
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002169 switch (h_errno) {
2170 case NO_DATA:
2171 got_nodata++;
2172 /* FALLTHROUGH */
2173 case HOST_NOT_FOUND:
2174 /* keep trying */
2175 break;
2176 case TRY_AGAIN:
2177 if (hp->rcode == SERVFAIL) {
2178 /* try next search element, if any */
2179 got_servfail++;
2180 break;
2181 }
2182 /* FALLTHROUGH */
2183 default:
2184 /* anything else implies that we're done */
2185 done++;
2186 }
2187 /*
2188 * if we got here for some reason other than DNSRCH,
2189 * we only wanted one iteration of the loop, so stop.
2190 */
2191 if (!(res->options & RES_DNSRCH)) done++;
2192 }
2193 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002194
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002195 /*
2196 * if we have not already tried the name "as is", do that now.
2197 * note that we do this regardless of how many dots were in the
2198 * name or whether it ends with a dot.
2199 */
2200 if (!tried_as_is) {
2201 ret = res_querydomainN(name, NULL, target, res);
2202 if (ret > 0) return ret;
2203 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002204
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002205 /*
2206 * if we got here, we didn't satisfy the search.
2207 * if we did an initial full query, return that query's h_errno
2208 * (note that we wouldn't be here if that query had succeeded).
2209 * else if we ever got a nodata, send that back as the reason.
2210 * else send back meaningless h_errno, that being the one from
2211 * the last DNSRCH we did.
2212 */
2213 if (saved_herrno != -1)
2214 h_errno = saved_herrno;
2215 else if (got_nodata)
2216 h_errno = NO_DATA;
2217 else if (got_servfail)
2218 h_errno = TRY_AGAIN;
2219 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09002220}
2221
2222/*
2223 * Perform a call on res_query on the concatenation of name and domain,
2224 * removing a trailing dot from name if domain is NULL.
2225 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002226static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
2227 res_state res) {
2228 char nbuf[MAXDNAME];
2229 const char* longname = nbuf;
2230 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09002231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002232 assert(name != NULL);
2233 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09002234
2235#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002236 if (res->options & RES_DEBUG)
2237 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09002238#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002239 if (domain == NULL) {
2240 /*
2241 * Check for trailing '.';
2242 * copy without '.' if present.
2243 */
2244 n = strlen(name);
2245 if (n + 1 > sizeof(nbuf)) {
2246 h_errno = NO_RECOVERY;
2247 return -1;
2248 }
2249 if (n > 0 && name[--n] == '.') {
2250 strncpy(nbuf, name, n);
2251 nbuf[n] = '\0';
2252 } else
2253 longname = name;
2254 } else {
2255 n = strlen(name);
2256 d = strlen(domain);
2257 if (n + 1 + d + 1 > sizeof(nbuf)) {
2258 h_errno = NO_RECOVERY;
2259 return -1;
2260 }
2261 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2262 }
2263 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09002264}