]>
Commit | Line | Data |
---|---|---|
1 | /*\r | |
2 | * Copyright (c) 1996 by Internet Software Consortium.\r | |
3 | *\r | |
4 | * Permission to use, copy, modify, and distribute this software for any\r | |
5 | * purpose with or without fee is hereby granted, provided that the above\r | |
6 | * copyright notice and this permission notice appear in all copies.\r | |
7 | *\r | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r | |
9 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r | |
10 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r | |
11 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r | |
12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r | |
13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r | |
14 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r | |
15 | * SOFTWARE.\r | |
16 | */\r | |
17 | \r | |
18 | /*\r | |
19 | * Portions copyright (c) 1999, 2000\r | |
20 | * Intel Corporation.\r | |
21 | * All rights reserved.\r | |
22 | * \r | |
23 | * Redistribution and use in source and binary forms, with or without\r | |
24 | * modification, are permitted provided that the following conditions\r | |
25 | * are met:\r | |
26 | * \r | |
27 | * 1. Redistributions of source code must retain the above copyright\r | |
28 | * notice, this list of conditions and the following disclaimer.\r | |
29 | * \r | |
30 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
31 | * notice, this list of conditions and the following disclaimer in the\r | |
32 | * documentation and/or other materials provided with the distribution.\r | |
33 | * \r | |
34 | * 3. All advertising materials mentioning features or use of this software\r | |
35 | * must display the following acknowledgement:\r | |
36 | * \r | |
37 | * This product includes software developed by Intel Corporation and\r | |
38 | * its contributors.\r | |
39 | * \r | |
40 | * 4. Neither the name of Intel Corporation or its contributors may be\r | |
41 | * used to endorse or promote products derived from this software\r | |
42 | * without specific prior written permission.\r | |
43 | * \r | |
44 | * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''\r | |
45 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE\r | |
48 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r | |
49 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r | |
50 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r | |
51 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r | |
52 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r | |
53 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r | |
54 | * THE POSSIBILITY OF SUCH DAMAGE.\r | |
55 | * \r | |
56 | */\r | |
57 | \r | |
58 | #include <sys/types.h>\r | |
59 | \r | |
60 | #include <netinet/in.h>\r | |
61 | #include <arpa/nameser.h>\r | |
62 | \r | |
63 | #include <errno.h>\r | |
64 | #include <resolv.h>\r | |
65 | #include <string.h>\r | |
66 | \r | |
67 | /* These need to be in the same order as the nres.h:ns_flag enum. */\r | |
68 | struct _ns_flagdata _ns_flagdata[16] = {\r | |
69 | { 0x8000, 15 }, /* qr. */\r | |
70 | { 0x7800, 11 }, /* opcode. */\r | |
71 | { 0x0400, 10 }, /* aa. */\r | |
72 | { 0x0200, 9 }, /* tc. */\r | |
73 | { 0x0100, 8 }, /* rd. */\r | |
74 | { 0x0080, 7 }, /* ra. */\r | |
75 | { 0x0040, 6 }, /* z. */\r | |
76 | { 0x0020, 5 }, /* ad. */\r | |
77 | { 0x0010, 4 }, /* cd. */\r | |
78 | { 0x000f, 0 }, /* rcode. */\r | |
79 | { 0x0000, 0 }, /* expansion (1/6). */\r | |
80 | { 0x0000, 0 }, /* expansion (2/6). */\r | |
81 | { 0x0000, 0 }, /* expansion (3/6). */\r | |
82 | { 0x0000, 0 }, /* expansion (4/6). */\r | |
83 | { 0x0000, 0 }, /* expansion (5/6). */\r | |
84 | { 0x0000, 0 }, /* expansion (6/6). */\r | |
85 | };\r | |
86 | \r | |
87 | static int\r | |
88 | skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {\r | |
89 | const u_char *optr = ptr;\r | |
90 | \r | |
91 | for ((void)NULL; count > 0; count--) {\r | |
92 | int b, rdlength;\r | |
93 | \r | |
94 | b = dn_skipname(ptr, eom);\r | |
95 | if (b < 0)\r | |
96 | goto emsgsize;\r | |
97 | ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;\r | |
98 | if (section != ns_s_qd) {\r | |
99 | if (ptr + NS_INT32SZ > eom)\r | |
100 | goto emsgsize;\r | |
101 | ptr += NS_INT32SZ/*TTL*/;\r | |
102 | if (ptr + NS_INT16SZ > eom)\r | |
103 | goto emsgsize;\r | |
104 | NS_GET16(rdlength, ptr);\r | |
105 | ptr += rdlength/*RData*/;\r | |
106 | }\r | |
107 | }\r | |
108 | if (ptr > eom)\r | |
109 | goto emsgsize;\r | |
110 | return ((int)(ptr - optr));\r | |
111 | emsgsize:\r | |
112 | errno = EMSGSIZE;\r | |
113 | return (-1);\r | |
114 | }\r | |
115 | \r | |
116 | int\r | |
117 | ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {\r | |
118 | const u_char *eom = msg + msglen;\r | |
119 | int i;\r | |
120 | \r | |
121 | memset(handle, 0x5e, sizeof *handle);\r | |
122 | handle->_msg = msg;\r | |
123 | handle->_eom = eom;\r | |
124 | if (msg + NS_INT16SZ > eom)\r | |
125 | goto emsgsize;\r | |
126 | NS_GET16(handle->_id, msg);\r | |
127 | if (msg + NS_INT16SZ > eom)\r | |
128 | goto emsgsize;\r | |
129 | NS_GET16(handle->_flags, msg);\r | |
130 | for (i = 0; i < ns_s_max; i++) {\r | |
131 | if (msg + NS_INT16SZ > eom)\r | |
132 | goto emsgsize;\r | |
133 | NS_GET16(handle->_counts[i], msg);\r | |
134 | }\r | |
135 | for (i = 0; i < ns_s_max; i++)\r | |
136 | if (handle->_counts[i] == 0)\r | |
137 | handle->_sections[i] = NULL;\r | |
138 | else {\r | |
139 | int b = skiprr(msg, eom, (ns_sect)i,\r | |
140 | handle->_counts[i]);\r | |
141 | \r | |
142 | if (b < 0)\r | |
143 | return (-1);\r | |
144 | handle->_sections[i] = msg;\r | |
145 | msg += b;\r | |
146 | }\r | |
147 | if (msg != eom)\r | |
148 | goto emsgsize;\r | |
149 | handle->_sect = ns_s_max;\r | |
150 | handle->_rrnum = -1;\r | |
151 | handle->_msg_ptr = NULL;\r | |
152 | return (0);\r | |
153 | emsgsize:\r | |
154 | errno = EMSGSIZE;\r | |
155 | return (-1);\r | |
156 | }\r | |
157 | \r | |
158 | int\r | |
159 | ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {\r | |
160 | int b;\r | |
161 | \r | |
162 | /* Make section right. */\r | |
163 | if (section < 0 || section >= ns_s_max)\r | |
164 | goto enodev;\r | |
165 | if ((int)section != (int)handle->_sect) {\r | |
166 | handle->_sect = section;\r | |
167 | handle->_rrnum = 0;\r | |
168 | handle->_msg_ptr = handle->_sections[(int)section];\r | |
169 | }\r | |
170 | \r | |
171 | /* Make rrnum right. */\r | |
172 | if (rrnum == -1)\r | |
173 | rrnum = handle->_rrnum;\r | |
174 | if (rrnum < 0 || rrnum >= handle->_counts[(int)section])\r | |
175 | goto enodev;\r | |
176 | if (rrnum < handle->_rrnum) {\r | |
177 | handle->_rrnum = 0;\r | |
178 | handle->_msg_ptr = handle->_sections[(int)section];\r | |
179 | }\r | |
180 | \r | |
181 | b = skiprr(handle->_msg, handle->_eom, section,\r | |
182 | rrnum - handle->_rrnum);\r | |
183 | if (b < 0)\r | |
184 | return (-1);\r | |
185 | handle->_msg_ptr += b;\r | |
186 | handle->_rrnum = rrnum;\r | |
187 | \r | |
188 | /* Do the parse. */\r | |
189 | b = dn_expand(handle->_msg, handle->_eom,\r | |
190 | handle->_msg_ptr, rr->name, NS_MAXDNAME);\r | |
191 | if (b < 0)\r | |
192 | return (-1);\r | |
193 | handle->_msg_ptr += b;\r | |
194 | if (handle->_msg_ptr + NS_INT16SZ > handle->_eom)\r | |
195 | goto emsgsize;\r | |
196 | NS_GET16(rr->type, handle->_msg_ptr);\r | |
197 | if (handle->_msg_ptr + NS_INT16SZ > handle->_eom)\r | |
198 | goto emsgsize;\r | |
199 | NS_GET16(rr->rr_class, handle->_msg_ptr);\r | |
200 | if (section == ns_s_qd) {\r | |
201 | rr->ttl = 0;\r | |
202 | rr->rdlength = 0;\r | |
203 | rr->rdata = NULL;\r | |
204 | } else {\r | |
205 | if (handle->_msg_ptr + NS_INT32SZ > handle->_eom)\r | |
206 | goto emsgsize;\r | |
207 | NS_GET32(rr->ttl, handle->_msg_ptr);\r | |
208 | if (handle->_msg_ptr + NS_INT16SZ > handle->_eom)\r | |
209 | goto emsgsize;\r | |
210 | NS_GET16(rr->rdlength, handle->_msg_ptr);\r | |
211 | if (handle->_msg_ptr + rr->rdlength > handle->_eom)\r | |
212 | goto emsgsize;\r | |
213 | rr->rdata = handle->_msg_ptr;\r | |
214 | handle->_msg_ptr += rr->rdlength;\r | |
215 | }\r | |
216 | handle->_rrnum++;\r | |
217 | \r | |
218 | /* All done. */\r | |
219 | return (0);\r | |
220 | enodev:\r | |
221 | errno = ENODEV;\r | |
222 | return (-1);\r | |
223 | emsgsize:\r | |
224 | errno = EMSGSIZE;\r | |
225 | return (-1);\r | |
226 | }\r |