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