Blame resolv/ns_parse.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
Packit 6c4009
 * Copyright (c) 1996,1999 by Internet Software Consortium.
Packit 6c4009
 *
Packit 6c4009
 * Permission to use, copy, modify, and distribute this software for any
Packit 6c4009
 * purpose with or without fee is hereby granted, provided that the above
Packit 6c4009
 * copyright notice and this permission notice appear in all copies.
Packit 6c4009
 *
Packit 6c4009
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
Packit 6c4009
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit 6c4009
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
Packit 6c4009
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit 6c4009
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit 6c4009
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
Packit 6c4009
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/* Import. */
Packit 6c4009
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <arpa/nameser.h>
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <resolv.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* Forward. */
Packit 6c4009
Packit 6c4009
static void	setsection(ns_msg *msg, ns_sect sect);
Packit 6c4009
Packit 6c4009
/* Macros. */
Packit 6c4009
Packit 6c4009
#define RETERR(err) do { __set_errno (err); return (-1); } while (0)
Packit 6c4009
Packit 6c4009
/* Public. */
Packit 6c4009
Packit 6c4009
/* These need to be in the same order as the nres.h:ns_flag enum. */
Packit 6c4009
const struct _ns_flagdata _ns_flagdata[16] = {
Packit 6c4009
	{ 0x8000, 15 },		/*%< qr. */
Packit 6c4009
	{ 0x7800, 11 },		/*%< opcode. */
Packit 6c4009
	{ 0x0400, 10 },		/*%< aa. */
Packit 6c4009
	{ 0x0200, 9 },		/*%< tc. */
Packit 6c4009
	{ 0x0100, 8 },		/*%< rd. */
Packit 6c4009
	{ 0x0080, 7 },		/*%< ra. */
Packit 6c4009
	{ 0x0040, 6 },		/*%< z. */
Packit 6c4009
	{ 0x0020, 5 },		/*%< ad. */
Packit 6c4009
	{ 0x0010, 4 },		/*%< cd. */
Packit 6c4009
	{ 0x000f, 0 },		/*%< rcode. */
Packit 6c4009
	{ 0x0000, 0 },		/*%< expansion (1/6). */
Packit 6c4009
	{ 0x0000, 0 },		/*%< expansion (2/6). */
Packit 6c4009
	{ 0x0000, 0 },		/*%< expansion (3/6). */
Packit 6c4009
	{ 0x0000, 0 },		/*%< expansion (4/6). */
Packit 6c4009
	{ 0x0000, 0 },		/*%< expansion (5/6). */
Packit 6c4009
	{ 0x0000, 0 },		/*%< expansion (6/6). */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
#undef ns_msg_getflag
Packit 6c4009
int ns_msg_getflag(ns_msg handle, int flag) {
Packit 6c4009
	return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
Packit 6c4009
	const u_char *optr = ptr;
Packit 6c4009
Packit 6c4009
	for ((void)NULL; count > 0; count--) {
Packit 6c4009
		int b, rdlength;
Packit 6c4009
Packit 6c4009
		b = dn_skipname(ptr, eom);
Packit 6c4009
		if (b < 0)
Packit 6c4009
			RETERR(EMSGSIZE);
Packit 6c4009
		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
Packit 6c4009
		if (section != ns_s_qd) {
Packit 6c4009
			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
Packit 6c4009
				RETERR(EMSGSIZE);
Packit 6c4009
			ptr += NS_INT32SZ/*TTL*/;
Packit 6c4009
			NS_GET16(rdlength, ptr);
Packit 6c4009
			ptr += rdlength/*RData*/;
Packit 6c4009
		}
Packit 6c4009
	}
Packit 6c4009
	if (ptr > eom)
Packit 6c4009
		RETERR(EMSGSIZE);
Packit 6c4009
	return (ptr - optr);
Packit 6c4009
}
Packit 6c4009
libresolv_hidden_def (ns_skiprr)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
Packit 6c4009
	const u_char *eom = msg + msglen;
Packit 6c4009
	int i;
Packit 6c4009
Packit 6c4009
	memset(handle, 0x5e, sizeof *handle);
Packit 6c4009
	handle->_msg = msg;
Packit 6c4009
	handle->_eom = eom;
Packit 6c4009
	if (msg + NS_INT16SZ > eom)
Packit 6c4009
		RETERR(EMSGSIZE);
Packit 6c4009
	NS_GET16(handle->_id, msg);
Packit 6c4009
	if (msg + NS_INT16SZ > eom)
Packit 6c4009
		RETERR(EMSGSIZE);
Packit 6c4009
	NS_GET16(handle->_flags, msg);
Packit 6c4009
	for (i = 0; i < ns_s_max; i++) {
Packit 6c4009
		if (msg + NS_INT16SZ > eom)
Packit 6c4009
			RETERR(EMSGSIZE);
Packit 6c4009
		NS_GET16(handle->_counts[i], msg);
Packit 6c4009
	}
Packit 6c4009
	for (i = 0; i < ns_s_max; i++)
Packit 6c4009
		if (handle->_counts[i] == 0)
Packit 6c4009
			handle->_sections[i] = NULL;
Packit 6c4009
		else {
Packit 6c4009
			int b = ns_skiprr(msg, eom, (ns_sect)i,
Packit 6c4009
					  handle->_counts[i]);
Packit 6c4009
Packit 6c4009
			if (b < 0)
Packit 6c4009
				return (-1);
Packit 6c4009
			handle->_sections[i] = msg;
Packit 6c4009
			msg += b;
Packit 6c4009
		}
Packit 6c4009
	if (msg != eom)
Packit 6c4009
		RETERR(EMSGSIZE);
Packit 6c4009
	setsection(handle, ns_s_max);
Packit 6c4009
	return (0);
Packit 6c4009
}
Packit 6c4009
libresolv_hidden_def (ns_initparse)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
Packit 6c4009
	int b;
