diff --git a/TunnelConfig.h b/TunnelConfig.h index 76434f9c..d9827fe8 100644 --- a/TunnelConfig.h +++ b/TunnelConfig.h @@ -107,7 +107,7 @@ namespace tunnel } }; - class TunnelConfig + class TunnelConfig: public std::enable_shared_from_this { public: @@ -170,10 +170,24 @@ namespace tunnel return num; } + bool IsInbound () const { return m_FirstHop->isGateway; } + + std::vector > GetPeers () const + { + std::vector > peers; + TunnelHopConfig * hop = m_FirstHop; + while (hop) + { + peers.push_back (hop->router); + hop = hop->next; + } + return peers; + } + void Print (std::stringstream& s) const { TunnelHopConfig * hop = m_FirstHop; - if (!m_FirstHop->isGateway) + if (!IsInbound ()) // outbound s << "me"; s << "-->" << m_FirstHop->tunnelID; while (hop) @@ -191,42 +205,15 @@ namespace tunnel std::shared_ptr Invert () const { - auto newConfig = new TunnelConfig (); - TunnelHopConfig * hop = m_FirstHop, * nextNewHop = nullptr; - while (hop) - { - TunnelHopConfig * newHop = new TunnelHopConfig (hop->router); - if (nextNewHop) - newHop->SetNext (nextNewHop); - nextNewHop = newHop; - newHop->isEndpoint = hop->isGateway; - newHop->isGateway = hop->isEndpoint; - - if (!hop->prev) // first hop - { - newConfig->m_LastHop = newHop; - if (hop->isGateway) // inbound tunnel - newHop->SetReplyHop (m_FirstHop); // use it as reply tunnel - else - newHop->SetNextRouter (i2p::context.GetSharedRouterInfo ()); - } - if (!hop->next) newConfig->m_FirstHop = newHop; // last hop - - hop = hop->next; - } - return std::shared_ptr(newConfig); + auto peers = GetPeers (); + std::reverse (peers.begin (), peers.end ()); + // we use ourself as reply tunnel for outbound tunnel + return IsInbound () ? std::make_shared(peers, shared_from_this ()) : std::make_shared(peers); } std::shared_ptr Clone (std::shared_ptr replyTunnelConfig = nullptr) const { - std::vector > peers; - TunnelHopConfig * hop = m_FirstHop; - while (hop) - { - peers.push_back (hop->router); - hop = hop->next; - } - return std::make_shared (peers, replyTunnelConfig); + return std::make_shared (GetPeers (), replyTunnelConfig); } private: