Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / minires / ns_parse.c
1 /*
2  * Copyright (c) 1996,1999 by Internet Software Consortium.
3  *
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.
7  *
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
15  * SOFTWARE.
16  */
17
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_parse.c,v 1.2.2.1 2002/02/19 19:16:52 mellon Exp $";
20 #endif
21
22 /* Import. */
23
24 #include <sys/types.h>
25
26 #include <netinet/in.h>
27 #include <sys/socket.h>
28
29 #include <errno.h>
30 #include <string.h>
31
32 #include "minires/minires.h"
33 #include "arpa/nameser.h"
34
35 /* Forward. */
36
37 static void     setsection(ns_msg *msg, ns_sect sect);
38
39 /* Macros. */
40
41 /* Public. */
42
43 /* These need to be in the same order as the nres.h:ns_flag enum. */
44 struct _ns_flagdata _ns_flagdata[16] = {
45         { 0x8000, 15 },         /* qr. */
46         { 0x7800, 11 },         /* opcode. */
47         { 0x0400, 10 },         /* aa. */
48         { 0x0200, 9 },          /* tc. */
49         { 0x0100, 8 },          /* rd. */
50         { 0x0080, 7 },          /* ra. */
51         { 0x0040, 6 },          /* z. */
52         { 0x0020, 5 },          /* ad. */
53         { 0x0010, 4 },          /* cd. */
54         { 0x000f, 0 },          /* rcode. */
55         { 0x0000, 0 },          /* expansion (1/6). */
56         { 0x0000, 0 },          /* expansion (2/6). */
57         { 0x0000, 0 },          /* expansion (3/6). */
58         { 0x0000, 0 },          /* expansion (4/6). */
59         { 0x0000, 0 },          /* expansion (5/6). */
60         { 0x0000, 0 },          /* expansion (6/6). */
61 };
62
63 isc_result_t
64 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count,
65           int *rc) {
66         const u_char *optr = ptr;
67
68         for ((void)NULL; count > 0; count--) {
69                 int b, rdlength;
70
71                 b = dn_skipname(ptr, eom);
72                 if (b < 0)
73                         return ISC_R_INCOMPLETE;
74                 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
75                 if (section != ns_s_qd) {
76                         if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
77                                 return ISC_R_INCOMPLETE;
78                         ptr += NS_INT32SZ/*TTL*/;
79                         rdlength = getUShort(ptr);
80                         ptr += 2;
81                         ptr += rdlength/*RData*/;
82                 }
83         }
84         if (ptr > eom)
85                 return ISC_R_INCOMPLETE;
86         if (rc)
87                 *rc = ptr - optr;
88         return ISC_R_SUCCESS;
89 }
90
91 isc_result_t
92 ns_initparse(const u_char *msg, unsigned msglen, ns_msg *handle) {
93         const u_char *eom = msg + msglen;
94         int i;
95
96         memset(handle, 0x5e, sizeof *handle);
97         handle->_msg = msg;
98         handle->_eom = eom;
99         if (msg + NS_INT16SZ > eom)
100                 return ISC_R_INCOMPLETE;
101         handle->_id = getUShort (msg);
102         msg += 2;
103         if (msg + NS_INT16SZ > eom)
104                 return ISC_R_INCOMPLETE;
105         handle->_flags = getUShort (msg);
106         msg += 2;
107         for (i = 0; i < ns_s_max; i++) {
108                 if (msg + NS_INT16SZ > eom)
109                         return ISC_R_INCOMPLETE;
110                 handle->_counts[i] = getUShort (msg);
111                 msg += 2;
112         }
113         for (i = 0; i < ns_s_max; i++)
114                 if (handle->_counts[i] == 0)
115                         handle->_sections[i] = NULL;
116                 else {
117                         int b;
118                         isc_result_t status =
119                                 ns_skiprr(msg, eom, (ns_sect)i,
120                                           handle->_counts[i], &b);
121
122                         if (status != ISC_R_SUCCESS)
123                                 return STATUS;
124                         handle->_sections[i] = msg;
125                         msg += b;
126                 }
127         if (msg != eom)
128                 return ISC_R_INCOMPLETE;
129         setsection(handle, ns_s_max);
130         return ISC_R_SUCCESS;
131 }
132
133 isc_result_t
134 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
135         int b;
136         isc_result_t status;
137
138         /* Make section right. */
139         if (section < 0 || section >= ns_s_max)
140                 return ISC_R_NOTIMPLEMENTED;
141         if (section != handle->_sect)
142                 setsection(handle, section);
143
144         /* Make rrnum right. */
145         if (rrnum == -1)
146                 rrnum = handle->_rrnum;
147         if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
148                 return ISC_R_UNKNOWNATTRIBUTE;
149         if (rrnum < handle->_rrnum)
150                 setsection(handle, section);
151         if (rrnum > handle->_rrnum) {
152                 status = ns_skiprr(handle->_ptr, handle->_eom, section,
153                               rrnum - handle->_rrnum, &b);
154
155                 if (status != ISC_R_SUCCESS)
156                         return status;
157                 handle->_ptr += b;
158                 handle->_rrnum = rrnum;
159         }
160
161         /* Do the parse. */
162         b = dn_expand(handle->_msg, handle->_eom,
163                       handle->_ptr, rr->name, NS_MAXDNAME);
164         if (b < 0)
165                 return ISC_R_FORMERR;
166         handle->_ptr += b;
167         if (handle->_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
168                 return ISC_R_INCOMPLETE;
169         rr->type = getUShort (handle->_ptr);
170         handle -> _ptr += 2;
171         rr->rr_class = getUShort (handle->_ptr);
172         handle -> _ptr += 2;
173         if (section == ns_s_qd) {
174                 rr->ttl = 0;
175                 rr->rdlength = 0;
176                 rr->rdata = NULL;
177         } else {
178                 if (handle->_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
179                         return ISC_R_INCOMPLETE;
180                 rr->ttl = getULong (handle->_ptr);
181                 handle -> _ptr += 4;
182                 rr->rdlength = getUShort (handle->_ptr);
183                 handle -> _ptr += 2;
184                 if (handle->_ptr + rr->rdlength > handle->_eom)
185                         return ISC_R_INCOMPLETE;
186                 rr->rdata = handle->_ptr;
187                 handle->_ptr += rr->rdlength;
188         }
189         if (++handle->_rrnum > handle->_counts[(int)section])
190                 setsection(handle, (ns_sect)((int)section + 1));
191
192         /* All done. */
193         return ISC_R_SUCCESS;
194 }
195
196 /* Private. */
197
198 static void
199 setsection(ns_msg *msg, ns_sect sect) {
200         msg->_sect = sect;
201         if (sect == ns_s_max) {
202                 msg->_rrnum = -1;
203                 msg->_ptr = NULL;
204         } else {
205                 msg->_rrnum = 0;
206                 msg->_ptr = msg->_sections[(int)sect];
207         }
208 }