2
* Shamelessly duplicated from the bind9 public sources
3
* for use by the Squid Project under ISC written permission
4
* included "as found" below.
6
* Update/Maintenance History:
8
* 24-Sep-2007 : Copied from bind 9.3.3
9
* - Added protection around libray headers
10
* - Altered configure checks to import
12
* 06-Oct-2007 : Various fixes to allow the build on MinGW
14
* 28-Oct-2007: drop some dead code. now tested working without.
18
* Original License and code follows.
23
#ifndef HAVE_INET_PTON
26
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
27
* Copyright (c) 1996,1999 by Internet Software Consortium.
29
* Permission to use, copy, modify, and distribute this software for any
30
* purpose with or without fee is hereby granted, provided that the above
31
* copyright notice and this permission notice appear in all copies.
33
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
34
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
35
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
36
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
37
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
38
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
39
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42
#if defined(LIBC_SCCS) && !defined(lint)
43
static const char rcsid[] = "inet_pton.c,v 1.2.206.2 2005/07/28 07:43:18 marka Exp";
44
#endif /* LIBC_SCCS and not lint */
47
#include <sys/param.h>
50
#include <sys/types.h>
53
#include <sys/socket.h>
56
#include <netinet/in.h>
59
#include <arpa/inet.h>
61
#if HAVE_ARPA_NAMESER_H
62
#include <arpa/nameser.h>
71
#if ! defined(NS_INADDRSZ)
74
#if ! defined(NS_IN6ADDRSZ)
75
#define NS_IN6ADDRSZ 16
77
#if ! defined(NS_INT16SZ)
82
* WARNING: Don't even consider trying to compile this on a system where
83
* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
86
static int inet_pton4 (const char *src, u_char *dst);
87
static int inet_pton6 (const char *src, u_char *dst);
90
* inet_pton(af, src, dst)
91
* convert from presentation format (which usually means ASCII printable)
92
* to network format (which is usually some kind of binary format).
94
* 1 if the address was valid for the specified address family
95
* 0 if the address wasn't valid (`dst' is untouched in this case)
96
* -1 if some other error occurred (`dst' is untouched in this case, too)
101
xinet_pton(af, src, dst)
108
return (inet_pton4(src, dst));
110
return (inet_pton6(src, dst));
112
errno = EAFNOSUPPORT;
119
* inet_pton4(src, dst)
120
* like inet_aton() but without all the hexadecimal and shorthand.
122
* 1 if `src' is a valid dotted quad, else 0.
124
* does not touch `dst' unless it's returning 1.
133
static const char digits[] = "0123456789";
134
int saw_digit, octets, ch;
135
u_char tmp[NS_INADDRSZ], *tp;
140
while ((ch = *src++) != '\0') {
143
if ((pch = strchr(digits, ch)) != NULL) {
144
u_int new = *tp * 10 + (pch - digits);
146
if (saw_digit && *tp == 0)
156
} else if (ch == '.' && saw_digit) {
166
memcpy(dst, tmp, NS_INADDRSZ);
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.
188
static const char xdigits_l[] = "0123456789abcdef",
189
xdigits_u[] = "0123456789ABCDEF";
190
u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
191
const char *xdigits, *curtok;
192
int ch, seen_xdigits;
195
memset((tp = tmp), '\0', NS_IN6ADDRSZ);
196
endp = tp + NS_IN6ADDRSZ;
198
/* Leading :: requires some special handling. */
205
while ((ch = *src++) != '\0') {
208
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
209
pch = strchr((xdigits = xdigits_u), ch);
212
val |= (pch - xdigits);
213
if (++seen_xdigits > 4)
224
} else if (*src == '\0') {
227
if (tp + NS_INT16SZ > endp)
229
*tp++ = (u_char) (val >> 8) & 0xff;
230
*tp++ = (u_char) val & 0xff;
235
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
236
inet_pton4(curtok, tp) > 0) {
239
break; /* '\0' was seen by inet_pton4(). */
244
if (tp + NS_INT16SZ > endp)
246
*tp++ = (u_char) (val >> 8) & 0xff;
247
*tp++ = (u_char) val & 0xff;
249
if (colonp != NULL) {
251
* Since some memmove()'s erroneously fail to handle
252
* overlapping regions, we'll do the shift by hand.
254
const int n = tp - colonp;
259
for (i = 1; i <= n; i++) {
260
endp[- i] = colonp[n - i];
267
memcpy(dst, tmp, NS_IN6ADDRSZ);
271
#endif /* HAVE_INET_PTON */