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
22 #include <sys/types.h>
24 #ifdef HAVE_NETINET_IN_H
25 # include <netinet/in.h>
27 #ifdef HAVE_ARPA_NAMESER_H
28 # include <arpa/nameser.h>
39 static void setsection(ns_msg *msg, ns_sect sect);
43 #define RETERR(err) do { return (-1); } while (0)
45 #ifdef HAVE_NS_MSG__MSG_PTR
46 # define NS_PTR(ns_msg) ((ns_msg)->_msg_ptr)
48 # define NS_PTR(ns_msg) ((ns_msg)->_ptr)
54 dns_ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
55 const u_char *optr = ptr;
57 for ((void)NULL; count > 0; count--) {
60 b = dn_skipname(ptr, eom);
63 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
64 if (section != ns_s_qd) {
65 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
67 ptr += NS_INT32SZ/*TTL*/;
68 NS_GET16(rdlength, ptr);
69 ptr += rdlength/*RData*/;
78 dns_ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
79 const u_char *eom = msg + msglen;
82 memset(handle, 0x5e, sizeof *handle);
85 if (msg + NS_INT16SZ > eom)
87 NS_GET16(handle->_id, msg);
88 if (msg + NS_INT16SZ > eom)
90 NS_GET16(handle->_flags, msg);
91 for (i = 0; i < ns_s_max; i++) {
92 if (msg + NS_INT16SZ > eom)
94 NS_GET16(handle->_counts[i], msg);
96 for (i = 0; i < ns_s_max; i++)
97 if (handle->_counts[i] == 0)
98 handle->_sections[i] = NULL;
100 int b = dns_ns_skiprr(msg, eom, (ns_sect)i,
105 handle->_sections[i] = msg;
110 setsection(handle, ns_s_max);
115 dns_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
118 /* Make section right. */
119 if (section < 0 || section >= ns_s_max)
121 if (section != handle->_sect)
122 setsection(handle, section);
124 /* Make rrnum right. */
126 rrnum = handle->_rrnum;
127 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
129 if (rrnum < handle->_rrnum)
130 setsection(handle, section);
131 if (rrnum > handle->_rrnum) {
132 b = dns_ns_skiprr(NS_PTR(handle), handle->_eom, section,
133 rrnum - handle->_rrnum);
138 handle->_rrnum = rrnum;
142 b = dn_expand(handle->_msg, handle->_eom,
143 NS_PTR(handle), rr->name, NS_MAXDNAME);
147 if (NS_PTR(handle) + NS_INT16SZ + NS_INT16SZ > handle->_eom)
149 NS_GET16(rr->type, NS_PTR(handle));
150 NS_GET16(rr->rr_class, NS_PTR(handle));
151 if (section == ns_s_qd) {
156 if (NS_PTR(handle) + NS_INT32SZ + NS_INT16SZ > handle->_eom)
158 NS_GET32(rr->ttl, NS_PTR(handle));
159 NS_GET16(rr->rdlength, NS_PTR(handle));
160 if (NS_PTR(handle) + rr->rdlength > handle->_eom)
162 rr->rdata = NS_PTR(handle);
163 NS_PTR(handle) += rr->rdlength;
165 if (++handle->_rrnum > handle->_counts[(int)section])
166 setsection(handle, (ns_sect)((int)section + 1));
175 setsection(ns_msg *msg, ns_sect sect) {
177 if (sect == ns_s_max) {
182 NS_PTR(msg) = msg->_sections[(int)sect];