Skip to content

Commit e02f818

Browse files
committed
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
1 parent 1a7f4ed commit e02f818

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

doc/src/sgml/func.sgml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.145 2003/03/20 18:58:02 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.146 2003/03/21 21:54:29 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -5899,6 +5899,13 @@ SELECT TIMESTAMP 'now';
58995899
<entry><literal>netmask('192.168.1.5/24')</literal></entry>
59005900
<entry><literal>255.255.255.0</literal></entry>
59015901
</row>
5902+
<row>
5903+
<entry><function>hostmask</function>(<type>inet</type>)</entry>
5904+
<entry><type>inet</type></entry>
5905+
<entry>construct hostmask for network</entry>
5906+
<entry><literal>hostmask('192.168.23.20/30')</literal></entry>
5907+
<entry><literal>0.0.0.3</literal></entry>
5908+
</row>
59025909
<row>
59035910
<entry><literal><function>network</function>(<type>inet</type>)</literal></entry>
59045911
<entry><type>cidr</type></entry>

src/backend/utils/adt/network.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* is for IP V4 CIDR notation, but prepared for V6: just
44
* add the necessary bits where the comments indicate.
55
*
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.38 2002/11/13 00:39:47 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.39 2003/03/21 21:54:29 momjian Exp $
77
*
88
* Jon Postel RIP 16 Oct 1998
99
*/
@@ -605,6 +605,46 @@ network_netmask(PG_FUNCTION_ARGS)
605605
PG_RETURN_INET_P(dst);
606606
}
607607

608+
Datum
609+
network_hostmask(PG_FUNCTION_ARGS)
610+
{
611+
inet *ip = PG_GETARG_INET_P(0);
612+
inet *dst;
613+
614+
dst = (inet *) palloc(VARHDRSZ + sizeof(inet_struct));
615+
/* make sure any unused bits are zeroed */
616+
MemSet(dst, 0, VARHDRSZ + sizeof(inet_struct));
617+
618+
if (ip_family(ip) == AF_INET)
619+
{
620+
/* It's an IP V4 address: */
621+
unsigned long mask = 0xffffffff;
622+
623+
/*
624+
* Only shift if the mask len is < 32 bits ..
625+
*/
626+
627+
if (ip_bits(ip) < 32)
628+
mask >>= ip_bits(ip);
629+
else
630+
mask = 0;
631+
632+
ip_v4addr(dst) = htonl(mask);
633+
634+
ip_bits(dst) = 32;
635+
}
636+
else
637+
/* Go for an IPV6 address here, before faulting out: */
638+
elog(ERROR, "unknown address family (%d)", ip_family(ip));
639+
640+
ip_family(dst) = ip_family(ip);
641+
ip_type(dst) = 0;
642+
VARATT_SIZEP(dst) = VARHDRSZ
643+
+ ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
644+
+ ip_addrsize(dst);
645+
646+
PG_RETURN_INET_P(dst);
647+
}
608648

609649
/*
610650
* Convert a value of a network datatype to an approximate scalar value.

src/include/catalog/pg_proc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.289 2003/03/20 18:58:02 momjian Exp $
10+
* $Id: pg_proc.h,v 1.290 2003/03/21 21:54:29 momjian Exp $
1111
*
1212
* NOTES
1313
* 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
23582358
DESCR("show address octets only");
23592359
DATA(insert OID = 730 ( text PGNSP PGUID 12 f f t f i 1 25 "869" network_show - _null_ ));
23602360
DESCR("show all parts of inet/cidr value");
2361+
DATA(insert OID = 1362 ( hostmask PGNSP PGUID 12 f f t f i 1 869 "869" network_hostmask - _null_ ));
2362+
DESCR("hostmask of address");
23612363
DATA(insert OID = 1713 ( inet PGNSP PGUID 12 f f t f i 1 869 "25" text_inet - _null_ ));
23622364
DESCR("text to inet");
23632365
DATA(insert OID = 1714 ( cidr PGNSP PGUID 12 f f t f i 1 650 "25" text_cidr - _null_ ));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy