1 //==========================================================================
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $KAME: inet_pton.c,v 1.5 2001/08/20 02:32:40 itojun Exp $ */
25 * Copyright (c) 1996,1999 by Internet Software Consortium.
27 * Permission to use, copy, modify, and distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
31 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
32 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
34 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
35 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
36 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
37 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41 //#if defined(LIBC_SCCS) && !defined(lint)
42 //static char rcsid[] = "$Id: inet_pton.c,v 1.3 2002/02/20 17:39:39 gthomas Exp $";
43 //#endif /* LIBC_SCCS and not lint */
45 //#ifdef HAVE_CONFIG_H
49 #include <sys/param.h>
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <arpa/nameser.h>
58 //#ifndef HAVE_PORTABLE_PROTOTYPE
59 //#include "cdecl_ext.h"
62 //#ifndef HAVE_U_INT16_T
63 //#include "bittypes.h"
66 //#if !(defined(HAVE_INADDRSZ) && defined(HAVE_IN6ADDRSZ))
67 //#include "addrsize.h"
70 #define NS_INADDRSZ 4 /* IPv4 T_A */
71 #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
74 #define NS_INADDRSZ INADDRSZ
77 #define NS_IN6ADDRSZ IN6ADDRSZ
80 #define NS_INT16SZ sizeof(u_int16_t)
84 * WARNING: Don't even consider trying to compile this on a system where
85 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
88 static int inet_pton4 __P((const char *src, u_char *dst));
89 #ifdef CYGPKG_NET_INET6
90 static int inet_pton6 __P((const char *src, u_char *dst));
94 * inet_pton(af, src, dst)
95 * convert from presentation format (which usually means ASCII printable)
96 * to network format (which is usually some kind of binary format).
98 * 1 if the address was valid for the specified address family
99 * 0 if the address wasn't valid (`dst' is untouched in this case)
100 * -1 if some other error occurred (`dst' is untouched in this case, too)
105 inet_pton(int af, const char *src, void *dst)
109 return (inet_pton4(src, dst));
110 #ifdef CYGPKG_NET_INET6
112 return (inet_pton6(src, dst));
115 errno = EAFNOSUPPORT;
122 * inet_pton4(src, dst)
123 * like inet_aton() but without all the hexadecimal and shorthand.
125 * 1 if `src' is a valid dotted quad, else 0.
127 * does not touch `dst' unless it's returning 1.
132 inet_pton4(const char *src, u_char *dst)
134 static const char digits[] = "0123456789";
135 int saw_digit, octets, ch;
136 u_char tmp[NS_INADDRSZ], *tp;
141 while ((ch = *src++) != '\0') {
144 if ((pch = strchr(digits, ch)) != NULL) {
145 u_int new = *tp * 10 + (pch - digits);
155 } else if (ch == '.' && saw_digit) {
165 memcpy(dst, tmp, NS_INADDRSZ);
169 #ifdef CYGPKG_NET_INET6
171 * inet_pton6(src, dst)
172 * convert presentation level address to network order binary form.
174 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
176 * (1) does not touch `dst' unless it's returning 1.
177 * (2) :: in a full address is silently ignored.
179 * inspired by Mark Andrews.
184 inet_pton6(const char *src, u_char *dst)
186 static const char xdigits_l[] = "0123456789abcdef",
187 xdigits_u[] = "0123456789ABCDEF";
188 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
189 const char *xdigits, *curtok;
193 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
194 endp = tp + NS_IN6ADDRSZ;
196 /* Leading :: requires some special handling. */
203 while ((ch = *src++) != '\0') {
206 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
207 pch = strchr((xdigits = xdigits_u), ch);
210 val |= (pch - xdigits);
223 } else if (*src == '\0') {
226 if (tp + NS_INT16SZ > endp)
228 *tp++ = (u_char) (val >> 8) & 0xff;
229 *tp++ = (u_char) val & 0xff;
234 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
235 inet_pton4(curtok, tp) > 0) {
238 break; /* '\0' was seen by inet_pton4(). */
243 if (tp + NS_INT16SZ > endp)
245 *tp++ = (u_char) (val >> 8) & 0xff;
246 *tp++ = (u_char) val & 0xff;
248 if (colonp != NULL) {
250 * Since some memmove()'s erroneously fail to handle
251 * overlapping regions, we'll do the shift by hand.
253 const int n = tp - colonp;
258 for (i = 1; i <= n; i++) {
259 endp[- i] = colonp[n - i];
266 memcpy(dst, tmp, NS_IN6ADDRSZ);