PostgreSQL type extensions for IP and MAC addresses.
---------------------------------------------------
-$Id: README.ipaddr,v 1.1 1998/10/03 05:40:41 momjian Exp $
+$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
# 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 $
#
#-------------------------------------------------------------------------
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
/*
- * PostgreSQL type definitions for IP addresses. 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: ip.c,v 1.4 1998/10/04 16:24:30 momjian Exp $
+ * $Id: inet.c,v 1.1 1998/10/08 00:19:35 momjian Exp $
*/
#include <sys/types.h>
#include <postgres.h>
#include <utils/palloc.h>
#include <utils/builtins.h>
-#include <utils/mac.h>
+#include <utils/network.h>
+
+static int v4bitncmp(unsigned int a1, unsigned int a2, int bits);
/*
* Access macros. Add IPV6 support.
*/
-#define ip_addrsize(ipaddrptr) \
- (((ipaddr_struct *)VARDATA(ipaddrptr))->family == AF_INET ? 4 : -1)
+#define ip_addrsize(inetptr) \
+ (((inet_struct *)VARDATA(inetptr))->family == AF_INET ? 4 : -1)
-#define ip_family(ipaddrptr) \
- (((ipaddr_struct *)VARDATA(ipaddrptr))->family)
+#define ip_family(inetptr) \
+ (((inet_struct *)VARDATA(inetptr))->family)
-#define ip_bits(ipaddrptr) \
- (((ipaddr_struct *)VARDATA(ipaddrptr))->bits)
+#define ip_bits(inetptr) \
+ (((inet_struct *)VARDATA(inetptr))->bits)
-#define ip_v4addr(ipaddrptr) \
- (((ipaddr_struct *)VARDATA(ipaddrptr))->addr.ipv4_addr)
+#define ip_v4addr(inetptr) \
+ (((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr)
/*
* IP address reader.
*/
-ipaddr *
-ipaddr_in(char *src)
+inet *
+inet_in(char *src)
{
int bits;
- ipaddr *dst;
+ inet *dst;
- dst = palloc(VARHDRSZ + sizeof(ipaddr_struct));
+ dst = palloc(VARHDRSZ + sizeof(inet_struct));
if (dst == NULL)
{
- elog(ERROR, "unable to allocate memory in ipaddr_in()");
+ elog(ERROR, "unable to allocate memory in inet_in()");
return (NULL);
}
/* First, try for an IP V4 address: */
*/
char *
-ipaddr_out(ipaddr *src)
+inet_out(inet *src)
{
char *dst,
tmp[sizeof("255.255.255.255/32")];
dst = palloc(strlen(tmp) + 1);
if (dst == NULL)
{
- elog(ERROR, "unable to allocate memory in ipaddr_out()");
+ elog(ERROR, "unable to allocate memory in inet_out()");
return (NULL);
}
strcpy(dst, tmp);
*/
bool
-ipaddr_lt(ipaddr *a1, ipaddr *a2)
+inet_lt(inet *a1, inet *a2)
{
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
}
bool
-ipaddr_le(ipaddr *a1, ipaddr *a2)
+inet_le(inet *a1, inet *a2)
{
- return (ipaddr_lt(a1, a2) || ipaddr_eq(a1, a2));
+ return (inet_lt(a1, a2) || inet_eq(a1, a2));
}
bool
-ipaddr_eq(ipaddr *a1, ipaddr *a2)
+inet_eq(inet *a1, inet *a2)
{
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
}
bool
-ipaddr_ge(ipaddr *a1, ipaddr *a2)
+inet_ge(inet *a1, inet *a2)
{
- return (ipaddr_gt(a1, a2) || ipaddr_eq(a1, a2));
+ return (inet_gt(a1, a2) || inet_eq(a1, a2));
}
bool
-ipaddr_gt(ipaddr *a1, ipaddr *a2)
+inet_gt(inet *a1, inet *a2)
{
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
}
bool
-ipaddr_ne(ipaddr *a1, ipaddr *a2)
+inet_ne(inet *a1, inet *a2)
{
- return (!ipaddr_eq(a1, a2));
+ return (!inet_eq(a1, a2));
}
bool
-ipaddr_sub(ipaddr *a1, ipaddr *a2)
+inet_sub(inet *a1, inet *a2)
{
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
}
bool
-ipaddr_subeq(ipaddr *a1, ipaddr *a2)
+inet_subeq(inet *a1, inet *a2)
{
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
}
bool
-ipaddr_sup(ipaddr *a1, ipaddr *a2)
+inet_sup(inet *a1, inet *a2)
{
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
}
bool
-ipaddr_supeq(ipaddr *a1, ipaddr *a2)
+inet_supeq(inet *a1, inet *a2)
{
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
*/
int4
-ipaddr_cmp(ipaddr *a1, ipaddr *a2)
+inet_cmp(inet *a1, inet *a2)
{
if (ntohl(ip_v4addr(a1)) < ntohl(ip_v4addr(a2)))
return (-1);
* Bitwise comparison for V4 addresses. Add V6 implementation!
*/
-int
+static int
v4bitncmp(unsigned int a1, unsigned int a2, int bits)
{
unsigned long mask = 0;
/*
* 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 <stdio.h>
#include <postgres.h>
#include <utils/palloc.h>
#include <utils/builtins.h>
-#include <utils/mac.h>
+#include <utils/network.h>
manufacturer manufacturers[] = {
{0x00, 0x00, 0x0C, "Cisco"},
*
* 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
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 */
*
* 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
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 ));
/*
*
* 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
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");
*
* 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
/* 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 */
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
*
* 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.
#include <utils/nabstime.h>
#include <utils/int8.h>
#include <utils/cash.h>
-#include <utils/mac.h>
+#include <utils/network.h>
#include <utils/rel.h>
/*
/* 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 */
*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: mac.h,v 1.2 1998/10/04 15:31:09 momjian Exp $
+ * $Id: inet.h,v 1.1 1998/10/08 00:19:45 momjian Exp $
*
*-------------------------------------------------------------------------
*/
unsigned int ipv4_addr; /* network byte order */
/* add IPV6 address type here */
} addr;
-} ipaddr_struct;
+} inet_struct;
-typedef struct varlena ipaddr;
+typedef struct varlena inet;
/*
* This is the internal storage format for MAC addresses:
-Tword8 \
-Tyy_size_t \
-Tyy_state_type \
--Tipaddr \
+-Tinet \
-Tmacaddr \
/tmp/$$a >/tmp/$$ 2>&1
if [ "$?" -ne 0 -o -s /tmp/$$ ]