1 | /* |
2 | * Copyright (c) 1985, 1993 |
3 | * The Regents of the University of California. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * 4. Neither the name of the University nor the names of its contributors |
14 | * may be used to endorse or promote products derived from this software |
15 | * without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 | * SUCH DAMAGE. |
28 | */ |
29 | |
30 | /* |
31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
32 | * |
33 | * Permission to use, copy, modify, and distribute this software for any |
34 | * purpose with or without fee is hereby granted, provided that the above |
35 | * copyright notice and this permission notice appear in all copies, and that |
36 | * the name of Digital Equipment Corporation not be used in advertising or |
37 | * publicity pertaining to distribution of the document or software without |
38 | * specific, written prior permission. |
39 | * |
40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
43 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
44 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
45 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
46 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
47 | * SOFTWARE. |
48 | */ |
49 | |
50 | /* |
51 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
52 | * |
53 | * Permission to use, copy, modify, and distribute this software for any |
54 | * purpose with or without fee is hereby granted, provided that the above |
55 | * copyright notice and this permission notice appear in all copies. |
56 | * |
57 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
58 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
59 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
60 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
61 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
62 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
63 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
64 | * SOFTWARE. |
65 | */ |
66 | |
67 | #include <sys/types.h> |
68 | #include <sys/param.h> |
69 | #include <netinet/in.h> |
70 | #include <arpa/nameser.h> |
71 | #include <netdb.h> |
72 | #include <resolv/resolv-internal.h> |
73 | #include <stdio.h> |
74 | #include <string.h> |
75 | #include <sys/time.h> |
76 | |
77 | /* Options. Leave them on. */ |
78 | /* #define DEBUG */ |
79 | |
80 | #include <hp-timing.h> |
81 | #include <stdint.h> |
82 | #if HP_TIMING_AVAIL |
83 | # define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; } |
84 | #endif |
85 | |
86 | /* |
87 | * Form all types of queries. |
88 | * Returns the size of the result or -1. |
89 | */ |
90 | int |
91 | res_nmkquery(res_state statp, |
92 | int op, /* opcode of query */ |
93 | const char *dname, /* domain name */ |
94 | int class, int type, /* class and type of query */ |
95 | const u_char *data, /* resource record data */ |
96 | int datalen, /* length of data */ |
97 | const u_char *newrr_in, /* new rr for modify or append */ |
98 | u_char *buf, /* buffer to put query */ |
99 | int buflen) /* size of buffer */ |
100 | { |
101 | HEADER *hp; |
102 | u_char *cp; |
103 | int n; |
104 | u_char *dnptrs[20], **dpp, **lastdnptr; |
105 | |
106 | if (class < 0 || class > 65535 |
107 | || type < 0 || type > 65535) |
108 | return -1; |
109 | |
110 | #ifdef DEBUG |
111 | if (statp->options & RES_DEBUG) |
112 | printf(";; res_nmkquery(%s, %s, %s, %s)\n" , |
113 | _res_opcodes[op], dname, p_class(class), p_type(type)); |
114 | #endif |
115 | /* |
116 | * Initialize header fields. |
117 | */ |
118 | if ((buf == NULL) || (buflen < HFIXEDSZ)) |
119 | return (-1); |
120 | memset(buf, 0, HFIXEDSZ); |
121 | hp = (HEADER *) buf; |
122 | /* We randomize the IDs every time. The old code just |
123 | incremented by one after the initial randomization which |
124 | still predictable if the application does multiple |
125 | requests. */ |
126 | int randombits; |
127 | do |
128 | { |
129 | #ifdef RANDOM_BITS |
130 | RANDOM_BITS (randombits); |
131 | #else |
132 | struct timeval tv; |
133 | __gettimeofday (&tv, NULL); |
134 | randombits = (tv.tv_sec << 8) ^ tv.tv_usec; |
135 | #endif |
136 | } |
137 | while ((randombits & 0xffff) == 0); |
138 | statp->id = (statp->id + randombits) & 0xffff; |
139 | hp->id = statp->id; |
140 | hp->opcode = op; |
141 | hp->rd = (statp->options & RES_RECURSE) != 0; |
142 | hp->rcode = NOERROR; |
143 | cp = buf + HFIXEDSZ; |
144 | buflen -= HFIXEDSZ; |
145 | dpp = dnptrs; |
146 | *dpp++ = buf; |
147 | *dpp++ = NULL; |
148 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; |
149 | /* |
150 | * perform opcode specific processing |
151 | */ |
152 | switch (op) { |
153 | case NS_NOTIFY_OP: |
154 | if ((buflen -= QFIXEDSZ + (data == NULL ? 0 : RRFIXEDSZ)) < 0) |
155 | return (-1); |
156 | goto compose; |
157 | |
158 | case QUERY: |
159 | if ((buflen -= QFIXEDSZ) < 0) |
160 | return (-1); |
161 | compose: |
162 | n = ns_name_compress(dname, cp, buflen, |
163 | (const u_char **) dnptrs, |
164 | (const u_char **) lastdnptr); |
165 | if (n < 0) |
166 | return (-1); |
167 | cp += n; |
168 | buflen -= n; |
169 | NS_PUT16 (type, cp); |
170 | NS_PUT16 (class, cp); |
171 | hp->qdcount = htons(1); |
172 | if (op == QUERY || data == NULL) |
173 | break; |
174 | /* |
175 | * Make an additional record for completion domain. |
176 | */ |
177 | n = ns_name_compress((char *)data, cp, buflen, |
178 | (const u_char **) dnptrs, |
179 | (const u_char **) lastdnptr); |
180 | if (__glibc_unlikely (n < 0)) |
181 | return (-1); |
182 | cp += n; |
183 | buflen -= n; |
184 | NS_PUT16 (T_NULL, cp); |
185 | NS_PUT16 (class, cp); |
186 | NS_PUT32 (0, cp); |
187 | NS_PUT16 (0, cp); |
188 | hp->arcount = htons(1); |
189 | break; |
190 | |
191 | case IQUERY: |
192 | /* |
193 | * Initialize answer section |
194 | */ |
195 | if (__glibc_unlikely (buflen < 1 + RRFIXEDSZ + datalen)) |
196 | return (-1); |
197 | *cp++ = '\0'; /* no domain name */ |
198 | NS_PUT16 (type, cp); |
199 | NS_PUT16 (class, cp); |
200 | NS_PUT32 (0, cp); |
201 | NS_PUT16 (datalen, cp); |
202 | if (datalen) { |
203 | memcpy(cp, data, datalen); |
204 | cp += datalen; |
205 | } |
206 | hp->ancount = htons(1); |
207 | break; |
208 | |
209 | default: |
210 | return (-1); |
211 | } |
212 | return (cp - buf); |
213 | } |
214 | libresolv_hidden_def (res_nmkquery) |
215 | |
216 | |
217 | /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ |
218 | #ifndef T_OPT |
219 | #define T_OPT 41 |
220 | #endif |
221 | |
222 | int |
223 | __res_nopt(res_state statp, |
224 | int n0, /* current offset in buffer */ |
225 | u_char *buf, /* buffer to put query */ |
226 | int buflen, /* size of buffer */ |
227 | int anslen) /* UDP answer buffer size */ |
228 | { |
229 | u_int16_t flags = 0; |
230 | |
231 | #ifdef DEBUG |
232 | if ((statp->options & RES_DEBUG) != 0U) |
233 | printf(";; res_nopt()\n" ); |
234 | #endif |
235 | |
236 | HEADER *hp = (HEADER *) buf; |
237 | u_char *cp = buf + n0; |
238 | u_char *ep = buf + buflen; |
239 | |
240 | if ((ep - cp) < 1 + RRFIXEDSZ) |
241 | return -1; |
242 | |
243 | *cp++ = 0; /* "." */ |
244 | |
245 | NS_PUT16(T_OPT, cp); /* TYPE */ |
246 | |
247 | /* Lowering the advertised buffer size based on the actual |
248 | answer buffer size is desirable because the server will |
249 | minimize the reply to fit into the UDP packet (and A |
250 | non-minimal response might not fit the buffer). |
251 | |
252 | The RESOLV_EDNS_BUFFER_SIZE limit could still result in TCP |
253 | fallback and a non-minimal response which has to be |
254 | hard-truncated in the stub resolver, but this is price to |
255 | pay for avoiding fragmentation. (This issue does not |
256 | affect the nss_dns functions because they use the stub |
257 | resolver in such a way that it allocates a properly sized |
258 | response buffer.) */ |
259 | { |
260 | uint16_t buffer_size; |
261 | if (anslen < 512) |
262 | buffer_size = 512; |
263 | else if (anslen > RESOLV_EDNS_BUFFER_SIZE) |
264 | buffer_size = RESOLV_EDNS_BUFFER_SIZE; |
265 | else |
266 | buffer_size = anslen; |
267 | NS_PUT16 (buffer_size, cp); |
268 | } |
269 | |
270 | *cp++ = NOERROR; /* extended RCODE */ |
271 | *cp++ = 0; /* EDNS version */ |
272 | |
273 | if (statp->options & RES_USE_DNSSEC) { |
274 | #ifdef DEBUG |
275 | if (statp->options & RES_DEBUG) |
276 | printf(";; res_opt()... ENDS0 DNSSEC\n" ); |
277 | #endif |
278 | flags |= NS_OPT_DNSSEC_OK; |
279 | } |
280 | |
281 | NS_PUT16(flags, cp); |
282 | NS_PUT16(0, cp); /* RDLEN */ |
283 | hp->arcount = htons(ntohs(hp->arcount) + 1); |
284 | |
285 | return cp - buf; |
286 | } |
287 | |