2 * Copyright (c) 1996 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
17 * $FreeBSD: src/lib/libc/net/ns_parse.c,v 1.3 1999/08/28 00:00:14 peter Exp $
18 * $DragonFly: src/lib/libcr/net/Attic/ns_parse.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
21 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
30 /* These need to be in the same order as the nres.h:ns_flag enum. */
31 struct _ns_flagdata _ns_flagdata[16] = {
32 { 0x8000, 15 }, /* qr. */
33 { 0x7800, 11 }, /* opcode. */
34 { 0x0400, 10 }, /* aa. */
35 { 0x0200, 9 }, /* tc. */
36 { 0x0100, 8 }, /* rd. */
37 { 0x0080, 7 }, /* ra. */
38 { 0x0040, 6 }, /* z. */
39 { 0x0020, 5 }, /* ad. */
40 { 0x0010, 4 }, /* cd. */
41 { 0x000f, 0 }, /* rcode. */
42 { 0x0000, 0 }, /* expansion (1/6). */
43 { 0x0000, 0 }, /* expansion (2/6). */
44 { 0x0000, 0 }, /* expansion (3/6). */
45 { 0x0000, 0 }, /* expansion (4/6). */
46 { 0x0000, 0 }, /* expansion (5/6). */
47 { 0x0000, 0 }, /* expansion (6/6). */
51 skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
52 const u_char *optr = ptr;
54 for ((void)NULL; count > 0; count--) {
57 b = dn_skipname(ptr, eom);
60 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
61 if (section != ns_s_qd) {
62 if (ptr + NS_INT32SZ > eom)
64 ptr += NS_INT32SZ/*TTL*/;
65 if (ptr + NS_INT16SZ > eom)
67 NS_GET16(rdlength, ptr);
68 ptr += rdlength/*RData*/;
80 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
81 const u_char *eom = msg + msglen;
84 memset(handle, 0x5e, sizeof *handle);
87 if (msg + NS_INT16SZ > eom)
89 NS_GET16(handle->_id, msg);
90 if (msg + NS_INT16SZ > eom)
92 NS_GET16(handle->_flags, msg);
93 for (i = 0; i < ns_s_max; i++) {
94 if (msg + NS_INT16SZ > eom)
96 NS_GET16(handle->_counts[i], msg);
98 for (i = 0; i < ns_s_max; i++)
99 if (handle->_counts[i] == 0)
100 handle->_sections[i] = NULL;
102 int b = skiprr(msg, eom, (ns_sect)i,
107 handle->_sections[i] = msg;
112 handle->_sect = ns_s_max;
122 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
125 /* Make section right. */
126 if (section < 0 || section >= ns_s_max)
128 if ((int)section != (int)handle->_sect) {
129 handle->_sect = section;
131 handle->_ptr = handle->_sections[(int)section];
134 /* Make rrnum right. */
136 rrnum = handle->_rrnum;
137 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
139 if (rrnum < handle->_rrnum) {
141 handle->_ptr = handle->_sections[(int)section];
144 b = skiprr(handle->_msg, handle->_eom, section,
145 rrnum - handle->_rrnum);
149 handle->_rrnum = rrnum;
152 b = dn_expand(handle->_msg, handle->_eom,
153 handle->_ptr, rr->name, NS_MAXDNAME);
157 if (handle->_ptr + NS_INT16SZ > handle->_eom)
159 NS_GET16(rr->type, handle->_ptr);
160 if (handle->_ptr + NS_INT16SZ > handle->_eom)
162 NS_GET16(rr->rr_class, handle->_ptr);
163 if (section == ns_s_qd) {
168 if (handle->_ptr + NS_INT32SZ > handle->_eom)
170 NS_GET32(rr->ttl, handle->_ptr);
171 if (handle->_ptr + NS_INT16SZ > handle->_eom)
173 NS_GET16(rr->rdlength, handle->_ptr);
174 if (handle->_ptr + rr->rdlength > handle->_eom)
176 rr->rdata = handle->_ptr;
177 handle->_ptr += rr->rdlength;