SSUData.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef SSU_DATA_H__
  2. #define SSU_DATA_H__
  3. #include <inttypes.h>
  4. #include <string.h>
  5. #include <map>
  6. #include <vector>
  7. #include <unordered_set>
  8. #include <memory>
  9. #include <boost/asio.hpp>
  10. #include "I2NPProtocol.h"
  11. #include "Identity.h"
  12. #include "RouterInfo.h"
  13. namespace i2p
  14. {
  15. namespace transport
  16. {
  17. const size_t SSU_MTU_V4 = 1484;
  18. #ifdef MESHNET
  19. const size_t SSU_MTU_V6 = 1286;
  20. #else
  21. const size_t SSU_MTU_V6 = 1488;
  22. #endif
  23. const size_t IPV4_HEADER_SIZE = 20;
  24. const size_t IPV6_HEADER_SIZE = 40;
  25. const size_t UDP_HEADER_SIZE = 8;
  26. const size_t SSU_V4_MAX_PACKET_SIZE = SSU_MTU_V4 - IPV4_HEADER_SIZE - UDP_HEADER_SIZE; // 1456
  27. const size_t SSU_V6_MAX_PACKET_SIZE = SSU_MTU_V6 - IPV6_HEADER_SIZE - UDP_HEADER_SIZE; // 1440
  28. const int RESEND_INTERVAL = 3; // in seconds
  29. const int MAX_NUM_RESENDS = 5;
  30. const int DECAY_INTERVAL = 20; // in seconds
  31. const int INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT = 30; // in seconds
  32. const unsigned int MAX_NUM_RECEIVED_MESSAGES = 1000; // how many msgID we store for duplicates check
  33. const int MAX_OUTGOING_WINDOW_SIZE = 200; // how many unacked message we can store
  34. // data flags
  35. const uint8_t DATA_FLAG_EXTENDED_DATA_INCLUDED = 0x02;
  36. const uint8_t DATA_FLAG_WANT_REPLY = 0x04;
  37. const uint8_t DATA_FLAG_REQUEST_PREVIOUS_ACKS = 0x08;
  38. const uint8_t DATA_FLAG_EXPLICIT_CONGESTION_NOTIFICATION = 0x10;
  39. const uint8_t DATA_FLAG_ACK_BITFIELDS_INCLUDED = 0x40;
  40. const uint8_t DATA_FLAG_EXPLICIT_ACKS_INCLUDED = 0x80;
  41. struct Fragment
  42. {
  43. int fragmentNum;
  44. size_t len;
  45. bool isLast;
  46. uint8_t buf[SSU_V4_MAX_PACKET_SIZE + 18]; // use biggest
  47. Fragment () = default;
  48. Fragment (int n, const uint8_t * b, int l, bool last):
  49. fragmentNum (n), len (l), isLast (last) { memcpy (buf, b, len); };
  50. };
  51. struct FragmentCmp
  52. {
  53. bool operator() (const std::unique_ptr<Fragment>& f1, const std::unique_ptr<Fragment>& f2) const
  54. {
  55. return f1->fragmentNum < f2->fragmentNum;
  56. };
  57. };
  58. struct IncompleteMessage
  59. {
  60. std::shared_ptr<I2NPMessage> msg;
  61. int nextFragmentNum;
  62. uint32_t lastFragmentInsertTime; // in seconds
  63. std::set<std::unique_ptr<Fragment>, FragmentCmp> savedFragments;
  64. IncompleteMessage (std::shared_ptr<I2NPMessage> m): msg (m), nextFragmentNum (0), lastFragmentInsertTime (0) {};
  65. void AttachNextFragment (const uint8_t * fragment, size_t fragmentSize);
  66. };
  67. struct SentMessage
  68. {
  69. std::vector<std::unique_ptr<Fragment> > fragments;
  70. uint32_t nextResendTime; // in seconds
  71. int numResends;
  72. };
  73. class SSUSession;
  74. class SSUData
  75. {
  76. public:
  77. SSUData (SSUSession& session);
  78. ~SSUData ();
  79. void Start ();
  80. void Stop ();
  81. void ProcessMessage (uint8_t * buf, size_t len);
  82. void FlushReceivedMessage ();
  83. void Send (std::shared_ptr<i2p::I2NPMessage> msg);
  84. void AdjustPacketSize (std::shared_ptr<const i2p::data::RouterInfo> remoteRouter);
  85. void UpdatePacketSize (const i2p::data::IdentHash& remoteIdent);
  86. private:
  87. void SendMsgAck (uint32_t msgID);
  88. void SendFragmentAck (uint32_t msgID, int fragmentNum);
  89. void ProcessAcks (uint8_t *& buf, uint8_t flag);
  90. void ProcessFragments (uint8_t * buf);
  91. void ProcessSentMessageAck (uint32_t msgID);
  92. void ScheduleResend ();
  93. void HandleResendTimer (const boost::system::error_code& ecode);
  94. void ScheduleIncompleteMessagesCleanup ();
  95. void HandleIncompleteMessagesCleanupTimer (const boost::system::error_code& ecode);
  96. private:
  97. SSUSession& m_Session;
  98. std::map<uint32_t, std::unique_ptr<IncompleteMessage> > m_IncompleteMessages;
  99. std::map<uint32_t, std::unique_ptr<SentMessage> > m_SentMessages;
  100. std::unordered_set<uint32_t> m_ReceivedMessages;
  101. boost::asio::deadline_timer m_ResendTimer, m_IncompleteMessagesCleanupTimer;
  102. int m_MaxPacketSize, m_PacketSize;
  103. i2p::I2NPMessagesHandler m_Handler;
  104. uint32_t m_LastMessageReceivedTime; // in second
  105. };
  106. }
  107. }
  108. #endif