Packit 6c4009
	int tmp;
Packit 6c4009
Packit 6c4009
	/* Make section right. */
Packit 6c4009
	tmp = section;
Packit 6c4009
	if (tmp < 0 || section >= ns_s_max)
Packit 6c4009
		RETERR(ENODEV);
Packit 6c4009
	if (section != handle->_sect)
Packit 6c4009
		setsection(handle, section);
Packit 6c4009
Packit 6c4009
	/* Make rrnum right. */
Packit 6c4009
	if (rrnum == -1)
Packit 6c4009
		rrnum = handle->_rrnum;
Packit 6c4009
	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
Packit 6c4009
		RETERR(ENODEV);
Packit 6c4009
	if (rrnum < handle->_rrnum)
Packit 6c4009
		setsection(handle, section);
Packit 6c4009
	if (rrnum > handle->_rrnum) {
Packit 6c4009
		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
Packit 6c4009
			      rrnum - handle->_rrnum);
Packit 6c4009
Packit 6c4009
		if (b < 0)
Packit 6c4009
			return (-1);
Packit 6c4009
		handle->_msg_ptr += b;
Packit 6c4009
		handle->_rrnum = rrnum;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	/* Do the parse. */
Packit 6c4009
	b = dn_expand(handle->_msg, handle->_eom,
Packit 6c4009
		      handle->_msg_ptr, rr->name, NS_MAXDNAME);
Packit 6c4009
	if (b < 0)
Packit 6c4009
		return (-1);
Packit 6c4009
	handle->_msg_ptr += b;
Packit 6c4009
	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
Packit 6c4009
		RETERR(EMSGSIZE);
Packit 6c4009
	NS_GET16(rr->type, handle->_msg_ptr);
Packit 6c4009
	NS_GET16(rr->rr_class, handle->_msg_ptr);
Packit 6c4009
	if (section == ns_s_qd) {
Packit 6c4009
		rr->ttl = 0;
Packit 6c4009
		rr->rdlength = 0;
Packit 6c4009
		rr->rdata = NULL;
Packit 6c4009
	} else {
Packit 6c4009
		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
Packit 6c4009
			RETERR(EMSGSIZE);
Packit 6c4009
		NS_GET32(rr->ttl, handle->_msg_ptr);
Packit 6c4009
		NS_GET16(rr->rdlength, handle->_msg_ptr);
Packit 6c4009
		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
Packit 6c4009
			RETERR(EMSGSIZE);
Packit 6c4009
		rr->rdata = handle->_msg_ptr;
Packit 6c4009
		handle->_msg_ptr += rr->rdlength;
Packit 6c4009
	}
Packit 6c4009
	if (++handle->_rrnum > handle->_counts[(int)section])
Packit 6c4009
		setsection(handle, (ns_sect)((int)section + 1));
Packit 6c4009
Packit 6c4009
	/* All done. */
Packit 6c4009
	return (0);
Packit 6c4009
}
Packit 6c4009
libresolv_hidden_def (ns_parserr)
Packit 6c4009
Packit 6c4009
/* Private. */
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
setsection(ns_msg *msg, ns_sect sect) {
Packit 6c4009
	msg->_sect = sect;
Packit 6c4009
	if (sect == ns_s_max) {
Packit 6c4009
		msg->_rrnum = -1;
Packit 6c4009
		msg->_msg_ptr = NULL;
Packit 6c4009
	} else {
Packit 6c4009
		msg->_rrnum = 0;
Packit 6c4009
		msg->_msg_ptr = msg->_sections[(int)sect];
Packit 6c4009
	}
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*! \file */