diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b8806509..96610e2bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -303,11 +303,7 @@ add_subdirectory(crypto) add_subdirectory(libutp) add_subdirectory(llarp) -set(RC_SRC - daemon/rcutil.cpp -) - -set(ALL_SRC ${RC_SRC} ${EXE_SRC} ${LIB_PLATFORM_SRC} ${LIB_SRC}) +set(ALL_SRC ${EXE_SRC} ${LIB_PLATFORM_SRC} ${LIB_SRC}) foreach(F ${ALL_SRC}) set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS -DLOG_TAG=\\\"${F}\\\") diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index d45edba10..24bee59db 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -177,6 +177,7 @@ set(LIB_SRC path/transit_hop.cpp pow.cpp profiling.cpp + router/abstractrouter.cpp router/router.cpp router_contact.cpp router_id.cpp diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index 2a47847d3..b8d724ecf 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -130,8 +130,10 @@ namespace llarp LoadFromFile(const char *fname); }; - using ShortHash = AlignedBuffer< SHORTHASHSIZE >; - using Signature = AlignedBuffer< SIGSIZE >; + using ShortHash = AlignedBuffer< SHORTHASHSIZE >; + struct Signature final : public AlignedBuffer< SIGSIZE > + { + }; using TunnelNonce = AlignedBuffer< TUNNONCESIZE >; using SymmNonce = AlignedBuffer< NONCESIZE >; using SymmKey = AlignedBuffer< 32 >; diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 68a5db206..b33ce7431 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -15,7 +15,9 @@ #include #include #include -#include +#include +#include +#include #include @@ -237,7 +239,7 @@ namespace llarp } void - Context::Init(const Key_t &us, llarp::Router *r, + Context::Init(const Key_t &us, AbstractRouter *r, llarp_time_t exploreInterval) { router = r; diff --git a/llarp/dht/context.hpp b/llarp/dht/context.hpp index 2bd0381d9..11e17d3db 100644 --- a/llarp/dht/context.hpp +++ b/llarp/dht/context.hpp @@ -180,7 +180,7 @@ namespace llarp /// initialize dht context and explore every exploreInterval milliseconds void - Init(const Key_t& us, llarp::Router* router, + Init(const Key_t& us, AbstractRouter* router, llarp_time_t exploreInterval); /// get localally stored introset by service address @@ -262,8 +262,8 @@ namespace llarp struct llarp_dht_context { llarp::dht::Context impl; - llarp::Router* parent; - llarp_dht_context(llarp::Router* router); + llarp::AbstractRouter* parent; + llarp_dht_context(llarp::AbstractRouter* router); }; #endif diff --git a/llarp/dht/dht.cpp b/llarp/dht/dht.cpp index 780c0d5a1..a3b71fc54 100644 --- a/llarp/dht/dht.cpp +++ b/llarp/dht/dht.cpp @@ -2,13 +2,13 @@ #include #include -llarp_dht_context::llarp_dht_context(llarp::Router *router) +llarp_dht_context::llarp_dht_context(llarp::AbstractRouter *router) { parent = router; } struct llarp_dht_context * -llarp_dht_context_new(llarp::Router *router) +llarp_dht_context_new(llarp::AbstractRouter *router) { return new llarp_dht_context(router); } diff --git a/llarp/dht/dht.h b/llarp/dht/dht.h index cd3f163ea..24b84468c 100644 --- a/llarp/dht/dht.h +++ b/llarp/dht/dht.h @@ -15,12 +15,12 @@ struct llarp_dht_context; namespace llarp { - struct Router; + struct AbstractRouter; } /// allocator struct llarp_dht_context* -llarp_dht_context_new(llarp::Router* parent); +llarp_dht_context_new(llarp::AbstractRouter* parent); /// deallocator void diff --git a/llarp/dht/explorenetworkjob.cpp b/llarp/dht/explorenetworkjob.cpp index 5683d75d0..082dd9ccb 100644 --- a/llarp/dht/explorenetworkjob.cpp +++ b/llarp/dht/explorenetworkjob.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/dht/localrouterlookup.cpp b/llarp/dht/localrouterlookup.cpp index fe5bcd2dc..6f7c33fa7 100644 --- a/llarp/dht/localrouterlookup.cpp +++ b/llarp/dht/localrouterlookup.cpp @@ -3,7 +3,8 @@ #include #include #include -#include +#include +#include #include namespace llarp diff --git a/llarp/dht/localserviceaddresslookup.cpp b/llarp/dht/localserviceaddresslookup.cpp index def38fc67..187b5331a 100644 --- a/llarp/dht/localserviceaddresslookup.cpp +++ b/llarp/dht/localserviceaddresslookup.cpp @@ -2,7 +2,9 @@ #include #include -#include +#include +#include +#include #include namespace llarp diff --git a/llarp/dht/localtaglookup.cpp b/llarp/dht/localtaglookup.cpp index a55849590..a0cfb6d20 100644 --- a/llarp/dht/localtaglookup.cpp +++ b/llarp/dht/localtaglookup.cpp @@ -3,7 +3,8 @@ #include #include #include -#include +#include +#include namespace llarp { diff --git a/llarp/dht/messages/findrouter.cpp b/llarp/dht/messages/findrouter.cpp index e7f490d25..4cc5efd71 100644 --- a/llarp/dht/messages/findrouter.cpp +++ b/llarp/dht/messages/findrouter.cpp @@ -3,7 +3,9 @@ #include #include #include -#include +#include +#include +#include namespace llarp { diff --git a/llarp/dht/messages/gotintro.cpp b/llarp/dht/messages/gotintro.cpp index 98f405d22..ed12fe685 100644 --- a/llarp/dht/messages/gotintro.cpp +++ b/llarp/dht/messages/gotintro.cpp @@ -2,7 +2,8 @@ #include #include -#include +#include +#include namespace llarp { diff --git a/llarp/dht/messages/gotrouter.cpp b/llarp/dht/messages/gotrouter.cpp index 0c856516a..05837ec21 100644 --- a/llarp/dht/messages/gotrouter.cpp +++ b/llarp/dht/messages/gotrouter.cpp @@ -1,7 +1,8 @@ #include #include -#include +#include +#include namespace llarp { @@ -107,7 +108,7 @@ namespace llarp if(!dht.pendingRouterLookups.HasPendingLookupFrom(owner)) { - llarp::LogWarn("Unwarrented GRM from ", From, " txid=", txid); + llarp::LogWarn("Unwarranted GRM from ", From, " txid=", txid); return false; } // no pending lookup diff --git a/llarp/dht/messages/pubintro.cpp b/llarp/dht/messages/pubintro.cpp index e3fd876c8..6578e68aa 100644 --- a/llarp/dht/messages/pubintro.cpp +++ b/llarp/dht/messages/pubintro.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include namespace llarp { diff --git a/llarp/exit/close_exit.cpp b/llarp/exit/close_exit.cpp index 71dd03268..3af088a24 100644 --- a/llarp/exit/close_exit.cpp +++ b/llarp/exit/close_exit.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace llarp { @@ -76,7 +77,7 @@ namespace llarp } bool - CloseExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + CloseExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { return h->HandleCloseExitMessage(this, r); } diff --git a/llarp/exit/context.cpp b/llarp/exit/context.cpp index 3e1e7b38c..8f798aa40 100644 --- a/llarp/exit/context.cpp +++ b/llarp/exit/context.cpp @@ -4,7 +4,7 @@ namespace llarp { namespace exit { - Context::Context(llarp::Router* r) : m_Router(r) + Context::Context(AbstractRouter* r) : m_Router(r) { } Context::~Context() diff --git a/llarp/exit/context.hpp b/llarp/exit/context.hpp index a199e30d2..7cf036193 100644 --- a/llarp/exit/context.hpp +++ b/llarp/exit/context.hpp @@ -15,7 +15,7 @@ namespace llarp { using Config_t = std::unordered_multimap< std::string, std::string >; - Context(llarp::Router *r); + Context(AbstractRouter *r); ~Context(); void @@ -50,7 +50,7 @@ namespace llarp CalculateExitTraffic(TrafficStats &stats); private: - llarp::Router *m_Router; + AbstractRouter *m_Router; std::unordered_map< std::string, std::unique_ptr< llarp::handlers::ExitEndpoint > > m_Exits; diff --git a/llarp/exit/endpoint.cpp b/llarp/exit/endpoint.cpp index dd630c847..69932ae89 100644 --- a/llarp/exit/endpoint.cpp +++ b/llarp/exit/endpoint.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace llarp { @@ -194,7 +194,8 @@ namespace llarp Endpoint::GetCurrentPath() const { auto router = m_Parent->GetRouter(); - return router->paths.GetByUpstream(router->pubkey(), m_CurrentPath); + return router->pathContext().GetByUpstream(router->pubkey(), + m_CurrentPath); } } // namespace exit } // namespace llarp diff --git a/llarp/exit/endpoint.hpp b/llarp/exit/endpoint.hpp index 02ce4826d..c9e7facb1 100644 --- a/llarp/exit/endpoint.hpp +++ b/llarp/exit/endpoint.hpp @@ -6,6 +6,8 @@ #include #include +#include + namespace llarp { namespace handlers diff --git a/llarp/exit/grant_exit.cpp b/llarp/exit/grant_exit.cpp index ad98beafa..07df83e03 100644 --- a/llarp/exit/grant_exit.cpp +++ b/llarp/exit/grant_exit.cpp @@ -1,4 +1,6 @@ #include + +#include #include namespace llarp @@ -81,7 +83,7 @@ namespace llarp } bool - GrantExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + GrantExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { return h->HandleGrantExitMessage(this, r); } diff --git a/llarp/exit/obtain_exit.cpp b/llarp/exit/obtain_exit.cpp index 59e310bd3..bd720dbe1 100644 --- a/llarp/exit/obtain_exit.cpp +++ b/llarp/exit/obtain_exit.cpp @@ -1,5 +1,6 @@ #include +#include #include namespace llarp @@ -11,7 +12,7 @@ namespace llarp { std::array< byte_t, 1024 > tmp; llarp_buffer_t buf(tmp); - I = llarp::seckey_topublic(sk); + I = seckey_topublic(sk); Z.Zero(); if(!BEncode(&buf)) { @@ -92,7 +93,8 @@ namespace llarp } bool - ObtainExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + ObtainExitMessage::HandleMessage(IMessageHandler* h, + AbstractRouter* r) const { return h->HandleObtainExitMessage(this, r); } diff --git a/llarp/exit/reject_exit.cpp b/llarp/exit/reject_exit.cpp index 3c17ac074..4d3038ba1 100644 --- a/llarp/exit/reject_exit.cpp +++ b/llarp/exit/reject_exit.cpp @@ -1,4 +1,6 @@ #include + +#include #include namespace llarp @@ -91,7 +93,8 @@ namespace llarp } bool - RejectExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + RejectExitMessage::HandleMessage(IMessageHandler* h, + AbstractRouter* r) const { return h->HandleRejectExitMessage(this, r); } diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 130439100..17d7d2fc1 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -1,7 +1,8 @@ #include +#include #include -#include +#include namespace llarp { @@ -9,8 +10,8 @@ namespace llarp { BaseSession::BaseSession( const llarp::RouterID& router, - std::function< bool(const llarp_buffer_t&) > writepkt, llarp::Router* r, - size_t numpaths, size_t hoplen) + std::function< bool(const llarp_buffer_t&) > writepkt, + AbstractRouter* r, size_t numpaths, size_t hoplen) : llarp::path::Builder(r, r->dht(), numpaths, hoplen) , m_ExitRouter(router) , m_WritePacket(writepkt) @@ -232,13 +233,14 @@ namespace llarp SNodeSession::SNodeSession( const llarp::RouterID& snodeRouter, - std::function< bool(const llarp_buffer_t&) > writepkt, llarp::Router* r, - size_t numpaths, size_t hoplen, bool useRouterSNodeKey) + std::function< bool(const llarp_buffer_t&) > writepkt, + AbstractRouter* r, size_t numpaths, size_t hoplen, + bool useRouterSNodeKey) : BaseSession(snodeRouter, writepkt, r, numpaths, hoplen) { if(useRouterSNodeKey) { - m_ExitIdentity = r->identity; + m_ExitIdentity = r->identity(); } } } // namespace exit diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index b1212cb57..b669e67ee 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -21,7 +21,7 @@ namespace llarp BaseSession(const llarp::RouterID& exitRouter, std::function< bool(const llarp_buffer_t&) > writepkt, - llarp::Router* r, size_t numpaths, size_t hoplen); + AbstractRouter* r, size_t numpaths, size_t hoplen); virtual ~BaseSession(); @@ -113,7 +113,7 @@ namespace llarp { ExitSession(const llarp::RouterID& snodeRouter, std::function< bool(const llarp_buffer_t&) > writepkt, - llarp::Router* r, size_t numpaths, size_t hoplen) + AbstractRouter* r, size_t numpaths, size_t hoplen) : BaseSession(snodeRouter, writepkt, r, numpaths, hoplen){}; ~ExitSession(){}; @@ -132,7 +132,7 @@ namespace llarp { SNodeSession(const llarp::RouterID& snodeRouter, std::function< bool(const llarp_buffer_t&) > writepkt, - llarp::Router* r, size_t numpaths, size_t hoplen, + AbstractRouter* r, size_t numpaths, size_t hoplen, bool useRouterSNodeKey = false); ~SNodeSession(){}; diff --git a/llarp/exit/transfer_traffic.cpp b/llarp/exit/transfer_traffic.cpp index ab67de1a3..2058781a1 100644 --- a/llarp/exit/transfer_traffic.cpp +++ b/llarp/exit/transfer_traffic.cpp @@ -64,7 +64,7 @@ namespace llarp bool TransferTrafficMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h->HandleTransferTrafficMessage(this, r); } diff --git a/llarp/exit/update_exit.cpp b/llarp/exit/update_exit.cpp index 2918b0b79..9b08db6a1 100644 --- a/llarp/exit/update_exit.cpp +++ b/llarp/exit/update_exit.cpp @@ -1,4 +1,6 @@ #include + +#include #include namespace llarp @@ -82,7 +84,8 @@ namespace llarp } bool - UpdateExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + UpdateExitMessage::HandleMessage(IMessageHandler* h, + AbstractRouter* r) const { return h->HandleUpdateExitMessage(this, r); } @@ -119,7 +122,7 @@ namespace llarp bool UpdateExitVerifyMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h->HandleUpdateExitVerifyMessage(this, r); } diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index e1a077586..4d6fde60d 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -23,13 +23,13 @@ namespace llarp static_cast< ExitEndpoint * >(tun->user)->Flush(); } - ExitEndpoint::ExitEndpoint(const std::string &name, Router *r) + ExitEndpoint::ExitEndpoint(const std::string &name, AbstractRouter *r) : m_Router(r) - , m_Resolver(r->netloop, this) + , m_Resolver(r->netloop(), this) , m_Name(name) , m_Tun{{0}, 0, {0}, 0, 0, 0, 0, 0, 0, 0} , m_LocalResolverAddr("127.0.0.1", 53) - , m_InetToNetwork(name + "_exit_rx", r->netloop, r->netloop) + , m_InetToNetwork(name + "_exit_rx", r->netloop(), r->netloop()) { m_Tun.user = this; @@ -237,7 +237,7 @@ namespace llarp { if(m_ShouldInitTun) { - if(!llarp_ev_add_tun(GetRouter()->netloop, &m_Tun)) + if(!llarp_ev_add_tun(GetRouter()->netloop(), &m_Tun)) { llarp::LogWarn("Could not create tunnel for exit endpoint"); return false; @@ -251,7 +251,7 @@ namespace llarp return true; } - Router * + AbstractRouter * ExitEndpoint::GetRouter() { return m_Router; @@ -542,7 +542,8 @@ namespace llarp if(wantInternet && !m_PermitExit) return false; huint32_t ip = GetIPForIdent(pk); - if(GetRouter()->paths.TransitHopPreviousIsRouter(path, pk.as_array())) + if(GetRouter()->pathContext().TransitHopPreviousIsRouter(path, + pk.as_array())) { // we think this path belongs to a service node // mark it as such so we don't make an outbound session to them diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index e5d4697cc..88780eef4 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -8,12 +8,12 @@ namespace llarp { - struct Router; + struct AbstractRouter; namespace handlers { struct ExitEndpoint : public dns::IQueryHandler, public util::IStateful { - ExitEndpoint(const std::string& name, Router* r); + ExitEndpoint(const std::string& name, AbstractRouter* r); ~ExitEndpoint(); void @@ -52,7 +52,7 @@ namespace llarp void OnInetPacket(const llarp_buffer_t& buf); - Router* + AbstractRouter* GetRouter(); llarp_time_t @@ -125,7 +125,7 @@ namespace llarp void KickIdentOffExit(const PubKey& pk); - Router* m_Router; + AbstractRouter* m_Router; dns::Proxy m_Resolver; bool m_ShouldInitTun; std::string m_Name; diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index f52a7ed42..009847614 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -9,7 +9,7 @@ namespace llarp { struct NullEndpoint final : public llarp::service::Endpoint { - NullEndpoint(const std::string &name, llarp::Router *r, + NullEndpoint(const std::string &name, AbstractRouter *r, llarp::service::Context *parent) : llarp::service::Endpoint(name, r, parent){}; diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 9c10c03e5..fedbc7c92 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -10,7 +10,8 @@ #include #include -#include +#include +#include namespace llarp { @@ -29,12 +30,14 @@ namespace llarp self->Flush(); } - TunEndpoint::TunEndpoint(const std::string &nickname, llarp::Router *r, + TunEndpoint::TunEndpoint(const std::string &nickname, AbstractRouter *r, service::Context *parent) : service::Endpoint(nickname, r, parent) - , m_UserToNetworkPktQueue(nickname + "_sendq", r->netloop, r->netloop) - , m_NetworkToUserPktQueue(nickname + "_recvq", r->netloop, r->netloop) - , m_Resolver(r->netloop, this) + , m_UserToNetworkPktQueue(nickname + "_sendq", r->netloop(), + r->netloop()) + , m_NetworkToUserPktQueue(nickname + "_recvq", r->netloop(), + r->netloop()) + , m_Resolver(r->netloop(), this) { #ifdef ANDROID tunif.get_fd_promise = &get_tun_fd_promise; diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index b930cb0cf..573ec2360 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -20,7 +20,7 @@ namespace llarp struct TunEndpoint : public service::Endpoint, public dns::IQueryHandler { - TunEndpoint(const std::string& nickname, llarp::Router* r, + TunEndpoint(const std::string& nickname, AbstractRouter* r, llarp::service::Context* parent); ~TunEndpoint(); diff --git a/llarp/link/iwp.cpp b/llarp/link/iwp.cpp index 98c2fd6e9..91e7dccab 100644 --- a/llarp/link/iwp.cpp +++ b/llarp/link/iwp.cpp @@ -1,16 +1,17 @@ +#include #include -#include namespace llarp { namespace iwp { std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router*) + NewServerFromRouter(AbstractRouter*) { // TODO: implement me return nullptr; } + std::unique_ptr< ILinkLayer > NewServer(llarp::Crypto*, const SecretKey&, llarp::GetRCFunc, llarp::LinkMessageHandler, llarp::SessionEstablishedHandler, diff --git a/llarp/link/iwp.hpp b/llarp/link/iwp.hpp index a7572f453..c64494126 100644 --- a/llarp/link/iwp.hpp +++ b/llarp/link/iwp.hpp @@ -1,11 +1,14 @@ #ifndef LLARP_LINK_IWP_HPP #define LLARP_LINK_IWP_HPP -#include #include +#include + namespace llarp { + struct AbstractRouter; + namespace iwp { std::unique_ptr< ILinkLayer > @@ -17,7 +20,7 @@ namespace llarp llarp::SessionClosedHandler closed); std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router* r); + NewServerFromRouter(AbstractRouter* r); } // namespace iwp } // namespace llarp diff --git a/llarp/link/utp.cpp b/llarp/link/utp.cpp index 052d7eb17..5bbb43faf 100644 --- a/llarp/link/utp.cpp +++ b/llarp/link/utp.cpp @@ -1,9 +1,10 @@ #include +#include #include #include #include -#include +#include #include #include @@ -51,17 +52,17 @@ namespace llarp { parent = p; EnterState(eLinkEstablished); - llarp::LogDebug("link established with ", remoteAddr); + LogDebug("link established with ", remoteAddr); } - llarp::Crypto* - Session::Crypto() + Crypto* + Session::OurCrypto() { - return parent->Crypto(); + return parent->OurCrypto(); } - llarp::Crypto* - LinkLayer::Crypto() + Crypto* + LinkLayer::OurCrypto() { return _crypto; } @@ -140,19 +141,19 @@ namespace llarp std::copy(K.begin(), K.end(), tmp.begin()); std::copy(n.begin(), n.end(), tmp.begin() + K.size()); // t_h = HS(K + L.n) - if(!Crypto()->shorthash(t_h, llarp_buffer_t(tmp))) + if(!OurCrypto()->shorthash(t_h, llarp_buffer_t(tmp))) { - llarp::LogError("failed to mix key to ", remoteAddr); + LogError("failed to mix key to ", remoteAddr); return false; } // K = TKE(a.p, B_a.e, sk, t_h) if(!dh(K, other, secret, t_h)) { - llarp::LogError("key exchange with ", other, " failed"); + LogError("key exchange with ", other, " failed"); return false; } - llarp::LogDebug("keys mixed with session to ", remoteAddr); + LogDebug("keys mixed with session to ", remoteAddr); return true; } @@ -165,7 +166,7 @@ namespace llarp buf.cur += K.size(); std::copy(A.begin(), A.end(), buf.cur); buf.cur = buf.base; - return Crypto()->shorthash(K, buf); + return OurCrypto()->shorthash(K, buf); } void @@ -188,8 +189,8 @@ namespace llarp if(s >= left) { // yes it fills it - llarp::LogDebug("process leftovers, offset=", recvBufOffset, - " sz=", s, " left=", left); + LogDebug("process leftovers, offset=", recvBufOffset, " sz=", s, + " left=", left); std::copy(buf, buf + left, recvBuf.begin() + recvBufOffset); s -= left; recvBufOffset = 0; @@ -202,7 +203,7 @@ namespace llarp while(s >= FragmentBufferSize) { recvBufOffset = 0; - llarp::LogDebug("process full sz=", s); + LogDebug("process full sz=", s); if(!VerifyThenDecrypt(buf)) return false; buf += FragmentBufferSize; @@ -211,7 +212,7 @@ namespace llarp if(s) { // hold onto leftovers - llarp::LogDebug("leftovers sz=", s); + LogDebug("leftovers sz=", s); std::copy(buf, buf + s, recvBuf.begin() + recvBufOffset); recvBufOffset += s; } @@ -228,7 +229,7 @@ namespace llarp auto dlt = now - lastActive; if(dlt >= sessionTimeout) { - llarp::LogDebug("session timeout reached for ", remoteAddr); + LogDebug("session timeout reached for ", remoteAddr); return true; } return false; @@ -253,8 +254,7 @@ namespace llarp static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); if(l == nullptr) return 0; - llarp::LogDebug("utp_sendto ", Addr(*arg->address), " ", arg->len, - " bytes"); + LogDebug("utp_sendto ", Addr(*arg->address), " ", arg->len, " bytes"); // For whatever reason, the UTP_UDP_DONTFRAG flag is set // on the socket itself....which isn't correct and causes // winsock (at minimum) to reeee @@ -316,9 +316,9 @@ namespace llarp int err = WSAGetLastError(); FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, buf, 1024, nullptr); - llarp::LogError("sendto failed: ", buf); + LogError("sendto failed: ", buf); #else - llarp::LogError("sendto failed: ", strerror(errno)); + LogError("sendto failed: ", strerror(errno)); #endif } return 0; @@ -335,8 +335,8 @@ namespace llarp if(session && link) { link->HandleTimeout(session); - llarp::LogError(utp_error_code_names[arg->error_code], " via ", - session->remoteAddr); + LogError(utp_error_code_names[arg->error_code], " via ", + session->remoteAddr); if(arg->error_code != UTP_ETIMEDOUT) session->Close(); link->RemovePending(session); @@ -347,18 +347,16 @@ namespace llarp uint64 LinkLayer::OnLog(utp_callback_arguments* arg) { - llarp::LogDebug(arg->buf); + LogDebug(arg->buf); return 0; } - LinkLayer::LinkLayer(llarp::Crypto* crypto, - const SecretKey& routerEncSecret, - llarp::GetRCFunc getrc, llarp::LinkMessageHandler h, - llarp::SignBufferFunc sign, - llarp::SessionEstablishedHandler established, - llarp::SessionRenegotiateHandler reneg, - llarp::TimeoutHandler timeout, - llarp::SessionClosedHandler closed) + LinkLayer::LinkLayer(Crypto* crypto, const SecretKey& routerEncSecret, + GetRCFunc getrc, LinkMessageHandler h, + SignBufferFunc sign, + SessionEstablishedHandler established, + SessionRenegotiateHandler reneg, + TimeoutHandler timeout, SessionClosedHandler closed) : ILinkLayer(routerEncSecret, getrc, h, sign, established, reneg, timeout, closed) { @@ -428,7 +426,7 @@ namespace llarp if(errno == EAGAIN || errno == EWOULDBLOCK) errno = 0; else - llarp::LogError("failed to read icmp for utp ", strerror(errno)); + LogError("failed to read icmp for utp ", strerror(errno)); return; } @@ -511,7 +509,7 @@ namespace llarp bool LinkLayer::KeyGen(SecretKey& k) { - Crypto()->encryption_keygen(k); + OurCrypto()->encryption_keygen(k); return true; } @@ -535,12 +533,10 @@ namespace llarp } std::unique_ptr< ILinkLayer > - NewServer(llarp::Crypto* crypto, const SecretKey& routerEncSecret, - llarp::GetRCFunc getrc, llarp::LinkMessageHandler h, - llarp::SessionEstablishedHandler est, - llarp::SessionRenegotiateHandler reneg, - llarp::SignBufferFunc sign, llarp::TimeoutHandler timeout, - llarp::SessionClosedHandler closed) + NewServer(Crypto* crypto, const SecretKey& routerEncSecret, GetRCFunc getrc, + LinkMessageHandler h, SessionEstablishedHandler est, + SessionRenegotiateHandler reneg, SignBufferFunc sign, + TimeoutHandler timeout, SessionClosedHandler closed) { return std::unique_ptr< ILinkLayer >( new LinkLayer(crypto, routerEncSecret, getrc, h, sign, est, reneg, @@ -548,20 +544,17 @@ namespace llarp } std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router* r) + NewServerFromRouter(AbstractRouter* r) { + using namespace std::placeholders; return NewServer( - r->crypto(), r->encryption, std::bind(&llarp::Router::rc, r), - std::bind(&llarp::Router::HandleRecvLinkMessageBuffer, r, - std::placeholders::_1, std::placeholders::_2), - std::bind(&llarp::Router::OnSessionEstablished, r, - std::placeholders::_1), - std::bind(&llarp::Router::CheckRenegotiateValid, r, - std::placeholders::_1, std::placeholders::_2), - std::bind(&llarp::Router::Sign, r, std::placeholders::_1, - std::placeholders::_2), - std::bind(&llarp::Router::OnConnectTimeout, r, std::placeholders::_1), - std::bind(&llarp::Router::SessionClosed, r, std::placeholders::_1)); + r->crypto(), r->encryption(), std::bind(&AbstractRouter::rc, r), + std::bind(&AbstractRouter::HandleRecvLinkMessageBuffer, r, _1, _2), + std::bind(&AbstractRouter::OnSessionEstablished, r, _1), + std::bind(&AbstractRouter::CheckRenegotiateValid, r, _1, _2), + std::bind(&AbstractRouter::Sign, r, _1, _2), + std::bind(&AbstractRouter::OnConnectTimeout, r, _1), + std::bind(&AbstractRouter::SessionClosed, r, _1)); } /// base constructor @@ -595,7 +588,7 @@ namespace llarp recvBufOffset = 0; TimedOut = std::bind(&Session::IsTimedOut, this, std::placeholders::_1); GetPubKey = std::bind(&Session::RemotePubKey, this); - GetRemoteRC = [&]() -> llarp::RouterContact { return this->remoteRC; }; + GetRemoteRC = [&]() -> RouterContact { return this->remoteRC; }; GetLinkLayer = std::bind(&Session::GetParent, this); lastActive = parent->Now(); @@ -622,9 +615,9 @@ namespace llarp remoteTransportPubKey = addr.pubkey; remoteRC = rc; RouterID rid = remoteRC.pubkey; - Crypto()->shorthash(txKey, llarp_buffer_t(rid)); + OurCrypto()->shorthash(txKey, llarp_buffer_t(rid)); rid = p->GetOurRC().pubkey; - Crypto()->shorthash(rxKey, llarp_buffer_t(rid)); + OurCrypto()->shorthash(rxKey, llarp_buffer_t(rid)); sock = s; assert(utp_set_userdata(sock, this) == this); @@ -638,7 +631,7 @@ namespace llarp Session::Session(LinkLayer* p, utp_socket* s, const Addr& addr) : Session(p) { RouterID rid = p->GetOurRC().pubkey; - Crypto()->shorthash(rxKey, llarp_buffer_t(rid)); + OurCrypto()->shorthash(rxKey, llarp_buffer_t(rid)); remoteRC.Clear(); sock = s; assert(s == sock); @@ -665,10 +658,10 @@ namespace llarp if(!gotLIM) { remoteRC = msg->rc; - Crypto()->shorthash(txKey, llarp_buffer_t(remoteRC.pubkey)); + OurCrypto()->shorthash(txKey, llarp_buffer_t(remoteRC.pubkey)); - if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, Crypto(), _1, - _2, _3, _4), + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, OurCrypto(), + _1, _2, _3, _4), rxKey, msg->N, remoteRC.enckey, parent->TransportSecretKey())) return false; @@ -677,9 +670,9 @@ namespace llarp llarp_buffer_t buf(tmp); LinkIntroMessage replymsg; replymsg.rc = parent->GetOurRC(); - if(!replymsg.rc.Verify(Crypto(), parent->Now())) + if(!replymsg.rc.Verify(OurCrypto(), parent->Now())) { - llarp::LogError("our RC is invalid? closing session to", remoteAddr); + LogError("our RC is invalid? closing session to", remoteAddr); Close(); return false; } @@ -687,16 +680,15 @@ namespace llarp replymsg.P = DefaultLinkSessionLifetime; if(!replymsg.Sign(parent->Sign)) { - llarp::LogError("failed to sign LIM for inbound handshake from ", - remoteAddr); + LogError("failed to sign LIM for inbound handshake from ", + remoteAddr); Close(); return false; } // encode if(!replymsg.BEncode(&buf)) { - llarp::LogError("failed to encode LIM for handshake from ", - remoteAddr); + LogError("failed to encode LIM for handshake from ", remoteAddr); Close(); return false; } @@ -706,17 +698,17 @@ namespace llarp // send if(!SendMessageBuffer(buf)) { - llarp::LogError("failed to repl to handshake from ", remoteAddr); + LogError("failed to repl to handshake from ", remoteAddr); Close(); return false; } - if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, Crypto(), _1, - _2, _3, _4), + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, OurCrypto(), + _1, _2, _3, _4), txKey, replymsg.N, remoteRC.enckey, parent->RouterEncryptionSecret())) return false; - llarp::LogDebug("Sent reply LIM"); + LogDebug("Sent reply LIM"); gotLIM = true; EnterState(eSessionReady); /// future LIM are used for session renegotiation @@ -748,10 +740,10 @@ namespace llarp uint32_t s = std::min(FragmentBodyPayloadSize, sz); if(!EncryptThenHash(ptr, msgid, s, sz - s)) { - llarp::LogError("EncryptThenHash failed?!"); + LogError("EncryptThenHash failed?!"); return false; } - llarp::LogDebug("encrypted ", s, " bytes"); + LogDebug("encrypted ", s, " bytes"); ptr += s; sz -= s; } @@ -768,9 +760,10 @@ namespace llarp remoteRC = msg->rc; gotLIM = true; - if(!DoKeyExchange( - std::bind(&Crypto::transport_dh_server, Crypto(), _1, _2, _3, _4), - rxKey, msg->N, remoteRC.enckey, parent->RouterEncryptionSecret())) + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, OurCrypto(), _1, + _2, _3, _4), + rxKey, msg->N, remoteRC.enckey, + parent->RouterEncryptionSecret())) { Close(); return false; @@ -790,9 +783,9 @@ namespace llarp // build our RC LinkIntroMessage msg; msg.rc = parent->GetOurRC(); - if(!msg.rc.Verify(Crypto(), parent->Now())) + if(!msg.rc.Verify(OurCrypto(), parent->Now())) { - llarp::LogError("our RC is invalid? closing session to", remoteAddr); + LogError("our RC is invalid? closing session to", remoteAddr); Close(); return; } @@ -800,15 +793,14 @@ namespace llarp msg.P = DefaultLinkSessionLifetime; if(!msg.Sign(parent->Sign)) { - llarp::LogError("failed to sign LIM for outbound handshake to ", - remoteAddr); + LogError("failed to sign LIM for outbound handshake to ", remoteAddr); Close(); return; } // encode if(!msg.BEncode(&buf)) { - llarp::LogError("failed to encode LIM for handshake to ", remoteAddr); + LogError("failed to encode LIM for handshake to ", remoteAddr); Close(); return; } @@ -818,18 +810,17 @@ namespace llarp // send if(!SendMessageBuffer(buf)) { - llarp::LogError("failed to send handshake to ", remoteAddr); + LogError("failed to send handshake to ", remoteAddr); Close(); return; } - if(!DoKeyExchange( - std::bind(&Crypto::transport_dh_client, Crypto(), _1, _2, _3, _4), - txKey, msg.N, remoteTransportPubKey, - parent->RouterEncryptionSecret())) + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, OurCrypto(), _1, + _2, _3, _4), + txKey, msg.N, remoteTransportPubKey, + parent->RouterEncryptionSecret())) { - llarp::LogError("failed to mix keys for outbound session to ", - remoteAddr); + LogError("failed to mix keys for outbound session to ", remoteAddr); Close(); return; } @@ -863,7 +854,7 @@ namespace llarp } if(!self->Recv(arg->buf, arg->len)) { - llarp::LogDebug("recv fail for ", self->remoteAddr); + LogDebug("recv fail for ", self->remoteAddr); self->Close(); return 0; } @@ -871,7 +862,7 @@ namespace llarp } else { - llarp::LogWarn("utp_socket got data with no underlying session"); + LogWarn("utp_socket got data with no underlying session"); utp_close(arg->socket); } return 0; @@ -899,7 +890,7 @@ namespace llarp } else if(arg->state == UTP_STATE_EOF) { - llarp::LogDebug("got eof from ", session->remoteAddr); + LogDebug("got eof from ", session->remoteAddr); session->Close(); } } @@ -912,7 +903,7 @@ namespace llarp LinkLayer* self = static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); Addr remote(*arg->address); - llarp::LogDebug("utp accepted from ", remote); + LogDebug("utp accepted from ", remote); Session* session = new Session(self, arg->socket, remote); if(!self->PutSession(session)) { @@ -961,14 +952,14 @@ namespace llarp TunnelNonce nonce(noncePtr); // encrypt - if(!Crypto()->xchacha20(payload, txKey, nonce)) + if(!OurCrypto()->xchacha20(payload, txKey, nonce)) return false; payload.base = noncePtr; payload.cur = payload.base; payload.sz = FragmentBufferSize - FragmentHashSize; // key'd hash - if(!Crypto()->hmac(buf.data(), payload, txKey)) + if(!OurCrypto()->hmac(buf.data(), payload, txKey)) return false; return MutateKey(txKey, A); } @@ -999,7 +990,7 @@ namespace llarp remoteRC = msg->rc; // recalculate rx key return DoKeyExchange( - std::bind(&Crypto::transport_dh_server, Crypto(), _1, _2, _3, _4), + std::bind(&Crypto::transport_dh_server, OurCrypto(), _1, _2, _3, _4), rxKey, msg->N, remoteRC.enckey, parent->RouterEncryptionSecret()); } @@ -1025,28 +1016,28 @@ namespace llarp return false; // regen our tx Key return DoKeyExchange( - std::bind(&Crypto::transport_dh_client, Crypto(), _1, _2, _3, _4), + std::bind(&Crypto::transport_dh_client, OurCrypto(), _1, _2, _3, _4), txKey, lim.N, remoteRC.enckey, parent->RouterEncryptionSecret()); } bool Session::VerifyThenDecrypt(const byte_t* ptr) { - llarp::LogDebug("verify then decrypt ", remoteAddr); + LogDebug("verify then decrypt ", remoteAddr); ShortHash digest; llarp_buffer_t hbuf(ptr + FragmentHashSize, FragmentBufferSize - FragmentHashSize); - if(!Crypto()->hmac(digest.data(), hbuf, rxKey)) + if(!OurCrypto()->hmac(digest.data(), hbuf, rxKey)) { - llarp::LogError("keyed hash failed"); + LogError("keyed hash failed"); return false; } ShortHash expected(ptr); if(expected != digest) { - llarp::LogError("Message Integrity Failed: got ", digest, " from ", - remoteAddr, " instead of ", expected); + LogError("Message Integrity Failed: got ", digest, " from ", remoteAddr, + " instead of ", expected); Close(); return false; } @@ -1057,9 +1048,9 @@ namespace llarp llarp_buffer_t out(rxFragBody); // decrypt - if(!Crypto()->xchacha20_alt(out, in, rxKey, ptr + FragmentHashSize)) + if(!OurCrypto()->xchacha20_alt(out, in, rxKey, ptr + FragmentHashSize)) { - llarp::LogError("failed to decrypt message from ", remoteAddr); + LogError("failed to decrypt message from ", remoteAddr); return false; } // get inner nonce @@ -1070,7 +1061,7 @@ namespace llarp uint32_t msgid; if(!llarp_buffer_read_uint32(&out, &msgid)) { - llarp::LogError("failed to read msgid"); + LogError("failed to read msgid"); return false; } // read length and remaining @@ -1078,13 +1069,13 @@ namespace llarp if(!(llarp_buffer_read_uint16(&out, &length) && llarp_buffer_read_uint16(&out, &remaining))) { - llarp::LogError("failed to read the rest of the header"); + LogError("failed to read the rest of the header"); return false; } if(length > (out.sz - (out.cur - out.base))) { // too big length - llarp::LogError("fragment body too big"); + LogError("fragment body too big"); return false; } if(msgid < m_NextRXMsgID) @@ -1103,13 +1094,13 @@ namespace llarp // append data if(!itr->second.AppendData(out.cur, length)) { - llarp::LogError("inbound buffer is full"); + LogError("inbound buffer is full"); return false; // not enough room } // mutate key if(!MutateKey(rxKey, A)) { - llarp::LogError("failed to mutate rx key"); + LogError("failed to mutate rx key"); return false; } @@ -1123,7 +1114,7 @@ namespace llarp // rewind buf.underlying.cur = buf.underlying.base; // process buffer - llarp::LogDebug("got message ", msgid, " from ", remoteAddr); + LogDebug("got message ", msgid, " from ", remoteAddr); return parent->HandleMessage(this, buf.underlying); } return true; @@ -1142,7 +1133,7 @@ namespace llarp utp_shutdown(sock, SHUT_RDWR); utp_close(sock); } - llarp::LogDebug("utp_close ", remoteAddr); + LogDebug("utp_close ", remoteAddr); utp_set_userdata(sock, nullptr); } } diff --git a/llarp/link/utp.hpp b/llarp/link/utp.hpp index 5f482b28d..7032fef95 100644 --- a/llarp/link/utp.hpp +++ b/llarp/link/utp.hpp @@ -6,7 +6,7 @@ namespace llarp { - struct Router; + struct AbstractRouter; namespace utp { @@ -19,7 +19,7 @@ namespace llarp llarp::SessionClosedHandler closed); std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router* r); + NewServerFromRouter(AbstractRouter* r); } // namespace utp } // namespace llarp diff --git a/llarp/link/utp_internal.hpp b/llarp/link/utp_internal.hpp index 90eaa72e5..9deec7ce1 100644 --- a/llarp/link/utp_internal.hpp +++ b/llarp/link/utp_internal.hpp @@ -36,8 +36,7 @@ namespace llarp constexpr size_t FragmentBufferSize = FragmentOverheadSize + FragmentBodySize; - static_assert(FragmentBufferSize == 608, - "Fragement Buffer Size is not 608"); + static_assert(FragmentBufferSize == 608, "Fragment Buffer Size is not 608"); /// buffer for a single utp fragment using FragmentBuffer = llarp::AlignedBuffer< FragmentBufferSize >; @@ -161,7 +160,7 @@ namespace llarp // Router(); llarp::Crypto* - Crypto(); + OurCrypto(); /// session state, call EnterState(State) to set State state; @@ -327,7 +326,7 @@ namespace llarp #endif llarp::Crypto* - Crypto(); + OurCrypto(); /// pump sessions void @@ -337,7 +336,7 @@ namespace llarp void Stop(); - /// rengenerate transport keypair + /// regenerate transport keypair bool KeyGen(SecretKey& k); diff --git a/llarp/messages/dht.hpp b/llarp/messages/dht.hpp index be61d9ffc..d161495d6 100644 --- a/llarp/messages/dht.hpp +++ b/llarp/messages/dht.hpp @@ -24,7 +24,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override diff --git a/llarp/messages/dht_immediate.cpp b/llarp/messages/dht_immediate.cpp index 45cbec397..295d55b24 100644 --- a/llarp/messages/dht_immediate.cpp +++ b/llarp/messages/dht_immediate.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace llarp { @@ -65,7 +65,7 @@ namespace llarp } bool - DHTImmediateMessage::HandleMessage(llarp::Router *router) const + DHTImmediateMessage::HandleMessage(AbstractRouter *router) const { DHTImmediateMessage reply; reply.session = session; diff --git a/llarp/messages/dht_immediate.hpp b/llarp/messages/dht_immediate.hpp index 00c0cb4e6..9802eecb2 100644 --- a/llarp/messages/dht_immediate.hpp +++ b/llarp/messages/dht_immediate.hpp @@ -16,7 +16,7 @@ namespace llarp ~DHTImmediateMessage(); - std::vector< std::unique_ptr< llarp::dht::IMessage > > msgs; + std::vector< std::unique_ptr< dht::IMessage > > msgs; bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; @@ -25,7 +25,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; void Clear() override; diff --git a/llarp/messages/discard.hpp b/llarp/messages/discard.hpp index 098309dd9..eeac2e862 100644 --- a/llarp/messages/discard.hpp +++ b/llarp/messages/discard.hpp @@ -39,7 +39,7 @@ namespace llarp } bool - HandleMessage(__attribute__((unused)) llarp::Router* router) const override + HandleMessage(__attribute__((unused)) AbstractRouter* router) const override { return true; } @@ -67,7 +67,7 @@ namespace llarp } bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override { return h->HandleDataDiscardMessage(this, r); } diff --git a/llarp/messages/exit.hpp b/llarp/messages/exit.hpp index f9b223e5b..2deff8187 100644 --- a/llarp/messages/exit.hpp +++ b/llarp/messages/exit.hpp @@ -56,7 +56,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; struct GrantExitMessage final : public IMessage @@ -89,7 +89,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override @@ -143,7 +143,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; struct UpdateExitVerifyMessage final : public IMessage @@ -185,7 +185,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; struct UpdateExitMessage final : public IMessage @@ -220,7 +220,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override @@ -257,7 +257,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; bool Sign(llarp::Crypto* c, const llarp::SecretKey& sk); diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index c4ede56eb..6254bff70 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -1,7 +1,8 @@ #include +#include #include -#include +#include #include #include @@ -115,7 +116,7 @@ namespace llarp } bool - LinkIntroMessage::HandleMessage(llarp::Router* router) const + LinkIntroMessage::HandleMessage(AbstractRouter* router) const { if(!Verify(router->crypto())) return false; diff --git a/llarp/messages/link_intro.hpp b/llarp/messages/link_intro.hpp index 3d3b0310f..75c63c43e 100644 --- a/llarp/messages/link_intro.hpp +++ b/llarp/messages/link_intro.hpp @@ -34,7 +34,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; bool Sign(std::function< bool(Signature&, const llarp_buffer_t&) > signer); diff --git a/llarp/messages/link_message.cpp b/llarp/messages/link_message.cpp index 3bfb84743..00371da4c 100644 --- a/llarp/messages/link_message.cpp +++ b/llarp/messages/link_message.cpp @@ -1,12 +1,36 @@ #include +#include +#include +#include +#include +#include +#include #include #include #include namespace llarp { - InboundMessageParser::InboundMessageParser(Router* _router) : router(_router) + struct InboundMessageParser::msg_holder_t + { + LinkIntroMessage i; + RelayDownstreamMessage d; + RelayUpstreamMessage u; + DHTImmediateMessage m; + LR_CommitMessage c; + DiscardMessage x; + }; + + InboundMessageParser::InboundMessageParser(AbstractRouter* _router) + : router(_router) + , from(nullptr) + , msg(nullptr) + , holder(std::make_unique< msg_holder_t >()) + { + } + + InboundMessageParser::~InboundMessageParser() { } @@ -46,22 +70,22 @@ namespace llarp switch(*strbuf.cur) { case 'i': - handler->msg = &handler->holder.i; + handler->msg = &handler->holder->i; break; case 'd': - handler->msg = &handler->holder.d; + handler->msg = &handler->holder->d; break; case 'u': - handler->msg = &handler->holder.u; + handler->msg = &handler->holder->u; break; case 'm': - handler->msg = &handler->holder.m; + handler->msg = &handler->holder->m; break; case 'c': - handler->msg = &handler->holder.c; + handler->msg = &handler->holder->c; break; case 'x': - handler->msg = &handler->holder.x; + handler->msg = &handler->holder->x; break; default: return false; diff --git a/llarp/messages/link_message.hpp b/llarp/messages/link_message.hpp index 84dcffc05..6edd91b6c 100644 --- a/llarp/messages/link_message.hpp +++ b/llarp/messages/link_message.hpp @@ -5,15 +5,12 @@ #include #include -#include #include namespace llarp { struct ILinkSession; - struct Router; - - using SendQueue = std::queue< ILinkMessage* >; + struct AbstractRouter; /// parsed link layer message struct ILinkMessage : public IBEncodeMessage @@ -29,7 +26,7 @@ namespace llarp } virtual bool - HandleMessage(Router* router) const = 0; + HandleMessage(AbstractRouter* router) const = 0; virtual void Clear() = 0; diff --git a/llarp/messages/link_message_parser.hpp b/llarp/messages/link_message_parser.hpp index f1bd42520..0bc39a899 100644 --- a/llarp/messages/link_message_parser.hpp +++ b/llarp/messages/link_message_parser.hpp @@ -1,18 +1,21 @@ #ifndef LLARP_LINK_MESSAGE_PARSER_HPP #define LLARP_LINK_MESSAGE_PARSER_HPP -#include -#include -#include -#include -#include -#include +#include +#include + +#include namespace llarp { + struct AbstractRouter; + struct ILinkMessage; + struct ILinkSession; + struct InboundMessageParser { - InboundMessageParser(Router* router); + InboundMessageParser(AbstractRouter* router); + ~InboundMessageParser(); dict_reader reader; static bool @@ -37,21 +40,12 @@ namespace llarp private: bool firstkey; - Router* router; - ILinkSession* from = nullptr; - ILinkMessage* msg = nullptr; - - struct msg_holder_t - { - LinkIntroMessage i; - RelayDownstreamMessage d; - RelayUpstreamMessage u; - DHTImmediateMessage m; - LR_CommitMessage c; - DiscardMessage x; - }; - - msg_holder_t holder; + AbstractRouter* router; + ILinkSession* from; + ILinkMessage* msg; + + struct msg_holder_t; + std::unique_ptr< msg_holder_t > holder; }; } // namespace llarp #endif diff --git a/llarp/messages/path_confirm.hpp b/llarp/messages/path_confirm.hpp index 021cc31e6..3c80873f4 100644 --- a/llarp/messages/path_confirm.hpp +++ b/llarp/messages/path_confirm.hpp @@ -22,7 +22,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override diff --git a/llarp/messages/path_latency.hpp b/llarp/messages/path_latency.hpp index aae47e955..21cb3d48a 100644 --- a/llarp/messages/path_latency.hpp +++ b/llarp/messages/path_latency.hpp @@ -27,7 +27,7 @@ namespace llarp }; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; } // namespace routing } // namespace llarp diff --git a/llarp/messages/path_transfer.hpp b/llarp/messages/path_transfer.hpp index 97b333026..ee6814430 100644 --- a/llarp/messages/path_transfer.hpp +++ b/llarp/messages/path_transfer.hpp @@ -31,7 +31,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(IMessageHandler*, llarp::Router* r) const override; + HandleMessage(IMessageHandler*, AbstractRouter* r) const override; void Clear() override diff --git a/llarp/messages/relay.cpp b/llarp/messages/relay.cpp index 4aff164b4..d6694471a 100644 --- a/llarp/messages/relay.cpp +++ b/llarp/messages/relay.cpp @@ -1,6 +1,7 @@ #include -#include +#include +#include #include namespace llarp @@ -58,9 +59,9 @@ namespace llarp } bool - RelayUpstreamMessage::HandleMessage(llarp::Router *r) const + RelayUpstreamMessage::HandleMessage(AbstractRouter *r) const { - auto path = r->paths.GetByDownstream(session->GetPubKey(), pathid); + auto path = r->pathContext().GetByDownstream(session->GetPubKey(), pathid); if(path) { return path->HandleUpstream(llarp_buffer_t(X), Y, r); @@ -121,9 +122,9 @@ namespace llarp } bool - RelayDownstreamMessage::HandleMessage(llarp::Router *r) const + RelayDownstreamMessage::HandleMessage(AbstractRouter *r) const { - auto path = r->paths.GetByUpstream(session->GetPubKey(), pathid); + auto path = r->pathContext().GetByUpstream(session->GetPubKey(), pathid); if(path) { return path->HandleDownstream(llarp_buffer_t(X), Y, r); diff --git a/llarp/messages/relay.hpp b/llarp/messages/relay.hpp index 8d236e489..7b4b10be3 100644 --- a/llarp/messages/relay.hpp +++ b/llarp/messages/relay.hpp @@ -27,7 +27,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; void Clear() override; @@ -49,7 +49,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; void Clear() override; diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index d63f0cd5a..1a7bb7e69 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -2,10 +2,11 @@ #include #include -#include +#include #include #include #include +#include namespace llarp { @@ -60,7 +61,7 @@ namespace llarp } bool - LR_CommitMessage::HandleMessage(llarp::Router* router) const + LR_CommitMessage::HandleMessage(AbstractRouter* router) const { if(frames.size() != MAXHOPS) { @@ -68,12 +69,12 @@ namespace llarp "!=", MAXHOPS); return false; } - if(!router->paths.AllowingTransit()) + if(!router->pathContext().AllowingTransit()) { llarp::LogError("got LRCM when not permitting transit"); return false; } - return AsyncDecrypt(&router->paths); + return AsyncDecrypt(&router->pathContext()); } bool diff --git a/llarp/messages/relay_commit.hpp b/llarp/messages/relay_commit.hpp index a37aea74d..d68fce51e 100644 --- a/llarp/messages/relay_commit.hpp +++ b/llarp/messages/relay_commit.hpp @@ -13,6 +13,7 @@ namespace llarp { // forward declare + struct AbstractRouter; namespace path { struct PathContext; @@ -63,7 +64,7 @@ namespace llarp BEncode(llarp_buffer_t *buf) const; bool - HandleMessage(llarp::Router *router) const; + HandleMessage(AbstractRouter *router) const; bool AsyncDecrypt(llarp::path::PathContext *context) const; diff --git a/llarp/messages/transfer_traffic.hpp b/llarp/messages/transfer_traffic.hpp index 369d599d0..c56e05617 100644 --- a/llarp/messages/transfer_traffic.hpp +++ b/llarp/messages/transfer_traffic.hpp @@ -45,7 +45,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; } // namespace routing } // namespace llarp diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index 628fd2a4f..fea49d161 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -2,8 +2,13 @@ #include #include +#include +#include +#include +#include #include -#include +#include +#include #include #include @@ -13,7 +18,7 @@ namespace llarp { namespace path { - PathContext::PathContext(llarp::Router* router) + PathContext::PathContext(AbstractRouter* router) : m_Router(router), m_AllowTransit(false) { } @@ -37,25 +42,25 @@ namespace llarp llarp_threadpool* PathContext::Worker() { - return m_Router->tp; + return m_Router->threadpool(); } - llarp::Crypto* + Crypto* PathContext::Crypto() { return m_Router->crypto(); } - llarp::Logic* + Logic* PathContext::Logic() { return m_Router->logic(); } - llarp::SecretKey& + const SecretKey& PathContext::EncryptionSecretKey() { - return m_Router->encryption; + return m_Router->encryption(); } bool @@ -69,7 +74,7 @@ namespace llarp PathContext::ForwardLRCM(const RouterID& nextHop, const std::array< EncryptedFrame, 8 >& frames) { - llarp::LogDebug("forwarding LRCM to ", nextHop); + LogDebug("forwarding LRCM to ", nextHop); LR_CommitMessage msg; msg.frames = frames; return m_Router->SendToOrQueue(nextHop, &msg); @@ -217,7 +222,7 @@ namespace llarp return m_Router->pubkey(); } - llarp::Router* + AbstractRouter* PathContext::Router() { return m_Router; @@ -424,13 +429,12 @@ namespace llarp } else if(st == ePathBuilding) { - llarp::LogInfo("path ", Name(), " is building"); + LogInfo("path ", Name(), " is building"); buildStarted = now; } else if(st == ePathEstablished && _status == ePathBuilding) { - llarp::LogInfo("path ", Name(), " is built, took ", now - buildStarted, - " ms"); + LogInfo("path ", Name(), " is built, took ", now - buildStarted, " ms"); } _status = st; } @@ -489,7 +493,7 @@ namespace llarp } void - Path::Tick(llarp_time_t now, llarp::Router* r) + Path::Tick(llarp_time_t now, AbstractRouter* r) { if(Expired(now)) return; @@ -501,7 +505,7 @@ namespace llarp auto dlt = now - buildStarted; if(dlt >= PATH_BUILD_TIMEOUT) { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); return; } @@ -514,8 +518,8 @@ namespace llarp auto dlt = now - m_LastLatencyTestTime; if(dlt > 5000 && m_LastLatencyTestID == 0) { - llarp::routing::PathLatencyMessage latency; - latency.T = llarp::randint(); + routing::PathLatencyMessage latency; + latency.T = randint(); m_LastLatencyTestID = latency.T; m_LastLatencyTestTime = now; SendRoutingMessage(&latency, r); @@ -526,7 +530,7 @@ namespace llarp && now - m_LastRecvMessage > PATH_ALIVE_TIMEOUT) { // TODO: send close exit message - // r->routerProfiling.MarkPathFail(this); + // r->routerProfiling().MarkPathFail(this); // EnterState(ePathTimeout, now); return; } @@ -538,19 +542,19 @@ namespace llarp { if(m_CheckForDead(this, dlt)) { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); } } else { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); } } else if(dlt >= 10000 && m_LastRecvMessage == 0) { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); } } @@ -572,7 +576,7 @@ namespace llarp msg.pathid = TXID(); if(r->SendToOrQueue(Upstream(), &msg)) return true; - llarp::LogError("send to ", Upstream(), " failed"); + LogError("send to ", Upstream(), " failed"); return false; } @@ -613,7 +617,7 @@ namespace llarp { if(!r->ParseRoutingMessageBuffer(buf, this, RXID())) { - llarp::LogWarn("Failed to parse inbound routing message"); + LogWarn("Failed to parse inbound routing message"); return false; } m_LastRecvMessage = r->Now(); @@ -622,7 +626,7 @@ namespace llarp bool Path::HandleUpdateExitVerifyMessage( - const llarp::routing::UpdateExitVerifyMessage* msg, llarp::Router* r) + const routing::UpdateExitVerifyMessage* msg, AbstractRouter* r) { (void)r; if(m_UpdateExitTX && msg->T == m_UpdateExitTX) @@ -639,19 +643,18 @@ namespace llarp } bool - Path::SendRoutingMessage(const llarp::routing::IMessage* msg, - llarp::AbstractRouter* r) + Path::SendRoutingMessage(const routing::IMessage* msg, AbstractRouter* r) { std::array< byte_t, MAX_LINK_MSG_SIZE / 2 > tmp; llarp_buffer_t buf(tmp); - // should help prevent bad paths with uninitialised members - // FIXME: Why would we get uninitialised IMessages? + // should help prevent bad paths with uninitialized members + // FIXME: Why would we get uninitialized IMessages? if(msg->version != LLARP_PROTO_VERSION) return false; if(!msg->BEncode(&buf)) { - llarp::LogError("Bencode failed"); - llarp::DumpBuffer(buf); + LogError("Bencode failed"); + DumpBuffer(buf); return false; } // make nonce @@ -670,18 +673,18 @@ namespace llarp } bool - Path::HandlePathTransferMessage( - __attribute__((unused)) const llarp::routing::PathTransferMessage* msg, - __attribute__((unused)) llarp::Router* r) + Path::HandlePathTransferMessage(__attribute__((unused)) + const routing::PathTransferMessage* msg, + __attribute__((unused)) AbstractRouter* r) { - llarp::LogWarn("unwarranted path transfer message on tx=", TXID(), - " rx=", RXID()); + LogWarn("unwarranted path transfer message on tx=", TXID(), + " rx=", RXID()); return false; } bool - Path::HandleDataDiscardMessage( - const llarp::routing::DataDiscardMessage* msg, llarp::Router* r) + Path::HandleDataDiscardMessage(const routing::DataDiscardMessage* msg, + AbstractRouter* r) { MarkActive(r->Now()); if(m_DropHandler) @@ -690,9 +693,9 @@ namespace llarp } bool - Path::HandlePathConfirmMessage( - __attribute__((unused)) const llarp::routing::PathConfirmMessage* msg, - llarp::Router* r) + Path::HandlePathConfirmMessage(__attribute__((unused)) + const routing::PathConfirmMessage* msg, + AbstractRouter* r) { auto now = r->Now(); if(_status == ePathBuilding) @@ -700,33 +703,33 @@ namespace llarp // finish initializing introduction intro.expiresAt = buildStarted + hops[0].lifetime; - r->routerProfiling.MarkPathSuccess(this); + r->routerProfiling().MarkPathSuccess(this); // persist session with upstream router until the path is done r->PersistSessionUntil(Upstream(), intro.expiresAt); MarkActive(now); // send path latency test - llarp::routing::PathLatencyMessage latency; - latency.T = llarp::randint(); + routing::PathLatencyMessage latency; + latency.T = randint(); m_LastLatencyTestID = latency.T; m_LastLatencyTestTime = now; return SendRoutingMessage(&latency, r); } - llarp::LogWarn("got unwarrented path confirm message on tx=", RXID(), - " rx=", RXID()); + LogWarn("got unwarranted path confirm message on tx=", RXID(), + " rx=", RXID()); return false; } bool - Path::HandleHiddenServiceFrame(const llarp::service::ProtocolFrame* frame) + Path::HandleHiddenServiceFrame(const service::ProtocolFrame* frame) { MarkActive(m_PathSet->Now()); return m_DataHandler && m_DataHandler(this, frame); } bool - Path::HandlePathLatencyMessage( - const llarp::routing::PathLatencyMessage* msg, llarp::Router* r) + Path::HandlePathLatencyMessage(const routing::PathLatencyMessage* msg, + AbstractRouter* r) { auto now = r->Now(); MarkActive(now); @@ -743,15 +746,15 @@ namespace llarp } else { - llarp::LogWarn("unwarrented path latency message via ", Upstream()); + LogWarn("unwarranted path latency message via ", Upstream()); return false; } } bool - Path::HandleDHTMessage(const llarp::dht::IMessage* msg, llarp::Router* r) + Path::HandleDHTMessage(const dht::IMessage* msg, AbstractRouter* r) { - llarp::routing::DHTMessage reply; + routing::DHTMessage reply; if(!msg->HandleMessage(r->dht(), reply.M)) return false; MarkActive(r->Now()); @@ -761,102 +764,101 @@ namespace llarp } bool - Path::HandleCloseExitMessage(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) + Path::HandleCloseExitMessage(const routing::CloseExitMessage* msg, + AbstractRouter* r) { /// allows exits to close from their end if(SupportsAnyRoles(ePathRoleExit | ePathRoleSVC)) { if(msg->Verify(r->crypto(), EndpointPubKey())) { - llarp::LogInfo(Name(), " had its exit closed"); + LogInfo(Name(), " had its exit closed"); _role &= ~ePathRoleExit; return true; } else - llarp::LogError(Name(), " CXM from exit with bad signature"); + LogError(Name(), " CXM from exit with bad signature"); } else - llarp::LogError(Name(), " unwarrented CXM"); + LogError(Name(), " unwarranted CXM"); return false; } bool - Path::SendExitRequest(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) + Path::SendExitRequest(const routing::ObtainExitMessage* msg, + AbstractRouter* r) { - llarp::LogInfo(Name(), " sending exit request to ", Endpoint()); + LogInfo(Name(), " sending exit request to ", Endpoint()); m_ExitObtainTX = msg->T; return SendRoutingMessage(msg, r); } bool - Path::SendExitClose(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) + Path::SendExitClose(const routing::CloseExitMessage* msg, AbstractRouter* r) { - llarp::LogInfo(Name(), " closing exit to ", Endpoint()); + LogInfo(Name(), " closing exit to ", Endpoint()); // mark as not exit anymore _role &= ~ePathRoleExit; return SendRoutingMessage(msg, r); } bool - Path::HandleObtainExitMessage(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) + Path::HandleObtainExitMessage(const routing::ObtainExitMessage* msg, + AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(Name(), " got unwarrented OXM"); + LogError(Name(), " got unwarranted OXM"); return false; } bool - Path::HandleUpdateExitMessage(const llarp::routing::UpdateExitMessage* msg, - llarp::Router* r) + Path::HandleUpdateExitMessage(const routing::UpdateExitMessage* msg, + AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(Name(), " got unwarrented UXM"); + LogError(Name(), " got unwarranted UXM"); return false; } bool - Path::HandleRejectExitMessage(const llarp::routing::RejectExitMessage* msg, - llarp::Router* r) + Path::HandleRejectExitMessage(const routing::RejectExitMessage* msg, + AbstractRouter* r) { if(m_ExitObtainTX && msg->T == m_ExitObtainTX) { if(!msg->Verify(r->crypto(), EndpointPubKey())) { - llarp::LogError(Name(), "RXM invalid signature"); + LogError(Name(), "RXM invalid signature"); return false; } - llarp::LogInfo(Name(), " ", Endpoint(), " Rejected exit"); + LogInfo(Name(), " ", Endpoint(), " Rejected exit"); MarkActive(r->Now()); return InformExitResult(msg->B); } - llarp::LogError(Name(), " got unwarrented RXM"); + LogError(Name(), " got unwarranted RXM"); return false; } bool - Path::HandleGrantExitMessage(const llarp::routing::GrantExitMessage* msg, - llarp::Router* r) + Path::HandleGrantExitMessage(const routing::GrantExitMessage* msg, + AbstractRouter* r) { if(m_ExitObtainTX && msg->T == m_ExitObtainTX) { if(!msg->Verify(r->crypto(), EndpointPubKey())) { - llarp::LogError(Name(), " GXM signature failed"); + LogError(Name(), " GXM signature failed"); return false; } // we now can send exit traffic _role |= ePathRoleExit; - llarp::LogInfo(Name(), " ", Endpoint(), " Granted exit"); + LogInfo(Name(), " ", Endpoint(), " Granted exit"); MarkActive(r->Now()); return InformExitResult(0); } - llarp::LogError(Name(), " got unwarrented GXM"); + LogError(Name(), " got unwarranted GXM"); return false; } @@ -872,7 +874,7 @@ namespace llarp bool Path::HandleTransferTrafficMessage( - const llarp::routing::TransferTrafficMessage* msg, llarp::Router* r) + const routing::TransferTrafficMessage* msg, AbstractRouter* r) { // check if we can handle exit data if(!SupportsAnyRoles(ePathRoleExit | ePathRoleSVC)) diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 91c637f0a..ec3f81f71 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -32,10 +32,12 @@ namespace llarp { + class Logic; struct AbstractRouter; struct Crypto; struct LR_CommitMessage; struct LR_CommitRecord; + namespace path { struct TransitHopInfo @@ -198,46 +200,46 @@ namespace llarp bool HandleDataDiscardMessage(const llarp::routing::DataDiscardMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathConfirmMessage(const llarp::routing::PathConfirmMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathTransferMessage(const llarp::routing::PathTransferMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathLatencyMessage(const llarp::routing::PathLatencyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleObtainExitMessage(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitVerifyMessage( const llarp::routing::UpdateExitVerifyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleTransferTrafficMessage( const llarp::routing::TransferTrafficMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitMessage(const llarp::routing::UpdateExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleGrantExitMessage(const llarp::routing::GrantExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleRejectExitMessage(const llarp::routing::RejectExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleCloseExitMessage(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleHiddenServiceFrame(__attribute__(( @@ -253,7 +255,7 @@ namespace llarp bool HandleDHTMessage(const llarp::dht::IMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; // handle data in upstream direction bool @@ -422,7 +424,7 @@ namespace llarp Expired(llarp_time_t now) const override; void - Tick(llarp_time_t now, llarp::Router* r); + Tick(llarp_time_t now, AbstractRouter* r); bool SendRoutingMessage(const llarp::routing::IMessage* msg, @@ -430,47 +432,47 @@ namespace llarp bool HandleObtainExitMessage(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitVerifyMessage( const llarp::routing::UpdateExitVerifyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleTransferTrafficMessage( const llarp::routing::TransferTrafficMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitMessage(const llarp::routing::UpdateExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleCloseExitMessage(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleGrantExitMessage(const llarp::routing::GrantExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleRejectExitMessage(const llarp::routing::RejectExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleDataDiscardMessage(const llarp::routing::DataDiscardMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathConfirmMessage(const llarp::routing::PathConfirmMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathLatencyMessage(const llarp::routing::PathLatencyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathTransferMessage(const llarp::routing::PathTransferMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleHiddenServiceFrame( @@ -481,7 +483,7 @@ namespace llarp bool HandleDHTMessage(const llarp::dht::IMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleRoutingMessage(const llarp_buffer_t& buf, AbstractRouter* r); @@ -530,11 +532,11 @@ namespace llarp bool SendExitRequest(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r); + AbstractRouter* r); bool SendExitClose(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r); + AbstractRouter* r); private: /// call obtained exit hooks @@ -568,7 +570,7 @@ namespace llarp struct PathContext { - PathContext(llarp::Router* router); + PathContext(AbstractRouter* router); ~PathContext(); /// called from router tick function @@ -665,17 +667,17 @@ namespace llarp llarp::Logic* Logic(); - llarp::Router* + AbstractRouter* Router(); - llarp::SecretKey& + const llarp::SecretKey& EncryptionSecretKey(); const byte_t* OurRouterID() const; private: - llarp::Router* m_Router; + AbstractRouter* m_Router; SyncTransitMap_t m_TransitPaths; SyncTransitMap_t m_Paths; SyncOwnedPathsMap_t m_OurPaths; diff --git a/llarp/path/path_types.hpp b/llarp/path/path_types.hpp index 7f82dc386..8256c383c 100644 --- a/llarp/path/path_types.hpp +++ b/llarp/path/path_types.hpp @@ -6,7 +6,10 @@ namespace llarp { - using PathID_t = AlignedBuffer< PATHIDSIZE >; -} + struct PathID_t final : public AlignedBuffer< PATHIDSIZE > + { + }; + +} // namespace llarp #endif diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 9cdaf225f..35f902aaa 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -1,9 +1,12 @@ #include +#include #include #include -#include +#include +#include #include +#include #include @@ -21,7 +24,7 @@ namespace llarp Handler result; size_t idx = 0; - llarp::Router* router = nullptr; + AbstractRouter* router = nullptr; llarp_threadpool* worker = nullptr; llarp::Logic* logic = nullptr; llarp::Crypto* crypto = nullptr; @@ -123,7 +126,7 @@ namespace llarp { } - /// Generate all keys asynchronously and call hadler when done + /// Generate all keys asynchronously and call handler when done void AsyncGenerateKeys(Path_t* p, llarp::Logic* l, llarp_threadpool* pool, User* u, Handler func) @@ -154,7 +157,7 @@ namespace llarp // persist session with router until this path is done ctx->router->PersistSessionUntil(remote, ctx->path->ExpireTime()); // add own path - ctx->router->paths.AddOwnPath(ctx->pathset, ctx->path); + ctx->router->pathContext().AddOwnPath(ctx->pathset, ctx->path); ctx->path = nullptr; } else @@ -166,7 +169,7 @@ namespace llarp namespace path { - Builder::Builder(llarp::Router* p_router, struct llarp_dht_context* p_dht, + Builder::Builder(AbstractRouter* p_router, struct llarp_dht_context* p_dht, size_t pathNum, size_t hops) : llarp::path::PathSet(pathNum) , router(p_router) @@ -181,7 +184,7 @@ namespace llarp Builder::~Builder() { - router->paths.RemovePathBuilder(this); + router->pathContext().RemovePathBuilder(this); } util::StatusObject @@ -215,7 +218,7 @@ namespace llarp --tries; if(db->select_random_hop(prev, cur, hop)) return true; - } while(router->routerProfiling.IsBad(cur.pubkey) && tries > 0); + } while(router->routerProfiling().IsBad(cur.pubkey) && tries > 0); return false; } @@ -317,7 +320,7 @@ namespace llarp path->SetBuildResultHook(std::bind(&llarp::path::Builder::HandlePathBuilt, this, std::placeholders::_1)); ++keygens; - ctx->AsyncGenerateKeys(path, router->logic(), router->tp, this, + ctx->AsyncGenerateKeys(path, router->logic(), router->threadpool(), this, &PathBuilderKeysGenerated); } diff --git a/llarp/path/pathbuilder.hpp b/llarp/path/pathbuilder.hpp index c8c09d0e2..160145ee9 100644 --- a/llarp/path/pathbuilder.hpp +++ b/llarp/path/pathbuilder.hpp @@ -22,7 +22,7 @@ namespace llarp std::atomic< bool > _run; public: - llarp::Router* router; + AbstractRouter* router; llarp_dht_context* dht; llarp::SecretKey enckey; size_t numHops; @@ -33,7 +33,7 @@ namespace llarp std::atomic< uint8_t > keygens; /// construct - Builder(llarp::Router* p_router, llarp_dht_context* p_dht, + Builder(AbstractRouter* p_router, llarp_dht_context* p_dht, size_t numPaths, size_t numHops); virtual ~Builder(); diff --git a/llarp/path/pathset.cpp b/llarp/path/pathset.cpp index 1aa8558bd..48835d69f 100644 --- a/llarp/path/pathset.cpp +++ b/llarp/path/pathset.cpp @@ -58,7 +58,7 @@ namespace llarp } void - PathSet::Tick(llarp_time_t now, llarp::Router* r) + PathSet::Tick(llarp_time_t now, AbstractRouter* r) { Lock_t l(m_PathsMutex); for(auto& item : m_Paths) diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 894a3e6a2..dcaec43cd 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -65,7 +65,7 @@ namespace llarp /// tick owned paths void - Tick(llarp_time_t now, llarp::Router* r); + Tick(llarp_time_t now, AbstractRouter* r); /// count the number of paths that will exist at this timestamp in future size_t @@ -186,7 +186,7 @@ namespace llarp std::set< llarp::service::Introduction >& intros) const; virtual bool - PublishIntroSet(__attribute__((unused)) llarp::Router* r) + PublishIntroSet(__attribute__((unused)) AbstractRouter* r) { return false; } diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index f191e23da..ca2a524b1 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -1,8 +1,13 @@ #include #include +#include #include -#include +#include +#include +#include +#include +#include #include #include #include @@ -122,14 +127,14 @@ namespace llarp bool TransitHop::HandleDHTMessage(const llarp::dht::IMessage* msg, - llarp::Router* r) + AbstractRouter* r) { return r->dht()->impl.RelayRequestForPath(info.rxID, msg); } bool TransitHop::HandlePathLatencyMessage( - const llarp::routing::PathLatencyMessage* msg, llarp::Router* r) + const llarp::routing::PathLatencyMessage* msg, AbstractRouter* r) { llarp::routing::PathLatencyMessage reply; reply.L = msg->T; @@ -139,7 +144,7 @@ namespace llarp bool TransitHop::HandlePathConfirmMessage( __attribute__((unused)) const llarp::routing::PathConfirmMessage* msg, - __attribute__((unused)) llarp::Router* r) + __attribute__((unused)) AbstractRouter* r) { llarp::LogWarn("unwarranted path confirm message on ", info); return false; @@ -148,7 +153,7 @@ namespace llarp bool TransitHop::HandleDataDiscardMessage( __attribute__((unused)) const llarp::routing::DataDiscardMessage* msg, - __attribute__((unused)) llarp::Router* r) + __attribute__((unused)) AbstractRouter* r) { llarp::LogWarn("unwarranted path data discard message on ", info); return false; @@ -156,15 +161,15 @@ namespace llarp bool TransitHop::HandleObtainExitMessage( - const llarp::routing::ObtainExitMessage* msg, llarp::Router* r) + const llarp::routing::ObtainExitMessage* msg, AbstractRouter* r) { if(msg->Verify(r->crypto()) - && r->exitContext.ObtainNewExit(msg->I, info.rxID, msg->E != 0)) + && r->exitContext().ObtainNewExit(msg->I, info.rxID, msg->E != 0)) { llarp::routing::GrantExitMessage grant; grant.S = NextSeqNo(); grant.T = msg->T; - if(!grant.Sign(r->crypto(), r->identity)) + if(!grant.Sign(r->crypto(), r->identity())) { llarp::LogError("Failed to sign grant exit message"); return false; @@ -176,7 +181,7 @@ namespace llarp llarp::routing::RejectExitMessage reject; reject.S = NextSeqNo(); reject.T = msg->T; - if(!reject.Sign(r->crypto(), r->identity)) + if(!reject.Sign(r->crypto(), r->identity())) { llarp::LogError("Failed to sign reject exit message"); return false; @@ -186,17 +191,17 @@ namespace llarp bool TransitHop::HandleCloseExitMessage( - const llarp::routing::CloseExitMessage* msg, llarp::Router* r) + const llarp::routing::CloseExitMessage* msg, AbstractRouter* r) { llarp::routing::DataDiscardMessage discard(info.rxID, msg->S); - auto ep = r->exitContext.FindEndpointForPath(info.rxID); + auto ep = r->exitContext().FindEndpointForPath(info.rxID); if(ep && msg->Verify(r->crypto(), ep->PubKey())) { ep->Close(); // ep is now gone af llarp::routing::CloseExitMessage reply; reply.S = NextSeqNo(); - if(reply.Sign(r->crypto(), r->identity)) + if(reply.Sign(r->crypto(), r->identity())) return SendRoutingMessage(&reply, r); } return SendRoutingMessage(&discard, r); @@ -204,7 +209,7 @@ namespace llarp bool TransitHop::HandleUpdateExitVerifyMessage( - const llarp::routing::UpdateExitVerifyMessage* msg, llarp::Router* r) + const llarp::routing::UpdateExitVerifyMessage* msg, AbstractRouter* r) { (void)msg; (void)r; @@ -214,9 +219,9 @@ namespace llarp bool TransitHop::HandleUpdateExitMessage( - const llarp::routing::UpdateExitMessage* msg, llarp::Router* r) + const llarp::routing::UpdateExitMessage* msg, AbstractRouter* r) { - auto ep = r->exitContext.FindEndpointForPath(msg->P); + auto ep = r->exitContext().FindEndpointForPath(msg->P); if(ep) { if(!msg->Verify(r->crypto(), ep->PubKey())) @@ -237,29 +242,29 @@ namespace llarp bool TransitHop::HandleRejectExitMessage( - const llarp::routing::RejectExitMessage* msg, llarp::Router* r) + const llarp::routing::RejectExitMessage* msg, AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(info, " got unwarrented RXM"); + llarp::LogError(info, " got unwarranted RXM"); return false; } bool TransitHop::HandleGrantExitMessage( - const llarp::routing::GrantExitMessage* msg, llarp::Router* r) + const llarp::routing::GrantExitMessage* msg, AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(info, " got unwarrented GXM"); + llarp::LogError(info, " got unwarranted GXM"); return false; } bool TransitHop::HandleTransferTrafficMessage( - const llarp::routing::TransferTrafficMessage* msg, llarp::Router* r) + const llarp::routing::TransferTrafficMessage* msg, AbstractRouter* r) { - auto endpoint = r->exitContext.FindEndpointForPath(info.rxID); + auto endpoint = r->exitContext().FindEndpointForPath(info.rxID); if(endpoint) { bool sent = true; @@ -284,9 +289,9 @@ namespace llarp bool TransitHop::HandlePathTransferMessage( - const llarp::routing::PathTransferMessage* msg, llarp::Router* r) + const llarp::routing::PathTransferMessage* msg, AbstractRouter* r) { - auto path = r->paths.GetPathForTransfer(msg->P); + auto path = r->pathContext().GetPathForTransfer(msg->P); llarp::routing::DataDiscardMessage discarded(msg->P, msg->S); if(!path) { diff --git a/llarp/router/abstractrouter.cpp b/llarp/router/abstractrouter.cpp new file mode 100644 index 000000000..b0da46e73 --- /dev/null +++ b/llarp/router/abstractrouter.cpp @@ -0,0 +1,8 @@ +#include + +namespace llarp +{ + AbstractRouter::~AbstractRouter() + { + } +} // namespace llarp diff --git a/llarp/router/abstractrouter.hpp b/llarp/router/abstractrouter.hpp new file mode 100644 index 000000000..a5c96dbac --- /dev/null +++ b/llarp/router/abstractrouter.hpp @@ -0,0 +1,144 @@ +#ifndef LLARP_ABSTRACT_ROUTER_HPP +#define LLARP_ABSTRACT_ROUTER_HPP + +#include + +#include + +struct llarp_buffer_t; +struct llarp_dht_context; +struct llarp_ev_loop; +struct llarp_nodedb; +struct llarp_threadpool; + +namespace llarp +{ + struct Crypto; + class Logic; + struct RouterContact; + struct RouterID; + struct ILinkMessage; + struct ILinkSession; + struct PathID_t; + struct Profiling; + struct SecretKey; + struct Signature; + + namespace exit + { + struct Context; + } + + namespace path + { + struct PathContext; + } + + namespace routing + { + struct IMessageHandler; + } + + struct AbstractRouter + { + virtual ~AbstractRouter() = 0; + + virtual void + OnSessionEstablished(RouterContact rc) = 0; + + virtual bool + HandleRecvLinkMessageBuffer(ILinkSession *from, + const llarp_buffer_t &msg) = 0; + + virtual Logic * + logic() const = 0; + + virtual llarp_dht_context * + dht() const = 0; + + virtual Crypto * + crypto() const = 0; + + virtual llarp_nodedb * + nodedb() const = 0; + + virtual const path::PathContext & + pathContext() const = 0; + + virtual path::PathContext & + pathContext() = 0; + + virtual const RouterContact & + rc() const = 0; + + virtual exit::Context & + exitContext() = 0; + + virtual const SecretKey & + identity() const = 0; + + virtual const SecretKey & + encryption() const = 0; + + virtual Profiling & + routerProfiling() = 0; + + virtual llarp_ev_loop * + netloop() const = 0; + + virtual llarp_threadpool * + threadpool() = 0; + + virtual llarp_threadpool * + diskworker() = 0; + + virtual bool + Sign(Signature &sig, const llarp_buffer_t &buf) const = 0; + + virtual const byte_t * + pubkey() const = 0; + + virtual void + OnConnectTimeout(ILinkSession *session) = 0; + + /// called by link when a remote session has no more sessions open + virtual void + SessionClosed(RouterID remote) = 0; + + virtual llarp_time_t + Now() const = 0; + + virtual bool + GetRandomGoodRouter(RouterID &r) = 0; + + virtual bool + SendToOrQueue(const RouterID &remote, const ILinkMessage *msg) = 0; + + virtual void + PersistSessionUntil(const RouterID &remote, llarp_time_t until) = 0; + + virtual bool + ParseRoutingMessageBuffer(const llarp_buffer_t &buf, + routing::IMessageHandler *h, + const PathID_t &rxid) = 0; + + virtual size_t + NumberOfConnectedRouters() const = 0; + + virtual bool + GetRandomConnectedRouter(RouterContact &result) const = 0; + + virtual void + HandleDHTLookupForExplore(RouterID remote, + const std::vector< RouterContact > &results) = 0; + + /// check if newRc matches oldRC and update local rc for this remote contact + /// if valid + /// returns true on valid and updated + /// returns false otherwise + virtual bool + CheckRenegotiateValid(RouterContact newRc, RouterContact oldRC) = 0; + }; +} // namespace llarp + +#endif diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index c9e4d7a3c..324f803db 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -68,7 +68,7 @@ struct TryConnectJob void AttemptTimedout() { - router->routerProfiling.MarkTimeout(rc.pubkey); + router->routerProfiling().MarkTimeout(rc.pubkey); if(ShouldRetry()) { Attempt(); @@ -76,7 +76,7 @@ struct TryConnectJob } if(!router->IsServiceNode()) { - if(router->routerProfiling.IsBad(rc.pubkey)) + if(router->routerProfiling().IsBad(rc.pubkey)) { router->nodedb()->Remove(rc.pubkey); } @@ -168,12 +168,12 @@ namespace llarp } bool - Router::TryConnectAsync(llarp::RouterContact remote, uint16_t numretries) + Router::TryConnectAsync(RouterContact remote, uint16_t numretries) { // do we already have a pending job for this remote? if(HasPendingConnectJob(remote.pubkey)) { - llarp::LogDebug("We have pending connect jobs to ", remote.pubkey); + LogDebug("We have pending connect jobs to ", remote.pubkey); return false; } @@ -197,21 +197,21 @@ namespace llarp } void - Router::OnSessionEstablished(llarp::RouterContact rc) + Router::OnSessionEstablished(RouterContact rc) { async_verify_RC(rc, nullptr); - llarp::LogInfo("session with ", rc.pubkey, " established"); + LogInfo("session with ", rc.pubkey, " established"); } - Router::Router(struct llarp_threadpool *_tp, struct llarp_ev_loop *_netloop, - llarp::Logic *l) + Router::Router(struct llarp_threadpool *_tp, struct llarp_ev_loop *__netloop, + Logic *l) : ready(false) - , netloop(_netloop) + , _netloop(__netloop) , tp(_tp) , _logic(l) , _crypto(std::make_unique< sodium::CryptoLibSodium >()) , paths(this) - , exitContext(this) + , _exitContext(this) , _dht(llarp_dht_context_new(this)) , inbound_link_msg_parser(this) , hiddenServiceContext(this) @@ -239,12 +239,12 @@ namespace llarp { util::StatusObject obj{{"dht", _dht->impl.ExtractStatus()}, {"services", hiddenServiceContext.ExtractStatus()}, - {"exit", exitContext.ExtractStatus()}}; + {"exit", _exitContext.ExtractStatus()}}; return obj; } bool - Router::HandleRecvLinkMessageBuffer(llarp::ILinkSession *session, + Router::HandleRecvLinkMessageBuffer(ILinkSession *session, const llarp_buffer_t &buf) { if(_stopping) @@ -252,16 +252,16 @@ namespace llarp if(!session) { - llarp::LogWarn("no link session"); + LogWarn("no link session"); return false; } return inbound_link_msg_parser.ProcessFrom(session, buf); } void - Router::PersistSessionUntil(const llarp::RouterID &remote, llarp_time_t until) + Router::PersistSessionUntil(const RouterID &remote, llarp_time_t until) { - llarp::LogDebug("persist session to ", remote, " until ", until); + LogDebug("persist session to ", remote, " until ", until); m_PersistingSessions[remote] = std::max(until, m_PersistingSessions[remote]); } @@ -282,8 +282,7 @@ namespace llarp constexpr size_t MaxPendingSendQueueSize = 8; bool - Router::SendToOrQueue(const llarp::RouterID &remote, - const llarp::ILinkMessage *msg) + Router::SendToOrQueue(const RouterID &remote, const ILinkMessage *msg) { for(const auto &link : inboundLinks) { @@ -303,7 +302,7 @@ namespace llarp } // no link available - // this will create an entry in the obmq if it's not already there + // this will create an entry in the outbound mq if it's not already there auto itr = outboundMessageQueue.find(remote); if(itr == outboundMessageQueue.end()) { @@ -324,10 +323,10 @@ namespace llarp } else { - llarp::LogWarn("tried to queue a message to ", remote, - " but the queue is full so we drop it like it's hawt"); + LogWarn("tried to queue a message to ", remote, + " but the queue is full so we drop it like it's hawt"); } - llarp::RouterContact remoteRC; + RouterContact remoteRC; // we don't have an open session to that router right now if(nodedb()->Get(remote, remoteRC)) { @@ -344,9 +343,8 @@ namespace llarp } void - Router::HandleDHTLookupForSendTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) + Router::HandleDHTLookupForSendTo(RouterID remote, + const std::vector< RouterContact > &results) { if(results.size()) { @@ -367,58 +365,58 @@ namespace llarp void Router::ForEachPeer( - std::function< void(const llarp::ILinkSession *, bool) > visit) const + std::function< void(const ILinkSession *, bool) > visit) const { for(const auto &link : outboundLinks) { link->ForEachSession( - [visit](const llarp::ILinkSession *peer) { visit(peer, true); }); + [visit](const ILinkSession *peer) { visit(peer, true); }); } for(const auto &link : inboundLinks) { link->ForEachSession( - [visit](const llarp::ILinkSession *peer) { visit(peer, false); }); + [visit](const ILinkSession *peer) { visit(peer, false); }); } } void - Router::ForEachPeer(std::function< void(llarp::ILinkSession *) > visit) + Router::ForEachPeer(std::function< void(ILinkSession *) > visit) { for(const auto &link : inboundLinks) { - link->ForEachSession([visit](llarp::ILinkSession *peer) { visit(peer); }); + link->ForEachSession([visit](ILinkSession *peer) { visit(peer); }); } for(const auto &link : inboundLinks) { - link->ForEachSession([visit](llarp::ILinkSession *peer) { visit(peer); }); + link->ForEachSession([visit](ILinkSession *peer) { visit(peer); }); } } void Router::try_connect(fs::path rcfile) { - llarp::RouterContact remote; + RouterContact remote; if(!remote.Read(rcfile.string().c_str())) { - llarp::LogError("failure to decode or verify of remote RC"); + LogError("failure to decode or verify of remote RC"); return; } if(remote.Verify(crypto(), Now())) { - llarp::LogDebug("verified signature"); + LogDebug("verified signature"); // store into filesystem if(!nodedb()->Insert(remote)) { - llarp::LogWarn("failed to store"); + LogWarn("failed to store"); } if(!TryConnectAsync(remote, 10)) { // or error? - llarp::LogWarn("session already made"); + LogWarn("session already made"); } } else - llarp::LogError(rcfile, " contains invalid RC"); + LogError(rcfile, " contains invalid RC"); } bool @@ -428,20 +426,20 @@ namespace llarp return false; if(usingSNSeed) return llarp_loadServiceNodeIdentityKey(crypto(), ident_keyfile, - identity); + _identity); else - return llarp_findOrCreateIdentity(crypto(), ident_keyfile, identity); + return llarp_findOrCreateIdentity(crypto(), ident_keyfile, _identity); } bool Router::EnsureEncryptionKey() { return llarp_findOrCreateEncryption(crypto(), encryption_keyfile, - this->encryption); + _encryption); } void - Router::AddInboundLink(std::unique_ptr< llarp::ILinkLayer > &link) + Router::AddInboundLink(std::unique_ptr< ILinkLayer > &link) { inboundLinks.insert(std::move(link)); } @@ -469,11 +467,11 @@ namespace llarp bool Router::SaveRC() { - llarp::LogDebug("verify RC signature"); + LogDebug("verify RC signature"); if(!_rc.Verify(crypto(), Now())) { rc().Dump< MAX_RC_SIZE >(); - llarp::LogError("RC is invalid, not saving"); + LogError("RC is invalid, not saving"); return false; } std::string fname = our_rc_file.string(); @@ -489,8 +487,8 @@ namespace llarp void Router::Close() { - llarp::LogInfo("closing router"); - llarp_ev_loop_stop(netloop); + LogInfo("closing router"); + llarp_ev_loop_stop(netloop()); inboundLinks.clear(); outboundLinks.clear(); } @@ -498,10 +496,10 @@ namespace llarp void Router::on_verify_client_rc(llarp_async_verify_rc *job) { - llarp::async_verify_context *ctx = - static_cast< llarp::async_verify_context * >(job->user); + async_verify_context *ctx = + static_cast< async_verify_context * >(job->user); auto router = ctx->router; - llarp::PubKey pk(job->rc.pubkey); + PubKey pk(job->rc.pubkey); router->FlushOutboundFor(pk, router->GetLinkWithSessionByPubkey(pk)); delete ctx; router->pendingVerifyRC.erase(pk); @@ -511,10 +509,10 @@ namespace llarp void Router::on_verify_server_rc(llarp_async_verify_rc *job) { - llarp::async_verify_context *ctx = - static_cast< llarp::async_verify_context * >(job->user); + async_verify_context *ctx = + static_cast< async_verify_context * >(job->user); auto router = ctx->router; - llarp::PubKey pk(job->rc.pubkey); + PubKey pk(job->rc.pubkey); if(!job->valid) { if(ctx->establish_job) @@ -530,14 +528,14 @@ namespace llarp } // we're valid, which means it's already been committed to the nodedb - llarp::LogDebug("rc verified and saved to nodedb"); + LogDebug("rc verified and saved to nodedb"); if(router->validRouters.count(pk)) { router->validRouters.erase(pk); } - llarp::RouterContact rc = job->rc; + RouterContact rc = job->rc; router->validRouters.emplace(pk, rc); @@ -545,7 +543,7 @@ namespace llarp router->dht()->impl.nodes->PutNode(rc); // mark success in profile - router->routerProfiling.MarkSuccess(pk); + router->routerProfiling().MarkSuccess(pk); // this was an outbound establish job if(ctx->establish_job) @@ -571,13 +569,14 @@ namespace llarp bool Router::ParseRoutingMessageBuffer(const llarp_buffer_t &buf, - routing::IMessageHandler *h, PathID_t rxid) + routing::IMessageHandler *h, + const PathID_t &rxid) { return inbound_routing_msg_parser.ParseMessageBuffer(buf, h, rxid, this); } bool - Router::ConnectionToRouterAllowed(const llarp::RouterID &router) const + Router::ConnectionToRouterAllowed(const RouterID &router) const { if(strictConnectPubkeys.size() && strictConnectPubkeys.count(router) == 0) return false; @@ -588,9 +587,8 @@ namespace llarp } void - Router::HandleDHTLookupForExplore( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) + Router::HandleDHTLookupForExplore(RouterID remote, + const std::vector< RouterContact > &results) { if(results.size() == 0) return; @@ -608,26 +606,25 @@ namespace llarp } void - Router::TryEstablishTo(const llarp::RouterID &remote) + Router::TryEstablishTo(const RouterID &remote) { if(!ConnectionToRouterAllowed(remote)) { - llarp::LogWarn("not connecting to ", remote, - " as it's not permitted by config"); + LogWarn("not connecting to ", remote, " as it's not permitted by config"); return; } - llarp::RouterContact rc; + RouterContact rc; if(nodedb()->Get(remote, rc)) { // try connecting async TryConnectAsync(rc, 5); } - else if(IsServiceNode() || !routerProfiling.IsBad(remote)) + else if(IsServiceNode() || !routerProfiling().IsBad(remote)) { if(dht()->impl.HasRouterLookup(remote)) return; - llarp::LogInfo("looking up router ", remote); + LogInfo("looking up router ", remote); // dht lookup as we don't know it dht()->impl.LookupRouter( remote, @@ -636,7 +633,7 @@ namespace llarp } else { - llarp::LogWarn("not connecting to ", remote, " as it's unreliable"); + LogWarn("not connecting to ", remote, " as it's unreliable"); } } @@ -652,13 +649,12 @@ namespace llarp void Router::HandleDHTLookupForTryEstablishTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) + RouterID remote, const std::vector< RouterContact > &results) { if(results.size() == 0) { if(!IsServiceNode()) - routerProfiling.MarkTimeout(remote); + routerProfiling().MarkTimeout(remote); } for(const auto &result : results) { @@ -679,28 +675,28 @@ namespace llarp bool Router::UpdateOurRC(bool rotateKeys) { - llarp::SecretKey nextOnionKey; - llarp::RouterContact nextRC = _rc; + SecretKey nextOnionKey; + RouterContact nextRC = _rc; if(rotateKeys) { crypto()->encryption_keygen(nextOnionKey); std::string f = encryption_keyfile.string(); if(nextOnionKey.SaveToFile(f.c_str())) { - nextRC.enckey = llarp::seckey_topublic(nextOnionKey); - encryption = nextOnionKey; + nextRC.enckey = seckey_topublic(nextOnionKey); + _encryption = nextOnionKey; } } nextRC.last_updated = Now(); - if(!nextRC.Sign(crypto(), identity)) + if(!nextRC.Sign(crypto(), identity())) return false; _rc = nextRC; // propagate RC by renegotiating sessions - ForEachPeer([](llarp::ILinkSession *s) { + ForEachPeer([](ILinkSession *s) { if(s->RenegotiateSession()) - llarp::LogInfo("renegotiated session"); + LogInfo("renegotiated session"); else - llarp::LogWarn("failed to renegotiate session"); + LogWarn("failed to renegotiate session"); }); // TODO: do this async @@ -746,7 +742,7 @@ namespace llarp llarp::LogError("failed to ensure keyfile ", transport_keyfile); return; } - if(server->Configure(netloop, key, af, proto)) + if(server->Configure(netloop(), key, af, proto)) { AddInboundLink(server); return; @@ -758,7 +754,7 @@ namespace llarp llarp::LogInfo("link ", key, " failed to configure IPv6, trying IPv4"); af = AF_INET; - if(server->Configure(netloop, key, af, proto)) + if(server->Configure(netloop(), key, af, proto)) { AddInboundLink(server); return; @@ -772,7 +768,7 @@ namespace llarp if(StrEq(key, "profiles")) { routerProfilesFile = val; - routerProfiling.Load(val); + routerProfiling().Load(val); llarp::LogInfo("setting profiles to ", routerProfilesFile); } else if(StrEq(key, "strict-connect")) @@ -1010,14 +1006,14 @@ namespace llarp void Router::Tick() { - // llarp::LogDebug("tick router"); - auto now = llarp_ev_loop_time_now_ms(netloop); + // LogDebug("tick router"); + auto now = Now(); - if(_rc.ExpiresSoon(now, llarp::randint() % 10000)) + if(_rc.ExpiresSoon(now, randint() % 10000)) { - llarp::LogInfo("regenerating RC"); + LogInfo("regenerating RC"); if(!UpdateOurRC(false)) - llarp::LogError("Failed to update our RC"); + LogError("Failed to update our RC"); } if(IsServiceNode()) @@ -1025,7 +1021,7 @@ namespace llarp // only do this as service node // client endpoints do this on their own nodedb()->visit([&](const RouterContact &rc) -> bool { - if(rc.ExpiresSoon(now, llarp::randint() % 10000)) + if(rc.ExpiresSoon(now, randint() % 10000)) ServiceNodeLookupRouterWhenExpired(rc.pubkey); return true; }); @@ -1041,19 +1037,19 @@ namespace llarp { if(link) { - llarp::LogDebug("keepalive to ", itr->first); + LogDebug("keepalive to ", itr->first); link->KeepAliveSessionTo(itr->first); } else { - llarp::LogDebug("establish to ", itr->first); + LogDebug("establish to ", itr->first); TryEstablishTo(itr->first); } ++itr; } else { - llarp::LogInfo("commit to ", itr->first, " expired"); + LogInfo("commit to ", itr->first, " expired"); itr = m_PersistingSessions.erase(itr); } } @@ -1062,9 +1058,8 @@ namespace llarp size_t N = nodedb()->num_loaded(); if(N < minRequiredRouters) { - llarp::LogInfo("We need at least ", minRequiredRouters, - " service nodes to build paths but we have ", N, - " in nodedb"); + LogInfo("We need at least ", minRequiredRouters, + " service nodes to build paths but we have ", N, " in nodedb"); // TODO: only connect to random subset if(bootstrapRCList.size()) { @@ -1075,7 +1070,7 @@ namespace llarp } } else - llarp::LogError("we have no bootstrap nodes specified"); + LogError("we have no bootstrap nodes specified"); } if(inboundLinks.size() == 0) @@ -1087,33 +1082,32 @@ namespace llarp { ConnectToRandomRouters(minConnectedRouters); } - exitContext.Tick(now); + _exitContext.Tick(now); if(rpcCaller) rpcCaller->Tick(now); } bool - Router::Sign(llarp::Signature &sig, const llarp_buffer_t &buf) const + Router::Sign(Signature &sig, const llarp_buffer_t &buf) const { - return crypto()->sign(sig, identity, buf); + return crypto()->sign(sig, identity(), buf); } void - Router::SendTo(llarp::RouterID remote, const llarp::ILinkMessage *msg, - llarp::ILinkLayer *selected) + Router::SendTo(RouterID remote, const ILinkMessage *msg, ILinkLayer *selected) { llarp_buffer_t buf(linkmsg_buffer); if(!msg->BEncode(&buf)) { - llarp::LogWarn("failed to encode outbound message, buffer size left: ", - llarp_buffer_size_left(buf)); + LogWarn("failed to encode outbound message, buffer size left: ", + llarp_buffer_size_left(buf)); return; } // set size of message buf.sz = buf.cur - buf.base; buf.cur = buf.base; - llarp::LogDebug("send ", buf.sz, " bytes to ", remote); + LogDebug("send ", buf.sz, " bytes to ", remote); if(selected) { if(selected->SendTo(remote, buf)) @@ -1129,7 +1123,7 @@ namespace llarp if(link->SendTo(remote, buf)) return; } - llarp::LogWarn("message to ", remote, " was dropped"); + LogWarn("message to ", remote, " was dropped"); } void @@ -1139,16 +1133,16 @@ namespace llarp } void - Router::SessionClosed(llarp::RouterID remote) + Router::SessionClosed(RouterID remote) { __llarp_dht_remove_peer(dht(), remote.data()); // remove from valid routers if it's a valid router validRouters.erase(remote); - llarp::LogInfo("Session to ", remote, " fully closed"); + LogInfo("Session to ", remote, " fully closed"); } - llarp::ILinkLayer * - Router::GetLinkWithSessionByPubkey(const llarp::RouterID &pubkey) + ILinkLayer * + Router::GetLinkWithSessionByPubkey(const RouterID &pubkey) { for(const auto &link : outboundLinks) { @@ -1164,9 +1158,9 @@ namespace llarp } void - Router::FlushOutboundFor(llarp::RouterID remote, llarp::ILinkLayer *chosen) + Router::FlushOutboundFor(RouterID remote, ILinkLayer *chosen) { - llarp::LogDebug("Flush outbound for ", remote); + LogDebug("Flush outbound for ", remote); auto itr = outboundMessageQueue.find(remote); if(itr == outboundMessageQueue.end()) @@ -1184,8 +1178,8 @@ namespace llarp { llarp_buffer_t buf(itr->second.front()); if(!chosen->SendTo(remote, buf)) - llarp::LogWarn("failed to send outbound message to ", remote, " via ", - chosen->Name()); + LogWarn("failed to send outbound message to ", remote, " via ", + chosen->Name()); itr->second.pop(); } @@ -1193,20 +1187,20 @@ namespace llarp } void - Router::DiscardOutboundFor(const llarp::RouterID &remote) + Router::DiscardOutboundFor(const RouterID &remote) { outboundMessageQueue.erase(remote); } bool - Router::GetRandomConnectedRouter(llarp::RouterContact &result) const + Router::GetRandomConnectedRouter(RouterContact &result) const { auto sz = validRouters.size(); if(sz) { auto itr = validRouters.begin(); if(sz > 1) - std::advance(itr, llarp::randint() % sz); + std::advance(itr, randint() % sz); result = itr->second; return true; } @@ -1214,8 +1208,7 @@ namespace llarp } void - Router::async_verify_RC(const llarp::RouterContact &rc, - llarp::ILinkLayer *link) + Router::async_verify_RC(const RouterContact &rc, ILinkLayer *link) { if(pendingVerifyRC.count(rc.pubkey)) return; @@ -1223,15 +1216,15 @@ namespace llarp { if(lokinetRouters.find(rc.pubkey) == lokinetRouters.end()) { - llarp::LogInfo(rc.pubkey, " is NOT a valid service node, rejecting"); + LogInfo(rc.pubkey, " is NOT a valid service node, rejecting"); link->CloseSessionTo(rc.pubkey); return; } } - llarp_async_verify_rc *job = &pendingVerifyRC[rc.pubkey]; - llarp::async_verify_context *ctx = new llarp::async_verify_context(); - ctx->router = this; - ctx->establish_job = nullptr; + llarp_async_verify_rc *job = &pendingVerifyRC[rc.pubkey]; + async_verify_context *ctx = new async_verify_context(); + ctx->router = this; + ctx->establish_job = nullptr; auto itr = pendingEstablishJobs.find(rc.pubkey); if(itr != pendingEstablishJobs.end()) @@ -1267,49 +1260,49 @@ namespace llarp { rpcBindAddr = DefaultRPCBindAddr; } - rpcServer = std::make_unique< llarp::rpc::Server >(this); + rpcServer = std::make_unique< rpc::Server >(this); while(!rpcServer->Start(rpcBindAddr)) { - llarp::LogError("failed to bind jsonrpc to ", rpcBindAddr); + LogError("failed to bind jsonrpc to ", rpcBindAddr); #if defined(ANDROID) || defined(RPI) sleep(1); #else std::this_thread::sleep_for(std::chrono::seconds(1)); #endif } - llarp::LogInfo("Bound RPC server to ", rpcBindAddr); + LogInfo("Bound RPC server to ", rpcBindAddr); } if(whitelistRouters) { - rpcCaller = std::make_unique< llarp::rpc::Caller >(this); + rpcCaller = std::make_unique< rpc::Caller >(this); rpcCaller->SetBasicAuth(lokidRPCUser, lokidRPCPassword); while(!rpcCaller->Start(lokidRPCAddr)) { - llarp::LogError("failed to start jsonrpc caller to ", lokidRPCAddr); + LogError("failed to start jsonrpc caller to ", lokidRPCAddr); #if defined(ANDROID) || defined(RPI) sleep(1); #else std::this_thread::sleep_for(std::chrono::seconds(1)); #endif } - llarp::LogInfo("RPC Caller to ", lokidRPCAddr, " started"); + LogInfo("RPC Caller to ", lokidRPCAddr, " started"); } llarp_threadpool_start(tp); llarp_threadpool_start(disk); - routerProfiling.Load(routerProfilesFile.c_str()); + routerProfiling().Load(routerProfilesFile.c_str()); - llarp::Addr publicAddr(this->addrInfo); + Addr publicAddr(this->addrInfo); if(this->publicOverride) { - llarp::LogDebug("public address:port ", publicAddr); + LogDebug("public address:port ", publicAddr); } - llarp::LogInfo("You have ", inboundLinks.size(), " inbound links"); + LogInfo("You have ", inboundLinks.size(), " inbound links"); - llarp::AddressInfo ai; + AddressInfo ai; for(const auto &link : inboundLinks) { if(link->GetOurAddressInfo(ai)) @@ -1320,47 +1313,47 @@ namespace llarp ai.ip = *publicAddr.addr6(); ai.port = publicAddr.port(); } - if(llarp::IsBogon(ai.ip)) + if(IsBogon(ai.ip)) continue; _rc.addrs.push_back(ai); } } // set public encryption key - _rc.enckey = llarp::seckey_topublic(encryption); + _rc.enckey = seckey_topublic(encryption()); // set public signing key - _rc.pubkey = llarp::seckey_topublic(identity); + _rc.pubkey = seckey_topublic(identity()); if(ExitEnabled()) { - llarp::nuint32_t a = publicAddr.xtonl(); + nuint32_t a = publicAddr.xtonl(); _rc.exits.emplace_back(_rc.pubkey, a); - llarp::LogInfo( - "Neato tehl33toh, You are a freaking exit relay. w00t!!!!! your " + LogInfo( + "Neato teh l33toh, You are a freaking exit relay. w00t!!!!! your " "exit " "is advertised as exiting at ", a); } - llarp::LogInfo("Signing rc..."); - if(!_rc.Sign(crypto(), identity)) + LogInfo("Signing rc..."); + if(!_rc.Sign(crypto(), identity())) { - llarp::LogError("failed to sign rc"); + LogError("failed to sign rc"); return false; } if(!SaveRC()) { - llarp::LogError("failed to save RC"); + LogError("failed to save RC"); return false; } - llarp::LogInfo("have ", nodedb->num_loaded(), " routers"); + LogInfo("have ", nodedb->num_loaded(), " routers"); - llarp::LogInfo("starting outbound ", outboundLinks.size(), " links"); + LogInfo("starting outbound ", outboundLinks.size(), " links"); for(const auto &link : outboundLinks) { if(!link->Start(_logic)) { - llarp::LogWarn("outbound link '", link->Name(), "' failed to start"); + LogWarn("outbound link '", link->Name(), "' failed to start"); return false; } } @@ -1372,11 +1365,11 @@ namespace llarp { if(link->Start(_logic)) { - llarp::LogDebug("Link ", link->Name(), " started"); + LogDebug("Link ", link->Name(), " started"); IBLinksStarted++; } else - llarp::LogWarn("Link ", link->Name(), " failed to start"); + LogWarn("Link ", link->Name(), " failed to start"); } if(IBLinksStarted > 0) @@ -1384,23 +1377,23 @@ namespace llarp // initialize as service node if(!InitServiceNode()) { - llarp::LogError("Failed to initialize service node"); + LogError("Failed to initialize service node"); return false; } - llarp::RouterID us = pubkey(); - llarp::LogInfo("initalized service node: ", us); + RouterID us = pubkey(); + LogInfo("initalized service node: ", us); } else { // we are a client // regenerate keys and resign rc before everything else - crypto()->identity_keygen(identity); - crypto()->encryption_keygen(encryption); - _rc.pubkey = llarp::seckey_topublic(identity); - _rc.enckey = llarp::seckey_topublic(encryption); - if(!_rc.Sign(crypto(), identity)) + crypto()->identity_keygen(_identity); + crypto()->encryption_keygen(_encryption); + _rc.pubkey = seckey_topublic(identity()); + _rc.enckey = seckey_topublic(encryption()); + if(!_rc.Sign(crypto(), identity())) { - llarp::LogError("failed to regenerate keys and sign RC"); + LogError("failed to regenerate keys and sign RC"); return false; } @@ -1408,19 +1401,19 @@ namespace llarp if(this->ShouldCreateDefaultHiddenService()) { // generate default hidden service - llarp::LogInfo("setting up default network endpoint"); + LogInfo("setting up default network endpoint"); if(!CreateDefaultHiddenService()) { - llarp::LogError("failed to set up default network endpoint"); + LogError("failed to set up default network endpoint"); return false; } } } - llarp::LogInfo("starting hidden service context..."); + LogInfo("starting hidden service context..."); if(!hiddenServiceContext.StartAll()) { - llarp::LogError("Failed to start hidden service context"); + LogError("Failed to start hidden service context"); return false; } llarp_dht_context_start(dht(), pubkey()); @@ -1447,7 +1440,7 @@ namespace llarp void Router::StopLinks() { - llarp::LogInfo("stopping links"); + LogInfo("stopping links"); for(const auto &link : outboundLinks) link->Stop(); for(const auto &link : inboundLinks) @@ -1475,13 +1468,13 @@ namespace llarp { enabledOption = itr->second; } - llarp::LogDebug("IfName: ", defaultIfName, " IfAddr: ", defaultIfAddr, - " Enabled: ", enabledOption); - // llarp::LogInfo("IfAddr: ", itr->second); - // llarp::LogInfo("IfName: ", itr->second); + LogDebug("IfName: ", defaultIfName, " IfAddr: ", defaultIfAddr, + " Enabled: ", enabledOption); + // LogInfo("IfAddr: ", itr->second); + // LogInfo("IfName: ", itr->second); if(enabledOption == "false") { - llarp::LogInfo("Disabling default hidden service"); + LogInfo("Disabling default hidden service"); return false; } else if(enabledOption == "auto") @@ -1490,7 +1483,7 @@ namespace llarp // no if we have a endpoints if(hiddenServiceContext.hasEndpoints()) { - llarp::LogInfo("Auto mode detected and we have endpoints"); + LogInfo("Auto mode detected and we have endpoints"); netConfig.emplace("enabled", "false"); return false; } @@ -1503,13 +1496,13 @@ namespace llarp { // we don't have any endpoints, auto configure settings // set a default IP range - defaultIfAddr = llarp::findFreePrivateRange(); + defaultIfAddr = findFreePrivateRange(); if(defaultIfAddr == "") { - llarp::LogError( - "Could not find any free lokitun interface names, - can't auto set up " "default HS context for client"); defaultIfAddr = - "no"; netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); + LogError( + "Could not find any free lokitun interface names, can't + auto set up " "default HS context for client"); defaultIfAddr = "no"; + netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); return false; } netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); @@ -1517,10 +1510,10 @@ namespace llarp if(defaultIfName == "auto") { // pick an ifName - defaultIfName = llarp::findFreeLokiTunIfName(); + defaultIfName = findFreeLokiTunIfName(); if(defaultIfName == "") { - llarp::LogError( + LogError( "Could not find any free private ip ranges, can't auto set up " "default HS context for client"); defaultIfName = "no"; netConfig.emplace(std::make_pair("defaultIfName", defaultIfName)); @@ -1541,16 +1534,16 @@ namespace llarp return; _stopping.store(true); - llarp::LogInfo("stopping router"); + LogInfo("stopping router"); hiddenServiceContext.StopAll(); - exitContext.Stop(); + _exitContext.Stop(); if(rpcServer) rpcServer->Stop(); _logic->call_later({200, this, &RouterAfterStopIssued}); } bool - Router::HasSessionTo(const llarp::RouterID &remote) const + Router::HasSessionTo(const RouterID &remote) const { return validRouters.find(remote) != validRouters.end(); } @@ -1561,34 +1554,33 @@ namespace llarp int wanted = want; Router *self = this; - self->nodedb()->visit( - [self, &want](const llarp::RouterContact &other) -> bool { - // check if we really want to - if(other.ExpiresSoon(self->Now(), 30000)) - return want > 0; - if(!self->ConnectionToRouterAllowed(other.pubkey)) - return want > 0; - if(llarp::randint() % 2 == 0 - && !(self->HasSessionTo(other.pubkey) - || self->HasPendingConnectJob(other.pubkey))) - { - self->TryConnectAsync(other, 5); - --want; - } - return want > 0; - }); + self->nodedb()->visit([self, &want](const RouterContact &other) -> bool { + // check if we really want to + if(other.ExpiresSoon(self->Now(), 30000)) + return want > 0; + if(!self->ConnectionToRouterAllowed(other.pubkey)) + return want > 0; + if(randint() % 2 == 0 + && !(self->HasSessionTo(other.pubkey) + || self->HasPendingConnectJob(other.pubkey))) + { + self->TryConnectAsync(other, 5); + --want; + } + return want > 0; + }); if(wanted != want) - llarp::LogInfo("connecting to ", abs(want - wanted), " out of ", wanted, - " random routers"); + LogInfo("connecting to ", abs(want - wanted), " out of ", wanted, + " random routers"); } bool Router::InitServiceNode() { - llarp::LogInfo("accepting transit traffic"); + LogInfo("accepting transit traffic"); paths.AllowTransit(); llarp_dht_allow_transit(dht()); - return exitContext.AddExitEndpoint("default-connectivity", netConfig); + return _exitContext.AddExitEndpoint("default-connectivity", netConfig); } /// validate a new configuration against an already made and running @@ -1602,8 +1594,8 @@ namespace llarp { if(!OnEntry(section, key, val)) { - llarp::LogError("invalid entry in section [", section, "]: '", key, - "'='", val, "'"); + LogError("invalid entry in section [", section, "]: '", key, "'='", + val, "'"); valid = false; } } @@ -1660,10 +1652,8 @@ namespace llarp if(outboundLinks.size() > 0) return true; - static std::list< - std::function< std::unique_ptr< ILinkLayer >(llarp::Router *) > > - linkFactories = {llarp::utp::NewServerFromRouter, - llarp::iwp::NewServerFromRouter}; + static std::list< std::function< std::unique_ptr< ILinkLayer >(Router *) > > + linkFactories = {utp::NewServerFromRouter, iwp::NewServerFromRouter}; for(const auto &factory : linkFactories) { @@ -1672,7 +1662,7 @@ namespace llarp continue; if(!link->EnsureKeys(transport_keyfile.string().c_str())) { - llarp::LogError("failed to load ", transport_keyfile); + LogError("failed to load ", transport_keyfile); continue; } @@ -1680,7 +1670,7 @@ namespace llarp for(auto af : afs) { - if(!link->Configure(netloop, "*", af, m_OutboundPort)) + if(!link->Configure(netloop(), "*", af, m_OutboundPort)) continue; outboundLinks.insert(std::move(link)); break; @@ -1719,7 +1709,7 @@ namespace llarp } bool - Router::HasPendingConnectJob(const llarp::RouterID &remote) + Router::HasPendingConnectJob(const RouterID &remote) { return pendingEstablishJobs.find(remote) != pendingEstablishJobs.end(); } @@ -1727,13 +1717,13 @@ namespace llarp bool Router::LoadHiddenServiceConfig(const char *fname) { - llarp::LogDebug("opening hidden service config ", fname); - llarp::service::Config conf; + LogDebug("opening hidden service config ", fname); + service::Config conf; if(!conf.Load(fname)) return false; for(const auto &config : conf.services) { - llarp::service::Config::section_t filteredConfig; + service::Config::section_t filteredConfig; mergeHiddenServiceConfig(config.second, filteredConfig.second); filteredConfig.first = config.first; if(!hiddenServiceContext.AddEndpoint(filteredConfig)) diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 7b3e3b068..f42a9a4e4 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -1,6 +1,8 @@ #ifndef LLARP_ROUTER_HPP #define LLARP_ROUTER_HPP +#include + #include #include #include @@ -66,54 +68,6 @@ namespace llarp } }; - struct AbstractRouter - { - virtual ~AbstractRouter() = 0; - - virtual Logic * - logic() const = 0; - - virtual llarp_dht_context * - dht() const = 0; - - virtual Crypto * - crypto() const = 0; - - virtual llarp_nodedb * - nodedb() const = 0; - - virtual const path::PathContext & - pathContext() const = 0; - - virtual path::PathContext & - pathContext() = 0; - - virtual const llarp::RouterContact & - rc() const = 0; - - virtual const byte_t * - pubkey() const = 0; - - virtual llarp_time_t - Now() const = 0; - - virtual bool - SendToOrQueue(const llarp::RouterID &remote, - const llarp::ILinkMessage *msg) = 0; - - virtual void - PersistSessionUntil(const llarp::RouterID &remote, llarp_time_t until) = 0; - - virtual bool - ParseRoutingMessageBuffer(const llarp_buffer_t &buf, - routing::IMessageHandler *h, PathID_t rxid) = 0; - - virtual void - HandleDHTLookupForExplore( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) = 0; - }; - struct Router final : public AbstractRouter, public util::IStateful { bool ready; @@ -133,7 +87,7 @@ namespace llarp fs::path our_rc_file = "rc.signed"; // our router contact - llarp::RouterContact _rc; + RouterContact _rc; /// are we using the lokid service node seed ? bool usingSNSeed = false; @@ -180,31 +134,73 @@ namespace llarp return paths; } - const llarp::RouterContact & + const RouterContact & rc() const override { return _rc; } + exit::Context & + exitContext() override + { + return _exitContext; + } + + const SecretKey & + identity() const override + { + return _identity; + } + + const SecretKey & + encryption() const override + { + return _encryption; + } + + Profiling & + routerProfiling() override + { + return _routerProfiling; + } + + llarp_ev_loop * + netloop() const override + { + return _netloop; + } + + llarp_threadpool * + threadpool() override + { + return tp; + } + + llarp_threadpool * + diskworker() override + { + return disk; + } + // our ipv4 public setting bool publicOverride = false; struct sockaddr_in ip4addr; - llarp::AddressInfo addrInfo; + AddressInfo addrInfo; - llarp_ev_loop *netloop; + llarp_ev_loop *_netloop; llarp_threadpool *tp; Logic *_logic; std::unique_ptr< Crypto > _crypto; path::PathContext paths; - exit::Context exitContext; - SecretKey identity; - SecretKey encryption; + exit::Context _exitContext; + SecretKey _identity; + SecretKey _encryption; llarp_threadpool *disk; llarp_dht_context *_dht = nullptr; llarp_nodedb *_nodedb; bool - Sign(Signature &sig, const llarp_buffer_t &buf) const; + Sign(Signature &sig, const llarp_buffer_t &buf) const override; // buffer for serializing link messages std::array< byte_t, MAX_LINK_MSG_SIZE > linkmsg_buffer; @@ -223,10 +219,10 @@ namespace llarp uint32_t ticker_job_id = 0; - llarp::InboundMessageParser inbound_link_msg_parser; - llarp::routing::InboundMessageParser inbound_routing_msg_parser; + InboundMessageParser inbound_link_msg_parser; + routing::InboundMessageParser inbound_routing_msg_parser; - llarp::service::Context hiddenServiceContext; + service::Context hiddenServiceContext; using NetConfig_t = std::unordered_multimap< std::string, std::string >; @@ -235,10 +231,10 @@ namespace llarp /// identity keys whitelist of routers we will connect to directly (not for /// service nodes) - std::set< llarp::RouterID > strictConnectPubkeys; + std::set< RouterID > strictConnectPubkeys; /// bootstrap RCs - std::list< llarp::RouterContact > bootstrapRCList; + std::list< RouterContact > bootstrapRCList; bool ExitEnabled() const @@ -247,7 +243,7 @@ namespace llarp auto itr = netConfig.find("exit"); if(itr == netConfig.end()) return false; - return llarp::IsTrueValue(itr->second.c_str()); + return IsTrueValue(itr->second.c_str()); } bool @@ -258,76 +254,70 @@ namespace llarp const std::string DefaultRPCBindAddr = "127.0.0.1:1190"; bool enableRPCServer = true; - std::unique_ptr< llarp::rpc::Server > rpcServer; + std::unique_ptr< rpc::Server > rpcServer; std::string rpcBindAddr = DefaultRPCBindAddr; /// lokid caller const std::string DefaultLokidRPCAddr = "127.0.0.1:22023"; - std::unique_ptr< llarp::rpc::Caller > rpcCaller; + std::unique_ptr< rpc::Caller > rpcCaller; std::string lokidRPCAddr = DefaultLokidRPCAddr; std::string lokidRPCUser = ""; std::string lokidRPCPassword = ""; - std::set< std::unique_ptr< llarp::ILinkLayer >, - CompareLinks< llarp::ILinkLayer > > + std::set< std::unique_ptr< ILinkLayer >, CompareLinks< ILinkLayer > > outboundLinks; - std::set< std::unique_ptr< llarp::ILinkLayer >, - CompareLinks< llarp::ILinkLayer > > + std::set< std::unique_ptr< ILinkLayer >, CompareLinks< ILinkLayer > > inboundLinks; - llarp::Profiling routerProfiling; + Profiling _routerProfiling; std::string routerProfilesFile = "profiles.dat"; using MessageQueue = std::queue< std::vector< byte_t > >; /// outbound message queue - std::unordered_map< llarp::RouterID, MessageQueue, llarp::RouterID::Hash > + std::unordered_map< RouterID, MessageQueue, RouterID::Hash > outboundMessageQueue; /// loki verified routers - std::unordered_map< llarp::RouterID, llarp::RouterContact, - llarp::RouterID::Hash > - validRouters; + std::unordered_map< RouterID, RouterContact, RouterID::Hash > validRouters; // pending establishing session with routers - std::unordered_map< llarp::RouterID, std::unique_ptr< TryConnectJob >, - llarp::RouterID::Hash > + std::unordered_map< RouterID, std::unique_ptr< TryConnectJob >, + RouterID::Hash > pendingEstablishJobs; // pending RCs to be verified by pubkey - std::unordered_map< llarp::RouterID, llarp_async_verify_rc, - llarp::RouterID::Hash > + std::unordered_map< RouterID, llarp_async_verify_rc, RouterID::Hash > pendingVerifyRC; // sessions to persist -> timestamp to end persist at - std::unordered_map< llarp::RouterID, llarp_time_t, llarp::RouterID::Hash > + std::unordered_map< RouterID, llarp_time_t, RouterID::Hash > m_PersistingSessions; // lokinet routers from lokid, maps pubkey to when we think it will expire, // set to max value right now - std::unordered_map< llarp::RouterID, llarp_time_t, llarp::PubKey::Hash > - lokinetRouters; + std::unordered_map< RouterID, llarp_time_t, PubKey::Hash > lokinetRouters; - Router(struct llarp_threadpool *tp, struct llarp_ev_loop *netloop, - llarp::Logic *logic); + Router(struct llarp_threadpool *tp, struct llarp_ev_loop *__netloop, + Logic *logic); ~Router(); void - OnSessionEstablished(llarp::RouterContact rc); + OnSessionEstablished(RouterContact rc) override; bool - HandleRecvLinkMessageBuffer(llarp::ILinkSession *from, - const llarp_buffer_t &msg); + HandleRecvLinkMessageBuffer(ILinkSession *from, + const llarp_buffer_t &msg) override; void - AddInboundLink(std::unique_ptr< llarp::ILinkLayer > &link); + AddInboundLink(std::unique_ptr< ILinkLayer > &link); bool InitOutboundLinks(); bool - GetRandomGoodRouter(RouterID &r); + GetRandomGoodRouter(RouterID &r) override; /// initialize us as a service node /// return true on success @@ -345,7 +335,7 @@ namespace llarp LoadHiddenServiceConfig(const char *fname); bool - AddHiddenService(const llarp::service::Config::section_t &config); + AddHiddenService(const service::Config::section_t &config); bool Configure(Config *conf); @@ -365,8 +355,7 @@ namespace llarp StopLinks(); void - PersistSessionUntil(const llarp::RouterID &remote, - llarp_time_t until) override; + PersistSessionUntil(const RouterID &remote, llarp_time_t until) override; bool EnsureIdentity(); @@ -375,7 +364,7 @@ namespace llarp EnsureEncryptionKey(); bool - ConnectionToRouterAllowed(const llarp::RouterID &router) const; + ConnectionToRouterAllowed(const RouterID &router) const; bool SaveRC(); @@ -383,14 +372,14 @@ namespace llarp const byte_t * pubkey() const override { - return llarp::seckey_topublic(identity); + return seckey_topublic(_identity); } void - OnConnectTimeout(ILinkSession *session); + OnConnectTimeout(ILinkSession *session) override; bool - HasPendingConnectJob(const llarp::RouterID &remote); + HasPendingConnectJob(const RouterID &remote); void try_connect(fs::path rcfile); @@ -411,49 +400,44 @@ namespace llarp /// NOT threadsafe /// MUST be called in the logic thread bool - SendToOrQueue(const llarp::RouterID &remote, - const llarp::ILinkMessage *msg) override; + SendToOrQueue(const RouterID &remote, const ILinkMessage *msg) override; /// sendto or drop void - SendTo(llarp::RouterID remote, const llarp::ILinkMessage *msg, - llarp::ILinkLayer *chosen); + SendTo(RouterID remote, const ILinkMessage *msg, ILinkLayer *chosen); /// manually flush outbound message queue for just 1 router void - FlushOutboundFor(llarp::RouterID remote, - llarp::ILinkLayer *chosen = nullptr); + FlushOutboundFor(RouterID remote, ILinkLayer *chosen = nullptr); /// manually discard all pending messages to remote router void - DiscardOutboundFor(const llarp::RouterID &remote); + DiscardOutboundFor(const RouterID &remote); /// try establishing a session to a remote router void - TryEstablishTo(const llarp::RouterID &remote); + TryEstablishTo(const RouterID &remote); /// lookup a router by pubkey when it expires when we are a service node void - ServiceNodeLookupRouterWhenExpired(llarp::RouterID remote); + ServiceNodeLookupRouterWhenExpired(RouterID remote); void HandleDHTLookupForExplore( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) override; + RouterID remote, const std::vector< RouterContact > &results) override; void - ForEachPeer( - std::function< void(const llarp::ILinkSession *, bool) > visit) const; + ForEachPeer(std::function< void(const ILinkSession *, bool) > visit) const; void - ForEachPeer(std::function< void(llarp::ILinkSession *) > visit); + ForEachPeer(std::function< void(ILinkSession *) > visit); /// check if newRc matches oldRC and update local rc for this remote contact /// if valid /// returns true on valid and updated /// returns false otherwise bool - CheckRenegotiateValid(RouterContact newRc, RouterContact oldRC); + CheckRenegotiateValid(RouterContact newRc, RouterContact oldRC) override; /// flush outbound message queue void @@ -461,7 +445,7 @@ namespace llarp /// called by link when a remote session has no more sessions open void - SessionClosed(RouterID remote); + SessionClosed(RouterID remote) override; /// call internal router ticker void @@ -471,15 +455,15 @@ namespace llarp llarp_time_t Now() const override { - return llarp_ev_loop_time_now_ms(netloop); + return llarp_ev_loop_time_now_ms(_netloop); } /// schedule ticker to call i ms from now void ScheduleTicker(uint64_t i = 1000); - llarp::ILinkLayer * - GetLinkWithSessionByPubkey(const llarp::RouterID &remote); + ILinkLayer * + GetLinkWithSessionByPubkey(const RouterID &remote); /// parse a routing message in a buffer and handle it with a handler if /// successful parsing return true on parse and handle success otherwise @@ -487,35 +471,33 @@ namespace llarp bool ParseRoutingMessageBuffer(const llarp_buffer_t &buf, routing::IMessageHandler *h, - PathID_t rxid) override; + const PathID_t &rxid) override; void ConnectToRandomRouters(int N); size_t - NumberOfConnectedRouters() const; + NumberOfConnectedRouters() const override; bool - TryConnectAsync(llarp::RouterContact rc, uint16_t tries); + TryConnectAsync(RouterContact rc, uint16_t tries); bool - GetRandomConnectedRouter(llarp::RouterContact &result) const; + GetRandomConnectedRouter(RouterContact &result) const override; void - async_verify_RC(const llarp::RouterContact &rc, llarp::ILinkLayer *link); + async_verify_RC(const RouterContact &rc, ILinkLayer *link); void - HandleDHTLookupForSendTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results); + HandleDHTLookupForSendTo(RouterID remote, + const std::vector< RouterContact > &results); bool - HasSessionTo(const llarp::RouterID &remote) const; + HasSessionTo(const RouterID &remote) const; void HandleDHTLookupForTryEstablishTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results); + RouterID remote, const std::vector< RouterContact > &results); static void on_verify_client_rc(llarp_async_verify_rc *context); diff --git a/llarp/routing/dht_message.cpp b/llarp/routing/dht_message.cpp index dbaa9b8e4..95ef96a3b 100644 --- a/llarp/routing/dht_message.cpp +++ b/llarp/routing/dht_message.cpp @@ -1,6 +1,7 @@ #include -#include +#include +#include namespace llarp { @@ -49,7 +50,7 @@ namespace llarp } bool - DHTMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + DHTMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { // set source as us llarp::dht::Key_t us{r->pubkey()}; diff --git a/llarp/routing/handler.hpp b/llarp/routing/handler.hpp index 8133b0380..354b10831 100644 --- a/llarp/routing/handler.hpp +++ b/llarp/routing/handler.hpp @@ -1,68 +1,85 @@ #ifndef LLARP_ROUTING_HANDLER_HPP #define LLARP_ROUTING_HANDLER_HPP -#include -#include -#include -#include -#include -#include - namespace llarp { + struct AbstractRouter; + + namespace dht + { + struct IMessage; + } + + namespace service + { + struct ProtocolFrame; + } + namespace routing { struct DataDiscardMessage; + struct GrantExitMessage; + struct ObtainExitMessage; + struct RejectExitMessage; + struct TransferTrafficMessage; + struct UpdateExitMessage; + struct UpdateExitVerifyMessage; + struct CloseExitMessage; + struct PathTransferMessage; + struct PathConfirmMessage; + struct PathLatencyMessage; // handles messages on the routing level struct IMessageHandler { virtual bool HandleObtainExitMessage(const ObtainExitMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool - HandleGrantExitMessage(const GrantExitMessage *msg, llarp::Router *r) = 0; + HandleGrantExitMessage(const GrantExitMessage *msg, + AbstractRouter *r) = 0; virtual bool HandleRejectExitMessage(const RejectExitMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleTransferTrafficMessage(const TransferTrafficMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleUpdateExitMessage(const UpdateExitMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleUpdateExitVerifyMessage(const UpdateExitVerifyMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool - HandleCloseExitMessage(const CloseExitMessage *msg, llarp::Router *r) = 0; + HandleCloseExitMessage(const CloseExitMessage *msg, + AbstractRouter *r) = 0; virtual bool HandleDataDiscardMessage(const DataDiscardMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandlePathTransferMessage(const PathTransferMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleHiddenServiceFrame(const service::ProtocolFrame *msg) = 0; virtual bool HandlePathConfirmMessage(const PathConfirmMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandlePathLatencyMessage(const PathLatencyMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool - HandleDHTMessage(const llarp::dht::IMessage *msg, llarp::Router *r) = 0; + HandleDHTMessage(const dht::IMessage *msg, AbstractRouter *r) = 0; }; } // namespace routing } // namespace llarp diff --git a/llarp/routing/message.hpp b/llarp/routing/message.hpp index 9264a0fa6..c5fe40aa2 100644 --- a/llarp/routing/message.hpp +++ b/llarp/routing/message.hpp @@ -7,14 +7,14 @@ namespace llarp { - struct Router; + struct AbstractRouter; namespace routing { struct IMessageHandler; struct IMessage : public llarp::IBEncodeMessage { - llarp::PathID_t from; + PathID_t from; uint64_t S; IMessage() : llarp::IBEncodeMessage(), S(0) @@ -24,7 +24,7 @@ namespace llarp virtual ~IMessage(){}; virtual bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const = 0; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const = 0; virtual void Clear() = 0; diff --git a/llarp/routing/message_parser.cpp b/llarp/routing/message_parser.cpp index 31e9e18fa..be4c422b5 100644 --- a/llarp/routing/message_parser.cpp +++ b/llarp/routing/message_parser.cpp @@ -1,16 +1,47 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include #include namespace llarp { namespace routing { + struct InboundMessageParser::MessageHolder + { + DataDiscardMessage D; + PathLatencyMessage L; + DHTMessage M; + PathConfirmMessage P; + PathTransferMessage T; + service::ProtocolFrame H; + TransferTrafficMessage I; + GrantExitMessage G; + RejectExitMessage J; + ObtainExitMessage O; + UpdateExitMessage U; + CloseExitMessage C; + }; + InboundMessageParser::InboundMessageParser() + : firstKey(false) + , key('\0') + , msg(nullptr) + , m_Holder(std::make_unique< MessageHolder >()) { reader.user = this; reader.on_key = &OnKey; - firstKey = false; + } + + InboundMessageParser::~InboundMessageParser() + { } bool @@ -39,40 +70,40 @@ namespace llarp switch(self->key) { case 'D': - self->msg = &self->m_Holder.D; + self->msg = &self->m_Holder->D; break; case 'L': - self->msg = &self->m_Holder.L; + self->msg = &self->m_Holder->L; break; case 'M': - self->msg = &self->m_Holder.M; + self->msg = &self->m_Holder->M; break; case 'P': - self->msg = &self->m_Holder.P; + self->msg = &self->m_Holder->P; break; case 'T': - self->msg = &self->m_Holder.T; + self->msg = &self->m_Holder->T; break; case 'H': - self->msg = &self->m_Holder.H; + self->msg = &self->m_Holder->H; break; case 'I': - self->msg = &self->m_Holder.I; + self->msg = &self->m_Holder->I; break; case 'G': - self->msg = &self->m_Holder.G; + self->msg = &self->m_Holder->G; break; case 'J': - self->msg = &self->m_Holder.J; + self->msg = &self->m_Holder->J; break; case 'O': - self->msg = &self->m_Holder.O; + self->msg = &self->m_Holder->O; break; case 'U': - self->msg = &self->m_Holder.U; + self->msg = &self->m_Holder->U; break; case 'C': - self->msg = &self->m_Holder.C; + self->msg = &self->m_Holder->C; break; default: llarp::LogError("invalid routing message id: ", *strbuf.cur); @@ -90,7 +121,7 @@ namespace llarp InboundMessageParser::ParseMessageBuffer(const llarp_buffer_t& buf, IMessageHandler* h, const PathID_t& from, - llarp::Router* r) + AbstractRouter* r) { bool result = false; msg = nullptr; diff --git a/llarp/routing/message_parser.hpp b/llarp/routing/message_parser.hpp index d59391f78..3628ed01e 100644 --- a/llarp/routing/message_parser.hpp +++ b/llarp/routing/message_parser.hpp @@ -1,56 +1,42 @@ #ifndef LLARP_ROUTING_MESSAGE_PARSER_HPP #define LLARP_ROUTING_MESSAGE_PARSER_HPP -#include -#include -#include -#include -#include -#include -#include +#include #include +#include + namespace llarp { - struct Router; + struct AbstractRouter; + struct PathID_t; namespace routing { + struct IMessage; struct IMessageHandler; struct InboundMessageParser { InboundMessageParser(); + ~InboundMessageParser(); bool ParseMessageBuffer(const llarp_buffer_t& buf, IMessageHandler* handler, - const PathID_t& from, llarp::Router* r); + const PathID_t& from, AbstractRouter* r); private: static bool OnKey(dict_reader* r, llarp_buffer_t* key); + bool firstKey; char key; dict_reader reader; - struct MessageHolder - { - DataDiscardMessage D; - PathLatencyMessage L; - DHTMessage M; - PathConfirmMessage P; - PathTransferMessage T; - service::ProtocolFrame H; - TransferTrafficMessage I; - GrantExitMessage G; - RejectExitMessage J; - ObtainExitMessage O; - UpdateExitMessage U; - CloseExitMessage C; - }; - - IMessage* msg = nullptr; - MessageHolder m_Holder; + struct MessageHolder; + + IMessage* msg; + std::unique_ptr< MessageHolder > m_Holder; }; } // namespace routing } // namespace llarp diff --git a/llarp/routing/path_confirm.cpp b/llarp/routing/path_confirm.cpp index 388a13646..dec6ff447 100644 --- a/llarp/routing/path_confirm.cpp +++ b/llarp/routing/path_confirm.cpp @@ -53,7 +53,7 @@ namespace llarp bool PathConfirmMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h && h->HandlePathConfirmMessage(this, r); } diff --git a/llarp/routing/path_latency.cpp b/llarp/routing/path_latency.cpp index 845690369..e3ecbeabd 100644 --- a/llarp/routing/path_latency.cpp +++ b/llarp/routing/path_latency.cpp @@ -47,7 +47,7 @@ namespace llarp bool PathLatencyMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h && h->HandlePathLatencyMessage(this, r); } diff --git a/llarp/routing/path_transfer.cpp b/llarp/routing/path_transfer.cpp index b9c265e5e..6e360f533 100644 --- a/llarp/routing/path_transfer.cpp +++ b/llarp/routing/path_transfer.cpp @@ -59,7 +59,7 @@ namespace llarp bool PathTransferMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h->HandlePathTransferMessage(this, r); } diff --git a/llarp/rpc/rpc.cpp b/llarp/rpc/rpc.cpp index 04accd238..33b78f1e7 100644 --- a/llarp/rpc/rpc.cpp +++ b/llarp/rpc/rpc.cpp @@ -1,6 +1,6 @@ #include -#include +#include #ifdef USE_ABYSS #include @@ -116,7 +116,7 @@ namespace llarp struct CallerImpl : public ::abyss::http::JSONRPC { - Router* router; + AbstractRouter* router; llarp_time_t m_NextKeyUpdate = 0; const llarp_time_t KeyUpdateInterval = 1000 * 60 * 2; using PubkeyList_t = GetServiceNodeListHandler::PubkeyList_t; @@ -124,7 +124,7 @@ namespace llarp std::string username; std::string password; - CallerImpl(Router* r) : ::abyss::http::JSONRPC(), router(r) + CallerImpl(AbstractRouter* r) : ::abyss::http::JSONRPC(), router(r) { } @@ -207,8 +207,8 @@ namespace llarp struct Handler : public ::abyss::httpd::IRPCHandler { - Router* router; - Handler(::abyss::httpd::ConnImpl* conn, Router* r) + AbstractRouter* router; + Handler(::abyss::httpd::ConnImpl* conn, AbstractRouter* r) : ::abyss::httpd::IRPCHandler(conn), router(r) { } @@ -293,11 +293,11 @@ namespace llarp struct ReqHandlerImpl : public ::abyss::httpd::BaseReqHandler { - ReqHandlerImpl(Router* r, llarp_time_t reqtimeout) + ReqHandlerImpl(AbstractRouter* r, llarp_time_t reqtimeout) : ::abyss::httpd::BaseReqHandler(reqtimeout), router(r) { } - Router* router; + AbstractRouter* router; ::abyss::httpd::IRPCHandler* CreateHandler(::abyss::httpd::ConnImpl* conn) { @@ -307,10 +307,10 @@ namespace llarp struct ServerImpl { - Router* router; + AbstractRouter* router; ReqHandlerImpl _handler; - ServerImpl(Router* r) : router(r), _handler(r, 2000) + ServerImpl(AbstractRouter* r) : router(r), _handler(r, 2000) { } @@ -346,7 +346,7 @@ namespace llarp #else struct ServerImpl { - ServerImpl(__attribute__((unused)) Router* r){}; + ServerImpl(__attribute__((unused)) AbstractRouter* r){}; bool Start(__attribute__((unused)) const std::string& addr) @@ -362,7 +362,7 @@ namespace llarp struct CallerImpl { - CallerImpl(__attribute__((unused)) Router* r) + CallerImpl(__attribute__((unused)) AbstractRouter* r) { } @@ -395,7 +395,8 @@ namespace llarp #endif - Caller::Caller(Router* r) : m_Impl(std::make_unique< CallerImpl >(r)) + Caller::Caller(AbstractRouter* r) + : m_Impl(std::make_unique< CallerImpl >(r)) { } @@ -427,7 +428,8 @@ namespace llarp m_Impl->SetBasicAuth(user, passwd); } - Server::Server(Router* r) : m_Impl(std::make_unique< ServerImpl >(r)) + Server::Server(AbstractRouter* r) + : m_Impl(std::make_unique< ServerImpl >(r)) { } diff --git a/llarp/rpc/rpc.hpp b/llarp/rpc/rpc.hpp index 2fe990a03..3f9a48fa7 100644 --- a/llarp/rpc/rpc.hpp +++ b/llarp/rpc/rpc.hpp @@ -10,7 +10,7 @@ namespace llarp { struct PubKey; - struct Router; + struct AbstractRouter; namespace rpc { @@ -19,7 +19,7 @@ namespace llarp /// jsonrpc server struct Server { - Server(Router* r); + Server(AbstractRouter* r); ~Server(); bool @@ -38,7 +38,7 @@ namespace llarp /// jsonrpc caller struct Caller { - Caller(Router* r); + Caller(AbstractRouter* r); ~Caller(); /// set http basic auth for use with remote rpc endpoint diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index ab7a8d658..64d25baa7 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -2,14 +2,15 @@ #include #include -#include +#include +#include #include namespace llarp { namespace service { - Context::Context(llarp::Router *r) : m_Router(r) + Context::Context(AbstractRouter *r) : m_Router(r) { } @@ -312,16 +313,16 @@ namespace llarp static std::map< std::string, std::function< llarp::service::Endpoint *( - const std::string &, llarp::Router *, + const std::string &, AbstractRouter *, llarp::service::Context *) > > endpointConstructors = { {"tun", - [](const std::string &nick, llarp::Router *r, + [](const std::string &nick, AbstractRouter *r, llarp::service::Context *c) -> llarp::service::Endpoint * { return new llarp::handlers::TunEndpoint(nick, r, c); }}, {"null", - [](const std::string &nick, llarp::Router *r, + [](const std::string &nick, AbstractRouter *r, llarp::service::Context *c) -> llarp::service::Endpoint * { return new llarp::handlers::NullEndpoint(nick, r, c); }}}; diff --git a/llarp/service/context.hpp b/llarp/service/context.hpp index 80bcedfe1..80e14ad02 100644 --- a/llarp/service/context.hpp +++ b/llarp/service/context.hpp @@ -15,7 +15,7 @@ namespace llarp /// holds all the hidden service endpoints we own struct Context : public util::IStateful { - Context(llarp::Router *r); + Context(AbstractRouter *r); ~Context(); void @@ -96,7 +96,7 @@ namespace llarp StartAll(); private: - llarp::Router *m_Router; + AbstractRouter *m_Router; std::unordered_map< std::string, std::unique_ptr< Endpoint > > m_Endpoints; std::list< std::unique_ptr< Endpoint > > m_Stopped; diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 268b055e6..28dbecdb4 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -6,7 +6,10 @@ #include #include #include -#include +#include +#include +#include +#include #include #include @@ -16,7 +19,7 @@ namespace llarp { namespace service { - Endpoint::Endpoint(const std::string& name, llarp::Router* r, + Endpoint::Endpoint(const std::string& name, AbstractRouter* r, Context* parent) : path::Builder(r, r->dht(), 6, DEFAULT_HOP_LENGTH) , context(parent) @@ -69,7 +72,7 @@ namespace llarp if(m_IsolatedNetLoop) return m_IsolatedNetLoop; else - return m_Router->netloop; + return m_Router->netloop(); } bool @@ -623,7 +626,7 @@ namespace llarp } bool - Endpoint::PublishIntroSet(llarp::Router* r) + Endpoint::PublishIntroSet(AbstractRouter* r) { // publish via near router RouterID location = m_Identity.pub.Addr().as_array(); @@ -671,7 +674,7 @@ namespace llarp } bool - Endpoint::PublishIntroSetVia(llarp::Router* r, path::Path* path) + Endpoint::PublishIntroSetVia(AbstractRouter* r, path::Path* path) { auto job = new PublishIntroSetJob(this, GenTXID(), m_IntroSet); if(job->SendRequestViaPath(path, r)) @@ -809,8 +812,8 @@ namespace llarp return false; llarp_async_verify_rc* job = new llarp_async_verify_rc; job->nodedb = m_Router->nodedb(); - job->cryptoworker = m_Router->tp; - job->diskworker = m_Router->disk; + job->cryptoworker = m_Router->threadpool(); + job->diskworker = m_Router->diskworker(); job->logic = m_Router->logic(); job->hook = nullptr; job->rc = msg->R[0]; @@ -1312,12 +1315,13 @@ namespace llarp } } // no converstation - return EnsurePathToService(remote, - [](Address, OutboundContext* c) { - if(c) - c->UpdateIntroSet(true); - }, - 5000, false); + return EnsurePathToService( + remote, + [](Address, OutboundContext* c) { + if(c) + c->UpdateIntroSet(true); + }, + 5000, false); } bool @@ -1354,8 +1358,9 @@ namespace llarp { nodedb->select_random_hop(hops[hop - 1], hops[hop], hop); --tries; - } while(m_Endpoint->Router()->routerProfiling.IsBad(hops[hop].pubkey) - && tries > 0); + } while( + m_Endpoint->Router()->routerProfiling().IsBad(hops[hop].pubkey) + && tries > 0); return tries > 0; } return false; @@ -1898,7 +1903,7 @@ namespace llarp llarp_threadpool* Endpoint::Worker() { - return m_Router->tp; + return m_Router->threadpool(); } } // namespace service diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 53efacf4d..58b876fef 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -37,7 +37,7 @@ namespace llarp static const size_t MAX_OUTBOUND_CONTEXT_COUNT = 4; - Endpoint(const std::string& nickname, llarp::Router* r, Context* parent); + Endpoint(const std::string& nickname, AbstractRouter* r, Context* parent); ~Endpoint(); virtual util::StatusObject @@ -84,7 +84,7 @@ namespace llarp llarp_threadpool* Worker(); - llarp::Router* + AbstractRouter* Router() { return m_Router; @@ -106,10 +106,10 @@ namespace llarp EnsureReplyPath(const ServiceInfo& addr); bool - PublishIntroSet(llarp::Router* r) override; + PublishIntroSet(AbstractRouter* r) override; bool - PublishIntroSetVia(llarp::Router* r, path::Path* p); + PublishIntroSetVia(AbstractRouter* r, path::Path* p); bool HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg) override; @@ -461,7 +461,7 @@ namespace llarp std::unique_ptr< llarp::exit::BaseSession > m_Exit; private: - llarp::Router* m_Router; + AbstractRouter* m_Router; llarp_threadpool* m_IsolatedWorker = nullptr; llarp::Logic* m_IsolatedLogic = nullptr; llarp_ev_loop* m_IsolatedNetLoop = nullptr; diff --git a/llarp/service/lookup.cpp b/llarp/service/lookup.cpp index 0765b8ca9..b1f861fca 100644 --- a/llarp/service/lookup.cpp +++ b/llarp/service/lookup.cpp @@ -1,12 +1,13 @@ #include #include -#include #include #include namespace llarp { + struct AbstractRouter; + namespace service { IServiceLookup::IServiceLookup(ILookupHolder *p, uint64_t tx, @@ -18,7 +19,7 @@ namespace llarp } bool - IServiceLookup::SendRequestViaPath(path::Path *path, Router *r) + IServiceLookup::SendRequestViaPath(path::Path *path, AbstractRouter *r) { auto msg = BuildRequestMessage(); if(!msg) diff --git a/llarp/service/lookup.hpp b/llarp/service/lookup.hpp index d24788176..df9b4811b 100644 --- a/llarp/service/lookup.hpp +++ b/llarp/service/lookup.hpp @@ -48,7 +48,7 @@ namespace llarp /// build a new requset message and send it via a path bool - SendRequestViaPath(llarp::path::Path* p, llarp::Router* r); + SendRequestViaPath(llarp::path::Path* p, AbstractRouter* r); ILookupHolder* parent; uint64_t txid; diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index 127e027f1..90ef54ae9 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -392,7 +392,8 @@ namespace llarp bool ProtocolFrame::HandleMessage(llarp::routing::IMessageHandler* h, - __attribute__((unused)) llarp::Router* r) const + __attribute__((unused)) + AbstractRouter* r) const { return h->HandleHiddenServiceFrame(this); } diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index 6a56f6d8c..191c2a0b6 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -134,7 +134,7 @@ namespace llarp Verify(Crypto* c, const ServiceInfo& from) const; bool - HandleMessage(routing::IMessageHandler* h, Router* r) const override; + HandleMessage(routing::IMessageHandler* h, AbstractRouter* r) const override; }; } // namespace service } // namespace llarp diff --git a/test/exit/test_llarp_exit_context.cpp b/test/exit/test_llarp_exit_context.cpp index 7f3e9fd6f..85380f1dd 100644 --- a/test/exit/test_llarp_exit_context.cpp +++ b/test/exit/test_llarp_exit_context.cpp @@ -23,9 +23,9 @@ TEST_F(ExitTest, AddMultipleIP) conf.emplace("exit", "true"); conf.emplace("type", "null"); conf.emplace("ifaddr", "10.0.0.1/24"); - ASSERT_TRUE(r.exitContext.AddExitEndpoint("test-exit", conf)); - ASSERT_TRUE(r.exitContext.ObtainNewExit(pk, firstPath, true)); - ASSERT_TRUE(r.exitContext.ObtainNewExit(pk, secondPath, true)); - ASSERT_TRUE(r.exitContext.FindEndpointForPath(firstPath)->LocalIP() - == r.exitContext.FindEndpointForPath(secondPath)->LocalIP()); + ASSERT_TRUE(r.exitContext().AddExitEndpoint("test-exit", conf)); + ASSERT_TRUE(r.exitContext().ObtainNewExit(pk, firstPath, true)); + ASSERT_TRUE(r.exitContext().ObtainNewExit(pk, secondPath, true)); + ASSERT_TRUE(r.exitContext().FindEndpointForPath(firstPath)->LocalIP() + == r.exitContext().FindEndpointForPath(secondPath)->LocalIP()); };