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/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
31 # define SPRINTF(x) strlen(sprintf/**/x)
33 # define SPRINTF(x) ((size_t)sprintf x)
36 static int inet_net_pton_ipv4 (const char *src
, u_char
*dst
,
41 * inet_net_pton(af, src, dst, size)
42 * convert network number from presentation to network format.
43 * accepts hex octets, hex strings, decimal octets, and /CIDR.
44 * "size" is in bytes and describes "dst".
46 * number of bits, either imputed classfully or specified with /CIDR,
47 * or -1 if some failure occurred (check errno). ENOENT means it was
48 * not a valid network specification.
50 * Paul Vixie (ISC), June 1996
53 inet_net_pton (int af
, const char *src
, void *dst
, size_t size
)
57 return (inet_net_pton_ipv4(src
, dst
, size
));
59 __set_errno (EAFNOSUPPORT
);
66 * inet_net_pton_ipv4(src, dst, size)
67 * convert IPv4 network number from presentation to network format.
68 * accepts hex octets, hex strings, decimal octets, and /CIDR.
69 * "size" is in bytes and describes "dst".
71 * number of bits, either imputed classfully or specified with /CIDR,
72 * or -1 if some failure occurred (check errno). ENOENT means it was
73 * not an IPv4 network specification.
75 * network byte order assumed. this means 192.5.5.240/28 has
76 * 0b11110000 in its fourth octet.
78 * Paul Vixie (ISC), June 1996
81 inet_net_pton_ipv4 (const char *src
, u_char
*dst
, size_t size
)
83 static const char xdigits
[] = "0123456789abcdef";
84 int n
, ch
, tmp
, dirty
, bits
;
85 const u_char
*odst
= dst
;
88 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
89 && isascii(src
[1]) && isxdigit(src
[1])) {
90 /* Hexadecimal: Eat nybble string. */
94 tmp
= 0; /* To calm down gcc. */
95 src
++; /* skip x or X. */
96 while (isxdigit((ch
= *src
++))) {
98 n
= (const char *) __rawmemchr(xdigits
, ch
) - xdigits
;
99 assert(n
>= 0 && n
<= 15);
103 tmp
= (tmp
<< 4) | n
;
107 *dst
++ = (u_char
) tmp
;
111 if (dirty
) { /* Odd trailing nybble? */
114 *dst
++ = (u_char
) (tmp
<< 4);
116 } else if (isascii(ch
) && isdigit(ch
)) {
117 /* Decimal: eat dotted digit string. */
121 n
= ((const char *) __rawmemchr(xdigits
, ch
)
123 assert(n
>= 0 && n
<= 9);
128 } while (isascii((ch
= *src
++)) && isdigit(ch
));
131 *dst
++ = (u_char
) tmp
;
132 if (ch
== '\0' || ch
== '/')
137 if (!isascii(ch
) || !isdigit(ch
))
144 if (ch
== '/' && isascii(src
[0]) && isdigit(src
[0]) && dst
> odst
) {
145 /* CIDR width specifier. Nothing can follow it. */
146 ch
= *src
++; /* Skip over the /. */
149 n
= (const char *) __rawmemchr(xdigits
, ch
) - xdigits
;
150 assert(n
>= 0 && n
<= 9);
153 } while (isascii((ch
= *src
++)) && isdigit(ch
));
160 /* Firey death and destruction unless we prefetched EOS. */
164 /* If nothing was written to the destination, we found no address. */
167 /* If no CIDR spec was given, infer width from net class. */
169 if (*odst
>= 240) /* Class E */
171 else if (*odst
>= 224) /* Class D */
173 else if (*odst
>= 192) /* Class C */
175 else if (*odst
>= 128) /* Class B */
179 /* If imputed mask is narrower than specified octets, widen. */
180 if (bits
>= 8 && bits
< ((dst
- odst
) * 8))
181 bits
= (dst
- odst
) * 8;
183 /* Extend network to cover the actual mask. */
184 while (bits
> ((dst
- odst
) * 8)) {
192 __set_errno (ENOENT
);
196 __set_errno (EMSGSIZE
);