SSU.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef SSU_H__
  2. #define SSU_H__
  3. #include <inttypes.h>
  4. #include <string.h>
  5. #include <map>
  6. #include <list>
  7. #include <set>
  8. #include <thread>
  9. #include <mutex>
  10. #include <boost/asio.hpp>
  11. #include "Crypto.h"
  12. #include "I2PEndian.h"
  13. #include "Identity.h"
  14. #include "RouterInfo.h"
  15. #include "I2NPProtocol.h"
  16. #include "SSUSession.h"
  17. namespace i2p
  18. {
  19. namespace transport
  20. {
  21. const int SSU_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
  22. const int SSU_PEER_TEST_TIMEOUT = 60; // 60 seconds
  23. const int SSU_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
  24. const int SSU_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds
  25. const size_t SSU_MAX_NUM_INTRODUCERS = 3;
  26. const size_t SSU_SOCKET_RECEIVE_BUFFER_SIZE = 0x1FFFF; // 128K
  27. const size_t SSU_SOCKET_SEND_BUFFER_SIZE = 0x1FFFF; // 128K
  28. struct SSUPacket
  29. {
  30. i2p::crypto::AESAlignedBuffer<SSU_MTU_V6 + 18> buf; // max MTU + iv + size
  31. boost::asio::ip::udp::endpoint from;
  32. size_t len;
  33. };
  34. class SSUServer
  35. {
  36. public:
  37. SSUServer (int port);
  38. SSUServer (const boost::asio::ip::address & addr, int port); // ipv6 only constructor
  39. ~SSUServer ();
  40. void Start ();
  41. void Stop ();
  42. void CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false, bool v4only = false);
  43. void CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
  44. const boost::asio::ip::address& addr, int port, bool peerTest = false);
  45. void CreateDirectSession (std::shared_ptr<const i2p::data::RouterInfo> router, boost::asio::ip::udp::endpoint remoteEndpoint, bool peerTest);
  46. std::shared_ptr<SSUSession> FindSession (std::shared_ptr<const i2p::data::RouterInfo> router) const;
  47. std::shared_ptr<SSUSession> FindSession (const boost::asio::ip::udp::endpoint& e) const;
  48. std::shared_ptr<SSUSession> GetRandomEstablishedV4Session (std::shared_ptr<const SSUSession> excluded);
  49. std::shared_ptr<SSUSession> GetRandomEstablishedV6Session (std::shared_ptr<const SSUSession> excluded);
  50. void DeleteSession (std::shared_ptr<SSUSession> session);
  51. void DeleteAllSessions ();
  52. boost::asio::io_service& GetService () { return m_Service; };
  53. boost::asio::io_service& GetServiceV6 () { return m_ServiceV6; };
  54. const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; };
  55. void Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
  56. void AddRelay (uint32_t tag, std::shared_ptr<SSUSession> relay);
  57. void RemoveRelay (uint32_t tag);
  58. std::shared_ptr<SSUSession> FindRelaySession (uint32_t tag);
  59. void NewPeerTest (uint32_t nonce, PeerTestParticipant role, std::shared_ptr<SSUSession> session = nullptr);
  60. PeerTestParticipant GetPeerTestParticipant (uint32_t nonce);
  61. std::shared_ptr<SSUSession> GetPeerTestSession (uint32_t nonce);
  62. void UpdatePeerTest (uint32_t nonce, PeerTestParticipant role);
  63. void RemovePeerTest (uint32_t nonce);
  64. private:
  65. void OpenSocket ();
  66. void OpenSocketV6 ();
  67. void Run ();
  68. void RunV6 ();
  69. void RunReceivers ();
  70. void RunReceiversV6 ();
  71. void Receive ();
  72. void ReceiveV6 ();
  73. void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet);
  74. void HandleReceivedFromV6 (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet);
  75. void HandleReceivedPackets (std::vector<SSUPacket *> packets,
  76. std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> >* sessions);
  77. void CreateSessionThroughIntroducer (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false);
  78. template<typename Filter>
  79. std::shared_ptr<SSUSession> GetRandomV4Session (Filter filter);
  80. template<typename Filter>
  81. std::shared_ptr<SSUSession> GetRandomV6Session (Filter filter);
  82. std::set<SSUSession *> FindIntroducers (int maxNumIntroducers);
  83. void ScheduleIntroducersUpdateTimer ();
  84. void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode);
  85. void SchedulePeerTestsCleanupTimer ();
  86. void HandlePeerTestsCleanupTimer (const boost::system::error_code& ecode);
  87. // timer
  88. void ScheduleTermination ();
  89. void HandleTerminationTimer (const boost::system::error_code& ecode);
  90. void ScheduleTerminationV6 ();
  91. void HandleTerminationTimerV6 (const boost::system::error_code& ecode);
  92. private:
  93. struct PeerTest
  94. {
  95. uint64_t creationTime;
  96. PeerTestParticipant role;
  97. std::shared_ptr<SSUSession> session; // for Bob to Alice
  98. };
  99. bool m_OnlyV6;
  100. bool m_IsRunning;
  101. std::thread * m_Thread, * m_ThreadV6, * m_ReceiversThread, * m_ReceiversThreadV6;
  102. boost::asio::io_service m_Service, m_ServiceV6, m_ReceiversService, m_ReceiversServiceV6;
  103. boost::asio::io_service::work m_Work, m_WorkV6, m_ReceiversWork, m_ReceiversWorkV6;
  104. boost::asio::ip::udp::endpoint m_Endpoint, m_EndpointV6;
  105. boost::asio::ip::udp::socket m_Socket, m_SocketV6;
  106. boost::asio::deadline_timer m_IntroducersUpdateTimer, m_PeerTestsCleanupTimer,
  107. m_TerminationTimer, m_TerminationTimerV6;
  108. std::list<boost::asio::ip::udp::endpoint> m_Introducers; // introducers we are connected to
  109. std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSUSession> > m_Sessions, m_SessionsV6;
  110. std::map<uint32_t, std::shared_ptr<SSUSession> > m_Relays; // we are introducer
  111. std::map<uint32_t, PeerTest> m_PeerTests; // nonce -> creation time in milliseconds
  112. public:
  113. // for HTTP only
  114. const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
  115. const decltype(m_SessionsV6)& GetSessionsV6 () const { return m_SessionsV6; };
  116. };
  117. }
  118. }
  119. #endif