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