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.16 2003/08/04 00:43:25 momjian Exp $";
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
32 #include "utils/inet.h"
34 #include "utils/builtins.h"
36 static int inet_net_pton_ipv4(const char *src, u_char *dst);
37 static int inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size);
38 static int inet_net_pton_ipv6(const char *src, u_char *dst);
39 static int inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size);
43 * inet_net_pton(af, src, dst, size)
44 * convert network number from presentation to network format.
45 * accepts hex octets, hex strings, decimal octets, and /CIDR.
46 * "size" is in bytes and describes "dst".
48 * number of bits, either imputed classfully or specified with /CIDR,
49 * or -1 if some failure occurred (check errno). ENOENT means it was
50 * not a valid network specification.
52 * Paul Vixie (ISC), June 1996
55 * I added the inet_cidr_pton function (also from Paul) and changed
56 * the names to reflect their current use.
60 inet_net_pton(int af, const char *src, void *dst, size_t size)
66 inet_net_pton_ipv4(src, dst) :
67 inet_cidr_pton_ipv4(src, dst, size);
70 inet_net_pton_ipv6(src, dst) :
71 inet_cidr_pton_ipv6(src, dst, size);
80 * inet_cidr_pton_ipv4(src, dst, size)
81 * convert IPv4 network number from presentation to network format.
82 * accepts hex octets, hex strings, decimal octets, and /CIDR.
83 * "size" is in bytes and describes "dst".
85 * number of bits, either imputed classfully or specified with /CIDR,
86 * or -1 if some failure occurred (check errno). ENOENT means it was
87 * not an IPv4 network specification.
89 * network byte order assumed. this means 192.5.5.240/28 has
90 * 0x11110000 in its fourth octet.
92 * Paul Vixie (ISC), June 1996
95 inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size)
98 xdigits[] = "0123456789abcdef",
99 digits[] = "0123456789";
105 const u_char *odst = dst;
108 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
109 && isxdigit((unsigned char) src[1]))
111 /* Hexadecimal: Eat nybble string. */
116 src++; /* skip x or X. */
117 while ((ch = *src++) != '\0' && isxdigit((unsigned char) ch))
119 if (isupper((unsigned char) ch))
120 ch = tolower((unsigned char) ch);
121 n = strchr(xdigits, ch) - xdigits;
122 assert(n >= 0 && n <= 15);
126 tmp = (tmp << 4) | n;
131 *dst++ = (u_char) tmp;
136 { /* Odd trailing nybble? */
139 *dst++ = (u_char) (tmp << 4);
142 else if (isdigit((unsigned char) ch))
144 /* Decimal: eat dotted digit string. */
150 n = strchr(digits, ch) - digits;
151 assert(n >= 0 && n <= 9);
156 } while ((ch = *src++) != '\0' &&
157 isdigit((unsigned char) ch));
160 *dst++ = (u_char) tmp;
161 if (ch == '\0' || ch == '/')
166 if (!isdigit((unsigned char) ch))
174 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
176 /* CIDR width specifier. Nothing can follow it. */
177 ch = *src++; /* Skip over the /. */
181 n = strchr(digits, ch) - digits;
182 assert(n >= 0 && n <= 9);
185 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
192 /* Firey death and destruction unless we prefetched EOS. */
196 /* If nothing was written to the destination, we found no address. */
199 /* If no CIDR spec was given, infer width from net class. */
202 if (*odst >= 240) /* Class E */
204 else if (*odst >= 224) /* Class D */
206 else if (*odst >= 192) /* Class C */
208 else if (*odst >= 128) /* Class B */
213 /* If imputed mask is narrower than specified octets, widen. */
214 if (bits >= 8 && bits < ((dst - odst) * 8))
215 bits = (dst - odst) * 8;
217 /* Extend network to cover the actual mask. */
218 while (bits > ((dst - odst) * 8))
237 * inet_net_pton(af, src, dst, *bits)
238 * convert network address from presentation to network format.
239 * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
240 * "dst" is assumed large enough for its "af". "bits" is set to the
241 * /CIDR prefix length, which can have defaults (like /32 for IPv4).
243 * -1 if an error occurred (inspect errno; ENOENT means bad format).
244 * 0 if successful conversion occurred.
246 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
247 * as called for by inet_cidr_pton() but it can be a host address with
248 * an included netmask.
250 * Paul Vixie (ISC), October 1998
253 inet_net_pton_ipv4(const char *src, u_char *dst)
255 static const char digits[] = "0123456789";
256 const u_char *odst = dst;
263 /* Get the mantissa. */
264 while (ch = *src++, isdigit((unsigned char) ch))
269 n = strchr(digits, ch) - digits;
270 assert(n >= 0 && n <= 9);
275 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
278 *dst++ = (u_char) tmp;
279 if (ch == '\0' || ch == '/')
285 /* Get the prefix length if any. */
287 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
289 /* CIDR width specifier. Nothing can follow it. */
290 ch = *src++; /* Skip over the /. */
294 n = strchr(digits, ch) - digits;
295 assert(n >= 0 && n <= 9);
298 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
305 /* Firey death and destruction unless we prefetched EOS. */
309 /* Prefix length can default to /32 only if all four octets spec'd. */
318 /* If nothing was written to the destination, we found no address. */
322 /* If prefix length overspecifies mantissa, life is bad. */
323 if ((bits / 8) > (dst - odst))
326 /* Extend address to four octets. */
342 getbits(const char *src, int *bitsp)
344 static const char digits[] = "0123456789";
351 while ((ch = *src++) != '\0')
355 pch = strchr(digits, ch);
358 if (n++ != 0 && val == 0) /* no leading zeros */
361 val += (pch - digits);
362 if (val > 128) /* range */
375 getv4(const char *src, u_char *dst, int *bitsp)
377 static const char digits[] = "0123456789";
385 while ((ch = *src++) != '\0')
389 pch = strchr(digits, ch);
392 if (n++ != 0 && val == 0) /* no leading zeros */
395 val += (pch - digits);
396 if (val > 255) /* range */
400 if (ch == '.' || ch == '/')
402 if (dst - odst > 3) /* too many octets? */
406 return (getbits(src, bitsp));
415 if (dst - odst > 3) /* too many octets? */
422 inet_net_pton_ipv6(const char *src, u_char *dst)
424 return inet_cidr_pton_ipv6(src, dst, 16);
427 #define NS_IN6ADDRSZ 16
429 #define NS_INADDRSZ 4
432 inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size)
434 static const char xdigits_l[] = "0123456789abcdef",
435 xdigits_u[] = "0123456789ABCDEF";
436 u_char tmp[NS_IN6ADDRSZ],
448 if (size < NS_IN6ADDRSZ)
451 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
452 endp = tp + NS_IN6ADDRSZ;
454 /* Leading :: requires some special handling. */
463 while ((ch = *src++) != '\0')
467 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
468 pch = strchr((xdigits = xdigits_u), ch);
472 val |= (pch - xdigits);
488 else if (*src == '\0')
490 if (tp + NS_INT16SZ > endp)
492 *tp++ = (u_char) (val >> 8) & 0xff;
493 *tp++ = (u_char) val & 0xff;
499 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
500 getv4(curtok, tp, &bits) > 0)
504 break; /* '\0' was seen by inet_pton4(). */
506 if (ch == '/' && getbits(src, &bits) > 0)
512 if (tp + NS_INT16SZ > endp)
514 *tp++ = (u_char) (val >> 8) & 0xff;
515 *tp++ = (u_char) val & 0xff;
525 * Since some memmove()'s erroneously fail to handle overlapping
526 * regions, we'll do the shift by hand.
528 const int n = tp - colonp;
533 for (i = 1; i <= n; i++)
535 endp[-i] = colonp[n - i];
544 * Copy out the result.
546 memcpy(dst, tmp, NS_IN6ADDRSZ);