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 #if defined(LIBC_SCCS) && !defined(lint)
787c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
79*9525b14bSRao Shoaib static const char rcsid[] = "$Id: res_mkquery.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
807c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate #include "port_before.h"
837c478bd9Sstevel@tonic-gate #include <sys/types.h>
847c478bd9Sstevel@tonic-gate #include <sys/param.h>
857c478bd9Sstevel@tonic-gate #include <netinet/in.h>
867c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
897c478bd9Sstevel@tonic-gate #include <sys/socket.h>
907c478bd9Sstevel@tonic-gate #include <errno.h>
917c478bd9Sstevel@tonic-gate #include <sys/stat.h>
92*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
93*9525b14bSRao Shoaib
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate #include <netdb.h>
967c478bd9Sstevel@tonic-gate #include <resolv.h>
977c478bd9Sstevel@tonic-gate #include <stdio.h>
987c478bd9Sstevel@tonic-gate #include <string.h>
997c478bd9Sstevel@tonic-gate #include "port_after.h"
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /* Options. Leave them on. */
1027c478bd9Sstevel@tonic-gate #define DEBUG
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate extern const char *_res_opcodes[];
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
1077c478bd9Sstevel@tonic-gate static int _confcheck(res_state statp);
108*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
1097c478bd9Sstevel@tonic-gate
110*9525b14bSRao Shoaib
111*9525b14bSRao Shoaib /*%
1127c478bd9Sstevel@tonic-gate * Form all types of queries.
1137c478bd9Sstevel@tonic-gate * Returns the size of the result or -1.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@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)1167c478bd9Sstevel@tonic-gate res_nmkquery(res_state statp,
117*9525b14bSRao Shoaib int op, /*!< opcode of query */
118*9525b14bSRao Shoaib const char *dname, /*!< domain name */
119*9525b14bSRao Shoaib int class, int type, /*!< class and type of query */
120*9525b14bSRao Shoaib const u_char *data, /*!< resource record data */
121*9525b14bSRao Shoaib int datalen, /*!< length of data */
122*9525b14bSRao Shoaib const u_char *newrr_in, /*!< new rr for modify or append */
123*9525b14bSRao Shoaib u_char *buf, /*!< buffer to put query */
124*9525b14bSRao Shoaib int buflen) /*!< size of buffer */
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate register HEADER *hp;
1277c478bd9Sstevel@tonic-gate register u_char *cp, *ep;
1287c478bd9Sstevel@tonic-gate register int n;
1297c478bd9Sstevel@tonic-gate u_char *dnptrs[20], **dpp, **lastdnptr;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate UNUSED(newrr_in);
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate #ifdef DEBUG
1347c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1357c478bd9Sstevel@tonic-gate printf(";; res_nmkquery(%s, %s, %s, %s)\n",
1367c478bd9Sstevel@tonic-gate _res_opcodes[op], dname, p_class(class), p_type(type));
1377c478bd9Sstevel@tonic-gate #endif
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * 1247019, 1265838, and 4034368: Check to see if we can
1427c478bd9Sstevel@tonic-gate * bailout quickly.
1437c478bd9Sstevel@tonic-gate */
1447c478bd9Sstevel@tonic-gate if (_confcheck(statp) == -1) {
1457c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
1467c478bd9Sstevel@tonic-gate return(-1);
1477c478bd9Sstevel@tonic-gate }
148*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * Initialize header fields.
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate if ((buf == NULL) || (buflen < HFIXEDSZ))
1547c478bd9Sstevel@tonic-gate return (-1);
1557c478bd9Sstevel@tonic-gate memset(buf, 0, HFIXEDSZ);
1567c478bd9Sstevel@tonic-gate hp = (HEADER *) buf;
157*9525b14bSRao Shoaib statp->id = res_nrandomid(statp);
158*9525b14bSRao Shoaib hp->id = htons(statp->id);
1597c478bd9Sstevel@tonic-gate hp->opcode = op;
160*9525b14bSRao Shoaib hp->rd = (statp->options & RES_RECURSE) != 0U;
1617c478bd9Sstevel@tonic-gate hp->rcode = NOERROR;
1627c478bd9Sstevel@tonic-gate cp = buf + HFIXEDSZ;
1637c478bd9Sstevel@tonic-gate ep = buf + buflen;
1647c478bd9Sstevel@tonic-gate dpp = dnptrs;
1657c478bd9Sstevel@tonic-gate *dpp++ = buf;
1667c478bd9Sstevel@tonic-gate *dpp++ = NULL;
1677c478bd9Sstevel@tonic-gate lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate * perform opcode specific processing
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate switch (op) {
1727c478bd9Sstevel@tonic-gate case QUERY: /*FALLTHROUGH*/
1737c478bd9Sstevel@tonic-gate case NS_NOTIFY_OP:
1747c478bd9Sstevel@tonic-gate if (ep - cp < QFIXEDSZ)
1757c478bd9Sstevel@tonic-gate return (-1);
1767c478bd9Sstevel@tonic-gate if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
1777c478bd9Sstevel@tonic-gate lastdnptr)) < 0)
1787c478bd9Sstevel@tonic-gate return (-1);
1797c478bd9Sstevel@tonic-gate cp += n;
1807c478bd9Sstevel@tonic-gate ns_put16(type, cp);
1817c478bd9Sstevel@tonic-gate cp += INT16SZ;
1827c478bd9Sstevel@tonic-gate ns_put16(class, cp);
1837c478bd9Sstevel@tonic-gate cp += INT16SZ;
1847c478bd9Sstevel@tonic-gate hp->qdcount = htons(1);
1857c478bd9Sstevel@tonic-gate if (op == QUERY || data == NULL)
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * Make an additional record for completion domain.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate if ((ep - cp) < RRFIXEDSZ)
1917c478bd9Sstevel@tonic-gate return (-1);
1927c478bd9Sstevel@tonic-gate n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
1937c478bd9Sstevel@tonic-gate dnptrs, lastdnptr);
1947c478bd9Sstevel@tonic-gate if (n < 0)
1957c478bd9Sstevel@tonic-gate return (-1);
1967c478bd9Sstevel@tonic-gate cp += n;
1977c478bd9Sstevel@tonic-gate ns_put16(T_NULL, cp);
1987c478bd9Sstevel@tonic-gate cp += INT16SZ;
1997c478bd9Sstevel@tonic-gate ns_put16(class, cp);
2007c478bd9Sstevel@tonic-gate cp += INT16SZ;
2017c478bd9Sstevel@tonic-gate ns_put32(0, cp);
2027c478bd9Sstevel@tonic-gate cp += INT32SZ;
2037c478bd9Sstevel@tonic-gate ns_put16(0, cp);
2047c478bd9Sstevel@tonic-gate cp += INT16SZ;
2057c478bd9Sstevel@tonic-gate hp->arcount = htons(1);
2067c478bd9Sstevel@tonic-gate break;
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate case IQUERY:
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate * Initialize answer section
2117c478bd9Sstevel@tonic-gate */
2127c478bd9Sstevel@tonic-gate if (ep - cp < 1 + RRFIXEDSZ + datalen)
2137c478bd9Sstevel@tonic-gate return (-1);
214*9525b14bSRao Shoaib *cp++ = '\0'; /*%< no domain name */
2157c478bd9Sstevel@tonic-gate ns_put16(type, cp);
2167c478bd9Sstevel@tonic-gate cp += INT16SZ;
2177c478bd9Sstevel@tonic-gate ns_put16(class, cp);
2187c478bd9Sstevel@tonic-gate cp += INT16SZ;
2197c478bd9Sstevel@tonic-gate ns_put32(0, cp);
2207c478bd9Sstevel@tonic-gate cp += INT32SZ;
2217c478bd9Sstevel@tonic-gate ns_put16(datalen, cp);
2227c478bd9Sstevel@tonic-gate cp += INT16SZ;
2237c478bd9Sstevel@tonic-gate if (datalen) {
2247c478bd9Sstevel@tonic-gate memcpy(cp, data, datalen);
2257c478bd9Sstevel@tonic-gate cp += datalen;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate hp->ancount = htons(1);
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate default:
2317c478bd9Sstevel@tonic-gate return (-1);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate return (cp - buf);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0
2377c478bd9Sstevel@tonic-gate /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate int
res_nopt(res_state statp,int n0,u_char * buf,int buflen,int anslen)2407c478bd9Sstevel@tonic-gate res_nopt(res_state statp,
241*9525b14bSRao Shoaib int n0, /*%< current offset in buffer */
242*9525b14bSRao Shoaib u_char *buf, /*%< buffer to put query */
243*9525b14bSRao Shoaib int buflen, /*%< size of buffer */
244*9525b14bSRao Shoaib int anslen) /*%< UDP answer buffer size */
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate register HEADER *hp;
2477c478bd9Sstevel@tonic-gate register u_char *cp, *ep;
2487c478bd9Sstevel@tonic-gate u_int16_t flags = 0;
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate #ifdef DEBUG
251*9525b14bSRao Shoaib if ((statp->options & RES_DEBUG) != 0U)
2527c478bd9Sstevel@tonic-gate printf(";; res_nopt()\n");
2537c478bd9Sstevel@tonic-gate #endif
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate hp = (HEADER *) buf;
2567c478bd9Sstevel@tonic-gate cp = buf + n0;
2577c478bd9Sstevel@tonic-gate ep = buf + buflen;
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate if ((ep - cp) < 1 + RRFIXEDSZ)
2607c478bd9Sstevel@tonic-gate return (-1);
2617c478bd9Sstevel@tonic-gate
262*9525b14bSRao Shoaib *cp++ = 0; /*%< "." */
263*9525b14bSRao Shoaib ns_put16(ns_t_opt, cp); /*%< TYPE */
264*9525b14bSRao Shoaib cp += INT16SZ;
265*9525b14bSRao Shoaib ns_put16(anslen & 0xffff, cp); /*%< CLASS = UDP payload size */
266*9525b14bSRao Shoaib cp += INT16SZ;
267*9525b14bSRao Shoaib *cp++ = NOERROR; /*%< extended RCODE */
268*9525b14bSRao Shoaib *cp++ = 0; /*%< EDNS version */
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate if (statp->options & RES_USE_DNSSEC) {
2717c478bd9Sstevel@tonic-gate #ifdef DEBUG
2727c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
2737c478bd9Sstevel@tonic-gate printf(";; res_opt()... ENDS0 DNSSEC\n");
2747c478bd9Sstevel@tonic-gate #endif
2757c478bd9Sstevel@tonic-gate flags |= NS_OPT_DNSSEC_OK;
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate ns_put16(flags, cp);
2787c478bd9Sstevel@tonic-gate cp += INT16SZ;
279*9525b14bSRao Shoaib
280*9525b14bSRao Shoaib ns_put16(0U, cp); /*%< RDLEN */
2817c478bd9Sstevel@tonic-gate cp += INT16SZ;
282*9525b14bSRao Shoaib
2837c478bd9Sstevel@tonic-gate hp->arcount = htons(ntohs(hp->arcount) + 1);
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate return (cp - buf);
2867c478bd9Sstevel@tonic-gate }
287*9525b14bSRao Shoaib
288*9525b14bSRao Shoaib /*
289*9525b14bSRao Shoaib * Construct variable data (RDATA) block for OPT psuedo-RR, append it
290*9525b14bSRao Shoaib * to the buffer, then update the RDLEN field (previously set to zero by
291*9525b14bSRao Shoaib * res_nopt()) with the new RDATA length.
292*9525b14bSRao Shoaib */
293*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)294*9525b14bSRao Shoaib res_nopt_rdata(res_state statp,
295*9525b14bSRao Shoaib int n0, /*%< current offset in buffer */
296*9525b14bSRao Shoaib u_char *buf, /*%< buffer to put query */
297*9525b14bSRao Shoaib int buflen, /*%< size of buffer */
298*9525b14bSRao Shoaib u_char *rdata, /*%< ptr to start of opt rdata */
299*9525b14bSRao Shoaib u_short code, /*%< OPTION-CODE */
300*9525b14bSRao Shoaib u_short len, /*%< OPTION-LENGTH */
301*9525b14bSRao Shoaib u_char *data) /*%< OPTION_DATA */
302*9525b14bSRao Shoaib {
303*9525b14bSRao Shoaib register u_char *cp, *ep;
304*9525b14bSRao Shoaib
305*9525b14bSRao Shoaib #ifdef DEBUG
306*9525b14bSRao Shoaib if ((statp->options & RES_DEBUG) != 0U)
307*9525b14bSRao Shoaib printf(";; res_nopt_rdata()\n");
308*9525b14bSRao Shoaib #endif
309*9525b14bSRao Shoaib
310*9525b14bSRao Shoaib cp = buf + n0;
311*9525b14bSRao Shoaib ep = buf + buflen;
312*9525b14bSRao Shoaib
313*9525b14bSRao Shoaib if ((ep - cp) < (4 + len))
314*9525b14bSRao Shoaib return (-1);
315*9525b14bSRao Shoaib
316*9525b14bSRao Shoaib if (rdata < (buf + 2) || rdata >= ep)
317*9525b14bSRao Shoaib return (-1);
318*9525b14bSRao Shoaib
319*9525b14bSRao Shoaib ns_put16(code, cp);
320*9525b14bSRao Shoaib cp += INT16SZ;
321*9525b14bSRao Shoaib
322*9525b14bSRao Shoaib ns_put16(len, cp);
323*9525b14bSRao Shoaib cp += INT16SZ;
324*9525b14bSRao Shoaib
325*9525b14bSRao Shoaib memcpy(cp, data, len);
326*9525b14bSRao Shoaib cp += len;
327*9525b14bSRao Shoaib
328*9525b14bSRao Shoaib len = cp - rdata;
329*9525b14bSRao Shoaib ns_put16(len, rdata - 2); /* Update RDLEN field */
330*9525b14bSRao Shoaib
331*9525b14bSRao Shoaib return (cp - buf);
332*9525b14bSRao Shoaib }
3337c478bd9Sstevel@tonic-gate #endif
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate #ifdef SUNW_CONFCHECK
336*9525b14bSRao Shoaib
3377c478bd9Sstevel@tonic-gate /*
338*9525b14bSRao Shoaib * Time out quickly if there is no /etc/resolv.conf and a TCP connection
339*9525b14bSRao Shoaib * to the local DNS server fails.
3407c478bd9Sstevel@tonic-gate */
_confcheck(res_state statp)3417c478bd9Sstevel@tonic-gate static int _confcheck(res_state statp)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate int ns;
3447c478bd9Sstevel@tonic-gate struct stat rc_stat;
3457c478bd9Sstevel@tonic-gate struct sockaddr_in ns_sin;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /* First, we check to see if /etc/resolv.conf exists.
3487c478bd9Sstevel@tonic-gate * If it doesn't, then it is likely that the localhost is
3497c478bd9Sstevel@tonic-gate * the nameserver.
3507c478bd9Sstevel@tonic-gate */
3517c478bd9Sstevel@tonic-gate if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) {
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate /* Next, we check to see if _res.nsaddr is set to loopback.
3547c478bd9Sstevel@tonic-gate * If it isn't, it has been altered by the application
3557c478bd9Sstevel@tonic-gate * explicitly and we then want to bail with success.
3567c478bd9Sstevel@tonic-gate */
3577c478bd9Sstevel@tonic-gate if (statp->nsaddr.sin_addr.S_un.S_addr ==
3587c478bd9Sstevel@tonic-gate htonl(INADDR_LOOPBACK)) {
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate /* Lastly, we try to connect to the TCP port of the
3617c478bd9Sstevel@tonic-gate * nameserver. If this fails, then we know that
3627c478bd9Sstevel@tonic-gate * DNS is misconfigured and we can quickly exit.
3637c478bd9Sstevel@tonic-gate */
3647c478bd9Sstevel@tonic-gate ns = socket(AF_INET, SOCK_STREAM, 0);
3657c478bd9Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(&ns_sin);
3667c478bd9Sstevel@tonic-gate ns_sin.sin_port = htons(NAMESERVER_PORT);
3677c478bd9Sstevel@tonic-gate if (connect(ns, (struct sockaddr *) &ns_sin,
3687c478bd9Sstevel@tonic-gate sizeof ns_sin) == -1) {
3697c478bd9Sstevel@tonic-gate close(ns);
3707c478bd9Sstevel@tonic-gate return(-1);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate else {
3737c478bd9Sstevel@tonic-gate close(ns);
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate return(0);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate return(0);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate return (0);
3837c478bd9Sstevel@tonic-gate }
384*9525b14bSRao Shoaib #endif /* SUNW_CONFCHECK */
385*9525b14bSRao Shoaib
386*9525b14bSRao Shoaib /*! \file */
387