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