TunnelBase.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef TUNNEL_BASE_H__
  2. #define TUNNEL_BASE_H__
  3. #include <inttypes.h>
  4. #include <memory>
  5. #include "Timestamp.h"
  6. #include "I2NPProtocol.h"
  7. #include "Identity.h"
  8. namespace i2p
  9. {
  10. namespace tunnel
  11. {
  12. const size_t TUNNEL_DATA_MSG_SIZE = 1028;
  13. const size_t TUNNEL_DATA_ENCRYPTED_SIZE = 1008;
  14. const size_t TUNNEL_DATA_MAX_PAYLOAD_SIZE = 1003;
  15. enum TunnelDeliveryType
  16. {
  17. eDeliveryTypeLocal = 0,
  18. eDeliveryTypeTunnel = 1,
  19. eDeliveryTypeRouter = 2
  20. };
  21. struct TunnelMessageBlock
  22. {
  23. TunnelDeliveryType deliveryType;
  24. i2p::data::IdentHash hash;
  25. uint32_t tunnelID;
  26. std::shared_ptr<I2NPMessage> data;
  27. };
  28. class TunnelBase
  29. {
  30. public:
  31. TunnelBase (uint32_t tunnelID, uint32_t nextTunnelID, i2p::data::IdentHash nextIdent):
  32. m_TunnelID (tunnelID), m_NextTunnelID (nextTunnelID), m_NextIdent (nextIdent),
  33. m_CreationTime (i2p::util::GetSecondsSinceEpoch ()) {};
  34. virtual ~TunnelBase () {};
  35. virtual void Cleanup () {};
  36. virtual void HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg) = 0;
  37. virtual void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg) = 0;
  38. virtual void FlushTunnelDataMsgs () {};
  39. virtual void EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out) = 0;
  40. uint32_t GetNextTunnelID () const { return m_NextTunnelID; };
  41. const i2p::data::IdentHash& GetNextIdentHash () const { return m_NextIdent; };
  42. virtual uint32_t GetTunnelID () const { return m_TunnelID; }; // as known at our side
  43. uint32_t GetCreationTime () const { return m_CreationTime; };
  44. void SetCreationTime (uint32_t t) { m_CreationTime = t; };
  45. private:
  46. uint32_t m_TunnelID, m_NextTunnelID;
  47. i2p::data::IdentHash m_NextIdent;
  48. uint32_t m_CreationTime; // seconds since epoch
  49. };
  50. struct TunnelCreationTimeCmp
  51. {
  52. template<typename T>
  53. bool operator() (const std::shared_ptr<T> & t1, const std::shared_ptr<T> & t2) const
  54. {
  55. if (t1->GetCreationTime () != t2->GetCreationTime ())
  56. return t1->GetCreationTime () > t2->GetCreationTime ();
  57. else
  58. return t1 < t2;
  59. }
  60. };
  61. }
  62. }
  63. #endif