i2pd/SSU.h

89 lines
3.1 KiB
C
Raw Normal View History

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>
2014-01-23 21:10:33 +00:00
#include <boost/asio.hpp>
2014-11-18 17:37:04 +00:00
#include "aes.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
{
2014-09-07 00:43:20 +00:00
const int SSU_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
2014-09-05 20:35:02 +00:00
const int SSU_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
2014-09-09 12:03:05 +00:00
const size_t SSU_MAX_NUM_INTRODUCERS = 3;
2014-07-22 02:13:57 +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 ();
2014-11-24 17:26:11 +00: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 21:13:36 +00:00
void DeleteAllSessions ();
2014-03-31 02:55:03 +00:00
boost::asio::io_service& GetService () { return m_Socket.get_io_service(); };
2014-01-30 19:03:11 +00:00
const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; };
2014-06-10 14:39:29 +00:00
void Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
2014-04-16 19:54:28 +00:00
void AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay);
2014-11-24 17:26:11 +00:00
std::shared_ptr<SSUSession> FindRelaySession (uint32_t tag);
2014-01-23 21:10:33 +00:00
private:
2014-04-20 00:45:41 +00:00
void Run ();
void RunV6 ();
2014-01-23 21:10:33 +00:00
void Receive ();
2014-10-29 19:02:48 +00: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-23 21:10:33 +00:00
2014-09-03 20:49:48 +00:00
template<typename Filter>
2014-11-24 17:26:11 +00:00
std::shared_ptr<SSUSession> GetRandomSession (Filter filter);
2014-09-07 00:43:20 +00:00
std::set<SSUSession *> FindIntroducers (int maxNumIntroducers);
void ScheduleIntroducersUpdateTimer ();
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode);
2014-01-23 21:10:33 +00:00
private:
2014-04-20 00:45:41 +00:00
bool m_IsRunning;
std::thread * m_Thread, * m_ThreadV6;
boost::asio::io_service m_Service, m_ServiceV6;
boost::asio::io_service::work m_Work, m_WorkV6;
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::ip::udp::endpoint m_SenderEndpoint, m_SenderEndpointV6;
2014-09-07 00:43:20 +00:00
boost::asio::deadline_timer m_IntroducersUpdateTimer;
std::list<boost::asio::ip::udp::endpoint> m_Introducers; // introducers we are connected to
2014-11-18 17:37:04 +00:00
i2p::crypto::AESAlignedBuffer<2*SSU_MTU_V4> m_ReceiveBuffer;
i2p::crypto::AESAlignedBuffer<2*SSU_MTU_V6> m_ReceiveBufferV6;
2014-11-24 17:26:11 +00:00
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > m_Sessions;
2014-04-16 19:54:28 +00:00
std::map<uint32_t, boost::asio::ip::udp::endpoint> m_Relays; // we are introducer
2014-02-25 03:28:28 +00:00
public:
// for HTTP only
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
2014-01-23 21:10:33 +00:00
};
}
}
#endif