2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * src/backend/utils/adt/inet_net_pton.c
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "Id: inet_net_pton.c,v 1.4.2.3 2004/03/17 00:40:11 marka Exp $";
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
32 #include "utils/builtins.h" /* pgrminclude ignore */ /* needed on some
34 #include "utils/inet.h"
37 static int inet_net_pton_ipv4(const char *src, u_char *dst);
38 static int inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size);
39 static int inet_net_pton_ipv6(const char *src, u_char *dst);
40 static int inet_cidr_pton_ipv6(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);
72 inet_net_pton_ipv6(src, dst) :
73 inet_cidr_pton_ipv6(src, dst, size);
82 * inet_cidr_pton_ipv4(src, dst, size)
83 * convert IPv4 network number from presentation to network format.
84 * accepts hex octets, hex strings, decimal octets, and /CIDR.
85 * "size" is in bytes and describes "dst".
87 * number of bits, either imputed classfully or specified with /CIDR,
88 * or -1 if some failure occurred (check errno). ENOENT means it was
89 * not an IPv4 network specification.
91 * network byte order assumed. this means 192.5.5.240/28 has
92 * 0b11110000 in its fourth octet.
94 * Paul Vixie (ISC), June 1996
97 inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size)
99 static const char xdigits[] = "0123456789abcdef";
100 static const char digits[] = "0123456789";
106 const u_char *odst = dst;
109 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
110 && isxdigit((unsigned char) src[1]))
112 /* 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 < ((dst - odst) * 8))
215 bits = (dst - odst) * 8;
218 * If there are no additional bits specified for a class D address
221 if (bits == 8 && *odst == 224)
224 /* Extend network to cover the actual mask. */
225 while (bits > ((dst - odst) * 8))
244 * inet_net_pton(af, src, dst, *bits)
245 * convert network address from presentation to network format.
246 * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
247 * "dst" is assumed large enough for its "af". "bits" is set to the
248 * /CIDR prefix length, which can have defaults (like /32 for IPv4).
250 * -1 if an error occurred (inspect errno; ENOENT means bad format).
251 * 0 if successful conversion occurred.
253 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
254 * as called for by inet_cidr_pton() but it can be a host address with
255 * an included netmask.
257 * Paul Vixie (ISC), October 1998
260 inet_net_pton_ipv4(const char *src, u_char *dst)
262 static const char digits[] = "0123456789";
263 const u_char *odst = dst;
270 /* Get the mantissa. */
271 while (ch = *src++, isdigit((unsigned char) ch))
276 n = strchr(digits, ch) - digits;
277 assert(n >= 0 && n <= 9);
282 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
285 *dst++ = (u_char) tmp;
286 if (ch == '\0' || ch == '/')
292 /* Get the prefix length if any. */
294 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
296 /* CIDR width specifier. Nothing can follow it. */
297 ch = *src++; /* Skip over the /. */
301 n = strchr(digits, ch) - digits;
302 assert(n >= 0 && n <= 9);
305 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
312 /* Firey death and destruction unless we prefetched EOS. */
316 /* Prefix length can default to /32 only if all four octets spec'd. */
325 /* If nothing was written to the destination, we found no address. */
329 /* If prefix length overspecifies mantissa, life is bad. */
330 if ((bits / 8) > (dst - odst))
333 /* Extend address to four octets. */
349 getbits(const char *src, int *bitsp)
351 static const char digits[] = "0123456789";
358 while ((ch = *src++) != '\0')
362 pch = strchr(digits, ch);
365 if (n++ != 0 && val == 0) /* no leading zeros */
368 val += (pch - digits);
369 if (val > 128) /* range */
382 getv4(const char *src, u_char *dst, int *bitsp)
384 static const char digits[] = "0123456789";
392 while ((ch = *src++) != '\0')
396 pch = strchr(digits, ch);
399 if (n++ != 0 && val == 0) /* no leading zeros */
402 val += (pch - digits);
403 if (val > 255) /* range */
407 if (ch == '.' || ch == '/')
409 if (dst - odst > 3) /* too many octets? */
413 return (getbits(src, bitsp));
422 if (dst - odst > 3) /* too many octets? */
429 inet_net_pton_ipv6(const char *src, u_char *dst)
431 return inet_cidr_pton_ipv6(src, dst, 16);
434 #define NS_IN6ADDRSZ 16
436 #define NS_INADDRSZ 4
439 inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size)
441 static const char xdigits_l[] = "0123456789abcdef",
442 xdigits_u[] = "0123456789ABCDEF";
443 u_char tmp[NS_IN6ADDRSZ],
455 if (size < NS_IN6ADDRSZ)
458 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
459 endp = tp + NS_IN6ADDRSZ;
461 /* Leading :: requires some special handling. */
470 while ((ch = *src++) != '\0')
474 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
475 pch = strchr((xdigits = xdigits_u), ch);
479 val |= (pch - xdigits);
495 else if (*src == '\0')
497 if (tp + NS_INT16SZ > endp)
499 *tp++ = (u_char) (val >> 8) & 0xff;
500 *tp++ = (u_char) val & 0xff;
506 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
507 getv4(curtok, tp, &bits) > 0)
511 break; /* '\0' was seen by inet_pton4(). */
513 if (ch == '/' && getbits(src, &bits) > 0)
519 if (tp + NS_INT16SZ > endp)
521 *tp++ = (u_char) (val >> 8) & 0xff;
522 *tp++ = (u_char) val & 0xff;
532 * Since some memmove()'s erroneously fail to handle overlapping
533 * regions, we'll do the shift by hand.
535 const int n = tp - colonp;
540 for (i = 1; i <= n; i++)
542 endp[-i] = colonp[n - i];
551 * Copy out the result.
553 memcpy(dst, tmp, NS_IN6ADDRSZ);