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