1 /* $NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 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.9 2012/03/13 21:13:39 christos Exp $");
31 #include "port_before.h"
33 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
43 #include "port_after.h"
47 static void setsection(ns_msg
*msg
, ns_sect sect
);
51 #if !defined(SOLARIS2) || defined(__COVERITY__)
52 #define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0)
55 do { errno = (err); if (errno == errno) return (-1); } while (0)
58 #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
59 #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
63 /* These need to be in the same order as the nres.h:ns_flag enum. */
64 struct _ns_flagdata _ns_flagdata
[16] = {
65 { 0x8000, 15 }, /*%< qr. */
66 { 0x7800, 11 }, /*%< opcode. */
67 { 0x0400, 10 }, /*%< aa. */
68 { 0x0200, 9 }, /*%< tc. */
69 { 0x0100, 8 }, /*%< rd. */
70 { 0x0080, 7 }, /*%< ra. */
71 { 0x0040, 6 }, /*%< z. */
72 { 0x0020, 5 }, /*%< ad. */
73 { 0x0010, 4 }, /*%< cd. */
74 { 0x000f, 0 }, /*%< rcode. */
75 { 0x0000, 0 }, /*%< expansion (1/6). */
76 { 0x0000, 0 }, /*%< expansion (2/6). */
77 { 0x0000, 0 }, /*%< expansion (3/6). */
78 { 0x0000, 0 }, /*%< expansion (4/6). */
79 { 0x0000, 0 }, /*%< expansion (5/6). */
80 { 0x0000, 0 }, /*%< expansion (6/6). */
83 int ns_msg_getflag(ns_msg handle
, int flag
) {
84 return((u_int32_t
)((handle
)._flags
& _ns_flagdata
[flag
].mask
) >> _ns_flagdata
[flag
].shift
);
88 ns_skiprr(const u_char
*ptr
, const u_char
*eom
, ns_sect section
, int count
) {
89 const u_char
*optr
= ptr
;
91 for (; count
> 0; count
--) {
94 b
= dn_skipname(ptr
, eom
);
97 ptr
+= b
/*Name*/ + NS_INT16SZ
/*Type*/ + NS_INT16SZ
/*Class*/;
98 if (section
!= ns_s_qd
) {
99 if (ptr
+ NS_INT32SZ
+ NS_INT16SZ
> eom
)
101 ptr
+= NS_INT32SZ
/*TTL*/;
102 NS_GET16(rdlength
, ptr
);
103 ptr
+= rdlength
/*RData*/;
108 _DIAGASSERT(__type_fit(int, ptr
- optr
));
109 return (int)(ptr
- optr
);
113 ns_initparse(const u_char
*msg
, int msglen
, ns_msg
*handle
) {
114 const u_char
*eom
= msg
+ msglen
;
119 if (msg
+ NS_INT16SZ
> eom
)
121 NS_GET16(handle
->_id
, msg
);
122 if (msg
+ NS_INT16SZ
> eom
)
124 NS_GET16(handle
->_flags
, msg
);
125 for (i
= 0; i
< ns_s_max
; i
++) {
126 if (msg
+ NS_INT16SZ
> eom
)
128 NS_GET16(handle
->_counts
[i
], msg
);
130 for (i
= 0; i
< ns_s_max
; i
++)
131 if (handle
->_counts
[i
] == 0)
132 handle
->_sections
[i
] = NULL
;
134 int b
= ns_skiprr(msg
, eom
, (ns_sect
)i
,
139 handle
->_sections
[i
] = msg
;
144 setsection(handle
, ns_s_max
);
149 ns_parserr(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr
*rr
) {
153 /* Make section right. */
155 if (tmp
< 0 || section
>= ns_s_max
)
157 if (section
!= handle
->_sect
)
158 setsection(handle
, section
);
160 /* Make rrnum right. */
162 rrnum
= handle
->_rrnum
;
163 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
165 if (rrnum
< handle
->_rrnum
)
166 setsection(handle
, section
);
167 if (rrnum
> handle
->_rrnum
) {
168 b
= ns_skiprr(handle
->_msg_ptr
, handle
->_eom
, section
,
169 rrnum
- handle
->_rrnum
);
173 handle
->_msg_ptr
+= b
;
174 handle
->_rrnum
= rrnum
;
178 b
= dn_expand(handle
->_msg
, handle
->_eom
,
179 handle
->_msg_ptr
, rr
->name
, NS_MAXDNAME
);
182 handle
->_msg_ptr
+= b
;
183 if (handle
->_msg_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
185 NS_GET16(rr
->type
, handle
->_msg_ptr
);
186 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
187 if (section
== ns_s_qd
) {
192 if (handle
->_msg_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
194 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
195 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
196 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
198 rr
->rdata
= handle
->_msg_ptr
;
199 handle
->_msg_ptr
+= rr
->rdlength
;
201 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
202 setsection(handle
, (ns_sect
)((int)section
+ 1));
209 * This is identical to the above but uses network-format (uncompressed) names.
212 ns_parserr2(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr2
*rr
) {
216 /* Make section right. */
218 if (tmp
< 0 || section
>= ns_s_max
)
220 if (section
!= handle
->_sect
)
221 setsection(handle
, section
);
223 /* Make rrnum right. */
225 rrnum
= handle
->_rrnum
;
226 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
228 if (rrnum
< handle
->_rrnum
)
229 setsection(handle
, section
);
230 if (rrnum
> handle
->_rrnum
) {
231 b
= ns_skiprr(handle
->_msg_ptr
, handle
->_eom
, section
,
232 rrnum
- handle
->_rrnum
);
236 handle
->_msg_ptr
+= b
;
237 handle
->_rrnum
= rrnum
;
241 b
= ns_name_unpack2(handle
->_msg
, handle
->_eom
, handle
->_msg_ptr
,
242 rr
->nname
, NS_MAXNNAME
, &rr
->nnamel
);
245 handle
->_msg_ptr
+= b
;
246 if (handle
->_msg_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
248 NS_GET16(rr
->type
, handle
->_msg_ptr
);
249 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
250 if (section
== ns_s_qd
) {
255 if (handle
->_msg_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
257 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
258 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
259 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
261 rr
->rdata
= handle
->_msg_ptr
;
262 handle
->_msg_ptr
+= rr
->rdlength
;
264 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
265 setsection(handle
, (ns_sect
)((int)section
+ 1));
274 setsection(ns_msg
*msg
, ns_sect sect
) {
276 if (sect
== ns_s_max
) {
278 msg
->_msg_ptr
= NULL
;
281 msg
->_msg_ptr
= msg
->_sections
[(int)sect
];