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

Commitd4a4d4c

Browse files
committed
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidrset_masklen(inet): set masklen on the inet valuePatch also contains regression checks for these functions.Alex Pilosov
1 parent82dc797 commitd4a4d4c

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*is for IP V4 CIDR notation, but prepared for V6: just
44
*add the necessary bits where the comments indicate.
55
*
6-
*$Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.30 2001/06/09 22:16:18 tgl Exp $
6+
*$Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.31 2001/06/13 21:08:59 momjian Exp $
77
*
88
*Jon Postel RIP 16 Oct 1998
99
*/
@@ -21,6 +21,7 @@
2121
#include"utils/inet.h"
2222

2323

24+
staticDatumtext_network(text*src,inttype);
2425
staticint32network_cmp_internal(inet*a1,inet*a2);
2526
staticintv4bitncmp(unsigned longa1,unsigned longa2,intbits);
2627
staticboolv4addressOK(unsigned longa1,intbits);
@@ -149,6 +150,51 @@ cidr_out(PG_FUNCTION_ARGS)
149150
}
150151

151152

153+
Datum
154+
text_network(text*src,inttype)
155+
{
156+
intlen=VARSIZE(src)-VARHDRSZ;
157+
158+
char*str=palloc(len+1);
159+
memcpy(str,VARDATA(src),len);
160+
*(str+len)='\0';
161+
162+
PG_RETURN_INET_P(network_in(str,type));
163+
}
164+
165+
Datum
166+
text_cidr(PG_FUNCTION_ARGS)
167+
{
168+
returntext_network(PG_GETARG_TEXT_P(0),1);
169+
}
170+
171+
Datum
172+
text_inet(PG_FUNCTION_ARGS)
173+
{
174+
returntext_network(PG_GETARG_TEXT_P(0),0);
175+
}
176+
177+
Datum
178+
inet_set_masklen(PG_FUNCTION_ARGS)
179+
{
180+
inet*src=PG_GETARG_INET_P(0);
181+
intbits=PG_GETARG_INT32(1);
182+
inet*dst;
183+
184+
if ((bits<0)|| (bits>32))/* no support for v6 yet */
185+
{
186+
elog(ERROR,"set_masklen - invalid value '%d'",bits);
187+
}
188+
189+
/* clone the original data */
190+
dst= (inet*)palloc(VARHDRSZ+sizeof(inet_struct));
191+
memcpy(dst,src,VARHDRSZ+sizeof(inet_struct));
192+
193+
ip_bits(dst)=bits;
194+
195+
PG_RETURN_INET_P(dst);
196+
}
197+
152198
/*
153199
*Basic comparison function for sorting and inet/cidr comparisons.
154200
*

‎src/include/catalog/pg_proc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.191 2001/06/12 16:34:26 momjian Exp $
10+
* $Id: pg_proc.h,v 1.192 2001/06/13 21:08:59 momjian Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -2308,6 +2308,12 @@ DATA(insert OID = 699 ( hostPGUID 12 f t t t 1 f 25 "869" 100 0 0 100 netw
23082308
DESCR("show address octets only");
23092309
DATA(insertOID=730 (textPGUID12fttt1f25"869"10000100network_show- ));
23102310
DESCR("show all parts of inet/cidr value");
2311+
DATA(insertOID=1910 (inetPGUID12fttt1f869"25"10000100text_inet- ));
2312+
DESCR("text to inet");
2313+
DATA(insertOID=1911 (cidrPGUID12fttt1f650"25"10000100text_cidr- ));
2314+
DESCR("text to cidr");
2315+
DATA(insertOID=1912 (set_masklenPGUID12fttt2f869"869 23"10000100inet_set_masklen- ));
2316+
DESCR("change the netmask of an inet");
23112317

23122318
DATA(insertOID=1691 (boollePGUID12fttt2f16"16 16"10000100boolle- ));
23132319
DESCR("less-than-or-equal");

‎src/include/utils/builtins.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: builtins.h,v 1.152 2001/06/12 16:34:27 momjian Exp $
10+
* $Id: builtins.h,v 1.153 2001/06/13 21:08:59 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -515,6 +515,9 @@ extern Datum network_host(PG_FUNCTION_ARGS);
515515
externDatumnetwork_show(PG_FUNCTION_ARGS);
516516
externDatumnetwork_abbrev(PG_FUNCTION_ARGS);
517517
externdoubleconvert_network_to_scalar(Datumvalue,Oidtypid);
518+
externDatumtext_cidr(PG_FUNCTION_ARGS);
519+
externDatumtext_inet(PG_FUNCTION_ARGS);
520+
externDatuminet_set_masklen(PG_FUNCTION_ARGS);
518521

519522
/* mac.c */
520523
externDatummacaddr_in(PG_FUNCTION_ARGS);

‎src/test/regress/expected/inet.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ INSERT INTO INET_TBL (c, i) VALUES ('10', '9.1.2.3/8');
1818
-- check that CIDR rejects invalid input:
1919
INSERT INTO INET_TBL (c, i) VALUES ('192.168.1.2/24', '192.168.1.226');
2020
ERROR: invalid CIDR value '192.168.1.2/24': has bits set to right of mask
21+
-- check that CIDR rejects invalid input when converting from text:
22+
INSERT INTO INET_TBL (c, i) VALUES (cidr('192.168.1.2/24'), '192.168.1.226');
23+
ERROR: invalid CIDR value '192.168.1.2/24': has bits set to right of mask
2124
SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL;
2225
ten | cidr | inet
2326
-----+----------------+------------------
@@ -135,3 +138,19 @@ SELECT '' AS ten, i, c,
135138
| 9.1.2.3/8 | 10.0.0.0/8 | t | t | f | f | f | t | f | f | f | f
136139
(10 rows)
137140

141+
-- check the conversion to/from text and set_netmask
142+
select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
143+
ten | set_masklen
144+
-----+------------------
145+
| 192.168.1.226/24
146+
| 192.168.1.226/24
147+
| 10.1.2.3/24
148+
| 10.1.2.3/24
149+
| 10.1.2.3/24
150+
| 10.1.2.3/24
151+
| 10.1.2.3/24
152+
| 10.1.2.3/24
153+
| 11.1.2.3/24
154+
| 9.1.2.3/24
155+
(10 rows)
156+

‎src/test/regress/sql/inet.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ INSERT INTO INET_TBL (c, i) VALUES ('10', '11.1.2.3/8');
1818
INSERT INTO INET_TBL (c, i)VALUES ('10','9.1.2.3/8');
1919
-- check that CIDR rejects invalid input:
2020
INSERT INTO INET_TBL (c, i)VALUES ('192.168.1.2/24','192.168.1.226');
21+
-- check that CIDR rejects invalid input when converting from text:
22+
INSERT INTO INET_TBL (c, i)VALUES (cidr('192.168.1.2/24'),'192.168.1.226');
2123

2224
SELECT''AS ten, cAScidr, iASinetFROM INET_TBL;
2325

@@ -45,3 +47,5 @@ SELECT '' AS ten, i, c,
4547
i>> cAS sup, i>>= cAS spe
4648
FROM INET_TBL;
4749

50+
-- check the conversion to/from text and set_netmask
51+
select''AS ten, set_masklen(inet(text(i)),24)FROM INET_TBL;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp