Transports.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef TRANSPORTS_H__
  2. #define TRANSPORTS_H__
  3. #include <thread>
  4. #include <mutex>
  5. #include <condition_variable>
  6. #include <functional>
  7. #include <map>
  8. #include <vector>
  9. #include <queue>
  10. #include <string>
  11. #include <memory>
  12. #include <atomic>
  13. #include <boost/asio.hpp>
  14. #include "TransportSession.h"
  15. #include "NTCPSession.h"
  16. #include "SSU.h"
  17. #include "NTCP2.h"
  18. #include "RouterInfo.h"
  19. #include "I2NPProtocol.h"
  20. #include "Identity.h"
  21. namespace i2p
  22. {
  23. namespace transport
  24. {
  25. class DHKeysPairSupplier
  26. {
  27. public:
  28. DHKeysPairSupplier (int size);
  29. ~DHKeysPairSupplier ();
  30. void Start ();
  31. void Stop ();
  32. std::shared_ptr<i2p::crypto::DHKeys> Acquire ();
  33. void Return (std::shared_ptr<i2p::crypto::DHKeys> pair);
  34. private:
  35. void Run ();
  36. void CreateDHKeysPairs (int num);
  37. private:
  38. const int m_QueueSize;
  39. std::queue<std::shared_ptr<i2p::crypto::DHKeys> > m_Queue;
  40. bool m_IsRunning;
  41. std::thread * m_Thread;
  42. std::condition_variable m_Acquired;
  43. std::mutex m_AcquiredMutex;
  44. };
  45. struct Peer
  46. {
  47. int numAttempts;
  48. std::shared_ptr<const i2p::data::RouterInfo> router;
  49. std::list<std::shared_ptr<TransportSession> > sessions;
  50. uint64_t creationTime;
  51. std::vector<std::shared_ptr<i2p::I2NPMessage> > delayedMessages;
  52. void Done ()
  53. {
  54. for (auto& it: sessions)
  55. it->Done ();
  56. }
  57. };
  58. const size_t SESSION_CREATION_TIMEOUT = 10; // in seconds
  59. const int PEER_TEST_INTERVAL = 71; // in minutes
  60. const int MAX_NUM_DELAYED_MESSAGES = 50;
  61. class Transports
  62. {
  63. public:
  64. Transports ();
  65. ~Transports ();
  66. void Start (bool enableNTCP=true, bool enableSSU=true);
  67. void Stop ();
  68. bool IsBoundNTCP() const { return m_NTCPServer != nullptr; }
  69. bool IsBoundSSU() const { return m_SSUServer != nullptr; }
  70. bool IsBoundNTCP2() const { return m_NTCP2Server != nullptr; }
  71. bool IsOnline() const { return m_IsOnline; };
  72. void SetOnline (bool online) { m_IsOnline = online; };
  73. boost::asio::io_service& GetService () { return *m_Service; };
  74. std::shared_ptr<i2p::crypto::DHKeys> GetNextDHKeysPair ();
  75. void ReuseDHKeysPair (std::shared_ptr<i2p::crypto::DHKeys> pair);
  76. void SendMessage (const i2p::data::IdentHash& ident, std::shared_ptr<i2p::I2NPMessage> msg);
  77. void SendMessages (const i2p::data::IdentHash& ident, const std::vector<std::shared_ptr<i2p::I2NPMessage> >& msgs);
  78. void CloseSession (std::shared_ptr<const i2p::data::RouterInfo> router);
  79. void PeerConnected (std::shared_ptr<TransportSession> session);
  80. void PeerDisconnected (std::shared_ptr<TransportSession> session);
  81. bool IsConnected (const i2p::data::IdentHash& ident) const;
  82. void UpdateSentBytes (uint64_t numBytes) { m_TotalSentBytes += numBytes; };
  83. void UpdateReceivedBytes (uint64_t numBytes) { m_TotalReceivedBytes += numBytes; };
  84. uint64_t GetTotalSentBytes () const { return m_TotalSentBytes; };
  85. uint64_t GetTotalReceivedBytes () const { return m_TotalReceivedBytes; };
  86. uint64_t GetTotalTransitTransmittedBytes () const { return m_TotalTransitTransmittedBytes; }
  87. void UpdateTotalTransitTransmittedBytes (uint32_t add) { m_TotalTransitTransmittedBytes += add; };
  88. uint32_t GetInBandwidth () const { return m_InBandwidth; };
  89. uint32_t GetOutBandwidth () const { return m_OutBandwidth; };
  90. uint32_t GetTransitBandwidth () const { return m_TransitBandwidth; };
  91. bool IsBandwidthExceeded () const;
  92. bool IsTransitBandwidthExceeded () const;
  93. size_t GetNumPeers () const { return m_Peers.size (); };
  94. std::shared_ptr<const i2p::data::RouterInfo> GetRandomPeer () const;
  95. /** get a trusted first hop for restricted routes */
  96. std::shared_ptr<const i2p::data::RouterInfo> GetRestrictedPeer() const;
  97. /** do we want to use restricted routes? */
  98. bool RoutesRestricted() const;
  99. /** restrict routes to use only these router families for first hops */
  100. void RestrictRoutesToFamilies(std::set<std::string> families);
  101. /** restrict routes to use only these routers for first hops */
  102. void RestrictRoutesToRouters(std::set<i2p::data::IdentHash> routers);
  103. bool IsRestrictedPeer(const i2p::data::IdentHash & ident) const;
  104. void PeerTest ();
  105. private:
  106. void Run ();
  107. void RequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, const i2p::data::IdentHash& ident);
  108. void HandleRequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, i2p::data::IdentHash ident);
  109. void PostMessages (i2p::data::IdentHash ident, std::vector<std::shared_ptr<i2p::I2NPMessage> > msgs);
  110. void PostCloseSession (std::shared_ptr<const i2p::data::RouterInfo> router);
  111. bool ConnectToPeer (const i2p::data::IdentHash& ident, Peer& peer);
  112. void HandlePeerCleanupTimer (const boost::system::error_code& ecode);
  113. void HandlePeerTestTimer (const boost::system::error_code& ecode);
  114. void UpdateBandwidth ();
  115. void DetectExternalIP ();
  116. private:
  117. bool m_IsOnline, m_IsRunning, m_IsNAT;
  118. std::thread * m_Thread;
  119. boost::asio::io_service * m_Service;
  120. boost::asio::io_service::work * m_Work;
  121. boost::asio::deadline_timer * m_PeerCleanupTimer, * m_PeerTestTimer;
  122. NTCPServer * m_NTCPServer;
  123. SSUServer * m_SSUServer;
  124. NTCP2Server * m_NTCP2Server;
  125. mutable std::mutex m_PeersMutex;
  126. std::map<i2p::data::IdentHash, Peer> m_Peers;
  127. DHKeysPairSupplier m_DHKeysPairSupplier;
  128. std::atomic<uint64_t> m_TotalSentBytes, m_TotalReceivedBytes, m_TotalTransitTransmittedBytes;
  129. uint32_t m_InBandwidth, m_OutBandwidth, m_TransitBandwidth; // bytes per second
  130. uint64_t m_LastInBandwidthUpdateBytes, m_LastOutBandwidthUpdateBytes, m_LastTransitBandwidthUpdateBytes;
  131. uint64_t m_LastBandwidthUpdateTime;
  132. /** which router families to trust for first hops */
  133. std::vector<std::string> m_TrustedFamilies;
  134. mutable std::mutex m_FamilyMutex;
  135. /** which routers for first hop to trust */
  136. std::vector<i2p::data::IdentHash> m_TrustedRouters;
  137. mutable std::mutex m_TrustedRoutersMutex;
  138. i2p::I2NPMessagesHandler m_LoopbackHandler;
  139. public:
  140. // for HTTP only
  141. const NTCPServer * GetNTCPServer () const { return m_NTCPServer; };
  142. const SSUServer * GetSSUServer () const { return m_SSUServer; };
  143. const NTCP2Server * GetNTCP2Server () const { return m_NTCP2Server; };
  144. const decltype(m_Peers)& GetPeers () const { return m_Peers; };
  145. };
  146. extern Transports transports;
  147. }
  148. }
  149. #endif