1 /*	$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 #define LOG_TAG "resolv"
34 
35 #include "getaddrinfo.h"
36 
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
39 #include <assert.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <net/if.h>
44 #include <netdb.h>
45 #include <netinet/in.h>
46 #include <stdbool.h>
47 #include <stddef.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <sys/un.h>
54 #include <unistd.h>
55 
56 #include <chrono>
57 #include <future>
58 
59 #include <android-base/logging.h>
60 
61 #include "Experiments.h"
62 #include "netd_resolv/resolv.h"
63 #include "res_comp.h"
64 #include "res_debug.h"
65 #include "res_init.h"
66 #include "resolv_cache.h"
67 #include "resolv_private.h"
68 #include "util.h"
69 
70 #define ANY 0
71 
72 using android::net::NetworkDnsEventReported;
73 
74 const char in_addrany[] = {0, 0, 0, 0};
75 const char in_loopback[] = {127, 0, 0, 1};
76 const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
77 const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
78 
79 const struct afd {
80     int a_af;
81     int a_addrlen;
82     int a_socklen;
83     int a_off;
84     const char* a_addrany;
85     const char* a_loopback;
86     int a_scoped;
87 } afdl[] = {
88         {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
89          offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
90         {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
91          offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
92         {0, 0, 0, 0, NULL, NULL, 0},
93 };
94 
95 struct Explore {
96     int e_af;
97     int e_socktype;
98     int e_protocol;
99     int e_wild;
100 #define WILD_AF(ex) ((ex).e_wild & 0x01)
101 #define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
102 #define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
103 };
104 
105 const Explore explore_options[] = {
106         {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
107         {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
108         {PF_INET6, SOCK_RAW, ANY, 0x05},
109         {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
110         {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
111         {PF_INET, SOCK_RAW, ANY, 0x05},
112         {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
113         {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
114         {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
115 };
116 
117 #define PTON_MAX 16
118 
119 struct res_target {
120     struct res_target* next;
121     const char* name;                                                  // domain name
122     int qclass, qtype;                                                 // class and type of query
123     std::vector<uint8_t> answer = std::vector<uint8_t>(MAXPACKET, 0);  // buffer to put answer
124     int n = 0;                                                         // result length
125 };
126 
127 static int str2number(const char*);
128 static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
129                         const struct android_net_context*, NetworkDnsEventReported* event);
130 static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
131 static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
132                            const char*);
133 static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
134                                  struct addrinfo**);
135 static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
136 static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
137 static int get_portmatch(const struct addrinfo*, const char*);
138 static int get_port(const struct addrinfo*, const char*, int);
139 static const struct afd* find_afd(int);
140 static int ip6_str2scopeid(const char*, struct sockaddr_in6*, uint32_t*);
141 
142 static struct addrinfo* getanswer(const std::vector<uint8_t>&, int, const char*, int,
143                                   const struct addrinfo*, int* herrno);
144 static int dns_getaddrinfo(const char* name, const addrinfo* pai,
145                            const android_net_context* netcontext, addrinfo** rv,
146                            NetworkDnsEventReported* event);
147 static void _sethtent(FILE**);
148 static void _endhtent(FILE**);
149 static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
150 static struct addrinfo* getCustomHosts(const size_t netid, const char*, const struct addrinfo*);
151 static bool files_getaddrinfo(const size_t netid, const char* name, const addrinfo* pai,
152                               addrinfo** res);
153 static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
154 
155 static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
156 static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
157 static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
158                             int* herrno);
159 
160 const char* const ai_errlist[] = {
161         "Success",
162         "Address family for hostname not supported",    /* EAI_ADDRFAMILY */
163         "Temporary failure in name resolution",         /* EAI_AGAIN      */
164         "Invalid value for ai_flags",                   /* EAI_BADFLAGS   */
165         "Non-recoverable failure in name resolution",   /* EAI_FAIL       */
166         "ai_family not supported",                      /* EAI_FAMILY     */
167         "Memory allocation failure",                    /* EAI_MEMORY     */
168         "No address associated with hostname",          /* EAI_NODATA     */
169         "hostname nor servname provided, or not known", /* EAI_NONAME     */
170         "servname not supported for ai_socktype",       /* EAI_SERVICE    */
171         "ai_socktype not supported",                    /* EAI_SOCKTYPE   */
172         "System error returned in errno",               /* EAI_SYSTEM     */
173         "Invalid value for hints",                      /* EAI_BADHINTS	  */
174         "Resolved protocol is unknown",                 /* EAI_PROTOCOL   */
175         "Argument buffer overflow",                     /* EAI_OVERFLOW   */
176         "Unknown error",                                /* EAI_MAX        */
177 };
178 
179 /* XXX macros that make external reference is BAD. */
180 
181 #define GET_AI(ai, afd, addr)                                \
182     do {                                                     \
183         /* external reference: pai, error, and label free */ \
184         (ai) = get_ai(pai, (afd), (addr));                   \
185         if ((ai) == NULL) {                                  \
186             error = EAI_MEMORY;                              \
187             goto free;                                       \
188         }                                                    \
189     } while (0)
190 
191 #define GET_PORT(ai, serv)                             \
192     do {                                               \
193         /* external reference: error and label free */ \
194         error = get_port((ai), (serv), 0);             \
195         if (error != 0) goto free;                     \
196     } while (0)
197 
198 #define MATCH_FAMILY(x, y, w) \
199     ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
200 #define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
201 
gai_strerror(int ecode)202 const char* gai_strerror(int ecode) {
203     if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
204     return ai_errlist[ecode];
205 }
206 
freeaddrinfo(struct addrinfo * ai)207 void freeaddrinfo(struct addrinfo* ai) {
208     while (ai) {
209         struct addrinfo* next = ai->ai_next;
210         if (ai->ai_canonname) free(ai->ai_canonname);
211         // Also frees ai->ai_addr which points to extra space beyond addrinfo
212         free(ai);
213         ai = next;
214     }
215 }
216 
str2number(const char * p)217 static int str2number(const char* p) {
218     char* ep;
219     unsigned long v;
220 
221     assert(p != NULL);
222 
223     if (*p == '\0') return -1;
224     ep = NULL;
225     errno = 0;
226     v = strtoul(p, &ep, 10);
227     if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
228         return v;
229     else
230         return -1;
231 }
232 
233 /*
234  * The following functions determine whether IPv4 or IPv6 connectivity is
235  * available in order to implement AI_ADDRCONFIG.
236  *
237  * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
238  * available, but whether addresses of the specified family are "configured
239  * on the local system". However, bionic doesn't currently support getifaddrs,
240  * so checking for connectivity is the next best thing.
241  */
have_ipv6(unsigned mark,uid_t uid)242 static int have_ipv6(unsigned mark, uid_t uid) {
243     static const struct sockaddr_in6 sin6_test = {
244             .sin6_family = AF_INET6,
245             .sin6_addr.s6_addr = {// 2000::
246                                   0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
247     sockaddr_union addr = {.sin6 = sin6_test};
248     return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
249 }
250 
have_ipv4(unsigned mark,uid_t uid)251 static int have_ipv4(unsigned mark, uid_t uid) {
252     static const struct sockaddr_in sin_test = {
253             .sin_family = AF_INET,
254             .sin_addr.s_addr = __constant_htonl(0x08080808L)  // 8.8.8.8
255     };
256     sockaddr_union addr = {.sin = sin_test};
257     return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
258 }
259 
260 // Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
261 // NOTE: also called by resolv_set_nameservers().
getaddrinfo_numeric(const char * hostname,const char * servname,addrinfo hints,addrinfo ** result)262 int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
263                         addrinfo** result) {
264     hints.ai_flags = AI_NUMERICHOST;
265     const android_net_context netcontext = {
266             .app_netid = NETID_UNSET,
267             .app_mark = MARK_UNSET,
268             .dns_netid = NETID_UNSET,
269             .dns_mark = MARK_UNSET,
270             .uid = NET_CONTEXT_INVALID_UID,
271             .pid = NET_CONTEXT_INVALID_PID,
272     };
273     NetworkDnsEventReported event;
274     return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result,
275                                             &event);
276 }
277 
278 namespace {
279 
validateHints(const addrinfo * _Nonnull hints)280 int validateHints(const addrinfo* _Nonnull hints) {
281     if (!hints) return EAI_BADHINTS;
282 
283     // error check for hints
284     if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
285         return EAI_BADHINTS;
286     }
287     if (hints->ai_flags & ~AI_MASK) {
288         return EAI_BADFLAGS;
289     }
290     if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
291           hints->ai_family == PF_INET6)) {
292         return EAI_FAMILY;
293     }
294 
295     // Socket types which are not in explore_options.
296     switch (hints->ai_socktype) {
297         case SOCK_RAW:
298         case SOCK_DGRAM:
299         case SOCK_STREAM:
300         case ANY:
301             break;
302         default:
303             return EAI_SOCKTYPE;
304     }
305 
306     if (hints->ai_socktype == ANY || hints->ai_protocol == ANY) return 0;
307 
308     // if both socktype/protocol are specified, check if they are meaningful combination.
309     for (const Explore& ex : explore_options) {
310         if (hints->ai_family != ex.e_af) continue;
311         if (ex.e_socktype == ANY) continue;
312         if (ex.e_protocol == ANY) continue;
313         if (hints->ai_socktype == ex.e_socktype && hints->ai_protocol != ex.e_protocol) {
314             return EAI_BADHINTS;
315         }
316     }
317 
318     return 0;
319 }
320 
321 }  // namespace
322 
android_getaddrinfofornetcontext(const char * hostname,const char * servname,const addrinfo * hints,const android_net_context * netcontext,addrinfo ** res,NetworkDnsEventReported * event)323 int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
324                                      const addrinfo* hints, const android_net_context* netcontext,
325                                      addrinfo** res, NetworkDnsEventReported* event) {
326     // hostname is allowed to be nullptr
327     // servname is allowed to be nullptr
328     // hints is allowed to be nullptr
329     assert(res != nullptr);
330     assert(netcontext != nullptr);
331     assert(event != nullptr);
332 
333     addrinfo sentinel = {};
334     addrinfo* cur = &sentinel;
335     int error = 0;
336 
337     do {
338         if (hostname == nullptr && servname == nullptr) {
339             error = EAI_NONAME;
340             break;
341         }
342 
343         if (hints && (error = validateHints(hints))) break;
344         addrinfo ai = hints ? *hints : addrinfo{};
345 
346         // Check for special cases:
347         // (1) numeric servname is disallowed if socktype/protocol are left unspecified.
348         // (2) servname is disallowed for raw and other inet{,6} sockets.
349         if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
350             addrinfo tmp = ai;
351             if (tmp.ai_family == PF_UNSPEC) {
352                 tmp.ai_family = PF_INET6;
353             }
354             error = get_portmatch(&tmp, servname);
355             if (error) break;
356         }
357 
358         // NULL hostname, or numeric hostname
359         for (const Explore& ex : explore_options) {
360             /* PF_UNSPEC entries are prepared for DNS queries only */
361             if (ex.e_af == PF_UNSPEC) continue;
362 
363             if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
364             if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
365             if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
366 
367             addrinfo tmp = ai;
368             if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
369             if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
370             if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
371 
372             LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family
373                        << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
374             if (hostname == nullptr)
375                 error = explore_null(&tmp, servname, &cur->ai_next);
376             else
377                 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
378 
379             if (error) break;
380 
381             while (cur->ai_next) cur = cur->ai_next;
382         }
383         if (error) break;
384 
385         // If numeric representation of AF1 can be interpreted as FQDN
386         // representation of AF2, we need to think again about the code below.
387         if (sentinel.ai_next) break;
388 
389         if (hostname == nullptr) {
390             error = EAI_NODATA;
391             break;
392         }
393         if (ai.ai_flags & AI_NUMERICHOST) {
394             error = EAI_NONAME;
395             break;
396         }
397 
398         return resolv_getaddrinfo(hostname, servname, hints, netcontext, res, event);
399     } while (0);
400 
401     if (error) {
402         freeaddrinfo(sentinel.ai_next);
403         *res = nullptr;
404     } else {
405         *res = sentinel.ai_next;
406     }
407     return error;
408 }
409 
resolv_getaddrinfo(const char * _Nonnull hostname,const char * servname,const addrinfo * hints,const android_net_context * _Nonnull netcontext,addrinfo ** _Nonnull res,NetworkDnsEventReported * _Nonnull event)410 int resolv_getaddrinfo(const char* _Nonnull hostname, const char* servname, const addrinfo* hints,
411                        const android_net_context* _Nonnull netcontext, addrinfo** _Nonnull res,
412                        NetworkDnsEventReported* _Nonnull event) {
413     if (hostname == nullptr && servname == nullptr) return EAI_NONAME;
414     if (hostname == nullptr) return EAI_NODATA;
415 
416     // servname is allowed to be nullptr
417     // hints is allowed to be nullptr
418     assert(res != nullptr);
419     assert(netcontext != nullptr);
420     assert(event != nullptr);
421 
422     int error = EAI_FAIL;
423     if (hints && (error = validateHints(hints))) {
424         *res = nullptr;
425         return error;
426     }
427 
428     addrinfo ai = hints ? *hints : addrinfo{};
429     addrinfo sentinel = {};
430     addrinfo* cur = &sentinel;
431     // hostname as alphanumeric name.
432     // We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
433     for (const Explore& ex : explore_options) {
434         // Require exact match for family field
435         if (ai.ai_family != ex.e_af) continue;
436 
437         if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
438 
439         if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
440 
441         addrinfo tmp = ai;
442         if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
443         if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
444 
445         LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family
446                    << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
447         error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext, event);
448 
449         while (cur->ai_next) cur = cur->ai_next;
450     }
451 
452     // Propagate the last error from explore_fqdn(), but only when *all* attempts failed.
453     if ((*res = sentinel.ai_next)) return 0;
454 
455     // TODO: consider removing freeaddrinfo.
456     freeaddrinfo(sentinel.ai_next);
457     *res = nullptr;
458     return (error == 0) ? EAI_FAIL : error;
459 }
460 
461 // FQDN hostname, DNS lookup
explore_fqdn(const addrinfo * pai,const char * hostname,const char * servname,addrinfo ** res,const android_net_context * netcontext,NetworkDnsEventReported * event)462 static int explore_fqdn(const addrinfo* pai, const char* hostname, const char* servname,
463                         addrinfo** res, const android_net_context* netcontext,
464                         NetworkDnsEventReported* event) {
465     assert(pai != nullptr);
466     // hostname may be nullptr
467     // servname may be nullptr
468     assert(res != nullptr);
469 
470     addrinfo* result = nullptr;
471     int error = 0;
472 
473     // If the servname does not match socktype/protocol, return error code.
474     if ((error = get_portmatch(pai, servname))) return error;
475 
476     if (!files_getaddrinfo(netcontext->dns_netid, hostname, pai, &result)) {
477         error = dns_getaddrinfo(hostname, pai, netcontext, &result, event);
478     }
479     if (error) {
480         freeaddrinfo(result);
481         return error;
482     }
483 
484     for (addrinfo* cur = result; cur; cur = cur->ai_next) {
485         // canonname should be filled already
486         if ((error = get_port(cur, servname, 0))) {
487             freeaddrinfo(result);
488             return error;
489         }
490     }
491     *res = result;
492     return 0;
493 }
494 
495 /*
496  * hostname == NULL.
497  * passive socket -> anyaddr (0.0.0.0 or ::)
498  * non-passive socket -> localhost (127.0.0.1 or ::1)
499  */
explore_null(const struct addrinfo * pai,const char * servname,struct addrinfo ** res)500 static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
501     int s;
502     const struct afd* afd;
503     struct addrinfo* cur;
504     struct addrinfo sentinel;
505     int error;
506 
507     LOG(DEBUG) << __func__;
508 
509     assert(pai != NULL);
510     /* servname may be NULL */
511     assert(res != NULL);
512 
513     *res = NULL;
514     sentinel.ai_next = NULL;
515     cur = &sentinel;
516 
517     /*
518      * filter out AFs that are not supported by the kernel
519      * XXX errno?
520      */
521     s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
522     if (s < 0) {
523         if (errno != EMFILE) return 0;
524     } else
525         close(s);
526 
527     /*
528      * if the servname does not match socktype/protocol, ignore it.
529      */
530     if (get_portmatch(pai, servname) != 0) return 0;
531 
532     afd = find_afd(pai->ai_family);
533     if (afd == NULL) return 0;
534 
535     if (pai->ai_flags & AI_PASSIVE) {
536         GET_AI(cur->ai_next, afd, afd->a_addrany);
537         GET_PORT(cur->ai_next, servname);
538     } else {
539         GET_AI(cur->ai_next, afd, afd->a_loopback);
540         GET_PORT(cur->ai_next, servname);
541     }
542     cur = cur->ai_next;
543 
544     *res = sentinel.ai_next;
545     return 0;
546 
547 free:
548     freeaddrinfo(sentinel.ai_next);
549     return error;
550 }
551 
552 /*
553  * numeric hostname
554  */
explore_numeric(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res,const char * canonname)555 static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
556                            struct addrinfo** res, const char* canonname) {
557     const struct afd* afd;
558     struct addrinfo* cur;
559     struct addrinfo sentinel;
560     int error;
561     char pton[PTON_MAX];
562 
563     assert(pai != NULL);
564     /* hostname may be NULL */
565     /* servname may be NULL */
566     assert(res != NULL);
567 
568     *res = NULL;
569     sentinel.ai_next = NULL;
570     cur = &sentinel;
571 
572     /*
573      * if the servname does not match socktype/protocol, ignore it.
574      */
575     if (get_portmatch(pai, servname) != 0) return 0;
576 
577     afd = find_afd(pai->ai_family);
578     if (afd == NULL) return 0;
579 
580     if (inet_pton(afd->a_af, hostname, pton) == 1) {
581         if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
582             GET_AI(cur->ai_next, afd, pton);
583             GET_PORT(cur->ai_next, servname);
584             if ((pai->ai_flags & AI_CANONNAME)) {
585                 /*
586                  * Set the numeric address itself as
587                  * the canonical name, based on a
588                  * clarification in rfc2553bis-03.
589                  */
590                 error = get_canonname(pai, cur->ai_next, canonname);
591                 if (error != 0) {
592                     freeaddrinfo(sentinel.ai_next);
593                     return error;
594                 }
595             }
596             while (cur->ai_next) cur = cur->ai_next;
597         } else
598             return EAI_FAMILY;
599     }
600 
601     *res = sentinel.ai_next;
602     return 0;
603 
604 free:
605     freeaddrinfo(sentinel.ai_next);
606     return error;
607 }
608 
609 /*
610  * numeric hostname with scope
611  */
explore_numeric_scope(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res)612 static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
613                                  const char* servname, struct addrinfo** res) {
614     const struct afd* afd;
615     struct addrinfo* cur;
616     int error;
617     const char *cp, *scope, *addr;
618     struct sockaddr_in6* sin6;
619 
620     LOG(DEBUG) << __func__;
621 
622     assert(pai != NULL);
623     /* hostname may be NULL */
624     /* servname may be NULL */
625     assert(res != NULL);
626 
627     /*
628      * if the servname does not match socktype/protocol, ignore it.
629      */
630     if (get_portmatch(pai, servname) != 0) return 0;
631 
632     afd = find_afd(pai->ai_family);
633     if (afd == NULL) return 0;
634 
635     if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
636 
637     cp = strchr(hostname, SCOPE_DELIMITER);
638     if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
639 
640     /*
641      * Handle special case of <scoped_address><delimiter><scope id>
642      */
643     char* hostname2 = strdup(hostname);
644     if (hostname2 == NULL) return EAI_MEMORY;
645     /* terminate at the delimiter */
646     hostname2[cp - hostname] = '\0';
647     addr = hostname2;
648     scope = cp + 1;
649 
650     error = explore_numeric(pai, addr, servname, res, hostname);
651     if (error == 0) {
652         uint32_t scopeid;
653 
654         for (cur = *res; cur; cur = cur->ai_next) {
655             if (cur->ai_family != AF_INET6) continue;
656             sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
657             if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
658                 free(hostname2);
659                 return (EAI_NODATA); /* XXX: is return OK? */
660             }
661             sin6->sin6_scope_id = scopeid;
662         }
663     }
664 
665     free(hostname2);
666 
667     return error;
668 }
669 
get_canonname(const struct addrinfo * pai,struct addrinfo * ai,const char * str)670 static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
671     assert(pai != NULL);
672     assert(ai != NULL);
673     assert(str != NULL);
674 
675     if ((pai->ai_flags & AI_CANONNAME) != 0) {
676         ai->ai_canonname = strdup(str);
677         if (ai->ai_canonname == NULL) return EAI_MEMORY;
678     }
679     return 0;
680 }
681 
get_ai(const struct addrinfo * pai,const struct afd * afd,const char * addr)682 static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
683                                const char* addr) {
684     char* p;
685     struct addrinfo* ai;
686 
687     assert(pai != NULL);
688     assert(afd != NULL);
689     assert(addr != NULL);
690 
691     ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
692     if (ai == NULL) return NULL;
693 
694     memcpy(ai, pai, sizeof(struct addrinfo));
695     ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
696     memset(ai->ai_addr, 0, sizeof(sockaddr_union));
697 
698     ai->ai_addrlen = afd->a_socklen;
699     ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
700     p = (char*) (void*) (ai->ai_addr);
701     memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
702     return ai;
703 }
704 
get_portmatch(const struct addrinfo * ai,const char * servname)705 static int get_portmatch(const struct addrinfo* ai, const char* servname) {
706     assert(ai != NULL);
707     /* servname may be NULL */
708 
709     return get_port(ai, servname, 1);
710 }
711 
get_port(const struct addrinfo * ai,const char * servname,int matchonly)712 static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
713     const char* proto;
714     struct servent* sp;
715     int port;
716     int allownumeric;
717 
718     assert(ai != NULL);
719     /* servname may be NULL */
720 
721     if (servname == NULL) return 0;
722     switch (ai->ai_family) {
723         case AF_INET:
724         case AF_INET6:
725             break;
726         default:
727             return 0;
728     }
729 
730     switch (ai->ai_socktype) {
731         case SOCK_RAW:
732             return EAI_SERVICE;
733         case SOCK_DGRAM:
734         case SOCK_STREAM:
735         case ANY:
736             allownumeric = 1;
737             break;
738         default:
739             return EAI_SOCKTYPE;
740     }
741 
742     port = str2number(servname);
743     if (port >= 0) {
744         if (!allownumeric) return EAI_SERVICE;
745         if (port < 0 || port > 65535) return EAI_SERVICE;
746         port = htons(port);
747     } else {
748         if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
749 
750         switch (ai->ai_socktype) {
751             case SOCK_DGRAM:
752                 proto = "udp";
753                 break;
754             case SOCK_STREAM:
755                 proto = "tcp";
756                 break;
757             default:
758                 proto = NULL;
759                 break;
760         }
761 
762         if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
763         port = sp->s_port;
764     }
765 
766     if (!matchonly) {
767         switch (ai->ai_family) {
768             case AF_INET:
769                 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
770                 break;
771             case AF_INET6:
772                 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
773                 break;
774         }
775     }
776 
777     return 0;
778 }
779 
find_afd(int af)780 static const struct afd* find_afd(int af) {
781     const struct afd* afd;
782 
783     if (af == PF_UNSPEC) return NULL;
784     for (afd = afdl; afd->a_af; afd++) {
785         if (afd->a_af == af) return afd;
786     }
787     return NULL;
788 }
789 
790 // Convert a string to a scope identifier.
ip6_str2scopeid(const char * scope,struct sockaddr_in6 * sin6,uint32_t * scopeid)791 static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, uint32_t* scopeid) {
792     uint64_t lscopeid;
793     struct in6_addr* a6;
794     char* ep;
795 
796     assert(scope != NULL);
797     assert(sin6 != NULL);
798     assert(scopeid != NULL);
799 
800     a6 = &sin6->sin6_addr;
801 
802     /* empty scopeid portion is invalid */
803     if (*scope == '\0') return -1;
804 
805     if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
806         /*
807          * We currently assume a one-to-one mapping between links
808          * and interfaces, so we simply use interface indices for
809          * like-local scopes.
810          */
811         *scopeid = if_nametoindex(scope);
812         if (*scopeid != 0) return 0;
813     }
814 
815     // try to convert to a numeric id as a last resort
816     errno = 0;
817     lscopeid = strtoul(scope, &ep, 10);
818     *scopeid = (uint32_t)(lscopeid & 0xffffffffUL);
819     if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
820         return 0;
821     else
822         return -1;
823 }
824 
825 /* code duplicate with gethnamaddr.c */
826 
827 #define BOUNDED_INCR(x)      \
828     do {                     \
829         BOUNDS_CHECK(cp, x); \
830         cp += (x);           \
831     } while (0)
832 
833 #define BOUNDS_CHECK(ptr, count)     \
834     do {                             \
835         if (eom - (ptr) < (count)) { \
836             *herrno = NO_RECOVERY;   \
837             return NULL;             \
838         }                            \
839     } while (0)
840 
getanswer(const std::vector<uint8_t> & answer,int anslen,const char * qname,int qtype,const struct addrinfo * pai,int * herrno)841 static struct addrinfo* getanswer(const std::vector<uint8_t>& answer, int anslen, const char* qname,
842                                   int qtype, const struct addrinfo* pai, int* herrno) {
843     struct addrinfo sentinel = {};
844     struct addrinfo *cur;
845     struct addrinfo ai;
846     const struct afd* afd;
847     char* canonname;
848     const HEADER* hp;
849     const uint8_t* cp;
850     int n;
851     const uint8_t* eom;
852     char *bp, *ep;
853     int type, ancount, qdcount;
854     int haveanswer, had_error;
855     char tbuf[MAXDNAME];
856     char hostbuf[8 * 1024];
857 
858     assert(qname != NULL);
859     assert(pai != NULL);
860 
861     cur = &sentinel;
862 
863     canonname = NULL;
864     eom = answer.data() + anslen;
865 
866     bool (*name_ok)(const char* dn);
867     switch (qtype) {
868         case T_A:
869         case T_AAAA:
870         case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
871             name_ok = res_hnok;
872             break;
873         default:
874             return NULL; /* XXX should be abort(); */
875     }
876     /*
877      * find first satisfactory answer
878      */
879     hp = reinterpret_cast<const HEADER*>(answer.data());
880     ancount = ntohs(hp->ancount);
881     qdcount = ntohs(hp->qdcount);
882     bp = hostbuf;
883     ep = hostbuf + sizeof hostbuf;
884     cp = answer.data();
885     BOUNDED_INCR(HFIXEDSZ);
886     if (qdcount != 1) {
887         *herrno = NO_RECOVERY;
888         return (NULL);
889     }
890     n = dn_expand(answer.data(), eom, cp, bp, ep - bp);
891     if ((n < 0) || !(*name_ok)(bp)) {
892         *herrno = NO_RECOVERY;
893         return (NULL);
894     }
895     BOUNDED_INCR(n + QFIXEDSZ);
896     if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
897         /* res_send() has already verified that the query name is the
898          * same as the one we sent; this just gets the expanded name
899          * (i.e., with the succeeding search-domain tacked on).
900          */
901         n = strlen(bp) + 1; /* for the \0 */
902         if (n >= MAXHOSTNAMELEN) {
903             *herrno = NO_RECOVERY;
904             return (NULL);
905         }
906         canonname = bp;
907         bp += n;
908         /* The qname can be abbreviated, but h_name is now absolute. */
909         qname = canonname;
910     }
911     haveanswer = 0;
912     had_error = 0;
913     while (ancount-- > 0 && cp < eom && !had_error) {
914         n = dn_expand(answer.data(), eom, cp, bp, ep - bp);
915         if ((n < 0) || !(*name_ok)(bp)) {
916             had_error++;
917             continue;
918         }
919         cp += n; /* name */
920         BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
921         type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
922         cp += INT16SZ; /* type */
923         int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
924         cp += INT16SZ + INT32SZ; /* class, TTL */
925         n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
926         cp += INT16SZ; /* len */
927         BOUNDS_CHECK(cp, n);
928         if (cl != C_IN) {
929             /* XXX - debug? syslog? */
930             cp += n;
931             continue; /* XXX - had_error++ ? */
932         }
933         if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
934             n = dn_expand(answer.data(), eom, cp, tbuf, sizeof tbuf);
935             if ((n < 0) || !(*name_ok)(tbuf)) {
936                 had_error++;
937                 continue;
938             }
939             cp += n;
940             /* Get canonical name. */
941             n = strlen(tbuf) + 1; /* for the \0 */
942             if (n > ep - bp || n >= MAXHOSTNAMELEN) {
943                 had_error++;
944                 continue;
945             }
946             strlcpy(bp, tbuf, (size_t)(ep - bp));
947             canonname = bp;
948             bp += n;
949             continue;
950         }
951         if (qtype == T_ANY) {
952             if (!(type == T_A || type == T_AAAA)) {
953                 cp += n;
954                 continue;
955             }
956         } else if (type != qtype) {
957             if (type != T_KEY && type != T_SIG)
958                 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
959                            << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
960             cp += n;
961             continue; /* XXX - had_error++ ? */
962         }
963         switch (type) {
964             case T_A:
965             case T_AAAA:
966                 if (strcasecmp(canonname, bp) != 0) {
967                     LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp
968                                << "\"";
969                     cp += n;
970                     continue; /* XXX - had_error++ ? */
971                 }
972                 if (type == T_A && n != INADDRSZ) {
973                     cp += n;
974                     continue;
975                 }
976                 if (type == T_AAAA && n != IN6ADDRSZ) {
977                     cp += n;
978                     continue;
979                 }
980                 if (type == T_AAAA) {
981                     struct in6_addr in6;
982                     memcpy(&in6, cp, IN6ADDRSZ);
983                     if (IN6_IS_ADDR_V4MAPPED(&in6)) {
984                         cp += n;
985                         continue;
986                     }
987                 }
988                 if (!haveanswer) {
989                     int nn;
990 
991                     canonname = bp;
992                     nn = strlen(bp) + 1; /* for the \0 */
993                     bp += nn;
994                 }
995 
996                 /* don't overwrite pai */
997                 ai = *pai;
998                 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
999                 afd = find_afd(ai.ai_family);
1000                 if (afd == NULL) {
1001                     cp += n;
1002                     continue;
1003                 }
1004                 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1005                 if (cur->ai_next == NULL) had_error++;
1006                 while (cur && cur->ai_next) cur = cur->ai_next;
1007                 cp += n;
1008                 break;
1009             default:
1010                 abort();
1011         }
1012         if (!had_error) haveanswer++;
1013     }
1014     if (haveanswer) {
1015         if (!canonname)
1016             (void) get_canonname(pai, sentinel.ai_next, qname);
1017         else
1018             (void) get_canonname(pai, sentinel.ai_next, canonname);
1019         *herrno = NETDB_SUCCESS;
1020         return sentinel.ai_next;
1021     }
1022 
1023     *herrno = NO_RECOVERY;
1024     return NULL;
1025 }
1026 
1027 struct addrinfo_sort_elem {
1028     struct addrinfo* ai;
1029     int has_src_addr;
1030     sockaddr_union src_addr;
1031     int original_order;
1032 };
1033 
_get_scope(const struct sockaddr * addr)1034 static int _get_scope(const struct sockaddr* addr) {
1035     if (addr->sa_family == AF_INET6) {
1036         const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1037         if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1038             return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1039         } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1040                    IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1041             /*
1042              * RFC 4291 section 2.5.3 says loopback is to be treated as having
1043              * link-local scope.
1044              */
1045             return IPV6_ADDR_SCOPE_LINKLOCAL;
1046         } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1047             return IPV6_ADDR_SCOPE_SITELOCAL;
1048         } else {
1049             return IPV6_ADDR_SCOPE_GLOBAL;
1050         }
1051     } else if (addr->sa_family == AF_INET) {
1052         const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1053         unsigned long int na = ntohl(addr4->sin_addr.s_addr);
1054 
1055         if (IN_LOOPBACK(na) ||                 /* 127.0.0.0/8 */
1056             (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1057             return IPV6_ADDR_SCOPE_LINKLOCAL;
1058         } else {
1059             /*
1060              * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1061              * and shared addresses (100.64.0.0/10), are assigned global scope.
1062              */
1063             return IPV6_ADDR_SCOPE_GLOBAL;
1064         }
1065     } else {
1066         /*
1067          * This should never happen.
1068          * Return a scope with low priority as a last resort.
1069          */
1070         return IPV6_ADDR_SCOPE_NODELOCAL;
1071     }
1072 }
1073 
1074 /* These macros are modelled after the ones in <netinet/in6.h>. */
1075 
1076 /* RFC 4380, section 2.6 */
1077 #define IN6_IS_ADDR_TEREDO(a) \
1078     ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
1079 
1080 /* RFC 3056, section 2. */
1081 #define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
1082 
1083 /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
1084 #define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
1085 
1086 /*
1087  * Get the label for a given IPv4/IPv6 address.
1088  * RFC 6724, section 2.1.
1089  */
1090 
_get_label(const struct sockaddr * addr)1091 static int _get_label(const struct sockaddr* addr) {
1092     if (addr->sa_family == AF_INET) {
1093         return 4;
1094     } else if (addr->sa_family == AF_INET6) {
1095         const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1096         if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1097             return 0;
1098         } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1099             return 4;
1100         } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1101             return 2;
1102         } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1103             return 5;
1104         } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1105             return 13;
1106         } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1107             return 3;
1108         } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1109             return 11;
1110         } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1111             return 12;
1112         } else {
1113             /* All other IPv6 addresses, including global unicast addresses. */
1114             return 1;
1115         }
1116     } else {
1117         /*
1118          * This should never happen.
1119          * Return a semi-random label as a last resort.
1120          */
1121         return 1;
1122     }
1123 }
1124 
1125 /*
1126  * Get the precedence for a given IPv4/IPv6 address.
1127  * RFC 6724, section 2.1.
1128  */
1129 
_get_precedence(const struct sockaddr * addr)1130 static int _get_precedence(const struct sockaddr* addr) {
1131     if (addr->sa_family == AF_INET) {
1132         return 35;
1133     } else if (addr->sa_family == AF_INET6) {
1134         const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1135         if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1136             return 50;
1137         } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1138             return 35;
1139         } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1140             return 30;
1141         } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1142             return 5;
1143         } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1144             return 3;
1145         } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1146                    IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1147                    IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1148             return 1;
1149         } else {
1150             /* All other IPv6 addresses, including global unicast addresses. */
1151             return 40;
1152         }
1153     } else {
1154         return 1;
1155     }
1156 }
1157 
1158 /*
1159  * Find number of matching initial bits between the two addresses a1 and a2.
1160  */
1161 
_common_prefix_len(const struct in6_addr * a1,const struct in6_addr * a2)1162 static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1163     const char* p1 = (const char*) a1;
1164     const char* p2 = (const char*) a2;
1165     unsigned i;
1166 
1167     for (i = 0; i < sizeof(*a1); ++i) {
1168         int x, j;
1169 
1170         if (p1[i] == p2[i]) {
1171             continue;
1172         }
1173         x = p1[i] ^ p2[i];
1174         for (j = 0; j < CHAR_BIT; ++j) {
1175             if (x & (1 << (CHAR_BIT - 1))) {
1176                 return i * CHAR_BIT + j;
1177             }
1178             x <<= 1;
1179         }
1180     }
1181     return sizeof(*a1) * CHAR_BIT;
1182 }
1183 
1184 /*
1185  * Compare two source/destination address pairs.
1186  * RFC 6724, section 6.
1187  */
1188 
_rfc6724_compare(const void * ptr1,const void * ptr2)1189 static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1190     const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1191     const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1192     int scope_src1, scope_dst1, scope_match1;
1193     int scope_src2, scope_dst2, scope_match2;
1194     int label_src1, label_dst1, label_match1;
1195     int label_src2, label_dst2, label_match2;
1196     int precedence1, precedence2;
1197     int prefixlen1, prefixlen2;
1198 
1199     /* Rule 1: Avoid unusable destinations. */
1200     if (a1->has_src_addr != a2->has_src_addr) {
1201         return a2->has_src_addr - a1->has_src_addr;
1202     }
1203 
1204     /* Rule 2: Prefer matching scope. */
1205     scope_src1 = _get_scope(&a1->src_addr.sa);
1206     scope_dst1 = _get_scope(a1->ai->ai_addr);
1207     scope_match1 = (scope_src1 == scope_dst1);
1208 
1209     scope_src2 = _get_scope(&a2->src_addr.sa);
1210     scope_dst2 = _get_scope(a2->ai->ai_addr);
1211     scope_match2 = (scope_src2 == scope_dst2);
1212 
1213     if (scope_match1 != scope_match2) {
1214         return scope_match2 - scope_match1;
1215     }
1216 
1217     /*
1218      * Rule 3: Avoid deprecated addresses.
1219      * TODO(sesse): We don't currently have a good way of finding this.
1220      */
1221 
1222     /*
1223      * Rule 4: Prefer home addresses.
1224      * TODO(sesse): We don't currently have a good way of finding this.
1225      */
1226 
1227     /* Rule 5: Prefer matching label. */
1228     label_src1 = _get_label(&a1->src_addr.sa);
1229     label_dst1 = _get_label(a1->ai->ai_addr);
1230     label_match1 = (label_src1 == label_dst1);
1231 
1232     label_src2 = _get_label(&a2->src_addr.sa);
1233     label_dst2 = _get_label(a2->ai->ai_addr);
1234     label_match2 = (label_src2 == label_dst2);
1235 
1236     if (label_match1 != label_match2) {
1237         return label_match2 - label_match1;
1238     }
1239 
1240     /* Rule 6: Prefer higher precedence. */
1241     precedence1 = _get_precedence(a1->ai->ai_addr);
1242     precedence2 = _get_precedence(a2->ai->ai_addr);
1243     if (precedence1 != precedence2) {
1244         return precedence2 - precedence1;
1245     }
1246 
1247     /*
1248      * Rule 7: Prefer native transport.
1249      * TODO(sesse): We don't currently have a good way of finding this.
1250      */
1251 
1252     /* Rule 8: Prefer smaller scope. */
1253     if (scope_dst1 != scope_dst2) {
1254         return scope_dst1 - scope_dst2;
1255     }
1256 
1257     /*
1258      * Rule 9: Use longest matching prefix.
1259      * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1260      * to work very well directly applied to IPv4. (glibc uses information from
1261      * the routing table for a custom IPv4 implementation here.)
1262      */
1263     if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1264         a2->ai->ai_addr->sa_family == AF_INET6) {
1265         const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
1266         const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1267         const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
1268         const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1269         prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1270         prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1271         if (prefixlen1 != prefixlen2) {
1272             return prefixlen2 - prefixlen1;
1273         }
1274     }
1275 
1276     /*
1277      * Rule 10: Leave the order unchanged.
1278      * We need this since qsort() is not necessarily stable.
1279      */
1280     return a1->original_order - a2->original_order;
1281 }
1282 
1283 /*
1284  * Find the source address that will be used if trying to connect to the given
1285  * address. src_addr must be large enough to hold a struct sockaddr_in6.
1286  *
1287  * Returns 1 if a source address was found, 0 if the address is unreachable,
1288  * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1289  * undefined.
1290  */
1291 
_find_src_addr(const struct sockaddr * addr,struct sockaddr * src_addr,unsigned mark,uid_t uid)1292 static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1293                           uid_t uid) {
1294     int sock;
1295     int ret;
1296     socklen_t len;
1297 
1298     switch (addr->sa_family) {
1299         case AF_INET:
1300             len = sizeof(struct sockaddr_in);
1301             break;
1302         case AF_INET6:
1303             len = sizeof(struct sockaddr_in6);
1304             break;
1305         default:
1306             /* No known usable source address for non-INET families. */
1307             return 0;
1308     }
1309 
1310     sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1311     if (sock == -1) {
1312         if (errno == EAFNOSUPPORT) {
1313             return 0;
1314         } else {
1315             return -1;
1316         }
1317     }
1318     if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1319         close(sock);
1320         return 0;
1321     }
1322     if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1323         close(sock);
1324         return 0;
1325     }
1326     do {
1327         ret = connect(sock, addr, len);
1328     } while (ret == -1 && errno == EINTR);
1329 
1330     if (ret == -1) {
1331         close(sock);
1332         return 0;
1333     }
1334 
1335     if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1336         close(sock);
1337         return -1;
1338     }
1339     close(sock);
1340     return 1;
1341 }
1342 
1343 /*
1344  * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1345  * Will leave the list unchanged if an error occurs.
1346  */
1347 
_rfc6724_sort(struct addrinfo * list_sentinel,unsigned mark,uid_t uid)1348 static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1349     struct addrinfo* cur;
1350     int nelem = 0, i;
1351     struct addrinfo_sort_elem* elems;
1352 
1353     cur = list_sentinel->ai_next;
1354     while (cur) {
1355         ++nelem;
1356         cur = cur->ai_next;
1357     }
1358 
1359     elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1360     if (elems == NULL) {
1361         goto error;
1362     }
1363 
1364     /*
1365      * Convert the linked list to an array that also contains the candidate
1366      * source address for each destination address.
1367      */
1368     for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1369         int has_src_addr;
1370         assert(cur != NULL);
1371         elems[i].ai = cur;
1372         elems[i].original_order = i;
1373 
1374         has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
1375         if (has_src_addr == -1) {
1376             goto error;
1377         }
1378         elems[i].has_src_addr = has_src_addr;
1379     }
1380 
1381     /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1382     qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
1383 
1384     list_sentinel->ai_next = elems[0].ai;
1385     for (i = 0; i < nelem - 1; ++i) {
1386         elems[i].ai->ai_next = elems[i + 1].ai;
1387     }
1388     elems[nelem - 1].ai->ai_next = NULL;
1389 
1390 error:
1391     free(elems);
1392 }
1393 
dns_getaddrinfo(const char * name,const addrinfo * pai,const android_net_context * netcontext,addrinfo ** rv,NetworkDnsEventReported * event)1394 static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1395                            const android_net_context* netcontext, addrinfo** rv,
1396                            NetworkDnsEventReported* event) {
1397     res_target q = {};
1398     res_target q2 = {};
1399 
1400     switch (pai->ai_family) {
1401         case AF_UNSPEC: {
1402             /* prefer IPv6 */
1403             q.name = name;
1404             q.qclass = C_IN;
1405             int query_ipv6 = 1, query_ipv4 = 1;
1406             if (pai->ai_flags & AI_ADDRCONFIG) {
1407                 query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid);
1408                 query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid);
1409             }
1410             if (query_ipv6) {
1411                 q.qtype = T_AAAA;
1412                 if (query_ipv4) {
1413                     q.next = &q2;
1414                     q2.name = name;
1415                     q2.qclass = C_IN;
1416                     q2.qtype = T_A;
1417                 }
1418             } else if (query_ipv4) {
1419                 q.qtype = T_A;
1420             } else {
1421                 return EAI_NODATA;
1422             }
1423             break;
1424         }
1425         case AF_INET:
1426             q.name = name;
1427             q.qclass = C_IN;
1428             q.qtype = T_A;
1429             break;
1430         case AF_INET6:
1431             q.name = name;
1432             q.qclass = C_IN;
1433             q.qtype = T_AAAA;
1434             break;
1435         default:
1436             return EAI_FAMILY;
1437     }
1438 
1439     ResState res;
1440     res_init(&res, netcontext, event);
1441 
1442     int he;
1443     if (res_searchN(name, &q, &res, &he) < 0) {
1444         // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
1445         // Note that res_searchN() doesn't set the pair NETDB_INTERNAL and errno.
1446         // See also herrnoToAiErrno().
1447         return herrnoToAiErrno(he);
1448     }
1449 
1450     addrinfo sentinel = {};
1451     addrinfo* cur = &sentinel;
1452     addrinfo* ai = getanswer(q.answer, q.n, q.name, q.qtype, pai, &he);
1453     if (ai) {
1454         cur->ai_next = ai;
1455         while (cur && cur->ai_next) cur = cur->ai_next;
1456     }
1457     if (q.next) {
1458         ai = getanswer(q2.answer, q2.n, q2.name, q2.qtype, pai, &he);
1459         if (ai) cur->ai_next = ai;
1460     }
1461     if (sentinel.ai_next == NULL) {
1462         // Note that getanswer() doesn't set the pair NETDB_INTERNAL and errno.
1463         // See also herrnoToAiErrno().
1464         return herrnoToAiErrno(he);
1465     }
1466 
1467     _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
1468 
1469     *rv = sentinel.ai_next;
1470     return 0;
1471 }
1472 
_sethtent(FILE ** hostf)1473 static void _sethtent(FILE** hostf) {
1474     if (!*hostf)
1475         *hostf = fopen(_PATH_HOSTS, "re");
1476     else
1477         rewind(*hostf);
1478 }
1479 
_endhtent(FILE ** hostf)1480 static void _endhtent(FILE** hostf) {
1481     if (*hostf) {
1482         (void) fclose(*hostf);
1483         *hostf = NULL;
1484     }
1485 }
1486 
_gethtent(FILE ** hostf,const char * name,const struct addrinfo * pai)1487 static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1488     char* p;
1489     char *cp, *tname, *cname;
1490     struct addrinfo *res0, *res;
1491     int error;
1492     const char* addr;
1493     char hostbuf[8 * 1024];
1494 
1495     assert(name != NULL);
1496     assert(pai != NULL);
1497 
1498     if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1499 again:
1500     if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1501     if (*p == '#') goto again;
1502     if (!(cp = strpbrk(p, "#\n"))) goto again;
1503     *cp = '\0';
1504     if (!(cp = strpbrk(p, " \t"))) goto again;
1505     *cp++ = '\0';
1506     addr = p;
1507     /* if this is not something we're looking for, skip it. */
1508     cname = NULL;
1509     while (cp && *cp) {
1510         if (*cp == ' ' || *cp == '\t') {
1511             cp++;
1512             continue;
1513         }
1514         if (!cname) cname = cp;
1515         tname = cp;
1516         if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1517         if (strcasecmp(name, tname) == 0) goto found;
1518     }
1519     goto again;
1520 
1521 found:
1522     error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
1523     if (error) goto again;
1524     for (res = res0; res; res = res->ai_next) {
1525         /* cover it up */
1526         res->ai_flags = pai->ai_flags;
1527 
1528         if (pai->ai_flags & AI_CANONNAME) {
1529             if (get_canonname(pai, res, cname) != 0) {
1530                 freeaddrinfo(res0);
1531                 goto again;
1532             }
1533         }
1534     }
1535     return res0;
1536 }
1537 
getCustomHosts(const size_t netid,const char * _Nonnull name,const struct addrinfo * _Nonnull pai)1538 static struct addrinfo* getCustomHosts(const size_t netid, const char* _Nonnull name,
1539                                        const struct addrinfo* _Nonnull pai) {
1540     struct addrinfo sentinel = {};
1541     struct addrinfo *res0, *res;
1542     res = &sentinel;
1543     std::vector<std::string> hosts = getCustomizedTableByName(netid, name);
1544     for (const std::string& host : hosts) {
1545         int error = getaddrinfo_numeric(host.c_str(), nullptr, *pai, &res0);
1546         if (!error && res0 != nullptr) {
1547             res->ai_next = res0;
1548             res = res0;
1549             res0 = nullptr;
1550         }
1551     }
1552     return sentinel.ai_next;
1553 }
1554 
files_getaddrinfo(const size_t netid,const char * name,const addrinfo * pai,addrinfo ** res)1555 static bool files_getaddrinfo(const size_t netid, const char* name, const addrinfo* pai,
1556                               addrinfo** res) {
1557     struct addrinfo sentinel = {};
1558     struct addrinfo *p, *cur;
1559     FILE* hostf = nullptr;
1560 
1561     cur = &sentinel;
1562     _sethtent(&hostf);
1563     while ((p = _gethtent(&hostf, name, pai)) != nullptr) {
1564         cur->ai_next = p;
1565         while (cur && cur->ai_next) cur = cur->ai_next;
1566     }
1567     _endhtent(&hostf);
1568 
1569     if ((p = getCustomHosts(netid, name, pai)) != nullptr) {
1570         cur->ai_next = p;
1571     }
1572 
1573     *res = sentinel.ai_next;
1574     return sentinel.ai_next != nullptr;
1575 }
1576 
1577 /* resolver logic */
1578 
1579 namespace {
1580 
1581 constexpr int SLEEP_TIME_MS = 2;
1582 
getHerrnoFromRcode(int rcode)1583 int getHerrnoFromRcode(int rcode) {
1584     switch (rcode) {
1585         // Not defined in RFC.
1586         case RCODE_TIMEOUT:
1587             // DNS metrics monitors DNS query timeout.
1588             return NETD_RESOLV_H_ERRNO_EXT_TIMEOUT;  // extended h_errno.
1589         // Defined in RFC 1035 section 4.1.1.
1590         case NXDOMAIN:
1591             return HOST_NOT_FOUND;
1592         case SERVFAIL:
1593             return TRY_AGAIN;
1594         case NOERROR:
1595             return NO_DATA;
1596         case FORMERR:
1597         case NOTIMP:
1598         case REFUSED:
1599         default:
1600             return NO_RECOVERY;
1601     }
1602 }
1603 
1604 struct QueryResult {
1605     int ancount;
1606     int rcode;
1607     int herrno;
1608     NetworkDnsEventReported event;
1609 };
1610 
doQuery(const char * name,res_target * t,res_state res,std::chrono::milliseconds sleepTimeMs)1611 QueryResult doQuery(const char* name, res_target* t, res_state res,
1612                     std::chrono::milliseconds sleepTimeMs) {
1613     HEADER* hp = (HEADER*)(void*)t->answer.data();
1614 
1615     hp->rcode = NOERROR;  // default
1616 
1617     const int cl = t->qclass;
1618     const int type = t->qtype;
1619     const int anslen = t->answer.size();
1620 
1621     LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
1622 
1623     uint8_t buf[MAXPACKET];
1624 
1625     int n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, sizeof(buf),
1626                          res->netcontext_flags);
1627 
1628     if (n > 0 &&
1629         (res->netcontext_flags & (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS))) {
1630         n = res_nopt(res, n, buf, sizeof(buf), anslen);
1631     }
1632 
1633     NetworkDnsEventReported event;
1634     if (n <= 0) {
1635         LOG(ERROR) << __func__ << ": res_nmkquery failed";
1636         return {0, -1, NO_RECOVERY, event};
1637         return {
1638                 .ancount = 0,
1639                 .rcode = -1,
1640                 .herrno = NO_RECOVERY,
1641                 .event = event,
1642         };
1643     }
1644 
1645     ResState res_temp = fromResState(*res, &event);
1646 
1647     int rcode = NOERROR;
1648     n = res_nsend(&res_temp, buf, n, t->answer.data(), anslen, &rcode, 0, sleepTimeMs);
1649     if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1650         // if the query choked with EDNS0, retry without EDNS0
1651         if ((res_temp.netcontext_flags &
1652              (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
1653             (res_temp._flags & RES_F_EDNS0ERR)) {
1654             LOG(DEBUG) << __func__ << ": retry without EDNS0";
1655             n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf,
1656                              sizeof(buf), res->netcontext_flags);
1657             n = res_nsend(&res_temp, buf, n, t->answer.data(), anslen, &rcode, 0);
1658         }
1659     }
1660 
1661     LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
1662 
1663     t->n = n;
1664     return {
1665             .ancount = ntohs(hp->ancount),
1666             .rcode = rcode,
1667             .event = event,
1668     };
1669 }
1670 
1671 }  // namespace
1672 
res_queryN_parallel(const char * name,res_target * target,res_state res,int * herrno)1673 static int res_queryN_parallel(const char* name, res_target* target, res_state res, int* herrno) {
1674     std::vector<std::future<QueryResult>> results;
1675     results.reserve(2);
1676     std::chrono::milliseconds sleepTimeMs{};
1677     for (res_target* t = target; t; t = t->next) {
1678         results.emplace_back(std::async(std::launch::async, doQuery, name, t, res, sleepTimeMs));
1679         // Avoiding gateways drop packets if queries are sent too close together
1680         // Only needed if we have multiple queries in a row.
1681         if (t->next) {
1682             int sleepFlag = android::net::Experiments::getInstance()->getFlag(
1683                     "parallel_lookup_sleep_time", SLEEP_TIME_MS);
1684             if (sleepFlag > 1000) sleepFlag = 1000;
1685             sleepTimeMs = std::chrono::milliseconds(sleepFlag);
1686         }
1687     }
1688 
1689     int ancount = 0;
1690     int rcode = 0;
1691 
1692     for (auto& f : results) {
1693         const QueryResult& r = f.get();
1694         if (r.herrno == NO_RECOVERY) {
1695             *herrno = r.herrno;
1696             return -1;
1697         }
1698         res->event->MergeFrom(r.event);
1699         ancount += r.ancount;
1700         rcode = r.rcode;
1701     }
1702 
1703     if (ancount == 0) {
1704         *herrno = getHerrnoFromRcode(rcode);
1705         return -1;
1706     }
1707 
1708     return ancount;
1709 }
1710 
res_queryN_wrapper(const char * name,res_target * target,res_state res,int * herrno)1711 static int res_queryN_wrapper(const char* name, res_target* target, res_state res, int* herrno) {
1712     const bool parallel_lookup =
1713             android::net::Experiments::getInstance()->getFlag("parallel_lookup", 0);
1714     if (parallel_lookup) return res_queryN_parallel(name, target, res, herrno);
1715 
1716     return res_queryN(name, target, res, herrno);
1717 }
1718 
1719 /*
1720  * Formulate a normal query, send, and await answer.
1721  * Returned answer is placed in supplied buffer "answer".
1722  * Perform preliminary check of answer, returning success only
1723  * if no error is indicated and the answer count is nonzero.
1724  * Return the size of the response on success, -1 on error.
1725  * Error number is left in *herrno.
1726  *
1727  * Caller must parse answer and determine whether it answers the question.
1728  */
res_queryN(const char * name,res_target * target,res_state res,int * herrno)1729 static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
1730     uint8_t buf[MAXPACKET];
1731     int n;
1732     struct res_target* t;
1733     int rcode;
1734     int ancount;
1735 
1736     assert(name != NULL);
1737     /* XXX: target may be NULL??? */
1738 
1739     rcode = NOERROR;
1740     ancount = 0;
1741 
1742     for (t = target; t; t = t->next) {
1743         HEADER* hp = (HEADER*)(void*)t->answer.data();
1744         bool retried = false;
1745     again:
1746         hp->rcode = NOERROR; /* default */
1747 
1748         /* make it easier... */
1749         int cl = t->qclass;
1750         int type = t->qtype;
1751         const int anslen = t->answer.size();
1752 
1753         LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
1754 
1755         n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, sizeof(buf),
1756                          res->netcontext_flags);
1757         if (n > 0 &&
1758             (res->netcontext_flags &
1759              (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
1760             !retried)  // TODO:  remove the retry flag and provide a sufficient test coverage.
1761             n = res_nopt(res, n, buf, sizeof(buf), anslen);
1762         if (n <= 0) {
1763             LOG(ERROR) << __func__ << ": res_nmkquery failed";
1764             *herrno = NO_RECOVERY;
1765             return n;
1766         }
1767 
1768         n = res_nsend(res, buf, n, t->answer.data(), anslen, &rcode, 0);
1769         if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1770             // Record rcode from DNS response header only if no timeout.
1771             // Keep rcode timeout for reporting later if any.
1772             if (rcode != RCODE_TIMEOUT) rcode = hp->rcode;  // record most recent error
1773             // if the query choked with EDNS0, retry without EDNS0 that when the server
1774             // has no response, resovler won't retry and do nothing. Even fallback to UDP,
1775             // we also has the same symptom if EDNS is enabled.
1776             if ((res->netcontext_flags &
1777                  (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
1778                 (res->_flags & RES_F_EDNS0ERR) && !retried) {
1779                 LOG(DEBUG) << __func__ << ": retry without EDNS0";
1780                 retried = true;
1781                 goto again;
1782             }
1783             LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
1784             continue;
1785         }
1786 
1787         ancount += ntohs(hp->ancount);
1788 
1789         t->n = n;
1790     }
1791 
1792     if (ancount == 0) {
1793         *herrno = getHerrnoFromRcode(rcode);
1794         return -1;
1795     }
1796     return ancount;
1797 }
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 *herrno.
1804  */
res_searchN(const char * name,res_target * target,res_state res,int * herrno)1805 static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
1806     const char* cp;
1807     HEADER* hp;
1808     uint32_t dots;
1809     int ret, saved_herrno;
1810     int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1811 
1812     assert(name != NULL);
1813     assert(target != NULL);
1814 
1815     hp = (HEADER*)(void*)target->answer.data();
1816 
1817     errno = 0;
1818     *herrno = HOST_NOT_FOUND; /* default, if we never query */
1819     dots = 0;
1820     for (cp = name; *cp; cp++) dots += (*cp == '.');
1821     const bool trailing_dot = (cp > name && *--cp == '.') ? true : false;
1822 
1823     /*
1824      * If there are dots in the name already, let's just give it a try
1825      * 'as is'.  The threshold can be set with the "ndots" option.
1826      */
1827     saved_herrno = -1;
1828     if (dots >= res->ndots) {
1829         ret = res_querydomainN(name, NULL, target, res, herrno);
1830         if (ret > 0) return (ret);
1831         saved_herrno = *herrno;
1832         tried_as_is++;
1833     }
1834 
1835     /*
1836      * We do at least one level of search if
1837      *	- there is no dot, or
1838      *	- there is at least one dot and there is no trailing dot.
1839      */
1840     if ((!dots) || (dots && !trailing_dot)) {
1841         int done = 0;
1842 
1843         /* Unfortunately we need to set stuff up before
1844          * the domain stuff is tried.  Will have a better
1845          * fix after thread pools are used.
1846          */
1847         resolv_populate_res_for_net(res);
1848 
1849         for (const auto& domain : res->search_domains) {
1850             ret = res_querydomainN(name, domain.c_str(), target, res, herrno);
1851             if (ret > 0) return ret;
1852 
1853             /*
1854              * If no server present, give up.
1855              * If name isn't found in this domain,
1856              * keep trying higher domains in the search list
1857              * (if that's enabled).
1858              * On a NO_DATA error, keep trying, otherwise
1859              * a wildcard entry of another type could keep us
1860              * from finding this entry higher in the domain.
1861              * If we get some other error (negative answer or
1862              * server failure), then stop searching up,
1863              * but try the input name below in case it's
1864              * fully-qualified.
1865              */
1866             if (errno == ECONNREFUSED) {
1867                 *herrno = TRY_AGAIN;
1868                 return -1;
1869             }
1870 
1871             switch (*herrno) {
1872                 case NO_DATA:
1873                     got_nodata++;
1874                     [[fallthrough]];
1875                 case HOST_NOT_FOUND:
1876                     /* keep trying */
1877                     break;
1878                 case TRY_AGAIN:
1879                     if (hp->rcode == SERVFAIL) {
1880                         /* try next search element, if any */
1881                         got_servfail++;
1882                         break;
1883                     }
1884                     [[fallthrough]];
1885                 default:
1886                     /* anything else implies that we're done */
1887                     done++;
1888             }
1889         }
1890     }
1891 
1892     /*
1893      * if we have not already tried the name "as is", do that now.
1894      * note that we do this regardless of how many dots were in the
1895      * name or whether it ends with a dot.
1896      */
1897     if (!tried_as_is) {
1898         ret = res_querydomainN(name, NULL, target, res, herrno);
1899         if (ret > 0) return ret;
1900     }
1901 
1902     /*
1903      * if we got here, we didn't satisfy the search.
1904      * if we did an initial full query, return that query's h_errno
1905      * (note that we wouldn't be here if that query had succeeded).
1906      * else if we ever got a nodata, send that back as the reason.
1907      * else send back meaningless h_errno, that being the one from
1908      * the last DNSRCH we did.
1909      */
1910     if (saved_herrno != -1)
1911         *herrno = saved_herrno;
1912     else if (got_nodata)
1913         *herrno = NO_DATA;
1914     else if (got_servfail)
1915         *herrno = TRY_AGAIN;
1916     return -1;
1917 }
1918 
1919 // Perform a call on res_query on the concatenation of name and domain,
1920 // removing a trailing dot from name if domain is NULL.
res_querydomainN(const char * name,const char * domain,res_target * target,res_state res,int * herrno)1921 static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
1922                             int* herrno) {
1923     char nbuf[MAXDNAME];
1924     const char* longname = nbuf;
1925     size_t n, d;
1926 
1927     assert(name != NULL);
1928 
1929     if (domain == NULL) {
1930         // Check for trailing '.'; copy without '.' if present.
1931         n = strlen(name);
1932         if (n + 1 > sizeof(nbuf)) {
1933             *herrno = NO_RECOVERY;
1934             return -1;
1935         }
1936         if (n > 0 && name[--n] == '.') {
1937             strncpy(nbuf, name, n);
1938             nbuf[n] = '\0';
1939         } else
1940             longname = name;
1941     } else {
1942         n = strlen(name);
1943         d = strlen(domain);
1944         if (n + 1 + d + 1 > sizeof(nbuf)) {
1945             *herrno = NO_RECOVERY;
1946             return -1;
1947         }
1948         snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1949     }
1950     return res_queryN_wrapper(longname, target, res, herrno);
1951 }
1952