Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | |
Ken Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 33 | #define LOG_TAG "resolv" |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 34 | |
Bernie Innocenti | e71a28a | 2019-05-29 00:42:35 +0900 | [diff] [blame] | 35 | #include "getaddrinfo.h" |
| 36 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 37 | #include <arpa/inet.h> |
| 38 | #include <arpa/nameser.h> |
| 39 | #include <assert.h> |
| 40 | #include <ctype.h> |
| 41 | #include <errno.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 42 | #include <fcntl.h> |
| 43 | #include <net/if.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 44 | #include <netdb.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 45 | #include <netinet/in.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 46 | #include <stdbool.h> |
| 47 | #include <stddef.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 50 | #include <sys/param.h> |
| 51 | #include <sys/socket.h> |
| 52 | #include <sys/stat.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 53 | #include <sys/un.h> |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 54 | #include <unistd.h> |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 55 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 56 | #include <android-base/logging.h> |
| 57 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 58 | #include "netd_resolv/resolv.h" |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 59 | #include "res_init.h" |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 60 | #include "resolv_cache.h" |
| 61 | #include "resolv_private.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 62 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 63 | #define ANY 0 |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 64 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 65 | using android::net::NetworkDnsEventReported; |
| 66 | |
Bernie Innocenti | 4e374b6 | 2018-12-12 00:43:02 +0900 | [diff] [blame] | 67 | const char in_addrany[] = {0, 0, 0, 0}; |
| 68 | const char in_loopback[] = {127, 0, 0, 1}; |
| 69 | const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 70 | const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 71 | |
Bernie Innocenti | 4e374b6 | 2018-12-12 00:43:02 +0900 | [diff] [blame] | 72 | const struct afd { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 73 | int a_af; |
| 74 | int a_addrlen; |
| 75 | int a_socklen; |
| 76 | int a_off; |
| 77 | const char* a_addrany; |
| 78 | const char* a_loopback; |
| 79 | int a_scoped; |
| 80 | } afdl[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 81 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), |
| 82 | offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1}, |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 83 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), |
| 84 | offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0}, |
| 85 | {0, 0, 0, 0, NULL, NULL, 0}, |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | }; |
| 87 | |
Bernie Innocenti | b0b32bc | 2019-02-20 18:21:24 +0900 | [diff] [blame] | 88 | struct Explore { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 89 | int e_af; |
| 90 | int e_socktype; |
| 91 | int e_protocol; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 92 | int e_wild; |
Bernie Innocenti | b0b32bc | 2019-02-20 18:21:24 +0900 | [diff] [blame] | 93 | #define WILD_AF(ex) ((ex).e_wild & 0x01) |
| 94 | #define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02) |
| 95 | #define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 96 | }; |
| 97 | |
Bernie Innocenti | b0b32bc | 2019-02-20 18:21:24 +0900 | [diff] [blame] | 98 | const Explore explore_options[] = { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 99 | {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07}, |
| 100 | {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07}, |
| 101 | {PF_INET6, SOCK_RAW, ANY, 0x05}, |
| 102 | {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07}, |
| 103 | {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07}, |
| 104 | {PF_INET, SOCK_RAW, ANY, 0x05}, |
| 105 | {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07}, |
| 106 | {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07}, |
| 107 | {PF_UNSPEC, SOCK_RAW, ANY, 0x05}, |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 108 | }; |
| 109 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 110 | #define PTON_MAX 16 |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 111 | #define MAXPACKET (8 * 1024) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 112 | |
| 113 | typedef union { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 114 | HEADER hdr; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 115 | uint8_t buf[MAXPACKET]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 116 | } querybuf; |
| 117 | |
| 118 | struct res_target { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | struct res_target* next; |
| 120 | const char* name; /* domain name */ |
| 121 | int qclass, qtype; /* class and type of query */ |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 122 | uint8_t* answer; /* buffer to put answer */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 123 | int anslen; /* size of answer buffer */ |
| 124 | int n; /* result length */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 125 | }; |
| 126 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 127 | static int str2number(const char*); |
| 128 | static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 129 | const struct android_net_context*, NetworkDnsEventReported* event); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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); |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 140 | static int ip6_str2scopeid(const char*, struct sockaddr_in6*, uint32_t*); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 141 | |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 142 | static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*, |
| 143 | int* herrno); |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 144 | static int dns_getaddrinfo(const char* name, const addrinfo* pai, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 145 | const android_net_context* netcontext, addrinfo** rv, |
| 146 | NetworkDnsEventReported* event); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | static void _sethtent(FILE**); |
| 148 | static void _endhtent(FILE**); |
| 149 | static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*); |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 150 | static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 151 | static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 152 | |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 153 | static int res_queryN(const char* name, res_target* target, res_state res, int* herrno); |
| 154 | static int res_searchN(const char* name, res_target* target, res_state res, int* herrno); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 155 | static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res, |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 156 | int* herrno); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 157 | |
Bernie Innocenti | 4e374b6 | 2018-12-12 00:43:02 +0900 | [diff] [blame] | 158 | const char* const ai_errlist[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 159 | "Success", |
| 160 | "Address family for hostname not supported", /* EAI_ADDRFAMILY */ |
| 161 | "Temporary failure in name resolution", /* EAI_AGAIN */ |
| 162 | "Invalid value for ai_flags", /* EAI_BADFLAGS */ |
| 163 | "Non-recoverable failure in name resolution", /* EAI_FAIL */ |
| 164 | "ai_family not supported", /* EAI_FAMILY */ |
| 165 | "Memory allocation failure", /* EAI_MEMORY */ |
| 166 | "No address associated with hostname", /* EAI_NODATA */ |
| 167 | "hostname nor servname provided, or not known", /* EAI_NONAME */ |
| 168 | "servname not supported for ai_socktype", /* EAI_SERVICE */ |
| 169 | "ai_socktype not supported", /* EAI_SOCKTYPE */ |
| 170 | "System error returned in errno", /* EAI_SYSTEM */ |
| 171 | "Invalid value for hints", /* EAI_BADHINTS */ |
| 172 | "Resolved protocol is unknown", /* EAI_PROTOCOL */ |
| 173 | "Argument buffer overflow", /* EAI_OVERFLOW */ |
| 174 | "Unknown error", /* EAI_MAX */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | /* XXX macros that make external reference is BAD. */ |
| 178 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 179 | #define GET_AI(ai, afd, addr) \ |
| 180 | do { \ |
| 181 | /* external reference: pai, error, and label free */ \ |
| 182 | (ai) = get_ai(pai, (afd), (addr)); \ |
| 183 | if ((ai) == NULL) { \ |
| 184 | error = EAI_MEMORY; \ |
| 185 | goto free; \ |
| 186 | } \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 187 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 188 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 189 | #define GET_PORT(ai, serv) \ |
| 190 | do { \ |
| 191 | /* external reference: error and label free */ \ |
| 192 | error = get_port((ai), (serv), 0); \ |
| 193 | if (error != 0) goto free; \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 194 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 195 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 196 | #define MATCH_FAMILY(x, y, w) \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 197 | ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) |
| 198 | #define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY))) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 199 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 200 | const char* gai_strerror(int ecode) { |
| 201 | if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX; |
| 202 | return ai_errlist[ecode]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 203 | } |
| 204 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 205 | void freeaddrinfo(struct addrinfo* ai) { |
Bernie Innocenti | 18a5ab5 | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 206 | while (ai) { |
| 207 | struct addrinfo* next = ai->ai_next; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 208 | if (ai->ai_canonname) free(ai->ai_canonname); |
Bernie Innocenti | 18a5ab5 | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 209 | // Also frees ai->ai_addr which points to extra space beyond addrinfo |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 210 | free(ai); |
| 211 | ai = next; |
Bernie Innocenti | 18a5ab5 | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 212 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 213 | } |
| 214 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 215 | static int str2number(const char* p) { |
| 216 | char* ep; |
| 217 | unsigned long v; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 218 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 219 | assert(p != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 220 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 221 | if (*p == '\0') return -1; |
| 222 | ep = NULL; |
| 223 | errno = 0; |
| 224 | v = strtoul(p, &ep, 10); |
| 225 | if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) |
| 226 | return v; |
| 227 | else |
| 228 | return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * The following functions determine whether IPv4 or IPv6 connectivity is |
| 233 | * available in order to implement AI_ADDRCONFIG. |
| 234 | * |
| 235 | * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is |
| 236 | * available, but whether addresses of the specified family are "configured |
| 237 | * on the local system". However, bionic doesn't currently support getifaddrs, |
| 238 | * so checking for connectivity is the next best thing. |
| 239 | */ |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 240 | static int have_ipv6(unsigned mark, uid_t uid) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 241 | static const struct sockaddr_in6 sin6_test = { |
| 242 | .sin6_family = AF_INET6, |
| 243 | .sin6_addr.s6_addr = {// 2000:: |
| 244 | 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 245 | sockaddr_union addr = {.sin6 = sin6_test}; |
| 246 | return _find_src_addr(&addr.sa, NULL, mark, uid) == 1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 247 | } |
| 248 | |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 249 | static int have_ipv4(unsigned mark, uid_t uid) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 250 | static const struct sockaddr_in sin_test = { |
| 251 | .sin_family = AF_INET, |
| 252 | .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8 |
| 253 | }; |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 254 | sockaddr_union addr = {.sin = sin_test}; |
| 255 | return _find_src_addr(&addr.sa, NULL, mark, uid) == 1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 258 | // Internal version of getaddrinfo(), but limited to AI_NUMERICHOST. |
Mike Yu | c7649d1 | 2019-05-22 15:28:07 +0800 | [diff] [blame] | 259 | // NOTE: also called by resolv_set_nameservers(). |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 260 | int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints, |
| 261 | addrinfo** result) { |
| 262 | hints.ai_flags = AI_NUMERICHOST; |
| 263 | const android_net_context netcontext = { |
| 264 | .app_netid = NETID_UNSET, |
| 265 | .app_mark = MARK_UNSET, |
| 266 | .dns_netid = NETID_UNSET, |
| 267 | .dns_mark = MARK_UNSET, |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 268 | .uid = NET_CONTEXT_INVALID_UID, |
Praveen Moongalam Thyagarajan | 8ab18ba | 2019-09-04 14:46:50 -0700 | [diff] [blame] | 269 | .pid = NET_CONTEXT_INVALID_PID, |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 270 | }; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 271 | NetworkDnsEventReported event; |
| 272 | return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result, |
| 273 | &event); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 274 | } |
| 275 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 276 | namespace { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 277 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 278 | int validateHints(const addrinfo* _Nonnull hints) { |
| 279 | if (!hints) return EAI_BADHINTS; |
| 280 | |
| 281 | // error check for hints |
| 282 | if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) { |
| 283 | return EAI_BADHINTS; |
| 284 | } |
| 285 | if (hints->ai_flags & ~AI_MASK) { |
| 286 | return EAI_BADFLAGS; |
| 287 | } |
| 288 | if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET || |
| 289 | hints->ai_family == PF_INET6)) { |
| 290 | return EAI_FAMILY; |
| 291 | } |
| 292 | |
Luke Huang | d8ac475 | 2019-06-18 17:05:47 +0800 | [diff] [blame] | 293 | // Socket types which are not in explore_options. |
| 294 | switch (hints->ai_socktype) { |
| 295 | case SOCK_RAW: |
| 296 | case SOCK_DGRAM: |
| 297 | case SOCK_STREAM: |
| 298 | case ANY: |
| 299 | break; |
| 300 | default: |
| 301 | return EAI_SOCKTYPE; |
| 302 | } |
| 303 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 304 | if (hints->ai_socktype == ANY || hints->ai_protocol == ANY) return 0; |
| 305 | |
| 306 | // if both socktype/protocol are specified, check if they are meaningful combination. |
| 307 | for (const Explore& ex : explore_options) { |
| 308 | if (hints->ai_family != ex.e_af) continue; |
| 309 | if (ex.e_socktype == ANY) continue; |
| 310 | if (ex.e_protocol == ANY) continue; |
| 311 | if (hints->ai_socktype == ex.e_socktype && hints->ai_protocol != ex.e_protocol) { |
| 312 | return EAI_BADHINTS; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | } // namespace |
| 320 | |
| 321 | int android_getaddrinfofornetcontext(const char* hostname, const char* servname, |
| 322 | const addrinfo* hints, const android_net_context* netcontext, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 323 | addrinfo** res, NetworkDnsEventReported* event) { |
Bernie Innocenti | 0cfe9dc | 2019-02-20 18:39:35 +0900 | [diff] [blame] | 324 | // hostname is allowed to be nullptr |
| 325 | // servname is allowed to be nullptr |
| 326 | // hints is allowed to be nullptr |
| 327 | assert(res != nullptr); |
| 328 | assert(netcontext != nullptr); |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 329 | assert(event != nullptr); |
Bernie Innocenti | 2b1afbe | 2019-02-20 17:50:38 +0900 | [diff] [blame] | 330 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 331 | addrinfo sentinel = {}; |
| 332 | addrinfo* cur = &sentinel; |
| 333 | int error = 0; |
Bernie Innocenti | 2b1afbe | 2019-02-20 17:50:38 +0900 | [diff] [blame] | 334 | |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 335 | do { |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 336 | if (hostname == nullptr && servname == nullptr) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 337 | error = EAI_NONAME; |
| 338 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 339 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 340 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 341 | if (hints && (error = validateHints(hints))) break; |
| 342 | addrinfo ai = hints ? *hints : addrinfo{}; |
Bernie Innocenti | 2b1afbe | 2019-02-20 17:50:38 +0900 | [diff] [blame] | 343 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 344 | // Check for special cases: |
| 345 | // (1) numeric servname is disallowed if socktype/protocol are left unspecified. |
| 346 | // (2) servname is disallowed for raw and other inet{,6} sockets. |
Bernie Innocenti | 2b1afbe | 2019-02-20 17:50:38 +0900 | [diff] [blame] | 347 | if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) { |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 348 | addrinfo tmp = ai; |
Bernie Innocenti | 0cfe9dc | 2019-02-20 18:39:35 +0900 | [diff] [blame] | 349 | if (tmp.ai_family == PF_UNSPEC) { |
| 350 | tmp.ai_family = PF_INET6; |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 351 | } |
Bernie Innocenti | 0cfe9dc | 2019-02-20 18:39:35 +0900 | [diff] [blame] | 352 | error = get_portmatch(&tmp, servname); |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 353 | if (error) break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 354 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 355 | |
Bernie Innocenti | b0b32bc | 2019-02-20 18:21:24 +0900 | [diff] [blame] | 356 | // NULL hostname, or numeric hostname |
| 357 | for (const Explore& ex : explore_options) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 358 | /* PF_UNSPEC entries are prepared for DNS queries only */ |
Bernie Innocenti | b0b32bc | 2019-02-20 18:21:24 +0900 | [diff] [blame] | 359 | if (ex.e_af == PF_UNSPEC) continue; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 360 | |
Bernie Innocenti | b0b32bc | 2019-02-20 18:21:24 +0900 | [diff] [blame] | 361 | if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue; |
| 362 | if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue; |
| 363 | if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 364 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 365 | addrinfo tmp = ai; |
Bernie Innocenti | 0cfe9dc | 2019-02-20 18:39:35 +0900 | [diff] [blame] | 366 | if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af; |
| 367 | if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype; |
| 368 | if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol; |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 369 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 370 | LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 371 | << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol; |
Bernie Innocenti | 0cfe9dc | 2019-02-20 18:39:35 +0900 | [diff] [blame] | 372 | if (hostname == nullptr) |
| 373 | error = explore_null(&tmp, servname, &cur->ai_next); |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 374 | else |
Bernie Innocenti | 0cfe9dc | 2019-02-20 18:39:35 +0900 | [diff] [blame] | 375 | error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next); |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 376 | |
| 377 | if (error) break; |
| 378 | |
| 379 | while (cur->ai_next) cur = cur->ai_next; |
| 380 | } |
| 381 | if (error) break; |
| 382 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 383 | // If numeric representation of AF1 can be interpreted as FQDN |
| 384 | // representation of AF2, we need to think again about the code below. |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 385 | if (sentinel.ai_next) break; |
| 386 | |
Bernie Innocenti | b0b32bc | 2019-02-20 18:21:24 +0900 | [diff] [blame] | 387 | if (hostname == nullptr) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 388 | error = EAI_NODATA; |
| 389 | break; |
| 390 | } |
Bernie Innocenti | 2b1afbe | 2019-02-20 17:50:38 +0900 | [diff] [blame] | 391 | if (ai.ai_flags & AI_NUMERICHOST) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 392 | error = EAI_NONAME; |
| 393 | break; |
| 394 | } |
| 395 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 396 | return resolv_getaddrinfo(hostname, servname, hints, netcontext, res, event); |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 397 | } while (0); |
| 398 | |
| 399 | if (error) { |
| 400 | freeaddrinfo(sentinel.ai_next); |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 401 | *res = nullptr; |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 402 | } else { |
| 403 | *res = sentinel.ai_next; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 404 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 405 | return error; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 406 | } |
| 407 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 408 | int resolv_getaddrinfo(const char* _Nonnull hostname, const char* servname, const addrinfo* hints, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 409 | const android_net_context* _Nonnull netcontext, addrinfo** _Nonnull res, |
| 410 | NetworkDnsEventReported* _Nonnull event) { |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 411 | if (hostname == nullptr && servname == nullptr) return EAI_NONAME; |
| 412 | if (hostname == nullptr) return EAI_NODATA; |
| 413 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 414 | // servname is allowed to be nullptr |
| 415 | // hints is allowed to be nullptr |
| 416 | assert(res != nullptr); |
| 417 | assert(netcontext != nullptr); |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 418 | assert(event != nullptr); |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 419 | |
| 420 | int error = EAI_FAIL; |
| 421 | if (hints && (error = validateHints(hints))) { |
| 422 | *res = nullptr; |
| 423 | return error; |
| 424 | } |
| 425 | |
| 426 | addrinfo ai = hints ? *hints : addrinfo{}; |
| 427 | addrinfo sentinel = {}; |
| 428 | addrinfo* cur = &sentinel; |
| 429 | // hostname as alphanumeric name. |
| 430 | // We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs. |
| 431 | for (const Explore& ex : explore_options) { |
| 432 | // Require exact match for family field |
| 433 | if (ai.ai_family != ex.e_af) continue; |
| 434 | |
| 435 | if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue; |
| 436 | |
| 437 | if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue; |
| 438 | |
| 439 | addrinfo tmp = ai; |
| 440 | if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype; |
| 441 | if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol; |
| 442 | |
| 443 | LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family |
| 444 | << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 445 | error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext, event); |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 446 | |
| 447 | while (cur->ai_next) cur = cur->ai_next; |
| 448 | } |
| 449 | |
Luke Huang | d8ac475 | 2019-06-18 17:05:47 +0800 | [diff] [blame] | 450 | // Propagate the last error from explore_fqdn(), but only when *all* attempts failed. |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 451 | if ((*res = sentinel.ai_next)) return 0; |
| 452 | |
Luke Huang | d8ac475 | 2019-06-18 17:05:47 +0800 | [diff] [blame] | 453 | // TODO: consider removing freeaddrinfo. |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 454 | freeaddrinfo(sentinel.ai_next); |
| 455 | *res = nullptr; |
Luke Huang | d8ac475 | 2019-06-18 17:05:47 +0800 | [diff] [blame] | 456 | return (error == 0) ? EAI_FAIL : error; |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 457 | } |
| 458 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 459 | // FQDN hostname, DNS lookup |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 460 | static int explore_fqdn(const addrinfo* pai, const char* hostname, const char* servname, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 461 | addrinfo** res, const android_net_context* netcontext, |
| 462 | NetworkDnsEventReported* event) { |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 463 | assert(pai != nullptr); |
| 464 | // hostname may be nullptr |
| 465 | // servname may be nullptr |
| 466 | assert(res != nullptr); |
| 467 | |
| 468 | addrinfo* result = nullptr; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 469 | int error = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 470 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 471 | // If the servname does not match socktype/protocol, return error code. |
| 472 | if ((error = get_portmatch(pai, servname))) return error; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 473 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 474 | if (!files_getaddrinfo(hostname, pai, &result)) { |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 475 | error = dns_getaddrinfo(hostname, pai, netcontext, &result, event); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 476 | } |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 477 | if (error) { |
| 478 | freeaddrinfo(result); |
| 479 | return error; |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 480 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 481 | |
Luke Huang | 69e6718 | 2019-06-17 17:06:41 +0800 | [diff] [blame] | 482 | for (addrinfo* cur = result; cur; cur = cur->ai_next) { |
| 483 | // canonname should be filled already |
| 484 | if ((error = get_port(cur, servname, 0))) { |
| 485 | freeaddrinfo(result); |
| 486 | return error; |
| 487 | } |
| 488 | } |
| 489 | *res = result; |
| 490 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* |
| 494 | * hostname == NULL. |
| 495 | * passive socket -> anyaddr (0.0.0.0 or ::) |
| 496 | * non-passive socket -> localhost (127.0.0.1 or ::1) |
| 497 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 498 | static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) { |
| 499 | int s; |
| 500 | const struct afd* afd; |
| 501 | struct addrinfo* cur; |
| 502 | struct addrinfo sentinel; |
| 503 | int error; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 504 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 505 | LOG(DEBUG) << __func__; |
| 506 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 507 | assert(pai != NULL); |
| 508 | /* servname may be NULL */ |
| 509 | assert(res != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 510 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 511 | *res = NULL; |
| 512 | sentinel.ai_next = NULL; |
| 513 | cur = &sentinel; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 514 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 515 | /* |
| 516 | * filter out AFs that are not supported by the kernel |
| 517 | * XXX errno? |
| 518 | */ |
| 519 | s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 520 | if (s < 0) { |
| 521 | if (errno != EMFILE) return 0; |
| 522 | } else |
| 523 | close(s); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 524 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 525 | /* |
| 526 | * if the servname does not match socktype/protocol, ignore it. |
| 527 | */ |
| 528 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 529 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 530 | afd = find_afd(pai->ai_family); |
| 531 | if (afd == NULL) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 532 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 533 | if (pai->ai_flags & AI_PASSIVE) { |
| 534 | GET_AI(cur->ai_next, afd, afd->a_addrany); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 535 | GET_PORT(cur->ai_next, servname); |
| 536 | } else { |
| 537 | GET_AI(cur->ai_next, afd, afd->a_loopback); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 538 | GET_PORT(cur->ai_next, servname); |
| 539 | } |
| 540 | cur = cur->ai_next; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 541 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 542 | *res = sentinel.ai_next; |
| 543 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 544 | |
| 545 | free: |
Bernie Innocenti | 18a5ab5 | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 546 | freeaddrinfo(sentinel.ai_next); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 547 | return error; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
| 551 | * numeric hostname |
| 552 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 553 | static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname, |
| 554 | struct addrinfo** res, const char* canonname) { |
| 555 | const struct afd* afd; |
| 556 | struct addrinfo* cur; |
| 557 | struct addrinfo sentinel; |
| 558 | int error; |
| 559 | char pton[PTON_MAX]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 560 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 561 | assert(pai != NULL); |
| 562 | /* hostname may be NULL */ |
| 563 | /* servname may be NULL */ |
| 564 | assert(res != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 565 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 566 | *res = NULL; |
| 567 | sentinel.ai_next = NULL; |
| 568 | cur = &sentinel; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 569 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 570 | /* |
| 571 | * if the servname does not match socktype/protocol, ignore it. |
| 572 | */ |
| 573 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 574 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 575 | afd = find_afd(pai->ai_family); |
| 576 | if (afd == NULL) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 577 | |
Ken Chen | a4b3302 | 2018-10-17 00:19:59 +0800 | [diff] [blame] | 578 | if (inet_pton(afd->a_af, hostname, pton) == 1) { |
| 579 | if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) { |
| 580 | GET_AI(cur->ai_next, afd, pton); |
| 581 | GET_PORT(cur->ai_next, servname); |
| 582 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 583 | /* |
| 584 | * Set the numeric address itself as |
| 585 | * the canonical name, based on a |
| 586 | * clarification in rfc2553bis-03. |
| 587 | */ |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 588 | error = get_canonname(pai, cur->ai_next, canonname); |
| 589 | if (error != 0) { |
| 590 | freeaddrinfo(sentinel.ai_next); |
| 591 | return error; |
| 592 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 593 | } |
Ken Chen | a4b3302 | 2018-10-17 00:19:59 +0800 | [diff] [blame] | 594 | while (cur->ai_next) cur = cur->ai_next; |
| 595 | } else |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 596 | return EAI_FAMILY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 597 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 598 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 599 | *res = sentinel.ai_next; |
| 600 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 601 | |
| 602 | free: |
Bernie Innocenti | 18a5ab5 | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 603 | freeaddrinfo(sentinel.ai_next); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 604 | return error; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
| 608 | * numeric hostname with scope |
| 609 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 610 | static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname, |
| 611 | const char* servname, struct addrinfo** res) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 612 | const struct afd* afd; |
| 613 | struct addrinfo* cur; |
| 614 | int error; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 615 | const char *cp, *scope, *addr; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 616 | struct sockaddr_in6* sin6; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 617 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 618 | LOG(DEBUG) << __func__; |
| 619 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 620 | assert(pai != NULL); |
| 621 | /* hostname may be NULL */ |
| 622 | /* servname may be NULL */ |
| 623 | assert(res != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 624 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 625 | /* |
| 626 | * if the servname does not match socktype/protocol, ignore it. |
| 627 | */ |
| 628 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 629 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 630 | afd = find_afd(pai->ai_family); |
| 631 | if (afd == NULL) return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 632 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 633 | if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 634 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 635 | cp = strchr(hostname, SCOPE_DELIMITER); |
| 636 | if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 637 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 638 | /* |
| 639 | * Handle special case of <scoped_address><delimiter><scope id> |
| 640 | */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 641 | char* hostname2 = strdup(hostname); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 642 | if (hostname2 == NULL) return EAI_MEMORY; |
| 643 | /* terminate at the delimiter */ |
| 644 | hostname2[cp - hostname] = '\0'; |
| 645 | addr = hostname2; |
| 646 | scope = cp + 1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 647 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 648 | error = explore_numeric(pai, addr, servname, res, hostname); |
| 649 | if (error == 0) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 650 | uint32_t scopeid; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 651 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 652 | for (cur = *res; cur; cur = cur->ai_next) { |
| 653 | if (cur->ai_family != AF_INET6) continue; |
| 654 | sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr; |
| 655 | if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { |
| 656 | free(hostname2); |
| 657 | return (EAI_NODATA); /* XXX: is return OK? */ |
| 658 | } |
| 659 | sin6->sin6_scope_id = scopeid; |
| 660 | } |
| 661 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 662 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 663 | free(hostname2); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 664 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 665 | return error; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 666 | } |
| 667 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 668 | static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) { |
| 669 | assert(pai != NULL); |
| 670 | assert(ai != NULL); |
| 671 | assert(str != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 672 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 673 | if ((pai->ai_flags & AI_CANONNAME) != 0) { |
| 674 | ai->ai_canonname = strdup(str); |
| 675 | if (ai->ai_canonname == NULL) return EAI_MEMORY; |
| 676 | } |
| 677 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 678 | } |
| 679 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 680 | static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd, |
| 681 | const char* addr) { |
| 682 | char* p; |
| 683 | struct addrinfo* ai; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 684 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 685 | assert(pai != NULL); |
| 686 | assert(afd != NULL); |
| 687 | assert(addr != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 688 | |
nuccachen | a49c0ba | 2018-09-11 11:13:44 +0800 | [diff] [blame] | 689 | ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 690 | if (ai == NULL) return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 691 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 692 | memcpy(ai, pai, sizeof(struct addrinfo)); |
| 693 | ai->ai_addr = (struct sockaddr*) (void*) (ai + 1); |
nuccachen | a49c0ba | 2018-09-11 11:13:44 +0800 | [diff] [blame] | 694 | memset(ai->ai_addr, 0, sizeof(sockaddr_union)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 695 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 696 | ai->ai_addrlen = afd->a_socklen; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 697 | ai->ai_addr->sa_family = ai->ai_family = afd->a_af; |
| 698 | p = (char*) (void*) (ai->ai_addr); |
| 699 | memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen); |
| 700 | return ai; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 701 | } |
| 702 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 703 | static int get_portmatch(const struct addrinfo* ai, const char* servname) { |
| 704 | assert(ai != NULL); |
| 705 | /* servname may be NULL */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 706 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 707 | return get_port(ai, servname, 1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 708 | } |
| 709 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 710 | static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) { |
| 711 | const char* proto; |
| 712 | struct servent* sp; |
| 713 | int port; |
| 714 | int allownumeric; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 715 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 716 | assert(ai != NULL); |
| 717 | /* servname may be NULL */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 718 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 719 | if (servname == NULL) return 0; |
| 720 | switch (ai->ai_family) { |
| 721 | case AF_INET: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 722 | case AF_INET6: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 723 | break; |
| 724 | default: |
| 725 | return 0; |
| 726 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 727 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 728 | switch (ai->ai_socktype) { |
| 729 | case SOCK_RAW: |
| 730 | return EAI_SERVICE; |
| 731 | case SOCK_DGRAM: |
| 732 | case SOCK_STREAM: |
| 733 | allownumeric = 1; |
| 734 | break; |
| 735 | case ANY: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 736 | allownumeric = 1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 737 | break; |
| 738 | default: |
| 739 | return EAI_SOCKTYPE; |
| 740 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 741 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 749 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 761 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 762 | if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE; |
| 763 | port = sp->s_port; |
| 764 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 765 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 771 | case AF_INET6: |
| 772 | ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port; |
| 773 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 774 | } |
| 775 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 776 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 777 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 778 | } |
| 779 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 780 | static const struct afd* find_afd(int af) { |
| 781 | const struct afd* afd; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 782 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 788 | } |
| 789 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 790 | // Convert a string to a scope identifier. |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 791 | static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, uint32_t* scopeid) { |
| 792 | uint64_t lscopeid; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 793 | struct in6_addr* a6; |
| 794 | char* ep; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 795 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 796 | assert(scope != NULL); |
| 797 | assert(sin6 != NULL); |
| 798 | assert(scopeid != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 799 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 800 | a6 = &sin6->sin6_addr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 801 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 802 | /* empty scopeid portion is invalid */ |
| 803 | if (*scope == '\0') return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 804 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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); |
Bernie Innocenti | 63abf5b | 2019-06-11 21:46:51 +0900 | [diff] [blame] | 812 | if (*scopeid != 0) return 0; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 813 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 814 | |
Bernie Innocenti | 63abf5b | 2019-06-11 21:46:51 +0900 | [diff] [blame] | 815 | // try to convert to a numeric id as a last resort |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 816 | errno = 0; |
| 817 | lscopeid = strtoul(scope, &ep, 10); |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 818 | *scopeid = (uint32_t)(lscopeid & 0xffffffffUL); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 819 | if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) |
| 820 | return 0; |
| 821 | else |
| 822 | return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 823 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 824 | |
| 825 | /* code duplicate with gethnamaddr.c */ |
| 826 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 827 | #define BOUNDED_INCR(x) \ |
| 828 | do { \ |
| 829 | BOUNDS_CHECK(cp, x); \ |
| 830 | cp += (x); \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 831 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 832 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 833 | #define BOUNDS_CHECK(ptr, count) \ |
| 834 | do { \ |
| 835 | if (eom - (ptr) < (count)) { \ |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 836 | *herrno = NO_RECOVERY; \ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 837 | return NULL; \ |
| 838 | } \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 839 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 840 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 841 | static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype, |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 842 | const struct addrinfo* pai, int* herrno) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 843 | struct addrinfo sentinel = {}; |
| 844 | struct addrinfo *cur; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 845 | struct addrinfo ai; |
| 846 | const struct afd* afd; |
| 847 | char* canonname; |
| 848 | const HEADER* hp; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 849 | const uint8_t* cp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 850 | int n; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 851 | const uint8_t* eom; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 852 | char *bp, *ep; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 853 | int type, ancount, qdcount; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 854 | int haveanswer, had_error; |
| 855 | char tbuf[MAXDNAME]; |
| 856 | int (*name_ok)(const char*); |
| 857 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 858 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 859 | assert(answer != NULL); |
| 860 | assert(qname != NULL); |
| 861 | assert(pai != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 862 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 863 | cur = &sentinel; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 864 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 865 | canonname = NULL; |
| 866 | eom = answer->buf + anslen; |
| 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 = &answer->hdr; |
| 880 | ancount = ntohs(hp->ancount); |
| 881 | qdcount = ntohs(hp->qdcount); |
| 882 | bp = hostbuf; |
| 883 | ep = hostbuf + sizeof hostbuf; |
| 884 | cp = answer->buf; |
| 885 | BOUNDED_INCR(HFIXEDSZ); |
| 886 | if (qdcount != 1) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 887 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 888 | return (NULL); |
| 889 | } |
| 890 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 891 | if ((n < 0) || !(*name_ok)(bp)) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 892 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 903 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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->buf, 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); |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 921 | type = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 922 | cp += INT16SZ; /* type */ |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 923 | int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 924 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 925 | n = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 926 | cp += INT16SZ; /* len */ |
| 927 | BOUNDS_CHECK(cp, n); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 928 | if (cl != C_IN) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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->buf, 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) |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 958 | LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " " |
| 959 | << p_type(qtype) << "\", got type \"" << p_type(type) << "\""; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 967 | LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp |
| 968 | << "\""; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 990 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 991 | canonname = bp; |
| 992 | nn = strlen(bp) + 1; /* for the \0 */ |
| 993 | bp += nn; |
| 994 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 995 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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); |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1019 | *herrno = NETDB_SUCCESS; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1020 | return sentinel.ai_next; |
| 1021 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1022 | |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1023 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1024 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | struct addrinfo_sort_elem { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1028 | struct addrinfo* ai; |
| 1029 | int has_src_addr; |
| 1030 | sockaddr_union src_addr; |
| 1031 | int original_order; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1032 | }; |
| 1033 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1054 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /* These macros are modelled after the ones in <netinet/in6.h>. */ |
| 1075 | |
| 1076 | /* RFC 4380, section 2.6 */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1077 | #define IN6_IS_ADDR_TEREDO(a) \ |
| 1078 | ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000))) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1079 | |
| 1080 | /* RFC 3056, section 2. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1081 | #define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02)) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1082 | |
| 1083 | /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1084 | #define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe)) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1085 | |
| 1086 | /* |
| 1087 | * Get the label for a given IPv4/IPv6 address. |
| 1088 | * RFC 6724, section 2.1. |
| 1089 | */ |
| 1090 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * Get the precedence for a given IPv4/IPv6 address. |
| 1127 | * RFC 6724, section 2.1. |
| 1128 | */ |
| 1129 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Find number of matching initial bits between the two addresses a1 and a2. |
| 1160 | */ |
| 1161 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1166 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1167 | for (i = 0; i < sizeof(*a1); ++i) { |
| 1168 | int x, j; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1169 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | * Compare two source/destination address pairs. |
| 1186 | * RFC 6724, section 6. |
| 1187 | */ |
| 1188 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1198 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1203 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1204 | /* Rule 2: Prefer matching scope. */ |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1205 | scope_src1 = _get_scope(&a1->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1206 | scope_dst1 = _get_scope(a1->ai->ai_addr); |
| 1207 | scope_match1 = (scope_src1 == scope_dst1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1208 | |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1209 | scope_src2 = _get_scope(&a2->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1210 | scope_dst2 = _get_scope(a2->ai->ai_addr); |
| 1211 | scope_match2 = (scope_src2 == scope_dst2); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1212 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1213 | if (scope_match1 != scope_match2) { |
| 1214 | return scope_match2 - scope_match1; |
| 1215 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1216 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1217 | /* |
| 1218 | * Rule 3: Avoid deprecated addresses. |
| 1219 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1220 | */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1221 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1222 | /* |
| 1223 | * Rule 4: Prefer home addresses. |
| 1224 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1225 | */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1226 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1227 | /* Rule 5: Prefer matching label. */ |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1228 | label_src1 = _get_label(&a1->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1229 | label_dst1 = _get_label(a1->ai->ai_addr); |
| 1230 | label_match1 = (label_src1 == label_dst1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1231 | |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1232 | label_src2 = _get_label(&a2->src_addr.sa); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1233 | label_dst2 = _get_label(a2->ai->ai_addr); |
| 1234 | label_match2 = (label_src2 == label_dst2); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1235 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1236 | if (label_match1 != label_match2) { |
| 1237 | return label_match2 - label_match1; |
| 1238 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1239 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1246 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1247 | /* |
| 1248 | * Rule 7: Prefer native transport. |
| 1249 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1250 | */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1251 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1252 | /* Rule 8: Prefer smaller scope. */ |
| 1253 | if (scope_dst1 != scope_dst2) { |
| 1254 | return scope_dst1 - scope_dst2; |
| 1255 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1256 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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) { |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1265 | const struct sockaddr_in6* a1_src = &a1->src_addr.sin6; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1266 | const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr; |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1267 | const struct sockaddr_in6* a2_src = &a2->src_addr.sin6; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1275 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1297 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1309 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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 { |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 1327 | ret = connect(sock, addr, len); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1328 | } while (ret == -1 && errno == EINTR); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1329 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1330 | if (ret == -1) { |
| 1331 | close(sock); |
| 1332 | return 0; |
| 1333 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1334 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1335 | if (src_addr && getsockname(sock, src_addr, &len) == -1) { |
| 1336 | close(sock); |
| 1337 | return -1; |
| 1338 | } |
| 1339 | close(sock); |
| 1340 | return 1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1352 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1353 | cur = list_sentinel->ai_next; |
| 1354 | while (cur) { |
| 1355 | ++nelem; |
| 1356 | cur = cur->ai_next; |
| 1357 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1358 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1359 | elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem)); |
| 1360 | if (elems == NULL) { |
| 1361 | goto error; |
| 1362 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1363 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1373 | |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1374 | has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1375 | if (has_src_addr == -1) { |
| 1376 | goto error; |
| 1377 | } |
| 1378 | elems[i].has_src_addr = has_src_addr; |
| 1379 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1380 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1383 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 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; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1389 | |
| 1390 | error: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1391 | free(elems); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1392 | } |
| 1393 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1394 | static int dns_getaddrinfo(const char* name, const addrinfo* pai, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 1395 | const android_net_context* netcontext, addrinfo** rv, |
| 1396 | NetworkDnsEventReported* event) { |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 1397 | res_target q = {}; |
| 1398 | res_target q2 = {}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1399 | |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 1400 | auto buf = std::make_unique<querybuf>(); |
| 1401 | auto buf2 = std::make_unique<querybuf>(); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1402 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1403 | switch (pai->ai_family) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1404 | case AF_UNSPEC: { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1405 | /* prefer IPv6 */ |
| 1406 | q.name = name; |
| 1407 | q.qclass = C_IN; |
| 1408 | q.answer = buf->buf; |
| 1409 | q.anslen = sizeof(buf->buf); |
| 1410 | int query_ipv6 = 1, query_ipv4 = 1; |
| 1411 | if (pai->ai_flags & AI_ADDRCONFIG) { |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 1412 | query_ipv6 = have_ipv6(netcontext->app_mark, netcontext->uid); |
| 1413 | query_ipv4 = have_ipv4(netcontext->app_mark, netcontext->uid); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1414 | } |
| 1415 | if (query_ipv6) { |
| 1416 | q.qtype = T_AAAA; |
| 1417 | if (query_ipv4) { |
| 1418 | q.next = &q2; |
| 1419 | q2.name = name; |
| 1420 | q2.qclass = C_IN; |
| 1421 | q2.qtype = T_A; |
| 1422 | q2.answer = buf2->buf; |
| 1423 | q2.anslen = sizeof(buf2->buf); |
| 1424 | } |
| 1425 | } else if (query_ipv4) { |
| 1426 | q.qtype = T_A; |
| 1427 | } else { |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1428 | return EAI_NODATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1429 | } |
| 1430 | break; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1431 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1432 | case AF_INET: |
| 1433 | q.name = name; |
| 1434 | q.qclass = C_IN; |
| 1435 | q.qtype = T_A; |
| 1436 | q.answer = buf->buf; |
| 1437 | q.anslen = sizeof(buf->buf); |
| 1438 | break; |
| 1439 | case AF_INET6: |
| 1440 | q.name = name; |
| 1441 | q.qclass = C_IN; |
| 1442 | q.qtype = T_AAAA; |
| 1443 | q.answer = buf->buf; |
| 1444 | q.anslen = sizeof(buf->buf); |
| 1445 | break; |
| 1446 | default: |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1447 | return EAI_FAMILY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1448 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1449 | |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1450 | ResState res; |
| 1451 | res_init(&res, netcontext, event); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1452 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1453 | int he; |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1454 | if (res_searchN(name, &q, &res, &he) < 0) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1455 | // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA. |
| 1456 | // Note that res_searchN() doesn't set the pair NETDB_INTERNAL and errno. |
| 1457 | // See also herrnoToAiErrno(). |
| 1458 | return herrnoToAiErrno(he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1459 | } |
Bernie Innocenti | c50c43d | 2019-03-05 15:45:03 +0900 | [diff] [blame] | 1460 | |
| 1461 | addrinfo sentinel = {}; |
| 1462 | addrinfo* cur = &sentinel; |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1463 | addrinfo* ai = getanswer(buf.get(), q.n, q.name, q.qtype, pai, &he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1464 | if (ai) { |
| 1465 | cur->ai_next = ai; |
| 1466 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1467 | } |
| 1468 | if (q.next) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1469 | ai = getanswer(buf2.get(), q2.n, q2.name, q2.qtype, pai, &he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1470 | if (ai) cur->ai_next = ai; |
| 1471 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1472 | if (sentinel.ai_next == NULL) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 1473 | // Note that getanswer() doesn't set the pair NETDB_INTERNAL and errno. |
| 1474 | // See also herrnoToAiErrno(). |
| 1475 | return herrnoToAiErrno(he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1476 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1477 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1478 | _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1479 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1480 | *rv = sentinel.ai_next; |
| 1481 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1482 | } |
| 1483 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1484 | static void _sethtent(FILE** hostf) { |
| 1485 | if (!*hostf) |
| 1486 | *hostf = fopen(_PATH_HOSTS, "re"); |
| 1487 | else |
| 1488 | rewind(*hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1489 | } |
| 1490 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1491 | static void _endhtent(FILE** hostf) { |
| 1492 | if (*hostf) { |
| 1493 | (void) fclose(*hostf); |
| 1494 | *hostf = NULL; |
| 1495 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1496 | } |
| 1497 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1498 | static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) { |
| 1499 | char* p; |
| 1500 | char *cp, *tname, *cname; |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1501 | struct addrinfo *res0, *res; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1502 | int error; |
| 1503 | const char* addr; |
| 1504 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1505 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1506 | assert(name != NULL); |
| 1507 | assert(pai != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1508 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1509 | if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL); |
| 1510 | again: |
| 1511 | if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL); |
| 1512 | if (*p == '#') goto again; |
| 1513 | if (!(cp = strpbrk(p, "#\n"))) goto again; |
| 1514 | *cp = '\0'; |
| 1515 | if (!(cp = strpbrk(p, " \t"))) goto again; |
| 1516 | *cp++ = '\0'; |
| 1517 | addr = p; |
| 1518 | /* if this is not something we're looking for, skip it. */ |
| 1519 | cname = NULL; |
| 1520 | while (cp && *cp) { |
| 1521 | if (*cp == ' ' || *cp == '\t') { |
| 1522 | cp++; |
| 1523 | continue; |
| 1524 | } |
| 1525 | if (!cname) cname = cp; |
| 1526 | tname = cp; |
| 1527 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1528 | if (strcasecmp(name, tname) == 0) goto found; |
| 1529 | } |
| 1530 | goto again; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1531 | |
| 1532 | found: |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1533 | error = getaddrinfo_numeric(addr, nullptr, *pai, &res0); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1534 | if (error) goto again; |
| 1535 | for (res = res0; res; res = res->ai_next) { |
| 1536 | /* cover it up */ |
| 1537 | res->ai_flags = pai->ai_flags; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1538 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1539 | if (pai->ai_flags & AI_CANONNAME) { |
| 1540 | if (get_canonname(pai, res, cname) != 0) { |
| 1541 | freeaddrinfo(res0); |
| 1542 | goto again; |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | return res0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1547 | } |
| 1548 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1549 | static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) { |
Ken Chen | e0d73c9 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 1550 | struct addrinfo sentinel = {}; |
| 1551 | struct addrinfo *p, *cur; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1552 | FILE* hostf = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1553 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1554 | cur = &sentinel; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1555 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1556 | _sethtent(&hostf); |
| 1557 | while ((p = _gethtent(&hostf, name, pai)) != NULL) { |
| 1558 | cur->ai_next = p; |
| 1559 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1560 | } |
| 1561 | _endhtent(&hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1562 | |
Bernie Innocenti | 455c623 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1563 | *res = sentinel.ai_next; |
| 1564 | return sentinel.ai_next != NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /* resolver logic */ |
| 1568 | |
| 1569 | /* |
| 1570 | * Formulate a normal query, send, and await answer. |
| 1571 | * Returned answer is placed in supplied buffer "answer". |
| 1572 | * Perform preliminary check of answer, returning success only |
| 1573 | * if no error is indicated and the answer count is nonzero. |
| 1574 | * Return the size of the response on success, -1 on error. |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1575 | * Error number is left in *herrno. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1576 | * |
| 1577 | * Caller must parse answer and determine whether it answers the question. |
| 1578 | */ |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1579 | static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 1580 | uint8_t buf[MAXPACKET]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1581 | HEADER* hp; |
| 1582 | int n; |
| 1583 | struct res_target* t; |
| 1584 | int rcode; |
| 1585 | int ancount; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1586 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1587 | assert(name != NULL); |
| 1588 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1589 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1590 | rcode = NOERROR; |
| 1591 | ancount = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1592 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1593 | for (t = target; t; t = t->next) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 1594 | uint8_t* answer; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1595 | int anslen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1596 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1597 | hp = (HEADER*) (void*) t->answer; |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 1598 | bool retried = false; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1599 | again: |
| 1600 | hp->rcode = NOERROR; /* default */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1601 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1602 | /* make it easier... */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1603 | int cl = t->qclass; |
| 1604 | int type = t->qtype; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1605 | answer = t->answer; |
| 1606 | anslen = t->anslen; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 1607 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 1608 | LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1609 | |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 1610 | n = res_nmkquery(QUERY, name, cl, type, /*data=*/nullptr, /*datalen=*/0, buf, sizeof(buf), |
| 1611 | res->netcontext_flags); |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 1612 | if (n > 0 && |
| 1613 | (res->netcontext_flags & |
| 1614 | (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) && |
| 1615 | !retried) // TODO: remove the retry flag and provide a sufficient test coverage. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1616 | n = res_nopt(res, n, buf, sizeof(buf), anslen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1617 | if (n <= 0) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1618 | LOG(ERROR) << __func__ << ": res_nmkquery failed"; |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1619 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1620 | return n; |
| 1621 | } |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1622 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1623 | n = res_nsend(res, buf, n, answer, anslen, &rcode, 0); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1624 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1625 | // Record rcode from DNS response header only if no timeout. |
| 1626 | // Keep rcode timeout for reporting later if any. |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 1627 | if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; // record most recent error |
| 1628 | // if the query choked with EDNS0, retry without EDNS0 that when the server |
| 1629 | // has no response, resovler won't retry and do nothing. Even fallback to UDP, |
| 1630 | // we also has the same symptom if EDNS is enabled. |
| 1631 | if ((res->netcontext_flags & |
| 1632 | (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) && |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 1633 | (res->_flags & RES_F_EDNS0ERR) && !retried) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1634 | LOG(DEBUG) << __func__ << ": retry without EDNS0"; |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 1635 | retried = true; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1636 | goto again; |
| 1637 | } |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1638 | LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1639 | continue; |
| 1640 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1641 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1642 | ancount += ntohs(hp->ancount); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1643 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1644 | t->n = n; |
| 1645 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1646 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1647 | if (ancount == 0) { |
| 1648 | switch (rcode) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1649 | // Not defined in RFC. |
| 1650 | case RCODE_TIMEOUT: |
| 1651 | // DNS metrics monitors DNS query timeout. |
| 1652 | *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno. |
| 1653 | break; |
| 1654 | // Defined in RFC 1035 section 4.1.1. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1655 | case NXDOMAIN: |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1656 | *herrno = HOST_NOT_FOUND; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1657 | break; |
| 1658 | case SERVFAIL: |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1659 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1660 | break; |
| 1661 | case NOERROR: |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1662 | *herrno = NO_DATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1663 | break; |
| 1664 | case FORMERR: |
| 1665 | case NOTIMP: |
| 1666 | case REFUSED: |
| 1667 | default: |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1668 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1669 | break; |
| 1670 | } |
| 1671 | return -1; |
| 1672 | } |
| 1673 | return ancount; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /* |
| 1677 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 1678 | * Return the size of the response on success, -1 on error. |
| 1679 | * If enabled, implement search rules until answer or unrecoverable failure |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1680 | * is detected. Error code, if any, is left in *herrno. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1681 | */ |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1682 | static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) { |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1683 | const char* cp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1684 | HEADER* hp; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 1685 | uint32_t dots; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1686 | int ret, saved_herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1687 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1688 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1689 | assert(name != NULL); |
| 1690 | assert(target != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1691 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1692 | hp = (HEADER*) (void*) target->answer; /*XXX*/ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1693 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1694 | errno = 0; |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1695 | *herrno = HOST_NOT_FOUND; /* default, if we never query */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1696 | dots = 0; |
| 1697 | for (cp = name; *cp; cp++) dots += (*cp == '.'); |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1698 | const bool trailing_dot = (cp > name && *--cp == '.') ? true : false; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1699 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1700 | /* |
| 1701 | * If there are dots in the name already, let's just give it a try |
| 1702 | * 'as is'. The threshold can be set with the "ndots" option. |
| 1703 | */ |
| 1704 | saved_herrno = -1; |
| 1705 | if (dots >= res->ndots) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1706 | ret = res_querydomainN(name, NULL, target, res, herrno); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1707 | if (ret > 0) return (ret); |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1708 | saved_herrno = *herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1709 | tried_as_is++; |
| 1710 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1711 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1712 | /* |
| 1713 | * We do at least one level of search if |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1714 | * - there is no dot, or |
| 1715 | * - there is at least one dot and there is no trailing dot. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1716 | */ |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 1717 | if ((!dots) || (dots && !trailing_dot)) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1718 | int done = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1719 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1720 | /* Unfortunately we need to set stuff up before |
| 1721 | * the domain stuff is tried. Will have a better |
| 1722 | * fix after thread pools are used. |
| 1723 | */ |
| 1724 | _resolv_populate_res_for_net(res); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1725 | |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 1726 | for (const auto& domain : res->search_domains) { |
| 1727 | ret = res_querydomainN(name, domain.c_str(), target, res, herrno); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1728 | if (ret > 0) return ret; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1729 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1730 | /* |
| 1731 | * If no server present, give up. |
| 1732 | * If name isn't found in this domain, |
| 1733 | * keep trying higher domains in the search list |
| 1734 | * (if that's enabled). |
| 1735 | * On a NO_DATA error, keep trying, otherwise |
| 1736 | * a wildcard entry of another type could keep us |
| 1737 | * from finding this entry higher in the domain. |
| 1738 | * If we get some other error (negative answer or |
| 1739 | * server failure), then stop searching up, |
| 1740 | * but try the input name below in case it's |
| 1741 | * fully-qualified. |
| 1742 | */ |
| 1743 | if (errno == ECONNREFUSED) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1744 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1745 | return -1; |
| 1746 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1747 | |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1748 | switch (*herrno) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1749 | case NO_DATA: |
| 1750 | got_nodata++; |
Bernie Innocenti | f40b3bd | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 1751 | [[fallthrough]]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1752 | case HOST_NOT_FOUND: |
| 1753 | /* keep trying */ |
| 1754 | break; |
| 1755 | case TRY_AGAIN: |
| 1756 | if (hp->rcode == SERVFAIL) { |
| 1757 | /* try next search element, if any */ |
| 1758 | got_servfail++; |
| 1759 | break; |
| 1760 | } |
Bernie Innocenti | f40b3bd | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 1761 | [[fallthrough]]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1762 | default: |
| 1763 | /* anything else implies that we're done */ |
| 1764 | done++; |
| 1765 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1766 | } |
| 1767 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1768 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1769 | /* |
| 1770 | * if we have not already tried the name "as is", do that now. |
| 1771 | * note that we do this regardless of how many dots were in the |
| 1772 | * name or whether it ends with a dot. |
| 1773 | */ |
| 1774 | if (!tried_as_is) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1775 | ret = res_querydomainN(name, NULL, target, res, herrno); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1776 | if (ret > 0) return ret; |
| 1777 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1778 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1779 | /* |
| 1780 | * if we got here, we didn't satisfy the search. |
| 1781 | * if we did an initial full query, return that query's h_errno |
| 1782 | * (note that we wouldn't be here if that query had succeeded). |
| 1783 | * else if we ever got a nodata, send that back as the reason. |
| 1784 | * else send back meaningless h_errno, that being the one from |
| 1785 | * the last DNSRCH we did. |
| 1786 | */ |
| 1787 | if (saved_herrno != -1) |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1788 | *herrno = saved_herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1789 | else if (got_nodata) |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1790 | *herrno = NO_DATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1791 | else if (got_servfail) |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1792 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1793 | return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * Perform a call on res_query on the concatenation of name and domain, |
| 1798 | * removing a trailing dot from name if domain is NULL. |
| 1799 | */ |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 1800 | static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res, |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1801 | int* herrno) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1802 | char nbuf[MAXDNAME]; |
| 1803 | const char* longname = nbuf; |
| 1804 | size_t n, d; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1805 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1806 | assert(name != NULL); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1807 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1808 | if (domain == NULL) { |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 1809 | // Check for trailing '.'; copy without '.' if present. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1810 | n = strlen(name); |
| 1811 | if (n + 1 > sizeof(nbuf)) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1812 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1813 | return -1; |
| 1814 | } |
| 1815 | if (n > 0 && name[--n] == '.') { |
| 1816 | strncpy(nbuf, name, n); |
| 1817 | nbuf[n] = '\0'; |
| 1818 | } else |
| 1819 | longname = name; |
| 1820 | } else { |
| 1821 | n = strlen(name); |
| 1822 | d = strlen(domain); |
| 1823 | if (n + 1 + d + 1 > sizeof(nbuf)) { |
Hungming Chen | dd4bfb9 | 2018-12-25 15:47:47 +0800 | [diff] [blame] | 1824 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1825 | return -1; |
| 1826 | } |
| 1827 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 1828 | } |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 1829 | return res_queryN(longname, target, res, herrno); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1830 | } |