]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/ns_parse.c
Remove SocketPkg references
[mirror_edk2.git] / StdLib / BsdSocketLib / ns_parse.c
CommitLineData
d7ce7006 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#ifndef lint\r
59static char rcsid[] = "$Id: ns_parse.c,v 1.1.1.1 2003/11/19 01:51:33 kyu3 Exp $";\r
60#endif\r
61\r
62#include <sys/types.h>\r
63\r
64#include <netinet/in.h>\r
65#include <arpa/nameser.h>\r
66\r
67#include <errno.h>\r
68#include <resolv.h>\r
69#include <string.h>\r
70\r
71/* These need to be in the same order as the nres.h:ns_flag enum. */\r
72struct _ns_flagdata _ns_flagdata[16] = {\r
73 { 0x8000, 15 }, /* qr. */\r
74 { 0x7800, 11 }, /* opcode. */\r
75 { 0x0400, 10 }, /* aa. */\r
76 { 0x0200, 9 }, /* tc. */\r
77 { 0x0100, 8 }, /* rd. */\r
78 { 0x0080, 7 }, /* ra. */\r
79 { 0x0040, 6 }, /* z. */\r
80 { 0x0020, 5 }, /* ad. */\r
81 { 0x0010, 4 }, /* cd. */\r
82 { 0x000f, 0 }, /* rcode. */\r
83 { 0x0000, 0 }, /* expansion (1/6). */\r
84 { 0x0000, 0 }, /* expansion (2/6). */\r
85 { 0x0000, 0 }, /* expansion (3/6). */\r
86 { 0x0000, 0 }, /* expansion (4/6). */\r
87 { 0x0000, 0 }, /* expansion (5/6). */\r
88 { 0x0000, 0 }, /* expansion (6/6). */\r
89};\r
90\r
91static int\r
92skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {\r
93 const u_char *optr = ptr;\r
94\r
95 for ((void)NULL; count > 0; count--) {\r
96 int b, rdlength;\r
97\r
98 b = dn_skipname(ptr, eom);\r
99 if (b < 0)\r
100 goto emsgsize;\r
101 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;\r
102 if (section != ns_s_qd) {\r
103 if (ptr + NS_INT32SZ > eom)\r
104 goto emsgsize;\r
105 ptr += NS_INT32SZ/*TTL*/;\r
106 if (ptr + NS_INT16SZ > eom)\r
107 goto emsgsize;\r
108 NS_GET16(rdlength, ptr);\r
109 ptr += rdlength/*RData*/;\r
110 }\r
111 }\r
112 if (ptr > eom)\r
113 goto emsgsize;\r
114 return ((int)(ptr - optr));\r
115 emsgsize:\r
116 errno = EMSGSIZE;\r
117 return (-1);\r
118}\r
119\r
120int\r
121ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {\r
122 const u_char *eom = msg + msglen;\r
123 int i;\r
124\r
125 memset(handle, 0x5e, sizeof *handle);\r
126 handle->_msg = msg;\r
127 handle->_eom = eom;\r
128 if (msg + NS_INT16SZ > eom)\r
129 goto emsgsize;\r
130 NS_GET16(handle->_id, msg);\r
131 if (msg + NS_INT16SZ > eom)\r
132 goto emsgsize;\r
133 NS_GET16(handle->_flags, msg);\r
134 for (i = 0; i < ns_s_max; i++) {\r
135 if (msg + NS_INT16SZ > eom)\r
136 goto emsgsize;\r
137 NS_GET16(handle->_counts[i], msg);\r
138 }\r
139 for (i = 0; i < ns_s_max; i++)\r
140 if (handle->_counts[i] == 0)\r
141 handle->_sections[i] = NULL;\r
142 else {\r
143 int b = skiprr(msg, eom, (ns_sect)i,\r
144 handle->_counts[i]);\r
145\r
146 if (b < 0)\r
147 return (-1);\r
148 handle->_sections[i] = msg;\r
149 msg += b;\r
150 }\r
151 if (msg != eom)\r
152 goto emsgsize;\r
153 handle->_sect = ns_s_max;\r
154 handle->_rrnum = -1;\r
155 handle->_msg_ptr = NULL;\r
156 return (0);\r
157 emsgsize:\r
158 errno = EMSGSIZE;\r
159 return (-1);\r
160}\r
161\r
162int\r
163ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {\r
164 int b;\r
165\r
166 /* Make section right. */\r
167 if (section < 0 || section >= ns_s_max)\r
168 goto enodev;\r
169 if ((int)section != (int)handle->_sect) {\r
170 handle->_sect = section;\r
171 handle->_rrnum = 0;\r
172 handle->_msg_ptr = handle->_sections[(int)section];\r
173 }\r
174\r
175 /* Make rrnum right. */\r
176 if (rrnum == -1)\r
177 rrnum = handle->_rrnum;\r
178 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])\r
179 goto enodev;\r
180 if (rrnum < handle->_rrnum) {\r
181 handle->_rrnum = 0;\r
182 handle->_msg_ptr = handle->_sections[(int)section];\r
183 }\r
184 \r
185 b = skiprr(handle->_msg, handle->_eom, section,\r
186 rrnum - handle->_rrnum);\r
187 if (b < 0)\r
188 return (-1);\r
189 handle->_msg_ptr += b;\r
190 handle->_rrnum = rrnum;\r
191\r
192 /* Do the parse. */\r
193 b = dn_expand(handle->_msg, handle->_eom,\r
194 handle->_msg_ptr, rr->name, NS_MAXDNAME);\r
195 if (b < 0)\r
196 return (-1);\r
197 handle->_msg_ptr += b;\r
198 if (handle->_msg_ptr + NS_INT16SZ > handle->_eom)\r
199 goto emsgsize;\r
200 NS_GET16(rr->type, handle->_msg_ptr);\r
201 if (handle->_msg_ptr + NS_INT16SZ > handle->_eom)\r
202 goto emsgsize;\r
203 NS_GET16(rr->rr_class, handle->_msg_ptr);\r
204 if (section == ns_s_qd) {\r
205 rr->ttl = 0;\r
206 rr->rdlength = 0;\r
207 rr->rdata = NULL;\r
208 } else {\r
209 if (handle->_msg_ptr + NS_INT32SZ > handle->_eom)\r
210 goto emsgsize;\r
211 NS_GET32(rr->ttl, handle->_msg_ptr);\r
212 if (handle->_msg_ptr + NS_INT16SZ > handle->_eom)\r
213 goto emsgsize;\r
214 NS_GET16(rr->rdlength, handle->_msg_ptr);\r
215 if (handle->_msg_ptr + rr->rdlength > handle->_eom)\r
216 goto emsgsize;\r
217 rr->rdata = handle->_msg_ptr;\r
218 handle->_msg_ptr += rr->rdlength;\r
219 }\r
220 handle->_rrnum++;\r
221\r
222 /* All done. */\r
223 return (0);\r
224 enodev:\r
225 errno = ENODEV;\r
226 return (-1);\r
227 emsgsize:\r
228 errno = EMSGSIZE;\r
229 return (-1);\r
230}\r