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/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
33 #include "utils/builtins.h" /* pgrminclude ignore */ /* needed on some
35 #include "utils/inet.h"
38 static int inet_net_pton_ipv4(const char *src, u_char *dst);
39 static int inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size);
40 static int inet_net_pton_ipv6(const char *src, u_char *dst);
41 static int inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size);
46 * inet_net_pton(af, src, dst, size)
47 * convert network number from presentation to network format.
48 * accepts hex octets, hex strings, decimal octets, and /CIDR.
49 * "size" is in bytes and describes "dst".
51 * number of bits, either imputed classfully or specified with /CIDR,
52 * or -1 if some failure occurred (check errno). ENOENT means it was
53 * not a valid network specification.
55 * Paul Vixie (ISC), June 1996
58 * I added the inet_cidr_pton function (also from Paul) and changed
59 * the names to reflect their current use.
63 inet_net_pton(int af, const char *src, void *dst, size_t size)
69 inet_net_pton_ipv4(src, dst) :
70 inet_cidr_pton_ipv4(src, dst, size);
73 inet_net_pton_ipv6(src, dst) :
74 inet_cidr_pton_ipv6(src, dst, size);
83 * inet_cidr_pton_ipv4(src, dst, size)
84 * convert IPv4 network number from presentation to network format.
85 * accepts hex octets, hex strings, decimal octets, and /CIDR.
86 * "size" is in bytes and describes "dst".
88 * number of bits, either imputed classfully or specified with /CIDR,
89 * or -1 if some failure occurred (check errno). ENOENT means it was
90 * not an IPv4 network specification.
92 * network byte order assumed. this means 192.5.5.240/28 has
93 * 0b11110000 in its fourth octet.
95 * Paul Vixie (ISC), June 1996
98 inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size)
100 static const char xdigits[] = "0123456789abcdef";
101 static const char digits[] = "0123456789";
107 const u_char *odst = dst;
110 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
111 && isxdigit((unsigned char) src[1]))
113 /* Hexadecimal: Eat nybble string. */
117 src++; /* skip x or X. */
118 while ((ch = *src++) != '\0' && isxdigit((unsigned char) ch))
120 if (isupper((unsigned char) ch))
121 ch = tolower((unsigned char) ch);
122 n = strchr(xdigits, ch) - xdigits;
123 assert(n >= 0 && n <= 15);
127 tmp = (tmp << 4) | n;
132 *dst++ = (u_char) tmp;
137 { /* Odd trailing nybble? */
140 *dst++ = (u_char) (tmp << 4);
143 else if (isdigit((unsigned char) ch))
145 /* Decimal: eat dotted digit string. */
151 n = strchr(digits, ch) - digits;
152 assert(n >= 0 && n <= 9);
157 } while ((ch = *src++) != '\0' &&
158 isdigit((unsigned char) ch));
161 *dst++ = (u_char) tmp;
162 if (ch == '\0' || ch == '/')
167 if (!isdigit((unsigned char) ch))
175 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
177 /* CIDR width specifier. Nothing can follow it. */
178 ch = *src++; /* Skip over the /. */
182 n = strchr(digits, ch) - digits;
183 assert(n >= 0 && n <= 9);
186 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
193 /* Firey death and destruction unless we prefetched EOS. */
197 /* If nothing was written to the destination, we found no address. */
200 /* If no CIDR spec was given, infer width from net class. */
203 if (*odst >= 240) /* Class E */
205 else if (*odst >= 224) /* Class D */
207 else if (*odst >= 192) /* Class C */
209 else if (*odst >= 128) /* Class B */
214 /* If imputed mask is narrower than specified octets, widen. */
215 if (bits < ((dst - odst) * 8))
216 bits = (dst - odst) * 8;
219 * If there are no additional bits specified for a class D address
222 if (bits == 8 && *odst == 224)
225 /* Extend network to cover the actual mask. */
226 while (bits > ((dst - odst) * 8))
245 * inet_net_pton(af, src, dst, *bits)
246 * convert network address from presentation to network format.
247 * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
248 * "dst" is assumed large enough for its "af". "bits" is set to the
249 * /CIDR prefix length, which can have defaults (like /32 for IPv4).
251 * -1 if an error occurred (inspect errno; ENOENT means bad format).
252 * 0 if successful conversion occurred.
254 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
255 * as called for by inet_cidr_pton() but it can be a host address with
256 * an included netmask.
258 * Paul Vixie (ISC), October 1998
261 inet_net_pton_ipv4(const char *src, u_char *dst)
263 static const char digits[] = "0123456789";
264 const u_char *odst = dst;
271 /* Get the mantissa. */
272 while (ch = *src++, isdigit((unsigned char) ch))
277 n = strchr(digits, ch) - digits;
278 assert(n >= 0 && n <= 9);
283 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
286 *dst++ = (u_char) tmp;
287 if (ch == '\0' || ch == '/')
293 /* Get the prefix length if any. */
295 if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
297 /* CIDR width specifier. Nothing can follow it. */
298 ch = *src++; /* Skip over the /. */
302 n = strchr(digits, ch) - digits;
303 assert(n >= 0 && n <= 9);
306 } while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
313 /* Firey death and destruction unless we prefetched EOS. */
317 /* Prefix length can default to /32 only if all four octets spec'd. */
326 /* If nothing was written to the destination, we found no address. */
330 /* If prefix length overspecifies mantissa, life is bad. */
331 if ((bits / 8) > (dst - odst))
334 /* Extend address to four octets. */
350 getbits(const char *src, int *bitsp)
352 static const char digits[] = "0123456789";
359 while ((ch = *src++) != '\0')
363 pch = strchr(digits, ch);
366 if (n++ != 0 && val == 0) /* no leading zeros */
369 val += (pch - digits);
370 if (val > 128) /* range */
383 getv4(const char *src, u_char *dst, int *bitsp)
385 static const char digits[] = "0123456789";
393 while ((ch = *src++) != '\0')
397 pch = strchr(digits, ch);
400 if (n++ != 0 && val == 0) /* no leading zeros */
403 val += (pch - digits);
404 if (val > 255) /* range */
408 if (ch == '.' || ch == '/')
410 if (dst - odst > 3) /* too many octets? */
414 return (getbits(src, bitsp));
423 if (dst - odst > 3) /* too many octets? */
430 inet_net_pton_ipv6(const char *src, u_char *dst)
432 return inet_cidr_pton_ipv6(src, dst, 16);
435 #define NS_IN6ADDRSZ 16
437 #define NS_INADDRSZ 4
440 inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size)
442 static const char xdigits_l[] = "0123456789abcdef",
443 xdigits_u[] = "0123456789ABCDEF";
444 u_char tmp[NS_IN6ADDRSZ],
456 if (size < NS_IN6ADDRSZ)
459 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
460 endp = tp + NS_IN6ADDRSZ;
462 /* Leading :: requires some special handling. */
471 while ((ch = *src++) != '\0')
475 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
476 pch = strchr((xdigits = xdigits_u), ch);
480 val |= (pch - xdigits);
496 else if (*src == '\0')
498 if (tp + NS_INT16SZ > endp)
500 *tp++ = (u_char) (val >> 8) & 0xff;
501 *tp++ = (u_char) val & 0xff;
507 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
508 getv4(curtok, tp, &bits) > 0)
512 break; /* '\0' was seen by inet_pton4(). */
514 if (ch == '/' && getbits(src, &bits) > 0)
520 if (tp + NS_INT16SZ > endp)
522 *tp++ = (u_char) (val >> 8) & 0xff;
523 *tp++ = (u_char) val & 0xff;
533 * Since some memmove()'s erroneously fail to handle overlapping
534 * regions, we'll do the shift by hand.
536 const int n = tp - colonp;
541 for (i = 1; i <= n; i++)
543 endp[-i] = colonp[n - i];
552 * Copy out the result.
554 memcpy(dst, tmp, NS_IN6ADDRSZ);