mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-15 08:20:16 +08:00
New CIDR type and fixed INET type, from D'Arcy.
This commit is contained in:
parent
56f96f56df
commit
858a3b570a
@ -1,9 +1,9 @@
|
||||
/*
|
||||
* PostgreSQL type definitions for the INET type. This
|
||||
* PostgreSQL type definitions for the INET type. This
|
||||
* is for IP V4 CIDR notation, but prepared for V6: just
|
||||
* add the necessary bits where the comments indicate.
|
||||
*
|
||||
* $Id: inet.c,v 1.8 1998/10/21 04:25:25 momjian Exp $
|
||||
* $Id: inet.c,v 1.9 1998/10/21 16:06:45 momjian Exp $
|
||||
* Jon Postel RIP 16 Oct 1998
|
||||
*/
|
||||
|
||||
@ -22,7 +22,7 @@
|
||||
#include <utils/builtins.h>
|
||||
#include <utils/inet.h>
|
||||
|
||||
static int v4bitncmp(unsigned int a1, unsigned int a2, int bits);
|
||||
static int v4bitncmp(unsigned int a1, unsigned int a2, int bits);
|
||||
|
||||
/*
|
||||
* Access macros. Add IPV6 support.
|
||||
@ -37,11 +37,14 @@ static int v4bitncmp(unsigned int a1, unsigned int a2, int bits);
|
||||
#define ip_bits(inetptr) \
|
||||
(((inet_struct *)VARDATA(inetptr))->bits)
|
||||
|
||||
#define ip_type(inetptr) \
|
||||
(((inet_struct *)VARDATA(inetptr))->type)
|
||||
|
||||
#define ip_v4addr(inetptr) \
|
||||
(((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr)
|
||||
|
||||
/*
|
||||
* IP address reader.
|
||||
* INET address reader.
|
||||
*/
|
||||
|
||||
inet *
|
||||
@ -70,11 +73,12 @@ inet_in(char *src)
|
||||
+ ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
|
||||
+ ip_addrsize(dst);
|
||||
ip_bits(dst) = bits;
|
||||
ip_type(dst) = 0;
|
||||
return (dst);
|
||||
}
|
||||
|
||||
/*
|
||||
* IP address output function.
|
||||
* INET address output function.
|
||||
*/
|
||||
|
||||
char *
|
||||
@ -99,6 +103,8 @@ inet_out(inet *src)
|
||||
elog(ERROR, "unknown address family (%d)", ip_family(src));
|
||||
return (NULL);
|
||||
}
|
||||
if (ip_type(src) == 0 && ip_bits(src) == 32 && (dst = strchr(tmp, '/')) != NULL)
|
||||
*dst = 0;
|
||||
dst = palloc(strlen(tmp) + 1);
|
||||
if (dst == NULL)
|
||||
{
|
||||
@ -109,6 +115,39 @@ inet_out(inet *src)
|
||||
return (dst);
|
||||
}
|
||||
|
||||
/*
|
||||
* CIDR uses all of INET's funcs, just has a separate input func.
|
||||
*/
|
||||
|
||||
inet *
|
||||
cidr_in(char *src)
|
||||
{
|
||||
int bits;
|
||||
inet *dst;
|
||||
|
||||
dst = palloc(VARHDRSZ + sizeof(inet_struct));
|
||||
if (dst == NULL)
|
||||
{
|
||||
elog(ERROR, "unable to allocate memory in cidr_in()");
|
||||
return (NULL);
|
||||
}
|
||||
/* First, try for an IP V4 address: */
|
||||
ip_family(dst) = AF_INET;
|
||||
bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst), ip_addrsize(dst));
|
||||
if ((bits < 0) || (bits > 32))
|
||||
{
|
||||
/* Go for an IPV6 address here, before faulting out: */
|
||||
elog(ERROR, "could not parse \"%s\"", src);
|
||||
pfree(dst);
|
||||
return (NULL);
|
||||
}
|
||||
VARSIZE(dst) = VARHDRSZ
|
||||
+ ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
|
||||
+ ip_addrsize(dst);
|
||||
ip_bits(dst) = bits;
|
||||
return (dst);
|
||||
}
|
||||
|
||||
/*
|
||||
* Boolean tests for magnitude. Add V4/V6 testing!
|
||||
*/
|
||||
@ -267,20 +306,23 @@ inet_cmp(inet *a1, inet *a2)
|
||||
}
|
||||
|
||||
text *
|
||||
inet_netmask(inet *ip)
|
||||
inet_host(inet *ip)
|
||||
{
|
||||
text *ret;
|
||||
int len;
|
||||
text *ret;
|
||||
int len;
|
||||
char *ptr,
|
||||
tmp[sizeof("255.255.255.255/32")];
|
||||
|
||||
if (ip_type(ip))
|
||||
{
|
||||
elog(ERROR, "CIDR type has no host part");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ip_family(ip) == AF_INET)
|
||||
{
|
||||
/* It's an IP V4 address: */
|
||||
int addr = htonl((-1 << (32 - ip_bits(ip))) & 0xffffffff);
|
||||
|
||||
/* a little wasteful by why reinvent the wheel? */
|
||||
if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) < 0)
|
||||
if (inet_net_ntop(AF_INET, &ip_v4addr(ip), 32, tmp, sizeof(tmp)) < 0)
|
||||
{
|
||||
elog(ERROR, "unable to print netmask (%s)", strerror(errno));
|
||||
return (NULL);
|
||||
@ -298,7 +340,7 @@ inet_netmask(inet *ip)
|
||||
ret = palloc(len);
|
||||
if (ret == NULL)
|
||||
{
|
||||
elog(ERROR, "unable to allocate memory in inet_netmask()");
|
||||
elog(ERROR, "unable to allocate memory in inet_host()");
|
||||
return (NULL);
|
||||
}
|
||||
VARSIZE(ret) = len;
|
||||
@ -307,7 +349,7 @@ inet_netmask(inet *ip)
|
||||
}
|
||||
|
||||
int4
|
||||
inet_masklen(inet *ip)
|
||||
inet_netmasklen(inet *ip)
|
||||
{
|
||||
return ip_bits(ip);
|
||||
}
|
||||
@ -315,15 +357,16 @@ inet_masklen(inet *ip)
|
||||
text *
|
||||
inet_broadcast(inet *ip)
|
||||
{
|
||||
text *ret;
|
||||
int len;
|
||||
text *ret;
|
||||
int len;
|
||||
char *ptr,
|
||||
tmp[sizeof("255.255.255.255/32")] = "Hello";
|
||||
|
||||
if (ip_family(ip) == AF_INET)
|
||||
{
|
||||
/* It's an IP V4 address: */
|
||||
int addr = htonl(ntohl(ip_v4addr(ip)) | (0xffffffff >> ip_bits(ip)));
|
||||
int addr = htonl(ntohl(ip_v4addr(ip)) | (0xffffffff >> ip_bits(ip)));
|
||||
|
||||
/* int addr = htonl(ip_v4addr(ip) | (0xffffffff >> ip_bits(ip))); */
|
||||
|
||||
if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) < 0)
|
||||
@ -352,6 +395,45 @@ inet_broadcast(inet *ip)
|
||||
return (ret);
|
||||
}
|
||||
|
||||
text *
|
||||
inet_netmask(inet *ip)
|
||||
{
|
||||
text *ret;
|
||||
int len;
|
||||
char *ptr,
|
||||
tmp[sizeof("255.255.255.255/32")];
|
||||
|
||||
if (ip_family(ip) == AF_INET)
|
||||
{
|
||||
/* It's an IP V4 address: */
|
||||
int addr = htonl((-1 << (32 - ip_bits(ip))) & 0xffffffff);
|
||||
|
||||
if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) < 0)
|
||||
{
|
||||
elog(ERROR, "unable to print netmask (%s)", strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Go for an IPV6 address here, before faulting out: */
|
||||
elog(ERROR, "unknown address family (%d)", ip_family(ip));
|
||||
return (NULL);
|
||||
}
|
||||
if ((ptr = strchr(tmp, '/')) != NULL)
|
||||
*ptr = 0;
|
||||
len = VARHDRSZ + strlen(tmp);
|
||||
ret = palloc(len);
|
||||
if (ret == NULL)
|
||||
{
|
||||
elog(ERROR, "unable to allocate memory in inet_netmask()");
|
||||
return (NULL);
|
||||
}
|
||||
VARSIZE(ret) = len;
|
||||
strcpy(VARDATA(ret), tmp);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Bitwise comparison for V4 addresses. Add V6 implementation!
|
||||
*/
|
||||
@ -372,4 +454,3 @@ v4bitncmp(unsigned int a1, unsigned int a2, int bits)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: pg_opclass.h,v 1.13 1998/10/08 00:19:38 momjian Exp $
|
||||
* $Id: pg_opclass.h,v 1.14 1998/10/21 16:06:46 momjian Exp $
|
||||
*
|
||||
* NOTES
|
||||
* the genbki.sh script reads this file and generates .bki
|
||||
@ -111,5 +111,7 @@ DATA(insert OID = 810 ( macaddr_ops 829 ));
|
||||
DESCR("");
|
||||
DATA(insert OID = 935 ( inet_ops 869 ));
|
||||
DESCR("");
|
||||
DATA(insert OID = 652 ( inet_ops 650 ));
|
||||
DESCR("");
|
||||
|
||||
#endif /* PG_OPCLASS_H */
|
||||
|
@ -7,7 +7,7 @@
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: pg_operator.h,v 1.40 1998/10/08 00:19:39 momjian Exp $
|
||||
* $Id: pg_operator.h,v 1.41 1998/10/21 16:06:46 momjian Exp $
|
||||
*
|
||||
* NOTES
|
||||
* the genbki.sh script reads this file and generates .bki
|
||||
@ -643,7 +643,7 @@ DATA(insert OID = 1223 ( "<=" PGUID 0 b t f 829 829 16 1225 1224 0 0 macadd
|
||||
DATA(insert OID = 1224 ( ">" PGUID 0 b t f 829 829 16 1222 1223 0 0 macaddr_gt intltsel intltjoinsel ));
|
||||
DATA(insert OID = 1225 ( ">=" PGUID 0 b t f 829 829 16 1223 1222 0 0 macaddr_ge intltsel intltjoinsel ));
|
||||
|
||||
/* IP type */
|
||||
/* INET type */
|
||||
DATA(insert OID = 1201 ( "=" PGUID 0 b t t 869 869 16 1201 1202 0 0 inet_eq eqsel eqjoinsel ));
|
||||
DATA(insert OID = 1202 ( "<>" PGUID 0 b t f 869 869 16 1202 1201 0 0 inet_ne neqsel neqjoinsel ));
|
||||
DATA(insert OID = 1203 ( "<" PGUID 0 b t f 869 869 16 1205 1206 0 0 inet_lt intltsel intltjoinsel ));
|
||||
@ -655,6 +655,18 @@ DATA(insert OID = 932 ( "<<=" PGUID 0 b t f 869 869 16 934 933 0 0 in
|
||||
DATA(insert OID = 933 ( ">>" PGUID 0 b t f 869 869 16 931 932 0 0 inet_sup intltsel intltjoinsel ));
|
||||
DATA(insert OID = 934 ( ">>=" PGUID 0 b t f 869 869 16 932 931 0 0 inet_supeq intltsel intltjoinsel ));
|
||||
|
||||
/* CIDR type */
|
||||
DATA(insert OID = 820 ( "=" PGUID 0 b t t 650 650 16 820 821 0 0 inet_eq eqsel eqjoinsel ));
|
||||
DATA(insert OID = 821 ( "<>" PGUID 0 b t f 650 650 16 821 820 0 0 inet_ne neqsel neqjoinsel ));
|
||||
DATA(insert OID = 822 ( "<" PGUID 0 b t f 650 650 16 824 825 0 0 inet_lt intltsel intltjoinsel ));
|
||||
DATA(insert OID = 823 ( "<=" PGUID 0 b t f 650 650 16 825 824 0 0 inet_le intltsel intltjoinsel ));
|
||||
DATA(insert OID = 824 ( ">" PGUID 0 b t f 650 650 16 822 823 0 0 inet_gt intltsel intltjoinsel ));
|
||||
DATA(insert OID = 825 ( ">=" PGUID 0 b t f 650 650 16 823 822 0 0 inet_ge intltsel intltjoinsel ));
|
||||
DATA(insert OID = 826 ( "<<" PGUID 0 b t f 650 650 16 828 1004 0 0 inet_sub intltsel intltjoinsel ));
|
||||
DATA(insert OID = 827 ( "<<=" PGUID 0 b t f 650 650 16 1004 828 0 0 inet_subeq intltsel intltjoinsel ));
|
||||
DATA(insert OID = 828 ( ">>" PGUID 0 b t f 650 650 16 826 827 0 0 inet_sup intltsel intltjoinsel ));
|
||||
DATA(insert OID = 1004 ( ">>=" PGUID 0 b t f 650 650 16 827 826 0 0 inet_supeq intltsel intltjoinsel ));
|
||||
|
||||
|
||||
/*
|
||||
* function prototypes
|
||||
|
@ -6,7 +6,7 @@
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: pg_proc.h,v 1.74 1998/10/20 23:03:19 momjian Exp $
|
||||
* $Id: pg_proc.h,v 1.75 1998/10/21 16:06:47 momjian Exp $
|
||||
*
|
||||
* NOTES
|
||||
* The script catalog/genbki.sh reads this file and generates .bki
|
||||
@ -2068,12 +2068,17 @@ DESCR("less-equal-greater");
|
||||
DATA(insert OID = 837 ( macaddr_manuf PGUID 11 f t f 1 f 25 "829" 100 0 0 100 foo bar ));
|
||||
DESCR("MAC manufacturer");
|
||||
|
||||
/* for ip type support */
|
||||
/* for inet type support */
|
||||
DATA(insert OID = 910 ( inet_in PGUID 11 f t f 1 f 869 "0" 100 0 0 100 foo bar ));
|
||||
DESCR("(internal)");
|
||||
DATA(insert OID = 911 ( inet_out PGUID 11 f t f 1 f 23 "0" 100 0 0 100 foo bar ));
|
||||
DESCR("(internal)");
|
||||
|
||||
/* for cidr type support */
|
||||
DATA(insert OID = 1267 ( cidr_in PGUID 11 f t f 1 f 650 "0" 100 0 0 100 foo bar ));
|
||||
DESCR("(internal)");
|
||||
|
||||
/* these are used for both inet and cidr */
|
||||
DATA(insert OID = 920 ( inet_eq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar ));
|
||||
DESCR("equal");
|
||||
DATA(insert OID = 921 ( inet_lt PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar ));
|
||||
@ -2097,12 +2102,36 @@ DESCR("is-supernet");
|
||||
DATA(insert OID = 930 ( inet_supeq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar ));
|
||||
DESCR("is-supernet-or-equal");
|
||||
|
||||
/* inet/cidr base versions */
|
||||
DATA(insert OID = 940 ( inet_netmask PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
|
||||
DESCR("netmask of inet address");
|
||||
DATA(insert OID = 941 ( inet_masklen PGUID 11 f t f 1 f 23 "869" 100 0 0 100 foo bar ));
|
||||
DATA(insert OID = 941 ( inet_netmasklen PGUID 11 f t f 1 f 23 "869" 100 0 0 100 foo bar ));
|
||||
DESCR("netmask length");
|
||||
DATA(insert OID = 945 ( inet_broadcast PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
|
||||
DESCR("broadcast address");
|
||||
DATA(insert OID = 682 ( inet_host PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
|
||||
DESCR("host address");
|
||||
|
||||
/* inet versions */
|
||||
DATA(insert OID = 940 ( netmask PGUID 14 f t f 1 f 25 "869" 100 0 0 100 "select inet_netmask($1)" - ));
|
||||
DESCR("netmask of address");
|
||||
DATA(insert OID = 941 ( netmasklen PGUID 14 f t f 1 f 23 "869" 100 0 0 100 "select inet_netmasklen($1)" - ));
|
||||
DESCR("netmask length");
|
||||
DATA(insert OID = 945 ( broadcast PGUID 14 f t f 1 f 25 "869" 100 0 0 100 "select inet_broadcast($1)" - ));
|
||||
DESCR("broadcast address");
|
||||
DATA(insert OID = 682 ( host PGUID 14 f t f 1 f 25 "869" 100 0 0 100 "select inet_host($1)" - ));
|
||||
DESCR("host address");
|
||||
|
||||
/* cidr versions */
|
||||
DATA(insert OID = 940 ( netmask PGUID 14 f t f 1 f 25 "650" 100 0 0 100 "select inet_netmask($1)" - ));
|
||||
DESCR("netmask of address");
|
||||
DATA(insert OID = 941 ( netmasklen PGUID 14 f t f 1 f 23 "650" 100 0 0 100 "select inet_netmasklen($1)" - ));
|
||||
DESCR("netmask length");
|
||||
DATA(insert OID = 945 ( broadcast PGUID 14 f t f 1 f 25 "650" 100 0 0 100 "select inet_broadcast($1)" - ));
|
||||
DESCR("broadcast address");
|
||||
DATA(insert OID = 682 ( host PGUID 14 f t f 1 f 25 "650" 100 0 0 100 "select inet_host($1)" - ));
|
||||
DESCR("host address");
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
@ -7,7 +7,7 @@
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: pg_type.h,v 1.49 1998/10/08 00:19:42 momjian Exp $
|
||||
* $Id: pg_type.h,v 1.50 1998/10/21 16:06:48 momjian Exp $
|
||||
*
|
||||
* NOTES
|
||||
* the genbki.sh script reads this file and generates .bki
|
||||
@ -301,7 +301,9 @@ DATA(insert OID = 791 ( _money PGUID -1 -1 f b t \054 0 790 array_in array
|
||||
DATA(insert OID = 829 ( macaddr PGUID 6 -1 f b t \054 0 0 macaddr_in macaddr_out macaddr_in macaddr_out i _null_ ));
|
||||
DESCR("MAC address");
|
||||
DATA(insert OID = 869 ( inet PGUID -1 -1 f b t \054 0 0 inet_in inet_out inet_in inet_out i _null_ ));
|
||||
DESCR("IP address");
|
||||
DESCR("Host address");
|
||||
DATA(insert OID = 650 ( cidr PGUID -1 -1 f b t \054 0 0 cidr_in inet_out cidr_in inet_out i _null_ ));
|
||||
DESCR("Network address");
|
||||
|
||||
/* OIDS 900 - 999 */
|
||||
|
||||
@ -340,6 +342,7 @@ DESCR("access control list");
|
||||
DATA(insert OID = 1034 ( _aclitem PGUID -1 -1 f b t \054 0 1033 array_in array_out array_in array_out i _null_ ));
|
||||
DATA(insert OID = 1040 ( _macaddr PGUID -1 -1 f b t \054 0 829 array_in array_out array_in array_out i _null_ ));
|
||||
DATA(insert OID = 1041 ( _inet PGUID -1 -1 f b t \054 0 869 array_in array_out array_in array_out i _null_ ));
|
||||
DATA(insert OID = 651 ( _cidr PGUID -1 -1 f b t \054 0 650 array_in array_out array_in array_out i _null_ ));
|
||||
DATA(insert OID = 1042 ( bpchar PGUID -1 -1 f b t \054 0 18 bpcharin bpcharout bpcharin bpcharout i _null_ ));
|
||||
DESCR("blank-padded characters, length specifed when created");
|
||||
#define BPCHAROID 1042
|
||||
|
@ -6,7 +6,7 @@
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: builtins.h,v 1.62 1998/10/20 23:03:20 momjian Exp $
|
||||
* $Id: builtins.h,v 1.63 1998/10/21 16:06:49 momjian Exp $
|
||||
*
|
||||
* NOTES
|
||||
* This should normally only be included by fmgr.h.
|
||||
@ -518,6 +518,9 @@ int inet_net_pton(int af, const char *src, void *dst, size_t size);
|
||||
char *inet_cidr_ntop(int af, const void *src, size_t len, int bits, char *dst, size_t size);
|
||||
int inet_cidr_pton(int af, const void *src, void *dst, size_t size, int *used);
|
||||
|
||||
/* cidr.c */
|
||||
inet *cidr_in(char *str);
|
||||
|
||||
/* inet.c */
|
||||
inet *inet_in(char *str);
|
||||
char *inet_out(inet * addr);
|
||||
@ -534,8 +537,9 @@ bool inet_supeq(inet * a1, inet * a2);
|
||||
int4 inet_cmp(inet * a1, inet * a2);
|
||||
|
||||
text *inet_netmask(inet * addr);
|
||||
int4 inet_masklen(inet * addr);
|
||||
int4 inet_netmasklen(inet * addr);
|
||||
text *inet_broadcast(inet * addr);
|
||||
text *inet_host(inet * addr);
|
||||
|
||||
|
||||
/* mac.c */
|
||||
|
@ -6,7 +6,7 @@
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Id: inet.h,v 1.1 1998/10/08 00:19:45 momjian Exp $
|
||||
* $Id: inet.h,v 1.2 1998/10/21 16:06:50 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -21,6 +21,7 @@ typedef struct
|
||||
{
|
||||
unsigned char family;
|
||||
unsigned char bits;
|
||||
unsigned char type;
|
||||
union
|
||||
{
|
||||
unsigned int ipv4_addr; /* network byte order */
|
||||
|
Loading…
Reference in New Issue
Block a user