blob: 6d9a26972439c9aaf59ffb313c6dff2ab0f6f64e [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 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
Bernie Innocenti55864192018-08-30 04:05:20 +090073#include <arpa/nameser.h>
74#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090075#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090076#include <stdio.h>
77#include <string.h>
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090078#include <sys/param.h>
79#include <sys/types.h>
80
81#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090082
83/* Options. Leave them on. */
84#ifndef DEBUG
85#define DEBUG
86#endif
87
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#define UNUSED(a) (void) &a
Bernie Innocenti55864192018-08-30 04:05:20 +090089
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090090extern const char* _res_opcodes[];
Bernie Innocenti55864192018-08-30 04:05:20 +090091
92/*
93 * Form all types of queries.
94 * Returns the size of the result or -1.
95 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090096int res_nmkquery(res_state statp, 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 */
Bernie Innocenti55864192018-08-30 04:05:20 +0900104{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900105 register HEADER* hp;
106 register u_char *cp, *ep;
107 register int n;
108 u_char *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900109
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900110 UNUSED(newrr_in);
Bernie Innocenti55864192018-08-30 04:05:20 +0900111
112#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113 if (statp->options & RES_DEBUG)
114 printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(class),
115 p_type(type));
Bernie Innocenti55864192018-08-30 04:05:20 +0900116#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900117 /*
118 * Initialize header fields.
119 */
120 if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
121 memset(buf, 0, HFIXEDSZ);
122 hp = (HEADER*) (void*) buf;
123 hp->id = htons(res_randomid());
124 hp->opcode = op;
125 hp->rd = (statp->options & RES_RECURSE) != 0U;
126 hp->ad = (statp->options & RES_USE_DNSSEC) != 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) return (-1);
141 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
142 cp += n;
143 ns_put16(type, cp);
144 cp += INT16SZ;
145 ns_put16(class, cp);
146 cp += INT16SZ;
147 hp->qdcount = htons(1);
148 if (op == QUERY || data == NULL) break;
149 /*
150 * Make an additional record for completion domain.
151 */
152 if ((ep - cp) < RRFIXEDSZ) return (-1);
153 n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
154 if (n < 0) return (-1);
155 cp += n;
156 ns_put16(T_NULL, cp);
157 cp += INT16SZ;
158 ns_put16(class, cp);
159 cp += INT16SZ;
160 ns_put32(0, cp);
161 cp += INT32SZ;
162 ns_put16(0, cp);
163 cp += INT16SZ;
164 hp->arcount = htons(1);
165 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900166
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900167 case IQUERY:
168 /*
169 * Initialize answer section
170 */
171 if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
172 *cp++ = '\0'; /* no domain name */
173 ns_put16(type, cp);
174 cp += INT16SZ;
175 ns_put16(class, cp);
176 cp += INT16SZ;
177 ns_put32(0, cp);
178 cp += INT32SZ;
179 ns_put16(datalen, cp);
180 cp += INT16SZ;
181 if (datalen) {
182 memcpy(cp, data, (size_t) datalen);
183 cp += datalen;
184 }
185 hp->ancount = htons(1);
186 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900187
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900188 default:
189 return (-1);
190 }
191 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900192}
193
194#ifdef RES_USE_EDNS0
195/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
196#ifndef T_OPT
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197#define T_OPT 41
Bernie Innocenti55864192018-08-30 04:05:20 +0900198#endif
199
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900200int res_nopt(res_state statp, int n0, /* current offset in buffer */
201 u_char* buf, /* buffer to put query */
202 int buflen, /* size of buffer */
203 int anslen) /* UDP answer buffer size */
Bernie Innocenti55864192018-08-30 04:05:20 +0900204{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900205 register HEADER* hp;
206 register u_char *cp, *ep;
207 u_int16_t flags = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900208
209#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900210 if ((statp->options & RES_DEBUG) != 0U) printf(";; res_nopt()\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900211#endif
212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213 hp = (HEADER*) (void*) buf;
214 cp = buf + n0;
215 ep = buf + buflen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217 if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900218
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900219 *cp++ = 0; /* "." */
Bernie Innocenti55864192018-08-30 04:05:20 +0900220
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900221 ns_put16(T_OPT, cp); /* TYPE */
222 cp += INT16SZ;
223 if (anslen > 0xffff) anslen = 0xffff;
224 ns_put16(anslen, cp); /* CLASS = UDP payload size */
225 cp += INT16SZ;
226 *cp++ = NOERROR; /* extended RCODE */
227 *cp++ = 0; /* EDNS version */
228 if (statp->options & RES_USE_DNSSEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900229#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900230 if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900231#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232 flags |= NS_OPT_DNSSEC_OK;
233 }
234 ns_put16(flags, cp);
235 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900236#ifdef EDNS0_PADDING
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237 {
238 u_int16_t minlen = (cp - buf) + 3 * INT16SZ;
239 u_int16_t extra = minlen % EDNS0_PADDING;
240 u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING;
241 if (minlen > buflen) {
242 return (-1);
243 }
244 padlen = MIN(padlen, buflen - minlen);
245 ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
246 cp += INT16SZ;
247 ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
248 cp += INT16SZ;
249 ns_put16(padlen, cp); /* OPTION-LENGTH */
250 cp += INT16SZ;
251 memset(cp, 0, padlen);
252 cp += padlen;
253 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900254#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900255 ns_put16(0, cp); /* RDLEN */
256 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900257#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900258 hp->arcount = htons(ntohs(hp->arcount) + 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900259
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900260 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900261}
262#endif