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
* $PostgreSQL: pgsql/src/backend/utils/adt/inet_net_pton.c,v 1.19.4.1 2005-02-01 00:59:53 tgl Exp $
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/inet.h"
34
#include "utils/builtins.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 */
212
/* If imputed mask is narrower than specified octets, widen. */
213
if (bits < ((dst - odst) * 8))
214
bits = (dst - odst) * 8;
217
* If there are no additional bits specified for a class D address
220
if (bits == 8 && *odst == 224)
223
/* Extend network to cover the actual mask. */
224
while (bits > ((dst - odst) * 8))
243
* inet_net_pton(af, src, dst, *bits)
244
* convert network address from presentation to network format.
245
* accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
246
* "dst" is assumed large enough for its "af". "bits" is set to the
247
* /CIDR prefix length, which can have defaults (like /32 for IPv4).
249
* -1 if an error occurred (inspect errno; ENOENT means bad format).
250
* 0 if successful conversion occurred.
252
* 192.5.5.1/28 has a nonzero host part, which means it isn't a network
253
* as called for by inet_cidr_pton() but it can be a host address with
254
* an included netmask.
256
* Paul Vixie (ISC), October 1998
259
inet_net_pton_ipv4(const char *src, u_char *dst)
261
static const char digits[] = "0123456789";
262
const u_char *odst = dst;
269
/* Get the mantissa. */
270
while (ch = *src++, isdigit((unsigned char) ch))
275
n = strchr(digits, ch) - digits;
276
assert(n >= 0 && n <= 9);
281
} while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
284
*dst++ = (u_char) tmp;
285
if (ch == '\0' || ch == '/')
291
/* Get the prefix length if any. */
293
if (ch == '/' && isdigit((unsigned char) src[0]) && dst > odst)
295
/* CIDR width specifier. Nothing can follow it. */
296
ch = *src++; /* Skip over the /. */
300
n = strchr(digits, ch) - digits;
301
assert(n >= 0 && n <= 9);
304
} while ((ch = *src++) != '\0' && isdigit((unsigned char) ch));
311
/* Firey death and destruction unless we prefetched EOS. */
315
/* Prefix length can default to /32 only if all four octets spec'd. */
324
/* If nothing was written to the destination, we found no address. */
328
/* If prefix length overspecifies mantissa, life is bad. */
329
if ((bits / 8) > (dst - odst))
332
/* Extend address to four octets. */
348
getbits(const char *src, int *bitsp)
350
static const char digits[] = "0123456789";
357
while ((ch = *src++) != '\0')
361
pch = strchr(digits, ch);
364
if (n++ != 0 && val == 0) /* no leading zeros */
367
val += (pch - digits);
368
if (val > 128) /* range */
381
getv4(const char *src, u_char *dst, int *bitsp)
383
static const char digits[] = "0123456789";
391
while ((ch = *src++) != '\0')
395
pch = strchr(digits, ch);
398
if (n++ != 0 && val == 0) /* no leading zeros */
401
val += (pch - digits);
402
if (val > 255) /* range */
406
if (ch == '.' || ch == '/')
408
if (dst - odst > 3) /* too many octets? */
412
return (getbits(src, bitsp));
421
if (dst - odst > 3) /* too many octets? */
428
inet_net_pton_ipv6(const char *src, u_char *dst)
430
return inet_cidr_pton_ipv6(src, dst, 16);
433
#define NS_IN6ADDRSZ 16
435
#define NS_INADDRSZ 4
438
inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size)
440
static const char xdigits_l[] = "0123456789abcdef",
441
xdigits_u[] = "0123456789ABCDEF";
442
u_char tmp[NS_IN6ADDRSZ],
454
if (size < NS_IN6ADDRSZ)
457
memset((tp = tmp), '\0', NS_IN6ADDRSZ);
458
endp = tp + NS_IN6ADDRSZ;
460
/* Leading :: requires some special handling. */
469
while ((ch = *src++) != '\0')
473
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
474
pch = strchr((xdigits = xdigits_u), ch);
478
val |= (pch - xdigits);
494
else if (*src == '\0')
496
if (tp + NS_INT16SZ > endp)
498
*tp++ = (u_char) (val >> 8) & 0xff;
499
*tp++ = (u_char) val & 0xff;
505
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
506
getv4(curtok, tp, &bits) > 0)
510
break; /* '\0' was seen by inet_pton4(). */
512
if (ch == '/' && getbits(src, &bits) > 0)
518
if (tp + NS_INT16SZ > endp)
520
*tp++ = (u_char) (val >> 8) & 0xff;
521
*tp++ = (u_char) val & 0xff;
531
* Since some memmove()'s erroneously fail to handle overlapping
532
* regions, we'll do the shift by hand.
534
const int n = tp - colonp;
539
for (i = 1; i <= n; i++)
541
endp[-i] = colonp[n - i];
550
* Copy out the result.
552
memcpy(dst, tmp, NS_IN6ADDRSZ);