Skip to content

Commit

Permalink
client data message routing
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Dec 11, 2024
1 parent d8d794d commit 2db462d
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 94 deletions.
17 changes: 8 additions & 9 deletions llarp/address/ip_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ namespace llarp
return ret;
}

static const auto v4_header_version = oxenc::host_to_big(uint8_t{4});
static const auto udp_header_proto = oxenc::host_to_big(uint8_t{17});

void IPPacket::_init_internals()
{
_header = reinterpret_cast<ip_header*>(data());
Expand All @@ -65,12 +68,8 @@ namespace llarp
if (_buf.empty())
return;

// log::trace(logcat, "ippkt header: {}", buffer_printer{_buf});
// log::trace(logcat, "ippkt protocol: {}", _header->protocol);
// log::trace(logcat, "ippkt version: {}", _header->version);

_is_v4 = _header->version == oxenc::host_to_big(uint8_t{4});
_is_udp = _header->protocol == uint8_t{17};
_is_v4 = _header->version == v4_header_version;
_is_udp = _header->protocol == udp_header_proto;

uint16_t src_port =
(_is_udp) ? *reinterpret_cast<uint16_t*>(data() + (static_cast<ptrdiff_t>(_header->header_len) * 4)) : 0;
Expand Down Expand Up @@ -126,8 +125,8 @@ namespace llarp

std::basic_string_view<uint16_t> head_u16s{reinterpret_cast<const uint16_t*>(_header), sizeof(ip_header)};
// set new IP addresses
_header->src = src.addr;
_header->dest = dst.addr;
_header->src = oxenc::host_to_big(src.addr);
_header->dest = oxenc::host_to_big(dst.addr);

switch (_header->protocol)
{
Expand Down Expand Up @@ -290,7 +289,7 @@ namespace llarp
oxenc::write_host_as_big<uint16_t>(1500, itr);
itr += 2;

// copy ip header and first 8 bytes of datagram for icmp rject
// copy ip header and first 8 bytes of datagram for icmp reject
std::memcpy(itr, _buf.data(), ip_hdr_sz + ICMP_HEADER_SIZE);
itr += ip_hdr_sz + ICMP_HEADER_SIZE;

Expand Down
3 changes: 2 additions & 1 deletion llarp/address/ip_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ namespace llarp
for (const auto& e : excluding)
if (e == range)
return false;
log::debug(logcat, "{}", std::get<ipv4_range>(range).base);
return true;
};

Expand All @@ -109,7 +110,7 @@ namespace llarp
{
for (size_t n = 0; n < num_ipv6_private; ++n)
{
if (auto v6 = ipv6(0xfd2e, 0x6c6f, 0x6b69, n) / 64; filter(v6))
if (auto v6 = ipv6(0xfd2e, 0x6c6f, 0x6b69, n, 0x0000, 0x0000, 0x0000, 0x0001) / 64; filter(v6))
return v6;
}
}
Expand Down
28 changes: 12 additions & 16 deletions llarp/address/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,29 @@ namespace llarp
return ret;
};

inline constexpr auto DIGITS = "0123456789"sv;
inline constexpr auto PDIGITS = "0123456789."sv;
inline constexpr auto ALDIGITS = "0123456789abcdef:."sv;

inline std::pair<std::string, uint16_t> parse_addr(std::string_view addr, std::optional<uint16_t> default_port)
{
std::pair<std::string, uint16_t> result;
auto &[host, port] = result;

if (auto p = addr.find_last_not_of("0123456789");
if (auto p = addr.find_last_not_of(DIGITS);
p != std::string_view::npos && p + 2 <= addr.size() && addr[p] == ':')
{
if (!parse_int(addr.substr(p + 1), port))
throw std::invalid_argument{"Invalid address: could not parse port"};
addr.remove_suffix(addr.size() - p);
}
else if (default_port)
else if (default_port.has_value()) // use ::has_value() in case default_port is set but is == 0
{
// log::critical(utilcat, "Setting default port for addr parse!");
port = *default_port;
}
else
{
throw std::invalid_argument{"Invalid address: no port was specified and there is no default"};
}
throw std::invalid_argument{
"Invalid address: argument contains no port and no default was specified (input:{})"_format(addr)};

bool had_sq_brackets = false;

Expand All @@ -92,20 +94,14 @@ namespace llarp
had_sq_brackets = true;
}

if (auto p = addr.find_first_not_of("0123456789."); p != std::string_view::npos)
if (auto p = addr.find_first_not_of(PDIGITS); p != std::string_view::npos)
{
if (auto q = addr.find_first_not_of("0123456789abcdef:."); q != std::string_view::npos)
if (auto q = addr.find_first_not_of(ALDIGITS); q != std::string_view::npos)
throw std::invalid_argument{"Invalid address: does not look like IPv4 or IPv6!"};
if (!had_sq_brackets)
throw std::invalid_argument{"Invalid address: IPv6 addresses require [...] square brackets"};
}

// if (addr.empty())
// {
// log::critical(utilcat, "addr is empty, tough titties buddy"); // TESTNET: remove this log please
// // addr = "::";
// }

host = addr;
return result;
}
Expand All @@ -117,10 +113,10 @@ namespace llarp
std::array<ipv4_range, num_ipv4_private> ret{};

