i2pd/SSU.h

115 lines
4 KiB
C
Raw Normal View History

2014-01-23 22:10:33 +01:00
#ifndef SSU_H__
#define SSU_H__
#include <inttypes.h>
2014-06-17 19:15:32 +02:00
#include <string.h>
2014-01-24 22:30:07 +01:00
#include <map>
2014-02-10 00:28:34 +01:00
#include <list>
2014-04-09 20:58:30 +02:00
#include <set>
2014-04-20 02:45:41 +02:00
#include <thread>
2015-02-07 21:25:06 +01:00
#include <mutex>
2014-01-23 22:10:33 +01:00
#include <boost/asio.hpp>
2015-11-03 15:15:49 +01:00
#include "Crypto.h"
2014-01-27 22:52:17 +01:00
#include "I2PEndian.h"
2014-04-04 20:56:46 +02:00
#include "Identity.h"
2014-01-28 22:49:54 +01:00
#include "RouterInfo.h"
2014-01-29 22:49:53 +01:00
#include "I2NPProtocol.h"
2014-10-30 20:13:12 +01:00
#include "SSUSession.h"
2014-01-23 22:10:33 +01:00
namespace i2p
{
namespace transport
2014-01-23 22:10:33 +01:00
{
2014-09-07 02:43:20 +02:00
const int SSU_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
2015-02-26 20:54:28 +01:00
const int SSU_PEER_TEST_TIMEOUT = 60; // 60 seconds
2014-09-05 22:35:02 +02:00
const int SSU_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
2014-09-09 14:03:05 +02:00
const size_t SSU_MAX_NUM_INTRODUCERS = 3;
2015-02-09 02:28:18 +01:00
struct SSUPacket
{
i2p::crypto::AESAlignedBuffer<1500> buf;
boost::asio::ip::udp::endpoint from;
size_t len;
};
2014-07-22 04:13:57 +02:00
2014-01-23 22:10:33 +01:00
class SSUServer
{
public:
2014-04-20 02:45:41 +02:00
SSUServer (int port);
2014-01-24 22:30:07 +01:00
~SSUServer ();
2014-01-23 22:10:33 +01:00
void Start ();
void Stop ();
2014-11-24 18:26:11 +01:00
std::shared_ptr<SSUSession> GetSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false);
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> GetRandomEstablishedSession (std::shared_ptr<const SSUSession> excluded);
void DeleteSession (std::shared_ptr<SSUSession> session);
2014-02-21 22:13:36 +01:00
void DeleteAllSessions ();
2015-01-12 03:00:38 +01:00
boost::asio::io_service& GetService () { return m_Service; };
boost::asio::io_service& GetServiceV6 () { return m_ServiceV6; };
2014-01-30 20:03:11 +01:00
const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; };
2014-06-10 16:39:29 +02:00
void Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
2014-04-16 21:54:28 +02:00
void AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay);
2014-11-24 18:26:11 +01:00
std::shared_ptr<SSUSession> FindRelaySession (uint32_t tag);
2014-01-23 22:10:33 +01:00
2015-03-26 21:10:52 +01:00
void NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session = nullptr);
2015-02-26 03:56:51 +01:00
PeerTestParticipant GetPeerTestParticipant (uint32_t nonce);
2015-03-26 21:10:52 +01:00
std::shared_ptr<SSUSession> GetPeerTestSession (uint32_t nonce);
2015-02-26 03:56:51 +01:00
void UpdatePeerTest (uint32_t nonce, PeerTestParticipant role);
void RemovePeerTest (uint32_t nonce);
2015-02-25 21:26:06 +01:00
2014-01-23 22:10:33 +01:00
private:
2014-04-20 02:45:41 +02:00
void Run ();
void RunV6 ();
2015-02-09 02:28:18 +01:00
void RunReceivers ();
2014-01-23 22:10:33 +01:00
void Receive ();
2014-10-29 20:02:48 +01:00
void ReceiveV6 ();
2015-02-09 02:28:18 +01: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-02-09 17:53:11 +01:00
void HandleReceivedPackets (std::vector<SSUPacket *> packets);
2014-01-23 22:10:33 +01:00
2014-09-03 22:49:48 +02:00
template<typename Filter>
2014-11-24 18:26:11 +01:00
std::shared_ptr<SSUSession> GetRandomSession (Filter filter);
2014-09-07 02:43:20 +02:00
std::set<SSUSession *> FindIntroducers (int maxNumIntroducers);
void ScheduleIntroducersUpdateTimer ();
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode);
2015-02-25 21:26:06 +01:00
2015-02-26 20:54:28 +01:00
void SchedulePeerTestsCleanupTimer ();
void HandlePeerTestsCleanupTimer (const boost::system::error_code& ecode);
2014-01-23 22:10:33 +01:00
private:
2014-04-20 02:45:41 +02:00
2015-02-26 03:56:51 +01:00
struct PeerTest
{
uint64_t creationTime;
PeerTestParticipant role;
2015-03-26 21:10:52 +01:00
std::shared_ptr<SSUSession> session; // for Bob to Alice
2015-02-26 03:56:51 +01:00
};
2014-04-20 02:45:41 +02:00
bool m_IsRunning;
2015-02-09 02:28:18 +01:00
std::thread * m_Thread, * m_ThreadV6, * m_ReceiversThread;
boost::asio::io_service m_Service, m_ServiceV6, m_ReceiversService;
boost::asio::io_service::work m_Work, m_WorkV6, m_ReceiversWork;
2014-10-29 20:02:48 +01:00
boost::asio::ip::udp::endpoint m_Endpoint, m_EndpointV6;
boost::asio::ip::udp::socket m_Socket, m_SocketV6;
2015-02-26 20:54:28 +01:00
boost::asio::deadline_timer m_IntroducersUpdateTimer, m_PeerTestsCleanupTimer;
2014-09-07 02:43:20 +02:00
std::list<boost::asio::ip::udp::endpoint> m_Introducers; // introducers we are connected to
2015-02-27 19:07:32 +01:00
mutable std::mutex m_SessionsMutex;
2014-11-24 18:26:11 +01:00
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > m_Sessions;
2014-04-16 21:54:28 +02:00
std::map<uint32_t, boost::asio::ip::udp::endpoint> m_Relays; // we are introducer
2015-02-26 03:56:51 +01:00
std::map<uint32_t, PeerTest> m_PeerTests; // nonce -> creation time in milliseconds
2014-02-25 04:28:28 +01:00
public:
// for HTTP only
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
2014-01-23 22:10:33 +01:00
};
}
}
#endif