2 * Copyright (c) 1996,1999 by Internet Software Consortium.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 #ifdef HAVE_SYS_PARAM_H
25 #include <sys/param.h>
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
36 #ifdef HAVE_WINSOCK2_H
37 #include <winsock2.h> /* needed to define AF_ values on Windows */
38 #if _MSC_VER < 1600 /* errno.h defines EAFNOSUPPORT in Windows VC10 (and presumably eventually in VC11 ...) */
39 #define EAFNOSUPPORT WSAEAFNOSUPPORT
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
47 #ifdef HAVE_ARPA_INET_H
48 #include <arpa/inet.h>
51 #ifdef HAVE_ARPA_NAMESER_H
52 #include <arpa/nameser.h>
58 #include "inet_v6defs.h"
64 #define NS_IN6ADDRSZ 16
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.
76 static int inet_pton4 __P((const char *src, u_char *dst));
79 static int inet_pton6 __P((const char *src, u_char *dst));
83 * inet_pton(af, src, dst)
84 * convert from presentation format (which usually means ASCII printable)
85 * to network format (which is usually some kind of binary format).
87 * 1 if the address was valid for the specified address family
88 * 0 if the address wasn't valid (`dst' is untouched in this case)
89 * -1 if some other error occurred (`dst' is untouched in this case, too)
94 inet_pton(af, src, dst)
102 return (inet_pton4(src, dst));
106 return (inet_pton6(src, dst));
109 errno = EAFNOSUPPORT;
117 * inet_pton4(src, dst)
118 * like inet_aton() but without all the hexadecimal and shorthand.
120 * 1 if `src' is a valid dotted quad, else 0.
122 * does not touch `dst' unless it's returning 1.
131 static const char digits[] = "0123456789";
132 int saw_digit, octets, ch;
133 u_char tmp[NS_INADDRSZ], *tp;
138 while ((ch = *src++) != '\0') {
141 if ((pch = strchr(digits, ch)) != NULL) {
142 size_t new = *tp * 10 + (pch - digits);
152 } else if (ch == '.' && saw_digit) {
162 memcpy(dst, tmp, NS_INADDRSZ);
169 * inet_pton6(src, dst)
170 * convert presentation level address to network order binary form.
172 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
174 * (1) does not touch `dst' unless it's returning 1.
175 * (2) :: in a full address is silently ignored.
177 * inspired by Mark Andrews.
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 = (int) (tp - colonp);
258 for (i = 1; i <= n; i++) {
259 endp[- i] = colonp[n - i];
266 memcpy(dst, tmp, NS_IN6ADDRSZ);