Skip to content

Commit

Permalink
datagramIO
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Dec 7, 2024
1 parent 3e1089a commit 36a5e57
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 118 deletions.
63 changes: 28 additions & 35 deletions llarp/address/ip_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "utils.hpp"

#include <llarp/util/buffer.hpp>
#include <llarp/util/logging/buffer.hpp>
#include <llarp/util/time.hpp>

#include <oxenc/endian.h>
Expand All @@ -27,18 +28,14 @@ namespace llarp

IPPacket::IPPacket(ustring_view data) : IPPacket{data.data(), data.size()} {}

IPPacket::IPPacket(std::vector<uint8_t> data) : IPPacket{data.data(), data.size()} {}
IPPacket::IPPacket(std::vector<uint8_t>&& data) : IPPacket{data.data(), data.size()} {}

IPPacket::IPPacket(const uint8_t* buf, size_t len)
{
if (len < MIN_PACKET_SIZE)
{
_buf.resize(0);
}
else
if (len >= MIN_PACKET_SIZE)
{
_buf.resize(len);
std::copy_n(buf, len, _buf.data());
std::memcpy(_buf.data(), buf, len);
}

_init_internals();
Expand All @@ -65,7 +62,14 @@ namespace llarp
_header = reinterpret_cast<ip_header*>(data());
_v6_header = reinterpret_cast<ipv6_header*>(data());

_is_v4 = _header->protocol == uint8_t{4};
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};

uint16_t src_port =
Expand All @@ -76,20 +80,24 @@ namespace llarp

if (_is_v4)
{
auto src = in_addr{_header->src};
auto dest = in_addr{_header->dest};
auto srcv4 = ipv4{oxenc::big_to_host(_header->src)};
auto dstv4 = ipv4{oxenc::big_to_host(_header->dest)};

log::trace(logcat, "srcv4={}:{}, dstv4={}:{}", srcv4, src_port, dstv4, dest_port);

_src_addr.set_addr(&src);
_dst_addr.set_addr(&dest);
_src_addr = oxen::quic::Address{srcv4, src_port};
_dst_addr = oxen::quic::Address{dstv4, dest_port};
}
else
{
_src_addr.set_addr(&_v6_header->srcaddr);
_dst_addr.set_addr(&_v6_header->dstaddr);
}
auto srcv6 = ipv6{&_v6_header->srcaddr};
auto dstv6 = ipv6{&_v6_header->dstaddr};

log::trace(logcat, "srcv6={}:{}, dstv6={}:{}", srcv6, src_port, dstv6, dest_port);

_src_addr.set_port(src_port);
_dst_addr.set_port(dest_port);
_src_addr = oxen::quic::Address{srcv6, src_port};
_dst_addr = oxen::quic::Address{dstv6, dest_port};
}
}

std::optional<std::pair<const char*, size_t>> IPPacket::l4_data() const
Expand Down Expand Up @@ -283,7 +291,7 @@ namespace llarp
itr += 2;

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

// calculate checksum of ip header
Expand All @@ -302,29 +310,14 @@ namespace llarp
return NetworkPacket{oxen::quic::Path{_src_addr, _dst_addr}, bview()};
}

bool IPPacket::load(ustring_view data)
{
return load(data.data(), data.size());
}

bool IPPacket::load(std::string_view data)
{
return load(reinterpret_cast<const uint8_t*>(data.data()), data.size());
}

bool IPPacket::load(std::vector<uint8_t> data)
{
return load(data.data(), data.size());
}

