2 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials are licensed and made available under
4 the terms and conditions of the BSD License that accompanies this distribution.
5 The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php.
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 * Portions copyright (c) 1999, 2000
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
29 * This product includes software developed by Intel Corporation and
32 * 4. Neither the name of Intel Corporation or its contributors may be
33 * used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
37 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
40 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
46 * THE POSSIBILITY OF SUCH DAMAGE.
48 * Copyright (c) 1996 by Internet Software Consortium.
50 * Permission to use, copy, modify, and distribute this software for any
51 * purpose with or without fee is hereby granted, provided that the above
52 * copyright notice and this permission notice appear in all copies.
54 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
55 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
57 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
58 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
59 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
60 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64 #include <sys/types.h>
66 #include <netinet/in.h>
67 #include <arpa/nameser.h>
73 /* These need to be in the same order as the nres.h:ns_flag enum. */
74 struct _ns_flagdata _ns_flagdata
[16] = {
75 { 0x8000, 15 }, /* qr. */
76 { 0x7800, 11 }, /* opcode. */
77 { 0x0400, 10 }, /* aa. */
78 { 0x0200, 9 }, /* tc. */
79 { 0x0100, 8 }, /* rd. */
80 { 0x0080, 7 }, /* ra. */
81 { 0x0040, 6 }, /* z. */
82 { 0x0020, 5 }, /* ad. */
83 { 0x0010, 4 }, /* cd. */
84 { 0x000f, 0 }, /* rcode. */
85 { 0x0000, 0 }, /* expansion (1/6). */
86 { 0x0000, 0 }, /* expansion (2/6). */
87 { 0x0000, 0 }, /* expansion (3/6). */
88 { 0x0000, 0 }, /* expansion (4/6). */
89 { 0x0000, 0 }, /* expansion (5/6). */
90 { 0x0000, 0 }, /* expansion (6/6). */
94 skiprr(const u_char
*ptr
, const u_char
*eom
, ns_sect section
, int count
) {
95 const u_char
*optr
= ptr
;
97 for ((void)NULL
; count
> 0; count
--) {
100 b
= dn_skipname(ptr
, eom
);
103 ptr
+= b
/*Name*/ + NS_INT16SZ
/*Type*/ + NS_INT16SZ
/*Class*/;
104 if (section
!= ns_s_qd
) {
105 if (ptr
+ NS_INT32SZ
> eom
)
107 ptr
+= NS_INT32SZ
/*TTL*/;
108 if (ptr
+ NS_INT16SZ
> eom
)
110 NS_GET16(rdlength
, ptr
);
111 ptr
+= rdlength
/*RData*/;
116 return ((int)(ptr
- optr
));
123 ns_initparse(const u_char
*msg
, int msglen
, ns_msg
*handle
) {
124 const u_char
*eom
= msg
+ msglen
;
127 memset(handle
, 0x5e, sizeof *handle
);
130 if (msg
+ NS_INT16SZ
> eom
)
132 NS_GET16(handle
->_id
, msg
);
133 if (msg
+ NS_INT16SZ
> eom
)
135 NS_GET16(handle
->_flags
, msg
);
136 for (i
= 0; i
< ns_s_max
; i
++) {
137 if (msg
+ NS_INT16SZ
> eom
)
139 NS_GET16(handle
->_counts
[i
], msg
);
141 for (i
= 0; i
< ns_s_max
; i
++)
142 if (handle
->_counts
[i
] == 0)
143 handle
->_sections
[i
] = NULL
;
145 int b
= skiprr(msg
, eom
, (ns_sect
)i
,
150 handle
->_sections
[i
] = msg
;
155 handle
->_sect
= ns_s_max
;
157 handle
->_msg_ptr
= NULL
;
165 ns_parserr(ns_msg
*handle
, ns_sect section
, int rrnum
, ns_rr
*rr
) {
168 /* Make section right. */
169 if ((unsigned int)section
>= ns_s_max
)
171 if ((int)section
!= (int)handle
->_sect
) {
172 handle
->_sect
= section
;
174 handle
->_msg_ptr
= handle
->_sections
[(int)section
];
177 /* Make rrnum right. */
179 rrnum
= handle
->_rrnum
;
180 if (rrnum
< 0 || rrnum
>= handle
->_counts
[(int)section
])
182 if (rrnum
< handle
->_rrnum
) {
184 handle
->_msg_ptr
= handle
->_sections
[(int)section
];
187 b
= skiprr(handle
->_msg
, handle
->_eom
, section
,
188 rrnum
- handle
->_rrnum
);
191 handle
->_msg_ptr
+= b
;
192 handle
->_rrnum
= rrnum
;
195 b
= dn_expand(handle
->_msg
, handle
->_eom
,
196 handle
->_msg_ptr
, rr
->name
, NS_MAXDNAME
);
199 handle
->_msg_ptr
+= b
;
200 if (handle
->_msg_ptr
+ NS_INT16SZ
> handle
->_eom
)
202 NS_GET16(rr
->type
, handle
->_msg_ptr
);
203 if (handle
->_msg_ptr
+ NS_INT16SZ
> handle
->_eom
)
205 NS_GET16(rr
->rr_class
, handle
->_msg_ptr
);
206 if (section
== ns_s_qd
) {
211 if (handle
->_msg_ptr
+ NS_INT32SZ
> handle
->_eom
)
213 NS_GET32(rr
->ttl
, handle
->_msg_ptr
);
214 if (handle
->_msg_ptr
+ NS_INT16SZ
> handle
->_eom
)
216 NS_GET16(rr
->rdlength
, handle
->_msg_ptr
);
217 if (handle
->_msg_ptr
+ rr
->rdlength
> handle
->_eom
)
219 rr
->rdata
= handle
->_msg_ptr
;
220 handle
->_msg_ptr
+= rr
->rdlength
;