i2pd/NTCPSession.h

191 lines
4.8 KiB
C
Raw Normal View History

2013-09-10 05:35:46 +04:00
#ifndef NTCP_SESSION_H__
#define NTCP_SESSION_H__
#include <inttypes.h>
2013-11-29 16:52:09 +04:00
#include <list>
2013-09-10 05:35:46 +04:00
#include <boost/asio.hpp>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/adler32.h>
2014-05-07 06:30:09 +04:00
#include "aes.h"
2014-04-04 21:30:13 +04:00
#include "Identity.h"
2013-09-10 05:35:46 +04:00
#include "RouterInfo.h"
2013-10-23 06:43:29 +04:00
#include "I2NPProtocol.h"
2013-09-10 05:35:46 +04:00
namespace i2p
{
namespace ntcp
{
#pragma pack(1)
struct NTCPPhase1
{
uint8_t pubKey[256];
uint8_t HXxorHI[32];
};
struct NTCPPhase2
{
uint8_t pubKey[256];
struct
{
uint8_t hxy[32];
uint32_t timestamp;
uint8_t filler[12];
} encrypted;
};
struct NTCPPhase3
{
uint16_t size;
i2p::data::Identity ident;
2013-09-10 05:35:46 +04:00
uint32_t timestamp;
uint8_t padding[15];
uint8_t signature[40];
};
struct NTCPPhase4
{
uint8_t signature[40];
uint8_t padding[8];
};
struct SignedData // used for signature in Phase3 and Phase4
{
uint8_t x[256];
uint8_t y[256];
uint8_t ident[32];
uint32_t tsA;
uint32_t tsB;
};
#pragma pack()
const size_t NTCP_MAX_MESSAGE_SIZE = 16384;
2014-09-12 06:15:20 +04:00
const size_t NTCP_BUFFER_SIZE = 1040; // fits one tunnel message (1028)
2014-04-08 05:40:28 +04:00
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes
2013-09-10 05:35:46 +04:00
class NTCPSession
{
public:
2013-12-22 20:29:57 +04:00
NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo);
2014-04-04 21:30:13 +04:00
virtual ~NTCPSession ();
2013-09-10 05:35:46 +04:00
2013-11-29 16:52:09 +04:00
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
2013-09-10 05:35:46 +04:00
bool IsEstablished () const { return m_IsEstablished; };
2013-12-12 07:06:04 +04:00
i2p::data::RouterInfo& GetRemoteRouterInfo () { return m_RemoteRouterInfo; };
2013-09-10 05:35:46 +04:00
void ClientLogin ();
void ServerLogin ();
2013-10-23 06:43:29 +04:00
void SendI2NPMessage (I2NPMessage * msg);
size_t GetNumSentBytes () const { return m_NumSentBytes; };
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
2013-10-23 06:43:29 +04:00
protected:
2013-09-10 05:35:46 +04:00
void Terminate ();
2013-11-29 16:52:09 +04:00
virtual void Connected ();
void SendTimeSyncMessage ();
void SetIsEstablished (bool isEstablished) { m_IsEstablished = isEstablished; }
2013-09-10 05:35:46 +04:00
private:
void CreateAESKey (uint8_t * pubKey, uint8_t * aesKey);
// client
void SendPhase3 ();
void HandlePhase1Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandlePhase2Received (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA);
void HandlePhase4Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA);
//server
void SendPhase2 ();
void SendPhase4 (uint32_t tsB);
void HandlePhase1Received (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandlePhase2Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB);
void HandlePhase3Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB);
void HandlePhase4Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
// common
void Receive ();
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2014-09-18 19:11:51 +04:00
bool DecryptNextBlock (const uint8_t * encrypted);
2013-10-27 19:23:15 +04:00
void Send (i2p::I2NPMessage * msg);
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg);
2013-09-10 05:35:46 +04:00
2013-11-29 16:52:09 +04:00
// timer
void ScheduleTermination ();
void HandleTerminationTimer (const boost::system::error_code& ecode);
2013-09-10 05:35:46 +04:00
private:
2013-11-29 16:52:09 +04:00
boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_TerminationTimer;
2013-09-10 05:35:46 +04:00
bool m_IsEstablished;
2014-04-04 21:30:13 +04:00
i2p::data::DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
2013-09-10 05:35:46 +04:00
2014-05-07 06:30:09 +04:00
i2p::crypto::CBCDecryption m_Decryption;
i2p::crypto::CBCEncryption m_Encryption;
2013-09-10 05:35:46 +04:00
CryptoPP::Adler32 m_Adler;
2013-12-22 20:29:57 +04:00
i2p::data::RouterInfo& m_RemoteRouterInfo;
2014-09-12 06:15:20 +04:00
struct Establisher
{
NTCPPhase1 phase1;
NTCPPhase2 phase2;
NTCPPhase3 phase3;
NTCPPhase4 phase4;
} * m_Establisher;
2013-09-10 05:35:46 +04:00
2014-09-18 19:11:51 +04:00
uint8_t m_ReceiveBuffer[NTCP_BUFFER_SIZE + 16], m_TimeSyncBuffer[16];
2013-09-10 05:35:46 +04:00
int m_ReceiveBufferOffset;
2013-11-29 16:52:09 +04:00
i2p::I2NPMessage * m_NextMessage;
std::list<i2p::I2NPMessage *> m_DelayedMessages;
2013-10-27 19:23:15 +04:00
size_t m_NextMessageOffset;
size_t m_NumSentBytes, m_NumReceivedBytes;
2013-09-10 05:35:46 +04:00
};
class NTCPClient: public NTCPSession
{
public:
2014-01-22 03:01:11 +04:00
NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo);
2013-09-10 05:35:46 +04:00
private:
void Connect ();
void HandleConnect (const boost::system::error_code& ecode);
private:
boost::asio::ip::tcp::endpoint m_Endpoint;
};
class NTCPServerConnection: public NTCPSession
{
public:
2013-12-22 20:29:57 +04:00
NTCPServerConnection (boost::asio::io_service& service):
NTCPSession (service, m_DummyRemoteRouterInfo) {};
2013-09-10 05:35:46 +04:00
2013-11-29 16:52:09 +04:00
protected:
2013-09-10 05:35:46 +04:00
2013-11-29 16:52:09 +04:00
virtual void Connected ();
2013-12-22 20:29:57 +04:00
private:
i2p::data::RouterInfo m_DummyRemoteRouterInfo;
2013-09-10 05:35:46 +04:00
};
}
}
#endif