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