Annotation of src/lib/libc/resolv/res_send.c, Revision 1.30
1.30 ! christos 1: /* $NetBSD: res_send.c,v 1.29 2013/02/16 13:45:45 para Exp $ */
1.18 christos 2:
3: /*
4: * Portions Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5: * Portions Copyright (C) 1996-2003 Internet Software Consortium.
6: *
7: * Permission to use, copy, modify, and/or distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13: * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16: * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
1.1 christos 19:
20: /*
21: * Copyright (c) 1985, 1989, 1993
22: * The Regents of the University of California. All rights reserved.
1.18 christos 23: *
1.1 christos 24: * Redistribution and use in source and binary forms, with or without
25: * modification, are permitted provided that the following conditions
26: * are met:
27: * 1. Redistributions of source code must retain the above copyright
28: * notice, this list of conditions and the following disclaimer.
29: * 2. Redistributions in binary form must reproduce the above copyright
30: * notice, this list of conditions and the following disclaimer in the
31: * documentation and/or other materials provided with the distribution.
1.30 ! christos 32: * 3. Neither the name of the University nor the names of its contributors
1.1 christos 33: * may be used to endorse or promote products derived from this software
34: * without specific prior written permission.
1.18 christos 35: *
1.1 christos 36: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46: * SUCH DAMAGE.
47: */
48:
49: /*
50: * Portions Copyright (c) 1993 by Digital Equipment Corporation.
1.18 christos 51: *
1.1 christos 52: * Permission to use, copy, modify, and distribute this software for any
53: * purpose with or without fee is hereby granted, provided that the above
54: * copyright notice and this permission notice appear in all copies, and that
55: * the name of Digital Equipment Corporation not be used in advertising or
56: * publicity pertaining to distribution of the document or software without
57: * specific, written prior permission.
1.18 christos 58: *
1.1 christos 59: * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
60: * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
61: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
62: * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
63: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
64: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
65: * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
66: * SOFTWARE.
67: */
68:
69: /*
1.10 christos 70: * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
1.1 christos 71: * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
72: *
73: * Permission to use, copy, modify, and distribute this software for any
74: * purpose with or without fee is hereby granted, provided that the above
75: * copyright notice and this permission notice appear in all copies.
76: *
77: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
78: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
79: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
80: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
81: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
82: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
83: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
84: */
85:
1.4 christos 86: #include <sys/cdefs.h>
1.1 christos 87: #if defined(LIBC_SCCS) && !defined(lint)
1.4 christos 88: #ifdef notdef
1.1 christos 89: static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
1.18 christos 90: static const char rcsid[] = "Id: res_send.c,v 1.22 2009/01/22 23:49:23 tbox Exp";
1.4 christos 91: #else
1.30 ! christos 92: __RCSID("$NetBSD: res_send.c,v 1.29 2013/02/16 13:45:45 para Exp $");
1.4 christos 93: #endif
1.1 christos 94: #endif /* LIBC_SCCS and not lint */
95:
1.10 christos 96: /*! \file
97: * \brief
1.1 christos 98: * Send query to name server and wait for reply.
99: */
100:
1.8 kleink 101: #include "namespace.h"
1.1 christos 102: #include "port_before.h"
1.26 christos 103: #ifndef USE_KQUEUE
1.1 christos 104: #include "fd_setsize.h"
1.26 christos 105: #endif /* USE_KQUEUE */
1.1 christos 106:
107: #include <sys/types.h>
108: #include <sys/param.h>
109: #include <sys/time.h>
110: #include <sys/socket.h>
111: #include <sys/uio.h>
1.26 christos 112: #ifdef USE_KQUEUE
113: #include <sys/event.h>
114: #endif /* USE_KQUEUE */
1.1 christos 115:
116: #include <netinet/in.h>
117: #include <arpa/nameser.h>
118: #include <arpa/inet.h>
119:
1.23 christos 120: #include <assert.h>
1.1 christos 121: #include <errno.h>
122: #include <netdb.h>
123: #include <resolv.h>
124: #include <signal.h>
125: #include <stdio.h>
1.26 christos 126: #include <fcntl.h>
1.1 christos 127: #include <stdlib.h>
128: #include <string.h>
129: #include <unistd.h>
130:
131: #include <isc/eventlib.h>
132:
133: #include "port_after.h"
134:
1.6 christos 135: #if 0
1.5 christos 136: #ifdef __weak_alias
137: __weak_alias(res_ourserver_p,__res_ourserver_p)
138: __weak_alias(res_nameinquery,__res_nameinquery)
139: __weak_alias(res_queriesmatch,__res_queriesmatch)
140: __weak_alias(res_nsend,__res_nsend)
141: #endif
1.6 christos 142: #endif
1.5 christos 143:
1.26 christos 144: #ifndef SOCK_NOSIGPIPE
145: #define SOCK_NOSIGPIPE 0
146: #endif
147: #ifndef SOCK_NOCLOEXEC
148: #define SOCK_NOCLOEXEC 0
149: #endif
1.10 christos 150:
151: #ifdef USE_POLL
152: #ifdef HAVE_STROPTS_H
153: #include <stropts.h>
154: #endif
155: #include <poll.h>
156: #endif /* USE_POLL */
157:
1.1 christos 158: /* Options. Leave them on. */
1.15 christos 159: #ifndef DEBUG
1.1 christos 160: #define DEBUG
1.15 christos 161: #endif
1.1 christos 162: #include "res_debug.h"
163: #include "res_private.h"
164:
165: #define EXT(res) ((res)->_u._ext)
166:
1.26 christos 167: #if !defined(USE_POLL) && !defined(USE_KQUEUE)
1.1 christos 168: static const int highestFD = FD_SETSIZE - 1;
1.10 christos 169: #endif
1.1 christos 170:
171: /* Forward. */
172:
1.27 christos 173: static socklen_t get_salen(const struct sockaddr *);
1.24 matt 174: static struct sockaddr * get_nsaddr(res_state, size_t);
1.28 christos 175: static int send_vc(res_state, const void *, size_t,
1.1 christos 176: u_char *, int, int *, int);
1.26 christos 177: static int send_dg(res_state,
178: #ifdef USE_KQUEUE
179: int,
180: #endif
1.27 christos 181: const void *, size_t,
1.10 christos 182: u_char *, int, int *, int, int,
1.1 christos 183: int *, int *);
184: static void Aerror(const res_state, FILE *, const char *, int,
1.27 christos 185: const struct sockaddr *, socklen_t);
1.1 christos 186: static void Perror(const res_state, FILE *, const char *, int);
187: static int sock_eq(struct sockaddr *, struct sockaddr *);
1.26 christos 188: #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1.1 christos 189: static int pselect(int, void *, void *, void *,
190: struct timespec *,
191: const sigset_t *);
192: #endif
193: void res_pquery(const res_state, const u_char *, int, FILE *);
194:
195: static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
196:
197: /* Public. */
198:
1.10 christos 199: /*%
1.1 christos 200: * looks up "ina" in _res.ns_addr_list[]
1.10 christos 201: *
1.1 christos 202: * returns:
1.10 christos 203: *\li 0 : not found
204: *\li >0 : found
205: *
1.1 christos 206: * author:
1.10 christos 207: *\li paul vixie, 29may94
1.1 christos 208: */
209: int
210: res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
211: const struct sockaddr_in *inp, *srv;
212: const struct sockaddr_in6 *in6p, *srv6;
213: int ns;
214:
215: switch (sa->sa_family) {
216: case AF_INET:
1.3 christos 217: inp = (const struct sockaddr_in *)(const void *)sa;
1.1 christos 218: for (ns = 0; ns < statp->nscount; ns++) {
1.2 christos 219: srv = (struct sockaddr_in *)(void *)get_nsaddr(statp, (size_t)ns);
1.1 christos 220: if (srv->sin_family == inp->sin_family &&
221: srv->sin_port == inp->sin_port &&
222: (srv->sin_addr.s_addr == INADDR_ANY ||
223: srv->sin_addr.s_addr == inp->sin_addr.s_addr))
224: return (1);
225: }
226: break;
227: case AF_INET6:
228: if (EXT(statp).ext == NULL)
229: break;
1.3 christos 230: in6p = (const struct sockaddr_in6 *)(const void *)sa;
1.1 christos 231: for (ns = 0; ns < statp->nscount; ns++) {
1.2 christos 232: srv6 = (struct sockaddr_in6 *)(void *)get_nsaddr(statp, (size_t)ns);
1.1 christos 233: if (srv6->sin6_family == in6p->sin6_family &&
234: srv6->sin6_port == in6p->sin6_port &&
235: #ifdef HAVE_SIN6_SCOPE_ID
1.7 christos 236: (srv6->sin6_scope_id == 0 ||
237: srv6->sin6_scope_id == in6p->sin6_scope_id) &&
1.1 christos 238: #endif
239: (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
240: IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
241: return (1);
242: }
243: break;
244: default:
245: break;
246: }
247: return (0);
248: }
249:
1.10 christos 250: /*%
1.1 christos 251: * look for (name,type,class) in the query section of packet (buf,eom)
1.10 christos 252: *
1.1 christos 253: * requires:
1.10 christos 254: *\li buf + HFIXEDSZ <= eom
255: *
1.1 christos 256: * returns:
1.10 christos 257: *\li -1 : format error
258: *\li 0 : not found
259: *\li >0 : found
260: *
1.1 christos 261: * author:
1.10 christos 262: *\li paul vixie, 29may94
1.1 christos 263: */
264: int
265: res_nameinquery(const char *name, int type, int class,
266: const u_char *buf, const u_char *eom)
267: {
268: const u_char *cp = buf + HFIXEDSZ;
1.2 christos 269: int qdcount = ntohs(((const HEADER*)(const void *)buf)->qdcount);
1.1 christos 270:
271: while (qdcount-- > 0) {
272: char tname[MAXDNAME+1];
273: int n, ttype, tclass;
274:
1.23 christos 275: n = dn_expand(buf, eom, cp, tname, (int)sizeof tname);
1.1 christos 276: if (n < 0)
277: return (-1);
278: cp += n;
279: if (cp + 2 * INT16SZ > eom)
280: return (-1);
281: ttype = ns_get16(cp); cp += INT16SZ;
282: tclass = ns_get16(cp); cp += INT16SZ;
283: if (ttype == type && tclass == class &&
284: ns_samename(tname, name) == 1)
285: return (1);
286: }
287: return (0);
288: }
289:
1.10 christos 290: /*%
1.1 christos 291: * is there a 1:1 mapping of (name,type,class)
292: * in (buf1,eom1) and (buf2,eom2)?
1.10 christos 293: *
1.1 christos 294: * returns:
1.10 christos 295: *\li -1 : format error
296: *\li 0 : not a 1:1 mapping
297: *\li >0 : is a 1:1 mapping
298: *
1.1 christos 299: * author:
1.10 christos 300: *\li paul vixie, 29may94
1.1 christos 301: */
302: int
303: res_queriesmatch(const u_char *buf1, const u_char *eom1,
304: const u_char *buf2, const u_char *eom2)
305: {
306: const u_char *cp = buf1 + HFIXEDSZ;
1.2 christos 307: int qdcount = ntohs(((const HEADER*)(const void *)buf1)->qdcount);
1.1 christos 308:
309: if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
310: return (-1);
311:
312: /*
313: * Only header section present in replies to
314: * dynamic update packets.
315: */
1.2 christos 316: if ((((const HEADER *)(const void *)buf1)->opcode == ns_o_update) &&
317: (((const HEADER *)(const void *)buf2)->opcode == ns_o_update))
1.1 christos 318: return (1);
319:
1.2 christos 320: if (qdcount != ntohs(((const HEADER*)(const void *)buf2)->qdcount))
1.1 christos 321: return (0);
322: while (qdcount-- > 0) {
323: char tname[MAXDNAME+1];
324: int n, ttype, tclass;
325:
1.23 christos 326: n = dn_expand(buf1, eom1, cp, tname, (int)sizeof tname);
1.1 christos 327: if (n < 0)
328: return (-1);
329: cp += n;
330: if (cp + 2 * INT16SZ > eom1)
331: return (-1);
332: ttype = ns_get16(cp); cp += INT16SZ;
333: tclass = ns_get16(cp); cp += INT16SZ;
334: if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
335: return (0);
336: }
337: return (1);
338: }
339:
340: int
341: res_nsend(res_state statp,
342: const u_char *buf, int buflen, u_char *ans, int anssiz)
343: {
1.14 christos 344: int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
1.26 christos 345: #ifdef USE_KQUEUE
346: int kq;
347: #endif
1.1 christos 348: char abuf[NI_MAXHOST];
349:
1.20 christos 350: (void)res_check(statp, NULL);
1.19 christos 351:
1.10 christos 352: /* No name servers or res_init() failure */
353: if (statp->nscount == 0 || EXT(statp).ext == NULL) {
1.1 christos 354: errno = ESRCH;
355: return (-1);
356: }
357: if (anssiz < HFIXEDSZ) {
358: errno = EINVAL;
359: return (-1);
360: }
361: DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
362: (stdout, ";; res_send()\n"), buf, buflen);
363: v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
364: gotsomewhere = 0;
365: terrno = ETIMEDOUT;
366:
1.26 christos 367: #ifdef USE_KQUEUE
368: if ((kq = kqueue1(O_CLOEXEC)) == -1) {
369: return (-1);
370: }
371: #endif
372:
1.1 christos 373: /*
374: * If the ns_addr_list in the resolver context has changed, then
375: * invalidate our cached copy and the associated timing data.
376: */
377: if (EXT(statp).nscount != 0) {
378: int needclose = 0;
379: struct sockaddr_storage peer;
380: ISC_SOCKLEN_T peerlen;
381:
382: if (EXT(statp).nscount != statp->nscount)
383: needclose++;
384: else
385: for (ns = 0; ns < statp->nscount; ns++) {
386: if (statp->nsaddr_list[ns].sin_family &&
1.2 christos 387: !sock_eq((struct sockaddr *)(void *)&statp->nsaddr_list[ns],
388: (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[ns])) {
1.1 christos 389: needclose++;
390: break;
391: }
392:
393: if (EXT(statp).nssocks[ns] == -1)
394: continue;
395: peerlen = sizeof(peer);
1.17 mrg 396: if (getpeername(EXT(statp).nssocks[ns],
1.2 christos 397: (struct sockaddr *)(void *)&peer, &peerlen) < 0) {
1.1 christos 398: needclose++;
399: break;
400: }
1.2 christos 401: if (!sock_eq((struct sockaddr *)(void *)&peer,
402: get_nsaddr(statp, (size_t)ns))) {
1.1 christos 403: needclose++;
404: break;
405: }
406: }
407: if (needclose) {
408: res_nclose(statp);
409: EXT(statp).nscount = 0;
410: }
411: }
412:
413: /*
414: * Maybe initialize our private copy of the ns_addr_list.
415: */
416: if (EXT(statp).nscount == 0) {
417: for (ns = 0; ns < statp->nscount; ns++) {
418: EXT(statp).nstimes[ns] = RES_MAXTIME;
419: EXT(statp).nssocks[ns] = -1;
420: if (!statp->nsaddr_list[ns].sin_family)
421: continue;
422: EXT(statp).ext->nsaddrs[ns].sin =
423: statp->nsaddr_list[ns];
424: }
425: EXT(statp).nscount = statp->nscount;
426: }
427:
428: /*
429: * Some resolvers want to even out the load on their nameservers.
430: * Note that RES_BLAST overrides RES_ROTATE.
431: */
432: if ((statp->options & RES_ROTATE) != 0U &&
433: (statp->options & RES_BLAST) == 0U) {
434: union res_sockaddr_union inu;
435: struct sockaddr_in ina;
436: int lastns = statp->nscount - 1;
437: int fd;
438: u_int16_t nstime;
439:
440: if (EXT(statp).ext != NULL)
441: inu = EXT(statp).ext->nsaddrs[0];
442: ina = statp->nsaddr_list[0];
443: fd = EXT(statp).nssocks[0];
444: nstime = EXT(statp).nstimes[0];
445: for (ns = 0; ns < lastns; ns++) {
446: if (EXT(statp).ext != NULL)
1.18 christos 447: EXT(statp).ext->nsaddrs[ns] =
1.1 christos 448: EXT(statp).ext->nsaddrs[ns + 1];
449: statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
450: EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
451: EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
452: }
453: if (EXT(statp).ext != NULL)
454: EXT(statp).ext->nsaddrs[lastns] = inu;
455: statp->nsaddr_list[lastns] = ina;
456: EXT(statp).nssocks[lastns] = fd;
457: EXT(statp).nstimes[lastns] = nstime;
458: }
459:
460: /*
461: * Send request, RETRY times, or until successful.
462: */
1.14 christos 463: for (tries = 0; tries < statp->retry; tries++) {
1.1 christos 464: for (ns = 0; ns < statp->nscount; ns++) {
465: struct sockaddr *nsap;
1.27 christos 466: socklen_t nsaplen;
1.2 christos 467: nsap = get_nsaddr(statp, (size_t)ns);
1.1 christos 468: nsaplen = get_salen(nsap);
469: statp->_flags &= ~RES_F_LASTMASK;
470: statp->_flags |= (ns << RES_F_LASTSHIFT);
471: same_ns:
472: if (statp->qhook) {
473: int done = 0, loops = 0;
474:
475: do {
476: res_sendhookact act;
477:
478: act = (*statp->qhook)(&nsap, &buf, &buflen,
479: ans, anssiz, &resplen);
480: switch (act) {
481: case res_goahead:
482: done = 1;
483: break;
484: case res_nextns:
485: res_nclose(statp);
486: goto next_ns;
487: case res_done:
1.26 christos 488: #ifdef USE_KQUEUE
489: close(kq);
490: #endif
1.1 christos 491: return (resplen);
492: case res_modified:
493: /* give the hook another try */
494: if (++loops < 42) /*doug adams*/
495: break;
496: /*FALLTHROUGH*/
497: case res_error:
498: /*FALLTHROUGH*/
499: default:
500: goto fail;
501: }
502: } while (!done);
503: }
504:
505: Dprint(((statp->options & RES_DEBUG) &&
1.27 christos 506: getnameinfo(nsap, nsaplen, abuf,
1.23 christos 507: (socklen_t)sizeof(abuf), NULL, 0, niflags) == 0),
1.1 christos 508: (stdout, ";; Querying server (# %d) address = %s\n",
509: ns + 1, abuf));
510:
511:
512: if (v_circuit) {
513: /* Use VC; at most one attempt per server. */
1.14 christos 514: tries = statp->retry;
1.29 para 515: n = send_vc(statp, buf, (size_t)buflen, ans, anssiz, &terrno,
1.1 christos 516: ns);
517: if (n < 0)
518: goto fail;
519: if (n == 0)
520: goto next_ns;
521: resplen = n;
522: } else {
523: /* Use datagrams. */
1.26 christos 524: n = send_dg(statp,
525: #ifdef USE_KQUEUE
526: kq,
527: #endif
1.27 christos 528: buf, (size_t)buflen, ans, anssiz, &terrno,
1.26 christos 529: ns, tries, &v_circuit, &gotsomewhere);
1.1 christos 530: if (n < 0)
531: goto fail;
532: if (n == 0)
533: goto next_ns;
534: if (v_circuit)
535: goto same_ns;
536: resplen = n;
537: }
538:
539: Dprint((statp->options & RES_DEBUG) ||
540: ((statp->pfcode & RES_PRF_REPLY) &&
541: (statp->pfcode & RES_PRF_HEAD1)),
542: (stdout, ";; got answer:\n"));
543:
544: DprintQ((statp->options & RES_DEBUG) ||
545: (statp->pfcode & RES_PRF_REPLY),
546: (stdout, "%s", ""),
547: ans, (resplen > anssiz) ? anssiz : resplen);
548:
549: /*
550: * If we have temporarily opened a virtual circuit,
551: * or if we haven't been asked to keep a socket open,
552: * close the socket.
553: */
554: if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
555: (statp->options & RES_STAYOPEN) == 0U) {
556: res_nclose(statp);
557: }
558: if (statp->rhook) {
559: int done = 0, loops = 0;
560:
561: do {
562: res_sendhookact act;
563:
564: act = (*statp->rhook)(nsap, buf, buflen,
565: ans, anssiz, &resplen);
566: switch (act) {
567: case res_goahead:
568: case res_done:
569: done = 1;
570: break;
571: case res_nextns:
572: res_nclose(statp);
573: goto next_ns;
574: case res_modified:
575: /* give the hook another try */
576: if (++loops < 42) /*doug adams*/
577: break;
578: /*FALLTHROUGH*/
579: case res_error:
580: /*FALLTHROUGH*/
581: default:
582: goto fail;
583: }
584: } while (!done);
585:
586: }
1.26 christos 587: #ifdef USE_KQUEUE
588: close(kq);
589: #endif
1.1 christos 590: return (resplen);
591: next_ns: ;
592: } /*foreach ns*/
593: } /*foreach retry*/
594: res_nclose(statp);
1.26 christos 595: #ifdef USE_KQUEUE
596: close(kq);
597: #endif
1.1 christos 598: if (!v_circuit) {
599: if (!gotsomewhere)
1.10 christos 600: errno = ECONNREFUSED; /*%< no nameservers found */
1.1 christos 601: else
1.10 christos 602: errno = ETIMEDOUT; /*%< no answer obtained */
1.1 christos 603: } else
604: errno = terrno;
605: return (-1);
606: fail:
607: res_nclose(statp);
1.26 christos 608: #ifdef USE_KQUEUE
609: close(kq);
610: #endif
1.1 christos 611: return (-1);
612: }
613:
614: /* Private */
615:
1.27 christos 616: static socklen_t
1.24 matt 617: get_salen(const struct sockaddr *sa)
1.1 christos 618: {
619:
620: #ifdef HAVE_SA_LEN
621: /* There are people do not set sa_len. Be forgiving to them. */
622: if (sa->sa_len)
1.27 christos 623: return (socklen_t)sa->sa_len;
1.1 christos 624: #endif
625:
626: if (sa->sa_family == AF_INET)
1.27 christos 627: return (socklen_t)sizeof(struct sockaddr_in);
1.1 christos 628: else if (sa->sa_family == AF_INET6)
1.27 christos 629: return (socklen_t)sizeof(struct sockaddr_in6);
1.1 christos 630: else
1.27 christos 631: return 0; /*%< unknown, die on connect */
1.1 christos 632: }
633:
1.10 christos 634: /*%
1.1 christos 635: * pick appropriate nsaddr_list for use. see res_init() for initialization.
636: */
637: static struct sockaddr *
1.24 matt 638: get_nsaddr(res_state statp, size_t n)
1.1 christos 639: {
640:
641: if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
642: /*
643: * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
644: * than struct sockaddr, and
645: * - user code did not update statp->nsaddr_list[n].
646: */
647: return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
648: } else {
649: /*
650: * - user code updated statp->nsaddr_list[n], or
651: * - statp->nsaddr_list[n] has the same content as
652: * EXT(statp).ext->nsaddrs[n].
653: */
654: return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
655: }
656: }
657:
658: static int
659: send_vc(res_state statp,
1.28 christos 660: const void *buf, size_t buflen, u_char *ans, int anssiz,
1.1 christos 661: int *terrno, int ns)
662: {
1.3 christos 663: const HEADER *hp = (const HEADER *)(const void *)buf;
664: HEADER *anhp = (HEADER *)(void *)ans;
1.1 christos 665: struct sockaddr *nsap;
1.27 christos 666: socklen_t nsaplen;
1.23 christos 667: int truncating, connreset, resplen;
668: ssize_t n;
1.1 christos 669: struct iovec iov[2];
670: u_short len;
671: u_char *cp;
672: void *tmp;
1.26 christos 673: #if defined(SO_NOSIGPIPE) && SOCK_NOSIGPIPE == 0
1.14 christos 674: int on = 1;
675: #endif
1.1 christos 676:
1.2 christos 677: nsap = get_nsaddr(statp, (size_t)ns);
1.1 christos 678: nsaplen = get_salen(nsap);
679:
680: connreset = 0;
681: same_ns:
682: truncating = 0;
683:
684: /* Are we still talking to whom we want to talk to? */
685: if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
686: struct sockaddr_storage peer;
687: ISC_SOCKLEN_T size = sizeof peer;
688:
689: if (getpeername(statp->_vcsock,
1.2 christos 690: (struct sockaddr *)(void *)&peer, &size) < 0 ||
691: !sock_eq((struct sockaddr *)(void *)&peer, nsap)) {
1.1 christos 692: res_nclose(statp);
693: statp->_flags &= ~RES_F_VC;
694: }
695: }
696:
697: if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
698: if (statp->_vcsock >= 0)
699: res_nclose(statp);
700:
1.26 christos 701: statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM
702: | SOCK_NOSIGPIPE | SOCK_CLOEXEC, 0);
703: #if SOCK_CLOEXEC == 0
704: fcntl(statp->_vcsock, F_SETFD, FD_CLOEXEC);
705: #endif
706: #if !defined(USE_POLL) && !defined(USE_KQUEUE)
1.1 christos 707: if (statp->_vcsock > highestFD) {
708: res_nclose(statp);
709: errno = ENOTSOCK;
710: }
1.21 pooka 711: #endif
1.1 christos 712: if (statp->_vcsock < 0) {
713: switch (errno) {
714: case EPROTONOSUPPORT:
715: #ifdef EPFNOSUPPORT
716: case EPFNOSUPPORT:
717: #endif
718: case EAFNOSUPPORT:
719: Perror(statp, stderr, "socket(vc)", errno);
720: return (0);
721: default:
722: *terrno = errno;
723: Perror(statp, stderr, "socket(vc)", errno);
724: return (-1);
725: }
726: }
1.26 christos 727: #if defined(SO_NOSIGPIPE) && SOCK_NOSIGPIPE == 0
1.14 christos 728: /*
729: * Disable generation of SIGPIPE when writing to a closed
730: * socket. Write should return -1 and set errno to EPIPE
1.18 christos 731: * instead.
1.14 christos 732: *
733: * Push on even if setsockopt(SO_NOSIGPIPE) fails.
734: */
735: (void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
1.26 christos 736: (socklen_t)sizeof(on));
1.14 christos 737: #endif
1.1 christos 738: errno = 0;
1.27 christos 739: if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
1.1 christos 740: *terrno = errno;
741: Aerror(statp, stderr, "connect/vc", errno, nsap,
742: nsaplen);
743: res_nclose(statp);
744: return (0);
745: }
746: statp->_flags |= RES_F_VC;
747: }
748:
749: /*
750: * Send length & message
751: */
1.2 christos 752: ns_put16((u_short)buflen, (u_char*)(void *)&len);
1.1 christos 753: iov[0] = evConsIovec(&len, INT16SZ);
754: DE_CONST(buf, tmp);
1.29 para 755: iov[1] = evConsIovec(tmp, (size_t)buflen);
1.27 christos 756: if (writev(statp->_vcsock, iov, 2) != (ssize_t)(INT16SZ + buflen)) {
1.1 christos 757: *terrno = errno;
758: Perror(statp, stderr, "write failed", errno);
759: res_nclose(statp);
760: return (0);
761: }
762: /*
763: * Receive length & response
764: */
765: read_len:
766: cp = ans;
767: len = INT16SZ;
1.2 christos 768: while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
1.1 christos 769: cp += n;
1.23 christos 770: if ((len -= (u_short)n) == 0)
1.1 christos 771: break;
772: }
773: if (n <= 0) {
774: *terrno = errno;
775: Perror(statp, stderr, "read failed", errno);
776: res_nclose(statp);
777: /*
778: * A long running process might get its TCP
779: * connection reset if the remote server was
780: * restarted. Requery the server instead of
781: * trying a new one. When there is only one
782: * server, this means that a query might work
783: * instead of failing. We only allow one reset
784: * per query to prevent looping.
785: */
786: if (*terrno == ECONNRESET && !connreset) {
787: connreset = 1;
788: res_nclose(statp);
789: goto same_ns;
790: }
791: res_nclose(statp);
792: return (0);
793: }
794: resplen = ns_get16(ans);
795: if (resplen > anssiz) {
796: Dprint(statp->options & RES_DEBUG,
797: (stdout, ";; response truncated\n")
798: );
799: truncating = 1;
800: len = anssiz;
801: } else
802: len = resplen;
803: if (len < HFIXEDSZ) {
804: /*
805: * Undersized message.
806: */
807: Dprint(statp->options & RES_DEBUG,
808: (stdout, ";; undersized: %d\n", len));
809: *terrno = EMSGSIZE;
810: res_nclose(statp);
811: return (0);
812: }
813: cp = ans;
1.2 christos 814: while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
1.1 christos 815: cp += n;
1.23 christos 816: len -= (u_short)n;
1.1 christos 817: }
818: if (n <= 0) {
819: *terrno = errno;
820: Perror(statp, stderr, "read(vc)", errno);
821: res_nclose(statp);
822: return (0);
823: }
824: if (truncating) {
825: /*
826: * Flush rest of answer so connection stays in synch.
827: */
828: anhp->tc = 1;
829: len = resplen - anssiz;
830: while (len != 0) {
831: char junk[PACKETSZ];
832:
833: n = read(statp->_vcsock, junk,
834: (len > sizeof junk) ? sizeof junk : len);
835: if (n > 0)
1.23 christos 836: len -= (u_short)n;
1.1 christos 837: else
838: break;
839: }
840: }
841: /*
842: * If the calling applicating has bailed out of
843: * a previous call and failed to arrange to have
844: * the circuit closed or the server has got
845: * itself confused, then drop the packet and
846: * wait for the correct one.
847: */
848: if (hp->id != anhp->id) {
849: DprintQ((statp->options & RES_DEBUG) ||
850: (statp->pfcode & RES_PRF_REPLY),
851: (stdout, ";; old answer (unexpected):\n"),
852: ans, (resplen > anssiz) ? anssiz: resplen);
853: goto read_len;
854: }
855:
856: /*
857: * All is well, or the error is fatal. Signal that the
858: * next nameserver ought not be tried.
859: */
860: return (resplen);
861: }
862:
863: static int
1.26 christos 864: send_dg(res_state statp,
865: #ifdef USE_KQUEUE
866: int kq,
867: #endif
1.27 christos 868: const void *buf, size_t buflen, u_char *ans,
1.14 christos 869: int anssiz, int *terrno, int ns, int tries, int *v_circuit,
1.10 christos 870: int *gotsomewhere)
1.1 christos 871: {
1.2 christos 872: const HEADER *hp = (const HEADER *)(const void *)buf;
873: HEADER *anhp = (HEADER *)(void *)ans;
1.1 christos 874: const struct sockaddr *nsap;
1.27 christos 875: socklen_t nsaplen;
1.1 christos 876: struct timespec now, timeout, finish;
877: struct sockaddr_storage from;
878: ISC_SOCKLEN_T fromlen;
1.23 christos 879: ssize_t resplen;
880: int seconds, n, s;
1.26 christos 881: #ifdef USE_KQUEUE
882: struct kevent kv;
883: #else
1.10 christos 884: #ifdef USE_POLL
885: int polltimeout;
886: struct pollfd pollfd;
887: #else
888: fd_set dsmask;
889: #endif
1.26 christos 890: #endif
1.1 christos 891:
1.2 christos 892: nsap = get_nsaddr(statp, (size_t)ns);
1.1 christos 893: nsaplen = get_salen(nsap);
894: if (EXT(statp).nssocks[ns] == -1) {
1.26 christos 895: EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM
896: | SOCK_CLOEXEC, 0);
897: #if SOCK_CLOEXEC == 0
898: fcntl(EXT(statp)nssocks[ns], F_SETFD, FD_CLOEXEC);
899: #endif
900: #if !defined(USE_POLL) && !defined(USE_KQUEUE)
1.1 christos 901: if (EXT(statp).nssocks[ns] > highestFD) {
902: res_nclose(statp);
903: errno = ENOTSOCK;
904: }
1.21 pooka 905: #endif
1.1 christos 906: if (EXT(statp).nssocks[ns] < 0) {
907: switch (errno) {
908: case EPROTONOSUPPORT:
909: #ifdef EPFNOSUPPORT
910: case EPFNOSUPPORT:
911: #endif
912: case EAFNOSUPPORT:
913: Perror(statp, stderr, "socket(dg)", errno);
914: return (0);
915: default:
916: *terrno = errno;
917: Perror(statp, stderr, "socket(dg)", errno);
918: return (-1);
919: }
920: }
921: #ifndef CANNOT_CONNECT_DGRAM
922: /*
923: * On a 4.3BSD+ machine (client and server,
924: * actually), sending to a nameserver datagram
925: * port with no nameserver will cause an
926: * ICMP port unreachable message to be returned.
927: * If our datagram socket is "connected" to the
928: * server, we get an ECONNREFUSED error on the next
929: * socket operation, and select returns if the
930: * error message is received. We can thus detect
931: * the absence of a nameserver without timing out.
1.26 christos 932: *
933: * When the option "insecure1" is specified, we'd
934: * rather expect to see responses from an "unknown"
935: * address. In order to let the kernel accept such
936: * responses, do not connect the socket here.
937: * XXX: or do we need an explicit option to disable
938: * connecting?
939: */
940: if (!(statp->options & RES_INSECURE1) &&
1.27 christos 941: connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
1.1 christos 942: Aerror(statp, stderr, "connect(dg)", errno, nsap,
943: nsaplen);
944: res_nclose(statp);
945: return (0);
946: }
947: #endif /* !CANNOT_CONNECT_DGRAM */
948: Dprint(statp->options & RES_DEBUG,
949: (stdout, ";; new DG socket\n"))
950: }
951: s = EXT(statp).nssocks[ns];
952: #ifndef CANNOT_CONNECT_DGRAM
1.26 christos 953: if (statp->options & RES_INSECURE1) {
1.27 christos 954: if (sendto(s, buf, buflen, 0, nsap, nsaplen) !=
955: (ssize_t)buflen) {
1.26 christos 956: Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
957: res_nclose(statp);
958: return (0);
959: }
1.27 christos 960: } else if (send(s, buf, buflen, 0) != (ssize_t)buflen) {
1.1 christos 961: Perror(statp, stderr, "send", errno);
962: res_nclose(statp);
963: return (0);
964: }
965: #else /* !CANNOT_CONNECT_DGRAM */
1.27 christos 966: if (sendto(s, buf, buflen, 0, nsap, nsaplen) != (ssize_t)buflen)
1.1 christos 967: {
968: Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
969: res_nclose(statp);
970: return (0);
971: }
972: #endif /* !CANNOT_CONNECT_DGRAM */
973:
974: /*
975: * Wait for reply.
976: */
1.14 christos 977: seconds = (statp->retrans << tries);
1.1 christos 978: if (ns > 0)
979: seconds /= statp->nscount;
980: if (seconds <= 0)
981: seconds = 1;
982: now = evNowTime();
1.25 christos 983: timeout = evConsTime((time_t)seconds, 0L);
1.1 christos 984: finish = evAddTime(now, timeout);
985: goto nonow;
986: wait:
987: now = evNowTime();
988: nonow:
1.10 christos 989: #ifndef USE_POLL
1.1 christos 990: if (evCmpTime(finish, now) > 0)
991: timeout = evSubTime(finish, now);
992: else
1.3 christos 993: timeout = evConsTime(0L, 0L);
1.26 christos 994: #ifdef USE_KQUEUE
995: EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
996: n = kevent(kq, &kv, 1, &kv, 1, &timeout);
997: #else
998: FD_ZERO(&dsmask);
999: FD_SET(s, &dsmask);
1.1 christos 1000: n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
1.26 christos 1001: #endif
1.10 christos 1002: #else
1003: timeout = evSubTime(finish, now);
1004: if (timeout.tv_sec < 0)
1.25 christos 1005: timeout = evConsTime((time_t)0, 0L);
1.16 christos 1006: polltimeout = 1000*(int)timeout.tv_sec +
1007: (int)timeout.tv_nsec/1000000;
1.10 christos 1008: pollfd.fd = s;
1009: pollfd.events = POLLRDNORM;
1010: n = poll(&pollfd, 1, polltimeout);
1011: #endif /* USE_POLL */
1012:
1.1 christos 1013: if (n == 0) {
1014: Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1015: *gotsomewhere = 1;
1016: return (0);
1017: }
1018: if (n < 0) {
1.26 christos 1019: #if defined(USE_POLL)
1020: static const char *fun = "poll";
1021: #elif defined(USE_SELECT)
1022: static const char *fun = "select";
1023: #elif defined(USE_KQUEUE)
1024: static const char *fun = "kevent";
1025: #endif
1.1 christos 1026: if (errno == EINTR)
1027: goto wait;
1.26 christos 1028: Perror(statp, stderr, fun, errno);
1.1 christos 1029: res_nclose(statp);
1030: return (0);
1031: }
1.26 christos 1032: #ifdef USE_KQUEUE
1033: if ((int)kv.ident != s)
1034: goto wait;
1035: #endif
1.1 christos 1036: errno = 0;
1037: fromlen = sizeof(from);
1.2 christos 1038: resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
1039: (struct sockaddr *)(void *)&from, &fromlen);
1.1 christos 1040: if (resplen <= 0) {
1041: Perror(statp, stderr, "recvfrom", errno);
1042: res_nclose(statp);
1043: return (0);
1044: }
1045: *gotsomewhere = 1;
1046: if (resplen < HFIXEDSZ) {
1047: /*
1048: * Undersized message.
1049: */
1050: Dprint(statp->options & RES_DEBUG,
1.23 christos 1051: (stdout, ";; undersized: %zd\n",
1.1 christos 1052: resplen));
1053: *terrno = EMSGSIZE;
1054: res_nclose(statp);
1055: return (0);
1056: }
1057: if (hp->id != anhp->id) {
1058: /*
1059: * response from old query, ignore it.
1060: * XXX - potential security hazard could
1061: * be detected here.
1062: */
1063: DprintQ((statp->options & RES_DEBUG) ||
1064: (statp->pfcode & RES_PRF_REPLY),
1065: (stdout, ";; old answer:\n"),
1066: ans, (resplen > anssiz) ? anssiz : resplen);
1067: goto wait;
1068: }
1069: if (!(statp->options & RES_INSECURE1) &&
1.2 christos 1070: !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
1.1 christos 1071: /*
1072: * response from wrong server? ignore it.
1073: * XXX - potential security hazard could
1074: * be detected here.
1075: */
1076: DprintQ((statp->options & RES_DEBUG) ||
1077: (statp->pfcode & RES_PRF_REPLY),
1078: (stdout, ";; not our server:\n"),
1079: ans, (resplen > anssiz) ? anssiz : resplen);
1080: goto wait;
1081: }
1082: #ifdef RES_USE_EDNS0
1083: if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1084: /*
1085: * Do not retry if the server do not understand EDNS0.
1086: * The case has to be captured here, as FORMERR packet do not
1087: * carry query section, hence res_queriesmatch() returns 0.
1088: */
1089: DprintQ(statp->options & RES_DEBUG,
1090: (stdout, "server rejected query with EDNS0:\n"),
1091: ans, (resplen > anssiz) ? anssiz : resplen);
1092: /* record the error */
1093: statp->_flags |= RES_F_EDNS0ERR;
1094: res_nclose(statp);
1095: return (0);
1096: }
1097: #endif
1098: if (!(statp->options & RES_INSECURE2) &&
1.27 christos 1099: !res_queriesmatch(buf, (const u_char *)buf + buflen,
1.1 christos 1100: ans, ans + anssiz)) {
1101: /*
1102: * response contains wrong query? ignore it.
1103: * XXX - potential security hazard could
1104: * be detected here.
1105: */
1106: DprintQ((statp->options & RES_DEBUG) ||
1107: (statp->pfcode & RES_PRF_REPLY),
1108: (stdout, ";; wrong query name:\n"),
1.23 christos 1109: ans, (int)(resplen > anssiz) ? anssiz : resplen);
1.1 christos 1110: goto wait;
1111: }
1112: if (anhp->rcode == SERVFAIL ||
1113: anhp->rcode == NOTIMP ||
1114: anhp->rcode == REFUSED) {
1115: DprintQ(statp->options & RES_DEBUG,
1116: (stdout, "server rejected query:\n"),
1.23 christos 1117: ans, (int)(resplen > anssiz) ? anssiz : resplen);
1.1 christos 1118: res_nclose(statp);
1119: /* don't retry if called from dig */
1120: if (!statp->pfcode)
1121: return (0);
1122: }
1123: if (!(statp->options & RES_IGNTC) && anhp->tc) {
1124: /*
1125: * To get the rest of answer,
1126: * use TCP with same server.
1127: */
1128: Dprint(statp->options & RES_DEBUG,
1129: (stdout, ";; truncated answer\n"));
1130: *v_circuit = 1;
1131: res_nclose(statp);
1132: return (1);
1133: }
1134: /*
1135: * All is well, or the error is fatal. Signal that the
1136: * next nameserver ought not be tried.
1137: */
1.23 christos 1138: _DIAGASSERT(__type_fit(int, resplen));
1139: return (int)resplen;
1.1 christos 1140: }
1141:
1142: static void
1143: Aerror(const res_state statp, FILE *file, const char *string, int error,
1.27 christos 1144: const struct sockaddr *address, socklen_t alen)
1.1 christos 1145: {
1146: int save = errno;
1147: char hbuf[NI_MAXHOST];
1148: char sbuf[NI_MAXSERV];
1149:
1150: if ((statp->options & RES_DEBUG) != 0U) {
1.27 christos 1151: if (getnameinfo(address, alen, hbuf,
1.23 christos 1152: (socklen_t)sizeof(hbuf), sbuf, (socklen_t)sizeof(sbuf),
1153: niflags)) {
1.1 christos 1154: strncpy(hbuf, "?", sizeof(hbuf) - 1);
1155: hbuf[sizeof(hbuf) - 1] = '\0';
1156: strncpy(sbuf, "?", sizeof(sbuf) - 1);
1157: sbuf[sizeof(sbuf) - 1] = '\0';
1158: }
1159: fprintf(file, "res_send: %s ([%s].%s): %s\n",
1160: string, hbuf, sbuf, strerror(error));
1161: }
1162: errno = save;
1163: }
1164:
1165: static void
1166: Perror(const res_state statp, FILE *file, const char *string, int error) {
1167: int save = errno;
1168:
1169: if ((statp->options & RES_DEBUG) != 0U)
1170: fprintf(file, "res_send: %s: %s\n",
1171: string, strerror(error));
1172: errno = save;
1173: }
1174:
1175: static int
1176: sock_eq(struct sockaddr *a, struct sockaddr *b) {
1177: struct sockaddr_in *a4, *b4;
1178: struct sockaddr_in6 *a6, *b6;
1179:
1180: if (a->sa_family != b->sa_family)
1181: return 0;
1182: switch (a->sa_family) {
1183: case AF_INET:
1.2 christos 1184: a4 = (struct sockaddr_in *)(void *)a;
1185: b4 = (struct sockaddr_in *)(void *)b;
1.1 christos 1186: return a4->sin_port == b4->sin_port &&
1187: a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1188: case AF_INET6:
1.2 christos 1189: a6 = (struct sockaddr_in6 *)(void *)a;
1190: b6 = (struct sockaddr_in6 *)(void *)b;
1.1 christos 1191: return a6->sin6_port == b6->sin6_port &&
1192: #ifdef HAVE_SIN6_SCOPE_ID
1193: a6->sin6_scope_id == b6->sin6_scope_id &&
1194: #endif
1195: IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1196: default:
1197: return 0;
1198: }
1199: }
1200:
1.26 christos 1201: #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1.1 christos 1202: /* XXX needs to move to the porting library. */
1203: static int
1204: pselect(int nfds, void *rfds, void *wfds, void *efds,
1205: struct timespec *tsp, const sigset_t *sigmask)
1206: {
1207: struct timeval tv, *tvp;
1208: sigset_t sigs;
1209: int n;
1210:
1211: if (tsp) {
1212: tvp = &tv;
1213: tv = evTimeVal(*tsp);
1214: } else
1215: tvp = NULL;
1216: if (sigmask)
1217: sigprocmask(SIG_SETMASK, sigmask, &sigs);
1218: n = select(nfds, rfds, wfds, efds, tvp);
1219: if (sigmask)
1220: sigprocmask(SIG_SETMASK, &sigs, NULL);
1221: if (tsp)
1222: *tsp = evTimeSpec(tv);
1223: return (n);
1224: }
1225: #endif
CVSweb <webmaster@jp.NetBSD.org>