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;
