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