i2pd/libi2pd/SSU2.h

195 lines
8.8 KiB
C
Raw Normal View History

2022-02-04 23:01:18 +03:00
/*
* Copyright (c) 2022-2024, The PurpleI2P Project
2022-02-04 23:01:18 +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
*/
#ifndef SSU2_H__
#define SSU2_H__
2022-02-28 04:15:14 +03:00
#include <unordered_map>
#include <unordered_set>
2024-06-08 23:08:32 +03:00
#include <vector>
#include <mutex>
2024-06-08 05:10:52 +03:00
#include <random>
2022-06-06 02:33:36 +03:00
#include "util.h"
#include "SSU2Session.h"
#include "Socks5.h"
2022-02-04 23:01:18 +03:00
namespace i2p
{
namespace transport
{
2024-08-27 03:57:28 +03:00
const int SSU2_TERMINATION_CHECK_TIMEOUT = 23; // in seconds
const int SSU2_TERMINATION_CHECK_TIMEOUT_VARIANCE = 5; // in seconds
2023-01-15 01:05:09 +03:00
const int SSU2_CLEANUP_INTERVAL = 72; // in seconds
2024-03-14 17:34:40 +03:00
const int SSU2_RESEND_CHECK_TIMEOUT = 40; // in milliseconds
const int SSU2_RESEND_CHECK_TIMEOUT_VARIANCE = 10; // in milliseconds
2024-05-15 18:57:14 +03:00
const int SSU2_RESEND_CHECK_MORE_TIMEOUT = 4; // in milliseconds
const int SSU2_RESEND_CHECK_MORE_TIMEOUT_VARIANCE = 9; // in milliseconds
const size_t SSU2_MAX_RESEND_PACKETS = 128; // packets to resend at the time
const uint64_t SSU2_SOCKET_MIN_BUFFER_SIZE = 128 * 1024;
const uint64_t SSU2_SOCKET_MAX_BUFFER_SIZE = 4 * 1024 * 1024;
2022-07-11 00:13:25 +03:00
const size_t SSU2_MAX_NUM_INTRODUCERS = 3;
const size_t SSU2_MIN_RECEIVED_PACKET_SIZE = 40; // 16 byte short header + 8 byte minimum payload + 16 byte MAC
2022-07-11 00:13:25 +03:00
const int SSU2_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
const int SSU2_TO_INTRODUCER_SESSION_EXPIRATION = 4800; // 80 minutes
const int SSU2_KEEP_ALIVE_INTERVAL = 15; // in seconds
const int SSU2_KEEP_ALIVE_INTERVAL_VARIANCE = 4; // in seconds
2022-10-28 21:06:45 +03:00
const int SSU2_PROXY_CONNECT_RETRY_TIMEOUT = 30; // in seconds
class SSU2Server: private i2p::util::RunnableServiceWithWork
2022-02-28 04:15:14 +03:00
{
2022-03-15 02:25:59 +03:00
struct Packet
{
2022-07-10 00:05:23 +03:00
uint8_t buf[SSU2_MAX_PACKET_SIZE];
2022-03-15 02:25:59 +03:00
size_t len;
boost::asio::ip::udp::endpoint from;
};
2022-04-05 23:14:13 +03: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-02-28 04:15:14 +03:00
public:
2022-03-12 00:17:44 +03:00
SSU2Server ();
2022-02-28 04:15:14 +03:00
~SSU2Server () {};
2022-03-12 00:17:44 +03:00
void Start ();
void Stop ();
boost::asio::io_service& GetService () { return GetIOService (); };
void SetLocalAddress (const boost::asio::ip::address& localAddress);
2022-10-26 23:05:40 +03:00
bool SetProxy (const std::string& address, uint16_t port);
bool UsesProxy () const { return m_IsThroughProxy; };
bool IsSupported (const boost::asio::ip::address& addr) const;
2022-08-03 03:02:55 +03:00
uint16_t GetPort (bool v4) const;
2024-06-08 05:10:52 +03:00
std::mt19937& GetRng () { return m_Rng; }
bool IsMaxNumIntroducers (bool v4) const { return (v4 ? m_Introducers.size () : m_IntroducersV6.size ()) >= SSU2_MAX_NUM_INTRODUCERS; }
2022-08-09 02:57:48 +03:00
bool IsSyncClockFromPeers () const { return m_IsSyncClockFromPeers; };
void AdjustTimeOffset (int64_t offset, std::shared_ptr<const i2p::data::IdentityEx> from);
2022-03-27 23:39:58 +03:00
void AddSession (std::shared_ptr<SSU2Session> session);
void RemoveSession (uint64_t connID);
2022-04-28 20:11:51 +03:00
void AddSessionByRouterHash (std::shared_ptr<SSU2Session> session);
bool AddPendingOutgoingSession (std::shared_ptr<SSU2Session> session);
void RemovePendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep);
std::shared_ptr<SSU2Session> FindSession (const i2p::data::IdentHash& ident) const;
std::shared_ptr<SSU2Session> FindPendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep) const;
std::shared_ptr<SSU2Session> GetRandomPeerTestSession (i2p::data::RouterInfo::CompatibleTransports remoteTransports,
const i2p::data::IdentHash& excluded);
void AddRelay (uint32_t tag, std::shared_ptr<SSU2Session> relay);
void RemoveRelay (uint32_t tag);
std::shared_ptr<SSU2Session> FindRelaySession (uint32_t tag);
void Send (const uint8_t * header, size_t headerLen, const uint8_t * payload, size_t payloadLen,
2022-03-26 23:35:07 +03:00
const boost::asio::ip::udp::endpoint& to);
void Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
2022-03-01 05:46:00 +03:00
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to);
2022-03-17 04:11:48 +03:00
bool CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
std::shared_ptr<const i2p::data::RouterInfo::Address> address, bool peerTest = false);
2022-06-02 04:51:02 +03:00
bool StartPeerTest (std::shared_ptr<const i2p::data::RouterInfo> router, bool v4);
2022-03-24 02:13:44 +03:00
void UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp);
2022-12-03 23:05:27 +03:00
uint64_t FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep);
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 04:55:48 +03:00
void RescheduleIntroducersUpdateTimer ();
void RescheduleIntroducersUpdateTimerV6 ();
2022-08-05 01:13:44 +03:00
i2p::util::MemoryPool<SSU2SentPacket>& GetSentPacketsPool () { return m_SentPacketsPool; };
2023-01-18 05:32:36 +03:00
i2p::util::MemoryPool<SSU2IncompleteMessage>& GetIncompleteMessagesPool () { return m_IncompleteMessagesPool; };
2023-01-15 01:05:09 +03:00
i2p::util::MemoryPool<SSU2IncompleteMessage::Fragment>& GetFragmentsPool () { return m_FragmentsPool; };
2022-02-28 04:15:14 +03:00
private:
2022-03-18 01:45:14 +03:00
boost::asio::ip::udp::socket& OpenSocket (const boost::asio::ip::udp::endpoint& localEndpoint);
void Receive (boost::asio::ip::udp::socket& socket);
void HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred,
2022-03-18 01:45:14 +03:00
Packet * packet, boost::asio::ip::udp::socket& socket);
2022-04-13 19:33:59 +03:00
void HandleReceivedPacket (Packet * packet);
void HandleReceivedPackets (std::vector<Packet *> packets);
2022-03-01 05:46:00 +03:00
void ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
2022-03-19 03:21:31 +03:00
void ScheduleTermination ();
void HandleTerminationTimer (const boost::system::error_code& ecode);
2022-03-31 22:35:55 +03:00
2023-01-15 01:05:09 +03:00
void ScheduleCleanup ();
void HandleCleanupTimer (const boost::system::error_code& ecode);
void ScheduleResend (bool more);
2022-03-31 22:35:55 +03:00
void HandleResendTimer (const boost::system::error_code& ecode);
2022-05-01 17:33:25 +03:00
void ConnectThroughIntroducer (std::shared_ptr<SSU2Session> session);
2024-06-08 23:08:32 +03:00
std::vector<std::shared_ptr<SSU2Session> > FindIntroducers (int maxNumIntroducers,
bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded) const;
2022-07-11 00:13:25 +03:00
void UpdateIntroducers (bool v4);
2022-07-21 04:55:48 +03:00
void ScheduleIntroducersUpdateTimer ();
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);
void ScheduleIntroducersUpdateTimerV6 ();
2022-10-17 04:23:28 +03:00
void SendThroughProxy (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to);
2022-10-17 05:16:16 +03:00
void ProcessNextPacketFromProxy (uint8_t * buf, size_t len);
void ConnectToProxy ();
2022-10-28 21:06:45 +03:00
void ReconnectToProxy ();
void HandshakeWithProxy ();
void ReadHandshakeWithProxyReply ();
void SendUDPAssociateRequest ();
void ReadUDPAssociateReply ();
void ReadUDPAssociateSocket (); // handle if closed by peer
2022-02-28 04:15:14 +03:00
private:
ReceiveService m_ReceiveService;
2022-04-05 23:14:13 +03:00
boost::asio::ip::udp::socket m_SocketV4, m_SocketV6;
boost::asio::ip::address m_AddressV4, m_AddressV6;
2022-02-28 04:15:14 +03:00
std::unordered_map<uint64_t, std::shared_ptr<SSU2Session> > m_Sessions;
2022-07-24 23:44:02 +03:00
std::unordered_map<i2p::data::IdentHash, std::shared_ptr<SSU2Session> > m_SessionsByRouterHash;
2022-03-01 05:46:00 +03:00
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSU2Session> > m_PendingOutgoingSessions;
mutable std::mutex m_PendingOutgoingSessionsMutex;
2022-03-24 02:13:44 +03:00
std::map<boost::asio::ip::udp::endpoint, std::pair<uint64_t, uint32_t> > m_IncomingTokens, m_OutgoingTokens; // remote endpoint -> (token, expires in seconds)
std::map<uint32_t, std::shared_ptr<SSU2Session> > m_Relays; // we are introducer, relay tag -> session
std::list<i2p::data::IdentHash> m_Introducers, m_IntroducersV6; // introducers we are connected to
2022-03-15 02:25:59 +03:00
i2p::util::MemoryPoolMt<Packet> m_PacketsPool;
2022-08-05 01:13:44 +03:00
i2p::util::MemoryPool<SSU2SentPacket> m_SentPacketsPool;
2023-01-18 05:32:36 +03:00
i2p::util::MemoryPool<SSU2IncompleteMessage> m_IncompleteMessagesPool;
2023-01-15 01:05:09 +03:00
i2p::util::MemoryPool<SSU2IncompleteMessage::Fragment> m_FragmentsPool;
boost::asio::deadline_timer m_TerminationTimer, m_CleanupTimer, m_ResendTimer,
2022-07-21 04:55:48 +03:00
m_IntroducersUpdateTimer, m_IntroducersUpdateTimerV6;
std::shared_ptr<SSU2Session> m_LastSession;
2022-07-21 04:55:48 +03:00
bool m_IsPublished; // if we maintain introducers
2022-08-09 02:57:48 +03:00
bool m_IsSyncClockFromPeers;
int64_t m_PendingTimeOffset; // during peer test
std::shared_ptr<const i2p::data::IdentityEx> m_PendingTimeOffsetFrom;
2024-06-08 05:10:52 +03:00
std::mt19937 m_Rng;
2022-10-17 04:23:28 +03:00
// proxy
bool m_IsThroughProxy;
uint8_t m_UDPRequestHeader[SOCKS5_UDP_IPV6_REQUEST_HEADER_SIZE];
std::unique_ptr<boost::asio::ip::tcp::endpoint> m_ProxyEndpoint;
std::unique_ptr<boost::asio::ip::tcp::socket> m_UDPAssociateSocket;
std::unique_ptr<boost::asio::ip::udp::endpoint> m_ProxyRelayEndpoint;
2022-10-28 21:06:45 +03:00
std::unique_ptr<boost::asio::deadline_timer> m_ProxyConnectRetryTimer;
2022-03-28 02:29:50 +03:00
public:
// for HTTP/I2PControl
const decltype(m_Sessions)& GetSSU2Sessions () const { return m_Sessions; };
};
2022-02-04 23:01:18 +03:00
}
}
#endif