Skip to content

Commit d4a4d4c

Browse files
committed
Attached is a patch adding following functions:
inet(text), cidr(text): convert a text value into inet/cidr set_masklen(inet): set masklen on the inet value Patch also contains regression checks for these functions. Alex Pilosov
1 parent 82dc797 commit d4a4d4c

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+
static Datum text_network(text *src, int type);
2425
static int32 network_cmp_internal(inet *a1, inet *a2);
2526
static int v4bitncmp(unsigned long a1, unsigned long a2, int bits);
2627
static bool v4addressOK(unsigned long a1, int bits);
@@ -149,6 +150,51 @@ cidr_out(PG_FUNCTION_ARGS)
149150
}
150151

151152

153+
Datum
154+
text_network(text *src, int type)
155+
{
156+
int len = 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+
return text_network( PG_GETARG_TEXT_P(0), 1);
169+
}
170+
171+
Datum
172+
text_inet(PG_FUNCTION_ARGS)
173+
{
174+
return text_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+
int bits = 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 ( host PGUID 12 f t t t 1 f 25 "869" 100 0 0 100 netw
23082308
DESCR("show address octets only");
23092309
DATA(insert OID = 730 ( text PGUID 12 f t t t 1 f 25 "869" 100 0 0 100 network_show - ));
23102310
DESCR("show all parts of inet/cidr value");
2311+
DATA(insert OID = 1910 ( inet PGUID 12 f t t t 1 f 869 "25" 100 0 0 100 text_inet - ));
2312+
DESCR("text to inet");
2313+
DATA(insert OID = 1911 ( cidr PGUID 12 f t t t 1 f 650 "25" 100 0 0 100 text_cidr - ));
2314+
DESCR("text to cidr");
2315+
DATA(insert OID = 1912 ( set_masklen PGUID 12 f t t t 2 f 869 "869 23" 100 0 0 100 inet_set_masklen - ));
2316+
DESCR("change the netmask of an inet");
23112317

23122318
DATA(insert OID = 1691 ( boolle PGUID 12 f t t t 2 f 16 "16 16" 100 0 0 100 boolle - ));
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
extern Datum network_show(PG_FUNCTION_ARGS);
516516
extern Datum network_abbrev(PG_FUNCTION_ARGS);
517517
extern double convert_network_to_scalar(Datum value, Oid typid);
518+
extern Datum text_cidr(PG_FUNCTION_ARGS);
519+
extern Datum text_inet(PG_FUNCTION_ARGS);
520+
extern Datum inet_set_masklen(PG_FUNCTION_ARGS);
518521

519522
/* mac.c */
520523
extern Datum macaddr_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, c AS cidr, i AS inet FROM INET_TBL;
2325

@@ -45,3 +47,5 @@ SELECT '' AS ten, i, c,
4547
i >> c AS sup, i >>= c AS 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy