11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*	$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $	*/
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 1985, 1993
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    The Regents of the University of California.  All rights reserved.
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3. All advertising materials mentioning features or use of this software
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    must display the following acknowledgement:
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 	This product includes software developed by the University of
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 	California, Berkeley and its contributors.
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 4. Neither the name of the University nor the names of its contributors
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    may be used to endorse or promote products derived from this software
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    without specific prior written permission.
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Portions Copyright (c) 1993 by Digital Equipment Corporation.
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Permission to use, copy, modify, and distribute this software for any
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * purpose with or without fee is hereby granted, provided that the above
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * copyright notice and this permission notice appear in all copies, and that
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the name of Digital Equipment Corporation not be used in advertising or
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * publicity pertaining to distribution of the document or software without
441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * specific, written prior permission.
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SOFTWARE.
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Permission to use, copy, modify, and distribute this software for any
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * purpose with or without fee is hereby granted, provided that the above
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * copyright notice and this permission notice appear in all copies.
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/cdefs.h>
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(LIBC_SCCS) && !defined(lint)
751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef notdef
761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic const char sccsid[] = "@(#)res_mkquery.c	8.1 (Berkeley) 6/4/93";
771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else
791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project__RCSID("$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $");
801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* LIBC_SCCS and not lint */
821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/types.h>
861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/param.h>
871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <netinet/in.h>
88569fb9887d00104509df79fa77d92e3cf9afe7dbCalin Juravle#include <arpa/nameser.h>
891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <netdb.h>
901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_CHANGES
911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "resolv_private.h"
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else
931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <resolv.h>
941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h>
961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h>
971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Options.  Leave them on. */
991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef DEBUG
1001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define DEBUG
1011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef lint
1041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define UNUSED(a)	(void)&a
1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else
1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define UNUSED(a)	a = a
1071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectextern const char *_res_opcodes[];
1101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Form all types of queries.
1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the size of the result or -1.
1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
1161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectres_nmkquery(res_state statp,
1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     int op,			/* opcode of query */
1181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     const char *dname,		/* domain name */
1191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     int class, int type,	/* class and type of query */
1201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     const u_char *data,	/* resource record data */
1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     int datalen,		/* length of data */
1221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     const u_char *newrr_in,	/* new rr for modify or append */
1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     u_char *buf,		/* buffer to put query */
1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	     int buflen)		/* size of buffer */
1251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
1261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	register HEADER *hp;
1271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	register u_char *cp, *ep;
1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	register int n;
1291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	u_char *dnptrs[20], **dpp, **lastdnptr;
1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	UNUSED(newrr_in);
1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef DEBUG
1341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (statp->options & RES_DEBUG)
1351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		printf(";; res_nmkquery(%s, %s, %s, %s)\n",
1361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		       _res_opcodes[op], dname, p_class(class), p_type(type));
1371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	/*
1391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 * Initialize header fields.
1401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 */
1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if ((buf == NULL) || (buflen < HFIXEDSZ))
1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		return (-1);
1431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	memset(buf, 0, HFIXEDSZ);
1441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp = (HEADER *)(void *)buf;
1451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp->id = htons(res_randomid());
1461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp->opcode = op;
1471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp->rd = (statp->options & RES_RECURSE) != 0U;
1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp->rcode = NOERROR;
1491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	cp = buf + HFIXEDSZ;
1501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	ep = buf + buflen;
1511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	dpp = dnptrs;
1521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	*dpp++ = buf;
1531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	*dpp++ = NULL;
1541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	/*
1561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 * perform opcode specific processing
1571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 */
1581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	switch (op) {
1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	case QUERY:	/*FALLTHROUGH*/
1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	case NS_NOTIFY_OP:
1611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (ep - cp < QFIXEDSZ)
1621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			return (-1);
1631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
1641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		    lastdnptr)) < 0)
1651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			return (-1);
1661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += n;
1671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(type, cp);
1681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
1691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(class, cp);
1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		hp->qdcount = htons(1);
1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (op == QUERY || data == NULL)
1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			break;
1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		/*
1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		 * Make an additional record for completion domain.
1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		 */
1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if ((ep - cp) < RRFIXEDSZ)
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			return (-1);
1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			    dnptrs, lastdnptr);
1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (n < 0)
1821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			return (-1);
1831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += n;
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(T_NULL, cp);
1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(class, cp);
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put32(0, cp);
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT32SZ;
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(0, cp);
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		hp->arcount = htons(1);
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		break;
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	case IQUERY:
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		/*
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		 * Initialize answer section
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		 */
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (ep - cp < 1 + RRFIXEDSZ + datalen)
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			return (-1);
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		*cp++ = '\0';	/* no domain name */
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(type, cp);
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(class, cp);
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put32(0, cp);
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT32SZ;
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		ns_put16(datalen, cp);
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		cp += INT16SZ;
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (datalen) {
2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			memcpy(cp, data, (size_t)datalen);
2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			cp += datalen;
2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		}
2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		hp->ancount = htons(1);
2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		break;
2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	default:
2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		return (-1);
2191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	return (cp - buf);
2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef RES_USE_EDNS0
2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef T_OPT
2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define T_OPT	41
2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectres_nopt(res_state statp,
2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 int n0,		/* current offset in buffer */
2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 u_char *buf,		/* buffer to put query */
2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 int buflen,		/* size of buffer */
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 int anslen)		/* UDP answer buffer size */
2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	register HEADER *hp;
2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	register u_char *cp, *ep;
2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	u_int16_t flags = 0;
2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef DEBUG
2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if ((statp->options & RES_DEBUG) != 0U)
2421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		printf(";; res_nopt()\n");
2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
2441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp = (HEADER *)(void *)buf;
2461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	cp = buf + n0;
2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	ep = buf + buflen;
2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if ((ep - cp) < 1 + RRFIXEDSZ)
2501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		return (-1);
2511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	*cp++ = 0;	/* "." */
2531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	ns_put16(T_OPT, cp);	/* TYPE */
2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	cp += INT16SZ;
2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	ns_put16(anslen & 0xffff, cp);	/* CLASS = UDP payload size */
2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	cp += INT16SZ;
2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	*cp++ = NOERROR;	/* extended RCODE */
2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	*cp++ = 0;		/* EDNS version */
2601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (statp->options & RES_USE_DNSSEC) {
2611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef DEBUG
2621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (statp->options & RES_DEBUG)
2631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			printf(";; res_opt()... ENDS0 DNSSEC\n");
2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		flags |= NS_OPT_DNSSEC_OK;
2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
2671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	ns_put16(flags, cp);
2681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	cp += INT16SZ;
2691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	ns_put16(0, cp);	/* RDLEN */
2701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	cp += INT16SZ;
2711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp->arcount = htons(ntohs(hp->arcount) + 1);
2721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	return (cp - buf);
2741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
276