1 | /* Copyright (C) 2016 Free Software Foundation, Inc. |
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 |
16 | <http://www.gnu.org/licenses/>. */ |
17 | |
18 | /* |
19 | * Copyright (c) 1985, 1989, 1993 |
20 | * The Regents of the University of California. All rights reserved. |
21 | * |
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. |
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. |
33 | * |
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. |
45 | */ |
46 | |
47 | /* |
48 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
49 | * |
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. |
56 | * |
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. |
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. |
82 | */ |
83 | |
84 | #if defined(LIBC_SCCS) && !defined(lint) |
85 | static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93" ; |
86 | static const char rcsid[] = "$BINDId: res_send.c,v 8.38 2000/03/30 20:16:51 vixie Exp $" ; |
87 | #endif /* LIBC_SCCS and not lint */ |
88 | |
89 | /* |
90 | * Send query to name server and wait for reply. |
91 | */ |
92 | |
93 | #include <assert.h> |
94 | #include <sys/types.h> |
95 | #include <sys/param.h> |
96 | #include <sys/time.h> |
97 | #include <sys/socket.h> |
98 | #include <sys/uio.h> |
99 | #include <sys/poll.h> |
100 | |
101 | #include <netinet/in.h> |
102 | #include <arpa/nameser.h> |
103 | #include <arpa/inet.h> |
104 | #include <sys/ioctl.h> |
105 | |
106 | #include <errno.h> |
107 | #include <fcntl.h> |
108 | #include <netdb.h> |
109 | #include <resolv.h> |
110 | #include <signal.h> |
111 | #include <stdio.h> |
112 | #include <stdlib.h> |
113 | #include <string.h> |
114 | #include <unistd.h> |
115 | #include <kernel-features.h> |
116 | #include <libc-internal.h> |
117 | |
118 | #if PACKETSZ > 65536 |
119 | #define MAXPACKET PACKETSZ |
120 | #else |
121 | #define MAXPACKET 65536 |
122 | #endif |
123 | |
124 | /* From ev_streams.c. */ |
125 | |
126 | static inline void |
127 | __attribute ((always_inline)) |
128 | evConsIovec(void *buf, size_t cnt, struct iovec *vec) { |
129 | memset(vec, 0xf5, sizeof (*vec)); |
130 | vec->iov_base = buf; |
131 | vec->iov_len = cnt; |
132 | } |
133 | |
134 | /* From ev_timers.c. */ |
135 | |
136 | #define BILLION 1000000000 |
137 | |
138 | static inline void |
139 | evConsTime(struct timespec *res, time_t sec, long nsec) { |
140 | res->tv_sec = sec; |
141 | res->tv_nsec = nsec; |
142 | } |
143 | |
144 | static inline void |
145 | evAddTime(struct timespec *res, const struct timespec *addend1, |
146 | const struct timespec *addend2) { |
147 | res->tv_sec = addend1->tv_sec + addend2->tv_sec; |
148 | res->tv_nsec = addend1->tv_nsec + addend2->tv_nsec; |
149 | if (res->tv_nsec >= BILLION) { |
150 | res->tv_sec++; |
151 | res->tv_nsec -= BILLION; |
152 | } |
153 | } |
154 | |
155 | static inline void |
156 | evSubTime(struct timespec *res, const struct timespec *minuend, |
157 | const struct timespec *subtrahend) { |
158 | res->tv_sec = minuend->tv_sec - subtrahend->tv_sec; |
159 | if (minuend->tv_nsec >= subtrahend->tv_nsec) |
160 | res->tv_nsec = minuend->tv_nsec - subtrahend->tv_nsec; |
161 | else { |
162 | res->tv_nsec = (BILLION |
163 | - subtrahend->tv_nsec + minuend->tv_nsec); |
164 | res->tv_sec--; |
165 | } |
166 | } |
167 | |
168 | static int |
169 | evCmpTime(struct timespec a, struct timespec b) { |
170 | long x = a.tv_sec - b.tv_sec; |
171 | |
172 | if (x == 0L) |
173 | x = a.tv_nsec - b.tv_nsec; |
174 | return (x < 0L ? (-1) : x > 0L ? (1) : (0)); |
175 | } |
176 | |
177 | static void |
178 | evNowTime(struct timespec *res) { |
179 | struct timeval now; |
180 | |
181 | if (gettimeofday(&now, NULL) < 0) |
182 | evConsTime(res, 0, 0); |
183 | else |
184 | TIMEVAL_TO_TIMESPEC (&now, res); |
185 | } |
186 | |
187 | |
188 | /* Options. Leave them on. */ |
189 | /* #undef DEBUG */ |
190 | #include "res_debug.h" |
191 | |
192 | #define EXT(res) ((res)->_u._ext) |
193 | |
194 | /* Forward. */ |
195 | |
196 | static struct sockaddr *get_nsaddr (res_state, int); |
197 | static int send_vc(res_state, const u_char *, int, |
198 | const u_char *, int, |
199 | u_char **, int *, int *, int, u_char **, |
200 | u_char **, int *, int *, int *); |
201 | static int send_dg(res_state, const u_char *, int, |
202 | const u_char *, int, |
203 | u_char **, int *, int *, int, |
204 | int *, int *, u_char **, |
205 | u_char **, int *, int *, int *); |
206 | #ifdef DEBUG |
207 | static void Aerror(const res_state, FILE *, const char *, int, |
208 | const struct sockaddr *); |
209 | static void Perror(const res_state, FILE *, const char *, int); |
210 | #endif |
211 | static int sock_eq(struct sockaddr_in6 *, struct sockaddr_in6 *); |
212 | |
213 | /* Public. */ |
214 | |
215 | /* int |
216 | * res_isourserver(ina) |
217 | * looks up "ina" in _res.ns_addr_list[] |
218 | * returns: |
219 | * 0 : not found |
220 | * >0 : found |
221 | * author: |
222 | * paul vixie, 29may94 |
223 | */ |
224 | int |
225 | res_ourserver_p(const res_state statp, const struct sockaddr_in6 *inp) |
226 | { |
227 | int ns; |
228 | |
229 | if (inp->sin6_family == AF_INET) { |
230 | struct sockaddr_in *in4p = (struct sockaddr_in *) inp; |
231 | in_port_t port = in4p->sin_port; |
232 | in_addr_t addr = in4p->sin_addr.s_addr; |
233 | |
234 | for (ns = 0; ns < statp->nscount; ns++) { |
235 | const struct sockaddr_in *srv = |
236 | (struct sockaddr_in *) get_nsaddr (statp, ns); |
237 | |
238 | if ((srv->sin_family == AF_INET) && |
239 | (srv->sin_port == port) && |
240 | (srv->sin_addr.s_addr == INADDR_ANY || |
241 | srv->sin_addr.s_addr == addr)) |
242 | return (1); |
243 | } |
244 | } else if (inp->sin6_family == AF_INET6) { |
245 | for (ns = 0; ns < statp->nscount; ns++) { |
246 | const struct sockaddr_in6 *srv |
247 | = (struct sockaddr_in6 *) get_nsaddr (statp, ns); |
248 | if ((srv->sin6_family == AF_INET6) && |
249 | (srv->sin6_port == inp->sin6_port) && |
250 | !(memcmp(&srv->sin6_addr, &in6addr_any, |
251 | sizeof (struct in6_addr)) && |
252 | memcmp(&srv->sin6_addr, &inp->sin6_addr, |
253 | sizeof (struct in6_addr)))) |
254 | return (1); |
255 | } |
256 | } |
257 | return (0); |
258 | } |
259 | |
260 | /* int |
261 | * res_nameinquery(name, type, class, buf, eom) |
262 | * look for (name,type,class) in the query section of packet (buf,eom) |
263 | * requires: |
264 | * buf + HFIXEDSZ <= eom |
265 | * returns: |
266 | * -1 : format error |
267 | * 0 : not found |
268 | * >0 : found |
269 | * author: |
270 | * paul vixie, 29may94 |
271 | */ |
272 | int |
273 | res_nameinquery(const char *name, int type, int class, |
274 | const u_char *buf, const u_char *eom) |
275 | { |
276 | const u_char *cp = buf + HFIXEDSZ; |
277 | int qdcount = ntohs(((HEADER*)buf)->qdcount); |
278 | |
279 | while (qdcount-- > 0) { |
280 | char tname[MAXDNAME+1]; |
281 | int n, ttype, tclass; |
282 | |
283 | n = dn_expand(buf, eom, cp, tname, sizeof tname); |
284 | if (n < 0) |
285 | return (-1); |
286 | cp += n; |
287 | if (cp + 2 * INT16SZ > eom) |
288 | return (-1); |
289 | NS_GET16(ttype, cp); |
290 | NS_GET16(tclass, cp); |
291 | if (ttype == type && tclass == class && |
292 | ns_samename(tname, name) == 1) |
293 | return (1); |
294 | } |
295 | return (0); |
296 | } |
297 | libresolv_hidden_def (res_nameinquery) |
298 | |
299 | /* int |
300 | * res_queriesmatch(buf1, eom1, buf2, eom2) |
301 | * is there a 1:1 mapping of (name,type,class) |
302 | * in (buf1,eom1) and (buf2,eom2)? |
303 | * returns: |
304 | * -1 : format error |
305 | * 0 : not a 1:1 mapping |
306 | * >0 : is a 1:1 mapping |
307 | * author: |
308 | * paul vixie, 29may94 |
309 | */ |
310 | int |
311 | res_queriesmatch(const u_char *buf1, const u_char *eom1, |
312 | const u_char *buf2, const u_char *eom2) |
313 | { |
314 | if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) |
315 | return (-1); |
316 | |
317 | /* |
318 | * Only header section present in replies to |
319 | * dynamic update packets. |
320 | */ |
321 | if ((((HEADER *)buf1)->opcode == ns_o_update) && |
322 | (((HEADER *)buf2)->opcode == ns_o_update)) |
323 | return (1); |
324 | |
325 | /* Note that we initially do not convert QDCOUNT to the host byte |
326 | order. We can compare it with the second buffer's QDCOUNT |
327 | value without doing this. */ |
328 | int qdcount = ((HEADER*)buf1)->qdcount; |
329 | if (qdcount != ((HEADER*)buf2)->qdcount) |
330 | return (0); |
331 | |
332 | qdcount = htons (qdcount); |
333 | const u_char *cp = buf1 + HFIXEDSZ; |
334 | |
335 | while (qdcount-- > 0) { |
336 | char tname[MAXDNAME+1]; |
337 | int n, ttype, tclass; |
338 | |
339 | n = dn_expand(buf1, eom1, cp, tname, sizeof tname); |
340 | if (n < 0) |
341 | return (-1); |
342 | cp += n; |
343 | if (cp + 2 * INT16SZ > eom1) |
344 | return (-1); |
345 | NS_GET16(ttype, cp); |
346 | NS_GET16(tclass, cp); |
347 | if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) |
348 | return (0); |
349 | } |
350 | return (1); |
351 | } |
352 | libresolv_hidden_def (res_queriesmatch) |
353 | |
354 | int |
355 | __libc_res_nsend(res_state statp, const u_char *buf, int buflen, |
356 | const u_char *buf2, int buflen2, |
357 | u_char *ans, int anssiz, u_char **ansp, u_char **ansp2, |
358 | int *nansp2, int *resplen2, int *ansp2_malloced) |
359 | { |
360 | int gotsomewhere, terrno, try, v_circuit, resplen, ns, n; |
361 | |
362 | if (statp->nscount == 0) { |
363 | __set_errno (ESRCH); |
364 | return (-1); |
365 | } |
366 | |
367 | if (anssiz < (buf2 == NULL ? 1 : 2) * HFIXEDSZ) { |
368 | __set_errno (EINVAL); |
369 | return (-1); |
370 | } |
371 | |
372 | #ifdef USE_HOOKS |
373 | if (__glibc_unlikely (statp->qhook || statp->rhook)) { |
374 | if (anssiz < MAXPACKET && ansp) { |
375 | /* Always allocate MAXPACKET, callers expect |
376 | this specific size. */ |
377 | u_char *buf = malloc (MAXPACKET); |
378 | if (buf == NULL) |
379 | return (-1); |
380 | memcpy (buf, ans, HFIXEDSZ); |
381 | *ansp = buf; |
382 | ans = buf; |
383 | anssiz = MAXPACKET; |
384 | } |
385 | } |
386 | #endif |
387 | |
388 | DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY), |
389 | (stdout, ";; res_send()\n" ), buf, buflen); |
390 | v_circuit = ((statp->options & RES_USEVC) |
391 | || buflen > PACKETSZ |
392 | || buflen2 > PACKETSZ); |
393 | gotsomewhere = 0; |
394 | terrno = ETIMEDOUT; |
395 | |
396 | /* |
397 | * If the ns_addr_list in the resolver context has changed, then |
398 | * invalidate our cached copy and the associated timing data. |
399 | */ |
400 | if (EXT(statp).nscount != 0) { |
401 | int needclose = 0; |
402 | |
403 | if (EXT(statp).nscount != statp->nscount) |
404 | needclose++; |
405 | else |
406 | for (ns = 0; ns < statp->nscount; ns++) { |
407 | if (statp->nsaddr_list[ns].sin_family != 0 |
408 | && !sock_eq((struct sockaddr_in6 *) |
409 | &statp->nsaddr_list[ns], |
410 | EXT(statp).nsaddrs[ns])) |
411 | { |
412 | needclose++; |
413 | break; |
414 | } |
415 | } |
416 | if (needclose) { |
417 | __res_iclose(statp, false); |
418 | EXT(statp).nscount = 0; |
419 | } |
420 | } |
421 | |
422 | /* |
423 | * Maybe initialize our private copy of the ns_addr_list. |
424 | */ |
425 | if (EXT(statp).nscount == 0) { |
426 | for (ns = 0; ns < statp->nscount; ns++) { |
427 | EXT(statp).nssocks[ns] = -1; |
428 | if (statp->nsaddr_list[ns].sin_family == 0) |
429 | continue; |
430 | if (EXT(statp).nsaddrs[ns] == NULL) |
431 | EXT(statp).nsaddrs[ns] = |
432 | malloc(sizeof (struct sockaddr_in6)); |
433 | if (EXT(statp).nsaddrs[ns] != NULL) |
434 | memset (mempcpy(EXT(statp).nsaddrs[ns], |
435 | &statp->nsaddr_list[ns], |
436 | sizeof (struct sockaddr_in)), |
437 | '\0', |
438 | sizeof (struct sockaddr_in6) |
439 | - sizeof (struct sockaddr_in)); |
440 | } |
441 | EXT(statp).nscount = statp->nscount; |
442 | } |
443 | |
444 | /* |
445 | * Some resolvers want to even out the load on their nameservers. |
446 | * Note that RES_BLAST overrides RES_ROTATE. |
447 | */ |
448 | if (__builtin_expect ((statp->options & RES_ROTATE) != 0, 0) && |
449 | (statp->options & RES_BLAST) == 0) { |
450 | struct sockaddr_in ina; |
451 | struct sockaddr_in6 *inp; |
452 | int lastns = statp->nscount - 1; |
453 | int fd; |
454 | |
455 | inp = EXT(statp).nsaddrs[0]; |
456 | ina = statp->nsaddr_list[0]; |
457 | fd = EXT(statp).nssocks[0]; |
458 | for (ns = 0; ns < lastns; ns++) { |
459 | EXT(statp).nsaddrs[ns] = EXT(statp).nsaddrs[ns + 1]; |
460 | statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1]; |
461 | EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1]; |
462 | } |
463 | EXT(statp).nsaddrs[lastns] = inp; |
464 | statp->nsaddr_list[lastns] = ina; |
465 | EXT(statp).nssocks[lastns] = fd; |
466 | } |
467 | |
468 | /* |
469 | * Send request, RETRY times, or until successful. |
470 | */ |
471 | for (try = 0; try < statp->retry; try++) { |
472 | for (ns = 0; ns < statp->nscount; ns++) |
473 | { |
474 | #ifdef DEBUG |
475 | char tmpbuf[40]; |
476 | #endif |
477 | #if defined USE_HOOKS || defined DEBUG |
478 | struct sockaddr *nsap = get_nsaddr (statp, ns); |
479 | #endif |
480 | |
481 | same_ns: |
482 | #ifdef USE_HOOKS |
483 | if (__glibc_unlikely (statp->qhook != NULL)) { |
484 | int done = 0, loops = 0; |
485 | |
486 | do { |
487 | res_sendhookact act; |
488 | |
489 | struct sockaddr_in *nsap4; |
490 | nsap4 = (struct sockaddr_in *) nsap; |
491 | act = (*statp->qhook)(&nsap4, &buf, &buflen, |
492 | ans, anssiz, &resplen); |
493 | nsap = (struct sockaddr_in6 *) nsap4; |
494 | switch (act) { |
495 | case res_goahead: |
496 | done = 1; |
497 | break; |
498 | case res_nextns: |
499 | __res_iclose(statp, false); |
500 | goto next_ns; |
501 | case res_done: |
502 | return (resplen); |
503 | case res_modified: |
504 | /* give the hook another try */ |
505 | if (++loops < 42) /*doug adams*/ |
506 | break; |
507 | /*FALLTHROUGH*/ |
508 | case res_error: |
509 | /*FALLTHROUGH*/ |
510 | default: |
511 | return (-1); |
512 | } |
513 | } while (!done); |
514 | } |
515 | #endif |
516 | |
517 | Dprint(statp->options & RES_DEBUG, |
518 | (stdout, ";; Querying server (# %d) address = %s\n" , |
519 | ns + 1, inet_ntop(nsap->sa_family, |
520 | (nsap->sa_family == AF_INET6 |
521 | ? (void *) &((struct sockaddr_in6 *) nsap)->sin6_addr |
522 | : (void *) &((struct sockaddr_in *) nsap)->sin_addr), |
523 | tmpbuf, sizeof (tmpbuf)))); |
524 | |
525 | if (__glibc_unlikely (v_circuit)) { |
526 | /* Use VC; at most one attempt per server. */ |
527 | try = statp->retry; |
528 | n = send_vc(statp, buf, buflen, buf2, buflen2, |
529 | &ans, &anssiz, &terrno, |
530 | ns, ansp, ansp2, nansp2, resplen2, |
531 | ansp2_malloced); |
532 | if (n < 0) |
533 | return (-1); |
534 | if (n == 0 && (buf2 == NULL || *resplen2 == 0)) |
535 | goto next_ns; |
536 | } else { |
537 | /* Use datagrams. */ |
538 | n = send_dg(statp, buf, buflen, buf2, buflen2, |
539 | &ans, &anssiz, &terrno, |
540 | ns, &v_circuit, &gotsomewhere, ansp, |
541 | ansp2, nansp2, resplen2, ansp2_malloced); |
542 | if (n < 0) |
543 | return (-1); |
544 | if (n == 0 && (buf2 == NULL || *resplen2 == 0)) |
545 | goto next_ns; |
546 | if (v_circuit) |
547 | // XXX Check whether both requests failed or |
548 | // XXX whether one has been answered successfully |
549 | goto same_ns; |
550 | } |
551 | |
552 | resplen = n; |
553 | |
554 | Dprint((statp->options & RES_DEBUG) || |
555 | ((statp->pfcode & RES_PRF_REPLY) && |
556 | (statp->pfcode & RES_PRF_HEAD1)), |
557 | (stdout, ";; got answer:\n" )); |
558 | |
559 | DprintQ((statp->options & RES_DEBUG) || |
560 | (statp->pfcode & RES_PRF_REPLY), |
561 | (stdout, "%s" , "" ), |
562 | ans, (resplen > anssiz) ? anssiz : resplen); |
563 | if (buf2 != NULL) { |
564 | DprintQ((statp->options & RES_DEBUG) || |
565 | (statp->pfcode & RES_PRF_REPLY), |
566 | (stdout, "%s" , "" ), |
567 | *ansp2, (*resplen2 > *nansp2) ? *nansp2 : *resplen2); |
568 | } |
569 | |
570 | /* |
571 | * If we have temporarily opened a virtual circuit, |
572 | * or if we haven't been asked to keep a socket open, |
573 | * close the socket. |
574 | */ |
575 | if ((v_circuit && (statp->options & RES_USEVC) == 0) || |
576 | (statp->options & RES_STAYOPEN) == 0) { |
577 | __res_iclose(statp, false); |
578 | } |
579 | #ifdef USE_HOOKS |
580 | if (__glibc_unlikely (statp->rhook)) { |
581 | int done = 0, loops = 0; |
582 | |
583 | do { |
584 | res_sendhookact act; |
585 | |
586 | act = (*statp->rhook)((struct sockaddr_in *) |
587 | nsap, buf, buflen, |
588 | ans, anssiz, &resplen); |
589 | switch (act) { |
590 | case res_goahead: |
591 | case res_done: |
592 | done = 1; |
593 | break; |
594 | case res_nextns: |
595 | __res_iclose(statp, false); |
596 | goto next_ns; |
597 | case res_modified: |
598 | /* give the hook another try */ |
599 | if (++loops < 42) /*doug adams*/ |
600 | break; |
601 | /*FALLTHROUGH*/ |
602 | case res_error: |
603 | /*FALLTHROUGH*/ |
604 | default: |
605 | return (-1); |
606 | } |
607 | } while (!done); |
608 | |
609 | } |
610 | #endif |
611 | return (resplen); |
612 | next_ns: ; |
613 | } /*foreach ns*/ |
614 | } /*foreach retry*/ |
615 | __res_iclose(statp, false); |
616 | if (!v_circuit) { |
617 | if (!gotsomewhere) |
618 | __set_errno (ECONNREFUSED); /* no nameservers found */ |
619 | else |
620 | __set_errno (ETIMEDOUT); /* no answer obtained */ |
621 | } else |
622 | __set_errno (terrno); |
623 | return (-1); |
624 | } |
625 | |
626 | int |
627 | res_nsend(res_state statp, |
628 | const u_char *buf, int buflen, u_char *ans, int anssiz) |
629 | { |
630 | return __libc_res_nsend(statp, buf, buflen, NULL, 0, ans, anssiz, |
631 | NULL, NULL, NULL, NULL, NULL); |
632 | } |
633 | libresolv_hidden_def (res_nsend) |
634 | |
635 | /* Private */ |
636 | |
637 | static struct sockaddr * |
638 | get_nsaddr (res_state statp, int n) |
639 | { |
640 | |
641 | if (statp->nsaddr_list[n].sin_family == 0 && EXT(statp).nsaddrs[n] != NULL) |
642 | /* EXT(statp).nsaddrs[n] holds an address that is larger than |
643 | struct sockaddr, and user code did not update |
644 | statp->nsaddr_list[n]. */ |
645 | return (struct sockaddr *) EXT(statp).nsaddrs[n]; |
646 | else |
647 | /* User code updated statp->nsaddr_list[n], or statp->nsaddr_list[n] |
648 | has the same content as EXT(statp).nsaddrs[n]. */ |
649 | return (struct sockaddr *) (void *) &statp->nsaddr_list[n]; |
650 | } |
651 | |
652 | /* The send_vc function is responsible for sending a DNS query over TCP |
653 | to the nameserver numbered NS from the res_state STATP i.e. |
654 | EXT(statp).nssocks[ns]. The function supports sending both IPv4 and |
655 | IPv6 queries at the same serially on the same socket. |
656 | |
657 | Please note that for TCP there is no way to disable sending both |
658 | queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP |
659 | and sends the queries serially and waits for the result after each |
660 | sent query. This implemetnation should be corrected to honour these |
661 | options. |
662 | |
663 | Please also note that for TCP we send both queries over the same |
664 | socket one after another. This technically violates best practice |
665 | since the server is allowed to read the first query, respond, and |
666 | then close the socket (to service another client). If the server |
667 | does this, then the remaining second query in the socket data buffer |
668 | will cause the server to send the client an RST which will arrive |
669 | asynchronously and the client's OS will likely tear down the socket |
670 | receive buffer resulting in a potentially short read and lost |
671 | response data. This will force the client to retry the query again, |
672 | and this process may repeat until all servers and connection resets |
673 | are exhausted and then the query will fail. It's not known if this |
674 | happens with any frequency in real DNS server implementations. This |
675 | implementation should be corrected to use two sockets by default for |
676 | parallel queries. |
677 | |
678 | The query stored in BUF of BUFLEN length is sent first followed by |
679 | the query stored in BUF2 of BUFLEN2 length. Queries are sent |
680 | serially on the same socket. |
681 | |
682 | Answers to the query are stored firstly in *ANSP up to a max of |
683 | *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP |
684 | is non-NULL (to indicate that modifying the answer buffer is allowed) |
685 | then malloc is used to allocate a new response buffer and ANSCP and |
686 | ANSP will both point to the new buffer. If more than *ANSSIZP bytes |
687 | are needed but ANSCP is NULL, then as much of the response as |
688 | possible is read into the buffer, but the results will be truncated. |
689 | When truncation happens because of a small answer buffer the DNS |
690 | packets header field TC will bet set to 1, indicating a truncated |
691 | message and the rest of the socket data will be read and discarded. |
692 | |
693 | Answers to the query are stored secondly in *ANSP2 up to a max of |
694 | *ANSSIZP2 bytes, with the actual response length stored in |
695 | *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2 |
696 | is non-NULL (required for a second query) then malloc is used to |
697 | allocate a new response buffer, *ANSSIZP2 is set to the new buffer |
698 | size and *ANSP2_MALLOCED is set to 1. |
699 | |
700 | The ANSP2_MALLOCED argument will eventually be removed as the |
701 | change in buffer pointer can be used to detect the buffer has |
702 | changed and that the caller should use free on the new buffer. |
703 | |
704 | Note that the answers may arrive in any order from the server and |
705 | therefore the first and second answer buffers may not correspond to |
706 | the first and second queries. |
707 | |
708 | It is not supported to call this function with a non-NULL ANSP2 |
709 | but a NULL ANSCP. Put another way, you can call send_vc with a |
710 | single unmodifiable buffer or two modifiable buffers, but no other |
711 | combination is supported. |
712 | |
713 | It is the caller's responsibility to free the malloc allocated |
714 | buffers by detecting that the pointers have changed from their |
715 | original values i.e. *ANSCP or *ANSP2 has changed. |
716 | |
717 | If errors are encountered then *TERRNO is set to an appropriate |
718 | errno value and a zero result is returned for a recoverable error, |
719 | and a less-than zero result is returned for a non-recoverable error. |
720 | |
721 | If no errors are encountered then *TERRNO is left unmodified and |
722 | a the length of the first response in bytes is returned. */ |
723 | static int |
724 | send_vc(res_state statp, |
725 | const u_char *buf, int buflen, const u_char *buf2, int buflen2, |
726 | u_char **ansp, int *anssizp, |
727 | int *terrno, int ns, u_char **anscp, u_char **ansp2, int *anssizp2, |
728 | int *resplen2, int *ansp2_malloced) |
729 | { |
730 | const HEADER *hp = (HEADER *) buf; |
731 | const HEADER *hp2 = (HEADER *) buf2; |
732 | HEADER *anhp = (HEADER *) *ansp; |
733 | struct sockaddr *nsap = get_nsaddr (statp, ns); |
734 | int truncating, connreset, n; |
735 | /* On some architectures compiler might emit a warning indicating |
736 | 'resplen' may be used uninitialized. However if buf2 == NULL |
737 | then this code won't be executed; if buf2 != NULL, then first |
738 | time round the loop recvresp1 and recvresp2 will be 0 so this |
739 | code won't be executed but "thisresplenp = &resplen;" followed |
740 | by "*thisresplenp = rlen;" will be executed so that subsequent |
741 | times round the loop resplen has been initialized. So this is |
742 | a false-positive. |
743 | */ |
744 | DIAG_PUSH_NEEDS_COMMENT; |
745 | DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized" ); |
746 | int resplen; |
747 | DIAG_POP_NEEDS_COMMENT; |
748 | struct iovec iov[4]; |
749 | u_short len; |
750 | u_short len2; |
751 | u_char *cp; |
752 | |
753 | if (resplen2 != NULL) |
754 | *resplen2 = 0; |
755 | connreset = 0; |
756 | same_ns: |
757 | truncating = 0; |
758 | |
759 | /* Are we still talking to whom we want to talk to? */ |
760 | if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) { |
761 | struct sockaddr_in6 peer; |
762 | socklen_t size = sizeof peer; |
763 | |
764 | if (getpeername(statp->_vcsock, |
765 | (struct sockaddr *)&peer, &size) < 0 || |
766 | !sock_eq(&peer, (struct sockaddr_in6 *) nsap)) { |
767 | __res_iclose(statp, false); |
768 | statp->_flags &= ~RES_F_VC; |
769 | } |
770 | } |
771 | |
772 | if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) { |
773 | if (statp->_vcsock >= 0) |
774 | __res_iclose(statp, false); |
775 | |
776 | statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0); |
777 | if (statp->_vcsock < 0) { |
778 | *terrno = errno; |
779 | Perror(statp, stderr, "socket(vc)" , errno); |
780 | return (-1); |
781 | } |
782 | __set_errno (0); |
783 | if (connect(statp->_vcsock, nsap, |
784 | nsap->sa_family == AF_INET |
785 | ? sizeof (struct sockaddr_in) |
786 | : sizeof (struct sockaddr_in6)) < 0) { |
787 | *terrno = errno; |
788 | Aerror(statp, stderr, "connect/vc" , errno, nsap); |
789 | __res_iclose(statp, false); |
790 | return (0); |
791 | } |
792 | statp->_flags |= RES_F_VC; |
793 | } |
794 | |
795 | /* |
796 | * Send length & message |
797 | */ |
798 | len = htons ((u_short) buflen); |
799 | evConsIovec(&len, INT16SZ, &iov[0]); |
800 | evConsIovec((void*)buf, buflen, &iov[1]); |
801 | int niov = 2; |
802 | ssize_t explen = INT16SZ + buflen; |
803 | if (buf2 != NULL) { |
804 | len2 = htons ((u_short) buflen2); |
805 | evConsIovec(&len2, INT16SZ, &iov[2]); |
806 | evConsIovec((void*)buf2, buflen2, &iov[3]); |
807 | niov = 4; |
808 | explen += INT16SZ + buflen2; |
809 | } |
810 | if (TEMP_FAILURE_RETRY (writev(statp->_vcsock, iov, niov)) != explen) { |
811 | *terrno = errno; |
812 | Perror(statp, stderr, "write failed" , errno); |
813 | __res_iclose(statp, false); |
814 | return (0); |
815 | } |
816 | /* |
817 | * Receive length & response |
818 | */ |
819 | int recvresp1 = 0; |
820 | /* Skip the second response if there is no second query. |
821 | To do that we mark the second response as received. */ |
822 | int recvresp2 = buf2 == NULL; |
823 | uint16_t rlen16; |
824 | read_len: |
825 | cp = (u_char *)&rlen16; |
826 | len = sizeof(rlen16); |
827 | while ((n = TEMP_FAILURE_RETRY (read(statp->_vcsock, cp, |
828 | (int)len))) > 0) { |
829 | cp += n; |
830 | if ((len -= n) <= 0) |
831 | break; |
832 | } |
833 | if (n <= 0) { |
834 | *terrno = errno; |
835 | Perror(statp, stderr, "read failed" , errno); |
836 | __res_iclose(statp, false); |
837 | /* |
838 | * A long running process might get its TCP |
839 | * connection reset if the remote server was |
840 | * restarted. Requery the server instead of |
841 | * trying a new one. When there is only one |
842 | * server, this means that a query might work |
843 | * instead of failing. We only allow one reset |
844 | * per query to prevent looping. |
845 | */ |
846 | if (*terrno == ECONNRESET && !connreset) { |
847 | connreset = 1; |
848 | goto same_ns; |
849 | } |
850 | return (0); |
851 | } |
852 | int rlen = ntohs (rlen16); |
853 | |
854 | int *thisanssizp; |
855 | u_char **thisansp; |
856 | int *thisresplenp; |
857 | if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) { |
858 | /* We have not received any responses |
859 | yet or we only have one response to |
860 | receive. */ |
861 | thisanssizp = anssizp; |
862 | thisansp = anscp ?: ansp; |
863 | assert (anscp != NULL || ansp2 == NULL); |
864 | thisresplenp = &resplen; |
865 | } else { |
866 | thisanssizp = anssizp2; |
867 | thisansp = ansp2; |
868 | thisresplenp = resplen2; |
869 | } |
870 | anhp = (HEADER *) *thisansp; |
871 | |
872 | *thisresplenp = rlen; |
873 | /* Is the answer buffer too small? */ |
874 | if (*thisanssizp < rlen) { |
875 | /* If the current buffer is not the the static |
876 | user-supplied buffer then we can reallocate |
877 | it. */ |
878 | if (thisansp != NULL && thisansp != ansp) { |
879 | /* Always allocate MAXPACKET, callers expect |
880 | this specific size. */ |
881 | u_char *newp = malloc (MAXPACKET); |
882 | if (newp == NULL) { |
883 | *terrno = ENOMEM; |
884 | __res_iclose(statp, false); |
885 | return (0); |
886 | } |
887 | *thisanssizp = MAXPACKET; |
888 | *thisansp = newp; |
889 | if (thisansp == ansp2) |
890 | *ansp2_malloced = 1; |
891 | anhp = (HEADER *) newp; |
892 | /* A uint16_t can't be larger than MAXPACKET |
893 | thus it's safe to allocate MAXPACKET but |
894 | read RLEN bytes instead. */ |
895 | len = rlen; |
896 | } else { |
897 | Dprint(statp->options & RES_DEBUG, |
898 | (stdout, ";; response truncated\n" ) |
899 | ); |
900 | truncating = 1; |
901 | len = *thisanssizp; |
902 | } |
903 | } else |
904 | len = rlen; |
905 | |
906 | if (__glibc_unlikely (len < HFIXEDSZ)) { |
907 | /* |
908 | * Undersized message. |
909 | */ |
910 | Dprint(statp->options & RES_DEBUG, |
911 | (stdout, ";; undersized: %d\n" , len)); |
912 | *terrno = EMSGSIZE; |
913 | __res_iclose(statp, false); |
914 | return (0); |
915 | } |
916 | |
917 | cp = *thisansp; |
918 | while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){ |
919 | cp += n; |
920 | len -= n; |
921 | } |
922 | if (__glibc_unlikely (n <= 0)) { |
923 | *terrno = errno; |
924 | Perror(statp, stderr, "read(vc)" , errno); |
925 | __res_iclose(statp, false); |
926 | return (0); |
927 | } |
928 | if (__glibc_unlikely (truncating)) { |
929 | /* |
930 | * Flush rest of answer so connection stays in synch. |
931 | */ |
932 | anhp->tc = 1; |
933 | len = rlen - *thisanssizp; |
934 | while (len != 0) { |
935 | char junk[PACKETSZ]; |
936 | |
937 | n = read(statp->_vcsock, junk, |
938 | (len > sizeof junk) ? sizeof junk : len); |
939 | if (n > 0) |
940 | len -= n; |
941 | else |
942 | break; |
943 | } |
944 | } |
945 | /* |
946 | * If the calling application has bailed out of |
947 | * a previous call and failed to arrange to have |
948 | * the circuit closed or the server has got |
949 | * itself confused, then drop the packet and |
950 | * wait for the correct one. |
951 | */ |
952 | if ((recvresp1 || hp->id != anhp->id) |
953 | && (recvresp2 || hp2->id != anhp->id)) { |
954 | DprintQ((statp->options & RES_DEBUG) || |
955 | (statp->pfcode & RES_PRF_REPLY), |
956 | (stdout, ";; old answer (unexpected):\n" ), |
957 | *thisansp, |
958 | (rlen > *thisanssizp) ? *thisanssizp: rlen); |
959 | goto read_len; |
960 | } |
961 | |
962 | /* Mark which reply we received. */ |
963 | if (recvresp1 == 0 && hp->id == anhp->id) |
964 | recvresp1 = 1; |
965 | else |
966 | recvresp2 = 1; |
967 | /* Repeat waiting if we have a second answer to arrive. */ |
968 | if ((recvresp1 & recvresp2) == 0) |
969 | goto read_len; |
970 | |
971 | /* |
972 | * All is well, or the error is fatal. Signal that the |
973 | * next nameserver ought not be tried. |
974 | */ |
975 | return resplen; |
976 | } |
977 | |
978 | static int |
979 | reopen (res_state statp, int *terrno, int ns) |
980 | { |
981 | if (EXT(statp).nssocks[ns] == -1) { |
982 | struct sockaddr *nsap = get_nsaddr (statp, ns); |
983 | socklen_t slen; |
984 | |
985 | /* only try IPv6 if IPv6 NS and if not failed before */ |
986 | if (nsap->sa_family == AF_INET6 && !statp->ipv6_unavail) { |
987 | EXT(statp).nssocks[ns] |
988 | = socket(PF_INET6, SOCK_DGRAM|SOCK_NONBLOCK, 0); |
989 | if (EXT(statp).nssocks[ns] < 0) |
990 | statp->ipv6_unavail = errno == EAFNOSUPPORT; |
991 | slen = sizeof (struct sockaddr_in6); |
992 | } else if (nsap->sa_family == AF_INET) { |
993 | EXT(statp).nssocks[ns] |
994 | = socket(PF_INET, SOCK_DGRAM|SOCK_NONBLOCK, 0); |
995 | slen = sizeof (struct sockaddr_in); |
996 | } |
997 | if (EXT(statp).nssocks[ns] < 0) { |
998 | *terrno = errno; |
999 | Perror(statp, stderr, "socket(dg)" , errno); |
1000 | return (-1); |
1001 | } |
1002 | |
1003 | /* |
1004 | * On a 4.3BSD+ machine (client and server, |
1005 | * actually), sending to a nameserver datagram |
1006 | * port with no nameserver will cause an |
1007 | * ICMP port unreachable message to be returned. |
1008 | * If our datagram socket is "connected" to the |
1009 | * server, we get an ECONNREFUSED error on the next |
1010 | * socket operation, and select returns if the |
1011 | * error message is received. We can thus detect |
1012 | * the absence of a nameserver without timing out. |
1013 | */ |
1014 | if (connect(EXT(statp).nssocks[ns], nsap, slen) < 0) { |
1015 | Aerror(statp, stderr, "connect(dg)" , errno, nsap); |
1016 | __res_iclose(statp, false); |
1017 | return (0); |
1018 | } |
1019 | } |
1020 | |
1021 | return 1; |
1022 | } |
1023 | |
1024 | /* The send_dg function is responsible for sending a DNS query over UDP |
1025 | to the nameserver numbered NS from the res_state STATP i.e. |
1026 | EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries |
1027 | along with the ability to send the query in parallel for both stacks |
1028 | (default) or serially (RES_SINGLKUP). It also supports serial lookup |
1029 | with a close and reopen of the socket used to talk to the server |
1030 | (RES_SNGLKUPREOP) to work around broken name servers. |
1031 | |
1032 | The query stored in BUF of BUFLEN length is sent first followed by |
1033 | the query stored in BUF2 of BUFLEN2 length. Queries are sent |
1034 | in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP). |
1035 | |
1036 | Answers to the query are stored firstly in *ANSP up to a max of |
1037 | *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP |
1038 | is non-NULL (to indicate that modifying the answer buffer is allowed) |
1039 | then malloc is used to allocate a new response buffer and ANSCP and |
1040 | ANSP will both point to the new buffer. If more than *ANSSIZP bytes |
1041 | are needed but ANSCP is NULL, then as much of the response as |
1042 | possible is read into the buffer, but the results will be truncated. |
1043 | When truncation happens because of a small answer buffer the DNS |
1044 | packets header field TC will bet set to 1, indicating a truncated |
1045 | message, while the rest of the UDP packet is discarded. |
1046 | |
1047 | Answers to the query are stored secondly in *ANSP2 up to a max of |
1048 | *ANSSIZP2 bytes, with the actual response length stored in |
1049 | *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2 |
1050 | is non-NULL (required for a second query) then malloc is used to |
1051 | allocate a new response buffer, *ANSSIZP2 is set to the new buffer |
1052 | size and *ANSP2_MALLOCED is set to 1. |
1053 | |
1054 | The ANSP2_MALLOCED argument will eventually be removed as the |
1055 | change in buffer pointer can be used to detect the buffer has |
1056 | changed and that the caller should use free on the new buffer. |
1057 | |
1058 | Note that the answers may arrive in any order from the server and |
1059 | therefore the first and second answer buffers may not correspond to |
1060 | the first and second queries. |
1061 | |
1062 | It is not supported to call this function with a non-NULL ANSP2 |
1063 | but a NULL ANSCP. Put another way, you can call send_vc with a |
1064 | single unmodifiable buffer or two modifiable buffers, but no other |
1065 | combination is supported. |
1066 | |
1067 | It is the caller's responsibility to free the malloc allocated |
1068 | buffers by detecting that the pointers have changed from their |
1069 | original values i.e. *ANSCP or *ANSP2 has changed. |
1070 | |
1071 | If an answer is truncated because of UDP datagram DNS limits then |
1072 | *V_CIRCUIT is set to 1 and the return value non-zero to indicate to |
1073 | the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1 |
1074 | if any progress was made reading a response from the nameserver and |
1075 | is used by the caller to distinguish between ECONNREFUSED and |
1076 | ETIMEDOUT (the latter if *GOTSOMEWHERE is 1). |
1077 | |
1078 | If errors are encountered then *TERRNO is set to an appropriate |
1079 | errno value and a zero result is returned for a recoverable error, |
1080 | and a less-than zero result is returned for a non-recoverable error. |
1081 | |
1082 | If no errors are encountered then *TERRNO is left unmodified and |
1083 | a the length of the first response in bytes is returned. */ |
1084 | static int |
1085 | send_dg(res_state statp, |
1086 | const u_char *buf, int buflen, const u_char *buf2, int buflen2, |
1087 | u_char **ansp, int *anssizp, |
1088 | int *terrno, int ns, int *v_circuit, int *gotsomewhere, u_char **anscp, |
1089 | u_char **ansp2, int *anssizp2, int *resplen2, int *ansp2_malloced) |
1090 | { |
1091 | const HEADER *hp = (HEADER *) buf; |
1092 | const HEADER *hp2 = (HEADER *) buf2; |
1093 | struct timespec now, timeout, finish; |
1094 | struct pollfd pfd[1]; |
1095 | int ptimeout; |
1096 | struct sockaddr_in6 from; |
1097 | int resplen = 0; |
1098 | int n; |
1099 | |
1100 | /* |
1101 | * Compute time for the total operation. |
1102 | */ |
1103 | int seconds = (statp->retrans << ns); |
1104 | if (ns > 0) |
1105 | seconds /= statp->nscount; |
1106 | if (seconds <= 0) |
1107 | seconds = 1; |
1108 | bool single_request_reopen = (statp->options & RES_SNGLKUPREOP) != 0; |
1109 | bool single_request = (((statp->options & RES_SNGLKUP) != 0) |
1110 | | single_request_reopen); |
1111 | int save_gotsomewhere = *gotsomewhere; |
1112 | |
1113 | int retval; |
1114 | retry_reopen: |
1115 | retval = reopen (statp, terrno, ns); |
1116 | if (retval <= 0) |
1117 | return retval; |
1118 | retry: |
1119 | evNowTime(&now); |
1120 | evConsTime(&timeout, seconds, 0); |
1121 | evAddTime(&finish, &now, &timeout); |
1122 | int need_recompute = 0; |
1123 | int nwritten = 0; |
1124 | int recvresp1 = 0; |
1125 | /* Skip the second response if there is no second query. |
1126 | To do that we mark the second response as received. */ |
1127 | int recvresp2 = buf2 == NULL; |
1128 | pfd[0].fd = EXT(statp).nssocks[ns]; |
1129 | pfd[0].events = POLLOUT; |
1130 | if (resplen2 != NULL) |
1131 | *resplen2 = 0; |
1132 | wait: |
1133 | if (need_recompute) { |
1134 | recompute_resend: |
1135 | evNowTime(&now); |
1136 | if (evCmpTime(finish, now) <= 0) { |
1137 | poll_err_out: |
1138 | Perror(statp, stderr, "poll" , errno); |
1139 | err_out: |
1140 | __res_iclose(statp, false); |
1141 | return (0); |
1142 | } |
1143 | evSubTime(&timeout, &finish, &now); |
1144 | need_recompute = 0; |
1145 | } |
1146 | /* Convert struct timespec in milliseconds. */ |
1147 | ptimeout = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000; |
1148 | |
1149 | n = 0; |
1150 | if (nwritten == 0) |
1151 | n = __poll (pfd, 1, 0); |
1152 | if (__glibc_unlikely (n == 0)) { |
1153 | n = __poll (pfd, 1, ptimeout); |
1154 | need_recompute = 1; |
1155 | } |
1156 | if (n == 0) { |
1157 | Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n" )); |
1158 | if (resplen > 1 && (recvresp1 || (buf2 != NULL && recvresp2))) |
1159 | { |
1160 | /* There are quite a few broken name servers out |
1161 | there which don't handle two outstanding |
1162 | requests from the same source. There are also |
1163 | broken firewall settings. If we time out after |
1164 | having received one answer switch to the mode |
1165 | where we send the second request only once we |
1166 | have received the first answer. */ |
1167 | if (!single_request) |
1168 | { |
1169 | statp->options |= RES_SNGLKUP; |
1170 | single_request = true; |
1171 | *gotsomewhere = save_gotsomewhere; |
1172 | goto retry; |
1173 | } |
1174 | else if (!single_request_reopen) |
1175 | { |
1176 | statp->options |= RES_SNGLKUPREOP; |
1177 | single_request_reopen = true; |
1178 | *gotsomewhere = save_gotsomewhere; |
1179 | __res_iclose (statp, false); |
1180 | goto retry_reopen; |
1181 | } |
1182 | |
1183 | *resplen2 = 1; |
1184 | return resplen; |
1185 | } |
1186 | |
1187 | *gotsomewhere = 1; |
1188 | return (0); |
1189 | } |
1190 | if (n < 0) { |
1191 | if (errno == EINTR) |
1192 | goto recompute_resend; |
1193 | |
1194 | goto poll_err_out; |
1195 | } |
1196 | __set_errno (0); |
1197 | if (pfd[0].revents & POLLOUT) { |
1198 | #ifndef __ASSUME_SENDMMSG |
1199 | static int have_sendmmsg; |
1200 | #else |
1201 | # define have_sendmmsg 1 |
1202 | #endif |
1203 | if (have_sendmmsg >= 0 && nwritten == 0 && buf2 != NULL |
1204 | && !single_request) |
1205 | { |
1206 | struct iovec iov[2]; |
1207 | struct mmsghdr reqs[2]; |
1208 | reqs[0].msg_hdr.msg_name = NULL; |
1209 | reqs[0].msg_hdr.msg_namelen = 0; |
1210 | reqs[0].msg_hdr.msg_iov = &iov[0]; |
1211 | reqs[0].msg_hdr.msg_iovlen = 1; |
1212 | iov[0].iov_base = (void *) buf; |
1213 | iov[0].iov_len = buflen; |
1214 | reqs[0].msg_hdr.msg_control = NULL; |
1215 | reqs[0].msg_hdr.msg_controllen = 0; |
1216 | |
1217 | reqs[1].msg_hdr.msg_name = NULL; |
1218 | reqs[1].msg_hdr.msg_namelen = 0; |
1219 | reqs[1].msg_hdr.msg_iov = &iov[1]; |
1220 | reqs[1].msg_hdr.msg_iovlen = 1; |
1221 | iov[1].iov_base = (void *) buf2; |
1222 | iov[1].iov_len = buflen2; |
1223 | reqs[1].msg_hdr.msg_control = NULL; |
1224 | reqs[1].msg_hdr.msg_controllen = 0; |
1225 | |
1226 | int ndg = __sendmmsg (pfd[0].fd, reqs, 2, MSG_NOSIGNAL); |
1227 | if (__glibc_likely (ndg == 2)) |
1228 | { |
1229 | if (reqs[0].msg_len != buflen |
1230 | || reqs[1].msg_len != buflen2) |
1231 | goto fail_sendmmsg; |
1232 | |
1233 | pfd[0].events = POLLIN; |
1234 | nwritten += 2; |
1235 | } |
1236 | else if (ndg == 1 && reqs[0].msg_len == buflen) |
1237 | goto just_one; |
1238 | else if (ndg < 0 && (errno == EINTR || errno == EAGAIN)) |
1239 | goto recompute_resend; |
1240 | else |
1241 | { |
1242 | #ifndef __ASSUME_SENDMMSG |
1243 | if (__glibc_unlikely (have_sendmmsg == 0)) |
1244 | { |
1245 | if (ndg < 0 && errno == ENOSYS) |
1246 | { |
1247 | have_sendmmsg = -1; |
1248 | goto try_send; |
1249 | } |
1250 | have_sendmmsg = 1; |
1251 | } |
1252 | #endif |
1253 | |
1254 | fail_sendmmsg: |
1255 | Perror(statp, stderr, "sendmmsg" , errno); |
1256 | goto err_out; |
1257 | } |
1258 | } |
1259 | else |
1260 | { |
1261 | ssize_t sr; |
1262 | #ifndef __ASSUME_SENDMMSG |
1263 | try_send: |
1264 | #endif |
1265 | if (nwritten != 0) |
1266 | sr = send (pfd[0].fd, buf2, buflen2, MSG_NOSIGNAL); |
1267 | else |
1268 | sr = send (pfd[0].fd, buf, buflen, MSG_NOSIGNAL); |
1269 | |
1270 | if (sr != (nwritten != 0 ? buflen2 : buflen)) { |
1271 | if (errno == EINTR || errno == EAGAIN) |
1272 | goto recompute_resend; |
1273 | Perror(statp, stderr, "send" , errno); |
1274 | goto err_out; |
1275 | } |
1276 | just_one: |
1277 | if (nwritten != 0 || buf2 == NULL || single_request) |
1278 | pfd[0].events = POLLIN; |
1279 | else |
1280 | pfd[0].events = POLLIN | POLLOUT; |
1281 | ++nwritten; |
1282 | } |
1283 | goto wait; |
1284 | } else if (pfd[0].revents & POLLIN) { |
1285 | int *thisanssizp; |
1286 | u_char **thisansp; |
1287 | int *thisresplenp; |
1288 | |
1289 | if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) { |
1290 | /* We have not received any responses |
1291 | yet or we only have one response to |
1292 | receive. */ |
1293 | thisanssizp = anssizp; |
1294 | thisansp = anscp ?: ansp; |
1295 | assert (anscp != NULL || ansp2 == NULL); |
1296 | thisresplenp = &resplen; |
1297 | } else { |
1298 | thisanssizp = anssizp2; |
1299 | thisansp = ansp2; |
1300 | thisresplenp = resplen2; |
1301 | } |
1302 | |
1303 | if (*thisanssizp < MAXPACKET |
1304 | /* If the current buffer is not the the static |
1305 | user-supplied buffer then we can reallocate |
1306 | it. */ |
1307 | && (thisansp != NULL && thisansp != ansp) |
1308 | #ifdef FIONREAD |
1309 | /* Is the size too small? */ |
1310 | && (ioctl (pfd[0].fd, FIONREAD, thisresplenp) < 0 |
1311 | || *thisanssizp < *thisresplenp) |
1312 | #endif |
1313 | ) { |
1314 | /* Always allocate MAXPACKET, callers expect |
1315 | this specific size. */ |
1316 | u_char *newp = malloc (MAXPACKET); |
1317 | if (newp != NULL) { |
1318 | *thisanssizp = MAXPACKET; |
1319 | *thisansp = newp; |
1320 | if (thisansp == ansp2) |
1321 | *ansp2_malloced = 1; |
1322 | } |
1323 | } |
1324 | /* We could end up with truncation if anscp was NULL |
1325 | (not allowed to change caller's buffer) and the |
1326 | response buffer size is too small. This isn't a |
1327 | reliable way to detect truncation because the ioctl |
1328 | may be an inaccurate report of the UDP message size. |
1329 | Therefore we use this only to issue debug output. |
1330 | To do truncation accurately with UDP we need |
1331 | MSG_TRUNC which is only available on Linux. We |
1332 | can abstract out the Linux-specific feature in the |
1333 | future to detect truncation. */ |
1334 | if (__glibc_unlikely (*thisanssizp < *thisresplenp)) { |
1335 | Dprint(statp->options & RES_DEBUG, |
1336 | (stdout, ";; response may be truncated (UDP)\n" ) |
1337 | ); |
1338 | } |
1339 | |
1340 | HEADER *anhp = (HEADER *) *thisansp; |
1341 | socklen_t fromlen = sizeof(struct sockaddr_in6); |
1342 | assert (sizeof(from) <= fromlen); |
1343 | *thisresplenp = recvfrom(pfd[0].fd, (char*)*thisansp, |
1344 | *thisanssizp, 0, |
1345 | (struct sockaddr *)&from, &fromlen); |
1346 | if (__glibc_unlikely (*thisresplenp <= 0)) { |
1347 | if (errno == EINTR || errno == EAGAIN) { |
1348 | need_recompute = 1; |
1349 | goto wait; |
1350 | } |
1351 | Perror(statp, stderr, "recvfrom" , errno); |
1352 | goto err_out; |
1353 | } |
1354 | *gotsomewhere = 1; |
1355 | if (__glibc_unlikely (*thisresplenp < HFIXEDSZ)) { |
1356 | /* |
1357 | * Undersized message. |
1358 | */ |
1359 | Dprint(statp->options & RES_DEBUG, |
1360 | (stdout, ";; undersized: %d\n" , |
1361 | *thisresplenp)); |
1362 | *terrno = EMSGSIZE; |
1363 | goto err_out; |
1364 | } |
1365 | if ((recvresp1 || hp->id != anhp->id) |
1366 | && (recvresp2 || hp2->id != anhp->id)) { |
1367 | /* |
1368 | * response from old query, ignore it. |
1369 | * XXX - potential security hazard could |
1370 | * be detected here. |
1371 | */ |
1372 | DprintQ((statp->options & RES_DEBUG) || |
1373 | (statp->pfcode & RES_PRF_REPLY), |
1374 | (stdout, ";; old answer:\n" ), |
1375 | *thisansp, |
1376 | (*thisresplenp > *thisanssizp) |
1377 | ? *thisanssizp : *thisresplenp); |
1378 | goto wait; |
1379 | } |
1380 | if (!(statp->options & RES_INSECURE1) && |
1381 | !res_ourserver_p(statp, &from)) { |
1382 | /* |
1383 | * response from wrong server? ignore it. |
1384 | * XXX - potential security hazard could |
1385 | * be detected here. |
1386 | */ |
1387 | DprintQ((statp->options & RES_DEBUG) || |
1388 | (statp->pfcode & RES_PRF_REPLY), |
1389 | (stdout, ";; not our server:\n" ), |
1390 | *thisansp, |
1391 | (*thisresplenp > *thisanssizp) |
1392 | ? *thisanssizp : *thisresplenp); |
1393 | goto wait; |
1394 | } |
1395 | #ifdef RES_USE_EDNS0 |
1396 | if (anhp->rcode == FORMERR |
1397 | && (statp->options & RES_USE_EDNS0) != 0U) { |
1398 | /* |
1399 | * Do not retry if the server does not understand |
1400 | * EDNS0. The case has to be captured here, as |
1401 | * FORMERR packet do not carry query section, hence |
1402 | * res_queriesmatch() returns 0. |
1403 | */ |
1404 | DprintQ(statp->options & RES_DEBUG, |
1405 | (stdout, |
1406 | "server rejected query with EDNS0:\n" ), |
1407 | *thisansp, |
1408 | (*thisresplenp > *thisanssizp) |
1409 | ? *thisanssizp : *thisresplenp); |
1410 | /* record the error */ |
1411 | statp->_flags |= RES_F_EDNS0ERR; |
1412 | goto err_out; |
1413 | } |
1414 | #endif |
1415 | if (!(statp->options & RES_INSECURE2) |
1416 | && (recvresp1 || !res_queriesmatch(buf, buf + buflen, |
1417 | *thisansp, |
1418 | *thisansp |
1419 | + *thisanssizp)) |
1420 | && (recvresp2 || !res_queriesmatch(buf2, buf2 + buflen2, |
1421 | *thisansp, |
1422 | *thisansp |
1423 | + *thisanssizp))) { |
1424 | /* |
1425 | * response contains wrong query? ignore it. |
1426 | * XXX - potential security hazard could |
1427 | * be detected here. |
1428 | */ |
1429 | DprintQ((statp->options & RES_DEBUG) || |
1430 | (statp->pfcode & RES_PRF_REPLY), |
1431 | (stdout, ";; wrong query name:\n" ), |
1432 | *thisansp, |
1433 | (*thisresplenp > *thisanssizp) |
1434 | ? *thisanssizp : *thisresplenp); |
1435 | goto wait; |
1436 | } |
1437 | if (anhp->rcode == SERVFAIL || |
1438 | anhp->rcode == NOTIMP || |
1439 | anhp->rcode == REFUSED) { |
1440 | DprintQ(statp->options & RES_DEBUG, |
1441 | (stdout, "server rejected query:\n" ), |
1442 | *thisansp, |
1443 | (*thisresplenp > *thisanssizp) |
1444 | ? *thisanssizp : *thisresplenp); |
1445 | |
1446 | next_ns: |
1447 | if (recvresp1 || (buf2 != NULL && recvresp2)) { |
1448 | *resplen2 = 0; |
1449 | return resplen; |
1450 | } |
1451 | if (buf2 != NULL) |
1452 | { |
1453 | /* No data from the first reply. */ |
1454 | resplen = 0; |
1455 | /* We are waiting for a possible second reply. */ |
1456 | if (hp->id == anhp->id) |
1457 | recvresp1 = 1; |
1458 | else |
1459 | recvresp2 = 1; |
1460 | |
1461 | goto wait; |
1462 | } |
1463 | |
1464 | __res_iclose(statp, false); |
1465 | /* don't retry if called from dig */ |
1466 | if (!statp->pfcode) |
1467 | return (0); |
1468 | } |
1469 | if (anhp->rcode == NOERROR && anhp->ancount == 0 |
1470 | && anhp->aa == 0 && anhp->ra == 0 && anhp->arcount == 0) { |
1471 | DprintQ(statp->options & RES_DEBUG, |
1472 | (stdout, "referred query:\n" ), |
1473 | *thisansp, |
1474 | (*thisresplenp > *thisanssizp) |
1475 | ? *thisanssizp : *thisresplenp); |
1476 | goto next_ns; |
1477 | } |
1478 | if (!(statp->options & RES_IGNTC) && anhp->tc) { |
1479 | /* |
1480 | * To get the rest of answer, |
1481 | * use TCP with same server. |
1482 | */ |
1483 | Dprint(statp->options & RES_DEBUG, |
1484 | (stdout, ";; truncated answer\n" )); |
1485 | *v_circuit = 1; |
1486 | __res_iclose(statp, false); |
1487 | // XXX if we have received one reply we could |
1488 | // XXX use it and not repeat it over TCP... |
1489 | return (1); |
1490 | } |
1491 | /* Mark which reply we received. */ |
1492 | if (recvresp1 == 0 && hp->id == anhp->id) |
1493 | recvresp1 = 1; |
1494 | else |
1495 | recvresp2 = 1; |
1496 | /* Repeat waiting if we have a second answer to arrive. */ |
1497 | if ((recvresp1 & recvresp2) == 0) { |
1498 | if (single_request) { |
1499 | pfd[0].events = POLLOUT; |
1500 | if (single_request_reopen) { |
1501 | __res_iclose (statp, false); |
1502 | retval = reopen (statp, terrno, ns); |
1503 | if (retval <= 0) |
1504 | return retval; |
1505 | pfd[0].fd = EXT(statp).nssocks[ns]; |
1506 | } |
1507 | } |
1508 | goto wait; |
1509 | } |
1510 | /* |
1511 | * All is well, or the error is fatal. Signal that the |
1512 | * next nameserver ought not be tried. |
1513 | */ |
1514 | return (resplen); |
1515 | } else if (pfd[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { |
1516 | /* Something went wrong. We can stop trying. */ |
1517 | goto err_out; |
1518 | } |
1519 | else { |
1520 | /* poll should not have returned > 0 in this case. */ |
1521 | abort (); |
1522 | } |
1523 | } |
1524 | |
1525 | #ifdef DEBUG |
1526 | static void |
1527 | Aerror(const res_state statp, FILE *file, const char *string, int error, |
1528 | const struct sockaddr *address) |
1529 | { |
1530 | int save = errno; |
1531 | |
1532 | if ((statp->options & RES_DEBUG) != 0) { |
1533 | char tmp[sizeof "xxxx.xxxx.xxxx.255.255.255.255" ]; |
1534 | |
1535 | fprintf(file, "res_send: %s ([%s].%u): %s\n" , |
1536 | string, |
1537 | (address->sa_family == AF_INET |
1538 | ? inet_ntop(address->sa_family, |
1539 | &((const struct sockaddr_in *) address)->sin_addr, |
1540 | tmp, sizeof tmp) |
1541 | : inet_ntop(address->sa_family, |
1542 | &((const struct sockaddr_in6 *) address)->sin6_addr, |
1543 | tmp, sizeof tmp)), |
1544 | (address->sa_family == AF_INET |
1545 | ? ntohs(((struct sockaddr_in *) address)->sin_port) |
1546 | : address->sa_family == AF_INET6 |
1547 | ? ntohs(((struct sockaddr_in6 *) address)->sin6_port) |
1548 | : 0), |
1549 | strerror(error)); |
1550 | } |
1551 | __set_errno (save); |
1552 | } |
1553 | |
1554 | static void |
1555 | Perror(const res_state statp, FILE *file, const char *string, int error) { |
1556 | int save = errno; |
1557 | |
1558 | if ((statp->options & RES_DEBUG) != 0) |
1559 | fprintf(file, "res_send: %s: %s\n" , |
1560 | string, strerror(error)); |
1561 | __set_errno (save); |
1562 | } |
1563 | #endif |
1564 | |
1565 | static int |
1566 | sock_eq(struct sockaddr_in6 *a1, struct sockaddr_in6 *a2) { |
1567 | if (a1->sin6_family == a2->sin6_family) { |
1568 | if (a1->sin6_family == AF_INET) |
1569 | return ((((struct sockaddr_in *)a1)->sin_port == |
1570 | ((struct sockaddr_in *)a2)->sin_port) && |
1571 | (((struct sockaddr_in *)a1)->sin_addr.s_addr == |
1572 | ((struct sockaddr_in *)a2)->sin_addr.s_addr)); |
1573 | else |
1574 | return ((a1->sin6_port == a2->sin6_port) && |
1575 | !memcmp(&a1->sin6_addr, &a2->sin6_addr, |
1576 | sizeof (struct in6_addr))); |
1577 | } |
1578 | if (a1->sin6_family == AF_INET) { |
1579 | struct sockaddr_in6 *sap = a1; |
1580 | a1 = a2; |
1581 | a2 = sap; |
1582 | } /* assumes that AF_INET and AF_INET6 are the only possibilities */ |
1583 | return ((a1->sin6_port == ((struct sockaddr_in *)a2)->sin_port) && |
1584 | IN6_IS_ADDR_V4MAPPED(&a1->sin6_addr) && |
1585 | (a1->sin6_addr.s6_addr32[3] == |
1586 | ((struct sockaddr_in *)a2)->sin_addr.s_addr)); |
1587 | } |
1588 | |