michael@0: /* $NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") michael@0: * Copyright (c) 1996,1999 by Internet Software Consortium. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES michael@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF michael@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR michael@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN michael@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * This version of this file is derived from Android 2.3 "Gingerbread", michael@0: * which contains uncredited changes by Android/Google developers. It has michael@0: * been modified in 2011 for use in the Android build of Mozilla Firefox by michael@0: * Mozilla contributors (including Michael Edwards , michael@0: * and Steve Workman ). michael@0: * These changes are offered under the same license as the original NetBSD michael@0: * file, whose copyright and license are unchanged above. michael@0: */ michael@0: michael@0: #define ANDROID_CHANGES 1 michael@0: #define MOZILLA_NECKO_EXCLUDE_CODE 1 michael@0: michael@0: #include michael@0: #ifndef lint michael@0: #ifdef notdef michael@0: 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: #else michael@0: __RCSID("$NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $"); michael@0: #endif michael@0: #endif michael@0: michael@0: /* Import. */ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include "arpa_nameser.h" michael@0: michael@0: #include michael@0: #ifdef ANDROID_CHANGES michael@0: #include "resolv_private.h" michael@0: #else michael@0: #include michael@0: #endif michael@0: #include michael@0: michael@0: /* Forward. */ michael@0: michael@0: static void setsection(ns_msg *msg, ns_sect sect); michael@0: michael@0: /* Macros. */ michael@0: michael@0: #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0) michael@0: michael@0: /* Public. */ michael@0: michael@0: /* These need to be in the same order as the nres.h:ns_flag enum. */ michael@0: const struct _ns_flagdata _ns_flagdata[16] = { michael@0: { 0x8000, 15 }, /* qr. */ michael@0: { 0x7800, 11 }, /* opcode. */ michael@0: { 0x0400, 10 }, /* aa. */ michael@0: { 0x0200, 9 }, /* tc. */ michael@0: { 0x0100, 8 }, /* rd. */ michael@0: { 0x0080, 7 }, /* ra. */ michael@0: { 0x0040, 6 }, /* z. */ michael@0: { 0x0020, 5 }, /* ad. */ michael@0: { 0x0010, 4 }, /* cd. */ michael@0: { 0x000f, 0 }, /* rcode. */ michael@0: { 0x0000, 0 }, /* expansion (1/6). */ michael@0: { 0x0000, 0 }, /* expansion (2/6). */ michael@0: { 0x0000, 0 }, /* expansion (3/6). */ michael@0: { 0x0000, 0 }, /* expansion (4/6). */ michael@0: { 0x0000, 0 }, /* expansion (5/6). */ michael@0: { 0x0000, 0 }, /* expansion (6/6). */ michael@0: }; michael@0: michael@0: int ns_msg_getflag(ns_msg handle, int flag) { michael@0: return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); michael@0: } michael@0: michael@0: int michael@0: ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { michael@0: const u_char *optr = ptr; michael@0: michael@0: for (; count > 0; count--) { michael@0: int b, rdlength; michael@0: michael@0: b = dn_skipname(ptr, eom); michael@0: if (b < 0) michael@0: RETERR(EMSGSIZE); michael@0: ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; michael@0: if (section != ns_s_qd) { michael@0: if (ptr + NS_INT32SZ + NS_INT16SZ > eom) michael@0: RETERR(EMSGSIZE); michael@0: ptr += NS_INT32SZ/*TTL*/; michael@0: NS_GET16(rdlength, ptr); michael@0: ptr += rdlength/*RData*/; michael@0: } michael@0: } michael@0: if (ptr > eom) michael@0: RETERR(EMSGSIZE); michael@0: return (ptr - optr); michael@0: } michael@0: michael@0: int michael@0: ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { michael@0: const u_char *eom = msg + msglen; michael@0: int i; michael@0: michael@0: memset(handle, 0x5e, sizeof *handle); michael@0: handle->_msg = msg; michael@0: handle->_eom = eom; michael@0: if (msg + NS_INT16SZ > eom) michael@0: RETERR(EMSGSIZE); michael@0: NS_GET16(handle->_id, msg); michael@0: if (msg + NS_INT16SZ > eom) michael@0: RETERR(EMSGSIZE); michael@0: NS_GET16(handle->_flags, msg); michael@0: for (i = 0; i < ns_s_max; i++) { michael@0: if (msg + NS_INT16SZ > eom) michael@0: RETERR(EMSGSIZE); michael@0: NS_GET16(handle->_counts[i], msg); michael@0: } michael@0: for (i = 0; i < ns_s_max; i++) michael@0: if (handle->_counts[i] == 0) michael@0: handle->_sections[i] = NULL; michael@0: else { michael@0: int b = ns_skiprr(msg, eom, (ns_sect)i, michael@0: handle->_counts[i]); michael@0: michael@0: if (b < 0) michael@0: return (-1); michael@0: handle->_sections[i] = msg; michael@0: msg += b; michael@0: } michael@0: if (msg != eom) michael@0: RETERR(EMSGSIZE); michael@0: setsection(handle, ns_s_max); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { michael@0: int b; michael@0: michael@0: /* Make section right. */ michael@0: if ((unsigned)section >= (unsigned)ns_s_max) michael@0: RETERR(ENODEV); michael@0: if (section != handle->_sect) michael@0: setsection(handle, section); michael@0: michael@0: /* Make rrnum right. */ michael@0: if (rrnum == -1) michael@0: rrnum = handle->_rrnum; michael@0: if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) michael@0: RETERR(ENODEV); michael@0: if (rrnum < handle->_rrnum) michael@0: setsection(handle, section); michael@0: if (rrnum > handle->_rrnum) { michael@0: b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, michael@0: rrnum - handle->_rrnum); michael@0: michael@0: if (b < 0) michael@0: return (-1); michael@0: handle->_msg_ptr += b; michael@0: handle->_rrnum = rrnum; michael@0: } michael@0: michael@0: /* Do the parse. */ michael@0: b = dn_expand(handle->_msg, handle->_eom, michael@0: handle->_msg_ptr, rr->name, NS_MAXDNAME); michael@0: if (b < 0) michael@0: return (-1); michael@0: handle->_msg_ptr += b; michael@0: if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) michael@0: RETERR(EMSGSIZE); michael@0: NS_GET16(rr->type, handle->_msg_ptr); michael@0: NS_GET16(rr->rr_class, handle->_msg_ptr); michael@0: if (section == ns_s_qd) { michael@0: rr->ttl = 0; michael@0: rr->rdlength = 0; michael@0: rr->rdata = NULL; michael@0: } else { michael@0: if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) michael@0: RETERR(EMSGSIZE); michael@0: NS_GET32(rr->ttl, handle->_msg_ptr); michael@0: NS_GET16(rr->rdlength, handle->_msg_ptr); michael@0: if (handle->_msg_ptr + rr->rdlength > handle->_eom) michael@0: RETERR(EMSGSIZE); michael@0: rr->rdata = handle->_msg_ptr; michael@0: handle->_msg_ptr += rr->rdlength; michael@0: } michael@0: if (++handle->_rrnum > handle->_counts[(int)section]) michael@0: setsection(handle, (ns_sect)((int)section + 1)); michael@0: michael@0: /* All done. */ michael@0: return (0); michael@0: } michael@0: michael@0: /* Private. */ michael@0: michael@0: static void michael@0: setsection(ns_msg *msg, ns_sect sect) { michael@0: msg->_sect = sect; michael@0: if (sect == ns_s_max) { michael@0: msg->_rrnum = -1; michael@0: msg->_msg_ptr = NULL; michael@0: } else { michael@0: msg->_rrnum = 0; michael@0: msg->_msg_ptr = msg->_sections[(int)sect]; michael@0: } michael@0: }