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