]>
git.saurik.com Git - apple/libc.git/blob - net/FreeBSD/inet_net_pton.c
f2aebcc0ce25f0e70a8accfbf3bb68e43cd1def8
2 * Copyright (c) 1996 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 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char orig_rcsid
[] = "From Id: inet_net_pton.c,v 1.8 1996/11/21 10:28:12 vixie Exp $";
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD: src/lib/libc/net/inet_net_pton.c,v 1.8 2002/03/22 21:52:29 obrien Exp $");
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
37 # define SPRINTF(x) strlen(sprintf/**/x)
39 # define SPRINTF(x) ((size_t)sprintf x)
42 static int inet_net_pton_ipv4(const char *src
, u_char
*dst
, size_t size
);
46 * inet_net_pton(af, src, dst, size)
47 * convert network number from presentation to network format.
48 * accepts hex octets, hex strings, decimal octets, and /CIDR.
49 * "size" is in bytes and describes "dst".
51 * number of bits, either imputed classfully or specified with /CIDR,
52 * or -1 if some failure occurred (check errno). ENOENT means it was
53 * not a valid network specification.
55 * Paul Vixie (ISC), June 1996
58 inet_net_pton(af
, src
, dst
, size
)
66 return (inet_net_pton_ipv4(src
, dst
, size
));
75 * inet_net_pton_ipv4(src, dst, size)
76 * convert IPv4 network number from presentation to network format.
77 * accepts hex octets, hex strings, decimal octets, and /CIDR.
78 * "size" is in bytes and describes "dst".
80 * number of bits, either imputed classfully or specified with /CIDR,
81 * or -1 if some failure occurred (check errno). ENOENT means it was
82 * not an IPv4 network specification.
84 * network byte order assumed. this means 192.5.5.240/28 has
85 * 0x11110000 in its fourth octet.
87 * Paul Vixie (ISC), June 1996
90 inet_net_pton_ipv4(src
, dst
, size
)
96 xdigits
[] = "0123456789abcdef",
97 digits
[] = "0123456789";
98 int n
, ch
, tmp
, dirty
, bits
;
99 const u_char
*odst
= dst
;
102 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
103 && isascii(src
[1]) && isxdigit(src
[1])) {
104 /* Hexadecimal: Eat nybble string. */
108 src
++; /* skip x or X. */
109 while ((ch
= *src
++) != '\0' &&
110 isascii(ch
) && isxdigit(ch
)) {
113 n
= strchr(xdigits
, ch
) - xdigits
;
114 assert(n
>= 0 && n
<= 15);
119 *++dst
= 0, dirty
= 0;
125 } else if (isascii(ch
) && isdigit(ch
)) {
126 /* Decimal: eat dotted digit string. */
130 n
= strchr(digits
, ch
) - digits
;
131 assert(n
>= 0 && n
<= 9);
136 } while ((ch
= *src
++) != '\0' &&
137 isascii(ch
) && isdigit(ch
));
140 *dst
++ = (u_char
) tmp
;
141 if (ch
== '\0' || ch
== '/')
146 if (!isascii(ch
) || !isdigit(ch
))
153 if (ch
== '/' && isascii(src
[0]) && isdigit(src
[0]) && dst
> odst
) {
154 /* CIDR width specifier. Nothing can follow it. */
155 ch
= *src
++; /* Skip over the /. */
158 n
= strchr(digits
, ch
) - digits
;
159 assert(n
>= 0 && n
<= 9);
162 } while ((ch
= *src
++) != '\0' && isascii(ch
) && isdigit(ch
));
169 /* Firey death and destruction unless we prefetched EOS. */
173 /* If nothing was written to the destination, we found no address. */
176 /* If no CIDR spec was given, infer width from net class. */
178 if (*odst
>= 240) /* Class E */
180 else if (*odst
>= 224) /* Class D */
182 else if (*odst
>= 192) /* Class C */
184 else if (*odst
>= 128) /* Class B */
188 /* If imputed mask is narrower than specified octets, widen. */
189 if (bits
>= 8 && bits
< ((dst
- odst
) * 8))
190 bits
= (dst
- odst
) * 8;
192 /* Extend network to cover the actual mask. */
193 while (bits
> ((dst
- odst
) * 8)) {
210 * Weak aliases for applications that use certain private entry points,
211 * and fail to include <arpa/inet.h>.
214 __weak_reference(__inet_net_pton
, inet_net_pton
);