Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit858a3b5

Browse files
committed
New CIDR type and fixed INET type, from D'Arcy.
1 parent56f96f5 commit858a3b5

File tree

7 files changed

+161
-29
lines changed

7 files changed

+161
-29
lines changed

‎src/backend/utils/adt/inet.c‎

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
*PostgreSQL type definitions for the INET type.This
2+
*PostgreSQL type definitions for the INET type.This
33
*is for IP V4 CIDR notation, but prepared for V6: just
44
*add the necessary bits where the comments indicate.
55
*
6-
*$Id: inet.c,v 1.8 1998/10/2104:25:25 momjian Exp $
6+
*$Id: inet.c,v 1.9 1998/10/2116:06:45 momjian Exp $
77
*Jon Postel RIP 16 Oct 1998
88
*/
99

@@ -22,7 +22,7 @@
2222
#include<utils/builtins.h>
2323
#include<utils/inet.h>
2424

25-
staticintv4bitncmp(unsignedinta1,unsignedinta2,intbits);
25+
staticintv4bitncmp(unsignedinta1,unsignedinta2,intbits);
2626

2727
/*
2828
*Access macros.Add IPV6 support.
@@ -37,11 +37,14 @@ static int v4bitncmp(unsigned int a1, unsigned int a2, int bits);
3737
#defineip_bits(inetptr) \
3838
(((inet_struct *)VARDATA(inetptr))->bits)
3939

40+
#defineip_type(inetptr) \
41+
(((inet_struct *)VARDATA(inetptr))->type)
42+
4043
#defineip_v4addr(inetptr) \
4144
(((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr)
4245

4346
/*
44-
*IP address reader.
47+
*INET address reader.
4548
*/
4649

4750
inet*
@@ -70,11 +73,12 @@ inet_in(char *src)
7073
+ ((char*)&ip_v4addr(dst)- (char*)VARDATA(dst))
7174
+ip_addrsize(dst);
7275
ip_bits(dst)=bits;
76+
ip_type(dst)=0;
7377
return (dst);
7478
}
7579

7680
/*
77-
*IP address output function.
81+
*INET address output function.
7882
*/
7983

8084
char*
@@ -99,6 +103,8 @@ inet_out(inet *src)
99103
elog(ERROR,"unknown address family (%d)",ip_family(src));
100104
return (NULL);
101105
}
106+
if (ip_type(src)==0&&ip_bits(src)==32&& (dst=strchr(tmp,'/'))!=NULL)
107+
*dst=0;
102108
dst=palloc(strlen(tmp)+1);
103109
if (dst==NULL)
104110
{
@@ -109,6 +115,39 @@ inet_out(inet *src)
109115
return (dst);
110116
}
111117

118+
/*
119+
*CIDR uses all of INET's funcs, just has a separate input func.
120+
*/
121+
122+
inet*
123+
cidr_in(char*src)
124+
{
125+
intbits;
126+
inet*dst;
127+
128+
dst=palloc(VARHDRSZ+sizeof(inet_struct));
129+
if (dst==NULL)
130+
{
131+
elog(ERROR,"unable to allocate memory in cidr_in()");
132+
return (NULL);
133+
}
134+
/* First, try for an IP V4 address: */
135+
ip_family(dst)=AF_INET;
136+
bits=inet_net_pton(ip_family(dst),src,&ip_v4addr(dst),ip_addrsize(dst));
137+
if ((bits<0)|| (bits>32))
138+
{
139+
/* Go for an IPV6 address here, before faulting out: */
140+
elog(ERROR,"could not parse \"%s\"",src);
141+
pfree(dst);
142+
return (NULL);
143+
}
144+
VARSIZE(dst)=VARHDRSZ
145+
+ ((char*)&ip_v4addr(dst)- (char*)VARDATA(dst))
146+
+ip_addrsize(dst);
147+
ip_bits(dst)=bits;
148+
return (dst);
149+
}
150+
112151
/*
113152
*Boolean tests for magnitude. Add V4/V6 testing!
114153
*/
@@ -267,20 +306,23 @@ inet_cmp(inet *a1, inet *a2)
267306
}
268307

