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