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.
21 static const char rcsid
[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp";
26 #include "port_before.h"
28 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
37 #include "port_after.h"
41 static void setsection(ns_msg
*msg
, ns_sect sect
);
45 #if !defined(SOLARIS2) || defined(__COVERITY__)
46 #define RETERR(err) do { errno = (err); return (-1); } while (0)
49 do { errno = (err); if (errno == errno) return (-1); } while (0)
52 #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
53 #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
57 /* These need to be in the same order as the nres.h:ns_flag enum. */
58 struct _ns_flagdata _ns_flagdata
[16] = {
59 { 0x8000, 15 }, /*%< qr. */
60 { 0x7800, 11 }, /*%< opcode. */
61 { 0x0400, 10 }, /*%< aa. */
62 { 0x0200, 9 }, /*%< tc. */
63 { 0x0100, 8 }, /*%< rd. */
64 { 0x0080, 7 }, /*%< ra. */
65 { 0x0040, 6 }, /*%< z. */
66 { 0x0020, 5 }, /*%< ad. */
67 { 0x0010, 4 }, /*%< cd. */
68 { 0x000f, 0 }, /*%< rcode. */
69 { 0x0000, 0 }, /*%< expansion (1/6). */
70 { 0x0000, 0 }, /*%< expansion (2/6). */
71 { 0x0000, 0 }, /*%< expansion (3/6). */
72 { 0x0000, 0 }, /*%< expansion (4/6). */
73 { 0x0000, 0 }, /*%< expansion (5/6). */
74 { 0x0000, 0 }, /*%< expansion (6/6). */
77 int ns_msg_getflag(ns_msg handle
, int flag
) {
78 return(((handle
)._flags
& _ns_flagdata
[flag
].mask
) >> _ns_flagdata
[flag
].shift
);
82 ns_skiprr(const u_char
*ptr
, const u_char
*eom
, ns_sect section
, int count
) {
83 const u_char
*optr
= ptr
;
85 for ((void)NULL
; count
> 0; count
--) {
88 b
= dn_skipname(ptr
, eom
);
91 ptr
+= b
/*Name*/ + NS_INT16SZ
/*Type*/ + NS_INT16SZ
/*Class*/;
92 if (section
!= ns_s_qd
) {
93 if (ptr
+ NS_INT32SZ
+ NS_INT16SZ
> eom
)
95 ptr
+= NS_INT32SZ
/*TTL*/;
96 NS_GET16(rdlength
, ptr
);
97 ptr
+= rdlength
/*RData*/;
106 ns_initparse(const u_char
*msg
, int msglen
, ns_msg
*handle
) {
107 const u_char
*eom
= msg
+ msglen
;
112 if (msg
+ NS_INT16SZ
> eom
)
114 NS_GET16(handle
->_id
, msg
);
115 if (msg
+ NS_INT16SZ
> eom
)
117 NS_GET16(handle
->_flags
, msg
);
118 for (i
= 0; i
< ns_s_max
; i
++) {
119 if (msg
+ NS_INT16SZ
> eom
)
121 NS_GET16(handle
->_counts
[i
], msg
);
123 for (i
= 0; i
< ns_s_max
; i
++)
124 if (handle
->_counts
[i
] == 0)
125 handle
->_sections
[i
] = NULL
;
127 int b
= ns_skiprr(msg
, eom
, (ns_sect
)i
,
132 handle
->_sections
[i
] = msg
;
137 setsection(handle
, ns_s_max
);
142 ns_parserr(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr
*rr
) {
146 /* Make section right. */
148 if (tmp
< 0 || section
>= ns_s_max
)
150 if (section
!= handle
->_sect
)
151 setsection(handle
, section
);
153 /* Make rrnum right. */
155 rrnum
= handle
->_rrnum
;
156 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
158 if (rrnum
< handle
->_rrnum
)
159 setsection(handle
, section
);
160 if (rrnum
> handle
->_rrnum
) {
161 b
= ns_skiprr(handle
->_msg_ptr
, handle
->_eom
, section
,
162 rrnum
- handle
->_rrnum
);
166 handle
->_msg_ptr
+= b
;
167 handle
->_rrnum
= rrnum
;
171 b
= dn_expand(handle
->_msg
, handle
->_eom
,
172 handle
->_msg_ptr
, rr
->name
, NS_MAXDNAME
);
175 handle
->_msg_ptr
+= b
;
176 if (handle
->_msg_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
178 NS_GET16(rr
->type
, handle
->_msg_ptr
);
179 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
180 if (section
== ns_s_qd
) {
185 if (handle
->_msg_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
187 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
188 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
189 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
191 rr
->rdata
= handle
->_msg_ptr
;
192 handle
->_msg_ptr
+= rr
->rdlength
;
194 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
195 setsection(handle
, (ns_sect
)((int)section
+ 1));
202 * This is identical to the above but uses network-format (uncompressed) names.
205 ns_parserr2(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr2
*rr
) {
209 /* Make section right. */
210 if ((tmp
= section
) < 0 || section
>= ns_s_max
)
212 if (section
!= handle
->_sect
)
213 setsection(handle
, section
);
215 /* Make rrnum right. */
217 rrnum
= handle
->_rrnum
;
218 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
220 if (rrnum
< handle
->_rrnum
)
221 setsection(handle
, section
);
222 if (rrnum
> handle
->_rrnum
) {
223 b
= ns_skiprr(handle
->_msg_ptr
, handle
->_eom
, section
,
224 rrnum
- handle
->_rrnum
);
228 handle
->_msg_ptr
+= b
;
229 handle
->_rrnum
= rrnum
;
233 b
= ns_name_unpack2(handle
->_msg
, handle
->_eom
, handle
->_msg_ptr
,
234 rr
->nname
, NS_MAXNNAME
, &rr
->nnamel
);
237 handle
->_msg_ptr
+= b
;
238 if (handle
->_msg_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
240 NS_GET16(rr
->type
, handle
->_msg_ptr
);
241 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
242 if (section
== ns_s_qd
) {
247 if (handle
->_msg_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
249 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
250 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
251 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
253 rr
->rdata
= handle
->_msg_ptr
;
254 handle
->_msg_ptr
+= rr
->rdlength
;
256 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
257 setsection(handle
, (ns_sect
)((int)section
+ 1));
266 setsection(ns_msg
*msg
, ns_sect sect
) {
268 if (sect
== ns_s_max
) {
270 msg
->_msg_ptr
= NULL
;
273 msg
->_msg_ptr
= msg
->_sections
[(int)sect
];