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