Commit 945543d9 authored by Bruce Momjian's avatar Bruce Momjian

Add ipv6 address parsing support to 'inet' and 'cidr' data types.

        Regression tests for IPv6 operations added.

        Documentation updated to document IPv6 bits.

        Stop treating IPv4 as an "unsigned int" and IPv6 as an array of
        characters.  Instead, always use the array of characters so we
        can have one function fits all.  This makes bitncmp(), addressOK(),
        and several other functions "just work" on both address families.

        add family() function which returns integer 4 or 6 for IPv4 or
        IPv6.  (See examples below)  Note that to add this new function
        you will need to dump/initdb/reload or find the correct magic
        to add the function to the postgresql function catalogs.

        IPv4 addresses always sort before IPv6.

        On disk we use AF_INET for IPv4, and AF_INET+1 for IPv6 addresses.
        This prevents the need for a dump and reload, but lets IPv6 parsing
        work on machines without AF_INET6.

        To select all IPv4 addresses from a table:

                select * from foo where family(addr) = 4 ...

        Order by and other bits should all work.

Michael Graff
parent 4dab978c
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.117 2003/06/17 23:12:36 tgl Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.118 2003/06/24 22:21:21 momjian Exp $
-->
<chapter id="datatype">
......@@ -100,7 +100,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.117 2003/06/17 23:12:36 t
<row>
<entry><type>cidr</type></entry>
<entry></entry>
<entry>IP network address</entry>
<entry>IPv4 or IPv6 network address</entry>
</row>
<row>
......@@ -124,7 +124,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.117 2003/06/17 23:12:36 t
<row>
<entry><type>inet</type></entry>
<entry></entry>
<entry>IP host address</entry>
<entry>IPv4 or IPv6 host address</entry>
</row>
<row>
......@@ -2425,7 +2425,7 @@ SELECT * FROM test1 WHERE a;
</indexterm>
<para>
<productname>PostgreSQL</> offers data types to store IP and MAC
<productname>PostgreSQL</> offers data types to store IPv4, IPv6, and MAC
addresses, shown in <xref linkend="datatype-net-types-table">. It
is preferable to use these types over plain text types, because
these types offer input error checking and several specialized
......@@ -2446,14 +2446,14 @@ SELECT * FROM test1 WHERE a;
<row>
<entry><type>cidr</type></entry>
<entry>12 bytes</entry>
<entry>IPv4 networks</entry>
<entry>12 or 24 bytes</entry>
<entry>IPv4 or IPv6 networks</entry>
</row>
<row>
<entry><type>inet</type></entry>
<entry>12 bytes</entry>
<entry>IPv4 hosts and networks</entry>
<entry>12 or 24 bytes</entry>
<entry>IPv4 and IPv6 hosts and networks</entry>
</row>
<row>
......@@ -2467,7 +2467,10 @@ SELECT * FROM test1 WHERE a;
</table>
<para>
IPv6 is not yet supported.
When sorting <type>inet</type> or <type>cidr</type> data types,
IPv4 addresses will always sort before IPv6 addresses, including
IPv4 addresses encapsulated or mapped into IPv6 addresses, such as
::10.2.3.4 or ::ffff::10.4.3.2.
</para>
......@@ -2479,26 +2482,32 @@ SELECT * FROM test1 WHERE a;
</indexterm>
<para>
The <type>inet</type> type holds an IP host address, and
The <type>inet</type> type holds an IPv4 or IPv6 host address, and
optionally the identity of the subnet it is in, all in one field.
The subnet identity is represented by stating how many bits of
the host address represent the network address (the
<quote>netmask</quote>). If the netmask is 32, then the value
does not indicate a subnet, only a single host. Note that if you
<quote>netmask</quote>). If the netmask is 32 and the address is IPv4,
then the value does not indicate a subnet, only a single host.
In IPv6, the address length is 128 bits, so 128 bits will specify a
unique host address. Note that if you
want to accept networks only, you should use the
<type>cidr</type> type rather than <type>inet</type>.
</para>
<para>
The input format for this type is <replaceable
class="parameter">x.x.x.x/y</replaceable> where <replaceable
class="parameter">x.x.x.x</replaceable> is an IP address and
<replaceable class="parameter">y</replaceable> is the number of
bits in the netmask. If the <replaceable
class="parameter">/y</replaceable> part is left off, then the
netmask is 32, and the value represents just a single host.
On display, the <replaceable class="parameter">/y</replaceable>
portion is suppressed if the netmask is 32.
The input format for this type is
<replaceable class="parameter">address/y</replaceable>
where
<replaceable class="parameter">address</replaceable>
is an IPv4 or IPv6 address and
<replaceable class="parameter">y</replaceable>
is the number of bits in the netmask. If the
<replaceable class="parameter">/y</replaceable>
part is left off, then the
netmask is 32 for IPv4 and 128 for IPv6, and the value represents
just a single host. On display, the
<replaceable class="parameter">/y</replaceable>
portion is suppressed if the netmask specifies a single host.
</para>
</sect2>
......@@ -2510,12 +2519,13 @@ SELECT * FROM test1 WHERE a;
</indexterm>
<para>
The <type>cidr</type> type holds an IP network specification.
The <type>cidr</type> type holds an IPv4 or IPv6 network specification.
Input and output formats follow Classless Internet Domain Routing
conventions.
The format for specifying networks is <replaceable
class="parameter">x.x.x.x/y</> where <replaceable
class="parameter">x.x.x.x</> is the network and <replaceable
class="parameter">address/y</> where <replaceable
class="parameter">address</> is the network represented as an
IPv4 or IPv6 address, and <replaceable
class="parameter">y</> is the number of bits in the netmask. If
<replaceable class="parameter">y</> is omitted, it is calculated
using assumptions from the older classful network numbering system, except
......@@ -2594,6 +2604,28 @@ SELECT * FROM test1 WHERE a;
<entry>10.0.0.0/8</entry>
<entry>10/8</entry>
</row>
<row>
<entry>10.1.2.3/32</entry>
<entry>10.1.2.3/32</entry>
<entry>10.1.2.3/32</entry>
<row>
<entry>2001:4f8:3:ba::/64</entry>
<entry>2001:4f8:3:ba::/64</entry>
<entry>2001:4f8:3:ba::/64</entry>
</row>
<row>
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1</entry>
</row>
<row>
<entry>::ffff:1.2.3.0/120</entry>
<entry>::ffff:1.2.3.0/120</entry>
<entry>::ffff:1.2.3/120</entry>
<row>
<entry>::ffff:1.2.3.0/128</entry>
<entry>::ffff:1.2.3.0/128</entry>
<entry>::ffff:1.2.3.0/128</entry>
</tbody>
</tgroup>
</table>
......
......@@ -16,7 +16,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.12 2002/09/02 02:47:04 momjian Exp $";
static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.13 2003/06/24 22:21:22 momjian Exp $";
#endif
#include "postgres.h"
......@@ -27,8 +27,12 @@ static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.12 2002/09/02 02:47:04 mom
#include <errno.h>
#include "utils/inet.h"
#include "utils/builtins.h"
#define NS_IN6ADDRSZ 16
#define NS_INT16SZ 2
#ifdef SPRINTF_CHAR
#define SPRINTF(x) strlen(sprintf/**/x)
#else
......@@ -36,9 +40,13 @@ static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.12 2002/09/02 02:47:04 mom
#endif
static char *inet_net_ntop_ipv4(const u_char *src, int bits,
char *dst, size_t size);
char *dst, size_t size);
static char *inet_cidr_ntop_ipv4(const u_char *src, int bits,
char *dst, size_t size);
char *dst, size_t size);
static char *inet_net_ntop_ipv6(const u_char *src, int bits,
char *dst, size_t size);
static char *inet_cidr_ntop_ipv6(const u_char *src, int bits,
char *dst, size_t size);
/*
* char *
......@@ -55,8 +63,10 @@ inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
{
switch (af)
{
case AF_INET:
case PGSQL_AF_INET:
return (inet_cidr_ntop_ipv4(src, bits, dst, size));
case PGSQL_AF_INET6:
return (inet_cidr_ntop_ipv6(src, bits, dst, size));
default:
errno = EAFNOSUPPORT;
return (NULL);
......@@ -136,6 +146,148 @@ emsgsize:
return (NULL);
}
/*
* static char *
* inet_net_ntop_ipv6(src, bits, fakebits, dst, size)
* convert IPv6 network number from network to presentation format.
* generates CIDR style result always. Picks the shortest representation
* unless the IP is really IPv4.
* always prints specified number of bits (bits).
* return:
* pointer to dst, or NULL if an error occurred (check errno).
* note:
* network byte order assumed. this means 192.5.5.240/28 has
* 0x11110000 in its fourth octet.
* author:
* Vadim Kogan (UCB), June 2001
* Original version (IPv4) by Paul Vixie (ISC), July 1996
*/
static char *
inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
{
u_int m;
int b;
int p;
int zero_s, zero_l, tmp_zero_s, tmp_zero_l;
int i;
int is_ipv4 = 0;
int double_colon = 0;
unsigned char inbuf[16];
char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
char *cp;
int words;
u_char *s;
if (bits < 0 || bits > 128) {
errno = EINVAL;
return (NULL);
}
cp = outbuf;
double_colon = 0;
if (bits == 0) {
*cp++ = ':';
*cp++ = ':';
*cp = '\0';
double_colon = 1;
} else {
/* Copy src to private buffer. Zero host part. */
p = (bits + 7) / 8;
memcpy(inbuf, src, p);
memset(inbuf + p, 0, 16 - p);
b = bits % 8;
if (b != 0) {
m = ~0 << (8 - b);
inbuf[p-1] &= m;
}
s = inbuf;
/* how many words need to be displayed in output */
words = (bits + 15) / 16;
if (words == 1)
words = 2;
/* Find the longest substring of zero's */
zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
for (i = 0; i < (words * 2); i += 2) {
if ((s[i] | s[i+1]) == 0) {
if (tmp_zero_l == 0)
tmp_zero_s = i / 2;
tmp_zero_l++;
} else {
if (tmp_zero_l && zero_l < tmp_zero_l) {
zero_s = tmp_zero_s;
zero_l = tmp_zero_l;
tmp_zero_l = 0;
}
}
}
if (tmp_zero_l && zero_l < tmp_zero_l) {
zero_s = tmp_zero_s;
zero_l = tmp_zero_l;
}
if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
is_ipv4 = 1;
/* Format whole words. */
for (p = 0; p < words; p++) {
if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) {
/* Time to skip some zeros */
if (p == zero_s)
*cp++ = ':';
if (p == words - 1) {
*cp++ = ':';
double_colon = 1;
}
s++;
s++;
continue;
}
if (is_ipv4 && p > 5 ) {
*cp++ = (p == 6) ? ':' : '.';
cp += SPRINTF((cp, "%u", *s++));
/* we can potentially drop the last octet */
if (p != 7 || bits > 120) {
*cp++ = '.';
cp += SPRINTF((cp, "%u", *s++));
}
} else {
if (cp != outbuf)
*cp++ = ':';
cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
s += 2;
}
}
}
if (!double_colon) {
if (bits < 128 - 32)
cp += SPRINTF((cp, "::", bits));
else if (bits < 128 - 16)
cp += SPRINTF((cp, ":0", bits));
}
/* Format CIDR /width. */
SPRINTF((cp, "/%u", bits));
if (strlen(outbuf) + 1 > size)
goto emsgsize;
strcpy(dst, outbuf);
return (dst);
emsgsize:
errno = EMSGSIZE;
return (NULL);
}
/*
* char *
......@@ -156,8 +308,10 @@ inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
{
switch (af)
{
case AF_INET:
case PGSQL_AF_INET:
return (inet_net_ntop_ipv4(src, bits, dst, size));
case PGSQL_AF_INET6:
return (inet_net_ntop_ipv6(src, bits, dst, size));
default:
errno = EAFNOSUPPORT;
return (NULL);
......@@ -217,3 +371,127 @@ emsgsize:
errno = EMSGSIZE;
return (NULL);
}
static int
decoct(const u_char *src, int bytes, char *dst, size_t size) {
char *odst = dst;
char *t;
int b;
for (b = 1; b <= bytes; b++) {
if (size < sizeof "255.")
return (0);
t = dst;
dst += SPRINTF((dst, "%u", *src++));
if (b != bytes) {
*dst++ = '.';
*dst = '\0';
}
size -= (size_t)(dst - t);
}
return (dst - odst);
}
static char *
inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
* to contain a value of the specified size. On some systems, like
* Crays, there is no such thing as an integer variable with 16 bits.
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
char *tp;
struct { int base, len; } best, cur;
u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
int i;
if ((bits < -1) || (bits > 128)) {
errno = EINVAL;
return (NULL);
}
/*
* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
memset(words, '\0', sizeof words);
for (i = 0; i < NS_IN6ADDRSZ; i++)
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
best.base = -1;
cur.base = -1;
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
else
cur.len++;
} else {
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
cur.base = -1;
}
}
}
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
}
if (best.base != -1 && best.len < 2)
best.base = -1;
/*
* Format the result.
*/
tp = tmp;
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
/* Are we inside the best run of 0x00's? */
if (best.base != -1 && i >= best.base &&
i < (best.base + best.len)) {
if (i == best.base)
*tp++ = ':';
continue;
}
/* Are we following an initial run of 0x00s or any real hex? */
if (i != 0)
*tp++ = ':';
/* Is this address an encapsulated IPv4? */
if (i == 6 && best.base == 0 && (best.len == 6 ||
(best.len == 7 && words[7] != 0x0001) ||
(best.len == 5 && words[5] == 0xffff))) {
int n;
n = decoct(src+12, 4, tp, sizeof tmp - (tp - tmp));
if (n == 0) {
errno = EMSGSIZE;
return (NULL);
}
tp += strlen(tp);
break;
}
tp += SPRINTF((tp, "%x", words[i]));
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) ==
(NS_IN6ADDRSZ / NS_INT16SZ))
*tp++ = ':';
*tp = '\0';
if (bits != -1 && bits != 128)
tp += SPRINTF((tp, "/%u", bits));
/*
* Check for overflow, copy, and we're done.
*/
if ((size_t)(tp - tmp) > size) {
errno = EMSGSIZE;
return (NULL);
}
strcpy(dst, tmp);
return (dst);
}
......@@ -16,7 +16,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$Id: inet_net_pton.c,v 1.14 2002/09/02 02:47:04 momjian Exp $";
static const char rcsid[] = "$Id: inet_net_pton.c,v 1.15 2003/06/24 22:21:22 momjian Exp $";
#endif
#include "postgres.h"
......@@ -29,16 +29,14 @@ static const char rcsid[] = "$Id: inet_net_pton.c,v 1.14 2002/09/02 02:47:04 mom
#include <ctype.h>
#include <errno.h>
#include "utils/builtins.h"
#include "utils/inet.h"
#ifdef SPRINTF_CHAR
#define SPRINTF(x) strlen(sprintf/**/x)
#else
#define SPRINTF(x) ((size_t)sprintf x)
#endif
#include "utils/builtins.h"
static int inet_net_pton_ipv4(const char *src, u_char *dst);
static int inet_cidr_pton_ipv4(const char *src, u_char *dst, size_t size);
static int inet_net_pton_ipv6(const char *src, u_char *dst);
static int inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size);
/*
* static int
......@@ -63,10 +61,14 @@ inet_net_pton(int af, const char *src, void *dst, size_t size)
{
switch (af)
{
case AF_INET:
case PGSQL_AF_INET:
return size == -1 ?
inet_net_pton_ipv4(src, dst) :
inet_cidr_pton_ipv4(src, dst, size);
case PGSQL_AF_INET6:
return size == -1 ?
inet_net_pton_ipv6(src, dst) :
inet_cidr_pton_ipv6(src, dst, size);
default:
errno = EAFNOSUPPORT;
return (-1);
......@@ -335,3 +337,199 @@ emsgsize:
errno = EMSGSIZE;
return (-1);
}
static int
getbits(const char *src, int *bitsp) {
static const char digits[] = "0123456789";
int n;
int val;
char ch;
val = 0;
n = 0;
while ((ch = *src++) != '\0') {
const char *pch;
pch = strchr(digits, ch);
if (pch != NULL) {
if (n++ != 0 && val == 0) /* no leading zeros */
return (0);
val *= 10;
val += (pch - digits);
if (val > 128) /* range */
return (0);
continue;
}
return (0);
}
if (n == 0)
return (0);
*bitsp = val;
return (1);
}
static int
getv4(const char *src, u_char *dst, int *bitsp) {
static const char digits[] = "0123456789";
u_char *odst = dst;
int n;
u_int val;
char ch;
val = 0;
n = 0;
while ((ch = *src++) != '\0') {
const char *pch;
pch = strchr(digits, ch);
if (pch != NULL) {
if (n++ != 0 && val == 0) /* no leading zeros */
return (0);
val *= 10;
val += (pch - digits);
if (val > 255) /* range */
return (0);
continue;
}
if (ch == '.' || ch == '/') {
if (dst - odst > 3) /* too many octets? */
return (0);
*dst++ = val;
if (ch == '/')
return (getbits(src, bitsp));
val = 0;
n = 0;
continue;
}
return (0);
}
if (n == 0)
return (0);
if (dst - odst > 3) /* too many octets? */
return (0);
*dst++ = val;
return (1);
}
static int
inet_net_pton_ipv6(const char *src, u_char *dst)
{
return inet_cidr_pton_ipv6(src, dst, 16);
}
#define NS_IN6ADDRSZ 16
#define NS_INT16SZ 2
#define NS_INADDRSZ 4
static int
inet_cidr_pton_ipv6(const char *src, u_char *dst, size_t size) {
static const char xdigits_l[] = "0123456789abcdef",
xdigits_u[] = "0123456789ABCDEF";
u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
const char *xdigits, *curtok;
int ch, saw_xdigit;
u_int val;
int digits;
int bits;
if (size < NS_IN6ADDRSZ)
goto emsgsize;
memset((tp = tmp), '\0', NS_IN6ADDRSZ);
endp = tp + NS_IN6ADDRSZ;
colonp = NULL;
/* Leading :: requires some special handling. */
if (*src == ':')
if (*++src != ':')
goto enoent;
curtok = src;
saw_xdigit = 0;
val = 0;
digits = 0;
bits = -1;
while ((ch = *src++) != '\0') {
const char *pch;
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
pch = strchr((xdigits = xdigits_u), ch);
if (pch != NULL) {
val <<= 4;
val |= (pch - xdigits);
if (++digits > 4)
goto enoent;
saw_xdigit = 1;
continue;
}
if (ch == ':') {
curtok = src;
if (!saw_xdigit) {
if (colonp)
goto enoent;
colonp = tp;
continue;
} else if (*src == '\0')
goto enoent;
if (tp + NS_INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
saw_xdigit = 0;
digits = 0;
val = 0;
continue;
}
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
getv4(curtok, tp, &bits) > 0) {
tp += NS_INADDRSZ;
saw_xdigit = 0;
break; /* '\0' was seen by inet_pton4(). */
}
if (ch == '/' && getbits(src, &bits) > 0)
break;
goto enoent;
}
if (saw_xdigit) {
if (tp + NS_INT16SZ > endp)
goto enoent;
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
}
if (bits == -1)
bits = 128;
endp = tmp + 16;
if (colonp != NULL) {
/*
* Since some memmove()'s erroneously fail to handle
* overlapping regions, we'll do the shift by hand.
*/
const int n = tp - colonp;
int i;
if (tp == endp)
goto enoent;
for (i = 1; i <= n; i++) {
endp[- i] = colonp[n - i];
colonp[n - i] = 0;
}
tp = endp;
}
if (tp != endp)
goto enoent;
/*
* Copy out the result.
*/
memcpy(dst, tmp, NS_IN6ADDRSZ);
return (bits);
enoent:
errno = ENOENT;
return (-1);
emsgsize:
errno = EMSGSIZE;
return (-1);
}
This diff is collapsed.
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_proc.h,v 1.304 2003/06/22 22:04:55 tgl Exp $
* $Id: pg_proc.h,v 1.305 2003/06/24 22:21:23 momjian Exp $
*
* NOTES
* The script catalog/genbki.sh reads this file and generates .bki
......@@ -2369,6 +2369,8 @@ DESCR("is-supernet-or-equal");
/* inet/cidr functions */
DATA(insert OID = 605 ( abbrev PGNSP PGUID 12 f f t f i 1 25 "869" network_abbrev - _null_ ));
DESCR("abbreviated display of inet/cidr value");
DATA(insert OID = 711 ( family PGNSP PGUID 12 f f t f i 1 23 "869" network_family - _null_ ));
DESCR("return address family (4 for IPv4, 6 for IPv6)");
DATA(insert OID = 683 ( network PGNSP PGUID 12 f f t f i 1 650 "869" network_network - _null_ ));
DESCR("network part of address");
DATA(insert OID = 696 ( netmask PGNSP PGUID 12 f f t f i 1 869 "869" network_netmask - _null_ ));
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: builtins.h,v 1.219 2003/05/26 00:11:28 tgl Exp $
* $Id: builtins.h,v 1.220 2003/06/24 22:21:23 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -644,6 +644,7 @@ extern Datum network_network(PG_FUNCTION_ARGS);
extern Datum network_netmask(PG_FUNCTION_ARGS);
extern Datum network_hostmask(PG_FUNCTION_ARGS);
extern Datum network_masklen(PG_FUNCTION_ARGS);
extern Datum network_family(PG_FUNCTION_ARGS);
extern Datum network_broadcast(PG_FUNCTION_ARGS);
extern Datum network_host(PG_FUNCTION_ARGS);
extern Datum network_show(PG_FUNCTION_ARGS);
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: inet.h,v 1.13 2002/06/20 20:29:53 momjian Exp $
* $Id: inet.h,v 1.14 2003/06/24 22:21:23 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -23,13 +23,19 @@ typedef struct
unsigned char family;
unsigned char bits;
unsigned char type;
union
{
unsigned int ipv4_addr; /* network byte order */
/* add IPV6 address type here */
} addr;
unsigned char ip_addr[16]; /* 128 bits of address */
} inet_struct;
/*
* Referencing all of the non-AF_INET types to AF_INET lets us work on
* machines which may not have the appropriate address family (like
* inet6 addresses when AF_INET6 isn't present) but doesn't cause a
* dump/reload requirement. Existing databases used AF_INET for the family
* type on disk.
*/
#define PGSQL_AF_INET (AF_INET + 0)
#define PGSQL_AF_INET6 (AF_INET + 1)
/*
* Both INET and CIDR addresses are represented within Postgres as varlena
* objects, ie, there is a varlena header (basically a length word) in front
......
This diff is collapsed.
......@@ -20,16 +20,20 @@ INSERT INTO INET_TBL (c, i) VALUES ('10.1', '10.1.2.3/16');
INSERT INTO INET_TBL (c, i) VALUES ('10', '10.1.2.3/8');
INSERT INTO INET_TBL (c, i) VALUES ('10', '11.1.2.3/8');
INSERT INTO INET_TBL (c, i) VALUES ('10', '9.1.2.3/8');
INSERT INTO INET_TBL (c, i) VALUES ('10:23::f1', '10:23::f1/64');
INSERT INTO INET_TBL (c, i) VALUES ('10:23::8000/113', '10:23::ffff');
INSERT INTO INET_TBL (c, i) VALUES ('::ffff:1.2.3.4', '::4.3.2.1/24');
-- check that CIDR rejects invalid input:
INSERT INTO INET_TBL (c, i) VALUES ('192.168.1.2/24', '192.168.1.226');
INSERT INTO INET_TBL (c, i) VALUES ('1234::1234::1234', '::1.2.3.4');
-- check that CIDR rejects invalid input when converting from text:
INSERT INTO INET_TBL (c, i) VALUES (cidr('192.168.1.2/24'), '192.168.1.226');
INSERT INTO INET_TBL (c, i) VALUES (cidr('ffff:ffff:ffff:ffff::/24'), '::192.168.1.226');
SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL;
-- now test some support functions
SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL;
SELECT '' AS ten, i AS inet, host(i), text(i), family(i) FROM INET_TBL;
SELECT '' AS ten, c AS cidr, broadcast(c),
i AS inet, broadcast(i) FROM INET_TBL;
SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment