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
18 #if !defined(_LIBC) && !defined(lint)
19 static const char rcsid[] = "$BINDId: ns_parse.c,v 8.13 1999/10/13 16:39:35 vixie Exp $";
24 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
35 static void setsection(ns_msg *msg, ns_sect sect);
39 #define RETERR(err) do { __set_errno (err); return (-1); } while (0)
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) {
65 const u_char *optr = ptr;
67 for ((void)NULL; count > 0; count--) {
70 b = dn_skipname(ptr, eom);
73 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
74 if (section != ns_s_qd) {
75 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
77 ptr += NS_INT32SZ/*TTL*/;
78 NS_GET16(rdlength, ptr);
79 ptr += rdlength/*RData*/;
88 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
89 const u_char *eom = msg + msglen;
92 memset(handle, 0x5e, sizeof *handle);
95 if (msg + NS_INT16SZ > eom)
97 NS_GET16(handle->_id, msg);
98 if (msg + NS_INT16SZ > eom)
100 NS_GET16(handle->_flags, msg);
101 for (i = 0; i < ns_s_max; i++) {
102 if (msg + NS_INT16SZ > eom)
104 NS_GET16(handle->_counts[i], msg);
106 for (i = 0; i < ns_s_max; i++)
107 if (handle->_counts[i] == 0)
108 handle->_sections[i] = NULL;
110 int b = ns_skiprr(msg, eom, (ns_sect)i,
115 handle->_sections[i] = msg;
120 setsection(handle, ns_s_max);
125 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
128 /* Make section right. */
129 if (section < 0 || section >= ns_s_max)
131 if (section != handle->_sect)
132 setsection(handle, section);
134 /* Make rrnum right. */
136 rrnum = handle->_rrnum;
137 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
139 if (rrnum < handle->_rrnum)
140 setsection(handle, section);
141 if (rrnum > handle->_rrnum) {
142 b = ns_skiprr(handle->_ptr, handle->_eom, section,
143 rrnum - handle->_rrnum);
148 handle->_rrnum = rrnum;
152 b = dn_expand(handle->_msg, handle->_eom,
153 handle->_ptr, rr->name, NS_MAXDNAME);
157 if (handle->_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
159 NS_GET16(rr->type, handle->_ptr);
160 NS_GET16(rr->rr_class, handle->_ptr);
161 if (section == ns_s_qd) {
166 if (handle->_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
168 NS_GET32(rr->ttl, handle->_ptr);
169 NS_GET16(rr->rdlength, handle->_ptr);
170 if (handle->_ptr + rr->rdlength > handle->_eom)
172 rr->rdata = handle->_ptr;
173 handle->_ptr += rr->rdlength;
175 if (++handle->_rrnum > handle->_counts[(int)section])
176 setsection(handle, (ns_sect)((int)section + 1));
185 setsection(ns_msg *msg, ns_sect sect) {
187 if (sect == ns_s_max) {
192 msg->_ptr = msg->_sections[(int)sect];