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