]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/res_send.c
Change most internal uses of __gettimeofday to __clock_gettime.
[thirdparty/glibc.git] / resolv / res_send.c
CommitLineData
04277e02 1/* Copyright (C) 2016-2019 Free Software Foundation, Inc.
e9db92d3
CD
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
e9db92d3 17
28f540f4 18/*
28f540f4
RM
19 * Copyright (c) 1985, 1989, 1993
20 * The Regents of the University of California. All rights reserved.
e62b2105 21 *
28f540f4
RM
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
28f540f4
RM
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
e62b2105 33 *
28f540f4
RM
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
b43b13ac
UD
45 */
46
47/*
28f540f4 48 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
e62b2105 49 *
28f540f4
RM
50 * Permission to use, copy, modify, and distribute this software for any
51 * purpose with or without fee is hereby granted, provided that the above
52 * copyright notice and this permission notice appear in all copies, and that
53 * the name of Digital Equipment Corporation not be used in advertising or
54 * publicity pertaining to distribution of the document or software without
55 * specific, written prior permission.
e62b2105 56 *
28f540f4
RM
57 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
58 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
60 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64 * SOFTWARE.
b43b13ac
UD
65 */
66
67/*
68 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
69 *
70 * Permission to use, copy, modify, and distribute this software for any
71 * purpose with or without fee is hereby granted, provided that the above
72 * copyright notice and this permission notice appear in all copies.
73 *
74 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
75 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
77 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 * SOFTWARE.
28f540f4
RM
82 */
83
28f540f4
RM
84/*
85 * Send query to name server and wait for reply.
86 */
87
17a10319 88#include <assert.h>
df21c858 89#include <sys/types.h>
28f540f4
RM
90#include <sys/param.h>
91#include <sys/time.h>
92#include <sys/socket.h>
93#include <sys/uio.h>
e685e07d 94#include <sys/poll.h>
b43b13ac 95
28f540f4
RM
96#include <netinet/in.h>
97#include <arpa/nameser.h>
98#include <arpa/inet.h>
0420d888 99#include <sys/ioctl.h>
28f540f4 100
28f540f4 101#include <errno.h>
f433b06b 102#include <fcntl.h>
b43b13ac 103#include <netdb.h>
a7ff1da8 104#include <resolv/resolv-internal.h>
352f4ff9 105#include <resolv/resolv_context.h>
b43b13ac 106#include <signal.h>
b43b13ac
UD
107#include <stdlib.h>
108#include <string.h>
109#include <unistd.h>
9744268c 110#include <kernel-features.h>
9090848d 111#include <libc-diag.h>
359653aa 112#include <random-bits.h>
b43b13ac 113
0420d888
UD
114#if PACKETSZ > 65536
115#define MAXPACKET PACKETSZ
116#else
117#define MAXPACKET 65536
118#endif
119
e685e07d 120/* From ev_streams.c. */
e62b2105 121
25337753
UD
122static inline void
123__attribute ((always_inline))
124evConsIovec(void *buf, size_t cnt, struct iovec *vec) {
125 memset(vec, 0xf5, sizeof (*vec));
126 vec->iov_base = buf;
127 vec->iov_len = cnt;
e685e07d 128}
28f540f4 129
e685e07d 130/* From ev_timers.c. */
30f9ca19 131
b43b13ac 132#define BILLION 1000000000
e685e07d 133
25337753
UD
134static inline void
135evConsTime(struct timespec *res, time_t sec, long nsec) {
136 res->tv_sec = sec;
137 res->tv_nsec = nsec;
b43b13ac 138}
28f540f4 139
25337753
UD
140static inline void
141evAddTime(struct timespec *res, const struct timespec *addend1,
142 const struct timespec *addend2) {
143 res->tv_sec = addend1->tv_sec + addend2->tv_sec;
144 res->tv_nsec = addend1->tv_nsec + addend2->tv_nsec;
145 if (res->tv_nsec >= BILLION) {
146 res->tv_sec++;
147 res->tv_nsec -= BILLION;
b43b13ac 148 }
b43b13ac
UD
149}
150
25337753
UD
151static inline void
152evSubTime(struct timespec *res, const struct timespec *minuend,
153 const struct timespec *subtrahend) {
154 res->tv_sec = minuend->tv_sec - subtrahend->tv_sec;
155 if (minuend->tv_nsec >= subtrahend->tv_nsec)
156 res->tv_nsec = minuend->tv_nsec - subtrahend->tv_nsec;
b43b13ac 157 else {
25337753
UD
158 res->tv_nsec = (BILLION
159 - subtrahend->tv_nsec + minuend->tv_nsec);
160 res->tv_sec--;
b43b13ac 161 }
b43b13ac
UD
162}
163
f1d70dad 164static int
b43b13ac
UD
165evCmpTime(struct timespec a, struct timespec b) {
166 long x = a.tv_sec - b.tv_sec;
167
168 if (x == 0L)
169 x = a.tv_nsec - b.tv_nsec;
170 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
171}
172
f1d70dad 173static void
25337753 174evNowTime(struct timespec *res) {
4a39c34c 175 __clock_gettime(CLOCK_REALTIME, res);
b43b13ac 176}
b43b13ac 177
28f540f4 178
e685e07d 179#define EXT(res) ((res)->_u._ext)
28f540f4 180
e685e07d
UD
181/* Forward. */
182
5b757a51 183static struct sockaddr *get_nsaddr (res_state, unsigned int);
e685e07d 184static int send_vc(res_state, const u_char *, int,
1eb946b9
UD
185 const u_char *, int,
186 u_char **, int *, int *, int, u_char **,
ab09bf61 187 u_char **, int *, int *, int *);
e685e07d 188static int send_dg(res_state, const u_char *, int,
1eb946b9 189 const u_char *, int,
0420d888 190 u_char **, int *, int *, int,
1eb946b9 191 int *, int *, u_char **,
ab09bf61 192 u_char **, int *, int *, int *);
438e8239 193static int sock_eq(struct sockaddr_in6 *, struct sockaddr_in6 *);
e685e07d 194
e685e07d
UD
195/* Public. */
196
28f540f4
RM
197/* int
198 * res_isourserver(ina)
199 * looks up "ina" in _res.ns_addr_list[]
200 * returns:
201 * 0 : not found
202 * >0 : found
203 * author:
204 * paul vixie, 29may94
205 */
206int
438e8239 207res_ourserver_p(const res_state statp, const struct sockaddr_in6 *inp)
438e8239 208{
b43b13ac 209 int ns;
28f540f4 210
3a85895f
PB
211 if (inp->sin6_family == AF_INET) {
212 struct sockaddr_in *in4p = (struct sockaddr_in *) inp;
e62b2105
UD
213 in_port_t port = in4p->sin_port;
214 in_addr_t addr = in4p->sin_addr.s_addr;
438e8239 215
2212c142 216 for (ns = 0; ns < statp->nscount; ns++) {
3a85895f 217 const struct sockaddr_in *srv =
2212c142 218 (struct sockaddr_in *) get_nsaddr (statp, ns);
438e8239 219
2212c142 220 if ((srv->sin_family == AF_INET) &&
3a85895f
PB
221 (srv->sin_port == port) &&
222 (srv->sin_addr.s_addr == INADDR_ANY ||
223 srv->sin_addr.s_addr == addr))
224 return (1);
225 }
226 } else if (inp->sin6_family == AF_INET6) {
2212c142
AS
227 for (ns = 0; ns < statp->nscount; ns++) {
228 const struct sockaddr_in6 *srv
229 = (struct sockaddr_in6 *) get_nsaddr (statp, ns);
230 if ((srv->sin6_family == AF_INET6) &&
3a85895f
PB
231 (srv->sin6_port == inp->sin6_port) &&
232 !(memcmp(&srv->sin6_addr, &in6addr_any,
233 sizeof (struct in6_addr)) &&
234 memcmp(&srv->sin6_addr, &inp->sin6_addr,
235 sizeof (struct in6_addr))))
236 return (1);
237 }
238 }
b43b13ac 239 return (0);
28f540f4
RM
240}
241
ded60354
FW
242int
243res_isourserver (const struct sockaddr_in *inp)
244{
245 return res_ourserver_p (&_res, (const struct sockaddr_in6 *) inp);
246}
247
28f540f4
RM
248/* int
249 * res_nameinquery(name, type, class, buf, eom)
250 * look for (name,type,class) in the query section of packet (buf,eom)
66715f83 251 * requires:
b43b13ac 252 * buf + HFIXEDSZ <= eom
28f540f4
RM
253 * returns:
254 * -1 : format error
255 * 0 : not found
256 * >0 : found
257 * author:
258 * paul vixie, 29may94
259 */
260int
b43b13ac
UD
261res_nameinquery(const char *name, int type, int class,
262 const u_char *buf, const u_char *eom)
28f540f4 263{
b43b13ac 264 const u_char *cp = buf + HFIXEDSZ;
28f540f4
RM
265 int qdcount = ntohs(((HEADER*)buf)->qdcount);
266
267 while (qdcount-- > 0) {
268 char tname[MAXDNAME+1];
b43b13ac 269 int n, ttype, tclass;
28f540f4
RM
270
271 n = dn_expand(buf, eom, cp, tname, sizeof tname);
272 if (n < 0)
273 return (-1);
274 cp += n;
66715f83
UD
275 if (cp + 2 * INT16SZ > eom)
276 return (-1);
697e1628
UD
277 NS_GET16(ttype, cp);
278 NS_GET16(tclass, cp);
b43b13ac
UD
279 if (ttype == type && tclass == class &&
280 ns_samename(tname, name) == 1)
28f540f4
RM
281 return (1);
282 }
283 return (0);
284}
6f9d8e68 285libresolv_hidden_def (res_nameinquery)
28f540f4 286
5b757a51
FW
287/* Returns a shift value for the name server index. Used to implement
288 RES_ROTATE. */
289static unsigned int
290nameserver_offset (struct __res_state *statp)
291{
292 /* If we only have one name server or rotation is disabled, return
293 offset 0 (no rotation). */
294 unsigned int nscount = statp->nscount;
295 if (nscount <= 1 || !(statp->options & RES_ROTATE))
296 return 0;
297
298 /* Global offset. The lowest bit indicates whether the offset has
299 been initialized with a random value. Use relaxed MO to access
300 global_offset because all we need is a sequence of roughly
301 sequential value. */
302 static unsigned int global_offset;
303 unsigned int offset = atomic_fetch_add_relaxed (&global_offset, 2);
304 if ((offset & 1) == 0)
305 {
306 /* Initialization is required. */
359653aa 307 offset = random_bits ();
5b757a51
FW
308 /* The lowest bit is the most random. Preserve it. */
309 offset <<= 1;
310
311 /* Store the new starting value. atomic_fetch_add_relaxed
312 returns the old value, so emulate that by storing the new
313 (incremented) value. Concurrent initialization with
314 different random values is harmless. */
315 atomic_store_relaxed (&global_offset, (offset | 1) + 2);
316 }
317
318 /* Remove the initialization bit. */
319 offset >>= 1;
320
321 /* Avoid the division in the most common cases. */
322 switch (nscount)
323 {
324 case 2:
325 return offset & 1;
326 case 3:
327 return offset % 3;
328 case 4:
329 return offset & 3;
330 default:
331 return offset % nscount;
332 }
333}
334
28f540f4
RM
335/* int
336 * res_queriesmatch(buf1, eom1, buf2, eom2)
337 * is there a 1:1 mapping of (name,type,class)
338 * in (buf1,eom1) and (buf2,eom2)?
339 * returns:
340 * -1 : format error
341 * 0 : not a 1:1 mapping
342 * >0 : is a 1:1 mapping
343 * author:
344 * paul vixie, 29may94
345 */
346int
b43b13ac
UD
347res_queriesmatch(const u_char *buf1, const u_char *eom1,
348 const u_char *buf2, const u_char *eom2)
28f540f4 349{
66715f83
UD
350 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
351 return (-1);
352
b43b13ac
UD
353 /*
354 * Only header section present in replies to
355 * dynamic update packets.
356 */
e685e07d
UD
357 if ((((HEADER *)buf1)->opcode == ns_o_update) &&
358 (((HEADER *)buf2)->opcode == ns_o_update))
b43b13ac
UD
359 return (1);
360
697e1628 361 /* Note that we initially do not convert QDCOUNT to the host byte
8e45b1ac 362 order. We can compare it with the second buffer's QDCOUNT
697e1628
UD
363 value without doing this. */
364 int qdcount = ((HEADER*)buf1)->qdcount;
365 if (qdcount != ((HEADER*)buf2)->qdcount)
28f540f4 366 return (0);
697e1628
UD
367
368 qdcount = htons (qdcount);
369 const u_char *cp = buf1 + HFIXEDSZ;
370
28f540f4
RM
371 while (qdcount-- > 0) {
372 char tname[MAXDNAME+1];
b43b13ac 373 int n, ttype, tclass;
28f540f4
RM
374
375 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
376 if (n < 0)
377 return (-1);
378 cp += n;
66715f83
UD
379 if (cp + 2 * INT16SZ > eom1)
380 return (-1);
697e1628
UD
381 NS_GET16(ttype, cp);
382 NS_GET16(tclass, cp);
28f540f4
RM
383 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
384 return (0);
385 }
386 return (1);
387}
6f9d8e68 388libresolv_hidden_def (res_queriesmatch)
28f540f4
RM
389
390int
352f4ff9
FW
391__res_context_send (struct resolv_context *ctx,
392 const unsigned char *buf, int buflen,
393 const unsigned char *buf2, int buflen2,
394 unsigned char *ans, int anssiz,
395 unsigned char **ansp, unsigned char **ansp2,
396 int *nansp2, int *resplen2, int *ansp2_malloced)
28f540f4 397{
352f4ff9 398 struct __res_state *statp = ctx->resp;
d1bc2cbb
SL
399 int gotsomewhere, terrno, try, v_circuit, resplen;
400 /* On some architectures send_vc is inlined and the compiler might emit
401 a warning indicating 'resplen' may be used uninitialized. Note that
402 the warning belongs to resplen in send_vc which is used as return
403 value! There the maybe-uninitialized warning is already ignored as
404 it is a false-positive - see comment in send_vc.
405 Here the variable n is set to the return value of send_vc.
406 See below. */
407 DIAG_PUSH_NEEDS_COMMENT;
408 DIAG_IGNORE_NEEDS_COMMENT (9, "-Wmaybe-uninitialized");
409 int n;
410 DIAG_POP_NEEDS_COMMENT;
28f540f4 411
e685e07d
UD
412 if (statp->nscount == 0) {
413 __set_errno (ESRCH);
414 return (-1);
415 }
0420d888 416
1eb946b9 417 if (anssiz < (buf2 == NULL ? 1 : 2) * HFIXEDSZ) {
66715f83
UD
418 __set_errno (EINVAL);
419 return (-1);
420 }
0420d888 421
1eb946b9
UD
422 v_circuit = ((statp->options & RES_USEVC)
423 || buflen > PACKETSZ
424 || buflen2 > PACKETSZ);
28f540f4 425 gotsomewhere = 0;
28f540f4 426 terrno = ETIMEDOUT;
28f540f4 427
b43b13ac 428 /*
e685e07d
UD
429 * If the ns_addr_list in the resolver context has changed, then
430 * invalidate our cached copy and the associated timing data.
b43b13ac 431 */
2212c142 432 if (EXT(statp).nscount != 0) {
e685e07d
UD
433 int needclose = 0;
434
435 if (EXT(statp).nscount != statp->nscount)
436 needclose++;
437 else
5b757a51 438 for (unsigned int ns = 0; ns < statp->nscount; ns++) {
2212c142 439 if (statp->nsaddr_list[ns].sin_family != 0
b64e1566 440 && !sock_eq((struct sockaddr_in6 *)
2212c142 441 &statp->nsaddr_list[ns],
b64e1566 442 EXT(statp).nsaddrs[ns]))
438e8239 443 {
e685e07d
UD
444 needclose++;
445 break;
446 }
b64e1566 447 }
2212c142 448 if (needclose) {
cb07f6f6 449 __res_iclose(statp, false);
2212c142
AS
450 EXT(statp).nscount = 0;
451 }
e685e07d
UD
452 }
453
454 /*
455 * Maybe initialize our private copy of the ns_addr_list.
456 */
2212c142 457 if (EXT(statp).nscount == 0) {
5b757a51 458 for (unsigned int ns = 0; ns < statp->nscount; ns++) {
2212c142
AS
459 EXT(statp).nssocks[ns] = -1;
460 if (statp->nsaddr_list[ns].sin_family == 0)
461 continue;
462 if (EXT(statp).nsaddrs[ns] == NULL)
463 EXT(statp).nsaddrs[ns] =
438e8239 464 malloc(sizeof (struct sockaddr_in6));
2212c142
AS
465 if (EXT(statp).nsaddrs[ns] != NULL)
466 memset (mempcpy(EXT(statp).nsaddrs[ns],
cabba934 467 &statp->nsaddr_list[ns],
0f8f993c
UD
468 sizeof (struct sockaddr_in)),
469 '\0',
470 sizeof (struct sockaddr_in6)
471 - sizeof (struct sockaddr_in));
f178e59f 472 else
dae6c43c 473 return -1;
b64e1566 474 }
2212c142 475 EXT(statp).nscount = statp->nscount;
e685e07d
UD
476 }
477
5b757a51
FW
478 /* Name server index offset. Used to implement
479 RES_ROTATE. */
480 unsigned int ns_offset = nameserver_offset (statp);
30f9ca19 481
28f540f4 482 /*
e685e07d 483 * Send request, RETRY times, or until successful.
28f540f4 484 */
b43b13ac 485 for (try = 0; try < statp->retry; try++) {
5b757a51 486 for (unsigned ns_shift = 0; ns_shift < statp->nscount; ns_shift++)
438e8239 487 {
5b757a51
FW
488 /* The actual name server index. This implements
489 RES_ROTATE. */
490 unsigned int ns = ns_shift + ns_offset;
491 if (ns >= statp->nscount)
492 ns -= statp->nscount;
438e8239 493
1eb946b9 494 same_ns:
a1ffb40e 495 if (__glibc_unlikely (v_circuit)) {
b43b13ac
UD
496 /* Use VC; at most one attempt per server. */
497 try = statp->retry;
1eb946b9
UD
498 n = send_vc(statp, buf, buflen, buf2, buflen2,
499 &ans, &anssiz, &terrno,
ab09bf61
AS
500 ns, ansp, ansp2, nansp2, resplen2,
501 ansp2_malloced);
e685e07d
UD
502 if (n < 0)
503 return (-1);
d1bc2cbb
SL
504 /* See comment at the declaration of n. */
505 DIAG_PUSH_NEEDS_COMMENT;
506 DIAG_IGNORE_NEEDS_COMMENT (9, "-Wmaybe-uninitialized");
57912a71 507 if (n == 0 && (buf2 == NULL || *resplen2 == 0))
28f540f4 508 goto next_ns;
d1bc2cbb 509 DIAG_POP_NEEDS_COMMENT;
28f540f4 510 } else {
e685e07d 511 /* Use datagrams. */
1eb946b9
UD
512 n = send_dg(statp, buf, buflen, buf2, buflen2,
513 &ans, &anssiz, &terrno,
514 ns, &v_circuit, &gotsomewhere, ansp,
ab09bf61 515 ansp2, nansp2, resplen2, ansp2_malloced);
e685e07d
UD
516 if (n < 0)
517 return (-1);
57912a71 518 if (n == 0 && (buf2 == NULL || *resplen2 == 0))
b43b13ac 519 goto next_ns;
e685e07d 520 if (v_circuit)
1eb946b9 521 // XXX Check whether both requests failed or
b7da31a1 522 // XXX whether one has been answered successfully
28f540f4 523 goto same_ns;
e685e07d
UD
524 }
525
1eb946b9
UD
526 resplen = n;
527
28f540f4 528 /*
28f540f4
RM
529 * If we have temporarily opened a virtual circuit,
530 * or if we haven't been asked to keep a socket open,
531 * close the socket.
532 */
e685e07d
UD
533 if ((v_circuit && (statp->options & RES_USEVC) == 0) ||
534 (statp->options & RES_STAYOPEN) == 0) {
cb07f6f6 535 __res_iclose(statp, false);
28f540f4 536 }
b43b13ac
UD
537 return (resplen);
538 next_ns: ;
28f540f4
RM
539 } /*foreach ns*/
540 } /*foreach retry*/
cb07f6f6 541 __res_iclose(statp, false);
7ef90c15 542 if (!v_circuit) {
28f540f4 543 if (!gotsomewhere)
b43b13ac 544 __set_errno (ECONNREFUSED); /* no nameservers found */
28f540f4 545 else
b43b13ac 546 __set_errno (ETIMEDOUT); /* no answer obtained */
7ef90c15 547 } else
c4029823 548 __set_errno (terrno);
b43b13ac 549 return (-1);
28f540f4
RM
550}
551
352f4ff9
FW
552/* Common part of res_nsend and res_send. */
553static int
554context_send_common (struct resolv_context *ctx,
555 const unsigned char *buf, int buflen,
556 unsigned char *ans, int anssiz)
557{
558 if (ctx == NULL)
559 {
560 RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
561 return -1;
562 }
563 int result = __res_context_send (ctx, buf, buflen, NULL, 0, ans, anssiz,
564 NULL, NULL, NULL, NULL, NULL);
565 __resolv_context_put (ctx);
566 return result;
567}
568
0420d888 569int
352f4ff9
FW
570res_nsend (res_state statp, const unsigned char *buf, int buflen,
571 unsigned char *ans, int anssiz)
0420d888 572{
352f4ff9
FW
573 return context_send_common
574 (__resolv_context_get_override (statp), buf, buflen, ans, anssiz);
0420d888
UD
575}
576
ded60354
FW
577int
578res_send (const unsigned char *buf, int buflen, unsigned char *ans, int anssiz)
579{
352f4ff9
FW
580 return context_send_common
581 (__resolv_context_get (), buf, buflen, ans, anssiz);
ded60354
FW
582}
583
e685e07d
UD
584/* Private */
585
2212c142 586static struct sockaddr *
5b757a51 587get_nsaddr (res_state statp, unsigned int n)
2212c142 588{
5b757a51 589 assert (n < statp->nscount);
2212c142
AS
590
591 if (statp->nsaddr_list[n].sin_family == 0 && EXT(statp).nsaddrs[n] != NULL)
592 /* EXT(statp).nsaddrs[n] holds an address that is larger than
593 struct sockaddr, and user code did not update
594 statp->nsaddr_list[n]. */
595 return (struct sockaddr *) EXT(statp).nsaddrs[n];
596 else
597 /* User code updated statp->nsaddr_list[n], or statp->nsaddr_list[n]
598 has the same content as EXT(statp).nsaddrs[n]. */
599 return (struct sockaddr *) (void *) &statp->nsaddr_list[n];
600}
601
b66d837b
FW
602/* Close the resolver structure, assign zero to *RESPLEN2 if RESPLEN2
603 is not NULL, and return zero. */
604static int
605__attribute__ ((warn_unused_result))
606close_and_return_error (res_state statp, int *resplen2)
607{
608 __res_iclose(statp, false);
609 if (resplen2 != NULL)
610 *resplen2 = 0;
611 return 0;
612}
613
e9db92d3
CD
614/* The send_vc function is responsible for sending a DNS query over TCP
615 to the nameserver numbered NS from the res_state STATP i.e.
616 EXT(statp).nssocks[ns]. The function supports sending both IPv4 and
617 IPv6 queries at the same serially on the same socket.
618
619 Please note that for TCP there is no way to disable sending both
620 queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP
621 and sends the queries serially and waits for the result after each
12f1ae05 622 sent query. This implementation should be corrected to honour these
e9db92d3
CD
623 options.
624
625 Please also note that for TCP we send both queries over the same
626 socket one after another. This technically violates best practice
627 since the server is allowed to read the first query, respond, and
628 then close the socket (to service another client). If the server
629 does this, then the remaining second query in the socket data buffer
630 will cause the server to send the client an RST which will arrive
631 asynchronously and the client's OS will likely tear down the socket
632 receive buffer resulting in a potentially short read and lost
633 response data. This will force the client to retry the query again,
634 and this process may repeat until all servers and connection resets
635 are exhausted and then the query will fail. It's not known if this
636 happens with any frequency in real DNS server implementations. This
637 implementation should be corrected to use two sockets by default for
638 parallel queries.
639
640 The query stored in BUF of BUFLEN length is sent first followed by
641 the query stored in BUF2 of BUFLEN2 length. Queries are sent
642 serially on the same socket.
643
644 Answers to the query are stored firstly in *ANSP up to a max of
645 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
646 is non-NULL (to indicate that modifying the answer buffer is allowed)
647 then malloc is used to allocate a new response buffer and ANSCP and
648 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
649 are needed but ANSCP is NULL, then as much of the response as
650 possible is read into the buffer, but the results will be truncated.
651 When truncation happens because of a small answer buffer the DNS
652 packets header field TC will bet set to 1, indicating a truncated
653 message and the rest of the socket data will be read and discarded.
654
655 Answers to the query are stored secondly in *ANSP2 up to a max of
656 *ANSSIZP2 bytes, with the actual response length stored in
657 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
658 is non-NULL (required for a second query) then malloc is used to
659 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
660 size and *ANSP2_MALLOCED is set to 1.
661
662 The ANSP2_MALLOCED argument will eventually be removed as the
663 change in buffer pointer can be used to detect the buffer has
664 changed and that the caller should use free on the new buffer.
665
666 Note that the answers may arrive in any order from the server and
667 therefore the first and second answer buffers may not correspond to
668 the first and second queries.
669
670 It is not supported to call this function with a non-NULL ANSP2
671 but a NULL ANSCP. Put another way, you can call send_vc with a
672 single unmodifiable buffer or two modifiable buffers, but no other
673 combination is supported.
674
675 It is the caller's responsibility to free the malloc allocated
676 buffers by detecting that the pointers have changed from their
677 original values i.e. *ANSCP or *ANSP2 has changed.
678
679 If errors are encountered then *TERRNO is set to an appropriate
680 errno value and a zero result is returned for a recoverable error,
681 and a less-than zero result is returned for a non-recoverable error.
682
683 If no errors are encountered then *TERRNO is left unmodified and
684 a the length of the first response in bytes is returned. */
e685e07d
UD
685static int
686send_vc(res_state statp,
1eb946b9
UD
687 const u_char *buf, int buflen, const u_char *buf2, int buflen2,
688 u_char **ansp, int *anssizp,
689 int *terrno, int ns, u_char **anscp, u_char **ansp2, int *anssizp2,
ab09bf61 690 int *resplen2, int *ansp2_malloced)
e685e07d
UD
691{
692 const HEADER *hp = (HEADER *) buf;
1eb946b9 693 const HEADER *hp2 = (HEADER *) buf2;
e9db92d3 694 HEADER *anhp = (HEADER *) *ansp;
2212c142 695 struct sockaddr *nsap = get_nsaddr (statp, ns);
48e435cd
SL
696 int truncating, connreset, n;
697 /* On some architectures compiler might emit a warning indicating
698 'resplen' may be used uninitialized. However if buf2 == NULL
699 then this code won't be executed; if buf2 != NULL, then first
700 time round the loop recvresp1 and recvresp2 will be 0 so this
701 code won't be executed but "thisresplenp = &resplen;" followed
702 by "*thisresplenp = rlen;" will be executed so that subsequent
703 times round the loop resplen has been initialized. So this is
704 a false-positive.
705 */
48e435cd 706 DIAG_PUSH_NEEDS_COMMENT;
0cb9dcc8 707 DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
48e435cd 708 int resplen;
48e435cd 709 DIAG_POP_NEEDS_COMMENT;
1eb946b9 710 struct iovec iov[4];
e685e07d 711 u_short len;
1eb946b9 712 u_short len2;
e685e07d
UD
713 u_char *cp;
714
715 connreset = 0;
716 same_ns:
717 truncating = 0;
718
719 /* Are we still talking to whom we want to talk to? */
720 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
438e8239 721 struct sockaddr_in6 peer;
9cfe5381 722 socklen_t size = sizeof peer;
e685e07d
UD
723
724 if (getpeername(statp->_vcsock,
725 (struct sockaddr *)&peer, &size) < 0 ||
2212c142
AS
726 !sock_eq(&peer, (struct sockaddr_in6 *) nsap)) {
727 __res_iclose(statp, false);
e685e07d
UD
728 statp->_flags &= ~RES_F_VC;
729 }
730 }
731
732 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
733 if (statp->_vcsock >= 0)
cb07f6f6 734 __res_iclose(statp, false);
e685e07d 735
2f83a729
FW
736 statp->_vcsock = socket
737 (nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
e685e07d
UD
738 if (statp->_vcsock < 0) {
739 *terrno = errno;
b9bdfa7c
FW
740 if (resplen2 != NULL)
741 *resplen2 = 0;
e685e07d
UD
742 return (-1);
743 }
744 __set_errno (0);
2212c142
AS
745 if (connect(statp->_vcsock, nsap,
746 nsap->sa_family == AF_INET
9fc42dfd
UD
747 ? sizeof (struct sockaddr_in)
748 : sizeof (struct sockaddr_in6)) < 0) {
e685e07d 749 *terrno = errno;
b9bdfa7c 750 return close_and_return_error (statp, resplen2);
e685e07d
UD
751 }
752 statp->_flags |= RES_F_VC;
28f540f4 753 }
e685e07d
UD
754
755 /*
756 * Send length & message
757 */
1eb946b9 758 len = htons ((u_short) buflen);
25337753
UD
759 evConsIovec(&len, INT16SZ, &iov[0]);
760 evConsIovec((void*)buf, buflen, &iov[1]);
1eb946b9
UD
761 int niov = 2;
762 ssize_t explen = INT16SZ + buflen;
763 if (buf2 != NULL) {
764 len2 = htons ((u_short) buflen2);
765 evConsIovec(&len2, INT16SZ, &iov[2]);
766 evConsIovec((void*)buf2, buflen2, &iov[3]);
767 niov = 4;
768 explen += INT16SZ + buflen2;
769 }
770 if (TEMP_FAILURE_RETRY (writev(statp->_vcsock, iov, niov)) != explen) {
e685e07d 771 *terrno = errno;
b9bdfa7c 772 return close_and_return_error (statp, resplen2);
e685e07d
UD
773 }
774 /*
775 * Receive length & response
776 */
1eb946b9 777 int recvresp1 = 0;
e9db92d3
CD
778 /* Skip the second response if there is no second query.
779 To do that we mark the second response as received. */
1eb946b9 780 int recvresp2 = buf2 == NULL;
b7da31a1 781 uint16_t rlen16;
e39e6946
UD
782 read_len:
783 cp = (u_char *)&rlen16;
b7da31a1 784 len = sizeof(rlen16);
e39e6946 785 while ((n = TEMP_FAILURE_RETRY (read(statp->_vcsock, cp,
25337753 786 (int)len))) > 0) {
e685e07d
UD
787 cp += n;
788 if ((len -= n) <= 0)
789 break;
790 }
791 if (n <= 0) {
792 *terrno = errno;
e685e07d
UD
793 /*
794 * A long running process might get its TCP
795 * connection reset if the remote server was
796 * restarted. Requery the server instead of
797 * trying a new one. When there is only one
798 * server, this means that a query might work
799 * instead of failing. We only allow one reset
800 * per query to prevent looping.
801 */
b9bdfa7c
FW
802 if (*terrno == ECONNRESET && !connreset)
803 {
804 __res_iclose (statp, false);
805 connreset = 1;
806 goto same_ns;
807 }
808 return close_and_return_error (statp, resplen2);
e685e07d 809 }
b7da31a1 810 int rlen = ntohs (rlen16);
1eb946b9
UD
811
812 int *thisanssizp;
813 u_char **thisansp;
814 int *thisresplenp;
815 if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
e9db92d3
CD
816 /* We have not received any responses
817 yet or we only have one response to
818 receive. */
1eb946b9
UD
819 thisanssizp = anssizp;
820 thisansp = anscp ?: ansp;
821 assert (anscp != NULL || ansp2 == NULL);
822 thisresplenp = &resplen;
823 } else {
1eb946b9
UD
824 thisanssizp = anssizp2;
825 thisansp = ansp2;
826 thisresplenp = resplen2;
827 }
828 anhp = (HEADER *) *thisansp;
829
b7da31a1 830 *thisresplenp = rlen;
e9db92d3
CD
831 /* Is the answer buffer too small? */
832 if (*thisanssizp < rlen) {
833 /* If the current buffer is not the the static
834 user-supplied buffer then we can reallocate
835 it. */
836 if (thisansp != NULL && thisansp != ansp) {
837 /* Always allocate MAXPACKET, callers expect
838 this specific size. */
1eb946b9 839 u_char *newp = malloc (MAXPACKET);
b9bdfa7c
FW
840 if (newp == NULL)
841 {
842 *terrno = ENOMEM;
843 return close_and_return_error (statp, resplen2);
844 }
1eb946b9
UD
845 *thisanssizp = MAXPACKET;
846 *thisansp = newp;
ab09bf61
AS
847 if (thisansp == ansp2)
848 *ansp2_malloced = 1;
1eb946b9 849 anhp = (HEADER *) newp;
e9db92d3
CD
850 /* A uint16_t can't be larger than MAXPACKET
851 thus it's safe to allocate MAXPACKET but
852 read RLEN bytes instead. */
b7da31a1 853 len = rlen;
0420d888 854 } else {
0420d888 855 truncating = 1;
1eb946b9 856 len = *thisanssizp;
0420d888 857 }
e685e07d 858 } else
b7da31a1 859 len = rlen;
1eb946b9 860
a1ffb40e 861 if (__glibc_unlikely (len < HFIXEDSZ)) {
e685e07d
UD
862 /*
863 * Undersized message.
864 */
e685e07d 865 *terrno = EMSGSIZE;
b9bdfa7c 866 return close_and_return_error (statp, resplen2);
e685e07d 867 }
1eb946b9
UD
868
869 cp = *thisansp;
e685e07d
UD
870 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
871 cp += n;
872 len -= n;
873 }
a1ffb40e 874 if (__glibc_unlikely (n <= 0)) {
e685e07d 875 *terrno = errno;
b9bdfa7c 876 return close_and_return_error (statp, resplen2);
e685e07d 877 }
a1ffb40e 878 if (__glibc_unlikely (truncating)) {
e685e07d
UD
879 /*
880 * Flush rest of answer so connection stays in synch.
881 */
882 anhp->tc = 1;
b7da31a1 883 len = rlen - *thisanssizp;
e685e07d
UD
884 while (len != 0) {
885 char junk[PACKETSZ];
886
887 n = read(statp->_vcsock, junk,
888 (len > sizeof junk) ? sizeof junk : len);
889 if (n > 0)
890 len -= n;
891 else
892 break;
893 }
894 }
895 /*
c0c3f78a 896 * If the calling application has bailed out of
e685e07d
UD
897 * a previous call and failed to arrange to have
898 * the circuit closed or the server has got
899 * itself confused, then drop the packet and
900 * wait for the correct one.
901 */
1eb946b9 902 if ((recvresp1 || hp->id != anhp->id)
09fbb56a 903 && (recvresp2 || hp2->id != anhp->id))
e685e07d 904 goto read_len;
e685e07d 905
1eb946b9
UD
906 /* Mark which reply we received. */
907 if (recvresp1 == 0 && hp->id == anhp->id)
908 recvresp1 = 1;
909 else
910 recvresp2 = 1;
911 /* Repeat waiting if we have a second answer to arrive. */
912 if ((recvresp1 & recvresp2) == 0)
913 goto read_len;
914
e685e07d
UD
915 /*
916 * All is well, or the error is fatal. Signal that the
917 * next nameserver ought not be tried.
918 */
b7da31a1 919 return resplen;
28f540f4 920}
845dcb57 921
b43b13ac 922static int
44d20bca 923reopen (res_state statp, int *terrno, int ns)
e685e07d 924{
e685e07d 925 if (EXT(statp).nssocks[ns] == -1) {
2212c142 926 struct sockaddr *nsap = get_nsaddr (statp, ns);
ace4e23f 927 socklen_t slen;
44d20bca 928
438e8239 929 /* only try IPv6 if IPv6 NS and if not failed before */
ace4e23f 930 if (nsap->sa_family == AF_INET6 && !statp->ipv6_unavail) {
2f83a729
FW
931 EXT(statp).nssocks[ns] = socket
932 (PF_INET6,
933 SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
ae1ad3ae
UD
934 if (EXT(statp).nssocks[ns] < 0)
935 statp->ipv6_unavail = errno == EAFNOSUPPORT;
ace4e23f
UD
936 slen = sizeof (struct sockaddr_in6);
937 } else if (nsap->sa_family == AF_INET) {
2f83a729
FW
938 EXT(statp).nssocks[ns] = socket
939 (PF_INET,
940 SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
ace4e23f 941 slen = sizeof (struct sockaddr_in);
9744268c 942 }
e685e07d
UD
943 if (EXT(statp).nssocks[ns] < 0) {
944 *terrno = errno;
e685e07d
UD
945 return (-1);
946 }
ae1ad3ae 947
08504de7
FW
948 /* Enable full ICMP error reporting for this
949 socket. */
950 if (__res_enable_icmp (nsap->sa_family,
951 EXT (statp).nssocks[ns]) < 0)
952 {
953 int saved_errno = errno;
954 __res_iclose (statp, false);
955 __set_errno (saved_errno);
956 *terrno = saved_errno;
957 return -1;
958 }
959
e685e07d
UD
960 /*
961 * On a 4.3BSD+ machine (client and server,
962 * actually), sending to a nameserver datagram
963 * port with no nameserver will cause an
964 * ICMP port unreachable message to be returned.
965 * If our datagram socket is "connected" to the
966 * server, we get an ECONNREFUSED error on the next
967 * socket operation, and select returns if the
968 * error message is received. We can thus detect
969 * the absence of a nameserver without timing out.
970 */
93fe09cb
CD
971 /* With GCC 5.3 when compiling with -Os the compiler
972 emits a warning that slen may be used uninitialized,
973 but that is never true. Both slen and
974 EXT(statp).nssocks[ns] are initialized together or
975 the function return -1 before control flow reaches
976 the call to connect with slen. */
977 DIAG_PUSH_NEEDS_COMMENT;
0cb9dcc8 978 DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
ace4e23f 979 if (connect(EXT(statp).nssocks[ns], nsap, slen) < 0) {
93fe09cb 980 DIAG_POP_NEEDS_COMMENT;
cb07f6f6 981 __res_iclose(statp, false);
e685e07d
UD
982 return (0);
983 }
e685e07d 984 }
17a10319 985
44d20bca
UD
986 return 1;
987}
988
e9db92d3
CD
989/* The send_dg function is responsible for sending a DNS query over UDP
990 to the nameserver numbered NS from the res_state STATP i.e.
991 EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries
992 along with the ability to send the query in parallel for both stacks
993 (default) or serially (RES_SINGLKUP). It also supports serial lookup
994 with a close and reopen of the socket used to talk to the server
995 (RES_SNGLKUPREOP) to work around broken name servers.
996
997 The query stored in BUF of BUFLEN length is sent first followed by
998 the query stored in BUF2 of BUFLEN2 length. Queries are sent
999 in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP).
1000
1001 Answers to the query are stored firstly in *ANSP up to a max of
1002 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
1003 is non-NULL (to indicate that modifying the answer buffer is allowed)
1004 then malloc is used to allocate a new response buffer and ANSCP and
1005 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
1006 are needed but ANSCP is NULL, then as much of the response as
1007 possible is read into the buffer, but the results will be truncated.
1008 When truncation happens because of a small answer buffer the DNS
1009 packets header field TC will bet set to 1, indicating a truncated
1010 message, while the rest of the UDP packet is discarded.
1011
1012 Answers to the query are stored secondly in *ANSP2 up to a max of
1013 *ANSSIZP2 bytes, with the actual response length stored in
1014 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
1015 is non-NULL (required for a second query) then malloc is used to
1016 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
1017 size and *ANSP2_MALLOCED is set to 1.
1018
1019 The ANSP2_MALLOCED argument will eventually be removed as the
1020 change in buffer pointer can be used to detect the buffer has
1021 changed and that the caller should use free on the new buffer.
1022
1023 Note that the answers may arrive in any order from the server and
1024 therefore the first and second answer buffers may not correspond to
1025 the first and second queries.
1026
1027 It is not supported to call this function with a non-NULL ANSP2
1028 but a NULL ANSCP. Put another way, you can call send_vc with a
1029 single unmodifiable buffer or two modifiable buffers, but no other
1030 combination is supported.
1031
1032 It is the caller's responsibility to free the malloc allocated
1033 buffers by detecting that the pointers have changed from their
1034 original values i.e. *ANSCP or *ANSP2 has changed.
1035
1036 If an answer is truncated because of UDP datagram DNS limits then
1037 *V_CIRCUIT is set to 1 and the return value non-zero to indicate to
1038 the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1
1039 if any progress was made reading a response from the nameserver and
1040 is used by the caller to distinguish between ECONNREFUSED and
1041 ETIMEDOUT (the latter if *GOTSOMEWHERE is 1).
1042
1043 If errors are encountered then *TERRNO is set to an appropriate
1044 errno value and a zero result is returned for a recoverable error,
1045 and a less-than zero result is returned for a non-recoverable error.
1046
1047 If no errors are encountered then *TERRNO is left unmodified and
1048 a the length of the first response in bytes is returned. */
44d20bca
UD
1049static int
1050send_dg(res_state statp,
1051 const u_char *buf, int buflen, const u_char *buf2, int buflen2,
1052 u_char **ansp, int *anssizp,
1053 int *terrno, int ns, int *v_circuit, int *gotsomewhere, u_char **anscp,
ab09bf61 1054 u_char **ansp2, int *anssizp2, int *resplen2, int *ansp2_malloced)
44d20bca
UD
1055{
1056 const HEADER *hp = (HEADER *) buf;
1057 const HEADER *hp2 = (HEADER *) buf2;
44d20bca
UD
1058 struct timespec now, timeout, finish;
1059 struct pollfd pfd[1];
3a85895f 1060 int ptimeout;
44d20bca 1061 struct sockaddr_in6 from;
75ded9bc
UD
1062 int resplen = 0;
1063 int n;
44d20bca 1064
f433b06b
UD
1065 /*
1066 * Compute time for the total operation.
1067 */
ae061910 1068 int seconds = (statp->retrans << ns);
f433b06b
UD
1069 if (ns > 0)
1070 seconds /= statp->nscount;
1071 if (seconds <= 0)
1072 seconds = 1;
44d20bca 1073 bool single_request_reopen = (statp->options & RES_SNGLKUPREOP) != 0;
c030f70c
UD
1074 bool single_request = (((statp->options & RES_SNGLKUP) != 0)
1075 | single_request_reopen);
ae061910 1076 int save_gotsomewhere = *gotsomewhere;
44d20bca
UD
1077
1078 int retval;
1079 retry_reopen:
1080 retval = reopen (statp, terrno, ns);
1081 if (retval <= 0)
b66d837b
FW
1082 {
1083 if (resplen2 != NULL)
1084 *resplen2 = 0;
1085 return retval;
1086 }
ae061910 1087 retry:
f433b06b
UD
1088 evNowTime(&now);
1089 evConsTime(&timeout, seconds, 0);
1090 evAddTime(&finish, &now, &timeout);
1091 int need_recompute = 0;
17a10319 1092 int nwritten = 0;
1eb946b9 1093 int recvresp1 = 0;
e9db92d3
CD
1094 /* Skip the second response if there is no second query.
1095 To do that we mark the second response as received. */
1eb946b9 1096 int recvresp2 = buf2 == NULL;
17a10319
UD
1097 pfd[0].fd = EXT(statp).nssocks[ns];
1098 pfd[0].events = POLLOUT;
1099 wait:
1100 if (need_recompute) {
8e45b1ac 1101 recompute_resend:
17a10319
UD
1102 evNowTime(&now);
1103 if (evCmpTime(finish, now) <= 0) {
8e45b1ac 1104 poll_err_out:
b66d837b 1105 return close_and_return_error (statp, resplen2);
17a10319
UD
1106 }
1107 evSubTime(&timeout, &finish, &now);
ae061910 1108 need_recompute = 0;
17a10319 1109 }
3a85895f 1110 /* Convert struct timespec in milliseconds. */
f433b06b
UD
1111 ptimeout = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
1112
17a10319
UD
1113 n = 0;
1114 if (nwritten == 0)
1115 n = __poll (pfd, 1, 0);
a1ffb40e 1116 if (__glibc_unlikely (n == 0)) {
f433b06b
UD
1117 n = __poll (pfd, 1, ptimeout);
1118 need_recompute = 1;
1119 }
f433b06b 1120 if (n == 0) {
74b3cf22 1121 if (resplen > 1 && (recvresp1 || (buf2 != NULL && recvresp2)))
e2003883 1122 {
ae061910
UD
1123 /* There are quite a few broken name servers out
1124 there which don't handle two outstanding
1125 requests from the same source. There are also
1126 broken firewall settings. If we time out after
1127 having received one answer switch to the mode
1128 where we send the second request only once we
1129 have received the first answer. */
74b3cf22
UD
1130 if (!single_request)
1131 {
310647e9 1132 statp->options |= RES_SNGLKUP;
74b3cf22
UD
1133 single_request = true;
1134 *gotsomewhere = save_gotsomewhere;
1135 goto retry;
1136 }
44d20bca
UD
1137 else if (!single_request_reopen)
1138 {
1139 statp->options |= RES_SNGLKUPREOP;
1140 single_request_reopen = true;
1141 *gotsomewhere = save_gotsomewhere;
1142 __res_iclose (statp, false);
1143 goto retry_reopen;
1144 }
74b3cf22
UD
1145
1146 *resplen2 = 1;
1147 return resplen;
e2003883 1148 }
5908f779 1149
f433b06b 1150 *gotsomewhere = 1;
b66d837b
FW
1151 if (resplen2 != NULL)
1152 *resplen2 = 0;
1153 return 0;
f433b06b
UD
1154 }
1155 if (n < 0) {
8e45b1ac
UD
1156 if (errno == EINTR)
1157 goto recompute_resend;
1158
1159 goto poll_err_out;
f433b06b
UD
1160 }
1161 __set_errno (0);
17a10319 1162 if (pfd[0].revents & POLLOUT) {
c030f70c
UD
1163#ifndef __ASSUME_SENDMMSG
1164 static int have_sendmmsg;
1165#else
1166# define have_sendmmsg 1
1167#endif
1168 if (have_sendmmsg >= 0 && nwritten == 0 && buf2 != NULL
1169 && !single_request)
1170 {
583a27d5
FW
1171 struct iovec iov =
1172 { .iov_base = (void *) buf, .iov_len = buflen };
1173 struct iovec iov2 =
1174 { .iov_base = (void *) buf2, .iov_len = buflen2 };
1175 struct mmsghdr reqs[2] =
1176 {
1177 {
1178 .msg_hdr =
1179 {
1180 .msg_iov = &iov,
1181 .msg_iovlen = 1,
1182 },
1183 },
1184 {
1185 .msg_hdr =
1186 {
1187 .msg_iov = &iov2,
1188 .msg_iovlen = 1,
1189 }
1190 },
1191 };
c030f70c 1192
123be9de 1193 int ndg = __sendmmsg (pfd[0].fd, reqs, 2, MSG_NOSIGNAL);
a1ffb40e 1194 if (__glibc_likely (ndg == 2))
c030f70c 1195 {
966977f1
UD
1196 if (reqs[0].msg_len != buflen
1197 || reqs[1].msg_len != buflen2)
1198 goto fail_sendmmsg;
1eb946b9 1199
c030f70c
UD
1200 pfd[0].events = POLLIN;
1201 nwritten += 2;
1202 }
1203 else if (ndg == 1 && reqs[0].msg_len == buflen)
1204 goto just_one;
966977f1 1205 else if (ndg < 0 && (errno == EINTR || errno == EAGAIN))
c030f70c
UD
1206 goto recompute_resend;
1207 else
1208 {
1209#ifndef __ASSUME_SENDMMSG
a1ffb40e 1210 if (__glibc_unlikely (have_sendmmsg == 0))
c030f70c 1211 {
966977f1 1212 if (ndg < 0 && errno == ENOSYS)
c030f70c
UD
1213 {
1214 have_sendmmsg = -1;
1215 goto try_send;
1216 }
1217 have_sendmmsg = 1;
1218 }
1219#endif
1220
966977f1 1221 fail_sendmmsg:
b66d837b 1222 return close_and_return_error (statp, resplen2);
c030f70c
UD
1223 }
1224 }
1eb946b9 1225 else
c030f70c
UD
1226 {
1227 ssize_t sr;
1228#ifndef __ASSUME_SENDMMSG
1229 try_send:
1230#endif
1231 if (nwritten != 0)
1232 sr = send (pfd[0].fd, buf2, buflen2, MSG_NOSIGNAL);
1233 else
1234 sr = send (pfd[0].fd, buf, buflen, MSG_NOSIGNAL);
1235
8e6d1083 1236 if (sr != (nwritten != 0 ? buflen2 : buflen)) {
c030f70c
UD
1237 if (errno == EINTR || errno == EAGAIN)
1238 goto recompute_resend;
b66d837b 1239 return close_and_return_error (statp, resplen2);
c030f70c
UD
1240 }
1241 just_one:
1242 if (nwritten != 0 || buf2 == NULL || single_request)
1243 pfd[0].events = POLLIN;
1244 else
1245 pfd[0].events = POLLIN | POLLOUT;
1246 ++nwritten;
1247 }
17a10319 1248 goto wait;
8aeb5058 1249 } else if (pfd[0].revents & POLLIN) {
1eb946b9
UD
1250 int *thisanssizp;
1251 u_char **thisansp;
1252 int *thisresplenp;
1253
1254 if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
e9db92d3
CD
1255 /* We have not received any responses
1256 yet or we only have one response to
1257 receive. */
1eb946b9
UD
1258 thisanssizp = anssizp;
1259 thisansp = anscp ?: ansp;
1260 assert (anscp != NULL || ansp2 == NULL);
1261 thisresplenp = &resplen;
1262 } else {
1eb946b9
UD
1263 thisanssizp = anssizp2;
1264 thisansp = ansp2;
1265 thisresplenp = resplen2;
1266 }
1267
1268 if (*thisanssizp < MAXPACKET
e9db92d3
CD
1269 /* If the current buffer is not the the static
1270 user-supplied buffer then we can reallocate
1271 it. */
1272 && (thisansp != NULL && thisansp != ansp)
c4e42566 1273#ifdef FIONREAD
e9db92d3 1274 /* Is the size too small? */
1eb946b9 1275 && (ioctl (pfd[0].fd, FIONREAD, thisresplenp) < 0
c4e42566
RM
1276 || *thisanssizp < *thisresplenp)
1277#endif
1278 ) {
e9db92d3
CD
1279 /* Always allocate MAXPACKET, callers expect
1280 this specific size. */
1eb946b9
UD
1281 u_char *newp = malloc (MAXPACKET);
1282 if (newp != NULL) {
e9db92d3
CD
1283 *thisanssizp = MAXPACKET;
1284 *thisansp = newp;
ab09bf61
AS
1285 if (thisansp == ansp2)
1286 *ansp2_malloced = 1;
17a10319
UD
1287 }
1288 }
e9db92d3
CD
1289 /* We could end up with truncation if anscp was NULL
1290 (not allowed to change caller's buffer) and the
1291 response buffer size is too small. This isn't a
1292 reliable way to detect truncation because the ioctl
1293 may be an inaccurate report of the UDP message size.
1294 Therefore we use this only to issue debug output.
1295 To do truncation accurately with UDP we need
1296 MSG_TRUNC which is only available on Linux. We
1297 can abstract out the Linux-specific feature in the
1298 future to detect truncation. */
1eb946b9
UD
1299 HEADER *anhp = (HEADER *) *thisansp;
1300 socklen_t fromlen = sizeof(struct sockaddr_in6);
1301 assert (sizeof(from) <= fromlen);
1302 *thisresplenp = recvfrom(pfd[0].fd, (char*)*thisansp,
1303 *thisanssizp, 0,
1304 (struct sockaddr *)&from, &fromlen);
a1ffb40e 1305 if (__glibc_unlikely (*thisresplenp <= 0)) {
17a10319
UD
1306 if (errno == EINTR || errno == EAGAIN) {
1307 need_recompute = 1;
1308 goto wait;
1309 }
b66d837b 1310 return close_and_return_error (statp, resplen2);
17a10319 1311 }
e685e07d 1312 *gotsomewhere = 1;
a1ffb40e 1313 if (__glibc_unlikely (*thisresplenp < HFIXEDSZ)) {
17a10319
UD
1314 /*
1315 * Undersized message.
1316 */
17a10319 1317 *terrno = EMSGSIZE;
b66d837b 1318 return close_and_return_error (statp, resplen2);
17a10319 1319 }
1eb946b9
UD
1320 if ((recvresp1 || hp->id != anhp->id)
1321 && (recvresp2 || hp2->id != anhp->id)) {
17a10319
UD
1322 /*
1323 * response from old query, ignore it.
1324 * XXX - potential security hazard could
1325 * be detected here.
1326 */
f433b06b 1327 goto wait;
e685e07d 1328 }
33322186
FW
1329
1330 /* Paranoia check. Due to the connected UDP socket,
1331 the kernel has already filtered invalid addresses
1332 for us. */
1333 if (!res_ourserver_p(statp, &from))
1334 goto wait;
1335
1336 /* Check for the correct header layout and a matching
1337 question. */
1338 if ((recvresp1 || !res_queriesmatch(buf, buf + buflen,
1eb946b9
UD
1339 *thisansp,
1340 *thisansp
1341 + *thisanssizp))
1342 && (recvresp2 || !res_queriesmatch(buf2, buf2 + buflen2,
1343 *thisansp,
1344 *thisansp
33322186
FW
1345 + *thisanssizp)))
1346 goto wait;
1347
17a10319
UD
1348 if (anhp->rcode == SERVFAIL ||
1349 anhp->rcode == NOTIMP ||
1350 anhp->rcode == REFUSED) {
16b293a7 1351 next_ns:
4769ae77
UD
1352 if (recvresp1 || (buf2 != NULL && recvresp2)) {
1353 *resplen2 = 0;
e28b969b 1354 return resplen;
4769ae77 1355 }
e2003883
UD
1356 if (buf2 != NULL)
1357 {
4769ae77
UD
1358 /* No data from the first reply. */
1359 resplen = 0;
e2003883 1360 /* We are waiting for a possible second reply. */
e2003883
UD
1361 if (hp->id == anhp->id)
1362 recvresp1 = 1;
1363 else
1364 recvresp2 = 1;
1365
1366 goto wait;
1367 }
1368
17a10319
UD
1369 /* don't retry if called from dig */
1370 if (!statp->pfcode)
b66d837b
FW
1371 return close_and_return_error (statp, resplen2);
1372 __res_iclose(statp, false);
17a10319 1373 }
359bb2ef
UD
1374 if (anhp->rcode == NOERROR && anhp->ancount == 0
1375 && anhp->aa == 0 && anhp->ra == 0 && anhp->arcount == 0) {
359bb2ef
UD
1376 goto next_ns;
1377 }
17a10319
UD
1378 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1379 /*
1380 * To get the rest of answer,
1381 * use TCP with same server.
1382 */
17a10319 1383 *v_circuit = 1;
cb07f6f6 1384 __res_iclose(statp, false);
1eb946b9
UD
1385 // XXX if we have received one reply we could
1386 // XXX use it and not repeat it over TCP...
b66d837b
FW
1387 if (resplen2 != NULL)
1388 *resplen2 = 0;
17a10319
UD
1389 return (1);
1390 }
1eb946b9
UD
1391 /* Mark which reply we received. */
1392 if (recvresp1 == 0 && hp->id == anhp->id)
1393 recvresp1 = 1;
1394 else
1395 recvresp2 = 1;
1396 /* Repeat waiting if we have a second answer to arrive. */
ae061910 1397 if ((recvresp1 & recvresp2) == 0) {
c030f70c 1398 if (single_request) {
ae061910 1399 pfd[0].events = POLLOUT;
44d20bca
UD
1400 if (single_request_reopen) {
1401 __res_iclose (statp, false);
1402 retval = reopen (statp, terrno, ns);
1403 if (retval <= 0)
b66d837b
FW
1404 {
1405 if (resplen2 != NULL)
1406 *resplen2 = 0;
1407 return retval;
1408 }
f9d2d032 1409 pfd[0].fd = EXT(statp).nssocks[ns];
44d20bca
UD
1410 }
1411 }
1eb946b9 1412 goto wait;
ae061910 1413 }
b66d837b
FW
1414 /* All is well. We have received both responses (if
1415 two responses were requested). */
17a10319 1416 return (resplen);
b66d837b
FW
1417 } else if (pfd[0].revents & (POLLERR | POLLHUP | POLLNVAL))
1418 /* Something went wrong. We can stop trying. */
1419 return close_and_return_error (statp, resplen2);
9cfe5381 1420 else {
4769ae77 1421 /* poll should not have returned > 0 in this case. */
9cfe5381
RM
1422 abort ();
1423 }
e685e07d
UD
1424}
1425
e685e07d 1426static int
438e8239
UD
1427sock_eq(struct sockaddr_in6 *a1, struct sockaddr_in6 *a2) {
1428 if (a1->sin6_family == a2->sin6_family) {
1429 if (a1->sin6_family == AF_INET)
1430 return ((((struct sockaddr_in *)a1)->sin_port ==
1431 ((struct sockaddr_in *)a2)->sin_port) &&
1432 (((struct sockaddr_in *)a1)->sin_addr.s_addr ==
1433 ((struct sockaddr_in *)a2)->sin_addr.s_addr));
1434 else
1435 return ((a1->sin6_port == a2->sin6_port) &&
1436 !memcmp(&a1->sin6_addr, &a2->sin6_addr,
1437 sizeof (struct in6_addr)));
1438 }
1439 if (a1->sin6_family == AF_INET) {
1440 struct sockaddr_in6 *sap = a1;
1441 a1 = a2;
1442 a2 = sap;
1443 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1444 return ((a1->sin6_port == ((struct sockaddr_in *)a2)->sin_port) &&
1445 IN6_IS_ADDR_V4MAPPED(&a1->sin6_addr) &&
1446 (a1->sin6_addr.s6_addr32[3] ==
1447 ((struct sockaddr_in *)a2)->sin_addr.s_addr));
1448}