blob: d6912ecc7e8e188a39fca17af82baf34b9f7e1b6 [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 Innocenti1f4a9fd2018-09-07 21:10:25 +0900200static int ip6_str2scopeid(const 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 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900243 } while (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; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900250 } while (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; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900257 } while (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*/ \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900265 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900268 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
269#define MATCH(x, y, w) ((x) == (y) || ((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
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900522// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900523static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
524 struct addrinfo** res, const struct android_net_context* netcontext) {
525 struct addrinfo* result;
526 struct addrinfo* cur;
527 int error = 0;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900528 static const ns_dtab dtab[] = {
529 {NSSRC_FILES, _files_getaddrinfo, NULL},
530 {NSSRC_DNS, _dns_getaddrinfo, NULL},
531 {0, 0, 0}
532 };
Bernie Innocenti55864192018-08-30 04:05:20 +0900533
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900534 assert(pai != NULL);
535 /* hostname may be NULL */
536 /* servname may be NULL */
537 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900538
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900539 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900540
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541 /*
542 * if the servname does not match socktype/protocol, ignore it.
543 */
544 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai,
547 netcontext)) {
548 case NS_TRYAGAIN:
549 error = EAI_AGAIN;
550 goto free;
551 case NS_UNAVAIL:
552 error = EAI_FAIL;
553 goto free;
554 case NS_NOTFOUND:
555 error = EAI_NODATA;
556 goto free;
557 case NS_SUCCESS:
558 error = 0;
559 for (cur = result; cur; cur = cur->ai_next) {
560 GET_PORT(cur, servname);
561 /* canonname should be filled already */
562 }
563 break;
564 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900565
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900566 *res = result;
Bernie Innocenti55864192018-08-30 04:05:20 +0900567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900569
570free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 if (result) freeaddrinfo(result);
572 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900573}
574
575/*
576 * hostname == NULL.
577 * passive socket -> anyaddr (0.0.0.0 or ::)
578 * non-passive socket -> localhost (127.0.0.1 or ::1)
579 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900580static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
581 int s;
582 const struct afd* afd;
583 struct addrinfo* cur;
584 struct addrinfo sentinel;
585 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900586
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900587 assert(pai != NULL);
588 /* servname may be NULL */
589 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900590
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 *res = NULL;
592 sentinel.ai_next = NULL;
593 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900594
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900595 /*
596 * filter out AFs that are not supported by the kernel
597 * XXX errno?
598 */
599 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
600 if (s < 0) {
601 if (errno != EMFILE) return 0;
602 } else
603 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 /*
606 * if the servname does not match socktype/protocol, ignore it.
607 */
608 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900609
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 afd = find_afd(pai->ai_family);
611 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900612
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900613 if (pai->ai_flags & AI_PASSIVE) {
614 GET_AI(cur->ai_next, afd, afd->a_addrany);
615 /* xxx meaningless?
616 * GET_CANONNAME(cur->ai_next, "anyaddr");
617 */
618 GET_PORT(cur->ai_next, servname);
619 } else {
620 GET_AI(cur->ai_next, afd, afd->a_loopback);
621 /* xxx meaningless?
622 * GET_CANONNAME(cur->ai_next, "localhost");
623 */
624 GET_PORT(cur->ai_next, servname);
625 }
626 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628 *res = sentinel.ai_next;
629 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900630
631free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900632 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
633 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900634}
635
636/*
637 * numeric hostname
638 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
640 struct addrinfo** res, const char* canonname) {
641 const struct afd* afd;
642 struct addrinfo* cur;
643 struct addrinfo sentinel;
644 int error;
645 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900646
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900647 assert(pai != NULL);
648 /* hostname may be NULL */
649 /* servname may be NULL */
650 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900651
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900652 *res = NULL;
653 sentinel.ai_next = NULL;
654 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 /*
657 * if the servname does not match socktype/protocol, ignore it.
658 */
659 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661 afd = find_afd(pai->ai_family);
662 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900663
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900665#if 0 /*X/Open spec*/
666 case AF_INET:
667 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
668 if (pai->ai_family == afd->a_af ||
669 pai->ai_family == PF_UNSPEC /*?*/) {
670 GET_AI(cur->ai_next, afd, pton);
671 GET_PORT(cur->ai_next, servname);
672 if ((pai->ai_flags & AI_CANONNAME)) {
673 /*
674 * Set the numeric address itself as
675 * the canonical name, based on a
676 * clarification in rfc2553bis-03.
677 */
678 GET_CANONNAME(cur->ai_next, canonname);
679 }
680 while (cur && cur->ai_next)
681 cur = cur->ai_next;
682 } else
683 ERR(EAI_FAMILY); /*xxx*/
684 }
685 break;
686#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687 default:
688 if (inet_pton(afd->a_af, hostname, pton) == 1) {
689 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
690 GET_AI(cur->ai_next, afd, pton);
691 GET_PORT(cur->ai_next, servname);
692 if ((pai->ai_flags & AI_CANONNAME)) {
693 /*
694 * Set the numeric address itself as
695 * the canonical name, based on a
696 * clarification in rfc2553bis-03.
697 */
698 GET_CANONNAME(cur->ai_next, canonname);
699 }
700 while (cur->ai_next) cur = cur->ai_next;
701 } else
702 ERR(EAI_FAMILY); /*xxx*/
703 }
704 break;
705 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900706
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900707 *res = sentinel.ai_next;
708 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900709
710free:
711bad:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900712 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
713 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900714}
715
716/*
717 * numeric hostname with scope
718 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
720 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721 const struct afd* afd;
722 struct addrinfo* cur;
723 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900724 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900727 assert(pai != NULL);
728 /* hostname may be NULL */
729 /* servname may be NULL */
730 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732 /*
733 * if the servname does not match socktype/protocol, ignore it.
734 */
735 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900737 afd = find_afd(pai->ai_family);
738 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900739
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900740 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 cp = strchr(hostname, SCOPE_DELIMITER);
743 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900745 /*
746 * Handle special case of <scoped_address><delimiter><scope id>
747 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900748 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900749 if (hostname2 == NULL) return EAI_MEMORY;
750 /* terminate at the delimiter */
751 hostname2[cp - hostname] = '\0';
752 addr = hostname2;
753 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900754
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900755 error = explore_numeric(pai, addr, servname, res, hostname);
756 if (error == 0) {
757 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900758
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900759 for (cur = *res; cur; cur = cur->ai_next) {
760 if (cur->ai_family != AF_INET6) continue;
761 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
762 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
763 free(hostname2);
764 return (EAI_NODATA); /* XXX: is return OK? */
765 }
766 sin6->sin6_scope_id = scopeid;
767 }
768 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900769
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900770 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900772 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900773}
774
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900775static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
776 assert(pai != NULL);
777 assert(ai != NULL);
778 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900780 if ((pai->ai_flags & AI_CANONNAME) != 0) {
781 ai->ai_canonname = strdup(str);
782 if (ai->ai_canonname == NULL) return EAI_MEMORY;
783 }
784 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900785}
786
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900787static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
788 const char* addr) {
789 char* p;
790 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900791
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900792 assert(pai != NULL);
793 assert(afd != NULL);
794 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900795
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900796 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
797 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900798
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 memcpy(ai, pai, sizeof(struct addrinfo));
800 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
801 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900802
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900803 ai->ai_addrlen = afd->a_socklen;
804#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
805 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900806#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
808 p = (char*) (void*) (ai->ai_addr);
809 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
810 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900811}
812
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900813static int get_portmatch(const struct addrinfo* ai, const char* servname) {
814 assert(ai != NULL);
815 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900816
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900817 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900818}
819
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
821 const char* proto;
822 struct servent* sp;
823 int port;
824 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900825
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900826 assert(ai != NULL);
827 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900828
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 if (servname == NULL) return 0;
830 switch (ai->ai_family) {
831 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900832 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900833 break;
834 default:
835 return 0;
836 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900837
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900838 switch (ai->ai_socktype) {
839 case SOCK_RAW:
840 return EAI_SERVICE;
841 case SOCK_DGRAM:
842 case SOCK_STREAM:
843 allownumeric = 1;
844 break;
845 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 break;
848 default:
849 return EAI_SOCKTYPE;
850 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900852 port = str2number(servname);
853 if (port >= 0) {
854 if (!allownumeric) return EAI_SERVICE;
855 if (port < 0 || port > 65535) return EAI_SERVICE;
856 port = htons(port);
857 } else {
858 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900859
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900860 switch (ai->ai_socktype) {
861 case SOCK_DGRAM:
862 proto = "udp";
863 break;
864 case SOCK_STREAM:
865 proto = "tcp";
866 break;
867 default:
868 proto = NULL;
869 break;
870 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900871
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900872 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
873 port = sp->s_port;
874 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900875
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 if (!matchonly) {
877 switch (ai->ai_family) {
878 case AF_INET:
879 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
880 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881 case AF_INET6:
882 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
883 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900884 }
885 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900886
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900887 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900888}
889
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900890static const struct afd* find_afd(int af) {
891 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900892
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900893 if (af == PF_UNSPEC) return NULL;
894 for (afd = afdl; afd->a_af; afd++) {
895 if (afd->a_af == af) return afd;
896 }
897 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900898}
899
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900900// Convert a string to a scope identifier.
901static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900902 u_long lscopeid;
903 struct in6_addr* a6;
904 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900905
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 assert(scope != NULL);
907 assert(sin6 != NULL);
908 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900909
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900911
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912 /* empty scopeid portion is invalid */
913 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900914
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900915 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
916 /*
917 * We currently assume a one-to-one mapping between links
918 * and interfaces, so we simply use interface indices for
919 * like-local scopes.
920 */
921 *scopeid = if_nametoindex(scope);
922 if (*scopeid == 0) goto trynumeric;
923 return 0;
924 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900925
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900926 /* still unclear about literal, allow numeric only - placeholder */
927 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
928 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
929 goto trynumeric;
930 else
931 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900932
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900933 /* try to convert to a numeric id as a last resort */
934trynumeric:
935 errno = 0;
936 lscopeid = strtoul(scope, &ep, 10);
937 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
938 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
939 return 0;
940 else
941 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900942}
Bernie Innocenti55864192018-08-30 04:05:20 +0900943
944/* code duplicate with gethnamaddr.c */
945
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900946static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900947
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900948#define BOUNDED_INCR(x) \
949 do { \
950 BOUNDS_CHECK(cp, x); \
951 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900952 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900953
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900954#define BOUNDS_CHECK(ptr, count) \
955 do { \
956 if (eom - (ptr) < (count)) { \
957 h_errno = NO_RECOVERY; \
958 return NULL; \
959 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900960 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900961
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900962static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
963 const struct addrinfo* pai) {
964 struct addrinfo sentinel, *cur;
965 struct addrinfo ai;
966 const struct afd* afd;
967 char* canonname;
968 const HEADER* hp;
969 const u_char* cp;
970 int n;
971 const u_char* eom;
972 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900973 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900974 int haveanswer, had_error;
975 char tbuf[MAXDNAME];
976 int (*name_ok)(const char*);
977 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900978
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900979 assert(answer != NULL);
980 assert(qname != NULL);
981 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900982
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900983 memset(&sentinel, 0, sizeof(sentinel));
984 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900985
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900986 canonname = NULL;
987 eom = answer->buf + anslen;
988 switch (qtype) {
989 case T_A:
990 case T_AAAA:
991 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
992 name_ok = res_hnok;
993 break;
994 default:
995 return NULL; /* XXX should be abort(); */
996 }
997 /*
998 * find first satisfactory answer
999 */
1000 hp = &answer->hdr;
1001 ancount = ntohs(hp->ancount);
1002 qdcount = ntohs(hp->qdcount);
1003 bp = hostbuf;
1004 ep = hostbuf + sizeof hostbuf;
1005 cp = answer->buf;
1006 BOUNDED_INCR(HFIXEDSZ);
1007 if (qdcount != 1) {
1008 h_errno = NO_RECOVERY;
1009 return (NULL);
1010 }
1011 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1012 if ((n < 0) || !(*name_ok)(bp)) {
1013 h_errno = NO_RECOVERY;
1014 return (NULL);
1015 }
1016 BOUNDED_INCR(n + QFIXEDSZ);
1017 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1018 /* res_send() has already verified that the query name is the
1019 * same as the one we sent; this just gets the expanded name
1020 * (i.e., with the succeeding search-domain tacked on).
1021 */
1022 n = strlen(bp) + 1; /* for the \0 */
1023 if (n >= MAXHOSTNAMELEN) {
1024 h_errno = NO_RECOVERY;
1025 return (NULL);
1026 }
1027 canonname = bp;
1028 bp += n;
1029 /* The qname can be abbreviated, but h_name is now absolute. */
1030 qname = canonname;
1031 }
1032 haveanswer = 0;
1033 had_error = 0;
1034 while (ancount-- > 0 && cp < eom && !had_error) {
1035 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1036 if ((n < 0) || !(*name_ok)(bp)) {
1037 had_error++;
1038 continue;
1039 }
1040 cp += n; /* name */
1041 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1042 type = _getshort(cp);
1043 cp += INT16SZ; /* type */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001044 int cl = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001045 cp += INT16SZ + INT32SZ; /* class, TTL */
1046 n = _getshort(cp);
1047 cp += INT16SZ; /* len */
1048 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001049 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001050 /* XXX - debug? syslog? */
1051 cp += n;
1052 continue; /* XXX - had_error++ ? */
1053 }
1054 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1055 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1056 if ((n < 0) || !(*name_ok)(tbuf)) {
1057 had_error++;
1058 continue;
1059 }
1060 cp += n;
1061 /* Get canonical name. */
1062 n = strlen(tbuf) + 1; /* for the \0 */
1063 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1064 had_error++;
1065 continue;
1066 }
1067 strlcpy(bp, tbuf, (size_t)(ep - bp));
1068 canonname = bp;
1069 bp += n;
1070 continue;
1071 }
1072 if (qtype == T_ANY) {
1073 if (!(type == T_A || type == T_AAAA)) {
1074 cp += n;
1075 continue;
1076 }
1077 } else if (type != qtype) {
1078 if (type != T_KEY && type != T_SIG)
1079 syslog(LOG_NOTICE | LOG_AUTH,
1080 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1081 p_class(C_IN), p_type(qtype), p_type(type));
1082 cp += n;
1083 continue; /* XXX - had_error++ ? */
1084 }
1085 switch (type) {
1086 case T_A:
1087 case T_AAAA:
1088 if (strcasecmp(canonname, bp) != 0) {
1089 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1090 cp += n;
1091 continue; /* XXX - had_error++ ? */
1092 }
1093 if (type == T_A && n != INADDRSZ) {
1094 cp += n;
1095 continue;
1096 }
1097 if (type == T_AAAA && n != IN6ADDRSZ) {
1098 cp += n;
1099 continue;
1100 }
1101 if (type == T_AAAA) {
1102 struct in6_addr in6;
1103 memcpy(&in6, cp, IN6ADDRSZ);
1104 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1105 cp += n;
1106 continue;
1107 }
1108 }
1109 if (!haveanswer) {
1110 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001111
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001112 canonname = bp;
1113 nn = strlen(bp) + 1; /* for the \0 */
1114 bp += nn;
1115 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001116
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001117 /* don't overwrite pai */
1118 ai = *pai;
1119 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1120 afd = find_afd(ai.ai_family);
1121 if (afd == NULL) {
1122 cp += n;
1123 continue;
1124 }
1125 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1126 if (cur->ai_next == NULL) had_error++;
1127 while (cur && cur->ai_next) cur = cur->ai_next;
1128 cp += n;
1129 break;
1130 default:
1131 abort();
1132 }
1133 if (!had_error) haveanswer++;
1134 }
1135 if (haveanswer) {
1136 if (!canonname)
1137 (void) get_canonname(pai, sentinel.ai_next, qname);
1138 else
1139 (void) get_canonname(pai, sentinel.ai_next, canonname);
1140 h_errno = NETDB_SUCCESS;
1141 return sentinel.ai_next;
1142 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001143
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001144 h_errno = NO_RECOVERY;
1145 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001146}
1147
1148struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001149 struct addrinfo* ai;
1150 int has_src_addr;
1151 sockaddr_union src_addr;
1152 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001153};
1154
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001155static int _get_scope(const struct sockaddr* addr) {
1156 if (addr->sa_family == AF_INET6) {
1157 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1158 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1159 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1160 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1161 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1162 /*
1163 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1164 * link-local scope.
1165 */
1166 return IPV6_ADDR_SCOPE_LINKLOCAL;
1167 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1168 return IPV6_ADDR_SCOPE_SITELOCAL;
1169 } else {
1170 return IPV6_ADDR_SCOPE_GLOBAL;
1171 }
1172 } else if (addr->sa_family == AF_INET) {
1173 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1174 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001176 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1177 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1178 return IPV6_ADDR_SCOPE_LINKLOCAL;
1179 } else {
1180 /*
1181 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1182 * and shared addresses (100.64.0.0/10), are assigned global scope.
1183 */
1184 return IPV6_ADDR_SCOPE_GLOBAL;
1185 }
1186 } else {
1187 /*
1188 * This should never happen.
1189 * Return a scope with low priority as a last resort.
1190 */
1191 return IPV6_ADDR_SCOPE_NODELOCAL;
1192 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001193}
1194
1195/* These macros are modelled after the ones in <netinet/in6.h>. */
1196
1197/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001198#define IN6_IS_ADDR_TEREDO(a) \
1199 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001200
1201/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001202#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001203
1204/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001205#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001206
1207/*
1208 * Get the label for a given IPv4/IPv6 address.
1209 * RFC 6724, section 2.1.
1210 */
1211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001212static int _get_label(const struct sockaddr* addr) {
1213 if (addr->sa_family == AF_INET) {
1214 return 4;
1215 } else if (addr->sa_family == AF_INET6) {
1216 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1217 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1218 return 0;
1219 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1220 return 4;
1221 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1222 return 2;
1223 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1224 return 5;
1225 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1226 return 13;
1227 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1228 return 3;
1229 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1230 return 11;
1231 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1232 return 12;
1233 } else {
1234 /* All other IPv6 addresses, including global unicast addresses. */
1235 return 1;
1236 }
1237 } else {
1238 /*
1239 * This should never happen.
1240 * Return a semi-random label as a last resort.
1241 */
1242 return 1;
1243 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001244}
1245
1246/*
1247 * Get the precedence for a given IPv4/IPv6 address.
1248 * RFC 6724, section 2.1.
1249 */
1250
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001251static int _get_precedence(const struct sockaddr* addr) {
1252 if (addr->sa_family == AF_INET) {
1253 return 35;
1254 } else if (addr->sa_family == AF_INET6) {
1255 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1256 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1257 return 50;
1258 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1259 return 35;
1260 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1261 return 30;
1262 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1263 return 5;
1264 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1265 return 3;
1266 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1267 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1268 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1269 return 1;
1270 } else {
1271 /* All other IPv6 addresses, including global unicast addresses. */
1272 return 40;
1273 }
1274 } else {
1275 return 1;
1276 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001277}
1278
1279/*
1280 * Find number of matching initial bits between the two addresses a1 and a2.
1281 */
1282
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001283static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1284 const char* p1 = (const char*) a1;
1285 const char* p2 = (const char*) a2;
1286 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001287
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001288 for (i = 0; i < sizeof(*a1); ++i) {
1289 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001290
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001291 if (p1[i] == p2[i]) {
1292 continue;
1293 }
1294 x = p1[i] ^ p2[i];
1295 for (j = 0; j < CHAR_BIT; ++j) {
1296 if (x & (1 << (CHAR_BIT - 1))) {
1297 return i * CHAR_BIT + j;
1298 }
1299 x <<= 1;
1300 }
1301 }
1302 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001303}
1304
1305/*
1306 * Compare two source/destination address pairs.
1307 * RFC 6724, section 6.
1308 */
1309
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001310static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1311 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1312 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1313 int scope_src1, scope_dst1, scope_match1;
1314 int scope_src2, scope_dst2, scope_match2;
1315 int label_src1, label_dst1, label_match1;
1316 int label_src2, label_dst2, label_match2;
1317 int precedence1, precedence2;
1318 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001319
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001320 /* Rule 1: Avoid unusable destinations. */
1321 if (a1->has_src_addr != a2->has_src_addr) {
1322 return a2->has_src_addr - a1->has_src_addr;
1323 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001324
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001325 /* Rule 2: Prefer matching scope. */
1326 scope_src1 = _get_scope(&a1->src_addr.generic);
1327 scope_dst1 = _get_scope(a1->ai->ai_addr);
1328 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001329
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001330 scope_src2 = _get_scope(&a2->src_addr.generic);
1331 scope_dst2 = _get_scope(a2->ai->ai_addr);
1332 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001333
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001334 if (scope_match1 != scope_match2) {
1335 return scope_match2 - scope_match1;
1336 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001337
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001338 /*
1339 * Rule 3: Avoid deprecated addresses.
1340 * TODO(sesse): We don't currently have a good way of finding this.
1341 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001342
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001343 /*
1344 * Rule 4: Prefer home addresses.
1345 * TODO(sesse): We don't currently have a good way of finding this.
1346 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001347
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001348 /* Rule 5: Prefer matching label. */
1349 label_src1 = _get_label(&a1->src_addr.generic);
1350 label_dst1 = _get_label(a1->ai->ai_addr);
1351 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001352
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001353 label_src2 = _get_label(&a2->src_addr.generic);
1354 label_dst2 = _get_label(a2->ai->ai_addr);
1355 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001356
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001357 if (label_match1 != label_match2) {
1358 return label_match2 - label_match1;
1359 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001360
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001361 /* Rule 6: Prefer higher precedence. */
1362 precedence1 = _get_precedence(a1->ai->ai_addr);
1363 precedence2 = _get_precedence(a2->ai->ai_addr);
1364 if (precedence1 != precedence2) {
1365 return precedence2 - precedence1;
1366 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001367
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001368 /*
1369 * Rule 7: Prefer native transport.
1370 * TODO(sesse): We don't currently have a good way of finding this.
1371 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001372
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001373 /* Rule 8: Prefer smaller scope. */
1374 if (scope_dst1 != scope_dst2) {
1375 return scope_dst1 - scope_dst2;
1376 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001377
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001378 /*
1379 * Rule 9: Use longest matching prefix.
1380 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1381 * to work very well directly applied to IPv4. (glibc uses information from
1382 * the routing table for a custom IPv4 implementation here.)
1383 */
1384 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1385 a2->ai->ai_addr->sa_family == AF_INET6) {
1386 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1387 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1388 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1389 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1390 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1391 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1392 if (prefixlen1 != prefixlen2) {
1393 return prefixlen2 - prefixlen1;
1394 }
1395 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001396
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001397 /*
1398 * Rule 10: Leave the order unchanged.
1399 * We need this since qsort() is not necessarily stable.
1400 */
1401 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001402}
1403
1404/*
1405 * Find the source address that will be used if trying to connect to the given
1406 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1407 *
1408 * Returns 1 if a source address was found, 0 if the address is unreachable,
1409 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1410 * undefined.
1411 */
1412
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001413static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1414 uid_t uid) {
1415 int sock;
1416 int ret;
1417 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001419 switch (addr->sa_family) {
1420 case AF_INET:
1421 len = sizeof(struct sockaddr_in);
1422 break;
1423 case AF_INET6:
1424 len = sizeof(struct sockaddr_in6);
1425 break;
1426 default:
1427 /* No known usable source address for non-INET families. */
1428 return 0;
1429 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001430
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001431 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1432 if (sock == -1) {
1433 if (errno == EAFNOSUPPORT) {
1434 return 0;
1435 } else {
1436 return -1;
1437 }
1438 }
1439 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1440 close(sock);
1441 return 0;
1442 }
1443 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1444 close(sock);
1445 return 0;
1446 }
1447 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001448 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001449 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001450
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001451 if (ret == -1) {
1452 close(sock);
1453 return 0;
1454 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001455
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001456 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1457 close(sock);
1458 return -1;
1459 }
1460 close(sock);
1461 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001462}
1463
1464/*
1465 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1466 * Will leave the list unchanged if an error occurs.
1467 */
1468
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001469static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1470 struct addrinfo* cur;
1471 int nelem = 0, i;
1472 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001473
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001474 cur = list_sentinel->ai_next;
1475 while (cur) {
1476 ++nelem;
1477 cur = cur->ai_next;
1478 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001479
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001480 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1481 if (elems == NULL) {
1482 goto error;
1483 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001485 /*
1486 * Convert the linked list to an array that also contains the candidate
1487 * source address for each destination address.
1488 */
1489 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1490 int has_src_addr;
1491 assert(cur != NULL);
1492 elems[i].ai = cur;
1493 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001494
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001495 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1496 if (has_src_addr == -1) {
1497 goto error;
1498 }
1499 elems[i].has_src_addr = has_src_addr;
1500 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001501
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001502 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1503 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001504
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001505 list_sentinel->ai_next = elems[0].ai;
1506 for (i = 0; i < nelem - 1; ++i) {
1507 elems[i].ai->ai_next = elems[i + 1].ai;
1508 }
1509 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001510
1511error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001512 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001513}
1514
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001515static int _dns_getaddrinfo(void* rv, void* /*cb_data*/, va_list ap) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001516 struct addrinfo* ai;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001517 const char* name;
1518 const struct addrinfo* pai;
1519 struct addrinfo sentinel, *cur;
1520 struct res_target q, q2;
1521 res_state res;
1522 const struct android_net_context* netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001523
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001524 name = va_arg(ap, char*);
1525 pai = va_arg(ap, const struct addrinfo*);
1526 netcontext = va_arg(ap, const struct android_net_context*);
1527 // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001528
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001529 memset(&q, 0, sizeof(q));
1530 memset(&q2, 0, sizeof(q2));
1531 memset(&sentinel, 0, sizeof(sentinel));
1532 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001533
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001534 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001535 if (buf == NULL) {
1536 h_errno = NETDB_INTERNAL;
1537 return NS_NOTFOUND;
1538 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001539 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001540 if (buf2 == NULL) {
1541 free(buf);
1542 h_errno = NETDB_INTERNAL;
1543 return NS_NOTFOUND;
1544 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001546 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001547 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001548 /* prefer IPv6 */
1549 q.name = name;
1550 q.qclass = C_IN;
1551 q.answer = buf->buf;
1552 q.anslen = sizeof(buf->buf);
1553 int query_ipv6 = 1, query_ipv4 = 1;
1554 if (pai->ai_flags & AI_ADDRCONFIG) {
1555 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1556 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1557 }
1558 if (query_ipv6) {
1559 q.qtype = T_AAAA;
1560 if (query_ipv4) {
1561 q.next = &q2;
1562 q2.name = name;
1563 q2.qclass = C_IN;
1564 q2.qtype = T_A;
1565 q2.answer = buf2->buf;
1566 q2.anslen = sizeof(buf2->buf);
1567 }
1568 } else if (query_ipv4) {
1569 q.qtype = T_A;
1570 } else {
1571 free(buf);
1572 free(buf2);
1573 return NS_NOTFOUND;
1574 }
1575 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001576 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001577 case AF_INET:
1578 q.name = name;
1579 q.qclass = C_IN;
1580 q.qtype = T_A;
1581 q.answer = buf->buf;
1582 q.anslen = sizeof(buf->buf);
1583 break;
1584 case AF_INET6:
1585 q.name = name;
1586 q.qclass = C_IN;
1587 q.qtype = T_AAAA;
1588 q.answer = buf->buf;
1589 q.anslen = sizeof(buf->buf);
1590 break;
1591 default:
1592 free(buf);
1593 free(buf2);
1594 return NS_UNAVAIL;
1595 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001597 res = __res_get_state();
1598 if (res == NULL) {
1599 free(buf);
1600 free(buf2);
1601 return NS_NOTFOUND;
1602 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001603
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001604 /* this just sets our netid val in the thread private data so we don't have to
1605 * modify the api's all the way down to res_send.c's res_nsend. We could
1606 * fully populate the thread private data here, but if we get down there
1607 * and have a cache hit that would be wasted, so we do the rest there on miss
1608 */
1609 res_setnetcontext(res, netcontext);
1610 if (res_searchN(name, &q, res) < 0) {
1611 __res_put_state(res);
1612 free(buf);
1613 free(buf2);
1614 return NS_NOTFOUND;
1615 }
1616 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1617 if (ai) {
1618 cur->ai_next = ai;
1619 while (cur && cur->ai_next) cur = cur->ai_next;
1620 }
1621 if (q.next) {
1622 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1623 if (ai) cur->ai_next = ai;
1624 }
1625 free(buf);
1626 free(buf2);
1627 if (sentinel.ai_next == NULL) {
1628 __res_put_state(res);
1629 switch (h_errno) {
1630 case HOST_NOT_FOUND:
1631 return NS_NOTFOUND;
1632 case TRY_AGAIN:
1633 return NS_TRYAGAIN;
1634 default:
1635 return NS_UNAVAIL;
1636 }
1637 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001639 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001641 __res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001642
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001643 *((struct addrinfo**) rv) = sentinel.ai_next;
1644 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001645}
1646
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001647static void _sethtent(FILE** hostf) {
1648 if (!*hostf)
1649 *hostf = fopen(_PATH_HOSTS, "re");
1650 else
1651 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001652}
1653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001654static void _endhtent(FILE** hostf) {
1655 if (*hostf) {
1656 (void) fclose(*hostf);
1657 *hostf = NULL;
1658 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001659}
1660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001661static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1662 char* p;
1663 char *cp, *tname, *cname;
1664 struct addrinfo hints, *res0, *res;
1665 int error;
1666 const char* addr;
1667 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001669 // fprintf(stderr, "_gethtent() name = '%s'\n", name);
1670 assert(name != NULL);
1671 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001673 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1674again:
1675 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1676 if (*p == '#') goto again;
1677 if (!(cp = strpbrk(p, "#\n"))) goto again;
1678 *cp = '\0';
1679 if (!(cp = strpbrk(p, " \t"))) goto again;
1680 *cp++ = '\0';
1681 addr = p;
1682 /* if this is not something we're looking for, skip it. */
1683 cname = NULL;
1684 while (cp && *cp) {
1685 if (*cp == ' ' || *cp == '\t') {
1686 cp++;
1687 continue;
1688 }
1689 if (!cname) cname = cp;
1690 tname = cp;
1691 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1692 // fprintf(stderr, "\ttname = '%s'", tname);
1693 if (strcasecmp(name, tname) == 0) goto found;
1694 }
1695 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001696
1697found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001698 hints = *pai;
1699 hints.ai_flags = AI_NUMERICHOST;
1700 error = getaddrinfo(addr, NULL, &hints, &res0);
1701 if (error) goto again;
1702 for (res = res0; res; res = res->ai_next) {
1703 /* cover it up */
1704 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001705
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001706 if (pai->ai_flags & AI_CANONNAME) {
1707 if (get_canonname(pai, res, cname) != 0) {
1708 freeaddrinfo(res0);
1709 goto again;
1710 }
1711 }
1712 }
1713 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001714}
1715
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001716static int _files_getaddrinfo(void* rv, void* /*cb_data*/, va_list ap) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001717 const char* name;
1718 const struct addrinfo* pai;
1719 struct addrinfo sentinel, *cur;
1720 struct addrinfo* p;
1721 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 name = va_arg(ap, char*);
1724 pai = va_arg(ap, struct addrinfo*);
Bernie Innocenti55864192018-08-30 04:05:20 +09001725
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001726 // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
1727 memset(&sentinel, 0, sizeof(sentinel));
1728 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001729
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001730 _sethtent(&hostf);
1731 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1732 cur->ai_next = p;
1733 while (cur && cur->ai_next) cur = cur->ai_next;
1734 }
1735 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001737 *((struct addrinfo**) rv) = sentinel.ai_next;
1738 if (sentinel.ai_next == NULL) return NS_NOTFOUND;
1739 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001740}
1741
1742/* resolver logic */
1743
1744/*
1745 * Formulate a normal query, send, and await answer.
1746 * Returned answer is placed in supplied buffer "answer".
1747 * Perform preliminary check of answer, returning success only
1748 * if no error is indicated and the answer count is nonzero.
1749 * Return the size of the response on success, -1 on error.
1750 * Error number is left in h_errno.
1751 *
1752 * Caller must parse answer and determine whether it answers the question.
1753 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001754static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1755 res_state res) {
1756 u_char buf[MAXPACKET];
1757 HEADER* hp;
1758 int n;
1759 struct res_target* t;
1760 int rcode;
1761 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001762
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001763 assert(name != NULL);
1764 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001765
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001766 rcode = NOERROR;
1767 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001768
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001769 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001770 u_char* answer;
1771 int anslen;
1772 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001773
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001774 hp = (HEADER*) (void*) t->answer;
1775 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001777 again:
1778 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001780 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001781 int cl = t->qclass;
1782 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001783 answer = t->answer;
1784 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001785#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001786 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001787#endif
1788
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001789 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001790 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1791 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1792 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001793 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001794#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001795 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001796#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001797 h_errno = NO_RECOVERY;
1798 return n;
1799 }
1800 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001801
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001802 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1803 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001804 /* if the query choked with EDNS0, retry without EDNS0 */
1805 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1806 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1807 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001808#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001809 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001810#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001811 goto again;
1812 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001813#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001814 if (res->options & RES_DEBUG)
1815 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001816#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001817 continue;
1818 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001819
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001820 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001822 t->n = n;
1823 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001825 if (ancount == 0) {
1826 switch (rcode) {
1827 case NXDOMAIN:
1828 h_errno = HOST_NOT_FOUND;
1829 break;
1830 case SERVFAIL:
1831 h_errno = TRY_AGAIN;
1832 break;
1833 case NOERROR:
1834 h_errno = NO_DATA;
1835 break;
1836 case FORMERR:
1837 case NOTIMP:
1838 case REFUSED:
1839 default:
1840 h_errno = NO_RECOVERY;
1841 break;
1842 }
1843 return -1;
1844 }
1845 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001846}
1847
1848/*
1849 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1850 * Return the size of the response on success, -1 on error.
1851 * If enabled, implement search rules until answer or unrecoverable failure
1852 * is detected. Error code, if any, is left in h_errno.
1853 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001854static int res_searchN(const char* name, struct res_target* target, res_state res) {
1855 const char *cp, *const *domain;
1856 HEADER* hp;
1857 u_int dots;
1858 int trailing_dot, ret, saved_herrno;
1859 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001860
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001861 assert(name != NULL);
1862 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001863
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001864 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001865
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001866 errno = 0;
1867 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1868 dots = 0;
1869 for (cp = name; *cp; cp++) dots += (*cp == '.');
1870 trailing_dot = 0;
1871 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001873 // fprintf(stderr, "res_searchN() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001874
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001875 /*
1876 * if there aren't any dots, it could be a user-level alias
1877 */
1878 if (!dots && (cp = __hostalias(name)) != NULL) {
1879 ret = res_queryN(cp, target, res);
1880 return ret;
1881 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001882
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001883 /*
1884 * If there are dots in the name already, let's just give it a try
1885 * 'as is'. The threshold can be set with the "ndots" option.
1886 */
1887 saved_herrno = -1;
1888 if (dots >= res->ndots) {
1889 ret = res_querydomainN(name, NULL, target, res);
1890 if (ret > 0) return (ret);
1891 saved_herrno = h_errno;
1892 tried_as_is++;
1893 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001894
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001895 /*
1896 * We do at least one level of search if
1897 * - there is no dot and RES_DEFNAME is set, or
1898 * - there is at least one dot, there is no trailing dot,
1899 * and RES_DNSRCH is set.
1900 */
1901 if ((!dots && (res->options & RES_DEFNAMES)) ||
1902 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1903 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001904
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001905 /* Unfortunately we need to set stuff up before
1906 * the domain stuff is tried. Will have a better
1907 * fix after thread pools are used.
1908 */
1909 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001910
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001911 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1912 ret = res_querydomainN(name, *domain, target, res);
1913 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001914
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001915 /*
1916 * If no server present, give up.
1917 * If name isn't found in this domain,
1918 * keep trying higher domains in the search list
1919 * (if that's enabled).
1920 * On a NO_DATA error, keep trying, otherwise
1921 * a wildcard entry of another type could keep us
1922 * from finding this entry higher in the domain.
1923 * If we get some other error (negative answer or
1924 * server failure), then stop searching up,
1925 * but try the input name below in case it's
1926 * fully-qualified.
1927 */
1928 if (errno == ECONNREFUSED) {
1929 h_errno = TRY_AGAIN;
1930 return -1;
1931 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001932
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001933 switch (h_errno) {
1934 case NO_DATA:
1935 got_nodata++;
1936 /* FALLTHROUGH */
1937 case HOST_NOT_FOUND:
1938 /* keep trying */
1939 break;
1940 case TRY_AGAIN:
1941 if (hp->rcode == SERVFAIL) {
1942 /* try next search element, if any */
1943 got_servfail++;
1944 break;
1945 }
1946 /* FALLTHROUGH */
1947 default:
1948 /* anything else implies that we're done */
1949 done++;
1950 }
1951 /*
1952 * if we got here for some reason other than DNSRCH,
1953 * we only wanted one iteration of the loop, so stop.
1954 */
1955 if (!(res->options & RES_DNSRCH)) done++;
1956 }
1957 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001958
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001959 /*
1960 * if we have not already tried the name "as is", do that now.
1961 * note that we do this regardless of how many dots were in the
1962 * name or whether it ends with a dot.
1963 */
1964 if (!tried_as_is) {
1965 ret = res_querydomainN(name, NULL, target, res);
1966 if (ret > 0) return ret;
1967 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001968
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001969 /*
1970 * if we got here, we didn't satisfy the search.
1971 * if we did an initial full query, return that query's h_errno
1972 * (note that we wouldn't be here if that query had succeeded).
1973 * else if we ever got a nodata, send that back as the reason.
1974 * else send back meaningless h_errno, that being the one from
1975 * the last DNSRCH we did.
1976 */
1977 if (saved_herrno != -1)
1978 h_errno = saved_herrno;
1979 else if (got_nodata)
1980 h_errno = NO_DATA;
1981 else if (got_servfail)
1982 h_errno = TRY_AGAIN;
1983 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001984}
1985
1986/*
1987 * Perform a call on res_query on the concatenation of name and domain,
1988 * removing a trailing dot from name if domain is NULL.
1989 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001990static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1991 res_state res) {
1992 char nbuf[MAXDNAME];
1993 const char* longname = nbuf;
1994 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001995
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001996 assert(name != NULL);
1997 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001998
1999#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002000 if (res->options & RES_DEBUG)
2001 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09002002#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002003 if (domain == NULL) {
2004 /*
2005 * Check for trailing '.';
2006 * copy without '.' if present.
2007 */
2008 n = strlen(name);
2009 if (n + 1 > sizeof(nbuf)) {
2010 h_errno = NO_RECOVERY;
2011 return -1;
2012 }
2013 if (n > 0 && name[--n] == '.') {
2014 strncpy(nbuf, name, n);
2015 nbuf[n] = '\0';
2016 } else
2017 longname = name;
2018 } else {
2019 n = strlen(name);
2020 d = strlen(domain);
2021 if (n + 1 + d + 1 > sizeof(nbuf)) {
2022 h_errno = NO_RECOVERY;
2023 return -1;
2024 }
2025 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2026 }
2027 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09002028}