Home | History | Annotate | Line # | Download | only in nameser
ns_parse.c revision 1.6.8.1
      1 /*	$NetBSD: ns_parse.c,v 1.6.8.1 2009/05/13 19:18:25 jym Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #include <sys/cdefs.h>
     21 #ifndef lint
     22 #ifdef notdef
     23 static const char rcsid[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp";
     24 #else
     25 __RCSID("$NetBSD: ns_parse.c,v 1.6.8.1 2009/05/13 19:18:25 jym Exp $");
     26 #endif
     27 #endif
     28 
     29 /* Import. */
     30 
     31 #include "port_before.h"
     32 
     33 #include <sys/types.h>
     34 
     35 #include <netinet/in.h>
     36 #include <arpa/nameser.h>
     37 
     38 #include <errno.h>
     39 #include <resolv.h>
     40 #include <string.h>
     41 
     42 #include "port_after.h"
     43 
     44 /* Forward. */
     45 
     46 static void	setsection(ns_msg *msg, ns_sect sect);
     47 
     48 /* Macros. */
     49 
     50 #if !defined(SOLARIS2) || defined(__COVERITY__)
     51 #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0)
     52 #else
     53 #define RETERR(err) \
     54 	do { errno = (err); if (errno == errno) return (-1); } while (0)
     55 #endif
     56 
     57 #define PARSE_FMT_PRESO 0	/* Parse using presentation-format names */
     58 #define PARSE_FMT_WIRE 1	/* Parse using network-format names */
     59 
     60 /* Public. */
     61 
     62 /* These need to be in the same order as the nres.h:ns_flag enum. */
     63 struct _ns_flagdata _ns_flagdata[16] = {
     64 	{ 0x8000, 15 },		/*%< qr. */
     65 	{ 0x7800, 11 },		/*%< opcode. */
     66 	{ 0x0400, 10 },		/*%< aa. */
     67 	{ 0x0200, 9 },		/*%< tc. */
     68 	{ 0x0100, 8 },		/*%< rd. */
     69 	{ 0x0080, 7 },		/*%< ra. */
     70 	{ 0x0040, 6 },		/*%< z. */
     71 	{ 0x0020, 5 },		/*%< ad. */
     72 	{ 0x0010, 4 },		/*%< cd. */
     73 	{ 0x000f, 0 },		/*%< rcode. */
     74 	{ 0x0000, 0 },		/*%< expansion (1/6). */
     75 	{ 0x0000, 0 },		/*%< expansion (2/6). */
     76 	{ 0x0000, 0 },		/*%< expansion (3/6). */
     77 	{ 0x0000, 0 },		/*%< expansion (4/6). */
     78 	{ 0x0000, 0 },		/*%< expansion (5/6). */
     79 	{ 0x0000, 0 },		/*%< expansion (6/6). */
     80 };
     81 
     82 int ns_msg_getflag(ns_msg handle, int flag) {
     83 	return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
     84 }
     85 
     86 int
     87 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
     88 	const u_char *optr = ptr;
     89 
     90 	for (; count > 0; count--) {
     91 		int b, rdlength;
     92 
     93 		b = dn_skipname(ptr, eom);
     94 		if (b < 0)
     95 			RETERR(EMSGSIZE);
     96 		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
     97 		if (section != ns_s_qd) {
     98 			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
     99 				RETERR(EMSGSIZE);
    100 			ptr += NS_INT32SZ/*TTL*/;
    101 			NS_GET16(rdlength, ptr);
    102 			ptr += rdlength/*RData*/;
    103 		}
    104 	}
    105 	if (ptr > eom)
    106 		RETERR(EMSGSIZE);
    107 	return (ptr - optr);
    108 }
    109 
    110 int
    111 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
    112 	const u_char *eom = msg + msglen;
    113 	int i;
    114 
    115 	handle->_msg = msg;
    116 	handle->_eom = eom;
    117 	if (msg + NS_INT16SZ > eom)
    118 		RETERR(EMSGSIZE);
    119 	NS_GET16(handle->_id, msg);
    120 	if (msg + NS_INT16SZ > eom)
    121 		RETERR(EMSGSIZE);
    122 	NS_GET16(handle->_flags, msg);
    123 	for (i = 0; i < ns_s_max; i++) {
    124 		if (msg + NS_INT16SZ > eom)
    125 			RETERR(EMSGSIZE);
    126 		NS_GET16(handle->_counts[i], msg);
    127 	}
    128 	for (i = 0; i < ns_s_max; i++)
    129 		if (handle->_counts[i] == 0)
    130 			handle->_sections[i] = NULL;
    131 		else {
    132 			int b = ns_skiprr(msg, eom, (ns_sect)i,
    133 					  handle->_counts[i]);
    134 
    135 			if (b < 0)
    136 				return (-1);
    137 			handle->_sections[i] = msg;
    138 			msg += b;
    139 		}
    140 	if (msg != eom)
    141 		RETERR(EMSGSIZE);
    142 	setsection(handle, ns_s_max);
    143 	return (0);
    144 }
    145 
    146 int
    147 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
    148 	int b;
    149 	int tmp;
    150 
    151 	/* Make section right. */
    152 	tmp = section;
    153 	if (tmp < 0 || section >= ns_s_max)
    154 		RETERR(ENODEV);
    155 	if (section != handle->_sect)
    156 		setsection(handle, section);
    157 
    158 	/* Make rrnum right. */
    159 	if (rrnum == -1)
    160 		rrnum = handle->_rrnum;
    161 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
    162 		RETERR(ENODEV);
    163 	if (rrnum < handle->_rrnum)
    164 		setsection(handle, section);
    165 	if (rrnum > handle->_rrnum) {
    166 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
    167 			      rrnum - handle->_rrnum);
    168 
    169 		if (b < 0)
    170 			return (-1);
    171 		handle->_msg_ptr += b;
    172 		handle->_rrnum = rrnum;
    173 	}
    174 
    175 	/* Do the parse. */
    176 	b = dn_expand(handle->_msg, handle->_eom,
    177 		      handle->_msg_ptr, rr->name, NS_MAXDNAME);
    178 	if (b < 0)
    179 		return (-1);
    180 	handle->_msg_ptr += b;
    181 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
    182 		RETERR(EMSGSIZE);
    183 	NS_GET16(rr->type, handle->_msg_ptr);
    184 	NS_GET16(rr->rr_class, handle->_msg_ptr);
    185 	if (section == ns_s_qd) {
    186 		rr->ttl = 0;
    187 		rr->rdlength = 0;
    188 		rr->rdata = NULL;
    189 	} else {
    190 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
    191 			RETERR(EMSGSIZE);
    192 		NS_GET32(rr->ttl, handle->_msg_ptr);
    193 		NS_GET16(rr->rdlength, handle->_msg_ptr);
    194 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
    195 			RETERR(EMSGSIZE);
    196 		rr->rdata = handle->_msg_ptr;
    197 		handle->_msg_ptr += rr->rdlength;
    198 	}
    199 	if (++handle->_rrnum > handle->_counts[(int)section])
    200 		setsection(handle, (ns_sect)((int)section + 1));
    201 
    202 	/* All done. */
    203 	return (0);
    204 }
    205 
    206 /*
    207  * This is identical to the above but uses network-format (uncompressed) names.
    208  */
    209 int
    210 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
    211 	int b;
    212 	int tmp;
    213 
    214 	/* Make section right. */
    215 	tmp = section;
    216 	if (tmp < 0 || section >= ns_s_max)
    217 		RETERR(ENODEV);
    218 	if (section != handle->_sect)
    219 		setsection(handle, section);
    220 
    221 	/* Make rrnum right. */
    222 	if (rrnum == -1)
    223 		rrnum = handle->_rrnum;
    224 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
    225 		RETERR(ENODEV);
    226 	if (rrnum < handle->_rrnum)
    227 		setsection(handle, section);
    228 	if (rrnum > handle->_rrnum) {
    229 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
    230 			      rrnum - handle->_rrnum);
    231 
    232 		if (b < 0)
    233 			return (-1);
    234 		handle->_msg_ptr += b;
    235 		handle->_rrnum = rrnum;
    236 	}
    237 
    238 	/* Do the parse. */
    239 	b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
    240 			    rr->nname, NS_MAXNNAME, &rr->nnamel);
    241 	if (b < 0)
    242 		return (-1);
    243 	handle->_msg_ptr += b;
    244 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
    245 		RETERR(EMSGSIZE);
    246 	NS_GET16(rr->type, handle->_msg_ptr);
    247 	NS_GET16(rr->rr_class, handle->_msg_ptr);
    248 	if (section == ns_s_qd) {
    249 		rr->ttl = 0;
    250 		rr->rdlength = 0;
    251 		rr->rdata = NULL;
    252 	} else {
    253 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
    254 			RETERR(EMSGSIZE);
    255 		NS_GET32(rr->ttl, handle->_msg_ptr);
    256 		NS_GET16(rr->rdlength, handle->_msg_ptr);
    257 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
    258 			RETERR(EMSGSIZE);
    259 		rr->rdata = handle->_msg_ptr;
    260 		handle->_msg_ptr += rr->rdlength;
    261 	}
    262 	if (++handle->_rrnum > handle->_counts[(int)section])
    263 		setsection(handle, (ns_sect)((int)section + 1));
    264 
    265 	/* All done. */
    266 	return (0);
    267 }
    268 
    269 /* Private. */
    270 
    271 static void
    272 setsection(ns_msg *msg, ns_sect sect) {
    273 	msg->_sect = sect;
    274 	if (sect == ns_s_max) {
    275 		msg->_rrnum = -1;
    276 		msg->_msg_ptr = NULL;
    277 	} else {
    278 		msg->_rrnum = 0;
    279 		msg->_msg_ptr = msg->_sections[(int)sect];
    280 	}
    281 }
    282 
    283 /*! \file */
    284