blob: 8ebf3e77a7a73258957a5e2967a9c9c10da42ce2 [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>
76#include <sys/param.h>
77#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090078#ifdef ANDROID_CHANGES
79#include "resolv_private.h"
80#else
81#include <resolv.h>
82#endif
83#include <stdio.h>
84#include <string.h>
85
86/* Options. Leave them on. */
87#ifndef DEBUG
88#define DEBUG
89#endif
90
91#ifndef lint
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090092#define UNUSED(a) (void) &a
Bernie Innocenti55864192018-08-30 04:05:20 +090093#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090094#define UNUSED(a) a = a
Bernie Innocenti55864192018-08-30 04:05:20 +090095#endif
96
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090097extern const char* _res_opcodes[];
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 Innocentif12d5bb2018-08-31 14:09:46 +0900103int res_nmkquery(res_state statp, int op, /* opcode of query */
104 const char* dname, /* domain name */
105 int class, 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 Innocentif12d5bb2018-08-31 14:09:46 +0900112 register HEADER* hp;
113 register u_char *cp, *ep;
114 register int n;
115 u_char *dnptrs[20], **dpp, **lastdnptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900116
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900117 UNUSED(newrr_in);
Bernie Innocenti55864192018-08-30 04:05:20 +0900118
119#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 if (statp->options & RES_DEBUG)
121 printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(class),
122 p_type(type));
Bernie Innocenti55864192018-08-30 04:05:20 +0900123#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124 /*
125 * Initialize header fields.
126 */
127 if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
128 memset(buf, 0, HFIXEDSZ);
129 hp = (HEADER*) (void*) buf;
130 hp->id = htons(res_randomid());
131 hp->opcode = op;
132 hp->rd = (statp->options & RES_RECURSE) != 0U;
133 hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
134 hp->rcode = NOERROR;
135 cp = buf + HFIXEDSZ;
136 ep = buf + buflen;
137 dpp = dnptrs;
138 *dpp++ = buf;
139 *dpp++ = NULL;
140 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
141 /*
142 * perform opcode specific processing
143 */
144 switch (op) {
145 case QUERY: /*FALLTHROUGH*/
146 case NS_NOTIFY_OP:
147 if (ep - cp < QFIXEDSZ) return (-1);
148 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
149 cp += n;
150 ns_put16(type, cp);
151 cp += INT16SZ;
152 ns_put16(class, cp);
153 cp += INT16SZ;
154 hp->qdcount = htons(1);
155 if (op == QUERY || data == NULL) break;
156 /*
157 * Make an additional record for completion domain.
158 */
159 if ((ep - cp) < RRFIXEDSZ) return (-1);
160 n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
161 if (n < 0) return (-1);
162 cp += n;
163 ns_put16(T_NULL, cp);
164 cp += INT16SZ;
165 ns_put16(class, cp);
166 cp += INT16SZ;
167 ns_put32(0, cp);
168 cp += INT32SZ;
169 ns_put16(0, cp);
170 cp += INT16SZ;
171 hp->arcount = htons(1);
172 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900173
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900174 case IQUERY:
175 /*
176 * Initialize answer section
177 */
178 if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
179 *cp++ = '\0'; /* no domain name */
180 ns_put16(type, cp);
181 cp += INT16SZ;
182 ns_put16(class, cp);
183 cp += INT16SZ;
184 ns_put32(0, cp);
185 cp += INT32SZ;
186 ns_put16(datalen, cp);
187 cp += INT16SZ;
188 if (datalen) {
189 memcpy(cp, data, (size_t) datalen);
190 cp += datalen;
191 }
192 hp->ancount = htons(1);
193 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900194
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195 default:
196 return (-1);
197 }
198 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900199}
200
201#ifdef RES_USE_EDNS0
202/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
203#ifndef T_OPT
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204#define T_OPT 41
Bernie Innocenti55864192018-08-30 04:05:20 +0900205#endif
206
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207int res_nopt(res_state statp, int n0, /* current offset in buffer */
208 u_char* buf, /* buffer to put query */
209 int buflen, /* size of buffer */
210 int anslen) /* UDP answer buffer size */
Bernie Innocenti55864192018-08-30 04:05:20 +0900211{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212 register HEADER* hp;
213 register u_char *cp, *ep;
214 u_int16_t flags = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900215
216#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217 if ((statp->options & RES_DEBUG) != 0U) printf(";; res_nopt()\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900218#endif
219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220 hp = (HEADER*) (void*) buf;
221 cp = buf + n0;
222 ep = buf + buflen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900223
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900224 if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900225
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900226 *cp++ = 0; /* "." */
Bernie Innocenti55864192018-08-30 04:05:20 +0900227
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228 ns_put16(T_OPT, cp); /* TYPE */
229 cp += INT16SZ;
230 if (anslen > 0xffff) anslen = 0xffff;
231 ns_put16(anslen, cp); /* CLASS = UDP payload size */
232 cp += INT16SZ;
233 *cp++ = NOERROR; /* extended RCODE */
234 *cp++ = 0; /* EDNS version */
235 if (statp->options & RES_USE_DNSSEC) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900236#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237 if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900238#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900239 flags |= NS_OPT_DNSSEC_OK;
240 }
241 ns_put16(flags, cp);
242 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900243#ifdef EDNS0_PADDING
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900244 {
245 u_int16_t minlen = (cp - buf) + 3 * INT16SZ;
246 u_int16_t extra = minlen % EDNS0_PADDING;
247 u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING;
248 if (minlen > buflen) {
249 return (-1);
250 }
251 padlen = MIN(padlen, buflen - minlen);
252 ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
253 cp += INT16SZ;
254 ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
255 cp += INT16SZ;
256 ns_put16(padlen, cp); /* OPTION-LENGTH */
257 cp += INT16SZ;
258 memset(cp, 0, padlen);
259 cp += padlen;
260 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900261#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900262 ns_put16(0, cp); /* RDLEN */
263 cp += INT16SZ;
Bernie Innocenti55864192018-08-30 04:05:20 +0900264#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900265 hp->arcount = htons(ntohs(hp->arcount) + 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267 return (cp - buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900268}
269#endif