]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/NetUtil/inet_addr.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / NetUtil / inet_addr.c
CommitLineData
2aa62f2b 1/** @file\r
2 Convert a string internet address into an integer (32-bit) address.\r
3\r
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available under\r
6 the terms and conditions of the BSD License that accompanies this distribution.\r
7 The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 * Copyright (c) 1983, 1990, 1993\r
14 * The Regents of the University of California. All rights reserved.\r
15 *\r
16 * Redistribution and use in source and binary forms, with or without\r
17 * modification, are permitted provided that the following conditions\r
18 * are met:\r
19 * 1. Redistributions of source code must retain the above copyright\r
20 * notice, this list of conditions and the following disclaimer.\r
21 * 2. Redistributions in binary form must reproduce the above copyright\r
22 * notice, this list of conditions and the following disclaimer in the\r
23 * documentation and/or other materials provided with the distribution.\r
24 * 3. All advertising materials mentioning features or use of this software\r
25 * must display the following acknowledgement:\r
26 * This product includes software developed by the University of\r
27 * California, Berkeley and its contributors.\r
28 * 4. Neither the name of the University nor the names of its contributors\r
29 * may be used to endorse or promote products derived from this software\r
30 * without specific prior written permission.\r
31 *\r
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
42 * SUCH DAMAGE.\r
43\r
44 * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r
45 *\r
46 * Permission to use, copy, modify, and distribute this software for any\r
47 * purpose with or without fee is hereby granted, provided that the above\r
48 * copyright notice and this permission notice appear in all copies, and that\r
49 * the name of Digital Equipment Corporation not be used in advertising or\r
50 * publicity pertaining to distribution of the document or software without\r
51 * specific, written prior permission.\r
52 *\r
53 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r
54 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r
55 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r
56 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
57 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
58 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
59 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
60 * SOFTWARE.\r
61\r
62 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")\r
63 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.\r
64 *\r
65 * Permission to use, copy, modify, and distribute this software for any\r
66 * purpose with or without fee is hereby granted, provided that the above\r
67 * copyright notice and this permission notice appear in all copies.\r
68 *\r
69 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES\r
70 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\r
71 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR\r
72 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\r
73 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\r
74 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT\r
75 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\r
76\r
77 NetBSD: inet_addr.c,v 1.1 2005/12/20 19:28:51 christos Exp\r
78 inet_addr.c 8.1 (Berkeley) 6/17/93\r
79 inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp\r
80**/\r
81\r
82#if !defined(_KERNEL) && !defined(_STANDALONE)\r
83#include <LibConfig.h>\r
84\r
85//#include "port_before.h"\r
86\r
87#include <namespace.h>\r
88#include <sys/types.h>\r
89#include <sys/param.h>\r
90\r
91#include <netinet/in.h>\r
92#include <arpa/inet.h>\r
93\r
94#include <ctype.h>\r
95\r
96//#include "port_after.h"\r
97\r
98#ifdef __weak_alias\r
99 __weak_alias(inet_aton,_inet_aton)\r
100#endif\r
101\r
102#else // NOT (!defined(_KERNEL) && !defined(_STANDALONE))\r
103 #include <lib/libkern/libkern.h>\r
104 #include <netinet/in.h>\r
105#endif\r
106\r
107/*\r
108 * Ascii internet address interpretation routine.\r
109 * The value returned is in network order.\r
110 */\r
111u_int32_t\r
112inet_addr(const char *cp) {\r
113 struct in_addr val;\r
114\r
115 if (inet_aton(cp, &val))\r
116 return (val.s_addr);\r
117 return (INADDR_NONE);\r
118}\r
119\r
120/*\r
121 * Check whether "cp" is a valid ascii representation\r
122 * of an Internet address and convert to a binary address.\r
123 * Returns 1 if the address is valid, 0 if not.\r
124 * This replaces inet_addr, the return value from which\r
125 * cannot distinguish between failure and a local broadcast address.\r
126 */\r
127int\r
128inet_aton(const char *cp, struct in_addr *addr) {\r
129 u_int32_t val;\r
130 int base, n;\r
131 char c;\r
132 u_int8_t parts[4];\r
133 u_int8_t *pp = parts;\r
134 int digit;\r
135\r
136 c = *cp;\r
137 for (;;) {\r
138 /*\r
139 * Collect number up to ``.''.\r
140 * Values are specified as for C:\r
141 * 0x=hex, 0=octal, isdigit=decimal.\r
142 */\r
143 if (!isdigit((unsigned char)c))\r
144 return (0);\r
145 val = 0; base = 10; digit = 0;\r
146 if (c == '0') {\r
147 c = *++cp;\r
148 if (c == 'x' || c == 'X')\r
149 base = 16, c = *++cp;\r
150 else {\r
151 base = 8;\r
152 digit = 1 ;\r
153 }\r
154 }\r
155 for (;;) {\r
156 if (isascii(c) && isdigit((unsigned char)c)) {\r
157 if (base == 8 && (c == '8' || c == '9'))\r
158 return (0);\r
159 val = (val * base) + (c - '0');\r
160 c = *++cp;\r
161 digit = 1;\r
162 } else if (base == 16 && isascii(c) &&\r
163 isxdigit((unsigned char)c)) {\r
164 val = (val << 4) |\r
165 (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));\r
166 c = *++cp;\r
167 digit = 1;\r
168 } else\r
169 break;\r
170 }\r
171 if (c == '.') {\r
172 /*\r
173 * Internet format:\r
174 * a.b.c.d\r
175 * a.b.c (with c treated as 16 bits)\r
176 * a.b (with b treated as 24 bits)\r
177 */\r
178 if (pp >= parts + 3 || val > 0xffU)\r
179 return (0);\r
180 *pp++ = (u_int8_t)val;\r
181 c = *++cp;\r
182 } else\r
183 break;\r
184 }\r
185 /*\r
186 * Check for trailing characters.\r
187 */\r
188 if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))\r
189 return (0);\r
190 /*\r
191 * Did we get a valid digit?\r
192 */\r
193 if (!digit)\r
194 return (0);\r
195 /*\r
196 * Concoct the address according to\r
197 * the number of parts specified.\r
198 */\r
199 n = (int)(pp - parts + 1);\r
200 switch (n) {\r
201 case 1: /* a -- 32 bits */\r
202 break;\r
203\r
204 case 2: /* a.b -- 8.24 bits */\r
205 if (val > 0xffffffU)\r
206 return (0);\r
207 val |= parts[0] << 24;\r
208 break;\r
209\r
210 case 3: /* a.b.c -- 8.8.16 bits */\r
211 if (val > 0xffffU)\r
212 return (0);\r
213 val |= (parts[0] << 24) | (parts[1] << 16);\r
214 break;\r
215\r
216 case 4: /* a.b.c.d -- 8.8.8.8 bits */\r
217 if (val > 0xffU)\r
218 return (0);\r
219 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);\r
220 break;\r
221 }\r
222 if (addr != NULL)\r
223 addr->s_addr = htonl(val);\r
224 return (1);\r
225}\r