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