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