Skip to content

Commit

Permalink
squash
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Dec 13, 2024
1 parent 73c06de commit 3e14ff4
Show file tree
Hide file tree
Showing 26 changed files with 235 additions and 436 deletions.
38 changes: 28 additions & 10 deletions llarp/address/ip_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace llarp
{
if (_is_ipv4)
{
_base_ip = _addr.to_ipv4().to_base(_mask);
_ip_range = ipv4_range{std::get<ipv4>(_base_ip), _mask};
_max_ip = std::get<ipv4_range>(_ip_range).max_ip();
_ip_net = _addr.to_ipv4() % _mask;
_base_ip = _ipv4_range().ip;
_max_ip = _ipv4_net().max_ip();
}
else
{
_base_ip = _addr.to_ipv6().to_base(_mask);
_ip_range = ipv6_range{std::get<ipv6>(_base_ip), _mask};
_max_ip = std::get<ipv6_range>(_ip_range).max_ip();
_ip_net = _addr.to_ipv6() % _mask;
_base_ip = _ipv6_range().ip;
_max_ip = _ipv6_net().max_ip();
}
}

Expand Down Expand Up @@ -82,16 +82,34 @@ namespace llarp
return is_ipv4() ? _contains(std::get<ipv4>(other)) : _contains(std::get<ipv6>(other));
}

bool IPRange::contains(const ip_net_v& other) const
{
if (is_ipv4() ^ std::holds_alternative<ipv4_net>(other))
return false;

return is_ipv4() ? _contains(std::get<ipv4_net>(other).to_range().ip)
: _contains(std::get<ipv6_net>(other).to_range().ip);
}

ip_v IPRange::net_ip() const
{
if (is_ipv4())
return _ipv4_net().ip;
return _ipv6_net().ip;
}

