From 7b768ccb26a96b17cdf6ffb160519778339498cf Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Oct 2014 12:25:53 -0400 Subject: [PATCH] moved transports to 'transport' namespace --- Daemon.cpp | 4 ++-- HTTPServer.cpp | 4 ++-- I2NPProtocol.cpp | 11 ++++++----- NTCPSession.cpp | 18 +++++++++--------- NTCPSession.h | 4 ++-- NetDb.cpp | 10 ++++++---- SSU.cpp | 6 +++--- SSU.h | 4 ++-- SSUData.cpp | 2 +- SSUData.h | 2 +- TransitTunnel.cpp | 2 +- Transports.cpp | 34 +++++++++++++++++++--------------- Transports.h | 27 +++++++++++++++------------ Tunnel.cpp | 2 +- TunnelEndpoint.cpp | 4 ++-- TunnelGateway.cpp | 2 +- 16 files changed, 73 insertions(+), 63 deletions(-) diff --git a/Daemon.cpp b/Daemon.cpp index 13f99db6..ea98378b 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -103,7 +103,7 @@ namespace i2p LogPrint("HTTP Server started"); i2p::data::netdb.Start(); LogPrint("NetDB started"); - i2p::transports.Start(); + i2p::transport::transports.Start(); LogPrint("Transports started"); i2p::tunnel::tunnels.Start(); LogPrint("Tunnels started"); @@ -120,7 +120,7 @@ namespace i2p LogPrint("Client stoped"); i2p::tunnel::tunnels.Stop(); LogPrint("Tunnels stoped"); - i2p::transports.Stop(); + i2p::transport::transports.Stop(); LogPrint("Transports stoped"); i2p::data::netdb.Stop(); LogPrint("NetDB stoped"); diff --git a/HTTPServer.cpp b/HTTPServer.cpp index b08948d1..93cfb528 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -699,7 +699,7 @@ namespace util void HTTPConnection::ShowTransports (std::stringstream& s) { s << "NTCP
"; - for (auto it: i2p::transports.GetNTCPSessions ()) + for (auto it: i2p::transport::transports.GetNTCPSessions ()) { // RouterInfo of incoming connection doesn't have address bool outgoing = it.second->GetRemoteRouterInfo ().GetNTCPAddress (); @@ -714,7 +714,7 @@ namespace util } s << std::endl; } - auto ssuServer = i2p::transports.GetSSUServer (); + auto ssuServer = i2p::transport::transports.GetSSUServer (); if (ssuServer) { s << "
SSU
"; diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 12ee678f..d4c6d71e 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -13,9 +13,10 @@ #include "Garlic.h" #include "I2NPProtocol.h" +using namespace i2p::transport; + namespace i2p { - I2NPMessage * NewI2NPMessage () { return new I2NPMessageBuffer(); @@ -353,13 +354,13 @@ namespace i2p if (clearText.flag & 0x40) // we are endpoint of outboud tunnel { // so we send it to reply tunnel - i2p::transports.SendMessage (clearText.nextIdent, + transports.SendMessage (clearText.nextIdent, CreateTunnelGatewayMsg (be32toh (clearText.nextTunnel), eI2NPVariableTunnelBuildReply, buf, len, be32toh (clearText.nextMessageID))); } else - i2p::transports.SendMessage (clearText.nextIdent, + transports.SendMessage (clearText.nextIdent, CreateI2NPMessage (eI2NPVariableTunnelBuild, buf, len, be32toh (clearText.nextMessageID))); } } @@ -373,13 +374,13 @@ namespace i2p if (clearText.flag & 0x40) // we are endpoint of outbound tunnel { // so we send it to reply tunnel - i2p::transports.SendMessage (clearText.nextIdent, + transports.SendMessage (clearText.nextIdent, CreateTunnelGatewayMsg (be32toh (clearText.nextTunnel), eI2NPTunnelBuildReply, buf, len, be32toh (clearText.nextMessageID))); } else - i2p::transports.SendMessage (clearText.nextIdent, + transports.SendMessage (clearText.nextIdent, CreateI2NPMessage (eI2NPTunnelBuild, buf, len, be32toh (clearText.nextMessageID))); } } diff --git a/NTCPSession.cpp b/NTCPSession.cpp index aabe7fd4..a4a73dd4 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -17,14 +17,14 @@ using namespace i2p::crypto; namespace i2p { -namespace ntcp +namespace transport { NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo): m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false), m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_NumSentBytes (0), m_NumReceivedBytes (0) { - m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); + m_DHKeysPair = transports.GetNextDHKeysPair (); m_Establisher = new Establisher; } @@ -77,12 +77,12 @@ namespace ntcp { m_IsEstablished = false; m_Socket.close (); - i2p::transports.RemoveNTCPSession (this); + transports.RemoveNTCPSession (this); int numDelayed = 0; for (auto it :m_DelayedMessages) { // try to send them again - i2p::transports.SendMessage (m_RemoteRouterInfo.GetIdentHash (), it); + transports.SendMessage (m_RemoteRouterInfo.GetIdentHash (), it); numDelayed++; } m_DelayedMessages.clear (); @@ -119,7 +119,7 @@ namespace ntcp void NTCPSession::ClientLogin () { if (!m_DHKeysPair) - m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); + m_DHKeysPair = transports.GetNextDHKeysPair (); // send Phase1 const uint8_t * x = m_DHKeysPair->publicKey; memcpy (m_Establisher->phase1.pubKey, x, 256); @@ -189,7 +189,7 @@ namespace ntcp void NTCPSession::SendPhase2 () { if (!m_DHKeysPair) - m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); + m_DHKeysPair = transports.GetNextDHKeysPair (); const uint8_t * y = m_DHKeysPair->publicKey; memcpy (m_Establisher->phase2.pubKey, y, 256); uint8_t xy[512]; @@ -238,7 +238,7 @@ namespace ntcp if (ecode != boost::asio::error::operation_aborted) { GetRemoteRouterInfo ().SetUnreachable (true); // this RouterInfo is not valid - i2p::transports.ReuseDHKeysPair (m_DHKeysPair); + transports.ReuseDHKeysPair (m_DHKeysPair); m_DHKeysPair = nullptr; Terminate (); } @@ -263,7 +263,7 @@ namespace ntcp if (memcmp (hxy, m_Establisher->phase2.encrypted.hxy, 32)) { LogPrint ("Incorrect hash"); - i2p::transports.ReuseDHKeysPair (m_DHKeysPair); + transports.ReuseDHKeysPair (m_DHKeysPair); m_DHKeysPair = nullptr; Terminate (); return ; @@ -635,7 +635,7 @@ namespace ntcp { LogPrint ("NTCP server session connected"); SetIsEstablished (true); - i2p::transports.AddNTCPSession (this); + transports.AddNTCPSession (this); SendTimeSyncMessage (); SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are diff --git a/NTCPSession.h b/NTCPSession.h index adb69da6..4a5e463a 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -15,7 +15,7 @@ namespace i2p { -namespace ntcp +namespace transport { #pragma pack(1) @@ -67,7 +67,7 @@ namespace ntcp const size_t NTCP_BUFFER_SIZE = 1040; // fits one tunnel message (1028) const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes - class NTCPSession: public i2p::transport::TransportSession + class NTCPSession: public TransportSession { public: diff --git a/NetDb.cpp b/NetDb.cpp index eb7561f7..f1c20498 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -15,6 +15,8 @@ #include "Reseed.h" #include "util.h" +using namespace i2p::transport; + namespace i2p { namespace data @@ -403,7 +405,7 @@ namespace data RequestedDestination * dest = CreateRequestedDestination (destination, false, false, pool); auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ()); if (floodfill) - i2p::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ())); + transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ())); } } @@ -655,10 +657,10 @@ namespace data if (outbound) outbound->SendTunnelDataMsg (buf+32, replyTunnelID, replyMsg); else - i2p::transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, replyMsg)); + transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, replyMsg)); } else - i2p::transports.SendMessage (buf+32, replyMsg); + transports.SendMessage (buf+32, replyMsg); } i2p::DeleteI2NPMessage (msg); } @@ -712,7 +714,7 @@ namespace data }); } else - i2p::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ())); + i2p::transport::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ())); } else DeleteRequestedDestination (dest); diff --git a/SSU.cpp b/SSU.cpp index ce4c37bd..0ceff06e 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -12,7 +12,7 @@ namespace i2p { -namespace ssu +namespace transport { SSUSession::SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint, @@ -173,7 +173,7 @@ namespace ssu LogPrint ("Session request received"); m_RemoteEndpoint = senderEndpoint; if (!m_DHKeysPair) - m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); + m_DHKeysPair = transports.GetNextDHKeysPair (); CreateAESandMacKey (buf + sizeof (SSUHeader)); SendSessionCreated (buf + sizeof (SSUHeader)); } @@ -608,7 +608,7 @@ namespace ssu { // set connect timer ScheduleConnectTimer (); - m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); + m_DHKeysPair = transports.GetNextDHKeysPair (); SendSessionRequest (); } } diff --git a/SSU.h b/SSU.h index 261494ab..7f1d2563 100644 --- a/SSU.h +++ b/SSU.h @@ -18,7 +18,7 @@ namespace i2p { -namespace ssu +namespace transport { #pragma pack(1) struct SSUHeader @@ -58,7 +58,7 @@ namespace ssu }; class SSUServer; - class SSUSession: public i2p::transport::TransportSession + class SSUSession: public TransportSession { public: diff --git a/SSUData.cpp b/SSUData.cpp index 377c8fbb..8eb69051 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -8,7 +8,7 @@ namespace i2p { -namespace ssu +namespace transport { SSUData::SSUData (SSUSession& session): m_Session (session), m_ResendTimer (session.m_Server.GetService ()) diff --git a/SSUData.h b/SSUData.h index 9ac431a2..2b6abead 100644 --- a/SSUData.h +++ b/SSUData.h @@ -13,7 +13,7 @@ namespace i2p { -namespace ssu +namespace transport { const size_t SSU_MTU = 1484; diff --git a/TransitTunnel.cpp b/TransitTunnel.cpp index 9ed8a542..25b82424 100644 --- a/TransitTunnel.cpp +++ b/TransitTunnel.cpp @@ -34,7 +34,7 @@ namespace tunnel *(uint32_t *)(tunnelMsg->GetPayload ()) = htobe32 (m_NextTunnelID); FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData); - i2p::transports.SendMessage (m_NextIdent, tunnelMsg); + i2p::transport::transports.SendMessage (m_NextIdent, tunnelMsg); } void TransitTunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg) diff --git a/Transports.cpp b/Transports.cpp index 590dfa77..6747febd 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -10,6 +10,8 @@ using namespace i2p::data; namespace i2p +{ +namespace transport { DHKeysPairSupplier::DHKeysPairSupplier (int size): m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr) @@ -66,7 +68,7 @@ namespace i2p } } - i2p::transport::DHKeysPair * DHKeysPairSupplier::Acquire () + DHKeysPair * DHKeysPairSupplier::Acquire () { if (!m_Queue.empty ()) { @@ -78,14 +80,14 @@ namespace i2p } else // queue is empty, create new { - i2p::transport::DHKeysPair * pair = new i2p::transport::DHKeysPair (); + DHKeysPair * pair = new DHKeysPair (); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey); return pair; } } - void DHKeysPairSupplier::Return (i2p::transport::DHKeysPair * pair) + void DHKeysPairSupplier::Return (DHKeysPair * pair) { std::unique_lock l(m_AcquiredMutex); m_Queue.push (pair); @@ -119,7 +121,7 @@ namespace i2p boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port)); LogPrint ("Start listening TCP port ", address.port); - auto conn = new i2p::ntcp::NTCPServerConnection (m_Service); + auto conn = new NTCPServerConnection (m_Service); m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, conn, boost::asio::placeholders::error)); } @@ -127,7 +129,7 @@ namespace i2p { if (!m_SSUServer) { - m_SSUServer = new i2p::ssu::SSUServer (address.port); + m_SSUServer = new SSUServer (address.port); LogPrint ("Start listening UDP port ", address.port); m_SSUServer->Start (); DetectExternalIP (); @@ -179,19 +181,19 @@ namespace i2p } } - void Transports::AddNTCPSession (i2p::ntcp::NTCPSession * session) + void Transports::AddNTCPSession (NTCPSession * session) { if (session) m_NTCPSessions[session->GetRemoteRouterInfo ().GetIdentHash ()] = session; } - void Transports::RemoveNTCPSession (i2p::ntcp::NTCPSession * session) + void Transports::RemoveNTCPSession (NTCPSession * session) { if (session) m_NTCPSessions.erase (session->GetRemoteRouterInfo ().GetIdentHash ()); } - void Transports::HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error) + void Transports::HandleAccept (NTCPServerConnection * conn, const boost::system::error_code& error) { if (!error) { @@ -203,13 +205,13 @@ namespace i2p if (error != boost::asio::error::operation_aborted) { - conn = new i2p::ntcp::NTCPServerConnection (m_Service); + conn = new NTCPServerConnection (m_Service); m_NTCPAcceptor->async_accept(conn->GetSocket (), boost::bind (&Transports::HandleAccept, this, conn, boost::asio::placeholders::error)); } } - i2p::ntcp::NTCPSession * Transports::GetNextNTCPSession () + NTCPSession * Transports::GetNextNTCPSession () { for (auto session: m_NTCPSessions) if (session.second->IsEstablished ()) @@ -217,7 +219,7 @@ namespace i2p return 0; } - i2p::ntcp::NTCPSession * Transports::FindNTCPSession (const i2p::data::IdentHash& ident) + NTCPSession * Transports::FindNTCPSession (const i2p::data::IdentHash& ident) { auto it = m_NTCPSessions.find (ident); if (it != m_NTCPSessions.end ()) @@ -252,9 +254,9 @@ namespace i2p // existing session not found. create new // try NTCP first if message size < 16K auto address = r->GetNTCPAddress (); - if (address && !r->UsesIntroducer () && !r->IsUnreachable () && msg->GetLength () < i2p::ntcp::NTCP_MAX_MESSAGE_SIZE) + if (address && !r->UsesIntroducer () && !r->IsUnreachable () && msg->GetLength () < NTCP_MAX_MESSAGE_SIZE) { - auto s = new i2p::ntcp::NTCPClient (m_Service, address->host, address->port, *r); + auto s = new NTCPClient (m_Service, address->host, address->port, *r); AddNTCPSession (s); s->SendI2NPMessage (msg); } @@ -328,13 +330,15 @@ namespace i2p } } - i2p::transport::DHKeysPair * Transports::GetNextDHKeysPair () + DHKeysPair * Transports::GetNextDHKeysPair () { return m_DHKeysPairSupplier.Acquire (); } - void Transports::ReuseDHKeysPair (i2p::transport::DHKeysPair * pair) + void Transports::ReuseDHKeysPair (DHKeysPair * pair) { m_DHKeysPairSupplier.Return (pair); } } +} + diff --git a/Transports.h b/Transports.h index aa9812e4..c795c2b4 100644 --- a/Transports.h +++ b/Transports.h @@ -18,6 +18,8 @@ #include "Identity.h" namespace i2p +{ +namespace transport { class DHKeysPairSupplier { @@ -27,8 +29,8 @@ namespace i2p ~DHKeysPairSupplier (); void Start (); void Stop (); - i2p::transport::DHKeysPair * Acquire (); - void Return (i2p::transport::DHKeysPair * pair); + DHKeysPair * Acquire (); + void Return (DHKeysPair * pair); private: @@ -38,7 +40,7 @@ namespace i2p private: const int m_QueueSize; - std::queue m_Queue; + std::queue m_Queue; bool m_IsRunning; std::thread * m_Thread; @@ -59,13 +61,13 @@ namespace i2p boost::asio::io_service& GetService () { return m_Service; }; i2p::transport::DHKeysPair * GetNextDHKeysPair (); - void ReuseDHKeysPair (i2p::transport::DHKeysPair * pair); + void ReuseDHKeysPair (DHKeysPair * pair); - void AddNTCPSession (i2p::ntcp::NTCPSession * session); - void RemoveNTCPSession (i2p::ntcp::NTCPSession * session); + void AddNTCPSession (NTCPSession * session); + void RemoveNTCPSession (NTCPSession * session); - i2p::ntcp::NTCPSession * GetNextNTCPSession (); - i2p::ntcp::NTCPSession * FindNTCPSession (const i2p::data::IdentHash& ident); + NTCPSession * GetNextNTCPSession (); + NTCPSession * FindNTCPSession (const i2p::data::IdentHash& ident); void SendMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); void CloseSession (const i2p::data::RouterInfo * router); @@ -73,7 +75,7 @@ namespace i2p private: void Run (); - void HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error); + void HandleAccept (NTCPServerConnection * conn, const boost::system::error_code& error); void HandleResendTimer (const boost::system::error_code& ecode, boost::asio::deadline_timer * timer, const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); void PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); @@ -89,8 +91,8 @@ namespace i2p boost::asio::io_service::work m_Work; boost::asio::ip::tcp::acceptor * m_NTCPAcceptor; - std::map m_NTCPSessions; - i2p::ssu::SSUServer * m_SSUServer; + std::map m_NTCPSessions; + SSUServer * m_SSUServer; DHKeysPairSupplier m_DHKeysPairSupplier; @@ -98,10 +100,11 @@ namespace i2p // for HTTP only const decltype(m_NTCPSessions)& GetNTCPSessions () const { return m_NTCPSessions; }; - const i2p::ssu::SSUServer * GetSSUServer () const { return m_SSUServer; }; + const SSUServer * GetSSUServer () const { return m_SSUServer; }; }; extern Transports transports; } +} #endif diff --git a/Tunnel.cpp b/Tunnel.cpp index 2da68772..c16981ae 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -92,7 +92,7 @@ namespace tunnel if (outboundTunnel) outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, msg); else - i2p::transports.SendMessage (GetNextIdentHash (), msg); + i2p::transport::transports.SendMessage (GetNextIdentHash (), msg); } bool Tunnel::HandleTunnelBuildResponse (uint8_t * msg, size_t len) diff --git a/TunnelEndpoint.cpp b/TunnelEndpoint.cpp index ca46199a..6074254e 100644 --- a/TunnelEndpoint.cpp +++ b/TunnelEndpoint.cpp @@ -235,7 +235,7 @@ namespace tunnel i2p::HandleI2NPMessage (msg.data); break; case eDeliveryTypeTunnel: - i2p::transports.SendMessage (msg.hash, i2p::CreateTunnelGatewayMsg (msg.tunnelID, msg.data)); + i2p::transport::transports.SendMessage (msg.hash, i2p::CreateTunnelGatewayMsg (msg.tunnelID, msg.data)); break; case eDeliveryTypeRouter: if (msg.hash == i2p::context.GetRouterInfo ().GetIdentHash ()) // check if message is sent to us @@ -253,7 +253,7 @@ namespace tunnel *ds = *(msg.data); i2p::data::netdb.PostI2NPMsg (ds); } - i2p::transports.SendMessage (msg.hash, msg.data); + i2p::transport::transports.SendMessage (msg.hash, msg.data); } else // we shouldn't send this message. possible leakage { diff --git a/TunnelGateway.cpp b/TunnelGateway.cpp index c12c4519..d2996b17 100644 --- a/TunnelGateway.cpp +++ b/TunnelGateway.cpp @@ -186,7 +186,7 @@ namespace tunnel { m_Tunnel->EncryptTunnelMsg (tunnelMsg); FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData); - i2p::transports.SendMessage (m_Tunnel->GetNextIdentHash (), tunnelMsg); + i2p::transport::transports.SendMessage (m_Tunnel->GetNextIdentHash (), tunnelMsg); m_NumSentBytes += TUNNEL_DATA_MSG_SIZE; } m_Buffer.ClearTunnelDataMsgs ();