[PATCH] addition of text_inet, text_cidr and inet_set_masklen
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidr
set_masklen(inet): set masklen on the inet value
Patch also contains regression checks for these functions.
-alex
Attachments:
pg-network.patchtext/plain; charset=US-ASCII; name=pg-network.patchDownload
Index: backend/utils/adt/network.c
===================================================================
RCS file: /cvs/pgsql/pgsql/src/backend/utils/adt/network.c,v
retrieving revision 1.29
diff --unified -r1.29 network.c
--- backend/utils/adt/network.c 2001/03/22 03:59:52 1.29
+++ backend/utils/adt/network.c 2001/06/12 01:10:10
@@ -20,6 +20,7 @@
#include "utils/inet.h"
+static Datum text_network(text *src, int type);
static int32 network_cmp_internal(inet *a1, inet *a2);
static int v4bitncmp(unsigned long a1, unsigned long a2, int bits);
static bool v4addressOK(unsigned long a1, int bits);
@@ -147,6 +148,51 @@
return inet_out(fcinfo);
}
+
+Datum
+text_network(text *src, int type)
+{
+ int len = VARSIZE(src) - VARHDRSZ;
+
+ char *str = palloc(len + 1);
+ memcpy(str, VARDATA(src), len);
+ *(str + len) = '\0';
+
+ PG_RETURN_INET_P(network_in( str, type));
+}
+
+Datum
+text_cidr(PG_FUNCTION_ARGS)
+{
+ return text_network( PG_GETARG_TEXT_P(0), 1);
+}
+
+Datum
+text_inet(PG_FUNCTION_ARGS)
+{
+ return text_network( PG_GETARG_TEXT_P(0), 0);
+}
+
+Datum
+inet_set_masklen(PG_FUNCTION_ARGS)
+{
+ inet *src = PG_GETARG_INET_P(0);
+ int bits = PG_GETARG_INT32(1);
+ inet *dst;
+
+ if ((bits < 0) || (bits > 32)) /* no support for v6 yet */
+ {
+ elog(ERROR, "set_masklen - invalid value '%d'", bits);
+ }
+
+ /* clone the original data */
+ dst = (inet *) palloc(VARHDRSZ + sizeof(inet_struct));
+ memcpy(dst, src, VARHDRSZ + sizeof(inet_struct));
+
+ ip_bits(dst) = bits;
+
+ PG_RETURN_INET_P(dst);
+}
/*
* Basic comparison function for sorting and inet/cidr comparisons.
Index: include/catalog/pg_proc.h
===================================================================
RCS file: /cvs/pgsql/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.184
diff --unified -r1.184 pg_proc.h
--- include/catalog/pg_proc.h 2001/03/22 04:00:39 1.184
+++ include/catalog/pg_proc.h 2001/06/12 01:10:18
@@ -2308,6 +2308,12 @@
DESCR("show address octets only");
DATA(insert OID = 730 ( text PGUID 12 f t t t 1 f 25 "869" 100 0 0 100 network_show - ));
DESCR("show all parts of inet/cidr value");
+DATA(insert OID = 1910 ( inet PGUID 12 f t t t 1 f 869 "25" 100 0 0 100 text_inet - ));
+DESCR("text to inet");
+DATA(insert OID = 1911 ( cidr PGUID 12 f t t t 1 f 650 "25" 100 0 0 100 text_cidr - ));
+DESCR("text to cidr");
+DATA(insert OID = 1912 ( set_masklen PGUID 12 f t t t 2 f 869 "869 23" 100 0 0 100 inet_set_masklen - ));
+DESCR("change the netmask of an inet");
DATA(insert OID = 1691 ( boolle PGUID 12 f t t t 2 f 16 "16 16" 100 0 0 100 boolle - ));
DESCR("less-than-or-equal");
Index: include/utils/builtins.h
===================================================================
RCS file: /cvs/pgsql/pgsql/src/include/utils/builtins.h,v
retrieving revision 1.148
diff --unified -r1.148 builtins.h
--- include/utils/builtins.h 2001/03/22 04:01:11 1.148
+++ include/utils/builtins.h 2001/06/12 01:10:20
@@ -510,6 +510,9 @@
extern Datum network_host(PG_FUNCTION_ARGS);
extern Datum network_show(PG_FUNCTION_ARGS);
extern Datum network_abbrev(PG_FUNCTION_ARGS);
+extern Datum text_cidr(PG_FUNCTION_ARGS);
+extern Datum text_inet(PG_FUNCTION_ARGS);
+extern Datum inet_set_masklen(PG_FUNCTION_ARGS);
/* mac.c */
extern Datum macaddr_in(PG_FUNCTION_ARGS);
Index: test/regress/expected/inet.out
===================================================================
RCS file: /cvs/pgsql/pgsql/src/test/regress/expected/inet.out,v
retrieving revision 1.11
diff --unified -r1.11 inet.out
--- test/regress/expected/inet.out 2000/12/22 18:00:21 1.11
+++ test/regress/expected/inet.out 2001/06/12 01:10:29
@@ -18,6 +18,9 @@
-- check that CIDR rejects invalid input:
INSERT INTO INET_TBL (c, i) VALUES ('192.168.1.2/24', '192.168.1.226');
ERROR: invalid CIDR value '192.168.1.2/24': has bits set to right of mask
+-- 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');
+ERROR: invalid CIDR value '192.168.1.2/24': has bits set to right of mask
SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL;
ten | cidr | inet
-----+----------------+------------------
@@ -133,5 +136,21 @@
| 10.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | t | f | t
| 11.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | f | f | f
| 9.1.2.3/8 | 10.0.0.0/8 | t | t | f | f | f | t | f | f | f | f
+(10 rows)
+
+-- check the conversion to/from text and set_netmask
+select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
+ ten | set_masklen
+-----+------------------
+ | 192.168.1.226/24
+ | 192.168.1.226/24
+ | 10.1.2.3/24
+ | 10.1.2.3/24
+ | 10.1.2.3/24
+ | 10.1.2.3/24
+ | 10.1.2.3/24
+ | 10.1.2.3/24
+ | 11.1.2.3/24
+ | 9.1.2.3/24
(10 rows)
Index: test/regress/sql/inet.sql
===================================================================
RCS file: /cvs/pgsql/pgsql/src/test/regress/sql/inet.sql,v
retrieving revision 1.5
diff --unified -r1.5 inet.sql
--- test/regress/sql/inet.sql 2000/11/10 20:13:27 1.5
+++ test/regress/sql/inet.sql 2001/06/12 01:10:29
@@ -18,6 +18,8 @@
INSERT INTO INET_TBL (c, i) VALUES ('10', '9.1.2.3/8');
-- check that CIDR rejects invalid input:
INSERT INTO INET_TBL (c, i) VALUES ('192.168.1.2/24', '192.168.1.226');
+-- 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');
SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL;
@@ -45,3 +47,5 @@
i >> c AS sup, i >>= c AS spe
FROM INET_TBL;
+-- check the conversion to/from text and set_netmask
+select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
Your patch has been added to the PostgreSQL unapplied patches list at:
http://candle.pha.pa.us/cgi-bin/pgpatches
I will try to apply it within the next 48 hours.
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidr
set_masklen(inet): set masklen on the inet valuePatch also contains regression checks for these functions.
-alex
Content-Description: pg-network.patch
[ Attachment, skipping... ]
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Thanks. Patch applied. This knocks out another TODO item.
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidr
set_masklen(inet): set masklen on the inet valuePatch also contains regression checks for these functions.
-alex
Content-Description: pg-network.patch
[ Attachment, skipping... ]
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidr
set_masklen(inet): set masklen on the inet valuePatch also contains regression checks for these functions.
How about some documentation updates to go with that?
regards, tom lane
My bad.
Attached.
-alex
On Wed, 13 Jun 2001, Tom Lane wrote:
Show quoted text
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidr
set_masklen(inet): set masklen on the inet valuePatch also contains regression checks for these functions.
How about some documentation updates to go with that?
regards, tom lane
Attachments:
pg-network-doc.patchtext/plain; charset=US-ASCII; name=pg-network-doc.patchDownload
Index: func.sgml
===================================================================
RCS file: /cvs/pgsql/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.56
diff --unified -r1.56 func.sgml
--- func.sgml 2001/03/25 18:14:31 1.56
+++ func.sgml 2001/06/13 22:04:44
@@ -3214,6 +3214,13 @@
<entry>24</entry>
</row>
<row>
+ <entry>set_masklen(inet,integer)</entry>
+ <entry>inet</entry>
+ <entry>set netmask length for inet value</entry>
+ <entry>set_masklen('192.168.1.5/24',16)</entry>
+ <entry>192.168.1.5/16</entry>
+ </row>
+ <row>
<entry>netmask(inet)</entry>
<entry>inet</entry>
<entry>construct netmask for network</entry>
@@ -3249,7 +3256,9 @@
All of the functions for <type>inet</type> can be applied to
<type>cidr</type> values as well. The <function>host</>(),
<function>text</>(), and <function>abbrev</>() functions are primarily
- intended to offer alternative display formats.
+ intended to offer alternative display formats. You can cast a text
+ field to inet using normal casting syntax: inet(fieldname) or
+ fieldname::inet.
</para>
<para>
Patch applied.
My bad.
Attached.
-alex
On Wed, 13 Jun 2001, Tom Lane wrote:
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidr
set_masklen(inet): set masklen on the inet valuePatch also contains regression checks for these functions.
How about some documentation updates to go with that?
regards, tom lane
Content-Description:
[ Attachment, skipping... ]
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026