mirror of
https://github.com/tnodir/fort
synced 2024-11-15 13:37:40 +00:00
UI: IpRange: Sort IPv6 addresses
This commit is contained in:
parent
1028ac79e0
commit
906de27059
@ -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)
|
||||||
|
@ -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;
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user