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