for (size_t n = 16; n < 32; ++n)
ret[n - 16] = ipv4(172, n, 0, 0) / 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, 0) / 16;
ret[n + 16] = ipv4(10, n, 0, 1) / 16;

return ret;
}
Expand Down
7 changes: 2 additions & 5 deletions llarp/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,15 +1230,12 @@ namespace llarp
if (auto pos = arg_v.find(':'); pos != arg_v.npos)
{
// host = arg_v.substr(0, pos);
log::critical(logcat, "Parsing input: {}", arg);
std::tie(host, p) = detail::parse_addr(arg_v, DEFAULT_LISTEN_PORT);
log::critical(logcat, "Parsed input = {}:{}", host, p);
}

if (host.empty())
{
log::critical(
logcat, "Host value empty, port:{}{}", p, p == DEFAULT_LISTEN_PORT ? "(DEFAULT PORT)" : "");
log::debug(logcat, "Host value empty, port:{}{}", p, p == DEFAULT_LISTEN_PORT ? "(DEFAULT PORT)" : "");
given_port_only = p != DEFAULT_LISTEN_PORT;
maybe = net_ptr->get_best_public_address(true, p);
}
Expand All @@ -1248,7 +1245,7 @@ namespace llarp
if (maybe and maybe->is_loopback())
throw std::invalid_argument{"{} is a loopback address"_format(arg)};

log::critical(logcat, "parsed address: {}", *maybe);
log::trace(logcat, "parsed address: {}", *maybe);

return maybe;
};
Expand Down
4 changes: 2 additions & 2 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ namespace llarp::handlers
// {
// testnet_trigger = true;

// _router.loop()->call_later(10s, [this]() {
// _router.loop()->call_later(5s, [this]() {
// try
// {
// RouterID cpk{oxenc::from_base32z("6e9wdnd4cj3j3rgc9ze8ctxqj4z976tmu8osbzwgabruabb4u1ky")};
// RouterID cpk{oxenc::from_base32z("mprqiu67f4gr8hb4zx8kuuqmxanmct4b6fp1nkeeruhxx9tqwc7y")};
// 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
56 changes: 26 additions & 30 deletions llarp/handlers/tun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,13 +960,14 @@ namespace llarp::handlers
}
}

// handles an outbound packet going OUT to the network
void TunEndpoint::handle_outbound_packet(IPPacket pkt)
{
ip_v src, dest;

auto pkt_is_ipv4 = pkt.is_ipv4();

log::trace(logcat, "outbound packet is ipv{}", pkt_is_ipv4 ? "4" : "6");
log::debug(logcat, "outbound packet: {}", pkt.info_line());

if (pkt_is_ipv4)
{
Expand All @@ -979,6 +980,8 @@ namespace llarp::handlers
dest = pkt.dest_ipv6();
}

log::debug(logcat, "src:{}, dest:{}", src, dest);

if constexpr (llarp::platform::is_apple)
{
if (ip_equals_address(dest, _local_addr, pkt_is_ipv4))
Expand All @@ -1005,45 +1008,30 @@ namespace llarp::handlers
session->send_path_data_message(std::move(pkt).steal_payload());
}
else
log::warning(logcat, "Could not find session (remote: {}) for outbound packet!", remote);
log::info(logcat, "Could not find session (remote: {}) for outbound packet!", remote);
}
else
log::debug(logcat, "Could not find remote for route {}", pkt.info_line());
}

