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 rcsid[] = "$Id: inet_net_pton.c,v 1.13 2001/10/25 05:49:44 momjian Exp $";
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
32 #include "utils/builtins.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);
41 static int inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size);
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 * I added the inet_cidr_pton function (also from Paul) and changed
58 * the names to reflect their current use.
62 inet_net_pton(int af, const char *src, void *dst, size_t size)
68 inet_net_pton_ipv4(src, dst) :
69 inet_cidr_pton_ipv4(src, dst, size);
78 * inet_cidr_pton_ipv4(src, dst, size)
79 * convert IPv4 network number from presentation to network format.
80 * accepts hex octets, hex strings, decimal octets, and /CIDR.
81 * "size" is in bytes and describes "dst".
83 * number of bits, either imputed classfully or specified with /CIDR,
84 * or -1 if some failure occurred (check errno). ENOENT means it was
85 * not an IPv4 network specification.
87 * network byte order assumed. this means 192.5.5.240/28 has
88 * 0x11110000 in its fourth octet.
90 * Paul Vixie (ISC), June 1996
93 inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size)
96 xdigits[] = "0123456789abcdef",
97 digits[] = "0123456789";
103 const u_char *odst = dst;
106 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
107 && isxdigit((unsigned char) src[1]))
109 /* Hexadecimal: Eat nybble string. */
114 src++; /* skip x or X. */
115 while ((ch = *src++) != '\0' && isxdigit((unsigned char) ch))
117 if (isupper((unsigned char) ch))
118 ch = tolower((unsigned char) ch);
119 n = strchr(xdigits, ch) - xdigits;
120 assert(n >= 0 && n <= 15);
124 tmp = (tmp << 4) | n;
129 *dst++ = (u_char) tmp;
134 { /* Odd trailing nybble? */
137 *dst++ = (u_char) (tmp << 4);
140 else if (isdigit((unsigned char) ch))
142 /* Decimal: eat dotted digit string. */
148 n = strchr(digits, ch) - digits;
149 assert(n >= 0 && n <= 9);
154 } while ((ch = *src++) != '\0' &&
155 isdigit((unsigned char) ch));
158 *dst++ = (u_char) tmp;
159 if (ch == '\0' || ch == '/')
164 if (!isdigit((unsigned char) ch))
172 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
174 /* CIDR width specifier. Nothing can follow it. */
175 ch = *src++; /* Skip over the /. */
179 n = strchr(digits, ch) - digits;
180 assert(n >= 0 && n <= 9);
183 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
190 /* Firey death and destruction unless we prefetched EOS. */
194 /* If nothing was written to the destination, we found no address. */
197 /* If no CIDR spec was given, infer width from net class. */
200 if (*odst >= 240) /* Class E */
202 else if (*odst >= 224) /* Class D */
204 else if (*odst >= 192) /* Class C */
206 else if (*odst >= 128) /* Class B */
211 /* If imputed mask is narrower than specified octets, widen. */
212 if (bits >= 8 && bits < ((dst - odst) * 8))
213 bits = (dst - odst) * 8;
215 /* Extend network to cover the actual mask. */
216 while (bits > ((dst - odst) * 8))
235 * inet_net_pton(af, src, dst, *bits)
236 * convert network address from presentation to network format.
237 * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
238 * "dst" is assumed large enough for its "af". "bits" is set to the
239 * /CIDR prefix length, which can have defaults (like /32 for IPv4).
241 * -1 if an error occurred (inspect errno; ENOENT means bad format).
242 * 0 if successful conversion occurred.
244 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
245 * as called for by inet_cidr_pton() but it can be a host address with
246 * an included netmask.
248 * Paul Vixie (ISC), October 1998
251 inet_net_pton_ipv4(const char *src, u_char *dst)
253 static const char digits[] = "0123456789";
254 const u_char *odst = dst;
261 /* Get the mantissa. */
262 while (ch = *src++, isdigit((unsigned char) ch))
267 n = strchr(digits, ch) - digits;
268 assert(n >= 0 && n <= 9);
273 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
276 *dst++ = (u_char) tmp;
277 if (ch == '\0' || ch == '/')
283 /* Get the prefix length if any. */
285 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
287 /* CIDR width specifier. Nothing can follow it. */
288 ch = *src++; /* Skip over the /. */
292 n = strchr(digits, ch) - digits;
293 assert(n >= 0 && n <= 9);
296 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
303 /* Firey death and destruction unless we prefetched EOS. */
307 /* Prefix length can default to /32 only if all four octets spec'd. */
316 /* If nothing was written to the destination, we found no address. */
320 /* If prefix length overspecifies mantissa, life is bad. */
321 if ((bits / 8) > (dst - odst))
324 /* Extend address to four octets. */