diff options
Diffstat (limited to 'src/lib/libc/net/res_send.c')
-rw-r--r-- | src/lib/libc/net/res_send.c | 879 |
1 files changed, 879 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_send.c b/src/lib/libc/net/res_send.c new file mode 100644 index 0000000000..ee7ce43035 --- /dev/null +++ b/src/lib/libc/net/res_send.c | |||
@@ -0,0 +1,879 @@ | |||
1 | /* $OpenBSD: res_send.c,v 1.18 2005/03/30 02:58:28 tedu Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1989, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1989, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #ifndef INET6 | ||
55 | #define INET6 | ||
56 | #endif | ||
57 | |||
58 | #if defined(LIBC_SCCS) && !defined(lint) | ||
59 | #if 0 | ||
60 | static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93"; | ||
61 | static char rcsid[] = "$From: res_send.c,v 8.12 1996/10/08 04:51:06 vixie Exp $"; | ||
62 | #else | ||
63 | static char rcsid[] = "$OpenBSD: res_send.c,v 1.18 2005/03/30 02:58:28 tedu Exp $"; | ||
64 | #endif | ||
65 | #endif /* LIBC_SCCS and not lint */ | ||
66 | |||
67 | /* change this to "0" | ||
68 | * if you talk to a lot | ||
69 | * of multi-homed SunOS | ||
70 | * ("broken") name servers. | ||
71 | */ | ||
72 | #define CHECK_SRVR_ADDR 1 /* XXX - should be in options.h */ | ||
73 | |||
74 | /* | ||
75 | * Send query to name server and wait for reply. | ||
76 | */ | ||
77 | |||
78 | #include <sys/types.h> | ||
79 | #include <sys/param.h> | ||
80 | #include <sys/time.h> | ||
81 | #include <sys/socket.h> | ||
82 | #include <sys/uio.h> | ||
83 | #include <netinet/in.h> | ||
84 | #include <arpa/nameser.h> | ||
85 | #include <arpa/inet.h> | ||
86 | |||
87 | #include <stdio.h> | ||
88 | #include <netdb.h> | ||
89 | #include <errno.h> | ||
90 | #include <resolv.h> | ||
91 | #include <stdlib.h> | ||
92 | #include <string.h> | ||
93 | #include <unistd.h> | ||
94 | |||
95 | #include "thread_private.h" | ||
96 | |||
97 | static int s = -1; /* socket used for communications */ | ||
98 | static int connected = 0; /* is the socket connected */ | ||
99 | static int vc = 0; /* is the socket a virtual ciruit? */ | ||
100 | static int af = 0; /* address family of socket */ | ||
101 | |||
102 | #ifndef FD_SET | ||
103 | /* XXX - should be in portability.h */ | ||
104 | #define NFDBITS 32 | ||
105 | #define FD_SETSIZE 32 | ||
106 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) | ||
107 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) | ||
108 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) | ||
109 | #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) | ||
110 | #endif | ||
111 | |||
112 | #define CAN_RECONNECT 1 | ||
113 | |||
114 | #ifndef DEBUG | ||
115 | # define Dprint(cond, args) /*empty*/ | ||
116 | # define DprintQ(cond, args, query, size) /*empty*/ | ||
117 | # define Aerror(file, string, error, address) /*empty*/ | ||
118 | # define Perror(file, string, error) /*empty*/ | ||
119 | #else | ||
120 | # define Dprint(cond, args) if (cond) {fprintf args;} else {} | ||
121 | # define DprintQ(cond, args, query, size) if (cond) {\ | ||
122 | fprintf args;\ | ||
123 | __fp_nquery(query, size, stdout);\ | ||
124 | } else {} | ||
125 | static char abuf[NI_MAXHOST]; | ||
126 | static char pbuf[NI_MAXSERV]; | ||
127 | static void Aerror(FILE *, char *, int, struct sockaddr *); | ||
128 | static void Perror(FILE *, char *, int); | ||
129 | |||
130 | static void | ||
131 | Aerror(FILE *file, char *string, int error, struct sockaddr *address) | ||
132 | { | ||
133 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
134 | int save = errno; | ||
135 | |||
136 | if (_resp->options & RES_DEBUG) { | ||
137 | if (getnameinfo(address, address->sa_len, abuf, sizeof(abuf), | ||
138 | pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | ||
139 | strlcpy(abuf, "?", sizeof(abuf)); | ||
140 | strlcpy(pbuf, "?", sizeof(pbuf)); | ||
141 | } | ||
142 | fprintf(file, "res_send: %s ([%s].%s): %s\n", | ||
143 | string, abuf, pbuf, strerror(error)); | ||
144 | } | ||
145 | errno = save; | ||
146 | } | ||
147 | static void | ||
148 | Perror(FILE *file, char *string, int error) | ||
149 | { | ||
150 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
151 | int save = errno; | ||
152 | |||
153 | if (_resp->options & RES_DEBUG) { | ||
154 | fprintf(file, "res_send: %s: %s\n", | ||
155 | string, strerror(error)); | ||
156 | } | ||
157 | errno = save; | ||
158 | } | ||
159 | #endif | ||
160 | |||
161 | static res_send_qhook Qhook = NULL; | ||
162 | static res_send_rhook Rhook = NULL; | ||
163 | |||
164 | void | ||
165 | res_send_setqhook(res_send_qhook hook) | ||
166 | { | ||
167 | |||
168 | Qhook = hook; | ||
169 | } | ||
170 | |||
171 | void | ||
172 | res_send_setrhook(res_send_rhook hook) | ||
173 | { | ||
174 | |||
175 | Rhook = hook; | ||
176 | } | ||
177 | |||
178 | #ifdef INET6 | ||
179 | static struct sockaddr * get_nsaddr(size_t); | ||
180 | |||
181 | /* | ||
182 | * pick appropriate nsaddr_list for use. see res_init() for initialization. | ||
183 | */ | ||
184 | static struct sockaddr * | ||
185 | get_nsaddr(size_t n) | ||
186 | { | ||
187 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
188 | struct __res_state_ext *_res_extp = _THREAD_PRIVATE(_res_ext, _res_ext, | ||
189 | &_res_ext); | ||
190 | |||
191 | if (!_resp->nsaddr_list[n].sin_family) { | ||
192 | /* | ||
193 | * - _res_extp->nsaddr_list[n] holds an address that is larger | ||
194 | * than struct sockaddr, and | ||
195 | * - user code did not update _resp->nsaddr_list[n]. | ||
196 | */ | ||
197 | return (struct sockaddr *)&_res_extp->nsaddr_list[n]; | ||
198 | } else { | ||
199 | /* | ||
200 | * - user code updated _res.nsaddr_list[n], or | ||
201 | * - _resp->nsaddr_list[n] has the same content as | ||
202 | * _res_extp->nsaddr_list[n]. | ||
203 | */ | ||
204 | return (struct sockaddr *)&_resp->nsaddr_list[n]; | ||
205 | } | ||
206 | } | ||
207 | #else | ||
208 | #define get_nsaddr(n) ((struct sockaddr *)&_resp->nsaddr_list[(n)]) | ||
209 | #endif | ||
210 | |||
211 | /* int | ||
212 | * res_isourserver(ina) | ||
213 | * looks up "ina" in _resp->ns_addr_list[] | ||
214 | * returns: | ||
215 | * 0 : not found | ||
216 | * >0 : found | ||
217 | * author: | ||
218 | * paul vixie, 29may94 | ||
219 | */ | ||
220 | int | ||
221 | res_isourserver(const struct sockaddr_in *inp) | ||
222 | { | ||
223 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
224 | #ifdef INET6 | ||
225 | const struct sockaddr_in6 *in6p = (const struct sockaddr_in6 *)inp; | ||
226 | const struct sockaddr_in6 *srv6; | ||
227 | #endif | ||
228 | const struct sockaddr_in *srv; | ||
229 | int ns, ret; | ||
230 | |||
231 | ret = 0; | ||
232 | switch (inp->sin_family) { | ||
233 | #ifdef INET6 | ||
234 | case AF_INET6: | ||
235 | for (ns = 0; ns < _resp->nscount; ns++) { | ||
236 | srv6 = (struct sockaddr_in6 *)get_nsaddr(ns); | ||
237 | if (srv6->sin6_family == in6p->sin6_family && | ||
238 | srv6->sin6_port == in6p->sin6_port && | ||
239 | srv6->sin6_scope_id == in6p->sin6_scope_id && | ||
240 | (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) || | ||
241 | IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, | ||
242 | &in6p->sin6_addr))) { | ||
243 | ret++; | ||
244 | break; | ||
245 | } | ||
246 | } | ||
247 | break; | ||
248 | #endif | ||
249 | case AF_INET: | ||
250 | for (ns = 0; ns < _resp->nscount; ns++) { | ||
251 | srv = (struct sockaddr_in *)get_nsaddr(ns); | ||
252 | if (srv->sin_family == inp->sin_family && | ||
253 | srv->sin_port == inp->sin_port && | ||
254 | (srv->sin_addr.s_addr == INADDR_ANY || | ||
255 | srv->sin_addr.s_addr == inp->sin_addr.s_addr)) { | ||
256 | ret++; | ||
257 | break; | ||
258 | } | ||
259 | } | ||
260 | break; | ||
261 | } | ||
262 | return (ret); | ||
263 | } | ||
264 | |||
265 | /* int | ||
266 | * res_nameinquery(name, type, class, buf, eom) | ||
267 | * look for (name,type,class) in the query section of packet (buf,eom) | ||
268 | * returns: | ||
269 | * -1 : format error | ||
270 | * 0 : not found | ||
271 | * >0 : found | ||
272 | * author: | ||
273 | * paul vixie, 29may94 | ||
274 | */ | ||
275 | int | ||
276 | res_nameinquery(const char *name, int type, int class, const u_char *buf, | ||
277 | const u_char *eom) | ||
278 | { | ||
279 | const u_char *cp = buf + HFIXEDSZ; | ||
280 | int qdcount = ntohs(((HEADER*)buf)->qdcount); | ||
281 | |||
282 | while (qdcount-- > 0) { | ||
283 | char tname[MAXDNAME+1]; | ||
284 | int n, ttype, tclass; | ||
285 | |||
286 | n = dn_expand(buf, eom, cp, tname, sizeof tname); | ||
287 | if (n < 0) | ||
288 | return (-1); | ||
289 | cp += n; | ||
290 | ttype = _getshort(cp); cp += INT16SZ; | ||
291 | tclass = _getshort(cp); cp += INT16SZ; | ||
292 | if (ttype == type && | ||
293 | tclass == class && | ||
294 | strcasecmp(tname, name) == 0) | ||
295 | return (1); | ||
296 | } | ||
297 | return (0); | ||
298 | } | ||
299 | |||
300 | /* int | ||
301 | * res_queriesmatch(buf1, eom1, buf2, eom2) | ||
302 | * is there a 1:1 mapping of (name,type,class) | ||
303 | * in (buf1,eom1) and (buf2,eom2)? | ||
304 | * returns: | ||
305 | * -1 : format error | ||
306 | * 0 : not a 1:1 mapping | ||
307 | * >0 : is a 1:1 mapping | ||
308 | * author: | ||
309 | * paul vixie, 29may94 | ||
310 | */ | ||
311 | int | ||
312 | res_queriesmatch(const u_char *buf1, const u_char *eom1, const u_char *buf2, | ||
313 | const u_char *eom2) | ||
314 | { | ||
315 | const u_char *cp = buf1 + HFIXEDSZ; | ||
316 | int qdcount = ntohs(((HEADER*)buf1)->qdcount); | ||
317 | |||
318 | if (qdcount != ntohs(((HEADER*)buf2)->qdcount)) | ||
319 | return (0); | ||
320 | while (qdcount-- > 0) { | ||
321 | char tname[MAXDNAME+1]; | ||
322 | int n, ttype, tclass; | ||
323 | |||
324 | n = dn_expand(buf1, eom1, cp, tname, sizeof tname); | ||
325 | if (n < 0) | ||
326 | return (-1); | ||
327 | cp += n; | ||
328 | ttype = _getshort(cp); cp += INT16SZ; | ||
329 | tclass = _getshort(cp); cp += INT16SZ; | ||
330 | if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) | ||
331 | return (0); | ||
332 | } | ||
333 | return (1); | ||
334 | } | ||
335 | |||
336 | int | ||
337 | res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) | ||
338 | { | ||
339 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
340 | HEADER *hp = (HEADER *) buf; | ||
341 | HEADER *anhp = (HEADER *) ans; | ||
342 | int gotsomewhere, connreset, terrno, try, v_circuit, resplen, ns; | ||
343 | int n; | ||
344 | u_int badns; /* XXX NSMAX can't exceed #/bits in this var */ | ||
345 | |||
346 | if (_res_init(0) == -1) { | ||
347 | /* errno should have been set by res_init() in this case. */ | ||
348 | return (-1); | ||
349 | } | ||
350 | DprintQ((_resp->options & RES_DEBUG) || (_resp->pfcode & RES_PRF_QUERY), | ||
351 | (stdout, ";; res_send()\n"), buf, buflen); | ||
352 | v_circuit = (_resp->options & RES_USEVC) || buflen > PACKETSZ; | ||
353 | gotsomewhere = 0; | ||
354 | connreset = 0; | ||
355 | terrno = ETIMEDOUT; | ||
356 | badns = 0; | ||
357 | |||
358 | /* | ||
359 | * Send request, RETRY times, or until successful | ||
360 | */ | ||
361 | for (try = 0; try < _resp->retry; try++) { | ||
362 | for (ns = 0; ns < _resp->nscount; ns++) { | ||
363 | struct sockaddr *nsap = get_nsaddr(ns); | ||
364 | socklen_t salen; | ||
365 | |||
366 | if (nsap->sa_len) | ||
367 | salen = nsap->sa_len; | ||
368 | #ifdef INET6 | ||
369 | else if (nsap->sa_family == AF_INET6) | ||
370 | salen = sizeof(struct sockaddr_in6); | ||
371 | #endif | ||
372 | else if (nsap->sa_family == AF_INET) | ||
373 | salen = sizeof(struct sockaddr_in); | ||
374 | else | ||
375 | salen = 0; /*unknown, die on connect*/ | ||
376 | |||
377 | same_ns: | ||
378 | if (badns & (1 << ns)) { | ||
379 | res_close(); | ||
380 | goto next_ns; | ||
381 | } | ||
382 | |||
383 | if (Qhook) { | ||
384 | int done = 0, loops = 0; | ||
385 | |||
386 | do { | ||
387 | res_sendhookact act; | ||
388 | |||
389 | act = (*Qhook)((struct sockaddr_in **)&nsap, | ||
390 | &buf, &buflen, | ||
391 | ans, anssiz, &resplen); | ||
392 | switch (act) { | ||
393 | case res_goahead: | ||
394 | done = 1; | ||
395 | break; | ||
396 | case res_nextns: | ||
397 | res_close(); | ||
398 | goto next_ns; | ||
399 | case res_done: | ||
400 | return (resplen); | ||
401 | case res_modified: | ||
402 | /* give the hook another try */ | ||
403 | if (++loops < 42) /*doug adams*/ | ||
404 | break; | ||
405 | /*FALLTHROUGH*/ | ||
406 | case res_error: | ||
407 | /*FALLTHROUGH*/ | ||
408 | default: | ||
409 | return (-1); | ||
410 | } | ||
411 | } while (!done); | ||
412 | } | ||
413 | |||
414 | Dprint((_resp->options & RES_DEBUG) && | ||
415 | getnameinfo(nsap, salen, abuf, sizeof(abuf), | ||
416 | NULL, 0, NI_NUMERICHOST) == 0, | ||
417 | (stdout, ";; Querying server (# %d) address = %s\n", | ||
418 | ns + 1, abuf)); | ||
419 | |||
420 | if (v_circuit) { | ||
421 | int truncated; | ||
422 | struct iovec iov[2]; | ||
423 | u_short len; | ||
424 | u_char *cp; | ||
425 | |||
426 | /* | ||
427 | * Use virtual circuit; | ||
428 | * at most one attempt per server. | ||
429 | */ | ||
430 | try = _resp->retry; | ||
431 | truncated = 0; | ||
432 | if ((s < 0) || (!vc) || (af != nsap->sa_family)) { | ||
433 | if (s >= 0) | ||
434 | res_close(); | ||
435 | |||
436 | af = nsap->sa_family; | ||
437 | s = socket(af, SOCK_STREAM, 0); | ||
438 | if (s < 0) { | ||
439 | terrno = errno; | ||
440 | Perror(stderr, "socket(vc)", errno); | ||
441 | #if 0 | ||
442 | return (-1); | ||
443 | #else | ||
444 | badns |= (1 << ns); | ||
445 | res_close(); | ||
446 | goto next_ns; | ||
447 | #endif | ||
448 | } | ||
449 | errno = 0; | ||
450 | if (connect(s, nsap, salen) < 0) { | ||
451 | terrno = errno; | ||
452 | Aerror(stderr, "connect/vc", | ||
453 | errno, nsap); | ||
454 | badns |= (1 << ns); | ||
455 | res_close(); | ||
456 | goto next_ns; | ||
457 | } | ||
458 | vc = 1; | ||
459 | } | ||
460 | /* | ||
461 | * Send length & message | ||
462 | */ | ||
463 | putshort((u_short)buflen, (u_char*)&len); | ||
464 | iov[0].iov_base = (caddr_t)&len; | ||
465 | iov[0].iov_len = INT16SZ; | ||
466 | iov[1].iov_base = (caddr_t)buf; | ||
467 | iov[1].iov_len = buflen; | ||
468 | if (writev(s, iov, 2) != (INT16SZ + buflen)) { | ||
469 | terrno = errno; | ||
470 | Perror(stderr, "write failed", errno); | ||
471 | badns |= (1 << ns); | ||
472 | res_close(); | ||
473 | goto next_ns; | ||
474 | } | ||
475 | /* | ||
476 | * Receive length & response | ||
477 | */ | ||
478 | read_len: | ||
479 | cp = ans; | ||
480 | len = INT16SZ; | ||
481 | while ((n = read(s, (char *)cp, (int)len)) > 0) { | ||
482 | cp += n; | ||
483 | if ((len -= n) <= 0) | ||
484 | break; | ||
485 | } | ||
486 | if (n <= 0) { | ||
487 | terrno = errno; | ||
488 | Perror(stderr, "read failed", errno); | ||
489 | res_close(); | ||
490 | /* | ||
491 | * A long running process might get its TCP | ||
492 | * connection reset if the remote server was | ||
493 | * restarted. Requery the server instead of | ||
494 | * trying a new one. When there is only one | ||
495 | * server, this means that a query might work | ||
496 | * instead of failing. We only allow one reset | ||
497 | * per query to prevent looping. | ||
498 | */ | ||
499 | if (terrno == ECONNRESET && !connreset) { | ||
500 | connreset = 1; | ||
501 | res_close(); | ||
502 | goto same_ns; | ||
503 | } | ||
504 | res_close(); | ||
505 | goto next_ns; | ||
506 | } | ||
507 | resplen = _getshort(ans); | ||
508 | if (resplen > anssiz) { | ||
509 | Dprint(_resp->options & RES_DEBUG, | ||
510 | (stdout, ";; response truncated\n") | ||
511 | ); | ||
512 | truncated = 1; | ||
513 | len = anssiz; | ||
514 | } else | ||
515 | len = resplen; | ||
516 | cp = ans; | ||
517 | while (len != 0 && | ||
518 | (n = read(s, (char *)cp, (int)len)) > 0) { | ||
519 | cp += n; | ||
520 | len -= n; | ||
521 | } | ||
522 | if (n <= 0) { | ||
523 | terrno = errno; | ||
524 | Perror(stderr, "read(vc)", errno); | ||
525 | res_close(); | ||
526 | goto next_ns; | ||
527 | } | ||
528 | if (truncated) { | ||
529 | /* | ||
530 | * Flush rest of answer | ||
531 | * so connection stays in synch. | ||
532 | */ | ||
533 | anhp->tc = 1; | ||
534 | len = resplen - anssiz; | ||
535 | while (len != 0) { | ||
536 | char junk[PACKETSZ]; | ||
537 | |||
538 | n = (len > sizeof(junk) | ||
539 | ? sizeof(junk) | ||
540 | : len); | ||
541 | if ((n = read(s, junk, n)) > 0) | ||
542 | len -= n; | ||
543 | else | ||
544 | break; | ||
545 | } | ||
546 | } | ||
547 | /* | ||
548 | * The calling applicating has bailed out of | ||
549 | * a previous call and failed to arrange to have | ||
550 | * the circuit closed or the server has got | ||
551 | * itself confused. Anyway drop the packet and | ||
552 | * wait for the correct one. | ||
553 | */ | ||
554 | if (hp->id != anhp->id) { | ||
555 | DprintQ((_resp->options & RES_DEBUG) || | ||
556 | (_resp->pfcode & RES_PRF_REPLY), | ||
557 | (stdout, ";; old answer (unexpected):\n"), | ||
558 | ans, (resplen>anssiz)?anssiz:resplen); | ||
559 | goto read_len; | ||
560 | } | ||
561 | } else { | ||
562 | /* | ||
563 | * Use datagrams. | ||
564 | */ | ||
565 | struct timeval timeout; | ||
566 | fd_set *dsmaskp; | ||
567 | struct sockaddr_storage from; | ||
568 | socklen_t fromlen; | ||
569 | |||
570 | if ((s < 0) || vc || (af != nsap->sa_family)) { | ||
571 | if (vc) | ||
572 | res_close(); | ||
573 | af = nsap->sa_family; | ||
574 | s = socket(af, SOCK_DGRAM, 0); | ||
575 | if (s < 0) { | ||
576 | #if !CAN_RECONNECT | ||
577 | bad_dg_sock: | ||
578 | #endif | ||
579 | terrno = errno; | ||
580 | Perror(stderr, "socket(dg)", errno); | ||
581 | #if 0 | ||
582 | return (-1); | ||
583 | #else | ||
584 | badns |= (1 << ns); | ||
585 | res_close(); | ||
586 | goto next_ns; | ||
587 | #endif | ||
588 | } | ||
589 | #ifdef IPV6_MINMTU | ||
590 | if (af == AF_INET6) { | ||
591 | const int yes = 1; | ||
592 | (void)setsockopt(s, IPPROTO_IPV6, | ||
593 | IPV6_USE_MIN_MTU, &yes, | ||
594 | sizeof(yes)); | ||
595 | } | ||
596 | #endif | ||
597 | connected = 0; | ||
598 | } | ||
599 | /* | ||
600 | * On a 4.3BSD+ machine (client and server, | ||
601 | * actually), sending to a nameserver datagram | ||
602 | * port with no nameserver will cause an | ||
603 | * ICMP port unreachable message to be returned. | ||
604 | * If our datagram socket is "connected" to the | ||
605 | * server, we get an ECONNREFUSED error on the next | ||
606 | * socket operation, and select returns if the | ||
607 | * error message is received. We can thus detect | ||
608 | * the absence of a nameserver without timing out. | ||
609 | * If we have sent queries to at least two servers, | ||
610 | * however, we don't want to remain connected, | ||
611 | * as we wish to receive answers from the first | ||
612 | * server to respond. | ||
613 | */ | ||
614 | if (!(_resp->options & RES_INSECURE1) && | ||
615 | (_resp->nscount == 1 || (try == 0 && ns == 0))) { | ||
616 | /* | ||
617 | * Connect only if we are sure we won't | ||
618 | * receive a response from another server. | ||
619 | */ | ||
620 | if (!connected) { | ||
621 | if (connect(s, nsap, salen) < 0) { | ||
622 | Aerror(stderr, | ||
623 | "connect(dg)", | ||
624 | errno, nsap); | ||
625 | badns |= (1 << ns); | ||
626 | res_close(); | ||
627 | goto next_ns; | ||
628 | } | ||
629 | connected = 1; | ||
630 | } | ||
631 | if (send(s, (char*)buf, buflen, 0) != buflen) { | ||
632 | Perror(stderr, "send", errno); | ||
633 | badns |= (1 << ns); | ||
634 | res_close(); | ||
635 | goto next_ns; | ||
636 | } | ||
637 | } else { | ||
638 | /* | ||
639 | * Disconnect if we want to listen | ||
640 | * for responses from more than one server. | ||
641 | */ | ||
642 | if (connected) { | ||
643 | #if CAN_RECONNECT | ||
644 | #ifdef INET6 | ||
645 | /* XXX: any errornous address */ | ||
646 | #endif /* INET6 */ | ||
647 | struct sockaddr_in no_addr; | ||
648 | |||
649 | no_addr.sin_family = AF_INET; | ||
650 | no_addr.sin_addr.s_addr = INADDR_ANY; | ||
651 | no_addr.sin_port = 0; | ||
652 | (void) connect(s, | ||
653 | (struct sockaddr *) | ||
654 | &no_addr, | ||
655 | sizeof(no_addr)); | ||
656 | #else | ||
657 | int s1 = socket(af, SOCK_DGRAM,0); | ||
658 | if (s1 < 0) | ||
659 | goto bad_dg_sock; | ||
660 | (void) dup2(s1, s); | ||
661 | (void) close(s1); | ||
662 | Dprint(_resp->options & RES_DEBUG, | ||
663 | (stdout, ";; new DG socket\n")) | ||
664 | #endif | ||
665 | #ifdef IPV6_MINMTU | ||
666 | if (af == AF_INET6) { | ||
667 | const int yes = 1; | ||
668 | (void)setsockopt(s, IPPROTO_IPV6, | ||
669 | IPV6_USE_MIN_MTU, &yes, | ||
670 | sizeof(yes)); | ||
671 | } | ||
672 | #endif | ||
673 | connected = 0; | ||
674 | errno = 0; | ||
675 | } | ||
676 | if (sendto(s, (char*)buf, buflen, 0, | ||
677 | nsap, salen) != buflen) { | ||
678 | Aerror(stderr, "sendto", errno, nsap); | ||
679 | badns |= (1 << ns); | ||
680 | res_close(); | ||
681 | goto next_ns; | ||
682 | } | ||
683 | } | ||
684 | |||
685 | /* | ||
686 | * Wait for reply | ||
687 | */ | ||
688 | timeout.tv_sec = (_resp->retrans << try); | ||
689 | if (try > 0) | ||
690 | timeout.tv_sec /= _resp->nscount; | ||
691 | if ((long) timeout.tv_sec <= 0) | ||
692 | timeout.tv_sec = 1; | ||
693 | timeout.tv_usec = 0; | ||
694 | wait: | ||
695 | dsmaskp = (fd_set *)calloc(howmany(s+1, NFDBITS), | ||
696 | sizeof(fd_mask)); | ||
697 | if (dsmaskp == NULL) { | ||
698 | res_close(); | ||
699 | goto next_ns; | ||
700 | } | ||
701 | FD_SET(s, dsmaskp); | ||
702 | n = select(s+1, dsmaskp, (fd_set *)NULL, | ||
703 | (fd_set *)NULL, &timeout); | ||
704 | free(dsmaskp); | ||
705 | if (n < 0) { | ||
706 | if (errno == EINTR) | ||
707 | goto wait; | ||
708 | Perror(stderr, "select", errno); | ||
709 | res_close(); | ||
710 | goto next_ns; | ||
711 | } | ||
712 | if (n == 0) { | ||
713 | /* | ||
714 | * timeout | ||
715 | */ | ||
716 | Dprint(_resp->options & RES_DEBUG, | ||
717 | (stdout, ";; timeout\n")); | ||
718 | gotsomewhere = 1; | ||
719 | res_close(); | ||
720 | goto next_ns; | ||
721 | } | ||
722 | errno = 0; | ||
723 | fromlen = sizeof(from); | ||
724 | resplen = recvfrom(s, (char*)ans, anssiz, 0, | ||
725 | (struct sockaddr *)&from, &fromlen); | ||
726 | if (resplen <= 0) { | ||
727 | Perror(stderr, "recvfrom", errno); | ||
728 | res_close(); | ||
729 | goto next_ns; | ||
730 | } | ||
731 | gotsomewhere = 1; | ||
732 | if (hp->id != anhp->id) { | ||
733 | /* | ||
734 | * response from old query, ignore it. | ||
735 | * XXX - potential security hazard could | ||
736 | * be detected here. | ||
737 | */ | ||
738 | DprintQ((_resp->options & RES_DEBUG) || | ||
739 | (_resp->pfcode & RES_PRF_REPLY), | ||
740 | (stdout, ";; old answer:\n"), | ||
741 | ans, (resplen>anssiz)?anssiz:resplen); | ||
742 | goto wait; | ||
743 | } | ||
744 | #if CHECK_SRVR_ADDR | ||
745 | if (!(_resp->options & RES_INSECURE1) && | ||
746 | !res_isourserver((struct sockaddr_in *)&from)) { | ||
747 | /* | ||
748 | * response from wrong server? ignore it. | ||
749 | * XXX - potential security hazard could | ||
750 | * be detected here. | ||
751 | */ | ||
752 | DprintQ((_resp->options & RES_DEBUG) || | ||
753 | (_resp->pfcode & RES_PRF_REPLY), | ||
754 | (stdout, ";; not our server:\n"), | ||
755 | ans, (resplen>anssiz)?anssiz:resplen); | ||
756 | goto wait; | ||
757 | } | ||
758 | #endif | ||
759 | if (!(_resp->options & RES_INSECURE2) && | ||
760 | !res_queriesmatch(buf, buf + buflen, | ||
761 | ans, ans + anssiz)) { | ||
762 | /* | ||
763 | * response contains wrong query? ignore it. | ||
764 | * XXX - potential security hazard could | ||
765 | * be detected here. | ||
766 | */ | ||
767 | DprintQ((_resp->options & RES_DEBUG) || | ||
768 | (_resp->pfcode & RES_PRF_REPLY), | ||
769 | (stdout, ";; wrong query name:\n"), | ||
770 | ans, (resplen>anssiz)?anssiz:resplen); | ||
771 | goto wait; | ||
772 | } | ||
773 | if (anhp->rcode == SERVFAIL || | ||
774 | anhp->rcode == NOTIMP || | ||
775 | anhp->rcode == REFUSED) { | ||
776 | DprintQ(_resp->options & RES_DEBUG, | ||
777 | (stdout, "server rejected query:\n"), | ||
778 | ans, (resplen>anssiz)?anssiz:resplen); | ||
779 | badns |= (1 << ns); | ||
780 | res_close(); | ||
781 | /* don't retry if called from dig */ | ||
782 | if (!_resp->pfcode) | ||
783 | goto next_ns; | ||
784 | } | ||
785 | if (!(_resp->options & RES_IGNTC) && anhp->tc) { | ||
786 | /* | ||
787 | * get rest of answer; | ||
788 | * use TCP with same server. | ||
789 | */ | ||
790 | Dprint(_resp->options & RES_DEBUG, | ||
791 | (stdout, ";; truncated answer\n")); | ||
792 | v_circuit = 1; | ||
793 | res_close(); | ||
794 | goto same_ns; | ||
795 | } | ||
796 | } /*if vc/dg*/ | ||
797 | Dprint((_resp->options & RES_DEBUG) || | ||
798 | ((_resp->pfcode & RES_PRF_REPLY) && | ||
799 | (_resp->pfcode & RES_PRF_HEAD1)), | ||
800 | (stdout, ";; got answer:\n")); | ||
801 | DprintQ((_resp->options & RES_DEBUG) || | ||
802 | (_resp->pfcode & RES_PRF_REPLY), | ||
803 | (stdout, "%s", ""), | ||
804 | ans, (resplen>anssiz)?anssiz:resplen); | ||
805 | /* | ||
806 | * If using virtual circuits, we assume that the first server | ||
807 | * is preferred over the rest (i.e. it is on the local | ||
808 | * machine) and only keep that one open. | ||
809 | * If we have temporarily opened a virtual circuit, | ||
810 | * or if we haven't been asked to keep a socket open, | ||
811 | * close the socket. | ||
812 | */ | ||
813 | if ((v_circuit && (!(_resp->options & RES_USEVC) || ns != 0)) || | ||
814 | !(_resp->options & RES_STAYOPEN)) { | ||
815 | res_close(); | ||
816 | } | ||
817 | if (Rhook) { | ||
818 | int done = 0, loops = 0; | ||
819 | |||
820 | do { | ||
821 | res_sendhookact act; | ||
822 | |||
823 | act = (*Rhook)((struct sockaddr_in *)nsap, | ||
824 | buf, buflen, | ||
825 | ans, anssiz, &resplen); | ||
826 | switch (act) { | ||
827 | case res_goahead: | ||
828 | case res_done: | ||
829 | done = 1; | ||
830 | break; | ||
831 | case res_nextns: | ||
832 | res_close(); | ||
833 | goto next_ns; | ||
834 | case res_modified: | ||
835 | /* give the hook another try */ | ||
836 | if (++loops < 42) /*doug adams*/ | ||
837 | break; | ||
838 | /*FALLTHROUGH*/ | ||
839 | case res_error: | ||
840 | /*FALLTHROUGH*/ | ||
841 | default: | ||
842 | return (-1); | ||
843 | } | ||
844 | } while (!done); | ||
845 | |||
846 | } | ||
847 | return (resplen); | ||
848 | next_ns: ; | ||
849 | } /*foreach ns*/ | ||
850 | } /*foreach retry*/ | ||
851 | res_close(); | ||
852 | if (!v_circuit) { | ||
853 | if (!gotsomewhere) | ||
854 | errno = ECONNREFUSED; /* no nameservers found */ | ||
855 | else | ||
856 | errno = ETIMEDOUT; /* no answer obtained */ | ||
857 | } else | ||
858 | errno = terrno; | ||
859 | return (-1); | ||
860 | } | ||
861 | |||
862 | /* | ||
863 | * This routine is for closing the socket if a virtual circuit is used and | ||
864 | * the program wants to close it. This provides support for endhostent() | ||
865 | * which expects to close the socket. | ||
866 | * | ||
867 | * This routine is not expected to be user visible. | ||
868 | */ | ||
869 | void | ||
870 | res_close(void) | ||
871 | { | ||
872 | if (s >= 0) { | ||
873 | (void) close(s); | ||
874 | s = -1; | ||
875 | connected = 0; | ||
876 | vc = 0; | ||
877 | af = 0; | ||
878 | } | ||
879 | } | ||