1
1
/*
2
2
* PostgreSQL type definitions for the INET and CIDR types.
3
3
*
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 $
5
5
*
6
6
* Jon Postel RIP 16 Oct 1998
7
7
*/
22
22
#include "utils/inet.h"
23
23
24
24
25
- static Datum text_network (text * src , int type );
25
+ static Datum text_network (text * src , int is_cidr );
26
26
static int32 network_cmp_internal (inet * a1 , inet * a2 );
27
27
static int bitncmp (void * l , void * r , int n );
28
28
static bool addressOK (unsigned char * a , int bits , int family );
@@ -38,8 +38,8 @@ static int ip_addrsize(inet *inetptr);
38
38
#define ip_bits (inetptr ) \
39
39
(((inet_struct *)VARDATA(inetptr))->bits)
40
40
41
- #define ip_type (inetptr ) \
42
- (((inet_struct *)VARDATA(inetptr))->type )
41
+ #define ip_is_cidr (inetptr ) \
42
+ (((inet_struct *)VARDATA(inetptr))->is_cidr )
43
43
44
44
#define ip_addr (inetptr ) \
45
45
(((inet_struct *)VARDATA(inetptr))->ipaddr)
@@ -66,7 +66,7 @@ ip_addrsize(inet *inetptr)
66
66
67
67
/* Common input routine */
68
68
static inet *
69
- network_in (char * src , int type )
69
+ network_in (char * src , bool is_cidr )
70
70
{
71
71
int bits ;
72
72
inet * dst ;
@@ -85,18 +85,18 @@ network_in(char *src, int type)
85
85
ip_family (dst ) = PGSQL_AF_INET ;
86
86
87
87
bits = inet_net_pton (ip_family (dst ), src , ip_addr (dst ),
88
- type ? ip_addrsize (dst ) : -1 );
88
+ is_cidr ? ip_addrsize (dst ) : -1 );
89
89
if ((bits < 0 ) || (bits > ip_maxbits (dst )))
90
90
ereport (ERROR ,
91
91
(errcode (ERRCODE_INVALID_TEXT_REPRESENTATION ),
92
92
/* translator: first %s is inet or cidr */
93
93
errmsg ("invalid input syntax for type %s: \"%s\"" ,
94
- type ? "cidr" : "inet" , src )));
94
+ is_cidr ? "cidr" : "inet" , src )));
95
95
96
96
/*
97
97
* Error check: CIDR values must not have any bits set beyond the masklen.
98
98
*/
99
- if (type )
99
+ if (is_cidr )
100
100
{
101
101
if (!addressOK (ip_addr (dst ), bits , ip_family (dst )))
102
102
ereport (ERROR ,
@@ -109,7 +109,7 @@ network_in(char *src, int type)
109
109
+ ((char * ) ip_addr (dst ) - (char * ) VARDATA (dst ))
110
110
+ ip_addrsize (dst );
111
111
ip_bits (dst ) = bits ;
112
- ip_type (dst ) = type ;
112
+ ip_is_cidr (dst ) = is_cidr ;
113
113
114
114
return dst ;
115
115
}
@@ -152,7 +152,7 @@ inet_out(PG_FUNCTION_ARGS)
152
152
errmsg ("could not format inet value: %m" )));
153
153
154
154
/* For CIDR, add /n if not present */
155
- if (ip_type (src ) && strchr (tmp , '/' ) == NULL )
155
+ if (ip_is_cidr (src ) && strchr (tmp , '/' ) == NULL )
156
156
{
157
157
len = strlen (tmp );
158
158
snprintf (tmp + len , sizeof (tmp ) - len , "/%u" , ip_bits (src ));
@@ -174,7 +174,7 @@ cidr_out(PG_FUNCTION_ARGS)
174
174
* inet_recv - converts external binary format to inet
175
175
*
176
176
* 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.
178
178
*/
179
179
Datum
180
180
inet_recv (PG_FUNCTION_ARGS )
@@ -201,8 +201,8 @@ inet_recv(PG_FUNCTION_ARGS)
201
201
(errcode (ERRCODE_INVALID_BINARY_REPRESENTATION ),
202
202
errmsg ("invalid bits in external \"inet\" value" )));
203
203
ip_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 )
206
206
ereport (ERROR ,
207
207
(errcode (ERRCODE_INVALID_BINARY_REPRESENTATION ),
208
208
errmsg ("invalid type in external \"inet\" value" )));
@@ -222,7 +222,7 @@ inet_recv(PG_FUNCTION_ARGS)
222
222
/*
223
223
* Error check: CIDR values must not have any bits set beyond the masklen.
224
224
*/
225
- if (ip_type (addr ))
225
+ if (ip_is_cidr (addr ))
226
226
{
227
227
if (!addressOK (ip_addr (addr ), bits , ip_family (addr )))
228
228
ereport (ERROR ,
@@ -256,7 +256,7 @@ inet_send(PG_FUNCTION_ARGS)
256
256
pq_begintypsend (& buf );
257
257
pq_sendbyte (& buf , ip_family (addr ));
258
258
pq_sendbyte (& buf , ip_bits (addr ));
259
- pq_sendbyte (& buf , ip_type (addr ));
259
+ pq_sendbyte (& buf , ip_is_cidr (addr ));
260
260
nb = ip_addrsize (addr );
261
261
if (nb < 0 )
262
262
nb = 0 ;
@@ -276,7 +276,7 @@ cidr_send(PG_FUNCTION_ARGS)
276
276
277
277
278
278
static Datum
279
- text_network (text * src , int type )
279
+ text_network (text * src , bool is_cidr )
280
280
{
281
281
int len = VARSIZE (src ) - VARHDRSZ ;
282
282
@@ -285,7 +285,7 @@ text_network(text *src, int type)
285
285
memcpy (str , VARDATA (src ), len );
286
286
* (str + len ) = '\0' ;
287
287
288
- PG_RETURN_INET_P (network_in (str , type ));
288
+ PG_RETURN_INET_P (network_in (str , is_cidr ));
289
289
}
290
290
291
291
@@ -425,8 +425,8 @@ network_ne(PG_FUNCTION_ARGS)
425
425
/*
426
426
* Support function for hash indexes on inet/cidr.
427
427
*
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 use type .
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 use is_cidr .
430
430
*/
431
431
Datum
432
432
hashinet (PG_FUNCTION_ARGS )
@@ -575,7 +575,7 @@ network_abbrev(PG_FUNCTION_ARGS)
575
575
int len ;
576
576
char tmp [sizeof ("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128" )];
577
577
578
- if (ip_type (ip ))
578
+ if (ip_is_cidr (ip ))
579
579
dst = inet_cidr_ntop (ip_family (ip ), ip_addr (ip ),
580
580
ip_bits (ip ), tmp , sizeof (tmp ));
581
581
else
@@ -666,7 +666,7 @@ network_broadcast(PG_FUNCTION_ARGS)
666
666
667
667
ip_family (dst ) = ip_family (ip );
668
668
ip_bits (dst ) = ip_bits (ip );
669
- ip_type (dst ) = 0 ;
669
+ ip_is_cidr (dst ) = false ;
670
670
VARATT_SIZEP (dst ) = VARHDRSZ
671
671
+ ((char * ) ip_addr (dst ) - (char * ) VARDATA (dst ))
672
672
+ ip_addrsize (dst );
@@ -712,7 +712,7 @@ network_network(PG_FUNCTION_ARGS)
712
712
713
713
ip_family (dst ) = ip_family (ip );
714
714
ip_bits (dst ) = ip_bits (ip );
715
- ip_type (dst ) = 1 ;
715
+ ip_is_cidr (dst ) = true ;
716
716
VARATT_SIZEP (dst ) = VARHDRSZ
717
717
+ ((char * ) ip_addr (dst ) - (char * ) VARDATA (dst ))
718
718
+ ip_addrsize (dst );
@@ -756,7 +756,7 @@ network_netmask(PG_FUNCTION_ARGS)
756
756
757
757
ip_family (dst ) = ip_family (ip );
758
758
ip_bits (dst ) = ip_maxbits (ip );
759
- ip_type (dst ) = 0 ;
759
+ ip_is_cidr (dst ) = false ;
760
760
VARATT_SIZEP (dst ) = VARHDRSZ
761
761
+ ((char * ) ip_addr (dst ) - (char * ) VARDATA (dst ))
762
762
+ ip_addrsize (dst );
@@ -806,7 +806,7 @@ network_hostmask(PG_FUNCTION_ARGS)
806
806
807
807
ip_family (dst ) = ip_family (ip );
808
808
ip_bits (dst ) = ip_maxbits (ip );
809
- ip_type (dst ) = 0 ;
809
+ ip_is_cidr (dst ) = false ;
810
810
VARATT_SIZEP (dst ) = VARHDRSZ
811
811
+ ((char * ) ip_addr (dst ) - (char * ) VARDATA (dst ))
812
812
+ ip_addrsize (dst );
0 commit comments