1156952Sume/*
2270838Sume * Portions Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3270838Sume * Portions Copyright (C) 1996, 1997, 1988, 1999, 2001, 2003  Internet Software Consortium.
4270838Sume *
5270838Sume * Permission to use, copy, modify, and/or distribute this software for any
6270838Sume * purpose with or without fee is hereby granted, provided that the above
7270838Sume * copyright notice and this permission notice appear in all copies.
8270838Sume *
9270838Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10270838Sume * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11270838Sume * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12270838Sume * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13270838Sume * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14270838Sume * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15270838Sume * PERFORMANCE OF THIS SOFTWARE.
16270838Sume */
17270838Sume
18270838Sume/*
19156952Sume * Copyright (c) 1985, 1993
20156952Sume *    The Regents of the University of California.  All rights reserved.
21270838Sume *
22156952Sume * Redistribution and use in source and binary forms, with or without
23156952Sume * modification, are permitted provided that the following conditions
24156952Sume * are met:
25156952Sume * 1. Redistributions of source code must retain the above copyright
26156952Sume *    notice, this list of conditions and the following disclaimer.
27156952Sume * 2. Redistributions in binary form must reproduce the above copyright
28156952Sume *    notice, this list of conditions and the following disclaimer in the
29156952Sume *    documentation and/or other materials provided with the distribution.
30156952Sume * 4. Neither the name of the University nor the names of its contributors
31156952Sume *    may be used to endorse or promote products derived from this software
32156952Sume *    without specific prior written permission.
33270838Sume *
34156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44156952Sume * SUCH DAMAGE.
45156952Sume */
46156952Sume
47156952Sume/*
48156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation.
49270838Sume *
50156952Sume * Permission to use, copy, modify, and distribute this software for any
51156952Sume * purpose with or without fee is hereby granted, provided that the above
52156952Sume * copyright notice and this permission notice appear in all copies, and that
53156952Sume * the name of Digital Equipment Corporation not be used in advertising or
54156952Sume * publicity pertaining to distribution of the document or software without
55156952Sume * specific, written prior permission.
56270838Sume *
57156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
58156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
59156952Sume * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
60156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64156952Sume * SOFTWARE.
65156952Sume */
66156952Sume
67156952Sume#if defined(LIBC_SCCS) && !defined(lint)
68156952Sumestatic const char sccsid[] = "@(#)res_mkquery.c	8.1 (Berkeley) 6/4/93";
69270838Sumestatic const char rcsid[] = "$Id: res_mkquery.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
70156952Sume#endif /* LIBC_SCCS and not lint */
71156956Sume#include <sys/cdefs.h>
72156956Sume__FBSDID("$FreeBSD$");
73156952Sume
74156952Sume#include "port_before.h"
75156952Sume#include <sys/types.h>
76156952Sume#include <sys/param.h>
77156952Sume#include <netinet/in.h>
78156952Sume#include <arpa/nameser.h>
79156952Sume#include <netdb.h>
80156952Sume#include <resolv.h>
81156952Sume#include <stdio.h>
82156952Sume#include <string.h>
83156952Sume#include "port_after.h"
84156952Sume
85156952Sume/* Options.  Leave them on. */
86156952Sume#define DEBUG
87156952Sume
88156952Sumeextern const char *_res_opcodes[];
89156952Sume
90170244Sume/*%
91156952Sume * Form all types of queries.
92156952Sume * Returns the size of the result or -1.
93156952Sume */
94156952Sumeint
95156952Sumeres_nmkquery(res_state statp,
96170244Sume	     int op,			/*!< opcode of query  */
97170244Sume	     const char *dname,		/*!< domain name  */
98170244Sume	     int class, int type,	/*!< class and type of query  */
99170244Sume	     const u_char *data,	/*!< resource record data  */
100170244Sume	     int datalen,		/*!< length of data  */
101170244Sume	     const u_char *newrr_in,	/*!< new rr for modify or append  */
102170244Sume	     u_char *buf,		/*!< buffer to put query  */
103170244Sume	     int buflen)		/*!< size of buffer  */
104156952Sume{
105156956Sume	HEADER *hp;
106156956Sume	u_char *cp, *ep;
107156956Sume	int n;
108156952Sume	u_char *dnptrs[20], **dpp, **lastdnptr;
109156952Sume
110156952Sume	UNUSED(newrr_in);
111156952Sume
112156952Sume#ifdef DEBUG
113156952Sume	if (statp->options & RES_DEBUG)
114156952Sume		printf(";; res_nmkquery(%s, %s, %s, %s)\n",
115156952Sume		       _res_opcodes[op], dname, p_class(class), p_type(type));
116156952Sume#endif
117156952Sume	/*
118156952Sume	 * Initialize header fields.
119156952Sume	 */
120156952Sume	if ((buf == NULL) || (buflen < HFIXEDSZ))
121156952Sume		return (-1);
122156952Sume	memset(buf, 0, HFIXEDSZ);
123156952Sume	hp = (HEADER *) buf;
124270838Sume	statp->id = res_nrandomid(statp);
125270838Sume	hp->id = htons(statp->id);
126156952Sume	hp->opcode = op;
127156952Sume	hp->rd = (statp->options & RES_RECURSE) != 0U;
128156952Sume	hp->rcode = NOERROR;
129156952Sume	cp = buf + HFIXEDSZ;
130156952Sume	ep = buf + buflen;
131156952Sume	dpp = dnptrs;
132156952Sume	*dpp++ = buf;
133156952Sume	*dpp++ = NULL;
134156952Sume	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
135156952Sume	/*
136156952Sume	 * perform opcode specific processing
137156952Sume	 */
138156952Sume	switch (op) {
139156952Sume	case QUERY:	/*FALLTHROUGH*/
140156952Sume	case NS_NOTIFY_OP:
141156952Sume		if (ep - cp < QFIXEDSZ)
142156952Sume			return (-1);
143156952Sume		if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
144156952Sume		    lastdnptr)) < 0)
145156952Sume			return (-1);
146156952Sume		cp += n;
147156952Sume		ns_put16(type, cp);
148156952Sume		cp += INT16SZ;
149156952Sume		ns_put16(class, cp);
150156952Sume		cp += INT16SZ;
151156952Sume		hp->qdcount = htons(1);
152156952Sume		if (op == QUERY || data == NULL)
153156952Sume			break;
154156952Sume		/*
155156952Sume		 * Make an additional record for completion domain.
156156952Sume		 */
157156952Sume		if ((ep - cp) < RRFIXEDSZ)
158156952Sume			return (-1);
159156952Sume		n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
160156952Sume			    dnptrs, lastdnptr);
161156952Sume		if (n < 0)
162156952Sume			return (-1);
163156952Sume		cp += n;
164156952Sume		ns_put16(T_NULL, cp);
165156952Sume		cp += INT16SZ;
166156952Sume		ns_put16(class, cp);
167156952Sume		cp += INT16SZ;
168156952Sume		ns_put32(0, cp);
169156952Sume		cp += INT32SZ;
170156952Sume		ns_put16(0, cp);
171156952Sume		cp += INT16SZ;
172156952Sume		hp->arcount = htons(1);
173156952Sume		break;
174156952Sume
175156952Sume	case IQUERY:
176156952Sume		/*
177156952Sume		 * Initialize answer section
178156952Sume		 */
179156952Sume		if (ep - cp < 1 + RRFIXEDSZ + datalen)
180156952Sume			return (-1);
181170244Sume		*cp++ = '\0';	/*%< no domain name */
182156952Sume		ns_put16(type, cp);
183156952Sume		cp += INT16SZ;
184156952Sume		ns_put16(class, cp);
185156952Sume		cp += INT16SZ;
186156952Sume		ns_put32(0, cp);
187156952Sume		cp += INT32SZ;
188156952Sume		ns_put16(datalen, cp);
189156952Sume		cp += INT16SZ;
190156952Sume		if (datalen) {
191156952Sume			memcpy(cp, data, datalen);
192156952Sume			cp += datalen;
193156952Sume		}
194156952Sume		hp->ancount = htons(1);
195156952Sume		break;
196156952Sume
197156952Sume	default:
198156952Sume		return (-1);
199156952Sume	}
200156952Sume	return (cp - buf);
201156952Sume}
202156952Sume
203156952Sume#ifdef RES_USE_EDNS0
204156952Sume/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
205156952Sume
206156952Sumeint
207156952Sumeres_nopt(res_state statp,
208170244Sume	 int n0,		/*%< current offset in buffer */
209170244Sume	 u_char *buf,		/*%< buffer to put query */
210170244Sume	 int buflen,		/*%< size of buffer */
211170244Sume	 int anslen)		/*%< UDP answer buffer size */
212156952Sume{
213156956Sume	HEADER *hp;
214156956Sume	u_char *cp, *ep;
215156952Sume	u_int16_t flags = 0;
216156952Sume
217156952Sume#ifdef DEBUG
218156952Sume	if ((statp->options & RES_DEBUG) != 0U)
219156952Sume		printf(";; res_nopt()\n");
220156952Sume#endif
221156952Sume
222156952Sume	hp = (HEADER *) buf;
223156952Sume	cp = buf + n0;
224156952Sume	ep = buf + buflen;
225156952Sume
226156952Sume	if ((ep - cp) < 1 + RRFIXEDSZ)
227156952Sume		return (-1);
228156952Sume
229186090Sume	*cp++ = 0;				/*%< "." */
230186090Sume	ns_put16(ns_t_opt, cp);			/*%< TYPE */
231156952Sume	cp += INT16SZ;
232156956Sume	if (anslen > 0xffff)
233156956Sume		anslen = 0xffff;		/* limit to 16bit value */
234186090Sume	ns_put16(anslen & 0xffff, cp);		/*%< CLASS = UDP payload size */
235156952Sume	cp += INT16SZ;
236186090Sume	*cp++ = NOERROR;			/*%< extended RCODE */
237186090Sume	*cp++ = 0;				/*%< EDNS version */
238186090Sume
239156952Sume	if (statp->options & RES_USE_DNSSEC) {
240156952Sume#ifdef DEBUG
241156952Sume		if (statp->options & RES_DEBUG)
242156952Sume			printf(";; res_opt()... ENDS0 DNSSEC\n");
243156952Sume#endif
244156952Sume		flags |= NS_OPT_DNSSEC_OK;
245156952Sume	}
246156952Sume	ns_put16(flags, cp);
247156952Sume	cp += INT16SZ;
248186090Sume
249186090Sume	ns_put16(0U, cp);			/*%< RDLEN */
250156952Sume	cp += INT16SZ;
251186090Sume
252156952Sume	hp->arcount = htons(ntohs(hp->arcount) + 1);
253156952Sume
254156952Sume	return (cp - buf);
255156952Sume}
256186090Sume
257186090Sume/*
258186090Sume * Construct variable data (RDATA) block for OPT psuedo-RR, append it
259186090Sume * to the buffer, then update the RDLEN field (previously set to zero by
260186090Sume * res_nopt()) with the new RDATA length.
261186090Sume */
262186090Sumeint
263186090Sumeres_nopt_rdata(res_state statp,
264186090Sume	  int n0,	 	/*%< current offset in buffer */
265186090Sume	  u_char *buf,	 	/*%< buffer to put query */
266186090Sume	  int buflen,		/*%< size of buffer */
267186090Sume	  u_char *rdata,	/*%< ptr to start of opt rdata */
268186090Sume	  u_short code,		/*%< OPTION-CODE */
269186090Sume	  u_short len,		/*%< OPTION-LENGTH */
270186090Sume	  u_char *data)		/*%< OPTION_DATA */
271186090Sume{
272186090Sume	register u_char *cp, *ep;
273186090Sume
274186090Sume#ifdef DEBUG
275186090Sume	if ((statp->options & RES_DEBUG) != 0U)
276186090Sume		printf(";; res_nopt_rdata()\n");
277156952Sume#endif
278170244Sume
279186090Sume	cp = buf + n0;
280186090Sume	ep = buf + buflen;
281186090Sume
282186090Sume	if ((ep - cp) < (4 + len))
283186090Sume		return (-1);
284186090Sume
285186090Sume	if (rdata < (buf + 2) || rdata >= ep)
286186090Sume		return (-1);
287186090Sume
288186090Sume	ns_put16(code, cp);
289186090Sume	cp += INT16SZ;
290186090Sume
291186090Sume	ns_put16(len, cp);
292186090Sume	cp += INT16SZ;
293186090Sume
294186090Sume	memcpy(cp, data, len);
295186090Sume	cp += len;
296186090Sume
297186090Sume	len = cp - rdata;
298186090Sume	ns_put16(len, rdata - 2);	/* Update RDLEN field */
299186090Sume
300186090Sume	return (cp - buf);
301186090Sume}
302186090Sume#endif
303186090Sume
304170244Sume/*! \file */
305