blob: 1f2879d5b0b18a6d734a1a7ffc204a4207b2f915 [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
92// Defined in res_data.cpp
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090093extern const char* _res_opcodes[];
Bernie Innocenti55864192018-08-30 04:05:20 +090094
95/*
96 * Form all types of queries.
97 * Returns the size of the result or -1.
98 */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +090099int res_nmkquery(res_state statp, int op, /* opcode of query */
100 const char* dname, /* domain name */
101 int cl, int type, /* class and type of query */
102 const u_char* data, /* resource record data */
103 int datalen, /* length of data */
104 const u_char* /*newrr_in*/, /* new rr for modify or append */
105 u_char* buf, /* buffer to put query */
106 int buflen) /* size of buffer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900107{
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900108 HEADER* hp;
109 u_char *cp, *ep;
110 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 u_char *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900112
Bernie Innocenti55864192018-08-30 04:05:20 +0900113#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114 if (statp->options & RES_DEBUG)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900115 printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(cl),
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116 p_type(type));
Bernie Innocenti55864192018-08-30 04:05:20 +0900117#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900118 /*
119 * Initialize header fields.
120 */
121 if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
122 memset(buf, 0, HFIXEDSZ);
123 hp = (HEADER*) (void*) buf;
124 hp->id = htons(res_randomid());
125 hp->opcode = op;
126 hp->rd = (statp->options & RES_RECURSE) != 0U;
127 hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
128 hp->rcode = NOERROR;
129 cp = buf + HFIXEDSZ;
130 ep = buf + buflen;
131 dpp = dnptrs;
132 *dpp++ = buf;
133 *dpp++ = NULL;
134 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
135 /*
136 * perform opcode specific processing
137 */
138 switch (op) {
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900139 case QUERY:
140 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141 case NS_NOTIFY_OP:
142 if (ep - cp < QFIXEDSZ) return (-1);
143 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
144 cp += n;
145 ns_put16(type, cp);
146 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900147 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900148 cp += INT16SZ;
149 hp->qdcount = htons(1);
150 if (op == QUERY || data == NULL) break;
151 /*
152 * Make an additional record for completion domain.
153 */
154 if ((ep - cp) < RRFIXEDSZ) return (-1);
155 n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
156 if (n < 0) return (-1);
157 cp += n;
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900158 ns_put16(ns_t_null, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900159 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900160 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900161 cp += INT16SZ;
162 ns_put32(0, cp);
163 cp += INT32SZ;
164 ns_put16(0, cp);
165 cp += INT16SZ;
166 hp->arcount = htons(1);
167 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900168
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900169 case IQUERY:
170 /*
171 * Initialize answer section
172 */
173 if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
174 *cp++ = '\0'; /* no domain name */
175 ns_put16(type, cp);
176 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900177 ns_put16(cl, cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178 cp += INT16SZ;
179 ns_put32(0, cp);
180 cp += INT32SZ;
181 ns_put16(datalen, cp);
182 cp += INT16SZ;
183 if (datalen) {
184 memcpy(cp, data, (size_t) datalen);
185 cp += datalen;
186 }
187 hp->ancount = htons(1);
188 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900189
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190 default:
191 return (-1);
192 }
193 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900194}
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 Innocenti1fbca5c2018-10-01 20:46:20 +0900217 // Attach OPT pseudo-RR, as documented in RFC2671 (EDNS0).
218 ns_put16(ns_t_opt, cp); /* TYPE */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900219 cp += INT16SZ;
220 if (anslen > 0xffff) anslen = 0xffff;
221 ns_put16(anslen, cp); /* CLASS = UDP payload size */
222 cp += INT16SZ;
223 *cp++ = NOERROR; /* extended RCODE */
224 *cp++ = 0; /* EDNS version */
225 if (statp->options & RES_USE_DNSSEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900226#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900227 if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900228#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900229 flags |= NS_OPT_DNSSEC_OK;
230 }
231 ns_put16(flags, cp);
232 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocenti1fbca5c2018-10-01 20:46:20 +0900234 // EDNS0 padding
235 const uint16_t minlen = static_cast<uint16_t>(cp - buf) + 3 * INT16SZ;
236 const uint16_t extra = minlen % kEdns0Padding;
237 uint16_t padlen = (kEdns0Padding - extra) % kEdns0Padding;
238 if (minlen > buflen) {
239 return -1;
240 }
241 padlen = std::min(padlen, static_cast<uint16_t>(buflen - minlen));
242 ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
243 cp += INT16SZ;
244 ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
245 cp += INT16SZ;
246 ns_put16(padlen, cp); /* OPTION-LENGTH */
247 cp += INT16SZ;
248 memset(cp, 0, padlen);
249 cp += padlen;
250
251 hp->arcount = htons(ntohs(hp->arcount) + 1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900252 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900253}