xref: /titanic_51/usr/src/lib/libresolv2/common/nameser/ns_parse.c (revision cbdcbd056f15c9c9fd82d5543b3a502677c4d391)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
20 #endif
21 
22 /* Import. */
23 
24 #include "port_before.h"
25 
26 #include <sys/types.h>
27 
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30 
31 #include <errno.h>
32 #include <resolv.h>
33 #include <string.h>
34 
35 #include "port_after.h"
36 
37 /* Forward. */
38 
39 static void	setsection(ns_msg *msg, ns_sect sect);
40 
41 /* Macros. */
42 
43 #if !defined(SOLARIS2) || defined(__COVERITY__)
44 #define RETERR(err) do { errno = (err); return (-1); } while (0)
45 #else
46 #define RETERR(err) \
47 	do { errno = (err); if (errno == errno) return (-1); } while (0)
48 #endif
49 
50 #define PARSE_FMT_PRESO 0	/* Parse using presentation-format names */
51 #define PARSE_FMT_WIRE 1	/* Parse using network-format names */
52 
53 /* Public. */
54 
55 /* These need to be in the same order as the nres.h:ns_flag enum. */
56 struct _ns_flagdata _ns_flagdata[16] = {
57 	{ 0x8000, 15 },		/*%< qr. */
58 	{ 0x7800, 11 },		/*%< opcode. */
59 	{ 0x0400, 10 },		/*%< aa. */
60 	{ 0x0200, 9 },		/*%< tc. */
61 	{ 0x0100, 8 },		/*%< rd. */
62 	{ 0x0080, 7 },		/*%< ra. */
63 	{ 0x0040, 6 },		/*%< z. */
64 	{ 0x0020, 5 },		/*%< ad. */
65 	{ 0x0010, 4 },		/*%< cd. */
66 	{ 0x000f, 0 },		/*%< rcode. */
67 	{ 0x0000, 0 },		/*%< expansion (1/6). */
68 	{ 0x0000, 0 },		/*%< expansion (2/6). */
69 	{ 0x0000, 0 },		/*%< expansion (3/6). */
70 	{ 0x0000, 0 },		/*%< expansion (4/6). */
71 	{ 0x0000, 0 },		/*%< expansion (5/6). */
72 	{ 0x0000, 0 },		/*%< expansion (6/6). */
73 };
74 
75 int ns_msg_getflag(ns_msg handle, int flag) {
76 	return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
77 }
78 
79 int
80 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
81 	const u_char *optr = ptr;
82 
83 	for ((void)NULL; count > 0; count--) {
84 		int b, rdlength;
85 
86 		b = dn_skipname(ptr, eom);
87 		if (b < 0)
88 			RETERR(EMSGSIZE);
89 		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
90 		if (section != ns_s_qd) {
91 			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
92 				RETERR(EMSGSIZE);
93 			ptr += NS_INT32SZ/*TTL*/;
94 			NS_GET16(rdlength, ptr);
95 			ptr += rdlength/*RData*/;
96 		}
97 	}
98 	if (ptr > eom)
99 		RETERR(EMSGSIZE);
100 	return (ptr - optr);
101 }
102 
103 int
104 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
105 	const u_char *eom = msg + msglen;
106 	int i;
107 
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 
139 int
140 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
141 	int b;
142 	int tmp;
143 
144 	/* Make section right. */
145 	tmp = section;
146 	if (tmp < 0 || section >= ns_s_max)
147 		RETERR(ENODEV);
148 	if (section != handle->_sect)
149 		setsection(handle, section);
150 
151 	/* Make rrnum right. */
152 	if (rrnum == -1)
153 		rrnum = handle->_rrnum;
154 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
155 		RETERR(ENODEV);
156 	if (rrnum < handle->_rrnum)
157 		setsection(handle, section);
158 	if (rrnum > handle->_rrnum) {
159 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
160 			      rrnum - handle->_rrnum);
161 
162 		if (b < 0)
163 			return (-1);
164 		handle->_msg_ptr += b;
165 		handle->_rrnum = rrnum;
166 	}
167 
168 	/* Do the parse. */
169 	b = dn_expand(handle->_msg, handle->_eom,
170 		      handle->_msg_ptr, rr->name, NS_MAXDNAME);
171 	if (b < 0)
172 		return (-1);
173 	handle->_msg_ptr += b;
174 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
175 		RETERR(EMSGSIZE);
176 	NS_GET16(rr->type, handle->_msg_ptr);
177 	NS_GET16(rr->rr_class, handle->_msg_ptr);
178 	if (section == ns_s_qd) {
179 		rr->ttl = 0;
180 		rr->rdlength = 0;
181 		rr->rdata = NULL;
182 	} else {
183 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
184 			RETERR(EMSGSIZE);
185 		NS_GET32(rr->ttl, handle->_msg_ptr);
186 		NS_GET16(rr->rdlength, handle->_msg_ptr);
187 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
188 			RETERR(EMSGSIZE);
189 		rr->rdata = handle->_msg_ptr;
190 		handle->_msg_ptr += rr->rdlength;
191 	}
192 	if (++handle->_rrnum > handle->_counts[(int)section])
193 		setsection(handle, (ns_sect)((int)section + 1));
194 
195 	/* All done. */
196 	return (0);
197 }
198 
199 /*
200  * This is identical to the above but uses network-format (uncompressed) names.
201  */
202 int
203 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
204 	int b;
205 	int tmp;
206 
207 	/* Make section right. */
208 	if ((tmp = section) < 0 || section >= ns_s_max)
209 		RETERR(ENODEV);
210 	if (section != handle->_sect)
211 		setsection(handle, section);
212 
213 	/* Make rrnum right. */
214 	if (rrnum == -1)
215 		rrnum = handle->_rrnum;
216 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
217 		RETERR(ENODEV);
218 	if (rrnum < handle->_rrnum)
219 		setsection(handle, section);
220 	if (rrnum > handle->_rrnum) {
221 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
222 			      rrnum - handle->_rrnum);
223 
224 		if (b < 0)
225 			return (-1);
226 		handle->_msg_ptr += b;
227 		handle->_rrnum = rrnum;
228 	}
229 
230 	/* Do the parse. */
231 	b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
232 			    rr->nname, NS_MAXNNAME, &rr->nnamel);
233 	if (b < 0)
234 		return (-1);
235 	handle->_msg_ptr += b;
236 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
237 		RETERR(EMSGSIZE);
238 	NS_GET16(rr->type, handle->_msg_ptr);
239 	NS_GET16(rr->rr_class, handle->_msg_ptr);
240 	if (section == ns_s_qd) {
241 		rr->ttl = 0;
242 		rr->rdlength = 0;
243 		rr->rdata = NULL;
244 	} else {
245 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
246 			RETERR(EMSGSIZE);
247 		NS_GET32(rr->ttl, handle->_msg_ptr);
248 		NS_GET16(rr->rdlength, handle->_msg_ptr);
249 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
250 			RETERR(EMSGSIZE);
251 		rr->rdata = handle->_msg_ptr;
252 		handle->_msg_ptr += rr->rdlength;
253 	}
254 	if (++handle->_rrnum > handle->_counts[(int)section])
255 		setsection(handle, (ns_sect)((int)section + 1));
256 
257 	/* All done. */
258 	return (0);
259 }
260 
261 /* Private. */
262 
263 static void
264 setsection(ns_msg *msg, ns_sect sect) {
265 	msg->_sect = sect;
266 	if (sect == ns_s_max) {
267 		msg->_rrnum = -1;
268 		msg->_msg_ptr = NULL;
269 	} else {
270 		msg->_rrnum = 0;
271 		msg->_msg_ptr = msg->_sections[(int)sect];
272 	}
273 }
274 
275 /*! \file */
276