2022-02-04 20:01:18 +00:00
|
|
|
/*
|
2022-02-18 06:20:06 +00:00
|
|
|
* Copyright (c) 2022, The PurpleI2P Project
|
2022-02-04 20:01:18 +00: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SSU2_H__
|
|
|
|
#define SSU2_H__
|
|
|
|
|
2022-02-28 01:15:14 +00:00
|
|
|
#include <unordered_map>
|
2022-06-05 23:33:36 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "SSU2Session.h"
|
2022-02-04 20:01:18 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace transport
|
|
|
|
{
|
2022-03-19 00:21:31 +00:00
|
|
|
const int SSU2_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
|
2022-03-01 02:46:00 +00:00
|
|
|
const size_t SSU2_SOCKET_RECEIVE_BUFFER_SIZE = 0x1FFFF; // 128K
|
|
|
|
const size_t SSU2_SOCKET_SEND_BUFFER_SIZE = 0x1FFFF; // 128K
|
2022-07-10 21:13:25 +00:00
|
|
|
const size_t SSU2_MAX_NUM_INTRODUCERS = 3;
|
|
|
|
const int SSU2_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
|
|
|
|
const int SSU2_TO_INTRODUCER_SESSION_EXPIRATION = 4800; // 80 minutes
|
2022-07-21 01:55:48 +00:00
|
|
|
const int SSU2_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
|
2022-06-05 23:33:36 +00:00
|
|
|
|
2022-05-20 16:56:05 +00:00
|
|
|
class SSU2Server: private i2p::util::RunnableServiceWithWork
|
2022-02-28 01:15:14 +00:00
|
|
|
{
|
2022-03-14 23:25:59 +00:00
|
|
|
struct Packet
|
|
|
|
{
|
2022-07-09 21:05:23 +00:00
|
|
|
uint8_t buf[SSU2_MAX_PACKET_SIZE];
|
2022-03-14 23:25:59 +00:00
|
|
|
size_t len;
|
|
|
|
boost::asio::ip::udp::endpoint from;
|
2022-05-20 16:56:05 +00:00
|
|
|
};
|
2022-04-05 20:14:13 +00:00
|
|
|
|
|
|
|
class ReceiveService: public i2p::util::RunnableService
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ReceiveService (const std::string& name): RunnableService (name) {};
|
|
|
|
boost::asio::io_service& GetService () { return GetIOService (); };
|
|
|
|
void Start () { StartIOService (); };
|
|
|
|
void Stop () { StopIOService (); };
|
2022-05-20 16:56:05 +00:00
|
|
|
};
|
|
|
|
|
2022-02-28 01:15:14 +00:00
|
|
|
public:
|
|
|
|
|
2022-03-11 21:17:44 +00:00
|
|
|
SSU2Server ();
|
2022-02-28 01:15:14 +00:00
|
|
|
~SSU2Server () {};
|
|
|
|
|
2022-03-11 21:17:44 +00:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
boost::asio::io_service& GetService () { return GetIOService (); };
|
2022-06-17 19:16:12 +00:00
|
|
|
void SetLocalAddress (const boost::asio::ip::address& localAddress);
|
2022-06-21 20:20:39 +00:00
|
|
|
bool IsSupported (const boost::asio::ip::address& addr) const;
|
2022-06-17 19:16:12 +00:00
|
|
|
|
2022-03-27 20:39:58 +00:00
|
|
|
void AddSession (std::shared_ptr<SSU2Session> session);
|
|
|
|
void RemoveSession (uint64_t connID);
|
2022-04-28 17:11:51 +00:00
|
|
|
void AddSessionByRouterHash (std::shared_ptr<SSU2Session> session);
|
2022-06-22 15:59:29 +00:00
|
|
|
bool AddPendingOutgoingSession (std::shared_ptr<SSU2Session> session);
|
2022-06-22 17:15:25 +00:00
|
|
|
void RemovePendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep);
|
2022-06-07 16:55:58 +00:00
|
|
|
std::shared_ptr<SSU2Session> FindSession (const i2p::data::IdentHash& ident) const;
|
2022-06-22 15:59:29 +00:00
|
|
|
std::shared_ptr<SSU2Session> FindPendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep) const;
|
2022-06-26 14:07:39 +00:00
|
|
|
std::shared_ptr<SSU2Session> GetRandomSession (i2p::data::RouterInfo::CompatibleTransports remoteTransports,
|
|
|
|
const i2p::data::IdentHash& excluded) const;
|
2022-06-07 16:55:58 +00:00
|
|
|
|
2022-04-21 19:47:36 +00:00
|
|
|
void AddRelay (uint32_t tag, std::shared_ptr<SSU2Session> relay);
|
2022-05-20 16:56:05 +00:00
|
|
|
void RemoveRelay (uint32_t tag);
|
2022-04-21 19:47:36 +00:00
|
|
|
std::shared_ptr<SSU2Session> FindRelaySession (uint32_t tag);
|
2022-05-20 16:56:05 +00:00
|
|
|
|
|
|
|
void Send (const uint8_t * header, size_t headerLen, const uint8_t * payload, size_t payloadLen,
|
2022-03-26 20:35:07 +00:00
|
|
|
const boost::asio::ip::udp::endpoint& to);
|
2022-05-20 16:56:05 +00:00
|
|
|
void Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
|
2022-03-01 02:46:00 +00:00
|
|
|
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to);
|
2022-05-20 16:56:05 +00:00
|
|
|
|
2022-03-17 01:11:48 +00:00
|
|
|
bool CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
|
2022-06-12 01:26:23 +00:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo::Address> address, bool peerTest = false);
|
2022-06-02 01:51:02 +00:00
|
|
|
bool StartPeerTest (std::shared_ptr<const i2p::data::RouterInfo> router, bool v4);
|
|
|
|
|
2022-03-23 23:13:44 +00:00
|
|
|
void UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp);
|
2022-03-24 01:48:41 +00:00
|
|
|
uint64_t FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep) const;
|
2022-06-17 02:37:33 +00:00
|
|
|
uint64_t GetIncomingToken (const boost::asio::ip::udp::endpoint& ep);
|
|
|
|
std::pair<uint64_t, uint32_t> NewIncomingToken (const boost::asio::ip::udp::endpoint& ep);
|
|
|
|
|
2022-07-21 01:55:48 +00:00
|
|
|
void RescheduleIntroducersUpdateTimer ();
|
|
|
|
void RescheduleIntroducersUpdateTimerV6 ();
|
|
|
|
|
2022-02-28 01:15:14 +00:00
|
|
|
private:
|
|
|
|
|
2022-03-17 22:45:14 +00:00
|
|
|
boost::asio::ip::udp::socket& OpenSocket (const boost::asio::ip::udp::endpoint& localEndpoint);
|
|
|
|
void Receive (boost::asio::ip::udp::socket& socket);
|
2022-05-20 16:56:05 +00:00
|
|
|
void HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred,
|
2022-03-17 22:45:14 +00:00
|
|
|
Packet * packet, boost::asio::ip::udp::socket& socket);
|
2022-04-13 16:33:59 +00:00
|
|
|
void HandleReceivedPacket (Packet * packet);
|
|
|
|
void HandleReceivedPackets (std::vector<Packet *> packets);
|
2022-03-01 02:46:00 +00:00
|
|
|
void ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
2022-03-19 00:21:31 +00:00
|
|
|
|
|
|
|
void ScheduleTermination ();
|
|
|
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
2022-03-31 19:35:55 +00:00
|
|
|
|
|
|
|
void ScheduleResend ();
|
|
|
|
void HandleResendTimer (const boost::system::error_code& ecode);
|
2022-05-01 14:33:25 +00:00
|
|
|
|
2022-06-12 01:26:23 +00:00
|
|
|
void ConnectThroughIntroducer (std::shared_ptr<SSU2Session> session);
|
2022-07-10 21:13:25 +00:00
|
|
|
std::list<std::shared_ptr<SSU2Session> > FindIntroducers (int maxNumIntroducers,
|
|
|
|
bool v4, const std::set<i2p::data::IdentHash>& excluded) const;
|
|
|
|
void UpdateIntroducers (bool v4);
|
2022-07-21 01:55:48 +00:00
|
|
|
void ScheduleIntroducersUpdateTimer ();
|
|
|
|
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);
|
|
|
|
void ScheduleIntroducersUpdateTimerV6 ();
|
|
|
|
|
2022-02-28 01:15:14 +00:00
|
|
|
private:
|
|
|
|
|
2022-05-20 16:56:05 +00:00
|
|
|
ReceiveService m_ReceiveService;
|
2022-04-05 20:14:13 +00:00
|
|
|
boost::asio::ip::udp::socket m_SocketV4, m_SocketV6;
|
2022-06-17 19:16:12 +00:00
|
|
|
boost::asio::ip::address m_AddressV4, m_AddressV6;
|
2022-02-28 01:15:14 +00:00
|
|
|
std::unordered_map<uint64_t, std::shared_ptr<SSU2Session> > m_Sessions;
|
2022-07-24 20:44:02 +00:00
|
|
|
std::unordered_map<i2p::data::IdentHash, std::shared_ptr<SSU2Session> > m_SessionsByRouterHash;
|
2022-03-01 02:46:00 +00:00
|
|
|
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSU2Session> > m_PendingOutgoingSessions;
|
2022-03-23 23:13:44 +00:00
|
|
|
std::map<boost::asio::ip::udp::endpoint, std::pair<uint64_t, uint32_t> > m_IncomingTokens, m_OutgoingTokens; // remote endpoint -> (token, expires in seconds)
|
2022-05-20 16:56:05 +00:00
|
|
|
std::map<uint32_t, std::shared_ptr<SSU2Session> > m_Relays; // we are introducer, relay tag -> session
|
2022-07-21 23:38:18 +00:00
|
|
|
std::list<i2p::data::IdentHash> m_Introducers, m_IntroducersV6; // introducers we are connected to
|
2022-03-14 23:25:59 +00:00
|
|
|
i2p::util::MemoryPoolMt<Packet> m_PacketsPool;
|
2022-07-21 01:55:48 +00:00
|
|
|
boost::asio::deadline_timer m_TerminationTimer, m_ResendTimer,
|
|
|
|
m_IntroducersUpdateTimer, m_IntroducersUpdateTimerV6;
|
2022-04-07 14:57:57 +00:00
|
|
|
std::shared_ptr<SSU2Session> m_LastSession;
|
2022-07-21 01:55:48 +00:00
|
|
|
bool m_IsPublished; // if we maintain introducers
|
2022-07-10 21:13:25 +00:00
|
|
|
|
2022-03-27 23:29:50 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP/I2PControl
|
2022-05-20 16:56:05 +00:00
|
|
|
const decltype(m_Sessions)& GetSSU2Sessions () const { return m_Sessions; };
|
|
|
|
};
|
2022-02-04 20:01:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|