blob: cbe1cc47b01429edc2a97a8bca111abf0f5ab2b4 [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
Bernie Innocenti55864192018-08-30 04:05:20 +0900116#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900117
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900118static const char in_addrany[] = {0, 0, 0, 0};
119static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
121static 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 +0900122
Bernie Innocenti55864192018-08-30 04:05:20 +0900123static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124 int a_af;
125 int a_addrlen;
126 int a_socklen;
127 int a_off;
128 const char* a_addrany;
129 const char* a_loopback;
130 int a_scoped;
131} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900132 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
133 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
135 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
136 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900137};
138
139struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900140 int e_af;
141 int e_socktype;
142 int e_protocol;
143 const char* e_protostr;
144 int e_wild;
145#define WILD_AF(ex) ((ex)->e_wild & 0x01)
146#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
147#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900148};
149
150static const struct explore explore[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
152 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
153 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
155 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
156 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
157 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
158 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
159 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
160 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900161};
162
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163#define PTON_MAX 16
Bernie Innocenti55864192018-08-30 04:05:20 +0900164
165static const ns_src default_dns_files[] = {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900166 {NSSRC_FILES, NS_SUCCESS},
167 {NSSRC_DNS, NS_SUCCESS},
168 {0, 0}
169};
Bernie Innocenti55864192018-08-30 04:05:20 +0900170
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900171#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900172
173typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900174 HEADER hdr;
175 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900176} querybuf;
177
178struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900179 struct res_target* next;
180 const char* name; /* domain name */
181 int qclass, qtype; /* class and type of query */
182 u_char* answer; /* buffer to put answer */
183 int anslen; /* size of answer buffer */
184 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900185};
186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187static int str2number(const char*);
188static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
189 const struct android_net_context*);
190static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
191static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
192 const char*);
193static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
194 struct addrinfo**);
195static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
196static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
197static int get_portmatch(const struct addrinfo*, const char*);
198static int get_port(const struct addrinfo*, const char*, int);
199static const struct afd* find_afd(int);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900200static int ip6_str2scopeid(char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900201
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
203static int _dns_getaddrinfo(void*, void*, va_list);
204static void _sethtent(FILE**);
205static void _endhtent(FILE**);
206static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
207static int _files_getaddrinfo(void*, void*, va_list);
208static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900209
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900210static int res_queryN(const char*, struct res_target*, res_state);
211static int res_searchN(const char*, struct res_target*, res_state);
212static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900213
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214static const char* const ai_errlist[] = {
215 "Success",
216 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
217 "Temporary failure in name resolution", /* EAI_AGAIN */
218 "Invalid value for ai_flags", /* EAI_BADFLAGS */
219 "Non-recoverable failure in name resolution", /* EAI_FAIL */
220 "ai_family not supported", /* EAI_FAMILY */
221 "Memory allocation failure", /* EAI_MEMORY */
222 "No address associated with hostname", /* EAI_NODATA */
223 "hostname nor servname provided, or not known", /* EAI_NONAME */
224 "servname not supported for ai_socktype", /* EAI_SERVICE */
225 "ai_socktype not supported", /* EAI_SOCKTYPE */
226 "System error returned in errno", /* EAI_SYSTEM */
227 "Invalid value for hints", /* EAI_BADHINTS */
228 "Resolved protocol is unknown", /* EAI_PROTOCOL */
229 "Argument buffer overflow", /* EAI_OVERFLOW */
230 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900231};
232
233/* XXX macros that make external reference is BAD. */
234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235#define GET_AI(ai, afd, addr) \
236 do { \
237 /* external reference: pai, error, and label free */ \
238 (ai) = get_ai(pai, (afd), (addr)); \
239 if ((ai) == NULL) { \
240 error = EAI_MEMORY; \
241 goto free; \
242 } \
243 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900244
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900245#define GET_PORT(ai, serv) \
246 do { \
247 /* external reference: error and label free */ \
248 error = get_port((ai), (serv), 0); \
249 if (error != 0) goto free; \
250 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900252#define GET_CANONNAME(ai, str) \
253 do { \
254 /* external reference: pai, error and label free */ \
255 error = get_canonname(pai, (ai), (str)); \
256 if (error != 0) goto free; \
257 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900258
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900259#define ERR(err) \
260 do { \
261 /* external reference: error, and label bad */ \
262 error = (err); \
263 goto bad; \
264 /*NOTREACHED*/ \
265 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267#define MATCH_FAMILY(x, y, w) \
268 ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
269#define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900270
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900271const char* gai_strerror(int ecode) {
272 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
273 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900274}
275
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276void freeaddrinfo(struct addrinfo* ai) {
277 struct addrinfo* next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900278
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900279 if (ai == NULL) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900280
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900281 do {
282 next = ai->ai_next;
283 if (ai->ai_canonname) free(ai->ai_canonname);
284 /* no need to free(ai->ai_addr) */
285 free(ai);
286 ai = next;
287 } while (ai);
Bernie Innocenti55864192018-08-30 04:05:20 +0900288}
289
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900290static int str2number(const char* p) {
291 char* ep;
292 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900293
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900294 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900295
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900296 if (*p == '\0') return -1;
297 ep = NULL;
298 errno = 0;
299 v = strtoul(p, &ep, 10);
300 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
301 return v;
302 else
303 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900304}
305
306/*
307 * The following functions determine whether IPv4 or IPv6 connectivity is
308 * available in order to implement AI_ADDRCONFIG.
309 *
310 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
311 * available, but whether addresses of the specified family are "configured
312 * on the local system". However, bionic doesn't currently support getifaddrs,
313 * so checking for connectivity is the next best thing.
314 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900315static int _have_ipv6(unsigned mark, uid_t uid) {
316 static const struct sockaddr_in6 sin6_test = {
317 .sin6_family = AF_INET6,
318 .sin6_addr.s6_addr = {// 2000::
319 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
320 sockaddr_union addr = {.in6 = sin6_test};
321 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900322}
323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900324static int _have_ipv4(unsigned mark, uid_t uid) {
325 static const struct sockaddr_in sin_test = {
326 .sin_family = AF_INET,
327 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
328 };
329 sockaddr_union addr = {.in = sin_test};
330 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900331}
332
333bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900334 int32_t tmp;
335 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
336 return false;
337 }
338 *result = ntohl(tmp);
339 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900340}
341
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900342int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
343 struct addrinfo** res) {
344 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900345}
346
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900347int android_getaddrinfofornet(const char* hostname, const char* servname,
348 const struct addrinfo* hints, unsigned netid, unsigned mark,
349 struct addrinfo** res) {
350 struct android_net_context netcontext = {
351 .app_netid = netid,
352 .app_mark = mark,
353 .dns_netid = netid,
354 .dns_mark = mark,
355 .uid = NET_CONTEXT_INVALID_UID,
356 };
357 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900358}
359
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900360int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
361 const struct addrinfo* hints,
362 const struct android_net_context* netcontext,
363 struct addrinfo** res) {
364 struct addrinfo sentinel;
365 struct addrinfo* cur;
366 int error = 0;
367 struct addrinfo ai;
368 struct addrinfo ai0;
369 struct addrinfo* pai;
370 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900371
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900372 /* hostname is allowed to be NULL */
373 /* servname is allowed to be NULL */
374 /* hints is allowed to be NULL */
375 assert(res != NULL);
376 assert(netcontext != NULL);
377 memset(&sentinel, 0, sizeof(sentinel));
378 cur = &sentinel;
379 pai = &ai;
380 pai->ai_flags = 0;
381 pai->ai_family = PF_UNSPEC;
382 pai->ai_socktype = ANY;
383 pai->ai_protocol = ANY;
384 pai->ai_addrlen = 0;
385 pai->ai_canonname = NULL;
386 pai->ai_addr = NULL;
387 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900388
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900389 if (hostname == NULL && servname == NULL) return EAI_NONAME;
390 if (hints) {
391 /* error check for hints */
392 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
393 ERR(EAI_BADHINTS); /* xxx */
394 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
395 switch (hints->ai_family) {
396 case PF_UNSPEC:
397 case PF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900398 case PF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900399 break;
400 default:
401 ERR(EAI_FAMILY);
402 }
403 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900404
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900405 /*
406 * if both socktype/protocol are specified, check if they
407 * are meaningful combination.
408 */
409 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
410 for (ex = explore; ex->e_af >= 0; ex++) {
411 if (pai->ai_family != ex->e_af) continue;
412 if (ex->e_socktype == ANY) continue;
413 if (ex->e_protocol == ANY) continue;
414 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
415 ERR(EAI_BADHINTS);
416 }
417 }
418 }
419 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900420
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900421 /*
422 * check for special cases. (1) numeric servname is disallowed if
423 * socktype/protocol are left unspecified. (2) servname is disallowed
424 * for raw and other inet{,6} sockets.
425 */
426 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900427 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900428 ) {
429 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900430
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900431 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 pai->ai_family = PF_INET6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900433 }
434 error = get_portmatch(pai, servname);
435 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900436
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900437 *pai = ai0;
438 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900439
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900441
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900442 /* NULL hostname, or numeric hostname */
443 for (ex = explore; ex->e_af >= 0; ex++) {
444 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900445
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900446 /* PF_UNSPEC entries are prepared for DNS queries only */
447 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900448
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
450 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
451 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900452
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
454 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
455 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900457 if (hostname == NULL)
458 error = explore_null(pai, servname, &cur->ai_next);
459 else
460 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900461
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900462 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900463
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900464 while (cur->ai_next) cur = cur->ai_next;
465 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900466
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900467 /*
468 * XXX
469 * If numeric representation of AF1 can be interpreted as FQDN
470 * representation of AF2, we need to think again about the code below.
471 */
472 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900473
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900474 if (hostname == NULL) ERR(EAI_NODATA);
475 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900476
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477 /*
478 * hostname as alphabetical name.
479 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
480 * outer loop by AFs.
481 */
482 for (ex = explore; ex->e_af >= 0; ex++) {
483 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485 /* require exact match for family field */
486 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900487
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
489 continue;
490 }
491 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
492 continue;
493 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900494
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
496 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900497
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900499
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 while (cur && cur->ai_next) cur = cur->ai_next;
501 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 /* XXX */
504 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900505
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506 if (error) goto free;
507 if (error == 0) {
508 if (sentinel.ai_next) {
509 good:
510 *res = sentinel.ai_next;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900511 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900512 } else
513 error = EAI_FAIL;
514 }
515free:
516bad:
517 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
518 *res = NULL;
519 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900520}
521
522/*
523 * FQDN hostname, DNS lookup
524 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
526 struct addrinfo** res, const struct android_net_context* netcontext) {
527 struct addrinfo* result;
528 struct addrinfo* cur;
529 int error = 0;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900530 static const ns_dtab dtab[] = {
531 {NSSRC_FILES, _files_getaddrinfo, NULL},
532 {NSSRC_DNS, _dns_getaddrinfo, NULL},
533 {0, 0, 0}
534 };
Bernie Innocenti55864192018-08-30 04:05:20 +0900535
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900536 assert(pai != NULL);
537 /* hostname may be NULL */
538 /* servname may be NULL */
539 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900540
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900542
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900543 /*
544 * if the servname does not match socktype/protocol, ignore it.
545 */
546 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900547
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900548 switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai,
549 netcontext)) {
550 case NS_TRYAGAIN:
551 error = EAI_AGAIN;
552 goto free;
553 case NS_UNAVAIL:
554 error = EAI_FAIL;
555 goto free;
556 case NS_NOTFOUND:
557 error = EAI_NODATA;
558 goto free;
559 case NS_SUCCESS:
560 error = 0;
561 for (cur = result; cur; cur = cur->ai_next) {
562 GET_PORT(cur, servname);
563 /* canonname should be filled already */
564 }
565 break;
566 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 *res = result;
Bernie Innocenti55864192018-08-30 04:05:20 +0900569
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900570 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900571
572free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573 if (result) freeaddrinfo(result);
574 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900575}
576
577/*
578 * hostname == NULL.
579 * passive socket -> anyaddr (0.0.0.0 or ::)
580 * non-passive socket -> localhost (127.0.0.1 or ::1)
581 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900582static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
583 int s;
584 const struct afd* afd;
585 struct addrinfo* cur;
586 struct addrinfo sentinel;
587 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900588
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900589 assert(pai != NULL);
590 /* servname may be NULL */
591 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900592
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900593 *res = NULL;
594 sentinel.ai_next = NULL;
595 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 /*
598 * filter out AFs that are not supported by the kernel
599 * XXX errno?
600 */
601 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
602 if (s < 0) {
603 if (errno != EMFILE) return 0;
604 } else
605 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900606
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900607 /*
608 * if the servname does not match socktype/protocol, ignore it.
609 */
610 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900611
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900612 afd = find_afd(pai->ai_family);
613 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 if (pai->ai_flags & AI_PASSIVE) {
616 GET_AI(cur->ai_next, afd, afd->a_addrany);
617 /* xxx meaningless?
618 * GET_CANONNAME(cur->ai_next, "anyaddr");
619 */
620 GET_PORT(cur->ai_next, servname);
621 } else {
622 GET_AI(cur->ai_next, afd, afd->a_loopback);
623 /* xxx meaningless?
624 * GET_CANONNAME(cur->ai_next, "localhost");
625 */
626 GET_PORT(cur->ai_next, servname);
627 }
628 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900629
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900630 *res = sentinel.ai_next;
631 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900632
633free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900634 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
635 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900636}
637
638/*
639 * numeric hostname
640 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
642 struct addrinfo** res, const char* canonname) {
643 const struct afd* afd;
644 struct addrinfo* cur;
645 struct addrinfo sentinel;
646 int error;
647 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 assert(pai != NULL);
650 /* hostname may be NULL */
651 /* servname may be NULL */
652 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900654 *res = NULL;
655 sentinel.ai_next = NULL;
656 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900658 /*
659 * if the servname does not match socktype/protocol, ignore it.
660 */
661 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900662
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 afd = find_afd(pai->ai_family);
664 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900665
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900666 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900667#if 0 /*X/Open spec*/
668 case AF_INET:
669 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
670 if (pai->ai_family == afd->a_af ||
671 pai->ai_family == PF_UNSPEC /*?*/) {
672 GET_AI(cur->ai_next, afd, pton);
673 GET_PORT(cur->ai_next, servname);
674 if ((pai->ai_flags & AI_CANONNAME)) {
675 /*
676 * Set the numeric address itself as
677 * the canonical name, based on a
678 * clarification in rfc2553bis-03.
679 */
680 GET_CANONNAME(cur->ai_next, canonname);
681 }
682 while (cur && cur->ai_next)
683 cur = cur->ai_next;
684 } else
685 ERR(EAI_FAMILY); /*xxx*/
686 }
687 break;
688#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 default:
690 if (inet_pton(afd->a_af, hostname, pton) == 1) {
691 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
692 GET_AI(cur->ai_next, afd, pton);
693 GET_PORT(cur->ai_next, servname);
694 if ((pai->ai_flags & AI_CANONNAME)) {
695 /*
696 * Set the numeric address itself as
697 * the canonical name, based on a
698 * clarification in rfc2553bis-03.
699 */
700 GET_CANONNAME(cur->ai_next, canonname);
701 }
702 while (cur->ai_next) cur = cur->ai_next;
703 } else
704 ERR(EAI_FAMILY); /*xxx*/
705 }
706 break;
707 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900708
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709 *res = sentinel.ai_next;
710 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900711
712free:
713bad:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900714 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
715 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900716}
717
718/*
719 * numeric hostname with scope
720 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
722 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900723 const struct afd* afd;
724 struct addrinfo* cur;
725 int error;
726 char *cp, *hostname2 = NULL, *scope, *addr;
727 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900728
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900729 assert(pai != NULL);
730 /* hostname may be NULL */
731 /* servname may be NULL */
732 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900733
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900734 /*
735 * if the servname does not match socktype/protocol, ignore it.
736 */
737 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900739 afd = find_afd(pai->ai_family);
740 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900743
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 cp = strchr(hostname, SCOPE_DELIMITER);
745 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900746
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 /*
748 * Handle special case of <scoped_address><delimiter><scope id>
749 */
750 hostname2 = strdup(hostname);
751 if (hostname2 == NULL) return EAI_MEMORY;
752 /* terminate at the delimiter */
753 hostname2[cp - hostname] = '\0';
754 addr = hostname2;
755 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900756
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900757 error = explore_numeric(pai, addr, servname, res, hostname);
758 if (error == 0) {
759 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900760
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900761 for (cur = *res; cur; cur = cur->ai_next) {
762 if (cur->ai_family != AF_INET6) continue;
763 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
764 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
765 free(hostname2);
766 return (EAI_NODATA); /* XXX: is return OK? */
767 }
768 sin6->sin6_scope_id = scopeid;
769 }
770 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900772 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900773
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900774 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900775}
776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
778 assert(pai != NULL);
779 assert(ai != NULL);
780 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900781
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900782 if ((pai->ai_flags & AI_CANONNAME) != 0) {
783 ai->ai_canonname = strdup(str);
784 if (ai->ai_canonname == NULL) return EAI_MEMORY;
785 }
786 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900787}
788
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900789static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
790 const char* addr) {
791 char* p;
792 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900793
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900794 assert(pai != NULL);
795 assert(afd != NULL);
796 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900797
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900798 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
799 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 memcpy(ai, pai, sizeof(struct addrinfo));
802 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
803 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900805 ai->ai_addrlen = afd->a_socklen;
806#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
807 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900808#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900809 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
810 p = (char*) (void*) (ai->ai_addr);
811 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
812 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900813}
814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815static int get_portmatch(const struct addrinfo* ai, const char* servname) {
816 assert(ai != NULL);
817 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900820}
821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
823 const char* proto;
824 struct servent* sp;
825 int port;
826 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 assert(ai != NULL);
829 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 if (servname == NULL) return 0;
832 switch (ai->ai_family) {
833 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900834 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835 break;
836 default:
837 return 0;
838 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 switch (ai->ai_socktype) {
841 case SOCK_RAW:
842 return EAI_SERVICE;
843 case SOCK_DGRAM:
844 case SOCK_STREAM:
845 allownumeric = 1;
846 break;
847 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849 break;
850 default:
851 return EAI_SOCKTYPE;
852 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900853
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900854 port = str2number(servname);
855 if (port >= 0) {
856 if (!allownumeric) return EAI_SERVICE;
857 if (port < 0 || port > 65535) return EAI_SERVICE;
858 port = htons(port);
859 } else {
860 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900861
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900862 switch (ai->ai_socktype) {
863 case SOCK_DGRAM:
864 proto = "udp";
865 break;
866 case SOCK_STREAM:
867 proto = "tcp";
868 break;
869 default:
870 proto = NULL;
871 break;
872 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900873
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900874 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
875 port = sp->s_port;
876 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900877
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900878 if (!matchonly) {
879 switch (ai->ai_family) {
880 case AF_INET:
881 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
882 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900883 case AF_INET6:
884 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
885 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900886 }
887 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900888
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900889 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900890}
891
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900892static const struct afd* find_afd(int af) {
893 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900894
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900895 if (af == PF_UNSPEC) return NULL;
896 for (afd = afdl; afd->a_af; afd++) {
897 if (afd->a_af == af) return afd;
898 }
899 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900900}
901
Bernie Innocenti357339c2018-08-31 16:11:41 +0900902/* convert a string to a scope identifier. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
904 u_long lscopeid;
905 struct in6_addr* a6;
906 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900907
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900908 assert(scope != NULL);
909 assert(sin6 != NULL);
910 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900911
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900913
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 /* empty scopeid portion is invalid */
915 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900916
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900917 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
918 /*
919 * We currently assume a one-to-one mapping between links
920 * and interfaces, so we simply use interface indices for
921 * like-local scopes.
922 */
923 *scopeid = if_nametoindex(scope);
924 if (*scopeid == 0) goto trynumeric;
925 return 0;
926 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900927
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900928 /* still unclear about literal, allow numeric only - placeholder */
929 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
930 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
931 goto trynumeric;
932 else
933 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900934
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900935 /* try to convert to a numeric id as a last resort */
936trynumeric:
937 errno = 0;
938 lscopeid = strtoul(scope, &ep, 10);
939 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
940 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
941 return 0;
942 else
943 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900944}
Bernie Innocenti55864192018-08-30 04:05:20 +0900945
946/* code duplicate with gethnamaddr.c */
947
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900948static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900949
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900950#define BOUNDED_INCR(x) \
951 do { \
952 BOUNDS_CHECK(cp, x); \
953 cp += (x); \
954 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900955
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900956#define BOUNDS_CHECK(ptr, count) \
957 do { \
958 if (eom - (ptr) < (count)) { \
959 h_errno = NO_RECOVERY; \
960 return NULL; \
961 } \
962 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900963
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900964static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
965 const struct addrinfo* pai) {
966 struct addrinfo sentinel, *cur;
967 struct addrinfo ai;
968 const struct afd* afd;
969 char* canonname;
970 const HEADER* hp;
971 const u_char* cp;
972 int n;
973 const u_char* eom;
974 char *bp, *ep;
975 int type, class, ancount, qdcount;
976 int haveanswer, had_error;
977 char tbuf[MAXDNAME];
978 int (*name_ok)(const char*);
979 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900980
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900981 assert(answer != NULL);
982 assert(qname != NULL);
983 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900984
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 memset(&sentinel, 0, sizeof(sentinel));
986 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900987
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900988 canonname = NULL;
989 eom = answer->buf + anslen;
990 switch (qtype) {
991 case T_A:
992 case T_AAAA:
993 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
994 name_ok = res_hnok;
995 break;
996 default:
997 return NULL; /* XXX should be abort(); */
998 }
999 /*
1000 * find first satisfactory answer
1001 */
1002 hp = &answer->hdr;
1003 ancount = ntohs(hp->ancount);
1004 qdcount = ntohs(hp->qdcount);
1005 bp = hostbuf;
1006 ep = hostbuf + sizeof hostbuf;
1007 cp = answer->buf;
1008 BOUNDED_INCR(HFIXEDSZ);
1009 if (qdcount != 1) {
1010 h_errno = NO_RECOVERY;
1011 return (NULL);
1012 }
1013 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1014 if ((n < 0) || !(*name_ok)(bp)) {
1015 h_errno = NO_RECOVERY;
1016 return (NULL);
1017 }
1018 BOUNDED_INCR(n + QFIXEDSZ);
1019 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1020 /* res_send() has already verified that the query name is the
1021 * same as the one we sent; this just gets the expanded name
1022 * (i.e., with the succeeding search-domain tacked on).
1023 */
1024 n = strlen(bp) + 1; /* for the \0 */
1025 if (n >= MAXHOSTNAMELEN) {
1026 h_errno = NO_RECOVERY;
1027 return (NULL);
1028 }
1029 canonname = bp;
1030 bp += n;
1031 /* The qname can be abbreviated, but h_name is now absolute. */
1032 qname = canonname;
1033 }
1034 haveanswer = 0;
1035 had_error = 0;
1036 while (ancount-- > 0 && cp < eom && !had_error) {
1037 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1038 if ((n < 0) || !(*name_ok)(bp)) {
1039 had_error++;
1040 continue;
1041 }
1042 cp += n; /* name */
1043 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1044 type = _getshort(cp);
1045 cp += INT16SZ; /* type */
1046 class = _getshort(cp);
1047 cp += INT16SZ + INT32SZ; /* class, TTL */
1048 n = _getshort(cp);
1049 cp += INT16SZ; /* len */
1050 BOUNDS_CHECK(cp, n);
1051 if (class != C_IN) {
1052 /* XXX - debug? syslog? */
1053 cp += n;
1054 continue; /* XXX - had_error++ ? */
1055 }
1056 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1057 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1058 if ((n < 0) || !(*name_ok)(tbuf)) {
1059 had_error++;
1060 continue;
1061 }
1062 cp += n;
1063 /* Get canonical name. */
1064 n = strlen(tbuf) + 1; /* for the \0 */
1065 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1066 had_error++;
1067 continue;
1068 }
1069 strlcpy(bp, tbuf, (size_t)(ep - bp));
1070 canonname = bp;
1071 bp += n;
1072 continue;
1073 }
1074 if (qtype == T_ANY) {
1075 if (!(type == T_A || type == T_AAAA)) {
1076 cp += n;
1077 continue;
1078 }
1079 } else if (type != qtype) {
1080 if (type != T_KEY && type != T_SIG)
1081 syslog(LOG_NOTICE | LOG_AUTH,
1082 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1083 p_class(C_IN), p_type(qtype), p_type(type));
1084 cp += n;
1085 continue; /* XXX - had_error++ ? */
1086 }
1087 switch (type) {
1088 case T_A:
1089 case T_AAAA:
1090 if (strcasecmp(canonname, bp) != 0) {
1091 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1092 cp += n;
1093 continue; /* XXX - had_error++ ? */
1094 }
1095 if (type == T_A && n != INADDRSZ) {
1096 cp += n;
1097 continue;
1098 }
1099 if (type == T_AAAA && n != IN6ADDRSZ) {
1100 cp += n;
1101 continue;
1102 }
1103 if (type == T_AAAA) {
1104 struct in6_addr in6;
1105 memcpy(&in6, cp, IN6ADDRSZ);
1106 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1107 cp += n;
1108 continue;
1109 }
1110 }
1111 if (!haveanswer) {
1112 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001113
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001114 canonname = bp;
1115 nn = strlen(bp) + 1; /* for the \0 */
1116 bp += nn;
1117 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001119 /* don't overwrite pai */
1120 ai = *pai;
1121 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1122 afd = find_afd(ai.ai_family);
1123 if (afd == NULL) {
1124 cp += n;
1125 continue;
1126 }
1127 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1128 if (cur->ai_next == NULL) had_error++;
1129 while (cur && cur->ai_next) cur = cur->ai_next;
1130 cp += n;
1131 break;
1132 default:
1133 abort();
1134 }
1135 if (!had_error) haveanswer++;
1136 }
1137 if (haveanswer) {
1138 if (!canonname)
1139 (void) get_canonname(pai, sentinel.ai_next, qname);
1140 else
1141 (void) get_canonname(pai, sentinel.ai_next, canonname);
1142 h_errno = NETDB_SUCCESS;
1143 return sentinel.ai_next;
1144 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001145
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001146 h_errno = NO_RECOVERY;
1147 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001148}
1149
1150struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001151 struct addrinfo* ai;
1152 int has_src_addr;
1153 sockaddr_union src_addr;
1154 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001155};
1156
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001157static int _get_scope(const struct sockaddr* addr) {
1158 if (addr->sa_family == AF_INET6) {
1159 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1160 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1161 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1162 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1163 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1164 /*
1165 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1166 * link-local scope.
1167 */
1168 return IPV6_ADDR_SCOPE_LINKLOCAL;
1169 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1170 return IPV6_ADDR_SCOPE_SITELOCAL;
1171 } else {
1172 return IPV6_ADDR_SCOPE_GLOBAL;
1173 }
1174 } else if (addr->sa_family == AF_INET) {
1175 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1176 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001177
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001178 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1179 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1180 return IPV6_ADDR_SCOPE_LINKLOCAL;
1181 } else {
1182 /*
1183 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1184 * and shared addresses (100.64.0.0/10), are assigned global scope.
1185 */
1186 return IPV6_ADDR_SCOPE_GLOBAL;
1187 }
1188 } else {
1189 /*
1190 * This should never happen.
1191 * Return a scope with low priority as a last resort.
1192 */
1193 return IPV6_ADDR_SCOPE_NODELOCAL;
1194 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001195}
1196
1197/* These macros are modelled after the ones in <netinet/in6.h>. */
1198
1199/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001200#define IN6_IS_ADDR_TEREDO(a) \
1201 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001202
1203/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001204#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001205
1206/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001207#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001208
1209/*
1210 * Get the label for a given IPv4/IPv6 address.
1211 * RFC 6724, section 2.1.
1212 */
1213
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001214static int _get_label(const struct sockaddr* addr) {
1215 if (addr->sa_family == AF_INET) {
1216 return 4;
1217 } else if (addr->sa_family == AF_INET6) {
1218 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1219 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1220 return 0;
1221 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1222 return 4;
1223 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1224 return 2;
1225 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1226 return 5;
1227 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1228 return 13;
1229 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1230 return 3;
1231 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1232 return 11;
1233 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1234 return 12;
1235 } else {
1236 /* All other IPv6 addresses, including global unicast addresses. */
1237 return 1;
1238 }
1239 } else {
1240 /*
1241 * This should never happen.
1242 * Return a semi-random label as a last resort.
1243 */
1244 return 1;
1245 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001246}
1247
1248/*
1249 * Get the precedence for a given IPv4/IPv6 address.
1250 * RFC 6724, section 2.1.
1251 */
1252
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001253static int _get_precedence(const struct sockaddr* addr) {
1254 if (addr->sa_family == AF_INET) {
1255 return 35;
1256 } else if (addr->sa_family == AF_INET6) {
1257 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1258 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1259 return 50;
1260 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1261 return 35;
1262 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1263 return 30;
1264 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1265 return 5;
1266 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1267 return 3;
1268 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1269 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1270 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1271 return 1;
1272 } else {
1273 /* All other IPv6 addresses, including global unicast addresses. */
1274 return 40;
1275 }
1276 } else {
1277 return 1;
1278 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001279}
1280
1281/*
1282 * Find number of matching initial bits between the two addresses a1 and a2.
1283 */
1284
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001285static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1286 const char* p1 = (const char*) a1;
1287 const char* p2 = (const char*) a2;
1288 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001289
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001290 for (i = 0; i < sizeof(*a1); ++i) {
1291 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001292
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001293 if (p1[i] == p2[i]) {
1294 continue;
1295 }
1296 x = p1[i] ^ p2[i];
1297 for (j = 0; j < CHAR_BIT; ++j) {
1298 if (x & (1 << (CHAR_BIT - 1))) {
1299 return i * CHAR_BIT + j;
1300 }
1301 x <<= 1;
1302 }
1303 }
1304 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001305}
1306
1307/*
1308 * Compare two source/destination address pairs.
1309 * RFC 6724, section 6.
1310 */
1311
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001312static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1313 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1314 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1315 int scope_src1, scope_dst1, scope_match1;
1316 int scope_src2, scope_dst2, scope_match2;
1317 int label_src1, label_dst1, label_match1;
1318 int label_src2, label_dst2, label_match2;
1319 int precedence1, precedence2;
1320 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001321
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001322 /* Rule 1: Avoid unusable destinations. */
1323 if (a1->has_src_addr != a2->has_src_addr) {
1324 return a2->has_src_addr - a1->has_src_addr;
1325 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001326
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001327 /* Rule 2: Prefer matching scope. */
1328 scope_src1 = _get_scope(&a1->src_addr.generic);
1329 scope_dst1 = _get_scope(a1->ai->ai_addr);
1330 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001332 scope_src2 = _get_scope(&a2->src_addr.generic);
1333 scope_dst2 = _get_scope(a2->ai->ai_addr);
1334 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001335
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001336 if (scope_match1 != scope_match2) {
1337 return scope_match2 - scope_match1;
1338 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001339
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001340 /*
1341 * Rule 3: Avoid deprecated addresses.
1342 * TODO(sesse): We don't currently have a good way of finding this.
1343 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001344
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001345 /*
1346 * Rule 4: Prefer home addresses.
1347 * TODO(sesse): We don't currently have a good way of finding this.
1348 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001349
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001350 /* Rule 5: Prefer matching label. */
1351 label_src1 = _get_label(&a1->src_addr.generic);
1352 label_dst1 = _get_label(a1->ai->ai_addr);
1353 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001354
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001355 label_src2 = _get_label(&a2->src_addr.generic);
1356 label_dst2 = _get_label(a2->ai->ai_addr);
1357 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001358
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001359 if (label_match1 != label_match2) {
1360 return label_match2 - label_match1;
1361 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001362
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001363 /* Rule 6: Prefer higher precedence. */
1364 precedence1 = _get_precedence(a1->ai->ai_addr);
1365 precedence2 = _get_precedence(a2->ai->ai_addr);
1366 if (precedence1 != precedence2) {
1367 return precedence2 - precedence1;
1368 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001369
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001370 /*
1371 * Rule 7: Prefer native transport.
1372 * TODO(sesse): We don't currently have a good way of finding this.
1373 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001374
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001375 /* Rule 8: Prefer smaller scope. */
1376 if (scope_dst1 != scope_dst2) {
1377 return scope_dst1 - scope_dst2;
1378 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001379
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001380 /*
1381 * Rule 9: Use longest matching prefix.
1382 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1383 * to work very well directly applied to IPv4. (glibc uses information from
1384 * the routing table for a custom IPv4 implementation here.)
1385 */
1386 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1387 a2->ai->ai_addr->sa_family == AF_INET6) {
1388 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1389 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1390 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1391 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1392 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1393 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1394 if (prefixlen1 != prefixlen2) {
1395 return prefixlen2 - prefixlen1;
1396 }
1397 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 /*
1400 * Rule 10: Leave the order unchanged.
1401 * We need this since qsort() is not necessarily stable.
1402 */
1403 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001404}
1405
1406/*
1407 * Find the source address that will be used if trying to connect to the given
1408 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1409 *
1410 * Returns 1 if a source address was found, 0 if the address is unreachable,
1411 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1412 * undefined.
1413 */
1414
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001415static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1416 uid_t uid) {
1417 int sock;
1418 int ret;
1419 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001420
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001421 switch (addr->sa_family) {
1422 case AF_INET:
1423 len = sizeof(struct sockaddr_in);
1424 break;
1425 case AF_INET6:
1426 len = sizeof(struct sockaddr_in6);
1427 break;
1428 default:
1429 /* No known usable source address for non-INET families. */
1430 return 0;
1431 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001432
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001433 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1434 if (sock == -1) {
1435 if (errno == EAFNOSUPPORT) {
1436 return 0;
1437 } else {
1438 return -1;
1439 }
1440 }
1441 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1442 close(sock);
1443 return 0;
1444 }
1445 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1446 close(sock);
1447 return 0;
1448 }
1449 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001450 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001451 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001452
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001453 if (ret == -1) {
1454 close(sock);
1455 return 0;
1456 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001457
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001458 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1459 close(sock);
1460 return -1;
1461 }
1462 close(sock);
1463 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001464}
1465
1466/*
1467 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1468 * Will leave the list unchanged if an error occurs.
1469 */
1470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001471static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1472 struct addrinfo* cur;
1473 int nelem = 0, i;
1474 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001475
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001476 cur = list_sentinel->ai_next;
1477 while (cur) {
1478 ++nelem;
1479 cur = cur->ai_next;
1480 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001481
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001482 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1483 if (elems == NULL) {
1484 goto error;
1485 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001486
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001487 /*
1488 * Convert the linked list to an array that also contains the candidate
1489 * source address for each destination address.
1490 */
1491 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1492 int has_src_addr;
1493 assert(cur != NULL);
1494 elems[i].ai = cur;
1495 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001496
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001497 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1498 if (has_src_addr == -1) {
1499 goto error;
1500 }
1501 elems[i].has_src_addr = has_src_addr;
1502 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001503
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001504 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1505 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001506
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001507 list_sentinel->ai_next = elems[0].ai;
1508 for (i = 0; i < nelem - 1; ++i) {
1509 elems[i].ai->ai_next = elems[i + 1].ai;
1510 }
1511 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001512
1513error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001514 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001515}
1516
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001517static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1518 struct addrinfo* ai;
1519 querybuf *buf, *buf2;
1520 const char* name;
1521 const struct addrinfo* pai;
1522 struct addrinfo sentinel, *cur;
1523 struct res_target q, q2;
1524 res_state res;
1525 const struct android_net_context* netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001526
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001527 name = va_arg(ap, char*);
1528 pai = va_arg(ap, const struct addrinfo*);
1529 netcontext = va_arg(ap, const struct android_net_context*);
1530 // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001531
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001532 memset(&q, 0, sizeof(q));
1533 memset(&q2, 0, sizeof(q2));
1534 memset(&sentinel, 0, sizeof(sentinel));
1535 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001536
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001537 buf = malloc(sizeof(*buf));
1538 if (buf == NULL) {
1539 h_errno = NETDB_INTERNAL;
1540 return NS_NOTFOUND;
1541 }
1542 buf2 = malloc(sizeof(*buf2));
1543 if (buf2 == NULL) {
1544 free(buf);
1545 h_errno = NETDB_INTERNAL;
1546 return NS_NOTFOUND;
1547 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001548
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001549 switch (pai->ai_family) {
1550 case AF_UNSPEC:
1551 /* prefer IPv6 */
1552 q.name = name;
1553 q.qclass = C_IN;
1554 q.answer = buf->buf;
1555 q.anslen = sizeof(buf->buf);
1556 int query_ipv6 = 1, query_ipv4 = 1;
1557 if (pai->ai_flags & AI_ADDRCONFIG) {
1558 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1559 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1560 }
1561 if (query_ipv6) {
1562 q.qtype = T_AAAA;
1563 if (query_ipv4) {
1564 q.next = &q2;
1565 q2.name = name;
1566 q2.qclass = C_IN;
1567 q2.qtype = T_A;
1568 q2.answer = buf2->buf;
1569 q2.anslen = sizeof(buf2->buf);
1570 }
1571 } else if (query_ipv4) {
1572 q.qtype = T_A;
1573 } else {
1574 free(buf);
1575 free(buf2);
1576 return NS_NOTFOUND;
1577 }
1578 break;
1579 case AF_INET:
1580 q.name = name;
1581 q.qclass = C_IN;
1582 q.qtype = T_A;
1583 q.answer = buf->buf;
1584 q.anslen = sizeof(buf->buf);
1585 break;
1586 case AF_INET6:
1587 q.name = name;
1588 q.qclass = C_IN;
1589 q.qtype = T_AAAA;
1590 q.answer = buf->buf;
1591 q.anslen = sizeof(buf->buf);
1592 break;
1593 default:
1594 free(buf);
1595 free(buf2);
1596 return NS_UNAVAIL;
1597 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001598
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001599 res = __res_get_state();
1600 if (res == NULL) {
1601 free(buf);
1602 free(buf2);
1603 return NS_NOTFOUND;
1604 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 /* this just sets our netid val in the thread private data so we don't have to
1607 * modify the api's all the way down to res_send.c's res_nsend. We could
1608 * fully populate the thread private data here, but if we get down there
1609 * and have a cache hit that would be wasted, so we do the rest there on miss
1610 */
1611 res_setnetcontext(res, netcontext);
1612 if (res_searchN(name, &q, res) < 0) {
1613 __res_put_state(res);
1614 free(buf);
1615 free(buf2);
1616 return NS_NOTFOUND;
1617 }
1618 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1619 if (ai) {
1620 cur->ai_next = ai;
1621 while (cur && cur->ai_next) cur = cur->ai_next;
1622 }
1623 if (q.next) {
1624 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1625 if (ai) cur->ai_next = ai;
1626 }
1627 free(buf);
1628 free(buf2);
1629 if (sentinel.ai_next == NULL) {
1630 __res_put_state(res);
1631 switch (h_errno) {
1632 case HOST_NOT_FOUND:
1633 return NS_NOTFOUND;
1634 case TRY_AGAIN:
1635 return NS_TRYAGAIN;
1636 default:
1637 return NS_UNAVAIL;
1638 }
1639 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001641 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001642
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001643 __res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001644
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001645 *((struct addrinfo**) rv) = sentinel.ai_next;
1646 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001647}
1648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001649static void _sethtent(FILE** hostf) {
1650 if (!*hostf)
1651 *hostf = fopen(_PATH_HOSTS, "re");
1652 else
1653 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001654}
1655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001656static void _endhtent(FILE** hostf) {
1657 if (*hostf) {
1658 (void) fclose(*hostf);
1659 *hostf = NULL;
1660 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001661}
1662
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001663static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1664 char* p;
1665 char *cp, *tname, *cname;
1666 struct addrinfo hints, *res0, *res;
1667 int error;
1668 const char* addr;
1669 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001670
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001671 // fprintf(stderr, "_gethtent() name = '%s'\n", name);
1672 assert(name != NULL);
1673 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001674
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001675 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1676again:
1677 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1678 if (*p == '#') goto again;
1679 if (!(cp = strpbrk(p, "#\n"))) goto again;
1680 *cp = '\0';
1681 if (!(cp = strpbrk(p, " \t"))) goto again;
1682 *cp++ = '\0';
1683 addr = p;
1684 /* if this is not something we're looking for, skip it. */
1685 cname = NULL;
1686 while (cp && *cp) {
1687 if (*cp == ' ' || *cp == '\t') {
1688 cp++;
1689 continue;
1690 }
1691 if (!cname) cname = cp;
1692 tname = cp;
1693 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1694 // fprintf(stderr, "\ttname = '%s'", tname);
1695 if (strcasecmp(name, tname) == 0) goto found;
1696 }
1697 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001698
1699found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001700 hints = *pai;
1701 hints.ai_flags = AI_NUMERICHOST;
1702 error = getaddrinfo(addr, NULL, &hints, &res0);
1703 if (error) goto again;
1704 for (res = res0; res; res = res->ai_next) {
1705 /* cover it up */
1706 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001708 if (pai->ai_flags & AI_CANONNAME) {
1709 if (get_canonname(pai, res, cname) != 0) {
1710 freeaddrinfo(res0);
1711 goto again;
1712 }
1713 }
1714 }
1715 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001716}
1717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001718static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1719 const char* name;
1720 const struct addrinfo* pai;
1721 struct addrinfo sentinel, *cur;
1722 struct addrinfo* p;
1723 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001725 name = va_arg(ap, char*);
1726 pai = va_arg(ap, struct addrinfo*);
Bernie Innocenti55864192018-08-30 04:05:20 +09001727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001728 // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
1729 memset(&sentinel, 0, sizeof(sentinel));
1730 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001732 _sethtent(&hostf);
1733 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1734 cur->ai_next = p;
1735 while (cur && cur->ai_next) cur = cur->ai_next;
1736 }
1737 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001739 *((struct addrinfo**) rv) = sentinel.ai_next;
1740 if (sentinel.ai_next == NULL) return NS_NOTFOUND;
1741 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001742}
1743
1744/* resolver logic */
1745
1746/*
1747 * Formulate a normal query, send, and await answer.
1748 * Returned answer is placed in supplied buffer "answer".
1749 * Perform preliminary check of answer, returning success only
1750 * if no error is indicated and the answer count is nonzero.
1751 * Return the size of the response on success, -1 on error.
1752 * Error number is left in h_errno.
1753 *
1754 * Caller must parse answer and determine whether it answers the question.
1755 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001756static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1757 res_state res) {
1758 u_char buf[MAXPACKET];
1759 HEADER* hp;
1760 int n;
1761 struct res_target* t;
1762 int rcode;
1763 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001764
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001765 assert(name != NULL);
1766 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001768 rcode = NOERROR;
1769 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001771 for (t = target; t; t = t->next) {
1772 int class, type;
1773 u_char* answer;
1774 int anslen;
1775 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001777 hp = (HEADER*) (void*) t->answer;
1778 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001780 again:
1781 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001782
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001783 /* make it easier... */
1784 class = t->qclass;
1785 type = t->qtype;
1786 answer = t->answer;
1787 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001788#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001789 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001790#endif
1791
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001792 n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001793 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1794 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1795 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001796 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001797#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001798 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001799#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001800 h_errno = NO_RECOVERY;
1801 return n;
1802 }
1803 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001805 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1806 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001807 /* if the query choked with EDNS0, retry without EDNS0 */
1808 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1809 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1810 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001811#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001812 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001813#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001814 goto again;
1815 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001816#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001817 if (res->options & RES_DEBUG)
1818 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001819#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001820 continue;
1821 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001822
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001823 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001825 t->n = n;
1826 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001828 if (ancount == 0) {
1829 switch (rcode) {
1830 case NXDOMAIN:
1831 h_errno = HOST_NOT_FOUND;
1832 break;
1833 case SERVFAIL:
1834 h_errno = TRY_AGAIN;
1835 break;
1836 case NOERROR:
1837 h_errno = NO_DATA;
1838 break;
1839 case FORMERR:
1840 case NOTIMP:
1841 case REFUSED:
1842 default:
1843 h_errno = NO_RECOVERY;
1844 break;
1845 }
1846 return -1;
1847 }
1848 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001849}
1850
1851/*
1852 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1853 * Return the size of the response on success, -1 on error.
1854 * If enabled, implement search rules until answer or unrecoverable failure
1855 * is detected. Error code, if any, is left in h_errno.
1856 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001857static int res_searchN(const char* name, struct res_target* target, res_state res) {
1858 const char *cp, *const *domain;
1859 HEADER* hp;
1860 u_int dots;
1861 int trailing_dot, ret, saved_herrno;
1862 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001863
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001864 assert(name != NULL);
1865 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001866
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001867 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001868
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001869 errno = 0;
1870 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1871 dots = 0;
1872 for (cp = name; *cp; cp++) dots += (*cp == '.');
1873 trailing_dot = 0;
1874 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001875
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001876 // fprintf(stderr, "res_searchN() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001877
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001878 /*
1879 * if there aren't any dots, it could be a user-level alias
1880 */
1881 if (!dots && (cp = __hostalias(name)) != NULL) {
1882 ret = res_queryN(cp, target, res);
1883 return ret;
1884 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001885
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001886 /*
1887 * If there are dots in the name already, let's just give it a try
1888 * 'as is'. The threshold can be set with the "ndots" option.
1889 */
1890 saved_herrno = -1;
1891 if (dots >= res->ndots) {
1892 ret = res_querydomainN(name, NULL, target, res);
1893 if (ret > 0) return (ret);
1894 saved_herrno = h_errno;
1895 tried_as_is++;
1896 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001897
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001898 /*
1899 * We do at least one level of search if
1900 * - there is no dot and RES_DEFNAME is set, or
1901 * - there is at least one dot, there is no trailing dot,
1902 * and RES_DNSRCH is set.
1903 */
1904 if ((!dots && (res->options & RES_DEFNAMES)) ||
1905 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1906 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001907
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001908 /* Unfortunately we need to set stuff up before
1909 * the domain stuff is tried. Will have a better
1910 * fix after thread pools are used.
1911 */
1912 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001913
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001914 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1915 ret = res_querydomainN(name, *domain, target, res);
1916 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001917
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001918 /*
1919 * If no server present, give up.
1920 * If name isn't found in this domain,
1921 * keep trying higher domains in the search list
1922 * (if that's enabled).
1923 * On a NO_DATA error, keep trying, otherwise
1924 * a wildcard entry of another type could keep us
1925 * from finding this entry higher in the domain.
1926 * If we get some other error (negative answer or
1927 * server failure), then stop searching up,
1928 * but try the input name below in case it's
1929 * fully-qualified.
1930 */
1931 if (errno == ECONNREFUSED) {
1932 h_errno = TRY_AGAIN;
1933 return -1;
1934 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001935
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001936 switch (h_errno) {
1937 case NO_DATA:
1938 got_nodata++;
1939 /* FALLTHROUGH */
1940 case HOST_NOT_FOUND:
1941 /* keep trying */
1942 break;
1943 case TRY_AGAIN:
1944 if (hp->rcode == SERVFAIL) {
1945 /* try next search element, if any */
1946 got_servfail++;
1947 break;
1948 }
1949 /* FALLTHROUGH */
1950 default:
1951 /* anything else implies that we're done */
1952 done++;
1953 }
1954 /*
1955 * if we got here for some reason other than DNSRCH,
1956 * we only wanted one iteration of the loop, so stop.
1957 */
1958 if (!(res->options & RES_DNSRCH)) done++;
1959 }
1960 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001961
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001962 /*
1963 * if we have not already tried the name "as is", do that now.
1964 * note that we do this regardless of how many dots were in the
1965 * name or whether it ends with a dot.
1966 */
1967 if (!tried_as_is) {
1968 ret = res_querydomainN(name, NULL, target, res);
1969 if (ret > 0) return ret;
1970 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001971
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001972 /*
1973 * if we got here, we didn't satisfy the search.
1974 * if we did an initial full query, return that query's h_errno
1975 * (note that we wouldn't be here if that query had succeeded).
1976 * else if we ever got a nodata, send that back as the reason.
1977 * else send back meaningless h_errno, that being the one from
1978 * the last DNSRCH we did.
1979 */
1980 if (saved_herrno != -1)
1981 h_errno = saved_herrno;
1982 else if (got_nodata)
1983 h_errno = NO_DATA;
1984 else if (got_servfail)
1985 h_errno = TRY_AGAIN;
1986 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001987}
1988
1989/*
1990 * Perform a call on res_query on the concatenation of name and domain,
1991 * removing a trailing dot from name if domain is NULL.
1992 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001993static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1994 res_state res) {
1995 char nbuf[MAXDNAME];
1996 const char* longname = nbuf;
1997 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001998
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001999 assert(name != NULL);
2000 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09002001
2002#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002003 if (res->options & RES_DEBUG)
2004 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09002005#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002006 if (domain == NULL) {
2007 /*
2008 * Check for trailing '.';
2009 * copy without '.' if present.
2010 */
2011 n = strlen(name);
2012 if (n + 1 > sizeof(nbuf)) {
2013 h_errno = NO_RECOVERY;
2014 return -1;
2015 }
2016 if (n > 0 && name[--n] == '.') {
2017 strncpy(nbuf, name, n);
2018 nbuf[n] = '\0';
2019 } else
2020 longname = name;
2021 } else {
2022 n = strlen(name);
2023 d = strlen(domain);
2024 if (n + 1 + d + 1 > sizeof(nbuf)) {
2025 h_errno = NO_RECOVERY;
2026 return -1;
2027 }
2028 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2029 }
2030 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09002031}