blob: 6e2c90b81dfb7cea10dee4b3d6aff75b9e7cd423 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
2/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked.
36 * - Return values. There are nonstandard return values defined and used
37 * in the source code. This is because RFC2553 is silent about which error
38 * code must be returned for which situation.
39 * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
40 * says to use inet_aton() to convert IPv4 numeric to binary (alows
41 * classful form as a result).
42 * current code - disallow classful form for IPv4 (due to use of inet_pton).
43 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
44 * invalid.
45 * current code - SEGV on freeaddrinfo(NULL)
46 * Note:
47 * - We use getipnodebyname() just for thread-safeness. There's no intent
48 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
49 * getipnodebyname().
50 * - The code filters out AFs that are not supported by the kernel,
51 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
52 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
53 * in ai_flags?
54 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
55 * (1) what should we do against numeric hostname (2) what should we do
56 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
57 * non-loopback address configured? global address configured?
58 * - To avoid search order issue, we have a big amount of code duplicate
59 * from gethnamaddr.c and some other places. The issues that there's no
60 * lower layer function to lookup "IPv4 or IPv6" record. Calling
61 * gethostbyname2 from getaddrinfo will end up in wrong search order, as
62 * follows:
63 * - The code makes use of following calls when asked to resolver with
64 * ai_family = PF_UNSPEC:
65 * getipnodebyname(host, AF_INET6);
66 * getipnodebyname(host, AF_INET);
67 * This will result in the following queries if the node is configure to
68 * prefer /etc/hosts than DNS:
69 * lookup /etc/hosts for IPv6 address
70 * lookup DNS for IPv6 address
71 * lookup /etc/hosts for IPv4 address
72 * lookup DNS for IPv4 address
73 * which may not meet people's requirement.
74 * The right thing to happen is to have underlying layer which does
75 * PF_UNSPEC lookup (lookup both) and return chain of addrinfos.
76 * This would result in a bit of code duplicate with _dns_ghbyname() and
77 * friends.
78 */
79
Bernie Innocenti55864192018-08-30 04:05:20 +090080#include <arpa/inet.h>
81#include <arpa/nameser.h>
82#include <assert.h>
83#include <ctype.h>
84#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#include <fcntl.h>
86#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090087#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090089#include <stdbool.h>
90#include <stddef.h>
91#include <stdio.h>
92#include <stdlib.h>
93#include <string.h>
94#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090095#include <sys/param.h>
96#include <sys/socket.h>
97#include <sys/stat.h>
98#include <sys/types.h>
99#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900100#include <unistd.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900101#include "resolv_cache.h"
102#include "resolv_netid.h"
103#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900104
Bernie Innocenti55864192018-08-30 04:05:20 +0900105#include <stdarg.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106#include <syslog.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +0900107
Bernie Innocenti55864192018-08-30 04:05:20 +0900108#include "nsswitch.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900109
110typedef union sockaddr_union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 struct sockaddr generic;
112 struct sockaddr_in in;
Bernie Innocenti55864192018-08-30 04:05:20 +0900113 struct sockaddr_in6 in6;
114} sockaddr_union;
115
116#define SUCCESS 0
117#define ANY 0
118#define YES 1
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119#define NO 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900120
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121static const char in_addrany[] = {0, 0, 0, 0};
122static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +0900123#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
125static const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +0900126#endif
127
Bernie Innocenti55864192018-08-30 04:05:20 +0900128static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900129 int a_af;
130 int a_addrlen;
131 int a_socklen;
132 int a_off;
133 const char* a_addrany;
134 const char* a_loopback;
135 int a_scoped;
136} afdl[] = {
Bernie Innocenti55864192018-08-30 04:05:20 +0900137#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
139 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocenti55864192018-08-30 04:05:20 +0900140#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
142 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
143 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900144};
145
146struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147 int e_af;
148 int e_socktype;
149 int e_protocol;
150 const char* e_protostr;
151 int e_wild;
152#define WILD_AF(ex) ((ex)->e_wild & 0x01)
153#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
154#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900155};
156
157static const struct explore explore[] = {
158#if 0
159 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
160#endif
161#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
163 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
164 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900165#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
167 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
168 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
169 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
170 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
171 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
172 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900173};
174
175#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900176#define PTON_MAX 16
Bernie Innocenti55864192018-08-30 04:05:20 +0900177#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178#define PTON_MAX 4
Bernie Innocenti55864192018-08-30 04:05:20 +0900179#endif
180
181static const ns_src default_dns_files[] = {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900182 {NSSRC_FILES, NS_SUCCESS},
183 {NSSRC_DNS, NS_SUCCESS},
184 {0, 0}
185};
Bernie Innocenti55864192018-08-30 04:05:20 +0900186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
189typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190 HEADER hdr;
191 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900192} querybuf;
193
194struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195 struct res_target* next;
196 const char* name; /* domain name */
197 int qclass, qtype; /* class and type of query */
198 u_char* answer; /* buffer to put answer */
199 int anslen; /* size of answer buffer */
200 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900201};
202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203static int str2number(const char*);
204static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
205 const struct android_net_context*);
206static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
207static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
208 const char*);
209static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
210 struct addrinfo**);
211static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
212static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
213static int get_portmatch(const struct addrinfo*, const char*);
214static int get_port(const struct addrinfo*, const char*, int);
215static const struct afd* find_afd(int);
Bernie Innocenti55864192018-08-30 04:05:20 +0900216#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217static int ip6_str2scopeid(char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900218#endif
219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
221static int _dns_getaddrinfo(void*, void*, va_list);
222static void _sethtent(FILE**);
223static void _endhtent(FILE**);
224static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
225static int _files_getaddrinfo(void*, void*, va_list);
226static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228static int res_queryN(const char*, struct res_target*, res_state);
229static int res_searchN(const char*, struct res_target*, res_state);
230static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232static const char* const ai_errlist[] = {
233 "Success",
234 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
235 "Temporary failure in name resolution", /* EAI_AGAIN */
236 "Invalid value for ai_flags", /* EAI_BADFLAGS */
237 "Non-recoverable failure in name resolution", /* EAI_FAIL */
238 "ai_family not supported", /* EAI_FAMILY */
239 "Memory allocation failure", /* EAI_MEMORY */
240 "No address associated with hostname", /* EAI_NODATA */
241 "hostname nor servname provided, or not known", /* EAI_NONAME */
242 "servname not supported for ai_socktype", /* EAI_SERVICE */
243 "ai_socktype not supported", /* EAI_SOCKTYPE */
244 "System error returned in errno", /* EAI_SYSTEM */
245 "Invalid value for hints", /* EAI_BADHINTS */
246 "Resolved protocol is unknown", /* EAI_PROTOCOL */
247 "Argument buffer overflow", /* EAI_OVERFLOW */
248 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900249};
250
251/* XXX macros that make external reference is BAD. */
252
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900253#define GET_AI(ai, afd, addr) \
254 do { \
255 /* external reference: pai, error, and label free */ \
256 (ai) = get_ai(pai, (afd), (addr)); \
257 if ((ai) == NULL) { \
258 error = EAI_MEMORY; \
259 goto free; \
260 } \
261 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900262
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900263#define GET_PORT(ai, serv) \
264 do { \
265 /* external reference: error and label free */ \
266 error = get_port((ai), (serv), 0); \
267 if (error != 0) goto free; \
268 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900269
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270#define GET_CANONNAME(ai, str) \
271 do { \
272 /* external reference: pai, error and label free */ \
273 error = get_canonname(pai, (ai), (str)); \
274 if (error != 0) goto free; \
275 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900276
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900277#define ERR(err) \
278 do { \
279 /* external reference: error, and label bad */ \
280 error = (err); \
281 goto bad; \
282 /*NOTREACHED*/ \
283 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900284
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900285#define MATCH_FAMILY(x, y, w) \
286 ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
287#define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900288
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900289const char* gai_strerror(int ecode) {
290 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
291 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900292}
293
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900294void freeaddrinfo(struct addrinfo* ai) {
295 struct addrinfo* next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900296
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900297 if (ai == NULL) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900298
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900299 do {
300 next = ai->ai_next;
301 if (ai->ai_canonname) free(ai->ai_canonname);
302 /* no need to free(ai->ai_addr) */
303 free(ai);
304 ai = next;
305 } while (ai);
Bernie Innocenti55864192018-08-30 04:05:20 +0900306}
307
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900308static int str2number(const char* p) {
309 char* ep;
310 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900311
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900312 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900313
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900314 if (*p == '\0') return -1;
315 ep = NULL;
316 errno = 0;
317 v = strtoul(p, &ep, 10);
318 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
319 return v;
320 else
321 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900322}
323
324/*
325 * The following functions determine whether IPv4 or IPv6 connectivity is
326 * available in order to implement AI_ADDRCONFIG.
327 *
328 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
329 * available, but whether addresses of the specified family are "configured
330 * on the local system". However, bionic doesn't currently support getifaddrs,
331 * so checking for connectivity is the next best thing.
332 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900333static int _have_ipv6(unsigned mark, uid_t uid) {
334 static const struct sockaddr_in6 sin6_test = {
335 .sin6_family = AF_INET6,
336 .sin6_addr.s6_addr = {// 2000::
337 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
338 sockaddr_union addr = {.in6 = sin6_test};
339 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900340}
341
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900342static int _have_ipv4(unsigned mark, uid_t uid) {
343 static const struct sockaddr_in sin_test = {
344 .sin_family = AF_INET,
345 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
346 };
347 sockaddr_union addr = {.in = sin_test};
348 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900349}
350
351bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900352 int32_t tmp;
353 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
354 return false;
355 }
356 *result = ntohl(tmp);
357 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900358}
359
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900360int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
361 struct addrinfo** res) {
362 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900363}
364
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900365int android_getaddrinfofornet(const char* hostname, const char* servname,
366 const struct addrinfo* hints, unsigned netid, unsigned mark,
367 struct addrinfo** res) {
368 struct android_net_context netcontext = {
369 .app_netid = netid,
370 .app_mark = mark,
371 .dns_netid = netid,
372 .dns_mark = mark,
373 .uid = NET_CONTEXT_INVALID_UID,
374 };
375 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900376}
377
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900378int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
379 const struct addrinfo* hints,
380 const struct android_net_context* netcontext,
381 struct addrinfo** res) {
382 struct addrinfo sentinel;
383 struct addrinfo* cur;
384 int error = 0;
385 struct addrinfo ai;
386 struct addrinfo ai0;
387 struct addrinfo* pai;
388 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900389
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 /* hostname is allowed to be NULL */
391 /* servname is allowed to be NULL */
392 /* hints is allowed to be NULL */
393 assert(res != NULL);
394 assert(netcontext != NULL);
395 memset(&sentinel, 0, sizeof(sentinel));
396 cur = &sentinel;
397 pai = &ai;
398 pai->ai_flags = 0;
399 pai->ai_family = PF_UNSPEC;
400 pai->ai_socktype = ANY;
401 pai->ai_protocol = ANY;
402 pai->ai_addrlen = 0;
403 pai->ai_canonname = NULL;
404 pai->ai_addr = NULL;
405 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900406
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900407 if (hostname == NULL && servname == NULL) return EAI_NONAME;
408 if (hints) {
409 /* error check for hints */
410 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
411 ERR(EAI_BADHINTS); /* xxx */
412 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
413 switch (hints->ai_family) {
414 case PF_UNSPEC:
415 case PF_INET:
Bernie Innocenti55864192018-08-30 04:05:20 +0900416#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900417 case PF_INET6:
Bernie Innocenti55864192018-08-30 04:05:20 +0900418#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 break;
420 default:
421 ERR(EAI_FAMILY);
422 }
423 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900424
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 /*
426 * if both socktype/protocol are specified, check if they
427 * are meaningful combination.
428 */
429 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
430 for (ex = explore; ex->e_af >= 0; ex++) {
431 if (pai->ai_family != ex->e_af) continue;
432 if (ex->e_socktype == ANY) continue;
433 if (ex->e_protocol == ANY) continue;
434 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
435 ERR(EAI_BADHINTS);
436 }
437 }
438 }
439 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900440
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 /*
442 * check for special cases. (1) numeric servname is disallowed if
443 * socktype/protocol are left unspecified. (2) servname is disallowed
444 * for raw and other inet{,6} sockets.
445 */
446 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocenti55864192018-08-30 04:05:20 +0900447#ifdef PF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900448 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocenti55864192018-08-30 04:05:20 +0900449#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 ) {
451 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900452
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900454#ifdef PF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900455 pai->ai_family = PF_INET6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900457 pai->ai_family = PF_INET;
Bernie Innocenti55864192018-08-30 04:05:20 +0900458#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 }
460 error = get_portmatch(pai, servname);
461 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900462
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900463 *pai = ai0;
464 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900465
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900467
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900468 /* NULL hostname, or numeric hostname */
469 for (ex = explore; ex->e_af >= 0; ex++) {
470 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900471
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 /* PF_UNSPEC entries are prepared for DNS queries only */
473 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
476 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
477 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900478
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
480 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
481 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 if (hostname == NULL)
484 error = explore_null(pai, servname, &cur->ai_next);
485 else
486 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900487
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 while (cur->ai_next) cur = cur->ai_next;
491 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 /*
494 * XXX
495 * If numeric representation of AF1 can be interpreted as FQDN
496 * representation of AF2, we need to think again about the code below.
497 */
498 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900499
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 if (hostname == NULL) ERR(EAI_NODATA);
501 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 /*
504 * hostname as alphabetical name.
505 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
506 * outer loop by AFs.
507 */
508 for (ex = explore; ex->e_af >= 0; ex++) {
509 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 /* require exact match for family field */
512 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900513
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
515 continue;
516 }
517 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
518 continue;
519 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900520
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
522 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900523
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900524 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900525
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900526 while (cur && cur->ai_next) cur = cur->ai_next;
527 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900528
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900529 /* XXX */
530 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900531
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900532 if (error) goto free;
533 if (error == 0) {
534 if (sentinel.ai_next) {
535 good:
536 *res = sentinel.ai_next;
537 return SUCCESS;
538 } else
539 error = EAI_FAIL;
540 }
541free:
542bad:
543 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
544 *res = NULL;
545 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900546}
547
548/*
549 * FQDN hostname, DNS lookup
550 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
552 struct addrinfo** res, const struct android_net_context* netcontext) {
553 struct addrinfo* result;
554 struct addrinfo* cur;
555 int error = 0;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900556 static const ns_dtab dtab[] = {
557 {NSSRC_FILES, _files_getaddrinfo, NULL},
558 {NSSRC_DNS, _dns_getaddrinfo, NULL},
559 {0, 0, 0}
560 };
Bernie Innocenti55864192018-08-30 04:05:20 +0900561
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900562 assert(pai != NULL);
563 /* hostname may be NULL */
564 /* servname may be NULL */
565 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900568
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900569 /*
570 * if the servname does not match socktype/protocol, ignore it.
571 */
572 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900574 switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai,
575 netcontext)) {
576 case NS_TRYAGAIN:
577 error = EAI_AGAIN;
578 goto free;
579 case NS_UNAVAIL:
580 error = EAI_FAIL;
581 goto free;
582 case NS_NOTFOUND:
583 error = EAI_NODATA;
584 goto free;
585 case NS_SUCCESS:
586 error = 0;
587 for (cur = result; cur; cur = cur->ai_next) {
588 GET_PORT(cur, servname);
589 /* canonname should be filled already */
590 }
591 break;
592 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900593
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900594 *res = result;
Bernie Innocenti55864192018-08-30 04:05:20 +0900595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900597
598free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900599 if (result) freeaddrinfo(result);
600 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900601}
602
603/*
604 * hostname == NULL.
605 * passive socket -> anyaddr (0.0.0.0 or ::)
606 * non-passive socket -> localhost (127.0.0.1 or ::1)
607 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
609 int s;
610 const struct afd* afd;
611 struct addrinfo* cur;
612 struct addrinfo sentinel;
613 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 assert(pai != NULL);
616 /* servname may be NULL */
617 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900618
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 *res = NULL;
620 sentinel.ai_next = NULL;
621 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900622
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 /*
624 * filter out AFs that are not supported by the kernel
625 * XXX errno?
626 */
627 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
628 if (s < 0) {
629 if (errno != EMFILE) return 0;
630 } else
631 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900632
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633 /*
634 * if the servname does not match socktype/protocol, ignore it.
635 */
636 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900637
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900638 afd = find_afd(pai->ai_family);
639 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 if (pai->ai_flags & AI_PASSIVE) {
642 GET_AI(cur->ai_next, afd, afd->a_addrany);
643 /* xxx meaningless?
644 * GET_CANONNAME(cur->ai_next, "anyaddr");
645 */
646 GET_PORT(cur->ai_next, servname);
647 } else {
648 GET_AI(cur->ai_next, afd, afd->a_loopback);
649 /* xxx meaningless?
650 * GET_CANONNAME(cur->ai_next, "localhost");
651 */
652 GET_PORT(cur->ai_next, servname);
653 }
654 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 *res = sentinel.ai_next;
657 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900658
659free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900660 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
661 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900662}
663
664/*
665 * numeric hostname
666 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
668 struct addrinfo** res, const char* canonname) {
669 const struct afd* afd;
670 struct addrinfo* cur;
671 struct addrinfo sentinel;
672 int error;
673 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900674
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900675 assert(pai != NULL);
676 /* hostname may be NULL */
677 /* servname may be NULL */
678 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680 *res = NULL;
681 sentinel.ai_next = NULL;
682 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900683
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900684 /*
685 * if the servname does not match socktype/protocol, ignore it.
686 */
687 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 afd = find_afd(pai->ai_family);
690 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900693#if 0 /*X/Open spec*/
694 case AF_INET:
695 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
696 if (pai->ai_family == afd->a_af ||
697 pai->ai_family == PF_UNSPEC /*?*/) {
698 GET_AI(cur->ai_next, afd, pton);
699 GET_PORT(cur->ai_next, servname);
700 if ((pai->ai_flags & AI_CANONNAME)) {
701 /*
702 * Set the numeric address itself as
703 * the canonical name, based on a
704 * clarification in rfc2553bis-03.
705 */
706 GET_CANONNAME(cur->ai_next, canonname);
707 }
708 while (cur && cur->ai_next)
709 cur = cur->ai_next;
710 } else
711 ERR(EAI_FAMILY); /*xxx*/
712 }
713 break;
714#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715 default:
716 if (inet_pton(afd->a_af, hostname, pton) == 1) {
717 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
718 GET_AI(cur->ai_next, afd, pton);
719 GET_PORT(cur->ai_next, servname);
720 if ((pai->ai_flags & AI_CANONNAME)) {
721 /*
722 * Set the numeric address itself as
723 * the canonical name, based on a
724 * clarification in rfc2553bis-03.
725 */
726 GET_CANONNAME(cur->ai_next, canonname);
727 }
728 while (cur->ai_next) cur = cur->ai_next;
729 } else
730 ERR(EAI_FAMILY); /*xxx*/
731 }
732 break;
733 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 *res = sentinel.ai_next;
736 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
738free:
739bad:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900740 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
741 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900742}
743
744/*
745 * numeric hostname with scope
746 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
748 const char* servname, struct addrinfo** res) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900749#if !defined(SCOPE_DELIMITER) || !defined(INET6)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900751#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 const struct afd* afd;
753 struct addrinfo* cur;
754 int error;
755 char *cp, *hostname2 = NULL, *scope, *addr;
756 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 assert(pai != NULL);
759 /* hostname may be NULL */
760 /* servname may be NULL */
761 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900762
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900763 /*
764 * if the servname does not match socktype/protocol, ignore it.
765 */
766 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 afd = find_afd(pai->ai_family);
769 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 cp = strchr(hostname, SCOPE_DELIMITER);
774 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 /*
777 * Handle special case of <scoped_address><delimiter><scope id>
778 */
779 hostname2 = strdup(hostname);
780 if (hostname2 == NULL) return EAI_MEMORY;
781 /* terminate at the delimiter */
782 hostname2[cp - hostname] = '\0';
783 addr = hostname2;
784 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900785
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900786 error = explore_numeric(pai, addr, servname, res, hostname);
787 if (error == 0) {
788 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 for (cur = *res; cur; cur = cur->ai_next) {
791 if (cur->ai_family != AF_INET6) continue;
792 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
793 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
794 free(hostname2);
795 return (EAI_NODATA); /* XXX: is return OK? */
796 }
797 sin6->sin6_scope_id = scopeid;
798 }
799 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900802
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900803 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900804#endif
805}
806
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
808 assert(pai != NULL);
809 assert(ai != NULL);
810 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 if ((pai->ai_flags & AI_CANONNAME) != 0) {
813 ai->ai_canonname = strdup(str);
814 if (ai->ai_canonname == NULL) return EAI_MEMORY;
815 }
816 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817}
818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
820 const char* addr) {
821 char* p;
822 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 assert(pai != NULL);
825 assert(afd != NULL);
826 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
829 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 memcpy(ai, pai, sizeof(struct addrinfo));
832 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
833 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
835#ifdef HAVE_SA_LEN
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 ai->ai_addr->sa_len = afd->a_socklen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900837#endif
838
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 ai->ai_addrlen = afd->a_socklen;
840#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
841 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900842#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900843 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
844 p = (char*) (void*) (ai->ai_addr);
845 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
846 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900847}
848
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849static int get_portmatch(const struct addrinfo* ai, const char* servname) {
850 assert(ai != NULL);
851 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900854}
855
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900856static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
857 const char* proto;
858 struct servent* sp;
859 int port;
860 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900861
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900862 assert(ai != NULL);
863 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900864
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900865 if (servname == NULL) return 0;
866 switch (ai->ai_family) {
867 case AF_INET:
Bernie Innocenti55864192018-08-30 04:05:20 +0900868#ifdef AF_INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900869 case AF_INET6:
Bernie Innocenti55864192018-08-30 04:05:20 +0900870#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900871 break;
872 default:
873 return 0;
874 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900875
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 switch (ai->ai_socktype) {
877 case SOCK_RAW:
878 return EAI_SERVICE;
879 case SOCK_DGRAM:
880 case SOCK_STREAM:
881 allownumeric = 1;
882 break;
883 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900884 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900885 break;
886 default:
887 return EAI_SOCKTYPE;
888 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900889
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900890 port = str2number(servname);
891 if (port >= 0) {
892 if (!allownumeric) return EAI_SERVICE;
893 if (port < 0 || port > 65535) return EAI_SERVICE;
894 port = htons(port);
895 } else {
896 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900897
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900898 switch (ai->ai_socktype) {
899 case SOCK_DGRAM:
900 proto = "udp";
901 break;
902 case SOCK_STREAM:
903 proto = "tcp";
904 break;
905 default:
906 proto = NULL;
907 break;
908 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900909
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
911 port = sp->s_port;
912 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900913
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 if (!matchonly) {
915 switch (ai->ai_family) {
916 case AF_INET:
917 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
918 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900919#ifdef INET6
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900920 case AF_INET6:
921 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
922 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900923#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900924 }
925 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900926
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900927 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900928}
929
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900930static const struct afd* find_afd(int af) {
931 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900932
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900933 if (af == PF_UNSPEC) return NULL;
934 for (afd = afdl; afd->a_af; afd++) {
935 if (afd->a_af == af) return afd;
936 }
937 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900938}
939
940#ifdef INET6
941/* convert a string to a scope identifier. XXX: IPv6 specific */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900942static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
943 u_long lscopeid;
944 struct in6_addr* a6;
945 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900946
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 assert(scope != NULL);
948 assert(sin6 != NULL);
949 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900950
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900952
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900953 /* empty scopeid portion is invalid */
954 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900955
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900956 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
957 /*
958 * We currently assume a one-to-one mapping between links
959 * and interfaces, so we simply use interface indices for
960 * like-local scopes.
961 */
962 *scopeid = if_nametoindex(scope);
963 if (*scopeid == 0) goto trynumeric;
964 return 0;
965 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900966
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 /* still unclear about literal, allow numeric only - placeholder */
968 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
969 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
970 goto trynumeric;
971 else
972 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900973
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900974 /* try to convert to a numeric id as a last resort */
975trynumeric:
976 errno = 0;
977 lscopeid = strtoul(scope, &ep, 10);
978 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
979 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
980 return 0;
981 else
982 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900983}
984#endif
985
986/* code duplicate with gethnamaddr.c */
987
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900988static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900989
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900990#define BOUNDED_INCR(x) \
991 do { \
992 BOUNDS_CHECK(cp, x); \
993 cp += (x); \
994 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900995
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900996#define BOUNDS_CHECK(ptr, count) \
997 do { \
998 if (eom - (ptr) < (count)) { \
999 h_errno = NO_RECOVERY; \
1000 return NULL; \
1001 } \
1002 } while (/*CONSTCOND*/ 0)
Bernie Innocenti55864192018-08-30 04:05:20 +09001003
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001004static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
1005 const struct addrinfo* pai) {
1006 struct addrinfo sentinel, *cur;
1007 struct addrinfo ai;
1008 const struct afd* afd;
1009 char* canonname;
1010 const HEADER* hp;
1011 const u_char* cp;
1012 int n;
1013 const u_char* eom;
1014 char *bp, *ep;
1015 int type, class, ancount, qdcount;
1016 int haveanswer, had_error;
1017 char tbuf[MAXDNAME];
1018 int (*name_ok)(const char*);
1019 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001020
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001021 assert(answer != NULL);
1022 assert(qname != NULL);
1023 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001024
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001025 memset(&sentinel, 0, sizeof(sentinel));
1026 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001027
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001028 canonname = NULL;
1029 eom = answer->buf + anslen;
1030 switch (qtype) {
1031 case T_A:
1032 case T_AAAA:
1033 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
1034 name_ok = res_hnok;
1035 break;
1036 default:
1037 return NULL; /* XXX should be abort(); */
1038 }
1039 /*
1040 * find first satisfactory answer
1041 */
1042 hp = &answer->hdr;
1043 ancount = ntohs(hp->ancount);
1044 qdcount = ntohs(hp->qdcount);
1045 bp = hostbuf;
1046 ep = hostbuf + sizeof hostbuf;
1047 cp = answer->buf;
1048 BOUNDED_INCR(HFIXEDSZ);
1049 if (qdcount != 1) {
1050 h_errno = NO_RECOVERY;
1051 return (NULL);
1052 }
1053 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1054 if ((n < 0) || !(*name_ok)(bp)) {
1055 h_errno = NO_RECOVERY;
1056 return (NULL);
1057 }
1058 BOUNDED_INCR(n + QFIXEDSZ);
1059 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1060 /* res_send() has already verified that the query name is the
1061 * same as the one we sent; this just gets the expanded name
1062 * (i.e., with the succeeding search-domain tacked on).
1063 */
1064 n = strlen(bp) + 1; /* for the \0 */
1065 if (n >= MAXHOSTNAMELEN) {
1066 h_errno = NO_RECOVERY;
1067 return (NULL);
1068 }
1069 canonname = bp;
1070 bp += n;
1071 /* The qname can be abbreviated, but h_name is now absolute. */
1072 qname = canonname;
1073 }
1074 haveanswer = 0;
1075 had_error = 0;
1076 while (ancount-- > 0 && cp < eom && !had_error) {
1077 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1078 if ((n < 0) || !(*name_ok)(bp)) {
1079 had_error++;
1080 continue;
1081 }
1082 cp += n; /* name */
1083 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1084 type = _getshort(cp);
1085 cp += INT16SZ; /* type */
1086 class = _getshort(cp);
1087 cp += INT16SZ + INT32SZ; /* class, TTL */
1088 n = _getshort(cp);
1089 cp += INT16SZ; /* len */
1090 BOUNDS_CHECK(cp, n);
1091 if (class != C_IN) {
1092 /* XXX - debug? syslog? */
1093 cp += n;
1094 continue; /* XXX - had_error++ ? */
1095 }
1096 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1097 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1098 if ((n < 0) || !(*name_ok)(tbuf)) {
1099 had_error++;
1100 continue;
1101 }
1102 cp += n;
1103 /* Get canonical name. */
1104 n = strlen(tbuf) + 1; /* for the \0 */
1105 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1106 had_error++;
1107 continue;
1108 }
1109 strlcpy(bp, tbuf, (size_t)(ep - bp));
1110 canonname = bp;
1111 bp += n;
1112 continue;
1113 }
1114 if (qtype == T_ANY) {
1115 if (!(type == T_A || type == T_AAAA)) {
1116 cp += n;
1117 continue;
1118 }
1119 } else if (type != qtype) {
1120 if (type != T_KEY && type != T_SIG)
1121 syslog(LOG_NOTICE | LOG_AUTH,
1122 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1123 p_class(C_IN), p_type(qtype), p_type(type));
1124 cp += n;
1125 continue; /* XXX - had_error++ ? */
1126 }
1127 switch (type) {
1128 case T_A:
1129 case T_AAAA:
1130 if (strcasecmp(canonname, bp) != 0) {
1131 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1132 cp += n;
1133 continue; /* XXX - had_error++ ? */
1134 }
1135 if (type == T_A && n != INADDRSZ) {
1136 cp += n;
1137 continue;
1138 }
1139 if (type == T_AAAA && n != IN6ADDRSZ) {
1140 cp += n;
1141 continue;
1142 }
1143 if (type == T_AAAA) {
1144 struct in6_addr in6;
1145 memcpy(&in6, cp, IN6ADDRSZ);
1146 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1147 cp += n;
1148 continue;
1149 }
1150 }
1151 if (!haveanswer) {
1152 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001153
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001154 canonname = bp;
1155 nn = strlen(bp) + 1; /* for the \0 */
1156 bp += nn;
1157 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001158
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001159 /* don't overwrite pai */
1160 ai = *pai;
1161 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1162 afd = find_afd(ai.ai_family);
1163 if (afd == NULL) {
1164 cp += n;
1165 continue;
1166 }
1167 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1168 if (cur->ai_next == NULL) had_error++;
1169 while (cur && cur->ai_next) cur = cur->ai_next;
1170 cp += n;
1171 break;
1172 default:
1173 abort();
1174 }
1175 if (!had_error) haveanswer++;
1176 }
1177 if (haveanswer) {
1178 if (!canonname)
1179 (void) get_canonname(pai, sentinel.ai_next, qname);
1180 else
1181 (void) get_canonname(pai, sentinel.ai_next, canonname);
1182 h_errno = NETDB_SUCCESS;
1183 return sentinel.ai_next;
1184 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001185
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001186 h_errno = NO_RECOVERY;
1187 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001188}
1189
1190struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001191 struct addrinfo* ai;
1192 int has_src_addr;
1193 sockaddr_union src_addr;
1194 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001195};
1196
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001197static int _get_scope(const struct sockaddr* addr) {
1198 if (addr->sa_family == AF_INET6) {
1199 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1200 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1201 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1202 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1203 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1204 /*
1205 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1206 * link-local scope.
1207 */
1208 return IPV6_ADDR_SCOPE_LINKLOCAL;
1209 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1210 return IPV6_ADDR_SCOPE_SITELOCAL;
1211 } else {
1212 return IPV6_ADDR_SCOPE_GLOBAL;
1213 }
1214 } else if (addr->sa_family == AF_INET) {
1215 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1216 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001217
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001218 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1219 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1220 return IPV6_ADDR_SCOPE_LINKLOCAL;
1221 } else {
1222 /*
1223 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1224 * and shared addresses (100.64.0.0/10), are assigned global scope.
1225 */
1226 return IPV6_ADDR_SCOPE_GLOBAL;
1227 }
1228 } else {
1229 /*
1230 * This should never happen.
1231 * Return a scope with low priority as a last resort.
1232 */
1233 return IPV6_ADDR_SCOPE_NODELOCAL;
1234 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001235}
1236
1237/* These macros are modelled after the ones in <netinet/in6.h>. */
1238
1239/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001240#define IN6_IS_ADDR_TEREDO(a) \
1241 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001242
1243/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001244#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001245
1246/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001247#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001248
1249/*
1250 * Get the label for a given IPv4/IPv6 address.
1251 * RFC 6724, section 2.1.
1252 */
1253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001254static int _get_label(const struct sockaddr* addr) {
1255 if (addr->sa_family == AF_INET) {
1256 return 4;
1257 } else if (addr->sa_family == AF_INET6) {
1258 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1259 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1260 return 0;
1261 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1262 return 4;
1263 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1264 return 2;
1265 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1266 return 5;
1267 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1268 return 13;
1269 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1270 return 3;
1271 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1272 return 11;
1273 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1274 return 12;
1275 } else {
1276 /* All other IPv6 addresses, including global unicast addresses. */
1277 return 1;
1278 }
1279 } else {
1280 /*
1281 * This should never happen.
1282 * Return a semi-random label as a last resort.
1283 */
1284 return 1;
1285 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001286}
1287
1288/*
1289 * Get the precedence for a given IPv4/IPv6 address.
1290 * RFC 6724, section 2.1.
1291 */
1292
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001293static int _get_precedence(const struct sockaddr* addr) {
1294 if (addr->sa_family == AF_INET) {
1295 return 35;
1296 } else if (addr->sa_family == AF_INET6) {
1297 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1298 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1299 return 50;
1300 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1301 return 35;
1302 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1303 return 30;
1304 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1305 return 5;
1306 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1307 return 3;
1308 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1309 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1310 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1311 return 1;
1312 } else {
1313 /* All other IPv6 addresses, including global unicast addresses. */
1314 return 40;
1315 }
1316 } else {
1317 return 1;
1318 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001319}
1320
1321/*
1322 * Find number of matching initial bits between the two addresses a1 and a2.
1323 */
1324
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001325static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1326 const char* p1 = (const char*) a1;
1327 const char* p2 = (const char*) a2;
1328 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001329
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001330 for (i = 0; i < sizeof(*a1); ++i) {
1331 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001332
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001333 if (p1[i] == p2[i]) {
1334 continue;
1335 }
1336 x = p1[i] ^ p2[i];
1337 for (j = 0; j < CHAR_BIT; ++j) {
1338 if (x & (1 << (CHAR_BIT - 1))) {
1339 return i * CHAR_BIT + j;
1340 }
1341 x <<= 1;
1342 }
1343 }
1344 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001345}
1346
1347/*
1348 * Compare two source/destination address pairs.
1349 * RFC 6724, section 6.
1350 */
1351
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001352static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1353 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1354 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1355 int scope_src1, scope_dst1, scope_match1;
1356 int scope_src2, scope_dst2, scope_match2;
1357 int label_src1, label_dst1, label_match1;
1358 int label_src2, label_dst2, label_match2;
1359 int precedence1, precedence2;
1360 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001361
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001362 /* Rule 1: Avoid unusable destinations. */
1363 if (a1->has_src_addr != a2->has_src_addr) {
1364 return a2->has_src_addr - a1->has_src_addr;
1365 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001366
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001367 /* Rule 2: Prefer matching scope. */
1368 scope_src1 = _get_scope(&a1->src_addr.generic);
1369 scope_dst1 = _get_scope(a1->ai->ai_addr);
1370 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001371
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001372 scope_src2 = _get_scope(&a2->src_addr.generic);
1373 scope_dst2 = _get_scope(a2->ai->ai_addr);
1374 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001375
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001376 if (scope_match1 != scope_match2) {
1377 return scope_match2 - scope_match1;
1378 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001379
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001380 /*
1381 * Rule 3: Avoid deprecated addresses.
1382 * TODO(sesse): We don't currently have a good way of finding this.
1383 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001384
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 /*
1386 * Rule 4: Prefer home addresses.
1387 * TODO(sesse): We don't currently have a good way of finding this.
1388 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001389
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001390 /* Rule 5: Prefer matching label. */
1391 label_src1 = _get_label(&a1->src_addr.generic);
1392 label_dst1 = _get_label(a1->ai->ai_addr);
1393 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001394
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001395 label_src2 = _get_label(&a2->src_addr.generic);
1396 label_dst2 = _get_label(a2->ai->ai_addr);
1397 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 if (label_match1 != label_match2) {
1400 return label_match2 - label_match1;
1401 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001402
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001403 /* Rule 6: Prefer higher precedence. */
1404 precedence1 = _get_precedence(a1->ai->ai_addr);
1405 precedence2 = _get_precedence(a2->ai->ai_addr);
1406 if (precedence1 != precedence2) {
1407 return precedence2 - precedence1;
1408 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001409
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001410 /*
1411 * Rule 7: Prefer native transport.
1412 * TODO(sesse): We don't currently have a good way of finding this.
1413 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001414
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001415 /* Rule 8: Prefer smaller scope. */
1416 if (scope_dst1 != scope_dst2) {
1417 return scope_dst1 - scope_dst2;
1418 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001419
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001420 /*
1421 * Rule 9: Use longest matching prefix.
1422 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1423 * to work very well directly applied to IPv4. (glibc uses information from
1424 * the routing table for a custom IPv4 implementation here.)
1425 */
1426 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1427 a2->ai->ai_addr->sa_family == AF_INET6) {
1428 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1429 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1430 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1431 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1432 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1433 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1434 if (prefixlen1 != prefixlen2) {
1435 return prefixlen2 - prefixlen1;
1436 }
1437 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001438
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001439 /*
1440 * Rule 10: Leave the order unchanged.
1441 * We need this since qsort() is not necessarily stable.
1442 */
1443 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001444}
1445
1446/*
1447 * Find the source address that will be used if trying to connect to the given
1448 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1449 *
1450 * Returns 1 if a source address was found, 0 if the address is unreachable,
1451 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1452 * undefined.
1453 */
1454
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001455static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1456 uid_t uid) {
1457 int sock;
1458 int ret;
1459 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001460
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001461 switch (addr->sa_family) {
1462 case AF_INET:
1463 len = sizeof(struct sockaddr_in);
1464 break;
1465 case AF_INET6:
1466 len = sizeof(struct sockaddr_in6);
1467 break;
1468 default:
1469 /* No known usable source address for non-INET families. */
1470 return 0;
1471 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001472
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001473 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1474 if (sock == -1) {
1475 if (errno == EAFNOSUPPORT) {
1476 return 0;
1477 } else {
1478 return -1;
1479 }
1480 }
1481 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1482 close(sock);
1483 return 0;
1484 }
1485 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1486 close(sock);
1487 return 0;
1488 }
1489 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001490 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001491 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001493 if (ret == -1) {
1494 close(sock);
1495 return 0;
1496 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001497
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001498 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1499 close(sock);
1500 return -1;
1501 }
1502 close(sock);
1503 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001504}
1505
1506/*
1507 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1508 * Will leave the list unchanged if an error occurs.
1509 */
1510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001511static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1512 struct addrinfo* cur;
1513 int nelem = 0, i;
1514 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001515
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001516 cur = list_sentinel->ai_next;
1517 while (cur) {
1518 ++nelem;
1519 cur = cur->ai_next;
1520 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001521
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001522 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1523 if (elems == NULL) {
1524 goto error;
1525 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001526
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001527 /*
1528 * Convert the linked list to an array that also contains the candidate
1529 * source address for each destination address.
1530 */
1531 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1532 int has_src_addr;
1533 assert(cur != NULL);
1534 elems[i].ai = cur;
1535 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001536
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001537 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1538 if (has_src_addr == -1) {
1539 goto error;
1540 }
1541 elems[i].has_src_addr = has_src_addr;
1542 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001544 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1545 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001546
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001547 list_sentinel->ai_next = elems[0].ai;
1548 for (i = 0; i < nelem - 1; ++i) {
1549 elems[i].ai->ai_next = elems[i + 1].ai;
1550 }
1551 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001552
1553error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001554 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001555}
1556
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001557static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1558 struct addrinfo* ai;
1559 querybuf *buf, *buf2;
1560 const char* name;
1561 const struct addrinfo* pai;
1562 struct addrinfo sentinel, *cur;
1563 struct res_target q, q2;
1564 res_state res;
1565 const struct android_net_context* netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001567 name = va_arg(ap, char*);
1568 pai = va_arg(ap, const struct addrinfo*);
1569 netcontext = va_arg(ap, const struct android_net_context*);
1570 // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001571
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001572 memset(&q, 0, sizeof(q));
1573 memset(&q2, 0, sizeof(q2));
1574 memset(&sentinel, 0, sizeof(sentinel));
1575 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001576
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001577 buf = malloc(sizeof(*buf));
1578 if (buf == NULL) {
1579 h_errno = NETDB_INTERNAL;
1580 return NS_NOTFOUND;
1581 }
1582 buf2 = malloc(sizeof(*buf2));
1583 if (buf2 == NULL) {
1584 free(buf);
1585 h_errno = NETDB_INTERNAL;
1586 return NS_NOTFOUND;
1587 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001588
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001589 switch (pai->ai_family) {
1590 case AF_UNSPEC:
1591 /* prefer IPv6 */
1592 q.name = name;
1593 q.qclass = C_IN;
1594 q.answer = buf->buf;
1595 q.anslen = sizeof(buf->buf);
1596 int query_ipv6 = 1, query_ipv4 = 1;
1597 if (pai->ai_flags & AI_ADDRCONFIG) {
1598 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1599 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1600 }
1601 if (query_ipv6) {
1602 q.qtype = T_AAAA;
1603 if (query_ipv4) {
1604 q.next = &q2;
1605 q2.name = name;
1606 q2.qclass = C_IN;
1607 q2.qtype = T_A;
1608 q2.answer = buf2->buf;
1609 q2.anslen = sizeof(buf2->buf);
1610 }
1611 } else if (query_ipv4) {
1612 q.qtype = T_A;
1613 } else {
1614 free(buf);
1615 free(buf2);
1616 return NS_NOTFOUND;
1617 }
1618 break;
1619 case AF_INET:
1620 q.name = name;
1621 q.qclass = C_IN;
1622 q.qtype = T_A;
1623 q.answer = buf->buf;
1624 q.anslen = sizeof(buf->buf);
1625 break;
1626 case AF_INET6:
1627 q.name = name;
1628 q.qclass = C_IN;
1629 q.qtype = T_AAAA;
1630 q.answer = buf->buf;
1631 q.anslen = sizeof(buf->buf);
1632 break;
1633 default:
1634 free(buf);
1635 free(buf2);
1636 return NS_UNAVAIL;
1637 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001639 res = __res_get_state();
1640 if (res == NULL) {
1641 free(buf);
1642 free(buf2);
1643 return NS_NOTFOUND;
1644 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001645
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001646 /* this just sets our netid val in the thread private data so we don't have to
1647 * modify the api's all the way down to res_send.c's res_nsend. We could
1648 * fully populate the thread private data here, but if we get down there
1649 * and have a cache hit that would be wasted, so we do the rest there on miss
1650 */
1651 res_setnetcontext(res, netcontext);
1652 if (res_searchN(name, &q, res) < 0) {
1653 __res_put_state(res);
1654 free(buf);
1655 free(buf2);
1656 return NS_NOTFOUND;
1657 }
1658 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1659 if (ai) {
1660 cur->ai_next = ai;
1661 while (cur && cur->ai_next) cur = cur->ai_next;
1662 }
1663 if (q.next) {
1664 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1665 if (ai) cur->ai_next = ai;
1666 }
1667 free(buf);
1668 free(buf2);
1669 if (sentinel.ai_next == NULL) {
1670 __res_put_state(res);
1671 switch (h_errno) {
1672 case HOST_NOT_FOUND:
1673 return NS_NOTFOUND;
1674 case TRY_AGAIN:
1675 return NS_TRYAGAIN;
1676 default:
1677 return NS_UNAVAIL;
1678 }
1679 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001680
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001681 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001683 __res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001684
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001685 *((struct addrinfo**) rv) = sentinel.ai_next;
1686 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001687}
1688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001689static void _sethtent(FILE** hostf) {
1690 if (!*hostf)
1691 *hostf = fopen(_PATH_HOSTS, "re");
1692 else
1693 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001694}
1695
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001696static void _endhtent(FILE** hostf) {
1697 if (*hostf) {
1698 (void) fclose(*hostf);
1699 *hostf = NULL;
1700 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001701}
1702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001703static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1704 char* p;
1705 char *cp, *tname, *cname;
1706 struct addrinfo hints, *res0, *res;
1707 int error;
1708 const char* addr;
1709 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001710
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001711 // fprintf(stderr, "_gethtent() name = '%s'\n", name);
1712 assert(name != NULL);
1713 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001715 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1716again:
1717 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1718 if (*p == '#') goto again;
1719 if (!(cp = strpbrk(p, "#\n"))) goto again;
1720 *cp = '\0';
1721 if (!(cp = strpbrk(p, " \t"))) goto again;
1722 *cp++ = '\0';
1723 addr = p;
1724 /* if this is not something we're looking for, skip it. */
1725 cname = NULL;
1726 while (cp && *cp) {
1727 if (*cp == ' ' || *cp == '\t') {
1728 cp++;
1729 continue;
1730 }
1731 if (!cname) cname = cp;
1732 tname = cp;
1733 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1734 // fprintf(stderr, "\ttname = '%s'", tname);
1735 if (strcasecmp(name, tname) == 0) goto found;
1736 }
1737 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001738
1739found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001740 hints = *pai;
1741 hints.ai_flags = AI_NUMERICHOST;
1742 error = getaddrinfo(addr, NULL, &hints, &res0);
1743 if (error) goto again;
1744 for (res = res0; res; res = res->ai_next) {
1745 /* cover it up */
1746 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001747
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001748 if (pai->ai_flags & AI_CANONNAME) {
1749 if (get_canonname(pai, res, cname) != 0) {
1750 freeaddrinfo(res0);
1751 goto again;
1752 }
1753 }
1754 }
1755 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001756}
1757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001758static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) {
1759 const char* name;
1760 const struct addrinfo* pai;
1761 struct addrinfo sentinel, *cur;
1762 struct addrinfo* p;
1763 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001764
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001765 name = va_arg(ap, char*);
1766 pai = va_arg(ap, struct addrinfo*);
Bernie Innocenti55864192018-08-30 04:05:20 +09001767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001768 // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
1769 memset(&sentinel, 0, sizeof(sentinel));
1770 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001772 _sethtent(&hostf);
1773 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1774 cur->ai_next = p;
1775 while (cur && cur->ai_next) cur = cur->ai_next;
1776 }
1777 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001778
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001779 *((struct addrinfo**) rv) = sentinel.ai_next;
1780 if (sentinel.ai_next == NULL) return NS_NOTFOUND;
1781 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +09001782}
1783
1784/* resolver logic */
1785
1786/*
1787 * Formulate a normal query, send, and await answer.
1788 * Returned answer is placed in supplied buffer "answer".
1789 * Perform preliminary check of answer, returning success only
1790 * if no error is indicated and the answer count is nonzero.
1791 * Return the size of the response on success, -1 on error.
1792 * Error number is left in h_errno.
1793 *
1794 * Caller must parse answer and determine whether it answers the question.
1795 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001796static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1797 res_state res) {
1798 u_char buf[MAXPACKET];
1799 HEADER* hp;
1800 int n;
1801 struct res_target* t;
1802 int rcode;
1803 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001805 assert(name != NULL);
1806 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001808 rcode = NOERROR;
1809 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001810
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001811 for (t = target; t; t = t->next) {
1812 int class, type;
1813 u_char* answer;
1814 int anslen;
1815 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001816
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001817 hp = (HEADER*) (void*) t->answer;
1818 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001819
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001820 again:
1821 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001822
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001823 /* make it easier... */
1824 class = t->qclass;
1825 type = t->qtype;
1826 answer = t->answer;
1827 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001828#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001829 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001830#endif
1831
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001832 n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocenti55864192018-08-30 04:05:20 +09001833#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001834 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1835 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1836 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001837#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001838 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001839#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001840 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001841#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001842 h_errno = NO_RECOVERY;
1843 return n;
1844 }
1845 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001846#if 0
1847 if (n < 0) {
1848#ifdef DEBUG
1849 if (res->options & RES_DEBUG)
1850 printf(";; res_query: send error\n");
1851#endif
1852 h_errno = TRY_AGAIN;
1853 return n;
1854 }
1855#endif
1856
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001857 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1858 rcode = hp->rcode; /* record most recent error */
Bernie Innocenti55864192018-08-30 04:05:20 +09001859#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001860 /* if the query choked with EDNS0, retry without EDNS0 */
1861 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1862 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1863 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001864#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001865 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001866#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001867 goto again;
1868 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001869#endif
1870#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001871 if (res->options & RES_DEBUG)
1872 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001873#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001874 continue;
1875 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001876
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001877 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001879 t->n = n;
1880 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001881
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001882 if (ancount == 0) {
1883 switch (rcode) {
1884 case NXDOMAIN:
1885 h_errno = HOST_NOT_FOUND;
1886 break;
1887 case SERVFAIL:
1888 h_errno = TRY_AGAIN;
1889 break;
1890 case NOERROR:
1891 h_errno = NO_DATA;
1892 break;
1893 case FORMERR:
1894 case NOTIMP:
1895 case REFUSED:
1896 default:
1897 h_errno = NO_RECOVERY;
1898 break;
1899 }
1900 return -1;
1901 }
1902 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001903}
1904
1905/*
1906 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1907 * Return the size of the response on success, -1 on error.
1908 * If enabled, implement search rules until answer or unrecoverable failure
1909 * is detected. Error code, if any, is left in h_errno.
1910 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001911static int res_searchN(const char* name, struct res_target* target, res_state res) {
1912 const char *cp, *const *domain;
1913 HEADER* hp;
1914 u_int dots;
1915 int trailing_dot, ret, saved_herrno;
1916 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001917
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001918 assert(name != NULL);
1919 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001920
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001921 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001922
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001923 errno = 0;
1924 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1925 dots = 0;
1926 for (cp = name; *cp; cp++) dots += (*cp == '.');
1927 trailing_dot = 0;
1928 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001929
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001930 // fprintf(stderr, "res_searchN() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001931
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001932 /*
1933 * if there aren't any dots, it could be a user-level alias
1934 */
1935 if (!dots && (cp = __hostalias(name)) != NULL) {
1936 ret = res_queryN(cp, target, res);
1937 return ret;
1938 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001939
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001940 /*
1941 * If there are dots in the name already, let's just give it a try
1942 * 'as is'. The threshold can be set with the "ndots" option.
1943 */
1944 saved_herrno = -1;
1945 if (dots >= res->ndots) {
1946 ret = res_querydomainN(name, NULL, target, res);
1947 if (ret > 0) return (ret);
1948 saved_herrno = h_errno;
1949 tried_as_is++;
1950 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001951
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001952 /*
1953 * We do at least one level of search if
1954 * - there is no dot and RES_DEFNAME is set, or
1955 * - there is at least one dot, there is no trailing dot,
1956 * and RES_DNSRCH is set.
1957 */
1958 if ((!dots && (res->options & RES_DEFNAMES)) ||
1959 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1960 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001961
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001962 /* Unfortunately we need to set stuff up before
1963 * the domain stuff is tried. Will have a better
1964 * fix after thread pools are used.
1965 */
1966 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001967
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001968 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1969 ret = res_querydomainN(name, *domain, target, res);
1970 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001971
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001972 /*
1973 * If no server present, give up.
1974 * If name isn't found in this domain,
1975 * keep trying higher domains in the search list
1976 * (if that's enabled).
1977 * On a NO_DATA error, keep trying, otherwise
1978 * a wildcard entry of another type could keep us
1979 * from finding this entry higher in the domain.
1980 * If we get some other error (negative answer or
1981 * server failure), then stop searching up,
1982 * but try the input name below in case it's
1983 * fully-qualified.
1984 */
1985 if (errno == ECONNREFUSED) {
1986 h_errno = TRY_AGAIN;
1987 return -1;
1988 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001989
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001990 switch (h_errno) {
1991 case NO_DATA:
1992 got_nodata++;
1993 /* FALLTHROUGH */
1994 case HOST_NOT_FOUND:
1995 /* keep trying */
1996 break;
1997 case TRY_AGAIN:
1998 if (hp->rcode == SERVFAIL) {
1999 /* try next search element, if any */
2000 got_servfail++;
2001 break;
2002 }
2003 /* FALLTHROUGH */
2004 default:
2005 /* anything else implies that we're done */
2006 done++;
2007 }
2008 /*
2009 * if we got here for some reason other than DNSRCH,
2010 * we only wanted one iteration of the loop, so stop.
2011 */
2012 if (!(res->options & RES_DNSRCH)) done++;
2013 }
2014 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002015
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002016 /*
2017 * if we have not already tried the name "as is", do that now.
2018 * note that we do this regardless of how many dots were in the
2019 * name or whether it ends with a dot.
2020 */
2021 if (!tried_as_is) {
2022 ret = res_querydomainN(name, NULL, target, res);
2023 if (ret > 0) return ret;
2024 }
Bernie Innocenti55864192018-08-30 04:05:20 +09002025
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002026 /*
2027 * if we got here, we didn't satisfy the search.
2028 * if we did an initial full query, return that query's h_errno
2029 * (note that we wouldn't be here if that query had succeeded).
2030 * else if we ever got a nodata, send that back as the reason.
2031 * else send back meaningless h_errno, that being the one from
2032 * the last DNSRCH we did.
2033 */
2034 if (saved_herrno != -1)
2035 h_errno = saved_herrno;
2036 else if (got_nodata)
2037 h_errno = NO_DATA;
2038 else if (got_servfail)
2039 h_errno = TRY_AGAIN;
2040 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09002041}
2042
2043/*
2044 * Perform a call on res_query on the concatenation of name and domain,
2045 * removing a trailing dot from name if domain is NULL.
2046 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002047static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
2048 res_state res) {
2049 char nbuf[MAXDNAME];
2050 const char* longname = nbuf;
2051 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09002052
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002053 assert(name != NULL);
2054 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09002055
2056#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002057 if (res->options & RES_DEBUG)
2058 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09002059#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09002060 if (domain == NULL) {
2061 /*
2062 * Check for trailing '.';
2063 * copy without '.' if present.
2064 */
2065 n = strlen(name);
2066 if (n + 1 > sizeof(nbuf)) {
2067 h_errno = NO_RECOVERY;
2068 return -1;
2069 }
2070 if (n > 0 && name[--n] == '.') {
2071 strncpy(nbuf, name, n);
2072 nbuf[n] = '\0';
2073 } else
2074 longname = name;
2075 } else {
2076 n = strlen(name);
2077 d = strlen(domain);
2078 if (n + 1 + d + 1 > sizeof(nbuf)) {
2079 h_errno = NO_RECOVERY;
2080 return -1;
2081 }
2082 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2083 }
2084 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09002085}