Browse Source

TransportSession added

pull/105/head
orignal 10 years ago
parent
commit
c4dda06cde
  1. 6
      NTCPSession.cpp
  2. 6
      NTCPSession.h
  3. 3
      SSU.cpp
  4. 6
      SSU.h
  5. 30
      TransportSession.h
  6. 12
      Transports.cpp
  7. 17
      Transports.h

6
NTCPSession.cpp

@ -21,9 +21,8 @@ namespace ntcp @@ -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 @@ -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)

6
NTCPSession.h

@ -11,11 +11,10 @@ @@ -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 @@ -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 @@ -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;

3
SSU.cpp

@ -18,7 +18,7 @@ namespace ssu @@ -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)
{
@ -29,7 +29,6 @@ namespace ssu @@ -29,7 +29,6 @@ namespace ssu
SSUSession::~SSUSession ()
{
delete m_DHKeysPair;
}
void SSUSession::CreateAESandMacKey (const uint8_t * pubKey)

6
SSU.h

@ -13,12 +13,11 @@ @@ -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 @@ -59,7 +58,7 @@ namespace ssu
};
class SSUServer;
class SSUSession
class SSUSession: public i2p::transport::TransportSession
{
public:
@ -133,7 +132,6 @@ namespace ssu @@ -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;

30
TransportSession.h

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
#ifndef TRANSPORT_SESSION_H__
#define TRANSPORT_SESSION_H__
#include <inttypes.h>
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

12
Transports.cpp

@ -58,7 +58,7 @@ namespace i2p @@ -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<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair);
@ -66,7 +66,7 @@ namespace i2p @@ -66,7 +66,7 @@ namespace i2p
}
}
DHKeysPair * DHKeysPairSupplier::Acquire ()
i2p::transport::DHKeysPair * DHKeysPairSupplier::Acquire ()
{
if (!m_Queue.empty ())
{
@ -78,14 +78,14 @@ namespace i2p @@ -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<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair);
@ -328,12 +328,12 @@ namespace i2p @@ -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);
}

17
Transports.h

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
#include <string>
#include <cryptopp/osrng.h>
#include <boost/asio.hpp>
#include "TransportSession.h"
#include "NTCPSession.h"
#include "SSU.h"
#include "RouterInfo.h"
@ -18,12 +19,6 @@ @@ -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 @@ -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 @@ -43,7 +38,7 @@ namespace i2p
private:
const int m_QueueSize;
std::queue<DHKeysPair *> m_Queue;
std::queue<i2p::transport::DHKeysPair *> m_Queue;
bool m_IsRunning;
std::thread * m_Thread;
@ -63,8 +58,8 @@ namespace i2p @@ -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);

Loading…
Cancel
Save