Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / dhcp / minires / ns_parse.c
blob328ee6de366d1a327d00b154e287e13dedaae775
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-2003 by Internet Software Consortium
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Internet Systems Consortium, Inc.
18 * 950 Charter Street
19 * Redwood City, CA 94063
20 * <info@isc.org>
21 * http://www.isc.org/
24 #ifndef lint
25 static const char rcsid[] = "$Id: ns_parse.c,v 1.6 2005/08/11 17:13:26 drochner Exp $";
26 #endif
28 /* Import. */
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <sys/socket.h>
35 #include <errno.h>
36 #include <string.h>
38 #include "minires/minires.h"
39 #include "arpa/nameser.h"
41 /* Forward. */
43 static void setsection(ns_msg *msg, ns_sect sect);
45 /* Macros. */
47 /* Public. */
49 /* These need to be in the same order as the nres.h:ns_flag enum. */
50 struct _ns_flagdata _ns_flagdata[16] = {
51 { 0x8000, 15 }, /* qr. */
52 { 0x7800, 11 }, /* opcode. */
53 { 0x0400, 10 }, /* aa. */
54 { 0x0200, 9 }, /* tc. */
55 { 0x0100, 8 }, /* rd. */
56 { 0x0080, 7 }, /* ra. */
57 { 0x0040, 6 }, /* z. */
58 { 0x0020, 5 }, /* ad. */
59 { 0x0010, 4 }, /* cd. */
60 { 0x000f, 0 }, /* rcode. */
61 { 0x0000, 0 }, /* expansion (1/6). */
62 { 0x0000, 0 }, /* expansion (2/6). */
63 { 0x0000, 0 }, /* expansion (3/6). */
64 { 0x0000, 0 }, /* expansion (4/6). */
65 { 0x0000, 0 }, /* expansion (5/6). */
66 { 0x0000, 0 }, /* expansion (6/6). */
69 isc_result_t
70 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count,
71 int *rc) {
72 const u_char *optr = ptr;
74 for ((void)NULL; count > 0; count--) {
75 int b, rdlength;
77 b = dn_skipname(ptr, eom);
78 if (b < 0)
79 return ISC_R_INCOMPLETE;
80 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
81 if (section != ns_s_qd) {
82 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
83 return ISC_R_INCOMPLETE;
84 ptr += NS_INT32SZ/*TTL*/;
85 rdlength = getUShort(ptr);
86 ptr += 2;
87 ptr += rdlength/*RData*/;
90 if (ptr > eom)
91 return ISC_R_INCOMPLETE;
92 if (rc)
93 *rc = ptr - optr;
94 return ISC_R_SUCCESS;
97 isc_result_t
98 ns_initparse(const u_char *msg, unsigned msglen, ns_msg *handle) {
99 const u_char *eom = msg + msglen;
100 int i;
102 memset(handle, 0x5e, sizeof *handle);
103 handle->_msg = msg;
104 handle->_eom = eom;
105 if (msg + NS_INT16SZ > eom)
106 return ISC_R_INCOMPLETE;
107 handle->_id = getUShort (msg);
108 msg += 2;
109 if (msg + NS_INT16SZ > eom)
110 return ISC_R_INCOMPLETE;
111 handle->_flags = getUShort (msg);
112 msg += 2;
113 for (i = 0; i < ns_s_max; i++) {
114 if (msg + NS_INT16SZ > eom)
115 return ISC_R_INCOMPLETE;
116 handle->_counts[i] = getUShort (msg);
117 msg += 2;
119 for (i = 0; i < ns_s_max; i++)
120 if (handle->_counts[i] == 0)
121 handle->_sections[i] = NULL;
122 else {
123 int b;
124 isc_result_t status =
125 ns_skiprr(msg, eom, (ns_sect)i,
126 handle->_counts[i], &b);
128 if (status != ISC_R_SUCCESS)
129 return STATUS;
130 handle->_sections[i] = msg;
131 msg += b;
133 if (msg != eom)
134 return ISC_R_INCOMPLETE;
135 setsection(handle, ns_s_max);
136 return ISC_R_SUCCESS;
139 isc_result_t
140 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
141 int b;
142 isc_result_t status;
143 int tmp; /* XXX used to force a signed comparison below */
145 /* Make section right. */
146 if ((tmp = section) < 0 || section >= ns_s_max)
147 return ISC_R_NOTIMPLEMENTED;
148 if (section != handle->_sect)
149 setsection(handle, section);
151 /* Make rrnum right. */
152 if (rrnum == -1)
153 rrnum = handle->_rrnum;
154 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
155 return ISC_R_UNKNOWNATTRIBUTE;
156 if (rrnum < handle->_rrnum)
157 setsection(handle, section);
158 if (rrnum > handle->_rrnum) {
159 status = ns_skiprr(handle->_ptr, handle->_eom, section,
160 rrnum - handle->_rrnum, &b);
162 if (status != ISC_R_SUCCESS)
163 return status;
164 handle->_ptr += b;
165 handle->_rrnum = rrnum;
168 /* Do the parse. */
169 b = dn_expand(handle->_msg, handle->_eom,
170 handle->_ptr, rr->name, NS_MAXDNAME);
171 if (b < 0)
172 return ISC_R_FORMERR;
173 handle->_ptr += b;
174 if (handle->_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
175 return ISC_R_INCOMPLETE;
176 rr->type = getUShort (handle->_ptr);
177 handle -> _ptr += 2;
178 rr->rr_class = getUShort (handle->_ptr);
179 handle -> _ptr += 2;
180 if (section == ns_s_qd) {
181 rr->ttl = 0;
182 rr->rdlength = 0;
183 rr->rdata = NULL;
184 } else {
185 if (handle->_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
186 return ISC_R_INCOMPLETE;
187 rr->ttl = getULong (handle->_ptr);
188 handle -> _ptr += 4;
189 rr->rdlength = getUShort (handle->_ptr);
190 handle -> _ptr += 2;
191 if (handle->_ptr + rr->rdlength > handle->_eom)
192 return ISC_R_INCOMPLETE;
193 rr->rdata = handle->_ptr;
194 handle->_ptr += rr->rdlength;
196 if (++handle->_rrnum > handle->_counts[(int)section])
197 setsection(handle, (ns_sect)((int)section + 1));
199 /* All done. */
200 return ISC_R_SUCCESS;
203 /* Private. */
205 static void
206 setsection(ns_msg *msg, ns_sect sect) {
207 msg->_sect = sect;
208 if (sect == ns_s_max) {
209 msg->_rrnum = -1;
210 msg->_ptr = NULL;
211 } else {
212 msg->_rrnum = 0;
213 msg->_ptr = msg->_sections[(int)sect];