TransportSession.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef TRANSPORT_SESSION_H__
  2. #define TRANSPORT_SESSION_H__
  3. #include <inttypes.h>
  4. #include <iostream>
  5. #include <memory>
  6. #include <vector>
  7. #include "Identity.h"
  8. #include "Crypto.h"
  9. #include "RouterInfo.h"
  10. #include "I2NPProtocol.h"
  11. #include "Timestamp.h"
  12. namespace i2p
  13. {
  14. namespace transport
  15. {
  16. class SignedData
  17. {
  18. public:
  19. SignedData () {}
  20. SignedData (const SignedData& other)
  21. {
  22. m_Stream << other.m_Stream.rdbuf ();
  23. }
  24. void Insert (const uint8_t * buf, size_t len)
  25. {
  26. m_Stream.write ((char *)buf, len);
  27. }
  28. template<typename T>
  29. void Insert (T t)
  30. {
  31. m_Stream.write ((char *)&t, sizeof (T));
  32. }
  33. bool Verify (std::shared_ptr<const i2p::data::IdentityEx> ident, const uint8_t * signature) const
  34. {
  35. return ident->Verify ((const uint8_t *)m_Stream.str ().c_str (), m_Stream.str ().size (), signature);
  36. }
  37. void Sign (const i2p::data::PrivateKeys& keys, uint8_t * signature) const
  38. {
  39. keys.Sign ((const uint8_t *)m_Stream.str ().c_str (), m_Stream.str ().size (), signature);
  40. }
  41. private:
  42. std::stringstream m_Stream;
  43. };
  44. class TransportSession
  45. {
  46. public:
  47. TransportSession (std::shared_ptr<const i2p::data::RouterInfo> router, int terminationTimeout):
  48. m_DHKeysPair (nullptr), m_NumSentBytes (0), m_NumReceivedBytes (0), m_IsOutgoing (router), m_TerminationTimeout (terminationTimeout),
  49. m_LastActivityTimestamp (i2p::util::GetSecondsSinceEpoch ())
  50. {
  51. if (router)
  52. m_RemoteIdentity = router->GetRouterIdentity ();
  53. }
  54. virtual ~TransportSession () {};
  55. virtual void Done () = 0;
  56. std::string GetIdentHashBase64() const { return m_RemoteIdentity ? m_RemoteIdentity->GetIdentHash().ToBase64() : ""; }
  57. std::shared_ptr<const i2p::data::IdentityEx> GetRemoteIdentity () { return m_RemoteIdentity; };
  58. void SetRemoteIdentity (std::shared_ptr<const i2p::data::IdentityEx> ident) { m_RemoteIdentity = ident; };
  59. size_t GetNumSentBytes () const { return m_NumSentBytes; };
  60. size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
  61. bool IsOutgoing () const { return m_IsOutgoing; };
  62. int GetTerminationTimeout () const { return m_TerminationTimeout; };
  63. void SetTerminationTimeout (int terminationTimeout) { m_TerminationTimeout = terminationTimeout; };
  64. bool IsTerminationTimeoutExpired (uint64_t ts) const
  65. { return ts >= m_LastActivityTimestamp + GetTerminationTimeout (); };
  66. virtual void SendLocalRouterInfo () { SendI2NPMessages ({ CreateDatabaseStoreMsg () }); };
  67. virtual void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs) = 0;
  68. protected:
  69. std::shared_ptr<const i2p::data::IdentityEx> m_RemoteIdentity;
  70. std::shared_ptr<i2p::crypto::DHKeys> m_DHKeysPair; // X - for client and Y - for server
  71. size_t m_NumSentBytes, m_NumReceivedBytes;
  72. bool m_IsOutgoing;
  73. int m_TerminationTimeout;
  74. uint64_t m_LastActivityTimestamp;
  75. };
  76. }
  77. }
  78. #endif