bool TunEndpoint::obtain_src_for_remote(const NetworkAddress& remote, ip_v& src, bool use_ipv4)
std::optional<ip_v> TunEndpoint::obtain_src_for_remote(const NetworkAddress& remote, bool use_ipv4)
{
// we are receiving traffic from a session to a local exit node
if (auto maybe_src = _local_ip_mapping.get_local_from_remote(remote))
{
if (std::holds_alternative<ipv4>(*maybe_src))
{
if (use_ipv4)
src = *maybe_src;
else
{
auto quicaddr = oxen::quic::Address{std::get<ipv4>(*maybe_src)};
src = quicaddr.to_ipv6();
}
}
else
{
if (use_ipv4)
{
auto quicaddr = oxen::quic::Address{std::get<ipv6>(*maybe_src)};
src = quicaddr.to_ipv4();
}
else
src = *maybe_src;
return *maybe_src;
return oxen::quic::Address{std::get<ipv4>(*maybe_src)}.to_ipv6();
}
}
else
{
log::critical(logcat, "Unable to find local IP for inbound packet from remote: {}", remote);
return false;

if (use_ipv4)
return oxen::quic::Address{std::get<ipv6>(*maybe_src)}.to_ipv4();
return *maybe_src;
}

return true;
log::warning(logcat, "Unable to find src IP for inbound packet from remote: {}", remote);
return std::nullopt;
}

void TunEndpoint::send_packet_to_net_if(IPPacket&& pkt)
Expand All @@ -1058,9 +1046,11 @@ namespace llarp::handlers
else
pkt.update_ipv6_address(std::get<ipv6>(src), std::get<ipv6>(dest));

log::debug(logcat, "Rewritten packet: {}: {}", pkt.info_line(), buffer_printer{pkt.uview()});
send_packet_to_net_if(std::move(pkt));
}

// handles an inbound packet coming IN from the network
bool TunEndpoint::handle_inbound_packet(
IPPacket pkt, NetworkAddress remote, bool is_exit_session, bool is_outbound_session)
{
Expand All @@ -1070,6 +1060,7 @@ namespace llarp::handlers

if (is_exit_session and is_outbound_session)
{
log::debug(logcat, "inbound exit session pkt: {}", pkt.info_line());
// we are receiving traffic from a session to a remote exit node
if (pkt_is_ipv4)
{
Expand All @@ -1088,13 +1079,12 @@ namespace llarp::handlers

if (not maybe_remote)
{
log::critical(
logcat, "Could not find mapping of local IP (ip:{}) for session to remote: {}", src, remote);
log::info(logcat, "Could not find mapping of local IP (ip:{}) for session to remote: {}", src, remote);
return false;
}
if (*maybe_remote != remote)
{
log::critical(
log::info(
logcat,
"Internal mapping of local IP (ip:{}, remote:{}) did not match inbound packet from remote: {}",
src,
Expand All @@ -1107,6 +1097,7 @@ namespace llarp::handlers
{
if (is_exit_session and not is_outbound_session)
{
log::debug(logcat, "inbound exit session pkt: {}", pkt.info_line());
// we are receiving traffic from a session to a local exit node
if (not is_allowing_traffic(pkt))
return false;
Expand All @@ -1118,17 +1109,22 @@ namespace llarp::handlers
}
else
{
log::debug(logcat, "inbound service session pkt: {}", pkt.info_line());
// we are receiving hidden service traffic
if (pkt_is_ipv4)
dest = _local_addr.to_ipv4();
else
dest = _local_ipv6.to_ipv6();
}

if (not obtain_src_for_remote(remote, src, pkt_is_ipv4))
if (auto maybe_src = obtain_src_for_remote(remote, pkt_is_ipv4))
src = std::move(*maybe_src);
else
return false;
}

log::debug(logcat, "src:{}, dest:{}", src, dest);

rewrite_and_send_packet(std::move(pkt), src, dest);

return true;
Expand Down
6 changes: 3 additions & 3 deletions llarp/handlers/tun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ namespace llarp::handlers
void setup_dns();

// INPROGRESS: new API
// Handles an outbound packet going out INTO the network
// Handles an outbound packet going OUT to the network
void handle_outbound_packet(IPPacket pkt);

void rewrite_and_send_packet(IPPacket&& pkt, ip_v src, ip_v dest);

// Handle an inbound packet coming in FROM the network
// Handles an inbound packet coming IN from the network
bool handle_inbound_packet(IPPacket pkt, NetworkAddress remote, bool is_exit_session, bool is_outbound_session);

// Upon session creation, SessionHandler will instruct TunEndpoint to requisition a private IP through which to
Expand Down Expand Up @@ -162,7 +162,7 @@ namespace llarp::handlers
private:
std::optional<ip_v> get_next_local_ip();

bool obtain_src_for_remote(const NetworkAddress& remote, ip_v& src, bool use_ipv4);
std::optional<ip_v> obtain_src_for_remote(const NetworkAddress& remote, bool use_ipv4);

void send_packet_to_net_if(IPPacket&& pkt);
};
Expand Down
Loading

0 comments on commit 2db462d

Please sign in to comment.