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