1 /* $NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $ */
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
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
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
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)
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";
93 __RCSID("$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $");
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"
104 #ifdef ANDROID_CHANGES
105 #include "resolv_private.h"
112 /* Options. Leave them on. */
118 #define UNUSED(a) (void)&a
120 #define UNUSED(a) a = a
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 */
141 register u_char
*cp
, *ep
;
143 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
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
));
153 * Initialize header fields.
155 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
157 memset(buf
, 0, HFIXEDSZ
);
158 hp
= (HEADER
*)(void *)buf
;
159 hp
->id
= htons(res_randomid());
161 hp
->rd
= (statp
->options
& RES_RECURSE
) != 0U;
168 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
170 * perform opcode specific processing
173 case QUERY
: /*FALLTHROUGH*/
175 if (ep
- cp
< QFIXEDSZ
)
177 if ((n
= dn_comp(dname
, cp
, ep
- cp
- QFIXEDSZ
, dnptrs
,
185 hp
->qdcount
= htons(1);
186 if (op
== QUERY
|| data
== NULL
)
189 * Make an additional record for completion domain.
191 if ((ep
- cp
) < RRFIXEDSZ
)
193 n
= dn_comp((const char *)data
, cp
, ep
- cp
- RRFIXEDSZ
,
198 ns_put16(T_NULL
, cp
);
206 hp
->arcount
= htons(1);
211 * Initialize answer section
213 if (ep
- cp
< 1 + RRFIXEDSZ
+ datalen
)
215 *cp
++ = '\0'; /* no domain name */
222 ns_put16(datalen
, cp
);
225 memcpy(cp
, data
, (size_t)datalen
);
228 hp
->ancount
= htons(1);
238 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
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 */
251 register u_char
*cp
, *ep
;
255 if ((statp
->options
& RES_DEBUG
) != 0U)
256 printf(";; res_nopt()\n");
259 hp
= (HEADER
*)(void *)buf
;
263 if ((ep
- cp
) < 1 + RRFIXEDSZ
)
268 ns_put16(T_OPT
, cp
); /* TYPE */
270 ns_put16(anslen
& 0xffff, cp
); /* CLASS = UDP payload size */
272 *cp
++ = NOERROR
; /* extended RCODE */
273 *cp
++ = 0; /* EDNS version */
274 if (statp
->options
& RES_USE_DNSSEC
) {
276 if (statp
->options
& RES_DEBUG
)
277 printf(";; res_opt()... ENDS0 DNSSEC\n");
279 flags
|= NS_OPT_DNSSEC_OK
;
283 ns_put16(0, cp
); /* RDLEN */
285 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);