Add dl-brk.c, dl-sbrk.c, and sys/personality.h.
[glibc.git] / resolv / res_send.c
bloba163d06a0a4747d2dfc8ad43ea0404fa84395450
1 /*
2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 * SOFTWARE.
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64 * SOFTWARE.
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
69 static const char rcsid[] = "$BINDId: res_send.c,v 8.38 2000/03/30 20:16:51 vixie Exp $";
70 #endif /* LIBC_SCCS and not lint */
73 * Send query to name server and wait for reply.
76 #include <sys/types.h>
77 #include <sys/param.h>
78 #include <sys/time.h>
79 #include <sys/socket.h>
80 #include <sys/uio.h>
81 #ifdef _LIBC
82 #include <sys/poll.h>
83 #endif
85 #include <netinet/in.h>
86 #include <arpa/nameser.h>
87 #include <arpa/inet.h>
89 #include <errno.h>
90 #include <netdb.h>
91 #include <resolv.h>
92 #include <signal.h>
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <unistd.h>
98 #ifndef _LIBC
99 #include <isc/eventlib.h>
100 #else
102 /* From ev_streams.c. */
104 static inline struct iovec
105 evConsIovec(void *buf, size_t cnt) {
106 struct iovec ret;
108 memset(&ret, 0xf5, sizeof ret);
109 ret.iov_base = buf;
110 ret.iov_len = cnt;
111 return (ret);
114 /* From ev_timers.c. */
116 #define BILLION 1000000000
118 static inline struct timespec
119 evTimeSpec(struct timeval tv) {
120 struct timespec ts;
122 ts.tv_sec = tv.tv_sec;
123 ts.tv_nsec = tv.tv_usec * 1000;
124 return (ts);
127 static inline struct timespec
128 evConsTime(time_t sec, long nsec) {
129 struct timespec x;
131 x.tv_sec = sec;
132 x.tv_nsec = nsec;
133 return (x);
136 static inline struct timespec
137 evAddTime(struct timespec addend1, struct timespec addend2) {
138 struct timespec x;
140 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
141 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
142 if (x.tv_nsec >= BILLION) {
143 x.tv_sec++;
144 x.tv_nsec -= BILLION;
146 return (x);
149 static inline struct timespec
150 evSubTime(struct timespec minuend, struct timespec subtrahend) {
151 struct timespec x;
153 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
154 if (minuend.tv_nsec >= subtrahend.tv_nsec)
155 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
156 else {
157 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
158 x.tv_sec--;
160 return (x);
163 static inline int
164 evCmpTime(struct timespec a, struct timespec b) {
165 long x = a.tv_sec - b.tv_sec;
167 if (x == 0L)
168 x = a.tv_nsec - b.tv_nsec;
169 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
172 static inline struct timespec
173 evNowTime() {
174 struct timeval now;
176 if (gettimeofday(&now, NULL) < 0)
177 return (evConsTime(0, 0));
178 return (evTimeSpec(now));
181 #endif
183 /* Options. Leave them on. */
184 /* #undef DEBUG */
185 #include "res_debug.h"
187 #define EXT(res) ((res)->_u._ext)
189 #ifndef _LIBC
190 static const int highestFD = FD_SETSIZE - 1;
191 #endif
193 /* Forward. */
195 static int send_vc(res_state, const u_char *, int,
196 u_char *, int, int *, int);
197 static int send_dg(res_state, const u_char *, int,
198 u_char *, int, int *, int,
199 int *, int *);
200 #ifdef DEBUG
201 static void Aerror(const res_state, FILE *, const char *, int,
202 struct sockaddr_in);
203 static void Perror(const res_state, FILE *, const char *, int);
204 #endif
205 #ifdef _LIBC
206 static int sock_eq(struct sockaddr_in6 *, struct sockaddr_in6 *);
207 #else
208 static int sock_eq(struct sockaddr_in *, struct sockaddr_in *);
209 #endif
210 #ifdef NEED_PSELECT
211 static int pselect(int, void *, void *, void *,
212 struct timespec *,
213 const sigset_t *);
214 #endif
216 /* Reachover. */
218 #ifdef _LIBC
219 static void convaddr4to6(struct sockaddr_in6 *sa);
220 #endif
221 void res_pquery(const res_state, const u_char *, int, FILE *);
223 /* Public. */
225 /* int
226 * res_isourserver(ina)
227 * looks up "ina" in _res.ns_addr_list[]
228 * returns:
229 * 0 : not found
230 * >0 : found
231 * author:
232 * paul vixie, 29may94
235 #ifdef _LIBC
236 res_ourserver_p(const res_state statp, const struct sockaddr_in6 *inp)
237 #else
238 res_ourserver_p(const res_state statp, const struct sockaddr_in *inp)
239 #endif
241 int ns;
243 #ifdef _LIBC
244 if (inp->sin6_family == AF_INET) {
245 struct sockaddr_in *in4p = (struct sockaddr_in *) inp;
246 in_port_t port = in4p->sin_port;
247 in_addr_t addr = in4p->sin_addr.s_addr;
249 for (ns = 0; ns < MAXNS; ns++) {
250 const struct sockaddr_in *srv =
251 (struct sockaddr_in *)EXT(statp).nsaddrs[ns];
253 if ((srv != NULL) && (srv->sin_family == AF_INET) &&
254 (srv->sin_port == port) &&
255 (srv->sin_addr.s_addr == INADDR_ANY ||
256 srv->sin_addr.s_addr == addr))
257 return (1);
259 } else if (inp->sin6_family == AF_INET6) {
260 for (ns = 0; ns < MAXNS; ns++) {
261 const struct sockaddr_in6 *srv = EXT(statp).nsaddrs[ns];
262 if ((srv != NULL) && (srv->sin6_family == AF_INET6) &&
263 (srv->sin6_port == inp->sin6_port) &&
264 !(memcmp(&srv->sin6_addr, &in6addr_any,
265 sizeof (struct in6_addr)) &&
266 memcmp(&srv->sin6_addr, &inp->sin6_addr,
267 sizeof (struct in6_addr))))
268 return (1);
271 #else
272 struct sockaddr_in ina = *inp;
273 for (ns = 0; ns < statp->nscount; ns++) {
274 const struct sockaddr_in *srv = &statp->nsaddr_list[ns];
276 if (srv->sin_family == ina.sin_family &&
277 srv->sin_port == ina.sin_port &&
278 (srv->sin_addr.s_addr == INADDR_ANY ||
279 srv->sin_addr.s_addr == ina.sin_addr.s_addr))
280 return (1);
282 #endif
283 return (0);
286 /* int
287 * res_nameinquery(name, type, class, buf, eom)
288 * look for (name,type,class) in the query section of packet (buf,eom)
289 * requires:
290 * buf + HFIXEDSZ <= eom
291 * returns:
292 * -1 : format error
293 * 0 : not found
294 * >0 : found
295 * author:
296 * paul vixie, 29may94
299 res_nameinquery(const char *name, int type, int class,
300 const u_char *buf, const u_char *eom)
302 const u_char *cp = buf + HFIXEDSZ;
303 int qdcount = ntohs(((HEADER*)buf)->qdcount);
305 while (qdcount-- > 0) {
306 char tname[MAXDNAME+1];
307 int n, ttype, tclass;
309 n = dn_expand(buf, eom, cp, tname, sizeof tname);
310 if (n < 0)
311 return (-1);
312 cp += n;
313 if (cp + 2 * INT16SZ > eom)
314 return (-1);
315 ttype = ns_get16(cp); cp += INT16SZ;
316 tclass = ns_get16(cp); cp += INT16SZ;
317 if (ttype == type && tclass == class &&
318 ns_samename(tname, name) == 1)
319 return (1);
321 return (0);
324 /* int
325 * res_queriesmatch(buf1, eom1, buf2, eom2)
326 * is there a 1:1 mapping of (name,type,class)
327 * in (buf1,eom1) and (buf2,eom2)?
328 * returns:
329 * -1 : format error
330 * 0 : not a 1:1 mapping
331 * >0 : is a 1:1 mapping
332 * author:
333 * paul vixie, 29may94
336 res_queriesmatch(const u_char *buf1, const u_char *eom1,
337 const u_char *buf2, const u_char *eom2)
339 const u_char *cp = buf1 + HFIXEDSZ;
340 int qdcount = ntohs(((HEADER*)buf1)->qdcount);
342 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
343 return (-1);
346 * Only header section present in replies to
347 * dynamic update packets.
349 if ((((HEADER *)buf1)->opcode == ns_o_update) &&
350 (((HEADER *)buf2)->opcode == ns_o_update))
351 return (1);
353 if (qdcount != ntohs(((HEADER*)buf2)->qdcount))
354 return (0);
355 while (qdcount-- > 0) {
356 char tname[MAXDNAME+1];
357 int n, ttype, tclass;
359 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
360 if (n < 0)
361 return (-1);
362 cp += n;
363 if (cp + 2 * INT16SZ > eom1)
364 return (-1);
365 ttype = ns_get16(cp); cp += INT16SZ;
366 tclass = ns_get16(cp); cp += INT16SZ;
367 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
368 return (0);
370 return (1);
374 res_nsend(res_state statp,
375 const u_char *buf, int buflen, u_char *ans, int anssiz)
377 int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
379 if (statp->nscount == 0) {
380 __set_errno (ESRCH);
381 return (-1);
383 if (anssiz < HFIXEDSZ) {
384 __set_errno (EINVAL);
385 return (-1);
387 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
388 (stdout, ";; res_send()\n"), buf, buflen);
389 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
390 gotsomewhere = 0;
391 terrno = ETIMEDOUT;
394 * If the ns_addr_list in the resolver context has changed, then
395 * invalidate our cached copy and the associated timing data.
397 if (EXT(statp).nsinit) {
398 int needclose = 0;
400 if (EXT(statp).nscount != statp->nscount)
401 needclose++;
402 else
403 #ifdef _LIBC
404 for (ns = 0; ns < MAXNS; ns++) {
405 unsigned int map = EXT(statp).nsmap[ns];
406 if (map < MAXNS
407 && !sock_eq((struct sockaddr_in6 *)
408 &statp->nsaddr_list[map],
409 EXT(statp).nsaddrs[ns]))
410 #else
411 for (ns = 0; ns < statp->nscount; ns++) {
412 if (!sock_eq(&statp->nsaddr_list[ns],
413 &EXT(statp).nsaddrs[ns]))
414 #endif
416 needclose++;
417 break;
420 if (needclose)
421 res_nclose(statp);
425 * Maybe initialize our private copy of the ns_addr_list.
427 if (EXT(statp).nsinit == 0) {
428 #ifdef _LIBC
429 unsigned char map[MAXNS];
431 memset (map, MAXNS, sizeof (map));
432 for (n = 0; n < MAXNS; n++) {
433 ns = EXT(statp).nsmap[n];
434 if (ns < statp->nscount)
435 map[ns] = n;
436 else if (ns < MAXNS) {
437 free(EXT(statp).nsaddrs[n]);
438 EXT(statp).nsaddrs[n] = NULL;
439 EXT(statp).nsmap[n] = MAXNS;
442 n = statp->nscount;
443 if (statp->nscount > EXT(statp).nscount)
444 for (n = EXT(statp).nscount, ns = 0;
445 n < statp->nscount; n++) {
446 while (ns < MAXNS
447 && EXT(statp).nsmap[ns] != MAXNS)
448 ns++;
449 if (ns == MAXNS)
450 break;
451 EXT(statp).nsmap[ns] = n;
452 map[n] = ns++;
454 EXT(statp).nscount = n;
455 for (ns = 0; ns < EXT(statp).nscount; ns++) {
456 n = map[ns];
457 if (EXT(statp).nsaddrs[n] == NULL)
458 EXT(statp).nsaddrs[n] =
459 malloc(sizeof (struct sockaddr_in6));
460 if (EXT(statp).nsaddrs[n] != NULL) {
461 memcpy(EXT(statp).nsaddrs[n],
462 &statp->nsaddr_list[ns],
463 sizeof (struct sockaddr_in));
464 EXT(statp).nssocks[n] = -1;
465 n++;
468 #else
469 for (ns = 0; ns < statp->nscount; ns++) {
470 EXT(statp).nsaddrs[ns] = statp->nsaddr_list[ns];
471 EXT(statp).nssocks[ns] = -1;
473 EXT(statp).nscount = ns;
474 #endif
475 EXT(statp).nsinit = 1;
479 * Some resolvers want to even out the load on their nameservers.
480 * Note that RES_BLAST overrides RES_ROTATE.
482 if ((statp->options & RES_ROTATE) != 0 &&
483 (statp->options & RES_BLAST) == 0) {
484 #ifdef _LIBC
485 struct sockaddr_in6 *ina;
486 unsigned int map;
488 n = 0;
489 while (n < MAXNS && EXT(statp).nsmap[n] == MAXNS)
490 n++;
491 if (n < MAXNS) {
492 ina = EXT(statp).nsaddrs[n];
493 map = EXT(statp).nsmap[n];
494 for (;;) {
495 ns = n + 1;
496 while (ns < MAXNS
497 && EXT(statp).nsmap[ns] == MAXNS)
498 ns++;
499 if (ns == MAXNS)
500 break;
501 EXT(statp).nsaddrs[n] = EXT(statp).nsaddrs[ns];
502 EXT(statp).nsmap[n] = EXT(statp).nsmap[ns];
503 n = ns;
505 EXT(statp).nsaddrs[n] = ina;
506 EXT(statp).nsmap[n] = map;
508 #else
509 struct sockaddr_in ina;
510 int lastns = statp->nscount - 1;
512 ina = statp->nsaddr_list[0];
513 for (ns = 0; ns < lastns; ns++)
514 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
515 statp->nsaddr_list[lastns] = ina;
516 #endif
520 * Send request, RETRY times, or until successful.
522 for (try = 0; try < statp->retry; try++) {
523 #ifdef _LIBC
524 for (ns = 0; ns < MAXNS; ns++)
525 #else
526 for (ns = 0; ns < statp->nscount; ns++)
527 #endif
529 #ifdef _LIBC
530 struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
532 if (nsap == NULL)
533 goto next_ns;
534 #else
535 struct sockaddr_in *nsap = &statp->nsaddr_list[ns];
536 #endif
537 same_ns:
538 if (statp->qhook) {
539 int done = 0, loops = 0;
541 do {
542 res_sendhookact act;
544 #ifdef _LIBC
545 act = (*statp->qhook)((struct sockaddr_in **)
546 &nsap, &buf, &buflen,
547 ans, anssiz, &resplen);
548 #else
549 act = (*statp->qhook)(&nsap, &buf, &buflen,
550 ans, anssiz, &resplen);
551 #endif
552 switch (act) {
553 case res_goahead:
554 done = 1;
555 break;
556 case res_nextns:
557 res_nclose(statp);
558 goto next_ns;
559 case res_done:
560 return (resplen);
561 case res_modified:
562 /* give the hook another try */
563 if (++loops < 42) /*doug adams*/
564 break;
565 /*FALLTHROUGH*/
566 case res_error:
567 /*FALLTHROUGH*/
568 default:
569 return (-1);
571 } while (!done);
574 Dprint(statp->options & RES_DEBUG,
575 (stdout, ";; Querying server (# %d) address = %s\n",
576 ns + 1, inet_ntoa(nsap->sin_addr)));
578 if (v_circuit) {
579 /* Use VC; at most one attempt per server. */
580 try = statp->retry;
581 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
582 ns);
583 if (n < 0)
584 return (-1);
585 if (n == 0)
586 goto next_ns;
587 resplen = n;
588 } else {
589 /* Use datagrams. */
590 n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
591 ns, &v_circuit, &gotsomewhere);
592 if (n < 0)
593 return (-1);
594 if (n == 0)
595 goto next_ns;
596 if (v_circuit)
597 goto same_ns;
598 resplen = n;
601 Dprint((statp->options & RES_DEBUG) ||
602 ((statp->pfcode & RES_PRF_REPLY) &&
603 (statp->pfcode & RES_PRF_HEAD1)),
604 (stdout, ";; got answer:\n"));
606 DprintQ((statp->options & RES_DEBUG) ||
607 (statp->pfcode & RES_PRF_REPLY),
608 (stdout, ""),
609 ans, (resplen > anssiz) ? anssiz : resplen);
612 * If we have temporarily opened a virtual circuit,
613 * or if we haven't been asked to keep a socket open,
614 * close the socket.
616 if ((v_circuit && (statp->options & RES_USEVC) == 0) ||
617 (statp->options & RES_STAYOPEN) == 0) {
618 res_nclose(statp);
620 if (statp->rhook) {
621 int done = 0, loops = 0;
623 do {
624 res_sendhookact act;
626 #ifdef _LIBC
627 act = (*statp->rhook)((struct sockaddr_in *)
628 nsap, buf, buflen,
629 ans, anssiz, &resplen);
630 #else
631 act = (*statp->rhook)(nsap, buf, buflen,
632 ans, anssiz, &resplen);
633 #endif
634 switch (act) {
635 case res_goahead:
636 case res_done:
637 done = 1;
638 break;
639 case res_nextns:
640 res_nclose(statp);
641 goto next_ns;
642 case res_modified:
643 /* give the hook another try */
644 if (++loops < 42) /*doug adams*/
645 break;
646 /*FALLTHROUGH*/
647 case res_error:
648 /*FALLTHROUGH*/
649 default:
650 return (-1);
652 } while (!done);
655 return (resplen);
656 next_ns: ;
657 } /*foreach ns*/
658 } /*foreach retry*/
659 res_nclose(statp);
660 if (!v_circuit) {
661 if (!gotsomewhere)
662 __set_errno (ECONNREFUSED); /* no nameservers found */
663 else
664 __set_errno (ETIMEDOUT); /* no answer obtained */
665 } else
666 __set_errno (terrno);
667 return (-1);
670 /* Private */
672 static int
673 send_vc(res_state statp,
674 const u_char *buf, int buflen, u_char *ans, int anssiz,
675 int *terrno, int ns)
677 const HEADER *hp = (HEADER *) buf;
678 HEADER *anhp = (HEADER *) ans;
679 #ifdef _LIBC
680 struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
681 #else
682 struct sockaddr_in *nsap = &statp->nsaddr_list[ns];
683 #endif
684 int truncating, connreset, resplen, n;
685 struct iovec iov[2];
686 u_short len;
687 u_char *cp;
689 connreset = 0;
690 same_ns:
691 truncating = 0;
693 /* Are we still talking to whom we want to talk to? */
694 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
695 #ifdef _LIBC
696 struct sockaddr_in6 peer;
697 #else
698 struct sockaddr_in peer;
699 #endif
700 int size = sizeof peer;
702 if (getpeername(statp->_vcsock,
703 (struct sockaddr *)&peer, &size) < 0 ||
704 !sock_eq(&peer, nsap)) {
705 res_nclose(statp);
706 statp->_flags &= ~RES_F_VC;
710 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
711 if (statp->_vcsock >= 0)
712 res_nclose(statp);
714 #ifdef _LIBC
715 statp->_vcsock = socket(nsap->sin6_family, SOCK_STREAM, 0);
716 #else
717 statp->_vcsock = socket(PF_INET, SOCK_STREAM, 0);
718 if (statp->_vcsock > highestFD) {
719 res_nclose(statp);
720 __set_errno (ENOTSOCK);
722 #endif
723 if (statp->_vcsock < 0) {
724 *terrno = errno;
725 Perror(statp, stderr, "socket(vc)", errno);
726 return (-1);
728 __set_errno (0);
729 if (connect(statp->_vcsock, (struct sockaddr *)nsap,
730 sizeof *nsap) < 0) {
731 *terrno = errno;
732 Aerror(statp, stderr, "connect/vc", errno, *nsap);
733 res_nclose(statp);
734 return (0);
736 statp->_flags |= RES_F_VC;
740 * Send length & message
742 putshort((u_short)buflen, (u_char*)&len);
743 iov[0] = evConsIovec(&len, INT16SZ);
744 iov[1] = evConsIovec((void*)buf, buflen);
745 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
746 *terrno = errno;
747 Perror(statp, stderr, "write failed", errno);
748 res_nclose(statp);
749 return (0);
752 * Receive length & response
754 read_len:
755 cp = ans;
756 len = INT16SZ;
757 while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
758 cp += n;
759 if ((len -= n) <= 0)
760 break;
762 if (n <= 0) {
763 *terrno = errno;
764 Perror(statp, stderr, "read failed", errno);
765 res_nclose(statp);
767 * A long running process might get its TCP
768 * connection reset if the remote server was
769 * restarted. Requery the server instead of
770 * trying a new one. When there is only one
771 * server, this means that a query might work
772 * instead of failing. We only allow one reset
773 * per query to prevent looping.
775 if (*terrno == ECONNRESET && !connreset) {
776 connreset = 1;
777 res_nclose(statp);
778 goto same_ns;
780 res_nclose(statp);
781 return (0);
783 resplen = ns_get16(ans);
784 if (resplen > anssiz) {
785 Dprint(statp->options & RES_DEBUG,
786 (stdout, ";; response truncated\n")
788 truncating = 1;
789 len = anssiz;
790 } else
791 len = resplen;
792 if (len < HFIXEDSZ) {
794 * Undersized message.
796 Dprint(statp->options & RES_DEBUG,
797 (stdout, ";; undersized: %d\n", len));
798 *terrno = EMSGSIZE;
799 res_nclose(statp);
800 return (0);
802 cp = ans;
803 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
804 cp += n;
805 len -= n;
807 if (n <= 0) {
808 *terrno = errno;
809 Perror(statp, stderr, "read(vc)", errno);
810 res_nclose(statp);
811 return (0);
813 if (truncating) {
815 * Flush rest of answer so connection stays in synch.
817 anhp->tc = 1;
818 len = resplen - anssiz;
819 while (len != 0) {
820 char junk[PACKETSZ];
822 n = read(statp->_vcsock, junk,
823 (len > sizeof junk) ? sizeof junk : len);
824 if (n > 0)
825 len -= n;
826 else
827 break;
831 * If the calling applicating has bailed out of
832 * a previous call and failed to arrange to have
833 * the circuit closed or the server has got
834 * itself confused, then drop the packet and
835 * wait for the correct one.
837 if (hp->id != anhp->id) {
838 DprintQ((statp->options & RES_DEBUG) ||
839 (statp->pfcode & RES_PRF_REPLY),
840 (stdout, ";; old answer (unexpected):\n"),
841 ans, (resplen > anssiz) ? anssiz: resplen);
842 goto read_len;
846 * All is well, or the error is fatal. Signal that the
847 * next nameserver ought not be tried.
849 return (resplen);
852 static int
853 send_dg(res_state statp,
854 const u_char *buf, int buflen, u_char *ans, int anssiz,
855 int *terrno, int ns, int *v_circuit, int *gotsomewhere)
857 const HEADER *hp = (HEADER *) buf;
858 HEADER *anhp = (HEADER *) ans;
859 #ifdef _LIBC
860 struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
861 #else
862 const struct sockaddr_in *nsap = &statp->nsaddr_list[ns];
863 #endif
864 struct timespec now, timeout, finish;
865 #ifdef _LIBC
866 struct pollfd pfd[1];
867 int ptimeout;
868 struct sockaddr_in6 from;
869 static int socket_pf = 0;
870 #else
871 fd_set dsmask;
872 struct sockaddr_in from;
873 #endif
874 int fromlen, resplen, seconds, n, s;
876 if (EXT(statp).nssocks[ns] == -1) {
877 #ifdef _LIBC
878 /* only try IPv6 if IPv6 NS and if not failed before */
879 if ((EXT(statp).nscount6 > 0) && (socket_pf != PF_INET)) {
880 EXT(statp).nssocks[ns] =
881 socket(PF_INET6, SOCK_DGRAM, 0);
882 socket_pf = EXT(statp).nssocks[ns] < 0 ? PF_INET
883 : PF_INET6;
885 if (EXT(statp).nssocks[ns] < 0)
886 EXT(statp).nssocks[ns] = socket(PF_INET, SOCK_DGRAM, 0);
887 #else
888 EXT(statp).nssocks[ns] = socket(PF_INET, SOCK_DGRAM, 0);
889 if (EXT(statp).nssocks[ns] > highestFD) {
890 res_nclose(statp);
891 __set_errno (ENOTSOCK);
893 #endif
894 if (EXT(statp).nssocks[ns] < 0) {
895 *terrno = errno;
896 Perror(statp, stderr, "socket(dg)", errno);
897 return (-1);
899 #ifndef CANNOT_CONNECT_DGRAM
900 #ifdef _LIBC
901 /* If IPv6 socket and nsap is IPv4, make it IPv4-mapped */
902 if ((socket_pf == PF_INET6) && (nsap->sin6_family == AF_INET))
903 convaddr4to6(nsap);
904 #endif
906 * On a 4.3BSD+ machine (client and server,
907 * actually), sending to a nameserver datagram
908 * port with no nameserver will cause an
909 * ICMP port unreachable message to be returned.
910 * If our datagram socket is "connected" to the
911 * server, we get an ECONNREFUSED error on the next
912 * socket operation, and select returns if the
913 * error message is received. We can thus detect
914 * the absence of a nameserver without timing out.
916 if (connect(EXT(statp).nssocks[ns], (struct sockaddr *)nsap,
917 sizeof *nsap) < 0) {
918 Aerror(statp, stderr, "connect(dg)", errno, *nsap);
919 res_nclose(statp);
920 return (0);
922 #endif /* !CANNOT_CONNECT_DGRAM */
923 Dprint(statp->options & RES_DEBUG,
924 (stdout, ";; new DG socket\n"))
926 s = EXT(statp).nssocks[ns];
927 #ifndef CANNOT_CONNECT_DGRAM
928 if (send(s, (char*)buf, buflen, 0) != buflen) {
929 Perror(statp, stderr, "send", errno);
930 res_nclose(statp);
931 return (0);
933 #else /* !CANNOT_CONNECT_DGRAM */
934 #ifdef _LIBC
935 /* If IPv6 socket and nsap is IPv4, make it IPv4-mapped */
936 if ((socket_pf == PF_INET6) && (nsap->sin6_family == AF_INET))
937 convaddr4to6(nsap);
938 #endif
939 if (sendto(s, (char*)buf, buflen, 0,
940 (struct sockaddr *)nsap, sizeof *nsap) != buflen)
942 Aerror(statp, stderr, "sendto", errno, *nsap);
943 res_nclose(statp);
944 return (0);
946 #endif /* !CANNOT_CONNECT_DGRAM */
949 * Wait for reply.
951 seconds = (statp->retrans << ns);
952 if (ns > 0)
953 seconds /= statp->nscount;
954 if (seconds <= 0)
955 seconds = 1;
956 now = evNowTime();
957 timeout = evConsTime(seconds, 0);
958 finish = evAddTime(now, timeout);
959 wait:
960 #ifdef _LIBC
961 /* Convert struct timespec in milliseconds. */
962 ptimeout = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
964 pfd[0].fd = s;
965 pfd[0].events = POLLIN;
966 n = __poll (pfd, 1, ptimeout);
967 #else
968 FD_ZERO(&dsmask);
969 FD_SET(s, &dsmask);
970 n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
971 #endif
972 if (n == 0) {
973 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
974 *gotsomewhere = 1;
975 return (0);
977 if (n < 0) {
978 if (errno == EINTR) {
979 now = evNowTime();
980 if (evCmpTime(finish, now) > 0) {
981 timeout = evSubTime(finish, now);
982 goto wait;
985 Perror(statp, stderr, "select", errno);
986 res_nclose(statp);
987 return (0);
989 __set_errno (0);
990 #ifdef _LIBC
991 fromlen = sizeof(struct sockaddr_in6);
992 #else
993 fromlen = sizeof(struct sockaddr_in);
994 #endif
995 resplen = recvfrom(s, (char*)ans, anssiz,0,
996 (struct sockaddr *)&from, &fromlen);
997 if (resplen <= 0) {
998 Perror(statp, stderr, "recvfrom", errno);
999 res_nclose(statp);
1000 return (0);
1002 *gotsomewhere = 1;
1003 if (resplen < HFIXEDSZ) {
1005 * Undersized message.
1007 Dprint(statp->options & RES_DEBUG,
1008 (stdout, ";; undersized: %d\n",
1009 resplen));
1010 *terrno = EMSGSIZE;
1011 res_nclose(statp);
1012 return (0);
1014 if (hp->id != anhp->id) {
1016 * response from old query, ignore it.
1017 * XXX - potential security hazard could
1018 * be detected here.
1020 DprintQ((statp->options & RES_DEBUG) ||
1021 (statp->pfcode & RES_PRF_REPLY),
1022 (stdout, ";; old answer:\n"),
1023 ans, (resplen > anssiz) ? anssiz : resplen);
1024 goto wait;
1026 if (!(statp->options & RES_INSECURE1) &&
1027 !res_ourserver_p(statp, &from)) {
1029 * response from wrong server? ignore it.
1030 * XXX - potential security hazard could
1031 * be detected here.
1033 DprintQ((statp->options & RES_DEBUG) ||
1034 (statp->pfcode & RES_PRF_REPLY),
1035 (stdout, ";; not our server:\n"),
1036 ans, (resplen > anssiz) ? anssiz : resplen);
1037 goto wait;
1039 if (!(statp->options & RES_INSECURE2) &&
1040 !res_queriesmatch(buf, buf + buflen,
1041 ans, ans + anssiz)) {
1043 * response contains wrong query? ignore it.
1044 * XXX - potential security hazard could
1045 * be detected here.
1047 DprintQ((statp->options & RES_DEBUG) ||
1048 (statp->pfcode & RES_PRF_REPLY),
1049 (stdout, ";; wrong query name:\n"),
1050 ans, (resplen > anssiz) ? anssiz : resplen);
1051 goto wait;
1053 if (anhp->rcode == SERVFAIL ||
1054 anhp->rcode == NOTIMP ||
1055 anhp->rcode == REFUSED) {
1056 DprintQ(statp->options & RES_DEBUG,
1057 (stdout, "server rejected query:\n"),
1058 ans, (resplen > anssiz) ? anssiz : resplen);
1059 res_nclose(statp);
1060 /* don't retry if called from dig */
1061 if (!statp->pfcode)
1062 return (0);
1064 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1066 * To get the rest of answer,
1067 * use TCP with same server.
1069 Dprint(statp->options & RES_DEBUG,
1070 (stdout, ";; truncated answer\n"));
1071 *v_circuit = 1;
1072 res_nclose(statp);
1073 return (1);
1076 * All is well, or the error is fatal. Signal that the
1077 * next nameserver ought not be tried.
1079 return (resplen);
1082 #ifdef DEBUG
1083 static void
1084 Aerror(const res_state statp, FILE *file, const char *string, int error,
1085 struct sockaddr_in address)
1087 int save = errno;
1089 if ((statp->options & RES_DEBUG) != 0) {
1090 char tmp[sizeof "255.255.255.255"];
1092 fprintf(file, "res_send: %s ([%s].%u): %s\n",
1093 string,
1094 inet_ntop(address.sin_family, &address.sin_addr,
1095 tmp, sizeof tmp),
1096 ntohs(address.sin_port),
1097 strerror(error));
1099 __set_errno (save);
1102 static void
1103 Perror(const res_state statp, FILE *file, const char *string, int error) {
1104 int save = errno;
1106 if ((statp->options & RES_DEBUG) != 0)
1107 fprintf(file, "res_send: %s: %s\n",
1108 string, strerror(error));
1109 __set_errno (save);
1111 #endif
1113 static int
1114 #ifdef _LIBC
1115 sock_eq(struct sockaddr_in6 *a1, struct sockaddr_in6 *a2) {
1116 if (a1->sin6_family == a2->sin6_family) {
1117 if (a1->sin6_family == AF_INET)
1118 return ((((struct sockaddr_in *)a1)->sin_port ==
1119 ((struct sockaddr_in *)a2)->sin_port) &&
1120 (((struct sockaddr_in *)a1)->sin_addr.s_addr ==
1121 ((struct sockaddr_in *)a2)->sin_addr.s_addr));
1122 else
1123 return ((a1->sin6_port == a2->sin6_port) &&
1124 !memcmp(&a1->sin6_addr, &a2->sin6_addr,
1125 sizeof (struct in6_addr)));
1127 if (a1->sin6_family == AF_INET) {
1128 struct sockaddr_in6 *sap = a1;
1129 a1 = a2;
1130 a2 = sap;
1131 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1132 return ((a1->sin6_port == ((struct sockaddr_in *)a2)->sin_port) &&
1133 IN6_IS_ADDR_V4MAPPED(&a1->sin6_addr) &&
1134 (a1->sin6_addr.s6_addr32[3] ==
1135 ((struct sockaddr_in *)a2)->sin_addr.s_addr));
1137 #else
1138 sock_eq(struct sockaddr_in *a1, struct sockaddr_in *a2) {
1139 return ((a1->sin_family == a2->sin_family) &&
1140 (a1->sin_port == a2->sin_port) &&
1141 (a1->sin_addr.s_addr == a2->sin_addr.s_addr));
1143 #endif
1145 #ifdef _LIBC
1147 * Converts IPv4 family, address and port to
1148 * IPv6 family, IPv4-mapped IPv6 address and port.
1150 static void
1151 convaddr4to6(struct sockaddr_in6 *sa)
1153 struct sockaddr_in *sa4p = (struct sockaddr_in *) sa;
1154 in_port_t port = sa4p->sin_port;
1155 in_addr_t addr = sa4p->sin_addr.s_addr;
1157 sa->sin6_family = AF_INET6;
1158 sa->sin6_port = port;
1159 sa->sin6_addr.s6_addr32[0] = 0;
1160 sa->sin6_addr.s6_addr32[1] = 0;
1161 sa->sin6_addr.s6_addr32[2] = htonl(0xFFFF);
1162 sa->sin6_addr.s6_addr32[3] = addr;
1164 #endif
1166 #ifdef NEED_PSELECT
1167 /* XXX needs to move to the porting library. */
1168 static int
1169 pselect(int nfds, void *rfds, void *wfds, void *efds,
1170 struct timespec *tsp, const sigset_t *sigmask)
1172 struct timeval tv, *tvp;
1173 sigset_t sigs;
1174 int n;
1176 if (tsp) {
1177 tvp = &tv;
1178 tv = evTimeVal(*tsp);
1179 } else
1180 tvp = NULL;
1181 if (sigmask)
1182 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1183 n = select(nfds, rfds, wfds, efds, tvp);
1184 if (sigmask)
1185 sigprocmask(SIG_SETMASK, &sigs, NULL);
1186 if (tsp)
1187 *tsp = evTimeSpec(tv);
1188 return (n);
1190 #endif