]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/inet_net_pton.c
2 * Copyright (c) 1996 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 * Portions copyright (c) 1999, 2000
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
37 * This product includes software developed by Intel Corporation and
40 * 4. Neither the name of Intel Corporation or its contributors may be
41 * used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
58 #if defined(LIBC_SCCS) && !defined(lint)
59 static const char orig_rcsid
[] = "From Id: inet_net_pton.c,v 1.8 1996/11/21 10:28:12 vixie Exp $";
60 static const char rcsid
[] = "$Id: inet_net_pton.c,v 1.1.1.1 2003/11/19 01:51:29 kyu3 Exp $";
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
76 # define SPRINTF(x) strlen(sprintf/**/x)
78 # define SPRINTF(x) ((size_t)sprintf x)
81 static int inet_net_pton_ipv4 (const char *src
, u_char
*dst
,
86 * inet_net_pton(af, src, dst, size)
87 * convert network number from presentation to network format.
88 * accepts hex octets, hex strings, decimal octets, and /CIDR.
89 * "size" is in bytes and describes "dst".
91 * number of bits, either imputed classfully or specified with /CIDR,
92 * or -1 if some failure occurred (check errno). ENOENT means it was
93 * not a valid network specification.
95 * Paul Vixie (ISC), June 1996
107 return (inet_net_pton_ipv4(src
, dst
, size
));
109 errno
= EAFNOSUPPORT
;
116 * inet_net_pton_ipv4(src, dst, size)
117 * convert IPv4 network number from presentation to network format.
118 * accepts hex octets, hex strings, decimal octets, and /CIDR.
119 * "size" is in bytes and describes "dst".
121 * number of bits, either imputed classfully or specified with /CIDR,
122 * or -1 if some failure occurred (check errno). ENOENT means it was
123 * not an IPv4 network specification.
125 * network byte order assumed. this means 192.5.5.240/28 has
126 * 0x11110000 in its fourth octet.
128 * Paul Vixie (ISC), June 1996
137 static const char xdigits
[] = "0123456789abcdef";
138 static const char digits
[] = "0123456789";
144 const u_char
*odst
= dst
;
147 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
148 && isascii(src
[1]) && isxdigit(src
[1])) {
149 /* Hexadecimal: Eat nybble string. */
153 src
++; /* skip x or X. */
154 while ((ch
= *src
++) != '\0' &&
155 isascii(ch
) && isxdigit(ch
)) {
158 n
= (int)(strchr(xdigits
, ch
) - xdigits
);
159 assert(n
>= 0 && n
<= 15);
164 *++dst
= 0, dirty
= 0;
170 } else if (isascii(ch
) && isdigit(ch
)) {
171 /* Decimal: eat dotted digit string. */
175 n
= (int)(strchr(digits
, ch
) - digits
);
176 assert(n
>= 0 && n
<= 9);
181 } while ((ch
= *src
++) != '\0' &&
182 isascii(ch
) && isdigit(ch
));
185 *dst
++ = (u_char
) tmp
;
186 if (ch
== '\0' || ch
== '/')
191 if (!isascii(ch
) || !isdigit(ch
))
198 if (ch
== '/' && isascii(src
[0]) && isdigit(src
[0]) && dst
> odst
) {
199 /* CIDR width specifier. Nothing can follow it. */
200 ch
= *src
++; /* Skip over the /. */
203 n
= (int)(strchr(digits
, ch
) - digits
);
204 assert(n
>= 0 && n
<= 9);
207 } while ((ch
= *src
++) != '\0' && isascii(ch
) && isdigit(ch
));
214 /* Firey death and destruction unless we prefetched EOS. */
218 /* If nothing was written to the destination, we found no address. */
221 /* If no CIDR spec was given, infer width from net class. */
223 if (*odst
>= 240) /* Class E */
225 else if (*odst
>= 224) /* Class D */
227 else if (*odst
>= 192) /* Class C */
229 else if (*odst
>= 128) /* Class B */
233 /* If imputed mask is narrower than specified octets, widen. */
234 if (bits
>= 8 && bits
< ((dst
- odst
) * 8))
235 bits
= (int)(dst
- odst
) * 8;
237 /* Extend network to cover the actual mask. */
238 while (bits
> ((dst
- odst
) * 8)) {