std::optional<IPRange> IPRange::find_private_range(const std::list<IPRange>& excluding, bool ipv6_enabled)
{
if (excluding.empty())
return std::nullopt;

auto filter = [&excluding](const ip_range_v& range) -> bool {
auto filter = [&excluding](const ip_net_v& range) -> bool {
for (const auto& e : excluding)
if (e == range)
{
if (e.contains(range))
return false;
log::debug(logcat, "{}", std::get<ipv4_range>(range).base);
}
log::trace(logcat, "{}", std::get<ipv4_net>(range).ip);
return true;
};

Expand All @@ -110,7 +128,7 @@ namespace llarp
{
for (size_t n = 0; n < num_ipv6_private; ++n)
{
if (auto v6 = ipv6(0xfd2e, 0x6c6f, 0x6b69, n, 0x0000, 0x0000, 0x0000, 0x0001) / 64; filter(v6))
if (auto v6 = ipv6(0xfd2e, 0x6c6f, 0x6b69, n, 0x0000, 0x0000, 0x0000, 0x0001) % 64; filter(v6))
return v6;
}
}
Expand Down
58 changes: 35 additions & 23 deletions llarp/address/ip_range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ namespace llarp
struct IPRange
{
private:
ip_net_v _ip_net;

oxen::quic::Address _addr;
uint8_t _mask;
bool _is_ipv4;

ip_v _base_ip;
ip_range_v _ip_range;
ip_v _max_ip;

void _init_ip();
Expand All @@ -29,10 +30,13 @@ namespace llarp
bool _contains(const ipv6& other) const;

// getters to DRY out variant access
ipv4_range& _ipv4_range() { return std::get<ipv4_range>(_ip_range); }
const ipv4_range& _ipv4_range() const { return std::get<ipv4_range>(_ip_range); }
ipv6_range& _ipv6_range() { return std::get<ipv6_range>(_ip_range); }
const ipv6_range& _ipv6_range() const { return std::get<ipv6_range>(_ip_range); }
ipv4_net& _ipv4_net() { return std::get<ipv4_net>(_ip_net); }
const ipv4_net& _ipv4_net() const { return std::get<ipv4_net>(_ip_net); }
ipv4_range _ipv4_range() const { return _ipv4_net().to_range(); }

ipv6_net& _ipv6_net() { return std::get<ipv6_net>(_ip_net); }
const ipv6_net& _ipv6_net() const { return std::get<ipv6_net>(_ip_net); }
ipv6_range _ipv6_range() const { return _ipv6_net().to_range(); }

public:
IPRange() : IPRange{oxen::quic::Address{}, 0} {}
Expand All @@ -44,21 +48,21 @@ namespace llarp
_init_ip();
}

IPRange(const ipv4_range& ipv4)
: _addr{ipv4.base},
_mask{ipv4.mask},
IPRange(const ipv4_net& ipv4)
: _ip_net{ipv4},
_addr{_ipv4_net().ip},
_mask{_ipv4_net().mask},
_is_ipv4{true},
_base_ip{ipv4.base},
_ip_range{ipv4},
_max_ip{ipv4.max_ip()}
_base_ip{_ipv4_range().ip},
_max_ip{_ipv4_net().max_ip()}
{}

IPRange(const ipv6_range& ipv6)
: _addr{ipv6.base},
IPRange(const ipv6_net& ipv6)
: _ip_net{ipv6},
_addr{ipv6.ip},
_mask{ipv6.mask},
_is_ipv4{false},
_base_ip{ipv6.base},
_ip_range{ipv6},
_base_ip{ipv6.ip.to_base(_mask)},
_max_ip{ipv6.max_ip()}
{}

Expand All @@ -67,16 +71,19 @@ namespace llarp

void bt_encode(oxenc::bt_list_producer& btlp) const { btlp.append(to_string()); }

std::string to_string() const { return is_ipv4() ? _ipv4_range().to_string() : _ipv6_range().to_string(); }
std::string to_string() const { return is_ipv4() ? _ipv4_net().to_string() : _ipv6_net().to_string(); }

static std::optional<IPRange> from_string(std::string arg);

bool contains(const IPRange& other) const;
bool contains(const ip_v& other) const;
bool contains(const ip_net_v& other) const;

bool is_ipv4() const { return _is_ipv4; }

ip_range_v get_ip_range() const { return _ip_range; }
ip_net_v get_ip_net() const { return _ip_net; }

ip_v net_ip() const;

ip_v base_ip() const { return _base_ip; }

Expand All @@ -85,6 +92,11 @@ namespace llarp
const uint8_t& mask() const { return _mask; }
uint8_t mask() { return _mask; }

oxen::quic::Address base_address() const
{
return is_ipv4() ? oxen::quic::Address{_ipv4_range().ip} : oxen::quic::Address{_ipv6_range().ip};
}

const oxen::quic::Address& address() const { return _addr; }
oxen::quic::Address address() { return _addr; }

Expand All @@ -98,12 +110,12 @@ namespace llarp
return std::tie(_addr, _mask) == std::tie(other._addr, other._mask);
}

bool operator==(const ip_range_v& other) const
bool operator==(const ip_net_v& other) const
{
if (_is_ipv4 and std::holds_alternative<ipv4_range>(other))
return _ipv4_range() == std::get<ipv4_range>(other);
if (not _is_ipv4 and std::holds_alternative<ipv6_range>(other))
return _ipv6_range() == std::get<ipv6_range>(other);
if (_is_ipv4 and std::holds_alternative<ipv4_net>(other))
return _ipv4_net() == std::get<ipv4_net>(other);
if (not _is_ipv4 and std::holds_alternative<ipv6_net>(other))
return _ipv6_net() == std::get<ipv6_net>(other);

return false;
}
Expand Down Expand Up @@ -165,7 +177,7 @@ namespace llarp
IPRangeIterator() = default;

IPRangeIterator(const IPRange& range)
: _ip_range{range}, _is_ipv4{range.is_ipv4()}, _current_ip{range.base_ip()}, _max_ip{range.max_ip()}
: _ip_range{range}, _is_ipv4{range.is_ipv4()}, _current_ip{range.net_ip()}, _max_ip{range.max_ip()}
{}

// Returns the next ip address in the iterating range; returns std::nullopt if range is exhausted
Expand Down
10 changes: 6 additions & 4 deletions llarp/address/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ namespace llarp
using ipv4 = oxen::quic::ipv4;
using ipv6 = oxen::quic::ipv6;
using ip_v = std::variant<ipv4, ipv6>;
using ipv4_range = oxen::quic::ipv4_net;
using ipv6_range = oxen::quic::ipv6_net;
using ip_range_v = std::variant<ipv4_range, ipv6_range>;
using ipv4_range = oxen::quic::ipv4_range;
using ipv6_range = oxen::quic::ipv6_range;
using ipv4_net = oxen::quic::ipv4_net;
using ipv6_net = oxen::quic::ipv6_net;
using ip_net_v = std::variant<ipv4_net, ipv6_net>;

namespace concepts
{
template <typename ip_t>
concept IPType = std::is_same_v<ip_t, ipv4> || std::is_same_v<ip_t, ipv6>;

template <typename ip_range_t>
concept IPRangeType = std::is_same_v<ip_range_t, ipv4_range> || std::is_same_v<ip_range_t, ipv6_range>;
concept IPRangeType = std::is_same_v<ip_range_t, ipv4_net> || std::is_same_v<ip_range_t, ipv6_net>;
} // namespace concepts

using KeyedAddress = oxen::quic::RemoteAddress;
Expand Down
8 changes: 4 additions & 4 deletions llarp/address/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ namespace llarp

inline constexpr size_t num_ipv4_private{272};

inline constexpr std::array<ipv4_range, num_ipv4_private> generate_private_ipv4()
inline constexpr std::array<ipv4_net, num_ipv4_private> generate_private_ipv4()
{
std::array<ipv4_range, num_ipv4_private> ret{};
std::array<ipv4_net, num_ipv4_private> ret{};

for (size_t n = 16; n < 32; ++n)
ret[n - 16] = ipv4(172, n, 0, 1) / 16;
ret[n - 16] = ipv4(172, n, 0, 1) % 16;

for (size_t n = 0; n < 256; ++n)
ret[n + 16] = ipv4(10, n, 0, 1) / 16;
ret[n + 16] = ipv4(10, n, 0, 1) % 16;

return ret;
}
Expand Down
1 change: 1 addition & 0 deletions llarp/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ namespace llarp
// Used when in exit mode; pass down to LocalEndpoint
// std::set<IPRange> _routed_ranges; // moved into traffic_policy!

// TESTNET: move into ExitConfig!
bool enable_route_poker;
bool blackhole_routes;

Expand Down
14 changes: 7 additions & 7 deletions llarp/ev/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace llarp
}

