i2pd/SSU.h

86 lines
2.8 KiB
C
Raw Normal View History

2014-01-24 01:10:33 +04:00
#ifndef SSU_H__
#define SSU_H__
#include <inttypes.h>
2014-06-17 21:15:32 +04:00
#include <string.h>
2014-01-25 01:30:07 +04:00
#include <map>
2014-02-10 03:28:34 +04:00
#include <list>
2014-04-09 22:58:30 +04:00
#include <set>
2014-04-20 04:45:41 +04:00
#include <thread>
2014-01-24 01:10:33 +04:00
#include <boost/asio.hpp>
2014-01-28 01:52:17 +04:00
#include "I2PEndian.h"
2014-04-04 22:56:46 +04:00
#include "Identity.h"
2014-01-29 01:49:54 +04:00
#include "RouterInfo.h"
2014-01-30 01:49:53 +04:00
#include "I2NPProtocol.h"
2014-10-30 22:13:12 +03:00
#include "SSUSession.h"
2014-01-24 01:10:33 +04:00
namespace i2p
{
namespace transport
2014-01-24 01:10:33 +04:00
{
2014-09-07 04:43:20 +04:00
const int SSU_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
2014-09-06 00:35:02 +04:00
const int SSU_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
2014-09-09 16:03:05 +04:00
const size_t SSU_MAX_NUM_INTRODUCERS = 3;
2014-07-22 06:13:57 +04:00
2014-01-24 01:10:33 +04:00
class SSUServer
{
public:
2014-04-20 04:45:41 +04:00
SSUServer (int port);
2014-01-25 01:30:07 +04:00
~SSUServer ();
2014-01-24 01:10:33 +04:00
void Start ();
void Stop ();
2014-04-08 03:28:06 +04:00
SSUSession * GetSession (const i2p::data::RouterInfo * router, bool peerTest = false);
2014-03-26 05:17:03 +04:00
SSUSession * FindSession (const i2p::data::RouterInfo * router);
2014-04-16 23:54:28 +04:00
SSUSession * FindSession (const boost::asio::ip::udp::endpoint& e);
2014-09-06 00:35:02 +04:00
SSUSession * GetRandomEstablishedSession (const SSUSession * excluded);
2014-02-09 06:06:40 +04:00
void DeleteSession (SSUSession * session);
2014-02-22 01:13:36 +04:00
void DeleteAllSessions ();
2014-03-31 06:55:03 +04:00
boost::asio::io_service& GetService () { return m_Socket.get_io_service(); };
2014-01-30 23:03:11 +04:00
const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; };
2014-06-10 18:39:29 +04:00
void Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
2014-04-16 23:54:28 +04:00
void AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay);
SSUSession * FindRelaySession (uint32_t tag);
2014-01-24 01:10:33 +04:00
private:
2014-04-20 04:45:41 +04:00
void Run ();
2014-01-24 01:10:33 +04:00
void Receive ();
2014-10-29 22:02:48 +03:00
void ReceiveV6 ();
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleReceivedFromV6 (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleReceivedBuffer (boost::asio::ip::udp::endpoint& from, uint8_t * buf, std::size_t bytes_transferred);
2014-01-24 01:10:33 +04:00
2014-09-04 00:49:48 +04:00
template<typename Filter>
SSUSession * GetRandomSession (Filter filter);
2014-09-07 04:43:20 +04:00
std::set<SSUSession *> FindIntroducers (int maxNumIntroducers);
void ScheduleIntroducersUpdateTimer ();
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode);
2014-01-24 01:10:33 +04:00
private:
2014-04-20 04:45:41 +04:00
bool m_IsRunning;
std::thread * m_Thread;
boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work;
2014-10-29 22:02:48 +03:00
boost::asio::ip::udp::endpoint m_Endpoint, m_EndpointV6;
boost::asio::ip::udp::socket m_Socket, m_SocketV6;
boost::asio::ip::udp::endpoint m_SenderEndpoint, m_SenderEndpointV6;
2014-09-07 04:43:20 +04:00
boost::asio::deadline_timer m_IntroducersUpdateTimer;
std::list<boost::asio::ip::udp::endpoint> m_Introducers; // introducers we are connected to
2014-10-29 22:02:48 +03:00
uint8_t m_ReceiveBuffer[2*SSU_MTU_V4], m_ReceiveBufferV6[2*SSU_MTU_V6];
2014-01-25 01:30:07 +04:00
std::map<boost::asio::ip::udp::endpoint, SSUSession *> m_Sessions;
2014-04-16 23:54:28 +04:00
std::map<uint32_t, boost::asio::ip::udp::endpoint> m_Relays; // we are introducer
2014-02-25 07:28:28 +04:00
public:
// for HTTP only
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
2014-01-24 01:10:33 +04:00
};
}
}
#endif