10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
6*11038SRao.Shoaib@Sun.COM
7*11038SRao.Shoaib@Sun.COM /*
8*11038SRao.Shoaib@Sun.COM * Portions Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
9*11038SRao.Shoaib@Sun.COM * Portions Copyright (C) 1996, 1997, 1988, 1999, 2001, 2003 Internet Software Consortium.
10*11038SRao.Shoaib@Sun.COM *
11*11038SRao.Shoaib@Sun.COM * Permission to use, copy, modify, and/or distribute this software for any
12*11038SRao.Shoaib@Sun.COM * purpose with or without fee is hereby granted, provided that the above
13*11038SRao.Shoaib@Sun.COM * copyright notice and this permission notice appear in all copies.
14*11038SRao.Shoaib@Sun.COM *
15*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16*11038SRao.Shoaib@Sun.COM * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17*11038SRao.Shoaib@Sun.COM * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18*11038SRao.Shoaib@Sun.COM * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19*11038SRao.Shoaib@Sun.COM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20*11038SRao.Shoaib@Sun.COM * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*11038SRao.Shoaib@Sun.COM * PERFORMANCE OF THIS SOFTWARE.
22*11038SRao.Shoaib@Sun.COM */
23*11038SRao.Shoaib@Sun.COM
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate * Copyright (c) 1985, 1993
260Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
27*11038SRao.Shoaib@Sun.COM *
280Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
290Sstevel@tonic-gate * modification, are permitted provided that the following conditions
300Sstevel@tonic-gate * are met:
310Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
320Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
330Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
340Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
350Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
360Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
370Sstevel@tonic-gate * must display the following acknowledgement:
380Sstevel@tonic-gate * This product includes software developed by the University of
390Sstevel@tonic-gate * California, Berkeley and its contributors.
400Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
410Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
420Sstevel@tonic-gate * without specific prior written permission.
43*11038SRao.Shoaib@Sun.COM *
440Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
450Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
460Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
470Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
480Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
490Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
500Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
510Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
520Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
530Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
540Sstevel@tonic-gate * SUCH DAMAGE.
550Sstevel@tonic-gate */
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation.
59*11038SRao.Shoaib@Sun.COM *
600Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
610Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
620Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that
630Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or
640Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without
650Sstevel@tonic-gate * specific, written prior permission.
66*11038SRao.Shoaib@Sun.COM *
670Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
680Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
690Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
700Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
710Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
720Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
730Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
740Sstevel@tonic-gate * SOFTWARE.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate
770Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
780Sstevel@tonic-gate static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
79*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_mkquery.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
800Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
810Sstevel@tonic-gate
820Sstevel@tonic-gate #include "port_before.h"
830Sstevel@tonic-gate #include <sys/types.h>
840Sstevel@tonic-gate #include <sys/param.h>
850Sstevel@tonic-gate #include <netinet/in.h>
860Sstevel@tonic-gate #include <arpa/nameser.h>
870Sstevel@tonic-gate
880Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
890Sstevel@tonic-gate #include <sys/socket.h>
900Sstevel@tonic-gate #include <errno.h>
910Sstevel@tonic-gate #include <sys/stat.h>
92*11038SRao.Shoaib@Sun.COM #endif /* SUNW_CONFCHECK */
93*11038SRao.Shoaib@Sun.COM
940Sstevel@tonic-gate
950Sstevel@tonic-gate #include <netdb.h>
960Sstevel@tonic-gate #include <resolv.h>
970Sstevel@tonic-gate #include <stdio.h>
980Sstevel@tonic-gate #include <string.h>
990Sstevel@tonic-gate #include "port_after.h"
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /* Options. Leave them on. */
1020Sstevel@tonic-gate #define DEBUG
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate extern const char *_res_opcodes[];
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
1070Sstevel@tonic-gate static int _confcheck(res_state statp);
108*11038SRao.Shoaib@Sun.COM #endif /* SUNW_CONFCHECK */
1090Sstevel@tonic-gate
110*11038SRao.Shoaib@Sun.COM
111*11038SRao.Shoaib@Sun.COM /*%
1120Sstevel@tonic-gate * Form all types of queries.
1130Sstevel@tonic-gate * Returns the size of the result or -1.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate int
res_nmkquery(res_state statp,int op,const char * dname,int class,int type,const u_char * data,int datalen,const u_char * newrr_in,u_char * buf,int buflen)1160Sstevel@tonic-gate res_nmkquery(res_state statp,
117*11038SRao.Shoaib@Sun.COM int op, /*!< opcode of query */
118*11038SRao.Shoaib@Sun.COM const char *dname, /*!< domain name */
119*11038SRao.Shoaib@Sun.COM int class, int type, /*!< class and type of query */
120*11038SRao.Shoaib@Sun.COM const u_char *data, /*!< resource record data */
121*11038SRao.Shoaib@Sun.COM int datalen, /*!< length of data */
122*11038SRao.Shoaib@Sun.COM const u_char *newrr_in, /*!< new rr for modify or append */
123*11038SRao.Shoaib@Sun.COM u_char *buf, /*!< buffer to put query */
124*11038SRao.Shoaib@Sun.COM int buflen) /*!< size of buffer */
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate register HEADER *hp;
1270Sstevel@tonic-gate register u_char *cp, *ep;
1280Sstevel@tonic-gate register int n;
1290Sstevel@tonic-gate u_char *dnptrs[20], **dpp, **lastdnptr;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate UNUSED(newrr_in);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate #ifdef DEBUG
1340Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1350Sstevel@tonic-gate printf(";; res_nmkquery(%s, %s, %s, %s)\n",
1360Sstevel@tonic-gate _res_opcodes[op], dname, p_class(class), p_type(type));
1370Sstevel@tonic-gate #endif
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * 1247019, 1265838, and 4034368: Check to see if we can
1420Sstevel@tonic-gate * bailout quickly.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate if (_confcheck(statp) == -1) {
1450Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
1460Sstevel@tonic-gate return(-1);
1470Sstevel@tonic-gate }
148*11038SRao.Shoaib@Sun.COM #endif /* SUNW_CONFCHECK */
149*11038SRao.Shoaib@Sun.COM
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate * Initialize header fields.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate if ((buf == NULL) || (buflen < HFIXEDSZ))
1540Sstevel@tonic-gate return (-1);
1550Sstevel@tonic-gate memset(buf, 0, HFIXEDSZ);
1560Sstevel@tonic-gate hp = (HEADER *) buf;
157*11038SRao.Shoaib@Sun.COM statp->id = res_nrandomid(statp);
158*11038SRao.Shoaib@Sun.COM hp->id = htons(statp->id);
1590Sstevel@tonic-gate hp->opcode = op;
160*11038SRao.Shoaib@Sun.COM hp->rd = (statp->options & RES_RECURSE) != 0U;
1610Sstevel@tonic-gate hp->rcode = NOERROR;
1620Sstevel@tonic-gate cp = buf + HFIXEDSZ;
1630Sstevel@tonic-gate ep = buf + buflen;
1640Sstevel@tonic-gate dpp = dnptrs;
1650Sstevel@tonic-gate *dpp++ = buf;
1660Sstevel@tonic-gate *dpp++ = NULL;
1670Sstevel@tonic-gate lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * perform opcode specific processing
1700Sstevel@tonic-gate */
1710Sstevel@tonic-gate switch (op) {
1720Sstevel@tonic-gate case QUERY: /*FALLTHROUGH*/
1730Sstevel@tonic-gate case NS_NOTIFY_OP:
1740Sstevel@tonic-gate if (ep - cp < QFIXEDSZ)
1750Sstevel@tonic-gate return (-1);
1760Sstevel@tonic-gate if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
1770Sstevel@tonic-gate lastdnptr)) < 0)
1780Sstevel@tonic-gate return (-1);
1790Sstevel@tonic-gate cp += n;
1800Sstevel@tonic-gate ns_put16(type, cp);
1810Sstevel@tonic-gate cp += INT16SZ;
1820Sstevel@tonic-gate ns_put16(class, cp);
1830Sstevel@tonic-gate cp += INT16SZ;
1840Sstevel@tonic-gate hp->qdcount = htons(1);
1850Sstevel@tonic-gate if (op == QUERY || data == NULL)
1860Sstevel@tonic-gate break;
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate * Make an additional record for completion domain.
1890Sstevel@tonic-gate */
1900Sstevel@tonic-gate if ((ep - cp) < RRFIXEDSZ)
1910Sstevel@tonic-gate return (-1);
1920Sstevel@tonic-gate n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
1930Sstevel@tonic-gate dnptrs, lastdnptr);
1940Sstevel@tonic-gate if (n < 0)
1950Sstevel@tonic-gate return (-1);
1960Sstevel@tonic-gate cp += n;
1970Sstevel@tonic-gate ns_put16(T_NULL, cp);
1980Sstevel@tonic-gate cp += INT16SZ;
1990Sstevel@tonic-gate ns_put16(class, cp);
2000Sstevel@tonic-gate cp += INT16SZ;
2010Sstevel@tonic-gate ns_put32(0, cp);
2020Sstevel@tonic-gate cp += INT32SZ;
2030Sstevel@tonic-gate ns_put16(0, cp);
2040Sstevel@tonic-gate cp += INT16SZ;
2050Sstevel@tonic-gate hp->arcount = htons(1);
2060Sstevel@tonic-gate break;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate case IQUERY:
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * Initialize answer section
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate if (ep - cp < 1 + RRFIXEDSZ + datalen)
2130Sstevel@tonic-gate return (-1);
214*11038SRao.Shoaib@Sun.COM *cp++ = '\0'; /*%< no domain name */
2150Sstevel@tonic-gate ns_put16(type, cp);
2160Sstevel@tonic-gate cp += INT16SZ;
2170Sstevel@tonic-gate ns_put16(class, cp);
2180Sstevel@tonic-gate cp += INT16SZ;
2190Sstevel@tonic-gate ns_put32(0, cp);
2200Sstevel@tonic-gate cp += INT32SZ;
2210Sstevel@tonic-gate ns_put16(datalen, cp);
2220Sstevel@tonic-gate cp += INT16SZ;
2230Sstevel@tonic-gate if (datalen) {
2240Sstevel@tonic-gate memcpy(cp, data, datalen);
2250Sstevel@tonic-gate cp += datalen;
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate hp->ancount = htons(1);
2280Sstevel@tonic-gate break;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate default:
2310Sstevel@tonic-gate return (-1);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate return (cp - buf);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate #ifdef RES_USE_EDNS0
2370Sstevel@tonic-gate /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate int
res_nopt(res_state statp,int n0,u_char * buf,int buflen,int anslen)2400Sstevel@tonic-gate res_nopt(res_state statp,
241*11038SRao.Shoaib@Sun.COM int n0, /*%< current offset in buffer */
242*11038SRao.Shoaib@Sun.COM u_char *buf, /*%< buffer to put query */
243*11038SRao.Shoaib@Sun.COM int buflen, /*%< size of buffer */
244*11038SRao.Shoaib@Sun.COM int anslen) /*%< UDP answer buffer size */
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate register HEADER *hp;
2470Sstevel@tonic-gate register u_char *cp, *ep;
2480Sstevel@tonic-gate u_int16_t flags = 0;
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate #ifdef DEBUG
251*11038SRao.Shoaib@Sun.COM if ((statp->options & RES_DEBUG) != 0U)
2520Sstevel@tonic-gate printf(";; res_nopt()\n");
2530Sstevel@tonic-gate #endif
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate hp = (HEADER *) buf;
2560Sstevel@tonic-gate cp = buf + n0;
2570Sstevel@tonic-gate ep = buf + buflen;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate if ((ep - cp) < 1 + RRFIXEDSZ)
2600Sstevel@tonic-gate return (-1);
2610Sstevel@tonic-gate
262*11038SRao.Shoaib@Sun.COM *cp++ = 0; /*%< "." */
263*11038SRao.Shoaib@Sun.COM ns_put16(ns_t_opt, cp); /*%< TYPE */
2640Sstevel@tonic-gate cp += INT16SZ;
265*11038SRao.Shoaib@Sun.COM ns_put16(anslen & 0xffff, cp); /*%< CLASS = UDP payload size */
2660Sstevel@tonic-gate cp += INT16SZ;
267*11038SRao.Shoaib@Sun.COM *cp++ = NOERROR; /*%< extended RCODE */
268*11038SRao.Shoaib@Sun.COM *cp++ = 0; /*%< EDNS version */
269*11038SRao.Shoaib@Sun.COM
2700Sstevel@tonic-gate if (statp->options & RES_USE_DNSSEC) {
2710Sstevel@tonic-gate #ifdef DEBUG
2720Sstevel@tonic-gate if (statp->options & RES_DEBUG)
2730Sstevel@tonic-gate printf(";; res_opt()... ENDS0 DNSSEC\n");
2740Sstevel@tonic-gate #endif
2750Sstevel@tonic-gate flags |= NS_OPT_DNSSEC_OK;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate ns_put16(flags, cp);
2780Sstevel@tonic-gate cp += INT16SZ;
279*11038SRao.Shoaib@Sun.COM
280*11038SRao.Shoaib@Sun.COM ns_put16(0U, cp); /*%< RDLEN */
2810Sstevel@tonic-gate cp += INT16SZ;
282*11038SRao.Shoaib@Sun.COM
2830Sstevel@tonic-gate hp->arcount = htons(ntohs(hp->arcount) + 1);
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate return (cp - buf);
2860Sstevel@tonic-gate }
287*11038SRao.Shoaib@Sun.COM
288*11038SRao.Shoaib@Sun.COM /*
289*11038SRao.Shoaib@Sun.COM * Construct variable data (RDATA) block for OPT psuedo-RR, append it
290*11038SRao.Shoaib@Sun.COM * to the buffer, then update the RDLEN field (previously set to zero by
291*11038SRao.Shoaib@Sun.COM * res_nopt()) with the new RDATA length.
292*11038SRao.Shoaib@Sun.COM */
293*11038SRao.Shoaib@Sun.COM int
res_nopt_rdata(res_state statp,int n0,u_char * buf,int buflen,u_char * rdata,u_short code,u_short len,u_char * data)294*11038SRao.Shoaib@Sun.COM res_nopt_rdata(res_state statp,
295*11038SRao.Shoaib@Sun.COM int n0, /*%< current offset in buffer */
296*11038SRao.Shoaib@Sun.COM u_char *buf, /*%< buffer to put query */
297*11038SRao.Shoaib@Sun.COM int buflen, /*%< size of buffer */
298*11038SRao.Shoaib@Sun.COM u_char *rdata, /*%< ptr to start of opt rdata */
299*11038SRao.Shoaib@Sun.COM u_short code, /*%< OPTION-CODE */
300*11038SRao.Shoaib@Sun.COM u_short len, /*%< OPTION-LENGTH */
301*11038SRao.Shoaib@Sun.COM u_char *data) /*%< OPTION_DATA */
302*11038SRao.Shoaib@Sun.COM {
303*11038SRao.Shoaib@Sun.COM register u_char *cp, *ep;
304*11038SRao.Shoaib@Sun.COM
305*11038SRao.Shoaib@Sun.COM #ifdef DEBUG
306*11038SRao.Shoaib@Sun.COM if ((statp->options & RES_DEBUG) != 0U)
307*11038SRao.Shoaib@Sun.COM printf(";; res_nopt_rdata()\n");
308*11038SRao.Shoaib@Sun.COM #endif
309*11038SRao.Shoaib@Sun.COM
310*11038SRao.Shoaib@Sun.COM cp = buf + n0;
311*11038SRao.Shoaib@Sun.COM ep = buf + buflen;
312*11038SRao.Shoaib@Sun.COM
313*11038SRao.Shoaib@Sun.COM if ((ep - cp) < (4 + len))
314*11038SRao.Shoaib@Sun.COM return (-1);
315*11038SRao.Shoaib@Sun.COM
316*11038SRao.Shoaib@Sun.COM if (rdata < (buf + 2) || rdata >= ep)
317*11038SRao.Shoaib@Sun.COM return (-1);
318*11038SRao.Shoaib@Sun.COM
319*11038SRao.Shoaib@Sun.COM ns_put16(code, cp);
320*11038SRao.Shoaib@Sun.COM cp += INT16SZ;
321*11038SRao.Shoaib@Sun.COM
322*11038SRao.Shoaib@Sun.COM ns_put16(len, cp);
323*11038SRao.Shoaib@Sun.COM cp += INT16SZ;
324*11038SRao.Shoaib@Sun.COM
325*11038SRao.Shoaib@Sun.COM memcpy(cp, data, len);
326*11038SRao.Shoaib@Sun.COM cp += len;
327*11038SRao.Shoaib@Sun.COM
328*11038SRao.Shoaib@Sun.COM len = cp - rdata;
329*11038SRao.Shoaib@Sun.COM ns_put16(len, rdata - 2); /* Update RDLEN field */
330*11038SRao.Shoaib@Sun.COM
331*11038SRao.Shoaib@Sun.COM return (cp - buf);
332*11038SRao.Shoaib@Sun.COM }
3330Sstevel@tonic-gate #endif
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
336*11038SRao.Shoaib@Sun.COM
3370Sstevel@tonic-gate /*
338*11038SRao.Shoaib@Sun.COM * Time out quickly if there is no /etc/resolv.conf and a TCP connection
339*11038SRao.Shoaib@Sun.COM * to the local DNS server fails.
3400Sstevel@tonic-gate */
_confcheck(res_state statp)3410Sstevel@tonic-gate static int _confcheck(res_state statp)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate int ns;
3440Sstevel@tonic-gate struct stat rc_stat;
3450Sstevel@tonic-gate struct sockaddr_in ns_sin;
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /* First, we check to see if /etc/resolv.conf exists.
3480Sstevel@tonic-gate * If it doesn't, then it is likely that the localhost is
3490Sstevel@tonic-gate * the nameserver.
3500Sstevel@tonic-gate */
3510Sstevel@tonic-gate if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) {
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate /* Next, we check to see if _res.nsaddr is set to loopback.
3540Sstevel@tonic-gate * If it isn't, it has been altered by the application
3550Sstevel@tonic-gate * explicitly and we then want to bail with success.
3560Sstevel@tonic-gate */
3570Sstevel@tonic-gate if (statp->nsaddr.sin_addr.S_un.S_addr ==
3580Sstevel@tonic-gate htonl(INADDR_LOOPBACK)) {
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate /* Lastly, we try to connect to the TCP port of the
3610Sstevel@tonic-gate * nameserver. If this fails, then we know that
3620Sstevel@tonic-gate * DNS is misconfigured and we can quickly exit.
3630Sstevel@tonic-gate */
3640Sstevel@tonic-gate ns = socket(AF_INET, SOCK_STREAM, 0);
3650Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(&ns_sin);
3660Sstevel@tonic-gate ns_sin.sin_port = htons(NAMESERVER_PORT);
3670Sstevel@tonic-gate if (connect(ns, (struct sockaddr *) &ns_sin,
3680Sstevel@tonic-gate sizeof ns_sin) == -1) {
3690Sstevel@tonic-gate close(ns);
3700Sstevel@tonic-gate return(-1);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate else {
3730Sstevel@tonic-gate close(ns);
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate return(0);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate return(0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate return (0);
3830Sstevel@tonic-gate }
384*11038SRao.Shoaib@Sun.COM #endif /* SUNW_CONFCHECK */
385*11038SRao.Shoaib@Sun.COM
386*11038SRao.Shoaib@Sun.COM /*! \file */
387