2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 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.
19 static const char rcsid[] = "$Id: ns_parse.c,v 1.5.18.4 2007/08/27 03:34:24 marka Exp $";
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
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)
54 /* These need to be in the same order as the nres.h:ns_flag enum. */
55 struct _ns_flagdata _ns_flagdata[16] = {
56 { 0x8000, 15 }, /*%< qr. */
57 { 0x7800, 11 }, /*%< opcode. */
58 { 0x0400, 10 }, /*%< aa. */
59 { 0x0200, 9 }, /*%< tc. */
60 { 0x0100, 8 }, /*%< rd. */
61 { 0x0080, 7 }, /*%< ra. */
62 { 0x0040, 6 }, /*%< z. */
63 { 0x0020, 5 }, /*%< ad. */
64 { 0x0010, 4 }, /*%< cd. */
65 { 0x000f, 0 }, /*%< rcode. */
66 { 0x0000, 0 }, /*%< expansion (1/6). */
67 { 0x0000, 0 }, /*%< expansion (2/6). */
68 { 0x0000, 0 }, /*%< expansion (3/6). */
69 { 0x0000, 0 }, /*%< expansion (4/6). */
70 { 0x0000, 0 }, /*%< expansion (5/6). */
71 { 0x0000, 0 }, /*%< expansion (6/6). */
74 int ns_msg_getflag(ns_msg handle, int flag) {
75 return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
79 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
80 const u_char *optr = ptr;
82 for ((void)NULL; count > 0; count--) {
85 b = dn_skipname(ptr, eom);
88 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
89 if (section != ns_s_qd) {
90 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
92 ptr += NS_INT32SZ/*TTL*/;
93 NS_GET16(rdlength, ptr);
94 ptr += rdlength/*RData*/;
103 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
104 const u_char *eom = msg + msglen;
107 memset(handle, 0x5e, sizeof *handle);
110 if (msg + NS_INT16SZ > eom)
112 NS_GET16(handle->_id, msg);
113 if (msg + NS_INT16SZ > eom)
115 NS_GET16(handle->_flags, msg);
116 for (i = 0; i < ns_s_max; i++) {
117 if (msg + NS_INT16SZ > eom)
119 NS_GET16(handle->_counts[i], msg);
121 for (i = 0; i < ns_s_max; i++)
122 if (handle->_counts[i] == 0)
123 handle->_sections[i] = NULL;
125 int b = ns_skiprr(msg, eom, (ns_sect)i,
130 handle->_sections[i] = msg;
135 setsection(handle, ns_s_max);
140 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
144 /* Make section right. */
146 if (tmp < 0 || section >= ns_s_max)
148 if (section != handle->_sect)
149 setsection(handle, section);
151 /* Make rrnum right. */
153 rrnum = handle->_rrnum;
154 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
156 if (rrnum < handle->_rrnum)
157 setsection(handle, section);
158 if (rrnum > handle->_rrnum) {
159 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
160 rrnum - handle->_rrnum);
164 handle->_msg_ptr += b;
165 handle->_rrnum = rrnum;
169 b = dn_expand(handle->_msg, handle->_eom,
170 handle->_msg_ptr, rr->name, NS_MAXDNAME);
173 handle->_msg_ptr += b;
174 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
176 NS_GET16(rr->type, handle->_msg_ptr);
177 NS_GET16(rr->rr_class, handle->_msg_ptr);
178 if (section == ns_s_qd) {
183 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
185 NS_GET32(rr->ttl, handle->_msg_ptr);
186 NS_GET16(rr->rdlength, handle->_msg_ptr);
187 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
189 rr->rdata = handle->_msg_ptr;
190 handle->_msg_ptr += rr->rdlength;
192 if (++handle->_rrnum > handle->_counts[(int)section])
193 setsection(handle, (ns_sect)((int)section + 1));
202 setsection(ns_msg *msg, ns_sect sect) {
204 if (sect == ns_s_max) {
206 msg->_msg_ptr = NULL;
209 msg->_msg_ptr = msg->_sections[(int)sect];