]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/res_comp.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / StdLib / BsdSocketLib / res_comp.c
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Portions copyright (c) 1999, 2000
6 * Intel Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 *
23 * This product includes software developed by the University of
24 * California, Berkeley, Intel Corporation, and its contributors.
25 *
26 * 4. Neither the name of University, Intel Corporation, or their respective
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 */
43
44 /*
45 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
46 *
47 * Permission to use, copy, modify, and distribute this software for any
48 * purpose with or without fee is hereby granted, provided that the above
49 * copyright notice and this permission notice appear in all copies, and that
50 * the name of Digital Equipment Corporation not be used in advertising or
51 * publicity pertaining to distribution of the document or software without
52 * specific, written prior permission.
53 *
54 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
55 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
57 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
58 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
59 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
60 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
61 * SOFTWARE.
62 */
63
64 /*
65 * Portions Copyright (c) 1996 by Internet Software Consortium.
66 *
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies.
70 *
71 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
72 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
73 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
74 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
75 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
76 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
77 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
78 * SOFTWARE.
79 */
80
81 #if defined(LIBC_SCCS) && !defined(lint)
82 static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
83 static char orig_rcsid[] = "From: Id: res_comp.c,v 8.11 1997/05/21 19:31:04 halley Exp $";
84 static char rcsid[] = "$Id: res_comp.c,v 1.1.1.1 2003/11/19 01:51:35 kyu3 Exp $";
85 #endif /* LIBC_SCCS and not lint */
86
87 #include <sys/types.h>
88 #include <sys/param.h>
89 #include <netinet/in.h>
90 #include <arpa/nameser.h>
91 #include <ctype.h>
92 #include <resolv.h>
93 #include <stdio.h>
94 #include <string.h>
95 #include <unistd.h>
96
97 #define BIND_4_COMPAT
98
99 /*
100 * Expand compressed domain name 'comp_dn' to full domain name.
101 * 'msg' is a pointer to the begining of the message,
102 * 'eomorig' points to the first location after the message,
103 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
104 * Return size of compressed name or -1 if there was an error.
105 */
106 int
107 dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
108 char *dst, int dstsiz)
109 {
110 int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
111
112 if (n > 0 && dst[0] == '.')
113 dst[0] = '\0';
114 return (n);
115 }
116
117 /*
118 * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
119 * Return the size of the compressed name or -1.
120 * 'length' is the size of the array pointed to by 'comp_dn'.
121 */
122 int
123 dn_comp(const char *src, u_char *dst, int dstsiz,
124 u_char **dnptrs, u_char **lastdnptr)
125 {
126 return (ns_name_compress(src, dst, (size_t)dstsiz,
127 (const u_char **)dnptrs,
128 (const u_char **)lastdnptr));
129 }
130
131 /*
132 * Skip over a compressed domain name. Return the size or -1.
133 */
134 int
135 dn_skipname(const u_char *ptr, const u_char *eom) {
136 const u_char *saveptr = ptr;
137
138 if (ns_name_skip(&ptr, eom) == -1)
139 return (-1);
140 return ((int)(ptr - saveptr));
141 }
142
143 /*
144 * Verify that a domain name uses an acceptable character set.
145 */
146
147 /*
148 * Note the conspicuous absence of ctype macros in these definitions. On
149 * non-ASCII hosts, we can't depend on string literals or ctype macros to
150 * tell us anything about network-format data. The rest of the BIND system
151 * is not careful about this, but for some reason, we're doing it right here.
152 */
153 #define PERIOD 0x2e
154 #define hyphenchar(c) ((c) == 0x2d)
155 #define bslashchar(c) ((c) == 0x5c)
156 #define periodchar(c) ((c) == PERIOD)
157 #define asterchar(c) ((c) == 0x2a)
158 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
159 || ((c) >= 0x61 && (c) <= 0x7a))
160 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
161
162 #define borderchar(c) (alphachar(c) || digitchar(c))
163 #define middlechar(c) (borderchar(c) || hyphenchar(c))
164 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
165
166 int
167 res_hnok(
168 const char *dn
169 )
170 {
171 int ppch = '\0', pch = PERIOD, ch = *dn++;
172
173 while (ch != '\0') {
174 int nch = *dn++;
175
176 if (periodchar(ch)) {
177 (void)NULL;
178 } else if (periodchar(pch)) {
179 if (!borderchar(ch))
180 return (0);
181 } else if (periodchar(nch) || nch == '\0') {
182 if (!borderchar(ch))
183 return (0);
184 } else {
185 if (!middlechar(ch))
186 return (0);
187 }
188 ppch = pch, pch = ch, ch = nch;
189 }
190 return (1);
191 }
192
193 /*
194 * hostname-like (A, MX, WKS) owners can have "*" as their first label
195 * but must otherwise be as a host name.
196 */
197 int
198 res_ownok(
199 const char *dn
200 )
201 {
202 if (asterchar(dn[0])) {
203 if (periodchar(dn[1]))
204 return (res_hnok(dn+2));
205 if (dn[1] == '\0')
206 return (1);
207 }
208 return (res_hnok(dn));
209 }
210
211 /*
212 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
213 * label, but the rest of the name has to look like a host name.
214 */
215 int
216 res_mailok(
217 const char *dn
218 )
219 {
220 int ch, escaped = 0;
221
222 /* "." is a valid missing representation */
223 if (*dn == '\0')
224 return (1);
225
226 /* otherwise <label>.<hostname> */
227 while ((ch = *dn++) != '\0') {
228 if (!domainchar(ch))
229 return (0);
230 if (!escaped && periodchar(ch))
231 break;
232 if (escaped)
233 escaped = 0;
234 else if (bslashchar(ch))
235 escaped = 1;
236 }
237 if (periodchar(ch))
238 return (res_hnok(dn));
239 return (0);
240 }
241
242 /*
243 * This function is quite liberal, since RFC 1034's character sets are only
244 * recommendations.
245 */
246 int
247 res_dnok(
248 const char *dn
249 )
250 {
251 int ch;
252
253 while ((ch = *dn++) != '\0')
254 if (!domainchar(ch))
255 return (0);
256 return (1);
257 }
258
259 #ifdef BIND_4_COMPAT
260 /*
261 * This module must export the following externally-visible symbols:
262 * ___putlong
263 * ___putshort
264 * __getlong
265 * __getshort
266 * Note that one _ comes from C and the others come from us.
267 */
268 void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
269 void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
270 u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
271 u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
272 #endif /*BIND_4_COMPAT*/