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