mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
5.5 KiB
139 lines
5.5 KiB
11 years ago
|
#ifndef SSU_H__
|
||
|
#define SSU_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
11 years ago
|
#include <string.h>
|
||
11 years ago
|
#include <map>
|
||
11 years ago
|
#include <list>
|
||
11 years ago
|
#include <set>
|
||
11 years ago
|
#include <thread>
|
||
10 years ago
|
#include <mutex>
|
||
11 years ago
|
#include <boost/asio.hpp>
|
||
9 years ago
|
#include "Crypto.h"
|
||
11 years ago
|
#include "I2PEndian.h"
|
||
11 years ago
|
#include "Identity.h"
|
||
11 years ago
|
#include "RouterInfo.h"
|
||
11 years ago
|
#include "I2NPProtocol.h"
|
||
10 years ago
|
#include "SSUSession.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace transport
|
||
11 years ago
|
{
|
||
10 years ago
|
const int SSU_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
|
||
10 years ago
|
const int SSU_PEER_TEST_TIMEOUT = 60; // 60 seconds
|
||
10 years ago
|
const int SSU_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
|
||
8 years ago
|
const int SSU_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
|
||
10 years ago
|
const size_t SSU_MAX_NUM_INTRODUCERS = 3;
|
||
8 years ago
|
const size_t SSU_SOCKET_RECEIVE_BUFFER_SIZE = 0x1FFFF; // 128K
|
||
8 years ago
|
const size_t SSU_SOCKET_SEND_BUFFER_SIZE = 0x1FFFF; // 128K
|
||
10 years ago
|
|
||
|
struct SSUPacket
|
||
|
{
|
||
8 years ago
|
i2p::crypto::AESAlignedBuffer<SSU_MTU_V6 + 18> buf; // max MTU + iv + size
|
||
10 years ago
|
boost::asio::ip::udp::endpoint from;
|
||
|
size_t len;
|
||
|
};
|
||
11 years ago
|
|
||
11 years ago
|
class SSUServer
|
||
|
{
|
||
|
public:
|
||
|
|
||
11 years ago
|
SSUServer (int port);
|
||
9 years ago
|
SSUServer (const boost::asio::ip::address & addr, int port); // ipv6 only constructor
|
||
11 years ago
|
~SSUServer ();
|
||
11 years ago
|
void Start ();
|
||
|
void Stop ();
|
||
8 years ago
|
void CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false, bool v4only = false);
|
||
9 years ago
|
void CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
|
||
|
const boost::asio::ip::address& addr, int port, bool peerTest = false);
|
||
9 years ago
|
void CreateDirectSession (std::shared_ptr<const i2p::data::RouterInfo> router, boost::asio::ip::udp::endpoint remoteEndpoint, bool peerTest);
|
||
10 years ago
|
std::shared_ptr<SSUSession> FindSession (std::shared_ptr<const i2p::data::RouterInfo> router) const;
|
||
|
std::shared_ptr<SSUSession> FindSession (const boost::asio::ip::udp::endpoint& e) const;
|
||
9 years ago
|
std::shared_ptr<SSUSession> GetRandomEstablishedV4Session (std::shared_ptr<const SSUSession> excluded);
|
||
9 years ago
|
std::shared_ptr<SSUSession> GetRandomEstablishedV6Session (std::shared_ptr<const SSUSession> excluded);
|
||
10 years ago
|
void DeleteSession (std::shared_ptr<SSUSession> session);
|
||
11 years ago
|
void DeleteAllSessions ();
|
||
|
|
||
10 years ago
|
boost::asio::io_service& GetService () { return m_Service; };
|
||
|
boost::asio::io_service& GetServiceV6 () { return m_ServiceV6; };
|
||
11 years ago
|
const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; };
|
||
11 years ago
|
void Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
|
||
8 years ago
|
void AddRelay (uint32_t tag, std::shared_ptr<SSUSession> relay);
|
||
8 years ago
|
void RemoveRelay (uint32_t tag);
|
||
10 years ago
|
std::shared_ptr<SSUSession> FindRelaySession (uint32_t tag);
|
||
11 years ago
|
|
||
10 years ago
|
void NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session = nullptr);
|
||
10 years ago
|
PeerTestParticipant GetPeerTestParticipant (uint32_t nonce);
|
||
10 years ago
|
std::shared_ptr<SSUSession> GetPeerTestSession (uint32_t nonce);
|
||
10 years ago
|
void UpdatePeerTest (uint32_t nonce, PeerTestParticipant role);
|
||
|
void RemovePeerTest (uint32_t nonce);
|
||
9 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
8 years ago
|
void OpenSocket ();
|
||
|
void OpenSocketV6 ();
|
||
11 years ago
|
void Run ();
|
||
10 years ago
|
void RunV6 ();
|
||
10 years ago
|
void RunReceivers ();
|
||
8 years ago
|
void RunReceiversV6 ();
|
||
11 years ago
|
void Receive ();
|
||
10 years ago
|
void ReceiveV6 ();
|
||
10 years ago
|
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);
|
||
9 years ago
|
void HandleReceivedPackets (std::vector<SSUPacket *> packets,
|
||
9 years ago
|
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> >* sessions);
|
||
11 years ago
|
|
||
9 years ago
|
void CreateSessionThroughIntroducer (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false);
|
||
10 years ago
|
template<typename Filter>
|
||
9 years ago
|
std::shared_ptr<SSUSession> GetRandomV4Session (Filter filter);
|
||
9 years ago
|
template<typename Filter>
|
||
|
std::shared_ptr<SSUSession> GetRandomV6Session (Filter filter);
|
||
|
|
||
10 years ago
|
std::set<SSUSession *> FindIntroducers (int maxNumIntroducers);
|
||
|
void ScheduleIntroducersUpdateTimer ();
|
||
|
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode);
|
||
10 years ago
|
|
||
10 years ago
|
void SchedulePeerTestsCleanupTimer ();
|
||
|
void HandlePeerTestsCleanupTimer (const boost::system::error_code& ecode);
|
||
|
|
||
8 years ago
|
// timer
|
||
|
void ScheduleTermination ();
|
||
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
||
|
void ScheduleTerminationV6 ();
|
||
|
void HandleTerminationTimerV6 (const boost::system::error_code& ecode);
|
||
|
|
||
11 years ago
|
private:
|
||
11 years ago
|
|
||
10 years ago
|
struct PeerTest
|
||
|
{
|
||
|
uint64_t creationTime;
|
||
|
PeerTestParticipant role;
|
||
10 years ago
|
std::shared_ptr<SSUSession> session; // for Bob to Alice
|
||
9 years ago
|
};
|
||
10 years ago
|
|
||
9 years ago
|
bool m_OnlyV6;
|
||
11 years ago
|
bool m_IsRunning;
|
||
8 years ago
|
std::thread * m_Thread, * m_ThreadV6, * m_ReceiversThread, * m_ReceiversThreadV6;
|
||
|
boost::asio::io_service m_Service, m_ServiceV6, m_ReceiversService, m_ReceiversServiceV6;
|
||
|
boost::asio::io_service::work m_Work, m_WorkV6, m_ReceiversWork, m_ReceiversWorkV6;
|
||
10 years ago
|
boost::asio::ip::udp::endpoint m_Endpoint, m_EndpointV6;
|
||
|
boost::asio::ip::udp::socket m_Socket, m_SocketV6;
|
||
8 years ago
|
boost::asio::deadline_timer m_IntroducersUpdateTimer, m_PeerTestsCleanupTimer,
|
||
|
m_TerminationTimer, m_TerminationTimerV6;
|
||
10 years ago
|
std::list<boost::asio::ip::udp::endpoint> m_Introducers; // introducers we are connected to
|
||
9 years ago
|
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > m_Sessions, m_SessionsV6;
|
||
8 years ago
|
std::map<uint32_t, std::shared_ptr<SSUSession> > m_Relays; // we are introducer
|
||
10 years ago
|
std::map<uint32_t, PeerTest> m_PeerTests; // nonce -> creation time in milliseconds
|
||
8 years ago
|
|
||
11 years ago
|
public:
|
||
|
// for HTTP only
|
||
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
||
9 years ago
|
const decltype(m_SessionsV6)& GetSessionsV6 () const { return m_SessionsV6; };
|
||
11 years ago
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|