bool IPPacket::load(const uint8_t* buf, size_t len)
{
if (len < MIN_PACKET_SIZE)
return false;

_buf.clear();
_buf.resize(len);
std::copy_n(buf, len, _buf.data());
std::memcpy(_buf.data(), buf, len);

_init_internals();

Expand Down Expand Up @@ -376,7 +369,7 @@ namespace llarp

std::string IPPacket::info_line() const
{
return "IPPacket (src:{}, dest:{}, size:{})"_format(_src_addr, _dst_addr, size());
return "IPPacket:[src={} | dest={} | size={}]"_format(_src_addr, _dst_addr, size());
}

} // namespace llarp
12 changes: 3 additions & 9 deletions llarp/address/ip_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ namespace llarp
struct IPPacket
{
private:
std::vector<uint8_t> _buf;
std::vector<uint8_t> _buf{};

ip_header* _header{};
ipv6_header* _v6_header{};

oxen::quic::Address _src_addr{};
oxen::quic::Address _dst_addr{};

bool _is_v4{false};
bool _is_v4{true};
bool _is_udp{false};

void _init_internals();
Expand All @@ -50,7 +50,7 @@ namespace llarp
explicit IPPacket(size_t sz);
explicit IPPacket(bstring_view data);
explicit IPPacket(ustring_view data);
explicit IPPacket(std::vector<uint8_t> data);
explicit IPPacket(std::vector<uint8_t>&& data);
explicit IPPacket(const uint8_t* buf, size_t len);

static IPPacket from_netpkt(NetworkPacket pkt);
Expand Down Expand Up @@ -109,12 +109,6 @@ namespace llarp

bool empty() const { return _buf.empty(); }

bool load(ustring_view data);

bool load(std::string_view data);

bool load(std::vector<uint8_t> data);

bool load(const uint8_t* buf, size_t len);

// takes posession of the data
Expand Down
2 changes: 1 addition & 1 deletion llarp/dns/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ namespace llarp::dns
if (parent_ptr)
{
parent_ptr->call(
[self = shared_from_this(), parent_ptr = std::move(parent_ptr), buf = std::move(data)] {
[self = shared_from_this(), parent_ptr = std::move(parent_ptr), buf = std::move(data)]() mutable {
log::trace(
logcat,
"forwarding dns response from libunbound to userland (resolverAddr: {}, "
Expand Down
2 changes: 1 addition & 1 deletion llarp/dns/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ namespace llarp::dns
: QueryJob_Base{query}, src{std::move(source)}, resolver{to_}, asker{from_}
{}

void send_reply(std::vector<uint8_t> buf) override { src->send_to(asker, resolver, IPPacket{buf}); }
void send_reply(std::vector<uint8_t> buf) override { src->send_to(asker, resolver, IPPacket{std::move(buf)}); }
};

/// handler of dns query hooking
Expand Down
52 changes: 22 additions & 30 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ namespace llarp::handlers
update_and_publish_localcc(get_current_client_intros(), _srv_records);
}

static std::atomic<bool> testnet_trigger = false;
// static std::atomic<bool> testnet_trigger = false;

void SessionEndpoint::start_tickers()
{
Expand All @@ -155,24 +155,25 @@ namespace llarp::handlers
},
true);

if (not testnet_trigger)
{
testnet_trigger = true;

_router.loop()->call_later(15s, [this]() {
try
{
RouterID cpk{oxenc::from_base32z("4bfu94cyd5t1976qubcwc3nirarri34o4pxq865xrd4krcs8tzxo")};
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"); });
}
catch (const std::exception& e)
{
log::critical(logcat, "Failed to parse client netaddr: {}", e.what());
}
});
}
// if (not testnet_trigger)
// {
// testnet_trigger = true;

// _router.loop()->call_later(10s, [this]() {
// try
// {
// RouterID cpk{oxenc::from_base32z("midg5wcgtjhkih6wchpc4k67jsce5ei8g7zfu99kdpdgsjfutidy")};
// 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");
// });
// }
// catch (const std::exception& e)
// {
// log::critical(logcat, "Failed to parse client netaddr: {}", e.what());
// }
// });
// }
}
else
log::info(logcat, "SessionEndpoint configured to NOT publish ClientContact...");
Expand Down Expand Up @@ -590,17 +591,8 @@ namespace llarp::handlers

log::trace(logcat, "inner payload: {}", buffer_printer{inner_payload});

// add path-control wrapping for pivot to relay to aligned path
// auto& pivot = path->hops.back();
auto onion_nonce = SymmNonce::make_random();
// crypto::onion(
// reinterpret_cast<unsigned char*>(inner_payload.data()),
// inner_payload.size(),
// pivot.shared,
// onion_nonce,
// onion_nonce);

auto pivot_payload = ONION::serialize_hop(remote_intro.pivot_txid.to_view(), onion_nonce, inner_payload);
auto pivot_payload =
ONION::serialize_hop(remote_intro.pivot_txid.to_view(), SymmNonce::make_random(), inner_payload);
log::trace(logcat, "pivot payload: {}", buffer_printer{pivot_payload});

auto intermediate_payload = PATH::CONTROL::serialize("path_control", std::move(pivot_payload));
Expand Down
2 changes: 2 additions & 0 deletions llarp/handlers/tun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,8 @@ namespace llarp::handlers

auto pkt_is_ipv4 = pkt.is_ipv4();

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

if (pkt_is_ipv4)
{
src = pkt.source_ipv4();
Expand Down
Loading

0 comments on commit 36a5e57

Please sign in to comment.