blob: 7a2a3f7d9ed60c35ca63d7c78ce0efa1a02b9a52 [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
73#include <sys/cdefs.h>
74#if defined(LIBC_SCCS) && !defined(lint)
75#ifdef notdef
76static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
77static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
78#else
79__RCSID("$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $");
80#endif
81#endif /* LIBC_SCCS and not lint */
82
Bernie Innocenti55864192018-08-30 04:05:20 +090083#include <arpa/nameser.h>
84#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#include <netinet/in.h>
86#include <sys/param.h>
87#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090088#ifdef ANDROID_CHANGES
89#include "resolv_private.h"
90#else
91#include <resolv.h>
92#endif
93#include <stdio.h>
94#include <string.h>
95
96/* Options. Leave them on. */
97#ifndef DEBUG
98#define DEBUG
99#endif
100
101#ifndef lint
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900102#define UNUSED(a) (void) &a
Bernie Innocenti55864192018-08-30 04:05:20 +0900103#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900104#define UNUSED(a) a = a
Bernie Innocenti55864192018-08-30 04:05:20 +0900105#endif
106
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900107extern const char* _res_opcodes[];
Bernie Innocenti55864192018-08-30 04:05:20 +0900108
109/*
110 * Form all types of queries.
111 * Returns the size of the result or -1.
112 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113int res_nmkquery(res_state statp, int op, /* opcode of query */
114 const char* dname, /* domain name */
115 int class, int type, /* class and type of query */
116 const u_char* data, /* resource record data */
117 int datalen, /* length of data */
118 const u_char* newrr_in, /* new rr for modify or append */
119 u_char* buf, /* buffer to put query */
120 int buflen) /* size of buffer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900121{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122 register HEADER* hp;
123 register u_char *cp, *ep;
124 register int n;
125 u_char *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900126
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900127 UNUSED(newrr_in);
Bernie Innocenti55864192018-08-30 04:05:20 +0900128
129#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900130 if (statp->options & RES_DEBUG)
131 printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(class),
132 p_type(type));
Bernie Innocenti55864192018-08-30 04:05:20 +0900133#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134 /*
135 * Initialize header fields.
136 */
137 if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
138 memset(buf, 0, HFIXEDSZ);
139 hp = (HEADER*) (void*) buf;
140 hp->id = htons(res_randomid());
141 hp->opcode = op;
142 hp->rd = (statp->options & RES_RECURSE) != 0U;
143 hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
144 hp->rcode = NOERROR;
145 cp = buf + HFIXEDSZ;
146 ep = buf + buflen;
147 dpp = dnptrs;
148 *dpp++ = buf;
149 *dpp++ = NULL;
150 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
151 /*
152 * perform opcode specific processing
153 */
154 switch (op) {
155 case QUERY: /*FALLTHROUGH*/
156 case NS_NOTIFY_OP:
157 if (ep - cp < QFIXEDSZ) return (-1);
158 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
159 cp += n;
160 ns_put16(type, cp);
161 cp += INT16SZ;
162 ns_put16(class, cp);
163 cp += INT16SZ;
164 hp->qdcount = htons(1);
165 if (op == QUERY || data == NULL) break;
166 /*
167 * Make an additional record for completion domain.
168 */
169 if ((ep - cp) < RRFIXEDSZ) return (-1);
170 n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
171 if (n < 0) return (-1);
172 cp += n;
173 ns_put16(T_NULL, cp);
174 cp += INT16SZ;
175 ns_put16(class, cp);
176 cp += INT16SZ;
177 ns_put32(0, cp);
178 cp += INT32SZ;
179 ns_put16(0, cp);
180 cp += INT16SZ;
181 hp->arcount = htons(1);
182 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900183
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900184 case IQUERY:
185 /*
186 * Initialize answer section
187 */
188 if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
189 *cp++ = '\0'; /* no domain name */
190 ns_put16(type, cp);
191 cp += INT16SZ;
192 ns_put16(class, cp);
193 cp += INT16SZ;
194 ns_put32(0, cp);
195 cp += INT32SZ;
196 ns_put16(datalen, cp);
197 cp += INT16SZ;
198 if (datalen) {
199 memcpy(cp, data, (size_t) datalen);
200 cp += datalen;
201 }
202 hp->ancount = htons(1);
203 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900204
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900205 default:
206 return (-1);
207 }
208 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900209}
210
211#ifdef RES_USE_EDNS0
212/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
213#ifndef T_OPT
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214#define T_OPT 41
Bernie Innocenti55864192018-08-30 04:05:20 +0900215#endif
216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217int res_nopt(res_state statp, int n0, /* current offset in buffer */
218 u_char* buf, /* buffer to put query */
219 int buflen, /* size of buffer */
220 int anslen) /* UDP answer buffer size */
Bernie Innocenti55864192018-08-30 04:05:20 +0900221{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900222 register HEADER* hp;
223 register u_char *cp, *ep;
224 u_int16_t flags = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900225
226#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900227 if ((statp->options & RES_DEBUG) != 0U) printf(";; res_nopt()\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900228#endif
229
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900230 hp = (HEADER*) (void*) buf;
231 cp = buf + n0;
232 ep = buf + buflen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234 if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900235
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900236 *cp++ = 0; /* "." */
Bernie Innocenti55864192018-08-30 04:05:20 +0900237
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900238 ns_put16(T_OPT, cp); /* TYPE */
239 cp += INT16SZ;
240 if (anslen > 0xffff) anslen = 0xffff;
241 ns_put16(anslen, cp); /* CLASS = UDP payload size */
242 cp += INT16SZ;
243 *cp++ = NOERROR; /* extended RCODE */
244 *cp++ = 0; /* EDNS version */
245 if (statp->options & RES_USE_DNSSEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900246#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247 if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900248#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900249 flags |= NS_OPT_DNSSEC_OK;
250 }
251 ns_put16(flags, cp);
252 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900253#ifdef EDNS0_PADDING
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254 {
255 u_int16_t minlen = (cp - buf) + 3 * INT16SZ;
256 u_int16_t extra = minlen % EDNS0_PADDING;
257 u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING;
258 if (minlen > buflen) {
259 return (-1);
260 }
261 padlen = MIN(padlen, buflen - minlen);
262 ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
263 cp += INT16SZ;
264 ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
265 cp += INT16SZ;
266 ns_put16(padlen, cp); /* OPTION-LENGTH */
267 cp += INT16SZ;
268 memset(cp, 0, padlen);
269 cp += padlen;
270 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900271#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900272 ns_put16(0, cp); /* RDLEN */
273 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900274#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900275 hp->arcount = htons(ntohs(hp->arcount) + 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900276
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900277 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900278}
279#endif