2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 static const char rcsid
[] = "$Id: ns_parse.c,v 1.1 2006/03/01 19:01:37 majka Exp $";
27 #include "port_before.h"
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
40 #include "port_after.h"
45 static void setsection(ns_msg
*msg
, ns_sect sect
);
49 #define RETERR(err) do { errno = (err); return (-1); } while (0)
53 /* These need to be in the same order as the nres.h:ns_flag enum. */
54 struct _ns_flagdata _ns_flagdata
[16] = {
55 { 0x8000, 15 }, /* qr. */
56 { 0x7800, 11 }, /* opcode. */
57 { 0x0400, 10 }, /* aa. */
58 { 0x0200, 9 }, /* tc. */
59 { 0x0100, 8 }, /* rd. */
60 { 0x0080, 7 }, /* ra. */
61 { 0x0040, 6 }, /* z. */
62 { 0x0020, 5 }, /* ad. */
63 { 0x0010, 4 }, /* cd. */
64 { 0x000f, 0 }, /* rcode. */
65 { 0x0000, 0 }, /* expansion (1/6). */
66 { 0x0000, 0 }, /* expansion (2/6). */
67 { 0x0000, 0 }, /* expansion (3/6). */
68 { 0x0000, 0 }, /* expansion (4/6). */
69 { 0x0000, 0 }, /* expansion (5/6). */
70 { 0x0000, 0 }, /* expansion (6/6). */
73 int ns_msg_getflag(ns_msg handle
, int flag
) {
74 return(((handle
)._flags
& _ns_flagdata
[flag
].mask
) >> _ns_flagdata
[flag
].shift
);
78 ns_skiprr(const u_char
*ptr
, const u_char
*eom
, ns_sect section
, int count
) {
79 const u_char
*optr
= ptr
;
81 for ((void)NULL
; count
> 0; count
--) {
84 b
= dn_skipname(ptr
, eom
);
87 ptr
+= b
/*Name*/ + NS_INT16SZ
/*Type*/ + NS_INT16SZ
/*Class*/;
88 if (section
!= ns_s_qd
) {
89 if (ptr
+ NS_INT32SZ
+ NS_INT16SZ
> eom
)
91 ptr
+= NS_INT32SZ
/*TTL*/;
92 NS_GET16(rdlength
, ptr
);
93 ptr
+= rdlength
/*RData*/;
102 ns_initparse(const u_char
*msg
, int msglen
, ns_msg
*handle
) {
103 const u_char
*eom
= msg
+ msglen
;
106 memset(handle
, 0x5e, sizeof *handle
);
109 if (msg
+ NS_INT16SZ
> eom
)
111 NS_GET16(handle
->_id
, msg
);
112 if (msg
+ NS_INT16SZ
> eom
)
114 NS_GET16(handle
->_flags
, msg
);
115 for (i
= 0; i
< ns_s_max
; i
++) {
116 if (msg
+ NS_INT16SZ
> eom
)
118 NS_GET16(handle
->_counts
[i
], msg
);
120 for (i
= 0; i
< ns_s_max
; i
++)
121 if (handle
->_counts
[i
] == 0)
122 handle
->_sections
[i
] = NULL
;
124 int b
= ns_skiprr(msg
, eom
, (ns_sect
)i
,
129 handle
->_sections
[i
] = msg
;
134 setsection(handle
, ns_s_max
);
139 ns_parserr(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr
*rr
) {
142 /* Make section right. */
143 if (section
>= ns_s_max
)
145 if (section
!= handle
->_sect
)
146 setsection(handle
, section
);
148 /* Make rrnum right. */
150 rrnum
= handle
->_rrnum
;
151 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
153 if (rrnum
< handle
->_rrnum
)
154 setsection(handle
, section
);
155 if (rrnum
> handle
->_rrnum
) {
156 b
= ns_skiprr(handle
->_msg_ptr
, handle
->_eom
, section
,
157 rrnum
- handle
->_rrnum
);
161 handle
->_msg_ptr
+= b
;
162 handle
->_rrnum
= rrnum
;
166 b
= dn_expand(handle
->_msg
, handle
->_eom
,
167 handle
->_msg_ptr
, rr
->name
, NS_MAXDNAME
);
170 handle
->_msg_ptr
+= b
;
171 if (handle
->_msg_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
173 NS_GET16(rr
->type
, handle
->_msg_ptr
);
174 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
175 if (section
== ns_s_qd
) {
180 if (handle
->_msg_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
182 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
183 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
184 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
186 rr
->rdata
= handle
->_msg_ptr
;
187 handle
->_msg_ptr
+= rr
->rdlength
;
189 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
190 setsection(handle
, (ns_sect
)((int)section
+ 1));
199 setsection(ns_msg
*msg
, ns_sect sect
) {
201 if (sect
== ns_s_max
) {
203 msg
->_msg_ptr
= NULL
;
206 msg
->_msg_ptr
= msg
->_sections
[(int)sect
];