other-licenses/android/res_mkquery.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial