1 /* $NetBSD: ns_parse.c,v 1.8 2009/04/12 19:43:37 christos Exp $ */
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
20 #include <sys/cdefs.h>
23 static const char rcsid
[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp";
25 __RCSID("$NetBSD: ns_parse.c,v 1.8 2009/04/12 19:43:37 christos Exp $");
31 #include "port_before.h"
33 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
42 #include "port_after.h"
46 static void setsection(ns_msg
*msg
, ns_sect sect
);
50 #if !defined(SOLARIS2) || defined(__COVERITY__)
51 #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0)
54 do { errno = (err); if (errno == errno) return (-1); } while (0)
57 #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
58 #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
62 /* These need to be in the same order as the nres.h:ns_flag enum. */
63 struct _ns_flagdata _ns_flagdata
[16] = {
64 { 0x8000, 15 }, /*%< qr. */
65 { 0x7800, 11 }, /*%< opcode. */
66 { 0x0400, 10 }, /*%< aa. */
67 { 0x0200, 9 }, /*%< tc. */
68 { 0x0100, 8 }, /*%< rd. */
69 { 0x0080, 7 }, /*%< ra. */
70 { 0x0040, 6 }, /*%< z. */
71 { 0x0020, 5 }, /*%< ad. */
72 { 0x0010, 4 }, /*%< cd. */
73 { 0x000f, 0 }, /*%< rcode. */
74 { 0x0000, 0 }, /*%< expansion (1/6). */
75 { 0x0000, 0 }, /*%< expansion (2/6). */
76 { 0x0000, 0 }, /*%< expansion (3/6). */
77 { 0x0000, 0 }, /*%< expansion (4/6). */
78 { 0x0000, 0 }, /*%< expansion (5/6). */
79 { 0x0000, 0 }, /*%< expansion (6/6). */
82 int ns_msg_getflag(ns_msg handle
, int flag
) {
83 return((u_int32_t
)((handle
)._flags
& _ns_flagdata
[flag
].mask
) >> _ns_flagdata
[flag
].shift
);
87 ns_skiprr(const u_char
*ptr
, const u_char
*eom
, ns_sect section
, int count
) {
88 const u_char
*optr
= ptr
;
90 for (; count
> 0; count
--) {
93 b
= dn_skipname(ptr
, eom
);
96 ptr
+= b
/*Name*/ + NS_INT16SZ
/*Type*/ + NS_INT16SZ
/*Class*/;
97 if (section
!= ns_s_qd
) {
98 if (ptr
+ NS_INT32SZ
+ NS_INT16SZ
> eom
)
100 ptr
+= NS_INT32SZ
/*TTL*/;
101 NS_GET16(rdlength
, ptr
);
102 ptr
+= rdlength
/*RData*/;
111 ns_initparse(const u_char
*msg
, int msglen
, ns_msg
*handle
) {
112 const u_char
*eom
= msg
+ msglen
;
117 if (msg
+ NS_INT16SZ
> eom
)
119 NS_GET16(handle
->_id
, msg
);
120 if (msg
+ NS_INT16SZ
> eom
)
122 NS_GET16(handle
->_flags
, msg
);
123 for (i
= 0; i
< ns_s_max
; i
++) {
124 if (msg
+ NS_INT16SZ
> eom
)
126 NS_GET16(handle
->_counts
[i
], msg
);
128 for (i
= 0; i
< ns_s_max
; i
++)
129 if (handle
->_counts
[i
] == 0)
130 handle
->_sections
[i
] = NULL
;
132 int b
= ns_skiprr(msg
, eom
, (ns_sect
)i
,
137 handle
->_sections
[i
] = msg
;
142 setsection(handle
, ns_s_max
);
147 ns_parserr(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr
*rr
) {
151 /* Make section right. */
153 if (tmp
< 0 || section
>= ns_s_max
)
155 if (section
!= handle
->_sect
)
156 setsection(handle
, section
);
158 /* Make rrnum right. */
160 rrnum
= handle
->_rrnum
;
161 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
163 if (rrnum
< handle
->_rrnum
)
164 setsection(handle
, section
);
165 if (rrnum
> handle
->_rrnum
) {
166 b
= ns_skiprr(handle
->_msg_ptr
, handle
->_eom
, section
,
167 rrnum
- handle
->_rrnum
);
171 handle
->_msg_ptr
+= b
;
172 handle
->_rrnum
= rrnum
;
176 b
= dn_expand(handle
->_msg
, handle
->_eom
,
177 handle
->_msg_ptr
, rr
->name
, NS_MAXDNAME
);
180 handle
->_msg_ptr
+= b
;
181 if (handle
->_msg_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
183 NS_GET16(rr
->type
, handle
->_msg_ptr
);
184 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
185 if (section
== ns_s_qd
) {
190 if (handle
->_msg_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
192 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
193 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
194 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
196 rr
->rdata
= handle
->_msg_ptr
;
197 handle
->_msg_ptr
+= rr
->rdlength
;
199 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
200 setsection(handle
, (ns_sect
)((int)section
+ 1));
207 * This is identical to the above but uses network-format (uncompressed) names.
210 ns_parserr2(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr2
*rr
) {
214 /* Make section right. */
216 if (tmp
< 0 || section
>= ns_s_max
)
218 if (section
!= handle
->_sect
)
219 setsection(handle
, section
);
221 /* Make rrnum right. */
223 rrnum
= handle
->_rrnum
;
224 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
226 if (rrnum
< handle
->_rrnum
)
227 setsection(handle
, section
);
228 if (rrnum
> handle
->_rrnum
) {
229 b
= ns_skiprr(handle
->_msg_ptr
, handle
->_eom
, section
,
230 rrnum
- handle
->_rrnum
);
234 handle
->_msg_ptr
+= b
;
235 handle
->_rrnum
= rrnum
;
239 b
= ns_name_unpack2(handle
->_msg
, handle
->_eom
, handle
->_msg_ptr
,
240 rr
->nname
, NS_MAXNNAME
, &rr
->nnamel
);
243 handle
->_msg_ptr
+= b
;
244 if (handle
->_msg_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
246 NS_GET16(rr
->type
, handle
->_msg_ptr
);
247 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
248 if (section
== ns_s_qd
) {
253 if (handle
->_msg_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
255 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
256 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
257 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
259 rr
->rdata
= handle
->_msg_ptr
;
260 handle
->_msg_ptr
+= rr
->rdlength
;
262 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
263 setsection(handle
, (ns_sect
)((int)section
+ 1));
272 setsection(ns_msg
*msg
, ns_sect sect
) {
274 if (sect
== ns_s_max
) {
276 msg
->_msg_ptr
= NULL
;
279 msg
->_msg_ptr
= msg
->_sections
[(int)sect
];