2013-09-10 01:35:46 +00:00
|
|
|
#ifndef NTCP_SESSION_H__
|
|
|
|
#define NTCP_SESSION_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2015-01-11 22:41:56 +00:00
|
|
|
#include <map>
|
2014-11-25 21:30:15 +00:00
|
|
|
#include <memory>
|
2015-01-11 22:41:56 +00:00
|
|
|
#include <thread>
|
2015-01-12 17:15:54 +00:00
|
|
|
#include <mutex>
|
2015-01-11 22:41:56 +00:00
|
|
|
#include <boost/asio.hpp>
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Crypto.h"
|
2014-04-04 17:30:13 +00:00
|
|
|
#include "Identity.h"
|
2013-09-10 01:35:46 +00:00
|
|
|
#include "RouterInfo.h"
|
2013-10-23 02:43:29 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2014-10-20 20:09:59 +00:00
|
|
|
#include "TransportSession.h"
|
2013-09-10 01:35:46 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-21 16:25:53 +00:00
|
|
|
namespace transport
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
|
|
|
struct NTCPPhase1
|
|
|
|
{
|
|
|
|
uint8_t pubKey[256];
|
|
|
|
uint8_t HXxorHI[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NTCPPhase2
|
|
|
|
{
|
|
|
|
uint8_t pubKey[256];
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint8_t hxy[32];
|
2016-01-12 00:03:04 +00:00
|
|
|
uint8_t timestamp[4];
|
2013-09-10 01:35:46 +00:00
|
|
|
uint8_t filler[12];
|
|
|
|
} encrypted;
|
|
|
|
};
|
|
|
|
|
2014-07-04 16:35:02 +00:00
|
|
|
const size_t NTCP_MAX_MESSAGE_SIZE = 16384;
|
2015-01-23 03:00:41 +00:00
|
|
|
const size_t NTCP_BUFFER_SIZE = 4160; // fits 4 tunnel messages (4*1028)
|
2016-11-29 19:12:44 +00:00
|
|
|
const int NTCP_CONNECT_TIMEOUT = 5; // 5 seconds
|
2014-04-08 01:40:28 +00:00
|
|
|
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes
|
2016-08-17 14:58:57 +00:00
|
|
|
const int NTCP_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
|
2014-11-25 19:29:06 +00:00
|
|
|
const size_t NTCP_DEFAULT_PHASE3_SIZE = 2/*size*/ + i2p::data::DEFAULT_IDENTITY_SIZE/*387*/ + 4/*ts*/ + 15/*padding*/ + 40/*signature*/; // 448
|
2015-02-10 18:05:08 +00:00
|
|
|
const int NTCP_BAN_EXPIRATION_TIMEOUT = 70; // in second
|
2016-01-27 00:02:06 +00:00
|
|
|
const int NTCP_CLOCK_SKEW = 60; // in seconds
|
2016-07-12 20:26:36 +00:00
|
|
|
const int NTCP_MAX_OUTGOING_QUEUE_SIZE = 200; // how many messages we can queue up
|
2014-10-20 19:19:56 +00:00
|
|
|
|
2015-01-11 22:41:56 +00:00
|
|
|
class NTCPServer;
|
2014-11-25 21:30:15 +00:00
|
|
|
class NTCPSession: public TransportSession, public std::enable_shared_from_this<NTCPSession>
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-01-11 22:41:56 +00:00
|
|
|
NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
|
2014-10-24 19:50:48 +00:00
|
|
|
~NTCPSession ();
|
2014-11-25 20:16:03 +00:00
|
|
|
void Terminate ();
|
2015-02-07 01:53:48 +00:00
|
|
|
void Done ();
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
2016-08-24 15:21:49 +00:00
|
|
|
bool IsEstablished () const { return m_IsEstablished; };
|
2016-08-17 14:58:57 +00:00
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
void ClientLogin ();
|
|
|
|
void ServerLogin ();
|
2015-06-17 14:47:26 +00:00
|
|
|
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
2013-10-23 02:43:29 +00:00
|
|
|
|
2015-01-21 17:08:15 +00:00
|
|
|
private:
|
2013-10-23 02:43:29 +00:00
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
void PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
2014-11-25 20:16:03 +00:00
|
|
|
void Connected ();
|
2013-11-29 12:52:09 +00:00
|
|
|
void SendTimeSyncMessage ();
|
|
|
|
void SetIsEstablished (bool isEstablished) { m_IsEstablished = isEstablished; }
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2014-11-02 01:53:45 +00:00
|
|
|
void CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key);
|
2013-09-10 01:35:46 +00:00
|
|
|
|
|
|
|
// 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 ();
|
2014-11-25 15:35:35 +00:00
|
|
|
void SendPhase4 (uint32_t tsA, uint32_t tsB);
|
2013-09-10 01:35:46 +00:00
|
|
|
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);
|
2014-11-25 17:33:51 +00:00
|
|
|
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);
|
2013-09-10 01:35:46 +00:00
|
|
|
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 15:11:51 +00:00
|
|
|
bool DecryptNextBlock (const uint8_t * encrypted);
|
2013-10-27 15:23:15 +00:00
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
void Send (std::shared_ptr<i2p::I2NPMessage> msg);
|
|
|
|
boost::asio::const_buffers_1 CreateMsgBuffer (std::shared_ptr<I2NPMessage> msg);
|
|
|
|
void Send (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
|
|
|
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
2015-01-21 03:35:27 +00:00
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
private:
|
|
|
|
|
2015-01-11 22:41:56 +00:00
|
|
|
NTCPServer& m_Server;
|
2013-11-29 12:52:09 +00:00
|
|
|
boost::asio::ip::tcp::socket m_Socket;
|
2015-02-07 01:53:48 +00:00
|
|
|
bool m_IsEstablished, m_IsTerminated;
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2014-05-07 02:30:09 +00:00
|
|
|
i2p::crypto::CBCDecryption m_Decryption;
|
|
|
|
i2p::crypto::CBCEncryption m_Encryption;
|
2014-09-12 02:15:20 +00:00
|
|
|
|
|
|
|
struct Establisher
|
|
|
|
{
|
|
|
|
NTCPPhase1 phase1;
|
|
|
|
NTCPPhase2 phase2;
|
|
|
|
} * m_Establisher;
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2014-11-25 15:14:18 +00:00
|
|
|
i2p::crypto::AESAlignedBuffer<NTCP_BUFFER_SIZE + 16> m_ReceiveBuffer;
|
|
|
|
i2p::crypto::AESAlignedBuffer<16> m_TimeSyncBuffer;
|
2013-09-10 01:35:46 +00:00
|
|
|
int m_ReceiveBufferOffset;
|
|
|
|
|
2015-06-21 19:08:22 +00:00
|
|
|
std::shared_ptr<I2NPMessage> m_NextMessage;
|
2013-10-27 15:23:15 +00:00
|
|
|
size_t m_NextMessageOffset;
|
2015-01-23 03:00:41 +00:00
|
|
|
i2p::I2NPMessagesHandler m_Handler;
|
2015-01-28 00:12:27 +00:00
|
|
|
|
|
|
|
bool m_IsSending;
|
2015-06-17 14:47:26 +00:00
|
|
|
std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue;
|
2013-09-10 01:35:46 +00:00
|
|
|
};
|
2015-01-11 22:41:56 +00:00
|
|
|
|
|
|
|
// TODO: move to NTCP.h/.cpp
|
|
|
|
class NTCPServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-01-28 02:54:42 +00:00
|
|
|
NTCPServer ();
|
2015-01-11 22:41:56 +00:00
|
|
|
~NTCPServer ();
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
2015-11-25 16:51:35 +00:00
|
|
|
bool AddNTCPSession (std::shared_ptr<NTCPSession> session);
|
2015-01-11 22:41:56 +00:00
|
|
|
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);
|
2016-06-13 15:34:44 +00:00
|
|
|
|
2016-08-17 14:58:57 +00:00
|
|
|
bool IsBoundV4() const { return m_NTCPAcceptor != nullptr; };
|
|
|
|
bool IsBoundV6() const { return m_NTCPV6Acceptor != nullptr; };
|
2016-06-13 15:34:44 +00:00
|
|
|
|
2016-12-31 15:51:42 +00:00
|
|
|
boost::asio::io_service& GetService () { return m_Service; };
|
2015-02-10 18:05:08 +00:00
|
|
|
|
2015-01-11 22:41:56 +00:00
|
|
|
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);
|
|
|
|
|
2016-11-29 19:12:44 +00:00
|
|
|
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCPSession> conn, std::shared_ptr<boost::asio::deadline_timer> timer);
|
2016-08-17 14:58:57 +00:00
|
|
|
|
|
|
|
// timer
|
|
|
|
void ScheduleTermination ();
|
|
|
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
2015-01-11 22:41:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
|
|
|
boost::asio::io_service m_Service;
|
|
|
|
boost::asio::io_service::work m_Work;
|
2016-08-17 14:58:57 +00:00
|
|
|
boost::asio::deadline_timer m_TerminationTimer;
|
2015-01-11 22:41:56 +00:00
|
|
|
boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor;
|
2015-11-25 16:51:35 +00:00
|
|
|
std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions; // access from m_Thread only
|
2015-01-11 22:41:56 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP/I2PControl
|
|
|
|
const decltype(m_NTCPSessions)& GetNTCPSessions () const { return m_NTCPSessions; };
|
|
|
|
};
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|