Add hostmask() function:

+       <entry><function>hostmask</function>(<type>inet</type>)</entry>
+       <entry><type>inet</type></entry>
+       <entry>construct hostmask for network</entry>
+       <entry><literal>hostmask('192.168.23.20/30')</literal></entry>
+       <entry><literal>0.0.0.3</literal></entry>

Greg Wickham
This commit is contained in:
Bruce Momjian 2003-03-21 21:54:29 +00:00
parent 1a7f4ed525
commit e02f818311
3 changed files with 52 additions and 3 deletions

View File

@ -1,5 +1,5 @@
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.145 2003/03/20 18:58:02 momjian Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.146 2003/03/21 21:54:29 momjian Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
@ -5899,6 +5899,13 @@ SELECT TIMESTAMP 'now';
<entry><literal>netmask('192.168.1.5/24')</literal></entry> <entry><literal>netmask('192.168.1.5/24')</literal></entry>
<entry><literal>255.255.255.0</literal></entry> <entry><literal>255.255.255.0</literal></entry>
</row> </row>
<row>
<entry><function>hostmask</function>(<type>inet</type>)</entry>
<entry><type>inet</type></entry>
<entry>construct hostmask for network</entry>
<entry><literal>hostmask('192.168.23.20/30')</literal></entry>
<entry><literal>0.0.0.3</literal></entry>
</row>
<row> <row>
<entry><literal><function>network</function>(<type>inet</type>)</literal></entry> <entry><literal><function>network</function>(<type>inet</type>)</literal></entry>
<entry><type>cidr</type></entry> <entry><type>cidr</type></entry>

View File

@ -3,7 +3,7 @@
* is for IP V4 CIDR notation, but prepared for V6: just * is for IP V4 CIDR notation, but prepared for V6: just
* add the necessary bits where the comments indicate. * add the necessary bits where the comments indicate.
* *
* $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.38 2002/11/13 00:39:47 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.39 2003/03/21 21:54:29 momjian Exp $
* *
* Jon Postel RIP 16 Oct 1998 * Jon Postel RIP 16 Oct 1998
*/ */
@ -605,6 +605,46 @@ network_netmask(PG_FUNCTION_ARGS)
PG_RETURN_INET_P(dst); 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. * Convert a value of a network datatype to an approximate scalar value.

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_proc.h,v 1.289 2003/03/20 18:58:02 momjian Exp $ * $Id: pg_proc.h,v 1.290 2003/03/21 21:54:29 momjian Exp $
* *
* NOTES * NOTES
* The script catalog/genbki.sh reads this file and generates .bki * The script catalog/genbki.sh reads this file and generates .bki
@ -2358,6 +2358,8 @@ DATA(insert OID = 699 ( host PGNSP PGUID 12 f f t f i 1 25 "869" network_ho
DESCR("show address octets only"); DESCR("show address octets only");
DATA(insert OID = 730 ( text PGNSP PGUID 12 f f t f i 1 25 "869" network_show - _null_ )); 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"); 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_ )); DATA(insert OID = 1713 ( inet PGNSP PGUID 12 f f t f i 1 869 "25" text_inet - _null_ ));
DESCR("text to inet"); DESCR("text to inet");
DATA(insert OID = 1714 ( cidr PGNSP PGUID 12 f f t f i 1 650 "25" text_cidr - _null_ )); DATA(insert OID = 1714 ( cidr PGNSP PGUID 12 f f t f i 1 650 "25" text_cidr - _null_ ));