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