2 * Copyright (c) 1996 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
19 * Portions copyright (c) 1999, 2000
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
37 * This product includes software developed by Intel Corporation and
40 * 4. Neither the name of Intel Corporation or its contributors may be
41 * used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/types.h>
60 #include <netinet/in.h>
61 #include <arpa/nameser.h>
67 /* These need to be in the same order as the nres.h:ns_flag enum. */
68 struct _ns_flagdata _ns_flagdata
[16] = {
69 { 0x8000, 15 }, /* qr. */
70 { 0x7800, 11 }, /* opcode. */
71 { 0x0400, 10 }, /* aa. */
72 { 0x0200, 9 }, /* tc. */
73 { 0x0100, 8 }, /* rd. */
74 { 0x0080, 7 }, /* ra. */
75 { 0x0040, 6 }, /* z. */
76 { 0x0020, 5 }, /* ad. */
77 { 0x0010, 4 }, /* cd. */
78 { 0x000f, 0 }, /* rcode. */
79 { 0x0000, 0 }, /* expansion (1/6). */
80 { 0x0000, 0 }, /* expansion (2/6). */
81 { 0x0000, 0 }, /* expansion (3/6). */
82 { 0x0000, 0 }, /* expansion (4/6). */
83 { 0x0000, 0 }, /* expansion (5/6). */
84 { 0x0000, 0 }, /* expansion (6/6). */
88 skiprr(const u_char
*ptr
, const u_char
*eom
, ns_sect section
, int count
) {
89 const u_char
*optr
= ptr
;
91 for ((void)NULL
; 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
> eom
)
101 ptr
+= NS_INT32SZ
/*TTL*/;
102 if (ptr
+ NS_INT16SZ
> eom
)
104 NS_GET16(rdlength
, ptr
);
105 ptr
+= rdlength
/*RData*/;
110 return ((int)(ptr
- optr
));
117 ns_initparse(const u_char
*msg
, int msglen
, ns_msg
*handle
) {
118 const u_char
*eom
= msg
+ msglen
;
121 memset(handle
, 0x5e, sizeof *handle
);
124 if (msg
+ NS_INT16SZ
> eom
)
126 NS_GET16(handle
->_id
, msg
);
127 if (msg
+ NS_INT16SZ
> eom
)
129 NS_GET16(handle
->_flags
, msg
);
130 for (i
= 0; i
< ns_s_max
; i
++) {
131 if (msg
+ NS_INT16SZ
> eom
)
133 NS_GET16(handle
->_counts
[i
], msg
);
135 for (i
= 0; i
< ns_s_max
; i
++)
136 if (handle
->_counts
[i
] == 0)
137 handle
->_sections
[i
] = NULL
;
139 int b
= skiprr(msg
, eom
, (ns_sect
)i
,
144 handle
->_sections
[i
] = msg
;
149 handle
->_sect
= ns_s_max
;
151 handle
->_msg_ptr
= NULL
;
159 ns_parserr(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr
*rr
) {
162 /* Make section right. */
163 if (section
< 0 || section
>= ns_s_max
)
165 if ((int)section
!= (int)handle
->_sect
) {
166 handle
->_sect
= section
;
168 handle
->_msg_ptr
= handle
->_sections
[(int)section
];
171 /* Make rrnum right. */
173 rrnum
= handle
->_rrnum
;
174 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
176 if (rrnum
< handle
->_rrnum
) {
178 handle
->_msg_ptr
= handle
->_sections
[(int)section
];
181 b
= skiprr(handle
->_msg
, handle
->_eom
, section
,
182 rrnum
- handle
->_rrnum
);
185 handle
->_msg_ptr
+= b
;
186 handle
->_rrnum
= rrnum
;
189 b
= dn_expand(handle
->_msg
, handle
->_eom
,
190 handle
->_msg_ptr
, rr
->name
, NS_MAXDNAME
);
193 handle
->_msg_ptr
+= b
;
194 if (handle
->_msg_ptr
+ NS_INT16SZ
> handle
->_eom
)
196 NS_GET16(rr
->type
, handle
->_msg_ptr
);
197 if (handle
->_msg_ptr
+ NS_INT16SZ
> handle
->_eom
)
199 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
200 if (section
== ns_s_qd
) {
205 if (handle
->_msg_ptr
+ NS_INT32SZ
> handle
->_eom
)
207 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
208 if (handle
->_msg_ptr
+ NS_INT16SZ
> handle
->_eom
)
210 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
211 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
213 rr
->rdata
= handle
->_msg_ptr
;
214 handle
->_msg_ptr
+= rr
->rdlength
;