2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
28 #include "port_before.h"
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
39 #include "port_after.h"
43 static void setsection(ns_msg *msg, ns_sect sect);
47 #if !defined(SOLARIS2) || defined(__COVERITY__)
48 #define RETERR(err) do { errno = (err); return (-1); } while (0)
51 do { errno = (err); if (errno == errno) return (-1); } while (0)
54 #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
55 #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
59 /* These need to be in the same order as the nres.h:ns_flag enum. */
60 struct _ns_flagdata _ns_flagdata[16] = {
61 { 0x8000, 15 }, /*%< qr. */
62 { 0x7800, 11 }, /*%< opcode. */
63 { 0x0400, 10 }, /*%< aa. */
64 { 0x0200, 9 }, /*%< tc. */
65 { 0x0100, 8 }, /*%< rd. */
66 { 0x0080, 7 }, /*%< ra. */
67 { 0x0040, 6 }, /*%< z. */
68 { 0x0020, 5 }, /*%< ad. */
69 { 0x0010, 4 }, /*%< cd. */
70 { 0x000f, 0 }, /*%< rcode. */
71 { 0x0000, 0 }, /*%< expansion (1/6). */
72 { 0x0000, 0 }, /*%< expansion (2/6). */
73 { 0x0000, 0 }, /*%< expansion (3/6). */
74 { 0x0000, 0 }, /*%< expansion (4/6). */
75 { 0x0000, 0 }, /*%< expansion (5/6). */
76 { 0x0000, 0 }, /*%< expansion (6/6). */
79 int ns_msg_getflag(ns_msg handle, int flag) {
80 return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
84 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
85 const u_char *optr = ptr;
87 for ((void)NULL; count > 0; count--) {
90 b = dn_skipname(ptr, eom);
93 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
94 if (section != ns_s_qd) {
95 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
97 ptr += NS_INT32SZ/*TTL*/;
98 NS_GET16(rdlength, ptr);
99 ptr += rdlength/*RData*/;
108 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
109 const u_char *eom = msg + msglen;
114 if (msg + NS_INT16SZ > eom)
116 NS_GET16(handle->_id, msg);
117 if (msg + NS_INT16SZ > eom)
119 NS_GET16(handle->_flags, msg);
120 for (i = 0; i < ns_s_max; i++) {
121 if (msg + NS_INT16SZ > eom)
123 NS_GET16(handle->_counts[i], msg);
125 for (i = 0; i < ns_s_max; i++)
126 if (handle->_counts[i] == 0)
127 handle->_sections[i] = NULL;
129 int b = ns_skiprr(msg, eom, (ns_sect)i,
134 handle->_sections[i] = msg;
139 setsection(handle, ns_s_max);
144 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
148 /* Make section right. */
150 if (tmp < 0 || section >= ns_s_max)
152 if (section != handle->_sect)
153 setsection(handle, section);
155 /* Make rrnum right. */
157 rrnum = handle->_rrnum;
158 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
160 if (rrnum < handle->_rrnum)
161 setsection(handle, section);
162 if (rrnum > handle->_rrnum) {
163 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
164 rrnum - handle->_rrnum);
168 handle->_msg_ptr += b;
169 handle->_rrnum = rrnum;
173 b = dn_expand(handle->_msg, handle->_eom,
174 handle->_msg_ptr, rr->name, NS_MAXDNAME);
177 handle->_msg_ptr += b;
178 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
180 NS_GET16(rr->type, handle->_msg_ptr);
181 NS_GET16(rr->rr_class, handle->_msg_ptr);
182 if (section == ns_s_qd) {
187 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
189 NS_GET32(rr->ttl, handle->_msg_ptr);
190 NS_GET16(rr->rdlength, handle->_msg_ptr);
191 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
193 rr->rdata = handle->_msg_ptr;
194 handle->_msg_ptr += rr->rdlength;
196 if (++handle->_rrnum > handle->_counts[(int)section])
197 setsection(handle, (ns_sect)((int)section + 1));
204 * This is identical to the above but uses network-format (uncompressed) names.
207 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
211 /* Make section right. */
212 if ((tmp = section) < 0 || section >= ns_s_max)
214 if (section != handle->_sect)
215 setsection(handle, section);
217 /* Make rrnum right. */
219 rrnum = handle->_rrnum;
220 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
222 if (rrnum < handle->_rrnum)
223 setsection(handle, section);
224 if (rrnum > handle->_rrnum) {
225 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
226 rrnum - handle->_rrnum);
230 handle->_msg_ptr += b;
231 handle->_rrnum = rrnum;
235 b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
236 rr->nname, NS_MAXNNAME, &rr->nnamel);
239 handle->_msg_ptr += b;
240 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
242 NS_GET16(rr->type, handle->_msg_ptr);
243 NS_GET16(rr->rr_class, handle->_msg_ptr);
244 if (section == ns_s_qd) {
249 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
251 NS_GET32(rr->ttl, handle->_msg_ptr);
252 NS_GET16(rr->rdlength, handle->_msg_ptr);
253 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
255 rr->rdata = handle->_msg_ptr;
256 handle->_msg_ptr += rr->rdlength;
258 if (++handle->_rrnum > handle->_counts[(int)section])
259 setsection(handle, (ns_sect)((int)section + 1));
268 setsection(ns_msg *msg, ns_sect sect) {
270 if (sect == ns_s_max) {
272 msg->_msg_ptr = NULL;
275 msg->_msg_ptr = msg->_sections[(int)sect];