xref: /illumos-gate/usr/src/lib/libresolv2/common/resolv/res_mkquery.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*9525b14bSRao Shoaib 
7*9525b14bSRao Shoaib /*
8*9525b14bSRao Shoaib  * Portions Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
9*9525b14bSRao Shoaib  * Portions Copyright (C) 1996, 1997, 1988, 1999, 2001, 2003  Internet Software Consortium.
10*9525b14bSRao Shoaib  *
11*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
12*9525b14bSRao Shoaib  * purpose with or without fee is hereby granted, provided that the above
13*9525b14bSRao Shoaib  * copyright notice and this permission notice appear in all copies.
14*9525b14bSRao Shoaib  *
15*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
22*9525b14bSRao Shoaib  */
23*9525b14bSRao Shoaib 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * Copyright (c) 1985, 1993
267c478bd9Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
297c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
307c478bd9Sstevel@tonic-gate  * are met:
317c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
327c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
337c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
347c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
357c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
367c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
377c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
387c478bd9Sstevel@tonic-gate  * 	This product includes software developed by the University of
397c478bd9Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
407c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
417c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
427c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
457c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
467c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
477c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
487c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
497c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
507c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
517c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
527c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
537c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
547c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
617c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
627c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
637c478bd9Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
647c478bd9Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
657c478bd9Sstevel@tonic-gate  * specific, written prior permission.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
687c478bd9Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
697c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
707c478bd9Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
717c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
727c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
737c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
747c478bd9Sstevel@tonic-gate  * SOFTWARE.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include "port_before.h"
787c478bd9Sstevel@tonic-gate #include <sys/types.h>
797c478bd9Sstevel@tonic-gate #include <sys/param.h>
807c478bd9Sstevel@tonic-gate #include <netinet/in.h>
817c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
847c478bd9Sstevel@tonic-gate #include <sys/socket.h>
857c478bd9Sstevel@tonic-gate #include <errno.h>
867c478bd9Sstevel@tonic-gate #include <sys/stat.h>
87*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
88*9525b14bSRao Shoaib 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include <netdb.h>
917c478bd9Sstevel@tonic-gate #include <resolv.h>
927c478bd9Sstevel@tonic-gate #include <stdio.h>
937c478bd9Sstevel@tonic-gate #include <string.h>
947c478bd9Sstevel@tonic-gate #include "port_after.h"
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* Options.  Leave them on. */
977c478bd9Sstevel@tonic-gate #define DEBUG
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate extern const char *_res_opcodes[];
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
1027c478bd9Sstevel@tonic-gate static int      _confcheck(res_state statp);
103*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
1047c478bd9Sstevel@tonic-gate 
105*9525b14bSRao Shoaib 
106*9525b14bSRao Shoaib /*%
1077c478bd9Sstevel@tonic-gate  * Form all types of queries.
1087c478bd9Sstevel@tonic-gate  * Returns the size of the result or -1.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@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)1117c478bd9Sstevel@tonic-gate res_nmkquery(res_state statp,
112*9525b14bSRao Shoaib 	     int op,			/*!< opcode of query  */
113*9525b14bSRao Shoaib 	     const char *dname,		/*!< domain name  */
114*9525b14bSRao Shoaib 	     int class, int type,	/*!< class and type of query  */
115*9525b14bSRao Shoaib 	     const u_char *data,	/*!< resource record data  */
116*9525b14bSRao Shoaib 	     int datalen,		/*!< length of data  */
117*9525b14bSRao Shoaib 	     const u_char *newrr_in,	/*!< new rr for modify or append  */
118*9525b14bSRao Shoaib 	     u_char *buf,		/*!< buffer to put query  */
119*9525b14bSRao Shoaib 	     int buflen)		/*!< size of buffer  */
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	register HEADER *hp;
1227c478bd9Sstevel@tonic-gate 	register u_char *cp, *ep;
1237c478bd9Sstevel@tonic-gate 	register int n;
1247c478bd9Sstevel@tonic-gate 	u_char *dnptrs[20], **dpp, **lastdnptr;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	UNUSED(newrr_in);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #ifdef DEBUG
1297c478bd9Sstevel@tonic-gate 	if (statp->options & RES_DEBUG)
1307c478bd9Sstevel@tonic-gate 		printf(";; res_nmkquery(%s, %s, %s, %s)\n",
1317c478bd9Sstevel@tonic-gate 		       _res_opcodes[op], dname, p_class(class), p_type(type));
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
1357c478bd9Sstevel@tonic-gate         /*
1367c478bd9Sstevel@tonic-gate          * 1247019, 1265838, and 4034368: Check to see if we can
1377c478bd9Sstevel@tonic-gate          * bailout quickly.
1387c478bd9Sstevel@tonic-gate          */
1397c478bd9Sstevel@tonic-gate         if (_confcheck(statp) == -1) {
1407c478bd9Sstevel@tonic-gate                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
1417c478bd9Sstevel@tonic-gate                 return(-1);
1427c478bd9Sstevel@tonic-gate         }
143*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * Initialize header fields.
1477c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	if ((buf == NULL) || (buflen < HFIXEDSZ))
1497c478bd9Sstevel@tonic-gate 		return (-1);
1507c478bd9Sstevel@tonic-gate 	memset(buf, 0, HFIXEDSZ);
1517c478bd9Sstevel@tonic-gate 	hp = (HEADER *) buf;
152*9525b14bSRao Shoaib 	statp->id = res_nrandomid(statp);
153*9525b14bSRao Shoaib 	hp->id = htons(statp->id);
1547c478bd9Sstevel@tonic-gate 	hp->opcode = op;
155*9525b14bSRao Shoaib 	hp->rd = (statp->options & RES_RECURSE) != 0U;
1567c478bd9Sstevel@tonic-gate 	hp->rcode = NOERROR;
1577c478bd9Sstevel@tonic-gate 	cp = buf + HFIXEDSZ;
1587c478bd9Sstevel@tonic-gate 	ep = buf + buflen;
1597c478bd9Sstevel@tonic-gate 	dpp = dnptrs;
1607c478bd9Sstevel@tonic-gate 	*dpp++ = buf;
1617c478bd9Sstevel@tonic-gate 	*dpp++ = NULL;
1627c478bd9Sstevel@tonic-gate 	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * perform opcode specific processing
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	switch (op) {
1677c478bd9Sstevel@tonic-gate 	case QUERY:	/*FALLTHROUGH*/
1687c478bd9Sstevel@tonic-gate 	case NS_NOTIFY_OP:
1697c478bd9Sstevel@tonic-gate 		if (ep - cp < QFIXEDSZ)
1707c478bd9Sstevel@tonic-gate 			return (-1);
1717c478bd9Sstevel@tonic-gate 		if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
1727c478bd9Sstevel@tonic-gate 		    lastdnptr)) < 0)
1737c478bd9Sstevel@tonic-gate 			return (-1);
1747c478bd9Sstevel@tonic-gate 		cp += n;
1757c478bd9Sstevel@tonic-gate 		ns_put16(type, cp);
1767c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
1777c478bd9Sstevel@tonic-gate 		ns_put16(class, cp);
1787c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
1797c478bd9Sstevel@tonic-gate 		hp->qdcount = htons(1);
1807c478bd9Sstevel@tonic-gate 		if (op == QUERY || data == NULL)
1817c478bd9Sstevel@tonic-gate 			break;
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * Make an additional record for completion domain.
1847c478bd9Sstevel@tonic-gate 		 */
1857c478bd9Sstevel@tonic-gate 		if ((ep - cp) < RRFIXEDSZ)
1867c478bd9Sstevel@tonic-gate 			return (-1);
1877c478bd9Sstevel@tonic-gate 		n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
1887c478bd9Sstevel@tonic-gate 			    dnptrs, lastdnptr);
1897c478bd9Sstevel@tonic-gate 		if (n < 0)
1907c478bd9Sstevel@tonic-gate 			return (-1);
1917c478bd9Sstevel@tonic-gate 		cp += n;
1927c478bd9Sstevel@tonic-gate 		ns_put16(T_NULL, cp);
1937c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
1947c478bd9Sstevel@tonic-gate 		ns_put16(class, cp);
1957c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
1967c478bd9Sstevel@tonic-gate 		ns_put32(0, cp);
1977c478bd9Sstevel@tonic-gate 		cp += INT32SZ;
1987c478bd9Sstevel@tonic-gate 		ns_put16(0, cp);
1997c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
2007c478bd9Sstevel@tonic-gate 		hp->arcount = htons(1);
2017c478bd9Sstevel@tonic-gate 		break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	case IQUERY:
2047c478bd9Sstevel@tonic-gate 		/*
2057c478bd9Sstevel@tonic-gate 		 * Initialize answer section
2067c478bd9Sstevel@tonic-gate 		 */
2077c478bd9Sstevel@tonic-gate 		if (ep - cp < 1 + RRFIXEDSZ + datalen)
2087c478bd9Sstevel@tonic-gate 			return (-1);
209*9525b14bSRao Shoaib 		*cp++ = '\0';	/*%< no domain name */
2107c478bd9Sstevel@tonic-gate 		ns_put16(type, cp);
2117c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
2127c478bd9Sstevel@tonic-gate 		ns_put16(class, cp);
2137c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
2147c478bd9Sstevel@tonic-gate 		ns_put32(0, cp);
2157c478bd9Sstevel@tonic-gate 		cp += INT32SZ;
2167c478bd9Sstevel@tonic-gate 		ns_put16(datalen, cp);
2177c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
2187c478bd9Sstevel@tonic-gate 		if (datalen) {
2197c478bd9Sstevel@tonic-gate 			memcpy(cp, data, datalen);
2207c478bd9Sstevel@tonic-gate 			cp += datalen;
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 		hp->ancount = htons(1);
2237c478bd9Sstevel@tonic-gate 		break;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	default:
2267c478bd9Sstevel@tonic-gate 		return (-1);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	return (cp - buf);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0
2327c478bd9Sstevel@tonic-gate /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate int
res_nopt(res_state statp,int n0,u_char * buf,int buflen,int anslen)2357c478bd9Sstevel@tonic-gate res_nopt(res_state statp,
236*9525b14bSRao Shoaib 	 int n0,		/*%< current offset in buffer */
237*9525b14bSRao Shoaib 	 u_char *buf,		/*%< buffer to put query */
238*9525b14bSRao Shoaib 	 int buflen,		/*%< size of buffer */
239*9525b14bSRao Shoaib 	 int anslen)		/*%< UDP answer buffer size */
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	register HEADER *hp;
2427c478bd9Sstevel@tonic-gate 	register u_char *cp, *ep;
2437c478bd9Sstevel@tonic-gate 	u_int16_t flags = 0;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #ifdef DEBUG
246*9525b14bSRao Shoaib 	if ((statp->options & RES_DEBUG) != 0U)
2477c478bd9Sstevel@tonic-gate 		printf(";; res_nopt()\n");
2487c478bd9Sstevel@tonic-gate #endif
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	hp = (HEADER *) buf;
2517c478bd9Sstevel@tonic-gate 	cp = buf + n0;
2527c478bd9Sstevel@tonic-gate 	ep = buf + buflen;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if ((ep - cp) < 1 + RRFIXEDSZ)
2557c478bd9Sstevel@tonic-gate 		return (-1);
2567c478bd9Sstevel@tonic-gate 
257*9525b14bSRao Shoaib 	*cp++ = 0;				/*%< "." */
258*9525b14bSRao Shoaib 	ns_put16(ns_t_opt, cp);			/*%< TYPE */
259*9525b14bSRao Shoaib 	cp += INT16SZ;
260*9525b14bSRao Shoaib 	ns_put16(anslen & 0xffff, cp);		/*%< CLASS = UDP payload size */
261*9525b14bSRao Shoaib 	cp += INT16SZ;
262*9525b14bSRao Shoaib 	*cp++ = NOERROR;			/*%< extended RCODE */
263*9525b14bSRao Shoaib 	*cp++ = 0;				/*%< EDNS version */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (statp->options & RES_USE_DNSSEC) {
2667c478bd9Sstevel@tonic-gate #ifdef DEBUG
2677c478bd9Sstevel@tonic-gate 		if (statp->options & RES_DEBUG)
2687c478bd9Sstevel@tonic-gate 			printf(";; res_opt()... ENDS0 DNSSEC\n");
2697c478bd9Sstevel@tonic-gate #endif
2707c478bd9Sstevel@tonic-gate 		flags |= NS_OPT_DNSSEC_OK;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	ns_put16(flags, cp);
2737c478bd9Sstevel@tonic-gate 	cp += INT16SZ;
274*9525b14bSRao Shoaib 
275*9525b14bSRao Shoaib 	ns_put16(0U, cp);			/*%< RDLEN */
2767c478bd9Sstevel@tonic-gate 	cp += INT16SZ;
277*9525b14bSRao Shoaib 
2787c478bd9Sstevel@tonic-gate 	hp->arcount = htons(ntohs(hp->arcount) + 1);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (cp - buf);
2817c478bd9Sstevel@tonic-gate }
282*9525b14bSRao Shoaib 
283*9525b14bSRao Shoaib /*
284*9525b14bSRao Shoaib  * Construct variable data (RDATA) block for OPT psuedo-RR, append it
285*9525b14bSRao Shoaib  * to the buffer, then update the RDLEN field (previously set to zero by
286*9525b14bSRao Shoaib  * res_nopt()) with the new RDATA length.
287*9525b14bSRao Shoaib  */
288*9525b14bSRao Shoaib 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)289*9525b14bSRao Shoaib res_nopt_rdata(res_state statp,
290*9525b14bSRao Shoaib 	  int n0,	 	/*%< current offset in buffer */
291*9525b14bSRao Shoaib 	  u_char *buf,	 	/*%< buffer to put query */
292*9525b14bSRao Shoaib 	  int buflen,		/*%< size of buffer */
293*9525b14bSRao Shoaib 	  u_char *rdata,	/*%< ptr to start of opt rdata */
294*9525b14bSRao Shoaib 	  u_short code,		/*%< OPTION-CODE */
295*9525b14bSRao Shoaib 	  u_short len,		/*%< OPTION-LENGTH */
296*9525b14bSRao Shoaib 	  u_char *data)		/*%< OPTION_DATA */
297*9525b14bSRao Shoaib {
298*9525b14bSRao Shoaib 	register u_char *cp, *ep;
299*9525b14bSRao Shoaib 
300*9525b14bSRao Shoaib #ifdef DEBUG
301*9525b14bSRao Shoaib 	if ((statp->options & RES_DEBUG) != 0U)
302*9525b14bSRao Shoaib 		printf(";; res_nopt_rdata()\n");
303*9525b14bSRao Shoaib #endif
304*9525b14bSRao Shoaib 
305*9525b14bSRao Shoaib 	cp = buf + n0;
306*9525b14bSRao Shoaib 	ep = buf + buflen;
307*9525b14bSRao Shoaib 
308*9525b14bSRao Shoaib 	if ((ep - cp) < (4 + len))
309*9525b14bSRao Shoaib 		return (-1);
310*9525b14bSRao Shoaib 
311*9525b14bSRao Shoaib 	if (rdata < (buf + 2) || rdata >= ep)
312*9525b14bSRao Shoaib 		return (-1);
313*9525b14bSRao Shoaib 
314*9525b14bSRao Shoaib 	ns_put16(code, cp);
315*9525b14bSRao Shoaib 	cp += INT16SZ;
316*9525b14bSRao Shoaib 
317*9525b14bSRao Shoaib 	ns_put16(len, cp);
318*9525b14bSRao Shoaib 	cp += INT16SZ;
319*9525b14bSRao Shoaib 
320*9525b14bSRao Shoaib 	memcpy(cp, data, len);
321*9525b14bSRao Shoaib 	cp += len;
322*9525b14bSRao Shoaib 
323*9525b14bSRao Shoaib 	len = cp - rdata;
324*9525b14bSRao Shoaib 	ns_put16(len, rdata - 2);	/* Update RDLEN field */
325*9525b14bSRao Shoaib 
326*9525b14bSRao Shoaib 	return (cp - buf);
327*9525b14bSRao Shoaib }
3287c478bd9Sstevel@tonic-gate #endif
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
331*9525b14bSRao Shoaib 
3327c478bd9Sstevel@tonic-gate /*
333*9525b14bSRao Shoaib  * Time out quickly if there is no /etc/resolv.conf and a TCP connection
334*9525b14bSRao Shoaib  * to the local DNS server fails.
3357c478bd9Sstevel@tonic-gate  */
_confcheck(res_state statp)3367c478bd9Sstevel@tonic-gate static int _confcheck(res_state statp)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate         int ns;
3397c478bd9Sstevel@tonic-gate         struct stat rc_stat;
3407c478bd9Sstevel@tonic-gate         struct sockaddr_in ns_sin;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate         /* First, we check to see if /etc/resolv.conf exists.
3437c478bd9Sstevel@tonic-gate          * If it doesn't, then it is likely that the localhost is
3447c478bd9Sstevel@tonic-gate          * the nameserver.
3457c478bd9Sstevel@tonic-gate          */
3467c478bd9Sstevel@tonic-gate         if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) {
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate                 /* Next, we check to see if _res.nsaddr is set to loopback.
3497c478bd9Sstevel@tonic-gate                  * If it isn't, it has been altered by the application
3507c478bd9Sstevel@tonic-gate                  * explicitly and we then want to bail with success.
3517c478bd9Sstevel@tonic-gate                  */
3527c478bd9Sstevel@tonic-gate                 if (statp->nsaddr.sin_addr.S_un.S_addr ==
3537c478bd9Sstevel@tonic-gate 				htonl(INADDR_LOOPBACK)) {
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate                         /* Lastly, we try to connect to the TCP port of the
3567c478bd9Sstevel@tonic-gate                          * nameserver.  If this fails, then we know that
3577c478bd9Sstevel@tonic-gate                          * DNS is misconfigured and we can quickly exit.
3587c478bd9Sstevel@tonic-gate                          */
3597c478bd9Sstevel@tonic-gate                         ns = socket(AF_INET, SOCK_STREAM, 0);
3607c478bd9Sstevel@tonic-gate                         IN_SET_LOOPBACK_ADDR(&ns_sin);
3617c478bd9Sstevel@tonic-gate                         ns_sin.sin_port = htons(NAMESERVER_PORT);
3627c478bd9Sstevel@tonic-gate                         if (connect(ns, (struct sockaddr *) &ns_sin,
3637c478bd9Sstevel@tonic-gate                                     sizeof ns_sin) == -1) {
3647c478bd9Sstevel@tonic-gate                                 close(ns);
3657c478bd9Sstevel@tonic-gate                                 return(-1);
3667c478bd9Sstevel@tonic-gate                         }
3677c478bd9Sstevel@tonic-gate                         else {
3687c478bd9Sstevel@tonic-gate                                 close(ns);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate                                 return(0);
3717c478bd9Sstevel@tonic-gate                         }
3727c478bd9Sstevel@tonic-gate                 }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate                 return(0);
3757c478bd9Sstevel@tonic-gate         }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate         return (0);
3787c478bd9Sstevel@tonic-gate }
379*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
380*9525b14bSRao Shoaib 
381*9525b14bSRao Shoaib /*! \file */
382