i2pd/libi2pd/NTCPSession.h

233 lines
8.0 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>
2015-01-12 01:41:56 +03:00
#include <map>
2014-11-26 00:30:15 +03:00
#include <memory>
2015-01-12 01:41:56 +03:00
#include <thread>
2015-01-12 20:15:54 +03:00
#include <mutex>
2015-01-12 01:41:56 +03:00
#include <boost/asio.hpp>
2015-11-03 17:15:49 +03:00
#include "Crypto.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"
2014-10-21 00:09:59 +04:00
#include "TransportSession.h"
2018-02-20 19:38:48 +03:00
#include "CryptoWorker.h"
2013-09-10 05:35:46 +04:00
namespace i2p
{
namespace transport
2013-09-10 05:35:46 +04:00
{
struct NTCPPhase1
{
uint8_t pubKey[256];
uint8_t HXxorHI[32];
2017-05-29 08:28:16 +03:00
};
2013-09-10 05:35:46 +04:00
struct NTCPPhase2
{
uint8_t pubKey[256];
struct
{
uint8_t hxy[32];
2016-01-12 03:03:04 +03:00
uint8_t timestamp[4];
2013-09-10 05:35:46 +04:00
uint8_t filler[12];
2017-05-29 08:28:16 +03:00
} encrypted;
};
2013-09-10 05:35:46 +04:00
2017-05-29 08:28:16 +03:00
const size_t NTCP_MAX_MESSAGE_SIZE = 16384;
2017-01-03 19:52:28 +03:00
const size_t NTCP_BUFFER_SIZE = 1028; // fits 1 tunnel data message
2016-11-29 22:12:44 +03:00
const int NTCP_CONNECT_TIMEOUT = 5; // 5 seconds
2017-03-12 21:48:11 +03:00
const int NTCP_ESTABLISH_TIMEOUT = 10; // 10 seconds
2014-04-08 05:40:28 +04:00
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes
2017-05-29 08:28:16 +03:00
const int NTCP_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
const size_t NTCP_DEFAULT_PHASE3_SIZE = 2/*size*/ + i2p::data::DEFAULT_IDENTITY_SIZE/*387*/ + 4/*ts*/ + 15/*padding*/ + 40/*signature*/; // 448
const int NTCP_CLOCK_SKEW = 60; // in seconds
2016-07-12 23:26:36 +03:00
const int NTCP_MAX_OUTGOING_QUEUE_SIZE = 200; // how many messages we can queue up
2014-10-20 23:19:56 +04:00
2015-01-12 01:41:56 +03:00
class NTCPServer;
2014-11-26 00:30:15 +03:00
class NTCPSession: public TransportSession, public std::enable_shared_from_this<NTCPSession>
2013-09-10 05:35:46 +04:00
{
public:
2015-01-12 01:41:56 +03:00
NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
~NTCPSession ();
2014-11-25 23:16:03 +03:00
void Terminate ();
2015-02-07 04:53:48 +03:00
void Done ();
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; };
2018-02-20 20:40:28 +03:00
boost::asio::io_service & GetService();
2017-05-29 08:28:16 +03:00
bool IsEstablished () const { return m_IsEstablished; };
bool IsTerminated () const { return m_IsTerminated; };
2017-05-29 08:28:16 +03:00
2013-09-10 05:35:46 +04:00
void ClientLogin ();
void ServerLogin ();
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
2017-05-29 08:28:16 +03:00
2015-01-21 20:08:15 +03:00
private:
2013-10-23 06:43:29 +04:00
void PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs);
2014-11-25 23:16:03 +03:00
void Connected ();
2013-11-29 16:52:09 +04:00
void SendTimeSyncMessage ();
void SetIsEstablished (bool isEstablished) { m_IsEstablished = isEstablished; }
2013-09-10 05:35:46 +04:00
void CreateAESKey (uint8_t * pubKey);
2017-05-29 08:28:16 +03:00
2013-09-10 05:35:46 +04: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 HandlePhase2 ();
2013-09-10 05:35:46 +04:00
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 18:35:35 +03:00
void SendPhase4 (uint32_t tsA, uint32_t tsB);
2013-09-10 05:35:46 +04: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 20:33:51 +03: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 05:35:46 +04:00
void HandlePhase4Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2017-05-29 08:28:16 +03:00
2013-09-10 05:35:46 +04:00
// common
void Receive ();
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2017-05-29 08:28:16 +03:00
bool DecryptNextBlock (const uint8_t * encrypted);
2017-01-06 01:37:39 +03: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);
2017-01-06 01:37:39 +03:00
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs);
2017-05-29 08:28:16 +03:00
2013-09-10 05:35:46 +04:00
private:
2018-02-20 20:59:39 +03:00
2015-01-12 01:41:56 +03:00
NTCPServer& m_Server;
2013-11-29 16:52:09 +04:00
boost::asio::ip::tcp::socket m_Socket;
2015-02-07 04:53:48 +03:00
bool m_IsEstablished, m_IsTerminated;
2017-05-29 08:28:16 +03:00
2014-05-07 06:30:09 +04:00
i2p::crypto::CBCDecryption m_Decryption;
i2p::crypto::CBCEncryption m_Encryption;
2014-09-12 06:15:20 +04:00
struct Establisher
2017-05-29 08:28:16 +03:00
{
2014-09-12 06:15:20 +04:00
NTCPPhase1 phase1;
NTCPPhase2 phase2;
2017-05-29 08:28:16 +03:00
} * m_Establisher;
2014-11-25 18:14:18 +03:00
i2p::crypto::AESAlignedBuffer<NTCP_BUFFER_SIZE + 16> m_ReceiveBuffer;
i2p::crypto::AESAlignedBuffer<16> m_TimeSyncBuffer;
2017-05-29 08:28:16 +03:00
int m_ReceiveBufferOffset;
2013-09-10 05:35:46 +04:00
std::shared_ptr<I2NPMessage> m_NextMessage;
2013-10-27 19:23:15 +04:00
size_t m_NextMessageOffset;
2015-01-23 06:00:41 +03:00
i2p::I2NPMessagesHandler m_Handler;
2015-01-28 03:12:27 +03:00
bool m_IsSending;
std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue;
2017-05-29 08:28:16 +03:00
};
2015-01-12 01:41:56 +03:00
// TODO: move to NTCP.h/.cpp
class NTCPServer
{
public:
2018-02-20 20:59:39 +03:00
typedef i2p::worker::ThreadPool<NTCPSession> Pool;
2018-02-20 19:38:48 +03:00
enum RemoteAddressType
{
eIP4Address,
eIP6Address,
eHostname
};
enum ProxyType
{
eNoProxy,
eSocksProxy,
eHTTPProxy
};
2018-02-20 19:38:48 +03:00
NTCPServer (int workers=4);
2015-01-12 01:41:56 +03:00
~NTCPServer ();
void Start ();
void Stop ();
2015-11-25 19:51:35 +03:00
bool AddNTCPSession (std::shared_ptr<NTCPSession> session);
2015-01-12 01:41:56 +03:00
void RemoveNTCPSession (std::shared_ptr<NTCPSession> session);
std::shared_ptr<NTCPSession> FindNTCPSession (const i2p::data::IdentHash& ident);
void ConnectWithProxy (const std::string& addr, uint16_t port, RemoteAddressType addrtype, std::shared_ptr<NTCPSession> conn);
2017-05-29 08:28:16 +03:00
void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCPSession> conn);
bool IsBoundV4() const { return m_NTCPAcceptor != nullptr; };
bool IsBoundV6() const { return m_NTCPV6Acceptor != nullptr; };
bool NetworkIsReady() const { return IsBoundV4() || IsBoundV6() || UsingProxy(); };
bool UsingProxy() const { return m_ProxyType != eNoProxy; };
2017-05-29 08:28:16 +03:00
void UseProxy(ProxyType proxy, const std::string & address, uint16_t port);
2016-06-13 18:34:44 +03:00
2017-05-29 08:28:16 +03:00
boost::asio::io_service& GetService () { return m_Service; };
2015-02-10 21:05:08 +03:00
2017-10-30 15:27:55 +03:00
void SetSessionLimits(uint16_t softLimit, uint16_t hardLimit) { m_SoftLimit = softLimit; m_HardLimit = hardLimit; }
bool ShouldLimit() const { return ShouldHardLimit() || ShouldSoftLimit(); }
2018-02-20 20:40:28 +03:00
void Work(std::shared_ptr<NTCPSession> conn, Pool::WorkFunc work)
{
m_CryptoPool->Offer({conn, work});
}
2015-01-12 01:41:56 +03:00
private:
2017-10-30 15:27:55 +03:00
/** @brief return true for hard limit */
bool ShouldHardLimit() const { return m_HardLimit && m_NTCPSessions.size() >= m_HardLimit; }
/** @brief return true for probabalistic soft backoff */
bool ShouldSoftLimit() const
{
auto sessions = m_NTCPSessions.size();
return sessions && m_SoftLimit && m_SoftLimit < sessions && ( rand() % sessions ) <= m_SoftLimit;
}
2015-01-12 01:41:56 +03:00
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 22:12:44 +03:00
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCPSession> conn, std::shared_ptr<boost::asio::deadline_timer> timer);
void HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr<NTCPSession> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType adddrtype);
void AfterSocksHandshake(std::shared_ptr<NTCPSession> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType adddrtype);
2017-05-29 08:28:16 +03:00
// timer
void ScheduleTermination ();
void HandleTerminationTimer (const boost::system::error_code& ecode);
2017-05-29 08:28:16 +03:00
private:
2018-02-20 20:59:39 +03:00
2015-01-12 01:41:56 +03:00
bool m_IsRunning;
2017-05-29 08:28:16 +03:00
std::thread * m_Thread;
2015-01-12 01:41:56 +03:00
boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work;
boost::asio::deadline_timer m_TerminationTimer;
2015-01-12 01:41:56 +03:00
boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor;
2015-11-25 19:51:35 +03:00
std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions; // access from m_Thread only
std::list<std::shared_ptr<NTCPSession> > m_PendingIncomingSessions;
2015-01-12 01:41:56 +03:00
ProxyType m_ProxyType;
std::string m_ProxyAddress;
uint16_t m_ProxyPort;
2017-05-29 08:28:16 +03:00
boost::asio::ip::tcp::resolver m_Resolver;
boost::asio::ip::tcp::endpoint * m_ProxyEndpoint;
2017-10-30 15:27:55 +03:00
2018-02-20 20:59:39 +03:00
std::shared_ptr<Pool> m_CryptoPool;
2017-10-30 15:27:55 +03:00
uint16_t m_SoftLimit, m_HardLimit;
2015-01-12 01:41:56 +03:00
public:
// for HTTP/I2PControl
const decltype(m_NTCPSessions)& GetNTCPSessions () const { return m_NTCPSessions; };
2017-05-29 08:28:16 +03:00
};
}
}
2013-09-10 05:35:46 +04:00
#endif