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