log::info(logcat, "EventTrigger resuming callback iteration...");
self->begin();
self->start();
}
catch (const std::exception& e)
{
Expand All @@ -103,7 +103,7 @@ namespace llarp

if (start_immediately)
{
auto rv = begin();
auto rv = start();
log::debug(logcat, "EventTrigger started {}successfully!", rv ? "" : "un");
}
}
Expand Down Expand Up @@ -134,7 +134,7 @@ namespace llarp
event_add(ev.get(), &_null_tv);
}

bool EventTrigger::halt()
bool EventTrigger::stop()
{
_is_cooling_down = false;
_is_iterating = false;
Expand All @@ -147,7 +147,7 @@ namespace llarp
return ret;
}

bool EventTrigger::begin()
bool EventTrigger::start()
{
_is_cooling_down = false;
_is_iterating = true;
Expand Down Expand Up @@ -197,20 +197,20 @@ namespace llarp
},
this));

log::debug(logcat, "Linux poller configured to watch FD: {}", fd);
log::debug(logcat, "Linux poller configured to watch FD {}", fd);
}

bool LinuxPoller::start()
{
auto rv = event_add(ev.get(), nullptr) == 0;
log::info(logcat, "Linux poller {} watching FD: {}", rv ? "successfully began" : "failed to start", fd);
log::info(logcat, "Linux poller {} watching FD {}", rv ? "successfully began" : "failed to start", fd);
return rv;
}

