2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-09-03 15:56:56 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "ip_range.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/status.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2023-01-27 23:08:43 +00:00
|
|
|
#include <set>
|
2021-04-14 15:07:06 +00:00
|
|
|
#include <vector>
|
2019-09-03 15:56:56 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace net
|
|
|
|
{
|
|
|
|
/// a container that maps an ip range to a value that allows you to lookup
|
|
|
|
/// key by range hit
|
2022-02-04 20:00:10 +00:00
|
|
|
///
|
|
|
|
/// in the future we should do some kind of magic shit to ensure near constant time for lookups
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Value_t>
|
2019-09-03 15:56:56 +00:00
|
|
|
struct IPRangeMap
|
|
|
|
{
|
|
|
|
using Range_t = IPRange;
|
2020-04-07 18:38:56 +00:00
|
|
|
using IP_t = Range_t::Addr_t;
|
2019-09-03 15:56:56 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using Entry_t = std::pair<Range_t, Value_t>;
|
2021-04-14 15:07:06 +00:00
|
|
|
using Container_t = std::vector<Entry_t>;
|
2019-09-03 15:56:56 +00:00
|
|
|
|
|
|
|
/// get a set of all values
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<Value_t>
|
2019-09-03 15:56:56 +00:00
|
|
|
Values() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<Value_t> all;
|
|
|
|
for (const auto& entry : m_Entries)
|
2019-09-03 15:56:56 +00:00
|
|
|
all.insert(entry.second);
|
|
|
|
return all;
|
|
|
|
}
|
|
|
|
|
2020-09-24 00:28:38 +00:00
|
|
|
bool
|
|
|
|
Empty() const
|
|
|
|
{
|
|
|
|
return m_Entries.empty();
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
bool
|
|
|
|
ContainsValue(const Value_t& val) const
|
|
|
|
{
|
|
|
|
for (const auto& entry : m_Entries)
|
|
|
|
{
|
|
|
|
if (entry.second == val)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:56:56 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachValue(std::function<void(const Value_t&)> functor) const
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& entry : m_Entries)
|
2019-09-03 15:56:56 +00:00
|
|
|
functor(entry.second);
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
template <typename Visit_t>
|
|
|
|
void
|
|
|
|
ForEachEntry(Visit_t visit) const
|
|
|
|
{
|
2020-07-01 14:14:45 +00:00
|
|
|
for (const auto& [range, value] : m_Entries)
|
|
|
|
visit(range, value);
|
2020-06-24 13:24:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 14:34:27 +00:00
|
|
|
/// convert all values into type T using a transformer
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T, typename Transformer>
|
|
|
|
std::set<T>
|
2019-09-03 15:56:56 +00:00
|
|
|
TransformValues(Transformer transform) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<T> transformed;
|
|
|
|
for (const auto& entry : m_Entries)
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
|
|
|
T val = transform(entry.second);
|
|
|
|
transformed.insert(std::move(val));
|
|
|
|
}
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:39:01 +00:00
|
|
|
// get a value for this exact range
|
|
|
|
std::optional<Value_t>
|
|
|
|
GetExact(Range_t range) const
|
|
|
|
{
|
|
|
|
for (const auto& [r, value] : m_Entries)
|
|
|
|
{
|
|
|
|
if (r == range)
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:07:06 +00:00
|
|
|
/// return a set of all entries who's range contains this IP
|
|
|
|
std::set<Entry_t>
|
|
|
|
FindAllEntries(const IP_t& addr) const
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
2021-04-14 15:07:06 +00:00
|
|
|
std::set<Entry_t> found;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& entry : m_Entries)
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (entry.first.Contains(addr))
|
2021-04-14 15:07:06 +00:00
|
|
|
found.insert(entry);
|
2019-09-03 15:56:56 +00:00
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CompareEntry
|
|
|
|
{
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
operator()(const Entry_t& left, const Entry_t& right) const
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
|
|
|
return left.first < right.first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Insert(const Range_t& addr, const Value_t& val)
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
2021-04-14 15:07:06 +00:00
|
|
|
m_Entries.emplace_back(addr, val);
|
2019-09-03 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
template <typename Visit_t>
|
|
|
|
void
|
|
|
|
RemoveIf(Visit_t visit)
|
|
|
|
{
|
|
|
|
auto itr = m_Entries.begin();
|
|
|
|
while (itr != m_Entries.end())
|
|
|
|
{
|
|
|
|
if (visit(*itr))
|
|
|
|
itr = m_Entries.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 22:22:22 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const
|
|
|
|
{
|
|
|
|
util::StatusObject obj;
|
|
|
|
for (const auto& [range, value] : m_Entries)
|
|
|
|
{
|
|
|
|
obj[range.ToString()] = value.ToString();
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:56:56 +00:00
|
|
|
private:
|
|
|
|
Container_t m_Entries;
|
|
|
|
};
|
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|