xref: /netbsd/lib/libc/resolv/res_mkquery.c (revision 3873655b)
1 /*	$NetBSD: res_mkquery.c,v 1.9 2008/06/21 20:41:48 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.6.672.1 2008/04/03 02:12:21 marka Exp";
78 #else
79 __RCSID("$NetBSD: res_mkquery.c,v 1.9 2008/06/21 20:41:48 christos Exp $");
80 #endif
81 #endif /* LIBC_SCCS and not lint */
82 
83 #include "port_before.h"
84 
85 #include "namespace.h"
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 #include <resolv.h>
92 #include <stdio.h>
93 #include <string.h>
94 #include "port_after.h"
95 
96 #if 0
97 #ifdef __weak_alias
98 __weak_alias(res_nmkquery,_res_nmkquery)
99 __weak_alias(res_nopt,_res_nopt)
100 #endif
101 #endif
102 
103 /* Options.  Leave them on. */
104 #define DEBUG
105 
106 extern const char *_res_opcodes[];
107 
108 /*%
109  * Form all types of queries.
110  * Returns the size of the result or -1.
111  */
112 int
113 res_nmkquery(res_state statp,
114 	     int op,			/*!< opcode of query  */
115 	     const char *dname,		/*!< domain name  */
116 	     int class, int type,	/*!< class and type of query  */
117 	     const u_char *data,	/*!< resource record data  */
118 	     int datalen,		/*!< length of data  */
119 	     const u_char *newrr_in,	/*!< new rr for modify or append  */
120 	     u_char *buf,		/*!< buffer to put query  */
121 	     int buflen)		/*!< size of buffer  */
122 {
123 	register HEADER *hp;
124 	register u_char *cp, *ep;
125 	register int n;
126 	u_char *dnptrs[20], **dpp, **lastdnptr;
127 
128 	UNUSED(newrr_in);
129 
130 #ifdef DEBUG
131 	if (statp->options & RES_DEBUG)
132 		printf(";; res_nmkquery(%s, %s, %s, %s)\n",
133 		       _res_opcodes[op], dname, p_class(class), p_type(type));
134 #endif
135 	/*
136 	 * Initialize header fields.
137 	 */
138 	if ((buf == NULL) || (buflen < HFIXEDSZ))
139 		return (-1);
140 	memset(buf, 0, HFIXEDSZ);
141 	hp = (HEADER *)(void *)buf;
142 	hp->id = htons(++statp->id);
143 	hp->opcode = op;
144 	hp->rd = (statp->options & RES_RECURSE) != 0U;
145 	hp->rcode = NOERROR;
146 	cp = buf + HFIXEDSZ;
147 	ep = buf + buflen;
148 	dpp = dnptrs;
149 	*dpp++ = buf;
150 	*dpp++ = NULL;
151 	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
152 	/*
153 	 * perform opcode specific processing
154 	 */
155 	switch (op) {
156 	case QUERY:	/*FALLTHROUGH*/
157 	case NS_NOTIFY_OP:
158 		if (ep - cp < QFIXEDSZ)
159 			return (-1);
160 		if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
161 		    lastdnptr)) < 0)
162 			return (-1);
163 		cp += n;
164 		ns_put16(type, cp);
165 		cp += INT16SZ;
166 		ns_put16(class, cp);
167 		cp += INT16SZ;
168 		hp->qdcount = htons(1);
169 		if (op == QUERY || data == NULL)
170 			break;
171 		/*
172 		 * Make an additional record for completion domain.
173 		 */
174 		if ((ep - cp) < RRFIXEDSZ)
175 			return (-1);
176 		n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
177 			    dnptrs, lastdnptr);
178 		if (n < 0)
179 			return (-1);
180 		cp += n;
181 		ns_put16(T_NULL, cp);
182 		cp += INT16SZ;
183 		ns_put16(class, cp);
184 		cp += INT16SZ;
185 		ns_put32(0, cp);
186 		cp += INT32SZ;
187 		ns_put16(0, cp);
188 		cp += INT16SZ;
189 		hp->arcount = htons(1);
190 		break;
191 
192 	case IQUERY:
193 		/*
194 		 * Initialize answer section
195 		 */
196 		if (ep - cp < 1 + RRFIXEDSZ + datalen)
197 			return (-1);
198 		*cp++ = '\0';	/*%< no domain name */
199 		ns_put16(type, cp);
200 		cp += INT16SZ;
201 		ns_put16(class, cp);
202 		cp += INT16SZ;
203 		ns_put32(0, cp);
204 		cp += INT32SZ;
205 		ns_put16(datalen, cp);
206 		cp += INT16SZ;
207 		if (datalen) {
208 			memcpy(cp, data, (size_t)datalen);
209 			cp += datalen;
210 		}
211 		hp->ancount = htons(1);
212 		break;
213 
214 	default:
215 		return (-1);
216 	}
217 	return (cp - buf);
218 }
219 
220 #ifdef RES_USE_EDNS0
221 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
222 
223 int
224 res_nopt(res_state statp,
225 	 int n0,		/*%< current offset in buffer */
226 	 u_char *buf,		/*%< buffer to put query */
227 	 int buflen,		/*%< size of buffer */
228 	 int anslen)		/*%< UDP answer buffer size */
229 {
230 	register HEADER *hp;
231 	register u_char *cp, *ep;
232 	u_int16_t flags = 0;
233 
234 #ifdef DEBUG
235 	if ((statp->options & RES_DEBUG) != 0U)
236 		printf(";; res_nopt()\n");
237 #endif
238 
239 	hp = (HEADER *)(void *)buf;
240 	cp = buf + n0;
241 	ep = buf + buflen;
242 
243 	if ((ep - cp) < 1 + RRFIXEDSZ)
244 		return (-1);
245 
246 	*cp++ = 0;			/*%< "." */
247 	ns_put16(ns_t_opt, cp);		/*%< TYPE */
248 	cp += INT16SZ;
249 	ns_put16(anslen & 0xffff, cp);	/*%< CLASS = UDP payload size */
250 	cp += INT16SZ;
251 	*cp++ = NOERROR;		/*%< extended RCODE */
252 	*cp++ = 0;			/*%< EDNS version */
253 
254 	if (statp->options & RES_USE_DNSSEC) {
255 #ifdef DEBUG
256 		if (statp->options & RES_DEBUG)
257 			printf(";; res_opt()... ENDS0 DNSSEC\n");
258 #endif
259 		flags |= NS_OPT_DNSSEC_OK;
260 	}
261 	ns_put16(flags, cp);
262 	cp += INT16SZ;
263 
264 	ns_put16(0U, cp);		/*%< RDLEN */
265 	cp += INT16SZ;
266 
267 	hp->arcount = htons(ntohs(hp->arcount) + 1);
268 
269 	return (cp - buf);
270 }
271 
272 /*
273  * Construct variable data (RDATA) block for OPT psuedo-RR, append it
274  * to the buffer, then update the RDLEN field (previously set to zero by
275  * res_nopt()) with the new RDATA length.
276  */
277 int
278 res_nopt_rdata(res_state statp,
279 	  int n0,	 	/*%< current offset in buffer */
280 	  u_char *buf,	 	/*%< buffer to put query */
281 	  int buflen,		/*%< size of buffer */
282 	  u_char *rdata,	/*%< ptr to start of opt rdata */
283 	  u_short code,		/*%< OPTION-CODE */
284 	  u_short len,		/*%< OPTION-LENGTH */
285 	  u_char *data)		/*%< OPTION_DATA */
286 {
287 	register u_char *cp, *ep;
288 
289 #ifdef DEBUG
290 	if ((statp->options & RES_DEBUG) != 0U)
291 		printf(";; res_nopt_rdata()\n");
292 #endif
293 
294 	cp = buf + n0;
295 	ep = buf + buflen;
296 
297 	if ((ep - cp) < (4 + len))
298 		return (-1);
299 
300 	if (rdata < (buf + 2) || rdata >= ep)
301 		return (-1);
302 
303 	ns_put16(code, cp);
304 	cp += INT16SZ;
305 
306 	ns_put16(len, cp);
307 	cp += INT16SZ;
308 
309 	memcpy(cp, data, len);
310 	cp += len;
311 
312 	len = cp - rdata;
313 	ns_put16(len, rdata - 2);	/* Update RDLEN field */
314 
315 	return (cp - buf);
316 }
317 #endif
318 
319 /*! \file */
320