2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-2003 by Internet Software Consortium
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Internet Systems Consortium, Inc.
19 * Redwood City, CA 94063
24 #if !defined(lint) && !defined(__AROS__)
25 static const char rcsid
[] = "$Id$";
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <sys/socket.h>
38 #include "minires/minires.h"
39 #include "arpa/nameser.h"
43 static void setsection(ns_msg
*msg
, ns_sect sect
);
49 /* These need to be in the same order as the nres.h:ns_flag enum. */
50 struct _ns_flagdata _ns_flagdata
[16] = {
51 { 0x8000, 15 }, /* qr. */
52 { 0x7800, 11 }, /* opcode. */
53 { 0x0400, 10 }, /* aa. */
54 { 0x0200, 9 }, /* tc. */
55 { 0x0100, 8 }, /* rd. */
56 { 0x0080, 7 }, /* ra. */
57 { 0x0040, 6 }, /* z. */
58 { 0x0020, 5 }, /* ad. */
59 { 0x0010, 4 }, /* cd. */
60 { 0x000f, 0 }, /* rcode. */
61 { 0x0000, 0 }, /* expansion (1/6). */
62 { 0x0000, 0 }, /* expansion (2/6). */
63 { 0x0000, 0 }, /* expansion (3/6). */
64 { 0x0000, 0 }, /* expansion (4/6). */
65 { 0x0000, 0 }, /* expansion (5/6). */
66 { 0x0000, 0 }, /* expansion (6/6). */
70 ns_skiprr(const u_char
*ptr
, const u_char
*eom
, ns_sect section
, int count
,
72 const u_char
*optr
= ptr
;
74 for ((void)NULL
; count
> 0; count
--) {
77 b
= dn_skipname(ptr
, eom
);
79 return ISC_R_INCOMPLETE
;
80 ptr
+= b
/*Name*/ + NS_INT16SZ
/*Type*/ + NS_INT16SZ
/*Class*/;
81 if (section
!= ns_s_qd
) {
82 if (ptr
+ NS_INT32SZ
+ NS_INT16SZ
> eom
)
83 return ISC_R_INCOMPLETE
;
84 ptr
+= NS_INT32SZ
/*TTL*/;
85 rdlength
= getUShort(ptr
);
87 ptr
+= rdlength
/*RData*/;
91 return ISC_R_INCOMPLETE
;
98 ns_initparse(const u_char
*msg
, unsigned msglen
, ns_msg
*handle
) {
99 const u_char
*eom
= msg
+ msglen
;
102 memset(handle
, 0x5e, sizeof *handle
);
105 if (msg
+ NS_INT16SZ
> eom
)
106 return ISC_R_INCOMPLETE
;
107 handle
->_id
= getUShort (msg
);
109 if (msg
+ NS_INT16SZ
> eom
)
110 return ISC_R_INCOMPLETE
;
111 handle
->_flags
= getUShort (msg
);
113 for (i
= 0; i
< ns_s_max
; i
++) {
114 if (msg
+ NS_INT16SZ
> eom
)
115 return ISC_R_INCOMPLETE
;
116 handle
->_counts
[i
] = getUShort (msg
);
119 for (i
= 0; i
< ns_s_max
; i
++)
120 if (handle
->_counts
[i
] == 0)
121 handle
->_sections
[i
] = NULL
;
124 isc_result_t status
=
125 ns_skiprr(msg
, eom
, (ns_sect
)i
,
126 handle
->_counts
[i
], &b
);
128 if (status
!= ISC_R_SUCCESS
)
130 handle
->_sections
[i
] = msg
;
134 return ISC_R_INCOMPLETE
;
135 setsection(handle
, ns_s_max
);
136 return ISC_R_SUCCESS
;
140 ns_parserr(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr
*rr
) {
144 /* Make section right. */
145 if (section
< 0 || section
>= ns_s_max
)
146 return ISC_R_NOTIMPLEMENTED
;
147 if (section
!= handle
->_sect
)
148 setsection(handle
, section
);
150 /* Make rrnum right. */
152 rrnum
= handle
->_rrnum
;
153 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
154 return ISC_R_UNKNOWNATTRIBUTE
;
155 if (rrnum
< handle
->_rrnum
)
156 setsection(handle
, section
);
157 if (rrnum
> handle
->_rrnum
) {
158 status
= ns_skiprr(handle
->_ptr
, handle
->_eom
, section
,
159 rrnum
- handle
->_rrnum
, &b
);
161 if (status
!= ISC_R_SUCCESS
)
164 handle
->_rrnum
= rrnum
;
168 b
= dn_expand(handle
->_msg
, handle
->_eom
,
169 handle
->_ptr
, rr
->name
, NS_MAXDNAME
);
171 return ISC_R_FORMERR
;
173 if (handle
->_ptr
+ NS_INT16SZ
+ NS_INT16SZ
> handle
->_eom
)
174 return ISC_R_INCOMPLETE
;
175 rr
->type
= getUShort (handle
->_ptr
);
177 rr
->rr_class
= getUShort (handle
->_ptr
);
179 if (section
== ns_s_qd
) {
184 if (handle
->_ptr
+ NS_INT32SZ
+ NS_INT16SZ
> handle
->_eom
)
185 return ISC_R_INCOMPLETE
;
186 rr
->ttl
= getULong (handle
->_ptr
);
188 rr
->rdlength
= getUShort (handle
->_ptr
);
190 if (handle
->_ptr
+ rr
->rdlength
> handle
->_eom
)
191 return ISC_R_INCOMPLETE
;
192 rr
->rdata
= handle
->_ptr
;
193 handle
->_ptr
+= rr
->rdlength
;
195 if (++handle
->_rrnum
> handle
->_counts
[(int)section
])
196 setsection(handle
, (ns_sect
)((int)section
+ 1));
199 return ISC_R_SUCCESS
;
205 setsection(ns_msg
*msg
, ns_sect sect
) {
207 if (sect
== ns_s_max
) {
212 msg
->_ptr
= msg
->_sections
[(int)sect
];