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