]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/inet_pton.c
1 /* Copyright (c) 1996 by Internet Software Consortium.
3 * Permission to use, copy, modify, and distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
8 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
9 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
10 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 * Portions copyright (c) 1999, 2000
20 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
36 * This product includes software developed by Intel Corporation and
39 * 4. Neither the name of Intel Corporation or its contributors may be
40 * used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
44 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
53 * THE POSSIBILITY OF SUCH DAMAGE.
57 #if defined(LIBC_SCCS) && !defined(lint)
58 static char rcsid
[] = "$Id: inet_pton.c,v 1.1.1.1 2003/11/19 01:51:30 kyu3 Exp $";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/param.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
71 * WARNING: Don't even consider trying to compile this on a system where
72 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
75 static int inet_pton4 (const char *src
, u_char
*dst
);
76 static int inet_pton6 (const char *src
, u_char
*dst
);
79 * inet_pton(af, src, dst)
80 * convert from presentation format (which usually means ASCII printable)
81 * to network format (which is usually some kind of binary format).
83 * 1 if the address was valid for the specified address family
84 * 0 if the address wasn't valid (`dst' is untouched in this case)
85 * -1 if some other error occurred (`dst' is untouched in this case, too)
98 return (inet_pton4(src
, dst
));
100 return (inet_pton6(src
, dst
));
102 errno
= EAFNOSUPPORT
;
109 * inet_pton4(src, dst)
110 * like inet_aton() but without all the hexadecimal and shorthand.
112 * 1 if `src' is a valid dotted quad, else 0.
114 * does not touch `dst' unless it's returning 1.
124 static const char digits
[] = "0123456789";
125 int saw_digit
, octets
, ch
;
126 u_char tmp
[NS_INADDRSZ
], *tp
;
131 while ((ch
= *src
++) != '\0') {
134 if ((pch
= strchr(digits
, ch
)) != NULL
) {
135 u_int
new = *tp
* 10 + (u_int
)(pch
- digits
);
145 } else if (ch
== '.' && saw_digit
) {
156 memcpy(dst
, tmp
, NS_INADDRSZ
);
161 * inet_pton6(src, dst)
162 * convert presentation level address to network order binary form.
164 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
166 * (1) does not touch `dst' unless it's returning 1.
167 * (2) :: in a full address is silently ignored.
169 * inspired by Mark Andrews.
179 static const char xdigits_l
[] = "0123456789abcdef",
180 xdigits_u
[] = "0123456789ABCDEF";
181 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
182 const char *xdigits
, *curtok
;
186 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
187 endp
= tp
+ NS_IN6ADDRSZ
;
189 /* Leading :: requires some special handling. */
196 while ((ch
= *src
++) != '\0') {
199 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
200 pch
= strchr((xdigits
= xdigits_u
), ch
);
203 val
|= (pch
- xdigits
);
217 if (tp
+ NS_INT16SZ
> endp
)
219 *tp
++ = (u_char
) (val
>> 8) & 0xff;
220 *tp
++ = (u_char
) val
& 0xff;
225 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
226 inet_pton4(curtok
, tp
) > 0) {
229 break; /* '\0' was seen by inet_pton4(). */
234 if (tp
+ NS_INT16SZ
> endp
)
236 *tp
++ = (u_char
) (val
>> 8) & 0xff;
237 *tp
++ = (u_char
) val
& 0xff;
239 if (colonp
!= NULL
) {
241 * Since some memmove()'s erroneously fail to handle
242 * overlapping regions, we'll do the shift by hand.
244 const int n
= (int)(tp
- colonp
);
247 for (i
= 1; i
<= n
; i
++) {
248 endp
[- i
] = colonp
[n
- i
];
255 memcpy(dst
, tmp
, NS_IN6ADDRSZ
);