xref: /minix3/lib/libc/nameser/ns_parse.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras  * Copyright (c) 1996,1999 by Internet Software Consortium.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
82fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
92fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies.
102fe8fb19SBen Gras  *
112fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
122fe8fb19SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
132fe8fb19SBen Gras  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
142fe8fb19SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
152fe8fb19SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
162fe8fb19SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
172fe8fb19SBen Gras  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
182fe8fb19SBen Gras  */
192fe8fb19SBen Gras 
202fe8fb19SBen Gras #include <sys/cdefs.h>
212fe8fb19SBen Gras #ifndef lint
222fe8fb19SBen Gras #ifdef notdef
232fe8fb19SBen Gras static const char rcsid[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp";
242fe8fb19SBen Gras #else
25*f14fb602SLionel Sambuc __RCSID("$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $");
262fe8fb19SBen Gras #endif
272fe8fb19SBen Gras #endif
282fe8fb19SBen Gras 
292fe8fb19SBen Gras /* Import. */
302fe8fb19SBen Gras 
312fe8fb19SBen Gras #include "port_before.h"
322fe8fb19SBen Gras 
332fe8fb19SBen Gras #include <sys/types.h>
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <netinet/in.h>
362fe8fb19SBen Gras #include <arpa/nameser.h>
372fe8fb19SBen Gras 
38*f14fb602SLionel Sambuc #include <assert.h>
392fe8fb19SBen Gras #include <errno.h>
402fe8fb19SBen Gras #include <resolv.h>
412fe8fb19SBen Gras #include <string.h>
422fe8fb19SBen Gras 
432fe8fb19SBen Gras #include "port_after.h"
442fe8fb19SBen Gras 
452fe8fb19SBen Gras /* Forward. */
462fe8fb19SBen Gras 
472fe8fb19SBen Gras static void	setsection(ns_msg *msg, ns_sect sect);
482fe8fb19SBen Gras 
492fe8fb19SBen Gras /* Macros. */
502fe8fb19SBen Gras 
512fe8fb19SBen Gras #if !defined(SOLARIS2) || defined(__COVERITY__)
522fe8fb19SBen Gras #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0)
532fe8fb19SBen Gras #else
542fe8fb19SBen Gras #define RETERR(err) \
552fe8fb19SBen Gras 	do { errno = (err); if (errno == errno) return (-1); } while (0)
562fe8fb19SBen Gras #endif
572fe8fb19SBen Gras 
582fe8fb19SBen Gras #define PARSE_FMT_PRESO 0	/* Parse using presentation-format names */
592fe8fb19SBen Gras #define PARSE_FMT_WIRE 1	/* Parse using network-format names */
602fe8fb19SBen Gras 
612fe8fb19SBen Gras /* Public. */
622fe8fb19SBen Gras 
632fe8fb19SBen Gras /* These need to be in the same order as the nres.h:ns_flag enum. */
642fe8fb19SBen Gras struct _ns_flagdata _ns_flagdata[16] = {
652fe8fb19SBen Gras 	{ 0x8000, 15 },		/*%< qr. */
662fe8fb19SBen Gras 	{ 0x7800, 11 },		/*%< opcode. */
672fe8fb19SBen Gras 	{ 0x0400, 10 },		/*%< aa. */
682fe8fb19SBen Gras 	{ 0x0200, 9 },		/*%< tc. */
692fe8fb19SBen Gras 	{ 0x0100, 8 },		/*%< rd. */
702fe8fb19SBen Gras 	{ 0x0080, 7 },		/*%< ra. */
712fe8fb19SBen Gras 	{ 0x0040, 6 },		/*%< z. */
722fe8fb19SBen Gras 	{ 0x0020, 5 },		/*%< ad. */
732fe8fb19SBen Gras 	{ 0x0010, 4 },		/*%< cd. */
742fe8fb19SBen Gras 	{ 0x000f, 0 },		/*%< rcode. */
752fe8fb19SBen Gras 	{ 0x0000, 0 },		/*%< expansion (1/6). */
762fe8fb19SBen Gras 	{ 0x0000, 0 },		/*%< expansion (2/6). */
772fe8fb19SBen Gras 	{ 0x0000, 0 },		/*%< expansion (3/6). */
782fe8fb19SBen Gras 	{ 0x0000, 0 },		/*%< expansion (4/6). */
792fe8fb19SBen Gras 	{ 0x0000, 0 },		/*%< expansion (5/6). */
802fe8fb19SBen Gras 	{ 0x0000, 0 },		/*%< expansion (6/6). */
812fe8fb19SBen Gras };
822fe8fb19SBen Gras 
ns_msg_getflag(ns_msg handle,int flag)832fe8fb19SBen Gras int ns_msg_getflag(ns_msg handle, int flag) {
842fe8fb19SBen Gras 	return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
852fe8fb19SBen Gras }
862fe8fb19SBen Gras 
872fe8fb19SBen Gras int
ns_skiprr(const u_char * ptr,const u_char * eom,ns_sect section,int count)882fe8fb19SBen Gras ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
892fe8fb19SBen Gras 	const u_char *optr = ptr;
902fe8fb19SBen Gras 
912fe8fb19SBen Gras 	for (; count > 0; count--) {
922fe8fb19SBen Gras 		int b, rdlength;
932fe8fb19SBen Gras 
942fe8fb19SBen Gras 		b = dn_skipname(ptr, eom);
952fe8fb19SBen Gras 		if (b < 0)
962fe8fb19SBen Gras 			RETERR(EMSGSIZE);
972fe8fb19SBen Gras 		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
982fe8fb19SBen Gras 		if (section != ns_s_qd) {
992fe8fb19SBen Gras 			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
1002fe8fb19SBen Gras 				RETERR(EMSGSIZE);
1012fe8fb19SBen Gras 			ptr += NS_INT32SZ/*TTL*/;
1022fe8fb19SBen Gras 			NS_GET16(rdlength, ptr);
1032fe8fb19SBen Gras 			ptr += rdlength/*RData*/;
1042fe8fb19SBen Gras 		}
1052fe8fb19SBen Gras 	}
1062fe8fb19SBen Gras 	if (ptr > eom)
1072fe8fb19SBen Gras 		RETERR(EMSGSIZE);
108*f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, ptr - optr));
109*f14fb602SLionel Sambuc 	return (int)(ptr - optr);
1102fe8fb19SBen Gras }
1112fe8fb19SBen Gras 
1122fe8fb19SBen Gras int
ns_initparse(const u_char * msg,int msglen,ns_msg * handle)1132fe8fb19SBen Gras ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
1142fe8fb19SBen Gras 	const u_char *eom = msg + msglen;
1152fe8fb19SBen Gras 	int i;
1162fe8fb19SBen Gras 
1172fe8fb19SBen Gras 	handle->_msg = msg;
1182fe8fb19SBen Gras 	handle->_eom = eom;
1192fe8fb19SBen Gras 	if (msg + NS_INT16SZ > eom)
1202fe8fb19SBen Gras 		RETERR(EMSGSIZE);
1212fe8fb19SBen Gras 	NS_GET16(handle->_id, msg);
1222fe8fb19SBen Gras 	if (msg + NS_INT16SZ > eom)
1232fe8fb19SBen Gras 		RETERR(EMSGSIZE);
1242fe8fb19SBen Gras 	NS_GET16(handle->_flags, msg);
1252fe8fb19SBen Gras 	for (i = 0; i < ns_s_max; i++) {
1262fe8fb19SBen Gras 		if (msg + NS_INT16SZ > eom)
1272fe8fb19SBen Gras 			RETERR(EMSGSIZE);
1282fe8fb19SBen Gras 		NS_GET16(handle->_counts[i], msg);
1292fe8fb19SBen Gras 	}
1302fe8fb19SBen Gras 	for (i = 0; i < ns_s_max; i++)
1312fe8fb19SBen Gras 		if (handle->_counts[i] == 0)
1322fe8fb19SBen Gras 			handle->_sections[i] = NULL;
1332fe8fb19SBen Gras 		else {
1342fe8fb19SBen Gras 			int b = ns_skiprr(msg, eom, (ns_sect)i,
1352fe8fb19SBen Gras 					  handle->_counts[i]);
1362fe8fb19SBen Gras 
1372fe8fb19SBen Gras 			if (b < 0)
1382fe8fb19SBen Gras 				return (-1);
1392fe8fb19SBen Gras 			handle->_sections[i] = msg;
1402fe8fb19SBen Gras 			msg += b;
1412fe8fb19SBen Gras 		}
1422fe8fb19SBen Gras 	if (msg != eom)
1432fe8fb19SBen Gras 		RETERR(EMSGSIZE);
1442fe8fb19SBen Gras 	setsection(handle, ns_s_max);
1452fe8fb19SBen Gras 	return (0);
1462fe8fb19SBen Gras }
1472fe8fb19SBen Gras 
1482fe8fb19SBen Gras int
ns_parserr(ns_msg * handle,ns_sect section,int rrnum,ns_rr * rr)1492fe8fb19SBen Gras ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
1502fe8fb19SBen Gras 	int b;
1512fe8fb19SBen Gras 	int tmp;
1522fe8fb19SBen Gras 
1532fe8fb19SBen Gras 	/* Make section right. */
1542fe8fb19SBen Gras 	tmp = section;
1552fe8fb19SBen Gras 	if (tmp < 0 || section >= ns_s_max)
1562fe8fb19SBen Gras 		RETERR(ENODEV);
1572fe8fb19SBen Gras 	if (section != handle->_sect)
1582fe8fb19SBen Gras 		setsection(handle, section);
1592fe8fb19SBen Gras 
1602fe8fb19SBen Gras 	/* Make rrnum right. */
1612fe8fb19SBen Gras 	if (rrnum == -1)
1622fe8fb19SBen Gras 		rrnum = handle->_rrnum;
1632fe8fb19SBen Gras 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
1642fe8fb19SBen Gras 		RETERR(ENODEV);
1652fe8fb19SBen Gras 	if (rrnum < handle->_rrnum)
1662fe8fb19SBen Gras 		setsection(handle, section);
1672fe8fb19SBen Gras 	if (rrnum > handle->_rrnum) {
1682fe8fb19SBen Gras 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
1692fe8fb19SBen Gras 			      rrnum - handle->_rrnum);
1702fe8fb19SBen Gras 
1712fe8fb19SBen Gras 		if (b < 0)
1722fe8fb19SBen Gras 			return (-1);
1732fe8fb19SBen Gras 		handle->_msg_ptr += b;
1742fe8fb19SBen Gras 		handle->_rrnum = rrnum;
1752fe8fb19SBen Gras 	}
1762fe8fb19SBen Gras 
1772fe8fb19SBen Gras 	/* Do the parse. */
1782fe8fb19SBen Gras 	b = dn_expand(handle->_msg, handle->_eom,
1792fe8fb19SBen Gras 		      handle->_msg_ptr, rr->name, NS_MAXDNAME);
1802fe8fb19SBen Gras 	if (b < 0)
1812fe8fb19SBen Gras 		return (-1);
1822fe8fb19SBen Gras 	handle->_msg_ptr += b;
1832fe8fb19SBen Gras 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
1842fe8fb19SBen Gras 		RETERR(EMSGSIZE);
1852fe8fb19SBen Gras 	NS_GET16(rr->type, handle->_msg_ptr);
1862fe8fb19SBen Gras 	NS_GET16(rr->rr_class, handle->_msg_ptr);
1872fe8fb19SBen Gras 	if (section == ns_s_qd) {
1882fe8fb19SBen Gras 		rr->ttl = 0;
1892fe8fb19SBen Gras 		rr->rdlength = 0;
1902fe8fb19SBen Gras 		rr->rdata = NULL;
1912fe8fb19SBen Gras 	} else {
1922fe8fb19SBen Gras 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
1932fe8fb19SBen Gras 			RETERR(EMSGSIZE);
1942fe8fb19SBen Gras 		NS_GET32(rr->ttl, handle->_msg_ptr);
1952fe8fb19SBen Gras 		NS_GET16(rr->rdlength, handle->_msg_ptr);
1962fe8fb19SBen Gras 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
1972fe8fb19SBen Gras 			RETERR(EMSGSIZE);
1982fe8fb19SBen Gras 		rr->rdata = handle->_msg_ptr;
1992fe8fb19SBen Gras 		handle->_msg_ptr += rr->rdlength;
2002fe8fb19SBen Gras 	}
2012fe8fb19SBen Gras 	if (++handle->_rrnum > handle->_counts[(int)section])
2022fe8fb19SBen Gras 		setsection(handle, (ns_sect)((int)section + 1));
2032fe8fb19SBen Gras 
2042fe8fb19SBen Gras 	/* All done. */
2052fe8fb19SBen Gras 	return (0);
2062fe8fb19SBen Gras }
2072fe8fb19SBen Gras 
2082fe8fb19SBen Gras /*
2092fe8fb19SBen Gras  * This is identical to the above but uses network-format (uncompressed) names.
2102fe8fb19SBen Gras  */
2112fe8fb19SBen Gras int
ns_parserr2(ns_msg * handle,ns_sect section,int rrnum,ns_rr2 * rr)2122fe8fb19SBen Gras ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
2132fe8fb19SBen Gras 	int b;
2142fe8fb19SBen Gras 	int tmp;
2152fe8fb19SBen Gras 
2162fe8fb19SBen Gras 	/* Make section right. */
2172fe8fb19SBen Gras 	tmp = section;
2182fe8fb19SBen Gras 	if (tmp < 0 || section >= ns_s_max)
2192fe8fb19SBen Gras 		RETERR(ENODEV);
2202fe8fb19SBen Gras 	if (section != handle->_sect)
2212fe8fb19SBen Gras 		setsection(handle, section);
2222fe8fb19SBen Gras 
2232fe8fb19SBen Gras 	/* Make rrnum right. */
2242fe8fb19SBen Gras 	if (rrnum == -1)
2252fe8fb19SBen Gras 		rrnum = handle->_rrnum;
2262fe8fb19SBen Gras 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
2272fe8fb19SBen Gras 		RETERR(ENODEV);
2282fe8fb19SBen Gras 	if (rrnum < handle->_rrnum)
2292fe8fb19SBen Gras 		setsection(handle, section);
2302fe8fb19SBen Gras 	if (rrnum > handle->_rrnum) {
2312fe8fb19SBen Gras 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
2322fe8fb19SBen Gras 			      rrnum - handle->_rrnum);
2332fe8fb19SBen Gras 
2342fe8fb19SBen Gras 		if (b < 0)
2352fe8fb19SBen Gras 			return (-1);
2362fe8fb19SBen Gras 		handle->_msg_ptr += b;
2372fe8fb19SBen Gras 		handle->_rrnum = rrnum;
2382fe8fb19SBen Gras 	}
2392fe8fb19SBen Gras 
2402fe8fb19SBen Gras 	/* Do the parse. */
2412fe8fb19SBen Gras 	b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
2422fe8fb19SBen Gras 			    rr->nname, NS_MAXNNAME, &rr->nnamel);
2432fe8fb19SBen Gras 	if (b < 0)
2442fe8fb19SBen Gras 		return (-1);
2452fe8fb19SBen Gras 	handle->_msg_ptr += b;
2462fe8fb19SBen Gras 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
2472fe8fb19SBen Gras 		RETERR(EMSGSIZE);
2482fe8fb19SBen Gras 	NS_GET16(rr->type, handle->_msg_ptr);
2492fe8fb19SBen Gras 	NS_GET16(rr->rr_class, handle->_msg_ptr);
2502fe8fb19SBen Gras 	if (section == ns_s_qd) {
2512fe8fb19SBen Gras 		rr->ttl = 0;
2522fe8fb19SBen Gras 		rr->rdlength = 0;
2532fe8fb19SBen Gras 		rr->rdata = NULL;
2542fe8fb19SBen Gras 	} else {
2552fe8fb19SBen Gras 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
2562fe8fb19SBen Gras 			RETERR(EMSGSIZE);
2572fe8fb19SBen Gras 		NS_GET32(rr->ttl, handle->_msg_ptr);
2582fe8fb19SBen Gras 		NS_GET16(rr->rdlength, handle->_msg_ptr);
2592fe8fb19SBen Gras 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
2602fe8fb19SBen Gras 			RETERR(EMSGSIZE);
2612fe8fb19SBen Gras 		rr->rdata = handle->_msg_ptr;
2622fe8fb19SBen Gras 		handle->_msg_ptr += rr->rdlength;
2632fe8fb19SBen Gras 	}
2642fe8fb19SBen Gras 	if (++handle->_rrnum > handle->_counts[(int)section])
2652fe8fb19SBen Gras 		setsection(handle, (ns_sect)((int)section + 1));
2662fe8fb19SBen Gras 
2672fe8fb19SBen Gras 	/* All done. */
2682fe8fb19SBen Gras 	return (0);
2692fe8fb19SBen Gras }
2702fe8fb19SBen Gras 
2712fe8fb19SBen Gras /* Private. */
2722fe8fb19SBen Gras 
2732fe8fb19SBen Gras static void
setsection(ns_msg * msg,ns_sect sect)2742fe8fb19SBen Gras setsection(ns_msg *msg, ns_sect sect) {
2752fe8fb19SBen Gras 	msg->_sect = sect;
2762fe8fb19SBen Gras 	if (sect == ns_s_max) {
2772fe8fb19SBen Gras 		msg->_rrnum = -1;
2782fe8fb19SBen Gras 		msg->_msg_ptr = NULL;
2792fe8fb19SBen Gras 	} else {
2802fe8fb19SBen Gras 		msg->_rrnum = 0;
2812fe8fb19SBen Gras 		msg->_msg_ptr = msg->_sections[(int)sect];
2822fe8fb19SBen Gras 	}
2832fe8fb19SBen Gras }
2842fe8fb19SBen Gras 
2852fe8fb19SBen Gras /*! \file */
286