blob: 512a7af55b1d7c39cf43db7342a60c7c2d76fa4f [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/param.h>
96#include <sys/socket.h>
97#include <sys/stat.h>
98#include <sys/types.h>
99#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900100#include <unistd.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900101#include "resolv_cache.h"
102#include "resolv_netid.h"
103#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900104
Bernie Innocenti55864192018-08-30 04:05:20 +0900105#include <stdarg.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106#include <syslog.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +0900107
Bernie Innocenti55864192018-08-30 04:05:20 +0900108#include "nsswitch.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900109
110typedef union sockaddr_union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 struct sockaddr generic;
112 struct sockaddr_in in;
Bernie Innocenti55864192018-08-30 04:05:20 +0900113 struct sockaddr_in6 in6;
114} sockaddr_union;
115
116#define SUCCESS 0
117#define ANY 0
118#define YES 1
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119#define NO 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900120
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121static const char in_addrany[] = {0, 0, 0, 0};
122static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +0900123#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
125static 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 +0900126#endif
127
Bernie Innocenti55864192018-08-30 04:05:20 +0900128static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900129 int a_af;
130 int a_addrlen;
131 int a_socklen;
132 int a_off;
133 const char* a_addrany;
134 const char* a_loopback;
135 int a_scoped;
136} afdl[] = {
Bernie Innocenti55864192018-08-30 04:05:20 +0900137#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
139 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocenti55864192018-08-30 04:05:20 +0900140#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
142 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
143 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900144};
145
146struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147 int e_af;
148 int e_socktype;
149 int e_protocol;
150 const char* e_protostr;
151 int e_wild;
152#define WILD_AF(ex) ((ex)->e_wild & 0x01)
153#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
154#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900155};
156
157static const struct explore explore[] = {
158#if 0
159 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
160#endif
161#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
163 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
164 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900165#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
167 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
168 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
169 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
170 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
171 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
172 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900173};
174
175#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900176#define PTON_MAX 16
Bernie Innocenti55864192018-08-30 04:05:20 +0900177#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178#define PTON_MAX 4
Bernie Innocenti55864192018-08-30 04:05:20 +0900179#endif
180
181static const ns_src default_dns_files[] = {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900182 {NSSRC_FILES, NS_SUCCESS},
183 {NSSRC_DNS, NS_SUCCESS},
184 {0, 0}
185};
Bernie Innocenti55864192018-08-30 04:05:20 +0900186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
189typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190 HEADER hdr;
191 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900192} querybuf;
193
194struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195 struct res_target* next;
196 const char* name; /* domain name */
197 int qclass, qtype; /* class and type of query */
198 u_char* answer; /* buffer to put answer */
199 int anslen; /* size of answer buffer */
200 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900201};
202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203static int str2number(const char*);
204static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
205 const struct android_net_context*);
206static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
207static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
208 const char*);
209static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
210 struct addrinfo**);
211static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
212static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
213static int get_portmatch(const struct addrinfo*, const char*);
214static int get_port(const struct addrinfo*, const char*, int);
215static const struct afd* find_afd(int);
Bernie Innocenti55864192018-08-30 04:05:20 +0900216#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217static int ip6_str2scopeid(char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900218#endif
219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
221static int _dns_getaddrinfo(void*, void*, va_list);
222static void _sethtent(FILE**);
223static void _endhtent(FILE**);
224static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
225static int _files_getaddrinfo(void*, void*, va_list);
226static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228static int res_queryN(const char*, struct res_target*, res_state);
229static int res_searchN(const char*, struct res_target*, res_state);
230static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232static const char* const ai_errlist[] = {
233 "Success",
234 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
235 "Temporary failure in name resolution", /* EAI_AGAIN */
236 "Invalid value for ai_flags", /* EAI_BADFLAGS */
237 "Non-recoverable failure in name resolution", /* EAI_FAIL */
238 "ai_family not supported", /* EAI_FAMILY */
239 "Memory allocation failure", /* EAI_MEMORY */
240 "No address associated with hostname", /* EAI_NODATA */
241 "hostname nor servname provided, or not known", /* EAI_NONAME */
242 "servname not supported for ai_socktype", /* EAI_SERVICE */
243 "ai_socktype not supported", /* EAI_SOCKTYPE */
244 "System error returned in errno", /* EAI_SYSTEM */
245 "Invalid value for hints", /* EAI_BADHINTS */
246 "Resolved protocol is unknown", /* EAI_PROTOCOL */
247 "Argument buffer overflow", /* EAI_OVERFLOW */
248 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900249};
250
251/* XXX macros that make external reference is BAD. */
252
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900253#define GET_AI(ai, afd, addr) \
254 do { \
255 /* external reference: pai, error, and label free */ \
256 (ai) = get_ai(pai, (afd), (addr)); \
257 if ((ai) == NULL) { \
258 error = EAI_MEMORY; \
259 goto free; \
260 } \
261 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900262
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900263#define GET_PORT(ai, serv) \
264 do { \
265 /* external reference: error and label free */ \
266 error = get_port((ai), (serv), 0); \
267 if (error != 0) goto free; \
268 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900269
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270#define GET_CANONNAME(ai, str) \
271 do { \
272 /* external reference: pai, error and label free */ \
273 error = get_canonname(pai, (ai), (str)); \
274 if (error != 0) goto free; \
275 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900276
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900277#define ERR(err) \
278 do { \
279 /* external reference: error, and label bad */ \
280 error = (err); \
281 goto bad; \
282 /*NOTREACHED*/ \
283 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900284
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900285#define MATCH_FAMILY(x, y, w) \
286 ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
287#define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900288
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900289const char* gai_strerror(int ecode) {
290 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
291 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900292}
293
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900294void freeaddrinfo(struct addrinfo* ai) {
295 struct addrinfo* next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900296
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900297 if (ai == NULL) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900298
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900299 do {
300 next = ai->ai_next;
301 if (ai->ai_canonname) free(ai->ai_canonname);
302 /* no need to free(ai->ai_addr) */
303 free(ai);
304 ai = next;
305 } while (ai);
Bernie Innocenti55864192018-08-30 04:05:20 +0900306}
307
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900308static int str2number(const char* p) {
309 char* ep;
310 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900311
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900312 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900313
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900314 if (*p == '\0') return -1;
315 ep = NULL;
316 errno = 0;
317 v = strtoul(p, &ep, 10);
318 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
319 return v;
320 else
321 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900322}
323
324/*
325 * The following functions determine whether IPv4 or IPv6 connectivity is
326 * available in order to implement AI_ADDRCONFIG.
327 *
328 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
329 * available, but whether addresses of the specified family are "configured
330 * on the local system". However, bionic doesn't currently support getifaddrs,
331 * so checking for connectivity is the next best thing.
332 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900333static int _have_ipv6(unsigned mark, uid_t uid) {
334 static const struct sockaddr_in6 sin6_test = {
335 .sin6_family = AF_INET6,
336 .sin6_addr.s6_addr = {// 2000::
337 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
338 sockaddr_union addr = {.in6 = sin6_test};
339 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900340}
341
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900342static int _have_ipv4(unsigned mark, uid_t uid) {
343 static const struct sockaddr_in sin_test = {
344 .sin_family = AF_INET,
345 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
346 };
347 sockaddr_union addr = {.in = sin_test};
348 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900349}
350
351bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900352 int32_t tmp;
353 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
354 return false;
355 }
356 *result = ntohl(tmp);
357 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900358}
359
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900360int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
361 struct addrinfo** res) {
362 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900363}
364
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900365int android_getaddrinfofornet(const char* hostname, const char* servname,
366 const struct addrinfo* hints, unsigned netid, unsigned mark,
367 struct addrinfo** res) {
368 struct android_net_context netcontext = {
369 .app_netid = netid,
370 .app_mark = mark,
371 .dns_netid = netid,
372 .dns_mark = mark,
373 .uid = NET_CONTEXT_INVALID_UID,
374 };
375 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900376}
377
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900378int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
379 const struct addrinfo* hints,
380 const struct android_net_context* netcontext,
381 struct addrinfo** res) {
382 struct addrinfo sentinel;
383 struct addrinfo* cur;
384 int error = 0;
385 struct addrinfo ai;
386 struct addrinfo ai0;
387 struct addrinfo* pai;
388 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900389
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 /* hostname is allowed to be NULL */
391 /* servname is allowed to be NULL */
392 /* hints is allowed to be NULL */
393 assert(res != NULL);
394 assert(netcontext != NULL);
395 memset(&sentinel, 0, sizeof(sentinel));
396 cur = &sentinel;
397 pai = &ai;
398 pai->ai_flags = 0;
399 pai->ai_family = PF_UNSPEC;
400 pai->ai_socktype = ANY;
401 pai->ai_protocol = ANY;
402 pai->ai_addrlen = 0;
403 pai->ai_canonname = NULL;
404 pai->ai_addr = NULL;
405 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900406
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900407 if (hostname == NULL && servname == NULL) return EAI_NONAME;
408 if (hints) {
409 /* error check for hints */
410 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
411 ERR(EAI_BADHINTS); /* xxx */
412 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
413 switch (hints->ai_family) {
414 case PF_UNSPEC:
415 case PF_INET:
Bernie Innocenti55864192018-08-30 04:05:20 +0900416#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900417 case PF_INET6:
Bernie Innocenti55864192018-08-30 04:05:20 +0900418#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 break;
420 default:
421 ERR(EAI_FAMILY);
422 }
423 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900424
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 /*
426 * if both socktype/protocol are specified, check if they
427 * are meaningful combination.
428 */
429 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
430 for (ex = explore; ex->e_af >= 0; ex++) {
431 if (pai->ai_family != ex->e_af) continue;
432 if (ex->e_socktype == ANY) continue;
433 if (ex->e_protocol == ANY) continue;
434 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
435 ERR(EAI_BADHINTS);
436 }
437 }
438 }
439 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900440
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 /*
442 * check for special cases. (1) numeric servname is disallowed if
443 * socktype/protocol are left unspecified. (2) servname is disallowed
444 * for raw and other inet{,6} sockets.
445 */
446 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocenti55864192018-08-30 04:05:20 +0900447#ifdef PF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900448 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocenti55864192018-08-30 04:05:20 +0900449#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 ) {
451 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900452
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900454#ifdef PF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900455 pai->ai_family = PF_INET6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900457 pai->ai_family = PF_INET;
Bernie Innocenti55864192018-08-30 04:05:20 +0900458#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 }
460 error = get_portmatch(pai, servname);
461 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900462
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900463 *pai = ai0;
464 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900465
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900467
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900468 /* NULL hostname, or numeric hostname */
469 for (ex = explore; ex->e_af >= 0; ex++) {
470 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900471
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 /* PF_UNSPEC entries are prepared for DNS queries only */
473 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
476 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
477 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900478
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
480 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
481 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 if (hostname == NULL)
484 error = explore_null(pai, servname, &cur->ai_next);
485 else
486 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900487
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 while (cur->ai_next) cur = cur->ai_next;
491 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 /*
494 * XXX
495 * If numeric representation of AF1 can be interpreted as FQDN
496 * representation of AF2, we need to think again about the code below.
497 */
498 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900499
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 if (hostname == NULL) ERR(EAI_NODATA);
501 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 /*
504 * hostname as alphabetical name.
505 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
506 * outer loop by AFs.
507 */
508 for (ex = explore; ex->e_af >= 0; ex++) {
509 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 /* require exact match for family field */
512 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900513
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
515 continue;
516 }
517 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
518 continue;
519 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900520
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
522 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900523
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900524 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900525
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900526 while (cur && cur->ai_next) cur = cur->ai_next;
527 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900528
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900529 /* XXX */
530 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900531
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900532 if (error) goto free;
533 if (error == 0) {
534 if (sentinel.ai_next) {
535 good:
536 *res = sentinel.ai_next;
537 return SUCCESS;
538 } else
539 error = EAI_FAIL;
540 }
541free:
542bad:
543 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
544 *res = NULL;
545 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900546}
547
548/*
549 * FQDN hostname, DNS lookup
550 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
552 struct addrinfo** res, const struct android_net_context* netcontext) {
553 struct addrinfo* result;
554 struct addrinfo* cur;
555 int error = 0;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900556 static const ns_dtab dtab[] = {
557 {NSSRC_FILES, _files_getaddrinfo, NULL},
558 {NSSRC_DNS, _dns_getaddrinfo, NULL},
559 {0, 0, 0}
560 };
Bernie Innocenti55864192018-08-30 04:05:20 +0900561
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900562 assert(pai != NULL);
563 /* hostname may be NULL */
564 /* servname may be NULL */
565 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900568
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900569 /*
570 * if the servname does not match socktype/protocol, ignore it.
571 */
572 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900574 switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai,
575 netcontext)) {
576 case NS_TRYAGAIN:
577 error = EAI_AGAIN;
578 goto free;
579 case NS_UNAVAIL:
580 error = EAI_FAIL;
581 goto free;
582 case NS_NOTFOUND:
583 error = EAI_NODATA;
584 goto free;
585 case NS_SUCCESS:
586 error = 0;
587 for (cur = result; cur; cur = cur->ai_next) {
588 GET_PORT(cur, servname);
589 /* canonname should be filled already */
590 }
591 break;
592 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900593
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900594 *res = result;
Bernie Innocenti55864192018-08-30 04:05:20 +0900595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900597
598free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900599 if (result) freeaddrinfo(result);
600 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900601}
602
603/*
604 * hostname == NULL.
605 * passive socket -> anyaddr (0.0.0.0 or ::)
606 * non-passive socket -> localhost (127.0.0.1 or ::1)
607 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
609 int s;
610 const struct afd* afd;
611 struct addrinfo* cur;
612 struct addrinfo sentinel;
613 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 assert(pai != NULL);
616 /* servname may be NULL */
617 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900618
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 *res = NULL;
620 sentinel.ai_next = NULL;
621 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900622
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 /*
624 * filter out AFs that are not supported by the kernel
625 * XXX errno?
626 */
627 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
628 if (s < 0) {
629 if (errno != EMFILE) return 0;
630 } else
631 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900632
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633 /*
634 * if the servname does not match socktype/protocol, ignore it.
635 */
636 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900637
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900638 afd = find_afd(pai->ai_family);
639 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 if (pai->ai_flags & AI_PASSIVE) {
642 GET_AI(cur->ai_next, afd, afd->a_addrany);
643 /* xxx meaningless?
644 * GET_CANONNAME(cur->ai_next, "anyaddr");
645 */
646 GET_PORT(cur->ai_next, servname);
647 } else {
648 GET_AI(cur->ai_next, afd, afd->a_loopback);
649 /* xxx meaningless?
650 * GET_CANONNAME(cur->ai_next, "localhost");
651 */
652 GET_PORT(cur->ai_next, servname);
653 }
654 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 *res = sentinel.ai_next;
657 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900658
659free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900660 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
661 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900662}
663
664/*
665 * numeric hostname
666 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
668 struct addrinfo** res, const char* canonname) {
669 const struct afd* afd;
670 struct addrinfo* cur;
671 struct addrinfo sentinel;
672 int error;
673 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900674
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900675 assert(pai != NULL);
676 /* hostname may be NULL */
677 /* servname may be NULL */
678 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680 *res = NULL;
681 sentinel.ai_next = NULL;
682 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900683
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900684 /*
685 * if the servname does not match socktype/protocol, ignore it.
686 */
687 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 afd = find_afd(pai->ai_family);
690 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900693#if 0 /*X/Open spec*/
694 case AF_INET:
695 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
696 if (pai->ai_family == afd->a_af ||
697 pai->ai_family == PF_UNSPEC /*?*/) {
698 GET_AI(cur->ai_next, afd, pton);
699 GET_PORT(cur->ai_next, servname);
700 if ((pai->ai_flags & AI_CANONNAME)) {
701 /*
702 * Set the numeric address itself as
703 * the canonical name, based on a
704 * clarification in rfc2553bis-03.
705 */
706 GET_CANONNAME(cur->ai_next, canonname);
707 }
708 while (cur && cur->ai_next)
709 cur = cur->ai_next;
710 } else
711 ERR(EAI_FAMILY); /*xxx*/
712 }
713 break;
714#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715 default:
716 if (inet_pton(afd->a_af, hostname, pton) == 1) {
717 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
718 GET_AI(cur->ai_next, afd, pton);
719 GET_PORT(cur->ai_next, servname);
720 if ((pai->ai_flags & AI_CANONNAME)) {
721 /*
722 * Set the numeric address itself as
723 * the canonical name, based on a
724 * clarification in rfc2553bis-03.
725 */
726 GET_CANONNAME(cur->ai_next, canonname);
727 }
728 while (cur->ai_next) cur = cur->ai_next;
729 } else
730 ERR(EAI_FAMILY); /*xxx*/
731 }
732 break;
733 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 *res = sentinel.ai_next;
736 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
738free:
739bad:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900740 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
741 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900742}
743
744/*
745 * numeric hostname with scope
746 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
748 const char* servname, struct addrinfo** res) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900749#if !defined(SCOPE_DELIMITER) || !defined(INET6)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900751#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 const struct afd* afd;
753 struct addrinfo* cur;
754 int error;
755 char *cp, *hostname2 = NULL, *scope, *addr;
756 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 assert(pai != NULL);
759 /* hostname may be NULL */
760 /* servname may be NULL */
761 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900762
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900763 /*
764 * if the servname does not match socktype/protocol, ignore it.
765 */
766 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 afd = find_afd(pai->ai_family);
769 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 cp = strchr(hostname, SCOPE_DELIMITER);
774 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 /*
777 * Handle special case of <scoped_address><delimiter><scope id>
778 */
779 hostname2 = strdup(hostname);
780 if (hostname2 == NULL) return EAI_MEMORY;
781 /* terminate at the delimiter */
782 hostname2[cp - hostname] = '\0';
783 addr = hostname2;
784 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900785
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900786 error = explore_numeric(pai, addr, servname, res, hostname);
787 if (error == 0) {
788 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 for (cur = *res; cur; cur = cur->ai_next) {
791 if (cur->ai_family != AF_INET6) continue;
792 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
793 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
794 free(hostname2);
795 return (EAI_NODATA); /* XXX: is return OK? */
796 }
797 sin6->sin6_scope_id = scopeid;
798 }
799 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900802
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900803 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900804#endif
805}
806
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
808 assert(pai != NULL);
809 assert(ai != NULL);
810 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 if ((pai->ai_flags & AI_CANONNAME) != 0) {
813 ai->ai_canonname = strdup(str);
814 if (ai->ai_canonname == NULL) return EAI_MEMORY;
815 }
816 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817}
818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
820 const char* addr) {
821 char* p;
822 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 assert(pai != NULL);
825 assert(afd != NULL);
826 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
829 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 memcpy(ai, pai, sizeof(struct addrinfo));
832 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
833 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
835#ifdef HAVE_SA_LEN
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 ai->ai_addr->sa_len = afd->a_socklen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900837#endif
838
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 ai->ai_addrlen = afd->a_socklen;
840#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
841 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900842#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900843 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
844 p = (char*) (void*) (ai->ai_addr);
845 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
846 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900847}
848
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849static int get_portmatch(const struct addrinfo* ai, const char* servname) {
850 assert(ai != NULL);
851 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900854}
855
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900856static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
857 const char* proto;
858 struct servent* sp;
859 int port;
860 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900861
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900862 assert(ai != NULL);
863 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900864
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900865 if (servname == NULL) return 0;
866 switch (ai->ai_family) {
867 case AF_INET:
Bernie Innocenti55864192018-08-30 04:05:20 +0900868#ifdef AF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900869 case AF_INET6:
Bernie Innocenti55864192018-08-30 04:05:20 +0900870#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900871 break;
872 default:
873 return 0;
874 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900875
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 switch (ai->ai_socktype) {
877 case SOCK_RAW:
878 return EAI_SERVICE;
879 case SOCK_DGRAM:
880 case SOCK_STREAM:
881 allownumeric = 1;
882 break;
883 case ANY:
884#if 1 /* ANDROID-SPECIFIC CHANGE TO MATCH GLIBC */
885 allownumeric = 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900886#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900887 allownumeric = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900888#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900889 break;
890 default:
891 return EAI_SOCKTYPE;
892 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900893
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900894 port = str2number(servname);
895 if (port >= 0) {
896 if (!allownumeric) return EAI_SERVICE;
897 if (port < 0 || port > 65535) return EAI_SERVICE;
898 port = htons(port);
899 } else {
900 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900901
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900902 switch (ai->ai_socktype) {
903 case SOCK_DGRAM:
904 proto = "udp";
905 break;
906 case SOCK_STREAM:
907 proto = "tcp";
908 break;
909 default:
910 proto = NULL;
911 break;
912 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900913
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
915 port = sp->s_port;
916 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900917
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918 if (!matchonly) {
919 switch (ai->ai_family) {
920 case AF_INET:
921 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
922 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900923#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900924 case AF_INET6:
925 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
926 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900927#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900928 }
929 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900930
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900931 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900932}
933
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934static const struct afd* find_afd(int af) {
935 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900936
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900937 if (af == PF_UNSPEC) return NULL;
938 for (afd = afdl; afd->a_af; afd++) {
939 if (afd->a_af == af) return afd;
940 }
941 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900942}
943
944#ifdef INET6
945/* convert a string to a scope identifier. XXX: IPv6 specific */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900946static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
947 u_long lscopeid;
948 struct in6_addr* a6;
949 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900950
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 assert(scope != NULL);
952 assert(sin6 != NULL);
953 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900954
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900955 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900956
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900957 /* empty scopeid portion is invalid */
958 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900959
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900960 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
961 /*
962 * We currently assume a one-to-one mapping between links
963 * and interfaces, so we simply use interface indices for
964 * like-local scopes.
965 */
966 *scopeid = if_nametoindex(scope);
967 if (*scopeid == 0) goto trynumeric;
968 return 0;
969 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900970
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900971 /* still unclear about literal, allow numeric only - placeholder */
972 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
973 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
974 goto trynumeric;
975 else
976 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900977
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 /* try to convert to a numeric id as a last resort */
979trynumeric:
980 errno = 0;
981 lscopeid = strtoul(scope, &ep, 10);
982 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
983 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
984 return 0;
985 else
986 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900987}
988#endif
989
990/* code duplicate with gethnamaddr.c */
991
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900992static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900993
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900994#define BOUNDED_INCR(x) \
995 do { \
996 BOUNDS_CHECK(cp, x); \
997 cp += (x); \
998 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900999
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001000#define BOUNDS_CHECK(ptr, count) \
1001 do { \
1002 if (eom - (ptr) < (count)) { \
1003 h_errno = NO_RECOVERY; \
1004 return NULL; \
1005 } \
1006 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +09001007
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001008static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
1009 const struct addrinfo* pai) {
1010 struct addrinfo sentinel, *cur;
1011 struct addrinfo ai;
1012 const struct afd* afd;
1013 char* canonname;
1014 const HEADER* hp;
1015 const u_char* cp;
1016 int n;
1017 const u_char* eom;
1018 char *bp, *ep;
1019 int type, class, ancount, qdcount;
1020 int haveanswer, had_error;
1021 char tbuf[MAXDNAME];
1022 int (*name_ok)(const char*);
1023 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001024
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001025 assert(answer != NULL);
1026 assert(qname != NULL);
1027 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001028
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001029 memset(&sentinel, 0, sizeof(sentinel));
1030 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001031
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001032 canonname = NULL;
1033 eom = answer->buf + anslen;
1034 switch (qtype) {
1035 case T_A:
1036 case T_AAAA:
1037 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
1038 name_ok = res_hnok;
1039 break;
1040 default:
1041 return NULL; /* XXX should be abort(); */
1042 }
1043 /*
1044 * find first satisfactory answer
1045 */
1046 hp = &answer->hdr;
1047 ancount = ntohs(hp->ancount);
1048 qdcount = ntohs(hp->qdcount);
1049 bp = hostbuf;
1050 ep = hostbuf + sizeof hostbuf;
1051 cp = answer->buf;
1052 BOUNDED_INCR(HFIXEDSZ);
1053 if (qdcount != 1) {
1054 h_errno = NO_RECOVERY;
1055 return (NULL);
1056 }
1057 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1058 if ((n < 0) || !(*name_ok)(bp)) {
1059 h_errno = NO_RECOVERY;
1060 return (NULL);
1061 }
1062 BOUNDED_INCR(n + QFIXEDSZ);
1063 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1064 /* res_send() has already verified that the query name is the
1065 * same as the one we sent; this just gets the expanded name
1066 * (i.e., with the succeeding search-domain tacked on).
1067 */
1068 n = strlen(bp) + 1; /* for the \0 */
1069 if (n >= MAXHOSTNAMELEN) {
1070 h_errno = NO_RECOVERY;
1071 return (NULL);
1072 }
1073 canonname = bp;
1074 bp += n;
1075 /* The qname can be abbreviated, but h_name is now absolute. */
1076 qname = canonname;
1077 }
1078 haveanswer = 0;
1079 had_error = 0;
1080 while (ancount-- > 0 && cp < eom && !had_error) {
1081 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1082 if ((n < 0) || !(*name_ok)(bp)) {
1083 had_error++;
1084 continue;
1085 }
1086 cp += n; /* name */
1087 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1088 type = _getshort(cp);
1089 cp += INT16SZ; /* type */
1090 class = _getshort(cp);
1091 cp += INT16SZ + INT32SZ; /* class, TTL */
1092 n = _getshort(cp);
1093 cp += INT16SZ; /* len */
1094 BOUNDS_CHECK(cp, n);
1095 if (class != C_IN) {
1096 /* XXX - debug? syslog? */
1097 cp += n;
1098 continue; /* XXX - had_error++ ? */
1099 }
1100 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1101 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1102 if ((n < 0) || !(*name_ok)(tbuf)) {
1103 had_error++;
1104 continue;
1105 }
1106 cp += n;
1107 /* Get canonical name. */
1108 n = strlen(tbuf) + 1; /* for the \0 */
1109 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1110 had_error++;
1111 continue;
1112 }
1113 strlcpy(bp, tbuf, (size_t)(ep - bp));
1114 canonname = bp;
1115 bp += n;
1116 continue;
1117 }
1118 if (qtype == T_ANY) {
1119 if (!(type == T_A || type == T_AAAA)) {
1120 cp += n;
1121 continue;
1122 }
1123 } else if (type != qtype) {
1124 if (type != T_KEY && type != T_SIG)
1125 syslog(LOG_NOTICE | LOG_AUTH,
1126 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1127 p_class(C_IN), p_type(qtype), p_type(type));
1128 cp += n;
1129 continue; /* XXX - had_error++ ? */
1130 }
1131 switch (type) {
1132 case T_A:
1133 case T_AAAA:
1134 if (strcasecmp(canonname, bp) != 0) {
1135 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1136 cp += n;
1137 continue; /* XXX - had_error++ ? */
1138 }
1139 if (type == T_A && n != INADDRSZ) {
1140 cp += n;
1141 continue;
1142 }
1143 if (type == T_AAAA && n != IN6ADDRSZ) {
1144 cp += n;
1145 continue;
1146 }
1147 if (type == T_AAAA) {
1148 struct in6_addr in6;
1149 memcpy(&in6, cp, IN6ADDRSZ);
1150 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1151 cp += n;
1152 continue;
1153 }
1154 }
1155 if (!haveanswer) {
1156 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001157
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001158 canonname = bp;
1159 nn = strlen(bp) + 1; /* for the \0 */
1160 bp += nn;
1161 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001162
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001163 /* don't overwrite pai */
1164 ai = *pai;
1165 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1166 afd = find_afd(ai.ai_family);
1167 if (afd == NULL) {
1168 cp += n;
1169 continue;
1170 }
1171 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1172 if (cur->ai_next == NULL) had_error++;
1173 while (cur && cur->ai_next) cur = cur->ai_next;
1174 cp += n;
1175 break;
1176 default:
1177 abort();
1178 }
1179 if (!had_error) haveanswer++;
1180 }
1181 if (haveanswer) {
1182 if (!canonname)
1183 (void) get_canonname(pai, sentinel.ai_next, qname);
1184 else
1185 (void) get_canonname(pai, sentinel.ai_next, canonname);
1186 h_errno = NETDB_SUCCESS;
1187 return sentinel.ai_next;
1188 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001189
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001190 h_errno = NO_RECOVERY;
1191 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001192}
1193
1194struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001195 struct addrinfo* ai;
1196 int has_src_addr;
1197 sockaddr_union src_addr;
1198 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001199};
1200
1201/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001202static int _get_scope(const struct sockaddr* addr) {
1203 if (addr->sa_family == AF_INET6) {
1204 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1205 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1206 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1207 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1208 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1209 /*
1210 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1211 * link-local scope.
1212 */
1213 return IPV6_ADDR_SCOPE_LINKLOCAL;
1214 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1215 return IPV6_ADDR_SCOPE_SITELOCAL;
1216 } else {
1217 return IPV6_ADDR_SCOPE_GLOBAL;
1218 }
1219 } else if (addr->sa_family == AF_INET) {
1220 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1221 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001223 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1224 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1225 return IPV6_ADDR_SCOPE_LINKLOCAL;
1226 } else {
1227 /*
1228 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1229 * and shared addresses (100.64.0.0/10), are assigned global scope.
1230 */
1231 return IPV6_ADDR_SCOPE_GLOBAL;
1232 }
1233 } else {
1234 /*
1235 * This should never happen.
1236 * Return a scope with low priority as a last resort.
1237 */
1238 return IPV6_ADDR_SCOPE_NODELOCAL;
1239 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001240}
1241
1242/* These macros are modelled after the ones in <netinet/in6.h>. */
1243
1244/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001245#define IN6_IS_ADDR_TEREDO(a) \
1246 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001247
1248/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001249#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001250
1251/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001252#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001253
1254/*
1255 * Get the label for a given IPv4/IPv6 address.
1256 * RFC 6724, section 2.1.
1257 */
1258
1259/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001260static int _get_label(const struct sockaddr* addr) {
1261 if (addr->sa_family == AF_INET) {
1262 return 4;
1263 } else if (addr->sa_family == AF_INET6) {
1264 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1265 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1266 return 0;
1267 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1268 return 4;
1269 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1270 return 2;
1271 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1272 return 5;
1273 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1274 return 13;
1275 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1276 return 3;
1277 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1278 return 11;
1279 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1280 return 12;
1281 } else {
1282 /* All other IPv6 addresses, including global unicast addresses. */
1283 return 1;
1284 }
1285 } else {
1286 /*
1287 * This should never happen.
1288 * Return a semi-random label as a last resort.
1289 */
1290 return 1;
1291 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001292}
1293
1294/*
1295 * Get the precedence for a given IPv4/IPv6 address.
1296 * RFC 6724, section 2.1.
1297 */
1298
1299/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001300static int _get_precedence(const struct sockaddr* addr) {
1301 if (addr->sa_family == AF_INET) {
1302 return 35;
1303 } else if (addr->sa_family == AF_INET6) {
1304 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1305 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1306 return 50;
1307 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1308 return 35;
1309 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1310 return 30;
1311 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1312 return 5;
1313 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1314 return 3;
1315 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1316 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1317 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1318 return 1;
1319 } else {
1320 /* All other IPv6 addresses, including global unicast addresses. */
1321 return 40;
1322 }
1323 } else {
1324 return 1;
1325 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001326}
1327
1328/*
1329 * Find number of matching initial bits between the two addresses a1 and a2.
1330 */
1331
1332/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001333static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1334 const char* p1 = (const char*) a1;
1335 const char* p2 = (const char*) a2;
1336 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001337
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001338 for (i = 0; i < sizeof(*a1); ++i) {
1339 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001340
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001341 if (p1[i] == p2[i]) {
1342 continue;
1343 }
1344 x = p1[i] ^ p2[i];
1345 for (j = 0; j < CHAR_BIT; ++j) {
1346 if (x & (1 << (CHAR_BIT - 1))) {
1347 return i * CHAR_BIT + j;
1348 }
1349 x <<= 1;
1350 }
1351 }
1352 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001353}
1354
1355/*
1356 * Compare two source/destination address pairs.
1357 * RFC 6724, section 6.
1358 */
1359
1360/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001361static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1362 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1363 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1364 int scope_src1, scope_dst1, scope_match1;
1365 int scope_src2, scope_dst2, scope_match2;
1366 int label_src1, label_dst1, label_match1;
1367 int label_src2, label_dst2, label_match2;
1368 int precedence1, precedence2;
1369 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001370
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001371 /* Rule 1: Avoid unusable destinations. */
1372 if (a1->has_src_addr != a2->has_src_addr) {
1373 return a2->has_src_addr - a1->has_src_addr;
1374 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001375
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001376 /* Rule 2: Prefer matching scope. */
1377 scope_src1 = _get_scope(&a1->src_addr.generic);
1378 scope_dst1 = _get_scope(a1->ai->ai_addr);
1379 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001380
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001381 scope_src2 = _get_scope(&a2->src_addr.generic);
1382 scope_dst2 = _get_scope(a2->ai->ai_addr);
1383 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001384
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 if (scope_match1 != scope_match2) {
1386 return scope_match2 - scope_match1;
1387 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001388
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001389 /*
1390 * Rule 3: Avoid deprecated addresses.
1391 * TODO(sesse): We don't currently have a good way of finding this.
1392 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001393
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001394 /*
1395 * Rule 4: Prefer home addresses.
1396 * TODO(sesse): We don't currently have a good way of finding this.
1397 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 /* Rule 5: Prefer matching label. */
1400 label_src1 = _get_label(&a1->src_addr.generic);
1401 label_dst1 = _get_label(a1->ai->ai_addr);
1402 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001403
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001404 label_src2 = _get_label(&a2->src_addr.generic);
1405 label_dst2 = _get_label(a2->ai->ai_addr);
1406 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001407
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001408 if (label_match1 != label_match2) {
1409 return label_match2 - label_match1;
1410 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001411
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001412 /* Rule 6: Prefer higher precedence. */
1413 precedence1 = _get_precedence(a1->ai->ai_addr);
1414 precedence2 = _get_precedence(a2->ai->ai_addr);
1415 if (precedence1 != precedence2) {
1416 return precedence2 - precedence1;
1417 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001419 /*
1420 * Rule 7: Prefer native transport.
1421 * TODO(sesse): We don't currently have a good way of finding this.
1422 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001423
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001424 /* Rule 8: Prefer smaller scope. */
1425 if (scope_dst1 != scope_dst2) {
1426 return scope_dst1 - scope_dst2;
1427 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001428
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001429 /*
1430 * Rule 9: Use longest matching prefix.
1431 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1432 * to work very well directly applied to IPv4. (glibc uses information from
1433 * the routing table for a custom IPv4 implementation here.)
1434 */
1435 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1436 a2->ai->ai_addr->sa_family == AF_INET6) {
1437 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1438 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1439 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1440 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1441 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1442 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1443 if (prefixlen1 != prefixlen2) {
1444 return prefixlen2 - prefixlen1;
1445 }
1446 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001447
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001448 /*
1449 * Rule 10: Leave the order unchanged.
1450 * We need this since qsort() is not necessarily stable.
1451 */
1452 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001453}
1454
1455/*
1456 * Find the source address that will be used if trying to connect to the given
1457 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1458 *
1459 * Returns 1 if a source address was found, 0 if the address is unreachable,
1460 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1461 * undefined.
1462 */
1463
1464/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001465static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1466 uid_t uid) {
1467 int sock;
1468 int ret;
1469 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001471 switch (addr->sa_family) {
1472 case AF_INET:
1473 len = sizeof(struct sockaddr_in);
1474 break;
1475 case AF_INET6:
1476 len = sizeof(struct sockaddr_in6);
1477 break;
1478 default:
1479 /* No known usable source address for non-INET families. */
1480 return 0;
1481 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001483 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1484 if (sock == -1) {
1485 if (errno == EAFNOSUPPORT) {
1486 return 0;
1487 } else {
1488 return -1;
1489 }
1490 }
1491 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1492 close(sock);
1493 return 0;
1494 }
1495 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1496 close(sock);
1497 return 0;
1498 }
1499 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001500 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001501 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001503 if (ret == -1) {
1504 close(sock);
1505 return 0;
1506 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001507
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001508 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1509 close(sock);
1510 return -1;
1511 }
1512 close(sock);
1513 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001514}
1515
1516/*
1517 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1518 * Will leave the list unchanged if an error occurs.
1519 */
1520
1521/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001522static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1523 struct addrinfo* cur;
1524 int nelem = 0, i;
1525 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001526
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001527 cur = list_sentinel->ai_next;
1528 while (cur) {
1529 ++nelem;
1530 cur = cur->ai_next;
1531 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001532
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001533 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1534 if (elems == NULL) {
1535 goto error;
1536 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001537
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001538 /*
1539 * Convert the linked list to an array that also contains the candidate
1540 * source address for each destination address.
1541 */
1542 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1543 int has_src_addr;
1544 assert(cur != NULL);
1545 elems[i].ai = cur;
1546 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001547
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001548 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1549 if (has_src_addr == -1) {
1550 goto error;
1551 }
1552 elems[i].has_src_addr = has_src_addr;
1553 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001555 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1556 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001557
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001558 list_sentinel->ai_next = elems[0].ai;
1559 for (i = 0; i < nelem - 1; ++i) {
1560 elems[i].ai->ai_next = elems[i + 1].ai;
1561 }
1562 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001563
1564error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001565 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001566}
1567
1568/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001569static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1570 struct addrinfo* ai;
1571 querybuf *buf, *buf2;
1572 const char* name;
1573 const struct addrinfo* pai;
1574 struct addrinfo sentinel, *cur;
1575 struct res_target q, q2;
1576 res_state res;
1577 const struct android_net_context* netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001578
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001579 name = va_arg(ap, char*);
1580 pai = va_arg(ap, const struct addrinfo*);
1581 netcontext = va_arg(ap, const struct android_net_context*);
1582 // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001583
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001584 memset(&q, 0, sizeof(q));
1585 memset(&q2, 0, sizeof(q2));
1586 memset(&sentinel, 0, sizeof(sentinel));
1587 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001588
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001589 buf = malloc(sizeof(*buf));
1590 if (buf == NULL) {
1591 h_errno = NETDB_INTERNAL;
1592 return NS_NOTFOUND;
1593 }
1594 buf2 = malloc(sizeof(*buf2));
1595 if (buf2 == NULL) {
1596 free(buf);
1597 h_errno = NETDB_INTERNAL;
1598 return NS_NOTFOUND;
1599 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001601 switch (pai->ai_family) {
1602 case AF_UNSPEC:
1603 /* prefer IPv6 */
1604 q.name = name;
1605 q.qclass = C_IN;
1606 q.answer = buf->buf;
1607 q.anslen = sizeof(buf->buf);
1608 int query_ipv6 = 1, query_ipv4 = 1;
1609 if (pai->ai_flags & AI_ADDRCONFIG) {
1610 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1611 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1612 }
1613 if (query_ipv6) {
1614 q.qtype = T_AAAA;
1615 if (query_ipv4) {
1616 q.next = &q2;
1617 q2.name = name;
1618 q2.qclass = C_IN;
1619 q2.qtype = T_A;
1620 q2.answer = buf2->buf;
1621 q2.anslen = sizeof(buf2->buf);
1622 }
1623 } else if (query_ipv4) {
1624 q.qtype = T_A;
1625 } else {
1626 free(buf);
1627 free(buf2);
1628 return NS_NOTFOUND;
1629 }
1630 break;
1631 case AF_INET:
1632 q.name = name;
1633 q.qclass = C_IN;
1634 q.qtype = T_A;
1635 q.answer = buf->buf;
1636 q.anslen = sizeof(buf->buf);
1637 break;
1638 case AF_INET6:
1639 q.name = name;
1640 q.qclass = C_IN;
1641 q.qtype = T_AAAA;
1642 q.answer = buf->buf;
1643 q.anslen = sizeof(buf->buf);
1644 break;
1645 default:
1646 free(buf);
1647 free(buf2);
1648 return NS_UNAVAIL;
1649 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001650
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001651 res = __res_get_state();
1652 if (res == NULL) {
1653 free(buf);
1654 free(buf2);
1655 return NS_NOTFOUND;
1656 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001658 /* this just sets our netid val in the thread private data so we don't have to
1659 * modify the api's all the way down to res_send.c's res_nsend. We could
1660 * fully populate the thread private data here, but if we get down there
1661 * and have a cache hit that would be wasted, so we do the rest there on miss
1662 */
1663 res_setnetcontext(res, netcontext);
1664 if (res_searchN(name, &q, res) < 0) {
1665 __res_put_state(res);
1666 free(buf);
1667 free(buf2);
1668 return NS_NOTFOUND;
1669 }
1670 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1671 if (ai) {
1672 cur->ai_next = ai;
1673 while (cur && cur->ai_next) cur = cur->ai_next;
1674 }
1675 if (q.next) {
1676 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1677 if (ai) cur->ai_next = ai;
1678 }
1679 free(buf);
1680 free(buf2);
1681 if (sentinel.ai_next == NULL) {
1682 __res_put_state(res);
1683 switch (h_errno) {
1684 case HOST_NOT_FOUND:
1685 return NS_NOTFOUND;
1686 case TRY_AGAIN:
1687 return NS_TRYAGAIN;
1688 default:
1689 return NS_UNAVAIL;
1690 }
1691 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001693 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001695 __res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001697 *((struct addrinfo**) rv) = sentinel.ai_next;
1698 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001699}
1700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001701static void _sethtent(FILE** hostf) {
1702 if (!*hostf)
1703 *hostf = fopen(_PATH_HOSTS, "re");
1704 else
1705 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001706}
1707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001708static void _endhtent(FILE** hostf) {
1709 if (*hostf) {
1710 (void) fclose(*hostf);
1711 *hostf = NULL;
1712 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001713}
1714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001715static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1716 char* p;
1717 char *cp, *tname, *cname;
1718 struct addrinfo hints, *res0, *res;
1719 int error;
1720 const char* addr;
1721 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 // fprintf(stderr, "_gethtent() name = '%s'\n", name);
1724 assert(name != NULL);
1725 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001727 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1728again:
1729 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1730 if (*p == '#') goto again;
1731 if (!(cp = strpbrk(p, "#\n"))) goto again;
1732 *cp = '\0';
1733 if (!(cp = strpbrk(p, " \t"))) goto again;
1734 *cp++ = '\0';
1735 addr = p;
1736 /* if this is not something we're looking for, skip it. */
1737 cname = NULL;
1738 while (cp && *cp) {
1739 if (*cp == ' ' || *cp == '\t') {
1740 cp++;
1741 continue;
1742 }
1743 if (!cname) cname = cp;
1744 tname = cp;
1745 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1746 // fprintf(stderr, "\ttname = '%s'", tname);
1747 if (strcasecmp(name, tname) == 0) goto found;
1748 }
1749 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001750
1751found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001752 hints = *pai;
1753 hints.ai_flags = AI_NUMERICHOST;
1754 error = getaddrinfo(addr, NULL, &hints, &res0);
1755 if (error) goto again;
1756 for (res = res0; res; res = res->ai_next) {
1757 /* cover it up */
1758 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001760 if (pai->ai_flags & AI_CANONNAME) {
1761 if (get_canonname(pai, res, cname) != 0) {
1762 freeaddrinfo(res0);
1763 goto again;
1764 }
1765 }
1766 }
1767 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001768}
1769
1770/*ARGSUSED*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001771static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1772 const char* name;
1773 const struct addrinfo* pai;
1774 struct addrinfo sentinel, *cur;
1775 struct addrinfo* p;
1776 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001778 name = va_arg(ap, char*);
1779 pai = va_arg(ap, struct addrinfo*);
Bernie Innocenti55864192018-08-30 04:05:20 +09001780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001781 // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
1782 memset(&sentinel, 0, sizeof(sentinel));
1783 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001784
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001785 _sethtent(&hostf);
1786 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1787 cur->ai_next = p;
1788 while (cur && cur->ai_next) cur = cur->ai_next;
1789 }
1790 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001791
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001792 *((struct addrinfo**) rv) = sentinel.ai_next;
1793 if (sentinel.ai_next == NULL) return NS_NOTFOUND;
1794 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001795}
1796
1797/* resolver logic */
1798
1799/*
1800 * Formulate a normal query, send, and await answer.
1801 * Returned answer is placed in supplied buffer "answer".
1802 * Perform preliminary check of answer, returning success only
1803 * if no error is indicated and the answer count is nonzero.
1804 * Return the size of the response on success, -1 on error.
1805 * Error number is left in h_errno.
1806 *
1807 * Caller must parse answer and determine whether it answers the question.
1808 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001809static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1810 res_state res) {
1811 u_char buf[MAXPACKET];
1812 HEADER* hp;
1813 int n;
1814 struct res_target* t;
1815 int rcode;
1816 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001818 assert(name != NULL);
1819 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001820
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001821 rcode = NOERROR;
1822 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001824 for (t = target; t; t = t->next) {
1825 int class, type;
1826 u_char* answer;
1827 int anslen;
1828 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001829
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001830 hp = (HEADER*) (void*) t->answer;
1831 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001832
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001833 again:
1834 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001835
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001836 /* make it easier... */
1837 class = t->qclass;
1838 type = t->qtype;
1839 answer = t->answer;
1840 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001841#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001842 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001843#endif
1844
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001845 n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocenti55864192018-08-30 04:05:20 +09001846#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001847 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1848 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1849 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001850#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001851 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001852#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001853 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001854#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001855 h_errno = NO_RECOVERY;
1856 return n;
1857 }
1858 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001859#if 0
1860 if (n < 0) {
1861#ifdef DEBUG
1862 if (res->options & RES_DEBUG)
1863 printf(";; res_query: send error\n");
1864#endif
1865 h_errno = TRY_AGAIN;
1866 return n;
1867 }
1868#endif
1869
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001870 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1871 rcode = hp->rcode; /* record most recent error */
Bernie Innocenti55864192018-08-30 04:05:20 +09001872#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001873 /* if the query choked with EDNS0, retry without EDNS0 */
1874 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1875 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1876 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001877#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001878 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001879#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001880 goto again;
1881 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001882#endif
1883#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001884 if (res->options & RES_DEBUG)
1885 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001886#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001887 continue;
1888 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001889
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001890 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001891
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001892 t->n = n;
1893 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001894
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001895 if (ancount == 0) {
1896 switch (rcode) {
1897 case NXDOMAIN:
1898 h_errno = HOST_NOT_FOUND;
1899 break;
1900 case SERVFAIL:
1901 h_errno = TRY_AGAIN;
1902 break;
1903 case NOERROR:
1904 h_errno = NO_DATA;
1905 break;
1906 case FORMERR:
1907 case NOTIMP:
1908 case REFUSED:
1909 default:
1910 h_errno = NO_RECOVERY;
1911 break;
1912 }
1913 return -1;
1914 }
1915 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001916}
1917
1918/*
1919 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1920 * Return the size of the response on success, -1 on error.
1921 * If enabled, implement search rules until answer or unrecoverable failure
1922 * is detected. Error code, if any, is left in h_errno.
1923 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001924static int res_searchN(const char* name, struct res_target* target, res_state res) {
1925 const char *cp, *const *domain;
1926 HEADER* hp;
1927 u_int dots;
1928 int trailing_dot, ret, saved_herrno;
1929 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001930
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001931 assert(name != NULL);
1932 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001933
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001934 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001935
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001936 errno = 0;
1937 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1938 dots = 0;
1939 for (cp = name; *cp; cp++) dots += (*cp == '.');
1940 trailing_dot = 0;
1941 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001942
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001943 // fprintf(stderr, "res_searchN() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001944
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001945 /*
1946 * if there aren't any dots, it could be a user-level alias
1947 */
1948 if (!dots && (cp = __hostalias(name)) != NULL) {
1949 ret = res_queryN(cp, target, res);
1950 return ret;
1951 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001952
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001953 /*
1954 * If there are dots in the name already, let's just give it a try
1955 * 'as is'. The threshold can be set with the "ndots" option.
1956 */
1957 saved_herrno = -1;
1958 if (dots >= res->ndots) {
1959 ret = res_querydomainN(name, NULL, target, res);
1960 if (ret > 0) return (ret);
1961 saved_herrno = h_errno;
1962 tried_as_is++;
1963 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001964
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001965 /*
1966 * We do at least one level of search if
1967 * - there is no dot and RES_DEFNAME is set, or
1968 * - there is at least one dot, there is no trailing dot,
1969 * and RES_DNSRCH is set.
1970 */
1971 if ((!dots && (res->options & RES_DEFNAMES)) ||
1972 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1973 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001974
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001975 /* Unfortunately we need to set stuff up before
1976 * the domain stuff is tried. Will have a better
1977 * fix after thread pools are used.
1978 */
1979 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001980
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001981 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1982 ret = res_querydomainN(name, *domain, target, res);
1983 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001984
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001985 /*
1986 * If no server present, give up.
1987 * If name isn't found in this domain,
1988 * keep trying higher domains in the search list
1989 * (if that's enabled).
1990 * On a NO_DATA error, keep trying, otherwise
1991 * a wildcard entry of another type could keep us
1992 * from finding this entry higher in the domain.
1993 * If we get some other error (negative answer or
1994 * server failure), then stop searching up,
1995 * but try the input name below in case it's
1996 * fully-qualified.
1997 */
1998 if (errno == ECONNREFUSED) {
1999 h_errno = TRY_AGAIN;
2000 return -1;
2001 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002002
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002003 switch (h_errno) {
2004 case NO_DATA:
2005 got_nodata++;
2006 /* FALLTHROUGH */
2007 case HOST_NOT_FOUND:
2008 /* keep trying */
2009 break;
2010 case TRY_AGAIN:
2011 if (hp->rcode == SERVFAIL) {
2012 /* try next search element, if any */
2013 got_servfail++;
2014 break;
2015 }
2016 /* FALLTHROUGH */
2017 default:
2018 /* anything else implies that we're done */
2019 done++;
2020 }
2021 /*
2022 * if we got here for some reason other than DNSRCH,
2023 * we only wanted one iteration of the loop, so stop.
2024 */
2025 if (!(res->options & RES_DNSRCH)) done++;
2026 }
2027 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002028
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002029 /*
2030 * if we have not already tried the name "as is", do that now.
2031 * note that we do this regardless of how many dots were in the
2032 * name or whether it ends with a dot.
2033 */
2034 if (!tried_as_is) {
2035 ret = res_querydomainN(name, NULL, target, res);
2036 if (ret > 0) return ret;
2037 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002038
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002039 /*
2040 * if we got here, we didn't satisfy the search.
2041 * if we did an initial full query, return that query's h_errno
2042 * (note that we wouldn't be here if that query had succeeded).
2043 * else if we ever got a nodata, send that back as the reason.
2044 * else send back meaningless h_errno, that being the one from
2045 * the last DNSRCH we did.
2046 */
2047 if (saved_herrno != -1)
2048 h_errno = saved_herrno;
2049 else if (got_nodata)
2050 h_errno = NO_DATA;
2051 else if (got_servfail)
2052 h_errno = TRY_AGAIN;
2053 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09002054}
2055
2056/*
2057 * Perform a call on res_query on the concatenation of name and domain,
2058 * removing a trailing dot from name if domain is NULL.
2059 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002060static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
2061 res_state res) {
2062 char nbuf[MAXDNAME];
2063 const char* longname = nbuf;
2064 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09002065
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002066 assert(name != NULL);
2067 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09002068
2069#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002070 if (res->options & RES_DEBUG)
2071 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09002072#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002073 if (domain == NULL) {
2074 /*
2075 * Check for trailing '.';
2076 * copy without '.' if present.
2077 */
2078 n = strlen(name);
2079 if (n + 1 > sizeof(nbuf)) {
2080 h_errno = NO_RECOVERY;
2081 return -1;
2082 }
2083 if (n > 0 && name[--n] == '.') {
2084 strncpy(nbuf, name, n);
2085 nbuf[n] = '\0';
2086 } else
2087 longname = name;
2088 } else {
2089 n = strlen(name);
2090 d = strlen(domain);
2091 if (n + 1 + d + 1 > sizeof(nbuf)) {
2092 h_errno = NO_RECOVERY;
2093 return -1;
2094 }
2095 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2096 }
2097 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09002098}