backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / other-licenses / android / res_mkquery.c
blob0a18cdf6be5bd423b4e2ca086ae964d0141ac8d7
1 /* $NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $ */
3 /*
4 * Copyright (c) 2008 Android Open Source Project (query id randomization)
5 * Copyright (c) 1985, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
38 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
40 * Permission to use, copy, modify, and distribute this software for any
41 * purpose with or without fee is hereby granted, provided that the above
42 * copyright notice and this permission notice appear in all copies, and that
43 * the name of Digital Equipment Corporation not be used in advertising or
44 * publicity pertaining to distribution of the document or software without
45 * specific, written prior permission.
47 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
48 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
50 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
51 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
52 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
53 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
54 * SOFTWARE.
58 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
59 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
61 * Permission to use, copy, modify, and distribute this software for any
62 * purpose with or without fee is hereby granted, provided that the above
63 * copyright notice and this permission notice appear in all copies.
65 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
66 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
67 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
68 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
69 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
70 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
71 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
75 * This version of this file is derived from Android 2.3 "Gingerbread",
76 * which contains uncredited changes by Android/Google developers. It has
77 * been modified in 2011 for use in the Android build of Mozilla Firefox by
78 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
79 * and Steve Workman <sjhworkman@gmail.com>).
80 * These changes are offered under the same license as the original NetBSD
81 * file, whose copyright and license are unchanged above.
84 #define ANDROID_CHANGES 1
85 #define MOZILLA_NECKO_EXCLUDE_CODE 1
87 #include <sys/cdefs.h>
88 #if defined(LIBC_SCCS) && !defined(lint)
89 #ifdef notdef
90 static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
91 static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
92 #else
93 __RCSID("$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $");
94 #endif
95 #endif /* LIBC_SCCS and not lint */
99 #include <sys/types.h>
100 #include <sys/param.h>
101 #include <netinet/in.h>
102 #include "arpa_nameser.h"
103 #include <netdb.h>
104 #ifdef ANDROID_CHANGES
105 #include "resolv_private.h"
106 #else
107 #include <resolv.h>
108 #endif
109 #include <stdio.h>
110 #include <string.h>
112 /* Options. Leave them on. */
113 #ifndef DEBUG
114 #define DEBUG
115 #endif
117 #ifndef lint
118 #define UNUSED(a) (void)&a
119 #else
120 #define UNUSED(a) a = a
121 #endif
123 extern const char *_res_opcodes[];
126 * Form all types of queries.
127 * Returns the size of the result or -1.
130 res_nmkquery(res_state statp,
131 int op, /* opcode of query */
132 const char *dname, /* domain name */
133 int class, int type, /* class and type of query */
134 const u_char *data, /* resource record data */
135 int datalen, /* length of data */
136 const u_char *newrr_in, /* new rr for modify or append */
137 u_char *buf, /* buffer to put query */
138 int buflen) /* size of buffer */
140 register HEADER *hp;
141 register u_char *cp, *ep;
142 register int n;
143 u_char *dnptrs[20], **dpp, **lastdnptr;
145 UNUSED(newrr_in);
147 #ifdef DEBUG
148 if (statp->options & RES_DEBUG)
149 printf(";; res_nmkquery(%s, %s, %s, %s)\n",
150 _res_opcodes[op], dname, p_class(class), p_type(type));
151 #endif
153 * Initialize header fields.
155 if ((buf == NULL) || (buflen < HFIXEDSZ))
156 return (-1);
157 memset(buf, 0, HFIXEDSZ);
158 hp = (HEADER *)(void *)buf;
159 hp->id = htons(res_randomid());
160 hp->opcode = op;
161 hp->rd = (statp->options & RES_RECURSE) != 0U;
162 hp->rcode = NOERROR;
163 cp = buf + HFIXEDSZ;
164 ep = buf + buflen;
165 dpp = dnptrs;
166 *dpp++ = buf;
167 *dpp++ = NULL;
168 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
170 * perform opcode specific processing
172 switch (op) {
173 case QUERY: /*FALLTHROUGH*/
174 case NS_NOTIFY_OP:
175 if (ep - cp < QFIXEDSZ)
176 return (-1);
177 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
178 lastdnptr)) < 0)
179 return (-1);
180 cp += n;
181 ns_put16(type, cp);
182 cp += INT16SZ;
183 ns_put16(class, cp);
184 cp += INT16SZ;
185 hp->qdcount = htons(1);
186 if (op == QUERY || data == NULL)
187 break;
189 * Make an additional record for completion domain.
191 if ((ep - cp) < RRFIXEDSZ)
192 return (-1);
193 n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
194 dnptrs, lastdnptr);
195 if (n < 0)
196 return (-1);
197 cp += n;
198 ns_put16(T_NULL, cp);
199 cp += INT16SZ;
200 ns_put16(class, cp);
201 cp += INT16SZ;
202 ns_put32(0, cp);
203 cp += INT32SZ;
204 ns_put16(0, cp);
205 cp += INT16SZ;
206 hp->arcount = htons(1);
207 break;
209 case IQUERY:
211 * Initialize answer section
213 if (ep - cp < 1 + RRFIXEDSZ + datalen)
214 return (-1);
215 *cp++ = '\0'; /* no domain name */
216 ns_put16(type, cp);
217 cp += INT16SZ;
218 ns_put16(class, cp);
219 cp += INT16SZ;
220 ns_put32(0, cp);
221 cp += INT32SZ;
222 ns_put16(datalen, cp);
223 cp += INT16SZ;
224 if (datalen) {
225 memcpy(cp, data, (size_t)datalen);
226 cp += datalen;
228 hp->ancount = htons(1);
229 break;
231 default:
232 return (-1);
234 return (cp - buf);
237 #ifdef RES_USE_EDNS0
238 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
239 #ifndef T_OPT
240 #define T_OPT 41
241 #endif
244 res_nopt(res_state statp,
245 int n0, /* current offset in buffer */
246 u_char *buf, /* buffer to put query */
247 int buflen, /* size of buffer */
248 int anslen) /* UDP answer buffer size */
250 register HEADER *hp;
251 register u_char *cp, *ep;
252 u_int16_t flags = 0;
254 #ifdef DEBUG
255 if ((statp->options & RES_DEBUG) != 0U)
256 printf(";; res_nopt()\n");
257 #endif
259 hp = (HEADER *)(void *)buf;
260 cp = buf + n0;
261 ep = buf + buflen;
263 if ((ep - cp) < 1 + RRFIXEDSZ)
264 return (-1);
266 *cp++ = 0; /* "." */
268 ns_put16(T_OPT, cp); /* TYPE */
269 cp += INT16SZ;
270 ns_put16(anslen & 0xffff, cp); /* CLASS = UDP payload size */
271 cp += INT16SZ;
272 *cp++ = NOERROR; /* extended RCODE */
273 *cp++ = 0; /* EDNS version */
274 if (statp->options & RES_USE_DNSSEC) {
275 #ifdef DEBUG
276 if (statp->options & RES_DEBUG)
277 printf(";; res_opt()... ENDS0 DNSSEC\n");
278 #endif
279 flags |= NS_OPT_DNSSEC_OK;
281 ns_put16(flags, cp);
282 cp += INT16SZ;
283 ns_put16(0, cp); /* RDLEN */
284 cp += INT16SZ;
285 hp->arcount = htons(ntohs(hp->arcount) + 1);
287 return (cp - buf);
289 #endif