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