Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

Re: FW: New function - hostmask


  • From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
  • To: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
  • Cc: greg <greg(dot)wickham(at)grangenet(dot)net>
  • Subject: Re: FW: New function - hostmask
  • Date: Fri, 21 Mar 2003 16:52:40 -0500 (EST)
  • Message-id: <200303212152.h2LLqe615586@candle.pha.pa.us> <text/plain>

I have added this function after confirmation from another user that
this is a function of general value.

I changed the original oid because the original was was already in use.

---------------------------------------------------------------------------

pgman wrote:
> 
> Yes, and I emailed you back asking for the purpose of the function.  I
> looked in the patch and now see:
> 
> +       <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>
> 
> Is this something that is of general value?
> 
> ---------------------------------------------------------------------------
> 
> greg wrote:
> > 
> > Hi Bruce,
> > 
> > Apologies for sending this directly to you but I posted a patch last
> > week to the pgsql-patches mailing list.
> > 
> > I haven't heard anything back .. Not sure if this was overlooked, or was
> > problematic in a way not familiar to me.
> > 
> > Would appreciate a response.
> > 
> > Thanks,
> > 
> >    -Greg
> > 
> > --
> > Dr. Greg Wickham         GrangeNet Network Operations Manager
> > Phone: +61 2 6222 3552                         AARNet Pty Ltd
> > Mob : +61 4 0785 4566                 Australian Academic and
> > Email: greg(dot)wickham(at)grangenet(dot)net            Research Network
> > 
> > -----Original Message-----
> > From: greg [mailto:greg(dot)wickham(at)grangenet(dot)net]
> > Sent: Thursday, February 13, 2003 6:04 PM
> > To: 'pgsql-patches(at)postgresql(dot)org'
> > Subject: New function - hostmask
> > 
> > 
> > 
> > Hi,
> > 
> > Have (by necessity) copied and altered the existing 'netmask' function
> > in postgresql to perform 'hostmask' style duties.
> > 
> > A diff against v7.3.2 is attached.
> > 
> > The patch affects three files:
> > 
> >   postgresql-7.3.2/doc/src/sgml/func.sgml - Updated list of network
> > functions with a simple example
> >   postgresql-7.3.2/src/backend/utils/adt/network.c - The bulk of the new
> > 'hostmask' routine.
> >   postgresql-7.3.2/src/include/catalog/pg_proc.h - Addition of function
> > 'hostmask' into pg_proc
> > 
> > Note that I arbitrarily chose 2311 as the OID (in pg_proc) as it was
> > unused (although not guaranteed to be the lowest unused value).
> > 
> > Would appreciate any feedback on this (first submission to postgresql
> > and all),
> > 

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman(at)candle(dot)pha(dot)pa(dot)us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
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 ----
  	<entry><literal>255.255.255.0</literal></entry>
         </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>
  	<entry><literal><function>network</function>(<type>inet</type>)</literal></entry>
  	<entry><type>cidr</type></entry>
  	<entry>extract network part of address</entry>
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_ ));


Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group