UI: IpRange: Allow to wrap addresses by [ ]

This commit is contained in:
Nodir Temirkhodjaev 2024-04-08 13:53:52 +03:00
parent 4fac0dde3e
commit 6780cbd91a
4 changed files with 20 additions and 16 deletions

View File

@ -71,7 +71,10 @@ TEST_F(ConfUtilTest, confWriteRead)
QByteArray buf; QByteArray buf;
const int confIoSize = confUtil.write(conf, nullptr, envManager, 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 // Check the buffer
const char *data = buf.constData() + DriverCommon::confIoConfOff(); const char *data = buf.constData() + DriverCommon::confIoConfOff();

View File

@ -129,8 +129,8 @@ TEST_F(NetUtilTest, ip6Ranges)
ASSERT_TRUE(ipRange.fromText("2002::/16")); ASSERT_TRUE(ipRange.fromText("2002::/16"));
ASSERT_EQ(ipRange.toText(), QString("2002::-2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n")); ASSERT_EQ(ipRange.toText(), QString("2002::-2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"));
ASSERT_TRUE(ipRange.fromText("::2/126\n" ASSERT_TRUE(ipRange.fromText("[::2]/126\n"
"::1/126\n")); "[::1]/126\n"));
ASSERT_EQ(ipRange.toText(), ASSERT_EQ(ipRange.toText(),
QString("::1-::3\n" QString("::1-::3\n"
"::2-::3\n")); "::2-::3\n"));

View File

@ -156,13 +156,9 @@ bool IpRange::fromList(const StringViewList &list, bool sort)
IpRange::ParseError IpRange::parseIpLine( IpRange::ParseError IpRange::parseIpLine(
const QStringView line, ip4range_map_t &ip4RangeMap, int &pair4Size) const QStringView line, ip4range_map_t &ip4RangeMap, int &pair4Size)
{ {
static const QRegularExpression ip4Re(R"(^([\d.]+)\s*([\/-]?)\s*(\S*))"); static const QRegularExpression ipRe(R"(^\[?([A-Fa-f\d:.]+)\]?\s*([\/-]?)\s*(\S*))");
static const QRegularExpression ip6Re(R"(^([A-Fa-f\d:]+)\s*([\/-]?)\s*(\S*))");
const bool isIPv6 = !line.contains('.'); const auto match = StringUtil::match(ipRe, line);
const QRegularExpression &re = isIPv6 ? ip6Re : ip4Re;
const auto match = StringUtil::match(re, line);
if (!match.hasMatch()) { if (!match.hasMatch()) {
setErrorMessage(tr("Bad format")); setErrorMessage(tr("Bad format"));
return ErrorBadFormat; return ErrorBadFormat;
@ -179,6 +175,7 @@ IpRange::ParseError IpRange::parseIpLine(
} }
const char maskSep = sepStr.isEmpty() ? '\0' : sepStr.at(0).toLatin1(); const char maskSep = sepStr.isEmpty() ? '\0' : sepStr.at(0).toLatin1();
const bool isIPv6 = ip.contains(':');
return isIPv6 ? parseIp6Address(ip, mask, maskSep) return isIPv6 ? parseIp6Address(ip, mask, maskSep)
: parseIp4Address(ip, mask, ip4RangeMap, pair4Size, maskSep); : parseIp4Address(ip, mask, ip4RangeMap, pair4Size, maskSep);

View File

@ -171,21 +171,25 @@ QStringList NetUtil::localIpNetworks()
{ {
static QStringList list = { static QStringList list = {
"0.0.0.0/32", // non-routable meta-address "0.0.0.0/32", // non-routable meta-address
"10.0.0.0/8", "10.0.0.0/8", //
"100.64.0.0/10" // for carrier-grade NAT deployment "100.64.0.0/10", // for carrier-grade NAT deployment
"127.0.0.0/8", // Loopback "127.0.0.0/8", // Loopback
"169.254.0.0/16", // if cannot obtain a network address via DHCP "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 "239.255.255.250/32", // IP Multicast for DLNA/UPNP
"255.255.255.255/32", // IP Broadcast "255.255.255.255/32", // IP Broadcast
"::/0", // non-routable meta-address "::/0", // non-routable meta-address
"::/128", "::/128", //
"::1/128", // Localhost "::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::/32", // Global Unique Addresses (GUA) - Routable IPv6 addresses
"2001:20::/28", "2001:20::/28", //
"2001:db8::/32", // Documentation prefix used for examples "2001:db8::/32", // Documentation prefix used for examples
"2002::/16", "2002::/16", //
"fc00::/7", // Unique Local Addresses (ULA) - also known as “Private” IPv6 addresses "fc00::/7", // Unique Local Addresses (ULA) - also known as “Private” IPv6 addresses
"fe80::/10", // Link Local addresses, only valid inside a single broadcast domain "fe80::/10", // Link Local addresses, only valid inside a single broadcast domain
"ff00::/8", // Multicast addresses "ff00::/8", // Multicast addresses