Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: res_send.c,v 1.9 2006/01/24 17:41:25 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1985, 1989, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | * This product includes software developed by the University of |
| 18 | * California, Berkeley and its contributors. |
| 19 | * 4. Neither the name of the University nor the names of its contributors |
| 20 | * may be used to endorse or promote products derived from this software |
| 21 | * without specific prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
| 38 | * |
| 39 | * Permission to use, copy, modify, and distribute this software for any |
| 40 | * purpose with or without fee is hereby granted, provided that the above |
| 41 | * copyright notice and this permission notice appear in all copies, and that |
| 42 | * the name of Digital Equipment Corporation not be used in advertising or |
| 43 | * publicity pertaining to distribution of the document or software without |
| 44 | * specific, written prior permission. |
| 45 | * |
| 46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
| 47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
| 48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
| 49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 53 | * SOFTWARE. |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
| 58 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
| 59 | * |
| 60 | * Permission to use, copy, modify, and distribute this software for any |
| 61 | * purpose with or without fee is hereby granted, provided that the above |
| 62 | * copyright notice and this permission notice appear in all copies. |
| 63 | * |
| 64 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
| 65 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 66 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
| 67 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 68 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 69 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 70 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 71 | */ |
| 72 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 73 | /* |
| 74 | * Send query to name server and wait for reply. |
| 75 | */ |
| 76 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 77 | constexpr bool kVerboseLogging = false; |
| 78 | #define LOG_TAG "res_send" |
| 79 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 80 | #include <sys/param.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 81 | #include <sys/socket.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 82 | #include <sys/time.h> |
| 83 | #include <sys/types.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 84 | #include <sys/uio.h> |
| 85 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | #include <arpa/inet.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 87 | #include <arpa/nameser.h> |
| 88 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 89 | |
| 90 | #include <errno.h> |
| 91 | #include <fcntl.h> |
| 92 | #include <netdb.h> |
| 93 | #include <poll.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 94 | #include <signal.h> |
| 95 | #include <stdio.h> |
| 96 | #include <stdlib.h> |
| 97 | #include <string.h> |
| 98 | #include <time.h> |
| 99 | #include <unistd.h> |
| 100 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 101 | #include <android-base/logging.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 102 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 103 | #include "netd_resolv/resolv.h" |
| 104 | #include "netd_resolv/stats.h" |
Bernie Innocenti | 2fd418e | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 105 | #include "private/android_filesystem_config.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 106 | #include "res_state_ext.h" |
Bernie Innocenti | 2fd418e | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 107 | #include "resolv_cache.h" |
Bernie Innocenti | 2fd418e | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 108 | #include "resolv_private.h" |
Bernie Innocenti | 2fd418e | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 109 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 110 | |
| 111 | #define VLOG if (!kVerboseLogging) {} else LOG(INFO) |
| 112 | |
| 113 | #ifndef RESOLV_ALLOW_VERBOSE_LOGGING |
| 114 | static_assert(kVerboseLogging == false, |
| 115 | "Verbose logging floods logs at high-rate and exposes privacy-sensitive information. " |
| 116 | "Do not enable in release builds."); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 117 | #endif |
Bernie Innocenti | 2fd418e | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 118 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 119 | #ifndef DEBUG |
| 120 | #define Dprint(cond, args) /*empty*/ |
| 121 | #define DprintQ(cond, args, query, size) /*empty*/ |
| 122 | #else |
| 123 | // TODO: convert to Android logging |
| 124 | #define Dprint(cond, args) \ |
| 125 | if (cond) { \ |
| 126 | fprintf args; \ |
| 127 | } else { \ |
| 128 | } |
| 129 | #define DprintQ(cond, args, query, size) \ |
| 130 | if (cond) { \ |
| 131 | fprintf args; \ |
| 132 | res_pquery(statp, query, size, stdout); \ |
| 133 | } else { \ |
| 134 | } |
| 135 | #endif // DEBUG |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 136 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 137 | static int get_salen(const struct sockaddr*); |
| 138 | static struct sockaddr* get_nsaddr(res_state, size_t); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 139 | static int send_vc(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*, |
| 140 | int, time_t*, int*, int*); |
| 141 | static int send_dg(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*, |
| 142 | int, int*, int*, time_t*, int*, int*); |
| 143 | static void Aerror(const res_state, FILE*, const char*, int, const struct sockaddr*, int); |
| 144 | static void Perror(const res_state, FILE*, const char*, int); |
| 145 | static int sock_eq(struct sockaddr*, struct sockaddr*); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 146 | static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen, |
| 147 | const struct timespec timeout); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 148 | static int retrying_poll(const int sock, short events, const struct timespec* finish); |
| 149 | |
| 150 | /* BIONIC-BEGIN: implement source port randomization */ |
| 151 | typedef union { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 152 | struct sockaddr sa; |
| 153 | struct sockaddr_in sin; |
| 154 | struct sockaddr_in6 sin6; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 155 | } _sockaddr_union; |
| 156 | |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 157 | // BEGIN: Code copied from ISC eventlib |
| 158 | // TODO: move away from this code |
| 159 | |
| 160 | #define BILLION 1000000000 |
| 161 | |
| 162 | static struct timespec evConsTime(time_t sec, long nsec) { |
| 163 | struct timespec x; |
| 164 | |
| 165 | x.tv_sec = sec; |
| 166 | x.tv_nsec = nsec; |
| 167 | return (x); |
| 168 | } |
| 169 | |
| 170 | static struct timespec evAddTime(struct timespec addend1, struct timespec addend2) { |
| 171 | struct timespec x; |
| 172 | |
| 173 | x.tv_sec = addend1.tv_sec + addend2.tv_sec; |
| 174 | x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec; |
| 175 | if (x.tv_nsec >= BILLION) { |
| 176 | x.tv_sec++; |
| 177 | x.tv_nsec -= BILLION; |
| 178 | } |
| 179 | return (x); |
| 180 | } |
| 181 | |
| 182 | static struct timespec evSubTime(struct timespec minuend, struct timespec subtrahend) { |
| 183 | struct timespec x; |
| 184 | |
| 185 | x.tv_sec = minuend.tv_sec - subtrahend.tv_sec; |
| 186 | if (minuend.tv_nsec >= subtrahend.tv_nsec) |
| 187 | x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec; |
| 188 | else { |
| 189 | x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec; |
| 190 | x.tv_sec--; |
| 191 | } |
| 192 | return (x); |
| 193 | } |
| 194 | |
| 195 | static int evCmpTime(struct timespec a, struct timespec b) { |
| 196 | #define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0)); |
| 197 | time_t s = a.tv_sec - b.tv_sec; |
| 198 | long n; |
| 199 | |
| 200 | if (s != 0) return SGN(s); |
| 201 | |
| 202 | n = a.tv_nsec - b.tv_nsec; |
| 203 | return SGN(n); |
| 204 | } |
| 205 | |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 206 | static struct timespec evNowTime(void) { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 207 | struct timespec tsnow; |
Bernie Innocenti | 357339c | 2018-08-31 16:11:41 +0900 | [diff] [blame] | 208 | clock_gettime(CLOCK_REALTIME, &tsnow); |
| 209 | return tsnow; |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static struct iovec evConsIovec(void* buf, size_t cnt) { |
| 213 | struct iovec ret; |
| 214 | |
| 215 | memset(&ret, 0xf5, sizeof ret); |
| 216 | ret.iov_base = buf; |
| 217 | ret.iov_len = cnt; |
Bernie Innocenti | 357339c | 2018-08-31 16:11:41 +0900 | [diff] [blame] | 218 | return ret; |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // END: Code copied from ISC eventlib |
| 222 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 223 | static int random_bind(int s, int family) { |
| 224 | _sockaddr_union u; |
| 225 | int j; |
| 226 | socklen_t slen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 227 | |
| 228 | /* clear all, this also sets the IP4/6 address to 'any' */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 229 | memset(&u, 0, sizeof u); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 230 | |
| 231 | switch (family) { |
| 232 | case AF_INET: |
| 233 | u.sin.sin_family = family; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 234 | slen = sizeof u.sin; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 235 | break; |
| 236 | case AF_INET6: |
| 237 | u.sin6.sin6_family = family; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 238 | slen = sizeof u.sin6; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | errno = EPROTO; |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | /* first try to bind to a random source port a few times */ |
| 246 | for (j = 0; j < 10; j++) { |
| 247 | /* find a random port between 1025 .. 65534 */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 248 | int port = 1025 + (res_randomid() % (65535 - 1025)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 249 | if (family == AF_INET) |
| 250 | u.sin.sin_port = htons(port); |
| 251 | else |
| 252 | u.sin6.sin6_port = htons(port); |
| 253 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 254 | if (!bind(s, &u.sa, slen)) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 255 | } |
| 256 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 257 | // nothing after 10 attempts, our network table is probably busy |
| 258 | // let the system decide which port is best |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 259 | if (family == AF_INET) |
| 260 | u.sin.sin_port = 0; |
| 261 | else |
| 262 | u.sin6.sin6_port = 0; |
| 263 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 264 | return bind(s, &u.sa, slen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 265 | } |
| 266 | /* BIONIC-END */ |
| 267 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 268 | /* int |
| 269 | * res_isourserver(ina) |
| 270 | * looks up "ina" in _res.ns_addr_list[] |
| 271 | * returns: |
| 272 | * 0 : not found |
| 273 | * >0 : found |
| 274 | * author: |
| 275 | * paul vixie, 29may94 |
| 276 | */ |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 277 | static int res_ourserver_p(const res_state statp, const sockaddr* sa) { |
| 278 | const sockaddr_in *inp, *srv; |
| 279 | const sockaddr_in6 *in6p, *srv6; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 280 | int ns; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 281 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 282 | switch (sa->sa_family) { |
| 283 | case AF_INET: |
| 284 | inp = (const struct sockaddr_in*) (const void*) sa; |
| 285 | for (ns = 0; ns < statp->nscount; ns++) { |
| 286 | srv = (struct sockaddr_in*) (void*) get_nsaddr(statp, (size_t) ns); |
| 287 | if (srv->sin_family == inp->sin_family && srv->sin_port == inp->sin_port && |
| 288 | (srv->sin_addr.s_addr == INADDR_ANY || |
| 289 | srv->sin_addr.s_addr == inp->sin_addr.s_addr)) |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 290 | return 1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 291 | } |
| 292 | break; |
| 293 | case AF_INET6: |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 294 | if (statp->_u._ext.ext == NULL) break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 295 | in6p = (const struct sockaddr_in6*) (const void*) sa; |
| 296 | for (ns = 0; ns < statp->nscount; ns++) { |
| 297 | srv6 = (struct sockaddr_in6*) (void*) get_nsaddr(statp, (size_t) ns); |
| 298 | if (srv6->sin6_family == in6p->sin6_family && srv6->sin6_port == in6p->sin6_port && |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 299 | #ifdef HAVE_SIN6_SCOPE_ID |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 300 | (srv6->sin6_scope_id == 0 || srv6->sin6_scope_id == in6p->sin6_scope_id) && |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 301 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 302 | (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) || |
| 303 | IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr))) |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 304 | return 1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 305 | } |
| 306 | break; |
| 307 | default: |
| 308 | break; |
| 309 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 310 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* int |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 314 | * res_nameinquery(name, type, cl, buf, eom) |
| 315 | * look for (name, type, cl) in the query section of packet (buf, eom) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 316 | * requires: |
| 317 | * buf + HFIXEDSZ <= eom |
| 318 | * returns: |
| 319 | * -1 : format error |
| 320 | * 0 : not found |
| 321 | * >0 : found |
| 322 | * author: |
| 323 | * paul vixie, 29may94 |
| 324 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 325 | int res_nameinquery(const char* name, int type, int cl, const u_char* buf, const u_char* eom) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 326 | const u_char* cp = buf + HFIXEDSZ; |
| 327 | int qdcount = ntohs(((const HEADER*) (const void*) buf)->qdcount); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 328 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 329 | while (qdcount-- > 0) { |
| 330 | char tname[MAXDNAME + 1]; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 331 | int n = dn_expand(buf, eom, cp, tname, sizeof tname); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 332 | if (n < 0) return (-1); |
| 333 | cp += n; |
| 334 | if (cp + 2 * INT16SZ > eom) return (-1); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 335 | int ttype = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 336 | cp += INT16SZ; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 337 | int tclass = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 338 | cp += INT16SZ; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 339 | if (ttype == type && tclass == cl && ns_samename(tname, name) == 1) return (1); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 340 | } |
| 341 | return (0); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* int |
| 345 | * res_queriesmatch(buf1, eom1, buf2, eom2) |
| 346 | * is there a 1:1 mapping of (name,type,class) |
| 347 | * in (buf1,eom1) and (buf2,eom2)? |
| 348 | * returns: |
| 349 | * -1 : format error |
| 350 | * 0 : not a 1:1 mapping |
| 351 | * >0 : is a 1:1 mapping |
| 352 | * author: |
| 353 | * paul vixie, 29may94 |
| 354 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 355 | int res_queriesmatch(const u_char* buf1, const u_char* eom1, const u_char* buf2, |
| 356 | const u_char* eom2) { |
| 357 | const u_char* cp = buf1 + HFIXEDSZ; |
| 358 | int qdcount = ntohs(((const HEADER*) (const void*) buf1)->qdcount); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 359 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 360 | if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) return (-1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 361 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 362 | /* |
| 363 | * Only header section present in replies to |
| 364 | * dynamic update packets. |
| 365 | */ |
| 366 | if ((((const HEADER*) (const void*) buf1)->opcode == ns_o_update) && |
| 367 | (((const HEADER*) (const void*) buf2)->opcode == ns_o_update)) |
| 368 | return (1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 369 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 370 | if (qdcount != ntohs(((const HEADER*) (const void*) buf2)->qdcount)) return (0); |
| 371 | while (qdcount-- > 0) { |
| 372 | char tname[MAXDNAME + 1]; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 373 | int n = dn_expand(buf1, eom1, cp, tname, sizeof tname); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 374 | if (n < 0) return (-1); |
| 375 | cp += n; |
| 376 | if (cp + 2 * INT16SZ > eom1) return (-1); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 377 | int ttype = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 378 | cp += INT16SZ; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 379 | int tclass = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 380 | cp += INT16SZ; |
| 381 | if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) return (0); |
| 382 | } |
| 383 | return (1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 384 | } |
| 385 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 386 | int res_nsend(res_state statp, const u_char* buf, int buflen, u_char* ans, int anssiz) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 387 | int gotsomewhere, terrno, v_circuit, resplen, n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 388 | ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 389 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 390 | if (anssiz < HFIXEDSZ) { |
| 391 | errno = EINVAL; |
| 392 | return (-1); |
| 393 | } |
| 394 | DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY), |
| 395 | (stdout, ";; res_send()\n"), buf, buflen); |
| 396 | v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ; |
| 397 | gotsomewhere = 0; |
| 398 | terrno = ETIMEDOUT; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 399 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 400 | int anslen = 0; |
| 401 | cache_status = _resolv_cache_lookup(statp->netid, buf, buflen, ans, anssiz, &anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 402 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 403 | if (cache_status == RESOLV_CACHE_FOUND) { |
| 404 | return anslen; |
| 405 | } else if (cache_status != RESOLV_CACHE_UNSUPPORTED) { |
| 406 | // had a cache miss for a known network, so populate the thread private |
| 407 | // data so the normal resolve path can do its thing |
| 408 | _resolv_populate_res_for_net(statp); |
| 409 | } |
| 410 | if (statp->nscount == 0) { |
| 411 | // We have no nameservers configured, so there's no point trying. |
| 412 | // Tell the cache the query failed, or any retries and anyone else asking the same |
| 413 | // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast. |
| 414 | _resolv_cache_query_failed(statp->netid, buf, buflen); |
| 415 | errno = ESRCH; |
| 416 | return (-1); |
| 417 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 418 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 419 | /* |
| 420 | * If the ns_addr_list in the resolver context has changed, then |
| 421 | * invalidate our cached copy and the associated timing data. |
| 422 | */ |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 423 | if (statp->_u._ext.nscount != 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 424 | int needclose = 0; |
| 425 | struct sockaddr_storage peer; |
| 426 | socklen_t peerlen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 427 | |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 428 | if (statp->_u._ext.nscount != statp->nscount) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 429 | needclose++; |
| 430 | } else { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 431 | for (int ns = 0; ns < statp->nscount; ns++) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 432 | if (statp->nsaddr_list[ns].sin_family && |
| 433 | !sock_eq((struct sockaddr*) (void*) &statp->nsaddr_list[ns], |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 434 | (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[ns])) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 435 | needclose++; |
| 436 | break; |
| 437 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 438 | |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 439 | if (statp->_u._ext.nssocks[ns] == -1) continue; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 440 | peerlen = sizeof(peer); |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 441 | if (getpeername(statp->_u._ext.nssocks[ns], (struct sockaddr*) (void*) &peer, |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 442 | &peerlen) < 0) { |
| 443 | needclose++; |
| 444 | break; |
| 445 | } |
| 446 | if (!sock_eq((struct sockaddr*) (void*) &peer, get_nsaddr(statp, (size_t) ns))) { |
| 447 | needclose++; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | if (needclose) { |
| 453 | res_nclose(statp); |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 454 | statp->_u._ext.nscount = 0; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 455 | } |
| 456 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 457 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 458 | /* |
| 459 | * Maybe initialize our private copy of the ns_addr_list. |
| 460 | */ |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 461 | if (statp->_u._ext.nscount == 0) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 462 | for (int ns = 0; ns < statp->nscount; ns++) { |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 463 | statp->_u._ext.nstimes[ns] = RES_MAXTIME; |
| 464 | statp->_u._ext.nssocks[ns] = -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 465 | if (!statp->nsaddr_list[ns].sin_family) continue; |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 466 | statp->_u._ext.ext->nsaddrs[ns].sin = statp->nsaddr_list[ns]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 467 | } |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 468 | statp->_u._ext.nscount = statp->nscount; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 469 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 470 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 471 | /* |
| 472 | * Some resolvers want to even out the load on their nameservers. |
| 473 | * Note that RES_BLAST overrides RES_ROTATE. |
| 474 | */ |
| 475 | if ((statp->options & RES_ROTATE) != 0U && (statp->options & RES_BLAST) == 0U) { |
| 476 | union res_sockaddr_union inu; |
| 477 | struct sockaddr_in ina; |
| 478 | int lastns = statp->nscount - 1; |
| 479 | int fd; |
| 480 | u_int16_t nstime; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 481 | |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 482 | if (statp->_u._ext.ext != NULL) inu = statp->_u._ext.ext->nsaddrs[0]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 483 | ina = statp->nsaddr_list[0]; |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 484 | fd = statp->_u._ext.nssocks[0]; |
| 485 | nstime = statp->_u._ext.nstimes[0]; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 486 | for (int ns = 0; ns < lastns; ns++) { |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 487 | if (statp->_u._ext.ext != NULL) |
| 488 | statp->_u._ext.ext->nsaddrs[ns] = statp->_u._ext.ext->nsaddrs[ns + 1]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 489 | statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1]; |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 490 | statp->_u._ext.nssocks[ns] = statp->_u._ext.nssocks[ns + 1]; |
| 491 | statp->_u._ext.nstimes[ns] = statp->_u._ext.nstimes[ns + 1]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 492 | } |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 493 | if (statp->_u._ext.ext != NULL) statp->_u._ext.ext->nsaddrs[lastns] = inu; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 494 | statp->nsaddr_list[lastns] = ina; |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 495 | statp->_u._ext.nssocks[lastns] = fd; |
| 496 | statp->_u._ext.nstimes[lastns] = nstime; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 497 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 498 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 499 | /* |
| 500 | * Send request, RETRY times, or until successful. |
| 501 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 502 | for (int attempt = 0; attempt < statp->retry; ++attempt) { |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 503 | struct res_stats stats[MAXNS]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 504 | struct __res_params params; |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 505 | int revision_id = resolv_cache_get_resolver_stats(statp->netid, ¶ms, stats); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 506 | bool usable_servers[MAXNS]; |
| 507 | android_net_res_stats_get_usable_servers(¶ms, stats, statp->nscount, usable_servers); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 508 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 509 | for (int ns = 0; ns < statp->nscount; ns++) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 510 | if (!usable_servers[ns]) continue; |
| 511 | struct sockaddr* nsap; |
| 512 | int nsaplen; |
| 513 | time_t now = 0; |
| 514 | int rcode = RCODE_INTERNAL_ERROR; |
| 515 | int delay = 0; |
| 516 | nsap = get_nsaddr(statp, (size_t) ns); |
| 517 | nsaplen = get_salen(nsap); |
| 518 | statp->_flags &= ~RES_F_LASTMASK; |
| 519 | statp->_flags |= (ns << RES_F_LASTSHIFT); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 520 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 521 | same_ns: |
| 522 | if (statp->qhook) { |
| 523 | int done = 0, loops = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 524 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 525 | do { |
| 526 | res_sendhookact act; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 527 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 528 | act = (*statp->qhook)(&nsap, &buf, &buflen, ans, anssiz, &resplen); |
| 529 | switch (act) { |
| 530 | case res_goahead: |
| 531 | done = 1; |
| 532 | break; |
| 533 | case res_nextns: |
| 534 | res_nclose(statp); |
| 535 | goto next_ns; |
| 536 | case res_done: |
| 537 | if (cache_status == RESOLV_CACHE_NOTFOUND) { |
| 538 | _resolv_cache_add(statp->netid, buf, buflen, ans, resplen); |
| 539 | } |
| 540 | return (resplen); |
| 541 | case res_modified: |
| 542 | /* give the hook another try */ |
| 543 | if (++loops < 42) /*doug adams*/ |
| 544 | break; |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 545 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 546 | case res_error: |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 547 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 548 | default: |
| 549 | goto fail; |
| 550 | } |
| 551 | } while (!done); |
| 552 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 553 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 554 | [[maybe_unused]] static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; |
| 555 | [[maybe_unused]] char abuf[NI_MAXHOST]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 556 | Dprint(((statp->options & RES_DEBUG) && |
| 557 | getnameinfo(nsap, (socklen_t) nsaplen, abuf, sizeof(abuf), NULL, 0, niflags) == |
| 558 | 0), |
| 559 | (stdout, ";; Querying server (# %d) address = %s\n", ns + 1, abuf)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 560 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 561 | if (v_circuit) { |
| 562 | /* Use VC; at most one attempt per server. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 563 | attempt = statp->retry; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 564 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 565 | n = send_vc(statp, ¶ms, buf, buflen, ans, anssiz, &terrno, ns, &now, &rcode, |
| 566 | &delay); |
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 | /* |
| 569 | * Only record stats the first time we try a query. This ensures that |
| 570 | * queries that deterministically fail (e.g., a name that always returns |
| 571 | * SERVFAIL or times out) do not unduly affect the stats. |
| 572 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 573 | if (attempt == 0) { |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 574 | res_sample sample; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 575 | _res_stats_set_sample(&sample, now, rcode, delay); |
| 576 | _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample, |
| 577 | params.max_samples); |
| 578 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 579 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 580 | VLOG << "used send_vc " << n; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 581 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 582 | if (n < 0) goto fail; |
| 583 | if (n == 0) goto next_ns; |
| 584 | resplen = n; |
| 585 | } else { |
| 586 | /* Use datagrams. */ |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 587 | VLOG << "using send_dg"; |
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 | n = send_dg(statp, ¶ms, buf, buflen, ans, anssiz, &terrno, ns, &v_circuit, |
| 590 | &gotsomewhere, &now, &rcode, &delay); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 591 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 592 | /* Only record stats the first time we try a query. See above. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 593 | if (attempt == 0) { |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 594 | res_sample sample; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 595 | _res_stats_set_sample(&sample, now, rcode, delay); |
| 596 | _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample, |
| 597 | params.max_samples); |
| 598 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 599 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 600 | VLOG << "used send_dg " << n; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 601 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 602 | if (n < 0) goto fail; |
| 603 | if (n == 0) goto next_ns; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 604 | VLOG << "time=" << time(NULL); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 605 | if (v_circuit) goto same_ns; |
| 606 | resplen = n; |
| 607 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 608 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 609 | Dprint((statp->options & RES_DEBUG) || |
| 610 | ((statp->pfcode & RES_PRF_REPLY) && (statp->pfcode & RES_PRF_HEAD1)), |
| 611 | (stdout, ";; got answer:\n")); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 612 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 613 | DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY), |
| 614 | (stdout, "%s", ""), ans, (resplen > anssiz) ? anssiz : resplen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 615 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 616 | if (cache_status == RESOLV_CACHE_NOTFOUND) { |
| 617 | _resolv_cache_add(statp->netid, buf, buflen, ans, resplen); |
| 618 | } |
| 619 | /* |
| 620 | * If we have temporarily opened a virtual circuit, |
| 621 | * or if we haven't been asked to keep a socket open, |
| 622 | * close the socket. |
| 623 | */ |
| 624 | if ((v_circuit && (statp->options & RES_USEVC) == 0U) || |
| 625 | (statp->options & RES_STAYOPEN) == 0U) { |
| 626 | res_nclose(statp); |
| 627 | } |
| 628 | if (statp->rhook) { |
| 629 | int done = 0, loops = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 630 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 631 | do { |
| 632 | res_sendhookact act; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 633 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 634 | act = (*statp->rhook)(nsap, buf, buflen, ans, anssiz, &resplen); |
| 635 | switch (act) { |
| 636 | case res_goahead: |
| 637 | case res_done: |
| 638 | done = 1; |
| 639 | break; |
| 640 | case res_nextns: |
| 641 | res_nclose(statp); |
| 642 | goto next_ns; |
| 643 | case res_modified: |
| 644 | /* give the hook another try */ |
| 645 | if (++loops < 42) /*doug adams*/ |
| 646 | break; |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 647 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 648 | case res_error: |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 649 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 650 | default: |
| 651 | goto fail; |
| 652 | } |
| 653 | } while (!done); |
| 654 | } |
| 655 | return (resplen); |
| 656 | next_ns:; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 657 | } // for each ns |
| 658 | } // for each retry |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 659 | res_nclose(statp); |
| 660 | if (!v_circuit) { |
| 661 | if (!gotsomewhere) |
| 662 | errno = ECONNREFUSED; /* no nameservers found */ |
| 663 | else |
| 664 | errno = ETIMEDOUT; /* no answer obtained */ |
| 665 | } else |
| 666 | errno = terrno; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 667 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 668 | _resolv_cache_query_failed(statp->netid, buf, buflen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 669 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 670 | return (-1); |
| 671 | fail: |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 672 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 673 | _resolv_cache_query_failed(statp->netid, buf, buflen); |
| 674 | res_nclose(statp); |
| 675 | return (-1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* Private */ |
| 679 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 680 | static int get_salen(const struct sockaddr* sa) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 681 | if (sa->sa_family == AF_INET) |
| 682 | return (sizeof(struct sockaddr_in)); |
| 683 | else if (sa->sa_family == AF_INET6) |
| 684 | return (sizeof(struct sockaddr_in6)); |
| 685 | else |
| 686 | return (0); /* unknown, die on connect */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* |
| 690 | * pick appropriate nsaddr_list for use. see res_init() for initialization. |
| 691 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 692 | static struct sockaddr* get_nsaddr(res_state statp, size_t n) { |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 693 | if (!statp->nsaddr_list[n].sin_family && statp->_u._ext.ext) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 694 | /* |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 695 | * - statp->_u._ext.ext->nsaddrs[n] holds an address that is larger |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 696 | * than struct sockaddr, and |
| 697 | * - user code did not update statp->nsaddr_list[n]. |
| 698 | */ |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 699 | return (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[n]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 700 | } else { |
| 701 | /* |
| 702 | * - user code updated statp->nsaddr_list[n], or |
| 703 | * - statp->nsaddr_list[n] has the same content as |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 704 | * statp->_u._ext.ext->nsaddrs[n]. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 705 | */ |
| 706 | return (struct sockaddr*) (void*) &statp->nsaddr_list[n]; |
| 707 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 708 | } |
| 709 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 710 | static struct timespec get_timeout(const res_state statp, const struct __res_params* params, |
| 711 | const int ns) { |
| 712 | int msec; |
| 713 | if (params->base_timeout_msec != 0) { |
| 714 | // TODO: scale the timeout by retry attempt and maybe number of servers |
| 715 | msec = params->base_timeout_msec; |
| 716 | } else { |
| 717 | // Legacy algorithm which scales the timeout by nameserver number. |
| 718 | // For instance, with 4 nameservers: 5s, 2.5s, 5s, 10s |
| 719 | // This has no effect with 1 or 2 nameservers |
| 720 | msec = (statp->retrans * 1000) << ns; |
| 721 | if (ns > 0) { |
| 722 | msec /= statp->nscount; |
| 723 | } |
| 724 | if (msec < 1000) { |
| 725 | msec = 1000; // Use at least 100ms |
| 726 | } |
| 727 | } |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 728 | VLOG << "using timeout of " << msec << " msec"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 729 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 730 | struct timespec result; |
| 731 | result.tv_sec = msec / 1000; |
| 732 | result.tv_nsec = (msec % 1000) * 1000000; |
| 733 | return result; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 734 | } |
| 735 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 736 | static int send_vc(res_state statp, struct __res_params* params, const u_char* buf, int buflen, |
| 737 | u_char* ans, int anssiz, int* terrno, int ns, time_t* at, int* rcode, |
| 738 | int* delay) { |
| 739 | *at = time(NULL); |
| 740 | *rcode = RCODE_INTERNAL_ERROR; |
| 741 | *delay = 0; |
| 742 | const HEADER* hp = (const HEADER*) (const void*) buf; |
| 743 | HEADER* anhp = (HEADER*) (void*) ans; |
| 744 | struct sockaddr* nsap; |
| 745 | int nsaplen; |
| 746 | int truncating, connreset, resplen, n; |
| 747 | struct iovec iov[2]; |
| 748 | u_short len; |
| 749 | u_char* cp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 750 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 751 | VLOG << "using send_vc"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 752 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 753 | nsap = get_nsaddr(statp, (size_t) ns); |
| 754 | nsaplen = get_salen(nsap); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 755 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 756 | connreset = 0; |
| 757 | same_ns: |
| 758 | truncating = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 759 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 760 | struct timespec now = evNowTime(); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 761 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 762 | /* Are we still talking to whom we want to talk to? */ |
| 763 | if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) { |
| 764 | struct sockaddr_storage peer; |
| 765 | socklen_t size = sizeof peer; |
| 766 | unsigned old_mark; |
| 767 | socklen_t mark_size = sizeof(old_mark); |
| 768 | if (getpeername(statp->_vcsock, (struct sockaddr*) (void*) &peer, &size) < 0 || |
| 769 | !sock_eq((struct sockaddr*) (void*) &peer, nsap) || |
| 770 | getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 || |
| 771 | old_mark != statp->_mark) { |
| 772 | res_nclose(statp); |
| 773 | statp->_flags &= ~RES_F_VC; |
| 774 | } |
| 775 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 776 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 777 | if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) { |
| 778 | if (statp->_vcsock >= 0) res_nclose(statp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 779 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 780 | statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 781 | if (statp->_vcsock < 0) { |
| 782 | switch (errno) { |
| 783 | case EPROTONOSUPPORT: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 784 | case EPFNOSUPPORT: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 785 | case EAFNOSUPPORT: |
| 786 | Perror(statp, stderr, "socket(vc)", errno); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 787 | return 0; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 788 | default: |
| 789 | *terrno = errno; |
| 790 | Perror(statp, stderr, "socket(vc)", errno); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 791 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | fchown(statp->_vcsock, AID_DNS, -1); |
| 795 | if (statp->_mark != MARK_UNSET) { |
| 796 | if (setsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &statp->_mark, |
| 797 | sizeof(statp->_mark)) < 0) { |
| 798 | *terrno = errno; |
| 799 | Perror(statp, stderr, "setsockopt", errno); |
| 800 | return -1; |
| 801 | } |
| 802 | } |
| 803 | errno = 0; |
| 804 | if (random_bind(statp->_vcsock, nsap->sa_family) < 0) { |
| 805 | *terrno = errno; |
| 806 | Aerror(statp, stderr, "bind/vc", errno, nsap, nsaplen); |
| 807 | res_nclose(statp); |
| 808 | return (0); |
| 809 | } |
| 810 | if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t) nsaplen, |
| 811 | get_timeout(statp, params, ns)) < 0) { |
| 812 | *terrno = errno; |
| 813 | Aerror(statp, stderr, "connect/vc", errno, nsap, nsaplen); |
| 814 | res_nclose(statp); |
| 815 | /* |
| 816 | * The way connect_with_timeout() is implemented prevents us from reliably |
| 817 | * determining whether this was really a timeout or e.g. ECONNREFUSED. Since |
| 818 | * currently both cases are handled in the same way, there is no need to |
| 819 | * change this (yet). If we ever need to reliably distinguish between these |
| 820 | * cases, both connect_with_timeout() and retrying_poll() need to be |
| 821 | * modified, though. |
| 822 | */ |
| 823 | *rcode = RCODE_TIMEOUT; |
| 824 | return (0); |
| 825 | } |
| 826 | statp->_flags |= RES_F_VC; |
| 827 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 828 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 829 | /* |
| 830 | * Send length & message |
| 831 | */ |
| 832 | ns_put16((u_short) buflen, (u_char*) (void*) &len); |
| 833 | iov[0] = evConsIovec(&len, INT16SZ); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 834 | iov[1] = evConsIovec((void*) buf, (size_t) buflen); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 835 | if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) { |
| 836 | *terrno = errno; |
| 837 | Perror(statp, stderr, "write failed", errno); |
| 838 | res_nclose(statp); |
| 839 | return (0); |
| 840 | } |
| 841 | /* |
| 842 | * Receive length & response |
| 843 | */ |
| 844 | read_len: |
| 845 | cp = ans; |
| 846 | len = INT16SZ; |
| 847 | while ((n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) { |
| 848 | cp += n; |
| 849 | if ((len -= n) == 0) break; |
| 850 | } |
| 851 | if (n <= 0) { |
| 852 | *terrno = errno; |
| 853 | Perror(statp, stderr, "read failed", errno); |
| 854 | res_nclose(statp); |
| 855 | /* |
| 856 | * A long running process might get its TCP |
| 857 | * connection reset if the remote server was |
| 858 | * restarted. Requery the server instead of |
| 859 | * trying a new one. When there is only one |
| 860 | * server, this means that a query might work |
| 861 | * instead of failing. We only allow one reset |
| 862 | * per query to prevent looping. |
| 863 | */ |
| 864 | if (*terrno == ECONNRESET && !connreset) { |
| 865 | connreset = 1; |
| 866 | res_nclose(statp); |
| 867 | goto same_ns; |
| 868 | } |
| 869 | res_nclose(statp); |
| 870 | return (0); |
| 871 | } |
| 872 | resplen = ns_get16(ans); |
| 873 | if (resplen > anssiz) { |
| 874 | Dprint(statp->options & RES_DEBUG, (stdout, ";; response truncated\n")); |
| 875 | truncating = 1; |
| 876 | len = anssiz; |
| 877 | } else |
| 878 | len = resplen; |
| 879 | if (len < HFIXEDSZ) { |
| 880 | /* |
| 881 | * Undersized message. |
| 882 | */ |
| 883 | Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", len)); |
| 884 | *terrno = EMSGSIZE; |
| 885 | res_nclose(statp); |
| 886 | return (0); |
| 887 | } |
| 888 | cp = ans; |
| 889 | while (len != 0 && (n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) { |
| 890 | cp += n; |
| 891 | len -= n; |
| 892 | } |
| 893 | if (n <= 0) { |
| 894 | *terrno = errno; |
| 895 | Perror(statp, stderr, "read(vc)", errno); |
| 896 | res_nclose(statp); |
| 897 | return (0); |
| 898 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 899 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 900 | if (truncating) { |
| 901 | /* |
| 902 | * Flush rest of answer so connection stays in synch. |
| 903 | */ |
| 904 | anhp->tc = 1; |
| 905 | len = resplen - anssiz; |
| 906 | while (len != 0) { |
| 907 | char junk[PACKETSZ]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 908 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 909 | n = read(statp->_vcsock, junk, (len > sizeof junk) ? sizeof junk : len); |
| 910 | if (n > 0) |
| 911 | len -= n; |
| 912 | else |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | /* |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 917 | * If the calling application has bailed out of |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 918 | * a previous call and failed to arrange to have |
| 919 | * the circuit closed or the server has got |
| 920 | * itself confused, then drop the packet and |
| 921 | * wait for the correct one. |
| 922 | */ |
| 923 | if (hp->id != anhp->id) { |
| 924 | DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY), |
| 925 | (stdout, ";; old answer (unexpected):\n"), ans, |
| 926 | (resplen > anssiz) ? anssiz : resplen); |
| 927 | goto read_len; |
| 928 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 929 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 930 | /* |
| 931 | * All is well, or the error is fatal. Signal that the |
| 932 | * next nameserver ought not be tried. |
| 933 | */ |
| 934 | if (resplen > 0) { |
| 935 | struct timespec done = evNowTime(); |
| 936 | *delay = _res_stats_calculate_rtt(&done, &now); |
| 937 | *rcode = anhp->rcode; |
| 938 | } |
| 939 | return (resplen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* return -1 on error (errno set), 0 on success */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 943 | static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen, |
| 944 | const struct timespec timeout) { |
| 945 | int res, origflags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 946 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 947 | origflags = fcntl(sock, F_GETFL, 0); |
| 948 | fcntl(sock, F_SETFL, origflags | O_NONBLOCK); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 949 | |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 950 | res = connect(sock, nsap, salen); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 951 | if (res < 0 && errno != EINPROGRESS) { |
| 952 | res = -1; |
| 953 | goto done; |
| 954 | } |
| 955 | if (res != 0) { |
| 956 | struct timespec now = evNowTime(); |
| 957 | struct timespec finish = evAddTime(now, timeout); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 958 | VLOG << sock << " send_vc"; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 959 | res = retrying_poll(sock, POLLIN | POLLOUT, &finish); |
| 960 | if (res <= 0) { |
| 961 | res = -1; |
| 962 | } |
| 963 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 964 | done: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 965 | fcntl(sock, F_SETFL, origflags); |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 966 | VLOG << sock << " connect_with_const timeout returning " << res; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 967 | return res; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 968 | } |
| 969 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 970 | static int retrying_poll(const int sock, const short events, const struct timespec* finish) { |
| 971 | struct timespec now, timeout; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 972 | |
| 973 | retry: |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 974 | VLOG << " " << sock << " retrying_poll"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 975 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 976 | now = evNowTime(); |
| 977 | if (evCmpTime(*finish, now) > 0) |
| 978 | timeout = evSubTime(*finish, now); |
| 979 | else |
| 980 | timeout = evConsTime(0L, 0L); |
| 981 | struct pollfd fds = {.fd = sock, .events = events}; |
| 982 | int n = ppoll(&fds, 1, &timeout, /*sigmask=*/NULL); |
| 983 | if (n == 0) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 984 | VLOG << " " << sock << "retrying_poll timeout"; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 985 | errno = ETIMEDOUT; |
| 986 | return 0; |
| 987 | } |
| 988 | if (n < 0) { |
| 989 | if (errno == EINTR) goto retry; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 990 | VLOG << " " << sock << " retrying_poll got error " << n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 991 | return n; |
| 992 | } |
| 993 | if (fds.revents & (POLLIN | POLLOUT | POLLERR)) { |
| 994 | int error; |
| 995 | socklen_t len = sizeof(error); |
| 996 | if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) { |
| 997 | errno = error; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 998 | VLOG << " " << sock << " retrying_poll dot error2 " << errno; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 999 | return -1; |
| 1000 | } |
| 1001 | } |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1002 | VLOG << " " << sock << " retrying_poll returning " << n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1003 | return n; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1004 | } |
| 1005 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1006 | static int send_dg(res_state statp, struct __res_params* params, const u_char* buf, int buflen, |
| 1007 | u_char* ans, int anssiz, int* terrno, int ns, int* v_circuit, int* gotsomewhere, |
| 1008 | time_t* at, int* rcode, int* delay) { |
| 1009 | *at = time(NULL); |
| 1010 | *rcode = RCODE_INTERNAL_ERROR; |
| 1011 | *delay = 0; |
| 1012 | const HEADER* hp = (const HEADER*) (const void*) buf; |
| 1013 | HEADER* anhp = (HEADER*) (void*) ans; |
| 1014 | const struct sockaddr* nsap; |
| 1015 | int nsaplen; |
| 1016 | struct timespec now, timeout, finish, done; |
| 1017 | struct sockaddr_storage from; |
| 1018 | socklen_t fromlen; |
| 1019 | int resplen, n, s; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1020 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1021 | nsap = get_nsaddr(statp, (size_t) ns); |
| 1022 | nsaplen = get_salen(nsap); |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 1023 | if (statp->_u._ext.nssocks[ns] == -1) { |
| 1024 | statp->_u._ext.nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 1025 | if (statp->_u._ext.nssocks[ns] < 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1026 | switch (errno) { |
| 1027 | case EPROTONOSUPPORT: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1028 | case EPFNOSUPPORT: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1029 | case EAFNOSUPPORT: |
| 1030 | Perror(statp, stderr, "socket(dg)", errno); |
| 1031 | return (0); |
| 1032 | default: |
| 1033 | *terrno = errno; |
| 1034 | Perror(statp, stderr, "socket(dg)", errno); |
| 1035 | return (-1); |
| 1036 | } |
| 1037 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1038 | |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 1039 | fchown(statp->_u._ext.nssocks[ns], AID_DNS, -1); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1040 | if (statp->_mark != MARK_UNSET) { |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 1041 | if (setsockopt(statp->_u._ext.nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark), |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1042 | sizeof(statp->_mark)) < 0) { |
| 1043 | res_nclose(statp); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1047 | #ifndef CANNOT_CONNECT_DGRAM |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1048 | /* |
| 1049 | * On a 4.3BSD+ machine (client and server, |
| 1050 | * actually), sending to a nameserver datagram |
| 1051 | * port with no nameserver will cause an |
| 1052 | * ICMP port unreachable message to be returned. |
| 1053 | * If our datagram socket is "connected" to the |
| 1054 | * server, we get an ECONNREFUSED error on the next |
| 1055 | * socket operation, and select returns if the |
| 1056 | * error message is received. We can thus detect |
| 1057 | * the absence of a nameserver without timing out. |
| 1058 | */ |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 1059 | if (random_bind(statp->_u._ext.nssocks[ns], nsap->sa_family) < 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1060 | Aerror(statp, stderr, "bind(dg)", errno, nsap, nsaplen); |
| 1061 | res_nclose(statp); |
| 1062 | return (0); |
| 1063 | } |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 1064 | if (connect(statp->_u._ext.nssocks[ns], nsap, (socklen_t) nsaplen) < 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1065 | Aerror(statp, stderr, "connect(dg)", errno, nsap, nsaplen); |
| 1066 | res_nclose(statp); |
| 1067 | return (0); |
| 1068 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1069 | #endif /* !CANNOT_CONNECT_DGRAM */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1070 | Dprint(statp->options & RES_DEBUG, (stdout, ";; new DG socket\n")) |
| 1071 | } |
Ken Chen | cf6ded6 | 2018-10-16 23:12:09 +0800 | [diff] [blame^] | 1072 | s = statp->_u._ext.nssocks[ns]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1073 | #ifndef CANNOT_CONNECT_DGRAM |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1074 | if (send(s, (const char*) buf, (size_t) buflen, 0) != buflen) { |
| 1075 | Perror(statp, stderr, "send", errno); |
| 1076 | res_nclose(statp); |
| 1077 | return (0); |
| 1078 | } |
| 1079 | #else /* !CANNOT_CONNECT_DGRAM */ |
| 1080 | if (sendto(s, (const char*) buf, buflen, 0, nsap, nsaplen) != buflen) { |
| 1081 | Aerror(statp, stderr, "sendto", errno, nsap, nsaplen); |
| 1082 | res_nclose(statp); |
| 1083 | return (0); |
| 1084 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1085 | #endif /* !CANNOT_CONNECT_DGRAM */ |
| 1086 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1087 | /* |
| 1088 | * Wait for reply. |
| 1089 | */ |
| 1090 | timeout = get_timeout(statp, params, ns); |
| 1091 | now = evNowTime(); |
| 1092 | finish = evAddTime(now, timeout); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1093 | retry: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1094 | n = retrying_poll(s, POLLIN, &finish); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1095 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1096 | if (n == 0) { |
| 1097 | *rcode = RCODE_TIMEOUT; |
| 1098 | Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n")); |
| 1099 | *gotsomewhere = 1; |
| 1100 | return (0); |
| 1101 | } |
| 1102 | if (n < 0) { |
| 1103 | Perror(statp, stderr, "poll", errno); |
| 1104 | res_nclose(statp); |
| 1105 | return (0); |
| 1106 | } |
| 1107 | errno = 0; |
| 1108 | fromlen = sizeof(from); |
| 1109 | resplen = recvfrom(s, (char*) ans, (size_t) anssiz, 0, (struct sockaddr*) (void*) &from, |
| 1110 | &fromlen); |
| 1111 | if (resplen <= 0) { |
| 1112 | Perror(statp, stderr, "recvfrom", errno); |
| 1113 | res_nclose(statp); |
| 1114 | return (0); |
| 1115 | } |
| 1116 | *gotsomewhere = 1; |
| 1117 | if (resplen < HFIXEDSZ) { |
| 1118 | /* |
| 1119 | * Undersized message. |
| 1120 | */ |
| 1121 | Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", resplen)); |
| 1122 | *terrno = EMSGSIZE; |
| 1123 | res_nclose(statp); |
| 1124 | return (0); |
| 1125 | } |
| 1126 | if (hp->id != anhp->id) { |
| 1127 | /* |
| 1128 | * response from old query, ignore it. |
| 1129 | * XXX - potential security hazard could |
| 1130 | * be detected here. |
| 1131 | */ |
| 1132 | DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY), |
| 1133 | (stdout, ";; old answer:\n"), ans, (resplen > anssiz) ? anssiz : resplen); |
| 1134 | goto retry; |
| 1135 | } |
| 1136 | if (!(statp->options & RES_INSECURE1) && |
| 1137 | !res_ourserver_p(statp, (struct sockaddr*) (void*) &from)) { |
| 1138 | /* |
| 1139 | * response from wrong server? ignore it. |
| 1140 | * XXX - potential security hazard could |
| 1141 | * be detected here. |
| 1142 | */ |
| 1143 | DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY), |
| 1144 | (stdout, ";; not our server:\n"), ans, (resplen > anssiz) ? anssiz : resplen); |
| 1145 | goto retry; |
| 1146 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1147 | if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) { |
| 1148 | /* |
| 1149 | * Do not retry if the server do not understand EDNS0. |
| 1150 | * The case has to be captured here, as FORMERR packet do not |
| 1151 | * carry query section, hence res_queriesmatch() returns 0. |
| 1152 | */ |
| 1153 | DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query with EDNS0:\n"), ans, |
| 1154 | (resplen > anssiz) ? anssiz : resplen); |
| 1155 | /* record the error */ |
| 1156 | statp->_flags |= RES_F_EDNS0ERR; |
| 1157 | res_nclose(statp); |
| 1158 | return (0); |
| 1159 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1160 | if (!(statp->options & RES_INSECURE2) && |
| 1161 | !res_queriesmatch(buf, buf + buflen, ans, ans + anssiz)) { |
| 1162 | /* |
| 1163 | * response contains wrong query? ignore it. |
| 1164 | * XXX - potential security hazard could |
| 1165 | * be detected here. |
| 1166 | */ |
| 1167 | DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY), |
| 1168 | (stdout, ";; wrong query name:\n"), ans, (resplen > anssiz) ? anssiz : resplen); |
| 1169 | goto retry; |
| 1170 | ; |
| 1171 | } |
| 1172 | done = evNowTime(); |
| 1173 | *delay = _res_stats_calculate_rtt(&done, &now); |
| 1174 | if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || anhp->rcode == REFUSED) { |
| 1175 | DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query:\n"), ans, |
| 1176 | (resplen > anssiz) ? anssiz : resplen); |
| 1177 | res_nclose(statp); |
| 1178 | /* don't retry if called from dig */ |
| 1179 | if (!statp->pfcode) { |
| 1180 | *rcode = anhp->rcode; |
| 1181 | return (0); |
| 1182 | } |
| 1183 | } |
| 1184 | if (!(statp->options & RES_IGNTC) && anhp->tc) { |
| 1185 | /* |
| 1186 | * To get the rest of answer, |
| 1187 | * use TCP with same server. |
| 1188 | */ |
| 1189 | Dprint(statp->options & RES_DEBUG, (stdout, ";; truncated answer\n")); |
| 1190 | *v_circuit = 1; |
| 1191 | res_nclose(statp); |
| 1192 | return (1); |
| 1193 | } |
| 1194 | /* |
| 1195 | * All is well, or the error is fatal. Signal that the |
| 1196 | * next nameserver ought not be tried. |
| 1197 | */ |
| 1198 | if (resplen > 0) { |
| 1199 | *rcode = anhp->rcode; |
| 1200 | } |
| 1201 | return (resplen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1202 | } |
| 1203 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1204 | static void Aerror(const res_state statp, FILE* file, const char* string, int error, |
| 1205 | const struct sockaddr* address, int alen) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1206 | if (!kVerboseLogging) return; |
| 1207 | |
| 1208 | const int save = errno; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1209 | char hbuf[NI_MAXHOST]; |
| 1210 | char sbuf[NI_MAXSERV]; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1211 | constexpr int niflags = NI_NUMERICHOST | NI_NUMERICSERV; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1212 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1213 | if ((statp->options & RES_DEBUG) != 0U) { |
| 1214 | if (getnameinfo(address, (socklen_t) alen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), |
| 1215 | niflags)) { |
| 1216 | strncpy(hbuf, "?", sizeof(hbuf) - 1); |
| 1217 | hbuf[sizeof(hbuf) - 1] = '\0'; |
| 1218 | strncpy(sbuf, "?", sizeof(sbuf) - 1); |
| 1219 | sbuf[sizeof(sbuf) - 1] = '\0'; |
| 1220 | } |
| 1221 | fprintf(file, "res_send: %s ([%s].%s): %s\n", string, hbuf, sbuf, strerror(error)); |
| 1222 | } |
| 1223 | errno = save; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1224 | } |
| 1225 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1226 | static void Perror(const res_state statp, FILE* file, const char* string, int error) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1227 | if (!kVerboseLogging) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1228 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 1229 | const int save = errno; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1230 | if ((statp->options & RES_DEBUG) != 0U) |
| 1231 | fprintf(file, "res_send: %s: %s\n", string, strerror(error)); |
| 1232 | errno = save; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1233 | } |
| 1234 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1235 | static int sock_eq(struct sockaddr* a, struct sockaddr* b) { |
| 1236 | struct sockaddr_in *a4, *b4; |
| 1237 | struct sockaddr_in6 *a6, *b6; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1238 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1239 | if (a->sa_family != b->sa_family) return 0; |
| 1240 | switch (a->sa_family) { |
| 1241 | case AF_INET: |
| 1242 | a4 = (struct sockaddr_in*) (void*) a; |
| 1243 | b4 = (struct sockaddr_in*) (void*) b; |
| 1244 | return a4->sin_port == b4->sin_port && a4->sin_addr.s_addr == b4->sin_addr.s_addr; |
| 1245 | case AF_INET6: |
| 1246 | a6 = (struct sockaddr_in6*) (void*) a; |
| 1247 | b6 = (struct sockaddr_in6*) (void*) b; |
| 1248 | return a6->sin6_port == b6->sin6_port && |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1249 | #ifdef HAVE_SIN6_SCOPE_ID |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1250 | a6->sin6_scope_id == b6->sin6_scope_id && |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1251 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1252 | IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr); |
| 1253 | default: |
| 1254 | return 0; |
| 1255 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1256 | } |