blob: d3e8ce02ddcef33b71e1f7a465524f21f570755a [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
109typedef union sockaddr_union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900110 struct sockaddr generic;
111 struct sockaddr_in in;
Bernie Innocenti55864192018-08-30 04:05:20 +0900112 struct sockaddr_in6 in6;
113} sockaddr_union;
114
Bernie Innocenti55864192018-08-30 04:05:20 +0900115#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900116
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900117static const char in_addrany[] = {0, 0, 0, 0};
118static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
120static 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 +0900121
Bernie Innocenti55864192018-08-30 04:05:20 +0900122static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900123 int a_af;
124 int a_addrlen;
125 int a_socklen;
126 int a_off;
127 const char* a_addrany;
128 const char* a_loopback;
129 int a_scoped;
130} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900131 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
132 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900133 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
134 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
135 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900136};
137
138struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139 int e_af;
140 int e_socktype;
141 int e_protocol;
142 const char* e_protostr;
143 int e_wild;
144#define WILD_AF(ex) ((ex)->e_wild & 0x01)
145#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
146#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900147};
148
149static const struct explore explore[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
151 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
152 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
154 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
155 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
156 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
157 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
158 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
159 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900160};
161
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900164
165typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 HEADER hdr;
167 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900168} querybuf;
169
170struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900171 struct res_target* next;
172 const char* name; /* domain name */
173 int qclass, qtype; /* class and type of query */
174 u_char* answer; /* buffer to put answer */
175 int anslen; /* size of answer buffer */
176 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900177};
178
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900179static int str2number(const char*);
180static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
181 const struct android_net_context*);
182static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
183static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
184 const char*);
185static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
186 struct addrinfo**);
187static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
188static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
189static int get_portmatch(const struct addrinfo*, const char*);
190static int get_port(const struct addrinfo*, const char*, int);
191static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900192static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900195static int dns_getaddrinfo(const char* name, const addrinfo* pai,
196 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197static void _sethtent(FILE**);
198static void _endhtent(FILE**);
199static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900200static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203static int res_queryN(const char*, struct res_target*, res_state);
204static int res_searchN(const char*, struct res_target*, res_state);
205static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900206
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207static const char* const ai_errlist[] = {
208 "Success",
209 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
210 "Temporary failure in name resolution", /* EAI_AGAIN */
211 "Invalid value for ai_flags", /* EAI_BADFLAGS */
212 "Non-recoverable failure in name resolution", /* EAI_FAIL */
213 "ai_family not supported", /* EAI_FAMILY */
214 "Memory allocation failure", /* EAI_MEMORY */
215 "No address associated with hostname", /* EAI_NODATA */
216 "hostname nor servname provided, or not known", /* EAI_NONAME */
217 "servname not supported for ai_socktype", /* EAI_SERVICE */
218 "ai_socktype not supported", /* EAI_SOCKTYPE */
219 "System error returned in errno", /* EAI_SYSTEM */
220 "Invalid value for hints", /* EAI_BADHINTS */
221 "Resolved protocol is unknown", /* EAI_PROTOCOL */
222 "Argument buffer overflow", /* EAI_OVERFLOW */
223 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900224};
225
226/* XXX macros that make external reference is BAD. */
227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228#define GET_AI(ai, afd, addr) \
229 do { \
230 /* external reference: pai, error, and label free */ \
231 (ai) = get_ai(pai, (afd), (addr)); \
232 if ((ai) == NULL) { \
233 error = EAI_MEMORY; \
234 goto free; \
235 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900236 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900237
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900238#define GET_PORT(ai, serv) \
239 do { \
240 /* external reference: error and label free */ \
241 error = get_port((ai), (serv), 0); \
242 if (error != 0) goto free; \
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_CANONNAME(ai, str) \
246 do { \
247 /* external reference: pai, error and label free */ \
248 error = get_canonname(pai, (ai), (str)); \
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 ERR(err) \
253 do { \
254 /* external reference: error, and label bad */ \
255 error = (err); \
256 goto bad; \
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 MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900260 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
261#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900262
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900263const char* gai_strerror(int ecode) {
264 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
265 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900266}
267
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900268void freeaddrinfo(struct addrinfo* ai) {
269 struct addrinfo* next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900270
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900271 if (ai == NULL) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900272
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900273 do {
274 next = ai->ai_next;
275 if (ai->ai_canonname) free(ai->ai_canonname);
276 /* no need to free(ai->ai_addr) */
277 free(ai);
278 ai = next;
279 } while (ai);
Bernie Innocenti55864192018-08-30 04:05:20 +0900280}
281
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900282static int str2number(const char* p) {
283 char* ep;
284 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900286 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900287
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900288 if (*p == '\0') return -1;
289 ep = NULL;
290 errno = 0;
291 v = strtoul(p, &ep, 10);
292 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
293 return v;
294 else
295 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900296}
297
298/*
299 * The following functions determine whether IPv4 or IPv6 connectivity is
300 * available in order to implement AI_ADDRCONFIG.
301 *
302 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
303 * available, but whether addresses of the specified family are "configured
304 * on the local system". However, bionic doesn't currently support getifaddrs,
305 * so checking for connectivity is the next best thing.
306 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900307static int _have_ipv6(unsigned mark, uid_t uid) {
308 static const struct sockaddr_in6 sin6_test = {
309 .sin6_family = AF_INET6,
310 .sin6_addr.s6_addr = {// 2000::
311 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
312 sockaddr_union addr = {.in6 = sin6_test};
313 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900314}
315
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900316static int _have_ipv4(unsigned mark, uid_t uid) {
317 static const struct sockaddr_in sin_test = {
318 .sin_family = AF_INET,
319 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
320 };
321 sockaddr_union addr = {.in = sin_test};
322 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900323}
324
325bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900326 int32_t tmp;
327 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
328 return false;
329 }
330 *result = ntohl(tmp);
331 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900332}
333
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900334int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
335 struct addrinfo** res) {
336 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900337}
338
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900339int android_getaddrinfofornet(const char* hostname, const char* servname,
340 const struct addrinfo* hints, unsigned netid, unsigned mark,
341 struct addrinfo** res) {
342 struct android_net_context netcontext = {
343 .app_netid = netid,
344 .app_mark = mark,
345 .dns_netid = netid,
346 .dns_mark = mark,
347 .uid = NET_CONTEXT_INVALID_UID,
348 };
349 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900350}
351
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900352int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
353 const struct addrinfo* hints,
354 const struct android_net_context* netcontext,
355 struct addrinfo** res) {
356 struct addrinfo sentinel;
357 struct addrinfo* cur;
358 int error = 0;
359 struct addrinfo ai;
360 struct addrinfo ai0;
361 struct addrinfo* pai;
362 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900363
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900364 /* hostname is allowed to be NULL */
365 /* servname is allowed to be NULL */
366 /* hints is allowed to be NULL */
367 assert(res != NULL);
368 assert(netcontext != NULL);
369 memset(&sentinel, 0, sizeof(sentinel));
370 cur = &sentinel;
371 pai = &ai;
372 pai->ai_flags = 0;
373 pai->ai_family = PF_UNSPEC;
374 pai->ai_socktype = ANY;
375 pai->ai_protocol = ANY;
376 pai->ai_addrlen = 0;
377 pai->ai_canonname = NULL;
378 pai->ai_addr = NULL;
379 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900380
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900381 if (hostname == NULL && servname == NULL) return EAI_NONAME;
382 if (hints) {
383 /* error check for hints */
384 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
385 ERR(EAI_BADHINTS); /* xxx */
386 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
387 switch (hints->ai_family) {
388 case PF_UNSPEC:
389 case PF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 case PF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900391 break;
392 default:
393 ERR(EAI_FAMILY);
394 }
395 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900396
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900397 /*
398 * if both socktype/protocol are specified, check if they
399 * are meaningful combination.
400 */
401 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
402 for (ex = explore; ex->e_af >= 0; ex++) {
403 if (pai->ai_family != ex->e_af) continue;
404 if (ex->e_socktype == ANY) continue;
405 if (ex->e_protocol == ANY) continue;
406 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
407 ERR(EAI_BADHINTS);
408 }
409 }
410 }
411 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900412
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900413 /*
414 * check for special cases. (1) numeric servname is disallowed if
415 * socktype/protocol are left unspecified. (2) servname is disallowed
416 * for raw and other inet{,6} sockets.
417 */
418 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900420 ) {
421 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900422
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900423 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900424 pai->ai_family = PF_INET6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 }
426 error = get_portmatch(pai, servname);
427 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900428
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900429 *pai = ai0;
430 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900431
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900433
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900434 /* NULL hostname, or numeric hostname */
435 for (ex = explore; ex->e_af >= 0; ex++) {
436 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900437
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900438 /* PF_UNSPEC entries are prepared for DNS queries only */
439 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900440
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
442 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
443 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900444
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
446 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
447 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900448
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449 if (hostname == NULL)
450 error = explore_null(pai, servname, &cur->ai_next);
451 else
452 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900453
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900455
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900456 while (cur->ai_next) cur = cur->ai_next;
457 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900458
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 /*
460 * XXX
461 * If numeric representation of AF1 can be interpreted as FQDN
462 * representation of AF2, we need to think again about the code below.
463 */
464 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900465
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 if (hostname == NULL) ERR(EAI_NODATA);
467 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900468
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900469 /*
470 * hostname as alphabetical name.
471 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
472 * outer loop by AFs.
473 */
474 for (ex = explore; ex->e_af >= 0; ex++) {
475 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900476
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477 /* require exact match for family field */
478 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900479
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
481 continue;
482 }
483 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
484 continue;
485 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900486
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900487 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
488 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900491
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492 while (cur && cur->ai_next) cur = cur->ai_next;
493 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900494
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 /* XXX */
496 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900497
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 if (error) goto free;
499 if (error == 0) {
500 if (sentinel.ai_next) {
501 good:
502 *res = sentinel.ai_next;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900503 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900504 } else
505 error = EAI_FAIL;
506 }
507free:
508bad:
509 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
510 *res = NULL;
511 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900512}
513
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900514// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900515static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
516 struct addrinfo** res, const struct android_net_context* netcontext) {
517 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900518 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900519
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900520 assert(pai != NULL);
521 /* hostname may be NULL */
522 /* servname may be NULL */
523 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900524
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900526
Bernie Innocenti948f6572018-09-12 21:32:42 +0900527 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900528 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900529
Bernie Innocenti948f6572018-09-12 21:32:42 +0900530 if (!files_getaddrinfo(hostname, pai, &result)) {
531 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900532 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900533 if (!error) {
534 struct addrinfo* cur;
535 for (cur = result; cur; cur = cur->ai_next) {
536 GET_PORT(cur, servname);
537 /* canonname should be filled already */
538 }
539 *res = result;
540 return 0;
541 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900542
543free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 if (result) freeaddrinfo(result);
545 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900546}
547
548/*
549 * hostname == NULL.
550 * passive socket -> anyaddr (0.0.0.0 or ::)
551 * non-passive socket -> localhost (127.0.0.1 or ::1)
552 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
554 int s;
555 const struct afd* afd;
556 struct addrinfo* cur;
557 struct addrinfo sentinel;
558 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900559
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900560 assert(pai != NULL);
561 /* servname may be NULL */
562 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900563
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900564 *res = NULL;
565 sentinel.ai_next = NULL;
566 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 /*
569 * filter out AFs that are not supported by the kernel
570 * XXX errno?
571 */
572 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
573 if (s < 0) {
574 if (errno != EMFILE) return 0;
575 } else
576 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900577
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 /*
579 * if the servname does not match socktype/protocol, ignore it.
580 */
581 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900582
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900583 afd = find_afd(pai->ai_family);
584 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900585
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900586 if (pai->ai_flags & AI_PASSIVE) {
587 GET_AI(cur->ai_next, afd, afd->a_addrany);
588 /* xxx meaningless?
589 * GET_CANONNAME(cur->ai_next, "anyaddr");
590 */
591 GET_PORT(cur->ai_next, servname);
592 } else {
593 GET_AI(cur->ai_next, afd, afd->a_loopback);
594 /* xxx meaningless?
595 * GET_CANONNAME(cur->ai_next, "localhost");
596 */
597 GET_PORT(cur->ai_next, servname);
598 }
599 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 *res = sentinel.ai_next;
602 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900603
604free:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
606 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900607}
608
609/*
610 * numeric hostname
611 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900612static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
613 struct addrinfo** res, const char* canonname) {
614 const struct afd* afd;
615 struct addrinfo* cur;
616 struct addrinfo sentinel;
617 int error;
618 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900620 assert(pai != NULL);
621 /* hostname may be NULL */
622 /* servname may be NULL */
623 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900624
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 *res = NULL;
626 sentinel.ai_next = NULL;
627 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900628
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629 /*
630 * if the servname does not match socktype/protocol, ignore it.
631 */
632 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900633
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900634 afd = find_afd(pai->ai_family);
635 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900636
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900637 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900638#if 0 /*X/Open spec*/
639 case AF_INET:
640 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
641 if (pai->ai_family == afd->a_af ||
642 pai->ai_family == PF_UNSPEC /*?*/) {
643 GET_AI(cur->ai_next, afd, pton);
644 GET_PORT(cur->ai_next, servname);
645 if ((pai->ai_flags & AI_CANONNAME)) {
646 /*
647 * Set the numeric address itself as
648 * the canonical name, based on a
649 * clarification in rfc2553bis-03.
650 */
651 GET_CANONNAME(cur->ai_next, canonname);
652 }
653 while (cur && cur->ai_next)
654 cur = cur->ai_next;
655 } else
656 ERR(EAI_FAMILY); /*xxx*/
657 }
658 break;
659#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900660 default:
661 if (inet_pton(afd->a_af, hostname, pton) == 1) {
662 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
663 GET_AI(cur->ai_next, afd, pton);
664 GET_PORT(cur->ai_next, servname);
665 if ((pai->ai_flags & AI_CANONNAME)) {
666 /*
667 * Set the numeric address itself as
668 * the canonical name, based on a
669 * clarification in rfc2553bis-03.
670 */
671 GET_CANONNAME(cur->ai_next, canonname);
672 }
673 while (cur->ai_next) cur = cur->ai_next;
674 } else
675 ERR(EAI_FAMILY); /*xxx*/
676 }
677 break;
678 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680 *res = sentinel.ai_next;
681 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900682
683free:
684bad:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900685 if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
686 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900687}
688
689/*
690 * numeric hostname with scope
691 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
693 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 const struct afd* afd;
695 struct addrinfo* cur;
696 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900697 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900699
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 assert(pai != NULL);
701 /* hostname may be NULL */
702 /* servname may be NULL */
703 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900704
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900705 /*
706 * if the servname does not match socktype/protocol, ignore it.
707 */
708 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900709
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900710 afd = find_afd(pai->ai_family);
711 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715 cp = strchr(hostname, SCOPE_DELIMITER);
716 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900718 /*
719 * Handle special case of <scoped_address><delimiter><scope id>
720 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900721 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900722 if (hostname2 == NULL) return EAI_MEMORY;
723 /* terminate at the delimiter */
724 hostname2[cp - hostname] = '\0';
725 addr = hostname2;
726 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900728 error = explore_numeric(pai, addr, servname, res, hostname);
729 if (error == 0) {
730 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732 for (cur = *res; cur; cur = cur->ai_next) {
733 if (cur->ai_family != AF_INET6) continue;
734 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
735 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
736 free(hostname2);
737 return (EAI_NODATA); /* XXX: is return OK? */
738 }
739 sin6->sin6_scope_id = scopeid;
740 }
741 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900743 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900745 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900746}
747
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900748static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
749 assert(pai != NULL);
750 assert(ai != NULL);
751 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 if ((pai->ai_flags & AI_CANONNAME) != 0) {
754 ai->ai_canonname = strdup(str);
755 if (ai->ai_canonname == NULL) return EAI_MEMORY;
756 }
757 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900758}
759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
761 const char* addr) {
762 char* p;
763 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900764
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765 assert(pai != NULL);
766 assert(afd != NULL);
767 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900768
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900769 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
770 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900772 memcpy(ai, pai, sizeof(struct addrinfo));
773 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
774 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 ai->ai_addrlen = afd->a_socklen;
777#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
778 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900779#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900780 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
781 p = (char*) (void*) (ai->ai_addr);
782 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
783 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900784}
785
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900786static int get_portmatch(const struct addrinfo* ai, const char* servname) {
787 assert(ai != NULL);
788 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900791}
792
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900793static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
794 const char* proto;
795 struct servent* sp;
796 int port;
797 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900798
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 assert(ai != NULL);
800 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900801
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900802 if (servname == NULL) return 0;
803 switch (ai->ai_family) {
804 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900805 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900806 break;
807 default:
808 return 0;
809 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900810
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811 switch (ai->ai_socktype) {
812 case SOCK_RAW:
813 return EAI_SERVICE;
814 case SOCK_DGRAM:
815 case SOCK_STREAM:
816 allownumeric = 1;
817 break;
818 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820 break;
821 default:
822 return EAI_SOCKTYPE;
823 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825 port = str2number(servname);
826 if (port >= 0) {
827 if (!allownumeric) return EAI_SERVICE;
828 if (port < 0 || port > 65535) return EAI_SERVICE;
829 port = htons(port);
830 } else {
831 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900832
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900833 switch (ai->ai_socktype) {
834 case SOCK_DGRAM:
835 proto = "udp";
836 break;
837 case SOCK_STREAM:
838 proto = "tcp";
839 break;
840 default:
841 proto = NULL;
842 break;
843 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900844
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
846 port = sp->s_port;
847 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900848
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849 if (!matchonly) {
850 switch (ai->ai_family) {
851 case AF_INET:
852 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
853 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900854 case AF_INET6:
855 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
856 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900857 }
858 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900859
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900860 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900861}
862
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900863static const struct afd* find_afd(int af) {
864 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900865
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866 if (af == PF_UNSPEC) return NULL;
867 for (afd = afdl; afd->a_af; afd++) {
868 if (afd->a_af == af) return afd;
869 }
870 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900871}
872
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900873// Convert a string to a scope identifier.
874static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 u_long lscopeid;
876 struct in6_addr* a6;
877 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 assert(scope != NULL);
880 assert(sin6 != NULL);
881 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900882
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900883 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900884
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900885 /* empty scopeid portion is invalid */
886 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900887
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900888 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
889 /*
890 * We currently assume a one-to-one mapping between links
891 * and interfaces, so we simply use interface indices for
892 * like-local scopes.
893 */
894 *scopeid = if_nametoindex(scope);
895 if (*scopeid == 0) goto trynumeric;
896 return 0;
897 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900898
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 /* still unclear about literal, allow numeric only - placeholder */
900 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
901 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
902 goto trynumeric;
903 else
904 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900905
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 /* try to convert to a numeric id as a last resort */
907trynumeric:
908 errno = 0;
909 lscopeid = strtoul(scope, &ep, 10);
910 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
911 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
912 return 0;
913 else
914 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900915}
Bernie Innocenti55864192018-08-30 04:05:20 +0900916
917/* code duplicate with gethnamaddr.c */
918
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900919static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900920
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900921#define BOUNDED_INCR(x) \
922 do { \
923 BOUNDS_CHECK(cp, x); \
924 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900925 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900926
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900927#define BOUNDS_CHECK(ptr, count) \
928 do { \
929 if (eom - (ptr) < (count)) { \
930 h_errno = NO_RECOVERY; \
931 return NULL; \
932 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900933 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900934
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900935static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
936 const struct addrinfo* pai) {
937 struct addrinfo sentinel, *cur;
938 struct addrinfo ai;
939 const struct afd* afd;
940 char* canonname;
941 const HEADER* hp;
942 const u_char* cp;
943 int n;
944 const u_char* eom;
945 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900946 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 int haveanswer, had_error;
948 char tbuf[MAXDNAME];
949 int (*name_ok)(const char*);
950 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900951
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900952 assert(answer != NULL);
953 assert(qname != NULL);
954 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900955
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900956 memset(&sentinel, 0, sizeof(sentinel));
957 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900958
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900959 canonname = NULL;
960 eom = answer->buf + anslen;
961 switch (qtype) {
962 case T_A:
963 case T_AAAA:
964 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
965 name_ok = res_hnok;
966 break;
967 default:
968 return NULL; /* XXX should be abort(); */
969 }
970 /*
971 * find first satisfactory answer
972 */
973 hp = &answer->hdr;
974 ancount = ntohs(hp->ancount);
975 qdcount = ntohs(hp->qdcount);
976 bp = hostbuf;
977 ep = hostbuf + sizeof hostbuf;
978 cp = answer->buf;
979 BOUNDED_INCR(HFIXEDSZ);
980 if (qdcount != 1) {
981 h_errno = NO_RECOVERY;
982 return (NULL);
983 }
984 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
985 if ((n < 0) || !(*name_ok)(bp)) {
986 h_errno = NO_RECOVERY;
987 return (NULL);
988 }
989 BOUNDED_INCR(n + QFIXEDSZ);
990 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
991 /* res_send() has already verified that the query name is the
992 * same as the one we sent; this just gets the expanded name
993 * (i.e., with the succeeding search-domain tacked on).
994 */
995 n = strlen(bp) + 1; /* for the \0 */
996 if (n >= MAXHOSTNAMELEN) {
997 h_errno = NO_RECOVERY;
998 return (NULL);
999 }
1000 canonname = bp;
1001 bp += n;
1002 /* The qname can be abbreviated, but h_name is now absolute. */
1003 qname = canonname;
1004 }
1005 haveanswer = 0;
1006 had_error = 0;
1007 while (ancount-- > 0 && cp < eom && !had_error) {
1008 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1009 if ((n < 0) || !(*name_ok)(bp)) {
1010 had_error++;
1011 continue;
1012 }
1013 cp += n; /* name */
1014 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1015 type = _getshort(cp);
1016 cp += INT16SZ; /* type */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001017 int cl = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001018 cp += INT16SZ + INT32SZ; /* class, TTL */
1019 n = _getshort(cp);
1020 cp += INT16SZ; /* len */
1021 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001022 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001023 /* XXX - debug? syslog? */
1024 cp += n;
1025 continue; /* XXX - had_error++ ? */
1026 }
1027 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1028 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1029 if ((n < 0) || !(*name_ok)(tbuf)) {
1030 had_error++;
1031 continue;
1032 }
1033 cp += n;
1034 /* Get canonical name. */
1035 n = strlen(tbuf) + 1; /* for the \0 */
1036 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1037 had_error++;
1038 continue;
1039 }
1040 strlcpy(bp, tbuf, (size_t)(ep - bp));
1041 canonname = bp;
1042 bp += n;
1043 continue;
1044 }
1045 if (qtype == T_ANY) {
1046 if (!(type == T_A || type == T_AAAA)) {
1047 cp += n;
1048 continue;
1049 }
1050 } else if (type != qtype) {
1051 if (type != T_KEY && type != T_SIG)
1052 syslog(LOG_NOTICE | LOG_AUTH,
1053 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1054 p_class(C_IN), p_type(qtype), p_type(type));
1055 cp += n;
1056 continue; /* XXX - had_error++ ? */
1057 }
1058 switch (type) {
1059 case T_A:
1060 case T_AAAA:
1061 if (strcasecmp(canonname, bp) != 0) {
1062 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1063 cp += n;
1064 continue; /* XXX - had_error++ ? */
1065 }
1066 if (type == T_A && n != INADDRSZ) {
1067 cp += n;
1068 continue;
1069 }
1070 if (type == T_AAAA && n != IN6ADDRSZ) {
1071 cp += n;
1072 continue;
1073 }
1074 if (type == T_AAAA) {
1075 struct in6_addr in6;
1076 memcpy(&in6, cp, IN6ADDRSZ);
1077 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1078 cp += n;
1079 continue;
1080 }
1081 }
1082 if (!haveanswer) {
1083 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001084
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001085 canonname = bp;
1086 nn = strlen(bp) + 1; /* for the \0 */
1087 bp += nn;
1088 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001089
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001090 /* don't overwrite pai */
1091 ai = *pai;
1092 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1093 afd = find_afd(ai.ai_family);
1094 if (afd == NULL) {
1095 cp += n;
1096 continue;
1097 }
1098 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1099 if (cur->ai_next == NULL) had_error++;
1100 while (cur && cur->ai_next) cur = cur->ai_next;
1101 cp += n;
1102 break;
1103 default:
1104 abort();
1105 }
1106 if (!had_error) haveanswer++;
1107 }
1108 if (haveanswer) {
1109 if (!canonname)
1110 (void) get_canonname(pai, sentinel.ai_next, qname);
1111 else
1112 (void) get_canonname(pai, sentinel.ai_next, canonname);
1113 h_errno = NETDB_SUCCESS;
1114 return sentinel.ai_next;
1115 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001116
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001117 h_errno = NO_RECOVERY;
1118 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001119}
1120
1121struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001122 struct addrinfo* ai;
1123 int has_src_addr;
1124 sockaddr_union src_addr;
1125 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001126};
1127
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001128static int _get_scope(const struct sockaddr* addr) {
1129 if (addr->sa_family == AF_INET6) {
1130 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1131 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1132 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1133 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1134 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1135 /*
1136 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1137 * link-local scope.
1138 */
1139 return IPV6_ADDR_SCOPE_LINKLOCAL;
1140 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1141 return IPV6_ADDR_SCOPE_SITELOCAL;
1142 } else {
1143 return IPV6_ADDR_SCOPE_GLOBAL;
1144 }
1145 } else if (addr->sa_family == AF_INET) {
1146 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1147 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001148
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001149 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1150 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1151 return IPV6_ADDR_SCOPE_LINKLOCAL;
1152 } else {
1153 /*
1154 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1155 * and shared addresses (100.64.0.0/10), are assigned global scope.
1156 */
1157 return IPV6_ADDR_SCOPE_GLOBAL;
1158 }
1159 } else {
1160 /*
1161 * This should never happen.
1162 * Return a scope with low priority as a last resort.
1163 */
1164 return IPV6_ADDR_SCOPE_NODELOCAL;
1165 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001166}
1167
1168/* These macros are modelled after the ones in <netinet/in6.h>. */
1169
1170/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001171#define IN6_IS_ADDR_TEREDO(a) \
1172 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001173
1174/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001175#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001176
1177/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001178#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001179
1180/*
1181 * Get the label for a given IPv4/IPv6 address.
1182 * RFC 6724, section 2.1.
1183 */
1184
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001185static int _get_label(const struct sockaddr* addr) {
1186 if (addr->sa_family == AF_INET) {
1187 return 4;
1188 } else if (addr->sa_family == AF_INET6) {
1189 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1190 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1191 return 0;
1192 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1193 return 4;
1194 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1195 return 2;
1196 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1197 return 5;
1198 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1199 return 13;
1200 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1201 return 3;
1202 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1203 return 11;
1204 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1205 return 12;
1206 } else {
1207 /* All other IPv6 addresses, including global unicast addresses. */
1208 return 1;
1209 }
1210 } else {
1211 /*
1212 * This should never happen.
1213 * Return a semi-random label as a last resort.
1214 */
1215 return 1;
1216 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001217}
1218
1219/*
1220 * Get the precedence for a given IPv4/IPv6 address.
1221 * RFC 6724, section 2.1.
1222 */
1223
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001224static int _get_precedence(const struct sockaddr* addr) {
1225 if (addr->sa_family == AF_INET) {
1226 return 35;
1227 } else if (addr->sa_family == AF_INET6) {
1228 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1229 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1230 return 50;
1231 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1232 return 35;
1233 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1234 return 30;
1235 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1236 return 5;
1237 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1238 return 3;
1239 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1240 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1241 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1242 return 1;
1243 } else {
1244 /* All other IPv6 addresses, including global unicast addresses. */
1245 return 40;
1246 }
1247 } else {
1248 return 1;
1249 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001250}
1251
1252/*
1253 * Find number of matching initial bits between the two addresses a1 and a2.
1254 */
1255
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001256static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1257 const char* p1 = (const char*) a1;
1258 const char* p2 = (const char*) a2;
1259 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001260
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001261 for (i = 0; i < sizeof(*a1); ++i) {
1262 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001263
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001264 if (p1[i] == p2[i]) {
1265 continue;
1266 }
1267 x = p1[i] ^ p2[i];
1268 for (j = 0; j < CHAR_BIT; ++j) {
1269 if (x & (1 << (CHAR_BIT - 1))) {
1270 return i * CHAR_BIT + j;
1271 }
1272 x <<= 1;
1273 }
1274 }
1275 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001276}
1277
1278/*
1279 * Compare two source/destination address pairs.
1280 * RFC 6724, section 6.
1281 */
1282
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001283static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1284 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1285 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1286 int scope_src1, scope_dst1, scope_match1;
1287 int scope_src2, scope_dst2, scope_match2;
1288 int label_src1, label_dst1, label_match1;
1289 int label_src2, label_dst2, label_match2;
1290 int precedence1, precedence2;
1291 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001292
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001293 /* Rule 1: Avoid unusable destinations. */
1294 if (a1->has_src_addr != a2->has_src_addr) {
1295 return a2->has_src_addr - a1->has_src_addr;
1296 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001297
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001298 /* Rule 2: Prefer matching scope. */
1299 scope_src1 = _get_scope(&a1->src_addr.generic);
1300 scope_dst1 = _get_scope(a1->ai->ai_addr);
1301 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001302
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001303 scope_src2 = _get_scope(&a2->src_addr.generic);
1304 scope_dst2 = _get_scope(a2->ai->ai_addr);
1305 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001306
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001307 if (scope_match1 != scope_match2) {
1308 return scope_match2 - scope_match1;
1309 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001310
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001311 /*
1312 * Rule 3: Avoid deprecated addresses.
1313 * TODO(sesse): We don't currently have a good way of finding this.
1314 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001315
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001316 /*
1317 * Rule 4: Prefer home addresses.
1318 * TODO(sesse): We don't currently have a good way of finding this.
1319 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001320
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001321 /* Rule 5: Prefer matching label. */
1322 label_src1 = _get_label(&a1->src_addr.generic);
1323 label_dst1 = _get_label(a1->ai->ai_addr);
1324 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001325
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001326 label_src2 = _get_label(&a2->src_addr.generic);
1327 label_dst2 = _get_label(a2->ai->ai_addr);
1328 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001329
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001330 if (label_match1 != label_match2) {
1331 return label_match2 - label_match1;
1332 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001333
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001334 /* Rule 6: Prefer higher precedence. */
1335 precedence1 = _get_precedence(a1->ai->ai_addr);
1336 precedence2 = _get_precedence(a2->ai->ai_addr);
1337 if (precedence1 != precedence2) {
1338 return precedence2 - precedence1;
1339 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001340
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001341 /*
1342 * Rule 7: Prefer native transport.
1343 * TODO(sesse): We don't currently have a good way of finding this.
1344 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001345
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001346 /* Rule 8: Prefer smaller scope. */
1347 if (scope_dst1 != scope_dst2) {
1348 return scope_dst1 - scope_dst2;
1349 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001350
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001351 /*
1352 * Rule 9: Use longest matching prefix.
1353 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1354 * to work very well directly applied to IPv4. (glibc uses information from
1355 * the routing table for a custom IPv4 implementation here.)
1356 */
1357 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1358 a2->ai->ai_addr->sa_family == AF_INET6) {
1359 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1360 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1361 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1362 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1363 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1364 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1365 if (prefixlen1 != prefixlen2) {
1366 return prefixlen2 - prefixlen1;
1367 }
1368 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001369
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001370 /*
1371 * Rule 10: Leave the order unchanged.
1372 * We need this since qsort() is not necessarily stable.
1373 */
1374 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001375}
1376
1377/*
1378 * Find the source address that will be used if trying to connect to the given
1379 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1380 *
1381 * Returns 1 if a source address was found, 0 if the address is unreachable,
1382 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1383 * undefined.
1384 */
1385
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001386static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1387 uid_t uid) {
1388 int sock;
1389 int ret;
1390 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001391
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001392 switch (addr->sa_family) {
1393 case AF_INET:
1394 len = sizeof(struct sockaddr_in);
1395 break;
1396 case AF_INET6:
1397 len = sizeof(struct sockaddr_in6);
1398 break;
1399 default:
1400 /* No known usable source address for non-INET families. */
1401 return 0;
1402 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001403
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001404 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1405 if (sock == -1) {
1406 if (errno == EAFNOSUPPORT) {
1407 return 0;
1408 } else {
1409 return -1;
1410 }
1411 }
1412 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1413 close(sock);
1414 return 0;
1415 }
1416 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1417 close(sock);
1418 return 0;
1419 }
1420 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001421 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001422 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001423
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001424 if (ret == -1) {
1425 close(sock);
1426 return 0;
1427 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001428
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001429 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1430 close(sock);
1431 return -1;
1432 }
1433 close(sock);
1434 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001435}
1436
1437/*
1438 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1439 * Will leave the list unchanged if an error occurs.
1440 */
1441
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001442static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1443 struct addrinfo* cur;
1444 int nelem = 0, i;
1445 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001446
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001447 cur = list_sentinel->ai_next;
1448 while (cur) {
1449 ++nelem;
1450 cur = cur->ai_next;
1451 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001452
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001453 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1454 if (elems == NULL) {
1455 goto error;
1456 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001457
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001458 /*
1459 * Convert the linked list to an array that also contains the candidate
1460 * source address for each destination address.
1461 */
1462 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1463 int has_src_addr;
1464 assert(cur != NULL);
1465 elems[i].ai = cur;
1466 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001467
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001468 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1469 if (has_src_addr == -1) {
1470 goto error;
1471 }
1472 elems[i].has_src_addr = has_src_addr;
1473 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001475 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1476 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001477
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001478 list_sentinel->ai_next = elems[0].ai;
1479 for (i = 0; i < nelem - 1; ++i) {
1480 elems[i].ai->ai_next = elems[i + 1].ai;
1481 }
1482 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001483
1484error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001485 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001486}
1487
Bernie Innocenti948f6572018-09-12 21:32:42 +09001488static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1489 const android_net_context* netcontext, addrinfo** rv) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001490 struct addrinfo* ai;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001491 struct addrinfo sentinel, *cur;
1492 struct res_target q, q2;
1493 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001494
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001495 memset(&q, 0, sizeof(q));
1496 memset(&q2, 0, sizeof(q2));
1497 memset(&sentinel, 0, sizeof(sentinel));
1498 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001499
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001500 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001501 if (buf == NULL) {
1502 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001503 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001504 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001505 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001506 if (buf2 == NULL) {
1507 free(buf);
1508 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001509 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001510 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001511
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001512 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001513 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001514 /* prefer IPv6 */
1515 q.name = name;
1516 q.qclass = C_IN;
1517 q.answer = buf->buf;
1518 q.anslen = sizeof(buf->buf);
1519 int query_ipv6 = 1, query_ipv4 = 1;
1520 if (pai->ai_flags & AI_ADDRCONFIG) {
1521 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1522 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1523 }
1524 if (query_ipv6) {
1525 q.qtype = T_AAAA;
1526 if (query_ipv4) {
1527 q.next = &q2;
1528 q2.name = name;
1529 q2.qclass = C_IN;
1530 q2.qtype = T_A;
1531 q2.answer = buf2->buf;
1532 q2.anslen = sizeof(buf2->buf);
1533 }
1534 } else if (query_ipv4) {
1535 q.qtype = T_A;
1536 } else {
1537 free(buf);
1538 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001539 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001540 }
1541 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001542 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001543 case AF_INET:
1544 q.name = name;
1545 q.qclass = C_IN;
1546 q.qtype = T_A;
1547 q.answer = buf->buf;
1548 q.anslen = sizeof(buf->buf);
1549 break;
1550 case AF_INET6:
1551 q.name = name;
1552 q.qclass = C_IN;
1553 q.qtype = T_AAAA;
1554 q.answer = buf->buf;
1555 q.anslen = sizeof(buf->buf);
1556 break;
1557 default:
1558 free(buf);
1559 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001560 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001561 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001562
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001563 res = __res_get_state();
1564 if (res == NULL) {
1565 free(buf);
1566 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001567 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001568 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001569
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001570 /* this just sets our netid val in the thread private data so we don't have to
1571 * modify the api's all the way down to res_send.c's res_nsend. We could
1572 * fully populate the thread private data here, but if we get down there
1573 * and have a cache hit that would be wasted, so we do the rest there on miss
1574 */
1575 res_setnetcontext(res, netcontext);
1576 if (res_searchN(name, &q, res) < 0) {
1577 __res_put_state(res);
1578 free(buf);
1579 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001580 return EAI_NODATA; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001581 }
1582 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1583 if (ai) {
1584 cur->ai_next = ai;
1585 while (cur && cur->ai_next) cur = cur->ai_next;
1586 }
1587 if (q.next) {
1588 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1589 if (ai) cur->ai_next = ai;
1590 }
1591 free(buf);
1592 free(buf2);
1593 if (sentinel.ai_next == NULL) {
1594 __res_put_state(res);
1595 switch (h_errno) {
1596 case HOST_NOT_FOUND:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001597 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001598 case TRY_AGAIN:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001599 return EAI_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001600 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001601 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001602 }
1603 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001605 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001606
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001607 __res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001608
Bernie Innocenti948f6572018-09-12 21:32:42 +09001609 *rv = sentinel.ai_next;
1610 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001611}
1612
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001613static void _sethtent(FILE** hostf) {
1614 if (!*hostf)
1615 *hostf = fopen(_PATH_HOSTS, "re");
1616 else
1617 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001618}
1619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001620static void _endhtent(FILE** hostf) {
1621 if (*hostf) {
1622 (void) fclose(*hostf);
1623 *hostf = NULL;
1624 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001625}
1626
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001627static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1628 char* p;
1629 char *cp, *tname, *cname;
1630 struct addrinfo hints, *res0, *res;
1631 int error;
1632 const char* addr;
1633 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001634
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001635 assert(name != NULL);
1636 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001637
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001638 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1639again:
1640 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1641 if (*p == '#') goto again;
1642 if (!(cp = strpbrk(p, "#\n"))) goto again;
1643 *cp = '\0';
1644 if (!(cp = strpbrk(p, " \t"))) goto again;
1645 *cp++ = '\0';
1646 addr = p;
1647 /* if this is not something we're looking for, skip it. */
1648 cname = NULL;
1649 while (cp && *cp) {
1650 if (*cp == ' ' || *cp == '\t') {
1651 cp++;
1652 continue;
1653 }
1654 if (!cname) cname = cp;
1655 tname = cp;
1656 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1657 // fprintf(stderr, "\ttname = '%s'", tname);
1658 if (strcasecmp(name, tname) == 0) goto found;
1659 }
1660 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001661
1662found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001663 hints = *pai;
1664 hints.ai_flags = AI_NUMERICHOST;
1665 error = getaddrinfo(addr, NULL, &hints, &res0);
1666 if (error) goto again;
1667 for (res = res0; res; res = res->ai_next) {
1668 /* cover it up */
1669 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001670
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001671 if (pai->ai_flags & AI_CANONNAME) {
1672 if (get_canonname(pai, res, cname) != 0) {
1673 freeaddrinfo(res0);
1674 goto again;
1675 }
1676 }
1677 }
1678 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001679}
1680
Bernie Innocenti948f6572018-09-12 21:32:42 +09001681static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001682 struct addrinfo sentinel, *cur;
1683 struct addrinfo* p;
1684 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001685
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001686 memset(&sentinel, 0, sizeof(sentinel));
1687 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001689 _sethtent(&hostf);
1690 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1691 cur->ai_next = p;
1692 while (cur && cur->ai_next) cur = cur->ai_next;
1693 }
1694 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001695
Bernie Innocenti948f6572018-09-12 21:32:42 +09001696 *res = sentinel.ai_next;
1697 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001698}
1699
1700/* resolver logic */
1701
1702/*
1703 * Formulate a normal query, send, and await answer.
1704 * Returned answer is placed in supplied buffer "answer".
1705 * Perform preliminary check of answer, returning success only
1706 * if no error is indicated and the answer count is nonzero.
1707 * Return the size of the response on success, -1 on error.
1708 * Error number is left in h_errno.
1709 *
1710 * Caller must parse answer and determine whether it answers the question.
1711 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001712static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1713 res_state res) {
1714 u_char buf[MAXPACKET];
1715 HEADER* hp;
1716 int n;
1717 struct res_target* t;
1718 int rcode;
1719 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001720
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001721 assert(name != NULL);
1722 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001723
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001724 rcode = NOERROR;
1725 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001727 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001728 u_char* answer;
1729 int anslen;
1730 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001732 hp = (HEADER*) (void*) t->answer;
1733 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001735 again:
1736 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001738 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001739 int cl = t->qclass;
1740 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001741 answer = t->answer;
1742 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001743#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001744 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001745#endif
1746
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001747 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001748 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1749 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1750 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001751 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001752#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001753 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001754#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001755 h_errno = NO_RECOVERY;
1756 return n;
1757 }
1758 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001760 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1761 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001762 /* if the query choked with EDNS0, retry without EDNS0 */
1763 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1764 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1765 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001766#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001767 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001768#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001769 goto again;
1770 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001771#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001772 if (res->options & RES_DEBUG)
1773 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001774#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001775 continue;
1776 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001778 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001780 t->n = n;
1781 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001782
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001783 if (ancount == 0) {
1784 switch (rcode) {
1785 case NXDOMAIN:
1786 h_errno = HOST_NOT_FOUND;
1787 break;
1788 case SERVFAIL:
1789 h_errno = TRY_AGAIN;
1790 break;
1791 case NOERROR:
1792 h_errno = NO_DATA;
1793 break;
1794 case FORMERR:
1795 case NOTIMP:
1796 case REFUSED:
1797 default:
1798 h_errno = NO_RECOVERY;
1799 break;
1800 }
1801 return -1;
1802 }
1803 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001804}
1805
1806/*
1807 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1808 * Return the size of the response on success, -1 on error.
1809 * If enabled, implement search rules until answer or unrecoverable failure
1810 * is detected. Error code, if any, is left in h_errno.
1811 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001812static int res_searchN(const char* name, struct res_target* target, res_state res) {
1813 const char *cp, *const *domain;
1814 HEADER* hp;
1815 u_int dots;
1816 int trailing_dot, ret, saved_herrno;
1817 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001819 assert(name != NULL);
1820 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001822 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001824 errno = 0;
1825 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1826 dots = 0;
1827 for (cp = name; *cp; cp++) dots += (*cp == '.');
1828 trailing_dot = 0;
1829 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001831 // fprintf(stderr, "res_searchN() name = '%s'\n", name);
Bernie Innocenti55864192018-08-30 04:05:20 +09001832
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001833 /*
1834 * if there aren't any dots, it could be a user-level alias
1835 */
1836 if (!dots && (cp = __hostalias(name)) != NULL) {
1837 ret = res_queryN(cp, target, res);
1838 return ret;
1839 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001840
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001841 /*
1842 * If there are dots in the name already, let's just give it a try
1843 * 'as is'. The threshold can be set with the "ndots" option.
1844 */
1845 saved_herrno = -1;
1846 if (dots >= res->ndots) {
1847 ret = res_querydomainN(name, NULL, target, res);
1848 if (ret > 0) return (ret);
1849 saved_herrno = h_errno;
1850 tried_as_is++;
1851 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001853 /*
1854 * We do at least one level of search if
1855 * - there is no dot and RES_DEFNAME is set, or
1856 * - there is at least one dot, there is no trailing dot,
1857 * and RES_DNSRCH is set.
1858 */
1859 if ((!dots && (res->options & RES_DEFNAMES)) ||
1860 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1861 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001862
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001863 /* Unfortunately we need to set stuff up before
1864 * the domain stuff is tried. Will have a better
1865 * fix after thread pools are used.
1866 */
1867 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001868
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001869 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1870 ret = res_querydomainN(name, *domain, target, res);
1871 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001873 /*
1874 * If no server present, give up.
1875 * If name isn't found in this domain,
1876 * keep trying higher domains in the search list
1877 * (if that's enabled).
1878 * On a NO_DATA error, keep trying, otherwise
1879 * a wildcard entry of another type could keep us
1880 * from finding this entry higher in the domain.
1881 * If we get some other error (negative answer or
1882 * server failure), then stop searching up,
1883 * but try the input name below in case it's
1884 * fully-qualified.
1885 */
1886 if (errno == ECONNREFUSED) {
1887 h_errno = TRY_AGAIN;
1888 return -1;
1889 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001890
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001891 switch (h_errno) {
1892 case NO_DATA:
1893 got_nodata++;
1894 /* FALLTHROUGH */
1895 case HOST_NOT_FOUND:
1896 /* keep trying */
1897 break;
1898 case TRY_AGAIN:
1899 if (hp->rcode == SERVFAIL) {
1900 /* try next search element, if any */
1901 got_servfail++;
1902 break;
1903 }
1904 /* FALLTHROUGH */
1905 default:
1906 /* anything else implies that we're done */
1907 done++;
1908 }
1909 /*
1910 * if we got here for some reason other than DNSRCH,
1911 * we only wanted one iteration of the loop, so stop.
1912 */
1913 if (!(res->options & RES_DNSRCH)) done++;
1914 }
1915 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001916
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001917 /*
1918 * if we have not already tried the name "as is", do that now.
1919 * note that we do this regardless of how many dots were in the
1920 * name or whether it ends with a dot.
1921 */
1922 if (!tried_as_is) {
1923 ret = res_querydomainN(name, NULL, target, res);
1924 if (ret > 0) return ret;
1925 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001926
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001927 /*
1928 * if we got here, we didn't satisfy the search.
1929 * if we did an initial full query, return that query's h_errno
1930 * (note that we wouldn't be here if that query had succeeded).
1931 * else if we ever got a nodata, send that back as the reason.
1932 * else send back meaningless h_errno, that being the one from
1933 * the last DNSRCH we did.
1934 */
1935 if (saved_herrno != -1)
1936 h_errno = saved_herrno;
1937 else if (got_nodata)
1938 h_errno = NO_DATA;
1939 else if (got_servfail)
1940 h_errno = TRY_AGAIN;
1941 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001942}
1943
1944/*
1945 * Perform a call on res_query on the concatenation of name and domain,
1946 * removing a trailing dot from name if domain is NULL.
1947 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001948static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1949 res_state res) {
1950 char nbuf[MAXDNAME];
1951 const char* longname = nbuf;
1952 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001953
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001954 assert(name != NULL);
1955 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001956
1957#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001958 if (res->options & RES_DEBUG)
1959 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001960#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001961 if (domain == NULL) {
1962 /*
1963 * Check for trailing '.';
1964 * copy without '.' if present.
1965 */
1966 n = strlen(name);
1967 if (n + 1 > sizeof(nbuf)) {
1968 h_errno = NO_RECOVERY;
1969 return -1;
1970 }
1971 if (n > 0 && name[--n] == '.') {
1972 strncpy(nbuf, name, n);
1973 nbuf[n] = '\0';
1974 } else
1975 longname = name;
1976 } else {
1977 n = strlen(name);
1978 d = strlen(domain);
1979 if (n + 1 + d + 1 > sizeof(nbuf)) {
1980 h_errno = NO_RECOVERY;
1981 return -1;
1982 }
1983 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1984 }
1985 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001986}