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