269308
text*
270-
inet_netmask(inet*ip)
309+
inet_host(inet*ip)
271310
{
272-
text*ret;
273-
intlen;
311+
text*ret;
312+
intlen;
274313
char*ptr,
275314
tmp[sizeof("255.255.255.255/32")];
276315

316+
if (ip_type(ip))
317+
{
318+
elog(ERROR,"CIDR type has no host part");
319+
returnNULL;
320+
}
321+
277322
if (ip_family(ip)==AF_INET)
278323
{
279324
/* It's an IP V4 address: */
280-
intaddr=htonl((-1 << (32-ip_bits(ip)))&0xffffffff);
281-
282-
/* a little wasteful by why reinvent the wheel? */
283-
if (inet_net_ntop(AF_INET,&addr,32,tmp,sizeof(tmp))<0)
325+
if (inet_net_ntop(AF_INET,&ip_v4addr(ip),32,tmp,sizeof(tmp))<0)
284326
{
285327
elog(ERROR,"unable to print netmask (%s)",strerror(errno));
286328
return (NULL);
@@ -298,7 +340,7 @@ inet_netmask(inet *ip)
298340
ret=palloc(len);
299341
if (ret==NULL)
300342
{
301-
elog(ERROR,"unable to allocate memory ininet_netmask()");
343+
elog(ERROR,"unable to allocate memory ininet_host()");
302344
return (NULL);
303345
}
304346
VARSIZE(ret)=len;
@@ -307,23 +349,24 @@ inet_netmask(inet *ip)
307349
}
308350

309351
int4
310-
inet_masklen(inet*ip)
352+
inet_netmasklen(inet*ip)
311353
{
312354
returnip_bits(ip);
313355
}
314356

315357
text*
316358
inet_broadcast(inet*ip)
317359
{
318-
text*ret;
319-
intlen;
360+
text*ret;
361+
intlen;
320362
char*ptr,
321363
tmp[sizeof("255.255.255.255/32")]="Hello";
322364

323365
if (ip_family(ip)==AF_INET)
324366
{
325367
/* It's an IP V4 address: */
326-
intaddr=htonl(ntohl(ip_v4addr(ip)) | (0xffffffff >>ip_bits(ip)));
368+
intaddr=htonl(ntohl(ip_v4addr(ip)) | (0xffffffff >>ip_bits(ip)));
369+
327370
/* int addr = htonl(ip_v4addr(ip) | (0xffffffff >> ip_bits(ip))); */
328371

329372
if (inet_net_ntop(AF_INET,&addr,32,tmp,sizeof(tmp))<0)
@@ -352,6 +395,45 @@ inet_broadcast(inet *ip)
352395
return (ret);
353396
}
354397

398+
text*
399+
inet_netmask(inet*ip)
400+
{
401+
text*ret;
402+
intlen;
403+
char*ptr,
404+
tmp[sizeof("255.255.255.255/32")];
405+
406+
if (ip_family(ip)==AF_INET)
407+
{
408+
/* It's an IP V4 address: */
409+
intaddr=htonl((-1 << (32-ip_bits(ip)))&0xffffffff);
410+
411+
if (inet_net_ntop(AF_INET,&addr,32,tmp,sizeof(tmp))<0)
412+
{
413+
elog(ERROR,"unable to print netmask (%s)",strerror(errno));
414+
return (NULL);
415+
}
416+
}
417+
else
418+
{
419+
/* Go for an IPV6 address here, before faulting out: */
420+
elog(ERROR,"unknown address family (%d)",ip_family(ip));
421+
return (NULL);
422+
}
423+
if ((ptr=strchr(tmp,'/'))!=NULL)
424+
*ptr=0;
425+
len=VARHDRSZ+strlen(tmp);
426+
ret=palloc(len);
427+
if (ret==NULL)
428+
{
429+
elog(ERROR,"unable to allocate memory in inet_netmask()");
430+
return (NULL);
431+
}
432+
VARSIZE(ret)=len;
433+
strcpy(VARDATA(ret),tmp);
434+
return (ret);
435+
}
436+
355437
/*
356438
*Bitwise comparison for V4 addresses. Add V6 implementation!
357439
*/
@@ -372,4 +454,3 @@ v4bitncmp(unsigned int a1, unsigned int a2, int bits)
372454
return (1);
373455
return (0);
374456
}
375-

‎src/include/catalog/pg_opclass.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_opclass.h,v 1.13 1998/10/08 00:19:38 momjian Exp $
10+
* $Id: pg_opclass.h,v 1.14 1998/10/21 16:06:46 momjian Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -111,5 +111,7 @@ DATA(insert OID = 810 (macaddr_ops 829 ));
111111
DESCR("");
112112
DATA(insertOID=935 (inet_ops869 ));
113113
DESCR("");
114+
DATA(insertOID=652 (inet_ops650 ));
115+
DESCR("");
114116

115117
#endif/* PG_OPCLASS_H */

‎src/include/catalog/pg_operator.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_operator.h,v 1.40 1998/10/08 00:19:39 momjian Exp $
10+
* $Id: pg_operator.h,v 1.41 1998/10/21 16:06:46 momjian Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -643,7 +643,7 @@ DATA(insert OID = 1223 ( "<=" PGUID 0 b t f 829 829 16 1225 1224 0 0 macadd
643643
DATA(insertOID=1224 (">"PGUID0btf829829161222122300macaddr_gtintltselintltjoinsel ));
644644
DATA(insertOID=1225 (">="PGUID0btf829829161223122200macaddr_geintltselintltjoinsel ));
645645

646-
/*IP type */
646+
/*INET type */
647647
DATA(insertOID=1201 ("="PGUID0btt869869161201120200inet_eqeqseleqjoinsel ));
648648
DATA(insertOID=1202 ("<>"PGUID0btf869869161202120100inet_neneqselneqjoinsel ));
649649
DATA(insertOID=1203 ("<"PGUID0btf869869161205120600inet_ltintltselintltjoinsel ));
@@ -655,6 +655,18 @@ DATA(insert OID = 932 ( "<<=" PGUID 0 b t f 869 869 16 934 933 0 0 in
655655
DATA(insertOID=933 (">>"PGUID0btf8698691693193200inet_supintltselintltjoinsel ));
656656
DATA(insertOID=934 (">>="PGUID0btf8698691693293100inet_supeqintltselintltjoinsel ));
657657

658+
/* CIDR type */
659+
DATA(insertOID=820 ("="PGUID0btt6506501682082100inet_eqeqseleqjoinsel ));
660+
DATA(insertOID=821 ("<>"PGUID0btf6506501682182000inet_neneqselneqjoinsel ));
661+
DATA(insertOID=822 ("<"PGUID0btf6506501682482500inet_ltintltselintltjoinsel ));
662+
DATA(insertOID=823 ("<="PGUID0btf6506501682582400inet_leintltselintltjoinsel ));
663+
DATA(insertOID=824 (">"PGUID0btf6506501682282300inet_gtintltselintltjoinsel ));
664+
DATA(insertOID=825 (">="PGUID0btf6506501682382200inet_geintltselintltjoinsel ));
665+
DATA(insertOID=826 ("<<"PGUID0btf65065016828100400inet_subintltselintltjoinsel ));
666+
DATA(insertOID=827 ("<<="PGUID0btf65065016100482800inet_subeqintltselintltjoinsel ));
667+
DATA(insertOID=828 (">>"PGUID0btf6506501682682700inet_supintltselintltjoinsel ));
668+
DATA(insertOID=1004 (">>="PGUID0btf6506501682782600inet_supeqintltselintltjoinsel ));
669+
658670

659671
/*
660672
* function prototypes

‎src/include/catalog/pg_proc.h‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_proc.h,v 1.74 1998/10/20 23:03:19 momjian Exp $
9+
* $Id: pg_proc.h,v 1.75 1998/10/21 16:06:47 momjian Exp $
1010
*
1111
* NOTES
1212
* The script catalog/genbki.sh reads this file and generates .bki
@@ -2068,12 +2068,17 @@ DESCR("less-equal-greater");
20682068
DATA(insertOID=837 (macaddr_manufPGUID11ftf1f25"829"10000100foobar ));
20692069
DESCR("MAC manufacturer");
20702070

2071-
/* forip type support */
2071+
/* forinet type support */
20722072
DATA(insertOID=910 (inet_inPGUID11ftf1f869"0"10000100foobar ));
20732073
DESCR("(internal)");
20742074
DATA(insertOID=911 (inet_outPGUID11ftf1f23"0"10000100foobar ));
20752075
DESCR("(internal)");
20762076

2077+
/* for cidr type support */
2078+
DATA(insertOID=1267 (cidr_inPGUID11ftf1f650"0"10000100foobar ));
2079+
DESCR("(internal)");
2080+
2081+
/* these are used for both inet and cidr */
20772082
DATA(insertOID=920 (inet_eqPGUID11ftf2f16"869 869"10000100foobar ));
20782083
DESCR("equal");
20792084
DATA(insertOID=921 (inet_ltPGUID11ftf2f16"869 869"10000100foobar ));
@@ -2097,12 +2102,36 @@ DESCR("is-supernet");
20972102
DATA(insertOID=930 (inet_supeqPGUID11ftf2f16"869 869"10000100foobar ));
20982103
DESCR("is-supernet-or-equal");
20992104

2105+
/* inet/cidr base versions */
21002106
DATA(insertOID=940 (inet_netmaskPGUID11ftf1f25"869"10000100foobar ));
21012107
DESCR("netmask of inet address");
2102-
DATA(insertOID=941 (inet_masklenPGUID11ftf1f23"869"10000100foobar ));
2108+
DATA(insertOID=941 (inet_netmasklenPGUID11ftf1f23"869"10000100foobar ));
21032109
DESCR("netmask length");
21042110
DATA(insertOID=945 (inet_broadcastPGUID11ftf1f25"869"10000100foobar ));
21052111
DESCR("broadcast address");
2112+
DATA(insertOID=682 (inet_hostPGUID11ftf1f25"869"10000100foobar ));
2113+
DESCR("host address");
2114+
2115+
/* inet versions */
2116+
DATA(insertOID=940 (netmaskPGUID14ftf1f25"869"10000100"select inet_netmask($1)"- ));
2117+
DESCR("netmask of address");
2118+
DATA(insertOID=941 (netmasklenPGUID14ftf1f23"869"10000100"select inet_netmasklen($1)"- ));
2119+
DESCR("netmask length");
2120+
DATA(insertOID=945 (broadcastPGUID14ftf1f25"869"10000100"select inet_broadcast($1)"- ));
2121+
DESCR("broadcast address");
2122+
DATA(insertOID=682 (hostPGUID14ftf1f25"869"10000100"select inet_host($1)"- ));
2123+
DESCR("host address");
2124+
2125+
/* cidr versions */
2126+
DATA(insertOID=940 (netmaskPGUID14ftf1f25"650"10000100"select inet_netmask($1)"- ));
2127+
DESCR("netmask of address");
2128+
DATA(insertOID=941 (netmasklenPGUID14ftf1f23"650"10000100"select inet_netmasklen($1)"- ));
2129+
DESCR("netmask length");
2130+
DATA(insertOID=945 (broadcastPGUID14ftf1f25"650"10000100"select inet_broadcast($1)"- ));
2131+
DESCR("broadcast address");
2132+
DATA(insertOID=682 (hostPGUID14ftf1f25"650"10000100"select inet_host($1)"- ));
2133+
DESCR("host address");
2134+
21062135

21072136

21082137
/*

‎src/include/catalog/pg_type.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_type.h,v 1.49 1998/10/08 00:19:42 momjian Exp $
10+
* $Id: pg_type.h,v 1.50 1998/10/21 16:06:48 momjian Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -301,7 +301,9 @@ DATA(insert OID = 791 ( _money PGUID -1 -1 f b t \054 0 790 array_in array
301301
DATA(insertOID=829 (macaddrPGUID6-1fbt \05400macaddr_inmacaddr_outmacaddr_inmacaddr_outi_null_ ));
302302
DESCR("MAC address");
303303
DATA(insertOID=869 (inetPGUID-1-1fbt \05400inet_ininet_outinet_ininet_outi_null_ ));
304-
DESCR("IP address");
304+
DESCR("Host address");
305+
DATA(insertOID=650 (cidrPGUID-1-1fbt \05400cidr_ininet_outcidr_ininet_outi_null_ ));
306+
DESCR("Network address");
305307

306308
/* OIDS 900 - 999 */
307309

@@ -340,6 +342,7 @@ DESCR("access control list");
340342
DATA(insertOID=1034 (_aclitemPGUID-1-1fbt \05401033array_inarray_outarray_inarray_outi_null_ ));
341343
DATA(insertOID=1040 (_macaddrPGUID-1-1fbt \0540829array_inarray_outarray_inarray_outi_null_ ));
342344
DATA(insertOID=1041 (_inetPGUID-1-1fbt \0540869array_inarray_outarray_inarray_outi_null_ ));
345+
DATA(insertOID=651 (_cidrPGUID-1-1fbt \0540650array_inarray_outarray_inarray_outi_null_ ));
343346
DATA(insertOID=1042 (bpcharPGUID-1-1fbt \054018bpcharinbpcharoutbpcharinbpcharouti_null_ ));
344347
DESCR("blank-padded characters, length specifed when created");
345348
#defineBPCHAROID1042

‎src/include/utils/builtins.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: builtins.h,v 1.62 1998/10/20 23:03:20 momjian Exp $
9+
* $Id: builtins.h,v 1.63 1998/10/21 16:06:49 momjian Exp $
1010
*
1111
* NOTES
1212
* This should normally only be included by fmgr.h.
@@ -518,6 +518,9 @@ int inet_net_pton(int af, const char *src, void *dst, size_t size);
518518
char*inet_cidr_ntop(intaf,constvoid*src,size_tlen,intbits,char*dst,size_tsize);
519519
intinet_cidr_pton(intaf,constvoid*src,void*dst,size_tsize,int*used);
520520

521+
/* cidr.c */
522+
inet*cidr_in(char*str);
523+
521524
/* inet.c */
522525
inet*inet_in(char*str);
523526
char*inet_out(inet*addr);
@@ -534,8 +537,9 @@ boolinet_supeq(inet * a1, inet * a2);
534537
int4inet_cmp(inet*a1,inet*a2);
535538

536539
text*inet_netmask(inet*addr);
537-
int4inet_masklen(inet*addr);
540+
int4inet_netmasklen(inet*addr);
538541
text*inet_broadcast(inet*addr);
542+
text*inet_host(inet*addr);
539543

540544

541545
/* mac.c */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp