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