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