Datagram.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef DATAGRAM_H__
  2. #define DATAGRAM_H__
  3. #include <inttypes.h>
  4. #include <memory>
  5. #include <functional>
  6. #include <map>
  7. #include "Base.h"
  8. #include "Identity.h"
  9. #include "LeaseSet.h"
  10. #include "I2NPProtocol.h"
  11. #include "Garlic.h"
  12. namespace i2p
  13. {
  14. namespace client
  15. {
  16. class ClientDestination;
  17. }
  18. namespace datagram
  19. {
  20. // milliseconds for max session idle time
  21. const uint64_t DATAGRAM_SESSION_MAX_IDLE = 10 * 60 * 1000;
  22. // milliseconds for how long we try sticking to a dead routing path before trying to switch
  23. const uint64_t DATAGRAM_SESSION_PATH_TIMEOUT = 10 * 1000;
  24. // milliseconds interval a routing path is used before switching
  25. const uint64_t DATAGRAM_SESSION_PATH_SWITCH_INTERVAL = 20 * 60 * 1000;
  26. // milliseconds before lease expire should we try switching leases
  27. const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW = 30 * 1000;
  28. // milliseconds fudge factor for leases handover
  29. const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE = 1000;
  30. // milliseconds minimum time between path switches
  31. const uint64_t DATAGRAM_SESSION_PATH_MIN_LIFETIME = 5 * 1000;
  32. // max 64 messages buffered in send queue for each datagram session
  33. const size_t DATAGRAM_SEND_QUEUE_MAX_SIZE = 64;
  34. class DatagramSession : public std::enable_shared_from_this<DatagramSession>
  35. {
  36. public:
  37. DatagramSession(std::shared_ptr<i2p::client::ClientDestination> localDestination, const i2p::data::IdentHash & remoteIdent);
  38. void Start ();
  39. void Stop ();
  40. /** @brief ack the garlic routing path */
  41. void Ack();
  42. /** send an i2np message to remote endpoint for this session */
  43. void SendMsg(std::shared_ptr<I2NPMessage> msg);
  44. /** get the last time in milliseconds for when we used this datagram session */
  45. uint64_t LastActivity() const { return m_LastUse; }
  46. struct Info
  47. {
  48. std::shared_ptr<const i2p::data::IdentHash> IBGW;
  49. std::shared_ptr<const i2p::data::IdentHash> OBEP;
  50. const uint64_t activity;
  51. Info() : IBGW(nullptr), OBEP(nullptr), activity(0) {}
  52. Info(const uint8_t * ibgw, const uint8_t * obep, const uint64_t a) :
  53. activity(a) {
  54. if(ibgw) IBGW = std::make_shared<i2p::data::IdentHash>(ibgw);
  55. else IBGW = nullptr;
  56. if(obep) OBEP = std::make_shared<i2p::data::IdentHash>(obep);
  57. else OBEP = nullptr;
  58. }
  59. };
  60. Info GetSessionInfo() const;
  61. private:
  62. void FlushSendQueue();
  63. void ScheduleFlushSendQueue();
  64. void HandleSend(std::shared_ptr<I2NPMessage> msg);
  65. std::shared_ptr<i2p::garlic::GarlicRoutingPath> GetSharedRoutingPath();
  66. void HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls);
  67. private:
  68. std::shared_ptr<i2p::client::ClientDestination> m_LocalDestination;
  69. i2p::data::IdentHash m_RemoteIdent;
  70. std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
  71. std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
  72. std::shared_ptr<const i2p::data::Lease> m_CurrentRemoteLease;
  73. std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
  74. boost::asio::deadline_timer m_SendQueueTimer;
  75. std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue;
  76. uint64_t m_LastUse;
  77. bool m_RequestingLS;
  78. };
  79. typedef std::shared_ptr<DatagramSession> DatagramSession_ptr;
  80. const size_t MAX_DATAGRAM_SIZE = 32768;
  81. class DatagramDestination
  82. {
  83. typedef std::function<void (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)> Receiver;
  84. typedef std::function<void (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)> RawReceiver;
  85. public:
  86. DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner);
  87. ~DatagramDestination ();
  88. void SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash & ident, uint16_t fromPort = 0, uint16_t toPort = 0);
  89. void SendRawDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash & ident, uint16_t fromPort = 0, uint16_t toPort = 0);
  90. void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len, bool isRaw = false);
  91. void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
  92. void ResetReceiver () { m_Receiver = nullptr; };
  93. void SetReceiver (const Receiver& receiver, uint16_t port) { std::lock_guard<std::mutex> lock(m_ReceiversMutex); m_ReceiversByPorts[port] = receiver; };
  94. void ResetReceiver (uint16_t port) { std::lock_guard<std::mutex> lock(m_ReceiversMutex); m_ReceiversByPorts.erase (port); };
  95. void SetRawReceiver (const RawReceiver& receiver) { m_RawReceiver = receiver; };
  96. void ResetRawReceiver () { m_RawReceiver = nullptr; };
  97. std::shared_ptr<DatagramSession::Info> GetInfoForRemote(const i2p::data::IdentHash & remote);
  98. // clean up stale sessions
  99. void CleanUp ();
  100. private:
  101. std::shared_ptr<DatagramSession> ObtainSession(const i2p::data::IdentHash & ident);
  102. std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort, bool isRaw = false);
  103. void HandleDatagram (uint16_t fromPort, uint16_t toPort, uint8_t *const& buf, size_t len);
  104. void HandleRawDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
  105. /** find a receiver by port, if none by port is found try default receiever, otherwise returns nullptr */
  106. Receiver FindReceiver(uint16_t port);
  107. private:
  108. std::shared_ptr<i2p::client::ClientDestination> m_Owner;
  109. Receiver m_Receiver; // default
  110. RawReceiver m_RawReceiver; // default
  111. std::mutex m_SessionsMutex;
  112. std::map<i2p::data::IdentHash, DatagramSession_ptr > m_Sessions;
  113. std::mutex m_ReceiversMutex;
  114. std::map<uint16_t, Receiver> m_ReceiversByPorts;
  115. i2p::data::GzipInflator m_Inflator;
  116. i2p::data::GzipDeflator m_Deflator;
  117. };
  118. }
  119. }
  120. #endif