RouterContext.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef ROUTER_CONTEXT_H__
  2. #define ROUTER_CONTEXT_H__
  3. #include <inttypes.h>
  4. #include <string>
  5. #include <memory>
  6. #include <mutex>
  7. #include <chrono>
  8. #include <boost/asio.hpp>
  9. #include "Identity.h"
  10. #include "RouterInfo.h"
  11. #include "Garlic.h"
  12. namespace i2p
  13. {
  14. const char ROUTER_INFO[] = "router.info";
  15. const char ROUTER_KEYS[] = "router.keys";
  16. const char NTCP2_KEYS[] = "ntcp2.keys";
  17. const int ROUTER_INFO_UPDATE_INTERVAL = 1800; // 30 minutes
  18. enum RouterStatus
  19. {
  20. eRouterStatusOK = 0,
  21. eRouterStatusTesting = 1,
  22. eRouterStatusFirewalled = 2,
  23. eRouterStatusError = 3
  24. };
  25. enum RouterError
  26. {
  27. eRouterErrorNone = 0,
  28. eRouterErrorClockSkew = 1
  29. };
  30. class RouterContext: public i2p::garlic::GarlicDestination
  31. {
  32. private:
  33. struct NTCP2PrivateKeys
  34. {
  35. uint8_t staticPublicKey[32];
  36. uint8_t staticPrivateKey[32];
  37. uint8_t iv[16];
  38. };
  39. public:
  40. RouterContext ();
  41. void Init ();
  42. const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; };
  43. i2p::data::RouterInfo& GetRouterInfo () { return m_RouterInfo; };
  44. std::shared_ptr<const i2p::data::RouterInfo> GetSharedRouterInfo () const
  45. {
  46. return std::shared_ptr<const i2p::data::RouterInfo> (&m_RouterInfo,
  47. [](const i2p::data::RouterInfo *) {});
  48. }
  49. std::shared_ptr<i2p::garlic::GarlicDestination> GetSharedDestination ()
  50. {
  51. return std::shared_ptr<i2p::garlic::GarlicDestination> (this,
  52. [](i2p::garlic::GarlicDestination *) {});
  53. }
  54. const uint8_t * GetNTCP2StaticPublicKey () const { return m_NTCP2Keys ? m_NTCP2Keys->staticPublicKey : nullptr; };
  55. const uint8_t * GetNTCP2StaticPrivateKey () const { return m_NTCP2Keys ? m_NTCP2Keys->staticPrivateKey : nullptr; };
  56. const uint8_t * GetNTCP2IV () const { return m_NTCP2Keys ? m_NTCP2Keys->iv : nullptr; };
  57. i2p::crypto::X25519Keys& GetStaticKeys ();
  58. uint32_t GetUptime () const; // in seconds
  59. uint64_t GetLastUpdateTime () const { return m_LastUpdateTime; };
  60. uint64_t GetBandwidthLimit () const { return m_BandwidthLimit; };
  61. uint64_t GetTransitBandwidthLimit () const { return (m_BandwidthLimit*m_ShareRatio)/100LL; };
  62. RouterStatus GetStatus () const { return m_Status; };
  63. void SetStatus (RouterStatus status);
  64. RouterError GetError () const { return m_Error; };
  65. void SetError (RouterError error) { m_Status = eRouterStatusError; m_Error = error; };
  66. int GetNetID () const { return m_NetID; };
  67. void SetNetID (int netID) { m_NetID = netID; };
  68. bool DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const;
  69. void UpdatePort (int port); // called from Daemon
  70. void UpdateAddress (const boost::asio::ip::address& host); // called from SSU or Daemon
  71. void PublishNTCP2Address (int port, bool publish = true, bool v4only = false);
  72. void UpdateNTCP2Address (bool enable);
  73. void PublishNTCPAddress (bool publish, bool v4only = true);
  74. bool AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer);
  75. void RemoveIntroducer (const boost::asio::ip::udp::endpoint& e);
  76. bool IsUnreachable () const;
  77. void SetUnreachable ();
  78. void SetReachable ();
  79. bool IsFloodfill () const { return m_IsFloodfill; };
  80. void SetFloodfill (bool floodfill);
  81. void SetFamily (const std::string& family);
  82. std::string GetFamily () const;
  83. void SetBandwidth (int limit); /* in kilobytes */
  84. void SetBandwidth (char L); /* by letter */
  85. void SetShareRatio (int percents); // 0 - 100
  86. bool AcceptsTunnels () const { return m_AcceptsTunnels; };
  87. void SetAcceptsTunnels (bool acceptsTunnels) { m_AcceptsTunnels = acceptsTunnels; };
  88. bool SupportsV6 () const { return m_RouterInfo.IsV6 (); };
  89. bool SupportsV4 () const { return m_RouterInfo.IsV4 (); };
  90. void SetSupportsV6 (bool supportsV6);
  91. void SetSupportsV4 (bool supportsV4);
  92. void UpdateNTCP2V6Address (const boost::asio::ip::address& host); // called from Daemon. TODO: remove
  93. void UpdateStats ();
  94. void UpdateTimestamp (uint64_t ts); // in seconds, called from NetDb before publishing
  95. void CleanupDestination (); // garlic destination
  96. // implements LocalDestination
  97. std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Keys.GetPublic (); };
  98. bool Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const;
  99. void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); };
  100. void SetLeaseSetUpdated () {};
  101. // implements GarlicDestination
  102. std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSet () { return nullptr; };
  103. std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const;
  104. void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
  105. // override GarlicDestination
  106. void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
  107. void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
  108. private:
  109. void CreateNewRouter ();
  110. void NewRouterInfo ();
  111. void UpdateRouterInfo ();
  112. void NewNTCP2Keys ();
  113. bool Load ();
  114. void SaveKeys ();
  115. private:
  116. i2p::data::RouterInfo m_RouterInfo;
  117. i2p::data::PrivateKeys m_Keys;
  118. std::shared_ptr<i2p::crypto::CryptoKeyDecryptor> m_Decryptor;
  119. uint64_t m_LastUpdateTime; // in seconds
  120. bool m_AcceptsTunnels, m_IsFloodfill;
  121. std::chrono::time_point<std::chrono::steady_clock> m_StartupTime;
  122. uint64_t m_BandwidthLimit; // allowed bandwidth
  123. int m_ShareRatio;
  124. RouterStatus m_Status;
  125. RouterError m_Error;
  126. int m_NetID;
  127. std::mutex m_GarlicMutex;
  128. std::unique_ptr<NTCP2PrivateKeys> m_NTCP2Keys;
  129. std::unique_ptr<i2p::crypto::X25519Keys> m_StaticKeys;
  130. };
  131. extern RouterContext context;
  132. }
  133. #endif