From 2033d6bb8f3ed4cf92708c342fb234c44dc53d65 Mon Sep 17 00:00:00 2001 From: dr7ana Date: Tue, 14 Jan 2025 14:19:24 -0800 Subject: [PATCH] squash --- llarp/contact/relay_contact_remote.cpp | 4 +- llarp/contact/tag.hpp | 2 +- llarp/crypto/crypto.cpp | 4 +- llarp/crypto/crypto.hpp | 4 +- llarp/crypto/types.cpp | 4 +- llarp/crypto/types.hpp | 12 +-- llarp/dns/message.cpp | 2 +- llarp/dns/message.hpp | 4 +- llarp/dns/question.hpp | 2 +- llarp/dns/rr.hpp | 2 +- llarp/dns/serialize.hpp | 2 +- llarp/dns/server.hpp | 2 +- llarp/handlers/session.cpp | 45 +++++----- llarp/handlers/tun.cpp | 8 +- llarp/handlers/tun.hpp | 13 +-- llarp/link/link_manager.cpp | 5 +- llarp/messages/exit.hpp | 4 +- llarp/net/ip_headers.hpp | 47 ++++++----- llarp/net/ip_packet.cpp | 7 +- llarp/net/ip_packet.hpp | 5 +- llarp/net/policy.cpp | 110 +++++++++---------------- llarp/net/policy.hpp | 41 +++++++-- llarp/util/buffer.hpp | 57 ++----------- llarp/vpn/packet_router.cpp | 20 +++-- llarp/vpn/packet_router.hpp | 2 +- 25 files changed, 181 insertions(+), 227 deletions(-) diff --git a/llarp/contact/relay_contact_remote.cpp b/llarp/contact/relay_contact_remote.cpp index 5c31d95fac..6bffe3541f 100644 --- a/llarp/contact/relay_contact_remote.cpp +++ b/llarp/contact/relay_contact_remote.cpp @@ -32,7 +32,7 @@ namespace llarp log::trace(logcat, "{}B read from file (path:{})!", nread, fname); _payload.resize(nread); - oxenc::bt_dict_consumer btdc{_payload}; + oxenc::bt_dict_consumer btdc{ustring_view{_payload}}; bt_load(btdc); bt_verify(btdc); } @@ -47,7 +47,7 @@ namespace llarp bool RemoteRC::verify() const { - oxenc::bt_dict_consumer btdc{_payload}; + oxenc::bt_dict_consumer btdc{ustring_view{_payload}}; bt_verify(btdc); return true; } diff --git a/llarp/contact/tag.hpp b/llarp/contact/tag.hpp index fb111233f4..ce071ce52d 100644 --- a/llarp/contact/tag.hpp +++ b/llarp/contact/tag.hpp @@ -1,6 +1,6 @@ #pragma once -#include +// #include #include namespace llarp diff --git a/llarp/crypto/crypto.cpp b/llarp/crypto/crypto.cpp index dc2d96d3a5..89843b6dfd 100644 --- a/llarp/crypto/crypto.cpp +++ b/llarp/crypto/crypto.cpp @@ -238,7 +238,7 @@ namespace llarp SharedSecret& secret, const SymmNonce& nonce, const RouterID& remote, - uspan payload) + std::span payload) { // derive shared key if (!crypto::dh_client(secret, remote, shared_key, nonce)) @@ -262,7 +262,7 @@ namespace llarp SharedSecret& shared, const PubKey& remote, const SymmNonce& nonce, - uspan encrypted) + std::span encrypted) { // derive shared secret using shared secret and our secret key (and nonce) if (!crypto::dh_server(shared, remote, local_sk, nonce)) diff --git a/llarp/crypto/crypto.hpp b/llarp/crypto/crypto.hpp index 47c88f6e26..5a35ffc396 100644 --- a/llarp/crypto/crypto.hpp +++ b/llarp/crypto/crypto.hpp @@ -68,7 +68,7 @@ namespace llarp SharedSecret& secret, const SymmNonce& nonce, const RouterID& remote, - uspan payload); + std::span payload); // void derive_encrypt_outer_wrapping( // const Ed25519SecretKey& shared_key, @@ -85,7 +85,7 @@ namespace llarp SharedSecret& shared, const PubKey& remote, const SymmNonce& nonce, - uspan encrypted); + std::span encrypted); std::array make_scalar(const PubKey& k, uint64_t domain); diff --git a/llarp/crypto/types.cpp b/llarp/crypto/types.cpp index 6246a88d38..80cd8ab916 100644 --- a/llarp/crypto/types.cpp +++ b/llarp/crypto/types.cpp @@ -145,14 +145,14 @@ namespace llarp throw std::runtime_error{"Server DH failed -- should this even ever happen?"}; } - void shared_kx_data::encrypt(uspan data) + void shared_kx_data::encrypt(std::span data) { if (!crypto::xchacha20(data.data(), data.size(), shared_secret, nonce)) throw std::runtime_error{"xchacha20 encryption failed -- should this even ever happen?"}; } // identical methods, separated for clarity of use/logging for now - void shared_kx_data::decrypt(uspan data) + void shared_kx_data::decrypt(std::span data) { if (!crypto::xchacha20(data.data(), data.size(), shared_secret, nonce)) throw std::runtime_error{"xchacha20 decryption failed -- should this even ever happen?"}; diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index 5c7f235d91..382c891af9 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -67,13 +67,13 @@ namespace llarp explicit Ed25519PrivateData(const AlignedBuffer<64>& key_and_hash) : AlignedBuffer<64>(key_and_hash) {} // Returns writeable access to the 32-byte Ed25519 Private Scalar - uspan scalar() { return {data(), 32}; } + std::span scalar() { return {data(), 32}; } // Returns readable access to the 32-byte Ed25519 Private Scalar - const_uspan scalar() const { return {data(), 32}; } + uspan scalar() const { return {data(), 32}; } // Returns writeable access to the 32-byte Ed25519 Signing Hash - uspan signing_hash() { return {data() + 32, 32}; } + std::span signing_hash() { return {data() + 32, 32}; } // Returns readable access to the 32-byte Ed25519 Signing Hash - const_uspan signing_hash() const { return {data() + 32, 32}; } + uspan signing_hash() const { return {data() + 32, 32}; } PubKey to_pubkey() const; @@ -125,9 +125,9 @@ namespace llarp void server_dh(const Ed25519SecretKey& local_sk); - void encrypt(uspan data); + void encrypt(std::span data); - void decrypt(uspan enc); + void decrypt(std::span enc); }; } // namespace llarp diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index 22a7caba69..2f48e0937f 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -46,7 +46,7 @@ namespace llarp::dns return true; } - bool MessageHeader::decode(std::span b) + bool MessageHeader::decode(std::span b) { std::memcpy(_data.data(), b.data(), sizeof(_data)); for (auto& d : _data) diff --git a/llarp/dns/message.hpp b/llarp/dns/message.hpp index e489111edb..7492d8836b 100644 --- a/llarp/dns/message.hpp +++ b/llarp/dns/message.hpp @@ -39,7 +39,7 @@ namespace llarp bool Decode(llarp_buffer_t* buf) override; - bool decode(std::span b) override; + bool decode(std::span b) override; nlohmann::json ToJSON() const override; @@ -80,7 +80,7 @@ namespace llarp bool Decode(llarp_buffer_t* buf) override; - bool decode(std::span /* b */) override { return {}; }; // TODO: + bool decode(std::span /* b */) override { return {}; }; // TODO: // Wrapper around Encode that encodes into a new buffer and returns it std::vector to_buffer() const; diff --git a/llarp/dns/question.hpp b/llarp/dns/question.hpp index a087d2afb9..43bb674efd 100644 --- a/llarp/dns/question.hpp +++ b/llarp/dns/question.hpp @@ -21,7 +21,7 @@ namespace llarp::dns bool Decode(llarp_buffer_t* buf) override; - bool decode(std::span /* b */) override { return {}; } + bool decode(std::span /* b */) override { return {}; } std::string to_string() const; diff --git a/llarp/dns/rr.hpp b/llarp/dns/rr.hpp index be7e5488b0..e2b3e2e8af 100644 --- a/llarp/dns/rr.hpp +++ b/llarp/dns/rr.hpp @@ -25,7 +25,7 @@ namespace llarp::dns bool Decode(llarp_buffer_t* buf) override; - bool decode(std::span /* b */) override { return {}; }; + bool decode(std::span /* b */) override { return {}; }; nlohmann::json ToJSON() const override; diff --git a/llarp/dns/serialize.hpp b/llarp/dns/serialize.hpp index 5020d2b2bf..d6505b0115 100644 --- a/llarp/dns/serialize.hpp +++ b/llarp/dns/serialize.hpp @@ -19,7 +19,7 @@ namespace llarp::dns /// decode entity from buffer virtual bool Decode(llarp_buffer_t* buf) = 0; - virtual bool decode(std::span b) = 0; + virtual bool decode(std::span b) = 0; /// convert this whatever into json virtual nlohmann::json ToJSON() const = 0; diff --git a/llarp/dns/server.hpp b/llarp/dns/server.hpp index 471cf2c211..f06d68b16d 100644 --- a/llarp/dns/server.hpp +++ b/llarp/dns/server.hpp @@ -56,7 +56,7 @@ namespace llarp::dns virtual void send_to( const oxen::quic::Address& to, const oxen::quic::Address& from, std::vector data) const { - send_to(to, from, IPPacket{data.data(), data.size()}); + send_to(to, from, IPPacket{std::move(data)}); } /// stop reading packets and end operation diff --git a/llarp/handlers/session.cpp b/llarp/handlers/session.cpp index ab59cde7a6..e561a0b10c 100644 --- a/llarp/handlers/session.cpp +++ b/llarp/handlers/session.cpp @@ -137,7 +137,7 @@ namespace llarp::handlers update_and_publish_localcc(get_current_client_intros(), _srv_records); } - // static std::atomic testnet_trigger = false; + static std::atomic testnet_trigger = false; void SessionEndpoint::start_tickers() { @@ -152,25 +152,24 @@ namespace llarp::handlers }, true); - // if (not testnet_trigger) - // { - // testnet_trigger = true; - - // _router.loop()->call_later(5s, [this]() { - // try - // { - // RouterID cpk{oxenc::from_base32z("4g96taie3et7dbkumk5x6rycskaxxsgjeiunpe61469z1gsbfkuo")}; - // 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(5s, [this]() { + try + { + RouterID cpk{oxenc::from_base32z("acit6x8kwxdehpkzrpunw5nb8mf4w5u8tn3ojmxit9rpnhhhp81y")}; + 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..."); @@ -630,7 +629,11 @@ namespace llarp::handlers if (auto maybe_ip = _router.tun_endpoint()->map_session_to_local_ip(session->remote())) { log::info( - logcat, "TUN device successfully routing session to remote: {}", session->remote()); + logcat, + "TUN device successfully routing session (remote: {}) via local ip: {}", + session->remote(), + std::holds_alternative(*maybe_ip) ? std::get(*maybe_ip).to_string() + : std::get(*maybe_ip).to_string()); return hook(*maybe_ip); } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 661132ce8a..c2d60e7c1f 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -289,7 +289,7 @@ namespace llarp::handlers auto& net_conf = _router.config()->network; - _traffic_policy = net_conf.traffic_policy; + _exit_policy = net_conf.traffic_policy; _base_ipv6_range = net_conf._base_ipv6_range; if (net_conf.path_alignment_timeout) @@ -947,7 +947,7 @@ namespace llarp::handlers ip_v src, dest; auto pkt_is_ipv4 = pkt.is_ipv4(); - log::trace(logcat, "outbound packet: {}: {}", pkt.info_line(), buffer_printer{pkt.uview()}); + log::debug(logcat, "outbound packet: {}: {}", pkt.info_line(), buffer_printer{pkt.uview()}); if (pkt_is_ipv4) { @@ -1074,7 +1074,7 @@ namespace llarp::handlers { log::info(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)) + if (not _exit_policy->allow_ip_traffic(pkt)) return false; if (pkt_is_ipv4) @@ -1114,7 +1114,7 @@ namespace llarp::handlers bool TunEndpoint::is_allowing_traffic(const IPPacket& pkt) const { - return _traffic_policy ? _traffic_policy->allow_ip_traffic(pkt) : true; + return _exit_policy ? _exit_policy->allow_ip_traffic(pkt) : true; } bool TunEndpoint::has_mapping_to_remote(const NetworkAddress& addr) const diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 81abff2d5e..f33d4f090b 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -54,7 +54,7 @@ namespace llarp::handlers std::shared_ptr _packet_router; - std::optional _traffic_policy = std::nullopt; + std::optional _exit_policy = std::nullopt; /// a file to load / store the ephemeral address map to std::optional _persisting_addr_file = std::nullopt; @@ -125,7 +125,7 @@ namespace llarp::handlers bool has_if_addr() const { return true; } - std::optional get_traffic_policy() const { return _traffic_policy; } + std::optional get_traffic_policy() const { return _exit_policy; } std::chrono::milliseconds get_path_alignment_timeout() const { return _path_alignment_timeout; } @@ -144,15 +144,6 @@ namespace llarp::handlers void start_poller(); - // protected: - struct WritePacket - { - uint64_t seqno; - IPPacket pkt; - - bool operator>(const WritePacket& other) const { return seqno > other.seqno; } - }; - // Stores assigned IP's for each session in/out of this lokinet instance // - Reserved local addresses is directly pre-loaded from config // - Persisting address map is directly pre-loaded from config diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index 9f09d4a2f9..e21ba885be 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -24,7 +24,7 @@ namespace llarp { static auto logcat = llarp::log::Cat("lquic"); - static constexpr auto static_shared_key = "Lokinet static shared secret key"_usv; + static constexpr auto static_shared_key = "Lokinet static shared secret key"_usp; static static_secret make_static_secret(const Ed25519SecretKey& sk) { @@ -1470,7 +1470,8 @@ namespace llarp try { - std::tie(hop_id, nonce, payload) = ONION::deserialize_hop(oxenc::bt_dict_consumer{message}); + std::tie(hop_id, nonce, payload) = + ONION::deserialize_hop(oxenc::bt_dict_consumer{bstring_view{message}}); } catch (const std::exception& e) { diff --git a/llarp/messages/exit.hpp b/llarp/messages/exit.hpp index f288fdf09f..f911b501f3 100644 --- a/llarp/messages/exit.hpp +++ b/llarp/messages/exit.hpp @@ -9,9 +9,7 @@ namespace llarp { /* TODO: - - ADD PUBKEY FIELD OR AT LEAST SEE WHY LINKMANAGER::HANDLE_OBTAIN_EXIT() LOOKS FOR ONE - - - change these parameters to ustringviews and ustrings where needed after bumping oxenc + - change these parameters to uspans where needed after bumping oxenc - change std::string sig(64, '\0') --> std::array sig */ diff --git a/llarp/net/ip_headers.hpp b/llarp/net/ip_headers.hpp index 33d6d315a0..3e77adc906 100644 --- a/llarp/net/ip_headers.hpp +++ b/llarp/net/ip_headers.hpp @@ -2,6 +2,8 @@ #include "utils.hpp" +#include + namespace llarp { struct ip_header @@ -26,25 +28,32 @@ namespace llarp static_assert(sizeof(ip_header) == 20); - struct ipv6_header2 - { -#if __BYTE_ORDER == __LITTLE_ENDIAN - uint32_t flow_label : 20; - uint8_t traffic_class; - uint8_t version : 4; -#else - uint8_t version : 4; - uint8_t traffic_class : 8; - uint32_t flow_label : 20; -#endif - // uint16_t pload_len; // payload length - // uint8_t nxt_hdr; // next header (protocol) - // uint8_t hop_limit; - // in6_addr src; - // in6_addr dest; - }; + // TODO: WIP + // struct ipv6_header2 + // { + // private: + // std::array preamble; + + // // #if __BYTE_ORDER == __LITTLE_ENDIAN + // // uint32_t flow_label : 20; + // // uint8_t traffic_class; + // // uint8_t version : 4; + // // #else + // // uint8_t version : 4; + // // uint8_t traffic_class : 8; + // // uint32_t flow_label : 20; + // // #endif + // uint16_t pload_len; // payload length + // uint8_t nxt_hdr; // next header (protocol) + // uint8_t hop_limit; + // in6_addr src; + // in6_addr dest; + + // public: + + // }; - // static_assert(sizeof(ipv6_header2) == 40); + // static_assert(sizeof(ipv6_header2) == 40); struct ipv6_header { @@ -68,7 +77,7 @@ namespace llarp in6_addr dest; /// Returns the flowlabel (stored in network order) in HOST ORDER - uint32_t get_flowlabel() const { return ntohl(preamble.flowlabel & htonl(ipv6_flowlabel_mask)); } + uint32_t get_flowlabel() const { return oxenc::big_to_host(preamble.flowlabel & htonl(ipv6_flowlabel_mask)); } /// Sets a flowlabel in network order. Takes in a label in HOST ORDER void set_flowlabel(uint32_t label) diff --git a/llarp/net/ip_packet.cpp b/llarp/net/ip_packet.cpp index 4eba4ca3d3..8d7006de92 100644 --- a/llarp/net/ip_packet.cpp +++ b/llarp/net/ip_packet.cpp @@ -36,8 +36,6 @@ namespace llarp IPPacket::IPPacket(bstring_view data) : IPPacket{reinterpret_cast(data.data()), data.size()} {} - IPPacket::IPPacket(ustring_view data) : IPPacket{data.data(), data.size()} {} - IPPacket::IPPacket(std::vector&& data) : IPPacket{data.data(), data.size()} {} IPPacket::IPPacket(const uint8_t* buf, size_t len) @@ -383,11 +381,12 @@ namespace llarp std::vector IPPacket::give_buffer() { return {_buf}; } - std::string IPPacket::to_string() { return {reinterpret_cast(data()), size()}; } + std::string IPPacket::to_string() const { return {reinterpret_cast(data()), size()}; } std::string IPPacket::info_line() const { - return "IPPacket:[src={} | dest={} | size={}]"_format(_src_addr, _dst_addr, size()); + return "IPPacket:[type={} | src={} | dest={} | size={}]"_format( + ip_protocol_name(_proto), _src_addr, _dst_addr, size()); } } // namespace llarp diff --git a/llarp/net/ip_packet.hpp b/llarp/net/ip_packet.hpp index 0e4789797a..52fc5b51b9 100644 --- a/llarp/net/ip_packet.hpp +++ b/llarp/net/ip_packet.hpp @@ -44,7 +44,7 @@ namespace llarp bool _is_v4{true}; bool _is_udp{false}; - net::IPProtocol _proto; + net::IPProtocol _proto{}; void _init_internals(); @@ -52,7 +52,6 @@ namespace llarp IPPacket() : IPPacket{size_t{0}} {} explicit IPPacket(size_t sz); explicit IPPacket(bstring_view data); - explicit IPPacket(ustring_view data); explicit IPPacket(std::vector&& data); explicit IPPacket(const uint8_t* buf, size_t len); @@ -134,7 +133,7 @@ namespace llarp ustring_view uview() const { return {data(), size()}; } - std::string to_string(); + std::string to_string() const; std::string info_line() const; }; diff --git a/llarp/net/policy.cpp b/llarp/net/policy.cpp index a9f97b4b29..59514a92c6 100644 --- a/llarp/net/policy.cpp +++ b/llarp/net/policy.cpp @@ -33,41 +33,43 @@ namespace llarp::net return static_cast(intVal); } - throw std::invalid_argument{"no such ip protocol: '" + data + "'"}; + throw std::invalid_argument{"Call to ::getprotobyname failed for input: {}"_format(data)}; } - ProtocolInfo::ProtocolInfo(std::string_view data) - { - const auto parts = split(data, "/"); - proto = parse_ip_proto(std::string{parts[0]}); - if (parts.size() == 2) - { - uint16_t port_host{}; - - std::string portStr{parts[1]}; - std::string protoName = ip_proto_str(proto); - - if (const auto* serv = ::getservbyname(portStr.c_str(), protoName.c_str())) - { - port_host = serv->s_port; - } - else if (const auto port_int = std::stoi(portStr); port_int > 0) - { - port_host = port_int; - } - else - throw std::invalid_argument{"invalid port in protocol info: " + portStr}; - port = port_host; - } - else - port = std::nullopt; - } + // ProtocolInfo::ProtocolInfo(std::string_view data) + // { + // const auto parts = split(data, "/"); + // proto = parse_ip_proto(std::string{parts[0]}); + // if (parts.size() == 2) + // { + // uint16_t port_host{}; + + // std::string portStr{parts[1]}; + // std::string protoName = ip_proto_str(proto); + + // if (const auto* serv = ::getservbyname(portStr.c_str(), protoName.c_str())) + // { + // port_host = serv->s_port; + // } + // else if (const auto port_int = std::stoi(portStr); port_int > 0) + // { + // port_host = port_int; + // } + // else + // throw std::invalid_argument{"Invalid port in protocol info: {}"_format(portStr)}; + + // port = port_host; + // } + // else + // port = std::nullopt; + // } bool ProtocolInfo::matches_packet_proto(const IPPacket& pkt) const { return pkt.protocol() == proto; } bool ExitPolicy::allow_ip_traffic(const IPPacket& pkt) const { - log::debug(logcat, "{} called", __PRETTY_FUNCTION__); + log::trace(logcat, "{} called", __PRETTY_FUNCTION__); + if (protocols.empty() and ranges.empty()) return true; @@ -77,22 +79,18 @@ namespace llarp::net return true; } - ipv4 v4 = pkt.dest_ipv4(); - ipv6 v6 = pkt.dest_ipv6(); auto is_ipv4 = pkt.is_ipv4(); + ip_v pkt_ip; + + if (is_ipv4) + pkt_ip = pkt.dest_ipv4(); + else + pkt_ip = pkt.dest_ipv6(); for (const auto& range : ranges) { - if (is_ipv4) - { - if (range.contains(v4)) - return true; - } - else - { - if (range.contains(v6)) - return true; - } + if (range.contains(pkt_ip)) + return true; } return false; @@ -150,11 +148,11 @@ namespace llarp::net } } - ProtocolInfo::ProtocolInfo(std::string buf) + ProtocolInfo::ProtocolInfo(std::string_view buf) { try { - oxenc::bt_list_consumer btlc{std::move(buf)}; + oxenc::bt_list_consumer btlc{buf}; proto = IPProtocol{btlc.consume_integer()}; if (not btlc.is_finished()) @@ -178,7 +176,7 @@ namespace llarp::net while (not sublist.is_finished()) { - protocols.emplace(sublist.consume_string()); + protocols.emplace(sublist.consume_string_view()); } } @@ -239,30 +237,4 @@ namespace llarp::net return true; } - - // nlohmann::json ProtocolInfo::ExtractStatus() const - // { - // nlohmann::json status{ - // {"protocol", static_cast(protocol)}, - // }; - // if (port) - // status["port"] = *port; - // return status; - // } - - // nlohmann::json ExitProtocol::ExtractStatus() const - // { - // std::vector rangesStatus; - // std::transform(ranges.begin(), ranges.end(), std::back_inserter(rangesStatus), [](const auto& range) { - // return range.to_string(); - // }); - - // std::vector protosStatus; - // std::transform(protocols.begin(), protocols.end(), std::back_inserter(protosStatus), [](const auto& proto) { - // return proto.ExtractStatus(); - // }); - - // return nlohmann::json{{"ranges", rangesStatus}, {"protocols", protosStatus}}; - // } - } // namespace llarp::net diff --git a/llarp/net/policy.hpp b/llarp/net/policy.hpp index 1b987dbd81..dc7321c02c 100644 --- a/llarp/net/policy.hpp +++ b/llarp/net/policy.hpp @@ -22,6 +22,7 @@ namespace llarp TCP2QUIC = 1 << 5, }; + // TODO: WIP implementation struct ip_protocol { enum class type : uint8_t @@ -53,6 +54,33 @@ namespace llarp PGM = 0x71, }; + inline constexpr auto ip_protocol_name(IPProtocol p) + { + switch (p) + { + case IPProtocol::ICMP: + return "ICMP"sv; + case IPProtocol::IGMP: + return "IGMP"sv; + case IPProtocol::IPIP: + return "IPIP"sv; + case IPProtocol::TCP: + return "TCP"sv; + case IPProtocol::UDP: + return "UDP"sv; + case IPProtocol::GRE: + return "GRE"sv; + case IPProtocol::ICMP6: + return "ICMP6"sv; + case IPProtocol::OSPF: + return "OSPF"sv; + case IPProtocol::PGM: + return "PGM"sv; + default: + return ""sv; + } + } + /// information about an IP protocol struct ProtocolInfo { @@ -62,7 +90,8 @@ namespace llarp /// the layer 3 port IN HOST ORDER FFS std::optional port{std::nullopt}; - ProtocolInfo(std::string buf); + ProtocolInfo() = default; + ProtocolInfo(std::string_view buf); void bt_encode(oxenc::bt_list_producer& btlp) const; @@ -70,8 +99,7 @@ namespace llarp bool bt_decode(std::string_view buf); - /// returns true if an ip packet looks like it matches this protocol info - /// returns false otherwise + // Compares packet protocol with protocol info bool matches_packet_proto(const IPPacket& pkt) const; auto operator<=>(const ProtocolInfo& other) const @@ -86,9 +114,7 @@ namespace llarp return std::tie(proto, port) < std::tie(other.proto, other.port); } - ProtocolInfo() = default; - - explicit ProtocolInfo(std::string_view spec); + // explicit ProtocolInfo(std::string_view spec); }; /// information about what exit traffic an endpoint will carry @@ -115,8 +141,7 @@ namespace llarp bool operator==(const ExitPolicy& other) const { return (*this <=> other) == 0; } - /// returns true if we allow the traffic in this ip packet - /// returns false otherwise + // Verifies if IPPacket traffic is allowed; return true/false bool allow_ip_traffic(const IPPacket& pkt) const; }; } // namespace net diff --git a/llarp/util/buffer.hpp b/llarp/util/buffer.hpp index 27a0b4b095..ba0b98dd11 100644 --- a/llarp/util/buffer.hpp +++ b/llarp/util/buffer.hpp @@ -21,74 +21,27 @@ namespace llarp { - using const_cspan = oxenc::cspan; - using const_uspan = oxenc::uspan; - using const_span = oxenc::bspan; - - using cspan = std::span; - using uspan = std::span; - using bspan = std::span; + using cspan = oxenc::const_span; + using uspan = oxenc::const_span; + using span = oxenc::const_span; using ustring = std::basic_string; using ustring_view = std::basic_string_view; using bstring = std::basic_string; using bstring_view = std::basic_string_view; - namespace detail - { - template - struct bsv_literal - { - consteval bsv_literal(const char (&s)[N]) - { - for (size_t i = 0; i < N; i++) - str[i] = static_cast(s[i]); - } - std::byte str[N]; // we keep the null on the end, in case you pass .data() to a C func - using size = std::integral_constant; - }; - template - struct usv_literal - { - consteval usv_literal(const char (&s)[N]) - { - for (size_t i = 0; i < N; i++) - str[i] = static_cast(s[i]); - } - unsigned char str[N]; // we keep the null on the end, in case you pass .data() to a C func - using size = std::integral_constant; - }; - } // namespace detail - inline ustring operator""_us(const char* str, size_t len) noexcept { return {reinterpret_cast(str), len}; } - template - constexpr ustring_view operator""_usv() noexcept - { - return {UStr.str, decltype(UStr)::size::value}; - } - - template - constexpr bstring_view operator""_bsv() - { - return {BStr.str, decltype(BStr)::size::value}; - } - - inline bstring operator""_bs(const char* str, size_t len) noexcept - { - return {reinterpret_cast(str), len}; - } - // Helper function to switch between string_view and ustring_view inline ustring_view to_usv(std::string_view v) { return {reinterpret_cast(v.data()), v.size()}; } template - inline uspan to_uspan(std::basic_string& v) + inline std::span to_uspan(std::basic_string& v) { - return uspan{reinterpret_cast(v.data()), v.size()}; + return std::span{reinterpret_cast(v.data()), v.size()}; } } // namespace llarp diff --git a/llarp/vpn/packet_router.cpp b/llarp/vpn/packet_router.cpp index 111d93be03..37b2d62993 100644 --- a/llarp/vpn/packet_router.cpp +++ b/llarp/vpn/packet_router.cpp @@ -4,7 +4,7 @@ namespace llarp::vpn { - static auto logcat = log::Cat("ip_packet"); + static auto logcat = log::Cat("packet_router"); struct UDPPacketHandler : public Layer4Handler { @@ -16,6 +16,7 @@ namespace llarp::vpn void add_sub_handler(uint16_t localport, ip_pkt_hook handler) override { _port_mapped_handlers.emplace(localport, std::move(handler)); + log::debug(logcat, "UDP packet sub-handler registered for local port {}", localport); } void handle_ip_packet(IPPacket pkt) override @@ -53,7 +54,7 @@ namespace llarp::vpn PacketRouter::PacketRouter(ip_pkt_hook baseHandler) : _handler{std::move(baseHandler)} {} - void PacketRouter::handle_ip_packet(IPPacket pkt) + void PacketRouter::handle_ip_packet(IPPacket pkt) const { if (pkt.is_ipv4()) log::trace(logcat, "ipv4 pkt: {}", pkt.info_line()); @@ -63,7 +64,7 @@ namespace llarp::vpn return _handler(std::move(pkt)); auto proto = pkt.protocol(); - if (const auto itr = _ip_proto_handler.find(proto); itr != _ip_proto_handler.end()) + if (auto itr = _ip_proto_handler.find(proto); itr != _ip_proto_handler.end()) itr->second->handle_ip_packet(std::move(pkt)); else _handler(std::move(pkt)); @@ -71,11 +72,14 @@ namespace llarp::vpn void PacketRouter::add_udp_handler(uint16_t localport, ip_pkt_hook func) { - if (_ip_proto_handler.find(net::IPProtocol::UDP) == _ip_proto_handler.end()) - { - _ip_proto_handler.emplace(net::IPProtocol::UDP, std::make_unique(_handler)); - } - _ip_proto_handler[net::IPProtocol::UDP]->add_sub_handler(localport, func); + auto [it, b] = _ip_proto_handler.try_emplace(net::IPProtocol::UDP, nullptr); + + if (b) + it->second = std::make_unique(_handler); + else + log::info(logcat, "Packet router already holds registered UDP packet handler!"); + + it->second->add_sub_handler(localport, std::move(func)); } void PacketRouter::add_ip_proto_handler(net::IPProtocol proto, ip_pkt_hook func) diff --git a/llarp/vpn/packet_router.hpp b/llarp/vpn/packet_router.hpp index 8fd03c8435..9c6dfec2de 100644 --- a/llarp/vpn/packet_router.hpp +++ b/llarp/vpn/packet_router.hpp @@ -20,7 +20,7 @@ namespace llarp::vpn explicit PacketRouter(ip_pkt_hook baseHandler); /// feed in an ip packet for handling - void handle_ip_packet(IPPacket pkt); + void handle_ip_packet(IPPacket pkt) const; /// add a non udp packet handler using ip protocol proto void add_ip_proto_handler(net::IPProtocol proto, ip_pkt_hook func);