Index: doc/src/sgml/func.sgml =================================================================== RCS file: /cvsroot/pgsql-server/doc/src/sgml/func.sgml,v retrieving revision 1.145 diff -c -c -r1.145 func.sgml *** doc/src/sgml/func.sgml 20 Mar 2003 18:58:02 -0000 1.145 --- doc/src/sgml/func.sgml 21 Mar 2003 21:49:26 -0000 *************** *** 5900,5905 **** --- 5900,5912 ---- 255.255.255.0 + hostmask(inet) + inet + construct hostmask for network + hostmask('192.168.23.20/30') + 0.0.0.3 + + network(inet) cidr extract network part of address Index: src/backend/utils/adt/network.c =================================================================== RCS file: /cvsroot/pgsql-server/src/backend/utils/adt/network.c,v retrieving revision 1.38 diff -c -c -r1.38 network.c *** src/backend/utils/adt/network.c 13 Nov 2002 00:39:47 -0000 1.38 --- src/backend/utils/adt/network.c 21 Mar 2003 21:49:34 -0000 *************** *** 605,610 **** --- 605,650 ---- PG_RETURN_INET_P(dst); } + Datum + network_hostmask(PG_FUNCTION_ARGS) + { + inet *ip = PG_GETARG_INET_P(0); + inet *dst; + + dst = (inet *) palloc(VARHDRSZ + sizeof(inet_struct)); + /* make sure any unused bits are zeroed */ + MemSet(dst, 0, VARHDRSZ + sizeof(inet_struct)); + + if (ip_family(ip) == AF_INET) + { + /* It's an IP V4 address: */ + unsigned long mask = 0xffffffff; + + /* + * Only shift if the mask len is < 32 bits .. + */ + + if (ip_bits(ip) < 32) + mask >>= ip_bits(ip); + else + mask = 0; + + ip_v4addr(dst) = htonl(mask); + + ip_bits(dst) = 32; + } + else + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(ip)); + + ip_family(dst) = ip_family(ip); + ip_type(dst) = 0; + VARATT_SIZEP(dst) = VARHDRSZ + + ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst)) + + ip_addrsize(dst); + + PG_RETURN_INET_P(dst); + } /* * Convert a value of a network datatype to an approximate scalar value. Index: src/include/catalog/pg_proc.h =================================================================== RCS file: /cvsroot/pgsql-server/src/include/catalog/pg_proc.h,v retrieving revision 1.289 diff -c -c -r1.289 pg_proc.h *** src/include/catalog/pg_proc.h 20 Mar 2003 18:58:02 -0000 1.289 --- src/include/catalog/pg_proc.h 21 Mar 2003 21:49:42 -0000 *************** *** 2358,2363 **** --- 2358,2365 ---- DESCR("show address octets only"); DATA(insert OID = 730 ( text PGNSP PGUID 12 f f t f i 1 25 "869" network_show - _null_ )); DESCR("show all parts of inet/cidr value"); + DATA(insert OID = 1362 ( hostmask PGNSP PGUID 12 f f t f i 1 869 "869" network_hostmask - _null_ )); + DESCR("hostmask of address"); DATA(insert OID = 1713 ( inet PGNSP PGUID 12 f f t f i 1 869 "25" text_inet - _null_ )); DESCR("text to inet"); DATA(insert OID = 1714 ( cidr PGNSP PGUID 12 f f t f i 1 650 "25" text_cidr - _null_ ));