Skip to content

Commit

Permalink
TransportSession added
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Oct 20, 2014
1 parent a8871d9 commit c4dda06
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 32 deletions.
6 changes: 2 additions & 4 deletions NTCPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
#include "Identity.h"
#include "RouterInfo.h"
#include "I2NPProtocol.h"
#include "TransportSession.h"

namespace i2p
{
class DHKeysPair;

namespace ntcp
{

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions SSU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -28,8 +28,7 @@ namespace ssu
}

SSUSession::~SSUSession ()
{
delete m_DHKeysPair;
{
}

void SSUSession::CreateAESandMacKey (const uint8_t * pubKey)
Expand Down
6 changes: 2 additions & 4 deletions SSU.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -59,7 +58,7 @@ namespace ssu
};

class SSUServer;
class SSUSession
class SSUSession: public i2p::transport::TransportSession
{
public:

Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions TransportSession.h
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 6 deletions Transports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ 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);
}
}
}

DHKeysPair * DHKeysPairSupplier::Acquire ()
i2p::transport::DHKeysPair * DHKeysPairSupplier::Acquire ()
{
if (!m_Queue.empty ())
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
17 changes: 6 additions & 11 deletions Transports.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,12 +19,6 @@

namespace i2p
{
struct DHKeysPair // transient keys for transport sessions
{
uint8_t publicKey[256];
uint8_t privateKey[256];
};

class DHKeysPairSupplier
{
public:
Expand All @@ -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:

Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit c4dda06

Please sign in to comment.