11/*
22 *PostgreSQL type definitions for the INET and CIDR types.
33 *
4- *$PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.58 2006/01/11 08:43:12 neilc Exp $
4+ *$PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.59 2006/01/23 21:45:47 momjian Exp $
55 *
66 *Jon Postel RIP 16 Oct 1998
77 */
2222#include "utils/inet.h"
2323
2424
25- static Datum text_network (text * src ,int type );
25+ static Datum text_network (text * src ,int is_cidr );
2626static int32 network_cmp_internal (inet * a1 ,inet * a2 );
2727static int bitncmp (void * l ,void * r ,int n );
2828static bool addressOK (unsignedchar * a ,int bits ,int family );
@@ -38,8 +38,8 @@ static intip_addrsize(inet *inetptr);
3838#define ip_bits (inetptr ) \
3939(((inet_struct *)VARDATA(inetptr))->bits)
4040
41- #define ip_type (inetptr ) \
42- (((inet_struct *)VARDATA(inetptr))->type )
41+ #define ip_is_cidr (inetptr ) \
42+ (((inet_struct *)VARDATA(inetptr))->is_cidr )
4343
4444#define ip_addr (inetptr ) \
4545(((inet_struct *)VARDATA(inetptr))->ipaddr)
@@ -66,7 +66,7 @@ ip_addrsize(inet *inetptr)
6666
6767/* Common input routine */
6868static inet *
69- network_in (char * src ,int type )
69+ network_in (char * src ,bool is_cidr )
7070{
7171int bits ;
7272inet * dst ;
@@ -85,18 +85,18 @@ network_in(char *src, int type)
8585ip_family (dst )= PGSQL_AF_INET ;
8686
8787bits = inet_net_pton (ip_family (dst ),src ,ip_addr (dst ),
88- type ?ip_addrsize (dst ) :-1 );
88+ is_cidr ?ip_addrsize (dst ) :-1 );
8989if ((bits < 0 )|| (bits > ip_maxbits (dst )))
9090ereport (ERROR ,
9191(errcode (ERRCODE_INVALID_TEXT_REPRESENTATION ),
9292/* translator: first %s is inet or cidr */
9393errmsg ("invalid input syntax for type %s: \"%s\"" ,
94- type ?"cidr" :"inet" ,src )));
94+ is_cidr ?"cidr" :"inet" ,src )));
9595
9696/*
9797 * Error check: CIDR values must not have any bits set beyond the masklen.
9898 */
99- if (type )
99+ if (is_cidr )
100100{
101101if (!addressOK (ip_addr (dst ),bits ,ip_family (dst )))
102102ereport (ERROR ,
@@ -109,7 +109,7 @@ network_in(char *src, int type)
109109+ ((char * )ip_addr (dst )- (char * )VARDATA (dst ))
110110+ ip_addrsize (dst );
111111ip_bits (dst )= bits ;
112- ip_type (dst )= type ;
112+ ip_is_cidr (dst )= is_cidr ;
113113
114114return dst ;
115115}
@@ -152,7 +152,7 @@ inet_out(PG_FUNCTION_ARGS)
152152errmsg ("could not format inet value: %m" )));
153153
154154/* For CIDR, add /n if not present */
155- if (ip_type (src )&& strchr (tmp ,'/' )== NULL )
155+ if (ip_is_cidr (src )&& strchr (tmp ,'/' )== NULL )
156156{
157157len = strlen (tmp );
158158snprintf (tmp + len ,sizeof (tmp )- len ,"/%u" ,ip_bits (src ));
@@ -174,7 +174,7 @@ cidr_out(PG_FUNCTION_ARGS)
174174 *inet_recv- converts external binary format to inet
175175 *
176176 * The external representation is (one byte apiece for)
177- * family, bits,type , address length, address in network byte order.
177+ * family, bits,is_cidr , address length, address in network byte order.
178178 */
179179Datum
180180inet_recv (PG_FUNCTION_ARGS )
@@ -201,8 +201,8 @@ inet_recv(PG_FUNCTION_ARGS)
201201(errcode (ERRCODE_INVALID_BINARY_REPRESENTATION ),
202202errmsg ("invalid bits in external \"inet\" value" )));
203203ip_bits (addr )= bits ;
204- ip_type (addr )= pq_getmsgbyte (buf );
205- if (ip_type (addr )!= 0 && ip_type (addr )!= 1 )
204+ ip_is_cidr (addr )= pq_getmsgbyte (buf );
205+ if (ip_is_cidr (addr )!= false && ip_is_cidr (addr )!= true )
206206ereport (ERROR ,
207207(errcode (ERRCODE_INVALID_BINARY_REPRESENTATION ),
208208errmsg ("invalid type in external \"inet\" value" )));
@@ -222,7 +222,7 @@ inet_recv(PG_FUNCTION_ARGS)
222222/*
223223 * Error check: CIDR values must not have any bits set beyond the masklen.
224224 */
225- if (ip_type (addr ))
225+ if (ip_is_cidr (addr ))
226226{
227227if (!addressOK (ip_addr (addr ),bits ,ip_family (addr )))
228228ereport (ERROR ,
@@ -256,7 +256,7 @@ inet_send(PG_FUNCTION_ARGS)
256256pq_begintypsend (& buf );
257257pq_sendbyte (& buf ,ip_family (addr ));
258258pq_sendbyte (& buf ,ip_bits (addr ));
259- pq_sendbyte (& buf ,ip_type (addr ));
259+ pq_sendbyte (& buf ,ip_is_cidr (addr ));
260260nb = ip_addrsize (addr );
261261if (nb < 0 )
262262nb = 0 ;
@@ -276,7 +276,7 @@ cidr_send(PG_FUNCTION_ARGS)
276276
277277
278278static Datum
279- text_network (text * src ,int type )
279+ text_network (text * src ,bool is_cidr )
280280{
281281int len = VARSIZE (src )- VARHDRSZ ;
282282
@@ -285,7 +285,7 @@ text_network(text *src, int type)
285285memcpy (str ,VARDATA (src ),len );
286286* (str + len )= '\0' ;
287287
288- PG_RETURN_INET_P (network_in (str ,type ));
288+ PG_RETURN_INET_P (network_in (str ,is_cidr ));
289289}
290290
291291
@@ -425,8 +425,8 @@ network_ne(PG_FUNCTION_ARGS)
425425/*
426426 * Support function for hash indexes on inet/cidr.
427427 *
428- * Since network_cmp considers only ip_family, ip_bits, and ip_addr,
429- *only these fields may be used in the hash; in particular don't usetype .
428+ * Since network_cmp considers only ip_family, ip_bits, and ip_addr, only
429+ * these fields may be used in the hash; in particular don't useis_cidr .
430430 */
431431Datum
432432hashinet (PG_FUNCTION_ARGS )
@@ -575,7 +575,7 @@ network_abbrev(PG_FUNCTION_ARGS)
575575int len ;
576576char tmp [sizeof ("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128" )];
577577
578- if (ip_type (ip ))
578+ if (ip_is_cidr (ip ))
579579dst = inet_cidr_ntop (ip_family (ip ),ip_addr (ip ),
580580ip_bits (ip ),tmp ,sizeof (tmp ));
581581else
@@ -666,7 +666,7 @@ network_broadcast(PG_FUNCTION_ARGS)
666666
667667ip_family (dst )= ip_family (ip );
668668ip_bits (dst )= ip_bits (ip );
669- ip_type (dst )= 0 ;
669+ ip_is_cidr (dst )= false ;
670670VARATT_SIZEP (dst )= VARHDRSZ
671671+ ((char * )ip_addr (dst )- (char * )VARDATA (dst ))
672672+ ip_addrsize (dst );
@@ -712,7 +712,7 @@ network_network(PG_FUNCTION_ARGS)
712712
713713ip_family (dst )= ip_family (ip );
714714ip_bits (dst )= ip_bits (ip );
715- ip_type (dst )= 1 ;
715+ ip_is_cidr (dst )= true ;
716716VARATT_SIZEP (dst )= VARHDRSZ
717717+ ((char * )ip_addr (dst )- (char * )VARDATA (dst ))
718718+ ip_addrsize (dst );
@@ -756,7 +756,7 @@ network_netmask(PG_FUNCTION_ARGS)
756756
757757ip_family (dst )= ip_family (ip );
758758ip_bits (dst )= ip_maxbits (ip );
759- ip_type (dst )= 0 ;
759+ ip_is_cidr (dst )= false ;
760760VARATT_SIZEP (dst )= VARHDRSZ
761761+ ((char * )ip_addr (dst )- (char * )VARDATA (dst ))
762762+ ip_addrsize (dst );
@@ -806,7 +806,7 @@ network_hostmask(PG_FUNCTION_ARGS)
806806
807807ip_family (dst )= ip_family (ip );
808808ip_bits (dst )= ip_maxbits (ip );
809- ip_type (dst )= 0 ;
809+ ip_is_cidr (dst )= false ;
810810VARATT_SIZEP (dst )= VARHDRSZ
811811+ ((char * )ip_addr (dst )- (char * )VARDATA (dst ))
812812+ ip_addrsize (dst );