From 73d38f530f2eab209d9eb2a6142b324e3e8073bc Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 16 Apr 2014 15:54:28 -0400 Subject: [PATCH] store relay tag if introducer --- SSU.cpp | 34 +++++++++++++++++++++++++++++----- SSU.h | 4 ++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/SSU.cpp b/SSU.cpp index dffd338a..9e4e49ca 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -294,6 +294,7 @@ namespace ssu LogPrint ("SSU is not supported"); return; } + CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); uint8_t signedData[532]; // x,y, remote IP, remote port, our IP, our port, relayTag, signed on time memcpy (signedData, x, 256); // x @@ -311,8 +312,14 @@ namespace ssu memcpy (signedData + 512, payload - 6, 6); // remote endpoint IP and port *(uint32_t *)(signedData + 518) = htobe32 (address->host.to_v4 ().to_ulong ()); // our IP *(uint16_t *)(signedData + 522) = htobe16 (address->port); // our port - *(uint32_t *)(payload) = 0; // relay tag, always 0 for now - payload += 4; + uint32_t relayTag = 0; + if (i2p::context.GetRouterInfo ().IsIntroducer ()) + { + rnd.GenerateWord32 (relayTag); + m_Server.AddRelay (relayTag, m_RemoteEndpoint); + } + *(uint32_t *)(payload) = relayTag; + payload += 4; // relay tag *(uint32_t *)(payload) = htobe32 (i2p::util::GetSecondsSinceEpoch ()); // signed on time payload += 4; memcpy (signedData + 524, payload - 8, 8); // relayTag and signed on time @@ -320,7 +327,6 @@ namespace ssu // TODO: fill padding with random data uint8_t iv[16]; - CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); rnd.GenerateBlock (iv, 16); // random iv // encrypt signature and 8 bytes padding with newly created session key m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv); @@ -929,6 +935,19 @@ namespace ssu m_Socket.close (); } + void SSUServer::AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay) + { + m_Relays[tag] = relay; + } + + SSUSession * SSUServer::FindRelaySession (uint32_t tag) + { + auto it = m_Relays.find (tag); + if (it != m_Relays.end ()) + return FindSession (it->second); + return nullptr; + } + void SSUServer::Send (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to) { m_Socket.send_to (boost::asio::buffer (buf, len), to); @@ -968,12 +987,17 @@ namespace ssu if (!router) return nullptr; auto address = router->GetSSUAddress (); if (!address) return nullptr; - auto it = m_Sessions.find (boost::asio::ip::udp::endpoint (address->host, address->port)); + return FindSession (boost::asio::ip::udp::endpoint (address->host, address->port)); + } + + SSUSession * SSUServer::FindSession (const boost::asio::ip::udp::endpoint& e) + { + auto it = m_Sessions.find (e); if (it != m_Sessions.end ()) return it->second; else return nullptr; - } + } SSUSession * SSUServer::GetSession (const i2p::data::RouterInfo * router, bool peerTest) { diff --git a/SSU.h b/SSU.h index f648f5db..9945ff04 100644 --- a/SSU.h +++ b/SSU.h @@ -157,12 +157,15 @@ namespace ssu void Stop (); SSUSession * GetSession (const i2p::data::RouterInfo * router, bool peerTest = false); SSUSession * FindSession (const i2p::data::RouterInfo * router); + SSUSession * FindSession (const boost::asio::ip::udp::endpoint& e); void DeleteSession (SSUSession * session); void DeleteAllSessions (); boost::asio::io_service& GetService () { return m_Socket.get_io_service(); }; const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; }; void Send (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to); + void AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay); + SSUSession * FindRelaySession (uint32_t tag); private: @@ -176,6 +179,7 @@ namespace ssu boost::asio::ip::udp::endpoint m_SenderEndpoint; uint8_t m_ReceiveBuffer[2*SSU_MTU]; std::map m_Sessions; + std::map m_Relays; // we are introducer public: // for HTTP only