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