Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* $NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 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) 1996,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 | #ifndef lint |
michael@0 | 35 | #ifdef notdef |
michael@0 | 36 | static const char rcsid[] = "Id: ns_parse.c,v 1.3.2.1.4.1 2004/03/09 08:33:44 marka Exp"; |
michael@0 | 37 | #else |
michael@0 | 38 | __RCSID("$NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $"); |
michael@0 | 39 | #endif |
michael@0 | 40 | #endif |
michael@0 | 41 | |
michael@0 | 42 | /* Import. */ |
michael@0 | 43 | |
michael@0 | 44 | #include <sys/types.h> |
michael@0 | 45 | |
michael@0 | 46 | #include <netinet/in.h> |
michael@0 | 47 | #include "arpa_nameser.h" |
michael@0 | 48 | |
michael@0 | 49 | #include <errno.h> |
michael@0 | 50 | #ifdef ANDROID_CHANGES |
michael@0 | 51 | #include "resolv_private.h" |
michael@0 | 52 | #else |
michael@0 | 53 | #include <resolv.h> |
michael@0 | 54 | #endif |
michael@0 | 55 | #include <string.h> |
michael@0 | 56 | |
michael@0 | 57 | /* Forward. */ |
michael@0 | 58 | |
michael@0 | 59 | static void setsection(ns_msg *msg, ns_sect sect); |
michael@0 | 60 | |
michael@0 | 61 | /* Macros. */ |
michael@0 | 62 | |
michael@0 | 63 | #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0) |
michael@0 | 64 | |
michael@0 | 65 | /* Public. */ |
michael@0 | 66 | |
michael@0 | 67 | /* These need to be in the same order as the nres.h:ns_flag enum. */ |
michael@0 | 68 | const struct _ns_flagdata _ns_flagdata[16] = { |
michael@0 | 69 | { 0x8000, 15 }, /* qr. */ |
michael@0 | 70 | { 0x7800, 11 }, /* opcode. */ |
michael@0 | 71 | { 0x0400, 10 }, /* aa. */ |
michael@0 | 72 | { 0x0200, 9 }, /* tc. */ |
michael@0 | 73 | { 0x0100, 8 }, /* rd. */ |
michael@0 | 74 | { 0x0080, 7 }, /* ra. */ |
michael@0 | 75 | { 0x0040, 6 }, /* z. */ |
michael@0 | 76 | { 0x0020, 5 }, /* ad. */ |
michael@0 | 77 | { 0x0010, 4 }, /* cd. */ |
michael@0 | 78 | { 0x000f, 0 }, /* rcode. */ |
michael@0 | 79 | { 0x0000, 0 }, /* expansion (1/6). */ |
michael@0 | 80 | { 0x0000, 0 }, /* expansion (2/6). */ |
michael@0 | 81 | { 0x0000, 0 }, /* expansion (3/6). */ |
michael@0 | 82 | { 0x0000, 0 }, /* expansion (4/6). */ |
michael@0 | 83 | { 0x0000, 0 }, /* expansion (5/6). */ |
michael@0 | 84 | { 0x0000, 0 }, /* expansion (6/6). */ |
michael@0 | 85 | }; |
michael@0 | 86 | |
michael@0 | 87 | int ns_msg_getflag(ns_msg handle, int flag) { |
michael@0 | 88 | return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | int |
michael@0 | 92 | ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { |
michael@0 | 93 | const u_char *optr = ptr; |
michael@0 | 94 | |
michael@0 | 95 | for (; count > 0; count--) { |
michael@0 | 96 | int b, rdlength; |
michael@0 | 97 | |
michael@0 | 98 | b = dn_skipname(ptr, eom); |
michael@0 | 99 | if (b < 0) |
michael@0 | 100 | RETERR(EMSGSIZE); |
michael@0 | 101 | ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; |
michael@0 | 102 | if (section != ns_s_qd) { |
michael@0 | 103 | if (ptr + NS_INT32SZ + NS_INT16SZ > eom) |
michael@0 | 104 | RETERR(EMSGSIZE); |
michael@0 | 105 | ptr += NS_INT32SZ/*TTL*/; |
michael@0 | 106 | NS_GET16(rdlength, ptr); |
michael@0 | 107 | ptr += rdlength/*RData*/; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | if (ptr > eom) |
michael@0 | 111 | RETERR(EMSGSIZE); |
michael@0 | 112 | return (ptr - optr); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | int |
michael@0 | 116 | ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { |
michael@0 | 117 | const u_char *eom = msg + msglen; |
michael@0 | 118 | int i; |
michael@0 | 119 | |
michael@0 | 120 | memset(handle, 0x5e, sizeof *handle); |
michael@0 | 121 | handle->_msg = msg; |
michael@0 | 122 | handle->_eom = eom; |
michael@0 | 123 | if (msg + NS_INT16SZ > eom) |
michael@0 | 124 | RETERR(EMSGSIZE); |
michael@0 | 125 | NS_GET16(handle->_id, msg); |
michael@0 | 126 | if (msg + NS_INT16SZ > eom) |
michael@0 | 127 | RETERR(EMSGSIZE); |
michael@0 | 128 | NS_GET16(handle->_flags, msg); |
michael@0 | 129 | for (i = 0; i < ns_s_max; i++) { |
michael@0 | 130 | if (msg + NS_INT16SZ > eom) |
michael@0 | 131 | RETERR(EMSGSIZE); |
michael@0 | 132 | NS_GET16(handle->_counts[i], msg); |
michael@0 | 133 | } |
michael@0 | 134 | for (i = 0; i < ns_s_max; i++) |
michael@0 | 135 | if (handle->_counts[i] == 0) |
michael@0 | 136 | handle->_sections[i] = NULL; |
michael@0 | 137 | else { |
michael@0 | 138 | int b = ns_skiprr(msg, eom, (ns_sect)i, |
michael@0 | 139 | handle->_counts[i]); |
michael@0 | 140 | |
michael@0 | 141 | if (b < 0) |
michael@0 | 142 | return (-1); |
michael@0 | 143 | handle->_sections[i] = msg; |
michael@0 | 144 | msg += b; |
michael@0 | 145 | } |
michael@0 | 146 | if (msg != eom) |
michael@0 | 147 | RETERR(EMSGSIZE); |
michael@0 | 148 | setsection(handle, ns_s_max); |
michael@0 | 149 | return (0); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | int |
michael@0 | 153 | ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { |
michael@0 | 154 | int b; |
michael@0 | 155 | |
michael@0 | 156 | /* Make section right. */ |
michael@0 | 157 | if ((unsigned)section >= (unsigned)ns_s_max) |
michael@0 | 158 | RETERR(ENODEV); |
michael@0 | 159 | if (section != handle->_sect) |
michael@0 | 160 | setsection(handle, section); |
michael@0 | 161 | |
michael@0 | 162 | /* Make rrnum right. */ |
michael@0 | 163 | if (rrnum == -1) |
michael@0 | 164 | rrnum = handle->_rrnum; |
michael@0 | 165 | if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) |
michael@0 | 166 | RETERR(ENODEV); |
michael@0 | 167 | if (rrnum < handle->_rrnum) |
michael@0 | 168 | setsection(handle, section); |
michael@0 | 169 | if (rrnum > handle->_rrnum) { |
michael@0 | 170 | b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, |
michael@0 | 171 | rrnum - handle->_rrnum); |
michael@0 | 172 | |
michael@0 | 173 | if (b < 0) |
michael@0 | 174 | return (-1); |
michael@0 | 175 | handle->_msg_ptr += b; |
michael@0 | 176 | handle->_rrnum = rrnum; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | /* Do the parse. */ |
michael@0 | 180 | b = dn_expand(handle->_msg, handle->_eom, |
michael@0 | 181 | handle->_msg_ptr, rr->name, NS_MAXDNAME); |
michael@0 | 182 | if (b < 0) |
michael@0 | 183 | return (-1); |
michael@0 | 184 | handle->_msg_ptr += b; |
michael@0 | 185 | if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) |
michael@0 | 186 | RETERR(EMSGSIZE); |
michael@0 | 187 | NS_GET16(rr->type, handle->_msg_ptr); |
michael@0 | 188 | NS_GET16(rr->rr_class, handle->_msg_ptr); |
michael@0 | 189 | if (section == ns_s_qd) { |
michael@0 | 190 | rr->ttl = 0; |
michael@0 | 191 | rr->rdlength = 0; |
michael@0 | 192 | rr->rdata = NULL; |
michael@0 | 193 | } else { |
michael@0 | 194 | if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) |
michael@0 | 195 | RETERR(EMSGSIZE); |
michael@0 | 196 | NS_GET32(rr->ttl, handle->_msg_ptr); |
michael@0 | 197 | NS_GET16(rr->rdlength, handle->_msg_ptr); |
michael@0 | 198 | if (handle->_msg_ptr + rr->rdlength > handle->_eom) |
michael@0 | 199 | RETERR(EMSGSIZE); |
michael@0 | 200 | rr->rdata = handle->_msg_ptr; |
michael@0 | 201 | handle->_msg_ptr += rr->rdlength; |
michael@0 | 202 | } |
michael@0 | 203 | if (++handle->_rrnum > handle->_counts[(int)section]) |
michael@0 | 204 | setsection(handle, (ns_sect)((int)section + 1)); |
michael@0 | 205 | |
michael@0 | 206 | /* All done. */ |
michael@0 | 207 | return (0); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | /* Private. */ |
michael@0 | 211 | |
michael@0 | 212 | static void |
michael@0 | 213 | setsection(ns_msg *msg, ns_sect sect) { |
michael@0 | 214 | msg->_sect = sect; |
michael@0 | 215 | if (sect == ns_s_max) { |
michael@0 | 216 | msg->_rrnum = -1; |
michael@0 | 217 | msg->_msg_ptr = NULL; |
michael@0 | 218 | } else { |
michael@0 | 219 | msg->_rrnum = 0; |
michael@0 | 220 | msg->_msg_ptr = msg->_sections[(int)sect]; |
michael@0 | 221 | } |
michael@0 | 222 | } |