]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/inet_pton.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * Shamelessly duplicated from the bind9 public sources
11 * for use by the Squid Project under ISC written permission
12 * included "as found" below.
14 * Update/Maintenance History:
16 * 24-Sep-2007 : Copied from bind 9.3.3
17 * - Added protection around libray headers
18 * - Altered configure checks to import
20 * 06-Oct-2007 : Various fixes to allow the build on MinGW
22 * 28-Oct-2007: drop some dead code. now tested working without.
24 * Original License and code follows.
29 #if !HAVE_DECL_INET_PTON
32 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
33 * Copyright (c) 1996,1999 by Internet Software Consortium.
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
39 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
45 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 #if defined(LIBC_SCCS) && !defined(lint)
49 static const char rcsid
[] = "inet_pton.c,v 1.2.206.2 2005/07/28 07:43:18 marka Exp";
50 #endif /* LIBC_SCCS and not lint */
53 #include <sys/param.h>
56 #include <sys/types.h>
59 #include <sys/socket.h>
62 #include <netinet/in.h>
65 #include <arpa/inet.h>
67 #if HAVE_ARPA_NAMESER_H
68 #include <arpa/nameser.h>
77 #if ! defined(NS_INADDRSZ)
80 #if ! defined(NS_IN6ADDRSZ)
81 #define NS_IN6ADDRSZ 16
83 #if ! defined(NS_INT16SZ)
88 * WARNING: Don't even consider trying to compile this on a system where
89 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
92 static int inet_pton4 (const char *src
, u_char
*dst
);
93 static int inet_pton6 (const char *src
, u_char
*dst
);
96 * inet_pton(af, src, dst)
97 * convert from presentation format (which usually means ASCII printable)
98 * to network format (which is usually some kind of binary format).
100 * 1 if the address was valid for the specified address family
101 * 0 if the address wasn't valid (`dst' is untouched in this case)
102 * -1 if some other error occurred (`dst' is untouched in this case, too)
107 xinet_pton(af
, src
, dst
)
114 return (inet_pton4(src
, dst
));
116 return (inet_pton6(src
, dst
));
118 errno
= EAFNOSUPPORT
;
125 * inet_pton4(src, dst)
126 * like inet_aton() but without all the hexadecimal and shorthand.
128 * 1 if `src' is a valid dotted quad, else 0.
130 * does not touch `dst' unless it's returning 1.
139 static const char digits
[] = "0123456789";
140 int saw_digit
, octets
, ch
;
141 u_char tmp
[NS_INADDRSZ
], *tp
;
146 while ((ch
= *src
++) != '\0') {
149 if ((pch
= strchr(digits
, ch
)) != NULL
) {
150 u_int
new = *tp
* 10 + (pch
- digits
);
152 if (saw_digit
&& *tp
== 0)
162 } else if (ch
== '.' && saw_digit
) {
172 memcpy(dst
, tmp
, NS_INADDRSZ
);
177 * inet_pton6(src, dst)
178 * convert presentation level address to network order binary form.
180 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
182 * (1) does not touch `dst' unless it's returning 1.
183 * (2) :: in a full address is silently ignored.
185 * inspired by Mark Andrews.
194 static const char xdigits_l
[] = "0123456789abcdef",
195 xdigits_u
[] = "0123456789ABCDEF";
196 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
197 const char *xdigits
, *curtok
;
198 int ch
, seen_xdigits
;
201 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
202 endp
= tp
+ NS_IN6ADDRSZ
;
204 /* Leading :: requires some special handling. */
211 while ((ch
= *src
++) != '\0') {
214 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
215 pch
= strchr((xdigits
= xdigits_u
), ch
);
218 val
|= (pch
- xdigits
);
219 if (++seen_xdigits
> 4)
230 } else if (*src
== '\0') {
233 if (tp
+ NS_INT16SZ
> endp
)
235 *tp
++ = (u_char
) (val
>> 8) & 0xff;
236 *tp
++ = (u_char
) val
& 0xff;
241 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
242 inet_pton4(curtok
, tp
) > 0) {
245 break; /* '\0' was seen by inet_pton4(). */
250 if (tp
+ NS_INT16SZ
> endp
)
252 *tp
++ = (u_char
) (val
>> 8) & 0xff;
253 *tp
++ = (u_char
) val
& 0xff;
255 if (colonp
!= NULL
) {
257 * Since some memmove()'s erroneously fail to handle
258 * overlapping regions, we'll do the shift by hand.
260 const int n
= tp
- colonp
;
265 for (i
= 1; i
<= n
; i
++) {
266 endp
[- i
] = colonp
[n
- i
];
273 memcpy(dst
, tmp
, NS_IN6ADDRSZ
);
277 #endif /* HAVE_DECL_INET_PTON */