blob: 82c42d5d8ce7573c8392262ca5bc5998245909f0 [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 +090088extern const char* _res_opcodes[];
Bernie Innocenti55864192018-08-30 04:05:20 +090089
90/*
91 * Form all types of queries.
92 * Returns the size of the result or -1.
93 */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +090094int res_nmkquery(res_state statp, int op, /* opcode of query */
95 const char* dname, /* domain name */
96 int cl, int type, /* class and type of query */
97 const u_char* data, /* resource record data */
98 int datalen, /* length of data */
99 const u_char* /*newrr_in*/, /* new rr for modify or append */
100 u_char* buf, /* buffer to put query */
101 int buflen) /* size of buffer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900102{
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900103 HEADER* hp;
104 u_char *cp, *ep;
105 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106 u_char *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900107
Bernie Innocenti55864192018-08-30 04:05:20 +0900108#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900109 if (statp->options & RES_DEBUG)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900110 printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(cl),
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 p_type(type));
Bernie Innocenti55864192018-08-30 04:05:20 +0900112#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113 /*
114 * Initialize header fields.
115 */
116 if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
117 memset(buf, 0, HFIXEDSZ);
118 hp = (HEADER*) (void*) buf;
119 hp->id = htons(res_randomid());
120 hp->opcode = op;
121 hp->rd = (statp->options & RES_RECURSE) != 0U;
122 hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
123 hp->rcode = NOERROR;
124 cp = buf + HFIXEDSZ;
125 ep = buf + buflen;
126 dpp = dnptrs;
127 *dpp++ = buf;
128 *dpp++ = NULL;
129 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
130 /*
131 * perform opcode specific processing
132 */
133 switch (op) {
134 case QUERY: /*FALLTHROUGH*/
135 case NS_NOTIFY_OP:
136 if (ep - cp < QFIXEDSZ) return (-1);
137 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
138 cp += n;
139 ns_put16(type, cp);
140 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900141 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142 cp += INT16SZ;
143 hp->qdcount = htons(1);
144 if (op == QUERY || data == NULL) break;
145 /*
146 * Make an additional record for completion domain.
147 */
148 if ((ep - cp) < RRFIXEDSZ) return (-1);
149 n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
150 if (n < 0) return (-1);
151 cp += n;
152 ns_put16(T_NULL, cp);
153 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900154 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 cp += INT16SZ;
156 ns_put32(0, cp);
157 cp += INT32SZ;
158 ns_put16(0, cp);
159 cp += INT16SZ;
160 hp->arcount = htons(1);
161 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900162
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163 case IQUERY:
164 /*
165 * Initialize answer section
166 */
167 if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
168 *cp++ = '\0'; /* no domain name */
169 ns_put16(type, cp);
170 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900171 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172 cp += INT16SZ;
173 ns_put32(0, cp);
174 cp += INT32SZ;
175 ns_put16(datalen, cp);
176 cp += INT16SZ;
177 if (datalen) {
178 memcpy(cp, data, (size_t) datalen);
179 cp += datalen;
180 }
181 hp->ancount = htons(1);
182 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900183
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900184 default:
185 return (-1);
186 }
187 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900188}
189
190#ifdef RES_USE_EDNS0
191/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
192#ifndef T_OPT
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900193#define T_OPT 41
Bernie Innocenti55864192018-08-30 04:05:20 +0900194#endif
195
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196int res_nopt(res_state statp, int n0, /* current offset in buffer */
197 u_char* buf, /* buffer to put query */
198 int buflen, /* size of buffer */
199 int anslen) /* UDP answer buffer size */
Bernie Innocenti55864192018-08-30 04:05:20 +0900200{
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900201 HEADER* hp;
202 u_char *cp, *ep;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203 u_int16_t flags = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900204
205#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206 if ((statp->options & RES_DEBUG) != 0U) printf(";; res_nopt()\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900207#endif
208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900209 hp = (HEADER*) (void*) buf;
210 cp = buf + n0;
211 ep = buf + buflen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213 if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215 *cp++ = 0; /* "." */
Bernie Innocenti55864192018-08-30 04:05:20 +0900216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217 ns_put16(T_OPT, cp); /* TYPE */
218 cp += INT16SZ;
219 if (anslen > 0xffff) anslen = 0xffff;
220 ns_put16(anslen, cp); /* CLASS = UDP payload size */
221 cp += INT16SZ;
222 *cp++ = NOERROR; /* extended RCODE */
223 *cp++ = 0; /* EDNS version */
224 if (statp->options & RES_USE_DNSSEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900225#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900226 if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900227#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228 flags |= NS_OPT_DNSSEC_OK;
229 }
230 ns_put16(flags, cp);
231 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900232#ifdef EDNS0_PADDING
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233 {
234 u_int16_t minlen = (cp - buf) + 3 * INT16SZ;
235 u_int16_t extra = minlen % EDNS0_PADDING;
236 u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING;
237 if (minlen > buflen) {
238 return (-1);
239 }
240 padlen = MIN(padlen, buflen - minlen);
241 ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
242 cp += INT16SZ;
243 ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
244 cp += INT16SZ;
245 ns_put16(padlen, cp); /* OPTION-LENGTH */
246 cp += INT16SZ;
247 memset(cp, 0, padlen);
248 cp += padlen;
249 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900250#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900251 ns_put16(0, cp); /* RDLEN */
252 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900253#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254 hp->arcount = htons(ntohs(hp->arcount) + 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900255
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900256 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900257}
258#endif