]>
Commit | Line | Data |
---|---|---|
7ab27b76 FW |
1 | /* Creation of DNS query packets. |
2 | Copyright (C) 1995-2017 Free Software Foundation, Inc. | |
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 | ||
df21c858 | 85 | #include <sys/types.h> |
28f540f4 RM |
86 | #include <sys/param.h> |
87 | #include <netinet/in.h> | |
88 | #include <arpa/nameser.h> | |
3d61b63c | 89 | #include <netdb.h> |
e14a2772 | 90 | #include <resolv/resolv-internal.h> |
b43b13ac | 91 | #include <string.h> |
f890a59b | 92 | #include <sys/time.h> |
28f540f4 | 93 | |
ed3c7876 FW |
94 | #include <hp-timing.h> |
95 | #include <stdint.h> | |
96 | #if HP_TIMING_AVAIL | |
97 | # define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; } | |
98 | #endif | |
e33b89df | 99 | |
28f540f4 RM |
100 | /* |
101 | * Form all types of queries. | |
102 | * Returns the size of the result or -1. | |
103 | */ | |
104 | int | |
b43b13ac UD |
105 | res_nmkquery(res_state statp, |
106 | int op, /* opcode of query */ | |
107 | const char *dname, /* domain name */ | |
108 | int class, int type, /* class and type of query */ | |
109 | const u_char *data, /* resource record data */ | |
110 | int datalen, /* length of data */ | |
111 | const u_char *newrr_in, /* new rr for modify or append */ | |
112 | u_char *buf, /* buffer to put query */ | |
113 | int buflen) /* size of buffer */ | |
28f540f4 | 114 | { |
2e09a79a JM |
115 | HEADER *hp; |
116 | u_char *cp; | |
117 | int n; | |
28f540f4 RM |
118 | u_char *dnptrs[20], **dpp, **lastdnptr; |
119 | ||
fc82b0a2 FW |
120 | if (class < 0 || class > 65535 |
121 | || type < 0 || type > 65535) | |
122 | return -1; | |
123 | ||
28f540f4 RM |
124 | /* |
125 | * Initialize header fields. | |
126 | */ | |
127 | if ((buf == NULL) || (buflen < HFIXEDSZ)) | |
128 | return (-1); | |
b43b13ac | 129 | memset(buf, 0, HFIXEDSZ); |
28f540f4 | 130 | hp = (HEADER *) buf; |
e33b89df UD |
131 | /* We randomize the IDs every time. The old code just |
132 | incremented by one after the initial randomization which | |
133 | still predictable if the application does multiple | |
134 | requests. */ | |
e33b89df UD |
135 | int randombits; |
136 | do | |
137 | { | |
03eeff83 | 138 | #ifdef RANDOM_BITS |
e33b89df | 139 | RANDOM_BITS (randombits); |
03eeff83 | 140 | #else |
e33b89df UD |
141 | struct timeval tv; |
142 | __gettimeofday (&tv, NULL); | |
29e11320 | 143 | randombits = (tv.tv_sec << 8) ^ tv.tv_usec; |
03eeff83 | 144 | #endif |
e33b89df | 145 | } |
3feb8efa | 146 | while ((randombits & 0xffff) == 0); |
e33b89df | 147 | statp->id = (statp->id + randombits) & 0xffff; |
03eeff83 | 148 | hp->id = statp->id; |
28f540f4 | 149 | hp->opcode = op; |
b43b13ac | 150 | hp->rd = (statp->options & RES_RECURSE) != 0; |
28f540f4 RM |
151 | hp->rcode = NOERROR; |
152 | cp = buf + HFIXEDSZ; | |
153 | buflen -= HFIXEDSZ; | |
154 | dpp = dnptrs; | |
155 | *dpp++ = buf; | |
156 | *dpp++ = NULL; | |
157 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; | |
158 | /* | |
159 | * perform opcode specific processing | |
160 | */ | |
161 | switch (op) { | |
28f540f4 | 162 | case NS_NOTIFY_OP: |
bce16467 UD |
163 | if ((buflen -= QFIXEDSZ + (data == NULL ? 0 : RRFIXEDSZ)) < 0) |
164 | return (-1); | |
165 | goto compose; | |
166 | ||
167 | case QUERY: | |
28f540f4 RM |
168 | if ((buflen -= QFIXEDSZ) < 0) |
169 | return (-1); | |
bce16467 | 170 | compose: |
be1c55f2 UD |
171 | n = ns_name_compress(dname, cp, buflen, |
172 | (const u_char **) dnptrs, | |
173 | (const u_char **) lastdnptr); | |
174 | if (n < 0) | |
28f540f4 RM |
175 | return (-1); |
176 | cp += n; | |
177 | buflen -= n; | |
bce16467 UD |
178 | NS_PUT16 (type, cp); |
179 | NS_PUT16 (class, cp); | |
28f540f4 RM |
180 | hp->qdcount = htons(1); |
181 | if (op == QUERY || data == NULL) | |
182 | break; | |
183 | /* | |
184 | * Make an additional record for completion domain. | |
185 | */ | |
be1c55f2 UD |
186 | n = ns_name_compress((char *)data, cp, buflen, |
187 | (const u_char **) dnptrs, | |
188 | (const u_char **) lastdnptr); | |
a1ffb40e | 189 | if (__glibc_unlikely (n < 0)) |
28f540f4 RM |
190 | return (-1); |
191 | cp += n; | |
192 | buflen -= n; | |
bce16467 UD |
193 | NS_PUT16 (T_NULL, cp); |
194 | NS_PUT16 (class, cp); | |
195 | NS_PUT32 (0, cp); | |
196 | NS_PUT16 (0, cp); | |
28f540f4 RM |
197 | hp->arcount = htons(1); |
198 | break; | |
199 | ||
28f540f4 RM |
200 | default: |
201 | return (-1); | |
202 | } | |
203 | return (cp - buf); | |
204 | } | |
6f9d8e68 | 205 | libresolv_hidden_def (res_nmkquery) |
2bbb7d5b UD |
206 | |
207 | ||
208 | /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ | |
209 | #ifndef T_OPT | |
210 | #define T_OPT 41 | |
211 | #endif | |
212 | ||
213 | int | |
214 | __res_nopt(res_state statp, | |
215 | int n0, /* current offset in buffer */ | |
216 | u_char *buf, /* buffer to put query */ | |
217 | int buflen, /* size of buffer */ | |
218 | int anslen) /* UDP answer buffer size */ | |
219 | { | |
220 | u_int16_t flags = 0; | |
221 | ||
2bbb7d5b UD |
222 | HEADER *hp = (HEADER *) buf; |
223 | u_char *cp = buf + n0; | |
224 | u_char *ep = buf + buflen; | |
225 | ||
226 | if ((ep - cp) < 1 + RRFIXEDSZ) | |
227 | return -1; | |
228 | ||
229 | *cp++ = 0; /* "." */ | |
230 | ||
0073dd2d | 231 | NS_PUT16(T_OPT, cp); /* TYPE */ |
e14a2772 FW |
232 | |
233 | /* Lowering the advertised buffer size based on the actual | |
234 | answer buffer size is desirable because the server will | |
235 | minimize the reply to fit into the UDP packet (and A | |
236 | non-minimal response might not fit the buffer). | |
237 | ||
238 | The RESOLV_EDNS_BUFFER_SIZE limit could still result in TCP | |
239 | fallback and a non-minimal response which has to be | |
240 | hard-truncated in the stub resolver, but this is price to | |
241 | pay for avoiding fragmentation. (This issue does not | |
242 | affect the nss_dns functions because they use the stub | |
243 | resolver in such a way that it allocates a properly sized | |
244 | response buffer.) */ | |
245 | { | |
246 | uint16_t buffer_size; | |
247 | if (anslen < 512) | |
248 | buffer_size = 512; | |
249 | else if (anslen > RESOLV_EDNS_BUFFER_SIZE) | |
250 | buffer_size = RESOLV_EDNS_BUFFER_SIZE; | |
251 | else | |
252 | buffer_size = anslen; | |
253 | NS_PUT16 (buffer_size, cp); | |
254 | } | |
255 | ||
2bbb7d5b UD |
256 | *cp++ = NOERROR; /* extended RCODE */ |
257 | *cp++ = 0; /* EDNS version */ | |
2d0671cb AT |
258 | |
259 | if (statp->options & RES_USE_DNSSEC) { | |
2d0671cb AT |
260 | flags |= NS_OPT_DNSSEC_OK; |
261 | } | |
262 | ||
0073dd2d UD |
263 | NS_PUT16(flags, cp); |
264 | NS_PUT16(0, cp); /* RDLEN */ | |
2bbb7d5b UD |
265 | hp->arcount = htons(ntohs(hp->arcount) + 1); |
266 | ||
267 | return cp - buf; | |
268 | } |