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