revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / dhcp / minires / ns_parse.c
blob6e6247e72d9f2eea5976a0c33289c9257fcf3ea0
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 #if !defined(lint) && !defined(__AROS__)
25 static const char rcsid[] = "$Id$";
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;
144 /* Make section right. */
145 if (section < 0 || section >= ns_s_max)
146 return ISC_R_NOTIMPLEMENTED;
147 if (section != handle->_sect)
148 setsection(handle, section);
150 /* Make rrnum right. */
151 if (rrnum == -1)
152 rrnum = handle->_rrnum;
153 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
154 return ISC_R_UNKNOWNATTRIBUTE;
155 if (rrnum < handle->_rrnum)
156 setsection(handle, section);
157 if (rrnum > handle->_rrnum) {
158 status = ns_skiprr(handle->_ptr, handle->_eom, section,
159 rrnum - handle->_rrnum, &b);
161 if (status != ISC_R_SUCCESS)
162 return status;
163 handle->_ptr += b;
164 handle->_rrnum = rrnum;
167 /* Do the parse. */
168 b = dn_expand(handle->_msg, handle->_eom,
169 handle->_ptr, rr->name, NS_MAXDNAME);
170 if (b < 0)
171 return ISC_R_FORMERR;
172 handle->_ptr += b;
173 if (handle->_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
174 return ISC_R_INCOMPLETE;
175 rr->type = getUShort (handle->_ptr);
176 handle -> _ptr += 2;
177 rr->rr_class = getUShort (handle->_ptr);
178 handle -> _ptr += 2;
179 if (section == ns_s_qd) {
180 rr->ttl = 0;
181 rr->rdlength = 0;
182 rr->rdata = NULL;
183 } else {
184 if (handle->_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
185 return ISC_R_INCOMPLETE;
186 rr->ttl = getULong (handle->_ptr);
187 handle -> _ptr += 4;
188 rr->rdlength = getUShort (handle->_ptr);
189 handle -> _ptr += 2;
190 if (handle->_ptr + rr->rdlength > handle->_eom)
191 return ISC_R_INCOMPLETE;
192 rr->rdata = handle->_ptr;
193 handle->_ptr += rr->rdlength;
195 if (++handle->_rrnum > handle->_counts[(int)section])
196 setsection(handle, (ns_sect)((int)section + 1));
198 /* All done. */
199 return ISC_R_SUCCESS;
202 /* Private. */
204 static void
205 setsection(ns_msg *msg, ns_sect sect) {
206 msg->_sect = sect;
207 if (sect == ns_s_max) {
208 msg->_rrnum = -1;
209 msg->_ptr = NULL;
210 } else {
211 msg->_rrnum = 0;
212 msg->_ptr = msg->_sections[(int)sect];