Path.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_PATH_HPP
  14. #define ZT_PATH_HPP
  15. #include "Constants.hpp"
  16. #include "InetAddress.hpp"
  17. #include "SharedPtr.hpp"
  18. #include "Utils.hpp"
  19. #include "Mutex.hpp"
  20. #include "Meter.hpp"
  21. #include <cstdint>
  22. #include <cstring>
  23. #include <cstdlib>
  24. #include <stdexcept>
  25. #include <algorithm>
  26. #include <set>
  27. namespace ZeroTier {
  28. class RuntimeEnvironment;
  29. template<unsigned int MF,unsigned int GCT,unsigned int GCS>
  30. class Defragmenter;
  31. /**
  32. * A path across the physical network
  33. */
  34. class Path
  35. {
  36. friend class SharedPtr<Path>;
  37. // Allow defragmenter to access fragment in flight info stored in Path for performance reasons.
  38. template<unsigned int MF,unsigned int GCT,unsigned int GCS>
  39. friend class Defragmenter;
  40. public:
  41. ZT_INLINE Path(const int64_t l,const InetAddress &r) noexcept :
  42. _localSocket(l),
  43. _lastIn(0),
  44. _lastOut(0),
  45. _addr(r)
  46. {
  47. }
  48. /**
  49. * Send a packet via this path (last out time is also updated)
  50. *
  51. * @param RR Runtime environment
  52. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  53. * @param data Packet data
  54. * @param len Packet length
  55. * @param now Current time
  56. * @return True if transport reported success
  57. */
  58. bool send(const RuntimeEnvironment *RR,void *tPtr,const void *data,unsigned int len,int64_t now) noexcept;
  59. /**
  60. * Explicitly update last sent time
  61. *
  62. * @param now Time of send
  63. * @param bytes Bytes sent
  64. */
  65. ZT_INLINE void sent(const int64_t now,const unsigned int bytes) noexcept
  66. {
  67. _lastOut.store(now);
  68. _outMeter.log(now,bytes);
  69. }
  70. /**
  71. * Called when a packet is received from this remote path, regardless of content
  72. *
  73. * @param now Time of receive
  74. * @param bytes Bytes received
  75. */
  76. ZT_INLINE void received(const int64_t now,const unsigned int bytes) noexcept
  77. {
  78. _lastIn.store(now);
  79. _inMeter.log(now,bytes);
  80. }
  81. /**
  82. * Check path aliveness
  83. *
  84. * @param now Current time
  85. */
  86. ZT_INLINE bool alive(const int64_t now) const noexcept { return ((now - _lastIn.load()) < ZT_PATH_ALIVE_TIMEOUT); }
  87. /**
  88. * @return Physical address
  89. */
  90. ZT_INLINE const InetAddress &address() const noexcept { return _addr; }
  91. /**
  92. * @return Local socket as specified by external code
  93. */
  94. ZT_INLINE int64_t localSocket() const noexcept { return _localSocket; }
  95. /**
  96. * @return Last time we received anything
  97. */
  98. ZT_INLINE int64_t lastIn() const noexcept { return _lastIn.load(); }
  99. /**
  100. * @return Last time we sent something
  101. */
  102. ZT_INLINE int64_t lastOut() const noexcept { return _lastOut.load(); }
  103. private:
  104. const int64_t _localSocket;
  105. std::atomic<int64_t> _lastIn;
  106. std::atomic<int64_t> _lastOut;
  107. const InetAddress _addr;
  108. Meter<> _inMeter;
  109. Meter<> _outMeter;
  110. // These fields belong to Defragmenter but are kept in Path for performance
  111. // as it's much faster this way than having Defragmenter maintain another
  112. // mapping from paths to inbound message IDs.
  113. std::set<uint64_t> _inboundFragmentedMessages;
  114. Mutex _inboundFragmentedMessages_l;
  115. std::atomic<int> __refCount;
  116. };
  117. } // namespace ZeroTier
  118. #endif