ns_parse.c revision 1.1 1 1.1 christos /* $NetBSD: ns_parse.c,v 1.1 2004/05/20 20:01:31 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1996,1999 by Internet Software Consortium.
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #ifndef lint
21 1.1 christos static const char rcsid[] = "Id: ns_parse.c,v 1.3.2.1.4.1 2004/03/09 08:33:44 marka Exp";
22 1.1 christos #endif
23 1.1 christos
24 1.1 christos /* Import. */
25 1.1 christos
26 1.1 christos #include "port_before.h"
27 1.1 christos
28 1.1 christos #include <sys/types.h>
29 1.1 christos
30 1.1 christos #include <netinet/in.h>
31 1.1 christos #include <arpa/nameser.h>
32 1.1 christos
33 1.1 christos #include <errno.h>
34 1.1 christos #include <resolv.h>
35 1.1 christos #include <string.h>
36 1.1 christos
37 1.1 christos #include "port_after.h"
38 1.1 christos
39 1.1 christos /* Forward. */
40 1.1 christos
41 1.1 christos static void setsection(ns_msg *msg, ns_sect sect);
42 1.1 christos
43 1.1 christos /* Macros. */
44 1.1 christos
45 1.1 christos #define RETERR(err) do { errno = (err); return (-1); } while (0)
46 1.1 christos
47 1.1 christos /* Public. */
48 1.1 christos
49 1.1 christos /* These need to be in the same order as the nres.h:ns_flag enum. */
50 1.1 christos struct _ns_flagdata _ns_flagdata[16] = {
51 1.1 christos { 0x8000, 15 }, /* qr. */
52 1.1 christos { 0x7800, 11 }, /* opcode. */
53 1.1 christos { 0x0400, 10 }, /* aa. */
54 1.1 christos { 0x0200, 9 }, /* tc. */
55 1.1 christos { 0x0100, 8 }, /* rd. */
56 1.1 christos { 0x0080, 7 }, /* ra. */
57 1.1 christos { 0x0040, 6 }, /* z. */
58 1.1 christos { 0x0020, 5 }, /* ad. */
59 1.1 christos { 0x0010, 4 }, /* cd. */
60 1.1 christos { 0x000f, 0 }, /* rcode. */
61 1.1 christos { 0x0000, 0 }, /* expansion (1/6). */
62 1.1 christos { 0x0000, 0 }, /* expansion (2/6). */
63 1.1 christos { 0x0000, 0 }, /* expansion (3/6). */
64 1.1 christos { 0x0000, 0 }, /* expansion (4/6). */
65 1.1 christos { 0x0000, 0 }, /* expansion (5/6). */
66 1.1 christos { 0x0000, 0 }, /* expansion (6/6). */
67 1.1 christos };
68 1.1 christos
69 1.1 christos int ns_msg_getflag(ns_msg handle, int flag) {
70 1.1 christos return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
71 1.1 christos }
72 1.1 christos
73 1.1 christos int
74 1.1 christos ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
75 1.1 christos const u_char *optr = ptr;
76 1.1 christos
77 1.1 christos for ((void)NULL; count > 0; count--) {
78 1.1 christos int b, rdlength;
79 1.1 christos
80 1.1 christos b = dn_skipname(ptr, eom);
81 1.1 christos if (b < 0)
82 1.1 christos RETERR(EMSGSIZE);
83 1.1 christos ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
84 1.1 christos if (section != ns_s_qd) {
85 1.1 christos if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
86 1.1 christos RETERR(EMSGSIZE);
87 1.1 christos ptr += NS_INT32SZ/*TTL*/;
88 1.1 christos NS_GET16(rdlength, ptr);
89 1.1 christos ptr += rdlength/*RData*/;
90 1.1 christos }
91 1.1 christos }
92 1.1 christos if (ptr > eom)
93 1.1 christos RETERR(EMSGSIZE);
94 1.1 christos return (ptr - optr);
95 1.1 christos }
96 1.1 christos
97 1.1 christos int
98 1.1 christos ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
99 1.1 christos const u_char *eom = msg + msglen;
100 1.1 christos int i;
101 1.1 christos
102 1.1 christos memset(handle, 0x5e, sizeof *handle);
103 1.1 christos handle->_msg = msg;
104 1.1 christos handle->_eom = eom;
105 1.1 christos if (msg + NS_INT16SZ > eom)
106 1.1 christos RETERR(EMSGSIZE);
107 1.1 christos NS_GET16(handle->_id, msg);
108 1.1 christos if (msg + NS_INT16SZ > eom)
109 1.1 christos RETERR(EMSGSIZE);
110 1.1 christos NS_GET16(handle->_flags, msg);
111 1.1 christos for (i = 0; i < ns_s_max; i++) {
112 1.1 christos if (msg + NS_INT16SZ > eom)
113 1.1 christos RETERR(EMSGSIZE);
114 1.1 christos NS_GET16(handle->_counts[i], msg);
115 1.1 christos }
116 1.1 christos for (i = 0; i < ns_s_max; i++)
117 1.1 christos if (handle->_counts[i] == 0)
118 1.1 christos handle->_sections[i] = NULL;
119 1.1 christos else {
120 1.1 christos int b = ns_skiprr(msg, eom, (ns_sect)i,
121 1.1 christos handle->_counts[i]);
122 1.1 christos
123 1.1 christos if (b < 0)
124 1.1 christos return (-1);
125 1.1 christos handle->_sections[i] = msg;
126 1.1 christos msg += b;
127 1.1 christos }
128 1.1 christos if (msg != eom)
129 1.1 christos RETERR(EMSGSIZE);
130 1.1 christos setsection(handle, ns_s_max);
131 1.1 christos return (0);
132 1.1 christos }
133 1.1 christos
134 1.1 christos int
135 1.1 christos ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
136 1.1 christos int b;
137 1.1 christos int tmp;
138 1.1 christos
139 1.1 christos /* Make section right. */
140 1.1 christos if ((tmp = section) < 0 || section >= ns_s_max)
141 1.1 christos RETERR(ENODEV);
142 1.1 christos if (section != handle->_sect)
143 1.1 christos setsection(handle, section);
144 1.1 christos
145 1.1 christos /* Make rrnum right. */
146 1.1 christos if (rrnum == -1)
147 1.1 christos rrnum = handle->_rrnum;
148 1.1 christos if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
149 1.1 christos RETERR(ENODEV);
150 1.1 christos if (rrnum < handle->_rrnum)
151 1.1 christos setsection(handle, section);
152 1.1 christos if (rrnum > handle->_rrnum) {
153 1.1 christos b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
154 1.1 christos rrnum - handle->_rrnum);
155 1.1 christos
156 1.1 christos if (b < 0)
157 1.1 christos return (-1);
158 1.1 christos handle->_msg_ptr += b;
159 1.1 christos handle->_rrnum = rrnum;
160 1.1 christos }
161 1.1 christos
162 1.1 christos /* Do the parse. */
163 1.1 christos b = dn_expand(handle->_msg, handle->_eom,
164 1.1 christos handle->_msg_ptr, rr->name, NS_MAXDNAME);
165 1.1 christos if (b < 0)
166 1.1 christos return (-1);
167 1.1 christos handle->_msg_ptr += b;
168 1.1 christos if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
169 1.1 christos RETERR(EMSGSIZE);
170 1.1 christos NS_GET16(rr->type, handle->_msg_ptr);
171 1.1 christos NS_GET16(rr->rr_class, handle->_msg_ptr);
172 1.1 christos if (section == ns_s_qd) {
173 1.1 christos rr->ttl = 0;
174 1.1 christos rr->rdlength = 0;
175 1.1 christos rr->rdata = NULL;
176 1.1 christos } else {
177 1.1 christos if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
178 1.1 christos RETERR(EMSGSIZE);
179 1.1 christos NS_GET32(rr->ttl, handle->_msg_ptr);
180 1.1 christos NS_GET16(rr->rdlength, handle->_msg_ptr);
181 1.1 christos if (handle->_msg_ptr + rr->rdlength > handle->_eom)
182 1.1 christos RETERR(EMSGSIZE);
183 1.1 christos rr->rdata = handle->_msg_ptr;
184 1.1 christos handle->_msg_ptr += rr->rdlength;
185 1.1 christos }
186 1.1 christos if (++handle->_rrnum > handle->_counts[(int)section])
187 1.1 christos setsection(handle, (ns_sect)((int)section + 1));
188 1.1 christos
189 1.1 christos /* All done. */
190 1.1 christos return (0);
191 1.1 christos }
192 1.1 christos
193 1.1 christos /* Private. */
194 1.1 christos
195 1.1 christos static void
196 1.1 christos setsection(ns_msg *msg, ns_sect sect) {
197 1.1 christos msg->_sect = sect;
198 1.1 christos if (sect == ns_s_max) {
199 1.1 christos msg->_rrnum = -1;
200 1.1 christos msg->_msg_ptr = NULL;
201 1.1 christos } else {
202 1.1 christos msg->_rrnum = 0;
203 1.1 christos msg->_msg_ptr = msg->_sections[(int)sect];
204 1.1 christos }
205 1.1 christos }
206