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
19 static const char rcsid[] = "$Id: ns_parse.c,v 1.2.2.1 2002/02/19 19:16:52 mellon Exp $";
24 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <sys/socket.h>
32 #include "minires/minires.h"
33 #include "arpa/nameser.h"
37 static void setsection(ns_msg *msg, ns_sect sect);
43 /* These need to be in the same order as the nres.h:ns_flag enum. */
44 struct _ns_flagdata _ns_flagdata[16] = {
45 { 0x8000, 15 }, /* qr. */
46 { 0x7800, 11 }, /* opcode. */
47 { 0x0400, 10 }, /* aa. */
48 { 0x0200, 9 }, /* tc. */
49 { 0x0100, 8 }, /* rd. */
50 { 0x0080, 7 }, /* ra. */
51 { 0x0040, 6 }, /* z. */
52 { 0x0020, 5 }, /* ad. */
53 { 0x0010, 4 }, /* cd. */
54 { 0x000f, 0 }, /* rcode. */
55 { 0x0000, 0 }, /* expansion (1/6). */
56 { 0x0000, 0 }, /* expansion (2/6). */
57 { 0x0000, 0 }, /* expansion (3/6). */
58 { 0x0000, 0 }, /* expansion (4/6). */
59 { 0x0000, 0 }, /* expansion (5/6). */
60 { 0x0000, 0 }, /* expansion (6/6). */
64 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count,
66 const u_char *optr = ptr;
68 for ((void)NULL; count > 0; count--) {
71 b = dn_skipname(ptr, eom);
73 return ISC_R_INCOMPLETE;
74 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
75 if (section != ns_s_qd) {
76 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
77 return ISC_R_INCOMPLETE;
78 ptr += NS_INT32SZ/*TTL*/;
79 rdlength = getUShort(ptr);
81 ptr += rdlength/*RData*/;
85 return ISC_R_INCOMPLETE;
92 ns_initparse(const u_char *msg, unsigned msglen, ns_msg *handle) {
93 const u_char *eom = msg + msglen;
96 memset(handle, 0x5e, sizeof *handle);
99 if (msg + NS_INT16SZ > eom)
100 return ISC_R_INCOMPLETE;
101 handle->_id = getUShort (msg);
103 if (msg + NS_INT16SZ > eom)
104 return ISC_R_INCOMPLETE;
105 handle->_flags = getUShort (msg);
107 for (i = 0; i < ns_s_max; i++) {
108 if (msg + NS_INT16SZ > eom)
109 return ISC_R_INCOMPLETE;
110 handle->_counts[i] = getUShort (msg);
113 for (i = 0; i < ns_s_max; i++)
114 if (handle->_counts[i] == 0)
115 handle->_sections[i] = NULL;
118 isc_result_t status =
119 ns_skiprr(msg, eom, (ns_sect)i,
120 handle->_counts[i], &b);
122 if (status != ISC_R_SUCCESS)
124 handle->_sections[i] = msg;
128 return ISC_R_INCOMPLETE;
129 setsection(handle, ns_s_max);
130 return ISC_R_SUCCESS;
134 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
138 /* Make section right. */
139 if (section < 0 || section >= ns_s_max)
140 return ISC_R_NOTIMPLEMENTED;
141 if (section != handle->_sect)
142 setsection(handle, section);
144 /* Make rrnum right. */
146 rrnum = handle->_rrnum;
147 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
148 return ISC_R_UNKNOWNATTRIBUTE;
149 if (rrnum < handle->_rrnum)
150 setsection(handle, section);
151 if (rrnum > handle->_rrnum) {
152 status = ns_skiprr(handle->_ptr, handle->_eom, section,
153 rrnum - handle->_rrnum, &b);
155 if (status != ISC_R_SUCCESS)
158 handle->_rrnum = rrnum;
162 b = dn_expand(handle->_msg, handle->_eom,
163 handle->_ptr, rr->name, NS_MAXDNAME);
165 return ISC_R_FORMERR;
167 if (handle->_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
168 return ISC_R_INCOMPLETE;
169 rr->type = getUShort (handle->_ptr);
171 rr->rr_class = getUShort (handle->_ptr);
173 if (section == ns_s_qd) {
178 if (handle->_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
179 return ISC_R_INCOMPLETE;
180 rr->ttl = getULong (handle->_ptr);
182 rr->rdlength = getUShort (handle->_ptr);
184 if (handle->_ptr + rr->rdlength > handle->_eom)
185 return ISC_R_INCOMPLETE;
186 rr->rdata = handle->_ptr;
187 handle->_ptr += rr->rdlength;
189 if (++handle->_rrnum > handle->_counts[(int)section])
190 setsection(handle, (ns_sect)((int)section + 1));
193 return ISC_R_SUCCESS;
199 setsection(ns_msg *msg, ns_sect sect) {
201 if (sect == ns_s_max) {
206 msg->_ptr = msg->_sections[(int)sect];