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 | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 200 | static int ip6_str2scopeid(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 | } \ |
| 243 | } while (/*CONSTCOND*/ 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; \ |
| 250 | } while (/*CONSTCOND*/ 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; \ |
| 257 | } while (/*CONSTCOND*/ 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*/ \ |
| 265 | } while (/*CONSTCOND*/ 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) \ |
| 268 | ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) |
| 269 | #define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (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 | |
| 522 | /* |
| 523 | * FQDN hostname, DNS lookup |
| 524 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 525 | static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname, |
| 526 | struct addrinfo** res, const struct android_net_context* netcontext) { |
| 527 | struct addrinfo* result; |
| 528 | struct addrinfo* cur; |
| 529 | int error = 0; |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 530 | static const ns_dtab dtab[] = { |
| 531 | {NSSRC_FILES, _files_getaddrinfo, NULL}, |
| 532 | {NSSRC_DNS, _dns_getaddrinfo, NULL}, |
| 533 | {0, 0, 0} |
| 534 | }; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 535 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 536 | assert(pai != NULL); |
| 537 | /* hostname may be NULL */ |
| 538 | /* servname may be NULL */ |
| 539 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 540 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 541 | result = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 542 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 543 | /* |
| 544 | * if the servname does not match socktype/protocol, ignore it. |
| 545 | */ |
| 546 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 547 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 548 | switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai, |
| 549 | netcontext)) { |
| 550 | case NS_TRYAGAIN: |
| 551 | error = EAI_AGAIN; |
| 552 | goto free; |
| 553 | case NS_UNAVAIL: |
| 554 | error = EAI_FAIL; |
| 555 | goto free; |
| 556 | case NS_NOTFOUND: |
| 557 | error = EAI_NODATA; |
| 558 | goto free; |
| 559 | case NS_SUCCESS: |
| 560 | error = 0; |
| 561 | for (cur = result; cur; cur = cur->ai_next) { |
| 562 | GET_PORT(cur, servname); |
| 563 | /* canonname should be filled already */ |
| 564 | } |
| 565 | break; |
| 566 | } |
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 | *res = result; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 569 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 570 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 571 | |
| 572 | free: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 573 | if (result) freeaddrinfo(result); |
| 574 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
| 578 | * hostname == NULL. |
| 579 | * passive socket -> anyaddr (0.0.0.0 or ::) |
| 580 | * non-passive socket -> localhost (127.0.0.1 or ::1) |
| 581 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 582 | static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) { |
| 583 | int s; |
| 584 | const struct afd* afd; |
| 585 | struct addrinfo* cur; |
| 586 | struct addrinfo sentinel; |
| 587 | int error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 588 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 589 | assert(pai != NULL); |
| 590 | /* servname may be NULL */ |
| 591 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 592 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 593 | *res = NULL; |
| 594 | sentinel.ai_next = NULL; |
| 595 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 596 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 597 | /* |
| 598 | * filter out AFs that are not supported by the kernel |
| 599 | * XXX errno? |
| 600 | */ |
| 601 | s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 602 | if (s < 0) { |
| 603 | if (errno != EMFILE) return 0; |
| 604 | } else |
| 605 | close(s); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 606 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 607 | /* |
| 608 | * if the servname does not match socktype/protocol, ignore it. |
| 609 | */ |
| 610 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 611 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 612 | afd = find_afd(pai->ai_family); |
| 613 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 614 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 615 | if (pai->ai_flags & AI_PASSIVE) { |
| 616 | GET_AI(cur->ai_next, afd, afd->a_addrany); |
| 617 | /* xxx meaningless? |
| 618 | * GET_CANONNAME(cur->ai_next, "anyaddr"); |
| 619 | */ |
| 620 | GET_PORT(cur->ai_next, servname); |
| 621 | } else { |
| 622 | GET_AI(cur->ai_next, afd, afd->a_loopback); |
| 623 | /* xxx meaningless? |
| 624 | * GET_CANONNAME(cur->ai_next, "localhost"); |
| 625 | */ |
| 626 | GET_PORT(cur->ai_next, servname); |
| 627 | } |
| 628 | cur = cur->ai_next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 629 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 630 | *res = sentinel.ai_next; |
| 631 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 632 | |
| 633 | free: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 634 | if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next); |
| 635 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
| 639 | * numeric hostname |
| 640 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 641 | static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname, |
| 642 | struct addrinfo** res, const char* canonname) { |
| 643 | const struct afd* afd; |
| 644 | struct addrinfo* cur; |
| 645 | struct addrinfo sentinel; |
| 646 | int error; |
| 647 | char pton[PTON_MAX]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 648 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 649 | assert(pai != NULL); |
| 650 | /* hostname may be NULL */ |
| 651 | /* servname may be NULL */ |
| 652 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 653 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 654 | *res = NULL; |
| 655 | sentinel.ai_next = NULL; |
| 656 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 657 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 658 | /* |
| 659 | * if the servname does not match socktype/protocol, ignore it. |
| 660 | */ |
| 661 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 662 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 663 | afd = find_afd(pai->ai_family); |
| 664 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 665 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 666 | switch (afd->a_af) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 667 | #if 0 /*X/Open spec*/ |
| 668 | case AF_INET: |
| 669 | if (inet_aton(hostname, (struct in_addr *)pton) == 1) { |
| 670 | if (pai->ai_family == afd->a_af || |
| 671 | pai->ai_family == PF_UNSPEC /*?*/) { |
| 672 | GET_AI(cur->ai_next, afd, pton); |
| 673 | GET_PORT(cur->ai_next, servname); |
| 674 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 675 | /* |
| 676 | * Set the numeric address itself as |
| 677 | * the canonical name, based on a |
| 678 | * clarification in rfc2553bis-03. |
| 679 | */ |
| 680 | GET_CANONNAME(cur->ai_next, canonname); |
| 681 | } |
| 682 | while (cur && cur->ai_next) |
| 683 | cur = cur->ai_next; |
| 684 | } else |
| 685 | ERR(EAI_FAMILY); /*xxx*/ |
| 686 | } |
| 687 | break; |
| 688 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 689 | default: |
| 690 | if (inet_pton(afd->a_af, hostname, pton) == 1) { |
| 691 | if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) { |
| 692 | GET_AI(cur->ai_next, afd, pton); |
| 693 | GET_PORT(cur->ai_next, servname); |
| 694 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 695 | /* |
| 696 | * Set the numeric address itself as |
| 697 | * the canonical name, based on a |
| 698 | * clarification in rfc2553bis-03. |
| 699 | */ |
| 700 | GET_CANONNAME(cur->ai_next, canonname); |
| 701 | } |
| 702 | while (cur->ai_next) cur = cur->ai_next; |
| 703 | } else |
| 704 | ERR(EAI_FAMILY); /*xxx*/ |
| 705 | } |
| 706 | break; |
| 707 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 708 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 709 | *res = sentinel.ai_next; |
| 710 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 711 | |
| 712 | free: |
| 713 | bad: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 714 | if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next); |
| 715 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* |
| 719 | * numeric hostname with scope |
| 720 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 721 | static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname, |
| 722 | const char* servname, struct addrinfo** res) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 723 | const struct afd* afd; |
| 724 | struct addrinfo* cur; |
| 725 | int error; |
| 726 | char *cp, *hostname2 = NULL, *scope, *addr; |
| 727 | struct sockaddr_in6* sin6; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 728 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 729 | assert(pai != NULL); |
| 730 | /* hostname may be NULL */ |
| 731 | /* servname may be NULL */ |
| 732 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 733 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 734 | /* |
| 735 | * if the servname does not match socktype/protocol, ignore it. |
| 736 | */ |
| 737 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 738 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 739 | afd = find_afd(pai->ai_family); |
| 740 | if (afd == NULL) return 0; |
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 | if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 743 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 744 | cp = strchr(hostname, SCOPE_DELIMITER); |
| 745 | if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 746 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 747 | /* |
| 748 | * Handle special case of <scoped_address><delimiter><scope id> |
| 749 | */ |
| 750 | hostname2 = strdup(hostname); |
| 751 | if (hostname2 == NULL) return EAI_MEMORY; |
| 752 | /* terminate at the delimiter */ |
| 753 | hostname2[cp - hostname] = '\0'; |
| 754 | addr = hostname2; |
| 755 | scope = cp + 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 756 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 757 | error = explore_numeric(pai, addr, servname, res, hostname); |
| 758 | if (error == 0) { |
| 759 | u_int32_t scopeid; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 760 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 761 | for (cur = *res; cur; cur = cur->ai_next) { |
| 762 | if (cur->ai_family != AF_INET6) continue; |
| 763 | sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr; |
| 764 | if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { |
| 765 | free(hostname2); |
| 766 | return (EAI_NODATA); /* XXX: is return OK? */ |
| 767 | } |
| 768 | sin6->sin6_scope_id = scopeid; |
| 769 | } |
| 770 | } |
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 | free(hostname2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 773 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 774 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 775 | } |
| 776 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 777 | static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) { |
| 778 | assert(pai != NULL); |
| 779 | assert(ai != NULL); |
| 780 | assert(str != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 781 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 782 | if ((pai->ai_flags & AI_CANONNAME) != 0) { |
| 783 | ai->ai_canonname = strdup(str); |
| 784 | if (ai->ai_canonname == NULL) return EAI_MEMORY; |
| 785 | } |
| 786 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 787 | } |
| 788 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 789 | static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd, |
| 790 | const char* addr) { |
| 791 | char* p; |
| 792 | struct addrinfo* ai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 793 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 794 | assert(pai != NULL); |
| 795 | assert(afd != NULL); |
| 796 | assert(addr != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 797 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 798 | ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen)); |
| 799 | if (ai == NULL) return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 800 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 801 | memcpy(ai, pai, sizeof(struct addrinfo)); |
| 802 | ai->ai_addr = (struct sockaddr*) (void*) (ai + 1); |
| 803 | memset(ai->ai_addr, 0, (size_t) afd->a_socklen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 804 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 805 | ai->ai_addrlen = afd->a_socklen; |
| 806 | #if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__) |
| 807 | ai->__ai_pad0 = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 808 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 809 | ai->ai_addr->sa_family = ai->ai_family = afd->a_af; |
| 810 | p = (char*) (void*) (ai->ai_addr); |
| 811 | memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen); |
| 812 | return ai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 813 | } |
| 814 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 815 | static int get_portmatch(const struct addrinfo* ai, const char* servname) { |
| 816 | assert(ai != NULL); |
| 817 | /* servname may be NULL */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 818 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 819 | return get_port(ai, servname, 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 820 | } |
| 821 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 822 | static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) { |
| 823 | const char* proto; |
| 824 | struct servent* sp; |
| 825 | int port; |
| 826 | int allownumeric; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 827 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 828 | assert(ai != NULL); |
| 829 | /* servname may be NULL */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 830 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 831 | if (servname == NULL) return 0; |
| 832 | switch (ai->ai_family) { |
| 833 | case AF_INET: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 834 | case AF_INET6: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 835 | break; |
| 836 | default: |
| 837 | return 0; |
| 838 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 839 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 840 | switch (ai->ai_socktype) { |
| 841 | case SOCK_RAW: |
| 842 | return EAI_SERVICE; |
| 843 | case SOCK_DGRAM: |
| 844 | case SOCK_STREAM: |
| 845 | allownumeric = 1; |
| 846 | break; |
| 847 | case ANY: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 848 | allownumeric = 1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 849 | break; |
| 850 | default: |
| 851 | return EAI_SOCKTYPE; |
| 852 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 853 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 854 | port = str2number(servname); |
| 855 | if (port >= 0) { |
| 856 | if (!allownumeric) return EAI_SERVICE; |
| 857 | if (port < 0 || port > 65535) return EAI_SERVICE; |
| 858 | port = htons(port); |
| 859 | } else { |
| 860 | if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 861 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 862 | switch (ai->ai_socktype) { |
| 863 | case SOCK_DGRAM: |
| 864 | proto = "udp"; |
| 865 | break; |
| 866 | case SOCK_STREAM: |
| 867 | proto = "tcp"; |
| 868 | break; |
| 869 | default: |
| 870 | proto = NULL; |
| 871 | break; |
| 872 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 873 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 874 | if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE; |
| 875 | port = sp->s_port; |
| 876 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 877 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 878 | if (!matchonly) { |
| 879 | switch (ai->ai_family) { |
| 880 | case AF_INET: |
| 881 | ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port; |
| 882 | break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 883 | case AF_INET6: |
| 884 | ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port; |
| 885 | break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 886 | } |
| 887 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 888 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 889 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 890 | } |
| 891 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 892 | static const struct afd* find_afd(int af) { |
| 893 | const struct afd* afd; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 894 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 895 | if (af == PF_UNSPEC) return NULL; |
| 896 | for (afd = afdl; afd->a_af; afd++) { |
| 897 | if (afd->a_af == af) return afd; |
| 898 | } |
| 899 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 900 | } |
| 901 | |
Bernie Innocenti | 357339c | 2018-08-31 16:11:41 +0900 | [diff] [blame^] | 902 | /* convert a string to a scope identifier. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 903 | static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) { |
| 904 | u_long lscopeid; |
| 905 | struct in6_addr* a6; |
| 906 | char* ep; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 907 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 908 | assert(scope != NULL); |
| 909 | assert(sin6 != NULL); |
| 910 | assert(scopeid != NULL); |
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 | a6 = &sin6->sin6_addr; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 913 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 914 | /* empty scopeid portion is invalid */ |
| 915 | if (*scope == '\0') return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 916 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 917 | if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { |
| 918 | /* |
| 919 | * We currently assume a one-to-one mapping between links |
| 920 | * and interfaces, so we simply use interface indices for |
| 921 | * like-local scopes. |
| 922 | */ |
| 923 | *scopeid = if_nametoindex(scope); |
| 924 | if (*scopeid == 0) goto trynumeric; |
| 925 | return 0; |
| 926 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 927 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 928 | /* still unclear about literal, allow numeric only - placeholder */ |
| 929 | if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric; |
| 930 | if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) |
| 931 | goto trynumeric; |
| 932 | else |
| 933 | goto trynumeric; /* global */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 934 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 935 | /* try to convert to a numeric id as a last resort */ |
| 936 | trynumeric: |
| 937 | errno = 0; |
| 938 | lscopeid = strtoul(scope, &ep, 10); |
| 939 | *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); |
| 940 | if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) |
| 941 | return 0; |
| 942 | else |
| 943 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 944 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 945 | |
| 946 | /* code duplicate with gethnamaddr.c */ |
| 947 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 948 | static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 949 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 950 | #define BOUNDED_INCR(x) \ |
| 951 | do { \ |
| 952 | BOUNDS_CHECK(cp, x); \ |
| 953 | cp += (x); \ |
| 954 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 955 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 956 | #define BOUNDS_CHECK(ptr, count) \ |
| 957 | do { \ |
| 958 | if (eom - (ptr) < (count)) { \ |
| 959 | h_errno = NO_RECOVERY; \ |
| 960 | return NULL; \ |
| 961 | } \ |
| 962 | } while (/*CONSTCOND*/ 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 963 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 964 | static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype, |
| 965 | const struct addrinfo* pai) { |
| 966 | struct addrinfo sentinel, *cur; |
| 967 | struct addrinfo ai; |
| 968 | const struct afd* afd; |
| 969 | char* canonname; |
| 970 | const HEADER* hp; |
| 971 | const u_char* cp; |
| 972 | int n; |
| 973 | const u_char* eom; |
| 974 | char *bp, *ep; |
| 975 | int type, class, ancount, qdcount; |
| 976 | int haveanswer, had_error; |
| 977 | char tbuf[MAXDNAME]; |
| 978 | int (*name_ok)(const char*); |
| 979 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 980 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 981 | assert(answer != NULL); |
| 982 | assert(qname != NULL); |
| 983 | assert(pai != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 984 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 985 | memset(&sentinel, 0, sizeof(sentinel)); |
| 986 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 987 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 988 | canonname = NULL; |
| 989 | eom = answer->buf + anslen; |
| 990 | switch (qtype) { |
| 991 | case T_A: |
| 992 | case T_AAAA: |
| 993 | case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ |
| 994 | name_ok = res_hnok; |
| 995 | break; |
| 996 | default: |
| 997 | return NULL; /* XXX should be abort(); */ |
| 998 | } |
| 999 | /* |
| 1000 | * find first satisfactory answer |
| 1001 | */ |
| 1002 | hp = &answer->hdr; |
| 1003 | ancount = ntohs(hp->ancount); |
| 1004 | qdcount = ntohs(hp->qdcount); |
| 1005 | bp = hostbuf; |
| 1006 | ep = hostbuf + sizeof hostbuf; |
| 1007 | cp = answer->buf; |
| 1008 | BOUNDED_INCR(HFIXEDSZ); |
| 1009 | if (qdcount != 1) { |
| 1010 | h_errno = NO_RECOVERY; |
| 1011 | return (NULL); |
| 1012 | } |
| 1013 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 1014 | if ((n < 0) || !(*name_ok)(bp)) { |
| 1015 | h_errno = NO_RECOVERY; |
| 1016 | return (NULL); |
| 1017 | } |
| 1018 | BOUNDED_INCR(n + QFIXEDSZ); |
| 1019 | if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { |
| 1020 | /* res_send() has already verified that the query name is the |
| 1021 | * same as the one we sent; this just gets the expanded name |
| 1022 | * (i.e., with the succeeding search-domain tacked on). |
| 1023 | */ |
| 1024 | n = strlen(bp) + 1; /* for the \0 */ |
| 1025 | if (n >= MAXHOSTNAMELEN) { |
| 1026 | h_errno = NO_RECOVERY; |
| 1027 | return (NULL); |
| 1028 | } |
| 1029 | canonname = bp; |
| 1030 | bp += n; |
| 1031 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 1032 | qname = canonname; |
| 1033 | } |
| 1034 | haveanswer = 0; |
| 1035 | had_error = 0; |
| 1036 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 1037 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 1038 | if ((n < 0) || !(*name_ok)(bp)) { |
| 1039 | had_error++; |
| 1040 | continue; |
| 1041 | } |
| 1042 | cp += n; /* name */ |
| 1043 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
| 1044 | type = _getshort(cp); |
| 1045 | cp += INT16SZ; /* type */ |
| 1046 | class = _getshort(cp); |
| 1047 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
| 1048 | n = _getshort(cp); |
| 1049 | cp += INT16SZ; /* len */ |
| 1050 | BOUNDS_CHECK(cp, n); |
| 1051 | if (class != C_IN) { |
| 1052 | /* XXX - debug? syslog? */ |
| 1053 | cp += n; |
| 1054 | continue; /* XXX - had_error++ ? */ |
| 1055 | } |
| 1056 | if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) { |
| 1057 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 1058 | if ((n < 0) || !(*name_ok)(tbuf)) { |
| 1059 | had_error++; |
| 1060 | continue; |
| 1061 | } |
| 1062 | cp += n; |
| 1063 | /* Get canonical name. */ |
| 1064 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 1065 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 1066 | had_error++; |
| 1067 | continue; |
| 1068 | } |
| 1069 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 1070 | canonname = bp; |
| 1071 | bp += n; |
| 1072 | continue; |
| 1073 | } |
| 1074 | if (qtype == T_ANY) { |
| 1075 | if (!(type == T_A || type == T_AAAA)) { |
| 1076 | cp += n; |
| 1077 | continue; |
| 1078 | } |
| 1079 | } else if (type != qtype) { |
| 1080 | if (type != T_KEY && type != T_SIG) |
| 1081 | syslog(LOG_NOTICE | LOG_AUTH, |
| 1082 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname, |
| 1083 | p_class(C_IN), p_type(qtype), p_type(type)); |
| 1084 | cp += n; |
| 1085 | continue; /* XXX - had_error++ ? */ |
| 1086 | } |
| 1087 | switch (type) { |
| 1088 | case T_A: |
| 1089 | case T_AAAA: |
| 1090 | if (strcasecmp(canonname, bp) != 0) { |
| 1091 | syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp); |
| 1092 | cp += n; |
| 1093 | continue; /* XXX - had_error++ ? */ |
| 1094 | } |
| 1095 | if (type == T_A && n != INADDRSZ) { |
| 1096 | cp += n; |
| 1097 | continue; |
| 1098 | } |
| 1099 | if (type == T_AAAA && n != IN6ADDRSZ) { |
| 1100 | cp += n; |
| 1101 | continue; |
| 1102 | } |
| 1103 | if (type == T_AAAA) { |
| 1104 | struct in6_addr in6; |
| 1105 | memcpy(&in6, cp, IN6ADDRSZ); |
| 1106 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 1107 | cp += n; |
| 1108 | continue; |
| 1109 | } |
| 1110 | } |
| 1111 | if (!haveanswer) { |
| 1112 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1113 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1114 | canonname = bp; |
| 1115 | nn = strlen(bp) + 1; /* for the \0 */ |
| 1116 | bp += nn; |
| 1117 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1118 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1119 | /* don't overwrite pai */ |
| 1120 | ai = *pai; |
| 1121 | ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; |
| 1122 | afd = find_afd(ai.ai_family); |
| 1123 | if (afd == NULL) { |
| 1124 | cp += n; |
| 1125 | continue; |
| 1126 | } |
| 1127 | cur->ai_next = get_ai(&ai, afd, (const char*) cp); |
| 1128 | if (cur->ai_next == NULL) had_error++; |
| 1129 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1130 | cp += n; |
| 1131 | break; |
| 1132 | default: |
| 1133 | abort(); |
| 1134 | } |
| 1135 | if (!had_error) haveanswer++; |
| 1136 | } |
| 1137 | if (haveanswer) { |
| 1138 | if (!canonname) |
| 1139 | (void) get_canonname(pai, sentinel.ai_next, qname); |
| 1140 | else |
| 1141 | (void) get_canonname(pai, sentinel.ai_next, canonname); |
| 1142 | h_errno = NETDB_SUCCESS; |
| 1143 | return sentinel.ai_next; |
| 1144 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1145 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1146 | h_errno = NO_RECOVERY; |
| 1147 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | struct addrinfo_sort_elem { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1151 | struct addrinfo* ai; |
| 1152 | int has_src_addr; |
| 1153 | sockaddr_union src_addr; |
| 1154 | int original_order; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1155 | }; |
| 1156 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1157 | static int _get_scope(const struct sockaddr* addr) { |
| 1158 | if (addr->sa_family == AF_INET6) { |
| 1159 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1160 | if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) { |
| 1161 | return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr); |
| 1162 | } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) || |
| 1163 | IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) { |
| 1164 | /* |
| 1165 | * RFC 4291 section 2.5.3 says loopback is to be treated as having |
| 1166 | * link-local scope. |
| 1167 | */ |
| 1168 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1169 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1170 | return IPV6_ADDR_SCOPE_SITELOCAL; |
| 1171 | } else { |
| 1172 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1173 | } |
| 1174 | } else if (addr->sa_family == AF_INET) { |
| 1175 | const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr; |
| 1176 | unsigned long int na = ntohl(addr4->sin_addr.s_addr); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1177 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1178 | if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */ |
| 1179 | (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */ |
| 1180 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1181 | } else { |
| 1182 | /* |
| 1183 | * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses |
| 1184 | * and shared addresses (100.64.0.0/10), are assigned global scope. |
| 1185 | */ |
| 1186 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1187 | } |
| 1188 | } else { |
| 1189 | /* |
| 1190 | * This should never happen. |
| 1191 | * Return a scope with low priority as a last resort. |
| 1192 | */ |
| 1193 | return IPV6_ADDR_SCOPE_NODELOCAL; |
| 1194 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* These macros are modelled after the ones in <netinet/in6.h>. */ |
| 1198 | |
| 1199 | /* RFC 4380, section 2.6 */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1200 | #define IN6_IS_ADDR_TEREDO(a) \ |
| 1201 | ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000))) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1202 | |
| 1203 | /* RFC 3056, section 2. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1204 | #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] | 1205 | |
| 1206 | /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1207 | #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] | 1208 | |
| 1209 | /* |
| 1210 | * Get the label for a given IPv4/IPv6 address. |
| 1211 | * RFC 6724, section 2.1. |
| 1212 | */ |
| 1213 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1214 | static int _get_label(const struct sockaddr* addr) { |
| 1215 | if (addr->sa_family == AF_INET) { |
| 1216 | return 4; |
| 1217 | } else if (addr->sa_family == AF_INET6) { |
| 1218 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1219 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1220 | return 0; |
| 1221 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1222 | return 4; |
| 1223 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1224 | return 2; |
| 1225 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1226 | return 5; |
| 1227 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1228 | return 13; |
| 1229 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) { |
| 1230 | return 3; |
| 1231 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1232 | return 11; |
| 1233 | } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1234 | return 12; |
| 1235 | } else { |
| 1236 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1237 | return 1; |
| 1238 | } |
| 1239 | } else { |
| 1240 | /* |
| 1241 | * This should never happen. |
| 1242 | * Return a semi-random label as a last resort. |
| 1243 | */ |
| 1244 | return 1; |
| 1245 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * Get the precedence for a given IPv4/IPv6 address. |
| 1250 | * RFC 6724, section 2.1. |
| 1251 | */ |
| 1252 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1253 | static int _get_precedence(const struct sockaddr* addr) { |
| 1254 | if (addr->sa_family == AF_INET) { |
| 1255 | return 35; |
| 1256 | } else if (addr->sa_family == AF_INET6) { |
| 1257 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1258 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1259 | return 50; |
| 1260 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1261 | return 35; |
| 1262 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1263 | return 30; |
| 1264 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1265 | return 5; |
| 1266 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1267 | return 3; |
| 1268 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) || |
| 1269 | IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) || |
| 1270 | IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1271 | return 1; |
| 1272 | } else { |
| 1273 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1274 | return 40; |
| 1275 | } |
| 1276 | } else { |
| 1277 | return 1; |
| 1278 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * Find number of matching initial bits between the two addresses a1 and a2. |
| 1283 | */ |
| 1284 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1285 | static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) { |
| 1286 | const char* p1 = (const char*) a1; |
| 1287 | const char* p2 = (const char*) a2; |
| 1288 | unsigned i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1289 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1290 | for (i = 0; i < sizeof(*a1); ++i) { |
| 1291 | int x, j; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1292 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1293 | if (p1[i] == p2[i]) { |
| 1294 | continue; |
| 1295 | } |
| 1296 | x = p1[i] ^ p2[i]; |
| 1297 | for (j = 0; j < CHAR_BIT; ++j) { |
| 1298 | if (x & (1 << (CHAR_BIT - 1))) { |
| 1299 | return i * CHAR_BIT + j; |
| 1300 | } |
| 1301 | x <<= 1; |
| 1302 | } |
| 1303 | } |
| 1304 | return sizeof(*a1) * CHAR_BIT; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Compare two source/destination address pairs. |
| 1309 | * RFC 6724, section 6. |
| 1310 | */ |
| 1311 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1312 | static int _rfc6724_compare(const void* ptr1, const void* ptr2) { |
| 1313 | const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1; |
| 1314 | const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2; |
| 1315 | int scope_src1, scope_dst1, scope_match1; |
| 1316 | int scope_src2, scope_dst2, scope_match2; |
| 1317 | int label_src1, label_dst1, label_match1; |
| 1318 | int label_src2, label_dst2, label_match2; |
| 1319 | int precedence1, precedence2; |
| 1320 | int prefixlen1, prefixlen2; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1321 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1322 | /* Rule 1: Avoid unusable destinations. */ |
| 1323 | if (a1->has_src_addr != a2->has_src_addr) { |
| 1324 | return a2->has_src_addr - a1->has_src_addr; |
| 1325 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1326 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1327 | /* Rule 2: Prefer matching scope. */ |
| 1328 | scope_src1 = _get_scope(&a1->src_addr.generic); |
| 1329 | scope_dst1 = _get_scope(a1->ai->ai_addr); |
| 1330 | scope_match1 = (scope_src1 == scope_dst1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1331 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1332 | scope_src2 = _get_scope(&a2->src_addr.generic); |
| 1333 | scope_dst2 = _get_scope(a2->ai->ai_addr); |
| 1334 | scope_match2 = (scope_src2 == scope_dst2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1335 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1336 | if (scope_match1 != scope_match2) { |
| 1337 | return scope_match2 - scope_match1; |
| 1338 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1339 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1340 | /* |
| 1341 | * Rule 3: Avoid deprecated addresses. |
| 1342 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1343 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1344 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1345 | /* |
| 1346 | * Rule 4: Prefer home addresses. |
| 1347 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1348 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1349 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1350 | /* Rule 5: Prefer matching label. */ |
| 1351 | label_src1 = _get_label(&a1->src_addr.generic); |
| 1352 | label_dst1 = _get_label(a1->ai->ai_addr); |
| 1353 | label_match1 = (label_src1 == label_dst1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1354 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1355 | label_src2 = _get_label(&a2->src_addr.generic); |
| 1356 | label_dst2 = _get_label(a2->ai->ai_addr); |
| 1357 | label_match2 = (label_src2 == label_dst2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1358 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1359 | if (label_match1 != label_match2) { |
| 1360 | return label_match2 - label_match1; |
| 1361 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1362 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1363 | /* Rule 6: Prefer higher precedence. */ |
| 1364 | precedence1 = _get_precedence(a1->ai->ai_addr); |
| 1365 | precedence2 = _get_precedence(a2->ai->ai_addr); |
| 1366 | if (precedence1 != precedence2) { |
| 1367 | return precedence2 - precedence1; |
| 1368 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1369 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1370 | /* |
| 1371 | * Rule 7: Prefer native transport. |
| 1372 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1373 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1374 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1375 | /* Rule 8: Prefer smaller scope. */ |
| 1376 | if (scope_dst1 != scope_dst2) { |
| 1377 | return scope_dst1 - scope_dst2; |
| 1378 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1379 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1380 | /* |
| 1381 | * Rule 9: Use longest matching prefix. |
| 1382 | * We implement this for IPv6 only, as the rules in RFC 6724 don't seem |
| 1383 | * to work very well directly applied to IPv4. (glibc uses information from |
| 1384 | * the routing table for a custom IPv4 implementation here.) |
| 1385 | */ |
| 1386 | if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr && |
| 1387 | a2->ai->ai_addr->sa_family == AF_INET6) { |
| 1388 | const struct sockaddr_in6* a1_src = &a1->src_addr.in6; |
| 1389 | const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr; |
| 1390 | const struct sockaddr_in6* a2_src = &a2->src_addr.in6; |
| 1391 | const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr; |
| 1392 | prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr); |
| 1393 | prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr); |
| 1394 | if (prefixlen1 != prefixlen2) { |
| 1395 | return prefixlen2 - prefixlen1; |
| 1396 | } |
| 1397 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1398 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1399 | /* |
| 1400 | * Rule 10: Leave the order unchanged. |
| 1401 | * We need this since qsort() is not necessarily stable. |
| 1402 | */ |
| 1403 | return a1->original_order - a2->original_order; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Find the source address that will be used if trying to connect to the given |
| 1408 | * address. src_addr must be large enough to hold a struct sockaddr_in6. |
| 1409 | * |
| 1410 | * Returns 1 if a source address was found, 0 if the address is unreachable, |
| 1411 | * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are |
| 1412 | * undefined. |
| 1413 | */ |
| 1414 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1415 | static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark, |
| 1416 | uid_t uid) { |
| 1417 | int sock; |
| 1418 | int ret; |
| 1419 | socklen_t len; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1420 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1421 | switch (addr->sa_family) { |
| 1422 | case AF_INET: |
| 1423 | len = sizeof(struct sockaddr_in); |
| 1424 | break; |
| 1425 | case AF_INET6: |
| 1426 | len = sizeof(struct sockaddr_in6); |
| 1427 | break; |
| 1428 | default: |
| 1429 | /* No known usable source address for non-INET families. */ |
| 1430 | return 0; |
| 1431 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1432 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1433 | sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); |
| 1434 | if (sock == -1) { |
| 1435 | if (errno == EAFNOSUPPORT) { |
| 1436 | return 0; |
| 1437 | } else { |
| 1438 | return -1; |
| 1439 | } |
| 1440 | } |
| 1441 | if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) { |
| 1442 | close(sock); |
| 1443 | return 0; |
| 1444 | } |
| 1445 | if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) { |
| 1446 | close(sock); |
| 1447 | return 0; |
| 1448 | } |
| 1449 | do { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 1450 | ret = connect(sock, addr, len); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1451 | } while (ret == -1 && errno == EINTR); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1452 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1453 | if (ret == -1) { |
| 1454 | close(sock); |
| 1455 | return 0; |
| 1456 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1457 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1458 | if (src_addr && getsockname(sock, src_addr, &len) == -1) { |
| 1459 | close(sock); |
| 1460 | return -1; |
| 1461 | } |
| 1462 | close(sock); |
| 1463 | return 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * Sort the linked list starting at sentinel->ai_next in RFC6724 order. |
| 1468 | * Will leave the list unchanged if an error occurs. |
| 1469 | */ |
| 1470 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1471 | static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) { |
| 1472 | struct addrinfo* cur; |
| 1473 | int nelem = 0, i; |
| 1474 | struct addrinfo_sort_elem* elems; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1475 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1476 | cur = list_sentinel->ai_next; |
| 1477 | while (cur) { |
| 1478 | ++nelem; |
| 1479 | cur = cur->ai_next; |
| 1480 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1481 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1482 | elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem)); |
| 1483 | if (elems == NULL) { |
| 1484 | goto error; |
| 1485 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1486 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1487 | /* |
| 1488 | * Convert the linked list to an array that also contains the candidate |
| 1489 | * source address for each destination address. |
| 1490 | */ |
| 1491 | for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) { |
| 1492 | int has_src_addr; |
| 1493 | assert(cur != NULL); |
| 1494 | elems[i].ai = cur; |
| 1495 | elems[i].original_order = i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1496 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1497 | has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid); |
| 1498 | if (has_src_addr == -1) { |
| 1499 | goto error; |
| 1500 | } |
| 1501 | elems[i].has_src_addr = has_src_addr; |
| 1502 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1503 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1504 | /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */ |
| 1505 | qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1506 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1507 | list_sentinel->ai_next = elems[0].ai; |
| 1508 | for (i = 0; i < nelem - 1; ++i) { |
| 1509 | elems[i].ai->ai_next = elems[i + 1].ai; |
| 1510 | } |
| 1511 | elems[nelem - 1].ai->ai_next = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1512 | |
| 1513 | error: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1514 | free(elems); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1515 | } |
| 1516 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1517 | static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) { |
| 1518 | struct addrinfo* ai; |
| 1519 | querybuf *buf, *buf2; |
| 1520 | const char* name; |
| 1521 | const struct addrinfo* pai; |
| 1522 | struct addrinfo sentinel, *cur; |
| 1523 | struct res_target q, q2; |
| 1524 | res_state res; |
| 1525 | const struct android_net_context* netcontext; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1526 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1527 | name = va_arg(ap, char*); |
| 1528 | pai = va_arg(ap, const struct addrinfo*); |
| 1529 | netcontext = va_arg(ap, const struct android_net_context*); |
| 1530 | // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1531 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1532 | memset(&q, 0, sizeof(q)); |
| 1533 | memset(&q2, 0, sizeof(q2)); |
| 1534 | memset(&sentinel, 0, sizeof(sentinel)); |
| 1535 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1536 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1537 | buf = malloc(sizeof(*buf)); |
| 1538 | if (buf == NULL) { |
| 1539 | h_errno = NETDB_INTERNAL; |
| 1540 | return NS_NOTFOUND; |
| 1541 | } |
| 1542 | buf2 = malloc(sizeof(*buf2)); |
| 1543 | if (buf2 == NULL) { |
| 1544 | free(buf); |
| 1545 | h_errno = NETDB_INTERNAL; |
| 1546 | return NS_NOTFOUND; |
| 1547 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1548 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1549 | switch (pai->ai_family) { |
| 1550 | case AF_UNSPEC: |
| 1551 | /* prefer IPv6 */ |
| 1552 | q.name = name; |
| 1553 | q.qclass = C_IN; |
| 1554 | q.answer = buf->buf; |
| 1555 | q.anslen = sizeof(buf->buf); |
| 1556 | int query_ipv6 = 1, query_ipv4 = 1; |
| 1557 | if (pai->ai_flags & AI_ADDRCONFIG) { |
| 1558 | query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid); |
| 1559 | query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid); |
| 1560 | } |
| 1561 | if (query_ipv6) { |
| 1562 | q.qtype = T_AAAA; |
| 1563 | if (query_ipv4) { |
| 1564 | q.next = &q2; |
| 1565 | q2.name = name; |
| 1566 | q2.qclass = C_IN; |
| 1567 | q2.qtype = T_A; |
| 1568 | q2.answer = buf2->buf; |
| 1569 | q2.anslen = sizeof(buf2->buf); |
| 1570 | } |
| 1571 | } else if (query_ipv4) { |
| 1572 | q.qtype = T_A; |
| 1573 | } else { |
| 1574 | free(buf); |
| 1575 | free(buf2); |
| 1576 | return NS_NOTFOUND; |
| 1577 | } |
| 1578 | break; |
| 1579 | case AF_INET: |
| 1580 | q.name = name; |
| 1581 | q.qclass = C_IN; |
| 1582 | q.qtype = T_A; |
| 1583 | q.answer = buf->buf; |
| 1584 | q.anslen = sizeof(buf->buf); |
| 1585 | break; |
| 1586 | case AF_INET6: |
| 1587 | q.name = name; |
| 1588 | q.qclass = C_IN; |
| 1589 | q.qtype = T_AAAA; |
| 1590 | q.answer = buf->buf; |
| 1591 | q.anslen = sizeof(buf->buf); |
| 1592 | break; |
| 1593 | default: |
| 1594 | free(buf); |
| 1595 | free(buf2); |
| 1596 | return NS_UNAVAIL; |
| 1597 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1598 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1599 | res = __res_get_state(); |
| 1600 | if (res == NULL) { |
| 1601 | free(buf); |
| 1602 | free(buf2); |
| 1603 | return NS_NOTFOUND; |
| 1604 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1605 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1606 | /* this just sets our netid val in the thread private data so we don't have to |
| 1607 | * modify the api's all the way down to res_send.c's res_nsend. We could |
| 1608 | * fully populate the thread private data here, but if we get down there |
| 1609 | * and have a cache hit that would be wasted, so we do the rest there on miss |
| 1610 | */ |
| 1611 | res_setnetcontext(res, netcontext); |
| 1612 | if (res_searchN(name, &q, res) < 0) { |
| 1613 | __res_put_state(res); |
| 1614 | free(buf); |
| 1615 | free(buf2); |
| 1616 | return NS_NOTFOUND; |
| 1617 | } |
| 1618 | ai = getanswer(buf, q.n, q.name, q.qtype, pai); |
| 1619 | if (ai) { |
| 1620 | cur->ai_next = ai; |
| 1621 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1622 | } |
| 1623 | if (q.next) { |
| 1624 | ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); |
| 1625 | if (ai) cur->ai_next = ai; |
| 1626 | } |
| 1627 | free(buf); |
| 1628 | free(buf2); |
| 1629 | if (sentinel.ai_next == NULL) { |
| 1630 | __res_put_state(res); |
| 1631 | switch (h_errno) { |
| 1632 | case HOST_NOT_FOUND: |
| 1633 | return NS_NOTFOUND; |
| 1634 | case TRY_AGAIN: |
| 1635 | return NS_TRYAGAIN; |
| 1636 | default: |
| 1637 | return NS_UNAVAIL; |
| 1638 | } |
| 1639 | } |
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 | _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid); |
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 | __res_put_state(res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1644 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1645 | *((struct addrinfo**) rv) = sentinel.ai_next; |
| 1646 | return NS_SUCCESS; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1647 | } |
| 1648 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1649 | static void _sethtent(FILE** hostf) { |
| 1650 | if (!*hostf) |
| 1651 | *hostf = fopen(_PATH_HOSTS, "re"); |
| 1652 | else |
| 1653 | rewind(*hostf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1654 | } |
| 1655 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1656 | static void _endhtent(FILE** hostf) { |
| 1657 | if (*hostf) { |
| 1658 | (void) fclose(*hostf); |
| 1659 | *hostf = NULL; |
| 1660 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1661 | } |
| 1662 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1663 | static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) { |
| 1664 | char* p; |
| 1665 | char *cp, *tname, *cname; |
| 1666 | struct addrinfo hints, *res0, *res; |
| 1667 | int error; |
| 1668 | const char* addr; |
| 1669 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1670 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1671 | // fprintf(stderr, "_gethtent() name = '%s'\n", name); |
| 1672 | assert(name != NULL); |
| 1673 | assert(pai != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1674 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1675 | if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL); |
| 1676 | again: |
| 1677 | if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL); |
| 1678 | if (*p == '#') goto again; |
| 1679 | if (!(cp = strpbrk(p, "#\n"))) goto again; |
| 1680 | *cp = '\0'; |
| 1681 | if (!(cp = strpbrk(p, " \t"))) goto again; |
| 1682 | *cp++ = '\0'; |
| 1683 | addr = p; |
| 1684 | /* if this is not something we're looking for, skip it. */ |
| 1685 | cname = NULL; |
| 1686 | while (cp && *cp) { |
| 1687 | if (*cp == ' ' || *cp == '\t') { |
| 1688 | cp++; |
| 1689 | continue; |
| 1690 | } |
| 1691 | if (!cname) cname = cp; |
| 1692 | tname = cp; |
| 1693 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
| 1694 | // fprintf(stderr, "\ttname = '%s'", tname); |
| 1695 | if (strcasecmp(name, tname) == 0) goto found; |
| 1696 | } |
| 1697 | goto again; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1698 | |
| 1699 | found: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1700 | hints = *pai; |
| 1701 | hints.ai_flags = AI_NUMERICHOST; |
| 1702 | error = getaddrinfo(addr, NULL, &hints, &res0); |
| 1703 | if (error) goto again; |
| 1704 | for (res = res0; res; res = res->ai_next) { |
| 1705 | /* cover it up */ |
| 1706 | res->ai_flags = pai->ai_flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1707 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1708 | if (pai->ai_flags & AI_CANONNAME) { |
| 1709 | if (get_canonname(pai, res, cname) != 0) { |
| 1710 | freeaddrinfo(res0); |
| 1711 | goto again; |
| 1712 | } |
| 1713 | } |
| 1714 | } |
| 1715 | return res0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1716 | } |
| 1717 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1718 | static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) { |
| 1719 | const char* name; |
| 1720 | const struct addrinfo* pai; |
| 1721 | struct addrinfo sentinel, *cur; |
| 1722 | struct addrinfo* p; |
| 1723 | FILE* hostf = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1724 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1725 | name = va_arg(ap, char*); |
| 1726 | pai = va_arg(ap, struct addrinfo*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1727 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1728 | // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name); |
| 1729 | memset(&sentinel, 0, sizeof(sentinel)); |
| 1730 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1731 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1732 | _sethtent(&hostf); |
| 1733 | while ((p = _gethtent(&hostf, name, pai)) != NULL) { |
| 1734 | cur->ai_next = p; |
| 1735 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1736 | } |
| 1737 | _endhtent(&hostf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1738 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1739 | *((struct addrinfo**) rv) = sentinel.ai_next; |
| 1740 | if (sentinel.ai_next == NULL) return NS_NOTFOUND; |
| 1741 | return NS_SUCCESS; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | /* resolver logic */ |
| 1745 | |
| 1746 | /* |
| 1747 | * Formulate a normal query, send, and await answer. |
| 1748 | * Returned answer is placed in supplied buffer "answer". |
| 1749 | * Perform preliminary check of answer, returning success only |
| 1750 | * if no error is indicated and the answer count is nonzero. |
| 1751 | * Return the size of the response on success, -1 on error. |
| 1752 | * Error number is left in h_errno. |
| 1753 | * |
| 1754 | * Caller must parse answer and determine whether it answers the question. |
| 1755 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1756 | static int res_queryN(const char* name, /* domain name */ struct res_target* target, |
| 1757 | res_state res) { |
| 1758 | u_char buf[MAXPACKET]; |
| 1759 | HEADER* hp; |
| 1760 | int n; |
| 1761 | struct res_target* t; |
| 1762 | int rcode; |
| 1763 | int ancount; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1764 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1765 | assert(name != NULL); |
| 1766 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1767 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1768 | rcode = NOERROR; |
| 1769 | ancount = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1770 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1771 | for (t = target; t; t = t->next) { |
| 1772 | int class, type; |
| 1773 | u_char* answer; |
| 1774 | int anslen; |
| 1775 | u_int oflags; |
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 | hp = (HEADER*) (void*) t->answer; |
| 1778 | oflags = res->_flags; |
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 | again: |
| 1781 | hp->rcode = NOERROR; /* default */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1782 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1783 | /* make it easier... */ |
| 1784 | class = t->qclass; |
| 1785 | type = t->qtype; |
| 1786 | answer = t->answer; |
| 1787 | anslen = t->anslen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1788 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1789 | if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1790 | #endif |
| 1791 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1792 | n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1793 | if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 && |
| 1794 | (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0) |
| 1795 | n = res_nopt(res, n, buf, sizeof(buf), anslen); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1796 | if (n <= 0) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1797 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1798 | if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1799 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1800 | h_errno = NO_RECOVERY; |
| 1801 | return n; |
| 1802 | } |
| 1803 | n = res_nsend(res, buf, n, answer, anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1804 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1805 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
| 1806 | rcode = hp->rcode; /* record most recent error */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1807 | /* if the query choked with EDNS0, retry without EDNS0 */ |
| 1808 | if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && |
| 1809 | ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) { |
| 1810 | res->_flags |= RES_F_EDNS0ERR; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1811 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1812 | if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1813 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1814 | goto again; |
| 1815 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1816 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1817 | if (res->options & RES_DEBUG) |
| 1818 | printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1819 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1820 | continue; |
| 1821 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1822 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1823 | ancount += ntohs(hp->ancount); |
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 | t->n = n; |
| 1826 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1827 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1828 | if (ancount == 0) { |
| 1829 | switch (rcode) { |
| 1830 | case NXDOMAIN: |
| 1831 | h_errno = HOST_NOT_FOUND; |
| 1832 | break; |
| 1833 | case SERVFAIL: |
| 1834 | h_errno = TRY_AGAIN; |
| 1835 | break; |
| 1836 | case NOERROR: |
| 1837 | h_errno = NO_DATA; |
| 1838 | break; |
| 1839 | case FORMERR: |
| 1840 | case NOTIMP: |
| 1841 | case REFUSED: |
| 1842 | default: |
| 1843 | h_errno = NO_RECOVERY; |
| 1844 | break; |
| 1845 | } |
| 1846 | return -1; |
| 1847 | } |
| 1848 | return ancount; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | /* |
| 1852 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 1853 | * Return the size of the response on success, -1 on error. |
| 1854 | * If enabled, implement search rules until answer or unrecoverable failure |
| 1855 | * is detected. Error code, if any, is left in h_errno. |
| 1856 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1857 | static int res_searchN(const char* name, struct res_target* target, res_state res) { |
| 1858 | const char *cp, *const *domain; |
| 1859 | HEADER* hp; |
| 1860 | u_int dots; |
| 1861 | int trailing_dot, ret, saved_herrno; |
| 1862 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; |
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 | assert(name != NULL); |
| 1865 | assert(target != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1866 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1867 | hp = (HEADER*) (void*) target->answer; /*XXX*/ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1868 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1869 | errno = 0; |
| 1870 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ |
| 1871 | dots = 0; |
| 1872 | for (cp = name; *cp; cp++) dots += (*cp == '.'); |
| 1873 | trailing_dot = 0; |
| 1874 | if (cp > name && *--cp == '.') trailing_dot++; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1875 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1876 | // fprintf(stderr, "res_searchN() name = '%s'\n", name); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1877 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1878 | /* |
| 1879 | * if there aren't any dots, it could be a user-level alias |
| 1880 | */ |
| 1881 | if (!dots && (cp = __hostalias(name)) != NULL) { |
| 1882 | ret = res_queryN(cp, target, res); |
| 1883 | return ret; |
| 1884 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1885 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1886 | /* |
| 1887 | * If there are dots in the name already, let's just give it a try |
| 1888 | * 'as is'. The threshold can be set with the "ndots" option. |
| 1889 | */ |
| 1890 | saved_herrno = -1; |
| 1891 | if (dots >= res->ndots) { |
| 1892 | ret = res_querydomainN(name, NULL, target, res); |
| 1893 | if (ret > 0) return (ret); |
| 1894 | saved_herrno = h_errno; |
| 1895 | tried_as_is++; |
| 1896 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1897 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1898 | /* |
| 1899 | * We do at least one level of search if |
| 1900 | * - there is no dot and RES_DEFNAME is set, or |
| 1901 | * - there is at least one dot, there is no trailing dot, |
| 1902 | * and RES_DNSRCH is set. |
| 1903 | */ |
| 1904 | if ((!dots && (res->options & RES_DEFNAMES)) || |
| 1905 | (dots && !trailing_dot && (res->options & RES_DNSRCH))) { |
| 1906 | int done = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1907 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1908 | /* Unfortunately we need to set stuff up before |
| 1909 | * the domain stuff is tried. Will have a better |
| 1910 | * fix after thread pools are used. |
| 1911 | */ |
| 1912 | _resolv_populate_res_for_net(res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1913 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1914 | for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) { |
| 1915 | ret = res_querydomainN(name, *domain, target, res); |
| 1916 | if (ret > 0) return ret; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1917 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1918 | /* |
| 1919 | * If no server present, give up. |
| 1920 | * If name isn't found in this domain, |
| 1921 | * keep trying higher domains in the search list |
| 1922 | * (if that's enabled). |
| 1923 | * On a NO_DATA error, keep trying, otherwise |
| 1924 | * a wildcard entry of another type could keep us |
| 1925 | * from finding this entry higher in the domain. |
| 1926 | * If we get some other error (negative answer or |
| 1927 | * server failure), then stop searching up, |
| 1928 | * but try the input name below in case it's |
| 1929 | * fully-qualified. |
| 1930 | */ |
| 1931 | if (errno == ECONNREFUSED) { |
| 1932 | h_errno = TRY_AGAIN; |
| 1933 | return -1; |
| 1934 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1935 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1936 | switch (h_errno) { |
| 1937 | case NO_DATA: |
| 1938 | got_nodata++; |
| 1939 | /* FALLTHROUGH */ |
| 1940 | case HOST_NOT_FOUND: |
| 1941 | /* keep trying */ |
| 1942 | break; |
| 1943 | case TRY_AGAIN: |
| 1944 | if (hp->rcode == SERVFAIL) { |
| 1945 | /* try next search element, if any */ |
| 1946 | got_servfail++; |
| 1947 | break; |
| 1948 | } |
| 1949 | /* FALLTHROUGH */ |
| 1950 | default: |
| 1951 | /* anything else implies that we're done */ |
| 1952 | done++; |
| 1953 | } |
| 1954 | /* |
| 1955 | * if we got here for some reason other than DNSRCH, |
| 1956 | * we only wanted one iteration of the loop, so stop. |
| 1957 | */ |
| 1958 | if (!(res->options & RES_DNSRCH)) done++; |
| 1959 | } |
| 1960 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1961 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1962 | /* |
| 1963 | * if we have not already tried the name "as is", do that now. |
| 1964 | * note that we do this regardless of how many dots were in the |
| 1965 | * name or whether it ends with a dot. |
| 1966 | */ |
| 1967 | if (!tried_as_is) { |
| 1968 | ret = res_querydomainN(name, NULL, target, res); |
| 1969 | if (ret > 0) return ret; |
| 1970 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1971 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1972 | /* |
| 1973 | * if we got here, we didn't satisfy the search. |
| 1974 | * if we did an initial full query, return that query's h_errno |
| 1975 | * (note that we wouldn't be here if that query had succeeded). |
| 1976 | * else if we ever got a nodata, send that back as the reason. |
| 1977 | * else send back meaningless h_errno, that being the one from |
| 1978 | * the last DNSRCH we did. |
| 1979 | */ |
| 1980 | if (saved_herrno != -1) |
| 1981 | h_errno = saved_herrno; |
| 1982 | else if (got_nodata) |
| 1983 | h_errno = NO_DATA; |
| 1984 | else if (got_servfail) |
| 1985 | h_errno = TRY_AGAIN; |
| 1986 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | /* |
| 1990 | * Perform a call on res_query on the concatenation of name and domain, |
| 1991 | * removing a trailing dot from name if domain is NULL. |
| 1992 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1993 | static int res_querydomainN(const char* name, const char* domain, struct res_target* target, |
| 1994 | res_state res) { |
| 1995 | char nbuf[MAXDNAME]; |
| 1996 | const char* longname = nbuf; |
| 1997 | size_t n, d; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1998 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1999 | assert(name != NULL); |
| 2000 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2001 | |
| 2002 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2003 | if (res->options & RES_DEBUG) |
| 2004 | printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2005 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 2006 | if (domain == NULL) { |
| 2007 | /* |
| 2008 | * Check for trailing '.'; |
| 2009 | * copy without '.' if present. |
| 2010 | */ |
| 2011 | n = strlen(name); |
| 2012 | if (n + 1 > sizeof(nbuf)) { |
| 2013 | h_errno = NO_RECOVERY; |
| 2014 | return -1; |
| 2015 | } |
| 2016 | if (n > 0 && name[--n] == '.') { |
| 2017 | strncpy(nbuf, name, n); |
| 2018 | nbuf[n] = '\0'; |
| 2019 | } else |
| 2020 | longname = name; |
| 2021 | } else { |
| 2022 | n = strlen(name); |
| 2023 | d = strlen(domain); |
| 2024 | if (n + 1 + d + 1 > sizeof(nbuf)) { |
| 2025 | h_errno = NO_RECOVERY; |
| 2026 | return -1; |
| 2027 | } |
| 2028 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 2029 | } |
| 2030 | return res_queryN(longname, target, res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 2031 | } |