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