TransitTunnel.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <string.h>
  2. #include "I2PEndian.h"
  3. #include "Log.h"
  4. #include "RouterContext.h"
  5. #include "I2NPProtocol.h"
  6. #include "Tunnel.h"
  7. #include "Transports.h"
  8. #include "TransitTunnel.h"
  9. namespace i2p
  10. {
  11. namespace tunnel
  12. {
  13. TransitTunnel::TransitTunnel (uint32_t receiveTunnelID,
  14. const uint8_t * nextIdent, uint32_t nextTunnelID,
  15. const uint8_t * layerKey,const uint8_t * ivKey):
  16. TunnelBase (receiveTunnelID, nextTunnelID, nextIdent)
  17. {
  18. m_Encryption.SetKeys (layerKey, ivKey);
  19. }
  20. void TransitTunnel::EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out)
  21. {
  22. m_Encryption.Encrypt (in->GetPayload () + 4, out->GetPayload () + 4);
  23. i2p::transport::transports.UpdateTotalTransitTransmittedBytes (TUNNEL_DATA_MSG_SIZE);
  24. }
  25. TransitTunnelParticipant::~TransitTunnelParticipant ()
  26. {
  27. }
  28. void TransitTunnelParticipant::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
  29. {
  30. auto newMsg = CreateEmptyTunnelDataMsg ();
  31. EncryptTunnelMsg (tunnelMsg, newMsg);
  32. m_NumTransmittedBytes += tunnelMsg->GetLength ();
  33. htobe32buf (newMsg->GetPayload (), GetNextTunnelID ());
  34. newMsg->FillI2NPMessageHeader (eI2NPTunnelData);
  35. m_TunnelDataMsgs.push_back (newMsg);
  36. }
  37. void TransitTunnelParticipant::FlushTunnelDataMsgs ()
  38. {
  39. if (!m_TunnelDataMsgs.empty ())
  40. {
  41. auto num = m_TunnelDataMsgs.size ();
  42. if (num > 1)
  43. LogPrint (eLogDebug, "TransitTunnel: ", GetTunnelID (), "->", GetNextTunnelID (), " ", num);
  44. i2p::transport::transports.SendMessages (GetNextIdentHash (), m_TunnelDataMsgs);
  45. m_TunnelDataMsgs.clear ();
  46. }
  47. }
  48. void TransitTunnel::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
  49. {
  50. LogPrint (eLogError, "TransitTunnel: We are not a gateway for ", GetTunnelID ());
  51. }
  52. void TransitTunnel::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
  53. {
  54. LogPrint (eLogError, "TransitTunnel: Incoming tunnel message is not supported ", GetTunnelID ());
  55. }
  56. void TransitTunnelGateway::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
  57. {
  58. TunnelMessageBlock block;
  59. block.deliveryType = eDeliveryTypeLocal;
  60. block.data = msg;
  61. std::unique_lock<std::mutex> l(m_SendMutex);
  62. m_Gateway.PutTunnelDataMsg (block);
  63. }
  64. void TransitTunnelGateway::FlushTunnelDataMsgs ()
  65. {
  66. std::unique_lock<std::mutex> l(m_SendMutex);
  67. m_Gateway.SendBuffer ();
  68. }
  69. void TransitTunnelEndpoint::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
  70. {
  71. auto newMsg = CreateEmptyTunnelDataMsg ();
  72. EncryptTunnelMsg (tunnelMsg, newMsg);
  73. LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ());
  74. m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
  75. }
  76. std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
  77. const uint8_t * nextIdent, uint32_t nextTunnelID,
  78. const uint8_t * layerKey,const uint8_t * ivKey,
  79. bool isGateway, bool isEndpoint)
  80. {
  81. if (isEndpoint)
  82. {
  83. LogPrint (eLogDebug, "TransitTunnel: endpoint ", receiveTunnelID, " created");
  84. return std::make_shared<TransitTunnelEndpoint> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
  85. }
  86. else if (isGateway)
  87. {
  88. LogPrint (eLogInfo, "TransitTunnel: gateway ", receiveTunnelID, " created");
  89. return std::make_shared<TransitTunnelGateway> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
  90. }
  91. else
  92. {
  93. LogPrint (eLogDebug, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
  94. return std::make_shared<TransitTunnelParticipant> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
  95. }
  96. }
  97. }
  98. }