mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-21 16:49:10 +01:00
TransportSession added
This commit is contained in:
parent
a8871d9f98
commit
c4dda06cde
7 changed files with 50 additions and 32 deletions
|
@ -21,9 +21,8 @@ namespace ntcp
|
||||||
{
|
{
|
||||||
NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo):
|
NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo):
|
||||||
m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false),
|
m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false),
|
||||||
m_DHKeysPair (nullptr), m_RemoteRouterInfo (in_RemoteRouterInfo),
|
m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0),
|
||||||
m_ReceiveBufferOffset (0), m_NextMessage (nullptr),
|
m_NextMessage (nullptr), m_NumSentBytes (0), m_NumReceivedBytes (0)
|
||||||
m_NumSentBytes (0), m_NumReceivedBytes (0)
|
|
||||||
{
|
{
|
||||||
m_DHKeysPair = i2p::transports.GetNextDHKeysPair ();
|
m_DHKeysPair = i2p::transports.GetNextDHKeysPair ();
|
||||||
m_Establisher = new Establisher;
|
m_Establisher = new Establisher;
|
||||||
|
@ -32,7 +31,6 @@ namespace ntcp
|
||||||
NTCPSession::~NTCPSession ()
|
NTCPSession::~NTCPSession ()
|
||||||
{
|
{
|
||||||
delete m_Establisher;
|
delete m_Establisher;
|
||||||
delete m_DHKeysPair;
|
|
||||||
if (m_NextMessage)
|
if (m_NextMessage)
|
||||||
i2p::DeleteI2NPMessage (m_NextMessage);
|
i2p::DeleteI2NPMessage (m_NextMessage);
|
||||||
for (auto it :m_DelayedMessages)
|
for (auto it :m_DelayedMessages)
|
||||||
|
|
|
@ -11,11 +11,10 @@
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
#include "RouterInfo.h"
|
#include "RouterInfo.h"
|
||||||
#include "I2NPProtocol.h"
|
#include "I2NPProtocol.h"
|
||||||
|
#include "TransportSession.h"
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
class DHKeysPair;
|
|
||||||
|
|
||||||
namespace ntcp
|
namespace ntcp
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -68,7 +67,7 @@ namespace ntcp
|
||||||
const size_t NTCP_BUFFER_SIZE = 1040; // fits one tunnel message (1028)
|
const size_t NTCP_BUFFER_SIZE = 1040; // fits one tunnel message (1028)
|
||||||
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes
|
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes
|
||||||
|
|
||||||
class NTCPSession
|
class NTCPSession: public i2p::transport::TransportSession
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -130,7 +129,6 @@ namespace ntcp
|
||||||
boost::asio::ip::tcp::socket m_Socket;
|
boost::asio::ip::tcp::socket m_Socket;
|
||||||
boost::asio::deadline_timer m_TerminationTimer;
|
boost::asio::deadline_timer m_TerminationTimer;
|
||||||
bool m_IsEstablished;
|
bool m_IsEstablished;
|
||||||
DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
|
|
||||||
|
|
||||||
i2p::crypto::CBCDecryption m_Decryption;
|
i2p::crypto::CBCDecryption m_Decryption;
|
||||||
i2p::crypto::CBCEncryption m_Encryption;
|
i2p::crypto::CBCEncryption m_Encryption;
|
||||||
|
|
3
SSU.cpp
3
SSU.cpp
|
@ -18,7 +18,7 @@ namespace ssu
|
||||||
SSUSession::SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint,
|
SSUSession::SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint,
|
||||||
const i2p::data::RouterInfo * router, bool peerTest ):
|
const i2p::data::RouterInfo * router, bool peerTest ):
|
||||||
m_Server (server), m_RemoteEndpoint (remoteEndpoint), m_RemoteRouter (router),
|
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_State (eSessionStateUnknown), m_IsSessionKey (false), m_RelayTag (0),
|
||||||
m_Data (*this), m_NumSentBytes (0), m_NumReceivedBytes (0)
|
m_Data (*this), m_NumSentBytes (0), m_NumReceivedBytes (0)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,6 @@ namespace ssu
|
||||||
|
|
||||||
SSUSession::~SSUSession ()
|
SSUSession::~SSUSession ()
|
||||||
{
|
{
|
||||||
delete m_DHKeysPair;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSUSession::CreateAESandMacKey (const uint8_t * pubKey)
|
void SSUSession::CreateAESandMacKey (const uint8_t * pubKey)
|
||||||
|
|
6
SSU.h
6
SSU.h
|
@ -13,12 +13,11 @@
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
#include "RouterInfo.h"
|
#include "RouterInfo.h"
|
||||||
#include "I2NPProtocol.h"
|
#include "I2NPProtocol.h"
|
||||||
|
#include "TransportSession.h"
|
||||||
#include "SSUData.h"
|
#include "SSUData.h"
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
class DHKeysPair;
|
|
||||||
|
|
||||||
namespace ssu
|
namespace ssu
|
||||||
{
|
{
|
||||||
#pragma pack(1)
|
#pragma pack(1)
|
||||||
|
@ -59,7 +58,7 @@ namespace ssu
|
||||||
};
|
};
|
||||||
|
|
||||||
class SSUServer;
|
class SSUServer;
|
||||||
class SSUSession
|
class SSUSession: public i2p::transport::TransportSession
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -133,7 +132,6 @@ namespace ssu
|
||||||
const i2p::data::RouterInfo * m_RemoteRouter;
|
const i2p::data::RouterInfo * m_RemoteRouter;
|
||||||
i2p::data::IdentHash m_RemoteIdent; // if m_RemoteRouter is null
|
i2p::data::IdentHash m_RemoteIdent; // if m_RemoteRouter is null
|
||||||
boost::asio::deadline_timer m_Timer;
|
boost::asio::deadline_timer m_Timer;
|
||||||
DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
|
|
||||||
bool m_PeerTest;
|
bool m_PeerTest;
|
||||||
SessionState m_State;
|
SessionState m_State;
|
||||||
bool m_IsSessionKey;
|
bool m_IsSessionKey;
|
||||||
|
|
30
TransportSession.h
Normal file
30
TransportSession.h
Normal file
|
@ -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
|
|
@ -58,7 +58,7 @@ namespace i2p
|
||||||
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
||||||
for (int i = 0; i < num; i++)
|
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);
|
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey);
|
||||||
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
||||||
m_Queue.push (pair);
|
m_Queue.push (pair);
|
||||||
|
@ -66,7 +66,7 @@ namespace i2p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DHKeysPair * DHKeysPairSupplier::Acquire ()
|
i2p::transport::DHKeysPair * DHKeysPairSupplier::Acquire ()
|
||||||
{
|
{
|
||||||
if (!m_Queue.empty ())
|
if (!m_Queue.empty ())
|
||||||
{
|
{
|
||||||
|
@ -78,14 +78,14 @@ namespace i2p
|
||||||
}
|
}
|
||||||
else // queue is empty, create new
|
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);
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
||||||
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey);
|
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey);
|
||||||
return pair;
|
return pair;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHKeysPairSupplier::Return (DHKeysPair * pair)
|
void DHKeysPairSupplier::Return (i2p::transport::DHKeysPair * pair)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
||||||
m_Queue.push (pair);
|
m_Queue.push (pair);
|
||||||
|
@ -328,12 +328,12 @@ namespace i2p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DHKeysPair * Transports::GetNextDHKeysPair ()
|
i2p::transport::DHKeysPair * Transports::GetNextDHKeysPair ()
|
||||||
{
|
{
|
||||||
return m_DHKeysPairSupplier.Acquire ();
|
return m_DHKeysPairSupplier.Acquire ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transports::ReuseDHKeysPair (DHKeysPair * pair)
|
void Transports::ReuseDHKeysPair (i2p::transport::DHKeysPair * pair)
|
||||||
{
|
{
|
||||||
m_DHKeysPairSupplier.Return (pair);
|
m_DHKeysPairSupplier.Return (pair);
|
||||||
}
|
}
|
||||||
|
|
17
Transports.h
17
Transports.h
|
@ -10,6 +10,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cryptopp/osrng.h>
|
#include <cryptopp/osrng.h>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
#include "TransportSession.h"
|
||||||
#include "NTCPSession.h"
|
#include "NTCPSession.h"
|
||||||
#include "SSU.h"
|
#include "SSU.h"
|
||||||
#include "RouterInfo.h"
|
#include "RouterInfo.h"
|
||||||
|
@ -18,12 +19,6 @@
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
struct DHKeysPair // transient keys for transport sessions
|
|
||||||
{
|
|
||||||
uint8_t publicKey[256];
|
|
||||||
uint8_t privateKey[256];
|
|
||||||
};
|
|
||||||
|
|
||||||
class DHKeysPairSupplier
|
class DHKeysPairSupplier
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -32,8 +27,8 @@ namespace i2p
|
||||||
~DHKeysPairSupplier ();
|
~DHKeysPairSupplier ();
|
||||||
void Start ();
|
void Start ();
|
||||||
void Stop ();
|
void Stop ();
|
||||||
DHKeysPair * Acquire ();
|
i2p::transport::DHKeysPair * Acquire ();
|
||||||
void Return (DHKeysPair * pair);
|
void Return (i2p::transport::DHKeysPair * pair);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -43,7 +38,7 @@ namespace i2p
|
||||||
private:
|
private:
|
||||||
|
|
||||||
const int m_QueueSize;
|
const int m_QueueSize;
|
||||||
std::queue<DHKeysPair *> m_Queue;
|
std::queue<i2p::transport::DHKeysPair *> m_Queue;
|
||||||
|
|
||||||
bool m_IsRunning;
|
bool m_IsRunning;
|
||||||
std::thread * m_Thread;
|
std::thread * m_Thread;
|
||||||
|
@ -63,8 +58,8 @@ namespace i2p
|
||||||
void Stop ();
|
void Stop ();
|
||||||
|
|
||||||
boost::asio::io_service& GetService () { return m_Service; };
|
boost::asio::io_service& GetService () { return m_Service; };
|
||||||
DHKeysPair * GetNextDHKeysPair ();
|
i2p::transport::DHKeysPair * GetNextDHKeysPair ();
|
||||||
void ReuseDHKeysPair (DHKeysPair * pair);
|
void ReuseDHKeysPair (i2p::transport::DHKeysPair * pair);
|
||||||
|
|
||||||
void AddNTCPSession (i2p::ntcp::NTCPSession * session);
|
void AddNTCPSession (i2p::ntcp::NTCPSession * session);
|
||||||
void RemoveNTCPSession (i2p::ntcp::NTCPSession * session);
|
void RemoveNTCPSession (i2p::ntcp::NTCPSession * session);
|
||||||
|
|
Loading…
Add table
Reference in a new issue