Home | History | Annotate | Line # | Download | only in resolv
res_mkquery.c revision 1.3
      1 /*	$NetBSD: res_mkquery.c,v 1.3 2004/05/20 19:43:39 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, 1993
      5  *    The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  * 	This product includes software developed by the University of
     18  * 	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
     38  *
     39  * Permission to use, copy, modify, and distribute this software for any
     40  * purpose with or without fee is hereby granted, provided that the above
     41  * copyright notice and this permission notice appear in all copies, and that
     42  * the name of Digital Equipment Corporation not be used in advertising or
     43  * publicity pertaining to distribution of the document or software without
     44  * specific, written prior permission.
     45  *
     46  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
     47  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
     48  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
     49  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     50  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     51  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     52  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     53  * SOFTWARE.
     54  */
     55 
     56 /*
     57  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     58  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
     59  *
     60  * Permission to use, copy, modify, and distribute this software for any
     61  * purpose with or without fee is hereby granted, provided that the above
     62  * copyright notice and this permission notice appear in all copies.
     63  *
     64  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     65  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     66  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     67  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     68  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     69  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     70  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #if defined(LIBC_SCCS) && !defined(lint)
     75 #ifdef notdef
     76 static const char sccsid[] = "@(#)res_mkquery.c	8.1 (Berkeley) 6/4/93";
     77 static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
     78 #else
     79 __RCSID("$NetBSD: res_mkquery.c,v 1.3 2004/05/20 19:43:39 christos Exp $");
     80 #endif
     81 #endif /* LIBC_SCCS and not lint */
     82 
     83 #include "port_before.h"
     84 #include <sys/types.h>
     85 #include <sys/param.h>
     86 #include <netinet/in.h>
     87 #include <arpa/nameser.h>
     88 #include <netdb.h>
     89 #include <resolv.h>
     90 #include <stdio.h>
     91 #include <string.h>
     92 #include "port_after.h"
     93 
     94 /* Options.  Leave them on. */
     95 #define DEBUG
     96 
     97 extern const char *_res_opcodes[];
     98 
     99 /*
    100  * Form all types of queries.
    101  * Returns the size of the result or -1.
    102  */
    103 int
    104 res_nmkquery(res_state statp,
    105 	     int op,			/* opcode of query */
    106 	     const char *dname,		/* domain name */
    107 	     int class, int type,	/* class and type of query */
    108 	     const u_char *data,	/* resource record data */
    109 	     int datalen,		/* length of data */
    110 	     const u_char *newrr_in,	/* new rr for modify or append */
    111 	     u_char *buf,		/* buffer to put query */
    112 	     int buflen)		/* size of buffer */
    113 {
    114 	register HEADER *hp;
    115 	register u_char *cp, *ep;
    116 	register int n;
    117 	u_char *dnptrs[20], **dpp, **lastdnptr;
    118 
    119 	UNUSED(newrr_in);
    120 
    121 #ifdef DEBUG
    122 	if (statp->options & RES_DEBUG)
    123 		printf(";; res_nmkquery(%s, %s, %s, %s)\n",
    124 		       _res_opcodes[op], dname, p_class(class), p_type(type));
    125 #endif
    126 	/*
    127 	 * Initialize header fields.
    128 	 */
    129 	if ((buf == NULL) || (buflen < HFIXEDSZ))
    130 		return (-1);
    131 	memset(buf, 0, HFIXEDSZ);
    132 	hp = (HEADER *)(void *)buf;
    133 	hp->id = htons(++statp->id);
    134 	hp->opcode = op;
    135 	hp->rd = (statp->options & RES_RECURSE) != 0U;
    136 	hp->rcode = NOERROR;
    137 	cp = buf + HFIXEDSZ;
    138 	ep = buf + buflen;
    139 	dpp = dnptrs;
    140 	*dpp++ = buf;
    141 	*dpp++ = NULL;
    142 	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
    143 	/*
    144 	 * perform opcode specific processing
    145 	 */
    146 	switch (op) {
    147 	case QUERY:	/*FALLTHROUGH*/
    148 	case NS_NOTIFY_OP:
    149 		if (ep - cp < QFIXEDSZ)
    150 			return (-1);
    151 		if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
    152 		    lastdnptr)) < 0)
    153 			return (-1);
    154 		cp += n;
    155 		ns_put16(type, cp);
    156 		cp += INT16SZ;
    157 		ns_put16(class, cp);
    158 		cp += INT16SZ;
    159 		hp->qdcount = htons(1);
    160 		if (op == QUERY || data == NULL)
    161 			break;
    162 		/*
    163 		 * Make an additional record for completion domain.
    164 		 */
    165 		if ((ep - cp) < RRFIXEDSZ)
    166 			return (-1);
    167 		n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
    168 			    dnptrs, lastdnptr);
    169 		if (n < 0)
    170 			return (-1);
    171 		cp += n;
    172 		ns_put16(T_NULL, cp);
    173 		cp += INT16SZ;
    174 		ns_put16(class, cp);
    175 		cp += INT16SZ;
    176 		ns_put32(0, cp);
    177 		cp += INT32SZ;
    178 		ns_put16(0, cp);
    179 		cp += INT16SZ;
    180 		hp->arcount = htons(1);
    181 		break;
    182 
    183 	case IQUERY:
    184 		/*
    185 		 * Initialize answer section
    186 		 */
    187 		if (ep - cp < 1 + RRFIXEDSZ + datalen)
    188 			return (-1);
    189 		*cp++ = '\0';	/* no domain name */
    190 		ns_put16(type, cp);
    191 		cp += INT16SZ;
    192 		ns_put16(class, cp);
    193 		cp += INT16SZ;
    194 		ns_put32(0, cp);
    195 		cp += INT32SZ;
    196 		ns_put16(datalen, cp);
    197 		cp += INT16SZ;
    198 		if (datalen) {
    199 			memcpy(cp, data, (size_t)datalen);
    200 			cp += datalen;
    201 		}
    202 		hp->ancount = htons(1);
    203 		break;
    204 
    205 	default:
    206 		return (-1);
    207 	}
    208 	return (cp - buf);
    209 }
    210 
    211 #ifdef RES_USE_EDNS0
    212 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
    213 #ifndef T_OPT
    214 #define T_OPT	41
    215 #endif
    216 
    217 int
    218 res_nopt(res_state statp,
    219 	 int n0,		/* current offset in buffer */
    220 	 u_char *buf,		/* buffer to put query */
    221 	 int buflen,		/* size of buffer */
    222 	 int anslen)		/* UDP answer buffer size */
    223 {
    224 	register HEADER *hp;
    225 	register u_char *cp, *ep;
    226 	u_int16_t flags = 0;
    227 
    228 #ifdef DEBUG
    229 	if ((statp->options & RES_DEBUG) != 0U)
    230 		printf(";; res_nopt()\n");
    231 #endif
    232 
    233 	hp = (HEADER *)(void *)buf;
    234 	cp = buf + n0;
    235 	ep = buf + buflen;
    236 
    237 	if ((ep - cp) < 1 + RRFIXEDSZ)
    238 		return (-1);
    239 
    240 	*cp++ = 0;	/* "." */
    241 
    242 	ns_put16(T_OPT, cp);	/* TYPE */
    243 	cp += INT16SZ;
    244 	ns_put16(anslen & 0xffff, cp);	/* CLASS = UDP payload size */
    245 	cp += INT16SZ;
    246 	*cp++ = NOERROR;	/* extended RCODE */
    247 	*cp++ = 0;		/* EDNS version */
    248 	if (statp->options & RES_USE_DNSSEC) {
    249 #ifdef DEBUG
    250 		if (statp->options & RES_DEBUG)
    251 			printf(";; res_opt()... ENDS0 DNSSEC\n");
    252 #endif
    253 		flags |= NS_OPT_DNSSEC_OK;
    254 	}
    255 	ns_put16(flags, cp);
    256 	cp += INT16SZ;
    257 	ns_put16(0, cp);	/* RDLEN */
    258 	cp += INT16SZ;
    259 	hp->arcount = htons(ntohs(hp->arcount) + 1);
    260 
    261 	return (cp - buf);
    262 }
    263 #endif
    264