From 61784c54b59ccf810f95de88ca42c60a0d7d798e Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 22 Dec 2000 18:00:24 +0000 Subject: [PATCH] Change default output formatting for CIDR to be unabbreviated, per recommendation from Paul Vixie. Add a new abbrev() function to produce abbreviated format as text. No forced initdb, but new function is not available unless you do an initdb or add the pg_proc row manually. --- doc/src/sgml/datatype.sgml | 24 ++++-- doc/src/sgml/func.sgml | 17 ++-- src/backend/utils/adt/network.c | 62 +++++++++++--- src/include/catalog/pg_proc.h | 4 +- src/include/utils/builtins.h | 3 +- src/test/regress/expected/inet.out | 132 ++++++++++++++--------------- 6 files changed, 151 insertions(+), 91 deletions(-) diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index 6afe6cdb5a..6286b419dc 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -1,5 +1,5 @@ @@ -1818,7 +1818,7 @@ January 8 04:05:06 1999 PST class="parameter">x.x.x.x is an IP address and y is the number of bits in the netmask. If the y part is left off, then the + class="parameter">/y part is left off, then the netmask is 32, and the value represents just a single host. On display, the /y portion is suppressed if the netmask is 32. @@ -1837,7 +1837,7 @@ January 8 04:05:06 1999 PST class="parameter">x.x.x.x/y where x.x.x.x is the network and y is the number of bits in the netmask. If - y omitted, it is calculated + y is omitted, it is calculated using assumptions from the older classful numbering system, except that it will be at least large enough to include all of the octets written in the input. @@ -1848,56 +1848,68 @@ January 8 04:05:06 1999 PST <type>cidr</> Type Input Examples - + CIDR Input CIDR Displayed + abbrev(CIDR) 192.168.100.128/25 192.168.100.128/25 + 192.168.100.128/25 192.168/24 + 192.168.0.0/24 192.168.0/24 192.168/25 192.168.0.0/25 + 192.168.0.0/25 192.168.1 + 192.168.1.0/24 192.168.1/24 192.168 + 192.168.0.0/24 192.168.0/24 128.1 + 128.1.0.0/16 128.1/16 128 + 128.0.0.0/16 128.0/16 128.1.2 + 128.1.2.0/24 128.1.2/24 10.1.2 + 10.1.2.0/24 10.1.2/24 10.1 + 10.1.0.0/16 10.1/16 10 + 10.0.0.0/8 10/8 @@ -1917,8 +1929,8 @@ January 8 04:05:06 1999 PST If you do not like the output format for inet or - cidr values, try the host() and - text() functions. + cidr values, try the host(), + text(), and abbrev() functions. diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index f03fae625a..533c1f2757 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -2432,7 +2432,7 @@ Not defined by this name. Implements the intersection operator '#' cidr extract network part of address network('192.168.1.5/24') - 192.168.1/24 + 192.168.1.0/24 text(inet) @@ -2441,15 +2441,22 @@ Not defined by this name. Implements the intersection operator '#' text(inet '192.168.1.5') 192.168.1.5/32 + + abbrev(inet) + text + extract abbreviated display as text + abbrev(cidr '10.1.0.0/16') + 10.1/16 +
All of the functions for inet can be applied to - cidr values as well. The host() and - text() functions are primarily intended to offer - alternative display formats. + cidr values as well. The host(), + text(), and abbrev() functions are primarily + intended to offer alternative display formats. diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c index 75527fda02..97e0825adf 100644 --- a/src/backend/utils/adt/network.c +++ b/src/backend/utils/adt/network.c @@ -3,7 +3,7 @@ * is for IP V4 CIDR notation, but prepared for V6: just * add the necessary bits where the comments indicate. * - * $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.27 2000/11/25 21:30:54 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.28 2000/12/22 18:00:20 tgl Exp $ * * Jon Postel RIP 16 Oct 1998 */ @@ -111,19 +111,24 @@ inet_out(PG_FUNCTION_ARGS) inet *src = PG_GETARG_INET_P(0); char tmp[sizeof("255.255.255.255/32")]; char *dst; + int len; if (ip_family(src) == AF_INET) { /* It's an IP V4 address: */ - if (ip_type(src)) - dst = inet_cidr_ntop(AF_INET, &ip_v4addr(src), ip_bits(src), - tmp, sizeof(tmp)); - else - dst = inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src), - tmp, sizeof(tmp)); - + /* Use inet style for both inet and cidr, since we don't want + * abbreviated CIDR style here. + */ + dst = inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src), + tmp, sizeof(tmp)); if (dst == NULL) elog(ERROR, "unable to print address (%s)", strerror(errno)); + /* For CIDR, add /n if not present */ + if (ip_type(src) && strchr(tmp, '/') == NULL) + { + len = strlen(tmp); + snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src)); + } } else /* Go for an IPV6 address here, before faulting out: */ @@ -375,17 +380,50 @@ network_show(PG_FUNCTION_ARGS) /* force display of 32 bits, regardless of masklen... */ if (inet_net_ntop(AF_INET, &ip_v4addr(ip), 32, tmp, sizeof(tmp)) == NULL) elog(ERROR, "unable to print host (%s)", strerror(errno)); + /* Add /n if not present (which it won't be) */ + if (strchr(tmp, '/') == NULL) + { + len = strlen(tmp); + snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip)); + } } else /* Go for an IPV6 address here, before faulting out: */ elog(ERROR, "unknown address family (%d)", ip_family(ip)); - /* Add /n if not present */ - if (strchr(tmp, '/') == NULL) + /* Return string as a text datum */ + len = strlen(tmp); + ret = (text *) palloc(len + VARHDRSZ); + VARATT_SIZEP(ret) = len + VARHDRSZ; + memcpy(VARDATA(ret), tmp, len); + PG_RETURN_TEXT_P(ret); +} + +Datum +network_abbrev(PG_FUNCTION_ARGS) +{ + inet *ip = PG_GETARG_INET_P(0); + text *ret; + char *dst; + int len; + char tmp[sizeof("255.255.255.255/32")]; + + if (ip_family(ip) == AF_INET) { - len = strlen(tmp); - snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip)); + /* It's an IP V4 address: */ + if (ip_type(ip)) + dst = inet_cidr_ntop(AF_INET, &ip_v4addr(ip), ip_bits(ip), + tmp, sizeof(tmp)); + else + dst = inet_net_ntop(AF_INET, &ip_v4addr(ip), ip_bits(ip), + tmp, sizeof(tmp)); + + if (dst == NULL) + elog(ERROR, "unable to print address (%s)", strerror(errno)); } + else + /* Go for an IPV6 address here, before faulting out: */ + elog(ERROR, "unknown address family (%d)", ip_family(ip)); /* Return string as a text datum */ len = strlen(tmp); diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index c0914fd601..5c43faf1b0 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: pg_proc.h,v 1.180 2000/12/21 20:48:16 petere Exp $ + * $Id: pg_proc.h,v 1.181 2000/12/22 18:00:17 tgl Exp $ * * NOTES * The script catalog/genbki.sh reads this file and generates .bki @@ -2294,6 +2294,8 @@ DATA(insert OID = 930 ( network_supeq PGUID 12 f t t t 2 f 16 "869 869" 100 0 DESCR("is-supernet-or-equal"); /* inet/cidr functions */ +DATA(insert OID = 605 ( abbrev PGUID 12 f t t t 1 f 25 "869" 100 0 0 100 network_abbrev - )); +DESCR("abbreviated display of inet/cidr value"); DATA(insert OID = 683 ( network PGUID 12 f t t t 1 f 650 "869" 100 0 0 100 network_network - )); DESCR("network part of address"); DATA(insert OID = 696 ( netmask PGUID 12 f t t t 1 f 869 "869" 100 0 0 100 network_netmask - )); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index 97f03b75a2..c23e2f3312 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: builtins.h,v 1.144 2000/12/08 23:57:00 tgl Exp $ + * $Id: builtins.h,v 1.145 2000/12/22 18:00:18 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -508,6 +508,7 @@ extern Datum network_masklen(PG_FUNCTION_ARGS); extern Datum network_broadcast(PG_FUNCTION_ARGS); extern Datum network_host(PG_FUNCTION_ARGS); extern Datum network_show(PG_FUNCTION_ARGS); +extern Datum network_abbrev(PG_FUNCTION_ARGS); /* mac.c */ extern Datum macaddr_in(PG_FUNCTION_ARGS); diff --git a/src/test/regress/expected/inet.out b/src/test/regress/expected/inet.out index 009c098e1d..9c0beef363 100644 --- a/src/test/regress/expected/inet.out +++ b/src/test/regress/expected/inet.out @@ -19,18 +19,18 @@ INSERT INTO INET_TBL (c, i) VALUES ('10', '9.1.2.3/8'); INSERT INTO INET_TBL (c, i) VALUES ('192.168.1.2/24', '192.168.1.226'); ERROR: invalid CIDR value '192.168.1.2/24': has bits set to right of mask SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL; - ten | cidr | inet ------+--------------+------------------ - | 192.168.1/24 | 192.168.1.226/24 - | 192.168.1/24 | 192.168.1.226 - | 10/8 | 10.1.2.3/8 - | 10.0.0.0/32 | 10.1.2.3/8 - | 10.1.2.3/32 | 10.1.2.3 - | 10.1.2/24 | 10.1.2.3/24 - | 10.1/16 | 10.1.2.3/16 - | 10/8 | 10.1.2.3/8 - | 10/8 | 11.1.2.3/8 - | 10/8 | 9.1.2.3/8 + ten | cidr | inet +-----+----------------+------------------ + | 192.168.1.0/24 | 192.168.1.226/24 + | 192.168.1.0/24 | 192.168.1.226 + | 10.0.0.0/8 | 10.1.2.3/8 + | 10.0.0.0/32 | 10.1.2.3/8 + | 10.1.2.3/32 | 10.1.2.3 + | 10.1.2.0/24 | 10.1.2.3/24 + | 10.1.0.0/16 | 10.1.2.3/16 + | 10.0.0.0/8 | 10.1.2.3/8 + | 10.0.0.0/8 | 11.1.2.3/8 + | 10.0.0.0/8 | 9.1.2.3/8 (10 rows) -- now test some support functions @@ -51,61 +51,61 @@ SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL; SELECT '' AS ten, c AS cidr, broadcast(c), i AS inet, broadcast(i) FROM INET_TBL; - ten | cidr | broadcast | inet | broadcast ------+--------------+------------------+------------------+------------------ - | 192.168.1/24 | 192.168.1.255/24 | 192.168.1.226/24 | 192.168.1.255/24 - | 192.168.1/24 | 192.168.1.255/24 | 192.168.1.226 | 192.168.1.226 - | 10/8 | 10.255.255.255/8 | 10.1.2.3/8 | 10.255.255.255/8 - | 10.0.0.0/32 | 10.0.0.0 | 10.1.2.3/8 | 10.255.255.255/8 - | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3 | 10.1.2.3 - | 10.1.2/24 | 10.1.2.255/24 | 10.1.2.3/24 | 10.1.2.255/24 - | 10.1/16 | 10.1.255.255/16 | 10.1.2.3/16 | 10.1.255.255/16 - | 10/8 | 10.255.255.255/8 | 10.1.2.3/8 | 10.255.255.255/8 - | 10/8 | 10.255.255.255/8 | 11.1.2.3/8 | 11.255.255.255/8 - | 10/8 | 10.255.255.255/8 | 9.1.2.3/8 | 9.255.255.255/8 + ten | cidr | broadcast | inet | broadcast +-----+----------------+------------------+------------------+------------------ + | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.226/24 | 192.168.1.255/24 + | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.226 | 192.168.1.226 + | 10.0.0.0/8 | 10.255.255.255/8 | 10.1.2.3/8 | 10.255.255.255/8 + | 10.0.0.0/32 | 10.0.0.0 | 10.1.2.3/8 | 10.255.255.255/8 + | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3 | 10.1.2.3 + | 10.1.2.0/24 | 10.1.2.255/24 | 10.1.2.3/24 | 10.1.2.255/24 + | 10.1.0.0/16 | 10.1.255.255/16 | 10.1.2.3/16 | 10.1.255.255/16 + | 10.0.0.0/8 | 10.255.255.255/8 | 10.1.2.3/8 | 10.255.255.255/8 + | 10.0.0.0/8 | 10.255.255.255/8 | 11.1.2.3/8 | 11.255.255.255/8 + | 10.0.0.0/8 | 10.255.255.255/8 | 9.1.2.3/8 | 9.255.255.255/8 (10 rows) SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)", i AS inet, network(i) AS "network(inet)" FROM INET_TBL; - ten | cidr | network(cidr) | inet | network(inet) ------+--------------+---------------+------------------+------------------ - | 192.168.1/24 | 192.168.1/24 | 192.168.1.226/24 | 192.168.1/24 - | 192.168.1/24 | 192.168.1/24 | 192.168.1.226 | 192.168.1.226/32 - | 10/8 | 10/8 | 10.1.2.3/8 | 10/8 - | 10.0.0.0/32 | 10.0.0.0/32 | 10.1.2.3/8 | 10/8 - | 10.1.2.3/32 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3/32 - | 10.1.2/24 | 10.1.2/24 | 10.1.2.3/24 | 10.1.2/24 - | 10.1/16 | 10.1/16 | 10.1.2.3/16 | 10.1/16 - | 10/8 | 10/8 | 10.1.2.3/8 | 10/8 - | 10/8 | 10/8 | 11.1.2.3/8 | 11/8 - | 10/8 | 10/8 | 9.1.2.3/8 | 9/8 + ten | cidr | network(cidr) | inet | network(inet) +-----+----------------+----------------+------------------+------------------ + | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.226/24 | 192.168.1.0/24 + | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.226 | 192.168.1.226/32 + | 10.0.0.0/8 | 10.0.0.0/8 | 10.1.2.3/8 | 10.0.0.0/8 + | 10.0.0.0/32 | 10.0.0.0/32 | 10.1.2.3/8 | 10.0.0.0/8 + | 10.1.2.3/32 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3/32 + | 10.1.2.0/24 | 10.1.2.0/24 | 10.1.2.3/24 | 10.1.2.0/24 + | 10.1.0.0/16 | 10.1.0.0/16 | 10.1.2.3/16 | 10.1.0.0/16 + | 10.0.0.0/8 | 10.0.0.0/8 | 10.1.2.3/8 | 10.0.0.0/8 + | 10.0.0.0/8 | 10.0.0.0/8 | 11.1.2.3/8 | 11.0.0.0/8 + | 10.0.0.0/8 | 10.0.0.0/8 | 9.1.2.3/8 | 9.0.0.0/8 (10 rows) SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)", i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL; - ten | cidr | masklen(cidr) | inet | masklen(inet) ------+--------------+---------------+------------------+--------------- - | 192.168.1/24 | 24 | 192.168.1.226/24 | 24 - | 192.168.1/24 | 24 | 192.168.1.226 | 32 - | 10/8 | 8 | 10.1.2.3/8 | 8 - | 10.0.0.0/32 | 32 | 10.1.2.3/8 | 8 - | 10.1.2.3/32 | 32 | 10.1.2.3 | 32 - | 10.1.2/24 | 24 | 10.1.2.3/24 | 24 - | 10.1/16 | 16 | 10.1.2.3/16 | 16 - | 10/8 | 8 | 10.1.2.3/8 | 8 - | 10/8 | 8 | 11.1.2.3/8 | 8 - | 10/8 | 8 | 9.1.2.3/8 | 8 + ten | cidr | masklen(cidr) | inet | masklen(inet) +-----+----------------+---------------+------------------+--------------- + | 192.168.1.0/24 | 24 | 192.168.1.226/24 | 24 + | 192.168.1.0/24 | 24 | 192.168.1.226 | 32 + | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 + | 10.0.0.0/32 | 32 | 10.1.2.3/8 | 8 + | 10.1.2.3/32 | 32 | 10.1.2.3 | 32 + | 10.1.2.0/24 | 24 | 10.1.2.3/24 | 24 + | 10.1.0.0/16 | 16 | 10.1.2.3/16 | 16 + | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 + | 10.0.0.0/8 | 8 | 11.1.2.3/8 | 8 + | 10.0.0.0/8 | 8 | 9.1.2.3/8 | 8 (10 rows) SELECT '' AS four, c AS cidr, masklen(c) AS "masklen(cidr)", i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL WHERE masklen(c) <= 8; - four | cidr | masklen(cidr) | inet | masklen(inet) -------+------+---------------+------------+--------------- - | 10/8 | 8 | 10.1.2.3/8 | 8 - | 10/8 | 8 | 10.1.2.3/8 | 8 - | 10/8 | 8 | 11.1.2.3/8 | 8 - | 10/8 | 8 | 9.1.2.3/8 | 8 + four | cidr | masklen(cidr) | inet | masklen(inet) +------+------------+---------------+------------+--------------- + | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 + | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 + | 10.0.0.0/8 | 8 | 11.1.2.3/8 | 8 + | 10.0.0.0/8 | 8 | 9.1.2.3/8 | 8 (4 rows) SELECT '' AS six, c AS cidr, i AS inet FROM INET_TBL @@ -121,17 +121,17 @@ SELECT '' AS ten, i, c, i << c AS sb, i <<= c AS sbe, i >> c AS sup, i >>= c AS spe FROM INET_TBL; - ten | i | c | lt | le | eq | ge | gt | ne | sb | sbe | sup | spe ------+------------------+--------------+----+----+----+----+----+----+----+-----+-----+----- - | 192.168.1.226/24 | 192.168.1/24 | f | f | f | t | t | t | f | t | f | t - | 192.168.1.226 | 192.168.1/24 | f | f | f | t | t | t | t | t | f | f - | 10.1.2.3/8 | 10/8 | f | f | f | t | t | t | f | t | f | t - | 10.1.2.3/8 | 10.0.0.0/32 | t | t | f | f | f | t | f | f | t | t - | 10.1.2.3 | 10.1.2.3/32 | f | t | t | t | f | f | f | t | f | t - | 10.1.2.3/24 | 10.1.2/24 | f | f | f | t | t | t | f | t | f | t - | 10.1.2.3/16 | 10.1/16 | f | f | f | t | t | t | f | t | f | t - | 10.1.2.3/8 | 10/8 | f | f | f | t | t | t | f | t | f | t - | 11.1.2.3/8 | 10/8 | f | f | f | t | t | t | f | f | f | f - | 9.1.2.3/8 | 10/8 | t | t | f | f | f | t | f | f | f | f + ten | i | c | lt | le | eq | ge | gt | ne | sb | sbe | sup | spe +-----+------------------+----------------+----+----+----+----+----+----+----+-----+-----+----- + | 192.168.1.226/24 | 192.168.1.0/24 | f | f | f | t | t | t | f | t | f | t + | 192.168.1.226 | 192.168.1.0/24 | f | f | f | t | t | t | t | t | f | f + | 10.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | t | f | t + | 10.1.2.3/8 | 10.0.0.0/32 | t | t | f | f | f | t | f | f | t | t + | 10.1.2.3 | 10.1.2.3/32 | f | t | t | t | f | f | f | t | f | t + | 10.1.2.3/24 | 10.1.2.0/24 | f | f | f | t | t | t | f | t | f | t + | 10.1.2.3/16 | 10.1.0.0/16 | f | f | f | t | t | t | f | t | f | t + | 10.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | t | f | t + | 11.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | f | f | f + | 9.1.2.3/8 | 10.0.0.0/8 | t | t | f | f | f | t | f | f | f | f (10 rows) -- 2.40.0