Index: network.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/network.c,v
retrieving revision 1.58
retrieving revision 1.60
diff -c -r1.58 -r1.60
*** network.c	11 Jan 2006 08:43:12 -0000	1.58
--- network.c	23 Jan 2006 21:49:39 -0000	1.60
***************
*** 1,7 ****
  /*
   *	PostgreSQL type definitions for the INET and CIDR types.
   *
!  *	$PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.58 2006/01/11 08:43:12 neilc Exp $
   *
   *	Jon Postel RIP 16 Oct 1998
   */
--- 1,7 ----
  /*
   *	PostgreSQL type definitions for the INET and CIDR types.
   *
!  *	$PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.60 2006/01/23 21:49:39 momjian Exp $
   *
   *	Jon Postel RIP 16 Oct 1998
   */
***************
*** 22,28 ****
  #include "utils/inet.h"
  
  
! static Datum text_network(text *src, int type);
  static int32 network_cmp_internal(inet *a1, inet *a2);
  static int	bitncmp(void *l, void *r, int n);
  static bool addressOK(unsigned char *a, int bits, int family);
--- 22,28 ----
  #include "utils/inet.h"
  
  
! static Datum text_network(text *src, bool is_cidr);
  static int32 network_cmp_internal(inet *a1, inet *a2);
  static int	bitncmp(void *l, void *r, int n);
  static bool addressOK(unsigned char *a, int bits, int family);
***************
*** 38,45 ****
  #define ip_bits(inetptr) \
  	(((inet_struct *)VARDATA(inetptr))->bits)
  
! #define ip_type(inetptr) \
! 	(((inet_struct *)VARDATA(inetptr))->type)
  
  #define ip_addr(inetptr) \
  	(((inet_struct *)VARDATA(inetptr))->ipaddr)
--- 38,45 ----
  #define ip_bits(inetptr) \
  	(((inet_struct *)VARDATA(inetptr))->bits)
  
! #define ip_is_cidr(inetptr) \
! 	(((inet_struct *)VARDATA(inetptr))->is_cidr)
  
  #define ip_addr(inetptr) \
  	(((inet_struct *)VARDATA(inetptr))->ipaddr)
***************
*** 66,72 ****
  
  /* Common input routine */
  static inet *
! network_in(char *src, int type)
  {
  	int			bits;
  	inet	   *dst;
--- 66,72 ----
  
  /* Common input routine */
  static inet *
! network_in(char *src, bool is_cidr)
  {
  	int			bits;
  	inet	   *dst;
***************
*** 85,102 ****
  		ip_family(dst) = PGSQL_AF_INET;
  
  	bits = inet_net_pton(ip_family(dst), src, ip_addr(dst),
! 						 type ? ip_addrsize(dst) : -1);
  	if ((bits < 0) || (bits > ip_maxbits(dst)))
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
  		/* translator: first %s is inet or cidr */
  				 errmsg("invalid input syntax for type %s: \"%s\"",
! 						type ? "cidr" : "inet", src)));
  
  	/*
  	 * Error check: CIDR values must not have any bits set beyond the masklen.
  	 */
! 	if (type)
  	{
  		if (!addressOK(ip_addr(dst), bits, ip_family(dst)))
  			ereport(ERROR,
--- 85,102 ----
  		ip_family(dst) = PGSQL_AF_INET;
  
  	bits = inet_net_pton(ip_family(dst), src, ip_addr(dst),
! 						 is_cidr ? ip_addrsize(dst) : -1);
  	if ((bits < 0) || (bits > ip_maxbits(dst)))
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
  		/* translator: first %s is inet or cidr */
  				 errmsg("invalid input syntax for type %s: \"%s\"",
! 						is_cidr ? "cidr" : "inet", src)));
  
  	/*
  	 * Error check: CIDR values must not have any bits set beyond the masklen.
  	 */
! 	if (is_cidr)
  	{
  		if (!addressOK(ip_addr(dst), bits, ip_family(dst)))
  			ereport(ERROR,
***************
*** 109,115 ****
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
  	ip_bits(dst) = bits;
! 	ip_type(dst) = type;
  
  	return dst;
  }
--- 109,115 ----
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
  	ip_bits(dst) = bits;
! 	ip_is_cidr(dst) = is_cidr;
  
  	return dst;
  }
***************
*** 152,158 ****
  				 errmsg("could not format inet value: %m")));
  
  	/* For CIDR, add /n if not present */
! 	if (ip_type(src) && strchr(tmp, '/') == NULL)
  	{
  		len = strlen(tmp);
  		snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));
--- 152,158 ----
  				 errmsg("could not format inet value: %m")));
  
  	/* For CIDR, add /n if not present */
! 	if (ip_is_cidr(src) && strchr(tmp, '/') == NULL)
  	{
  		len = strlen(tmp);
  		snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));
***************
*** 174,180 ****
   *		inet_recv			- converts external binary format to inet
   *
   * The external representation is (one byte apiece for)
!  * family, bits, type, address length, address in network byte order.
   */
  Datum
  inet_recv(PG_FUNCTION_ARGS)
--- 174,180 ----
   *		inet_recv			- converts external binary format to inet
   *
   * The external representation is (one byte apiece for)
!  * family, bits, is_cidr, address length, address in network byte order.
   */
  Datum
  inet_recv(PG_FUNCTION_ARGS)
***************
*** 201,208 ****
  				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
  				 errmsg("invalid bits in external \"inet\" value")));
  	ip_bits(addr) = bits;
! 	ip_type(addr) = pq_getmsgbyte(buf);
! 	if (ip_type(addr) != 0 && ip_type(addr) != 1)
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
  				 errmsg("invalid type in external \"inet\" value")));
--- 201,208 ----
  				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
  				 errmsg("invalid bits in external \"inet\" value")));
  	ip_bits(addr) = bits;
! 	ip_is_cidr(addr) = pq_getmsgbyte(buf);
! 	if (ip_is_cidr(addr) != false && ip_is_cidr(addr) != true)
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
  				 errmsg("invalid type in external \"inet\" value")));
***************
*** 222,228 ****
  	/*
  	 * Error check: CIDR values must not have any bits set beyond the masklen.
  	 */
! 	if (ip_type(addr))
  	{
  		if (!addressOK(ip_addr(addr), bits, ip_family(addr)))
  			ereport(ERROR,
--- 222,228 ----
  	/*
  	 * Error check: CIDR values must not have any bits set beyond the masklen.
  	 */
! 	if (ip_is_cidr(addr))
  	{
  		if (!addressOK(ip_addr(addr), bits, ip_family(addr)))
  			ereport(ERROR,
***************
*** 256,262 ****
  	pq_begintypsend(&buf);
  	pq_sendbyte(&buf, ip_family(addr));
  	pq_sendbyte(&buf, ip_bits(addr));
! 	pq_sendbyte(&buf, ip_type(addr));
  	nb = ip_addrsize(addr);
  	if (nb < 0)
  		nb = 0;
--- 256,262 ----
  	pq_begintypsend(&buf);
  	pq_sendbyte(&buf, ip_family(addr));
  	pq_sendbyte(&buf, ip_bits(addr));
! 	pq_sendbyte(&buf, ip_is_cidr(addr));
  	nb = ip_addrsize(addr);
  	if (nb < 0)
  		nb = 0;
***************
*** 276,282 ****
  
  
  static Datum
! text_network(text *src, int type)
  {
  	int			len = VARSIZE(src) - VARHDRSZ;
  
--- 276,282 ----
  
  
  static Datum
! text_network(text *src, bool is_cidr)
  {
  	int			len = VARSIZE(src) - VARHDRSZ;
  
***************
*** 285,291 ****
  	memcpy(str, VARDATA(src), len);
  	*(str + len) = '\0';
  
! 	PG_RETURN_INET_P(network_in(str, type));
  }
  
  
--- 285,291 ----
  	memcpy(str, VARDATA(src), len);
  	*(str + len) = '\0';
  
! 	PG_RETURN_INET_P(network_in(str, is_cidr));
  }
  
  
***************
*** 425,432 ****
  /*
   * Support function for hash indexes on inet/cidr.
   *
!  * Since network_cmp considers only ip_family, ip_bits, and ip_addr,
!  * only these fields may be used in the hash; in particular don't use type.
   */
  Datum
  hashinet(PG_FUNCTION_ARGS)
--- 425,432 ----
  /*
   * Support function for hash indexes on inet/cidr.
   *
!  * Since network_cmp considers only ip_family, ip_bits, and ip_addr, only
!  * these fields may be used in the hash; in particular don't use is_cidr.
   */
  Datum
  hashinet(PG_FUNCTION_ARGS)
***************
*** 575,581 ****
  	int			len;
  	char		tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
  
! 	if (ip_type(ip))
  		dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip),
  							 ip_bits(ip), tmp, sizeof(tmp));
  	else
--- 575,581 ----
  	int			len;
  	char		tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
  
! 	if (ip_is_cidr(ip))
  		dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip),
  							 ip_bits(ip), tmp, sizeof(tmp));
  	else
***************
*** 666,672 ****
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_bits(ip);
! 	ip_type(dst) = 0;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
--- 666,672 ----
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_bits(ip);
! 	ip_is_cidr(dst) = false;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
***************
*** 712,718 ****
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_bits(ip);
! 	ip_type(dst) = 1;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
--- 712,718 ----
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_bits(ip);
! 	ip_is_cidr(dst) = true;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
***************
*** 756,762 ****
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_maxbits(ip);
! 	ip_type(dst) = 0;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
--- 756,762 ----
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_maxbits(ip);
! 	ip_is_cidr(dst) = false;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
***************
*** 806,812 ****
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_maxbits(ip);
! 	ip_type(dst) = 0;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
--- 806,812 ----
  
  	ip_family(dst) = ip_family(ip);
  	ip_bits(dst) = ip_maxbits(ip);
! 	ip_is_cidr(dst) = false;
  	VARATT_SIZEP(dst) = VARHDRSZ
  		+ ((char *) ip_addr(dst) - (char *) VARDATA(dst))
  		+ ip_addrsize(dst);
Index: inet.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/utils/inet.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -c -r1.20 -r1.21
*** inet.h	31 Dec 2004 22:03:46 -0000	1.20
--- inet.h	23 Jan 2006 21:45:47 -0000	1.21
***************
*** 7,13 ****
   * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
   * Portions Copyright (c) 1994, Regents of the University of California
   *
!  * $PostgreSQL: pgsql/src/include/utils/inet.h,v 1.20 2004/12/31 22:03:46 pgsql Exp $
   *
   *-------------------------------------------------------------------------
   */
--- 7,13 ----
   * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
   * Portions Copyright (c) 1994, Regents of the University of California
   *
!  * $PostgreSQL: pgsql/src/include/utils/inet.h,v 1.21 2006/01/23 21:45:47 momjian Exp $
   *
   *-------------------------------------------------------------------------
   */
***************
*** 22,28 ****
  {
  	unsigned char family;		/* PGSQL_AF_INET or PGSQL_AF_INET6 */
  	unsigned char bits;			/* number of bits in netmask */
! 	unsigned char type;			/* 0 = inet, 1 = cidr */
  	unsigned char ipaddr[16];	/* up to 128 bits of address */
  } inet_struct;
  
--- 22,28 ----
  {
  	unsigned char family;		/* PGSQL_AF_INET or PGSQL_AF_INET6 */
  	unsigned char bits;			/* number of bits in netmask */
! 	bool is_cidr;				/* is cidr? */
  	unsigned char ipaddr[16];	/* up to 128 bits of address */
  } inet_struct;
  
