2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 #include <sys/cdefs.h>
19 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: inet_net_pton.c,v 1.4.2.1 2002/08/02 02:17:21 marka Exp ";
27 #include "port_before.h"
29 #include "namespace.h"
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
34 #include <arpa/inet.h>
36 #include <isc/assertions.h>
43 #include "port_after.h"
46 __weak_alias(inet_net_pton
,_inet_net_pton
)
50 # define SPRINTF(x) strlen(sprintf/**/x)
52 # define SPRINTF(x) ((size_t)sprintf x)
58 * inet_net_pton_ipv4(src, dst, size)
59 * convert IPv4 network number from presentation to network format.
60 * accepts hex octets, hex strings, decimal octets, and /CIDR.
61 * "size" is in bytes and describes "dst".
63 * number of bits, either imputed classfully or specified with /CIDR,
64 * or -1 if some failure occurred (check errno). ENOENT means it was
65 * not an IPv4 network specification.
67 * network byte order assumed. this means 192.5.5.240/28 has
68 * 0b11110000 in its fourth octet.
70 * Paul Vixie (ISC), June 1996
73 inet_net_pton_ipv4( const char *src
, u_char
*dst
, size_t size
) {
74 static const char xdigits
[] = "0123456789abcdef";
75 static const char digits
[] = "0123456789";
76 int n
, ch
, tmp
= 0, dirty
, bits
;
77 const u_char
*odst
= dst
;
80 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
81 && isascii((u_char
)(src
[1]))
82 && isxdigit((u_char
)(src
[1]))) {
83 /* Hexadecimal: Eat nybble string. */
87 src
++; /* skip x or X. */
88 while ((ch
= *src
++) != '\0' && isascii((u_char
)ch
)
89 && isxdigit((u_char
)ch
)) {
90 if (isupper((u_char
)ch
))
91 ch
= tolower((u_char
)ch
);
92 n
= strchr(xdigits
, ch
) - xdigits
;
93 INSIST(n
>= 0 && n
<= 15);
101 *dst
++ = (u_char
) tmp
;
105 if (dirty
) { /* Odd trailing nybble? */
108 *dst
++ = (u_char
) (tmp
<< 4);
110 } else if (isascii((u_char
)ch
) && isdigit((u_char
)ch
)) {
111 /* Decimal: eat dotted digit string. */
115 n
= strchr(digits
, ch
) - digits
;
116 INSIST(n
>= 0 && n
<= 9);
121 } while ((ch
= *src
++) != '\0' &&
122 isascii((u_char
)ch
) && isdigit((u_char
)ch
));
125 *dst
++ = (u_char
) tmp
;
126 if (ch
== '\0' || ch
== '/')
131 if (!isascii((u_char
)ch
) || !isdigit((u_char
)ch
))
138 if (ch
== '/' && isascii((u_char
)(src
[0])) &&
139 isdigit((u_char
)(src
[0])) && dst
> odst
) {
140 /* CIDR width specifier. Nothing can follow it. */
141 ch
= *src
++; /* Skip over the /. */
144 n
= strchr(digits
, ch
) - digits
;
145 INSIST(n
>= 0 && n
<= 9);
148 } while ((ch
= *src
++) != '\0' && isascii((u_char
)ch
)
149 && isdigit((u_char
)ch
));
156 /* Firey death and destruction unless we prefetched EOS. */
160 /* If nothing was written to the destination, we found no address. */
163 /* If no CIDR spec was given, infer width from net class. */
165 if (*odst
>= 240) /* Class E */
167 else if (*odst
>= 224) /* Class D */
169 else if (*odst
>= 192) /* Class C */
171 else if (*odst
>= 128) /* Class B */
175 /* If imputed mask is narrower than specified octets, widen. */
176 if (bits
>= 8 && bits
< ((dst
- odst
) * 8))
177 bits
= (dst
- odst
) * 8;
179 /* Extend network to cover the actual mask. */
180 while (bits
> ((dst
- odst
) * 8)) {
197 getbits(const char *src
, int *bitsp
) {
198 static const char digits
[] = "0123456789";
205 while ((ch
= *src
++) != '\0') {
208 pch
= strchr(digits
, ch
);
210 if (n
++ != 0 && val
== 0) /* no leading zeros */
213 val
+= (pch
- digits
);
214 if (val
> 128) /* range */
227 getv4(const char *src
, u_char
*dst
, int *bitsp
) {
228 static const char digits
[] = "0123456789";
236 while ((ch
= *src
++) != '\0') {
239 pch
= strchr(digits
, ch
);
241 if (n
++ != 0 && val
== 0) /* no leading zeros */
244 val
+= (pch
- digits
);
245 if (val
> 255) /* range */
249 if (ch
== '.' || ch
== '/') {
250 if (dst
- odst
> 3) /* too many octets? */
254 return (getbits(src
, bitsp
));
263 if (dst
- odst
> 3) /* too many octets? */
270 inet_net_pton_ipv6(const char *src
, u_char
*dst
, size_t size
) {
271 static const char xdigits_l
[] = "0123456789abcdef",
272 xdigits_u
[] = "0123456789ABCDEF";
273 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
274 const char *xdigits
, *curtok
;
283 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
284 endp
= tp
+ NS_IN6ADDRSZ
;
286 /* Leading :: requires some special handling. */
296 while ((ch
= *src
++) != '\0') {
299 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
300 pch
= strchr((xdigits
= xdigits_u
), ch
);
303 val
|= (pch
- xdigits
);
316 } else if (*src
== '\0')
318 if (tp
+ NS_INT16SZ
> endp
)
320 *tp
++ = (u_char
) (val
>> 8) & 0xff;
321 *tp
++ = (u_char
) val
& 0xff;
327 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
328 getv4(curtok
, tp
, &bits
) > 0) {
332 break; /* '\0' was seen by inet_pton4(). */
334 if (ch
== '/' && getbits(src
, &bits
) > 0)
339 if (tp
+ NS_INT16SZ
> endp
)
341 *tp
++ = (u_char
) (val
>> 8) & 0xff;
342 *tp
++ = (u_char
) val
& 0xff;
347 words
= (bits
+ 15) / 16;
352 endp
= tmp
+ 2 * words
;
354 if (colonp
!= NULL
) {
356 * Since some memmove()'s erroneously fail to handle
357 * overlapping regions, we'll do the shift by hand.
359 const int n
= tp
- colonp
;
364 for (i
= 1; i
<= n
; i
++) {
365 endp
[- i
] = colonp
[n
- i
];
373 bytes
= (bits
+ 7) / 8;
376 memcpy(dst
, tmp
, bytes
);
390 * inet_net_pton(af, src, dst, size)
391 * convert network number from presentation to network format.
392 * accepts hex octets, hex strings, decimal octets, and /CIDR.
393 * "size" is in bytes and describes "dst".
395 * number of bits, either imputed classfully or specified with /CIDR,
396 * or -1 if some failure occurred (check errno). ENOENT means it was
397 * not a valid network specification.
399 * Paul Vixie (ISC), June 1996
402 inet_net_pton(int af
, const char *src
, void *dst
, size_t size
) {
405 return (inet_net_pton_ipv4(src
, dst
, size
));
407 return (inet_net_pton_ipv6(src
, dst
, size
));
409 errno
= EAFNOSUPPORT
;