]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/inet_pton.cc
2 * Copyright (C) 1996-2018 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 * 13-Jan-2015 : Various fixed for C++ and MinGW native build
26 * Original License and code follows.
31 #if !HAVE_DECL_INET_PTON
34 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
35 * Copyright (c) 1996,1999 by Internet Software Consortium.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
41 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
47 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
50 #if defined(LIBC_SCCS) && !defined(lint)
51 static const char rcsid
[] = "inet_pton.c,v 1.2.206.2 2005/07/28 07:43:18 marka Exp";
52 #endif /* LIBC_SCCS and not lint */
55 #include <sys/param.h>
58 #include <sys/types.h>
61 #include <sys/socket.h>
64 #include <netinet/in.h>
67 #include <arpa/inet.h>
69 #if HAVE_ARPA_NAMESER_H
70 #include <arpa/nameser.h>
79 #if ! defined(NS_INADDRSZ)
82 #if ! defined(NS_IN6ADDRSZ)
83 #define NS_IN6ADDRSZ 16
85 #if ! defined(NS_INT16SZ)
90 * WARNING: Don't even consider trying to compile this on a system where
91 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
94 static int inet_pton4 (const char *src
, u_char
*dst
);
95 static int inet_pton6 (const char *src
, u_char
*dst
);
98 * inet_pton(af, src, dst)
99 * convert from presentation format (which usually means ASCII printable)
100 * to network format (which is usually some kind of binary format).
102 * 1 if the address was valid for the specified address family
103 * 0 if the address wasn't valid (`dst' is untouched in this case)
104 * -1 if some other error occurred (`dst' is untouched in this case, too)
109 xinet_pton(int af
, const char *src
, void *dst
)
113 return (inet_pton4(src
, (u_char
*)dst
));
115 return (inet_pton6(src
, (u_char
*)dst
));
117 errno
= EAFNOSUPPORT
;
124 * inet_pton4(src, dst)
125 * like inet_aton() but without all the hexadecimal and shorthand.
127 * 1 if `src' is a valid dotted quad, else 0.
129 * does not touch `dst' unless it's returning 1.
134 inet_pton4(const char *src
, u_char
*dst
)
136 static const char digits
[] = "0123456789";
137 int saw_digit
, octets
, ch
;
138 u_char tmp
[NS_INADDRSZ
], *tp
;
143 while ((ch
= *src
++) != '\0') {
146 if ((pch
= strchr(digits
, ch
)) != NULL
) {
147 u_int nw
= *tp
* 10 + (pch
- digits
);
149 if (saw_digit
&& *tp
== 0)
159 } else if (ch
== '.' && saw_digit
) {
169 memcpy(dst
, tmp
, NS_INADDRSZ
);
174 * inet_pton6(src, dst)
175 * convert presentation level address to network order binary form.
177 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
179 * (1) does not touch `dst' unless it's returning 1.
180 * (2) :: in a full address is silently ignored.
182 * inspired by Mark Andrews.
187 inet_pton6(const char *src
, u_char
*dst
)
189 static const char xdigits_l
[] = "0123456789abcdef",
190 xdigits_u
[] = "0123456789ABCDEF";
191 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
192 const char *xdigits
, *curtok
;
193 int ch
, seen_xdigits
;
196 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
197 endp
= tp
+ NS_IN6ADDRSZ
;
199 /* Leading :: requires some special handling. */
206 while ((ch
= *src
++) != '\0') {
209 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
210 pch
= strchr((xdigits
= xdigits_u
), ch
);
213 val
|= (pch
- xdigits
);
214 if (++seen_xdigits
> 4)
225 } else if (*src
== '\0') {
228 if (tp
+ NS_INT16SZ
> endp
)
230 *tp
++ = (u_char
) (val
>> 8) & 0xff;
231 *tp
++ = (u_char
) val
& 0xff;
236 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
237 inet_pton4(curtok
, tp
) > 0) {
240 break; /* '\0' was seen by inet_pton4(). */
245 if (tp
+ NS_INT16SZ
> endp
)
247 *tp
++ = (u_char
) (val
>> 8) & 0xff;
248 *tp
++ = (u_char
) val
& 0xff;
250 if (colonp
!= NULL
) {
252 * Since some memmove()'s erroneously fail to handle
253 * overlapping regions, we'll do the shift by hand.
255 const int n
= tp
- colonp
;
260 for (i
= 1; i
<= n
; i
++) {
261 endp
[- i
] = colonp
[n
- i
];
268 memcpy(dst
, tmp
, NS_IN6ADDRSZ
);
272 #endif /* HAVE_DECL_INET_PTON */