From 592108639102c52fda5a25acde54fb4b25a23bba Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 23 Apr 2019 13:35:20 -0400 Subject: [PATCH] common codepath for sending protocol frames --- llarp/service/outbound_context.cpp | 8 +++--- llarp/service/sendcontext.cpp | 40 ++++++++---------------------- llarp/service/sendcontext.hpp | 10 ++++---- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index 99d322ffd..b18ad5944 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -178,9 +178,9 @@ namespace llarp void OutboundContext::AsyncGenIntro(const llarp_buffer_t& payload, - __attribute__((unused)) ProtocolType t) + ProtocolType t) { - auto path = m_PathSet->GetPathByRouter(remoteIntro.router); + auto path = m_PathSet->GetNewestPathByRouter(remoteIntro.router); if(path == nullptr) { // try parent as fallback @@ -199,9 +199,11 @@ namespace llarp m_Endpoint->GetIdentity(), currentIntroSet.K, remoteIntro, m_DataHandler, currentConvoTag); - ex->hook = std::bind(&OutboundContext::Send, this, std::placeholders::_1); + ex->hook = + std::bind(&OutboundContext::Send, this, std::placeholders::_1, path); ex->msg.PutBuffer(payload); + ex->msg.proto = t; ex->msg.introReply = path->intro; ex->frame.F = ex->msg.introReply.pathID; llarp_threadpool_queue_job(m_Endpoint->CryptoWorker(), diff --git a/llarp/service/sendcontext.cpp b/llarp/service/sendcontext.cpp index a505c7032..5176e5496 100644 --- a/llarp/service/sendcontext.cpp +++ b/llarp/service/sendcontext.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace llarp { @@ -22,26 +23,20 @@ namespace llarp } bool - SendContext::Send(const ProtocolFrame& msg) + SendContext::Send(const ProtocolFrame& msg, path::Path_ptr path) { - auto path = m_PathSet->GetByEndpointWithID(remoteIntro.router, msg.F); - if(path) - { - const routing::PathTransferMessage transfer(msg, remoteIntro.pathID); - if(path->SendRoutingMessage(transfer, m_Endpoint->Router())) + auto transfer = std::make_shared< const routing::PathTransferMessage >( + msg, remoteIntro.pathID); + m_Endpoint->RouterLogic()->queue_func([=]() { + if(path->SendRoutingMessage(*transfer, m_Endpoint->Router())) { - LogInfo("sent intro to ", remoteIntro.pathID, " on ", - remoteIntro.router, " seqno=", sequenceNo); lastGoodSend = m_Endpoint->Now(); ++sequenceNo; - return true; } else LogError("Failed to send frame on path"); - } - else - LogError("cannot send because we have no path to ", remoteIntro.router); - return false; + }); + return true; } /// send on an established convo tag @@ -50,8 +45,7 @@ namespace llarp { auto crypto = m_Endpoint->Router()->crypto(); SharedSecret shared; - routing::PathTransferMessage msg; - ProtocolFrame& f = msg.T; + ProtocolFrame f; f.N.Randomize(); f.T = currentConvoTag; f.S = m_Endpoint->GetSeqNoForConvo(f.T); @@ -94,20 +88,8 @@ namespace llarp LogError("No cached session key"); return; } - - msg.P = remoteIntro.pathID; - msg.Y.Randomize(); - if(path->SendRoutingMessage(msg, m_Endpoint->Router())) - { - LogDebug("sent message via ", remoteIntro.pathID, " on ", - remoteIntro.router); - ++sequenceNo; - lastGoodSend = now; - } - else - { - LogWarn("Failed to send routing message for data"); - } + ++sequenceNo; + Send(f, path); } void diff --git a/llarp/service/sendcontext.hpp b/llarp/service/sendcontext.hpp index 73dfca717..7357a6bb4 100644 --- a/llarp/service/sendcontext.hpp +++ b/llarp/service/sendcontext.hpp @@ -24,17 +24,17 @@ namespace llarp AsyncEncryptAndSendTo(const llarp_buffer_t& payload, ProtocolType t); /// send a fully encrypted hidden service frame - /// via a path on our pathset with path id p + /// via a path bool - Send(const ProtocolFrame& f); + Send(const ProtocolFrame& f, path::Path_ptr path); SharedSecret sharedKey; ServiceInfo remoteIdent; Introduction remoteIntro; ConvoTag currentConvoTag; - path::PathSet* m_PathSet; - IDataHandler* m_DataHandler; - Endpoint* m_Endpoint; + path::PathSet* const m_PathSet; + IDataHandler* const m_DataHandler; + Endpoint* const m_Endpoint; uint64_t sequenceNo = 0; llarp_time_t lastGoodSend = 0; llarp_time_t createdAt;