bool LinuxPoller::stop()
{
auto rv = event_del(ev.get());
log::info(logcat, "Linux poller {} watching FD: {}", rv ? "successfully stopped" : "failed to stop", fd);
log::info(logcat, "Linux poller {} watching FD {}", rv ? "successfully stopped" : "failed to stop", fd);
return rv;
}
} // namespace llarp
4 changes: 2 additions & 2 deletions llarp/ev/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ namespace llarp
~EventTrigger();

// Resumes iterative execution after successfully cooling down or being signalled to stop by the callback
bool begin();
bool start();

// Called by the passed callback to signal that the iterative invocation should STOP
bool halt();
bool stop();

private:
// Invokes the function `f`, incrementing `::current` up to `n` before cooling down
Expand Down
20 changes: 14 additions & 6 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace llarp::handlers
// _router.loop()->call_later(5s, [this]() {
// try
// {
// RouterID cpk{oxenc::from_base32z("mprqiu67f4gr8hb4zx8kuuqmxanmct4b6fp1nkeeruhxx9tqwc7y")};
// RouterID cpk{oxenc::from_base32z("p3thqq8toyidz3ssos7tx6xhsje3zfdkmpdw43gtb8cz7a96yedo")};
// log::info(logcat, "Beginning session init to client: {}", cpk.to_network_address(false));
// _initiate_session(
// NetworkAddress::from_pubkey(cpk, true), [](ip_v) { log::critical(logcat, "FUCK YEAH");
Expand Down Expand Up @@ -431,14 +431,21 @@ namespace llarp::handlers
bool SessionEndpoint::prefigure_session(
NetworkAddress initiator,
SessionTag tag,
HopID remote_pivot_txid,
std::shared_ptr<path::Path> path,
shared_kx_data kx_data,
bool use_tun)
{
bool ret = true;

auto inbound = std::make_shared<session::InboundSession>(
initiator, std::move(path), *this, std::move(tag), use_tun, std::move(kx_data));
initiator,
std::move(path),
*this,
std::move(remote_pivot_txid),
std::move(tag),
use_tun,
std::move(kx_data));

auto [session, _] = _sessions.insert_or_assign(std::move(initiator), std::move(inbound));

Expand Down Expand Up @@ -578,6 +585,7 @@ namespace llarp::handlers
std::tie(inner_payload, kx_data) = InitiateSession::serialize_encrypt(
_router.local_rid(),
remote.router_id(),
path->pivot_txid(),
tag,
remote_intro.pivot_txid,
fetch_auth_token(remote),
Expand All @@ -599,9 +607,9 @@ namespace llarp::handlers
remote,
tag,
path,
remote_pivot_txid = remote_intro.pivot_txid,
hook = std::move(cb),
session_keys = std::move(kx_data),
remote_intro = std::move(remote_intro)](oxen::quic::message m) mutable {
session_keys = std::move(kx_data)](oxen::quic::message m) mutable {
if (m)
{
log::critical(logcat, "Call to InitiateSession succeeded!");
Expand All @@ -610,9 +618,9 @@ namespace llarp::handlers
remote,
*this,
std::move(path),
std::move(remote_pivot_txid),
std::move(tag),
std::move(session_keys),
std::move(remote_intro));
std::move(session_keys));

auto [session, _] = _sessions.insert_or_assign(std::move(remote), std::move(outbound));

Expand Down
1 change: 1 addition & 0 deletions llarp/handlers/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ namespace llarp
bool prefigure_session(
NetworkAddress initiator,
SessionTag tag,
HopID remote_pivot_txid,
std::shared_ptr<path::Path> path,
shared_kx_data kx_data,
bool use_tun);
Expand Down
Loading

0 comments on commit 3e14ff4

Please sign in to comment.