From 6780cbd91a5afe48f43b31819f973254dccb8a6e Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Mon, 8 Apr 2024 13:53:52 +0300 Subject: [PATCH] UI: IpRange: Allow to wrap addresses by [ ] --- src/tests/UtilTest/tst_confutil.h | 5 ++++- src/tests/UtilTest/tst_netutil.h | 4 ++-- src/ui/util/net/iprange.cpp | 9 +++------ src/ui/util/net/netutil.cpp | 18 +++++++++++------- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/tests/UtilTest/tst_confutil.h b/src/tests/UtilTest/tst_confutil.h index 5aa6fe2d..947bcc71 100644 --- a/src/tests/UtilTest/tst_confutil.h +++ b/src/tests/UtilTest/tst_confutil.h @@ -71,7 +71,10 @@ TEST_F(ConfUtilTest, confWriteRead) QByteArray buf; const int confIoSize = confUtil.write(conf, nullptr, envManager, buf); - ASSERT_NE(confIoSize, 0); + if (confIoSize == 0) { + qCritical() << "Error:" << confUtil.errorMessage(); + ASSERT_NE(confIoSize, 0); + } // Check the buffer const char *data = buf.constData() + DriverCommon::confIoConfOff(); diff --git a/src/tests/UtilTest/tst_netutil.h b/src/tests/UtilTest/tst_netutil.h index 3f68c63e..a65148c3 100644 --- a/src/tests/UtilTest/tst_netutil.h +++ b/src/tests/UtilTest/tst_netutil.h @@ -129,8 +129,8 @@ TEST_F(NetUtilTest, ip6Ranges) ASSERT_TRUE(ipRange.fromText("2002::/16")); ASSERT_EQ(ipRange.toText(), QString("2002::-2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n")); - ASSERT_TRUE(ipRange.fromText("::2/126\n" - "::1/126\n")); + ASSERT_TRUE(ipRange.fromText("[::2]/126\n" + "[::1]/126\n")); ASSERT_EQ(ipRange.toText(), QString("::1-::3\n" "::2-::3\n")); diff --git a/src/ui/util/net/iprange.cpp b/src/ui/util/net/iprange.cpp index c84151a8..2eb04669 100644 --- a/src/ui/util/net/iprange.cpp +++ b/src/ui/util/net/iprange.cpp @@ -156,13 +156,9 @@ bool IpRange::fromList(const StringViewList &list, bool sort) IpRange::ParseError IpRange::parseIpLine( const QStringView line, ip4range_map_t &ip4RangeMap, int &pair4Size) { - static const QRegularExpression ip4Re(R"(^([\d.]+)\s*([\/-]?)\s*(\S*))"); - static const QRegularExpression ip6Re(R"(^([A-Fa-f\d:]+)\s*([\/-]?)\s*(\S*))"); + static const QRegularExpression ipRe(R"(^\[?([A-Fa-f\d:.]+)\]?\s*([\/-]?)\s*(\S*))"); - const bool isIPv6 = !line.contains('.'); - const QRegularExpression &re = isIPv6 ? ip6Re : ip4Re; - - const auto match = StringUtil::match(re, line); + const auto match = StringUtil::match(ipRe, line); if (!match.hasMatch()) { setErrorMessage(tr("Bad format")); return ErrorBadFormat; @@ -179,6 +175,7 @@ IpRange::ParseError IpRange::parseIpLine( } const char maskSep = sepStr.isEmpty() ? '\0' : sepStr.at(0).toLatin1(); + const bool isIPv6 = ip.contains(':'); return isIPv6 ? parseIp6Address(ip, mask, maskSep) : parseIp4Address(ip, mask, ip4RangeMap, pair4Size, maskSep); diff --git a/src/ui/util/net/netutil.cpp b/src/ui/util/net/netutil.cpp index 66d249a3..da982b18 100644 --- a/src/ui/util/net/netutil.cpp +++ b/src/ui/util/net/netutil.cpp @@ -171,21 +171,25 @@ QStringList NetUtil::localIpNetworks() { static QStringList list = { "0.0.0.0/32", // non-routable meta-address - "10.0.0.0/8", - "100.64.0.0/10" // for carrier-grade NAT deployment + "10.0.0.0/8", // + "100.64.0.0/10", // for carrier-grade NAT deployment "127.0.0.0/8", // Loopback "169.254.0.0/16", // if cannot obtain a network address via DHCP - "172.16.0.0/12", "192.168.0.0/16", + "172.16.0.0/12", // + "192.168.0.0/16", // "239.255.255.250/32", // IP Multicast for DLNA/UPNP "255.255.255.255/32", // IP Broadcast "::/0", // non-routable meta-address - "::/128", + "::/128", // "::1/128", // Localhost - "::ffff:0:0/96", "::ffff:0:0:0/96", "64:ff9b::/96", "100::/64", + "::ffff:0:0/96", // + "::ffff:0:0:0/96", // + "64:ff9b::/96", // + "100::/64", // "2001::/32", // Global Unique Addresses (GUA) - Routable IPv6 addresses - "2001:20::/28", + "2001:20::/28", // "2001:db8::/32", // Documentation prefix used for examples - "2002::/16", + "2002::/16", // "fc00::/7", // Unique Local Addresses (ULA) - also known as “Private” IPv6 addresses "fe80::/10", // Link Local addresses, only valid inside a single broadcast domain "ff00::/8", // Multicast addresses