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