other-licenses/android/res_data.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_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $ */
michael@0 2
michael@0 3 /*
michael@0 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
michael@0 5 * Copyright (c) 1995-1999 by Internet Software Consortium.
michael@0 6 *
michael@0 7 * Permission to use, copy, modify, and distribute this software for any
michael@0 8 * purpose with or without fee is hereby granted, provided that the above
michael@0 9 * copyright notice and this permission notice appear in all copies.
michael@0 10 *
michael@0 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
michael@0 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
michael@0 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
michael@0 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
michael@0 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
michael@0 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
michael@0 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
michael@0 18 */
michael@0 19
michael@0 20 /*
michael@0 21 * This version of this file is derived from Android 2.3 "Gingerbread",
michael@0 22 * which contains uncredited changes by Android/Google developers. It has
michael@0 23 * been modified in 2011 for use in the Android build of Mozilla Firefox by
michael@0 24 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
michael@0 25 * and Steve Workman <sjhworkman@gmail.com>).
michael@0 26 * These changes are offered under the same license as the original NetBSD
michael@0 27 * file, whose copyright and license are unchanged above.
michael@0 28 */
michael@0 29
michael@0 30 #define ANDROID_CHANGES 1
michael@0 31 #define MOZILLA_NECKO_EXCLUDE_CODE 1
michael@0 32
michael@0 33 #include <sys/cdefs.h>
michael@0 34 #if defined(LIBC_SCCS) && !defined(lint)
michael@0 35 #ifdef notdef
michael@0 36 static const char rcsid[] = "Id: res_data.c,v 1.1.206.2 2004/03/16 12:34:18 marka Exp";
michael@0 37 #else
michael@0 38 __RCSID("$NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $");
michael@0 39 #endif
michael@0 40 #endif /* LIBC_SCCS and not lint */
michael@0 41
michael@0 42
michael@0 43
michael@0 44 #include <sys/types.h>
michael@0 45 #include <sys/param.h>
michael@0 46 #include <sys/socket.h>
michael@0 47 #include <sys/time.h>
michael@0 48
michael@0 49 #include <netinet/in.h>
michael@0 50 #include <arpa/inet.h>
michael@0 51 #include "arpa_nameser.h"
michael@0 52
michael@0 53 #include <ctype.h>
michael@0 54 #include <netdb.h>
michael@0 55 #include "resolv_private.h"
michael@0 56 #include <stdio.h>
michael@0 57 #include <stdlib.h>
michael@0 58 #include <string.h>
michael@0 59 #include <unistd.h>
michael@0 60
michael@0 61
michael@0 62 const char * const _res_opcodes[] = {
michael@0 63 "QUERY",
michael@0 64 "IQUERY",
michael@0 65 "CQUERYM",
michael@0 66 "CQUERYU", /* experimental */
michael@0 67 "NOTIFY", /* experimental */
michael@0 68 "UPDATE",
michael@0 69 "6",
michael@0 70 "7",
michael@0 71 "8",
michael@0 72 "9",
michael@0 73 "10",
michael@0 74 "11",
michael@0 75 "12",
michael@0 76 "13",
michael@0 77 "ZONEINIT",
michael@0 78 "ZONEREF",
michael@0 79 };
michael@0 80
michael@0 81 #ifdef BIND_UPDATE
michael@0 82 const char * const _res_sectioncodes[] = {
michael@0 83 "ZONE",
michael@0 84 "PREREQUISITES",
michael@0 85 "UPDATE",
michael@0 86 "ADDITIONAL",
michael@0 87 };
michael@0 88 #endif
michael@0 89
michael@0 90 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 91 #ifndef __BIND_NOSTATIC
michael@0 92 extern struct __res_state _nres;
michael@0 93
michael@0 94 /* Proto. */
michael@0 95
michael@0 96 int res_ourserver_p(const res_state, const struct sockaddr *);
michael@0 97
michael@0 98 #ifdef ANDROID_CHANGES
michael@0 99 int res_need_init() {
michael@0 100 return ((_nres.options & RES_INIT) == 0U) || res_get_dns_changed();
michael@0 101 }
michael@0 102 #else
michael@0 103 #define res_need_init() ((_nres.options & RES_INIT) == 0U)
michael@0 104 #endif
michael@0 105
michael@0 106 int
michael@0 107 res_init(void) {
michael@0 108 int rv;
michael@0 109 extern int __res_vinit(res_state, int);
michael@0 110 #ifdef COMPAT__RES
michael@0 111 /*
michael@0 112 * Compatibility with program that were accessing _res directly
michael@0 113 * to set options. We keep another struct res that is the same
michael@0 114 * size as the original res structure, and then copy fields to
michael@0 115 * it so that we achieve the same initialization
michael@0 116 */
michael@0 117 extern void *__res_get_old_state(void);
michael@0 118 extern void __res_put_old_state(void *);
michael@0 119 res_state ores = __res_get_old_state();
michael@0 120
michael@0 121 if (ores->options != 0)
michael@0 122 _nres.options = ores->options;
michael@0 123 if (ores->retrans != 0)
michael@0 124 _nres.retrans = ores->retrans;
michael@0 125 if (ores->retry != 0)
michael@0 126 _nres.retry = ores->retry;
michael@0 127 #endif
michael@0 128
michael@0 129 /*
michael@0 130 * These three fields used to be statically initialized. This made
michael@0 131 * it hard to use this code in a shared library. It is necessary,
michael@0 132 * now that we're doing dynamic initialization here, that we preserve
michael@0 133 * the old semantics: if an application modifies one of these three
michael@0 134 * fields of _res before res_init() is called, res_init() will not
michael@0 135 * alter them. Of course, if an application is setting them to
michael@0 136 * _zero_ before calling res_init(), hoping to override what used
michael@0 137 * to be the static default, we can't detect it and unexpected results
michael@0 138 * will follow. Zero for any of these fields would make no sense,
michael@0 139 * so one can safely assume that the applications were already getting
michael@0 140 * unexpected results.
michael@0 141 *
michael@0 142 * _nres.options is tricky since some apps were known to diddle the bits
michael@0 143 * before res_init() was first called. We can't replicate that semantic
michael@0 144 * with dynamic initialization (they may have turned bits off that are
michael@0 145 * set in RES_DEFAULT). Our solution is to declare such applications
michael@0 146 * "broken". They could fool us by setting RES_INIT but none do (yet).
michael@0 147 */
michael@0 148 if (!_nres.retrans)
michael@0 149 _nres.retrans = RES_TIMEOUT;
michael@0 150 if (!_nres.retry)
michael@0 151 _nres.retry = 4;
michael@0 152 if (!(_nres.options & RES_INIT))
michael@0 153 _nres.options = RES_DEFAULT;
michael@0 154
michael@0 155 /*
michael@0 156 * This one used to initialize implicitly to zero, so unless the app
michael@0 157 * has set it to something in particular, we can randomize it now.
michael@0 158 */
michael@0 159 if (!_nres.id)
michael@0 160 _nres.id = res_randomid();
michael@0 161
michael@0 162 rv = __res_vinit(&_nres, 1);
michael@0 163 #ifdef COMPAT__RES
michael@0 164 __res_put_old_state(&_nres);
michael@0 165 #endif
michael@0 166 return rv;
michael@0 167 }
michael@0 168
michael@0 169 void
michael@0 170 p_query(const u_char *msg) {
michael@0 171 fp_query(msg, stdout);
michael@0 172 }
michael@0 173
michael@0 174 void
michael@0 175 fp_query(const u_char *msg, FILE *file) {
michael@0 176 fp_nquery(msg, PACKETSZ, file);
michael@0 177 }
michael@0 178
michael@0 179 void
michael@0 180 fp_nquery(const u_char *msg, int len, FILE *file) {
michael@0 181 if (res_need_init() && res_init() == -1)
michael@0 182 return;
michael@0 183
michael@0 184 res_pquery(&_nres, msg, len, file);
michael@0 185 }
michael@0 186
michael@0 187 int
michael@0 188 res_mkquery(int op, /* opcode of query */
michael@0 189 const char *dname, /* domain name */
michael@0 190 int class, int type, /* class and type of query */
michael@0 191 const u_char *data, /* resource record data */
michael@0 192 int datalen, /* length of data */
michael@0 193 const u_char *newrr_in, /* new rr for modify or append */
michael@0 194 u_char *buf, /* buffer to put query */
michael@0 195 int buflen) /* size of buffer */
michael@0 196 {
michael@0 197 if (res_need_init() && res_init() == -1) {
michael@0 198 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
michael@0 199 return (-1);
michael@0 200 }
michael@0 201 return (res_nmkquery(&_nres, op, dname, class, type,
michael@0 202 data, datalen,
michael@0 203 newrr_in, buf, buflen));
michael@0 204 }
michael@0 205
michael@0 206 #ifdef _LIBRESOLV
michael@0 207 int
michael@0 208 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
michael@0 209 if (res_need_init() && res_init() == -1) {
michael@0 210 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
michael@0 211 return (-1);
michael@0 212 }
michael@0 213
michael@0 214 return (res_nmkupdate(&_nres, rrecp_in, buf, buflen));
michael@0 215 }
michael@0 216 #endif
michael@0 217
michael@0 218 int
michael@0 219 res_query(const char *name, /* domain name */
michael@0 220 int class, int type, /* class and type of query */
michael@0 221 u_char *answer, /* buffer to put answer */
michael@0 222 int anslen) /* size of answer buffer */
michael@0 223 {
michael@0 224 if (res_need_init() && res_init() == -1) {
michael@0 225 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
michael@0 226 return (-1);
michael@0 227 }
michael@0 228 return (res_nquery(&_nres, name, class, type, answer, anslen));
michael@0 229 }
michael@0 230
michael@0 231 void
michael@0 232 res_send_setqhook(res_send_qhook hook) {
michael@0 233 _nres.qhook = hook;
michael@0 234 }
michael@0 235
michael@0 236 void
michael@0 237 res_send_setrhook(res_send_rhook hook) {
michael@0 238 _nres.rhook = hook;
michael@0 239 }
michael@0 240
michael@0 241 int
michael@0 242 res_isourserver(const struct sockaddr_in *inp) {
michael@0 243 return (res_ourserver_p(&_nres, (const struct sockaddr *)(const void *)inp));
michael@0 244 }
michael@0 245
michael@0 246 int
michael@0 247 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
michael@0 248 if (res_need_init() && res_init() == -1) {
michael@0 249 /* errno should have been set by res_init() in this case. */
michael@0 250 return (-1);
michael@0 251 }
michael@0 252
michael@0 253 return (res_nsend(&_nres, buf, buflen, ans, anssiz));
michael@0 254 }
michael@0 255
michael@0 256 #ifdef _LIBRESOLV
michael@0 257 int
michael@0 258 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
michael@0 259 u_char *ans, int anssiz)
michael@0 260 {
michael@0 261 if (res_need_init() && res_init() == -1) {
michael@0 262 /* errno should have been set by res_init() in this case. */
michael@0 263 return (-1);
michael@0 264 }
michael@0 265
michael@0 266 return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz));
michael@0 267 }
michael@0 268 #endif
michael@0 269
michael@0 270 void
michael@0 271 res_close(void) {
michael@0 272 res_nclose(&_nres);
michael@0 273 }
michael@0 274
michael@0 275 #ifdef _LIBRESOLV
michael@0 276 int
michael@0 277 res_update(ns_updrec *rrecp_in) {
michael@0 278 if (res_need_init() && res_init() == -1) {
michael@0 279 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
michael@0 280 return (-1);
michael@0 281 }
michael@0 282
michael@0 283 return (res_nupdate(&_nres, rrecp_in, NULL));
michael@0 284 }
michael@0 285 #endif
michael@0 286
michael@0 287 int
michael@0 288 res_search(const char *name, /* domain name */
michael@0 289 int class, int type, /* class and type of query */
michael@0 290 u_char *answer, /* buffer to put answer */
michael@0 291 int anslen) /* size of answer */
michael@0 292 {
michael@0 293 if (res_need_init() && res_init() == -1) {
michael@0 294 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
michael@0 295 return (-1);
michael@0 296 }
michael@0 297
michael@0 298 return (res_nsearch(&_nres, name, class, type, answer, anslen));
michael@0 299 }
michael@0 300
michael@0 301 int
michael@0 302 res_querydomain(const char *name,
michael@0 303 const char *domain,
michael@0 304 int class, int type, /* class and type of query */
michael@0 305 u_char *answer, /* buffer to put answer */
michael@0 306 int anslen) /* size of answer */
michael@0 307 {
michael@0 308 if (res_need_init() && res_init() == -1) {
michael@0 309 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
michael@0 310 return (-1);
michael@0 311 }
michael@0 312
michael@0 313 return (res_nquerydomain(&_nres, name, domain,
michael@0 314 class, type,
michael@0 315 answer, anslen));
michael@0 316 }
michael@0 317
michael@0 318 int
michael@0 319 res_opt(int a, u_char *b, int c, int d)
michael@0 320 {
michael@0 321 return res_nopt(&_nres, a, b, c, d);
michael@0 322 }
michael@0 323 #endif
michael@0 324 #endif
michael@0 325
michael@0 326 const char *
michael@0 327 hostalias(const char *name) {
michael@0 328 return NULL;
michael@0 329 }
michael@0 330
michael@0 331 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 332 #ifdef ultrix
michael@0 333 int
michael@0 334 local_hostname_length(const char *hostname) {
michael@0 335 int len_host, len_domain;
michael@0 336
michael@0 337 if (!*_nres.defdname)
michael@0 338 res_init();
michael@0 339 len_host = strlen(hostname);
michael@0 340 len_domain = strlen(_nres.defdname);
michael@0 341 if (len_host > len_domain &&
michael@0 342 !strcasecmp(hostname + len_host - len_domain, _nres.defdname) &&
michael@0 343 hostname[len_host - len_domain - 1] == '.')
michael@0 344 return (len_host - len_domain - 1);
michael@0 345 return (0);
michael@0 346 }
michael@0 347 #endif /*ultrix*/
michael@0 348 #endif

mercurial