From 9d483a12db1b8c1dc373991229838dc234e07549 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 16 Mar 2021 07:56:27 -0400 Subject: [PATCH] * add protocol type to snode traffic * make path::PathSet::SendPacketToRemote know about protocol type --- llarp/exit/session.cpp | 15 +++++++------ llarp/exit/session.hpp | 8 ++++--- llarp/handlers/exit.cpp | 6 ++++-- llarp/handlers/null.hpp | 3 ++- llarp/handlers/tun.cpp | 9 ++++---- llarp/handlers/tun.hpp | 3 ++- llarp/path/pathset.hpp | 3 ++- llarp/routing/transfer_traffic_message.cpp | 4 ++++ llarp/routing/transfer_traffic_message.hpp | 3 +++ llarp/service/endpoint.cpp | 25 +++++++++++++++------- llarp/service/endpoint.hpp | 3 ++- llarp/service/outbound_context.cpp | 5 +++-- llarp/service/outbound_context.hpp | 2 +- pybind/llarp/handlers/pyhandler.hpp | 2 +- 14 files changed, 60 insertions(+), 31 deletions(-) diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 56be7332f..77d191903 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -203,7 +203,8 @@ namespace llarp } bool - BaseSession::QueueUpstreamTraffic(llarp::net::IPPacket pkt, const size_t N) + BaseSession::QueueUpstreamTraffic( + llarp::net::IPPacket pkt, const size_t N, service::ProtocolType t) { const auto pktbuf = pkt.ConstBuffer(); const llarp_buffer_t& buf = pktbuf; @@ -214,6 +215,7 @@ namespace llarp if (queue.size() == 0) { queue.emplace_back(); + queue.back().protocol = t; return queue.back().PutBuffer(buf, m_Counter++); } auto& back = queue.back(); @@ -221,9 +223,10 @@ namespace llarp if (back.Size() + buf.sz > N) { queue.emplace_back(); + queue.back().protocol = t; return queue.back().PutBuffer(buf, m_Counter++); } - + back.protocol = t; return back.PutBuffer(buf, m_Counter++); } @@ -340,23 +343,23 @@ namespace llarp } void - SNodeSession::SendPacketToRemote(const llarp_buffer_t& buf) + SNodeSession::SendPacketToRemote(const llarp_buffer_t& buf, service::ProtocolType t) { net::IPPacket pkt; if (not pkt.Load(buf)) return; pkt.ZeroAddresses(); - QueueUpstreamTraffic(std::move(pkt), llarp::routing::ExitPadSize); + QueueUpstreamTraffic(std::move(pkt), llarp::routing::ExitPadSize, t); } void - ExitSession::SendPacketToRemote(const llarp_buffer_t& buf) + ExitSession::SendPacketToRemote(const llarp_buffer_t& buf, service::ProtocolType t) { net::IPPacket pkt; if (not pkt.Load(buf)) return; pkt.ZeroSourceAddress(); - QueueUpstreamTraffic(std::move(pkt), llarp::routing::ExitPadSize); + QueueUpstreamTraffic(std::move(pkt), llarp::routing::ExitPadSize, t); } } // namespace exit } // namespace llarp diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index 0e3f5b659..0c0e7dba6 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -1,6 +1,7 @@ #pragma once #include "exit_messages.hpp" +#include "service/protocol_type.hpp" #include #include #include @@ -76,7 +77,8 @@ namespace llarp HandlePathBuilt(llarp::path::Path_ptr p) override; bool - QueueUpstreamTraffic(llarp::net::IPPacket pkt, const size_t packSize); + QueueUpstreamTraffic( + llarp::net::IPPacket pkt, const size_t packSize, service::ProtocolType t); /// flush upstream to exit via paths bool @@ -181,7 +183,7 @@ namespace llarp Name() const override; virtual void - SendPacketToRemote(const llarp_buffer_t& pkt) override; + SendPacketToRemote(const llarp_buffer_t& pkt, service::ProtocolType t) override; protected: void @@ -210,7 +212,7 @@ namespace llarp Name() const override; virtual void - SendPacketToRemote(const llarp_buffer_t& pkt) override; + SendPacketToRemote(const llarp_buffer_t& pkt, service::ProtocolType t) override; protected: void diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 5c595547a..26f825991 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -8,6 +8,7 @@ #include #include +#include "service/protocol_type.hpp" namespace llarp { @@ -235,8 +236,9 @@ namespace llarp auto itr = m_SNodeSessions.find(pk); if (itr != m_SNodeSessions.end()) { - if (itr->second->QueueUpstreamTraffic(pkt, routing::ExitPadSize)) - return; + // TODO: quic (?) + itr->second->SendPacketToRemote(pkt.ConstBuffer(), service::ProtocolType::TrafficV4); + return; } } auto tryFlushingTraffic = [&](exit::Endpoint* const ep) -> bool { diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index 0e83a5595..1811b04cc 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include "service/protocol_type.hpp" namespace llarp { @@ -39,7 +40,7 @@ namespace llarp } void - SendPacketToRemote(const llarp_buffer_t&) override{}; + SendPacketToRemote(const llarp_buffer_t&, service::ProtocolType) override{}; huint128_t ObtainIPForAddr(std::variant) override { diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index dd63cb7e7..c342f5c8d 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -973,8 +973,9 @@ namespace llarp sendFunc = std::bind( &TunEndpoint::SendToSNodeOrQueue, this, - itr->second.as_array(), - std::placeholders::_1); + RouterID{itr->second.as_array()}, + std::placeholders::_1, + service::ProtocolType::TrafficV4); } else if (m_state->m_ExitEnabled and src != m_OurIP) { @@ -982,7 +983,7 @@ namespace llarp sendFunc = std::bind( &TunEndpoint::SendToServiceOrQueue, this, - service::Address(itr->second.as_array()), + service::Address{itr->second.as_array()}, std::placeholders::_1, service::ProtocolType::Exit); } @@ -991,7 +992,7 @@ namespace llarp sendFunc = std::bind( &TunEndpoint::SendToServiceOrQueue, this, - service::Address(itr->second.as_array()), + service::Address{itr->second.as_array()}, std::placeholders::_1, pkt.ServiceProtocol()); } diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 5fb5facad..5c039f894 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -15,6 +15,7 @@ #include #include #include +#include "service/protocol_type.hpp" namespace llarp { @@ -40,7 +41,7 @@ namespace llarp Configure(const NetworkConfig& conf, const DnsConfig& dnsConf) override; void - SendPacketToRemote(const llarp_buffer_t&) override{}; + SendPacketToRemote(const llarp_buffer_t&, service::ProtocolType) override{}; std::string GetIfName() const override; diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 5cb787053..88a54a2bb 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -1,6 +1,7 @@ #pragma once #include "path_types.hpp" +#include "service/protocol_type.hpp" #include #include #include @@ -276,7 +277,7 @@ namespace llarp BuildOneAlignedTo(const RouterID endpoint) = 0; virtual void - SendPacketToRemote(const llarp_buffer_t& pkt) = 0; + SendPacketToRemote(const llarp_buffer_t& pkt, service::ProtocolType t) = 0; virtual std::optional> GetHopsForBuild() = 0; diff --git a/llarp/routing/transfer_traffic_message.cpp b/llarp/routing/transfer_traffic_message.cpp index 580e000a7..254288134 100644 --- a/llarp/routing/transfer_traffic_message.cpp +++ b/llarp/routing/transfer_traffic_message.cpp @@ -30,6 +30,8 @@ namespace llarp return false; if (!BEncodeWriteDictMsgType(buf, "A", "I")) return false; + if (!BEncodeWriteDictInt("P", protocol, buf)) + return false; if (!BEncodeWriteDictInt("S", S, buf)) return false; if (!BEncodeWriteDictInt("V", version, buf)) @@ -45,6 +47,8 @@ namespace llarp bool read = false; if (!BEncodeMaybeReadDictInt("S", S, read, key, buf)) return false; + if (!BEncodeMaybeReadDictInt("P", protocol, read, key, buf)) + return false; if (!BEncodeMaybeReadDictInt("V", version, read, key, buf)) return false; if (!BEncodeMaybeReadDictList("X", X, read, key, buf)) diff --git a/llarp/routing/transfer_traffic_message.hpp b/llarp/routing/transfer_traffic_message.hpp index 6cdb0cdb5..82c0b2924 100644 --- a/llarp/routing/transfer_traffic_message.hpp +++ b/llarp/routing/transfer_traffic_message.hpp @@ -2,6 +2,7 @@ #include #include "message.hpp" +#include #include @@ -15,6 +16,7 @@ namespace llarp struct TransferTrafficMessage final : public IMessage { std::vector> X; + service::ProtocolType protocol; size_t _size = 0; void @@ -23,6 +25,7 @@ namespace llarp X.clear(); _size = 0; version = 0; + protocol = service::ProtocolType::TrafficV4; } size_t diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index ee56edfd5..f3c5870ad 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -23,6 +23,7 @@ #include "outbound_context.hpp" #include "protocol.hpp" #include "service/info.hpp" +#include "service/protocol_type.hpp" #include #include #include @@ -1380,22 +1381,30 @@ namespace llarp bool Endpoint::SendTo(ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t) { - // TODO: support snode - ServiceInfo ident{}; - if (not GetSenderFor(tag, ident)) - return false; - return SendToServiceOrQueue(ident.Addr(), pkt, t); + if (auto maybe = GetEndpointWithConvoTag(tag)) + { + auto addr = *maybe; + if (auto ptr = std::get_if
(&addr)) + { + return SendToServiceOrQueue(*ptr, pkt, t); + } + if (auto ptr = std::get_if(&addr)) + { + return SendToSNodeOrQueue(*ptr, pkt, t); + } + } + return false; } bool - Endpoint::SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& buf) + Endpoint::SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& buf, ProtocolType t) { auto pkt = std::make_shared(); if (!pkt->Load(buf)) return false; - EnsurePathToSNode(addr, [pkt](RouterID, exit::BaseSession_ptr s) { + EnsurePathToSNode(addr, [pkt, t](RouterID, exit::BaseSession_ptr s) { if (s) - s->QueueUpstreamTraffic(*pkt, routing::ExitPadSize); + s->SendPacketToRemote(pkt->ConstBuffer(), t); }); return true; } diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 0b7a4b105..959ef9dbf 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -13,6 +13,7 @@ #include "protocol.hpp" #include "quic/server.hpp" #include "sendcontext.hpp" +#include "service/protocol_type.hpp" #include "session.hpp" #include "lookup.hpp" #include @@ -361,7 +362,7 @@ namespace llarp SendToServiceOrQueue( const service::Address& addr, const llarp_buffer_t& payload, ProtocolType t); bool - SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& payload); + SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& payload, ProtocolType t); std::optional MaybeGetAuthInfoForEndpoint(service::Address addr); diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index ad6a83e14..b4262a35c 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -9,6 +9,7 @@ #include #include "endpoint_util.hpp" +#include "service/protocol_type.hpp" #include #include @@ -601,9 +602,9 @@ namespace llarp } void - OutboundContext::SendPacketToRemote(const llarp_buffer_t& buf) + OutboundContext::SendPacketToRemote(const llarp_buffer_t& buf, service::ProtocolType t) { - AsyncEncryptAndSendTo(buf, ProtocolType::Exit); + AsyncEncryptAndSendTo(buf, t); } } // namespace service diff --git a/llarp/service/outbound_context.hpp b/llarp/service/outbound_context.hpp index 570f70f7d..47c1761a0 100644 --- a/llarp/service/outbound_context.hpp +++ b/llarp/service/outbound_context.hpp @@ -72,7 +72,7 @@ namespace llarp /// for exits void - SendPacketToRemote(const llarp_buffer_t&) override; + SendPacketToRemote(const llarp_buffer_t&, ProtocolType t) override; bool ShouldBuildMore(llarp_time_t now) const override; diff --git a/pybind/llarp/handlers/pyhandler.hpp b/pybind/llarp/handlers/pyhandler.hpp index 2ed934548..452a29ea2 100644 --- a/pybind/llarp/handlers/pyhandler.hpp +++ b/pybind/llarp/handlers/pyhandler.hpp @@ -91,7 +91,7 @@ namespace llarp } void - SendPacketToRemote(const llarp_buffer_t&) override{}; + SendPacketToRemote(const llarp_buffer_t&, service::ProtocolType) override{}; std::string GetOurAddress() const