mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
6.1 KiB
182 lines
6.1 KiB
11 years ago
|
#ifndef NTCP_SESSION_H__
|
||
|
#define NTCP_SESSION_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
10 years ago
|
#include <map>
|
||
10 years ago
|
#include <memory>
|
||
10 years ago
|
#include <thread>
|
||
10 years ago
|
#include <mutex>
|
||
10 years ago
|
#include <boost/asio.hpp>
|
||
9 years ago
|
#include "Crypto.h"
|
||
11 years ago
|
#include "Identity.h"
|
||
11 years ago
|
#include "RouterInfo.h"
|
||
11 years ago
|
#include "I2NPProtocol.h"
|
||
10 years ago
|
#include "TransportSession.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace transport
|
||
11 years ago
|
{
|
||
|
struct NTCPPhase1
|
||
|
{
|
||
|
uint8_t pubKey[256];
|
||
|
uint8_t HXxorHI[32];
|
||
|
};
|
||
|
|
||
|
struct NTCPPhase2
|
||
|
{
|
||
|
uint8_t pubKey[256];
|
||
|
struct
|
||
|
{
|
||
|
uint8_t hxy[32];
|
||
9 years ago
|
uint8_t timestamp[4];
|
||
11 years ago
|
uint8_t filler[12];
|
||
|
} encrypted;
|
||
|
};
|
||
|
|
||
11 years ago
|
const size_t NTCP_MAX_MESSAGE_SIZE = 16384;
|
||
8 years ago
|
const size_t NTCP_BUFFER_SIZE = 1028; // fits 1 tunnel data message
|
||
8 years ago
|
const int NTCP_CONNECT_TIMEOUT = 5; // 5 seconds
|
||
8 years ago
|
const int NTCP_ESTABLISH_TIMEOUT = 10; // 10 seconds
|
||
11 years ago
|
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes
|
||
8 years ago
|
const int NTCP_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
|
||
10 years ago
|
const size_t NTCP_DEFAULT_PHASE3_SIZE = 2/*size*/ + i2p::data::DEFAULT_IDENTITY_SIZE/*387*/ + 4/*ts*/ + 15/*padding*/ + 40/*signature*/; // 448
|
||
9 years ago
|
const int NTCP_CLOCK_SKEW = 60; // in seconds
|
||
9 years ago
|
const int NTCP_MAX_OUTGOING_QUEUE_SIZE = 200; // how many messages we can queue up
|
||
10 years ago
|
|
||
10 years ago
|
class NTCPServer;
|
||
10 years ago
|
class NTCPSession: public TransportSession, public std::enable_shared_from_this<NTCPSession>
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
|
||
10 years ago
|
~NTCPSession ();
|
||
10 years ago
|
void Terminate ();
|
||
10 years ago
|
void Done ();
|
||
11 years ago
|
|
||
11 years ago
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
||
8 years ago
|
bool IsEstablished () const { return m_IsEstablished; };
|
||
8 years ago
|
bool IsTerminated () const { return m_IsTerminated; };
|
||
|
|
||
11 years ago
|
void ClientLogin ();
|
||
|
void ServerLogin ();
|
||
10 years ago
|
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
||
11 years ago
|
|
||
10 years ago
|
private:
|
||
11 years ago
|
|
||
10 years ago
|
void PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
||
10 years ago
|
void Connected ();
|
||
11 years ago
|
void SendTimeSyncMessage ();
|
||
|
void SetIsEstablished (bool isEstablished) { m_IsEstablished = isEstablished; }
|
||
11 years ago
|
|
||
8 years ago
|
void CreateAESKey (uint8_t * pubKey);
|
||
11 years ago
|
|
||
|
// 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);
|
||
8 years ago
|
void HandlePhase2 ();
|
||
11 years ago
|
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 ();
|
||
10 years ago
|
void SendPhase4 (uint32_t tsA, uint32_t tsB);
|
||
11 years ago
|
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);
|
||
10 years ago
|
void HandlePhase3ExtraReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB, size_t paddingLen);
|
||
|
void HandlePhase3 (uint32_t tsB, size_t paddingLen);
|
||
11 years ago
|
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);
|
||
10 years ago
|
bool DecryptNextBlock (const uint8_t * encrypted);
|
||
11 years ago
|
|
||
8 years ago
|
void Send (std::shared_ptr<i2p::I2NPMessage> msg);
|
||
|
boost::asio::const_buffers_1 CreateMsgBuffer (std::shared_ptr<I2NPMessage> msg);
|
||
10 years ago
|
void Send (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
||
8 years ago
|
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
||
10 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
10 years ago
|
NTCPServer& m_Server;
|
||
11 years ago
|
boost::asio::ip::tcp::socket m_Socket;
|
||
10 years ago
|
bool m_IsEstablished, m_IsTerminated;
|
||
11 years ago
|
|
||
11 years ago
|
i2p::crypto::CBCDecryption m_Decryption;
|
||
|
i2p::crypto::CBCEncryption m_Encryption;
|
||
10 years ago
|
|
||
|
struct Establisher
|
||
|
{
|
||
|
NTCPPhase1 phase1;
|
||
|
NTCPPhase2 phase2;
|
||
|
} * m_Establisher;
|
||
11 years ago
|
|
||
10 years ago
|
i2p::crypto::AESAlignedBuffer<NTCP_BUFFER_SIZE + 16> m_ReceiveBuffer;
|
||
|
i2p::crypto::AESAlignedBuffer<16> m_TimeSyncBuffer;
|
||
11 years ago
|
int m_ReceiveBufferOffset;
|
||
|
|
||
10 years ago
|
std::shared_ptr<I2NPMessage> m_NextMessage;
|
||
11 years ago
|
size_t m_NextMessageOffset;
|
||
10 years ago
|
i2p::I2NPMessagesHandler m_Handler;
|
||
10 years ago
|
|
||
|
bool m_IsSending;
|
||
10 years ago
|
std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue;
|
||
11 years ago
|
};
|
||
10 years ago
|
|
||
|
// TODO: move to NTCP.h/.cpp
|
||
|
class NTCPServer
|
||
|
{
|
||
|
public:
|
||
|
|
||
9 years ago
|
NTCPServer ();
|
||
10 years ago
|
~NTCPServer ();
|
||
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
9 years ago
|
bool AddNTCPSession (std::shared_ptr<NTCPSession> session);
|
||
10 years ago
|
void RemoveNTCPSession (std::shared_ptr<NTCPSession> session);
|
||
|
std::shared_ptr<NTCPSession> FindNTCPSession (const i2p::data::IdentHash& ident);
|
||
|
void Connect (const boost::asio::ip::address& address, int port, std::shared_ptr<NTCPSession> conn);
|
||
9 years ago
|
|
||
8 years ago
|
bool IsBoundV4() const { return m_NTCPAcceptor != nullptr; };
|
||
|
bool IsBoundV6() const { return m_NTCPV6Acceptor != nullptr; };
|
||
9 years ago
|
|
||
8 years ago
|
boost::asio::io_service& GetService () { return m_Service; };
|
||
10 years ago
|
|
||
10 years ago
|
private:
|
||
|
|
||
|
void Run ();
|
||
|
void HandleAccept (std::shared_ptr<NTCPSession> conn, const boost::system::error_code& error);
|
||
|
void HandleAcceptV6 (std::shared_ptr<NTCPSession> conn, const boost::system::error_code& error);
|
||
|
|
||
8 years ago
|
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCPSession> conn, std::shared_ptr<boost::asio::deadline_timer> timer);
|
||
8 years ago
|
|
||
|
// timer
|
||
|
void ScheduleTermination ();
|
||
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
|
bool m_IsRunning;
|
||
|
std::thread * m_Thread;
|
||
|
boost::asio::io_service m_Service;
|
||
|
boost::asio::io_service::work m_Work;
|
||
8 years ago
|
boost::asio::deadline_timer m_TerminationTimer;
|
||
10 years ago
|
boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor;
|
||
9 years ago
|
std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions; // access from m_Thread only
|
||
8 years ago
|
std::list<std::shared_ptr<NTCPSession> > m_PendingIncomingSessions;
|
||
10 years ago
|
|
||
|
public:
|
||
|
|
||
|
// for HTTP/I2PControl
|
||
|
const decltype(m_NTCPSessions)& GetNTCPSessions () const { return m_NTCPSessions; };
|
||
|
};
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|