1 /* Creation of DNS query packets.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
20 * Copyright (c) 1985, 1993
21 * The Regents of the University of California. All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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.
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.
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
49 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
69 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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
86 #include <sys/types.h>
87 #include <sys/param.h>
88 #include <netinet/in.h>
89 #include <arpa/nameser.h>
91 #include <resolv/resolv-internal.h>
92 #include <resolv/resolv_context.h>
95 #include <shlib-compat.h>
96 #include <random-bits.h>
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
)
106 unsigned char *dnptrs
[20], **dpp
, **lastdnptr
;
108 if (class < 0 || class > 65535 || type
< 0 || type
> 65535)
111 /* Initialize header fields. */
112 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
114 memset (buf
, 0, HFIXEDSZ
);
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. */
119 hp
->id
= random_bits ();
121 if (ctx
->resp
->options
& RES_TRUSTAD
)
123 hp
->rd
= (ctx
->resp
->options
& RES_RECURSE
) != 0;
130 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
132 /* Perform opcode specific processing. */
136 if ((buflen
-= QFIXEDSZ
+ (data
== NULL
? 0 : RRFIXEDSZ
)) < 0)
141 if ((buflen
-= QFIXEDSZ
) < 0)
144 n
= __ns_name_compress (dname
, cp
, buflen
,
145 (const unsigned char **) dnptrs
,
146 (const unsigned char **) lastdnptr
);
152 NS_PUT16 (class, cp
);
153 hp
->qdcount
= htons (1);
154 if (op
== QUERY
|| data
== NULL
)
157 /* Make an additional record for completion domain. */
158 n
= __ns_name_compress ((char *)data
, cp
, buflen
,
159 (const unsigned char **) dnptrs
,
160 (const unsigned char **) lastdnptr
);
161 if (__glibc_unlikely (n
< 0))
165 NS_PUT16 (T_NULL
, cp
);
166 NS_PUT16 (class, cp
);
169 hp
->arcount
= htons (1);
177 libc_hidden_def (__res_context_mkquery
)
179 /* Common part of res_nmkquery and res_mkquery. */
181 context_mkquery_common (struct resolv_context
*ctx
,
182 int op
, const char *dname
, int class, int type
,
183 const unsigned char *data
,
184 unsigned char *buf
, int buflen
)
188 int result
= __res_context_mkquery
189 (ctx
, op
, dname
, class, type
, data
, buf
, buflen
);
191 memcpy (&ctx
->resp
->id
, buf
, 2);
192 __resolv_context_put (ctx
);
196 /* Form all types of queries. Returns the size of the result or -1 on
199 STATP points to an initialized resolver state. OP is the opcode of
200 the query. DNAME is the domain. CLASS and TYPE are the DNS query
201 class and type. DATA can be NULL; otherwise, it is a pointer to a
202 domain name which is included in the generated packet (if op ==
203 NS_NOTIFY_OP). BUF must point to the out buffer of BUFLEN bytes.
205 DATALEN and NEWRR_IN are currently ignored. */
207 ___res_nmkquery (res_state statp
, int op
, const char *dname
,
209 const unsigned char *data
, int datalen
,
210 const unsigned char *newrr_in
,
211 unsigned char *buf
, int buflen
)
213 return context_mkquery_common
214 (__resolv_context_get_override (statp
),
215 op
, dname
, class, type
, data
, buf
, buflen
);
217 versioned_symbol (libc
, ___res_nmkquery
, res_nmkquery
, GLIBC_2_34
);
218 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
219 compat_symbol (libresolv
, ___res_nmkquery
, __res_nmkquery
, GLIBC_2_2
);
223 ___res_mkquery (int op
, const char *dname
, int class, int type
,
224 const unsigned char *data
, int datalen
,
225 const unsigned char *newrr_in
,
226 unsigned char *buf
, int buflen
)
228 return context_mkquery_common
229 (__resolv_context_get_preinit (),
230 op
, dname
, class, type
, data
, buf
, buflen
);
232 versioned_symbol (libc
, ___res_mkquery
, res_mkquery
, GLIBC_2_34
);
233 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
234 compat_symbol (libresolv
, ___res_mkquery
, res_mkquery
, GLIBC_2_0
);
236 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
237 compat_symbol (libresolv
, ___res_mkquery
, __res_mkquery
, GLIBC_2_2
);
240 /* Create an OPT resource record. Return the length of the final
241 packet, or -1 on error.
243 STATP must be an initialized resolver state. N0 is the current
244 number of bytes of the packet (already written to BUF by the
245 aller). BUF is the packet being constructed. The array it
246 pointers to must be BUFLEN bytes long. ANSLEN is the advertised
247 EDNS buffer size (to be included in the OPT resource record). */
249 __res_nopt (struct resolv_context
*ctx
,
250 int n0
, unsigned char *buf
, int buflen
, int anslen
)
253 HEADER
*hp
= (HEADER
*) buf
;
254 unsigned char *cp
= buf
+ n0
;
255 unsigned char *ep
= buf
+ buflen
;
257 if ((ep
- cp
) < 1 + RRFIXEDSZ
)
260 /* Add the root label. */
263 NS_PUT16 (T_OPT
, cp
); /* Record type. */
265 /* Lowering the advertised buffer size based on the actual
266 answer buffer size is desirable because the server will
267 minimize the reply to fit into the UDP packet (and A
268 non-minimal response might not fit the buffer).
270 The RESOLV_EDNS_BUFFER_SIZE limit could still result in TCP
271 fallback and a non-minimal response which has to be
272 hard-truncated in the stub resolver, but this is price to
273 pay for avoiding fragmentation. (This issue does not
274 affect the nss_dns functions because they use the stub
275 resolver in such a way that it allocates a properly sized
278 uint16_t buffer_size
;
281 else if (anslen
> RESOLV_EDNS_BUFFER_SIZE
)
282 buffer_size
= RESOLV_EDNS_BUFFER_SIZE
;
284 buffer_size
= anslen
;
285 NS_PUT16 (buffer_size
, cp
);
288 *cp
++ = NOERROR
; /* Extended RCODE. */
289 *cp
++ = 0; /* EDNS version. */
291 if (ctx
->resp
->options
& RES_USE_DNSSEC
)
292 flags
|= NS_OPT_DNSSEC_OK
;
294 NS_PUT16 (flags
, cp
);
295 NS_PUT16 (0, cp
); /* RDATA length (no options are preent). */
296 hp
->arcount
= htons (ntohs (hp
->arcount
) + 1);
300 libc_hidden_def (__res_nopt
)