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