i2pd/libi2pd/SSU.h

146 lines
5.6 KiB
C
Raw Normal View History

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