TunnelPool.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef TUNNEL_POOL__
  2. #define TUNNEL_POOL__
  3. #include <inttypes.h>
  4. #include <set>
  5. #include <vector>
  6. #include <utility>
  7. #include <mutex>
  8. #include <memory>
  9. #include "Identity.h"
  10. #include "LeaseSet.h"
  11. #include "RouterInfo.h"
  12. #include "I2NPProtocol.h"
  13. #include "TunnelBase.h"
  14. #include "RouterContext.h"
  15. #include "Garlic.h"
  16. namespace i2p
  17. {
  18. namespace tunnel
  19. {
  20. class Tunnel;
  21. class InboundTunnel;
  22. class OutboundTunnel;
  23. enum TunnelBuildResult {
  24. eBuildResultOkay, // tunnel was built okay
  25. eBuildResultRejected, // tunnel build was explicitly rejected
  26. eBuildResultTimeout // tunnel build timed out
  27. };
  28. typedef std::shared_ptr<const i2p::data::IdentityEx> Peer;
  29. typedef std::vector<Peer> Path;
  30. /** interface for custom tunnel peer selection algorithm */
  31. struct ITunnelPeerSelector
  32. {
  33. virtual ~ITunnelPeerSelector() {};
  34. virtual bool SelectPeers(Path & peers, int hops, bool isInbound) = 0;
  35. virtual bool OnBuildResult(const Path & peers, bool isInbound, TunnelBuildResult result) = 0;
  36. };
  37. typedef std::function<std::shared_ptr<const i2p::data::RouterInfo>(std::shared_ptr<const i2p::data::RouterInfo>)> SelectHopFunc;
  38. // standard peer selection algorithm
  39. bool StandardSelectPeers(Path & path, int hops, bool inbound, SelectHopFunc nextHop);
  40. class TunnelPool: public std::enable_shared_from_this<TunnelPool> // per local destination
  41. {
  42. public:
  43. TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels);
  44. ~TunnelPool ();
  45. std::shared_ptr<i2p::garlic::GarlicDestination> GetLocalDestination () const { return m_LocalDestination; };
  46. void SetLocalDestination (std::shared_ptr<i2p::garlic::GarlicDestination> destination) { m_LocalDestination = destination; };
  47. void SetExplicitPeers (std::shared_ptr<std::vector<i2p::data::IdentHash> > explicitPeers);
  48. void CreateTunnels ();
  49. void TunnelCreated (std::shared_ptr<InboundTunnel> createdTunnel);
  50. void TunnelExpired (std::shared_ptr<InboundTunnel> expiredTunnel);
  51. void TunnelCreated (std::shared_ptr<OutboundTunnel> createdTunnel);
  52. void TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel);
  53. void RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel);
  54. void RecreateOutboundTunnel (std::shared_ptr<OutboundTunnel> tunnel);
  55. std::vector<std::shared_ptr<InboundTunnel> > GetInboundTunnels (int num) const;
  56. std::shared_ptr<OutboundTunnel> GetNextOutboundTunnel (std::shared_ptr<OutboundTunnel> excluded = nullptr) const;
  57. std::shared_ptr<InboundTunnel> GetNextInboundTunnel (std::shared_ptr<InboundTunnel> excluded = nullptr) const;
  58. std::shared_ptr<OutboundTunnel> GetNewOutboundTunnel (std::shared_ptr<OutboundTunnel> old) const;
  59. void TestTunnels ();
  60. void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
  61. void ProcessDeliveryStatus (std::shared_ptr<I2NPMessage> msg);
  62. bool IsActive () const { return m_IsActive; };
  63. void SetActive (bool isActive) { m_IsActive = isActive; };
  64. void DetachTunnels ();
  65. int GetNumInboundTunnels () const { return m_NumInboundTunnels; };
  66. int GetNumOutboundTunnels () const { return m_NumOutboundTunnels; };
  67. int GetNumInboundHops() const { return m_NumInboundHops; };
  68. int GetNumOutboundHops() const { return m_NumOutboundHops; };
  69. /** i2cp reconfigure */
  70. bool Reconfigure(int inboundHops, int outboundHops, int inboundQuant, int outboundQuant);
  71. void SetCustomPeerSelector(ITunnelPeerSelector * selector);
  72. void UnsetCustomPeerSelector();
  73. bool HasCustomPeerSelector();
  74. /** @brief make this tunnel pool yield tunnels that fit latency range [min, max] */
  75. void RequireLatency(uint64_t min, uint64_t max) { m_MinLatency = min; m_MaxLatency = max; }
  76. /** @brief return true if this tunnel pool has a latency requirement */
  77. bool HasLatencyRequirement() const { return m_MinLatency > 0 && m_MaxLatency > 0; }
  78. /** @brief get the lowest latency tunnel in this tunnel pool regardless of latency requirements */
  79. std::shared_ptr<InboundTunnel> GetLowestLatencyInboundTunnel(std::shared_ptr<InboundTunnel> exclude=nullptr) const;
  80. std::shared_ptr<OutboundTunnel> GetLowestLatencyOutboundTunnel(std::shared_ptr<OutboundTunnel> exclude=nullptr) const;
  81. void OnTunnelBuildResult(std::shared_ptr<Tunnel> tunnel, TunnelBuildResult result);
  82. // for overriding tunnel peer selection
  83. std::shared_ptr<const i2p::data::RouterInfo> SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const;
  84. private:
  85. void CreateInboundTunnel ();
  86. void CreateOutboundTunnel ();
  87. void CreatePairedInboundTunnel (std::shared_ptr<OutboundTunnel> outboundTunnel);
  88. template<class TTunnels>
  89. typename TTunnels::value_type GetNextTunnel (TTunnels& tunnels, typename TTunnels::value_type excluded) const;
  90. bool SelectPeers (std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& hops, bool isInbound);
  91. bool SelectExplicitPeers (std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& hops, bool isInbound);
  92. private:
  93. std::shared_ptr<i2p::garlic::GarlicDestination> m_LocalDestination;
  94. int m_NumInboundHops, m_NumOutboundHops, m_NumInboundTunnels, m_NumOutboundTunnels;
  95. std::shared_ptr<std::vector<i2p::data::IdentHash> > m_ExplicitPeers;
  96. mutable std::mutex m_InboundTunnelsMutex;
  97. std::set<std::shared_ptr<InboundTunnel>, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first
  98. mutable std::mutex m_OutboundTunnelsMutex;
  99. std::set<std::shared_ptr<OutboundTunnel>, TunnelCreationTimeCmp> m_OutboundTunnels;
  100. mutable std::mutex m_TestsMutex;
  101. std::map<uint32_t, std::pair<std::shared_ptr<OutboundTunnel>, std::shared_ptr<InboundTunnel> > > m_Tests;
  102. bool m_IsActive;
  103. std::mutex m_CustomPeerSelectorMutex;
  104. ITunnelPeerSelector * m_CustomPeerSelector;
  105. uint64_t m_MinLatency=0; // if > 0 this tunnel pool will try building tunnels with minimum latency by ms
  106. uint64_t m_MaxLatency=0; // if > 0 this tunnel pool will try building tunnels with maximum latency by ms
  107. public:
  108. // for HTTP only
  109. const decltype(m_OutboundTunnels)& GetOutboundTunnels () const { return m_OutboundTunnels; };
  110. const decltype(m_InboundTunnels)& GetInboundTunnels () const { return m_InboundTunnels; };
  111. };
  112. }
  113. }
  114. #endif