xref: /haiku/src/system/libnetwork/netresolv/nameser/ns_parse.c (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
1 /*	$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 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
23 static const char rcsid[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp";
24 #else
25 //__RCSID("$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $");
26 #endif
27 #endif
28 
29 /* Import. */
30 
31 #include "port_before.h"
32 
33 #include <sys/types.h>
34 
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
37 
38 #include <assert.h>
39 #include <errno.h>
40 #include <resolv.h>
41 #include <string.h>
42 
43 #include "port_after.h"
44 
45 /* Forward. */
46 
47 static void	setsection(ns_msg *msg, ns_sect sect);
48 
49 /* Macros. */
50 
51 #if !defined(SOLARIS2) || defined(__COVERITY__)
52 #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0)
53 #else
54 #define RETERR(err) \
55 	do { errno = (err); if (errno == errno) return (-1); } while (0)
56 #endif
57 
58 #define PARSE_FMT_PRESO 0	/* Parse using presentation-format names */
59 #define PARSE_FMT_WIRE 1	/* Parse using network-format names */
60 
61 /* Public. */
62 
63 /* These need to be in the same order as the nres.h:ns_flag enum. */
64 struct _ns_flagdata _ns_flagdata[16] = {
65 	{ 0x8000, 15 },		/*%< qr. */
66 	{ 0x7800, 11 },		/*%< opcode. */
67 	{ 0x0400, 10 },		/*%< aa. */
68 	{ 0x0200, 9 },		/*%< tc. */
69 	{ 0x0100, 8 },		/*%< rd. */
70 	{ 0x0080, 7 },		/*%< ra. */
71 	{ 0x0040, 6 },		/*%< z. */
72 	{ 0x0020, 5 },		/*%< ad. */
73 	{ 0x0010, 4 },		/*%< cd. */
74 	{ 0x000f, 0 },		/*%< rcode. */
75 	{ 0x0000, 0 },		/*%< expansion (1/6). */
76 	{ 0x0000, 0 },		/*%< expansion (2/6). */
77 	{ 0x0000, 0 },		/*%< expansion (3/6). */
78 	{ 0x0000, 0 },		/*%< expansion (4/6). */
79 	{ 0x0000, 0 },		/*%< expansion (5/6). */
80 	{ 0x0000, 0 },		/*%< expansion (6/6). */
81 };
82 
83 int ns_msg_getflag(ns_msg handle, int flag) {
84 	return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
85 }
86 
87 int
88 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
89 	const u_char *optr = ptr;
90 
91 	for (; count > 0; count--) {
92 		int b, rdlength;
93 
94 		b = dn_skipname(ptr, eom);
95 		if (b < 0)
96 			RETERR(EMSGSIZE);
97 		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
98 		if (section != ns_s_qd) {
99 			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
100 				RETERR(EMSGSIZE);
101 			ptr += NS_INT32SZ/*TTL*/;
102 			NS_GET16(rdlength, ptr);
103 			ptr += rdlength/*RData*/;
104 		}
105 	}
106 	if (ptr > eom)
107 		RETERR(EMSGSIZE);
108 	assert(INT_MIN <= (ptr - optr) && (ptr - optr) <= INT_MAX);
109 	return (int)(ptr - optr);
110 }
111 
112 int
113 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
114 	const u_char *eom = msg + msglen;
115 	int i;
116 
117 	handle->_msg = msg;
118 	handle->_eom = eom;
119 	if (msg + NS_INT16SZ > eom)
120 		RETERR(EMSGSIZE);
121 	NS_GET16(handle->_id, msg);
122 	if (msg + NS_INT16SZ > eom)
123 		RETERR(EMSGSIZE);
124 	NS_GET16(handle->_flags, msg);
125 	for (i = 0; i < ns_s_max; i++) {
126 		if (msg + NS_INT16SZ > eom)
127 			RETERR(EMSGSIZE);
128 		NS_GET16(handle->_counts[i], msg);
129 	}
130 	for (i = 0; i < ns_s_max; i++)
131 		if (handle->_counts[i] == 0)
132 			handle->_sections[i] = NULL;
133 		else {
134 			int b = ns_skiprr(msg, eom, (ns_sect)i,
135 					  handle->_counts[i]);
136 
137 			if (b < 0)
138 				return (-1);
139 			handle->_sections[i] = msg;
140 			msg += b;
141 		}
142 	if (msg != eom)
143 		RETERR(EMSGSIZE);
144 	setsection(handle, ns_s_max);
145 	return (0);
146 }
147 
148 int
149 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
150 	int b;
151 	int tmp;
152 
153 	/* Make section right. */
154 	tmp = section;
155 	if (tmp < 0 || section >= ns_s_max)
156 		RETERR(ENODEV);
157 	if (section != handle->_sect)
158 		setsection(handle, section);
159 
160 	/* Make rrnum right. */
161 	if (rrnum == -1)
162 		rrnum = handle->_rrnum;
163 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
164 		RETERR(ENODEV);
165 	if (rrnum < handle->_rrnum)
166 		setsection(handle, section);
167 	if (rrnum > handle->_rrnum) {
168 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
169 			      rrnum - handle->_rrnum);
170 
171 		if (b < 0)
172 			return (-1);
173 		handle->_msg_ptr += b;
174 		handle->_rrnum = rrnum;
175 	}
176 
177 	/* Do the parse. */
178 	b = dn_expand(handle->_msg, handle->_eom,
179 		      handle->_msg_ptr, rr->name, NS_MAXDNAME);
180 	if (b < 0)
181 		return (-1);
182 	handle->_msg_ptr += b;
183 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
184 		RETERR(EMSGSIZE);
185 	NS_GET16(rr->type, handle->_msg_ptr);
186 	NS_GET16(rr->rr_class, handle->_msg_ptr);
187 	if (section == ns_s_qd) {
188 		rr->ttl = 0;
189 		rr->rdlength = 0;
190 		rr->rdata = NULL;
191 	} else {
192 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
193 			RETERR(EMSGSIZE);
194 		NS_GET32(rr->ttl, handle->_msg_ptr);
195 		NS_GET16(rr->rdlength, handle->_msg_ptr);
196 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
197 			RETERR(EMSGSIZE);
198 		rr->rdata = handle->_msg_ptr;
199 		handle->_msg_ptr += rr->rdlength;
200 	}
201 	if (++handle->_rrnum > handle->_counts[(int)section])
202 		setsection(handle, (ns_sect)((int)section + 1));
203 
204 	/* All done. */
205 	return (0);
206 }
207 
208 extern int
209 ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src,
210 		u_char *dst, size_t dstsiz, size_t *dstlen);
211 
212 /*
213  * This is identical to the above but uses network-format (uncompressed) names.
214  */
215 int
216 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
217 	int b;
218 	int tmp;
219 
220 	/* Make section right. */
221 	tmp = section;
222 	if (tmp < 0 || section >= ns_s_max)
223 		RETERR(ENODEV);
224 	if (section != handle->_sect)
225 		setsection(handle, section);
226 
227 	/* Make rrnum right. */
228 	if (rrnum == -1)
229 		rrnum = handle->_rrnum;
230 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
231 		RETERR(ENODEV);
232 	if (rrnum < handle->_rrnum)
233 		setsection(handle, section);
234 	if (rrnum > handle->_rrnum) {
235 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
236 			      rrnum - handle->_rrnum);
237 
238 		if (b < 0)
239 			return (-1);
240 		handle->_msg_ptr += b;
241 		handle->_rrnum = rrnum;
242 	}
243 
244 	/* Do the parse. */
245 	b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
246 			    rr->nname, NS_MAXNNAME, &rr->nnamel);
247 	if (b < 0)
248 		return (-1);
249 	handle->_msg_ptr += b;
250 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
251 		RETERR(EMSGSIZE);
252 	NS_GET16(rr->type, handle->_msg_ptr);
253 	NS_GET16(rr->rr_class, handle->_msg_ptr);
254 	if (section == ns_s_qd) {
255 		rr->ttl = 0;
256 		rr->rdlength = 0;
257 		rr->rdata = NULL;
258 	} else {
259 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
260 			RETERR(EMSGSIZE);
261 		NS_GET32(rr->ttl, handle->_msg_ptr);
262 		NS_GET16(rr->rdlength, handle->_msg_ptr);
263 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
264 			RETERR(EMSGSIZE);
265 		rr->rdata = handle->_msg_ptr;
266 		handle->_msg_ptr += rr->rdlength;
267 	}
268 	if (++handle->_rrnum > handle->_counts[(int)section])
269 		setsection(handle, (ns_sect)((int)section + 1));
270 
271 	/* All done. */
272 	return (0);
273 }
274 
275 /* Private. */
276 
277 static void
278 setsection(ns_msg *msg, ns_sect sect) {
279 	msg->_sect = sect;
280 	if (sect == ns_s_max) {
281 		msg->_rrnum = -1;
282 		msg->_msg_ptr = NULL;
283 	} else {
284 		msg->_rrnum = 0;
285 		msg->_msg_ptr = msg->_sections[(int)sect];
286 	}
287 }
288 
289 /*! \file */
290