2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2022-02-01 18:43:11 -05:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2014-01-23 16:10:33 -05:00
|
|
|
#ifndef SSU_H__
|
|
|
|
#define SSU_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-06-17 13:15:32 -04:00
|
|
|
#include <string.h>
|
2014-01-24 16:30:07 -05:00
|
|
|
#include <map>
|
2014-02-09 18:28:34 -05:00
|
|
|
#include <list>
|
2014-04-09 14:58:30 -04:00
|
|
|
#include <set>
|
2014-04-19 20:45:41 -04:00
|
|
|
#include <thread>
|
2015-02-07 15:25:06 -05:00
|
|
|
#include <mutex>
|
2014-01-23 16:10:33 -05:00
|
|
|
#include <boost/asio.hpp>
|
2015-11-03 09:15:49 -05:00
|
|
|
#include "Crypto.h"
|
2021-09-11 18:58:27 -04:00
|
|
|
#include "util.h"
|
2014-01-27 16:52:17 -05:00
|
|
|
#include "I2PEndian.h"
|
2014-04-04 14:56:46 -04:00
|
|
|
#include "Identity.h"
|
2014-01-28 16:49:54 -05:00
|
|
|
#include "RouterInfo.h"
|
2014-01-29 16:49:53 -05:00
|
|
|
#include "I2NPProtocol.h"
|
2014-10-30 15:13:12 -04:00
|
|
|
#include "SSUSession.h"
|
2014-01-23 16:10:33 -05:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-21 12:25:53 -04:00
|
|
|
namespace transport
|
2014-01-23 16:10:33 -05:00
|
|
|
{
|
2018-01-06 11:48:51 +08:00
|
|
|
const int SSU_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
|
|
|
|
const int SSU_PEER_TEST_TIMEOUT = 60; // 60 seconds
|
2014-09-05 16:35:02 -04:00
|
|
|
const int SSU_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
|
2021-04-23 12:17:48 -04:00
|
|
|
const int SSU_TO_INTRODUCER_SESSION_EXPIRATION = 4800; // 80 minutes
|
2016-08-24 11:21:49 -04:00
|
|
|
const int SSU_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
|
2014-09-09 08:03:05 -04:00
|
|
|
const size_t SSU_MAX_NUM_INTRODUCERS = 3;
|
2017-01-19 11:19:09 -05:00
|
|
|
const size_t SSU_SOCKET_RECEIVE_BUFFER_SIZE = 0x1FFFF; // 128K
|
2017-01-19 15:47:01 -05:00
|
|
|
const size_t SSU_SOCKET_SEND_BUFFER_SIZE = 0x1FFFF; // 128K
|
2015-02-08 20:28:18 -05:00
|
|
|
|
|
|
|
struct SSUPacket
|
|
|
|
{
|
2017-02-12 10:12:12 -05:00
|
|
|
i2p::crypto::AESAlignedBuffer<SSU_MTU_V6 + 18> buf; // max MTU + iv + size
|
2015-02-08 20:28:18 -05:00
|
|
|
boost::asio::ip::udp::endpoint from;
|
|
|
|
size_t len;
|
2018-01-06 11:48:51 +08:00
|
|
|
};
|
|
|
|
|
2014-01-23 16:10:33 -05:00
|
|
|
class SSUServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-04-19 20:45:41 -04:00
|
|
|
SSUServer (int port);
|
2014-01-24 16:30:07 -05:00
|
|
|
~SSUServer ();
|
2014-01-23 16:10:33 -05:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2021-04-04 10:36:22 -04:00
|
|
|
bool CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false, bool v4only = false);
|
|
|
|
bool CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
|
2021-02-22 21:04:26 -05:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo::Address> address, bool peerTest = false);
|
2015-12-09 22:17:43 -05:00
|
|
|
void CreateDirectSession (std::shared_ptr<const i2p::data::RouterInfo> router, boost::asio::ip::udp::endpoint remoteEndpoint, bool peerTest);
|
2014-11-24 12:26:11 -05:00
|
|
|
std::shared_ptr<SSUSession> FindSession (const boost::asio::ip::udp::endpoint& e) const;
|
2015-11-30 15:53:07 -05:00
|
|
|
std::shared_ptr<SSUSession> GetRandomEstablishedV4Session (std::shared_ptr<const SSUSession> excluded);
|
2016-07-22 13:08:41 -04:00
|
|
|
std::shared_ptr<SSUSession> GetRandomEstablishedV6Session (std::shared_ptr<const SSUSession> excluded);
|
2014-11-24 12:26:11 -05:00
|
|
|
void DeleteSession (std::shared_ptr<SSUSession> session);
|
2018-01-06 11:48:51 +08:00
|
|
|
void DeleteAllSessions ();
|
2014-02-21 16:13:36 -05:00
|
|
|
|
2015-01-11 21:00:38 -05:00
|
|
|
boost::asio::io_service& GetService () { return m_Service; };
|
2021-09-11 18:58:27 -04:00
|
|
|
i2p::util::MemoryPool<Fragment>& GetFragmentsPool () { return m_FragmentsPool; };
|
|
|
|
i2p::util::MemoryPool<IncompleteMessage>& GetIncompleteMessagesPool () { return m_IncompleteMessagesPool; };
|
|
|
|
i2p::util::MemoryPool<SentMessage>& GetSentMessagesPool () { return m_SentMessagesPool; };
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-03-01 12:20:53 -05:00
|
|
|
uint16_t GetPort () const { return m_Endpoint.port (); };
|
2022-02-01 18:43:11 -05:00
|
|
|
bool IsSyncClockFromPeers () const { return m_IsSyncClockFromPeers; };
|
2021-02-27 16:13:12 -05:00
|
|
|
void SetLocalAddress (const boost::asio::ip::address& localAddress);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2014-06-10 10:39:29 -04:00
|
|
|
void Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
|
2016-12-30 17:53:54 -05:00
|
|
|
void AddRelay (uint32_t tag, std::shared_ptr<SSUSession> relay);
|
2016-12-29 22:06:33 -05:00
|
|
|
void RemoveRelay (uint32_t tag);
|
2014-11-24 12:26:11 -05:00
|
|
|
std::shared_ptr<SSUSession> FindRelaySession (uint32_t tag);
|
2021-03-04 15:55:51 -05:00
|
|
|
void RescheduleIntroducersUpdateTimer ();
|
2021-04-20 20:02:30 -04:00
|
|
|
void RescheduleIntroducersUpdateTimerV6 ();
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2015-03-26 16:10:52 -04:00
|
|
|
void NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session = nullptr);
|
2015-02-25 21:56:51 -05:00
|
|
|
PeerTestParticipant GetPeerTestParticipant (uint32_t nonce);
|
2015-03-26 16:10:52 -04:00
|
|
|
std::shared_ptr<SSUSession> GetPeerTestSession (uint32_t nonce);
|
2015-02-25 21:56:51 -05:00
|
|
|
void UpdatePeerTest (uint32_t nonce, PeerTestParticipant role);
|
|
|
|
void RemovePeerTest (uint32_t nonce);
|
2018-01-06 11:48:51 +08:00
|
|
|
|
2014-01-23 16:10:33 -05:00
|
|
|
private:
|
|
|
|
|
2016-11-30 21:14:10 -05:00
|
|
|
void OpenSocket ();
|
|
|
|
void OpenSocketV6 ();
|
2014-04-19 20:45:41 -04:00
|
|
|
void Run ();
|
2015-02-08 20:28:18 -05:00
|
|
|
void RunReceivers ();
|
2017-01-19 15:47:01 -05:00
|
|
|
void RunReceiversV6 ();
|
2014-01-23 16:10:33 -05:00
|
|
|
void Receive ();
|
2014-10-29 15:02:48 -04:00
|
|
|
void ReceiveV6 ();
|
2015-02-08 20:28:18 -05:00
|
|
|
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet);
|
|
|
|
void HandleReceivedFromV6 (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet);
|
2015-11-30 15:53:07 -05:00
|
|
|
void HandleReceivedPackets (std::vector<SSUPacket *> packets,
|
2015-11-30 19:45:57 -05:00
|
|
|
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> >* sessions);
|
2014-01-23 16:10:33 -05:00
|
|
|
|
2021-11-27 23:30:35 +03:00
|
|
|
void CreateSessionThroughIntroducer (std::shared_ptr<const i2p::data::RouterInfo> router,
|
2021-02-28 18:58:25 -05:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo::Address> address, bool peerTest = false);
|
2014-09-03 16:49:48 -04:00
|
|
|
template<typename Filter>
|
2015-11-30 15:53:07 -05:00
|
|
|
std::shared_ptr<SSUSession> GetRandomV4Session (Filter filter);
|
2016-07-22 13:08:41 -04:00
|
|
|
template<typename Filter>
|
2018-01-06 11:48:51 +08:00
|
|
|
std::shared_ptr<SSUSession> GetRandomV6Session (Filter filter);
|
2016-07-22 13:08:41 -04:00
|
|
|
|
2021-04-24 14:56:34 -04:00
|
|
|
std::list<std::shared_ptr<SSUSession> > FindIntroducers (int maxNumIntroducers, bool v4, std::set<i2p::data::IdentHash>& excluded);
|
2014-09-06 20:43:20 -04:00
|
|
|
void ScheduleIntroducersUpdateTimer ();
|
2021-04-20 20:02:30 -04:00
|
|
|
void ScheduleIntroducersUpdateTimerV6 ();
|
|
|
|
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);
|
2015-02-25 15:26:06 -05:00
|
|
|
|
2015-02-26 14:54:28 -05:00
|
|
|
void SchedulePeerTestsCleanupTimer ();
|
|
|
|
void HandlePeerTestsCleanupTimer (const boost::system::error_code& ecode);
|
|
|
|
|
2016-08-24 11:21:49 -04:00
|
|
|
// timer
|
|
|
|
void ScheduleTermination ();
|
|
|
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
|
|
|
void ScheduleTerminationV6 ();
|
|
|
|
void HandleTerminationTimerV6 (const boost::system::error_code& ecode);
|
|
|
|
|
2014-01-23 16:10:33 -05:00
|
|
|
private:
|
2014-04-19 20:45:41 -04:00
|
|
|
|
2015-02-25 21:56:51 -05:00
|
|
|
struct PeerTest
|
|
|
|
{
|
|
|
|
uint64_t creationTime;
|
|
|
|
PeerTestParticipant role;
|
2015-03-26 16:10:52 -04:00
|
|
|
std::shared_ptr<SSUSession> session; // for Bob to Alice
|
2016-06-27 10:24:37 -04:00
|
|
|
};
|
2018-01-06 11:48:51 +08:00
|
|
|
|
2021-02-13 18:36:23 -05:00
|
|
|
volatile bool m_IsRunning;
|
2020-10-14 21:06:51 -04:00
|
|
|
std::thread * m_Thread, * m_ReceiversThread, * m_ReceiversThreadV6;
|
|
|
|
boost::asio::io_service m_Service, m_ReceiversService, m_ReceiversServiceV6;
|
|
|
|
boost::asio::io_service::work m_Work, m_ReceiversWork, m_ReceiversWorkV6;
|
2014-10-29 15:02:48 -04:00
|
|
|
boost::asio::ip::udp::endpoint m_Endpoint, m_EndpointV6;
|
|
|
|
boost::asio::ip::udp::socket m_Socket, m_SocketV6;
|
2021-11-27 23:30:35 +03:00
|
|
|
boost::asio::deadline_timer m_IntroducersUpdateTimer, m_IntroducersUpdateTimerV6,
|
2021-04-20 20:02:30 -04:00
|
|
|
m_PeerTestsCleanupTimer, m_TerminationTimer, m_TerminationTimerV6;
|
2022-02-01 18:43:11 -05:00
|
|
|
bool m_IsSyncClockFromPeers;
|
2021-04-20 20:02:30 -04:00
|
|
|
std::list<boost::asio::ip::udp::endpoint> m_Introducers, m_IntroducersV6; // introducers we are connected to
|
2015-11-30 15:53:07 -05:00
|
|
|
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > m_Sessions, m_SessionsV6;
|
2016-12-30 17:53:54 -05:00
|
|
|
std::map<uint32_t, std::shared_ptr<SSUSession> > m_Relays; // we are introducer
|
2021-09-25 18:30:17 -04:00
|
|
|
std::map<uint32_t, PeerTest> m_PeerTests; // nonce -> creation time in milliseconds
|
2018-01-06 11:48:51 +08:00
|
|
|
|
2021-09-11 18:58:27 -04:00
|
|
|
i2p::util::MemoryPool<Fragment> m_FragmentsPool;
|
|
|
|
i2p::util::MemoryPool<IncompleteMessage> m_IncompleteMessagesPool;
|
|
|
|
i2p::util::MemoryPool<SentMessage> m_SentMessagesPool;
|
2021-09-13 13:13:27 -04:00
|
|
|
i2p::util::MemoryPoolMt<SSUPacket> m_PacketsPool;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2014-02-24 22:28:28 -05:00
|
|
|
public:
|
|
|
|
// for HTTP only
|
|
|
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
2015-11-30 15:53:07 -05:00
|
|
|
const decltype(m_SessionsV6)& GetSessionsV6 () const { return m_SessionsV6; };
|
2014-01-23 16:10:33 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|