From c4dda06cdeddd1ae273f4004a931f0fa9cf97c8c Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 20 Oct 2014 16:09:59 -0400 Subject: [PATCH] TransportSession added --- NTCPSession.cpp | 6 ++---- NTCPSession.h | 6 ++---- SSU.cpp | 5 ++--- SSU.h | 6 ++---- TransportSession.h | 30 ++++++++++++++++++++++++++++++ Transports.cpp | 12 ++++++------ Transports.h | 17 ++++++----------- 7 files changed, 50 insertions(+), 32 deletions(-) create mode 100644 TransportSession.h diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 5c9c8a8a..aabe7fd4 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -21,9 +21,8 @@ namespace ntcp { NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo): m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false), - m_DHKeysPair (nullptr), m_RemoteRouterInfo (in_RemoteRouterInfo), - m_ReceiveBufferOffset (0), m_NextMessage (nullptr), - m_NumSentBytes (0), m_NumReceivedBytes (0) + m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), + m_NextMessage (nullptr), m_NumSentBytes (0), m_NumReceivedBytes (0) { m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); m_Establisher = new Establisher; @@ -32,7 +31,6 @@ namespace ntcp NTCPSession::~NTCPSession () { delete m_Establisher; - delete m_DHKeysPair; if (m_NextMessage) i2p::DeleteI2NPMessage (m_NextMessage); for (auto it :m_DelayedMessages) diff --git a/NTCPSession.h b/NTCPSession.h index 51215c38..adb69da6 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -11,11 +11,10 @@ #include "Identity.h" #include "RouterInfo.h" #include "I2NPProtocol.h" +#include "TransportSession.h" namespace i2p { - class DHKeysPair; - namespace ntcp { @@ -68,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 + class NTCPSession: public i2p::transport::TransportSession { public: @@ -130,7 +129,6 @@ namespace ntcp boost::asio::ip::tcp::socket m_Socket; boost::asio::deadline_timer m_TerminationTimer; bool m_IsEstablished; - DHKeysPair * m_DHKeysPair; // X - for client and Y - for server i2p::crypto::CBCDecryption m_Decryption; i2p::crypto::CBCEncryption m_Encryption; diff --git a/SSU.cpp b/SSU.cpp index b26b8319..ce4c37bd 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -18,7 +18,7 @@ namespace ssu SSUSession::SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint, const i2p::data::RouterInfo * router, bool peerTest ): m_Server (server), m_RemoteEndpoint (remoteEndpoint), m_RemoteRouter (router), - m_Timer (m_Server.GetService ()), m_DHKeysPair (nullptr), m_PeerTest (peerTest), + m_Timer (m_Server.GetService ()), m_PeerTest (peerTest), m_State (eSessionStateUnknown), m_IsSessionKey (false), m_RelayTag (0), m_Data (*this), m_NumSentBytes (0), m_NumReceivedBytes (0) { @@ -28,8 +28,7 @@ namespace ssu } SSUSession::~SSUSession () - { - delete m_DHKeysPair; + { } void SSUSession::CreateAESandMacKey (const uint8_t * pubKey) diff --git a/SSU.h b/SSU.h index d14fc4c4..261494ab 100644 --- a/SSU.h +++ b/SSU.h @@ -13,12 +13,11 @@ #include "Identity.h" #include "RouterInfo.h" #include "I2NPProtocol.h" +#include "TransportSession.h" #include "SSUData.h" namespace i2p { - class DHKeysPair; - namespace ssu { #pragma pack(1) @@ -59,7 +58,7 @@ namespace ssu }; class SSUServer; - class SSUSession + class SSUSession: public i2p::transport::TransportSession { public: @@ -133,7 +132,6 @@ namespace ssu const i2p::data::RouterInfo * m_RemoteRouter; i2p::data::IdentHash m_RemoteIdent; // if m_RemoteRouter is null boost::asio::deadline_timer m_Timer; - DHKeysPair * m_DHKeysPair; // X - for client and Y - for server bool m_PeerTest; SessionState m_State; bool m_IsSessionKey; diff --git a/TransportSession.h b/TransportSession.h new file mode 100644 index 00000000..37b58fe8 --- /dev/null +++ b/TransportSession.h @@ -0,0 +1,30 @@ +#ifndef TRANSPORT_SESSION_H__ +#define TRANSPORT_SESSION_H__ + +#include + +namespace i2p +{ +namespace transport +{ + struct DHKeysPair // transient keys for transport sessions + { + uint8_t publicKey[256]; + uint8_t privateKey[256]; + }; + + class TransportSession + { + public: + + TransportSession (): m_DHKeysPair (nullptr) {}; + virtual ~TransportSession () { delete m_DHKeysPair; }; + + protected: + + DHKeysPair * m_DHKeysPair; // X - for client and Y - for server + }; +} +} + +#endif diff --git a/Transports.cpp b/Transports.cpp index d5e2aefd..590dfa77 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -58,7 +58,7 @@ namespace i2p CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); for (int i = 0; i < num; i++) { - DHKeysPair * pair = new DHKeysPair (); + i2p::transport::DHKeysPair * pair = new i2p::transport::DHKeysPair (); dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey); std::unique_lock l(m_AcquiredMutex); m_Queue.push (pair); @@ -66,7 +66,7 @@ namespace i2p } } - DHKeysPair * DHKeysPairSupplier::Acquire () + i2p::transport::DHKeysPair * DHKeysPairSupplier::Acquire () { if (!m_Queue.empty ()) { @@ -78,14 +78,14 @@ namespace i2p } else // queue is empty, create new { - DHKeysPair * pair = new DHKeysPair (); + i2p::transport::DHKeysPair * pair = new i2p::transport::DHKeysPair (); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey); return pair; } } - void DHKeysPairSupplier::Return (DHKeysPair * pair) + void DHKeysPairSupplier::Return (i2p::transport::DHKeysPair * pair) { std::unique_lock l(m_AcquiredMutex); m_Queue.push (pair); @@ -328,12 +328,12 @@ namespace i2p } } - DHKeysPair * Transports::GetNextDHKeysPair () + i2p::transport::DHKeysPair * Transports::GetNextDHKeysPair () { return m_DHKeysPairSupplier.Acquire (); } - void Transports::ReuseDHKeysPair (DHKeysPair * pair) + void Transports::ReuseDHKeysPair (i2p::transport::DHKeysPair * pair) { m_DHKeysPairSupplier.Return (pair); } diff --git a/Transports.h b/Transports.h index f740a8f7..aa9812e4 100644 --- a/Transports.h +++ b/Transports.h @@ -10,6 +10,7 @@ #include #include #include +#include "TransportSession.h" #include "NTCPSession.h" #include "SSU.h" #include "RouterInfo.h" @@ -18,12 +19,6 @@ namespace i2p { - struct DHKeysPair // transient keys for transport sessions - { - uint8_t publicKey[256]; - uint8_t privateKey[256]; - }; - class DHKeysPairSupplier { public: @@ -32,8 +27,8 @@ namespace i2p ~DHKeysPairSupplier (); void Start (); void Stop (); - DHKeysPair * Acquire (); - void Return (DHKeysPair * pair); + i2p::transport::DHKeysPair * Acquire (); + void Return (i2p::transport::DHKeysPair * pair); private: @@ -43,7 +38,7 @@ namespace i2p private: const int m_QueueSize; - std::queue m_Queue; + std::queue m_Queue; bool m_IsRunning; std::thread * m_Thread; @@ -63,8 +58,8 @@ namespace i2p void Stop (); boost::asio::io_service& GetService () { return m_Service; }; - DHKeysPair * GetNextDHKeysPair (); - void ReuseDHKeysPair (DHKeysPair * pair); + i2p::transport::DHKeysPair * GetNextDHKeysPair (); + void ReuseDHKeysPair (i2p::transport::DHKeysPair * pair); void AddNTCPSession (i2p::ntcp::NTCPSession * session); void RemoveNTCPSession (i2p::ntcp::NTCPSession * session);