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 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$BINDId: inet_net_pton.c,v 1.11 1999/01/08 19:23:44 vixie Exp $";
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
35 # define SPRINTF(x) strlen(sprintf/**/x)
37 # define SPRINTF(x) ((size_t)sprintf x)
40 static int inet_net_pton_ipv4 (const char *src
, u_char
*dst
,
45 * inet_net_pton(af, src, dst, size)
46 * convert network number from presentation to network format.
47 * accepts hex octets, hex strings, decimal octets, and /CIDR.
48 * "size" is in bytes and describes "dst".
50 * number of bits, either imputed classfully or specified with /CIDR,
51 * or -1 if some failure occurred (check errno). ENOENT means it was
52 * not a valid network specification.
54 * Paul Vixie (ISC), June 1996
57 inet_net_pton (int af
, const char *src
, void *dst
, size_t size
)
61 return (inet_net_pton_ipv4(src
, dst
, size
));
63 __set_errno (EAFNOSUPPORT
);
70 * inet_net_pton_ipv4(src, dst, size)
71 * convert IPv4 network number from presentation to network format.
72 * accepts hex octets, hex strings, decimal octets, and /CIDR.
73 * "size" is in bytes and describes "dst".
75 * number of bits, either imputed classfully or specified with /CIDR,
76 * or -1 if some failure occurred (check errno). ENOENT means it was
77 * not an IPv4 network specification.
79 * network byte order assumed. this means 192.5.5.240/28 has
80 * 0b11110000 in its fourth octet.
82 * Paul Vixie (ISC), June 1996
85 inet_net_pton_ipv4 (const char *src
, u_char
*dst
, size_t size
)
87 static const char xdigits
[] = "0123456789abcdef";
88 int n
, ch
, tmp
, dirty
, bits
;
89 const u_char
*odst
= dst
;
92 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
93 && isascii(src
[1]) && isxdigit(src
[1])) {
94 /* Hexadecimal: Eat nybble string. */
98 tmp
= 0; /* To calm down gcc. */
99 src
++; /* skip x or X. */
100 while (isxdigit((ch
= *src
++))) {
102 n
= (const char *) __rawmemchr(xdigits
, ch
) - xdigits
;
103 assert(n
>= 0 && n
<= 15);
107 tmp
= (tmp
<< 4) | n
;
111 *dst
++ = (u_char
) tmp
;
115 if (dirty
) { /* Odd trailing nybble? */
118 *dst
++ = (u_char
) (tmp
<< 4);
120 } else if (isascii(ch
) && isdigit(ch
)) {
121 /* Decimal: eat dotted digit string. */
125 n
= ((const char *) __rawmemchr(xdigits
, ch
)
127 assert(n
>= 0 && n
<= 9);
132 } while (isascii((ch
= *src
++)) && isdigit(ch
));
135 *dst
++ = (u_char
) tmp
;
136 if (ch
== '\0' || ch
== '/')
141 if (!isascii(ch
) || !isdigit(ch
))
148 if (ch
== '/' && isascii(src
[0]) && isdigit(src
[0]) && dst
> odst
) {
149 /* CIDR width specifier. Nothing can follow it. */
150 ch
= *src
++; /* Skip over the /. */
153 n
= (const char *) __rawmemchr(xdigits
, ch
) - xdigits
;
154 assert(n
>= 0 && n
<= 9);
157 } while (isascii((ch
= *src
++)) && isdigit(ch
));
164 /* Firey death and destruction unless we prefetched EOS. */
168 /* If nothing was written to the destination, we found no address. */
171 /* If no CIDR spec was given, infer width from net class. */
173 if (*odst
>= 240) /* Class E */
175 else if (*odst
>= 224) /* Class D */
177 else if (*odst
>= 192) /* Class C */
179 else if (*odst
>= 128) /* Class B */
183 /* If imputed mask is narrower than specified octets, widen. */
184 if (bits
>= 8 && bits
< ((dst
- odst
) * 8))
185 bits
= (dst
- odst
) * 8;
187 /* Extend network to cover the actual mask. */
188 while (bits
> ((dst
- odst
) * 8)) {
196 __set_errno (ENOENT
);
200 __set_errno (EMSGSIZE
);