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