]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/res_mkquery.c
hurd: Fix build
[thirdparty/glibc.git] / resolv / res_mkquery.c
CommitLineData
7ab27b76 1/* Creation of DNS query packets.
04277e02 2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
7ab27b76
FW
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
28f540f4 19/*
28f540f4
RM
20 * Copyright (c) 1985, 1993
21 * The Regents of the University of California. All rights reserved.
e33b89df 22 *
28f540f4
RM
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
28f540f4
RM
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
e33b89df 34 *
28f540f4
RM
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
b43b13ac
UD
46 */
47
48/*
28f540f4 49 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
e33b89df 50 *
28f540f4
RM
51 * Permission to use, copy, modify, and distribute this software for any
52 * purpose with or without fee is hereby granted, provided that the above
53 * copyright notice and this permission notice appear in all copies, and that
54 * the name of Digital Equipment Corporation not be used in advertising or
55 * publicity pertaining to distribution of the document or software without
56 * specific, written prior permission.
e33b89df 57 *
28f540f4
RM
58 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
59 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
61 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
62 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
63 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
64 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
65 * SOFTWARE.
b43b13ac
UD
66 */
67
68/*
69 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
70 *
71 * Permission to use, copy, modify, and distribute this software for any
72 * purpose with or without fee is hereby granted, provided that the above
73 * copyright notice and this permission notice appear in all copies.
74 *
75 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
76 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
77 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
78 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
79 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
80 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
81 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
82 * SOFTWARE.
28f540f4
RM
83 */
84
359653aa 85#include <stdint.h>
df21c858 86#include <sys/types.h>
28f540f4
RM
87#include <sys/param.h>
88#include <netinet/in.h>
89#include <arpa/nameser.h>
3d61b63c 90#include <netdb.h>
e14a2772 91#include <resolv/resolv-internal.h>
352f4ff9 92#include <resolv/resolv_context.h>
b43b13ac 93#include <string.h>
f890a59b 94#include <sys/time.h>
5ca4aaea 95#include <shlib-compat.h>
359653aa 96#include <random-bits.h>
e33b89df 97
28f540f4 98int
352f4ff9
FW
99__res_context_mkquery (struct resolv_context *ctx, int op, const char *dname,
100 int class, int type, const unsigned char *data,
101 unsigned char *buf, int buflen)
28f540f4 102{
74084feb
FW
103 HEADER *hp;
104 unsigned char *cp;
105 int n;
106 unsigned char *dnptrs[20], **dpp, **lastdnptr;
28f540f4 107
74084feb
FW
108 if (class < 0 || class > 65535 || type < 0 || type > 65535)
109 return -1;
fc82b0a2 110
74084feb
FW
111 /* Initialize header fields. */
112 if ((buf == NULL) || (buflen < HFIXEDSZ))
113 return -1;
114 memset (buf, 0, HFIXEDSZ);
115 hp = (HEADER *) buf;
116 /* We randomize the IDs every time. The old code just incremented
117 by one after the initial randomization which still predictable if
118 the application does multiple requests. */
359653aa 119 hp->id = random_bits ();
74084feb 120 hp->opcode = op;
352f4ff9 121 hp->rd = (ctx->resp->options & RES_RECURSE) != 0;
74084feb
FW
122 hp->rcode = NOERROR;
123 cp = buf + HFIXEDSZ;
124 buflen -= HFIXEDSZ;
125 dpp = dnptrs;
126 *dpp++ = buf;
127 *dpp++ = NULL;
128 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
bce16467 129
74084feb
FW
130 /* Perform opcode specific processing. */
131 switch (op)
132 {
133 case NS_NOTIFY_OP:
134 if ((buflen -= QFIXEDSZ + (data == NULL ? 0 : RRFIXEDSZ)) < 0)
135 return -1;
136 goto compose;
28f540f4 137
74084feb
FW
138 case QUERY:
139 if ((buflen -= QFIXEDSZ) < 0)
140 return -1;
141 compose:
142 n = ns_name_compress (dname, cp, buflen,
143 (const unsigned char **) dnptrs,
144 (const unsigned char **) lastdnptr);
145 if (n < 0)
146 return -1;
147 cp += n;
148 buflen -= n;
149 NS_PUT16 (type, cp);
150 NS_PUT16 (class, cp);
151 hp->qdcount = htons (1);
152 if (op == QUERY || data == NULL)
153 break;
154
155 /* Make an additional record for completion domain. */
156 n = ns_name_compress ((char *)data, cp, buflen,
157 (const unsigned char **) dnptrs,
158 (const unsigned char **) lastdnptr);
159 if (__glibc_unlikely (n < 0))
160 return -1;
161 cp += n;
162 buflen -= n;
163 NS_PUT16 (T_NULL, cp);
164 NS_PUT16 (class, cp);
165 NS_PUT32 (0, cp);
166 NS_PUT16 (0, cp);
167 hp->arcount = htons (1);
168 break;
169
170 default:
171 return -1;
172 }
173 return cp - buf;
28f540f4 174}
352f4ff9
FW
175
176/* Common part of res_nmkquery and res_mkquery. */
177static int
178context_mkquery_common (struct resolv_context *ctx,
179 int op, const char *dname, int class, int type,
180 const unsigned char *data,
181 unsigned char *buf, int buflen)
182{
183 if (ctx == NULL)
184 return -1;
185 int result = __res_context_mkquery
186 (ctx, op, dname, class, type, data, buf, buflen);
187 if (result >= 2)
188 memcpy (&ctx->resp->id, buf, 2);
189 __resolv_context_put (ctx);
190 return result;
191}
192
193/* Form all types of queries. Returns the size of the result or -1 on
194 error.
195
196 STATP points to an initialized resolver state. OP is the opcode of
197 the query. DNAME is the domain. CLASS and TYPE are the DNS query
198 class and type. DATA can be NULL; otherwise, it is a pointer to a
199 domain name which is included in the generated packet (if op ==
200 NS_NOTIFY_OP). BUF must point to the out buffer of BUFLEN bytes.
201
202 DATALEN and NEWRR_IN are currently ignored. */
203int
204res_nmkquery (res_state statp, int op, const char *dname,
205 int class, int type,
206 const unsigned char *data, int datalen,
207 const unsigned char *newrr_in,
208 unsigned char *buf, int buflen)
209{
210 return context_mkquery_common
211 (__resolv_context_get_override (statp),
212 op, dname, class, type, data, buf, buflen);
213}
2bbb7d5b 214
5ca4aaea
FW
215int
216res_mkquery (int op, const char *dname, int class, int type,
217 const unsigned char *data, int datalen,
218 const unsigned char *newrr_in,
219 unsigned char *buf, int buflen)
220{
352f4ff9
FW
221 return context_mkquery_common
222 (__resolv_context_get_preinit (),
223 op, dname, class, type, data, buf, buflen);
5ca4aaea
FW
224}
225
74084feb
FW
226/* Create an OPT resource record. Return the length of the final
227 packet, or -1 on error.
2bbb7d5b 228
74084feb
FW
229 STATP must be an initialized resolver state. N0 is the current
230 number of bytes of the packet (already written to BUF by the
231 aller). BUF is the packet being constructed. The array it
232 pointers to must be BUFLEN bytes long. ANSLEN is the advertised
233 EDNS buffer size (to be included in the OPT resource record). */
2bbb7d5b 234int
352f4ff9
FW
235__res_nopt (struct resolv_context *ctx,
236 int n0, unsigned char *buf, int buflen, int anslen)
2bbb7d5b 237{
74084feb
FW
238 uint16_t flags = 0;
239 HEADER *hp = (HEADER *) buf;
240 unsigned char *cp = buf + n0;
241 unsigned char *ep = buf + buflen;
2bbb7d5b 242
74084feb
FW
243 if ((ep - cp) < 1 + RRFIXEDSZ)
244 return -1;
2bbb7d5b 245
74084feb
FW
246 /* Add the root label. */
247 *cp++ = 0;
2bbb7d5b 248
74084feb 249 NS_PUT16 (T_OPT, cp); /* Record type. */
e14a2772 250
74084feb
FW
251 /* Lowering the advertised buffer size based on the actual
252 answer buffer size is desirable because the server will
253 minimize the reply to fit into the UDP packet (and A
254 non-minimal response might not fit the buffer).
e14a2772 255
74084feb
FW
256 The RESOLV_EDNS_BUFFER_SIZE limit could still result in TCP
257 fallback and a non-minimal response which has to be
258 hard-truncated in the stub resolver, but this is price to
259 pay for avoiding fragmentation. (This issue does not
260 affect the nss_dns functions because they use the stub
261 resolver in such a way that it allocates a properly sized
262 response buffer.) */
263 {
264 uint16_t buffer_size;
265 if (anslen < 512)
266 buffer_size = 512;
267 else if (anslen > RESOLV_EDNS_BUFFER_SIZE)
268 buffer_size = RESOLV_EDNS_BUFFER_SIZE;
269 else
270 buffer_size = anslen;
271 NS_PUT16 (buffer_size, cp);
272 }
e14a2772 273
74084feb
FW
274 *cp++ = NOERROR; /* Extended RCODE. */
275 *cp++ = 0; /* EDNS version. */
2d0671cb 276
352f4ff9 277 if (ctx->resp->options & RES_USE_DNSSEC)
74084feb 278 flags |= NS_OPT_DNSSEC_OK;
2d0671cb 279
74084feb
FW
280 NS_PUT16 (flags, cp);
281 NS_PUT16 (0, cp); /* RDATA length (no options are preent). */
282 hp->arcount = htons (ntohs (hp->arcount) + 1);
2bbb7d5b 283
74084feb 284 return cp - buf;
2bbb7d5b 285}
5ca4aaea
FW
286
287#if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
288# undef res_mkquery
289weak_alias (__res_mkquery, res_mkquery);
290#endif