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