UI: IpRange: Sort IPv6 addresses

This commit is contained in:
Nodir Temirkhodjaev 2022-05-20 19:15:52 +03:00
parent 1028ac79e0
commit 906de27059
3 changed files with 49 additions and 2 deletions

View File

@ -98,6 +98,12 @@ TEST_F(NetUtilTest, ip6Ranges)
ASSERT_TRUE(ip6Range.fromText("2002::/16")); ASSERT_TRUE(ip6Range.fromText("2002::/16"));
ASSERT_EQ(ip6Range.toText(), QString("2002::-2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n")); ASSERT_EQ(ip6Range.toText(), QString("2002::-2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"));
ASSERT_TRUE(ip6Range.fromText("::2/126\n"
"::1/126\n"));
ASSERT_EQ(ip6Range.toText(),
QString("::1-::3\n"
"::2-::3\n"));
} }
TEST_F(NetUtilTest, taskTasix) TEST_F(NetUtilTest, taskTasix)

View File

@ -7,6 +7,43 @@
#include "netutil.h" #include "netutil.h"
namespace {
int compareIp6(const ip6_addr_t &l, const ip6_addr_t &r)
{
return memcmp(&l, &r, sizeof(ip6_addr_t));
}
void sortIp6Array(ip6_arr_t &array)
{
std::sort(array.begin(), array.end(), compareIp6);
}
void sortIp6PairArray(ip6_arr_t &fromArray, ip6_arr_t &toArray)
{
Q_ASSERT(fromArray.size() == toArray.size());
const int arraySize = fromArray.size();
ip6_pair_arr_t pairArray;
pairArray.reserve(arraySize);
for (int i = 0; i < arraySize; ++i) {
pairArray.append(Ip6Pair { fromArray[i], toArray[i] });
}
std::sort(pairArray.begin(), pairArray.end(),
[](const Ip6Pair &l, const Ip6Pair &r) { return compareIp6(l.from, r.from); });
for (int i = 0; i < arraySize; ++i) {
const Ip6Pair &pair = pairArray[i];
fromArray[i] = pair.from;
toArray[i] = pair.to;
}
}
}
IpRange::IpRange(QObject *parent) : QObject(parent) { } IpRange::IpRange(QObject *parent) : QObject(parent) { }
void IpRange::clear() void IpRange::clear()
@ -88,8 +125,6 @@ bool IpRange::fromText(const QString &text)
bool IpRange::fromList(const StringViewList &list, int emptyNetMask, bool sort) bool IpRange::fromList(const StringViewList &list, int emptyNetMask, bool sort)
{ {
Q_UNUSED(sort); // TODO: Sort parsed IP addresses
clear(); clear();
ip4range_map_t ip4RangeMap; ip4range_map_t ip4RangeMap;
@ -111,6 +146,11 @@ bool IpRange::fromList(const StringViewList &list, int emptyNetMask, bool sort)
fillIp4Range(ip4RangeMap, pair4Size); fillIp4Range(ip4RangeMap, pair4Size);
if (sort) {
sortIp6Array(m_ip6Array);
sortIp6PairArray(m_pair6FromArray, m_pair6ToArray);
}
setErrorLineNo(0); setErrorLineNo(0);
return true; return true;

View File

@ -21,6 +21,7 @@ using Ip6Pair = struct
using ip4range_map_t = QMap<quint32, quint32>; using ip4range_map_t = QMap<quint32, quint32>;
using ip4_arr_t = QVector<quint32>; using ip4_arr_t = QVector<quint32>;
using ip6_pair_arr_t = QVector<Ip6Pair>;
using ip6_arr_t = QVector<ip6_addr_t>; using ip6_arr_t = QVector<ip6_addr_t>;
class IpRange : public QObject class IpRange : public QObject