Prevent an implicit int promotion in malloc/tst-alloc_buffer.c
[glibc.git] / resolv / res_mkquery.c
blob8279d15de4271c84b90aeb4f1778edfb1fe85fec
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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.
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
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>
77 /* Options. Leave them on. */
78 /* #define DEBUG */
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
87 * Form all types of queries.
88 * Returns the size of the result or -1.
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 */
101 HEADER *hp;
102 u_char *cp;
103 int n;
104 u_char *dnptrs[20], **dpp, **lastdnptr;
106 if (class < 0 || class > 65535
107 || type < 0 || type > 65535)
108 return -1;
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
116 * Initialize header fields.
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;
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
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];
150 * perform opcode specific processing
152 switch (op) {
153 case NS_NOTIFY_OP:
154 if ((buflen -= QFIXEDSZ + (data == NULL ? 0 : RRFIXEDSZ)) < 0)
155 return (-1);
156 goto compose;
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;
175 * Make an additional record for completion domain.
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;
191 default:
192 return (-1);
194 return (cp - buf);
196 libresolv_hidden_def (res_nmkquery)
199 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
200 #ifndef T_OPT
201 #define T_OPT 41
202 #endif
205 __res_nopt(res_state statp,
206 int n0, /* current offset in buffer */
207 u_char *buf, /* buffer to put query */
208 int buflen, /* size of buffer */
209 int anslen) /* UDP answer buffer size */
211 u_int16_t flags = 0;
213 #ifdef DEBUG
214 if ((statp->options & RES_DEBUG) != 0U)
215 printf(";; res_nopt()\n");
216 #endif
218 HEADER *hp = (HEADER *) buf;
219 u_char *cp = buf + n0;
220 u_char *ep = buf + buflen;
222 if ((ep - cp) < 1 + RRFIXEDSZ)
223 return -1;
225 *cp++ = 0; /* "." */
227 NS_PUT16(T_OPT, cp); /* TYPE */
229 /* Lowering the advertised buffer size based on the actual
230 answer buffer size is desirable because the server will
231 minimize the reply to fit into the UDP packet (and A
232 non-minimal response might not fit the buffer).
234 The RESOLV_EDNS_BUFFER_SIZE limit could still result in TCP
235 fallback and a non-minimal response which has to be
236 hard-truncated in the stub resolver, but this is price to
237 pay for avoiding fragmentation. (This issue does not
238 affect the nss_dns functions because they use the stub
239 resolver in such a way that it allocates a properly sized
240 response buffer.) */
242 uint16_t buffer_size;
243 if (anslen < 512)
244 buffer_size = 512;
245 else if (anslen > RESOLV_EDNS_BUFFER_SIZE)
246 buffer_size = RESOLV_EDNS_BUFFER_SIZE;
247 else
248 buffer_size = anslen;
249 NS_PUT16 (buffer_size, cp);
252 *cp++ = NOERROR; /* extended RCODE */
253 *cp++ = 0; /* EDNS version */
255 if (statp->options & RES_USE_DNSSEC) {
256 #ifdef DEBUG
257 if (statp->options & RES_DEBUG)
258 printf(";; res_opt()... ENDS0 DNSSEC\n");
259 #endif
260 flags |= NS_OPT_DNSSEC_OK;
263 NS_PUT16(flags, cp);
264 NS_PUT16(0, cp); /* RDLEN */
265 hp->arcount = htons(ntohs(hp->arcount) + 1);
267 return cp - buf;