2019-09-03 15:56:56 +00:00
|
|
|
#ifndef LLARP_NET_IP_RANGE_MAP_HPP
|
|
|
|
#define LLARP_NET_IP_RANGE_MAP_HPP
|
|
|
|
|
|
|
|
#include <net/ip.hpp>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace net
|
|
|
|
{
|
|
|
|
/// a container that maps an ip range to a value that allows you to lookup
|
|
|
|
/// key by range hit
|
|
|
|
/// TODO: 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>;
|
|
|
|
using Container_t = std::forward_list<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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return a set of all values who's range contains this IP
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<Value_t>
|
|
|
|
FindAll(const IP_t& addr) const
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<Value_t> found;
|
|
|
|
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))
|
2019-09-03 15:56:56 +00:00
|
|
|
found.insert(entry.second);
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
m_Entries.emplace_front(addr, val);
|
|
|
|
m_Entries.sort(CompareEntry{});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Container_t m_Entries;
|
|
|
|
};
|
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|