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