blob: b2f97c715d4514b282a31ffcedf3f1ca383d6e4b [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 Innocenti1fbca5c2018-10-01 20:46:20 +090073#include <algorithm> // std::min()
74
Bernie Innocenti55864192018-08-30 04:05:20 +090075#include <arpa/nameser.h>
76#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090078#include <stdio.h>
79#include <string.h>
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090080#include <sys/types.h>
81
82#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090083
84/* Options. Leave them on. */
85#ifndef DEBUG
86#define DEBUG
87#endif
88
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +090089// Queries will be padded to a multiple of this length when EDNS0 is active.
90constexpr uint16_t kEdns0Padding = 128;
91
Mike Yu69615f62018-11-06 15:42:36 +080092extern const char* const _res_opcodes[] = {
93 "QUERY", "IQUERY", "CQUERYM", "CQUERYU", /* experimental */
94 "NOTIFY", /* experimental */
95 "UPDATE", "6", "7", "8", "9", "10",
96 "11", "12", "13", "ZONEINIT", "ZONEREF",
97};
Bernie Innocenti55864192018-08-30 04:05:20 +090098
99/*
100 * Form all types of queries.
101 * Returns the size of the result or -1.
102 */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900103int res_nmkquery(res_state statp, int op, /* opcode of query */
104 const char* dname, /* domain name */
105 int cl, int type, /* class and type of query */
106 const u_char* data, /* resource record data */
107 int datalen, /* length of data */
108 const u_char* /*newrr_in*/, /* new rr for modify or append */
109 u_char* buf, /* buffer to put query */
110 int buflen) /* size of buffer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900111{
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900112 HEADER* hp;
113 u_char *cp, *ep;
114 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900115 u_char *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900116
Bernie Innocenti55864192018-08-30 04:05:20 +0900117#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900118 if (statp->options & RES_DEBUG)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900119 printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(cl),
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 p_type(type));
Bernie Innocenti55864192018-08-30 04:05:20 +0900121#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122 /*
123 * Initialize header fields.
124 */
125 if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
126 memset(buf, 0, HFIXEDSZ);
127 hp = (HEADER*) (void*) buf;
Luke Huang042e9422019-02-13 14:05:02 +0800128 hp->id = htons(arc4random_uniform(65536));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900129 hp->opcode = op;
130 hp->rd = (statp->options & RES_RECURSE) != 0U;
131 hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
132 hp->rcode = NOERROR;
133 cp = buf + HFIXEDSZ;
134 ep = buf + buflen;
135 dpp = dnptrs;
136 *dpp++ = buf;
137 *dpp++ = NULL;
138 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
139 /*
140 * perform opcode specific processing
141 */
142 switch (op) {
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900143 case QUERY:
144 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145 case NS_NOTIFY_OP:
146 if (ep - cp < QFIXEDSZ) return (-1);
147 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
148 cp += n;
149 ns_put16(type, cp);
150 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900151 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 cp += INT16SZ;
153 hp->qdcount = htons(1);
154 if (op == QUERY || data == NULL) break;
155 /*
156 * Make an additional record for completion domain.
157 */
158 if ((ep - cp) < RRFIXEDSZ) return (-1);
159 n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
160 if (n < 0) return (-1);
161 cp += n;
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900162 ns_put16(ns_t_null, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900164 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900165 cp += INT16SZ;
166 ns_put32(0, cp);
167 cp += INT32SZ;
168 ns_put16(0, cp);
169 cp += INT16SZ;
170 hp->arcount = htons(1);
171 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900172
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173 case IQUERY:
174 /*
175 * Initialize answer section
176 */
177 if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
178 *cp++ = '\0'; /* no domain name */
179 ns_put16(type, cp);
180 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900181 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182 cp += INT16SZ;
183 ns_put32(0, cp);
184 cp += INT32SZ;
185 ns_put16(datalen, cp);
186 cp += INT16SZ;
187 if (datalen) {
188 memcpy(cp, data, (size_t) datalen);
189 cp += datalen;
190 }
191 hp->ancount = htons(1);
192 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194 default:
195 return (-1);
196 }
197 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900198}
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 Innocenti1f4a9fd2018-09-07 21:10:25 +0900205 HEADER* hp;
206 u_char *cp, *ep;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207 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 Innocenti1fbca5c2018-10-01 20:46:20 +0900221 // Attach OPT pseudo-RR, as documented in RFC2671 (EDNS0).
222 ns_put16(ns_t_opt, cp); /* TYPE */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223 cp += INT16SZ;
224 if (anslen > 0xffff) anslen = 0xffff;
225 ns_put16(anslen, cp); /* CLASS = UDP payload size */
226 cp += INT16SZ;
227 *cp++ = NOERROR; /* extended RCODE */
228 *cp++ = 0; /* EDNS version */
229 if (statp->options & RES_USE_DNSSEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900230#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900231 if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900232#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233 flags |= NS_OPT_DNSSEC_OK;
234 }
235 ns_put16(flags, cp);
236 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900237
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900238 // EDNS0 padding
239 const uint16_t minlen = static_cast<uint16_t>(cp - buf) + 3 * INT16SZ;
240 const uint16_t extra = minlen % kEdns0Padding;
241 uint16_t padlen = (kEdns0Padding - extra) % kEdns0Padding;
242 if (minlen > buflen) {
243 return -1;
244 }
245 padlen = std::min(padlen, static_cast<uint16_t>(buflen - minlen));
246 ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
247 cp += INT16SZ;
248 ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
249 cp += INT16SZ;
250 ns_put16(padlen, cp); /* OPTION-LENGTH */
251 cp += INT16SZ;
252 memset(cp, 0, padlen);
253 cp += padlen;
254
255 hp->arcount = htons(ntohs(hp->arcount) + 1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900256 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900257}