blob: 123f609db5ba8c6a102a74a91af14800ae4d209c [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).
Bernie Innocenti55864192018-08-30 04:05:20 +090043 * Note:
44 * - We use getipnodebyname() just for thread-safeness. There's no intent
45 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
46 * getipnodebyname().
47 * - The code filters out AFs that are not supported by the kernel,
48 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
49 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
50 * in ai_flags?
51 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
52 * (1) what should we do against numeric hostname (2) what should we do
53 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
54 * non-loopback address configured? global address configured?
55 * - To avoid search order issue, we have a big amount of code duplicate
56 * from gethnamaddr.c and some other places. The issues that there's no
57 * lower layer function to lookup "IPv4 or IPv6" record. Calling
58 * gethostbyname2 from getaddrinfo will end up in wrong search order, as
59 * follows:
60 * - The code makes use of following calls when asked to resolver with
61 * ai_family = PF_UNSPEC:
62 * getipnodebyname(host, AF_INET6);
63 * getipnodebyname(host, AF_INET);
64 * This will result in the following queries if the node is configure to
65 * prefer /etc/hosts than DNS:
66 * lookup /etc/hosts for IPv6 address
67 * lookup DNS for IPv6 address
68 * lookup /etc/hosts for IPv4 address
69 * lookup DNS for IPv4 address
70 * which may not meet people's requirement.
71 * The right thing to happen is to have underlying layer which does
72 * PF_UNSPEC lookup (lookup both) and return chain of addrinfos.
73 * This would result in a bit of code duplicate with _dns_ghbyname() and
74 * friends.
75 */
76
Bernie Innocenti55864192018-08-30 04:05:20 +090077#include <arpa/inet.h>
78#include <arpa/nameser.h>
79#include <assert.h>
80#include <ctype.h>
81#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#include <fcntl.h>
83#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090084#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090086#include <stdbool.h>
87#include <stddef.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
91#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090092#include <sys/param.h>
93#include <sys/socket.h>
94#include <sys/stat.h>
95#include <sys/types.h>
96#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090097#include <unistd.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090098#include "resolv_cache.h"
99#include "resolv_netid.h"
100#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900101
Bernie Innocenti55864192018-08-30 04:05:20 +0900102#include <stdarg.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900103#include <syslog.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +0900104
Bernie Innocenti55864192018-08-30 04:05:20 +0900105
106typedef union sockaddr_union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900107 struct sockaddr generic;
108 struct sockaddr_in in;
Bernie Innocenti55864192018-08-30 04:05:20 +0900109 struct sockaddr_in6 in6;
110} sockaddr_union;
111
Bernie Innocenti55864192018-08-30 04:05:20 +0900112#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900113
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114static const char in_addrany[] = {0, 0, 0, 0};
115static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
117static 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 +0900118
Bernie Innocenti55864192018-08-30 04:05:20 +0900119static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 int a_af;
121 int a_addrlen;
122 int a_socklen;
123 int a_off;
124 const char* a_addrany;
125 const char* a_loopback;
126 int a_scoped;
127} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900128 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
129 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900130 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
131 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
132 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900133};
134
135struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900136 int e_af;
137 int e_socktype;
138 int e_protocol;
139 const char* e_protostr;
140 int e_wild;
141#define WILD_AF(ex) ((ex)->e_wild & 0x01)
142#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
143#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900144};
145
146static const struct explore explore[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
148 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
149 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
151 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
152 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
153 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
154 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
155 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
156 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900157};
158
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900159#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900161
162typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163 HEADER hdr;
164 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900165} querybuf;
166
167struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900168 struct res_target* next;
169 const char* name; /* domain name */
170 int qclass, qtype; /* class and type of query */
171 u_char* answer; /* buffer to put answer */
172 int anslen; /* size of answer buffer */
173 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900174};
175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900176static int str2number(const char*);
177static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
178 const struct android_net_context*);
179static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
180static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
181 const char*);
182static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
183 struct addrinfo**);
184static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
185static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
186static int get_portmatch(const struct addrinfo*, const char*);
187static int get_port(const struct addrinfo*, const char*, int);
188static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900189static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900192static int dns_getaddrinfo(const char* name, const addrinfo* pai,
193 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194static void _sethtent(FILE**);
195static void _endhtent(FILE**);
196static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900197static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900199
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900200static int res_queryN(const char*, struct res_target*, res_state);
201static int res_searchN(const char*, struct res_target*, res_state);
202static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900203
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204static const char* const ai_errlist[] = {
205 "Success",
206 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
207 "Temporary failure in name resolution", /* EAI_AGAIN */
208 "Invalid value for ai_flags", /* EAI_BADFLAGS */
209 "Non-recoverable failure in name resolution", /* EAI_FAIL */
210 "ai_family not supported", /* EAI_FAMILY */
211 "Memory allocation failure", /* EAI_MEMORY */
212 "No address associated with hostname", /* EAI_NODATA */
213 "hostname nor servname provided, or not known", /* EAI_NONAME */
214 "servname not supported for ai_socktype", /* EAI_SERVICE */
215 "ai_socktype not supported", /* EAI_SOCKTYPE */
216 "System error returned in errno", /* EAI_SYSTEM */
217 "Invalid value for hints", /* EAI_BADHINTS */
218 "Resolved protocol is unknown", /* EAI_PROTOCOL */
219 "Argument buffer overflow", /* EAI_OVERFLOW */
220 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900221};
222
223/* XXX macros that make external reference is BAD. */
224
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900225#define GET_AI(ai, afd, addr) \
226 do { \
227 /* external reference: pai, error, and label free */ \
228 (ai) = get_ai(pai, (afd), (addr)); \
229 if ((ai) == NULL) { \
230 error = EAI_MEMORY; \
231 goto free; \
232 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900233 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235#define GET_PORT(ai, serv) \
236 do { \
237 /* external reference: error and label free */ \
238 error = get_port((ai), (serv), 0); \
239 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900240 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900241
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242#define GET_CANONNAME(ai, str) \
243 do { \
244 /* external reference: pai, error and label free */ \
245 error = get_canonname(pai, (ai), (str)); \
246 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900247 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900248
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900249#define ERR(err) \
250 do { \
251 /* external reference: error, and label bad */ \
252 error = (err); \
253 goto bad; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900254 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900255
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900256#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900257 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
258#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900259
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900260const char* gai_strerror(int ecode) {
261 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
262 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900263}
264
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900265void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900266 while (ai) {
267 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900268 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900269 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270 free(ai);
271 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900272 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900273}
274
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900275static int str2number(const char* p) {
276 char* ep;
277 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900278
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900279 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900280
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900281 if (*p == '\0') return -1;
282 ep = NULL;
283 errno = 0;
284 v = strtoul(p, &ep, 10);
285 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
286 return v;
287 else
288 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900289}
290
291/*
292 * The following functions determine whether IPv4 or IPv6 connectivity is
293 * available in order to implement AI_ADDRCONFIG.
294 *
295 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
296 * available, but whether addresses of the specified family are "configured
297 * on the local system". However, bionic doesn't currently support getifaddrs,
298 * so checking for connectivity is the next best thing.
299 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900300static int _have_ipv6(unsigned mark, uid_t uid) {
301 static const struct sockaddr_in6 sin6_test = {
302 .sin6_family = AF_INET6,
303 .sin6_addr.s6_addr = {// 2000::
304 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
305 sockaddr_union addr = {.in6 = sin6_test};
306 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900307}
308
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900309static int _have_ipv4(unsigned mark, uid_t uid) {
310 static const struct sockaddr_in sin_test = {
311 .sin_family = AF_INET,
312 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
313 };
314 sockaddr_union addr = {.in = sin_test};
315 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900316}
317
318bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900319 int32_t tmp;
320 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
321 return false;
322 }
323 *result = ntohl(tmp);
324 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900325}
326
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900327int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
328 struct addrinfo** res) {
329 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900330}
331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900332int android_getaddrinfofornet(const char* hostname, const char* servname,
333 const struct addrinfo* hints, unsigned netid, unsigned mark,
334 struct addrinfo** res) {
335 struct android_net_context netcontext = {
336 .app_netid = netid,
337 .app_mark = mark,
338 .dns_netid = netid,
339 .dns_mark = mark,
340 .uid = NET_CONTEXT_INVALID_UID,
341 };
342 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900343}
344
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900345int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
346 const struct addrinfo* hints,
347 const struct android_net_context* netcontext,
348 struct addrinfo** res) {
349 struct addrinfo sentinel;
350 struct addrinfo* cur;
351 int error = 0;
352 struct addrinfo ai;
353 struct addrinfo ai0;
354 struct addrinfo* pai;
355 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900356
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900357 /* hostname is allowed to be NULL */
358 /* servname is allowed to be NULL */
359 /* hints is allowed to be NULL */
360 assert(res != NULL);
361 assert(netcontext != NULL);
362 memset(&sentinel, 0, sizeof(sentinel));
363 cur = &sentinel;
364 pai = &ai;
365 pai->ai_flags = 0;
366 pai->ai_family = PF_UNSPEC;
367 pai->ai_socktype = ANY;
368 pai->ai_protocol = ANY;
369 pai->ai_addrlen = 0;
370 pai->ai_canonname = NULL;
371 pai->ai_addr = NULL;
372 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900373
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900374 if (hostname == NULL && servname == NULL) return EAI_NONAME;
375 if (hints) {
376 /* error check for hints */
377 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
378 ERR(EAI_BADHINTS); /* xxx */
379 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
380 switch (hints->ai_family) {
381 case PF_UNSPEC:
382 case PF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900383 case PF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900384 break;
385 default:
386 ERR(EAI_FAMILY);
387 }
388 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900389
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 /*
391 * if both socktype/protocol are specified, check if they
392 * are meaningful combination.
393 */
394 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
395 for (ex = explore; ex->e_af >= 0; ex++) {
396 if (pai->ai_family != ex->e_af) continue;
397 if (ex->e_socktype == ANY) continue;
398 if (ex->e_protocol == ANY) continue;
399 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
400 ERR(EAI_BADHINTS);
401 }
402 }
403 }
404 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900405
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900406 /*
407 * check for special cases. (1) numeric servname is disallowed if
408 * socktype/protocol are left unspecified. (2) servname is disallowed
409 * for raw and other inet{,6} sockets.
410 */
411 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900413 ) {
414 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900415
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900416 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900417 pai->ai_family = PF_INET6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900418 }
419 error = get_portmatch(pai, servname);
420 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900421
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900422 *pai = ai0;
423 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900424
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900426
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900427 /* NULL hostname, or numeric hostname */
428 for (ex = explore; ex->e_af >= 0; ex++) {
429 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900430
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900431 /* PF_UNSPEC entries are prepared for DNS queries only */
432 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900433
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900434 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
435 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
436 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900437
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900438 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
439 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
440 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900441
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900442 if (hostname == NULL)
443 error = explore_null(pai, servname, &cur->ai_next);
444 else
445 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900446
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900447 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900448
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449 while (cur->ai_next) cur = cur->ai_next;
450 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900451
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900452 /*
453 * XXX
454 * If numeric representation of AF1 can be interpreted as FQDN
455 * representation of AF2, we need to think again about the code below.
456 */
457 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900458
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 if (hostname == NULL) ERR(EAI_NODATA);
460 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900461
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900462 /*
463 * hostname as alphabetical name.
464 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
465 * outer loop by AFs.
466 */
467 for (ex = explore; ex->e_af >= 0; ex++) {
468 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900469
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900470 /* require exact match for family field */
471 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900472
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900473 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
474 continue;
475 }
476 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
477 continue;
478 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900479
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 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 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485 while (cur && cur->ai_next) cur = cur->ai_next;
486 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900487
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 /* XXX */
489 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900490
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900491 if (error) goto free;
492 if (error == 0) {
493 if (sentinel.ai_next) {
494 good:
495 *res = sentinel.ai_next;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900496 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900497 } else
498 error = EAI_FAIL;
499 }
500free:
501bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900502 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 *res = NULL;
504 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900505}
506
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900507// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900508static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
509 struct addrinfo** res, const struct android_net_context* netcontext) {
510 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900512
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 assert(pai != NULL);
514 /* hostname may be NULL */
515 /* servname may be NULL */
516 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900517
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900518 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900519
Bernie Innocenti948f6572018-09-12 21:32:42 +0900520 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900522
Bernie Innocenti948f6572018-09-12 21:32:42 +0900523 if (!files_getaddrinfo(hostname, pai, &result)) {
524 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900526 if (!error) {
527 struct addrinfo* cur;
528 for (cur = result; cur; cur = cur->ai_next) {
529 GET_PORT(cur, servname);
530 /* canonname should be filled already */
531 }
532 *res = result;
533 return 0;
534 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900535
536free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900537 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900539}
540
541/*
542 * hostname == NULL.
543 * passive socket -> anyaddr (0.0.0.0 or ::)
544 * non-passive socket -> localhost (127.0.0.1 or ::1)
545 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
547 int s;
548 const struct afd* afd;
549 struct addrinfo* cur;
550 struct addrinfo sentinel;
551 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900552
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553 assert(pai != NULL);
554 /* servname may be NULL */
555 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900556
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900557 *res = NULL;
558 sentinel.ai_next = NULL;
559 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900560
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900561 /*
562 * filter out AFs that are not supported by the kernel
563 * XXX errno?
564 */
565 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
566 if (s < 0) {
567 if (errno != EMFILE) return 0;
568 } else
569 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900570
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 /*
572 * if the servname does not match socktype/protocol, ignore it.
573 */
574 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900575
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900576 afd = find_afd(pai->ai_family);
577 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900578
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900579 if (pai->ai_flags & AI_PASSIVE) {
580 GET_AI(cur->ai_next, afd, afd->a_addrany);
581 /* xxx meaningless?
582 * GET_CANONNAME(cur->ai_next, "anyaddr");
583 */
584 GET_PORT(cur->ai_next, servname);
585 } else {
586 GET_AI(cur->ai_next, afd, afd->a_loopback);
587 /* xxx meaningless?
588 * GET_CANONNAME(cur->ai_next, "localhost");
589 */
590 GET_PORT(cur->ai_next, servname);
591 }
592 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900593
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900594 *res = sentinel.ai_next;
595 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900596
597free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900598 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900599 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900600}
601
602/*
603 * numeric hostname
604 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
606 struct addrinfo** res, const char* canonname) {
607 const struct afd* afd;
608 struct addrinfo* cur;
609 struct addrinfo sentinel;
610 int error;
611 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900612
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900613 assert(pai != NULL);
614 /* hostname may be NULL */
615 /* servname may be NULL */
616 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900617
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618 *res = NULL;
619 sentinel.ai_next = NULL;
620 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900621
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900622 /*
623 * if the servname does not match socktype/protocol, ignore it.
624 */
625 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900626
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900627 afd = find_afd(pai->ai_family);
628 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900629
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900630 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900631#if 0 /*X/Open spec*/
632 case AF_INET:
633 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
634 if (pai->ai_family == afd->a_af ||
635 pai->ai_family == PF_UNSPEC /*?*/) {
636 GET_AI(cur->ai_next, afd, pton);
637 GET_PORT(cur->ai_next, servname);
638 if ((pai->ai_flags & AI_CANONNAME)) {
639 /*
640 * Set the numeric address itself as
641 * the canonical name, based on a
642 * clarification in rfc2553bis-03.
643 */
644 GET_CANONNAME(cur->ai_next, canonname);
645 }
646 while (cur && cur->ai_next)
647 cur = cur->ai_next;
648 } else
649 ERR(EAI_FAMILY); /*xxx*/
650 }
651 break;
652#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653 default:
654 if (inet_pton(afd->a_af, hostname, pton) == 1) {
655 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
656 GET_AI(cur->ai_next, afd, pton);
657 GET_PORT(cur->ai_next, servname);
658 if ((pai->ai_flags & AI_CANONNAME)) {
659 /*
660 * Set the numeric address itself as
661 * the canonical name, based on a
662 * clarification in rfc2553bis-03.
663 */
664 GET_CANONNAME(cur->ai_next, canonname);
665 }
666 while (cur->ai_next) cur = cur->ai_next;
667 } else
668 ERR(EAI_FAMILY); /*xxx*/
669 }
670 break;
671 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 *res = sentinel.ai_next;
674 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900675
676free:
677bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900678 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900680}
681
682/*
683 * numeric hostname with scope
684 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900685static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
686 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687 const struct afd* afd;
688 struct addrinfo* cur;
689 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900690 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900693 assert(pai != NULL);
694 /* hostname may be NULL */
695 /* servname may be NULL */
696 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900697
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 /*
699 * if the servname does not match socktype/protocol, ignore it.
700 */
701 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703 afd = find_afd(pai->ai_family);
704 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900705
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900706 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708 cp = strchr(hostname, SCOPE_DELIMITER);
709 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900710
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900711 /*
712 * Handle special case of <scoped_address><delimiter><scope id>
713 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900714 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715 if (hostname2 == NULL) return EAI_MEMORY;
716 /* terminate at the delimiter */
717 hostname2[cp - hostname] = '\0';
718 addr = hostname2;
719 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900720
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721 error = explore_numeric(pai, addr, servname, res, hostname);
722 if (error == 0) {
723 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 for (cur = *res; cur; cur = cur->ai_next) {
726 if (cur->ai_family != AF_INET6) continue;
727 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
728 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
729 free(hostname2);
730 return (EAI_NODATA); /* XXX: is return OK? */
731 }
732 sin6->sin6_scope_id = scopeid;
733 }
734 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900735
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900736 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900739}
740
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900741static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
742 assert(pai != NULL);
743 assert(ai != NULL);
744 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900745
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900746 if ((pai->ai_flags & AI_CANONNAME) != 0) {
747 ai->ai_canonname = strdup(str);
748 if (ai->ai_canonname == NULL) return EAI_MEMORY;
749 }
750 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900751}
752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
754 const char* addr) {
755 char* p;
756 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 assert(pai != NULL);
759 assert(afd != NULL);
760 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
763 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900764
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765 memcpy(ai, pai, sizeof(struct addrinfo));
766 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
767 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900768
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900769 ai->ai_addrlen = afd->a_socklen;
770#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
771 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900772#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
774 p = (char*) (void*) (ai->ai_addr);
775 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
776 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900777}
778
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900779static int get_portmatch(const struct addrinfo* ai, const char* servname) {
780 assert(ai != NULL);
781 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900782
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900783 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900784}
785
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900786static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
787 const char* proto;
788 struct servent* sp;
789 int port;
790 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900791
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900792 assert(ai != NULL);
793 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900794
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900795 if (servname == NULL) return 0;
796 switch (ai->ai_family) {
797 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900798 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 break;
800 default:
801 return 0;
802 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900803
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900804 switch (ai->ai_socktype) {
805 case SOCK_RAW:
806 return EAI_SERVICE;
807 case SOCK_DGRAM:
808 case SOCK_STREAM:
809 allownumeric = 1;
810 break;
811 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900813 break;
814 default:
815 return EAI_SOCKTYPE;
816 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900818 port = str2number(servname);
819 if (port >= 0) {
820 if (!allownumeric) return EAI_SERVICE;
821 if (port < 0 || port > 65535) return EAI_SERVICE;
822 port = htons(port);
823 } else {
824 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900825
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900826 switch (ai->ai_socktype) {
827 case SOCK_DGRAM:
828 proto = "udp";
829 break;
830 case SOCK_STREAM:
831 proto = "tcp";
832 break;
833 default:
834 proto = NULL;
835 break;
836 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900837
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900838 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
839 port = sp->s_port;
840 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900841
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900842 if (!matchonly) {
843 switch (ai->ai_family) {
844 case AF_INET:
845 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
846 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 case AF_INET6:
848 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
849 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900850 }
851 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900854}
855
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900856static const struct afd* find_afd(int af) {
857 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900858
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900859 if (af == PF_UNSPEC) return NULL;
860 for (afd = afdl; afd->a_af; afd++) {
861 if (afd->a_af == af) return afd;
862 }
863 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900864}
865
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900866// Convert a string to a scope identifier.
867static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 u_long lscopeid;
869 struct in6_addr* a6;
870 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900871
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900872 assert(scope != NULL);
873 assert(sin6 != NULL);
874 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900875
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900877
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900878 /* empty scopeid portion is invalid */
879 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900880
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
882 /*
883 * We currently assume a one-to-one mapping between links
884 * and interfaces, so we simply use interface indices for
885 * like-local scopes.
886 */
887 *scopeid = if_nametoindex(scope);
888 if (*scopeid == 0) goto trynumeric;
889 return 0;
890 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900891
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900892 /* still unclear about literal, allow numeric only - placeholder */
893 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
894 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
895 goto trynumeric;
896 else
897 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900898
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 /* try to convert to a numeric id as a last resort */
900trynumeric:
901 errno = 0;
902 lscopeid = strtoul(scope, &ep, 10);
903 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
904 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
905 return 0;
906 else
907 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900908}
Bernie Innocenti55864192018-08-30 04:05:20 +0900909
910/* code duplicate with gethnamaddr.c */
911
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900913
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914#define BOUNDED_INCR(x) \
915 do { \
916 BOUNDS_CHECK(cp, x); \
917 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900918 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900919
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900920#define BOUNDS_CHECK(ptr, count) \
921 do { \
922 if (eom - (ptr) < (count)) { \
923 h_errno = NO_RECOVERY; \
924 return NULL; \
925 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900926 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900927
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900928static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
929 const struct addrinfo* pai) {
930 struct addrinfo sentinel, *cur;
931 struct addrinfo ai;
932 const struct afd* afd;
933 char* canonname;
934 const HEADER* hp;
935 const u_char* cp;
936 int n;
937 const u_char* eom;
938 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900939 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900940 int haveanswer, had_error;
941 char tbuf[MAXDNAME];
942 int (*name_ok)(const char*);
943 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900944
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900945 assert(answer != NULL);
946 assert(qname != NULL);
947 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900948
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900949 memset(&sentinel, 0, sizeof(sentinel));
950 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900951
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900952 canonname = NULL;
953 eom = answer->buf + anslen;
954 switch (qtype) {
955 case T_A:
956 case T_AAAA:
957 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
958 name_ok = res_hnok;
959 break;
960 default:
961 return NULL; /* XXX should be abort(); */
962 }
963 /*
964 * find first satisfactory answer
965 */
966 hp = &answer->hdr;
967 ancount = ntohs(hp->ancount);
968 qdcount = ntohs(hp->qdcount);
969 bp = hostbuf;
970 ep = hostbuf + sizeof hostbuf;
971 cp = answer->buf;
972 BOUNDED_INCR(HFIXEDSZ);
973 if (qdcount != 1) {
974 h_errno = NO_RECOVERY;
975 return (NULL);
976 }
977 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
978 if ((n < 0) || !(*name_ok)(bp)) {
979 h_errno = NO_RECOVERY;
980 return (NULL);
981 }
982 BOUNDED_INCR(n + QFIXEDSZ);
983 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
984 /* res_send() has already verified that the query name is the
985 * same as the one we sent; this just gets the expanded name
986 * (i.e., with the succeeding search-domain tacked on).
987 */
988 n = strlen(bp) + 1; /* for the \0 */
989 if (n >= MAXHOSTNAMELEN) {
990 h_errno = NO_RECOVERY;
991 return (NULL);
992 }
993 canonname = bp;
994 bp += n;
995 /* The qname can be abbreviated, but h_name is now absolute. */
996 qname = canonname;
997 }
998 haveanswer = 0;
999 had_error = 0;
1000 while (ancount-- > 0 && cp < eom && !had_error) {
1001 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1002 if ((n < 0) || !(*name_ok)(bp)) {
1003 had_error++;
1004 continue;
1005 }
1006 cp += n; /* name */
1007 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +09001008 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001009 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +09001010 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001011 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +09001012 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001013 cp += INT16SZ; /* len */
1014 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001015 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001016 /* XXX - debug? syslog? */
1017 cp += n;
1018 continue; /* XXX - had_error++ ? */
1019 }
1020 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1021 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1022 if ((n < 0) || !(*name_ok)(tbuf)) {
1023 had_error++;
1024 continue;
1025 }
1026 cp += n;
1027 /* Get canonical name. */
1028 n = strlen(tbuf) + 1; /* for the \0 */
1029 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1030 had_error++;
1031 continue;
1032 }
1033 strlcpy(bp, tbuf, (size_t)(ep - bp));
1034 canonname = bp;
1035 bp += n;
1036 continue;
1037 }
1038 if (qtype == T_ANY) {
1039 if (!(type == T_A || type == T_AAAA)) {
1040 cp += n;
1041 continue;
1042 }
1043 } else if (type != qtype) {
1044 if (type != T_KEY && type != T_SIG)
1045 syslog(LOG_NOTICE | LOG_AUTH,
1046 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1047 p_class(C_IN), p_type(qtype), p_type(type));
1048 cp += n;
1049 continue; /* XXX - had_error++ ? */
1050 }
1051 switch (type) {
1052 case T_A:
1053 case T_AAAA:
1054 if (strcasecmp(canonname, bp) != 0) {
1055 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1056 cp += n;
1057 continue; /* XXX - had_error++ ? */
1058 }
1059 if (type == T_A && n != INADDRSZ) {
1060 cp += n;
1061 continue;
1062 }
1063 if (type == T_AAAA && n != IN6ADDRSZ) {
1064 cp += n;
1065 continue;
1066 }
1067 if (type == T_AAAA) {
1068 struct in6_addr in6;
1069 memcpy(&in6, cp, IN6ADDRSZ);
1070 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1071 cp += n;
1072 continue;
1073 }
1074 }
1075 if (!haveanswer) {
1076 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001077
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001078 canonname = bp;
1079 nn = strlen(bp) + 1; /* for the \0 */
1080 bp += nn;
1081 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001082
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001083 /* don't overwrite pai */
1084 ai = *pai;
1085 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1086 afd = find_afd(ai.ai_family);
1087 if (afd == NULL) {
1088 cp += n;
1089 continue;
1090 }
1091 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1092 if (cur->ai_next == NULL) had_error++;
1093 while (cur && cur->ai_next) cur = cur->ai_next;
1094 cp += n;
1095 break;
1096 default:
1097 abort();
1098 }
1099 if (!had_error) haveanswer++;
1100 }
1101 if (haveanswer) {
1102 if (!canonname)
1103 (void) get_canonname(pai, sentinel.ai_next, qname);
1104 else
1105 (void) get_canonname(pai, sentinel.ai_next, canonname);
1106 h_errno = NETDB_SUCCESS;
1107 return sentinel.ai_next;
1108 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001109
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001110 h_errno = NO_RECOVERY;
1111 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001112}
1113
1114struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001115 struct addrinfo* ai;
1116 int has_src_addr;
1117 sockaddr_union src_addr;
1118 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001119};
1120
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001121static int _get_scope(const struct sockaddr* addr) {
1122 if (addr->sa_family == AF_INET6) {
1123 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1124 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1125 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1126 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1127 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1128 /*
1129 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1130 * link-local scope.
1131 */
1132 return IPV6_ADDR_SCOPE_LINKLOCAL;
1133 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1134 return IPV6_ADDR_SCOPE_SITELOCAL;
1135 } else {
1136 return IPV6_ADDR_SCOPE_GLOBAL;
1137 }
1138 } else if (addr->sa_family == AF_INET) {
1139 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1140 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001141
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001142 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1143 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1144 return IPV6_ADDR_SCOPE_LINKLOCAL;
1145 } else {
1146 /*
1147 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1148 * and shared addresses (100.64.0.0/10), are assigned global scope.
1149 */
1150 return IPV6_ADDR_SCOPE_GLOBAL;
1151 }
1152 } else {
1153 /*
1154 * This should never happen.
1155 * Return a scope with low priority as a last resort.
1156 */
1157 return IPV6_ADDR_SCOPE_NODELOCAL;
1158 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001159}
1160
1161/* These macros are modelled after the ones in <netinet/in6.h>. */
1162
1163/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001164#define IN6_IS_ADDR_TEREDO(a) \
1165 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001166
1167/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001168#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001169
1170/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001171#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001172
1173/*
1174 * Get the label for a given IPv4/IPv6 address.
1175 * RFC 6724, section 2.1.
1176 */
1177
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001178static int _get_label(const struct sockaddr* addr) {
1179 if (addr->sa_family == AF_INET) {
1180 return 4;
1181 } else if (addr->sa_family == AF_INET6) {
1182 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1183 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1184 return 0;
1185 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1186 return 4;
1187 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1188 return 2;
1189 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1190 return 5;
1191 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1192 return 13;
1193 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1194 return 3;
1195 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1196 return 11;
1197 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1198 return 12;
1199 } else {
1200 /* All other IPv6 addresses, including global unicast addresses. */
1201 return 1;
1202 }
1203 } else {
1204 /*
1205 * This should never happen.
1206 * Return a semi-random label as a last resort.
1207 */
1208 return 1;
1209 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001210}
1211
1212/*
1213 * Get the precedence for a given IPv4/IPv6 address.
1214 * RFC 6724, section 2.1.
1215 */
1216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001217static int _get_precedence(const struct sockaddr* addr) {
1218 if (addr->sa_family == AF_INET) {
1219 return 35;
1220 } else if (addr->sa_family == AF_INET6) {
1221 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1222 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1223 return 50;
1224 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1225 return 35;
1226 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1227 return 30;
1228 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1229 return 5;
1230 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1231 return 3;
1232 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1233 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1234 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1235 return 1;
1236 } else {
1237 /* All other IPv6 addresses, including global unicast addresses. */
1238 return 40;
1239 }
1240 } else {
1241 return 1;
1242 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001243}
1244
1245/*
1246 * Find number of matching initial bits between the two addresses a1 and a2.
1247 */
1248
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001249static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1250 const char* p1 = (const char*) a1;
1251 const char* p2 = (const char*) a2;
1252 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001254 for (i = 0; i < sizeof(*a1); ++i) {
1255 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001257 if (p1[i] == p2[i]) {
1258 continue;
1259 }
1260 x = p1[i] ^ p2[i];
1261 for (j = 0; j < CHAR_BIT; ++j) {
1262 if (x & (1 << (CHAR_BIT - 1))) {
1263 return i * CHAR_BIT + j;
1264 }
1265 x <<= 1;
1266 }
1267 }
1268 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001269}
1270
1271/*
1272 * Compare two source/destination address pairs.
1273 * RFC 6724, section 6.
1274 */
1275
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001276static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1277 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1278 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1279 int scope_src1, scope_dst1, scope_match1;
1280 int scope_src2, scope_dst2, scope_match2;
1281 int label_src1, label_dst1, label_match1;
1282 int label_src2, label_dst2, label_match2;
1283 int precedence1, precedence2;
1284 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001286 /* Rule 1: Avoid unusable destinations. */
1287 if (a1->has_src_addr != a2->has_src_addr) {
1288 return a2->has_src_addr - a1->has_src_addr;
1289 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001290
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001291 /* Rule 2: Prefer matching scope. */
1292 scope_src1 = _get_scope(&a1->src_addr.generic);
1293 scope_dst1 = _get_scope(a1->ai->ai_addr);
1294 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001295
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001296 scope_src2 = _get_scope(&a2->src_addr.generic);
1297 scope_dst2 = _get_scope(a2->ai->ai_addr);
1298 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001299
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001300 if (scope_match1 != scope_match2) {
1301 return scope_match2 - scope_match1;
1302 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001303
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001304 /*
1305 * Rule 3: Avoid deprecated addresses.
1306 * TODO(sesse): We don't currently have a good way of finding this.
1307 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001308
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001309 /*
1310 * Rule 4: Prefer home addresses.
1311 * TODO(sesse): We don't currently have a good way of finding this.
1312 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001313
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001314 /* Rule 5: Prefer matching label. */
1315 label_src1 = _get_label(&a1->src_addr.generic);
1316 label_dst1 = _get_label(a1->ai->ai_addr);
1317 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001318
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001319 label_src2 = _get_label(&a2->src_addr.generic);
1320 label_dst2 = _get_label(a2->ai->ai_addr);
1321 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001322
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001323 if (label_match1 != label_match2) {
1324 return label_match2 - label_match1;
1325 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001326
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001327 /* Rule 6: Prefer higher precedence. */
1328 precedence1 = _get_precedence(a1->ai->ai_addr);
1329 precedence2 = _get_precedence(a2->ai->ai_addr);
1330 if (precedence1 != precedence2) {
1331 return precedence2 - precedence1;
1332 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001333
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001334 /*
1335 * Rule 7: Prefer native transport.
1336 * TODO(sesse): We don't currently have a good way of finding this.
1337 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001338
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001339 /* Rule 8: Prefer smaller scope. */
1340 if (scope_dst1 != scope_dst2) {
1341 return scope_dst1 - scope_dst2;
1342 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001343
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001344 /*
1345 * Rule 9: Use longest matching prefix.
1346 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1347 * to work very well directly applied to IPv4. (glibc uses information from
1348 * the routing table for a custom IPv4 implementation here.)
1349 */
1350 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1351 a2->ai->ai_addr->sa_family == AF_INET6) {
1352 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1353 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1354 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1355 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1356 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1357 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1358 if (prefixlen1 != prefixlen2) {
1359 return prefixlen2 - prefixlen1;
1360 }
1361 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001362
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001363 /*
1364 * Rule 10: Leave the order unchanged.
1365 * We need this since qsort() is not necessarily stable.
1366 */
1367 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001368}
1369
1370/*
1371 * Find the source address that will be used if trying to connect to the given
1372 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1373 *
1374 * Returns 1 if a source address was found, 0 if the address is unreachable,
1375 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1376 * undefined.
1377 */
1378
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001379static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1380 uid_t uid) {
1381 int sock;
1382 int ret;
1383 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001384
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 switch (addr->sa_family) {
1386 case AF_INET:
1387 len = sizeof(struct sockaddr_in);
1388 break;
1389 case AF_INET6:
1390 len = sizeof(struct sockaddr_in6);
1391 break;
1392 default:
1393 /* No known usable source address for non-INET families. */
1394 return 0;
1395 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001396
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001397 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1398 if (sock == -1) {
1399 if (errno == EAFNOSUPPORT) {
1400 return 0;
1401 } else {
1402 return -1;
1403 }
1404 }
1405 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1406 close(sock);
1407 return 0;
1408 }
1409 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1410 close(sock);
1411 return 0;
1412 }
1413 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001414 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001415 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001416
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001417 if (ret == -1) {
1418 close(sock);
1419 return 0;
1420 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001421
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001422 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1423 close(sock);
1424 return -1;
1425 }
1426 close(sock);
1427 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001428}
1429
1430/*
1431 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1432 * Will leave the list unchanged if an error occurs.
1433 */
1434
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001435static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1436 struct addrinfo* cur;
1437 int nelem = 0, i;
1438 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001439
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001440 cur = list_sentinel->ai_next;
1441 while (cur) {
1442 ++nelem;
1443 cur = cur->ai_next;
1444 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001445
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001446 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1447 if (elems == NULL) {
1448 goto error;
1449 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001450
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001451 /*
1452 * Convert the linked list to an array that also contains the candidate
1453 * source address for each destination address.
1454 */
1455 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1456 int has_src_addr;
1457 assert(cur != NULL);
1458 elems[i].ai = cur;
1459 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001460
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001461 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1462 if (has_src_addr == -1) {
1463 goto error;
1464 }
1465 elems[i].has_src_addr = has_src_addr;
1466 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001467
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001468 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1469 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001471 list_sentinel->ai_next = elems[0].ai;
1472 for (i = 0; i < nelem - 1; ++i) {
1473 elems[i].ai->ai_next = elems[i + 1].ai;
1474 }
1475 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001476
1477error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001478 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001479}
1480
Bernie Innocenti948f6572018-09-12 21:32:42 +09001481static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1482 const android_net_context* netcontext, addrinfo** rv) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001483 struct addrinfo* ai;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001484 struct addrinfo sentinel, *cur;
1485 struct res_target q, q2;
1486 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001487
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001488 memset(&q, 0, sizeof(q));
1489 memset(&q2, 0, sizeof(q2));
1490 memset(&sentinel, 0, sizeof(sentinel));
1491 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001492
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001493 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001494 if (buf == NULL) {
1495 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001496 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001497 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001498 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001499 if (buf2 == NULL) {
1500 free(buf);
1501 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001502 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001503 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001504
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001505 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001506 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001507 /* prefer IPv6 */
1508 q.name = name;
1509 q.qclass = C_IN;
1510 q.answer = buf->buf;
1511 q.anslen = sizeof(buf->buf);
1512 int query_ipv6 = 1, query_ipv4 = 1;
1513 if (pai->ai_flags & AI_ADDRCONFIG) {
1514 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1515 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1516 }
1517 if (query_ipv6) {
1518 q.qtype = T_AAAA;
1519 if (query_ipv4) {
1520 q.next = &q2;
1521 q2.name = name;
1522 q2.qclass = C_IN;
1523 q2.qtype = T_A;
1524 q2.answer = buf2->buf;
1525 q2.anslen = sizeof(buf2->buf);
1526 }
1527 } else if (query_ipv4) {
1528 q.qtype = T_A;
1529 } else {
1530 free(buf);
1531 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001532 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001533 }
1534 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001535 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001536 case AF_INET:
1537 q.name = name;
1538 q.qclass = C_IN;
1539 q.qtype = T_A;
1540 q.answer = buf->buf;
1541 q.anslen = sizeof(buf->buf);
1542 break;
1543 case AF_INET6:
1544 q.name = name;
1545 q.qclass = C_IN;
1546 q.qtype = T_AAAA;
1547 q.answer = buf->buf;
1548 q.anslen = sizeof(buf->buf);
1549 break;
1550 default:
1551 free(buf);
1552 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001553 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001554 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001555
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001556 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001557 if (res == NULL) {
1558 free(buf);
1559 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001560 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001561 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001562
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001563 /* this just sets our netid val in the thread private data so we don't have to
1564 * modify the api's all the way down to res_send.c's res_nsend. We could
1565 * fully populate the thread private data here, but if we get down there
1566 * and have a cache hit that would be wasted, so we do the rest there on miss
1567 */
1568 res_setnetcontext(res, netcontext);
1569 if (res_searchN(name, &q, res) < 0) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001570 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001571 free(buf);
1572 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001573 return EAI_NODATA; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001574 }
1575 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1576 if (ai) {
1577 cur->ai_next = ai;
1578 while (cur && cur->ai_next) cur = cur->ai_next;
1579 }
1580 if (q.next) {
1581 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1582 if (ai) cur->ai_next = ai;
1583 }
1584 free(buf);
1585 free(buf2);
1586 if (sentinel.ai_next == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001587 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001588 switch (h_errno) {
1589 case HOST_NOT_FOUND:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001590 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001591 case TRY_AGAIN:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001592 return EAI_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001593 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001594 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001595 }
1596 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001597
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001598 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001599
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001600 res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001601
Bernie Innocenti948f6572018-09-12 21:32:42 +09001602 *rv = sentinel.ai_next;
1603 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001604}
1605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606static void _sethtent(FILE** hostf) {
1607 if (!*hostf)
1608 *hostf = fopen(_PATH_HOSTS, "re");
1609 else
1610 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001611}
1612
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001613static void _endhtent(FILE** hostf) {
1614 if (*hostf) {
1615 (void) fclose(*hostf);
1616 *hostf = NULL;
1617 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001618}
1619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001620static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1621 char* p;
1622 char *cp, *tname, *cname;
1623 struct addrinfo hints, *res0, *res;
1624 int error;
1625 const char* addr;
1626 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001628 assert(name != NULL);
1629 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001630
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001631 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1632again:
1633 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1634 if (*p == '#') goto again;
1635 if (!(cp = strpbrk(p, "#\n"))) goto again;
1636 *cp = '\0';
1637 if (!(cp = strpbrk(p, " \t"))) goto again;
1638 *cp++ = '\0';
1639 addr = p;
1640 /* if this is not something we're looking for, skip it. */
1641 cname = NULL;
1642 while (cp && *cp) {
1643 if (*cp == ' ' || *cp == '\t') {
1644 cp++;
1645 continue;
1646 }
1647 if (!cname) cname = cp;
1648 tname = cp;
1649 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1650 // fprintf(stderr, "\ttname = '%s'", tname);
1651 if (strcasecmp(name, tname) == 0) goto found;
1652 }
1653 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001654
1655found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001656 hints = *pai;
1657 hints.ai_flags = AI_NUMERICHOST;
1658 error = getaddrinfo(addr, NULL, &hints, &res0);
1659 if (error) goto again;
1660 for (res = res0; res; res = res->ai_next) {
1661 /* cover it up */
1662 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001663
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001664 if (pai->ai_flags & AI_CANONNAME) {
1665 if (get_canonname(pai, res, cname) != 0) {
1666 freeaddrinfo(res0);
1667 goto again;
1668 }
1669 }
1670 }
1671 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001672}
1673
Bernie Innocenti948f6572018-09-12 21:32:42 +09001674static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001675 struct addrinfo sentinel, *cur;
1676 struct addrinfo* p;
1677 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001678
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001679 memset(&sentinel, 0, sizeof(sentinel));
1680 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001681
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001682 _sethtent(&hostf);
1683 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1684 cur->ai_next = p;
1685 while (cur && cur->ai_next) cur = cur->ai_next;
1686 }
1687 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001688
Bernie Innocenti948f6572018-09-12 21:32:42 +09001689 *res = sentinel.ai_next;
1690 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001691}
1692
1693/* resolver logic */
1694
1695/*
1696 * Formulate a normal query, send, and await answer.
1697 * Returned answer is placed in supplied buffer "answer".
1698 * Perform preliminary check of answer, returning success only
1699 * if no error is indicated and the answer count is nonzero.
1700 * Return the size of the response on success, -1 on error.
1701 * Error number is left in h_errno.
1702 *
1703 * Caller must parse answer and determine whether it answers the question.
1704 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001705static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1706 res_state res) {
1707 u_char buf[MAXPACKET];
1708 HEADER* hp;
1709 int n;
1710 struct res_target* t;
1711 int rcode;
1712 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001714 assert(name != NULL);
1715 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001716
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001717 rcode = NOERROR;
1718 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001719
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001720 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001721 u_char* answer;
1722 int anslen;
1723 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001725 hp = (HEADER*) (void*) t->answer;
1726 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001728 again:
1729 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001731 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001732 int cl = t->qclass;
1733 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001734 answer = t->answer;
1735 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001736#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001737 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001738#endif
1739
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001740 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001741 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1742 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1743 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001744 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001745#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001746 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001747#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001748 h_errno = NO_RECOVERY;
1749 return n;
1750 }
1751 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001753 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1754 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001755 /* if the query choked with EDNS0, retry without EDNS0 */
1756 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1757 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1758 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001759#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001760 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001761#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001762 goto again;
1763 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001764#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001765 if (res->options & RES_DEBUG)
1766 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001767#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001768 continue;
1769 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001771 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001773 t->n = n;
1774 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001776 if (ancount == 0) {
1777 switch (rcode) {
1778 case NXDOMAIN:
1779 h_errno = HOST_NOT_FOUND;
1780 break;
1781 case SERVFAIL:
1782 h_errno = TRY_AGAIN;
1783 break;
1784 case NOERROR:
1785 h_errno = NO_DATA;
1786 break;
1787 case FORMERR:
1788 case NOTIMP:
1789 case REFUSED:
1790 default:
1791 h_errno = NO_RECOVERY;
1792 break;
1793 }
1794 return -1;
1795 }
1796 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001797}
1798
1799/*
1800 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1801 * Return the size of the response on success, -1 on error.
1802 * If enabled, implement search rules until answer or unrecoverable failure
1803 * is detected. Error code, if any, is left in h_errno.
1804 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001805static int res_searchN(const char* name, struct res_target* target, res_state res) {
1806 const char *cp, *const *domain;
1807 HEADER* hp;
1808 u_int dots;
1809 int trailing_dot, ret, saved_herrno;
1810 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001812 assert(name != NULL);
1813 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001815 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001816
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001817 errno = 0;
1818 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1819 dots = 0;
1820 for (cp = name; *cp; cp++) dots += (*cp == '.');
1821 trailing_dot = 0;
1822 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001824 /*
1825 * If there are dots in the name already, let's just give it a try
1826 * 'as is'. The threshold can be set with the "ndots" option.
1827 */
1828 saved_herrno = -1;
1829 if (dots >= res->ndots) {
1830 ret = res_querydomainN(name, NULL, target, res);
1831 if (ret > 0) return (ret);
1832 saved_herrno = h_errno;
1833 tried_as_is++;
1834 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001835
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001836 /*
1837 * We do at least one level of search if
1838 * - there is no dot and RES_DEFNAME is set, or
1839 * - there is at least one dot, there is no trailing dot,
1840 * and RES_DNSRCH is set.
1841 */
1842 if ((!dots && (res->options & RES_DEFNAMES)) ||
1843 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1844 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001845
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001846 /* Unfortunately we need to set stuff up before
1847 * the domain stuff is tried. Will have a better
1848 * fix after thread pools are used.
1849 */
1850 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001852 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1853 ret = res_querydomainN(name, *domain, target, res);
1854 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001855
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001856 /*
1857 * If no server present, give up.
1858 * If name isn't found in this domain,
1859 * keep trying higher domains in the search list
1860 * (if that's enabled).
1861 * On a NO_DATA error, keep trying, otherwise
1862 * a wildcard entry of another type could keep us
1863 * from finding this entry higher in the domain.
1864 * If we get some other error (negative answer or
1865 * server failure), then stop searching up,
1866 * but try the input name below in case it's
1867 * fully-qualified.
1868 */
1869 if (errno == ECONNREFUSED) {
1870 h_errno = TRY_AGAIN;
1871 return -1;
1872 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001873
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001874 switch (h_errno) {
1875 case NO_DATA:
1876 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001877 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001878 case HOST_NOT_FOUND:
1879 /* keep trying */
1880 break;
1881 case TRY_AGAIN:
1882 if (hp->rcode == SERVFAIL) {
1883 /* try next search element, if any */
1884 got_servfail++;
1885 break;
1886 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001887 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001888 default:
1889 /* anything else implies that we're done */
1890 done++;
1891 }
1892 /*
1893 * if we got here for some reason other than DNSRCH,
1894 * we only wanted one iteration of the loop, so stop.
1895 */
1896 if (!(res->options & RES_DNSRCH)) done++;
1897 }
1898 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001899
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001900 /*
1901 * if we have not already tried the name "as is", do that now.
1902 * note that we do this regardless of how many dots were in the
1903 * name or whether it ends with a dot.
1904 */
1905 if (!tried_as_is) {
1906 ret = res_querydomainN(name, NULL, target, res);
1907 if (ret > 0) return ret;
1908 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001909
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001910 /*
1911 * if we got here, we didn't satisfy the search.
1912 * if we did an initial full query, return that query's h_errno
1913 * (note that we wouldn't be here if that query had succeeded).
1914 * else if we ever got a nodata, send that back as the reason.
1915 * else send back meaningless h_errno, that being the one from
1916 * the last DNSRCH we did.
1917 */
1918 if (saved_herrno != -1)
1919 h_errno = saved_herrno;
1920 else if (got_nodata)
1921 h_errno = NO_DATA;
1922 else if (got_servfail)
1923 h_errno = TRY_AGAIN;
1924 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001925}
1926
1927/*
1928 * Perform a call on res_query on the concatenation of name and domain,
1929 * removing a trailing dot from name if domain is NULL.
1930 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001931static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1932 res_state res) {
1933 char nbuf[MAXDNAME];
1934 const char* longname = nbuf;
1935 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001936
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001937 assert(name != NULL);
1938 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001939
1940#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001941 if (res->options & RES_DEBUG)
1942 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001943#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001944 if (domain == NULL) {
1945 /*
1946 * Check for trailing '.';
1947 * copy without '.' if present.
1948 */
1949 n = strlen(name);
1950 if (n + 1 > sizeof(nbuf)) {
1951 h_errno = NO_RECOVERY;
1952 return -1;
1953 }
1954 if (n > 0 && name[--n] == '.') {
1955 strncpy(nbuf, name, n);
1956 nbuf[n] = '\0';
1957 } else
1958 longname = name;
1959 } else {
1960 n = strlen(name);
1961 d = strlen(domain);
1962 if (n + 1 + d + 1 > sizeof(nbuf)) {
1963 h_errno = NO_RECOVERY;
1964 return -1;
1965 }
1966 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1967 }
1968 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001969}