other-licenses/android/res_mkquery.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/android/res_mkquery.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,289 @@
     1.4 +/*	$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $	*/
     1.5 +
     1.6 +/*
     1.7 + * Copyright (c) 2008  Android Open Source Project (query id randomization)
     1.8 + * Copyright (c) 1985, 1993
     1.9 + *    The Regents of the University of California.  All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + * 1. Redistributions of source code must retain the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer.
    1.16 + * 2. Redistributions in binary form must reproduce the above copyright
    1.17 + *    notice, this list of conditions and the following disclaimer in the
    1.18 + *    documentation and/or other materials provided with the distribution.
    1.19 + * 3. All advertising materials mentioning features or use of this software
    1.20 + *    must display the following acknowledgement:
    1.21 + * 	This product includes software developed by the University of
    1.22 + * 	California, Berkeley and its contributors.
    1.23 + * 4. Neither the name of the University nor the names of its contributors
    1.24 + *    may be used to endorse or promote products derived from this software
    1.25 + *    without specific prior written permission.
    1.26 + *
    1.27 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    1.28 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.29 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.30 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    1.31 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.32 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.33 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.34 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.35 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.36 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.37 + * SUCH DAMAGE.
    1.38 + */
    1.39 +
    1.40 +/*
    1.41 + * Portions Copyright (c) 1993 by Digital Equipment Corporation.
    1.42 + *
    1.43 + * Permission to use, copy, modify, and distribute this software for any
    1.44 + * purpose with or without fee is hereby granted, provided that the above
    1.45 + * copyright notice and this permission notice appear in all copies, and that
    1.46 + * the name of Digital Equipment Corporation not be used in advertising or
    1.47 + * publicity pertaining to distribution of the document or software without
    1.48 + * specific, written prior permission.
    1.49 + *
    1.50 + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
    1.51 + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
    1.52 + * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
    1.53 + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
    1.54 + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
    1.55 + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
    1.56 + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
    1.57 + * SOFTWARE.
    1.58 + */
    1.59 +
    1.60 +/*
    1.61 + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
    1.62 + * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
    1.63 + *
    1.64 + * Permission to use, copy, modify, and distribute this software for any
    1.65 + * purpose with or without fee is hereby granted, provided that the above
    1.66 + * copyright notice and this permission notice appear in all copies.
    1.67 + *
    1.68 + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
    1.69 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    1.70 + * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
    1.71 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    1.72 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    1.73 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
    1.74 + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    1.75 + */
    1.76 +
    1.77 +/*
    1.78 + * This version of this file is derived from Android 2.3 "Gingerbread",
    1.79 + * which contains uncredited changes by Android/Google developers.  It has
    1.80 + * been modified in 2011 for use in the Android build of Mozilla Firefox by
    1.81 + * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    1.82 + * and Steve Workman <sjhworkman@gmail.com>).
    1.83 + * These changes are offered under the same license as the original NetBSD
    1.84 + * file, whose copyright and license are unchanged above.
    1.85 + */
    1.86 +
    1.87 +#define ANDROID_CHANGES 1
    1.88 +#define MOZILLA_NECKO_EXCLUDE_CODE 1
    1.89 +
    1.90 +#include <sys/cdefs.h>
    1.91 +#if defined(LIBC_SCCS) && !defined(lint)
    1.92 +#ifdef notdef
    1.93 +static const char sccsid[] = "@(#)res_mkquery.c	8.1 (Berkeley) 6/4/93";
    1.94 +static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
    1.95 +#else
    1.96 +__RCSID("$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $");
    1.97 +#endif
    1.98 +#endif /* LIBC_SCCS and not lint */
    1.99 +
   1.100 +
   1.101 +
   1.102 +#include <sys/types.h>
   1.103 +#include <sys/param.h>
   1.104 +#include <netinet/in.h>
   1.105 +#include "arpa_nameser.h"
   1.106 +#include <netdb.h>
   1.107 +#ifdef ANDROID_CHANGES
   1.108 +#include "resolv_private.h"
   1.109 +#else
   1.110 +#include <resolv.h>
   1.111 +#endif
   1.112 +#include <stdio.h>
   1.113 +#include <string.h>
   1.114 +
   1.115 +/* Options.  Leave them on. */
   1.116 +#ifndef DEBUG
   1.117 +#define DEBUG
   1.118 +#endif
   1.119 +
   1.120 +#ifndef lint
   1.121 +#define UNUSED(a)	(void)&a
   1.122 +#else
   1.123 +#define UNUSED(a)	a = a
   1.124 +#endif
   1.125 +
   1.126 +extern const char *_res_opcodes[];
   1.127 +
   1.128 +/*
   1.129 + * Form all types of queries.
   1.130 + * Returns the size of the result or -1.
   1.131 + */
   1.132 +int
   1.133 +res_nmkquery(res_state statp,
   1.134 +	     int op,			/* opcode of query */
   1.135 +	     const char *dname,		/* domain name */
   1.136 +	     int class, int type,	/* class and type of query */
   1.137 +	     const u_char *data,	/* resource record data */
   1.138 +	     int datalen,		/* length of data */
   1.139 +	     const u_char *newrr_in,	/* new rr for modify or append */
   1.140 +	     u_char *buf,		/* buffer to put query */
   1.141 +	     int buflen)		/* size of buffer */
   1.142 +{
   1.143 +	register HEADER *hp;
   1.144 +	register u_char *cp, *ep;
   1.145 +	register int n;
   1.146 +	u_char *dnptrs[20], **dpp, **lastdnptr;
   1.147 +
   1.148 +	UNUSED(newrr_in);
   1.149 +
   1.150 +#ifdef DEBUG
   1.151 +	if (statp->options & RES_DEBUG)
   1.152 +		printf(";; res_nmkquery(%s, %s, %s, %s)\n",
   1.153 +		       _res_opcodes[op], dname, p_class(class), p_type(type));
   1.154 +#endif
   1.155 +	/*
   1.156 +	 * Initialize header fields.
   1.157 +	 */
   1.158 +	if ((buf == NULL) || (buflen < HFIXEDSZ))
   1.159 +		return (-1);
   1.160 +	memset(buf, 0, HFIXEDSZ);
   1.161 +	hp = (HEADER *)(void *)buf;
   1.162 +	hp->id = htons(res_randomid());
   1.163 +	hp->opcode = op;
   1.164 +	hp->rd = (statp->options & RES_RECURSE) != 0U;
   1.165 +	hp->rcode = NOERROR;
   1.166 +	cp = buf + HFIXEDSZ;
   1.167 +	ep = buf + buflen;
   1.168 +	dpp = dnptrs;
   1.169 +	*dpp++ = buf;
   1.170 +	*dpp++ = NULL;
   1.171 +	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
   1.172 +	/*
   1.173 +	 * perform opcode specific processing
   1.174 +	 */
   1.175 +	switch (op) {
   1.176 +	case QUERY:	/*FALLTHROUGH*/
   1.177 +	case NS_NOTIFY_OP:
   1.178 +		if (ep - cp < QFIXEDSZ)
   1.179 +			return (-1);
   1.180 +		if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
   1.181 +		    lastdnptr)) < 0)
   1.182 +			return (-1);
   1.183 +		cp += n;
   1.184 +		ns_put16(type, cp);
   1.185 +		cp += INT16SZ;
   1.186 +		ns_put16(class, cp);
   1.187 +		cp += INT16SZ;
   1.188 +		hp->qdcount = htons(1);
   1.189 +		if (op == QUERY || data == NULL)
   1.190 +			break;
   1.191 +		/*
   1.192 +		 * Make an additional record for completion domain.
   1.193 +		 */
   1.194 +		if ((ep - cp) < RRFIXEDSZ)
   1.195 +			return (-1);
   1.196 +		n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
   1.197 +			    dnptrs, lastdnptr);
   1.198 +		if (n < 0)
   1.199 +			return (-1);
   1.200 +		cp += n;
   1.201 +		ns_put16(T_NULL, cp);
   1.202 +		cp += INT16SZ;
   1.203 +		ns_put16(class, cp);
   1.204 +		cp += INT16SZ;
   1.205 +		ns_put32(0, cp);
   1.206 +		cp += INT32SZ;
   1.207 +		ns_put16(0, cp);
   1.208 +		cp += INT16SZ;
   1.209 +		hp->arcount = htons(1);
   1.210 +		break;
   1.211 +
   1.212 +	case IQUERY:
   1.213 +		/*
   1.214 +		 * Initialize answer section
   1.215 +		 */
   1.216 +		if (ep - cp < 1 + RRFIXEDSZ + datalen)
   1.217 +			return (-1);
   1.218 +		*cp++ = '\0';	/* no domain name */
   1.219 +		ns_put16(type, cp);
   1.220 +		cp += INT16SZ;
   1.221 +		ns_put16(class, cp);
   1.222 +		cp += INT16SZ;
   1.223 +		ns_put32(0, cp);
   1.224 +		cp += INT32SZ;
   1.225 +		ns_put16(datalen, cp);
   1.226 +		cp += INT16SZ;
   1.227 +		if (datalen) {
   1.228 +			memcpy(cp, data, (size_t)datalen);
   1.229 +			cp += datalen;
   1.230 +		}
   1.231 +		hp->ancount = htons(1);
   1.232 +		break;
   1.233 +
   1.234 +	default:
   1.235 +		return (-1);
   1.236 +	}
   1.237 +	return (cp - buf);
   1.238 +}
   1.239 +
   1.240 +#ifdef RES_USE_EDNS0
   1.241 +/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
   1.242 +#ifndef T_OPT
   1.243 +#define T_OPT	41
   1.244 +#endif
   1.245 +
   1.246 +int
   1.247 +res_nopt(res_state statp,
   1.248 +	 int n0,		/* current offset in buffer */
   1.249 +	 u_char *buf,		/* buffer to put query */
   1.250 +	 int buflen,		/* size of buffer */
   1.251 +	 int anslen)		/* UDP answer buffer size */
   1.252 +{
   1.253 +	register HEADER *hp;
   1.254 +	register u_char *cp, *ep;
   1.255 +	u_int16_t flags = 0;
   1.256 +
   1.257 +#ifdef DEBUG
   1.258 +	if ((statp->options & RES_DEBUG) != 0U)
   1.259 +		printf(";; res_nopt()\n");
   1.260 +#endif
   1.261 +
   1.262 +	hp = (HEADER *)(void *)buf;
   1.263 +	cp = buf + n0;
   1.264 +	ep = buf + buflen;
   1.265 +
   1.266 +	if ((ep - cp) < 1 + RRFIXEDSZ)
   1.267 +		return (-1);
   1.268 +
   1.269 +	*cp++ = 0;	/* "." */
   1.270 +
   1.271 +	ns_put16(T_OPT, cp);	/* TYPE */
   1.272 +	cp += INT16SZ;
   1.273 +	ns_put16(anslen & 0xffff, cp);	/* CLASS = UDP payload size */
   1.274 +	cp += INT16SZ;
   1.275 +	*cp++ = NOERROR;	/* extended RCODE */
   1.276 +	*cp++ = 0;		/* EDNS version */
   1.277 +	if (statp->options & RES_USE_DNSSEC) {
   1.278 +#ifdef DEBUG
   1.279 +		if (statp->options & RES_DEBUG)
   1.280 +			printf(";; res_opt()... ENDS0 DNSSEC\n");
   1.281 +#endif
   1.282 +		flags |= NS_OPT_DNSSEC_OK;
   1.283 +	}
   1.284 +	ns_put16(flags, cp);
   1.285 +	cp += INT16SZ;
   1.286 +	ns_put16(0, cp);	/* RDLEN */
   1.287 +	cp += INT16SZ;
   1.288 +	hp->arcount = htons(ntohs(hp->arcount) + 1);
   1.289 +
   1.290 +	return (cp - buf);
   1.291 +}
   1.292 +#endif

mercurial