From 8849655d24bf6d73102b92f900d6d78b6600dca2 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 8 Oct 1998 00:19:47 +0000 Subject: I agree. I think, though, that the best argument presented in the debate was from Paul Vixie, who wanted INET to be the name covering both IPV4 and IPV6. The following kit makes the needed changes: Tom Ivar Helbekkmo --- doc/README.inet | 74 ++++++++++ doc/README.ipaddr | 74 ---------- src/backend/utils/adt/Makefile | 4 +- src/backend/utils/adt/inet.c | 287 ++++++++++++++++++++++++++++++++++++++ src/backend/utils/adt/ip.c | 285 ------------------------------------- src/backend/utils/adt/mac.c | 4 +- src/include/catalog/pg_opclass.h | 4 +- src/include/catalog/pg_operator.h | 22 +-- src/include/catalog/pg_proc.h | 28 ++-- src/include/catalog/pg_type.h | 6 +- src/include/utils/builtins.h | 33 +++-- src/include/utils/inet.h | 58 ++++++++ src/include/utils/mac.h | 58 -------- src/tools/pgindent/pgindent | 2 +- 14 files changed, 470 insertions(+), 469 deletions(-) create mode 100644 doc/README.inet delete mode 100644 doc/README.ipaddr create mode 100644 src/backend/utils/adt/inet.c delete mode 100644 src/backend/utils/adt/ip.c create mode 100644 src/include/utils/inet.h delete mode 100644 src/include/utils/mac.h diff --git a/doc/README.inet b/doc/README.inet new file mode 100644 index 0000000000..69df7dfc2c --- /dev/null +++ b/doc/README.inet @@ -0,0 +1,74 @@ +PostgreSQL type extensions for IP and MAC addresses. +--------------------------------------------------- + +$Id: README.inet,v 1.1 1998/10/08 00:19:32 momjian Exp $ + +I needed to record IP and MAC level ethernet addresses in a data +base, and I really didn't want to store them as plain strings, with +no enforced error checking, so I put together the accompanying code +as my first experiment with adding a data type to PostgreSQL. I +then thought that this might be useful to others, both directly and +as a very simple example of how to do this sort of thing, so I +submitted it to the PostgreSQL project for inclusion in the contrib +directory. Since then, that directory has been modified to contain +Aleksei Roudnev's implementation, which is based on mine. + +For those who have seen my previous contribution of these types, note +that much has changed: I've modified the IP address type to work the +way Paul Vixie did with his CIDR type. In fact, I've pretty much just +stolen his solution, modifying it into my framework in such a way as +to facilitate the addition of IPV6 handling code in the future. I've +pretty much ignored Aleksei's C code, but I've added his SQL code to +enter the necessary operators into the various system tables needed to +make the types indexable. + +IP addresses are implemented as a struct of fixed in-memory length, +but variable on-disk storage size. For IPV4, it contains the address +family (AF_INET), the CIDR prefix length and four byte address. For +IPV6, the address family will be different, and the address longer. + +The external representation of an IP address generally looks like +'158.37.96.15/32'. This address happens to be part of a subnet where +I work; '158.37.96.0/24', which itself is a part of the larger subnet +allocated to our site, which is '158.37.96.0/21', which again, if you +go by the old book, is part of the class "B" net '158.37.0.0/16'. + +Input and output functions are supplied, along with the "normal" <, +<=, =, >=, > and <> operators, which all do what you expect. In +addition, there are operators to check for networks or addresses being +subnets of or addresses contained within other networks. << tests +whether the left operand is contained within the right, <<= includes +equality, >> and >>= do the same things the opposite way. + +The input and output functions use routines from Paul Vixie's BIND, +and I've snarfed the source files inet_net_ntop.c and inet_net_pton.c +directly from a recent distribution of that code. They are included +here to avoid the need to fetch and install the BIND libraries to be +able to use this code. IANAL, but it looks from the copyright +messages in the files as if this should be acceptable. Read the +documentation in inet_net_pton.c to see the legal input formats. + +MAC level ethernet addresses are implemented as a 6 byte struct that +contains the address as unsigned chars. Several input forms are +accepted; the following are all the same address: '08002b:010203', +'08002b-010203', '0800.2b01.0203', '08-00-2b-01-02-03' and +'08:00:2b:01:02:03'. Upper and lower case is accepted for the digits +'a' through 'f'. Output is always in the latter of the given forms. + +As with IP addresses, input and output functions are supplied as well +as the "normal" operators, which do what you expect. As an extra +feature, a function macaddr_manuf() is defined, which returns the name +of the manufacturer as a string. This is currently held in a +hard-coded struct internal to the C module -- it might be smarter to +put this information into an actual data base table, and look up the +manufacturer there. + +Many thanks to Aleksei Roudnev and Paul Vixie for their fine work! + +I don't know what changes are needed to the Makefile for other systems +than the one I'm running (NetBSD 1.3), but anyway: to install on a BSD +system: fix the path names in the Makefile if you need to, then make, +make install, slurp the SQL files into psql or whatever, and you're +off. Enjoy! + +Bergen, Norway, 1998-08-09, Tom Ivar Helbekkmo (tih@nhh.no). diff --git a/doc/README.ipaddr b/doc/README.ipaddr deleted file mode 100644 index 7e7883a47e..0000000000 --- a/doc/README.ipaddr +++ /dev/null @@ -1,74 +0,0 @@ -PostgreSQL type extensions for IP and MAC addresses. ---------------------------------------------------- - -$Id: README.ipaddr,v 1.1 1998/10/03 05:40:41 momjian Exp $ - -I needed to record IP and MAC level ethernet addresses in a data -base, and I really didn't want to store them as plain strings, with -no enforced error checking, so I put together the accompanying code -as my first experiment with adding a data type to PostgreSQL. I -then thought that this might be useful to others, both directly and -as a very simple example of how to do this sort of thing, so I -submitted it to the PostgreSQL project for inclusion in the contrib -directory. Since then, that directory has been modified to contain -Aleksei Roudnev's implementation, which is based on mine. - -For those who have seen my previous contribution of these types, note -that much has changed: I've modified the IP address type to work the -way Paul Vixie did with his CIDR type. In fact, I've pretty much just -stolen his solution, modifying it into my framework in such a way as -to facilitate the addition of IPV6 handling code in the future. I've -pretty much ignored Aleksei's C code, but I've added his SQL code to -enter the necessary operators into the various system tables needed to -make the types indexable. - -IP addresses are implemented as a struct of fixed in-memory length, -but variable on-disk storage size. For IPV4, it contains the address -family (AF_INET), the CIDR prefix length and four byte address. For -IPV6, the address family will be different, and the address longer. - -The external representation of an IP address generally looks like -'158.37.96.15/32'. This address happens to be part of a subnet where -I work; '158.37.96.0/24', which itself is a part of the larger subnet -allocated to our site, which is '158.37.96.0/21', which again, if you -go by the old book, is part of the class "B" net '158.37.0.0/16'. - -Input and output functions are supplied, along with the "normal" <, -<=, =, >=, > and <> operators, which all do what you expect. In -addition, there are operators to check for networks or addresses being -subnets of or addresses contained within other networks. << tests -whether the left operand is contained within the right, <<= includes -equality, >> and >>= do the same things the opposite way. - -The input and output functions use routines from Paul Vixie's BIND, -and I've snarfed the source files inet_net_ntop.c and inet_net_pton.c -directly from a recent distribution of that code. They are included -here to avoid the need to fetch and install the BIND libraries to be -able to use this code. IANAL, but it looks from the copyright -messages in the files as if this should be acceptable. Read the -documentation in inet_net_pton.c to see the legal input formats. - -MAC level ethernet addresses are implemented as a 6 byte struct that -contains the address as unsigned chars. Several input forms are -accepted; the following are all the same address: '08002b:010203', -'08002b-010203', '0800.2b01.0203', '08-00-2b-01-02-03' and -'08:00:2b:01:02:03'. Upper and lower case is accepted for the digits -'a' through 'f'. Output is always in the latter of the given forms. - -As with IP addresses, input and output functions are supplied as well -as the "normal" operators, which do what you expect. As an extra -feature, a function macaddr_manuf() is defined, which returns the name -of the manufacturer as a string. This is currently held in a -hard-coded struct internal to the C module -- it might be smarter to -put this information into an actual data base table, and look up the -manufacturer there. - -Many thanks to Aleksei Roudnev and Paul Vixie for their fine work! - -I don't know what changes are needed to the Makefile for other systems -than the one I'm running (NetBSD 1.3), but anyway: to install on a BSD -system: fix the path names in the Makefile if you need to, then make, -make install, slurp the SQL files into psql or whatever, and you're -off. Enjoy! - -Bergen, Norway, 1998-08-09, Tom Ivar Helbekkmo (tih@nhh.no). diff --git a/src/backend/utils/adt/Makefile b/src/backend/utils/adt/Makefile index 686a2d0e8c..7ac61fc5d6 100644 --- a/src/backend/utils/adt/Makefile +++ b/src/backend/utils/adt/Makefile @@ -4,7 +4,7 @@ # Makefile for utils/adt # # IDENTIFICATION -# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.18 1998/10/03 05:40:47 momjian Exp $ +# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.19 1998/10/08 00:19:33 momjian Exp $ # #------------------------------------------------------------------------- @@ -24,7 +24,7 @@ OBJS = acl.o arrayfuncs.o arrayutils.o bool.o cash.o char.o chunk.o \ oid.o oracle_compat.o \ regexp.o regproc.o ruleutils.o selfuncs.o sets.o \ tid.o timestamp.o varchar.o varlena.o version.o \ - ip.o mac.o inet_net_ntop.o inet_net_pton.o + inet.o mac.o inet_net_ntop.o inet_net_pton.o all: SUBSYS.o diff --git a/src/backend/utils/adt/inet.c b/src/backend/utils/adt/inet.c new file mode 100644 index 0000000000..25e0436214 --- /dev/null +++ b/src/backend/utils/adt/inet.c @@ -0,0 +1,287 @@ +/* + * 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.1 1998/10/08 00:19:35 momjian Exp $ + */ + +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +static int v4bitncmp(unsigned int a1, unsigned int a2, int bits); + +/* + * Access macros. Add IPV6 support. + */ + +#define ip_addrsize(inetptr) \ + (((inet_struct *)VARDATA(inetptr))->family == AF_INET ? 4 : -1) + +#define ip_family(inetptr) \ + (((inet_struct *)VARDATA(inetptr))->family) + +#define ip_bits(inetptr) \ + (((inet_struct *)VARDATA(inetptr))->bits) + +#define ip_v4addr(inetptr) \ + (((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr) + +/* + * IP address reader. + */ + +inet * +inet_in(char *src) +{ + int bits; + inet *dst; + + dst = palloc(VARHDRSZ + sizeof(inet_struct)); + if (dst == NULL) + { + elog(ERROR, "unable to allocate memory in inet_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); +} + +/* + * IP address output function. + */ + +char * +inet_out(inet *src) +{ + char *dst, + tmp[sizeof("255.255.255.255/32")]; + + if (ip_family(src) == AF_INET) + { + /* It's an IP V4 address: */ + if (inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src), + tmp, sizeof(tmp)) < 0) + { + elog(ERROR, "unable to print address (%s)", strerror(errno)); + return (NULL); + } + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(src)); + return (NULL); + } + dst = palloc(strlen(tmp) + 1); + if (dst == NULL) + { + elog(ERROR, "unable to allocate memory in inet_out()"); + return (NULL); + } + strcpy(dst, tmp); + return (dst); +} + +/* + * Boolean tests for magnitude. Add V4/V6 testing! + */ + +bool +inet_lt(inet *a1, inet *a2) +{ + if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) + { + int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)); + + return ((order < 0) || ((order == 0) && (ip_bits(a1) < ip_bits(a2)))); + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "cannot compare address families %d and %d", + ip_family(a1), ip_family(a2)); + return (FALSE); + } +} + +bool +inet_le(inet *a1, inet *a2) +{ + return (inet_lt(a1, a2) || inet_eq(a1, a2)); +} + +bool +inet_eq(inet *a1, inet *a2) +{ + if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) + { + return ((ip_bits(a1) == ip_bits(a2)) + && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0)); + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "cannot compare address families %d and %d", + ip_family(a1), ip_family(a2)); + return (FALSE); + } +} + +bool +inet_ge(inet *a1, inet *a2) +{ + return (inet_gt(a1, a2) || inet_eq(a1, a2)); +} + +bool +inet_gt(inet *a1, inet *a2) +{ + if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) + { + int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)); + + return ((order > 0) || ((order == 0) && (ip_bits(a1) > ip_bits(a2)))); + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "cannot compare address families %d and %d", + ip_family(a1), ip_family(a2)); + return (FALSE); + } +} + +bool +inet_ne(inet *a1, inet *a2) +{ + return (!inet_eq(a1, a2)); +} + +bool +inet_sub(inet *a1, inet *a2) +{ + if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) + { + return ((ip_bits(a1) > ip_bits(a2)) + && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0)); + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "cannot compare address families %d and %d", + ip_family(a1), ip_family(a2)); + return (FALSE); + } +} + +bool +inet_subeq(inet *a1, inet *a2) +{ + if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) + { + return ((ip_bits(a1) >= ip_bits(a2)) + && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0)); + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "cannot compare address families %d and %d", + ip_family(a1), ip_family(a2)); + return (FALSE); + } +} + +bool +inet_sup(inet *a1, inet *a2) +{ + if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) + { + return ((ip_bits(a1) < ip_bits(a2)) + && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0)); + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "cannot compare address families %d and %d", + ip_family(a1), ip_family(a2)); + return (FALSE); + } +} + +bool +inet_supeq(inet *a1, inet *a2) +{ + if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) + { + return ((ip_bits(a1) <= ip_bits(a2)) + && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0)); + } + else + { + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "cannot compare address families %d and %d", + ip_family(a1), ip_family(a2)); + return (FALSE); + } +} + +/* + * Comparison function for sorting. Add V4/V6 testing! + */ + +int4 +inet_cmp(inet *a1, inet *a2) +{ + if (ntohl(ip_v4addr(a1)) < ntohl(ip_v4addr(a2))) + return (-1); + else if (ntohl(ip_v4addr(a1)) > ntohl(ip_v4addr(a2))) + return (1); + return 0; +} + +/* + * Bitwise comparison for V4 addresses. Add V6 implementation! + */ + +static int +v4bitncmp(unsigned int a1, unsigned int a2, int bits) +{ + unsigned long mask = 0; + int i; + + for (i = 0; i < bits; i++) + mask = (mask >> 1) | 0x80000000; + a1 = ntohl(a1); + a2 = ntohl(a2); + if ((a1 & mask) < (a2 & mask)) + return (-1); + else if ((a1 & mask) > (a2 & mask)) + return (1); + return (0); +} diff --git a/src/backend/utils/adt/ip.c b/src/backend/utils/adt/ip.c deleted file mode 100644 index 9b4ede1c64..0000000000 --- a/src/backend/utils/adt/ip.c +++ /dev/null @@ -1,285 +0,0 @@ -/* - * PostgreSQL type definitions for IP addresses. This - * is for IP V4 CIDR notation, but prepared for V6: just - * add the necessary bits where the comments indicate. - * - * $Id: ip.c,v 1.4 1998/10/04 16:24:30 momjian Exp $ - */ - -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -/* - * Access macros. Add IPV6 support. - */ - -#define ip_addrsize(ipaddrptr) \ - (((ipaddr_struct *)VARDATA(ipaddrptr))->family == AF_INET ? 4 : -1) - -#define ip_family(ipaddrptr) \ - (((ipaddr_struct *)VARDATA(ipaddrptr))->family) - -#define ip_bits(ipaddrptr) \ - (((ipaddr_struct *)VARDATA(ipaddrptr))->bits) - -#define ip_v4addr(ipaddrptr) \ - (((ipaddr_struct *)VARDATA(ipaddrptr))->addr.ipv4_addr) - -/* - * IP address reader. - */ - -ipaddr * -ipaddr_in(char *src) -{ - int bits; - ipaddr *dst; - - dst = palloc(VARHDRSZ + sizeof(ipaddr_struct)); - if (dst == NULL) - { - elog(ERROR, "unable to allocate memory in ipaddr_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); -} - -/* - * IP address output function. - */ - -char * -ipaddr_out(ipaddr *src) -{ - char *dst, - tmp[sizeof("255.255.255.255/32")]; - - if (ip_family(src) == AF_INET) - { - /* It's an IP V4 address: */ - if (inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src), - tmp, sizeof(tmp)) < 0) - { - elog(ERROR, "unable to print address (%s)", strerror(errno)); - return (NULL); - } - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "unknown address family (%d)", ip_family(src)); - return (NULL); - } - dst = palloc(strlen(tmp) + 1); - if (dst == NULL) - { - elog(ERROR, "unable to allocate memory in ipaddr_out()"); - return (NULL); - } - strcpy(dst, tmp); - return (dst); -} - -/* - * Boolean tests for magnitude. Add V4/V6 testing! - */ - -bool -ipaddr_lt(ipaddr *a1, ipaddr *a2) -{ - if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) - { - int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)); - - return ((order < 0) || ((order == 0) && (ip_bits(a1) < ip_bits(a2)))); - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "cannot compare address families %d and %d", - ip_family(a1), ip_family(a2)); - return (FALSE); - } -} - -bool -ipaddr_le(ipaddr *a1, ipaddr *a2) -{ - return (ipaddr_lt(a1, a2) || ipaddr_eq(a1, a2)); -} - -bool -ipaddr_eq(ipaddr *a1, ipaddr *a2) -{ - if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) - { - return ((ip_bits(a1) == ip_bits(a2)) - && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0)); - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "cannot compare address families %d and %d", - ip_family(a1), ip_family(a2)); - return (FALSE); - } -} - -bool -ipaddr_ge(ipaddr *a1, ipaddr *a2) -{ - return (ipaddr_gt(a1, a2) || ipaddr_eq(a1, a2)); -} - -bool -ipaddr_gt(ipaddr *a1, ipaddr *a2) -{ - if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) - { - int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)); - - return ((order > 0) || ((order == 0) && (ip_bits(a1) > ip_bits(a2)))); - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "cannot compare address families %d and %d", - ip_family(a1), ip_family(a2)); - return (FALSE); - } -} - -bool -ipaddr_ne(ipaddr *a1, ipaddr *a2) -{ - return (!ipaddr_eq(a1, a2)); -} - -bool -ipaddr_sub(ipaddr *a1, ipaddr *a2) -{ - if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) - { - return ((ip_bits(a1) > ip_bits(a2)) - && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0)); - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "cannot compare address families %d and %d", - ip_family(a1), ip_family(a2)); - return (FALSE); - } -} - -bool -ipaddr_subeq(ipaddr *a1, ipaddr *a2) -{ - if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) - { - return ((ip_bits(a1) >= ip_bits(a2)) - && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0)); - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "cannot compare address families %d and %d", - ip_family(a1), ip_family(a2)); - return (FALSE); - } -} - -bool -ipaddr_sup(ipaddr *a1, ipaddr *a2) -{ - if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) - { - return ((ip_bits(a1) < ip_bits(a2)) - && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0)); - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "cannot compare address families %d and %d", - ip_family(a1), ip_family(a2)); - return (FALSE); - } -} - -bool -ipaddr_supeq(ipaddr *a1, ipaddr *a2) -{ - if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) - { - return ((ip_bits(a1) <= ip_bits(a2)) - && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0)); - } - else - { - /* Go for an IPV6 address here, before faulting out: */ - elog(ERROR, "cannot compare address families %d and %d", - ip_family(a1), ip_family(a2)); - return (FALSE); - } -} - -/* - * Comparison function for sorting. Add V4/V6 testing! - */ - -int4 -ipaddr_cmp(ipaddr *a1, ipaddr *a2) -{ - if (ntohl(ip_v4addr(a1)) < ntohl(ip_v4addr(a2))) - return (-1); - else if (ntohl(ip_v4addr(a1)) > ntohl(ip_v4addr(a2))) - return (1); - return 0; -} - -/* - * Bitwise comparison for V4 addresses. Add V6 implementation! - */ - -int -v4bitncmp(unsigned int a1, unsigned int a2, int bits) -{ - unsigned long mask = 0; - int i; - - for (i = 0; i < bits; i++) - mask = (mask >> 1) | 0x80000000; - a1 = ntohl(a1); - a2 = ntohl(a2); - if ((a1 & mask) < (a2 & mask)) - return (-1); - else if ((a1 & mask) > (a2 & mask)) - return (1); - return (0); -} diff --git a/src/backend/utils/adt/mac.c b/src/backend/utils/adt/mac.c index 6d33f148dc..488f1dd11a 100644 --- a/src/backend/utils/adt/mac.c +++ b/src/backend/utils/adt/mac.c @@ -1,7 +1,7 @@ /* * PostgreSQL type definitions for MAC addresses. * - * $Id: mac.c,v 1.3 1998/10/04 16:24:32 momjian Exp $ + * $Id: mac.c,v 1.4 1998/10/08 00:19:36 momjian Exp $ */ #include @@ -10,7 +10,7 @@ #include #include #include -#include +#include manufacturer manufacturers[] = { {0x00, 0x00, 0x0C, "Cisco"}, diff --git a/src/include/catalog/pg_opclass.h b/src/include/catalog/pg_opclass.h index 388f598630..94186e469c 100644 --- a/src/include/catalog/pg_opclass.h +++ b/src/include/catalog/pg_opclass.h @@ -7,7 +7,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_opclass.h,v 1.12 1998/10/03 05:40:54 momjian Exp $ + * $Id: pg_opclass.h,v 1.13 1998/10/08 00:19:38 momjian Exp $ * * NOTES * the genbki.sh script reads this file and generates .bki @@ -109,7 +109,7 @@ DATA(insert OID = 1313 ( timespan_ops 1186 )); DESCR(""); DATA(insert OID = 810 ( macaddr_ops 829 )); DESCR(""); -DATA(insert OID = 935 ( ipaddr_ops 869 )); +DATA(insert OID = 935 ( inet_ops 869 )); DESCR(""); #endif /* PG_OPCLASS_H */ diff --git a/src/include/catalog/pg_operator.h b/src/include/catalog/pg_operator.h index 4604ccd7a9..14166d5aef 100644 --- a/src/include/catalog/pg_operator.h +++ b/src/include/catalog/pg_operator.h @@ -7,7 +7,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_operator.h,v 1.39 1998/10/03 05:40:55 momjian Exp $ + * $Id: pg_operator.h,v 1.40 1998/10/08 00:19:39 momjian Exp $ * * NOTES * the genbki.sh script reads this file and generates .bki @@ -644,16 +644,16 @@ DATA(insert OID = 1224 ( ">" PGUID 0 b t f 829 829 16 1222 1223 0 0 macaddr DATA(insert OID = 1225 ( ">=" PGUID 0 b t f 829 829 16 1223 1222 0 0 macaddr_ge intltsel intltjoinsel )); /* IP type */ -DATA(insert OID = 1201 ( "=" PGUID 0 b t t 869 869 16 1201 1202 0 0 ipaddr_eq eqsel eqjoinsel )); -DATA(insert OID = 1202 ( "<>" PGUID 0 b t f 869 869 16 1202 1201 0 0 ipaddr_ne neqsel neqjoinsel )); -DATA(insert OID = 1203 ( "<" PGUID 0 b t f 869 869 16 1205 1206 0 0 ipaddr_lt intltsel intltjoinsel )); -DATA(insert OID = 1204 ( "<=" PGUID 0 b t f 869 869 16 1206 1205 0 0 ipaddr_le intltsel intltjoinsel )); -DATA(insert OID = 1205 ( ">" PGUID 0 b t f 869 869 16 1203 1204 0 0 ipaddr_gt intltsel intltjoinsel )); -DATA(insert OID = 1206 ( ">=" PGUID 0 b t f 869 869 16 1204 1203 0 0 ipaddr_ge intltsel intltjoinsel )); -DATA(insert OID = 931 ( "<<" PGUID 0 b t f 869 869 16 933 934 0 0 ipaddr_sub intltsel intltjoinsel )); -DATA(insert OID = 932 ( "<<=" PGUID 0 b t f 869 869 16 934 933 0 0 ipaddr_subeq intltsel intltjoinsel )); -DATA(insert OID = 933 ( ">>" PGUID 0 b t f 869 869 16 931 932 0 0 ipaddr_sup intltsel intltjoinsel )); -DATA(insert OID = 934 ( ">>=" PGUID 0 b t f 869 869 16 932 931 0 0 ipaddr_supeq intltsel intltjoinsel )); +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 )); +DATA(insert OID = 1204 ( "<=" PGUID 0 b t f 869 869 16 1206 1205 0 0 inet_le intltsel intltjoinsel )); +DATA(insert OID = 1205 ( ">" PGUID 0 b t f 869 869 16 1203 1204 0 0 inet_gt intltsel intltjoinsel )); +DATA(insert OID = 1206 ( ">=" PGUID 0 b t f 869 869 16 1204 1203 0 0 inet_ge intltsel intltjoinsel )); +DATA(insert OID = 931 ( "<<" PGUID 0 b t f 869 869 16 933 934 0 0 inet_sub intltsel intltjoinsel )); +DATA(insert OID = 932 ( "<<=" PGUID 0 b t f 869 869 16 934 933 0 0 inet_subeq intltsel intltjoinsel )); +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 )); /* diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 76f92f0e61..969470a253 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_proc.h,v 1.71 1998/10/03 05:40:56 momjian Exp $ + * $Id: pg_proc.h,v 1.72 1998/10/08 00:19:40 momjian Exp $ * * NOTES * The script catalog/genbki.sh reads this file and generates .bki @@ -2069,32 +2069,32 @@ DATA(insert OID = 837 ( macaddr_manuf PGUID 11 f t f 1 f 25 "829" 100 0 0 10 DESCR("MAC manufacturer"); /* for ip type support */ -DATA(insert OID = 910 ( ipaddr_in PGUID 11 f t f 1 f 869 "0" 100 0 0 100 foo bar )); +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 ( ipaddr_out PGUID 11 f t f 1 f 23 "0" 100 0 0 100 foo bar )); +DATA(insert OID = 911 ( inet_out PGUID 11 f t f 1 f 23 "0" 100 0 0 100 foo bar )); DESCR("(internal)"); -DATA(insert OID = 920 ( ipaddr_eq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +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 ( ipaddr_lt PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 921 ( inet_lt PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("less-than"); -DATA(insert OID = 922 ( ipaddr_le PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 922 ( inet_le PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("less-than-or-equal"); -DATA(insert OID = 923 ( ipaddr_gt PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 923 ( inet_gt PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("greater-than"); -DATA(insert OID = 924 ( ipaddr_ge PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 924 ( inet_ge PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("greater-than-or-equal"); -DATA(insert OID = 925 ( ipaddr_ne PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 925 ( inet_ne PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("not equal"); -DATA(insert OID = 926 ( ipaddr_cmp PGUID 11 f t f 2 f 23 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 926 ( inet_cmp PGUID 11 f t f 2 f 23 "869 869" 100 0 0 100 foo bar )); DESCR("less-equal-greater"); -DATA(insert OID = 927 ( ipaddr_sub PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 927 ( inet_sub PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("is-subnet"); -DATA(insert OID = 928 ( ipaddr_subeq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 928 ( inet_subeq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("is-subnet-or-equal"); -DATA(insert OID = 929 ( ipaddr_sup PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +DATA(insert OID = 929 ( inet_sup PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); DESCR("is-supernet"); -DATA(insert OID = 930 ( ipaddr_supeq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar )); +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"); diff --git a/src/include/catalog/pg_type.h b/src/include/catalog/pg_type.h index 3a675c1c33..64118e3e9d 100644 --- a/src/include/catalog/pg_type.h +++ b/src/include/catalog/pg_type.h @@ -7,7 +7,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_type.h,v 1.48 1998/10/03 05:40:58 momjian Exp $ + * $Id: pg_type.h,v 1.49 1998/10/08 00:19:42 momjian Exp $ * * NOTES * the genbki.sh script reads this file and generates .bki @@ -300,7 +300,7 @@ DATA(insert OID = 791 ( _money PGUID -1 -1 f b t \054 0 790 array_in array /* OIDS 800 - 899 */ 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 ( ipaddr PGUID -1 -1 f b t \054 0 0 ipaddr_in ipaddr_out ipaddr_in ipaddr_out i _null_ )); +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"); /* OIDS 900 - 999 */ @@ -339,7 +339,7 @@ DATA(insert OID = 1033 ( aclitem PGUID 8 -1 f b t \054 0 0 aclitemin aclitem 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 ( _ipaddr PGUID -1 -1 f b t \054 0 869 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 = 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 diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index 9b3ca4c627..e02fd31bb6 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: builtins.h,v 1.57 1998/10/04 15:31:07 momjian Exp $ + * $Id: builtins.h,v 1.58 1998/10/08 00:19:43 momjian Exp $ * * NOTES * This should normally only be included by fmgr.h. @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include /* @@ -518,21 +518,20 @@ char *inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size); /* inet_net_pton.c */ int inet_net_pton(int af, const char *src, void *dst, size_t size); -/* ip.c */ -ipaddr *ipaddr_in(char *str); -char *ipaddr_out(ipaddr * addr); -bool ipaddr_lt(ipaddr * a1, ipaddr * a2); -bool ipaddr_le(ipaddr * a1, ipaddr * a2); -bool ipaddr_eq(ipaddr * a1, ipaddr * a2); -bool ipaddr_ge(ipaddr * a1, ipaddr * a2); -bool ipaddr_gt(ipaddr * a1, ipaddr * a2); -bool ipaddr_ne(ipaddr * a1, ipaddr * a2); -bool ipaddr_sub(ipaddr * a1, ipaddr * a2); -bool ipaddr_subeq(ipaddr * a1, ipaddr * a2); -bool ipaddr_sup(ipaddr * a1, ipaddr * a2); -bool ipaddr_supeq(ipaddr * a1, ipaddr * a2); -int4 ipaddr_cmp(ipaddr * a1, ipaddr * a2); -int v4bitncmp(unsigned int a1, unsigned int a2, int bits); +/* inet.c */ +inet *inet_in(char *str); +char *inet_out(inet * addr); +bool inet_lt(inet * a1, inet * a2); +bool inet_le(inet * a1, inet * a2); +bool inet_eq(inet * a1, inet * a2); +bool inet_ge(inet * a1, inet * a2); +bool inet_gt(inet * a1, inet * a2); +bool inet_ne(inet * a1, inet * a2); +bool inet_sub(inet * a1, inet * a2); +bool inet_subeq(inet * a1, inet * a2); +bool inet_sup(inet * a1, inet * a2); +bool inet_supeq(inet * a1, inet * a2); +int4 inet_cmp(inet * a1, inet * a2); /* mac.c */ diff --git a/src/include/utils/inet.h b/src/include/utils/inet.h new file mode 100644 index 0000000000..5b5546ffbf --- /dev/null +++ b/src/include/utils/inet.h @@ -0,0 +1,58 @@ +/*------------------------------------------------------------------------- + * + * builtins.h-- + * Declarations for operations on built-in types. + * + * + * Copyright (c) 1994, Regents of the University of California + * + * $Id: inet.h,v 1.1 1998/10/08 00:19:45 momjian Exp $ + * + *------------------------------------------------------------------------- + */ +#ifndef MAC_H +#define MAC_H + +/* + * This is the internal storage format for IP addresses: + */ + +typedef struct +{ + unsigned char family; + unsigned char bits; + union + { + unsigned int ipv4_addr; /* network byte order */ + /* add IPV6 address type here */ + } addr; +} inet_struct; + +typedef struct varlena inet; + +/* + * This is the internal storage format for MAC addresses: + */ +typedef struct macaddr +{ + unsigned char a; + unsigned char b; + unsigned char c; + unsigned char d; + unsigned char e; + unsigned char f; +} macaddr; + + +typedef struct manufacturer +{ + unsigned char a; + unsigned char b; + unsigned char c; + char *name; +} manufacturer; + +extern manufacturer manufacturers[]; + +#endif /* MAC_H */ + diff --git a/src/include/utils/mac.h b/src/include/utils/mac.h deleted file mode 100644 index df8a676221..0000000000 --- a/src/include/utils/mac.h +++ /dev/null @@ -1,58 +0,0 @@ -/*------------------------------------------------------------------------- - * - * builtins.h-- - * Declarations for operations on built-in types. - * - * - * Copyright (c) 1994, Regents of the University of California - * - * $Id: mac.h,v 1.2 1998/10/04 15:31:09 momjian Exp $ - * - *------------------------------------------------------------------------- - */ -#ifndef MAC_H -#define MAC_H - -/* - * This is the internal storage format for IP addresses: - */ - -typedef struct -{ - unsigned char family; - unsigned char bits; - union - { - unsigned int ipv4_addr; /* network byte order */ - /* add IPV6 address type here */ - } addr; -} ipaddr_struct; - -typedef struct varlena ipaddr; - -/* - * This is the internal storage format for MAC addresses: - */ -typedef struct macaddr -{ - unsigned char a; - unsigned char b; - unsigned char c; - unsigned char d; - unsigned char e; - unsigned char f; -} macaddr; - - -typedef struct manufacturer -{ - unsigned char a; - unsigned char b; - unsigned char c; - char *name; -} manufacturer; - -extern manufacturer manufacturers[]; - -#endif /* MAC_H */ - diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent index 5873ae2702..b5e47b543a 100755 --- a/src/tools/pgindent/pgindent +++ b/src/tools/pgindent/pgindent @@ -786,7 +786,7 @@ do -Tword8 \ -Tyy_size_t \ -Tyy_state_type \ --Tipaddr \ +-Tinet \ -Tmacaddr \ /tmp/$$a >/tmp/$$ 2>&1 if [ "$?" -ne 0 -o -s /tmp/$$ ] -- cgit v1.2.1