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