Path.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <cstdint>
  16. #include <cstring>
  17. #include <cstdlib>
  18. #include <stdexcept>
  19. #include <algorithm>
  20. #include <set>
  21. #include "Constants.hpp"
  22. #include "InetAddress.hpp"
  23. #include "SharedPtr.hpp"
  24. #include "Utils.hpp"
  25. #include "Mutex.hpp"
  26. namespace ZeroTier {
  27. class RuntimeEnvironment;
  28. template<unsigned int MF,unsigned int GCT,unsigned int GCS>
  29. class Defragmenter;
  30. /**
  31. * A path across the physical network
  32. */
  33. class Path
  34. {
  35. friend class SharedPtr<Path>;
  36. // Allow defragmenter to access fragment in flight info stored in Path for performance reasons.
  37. template<unsigned int MF,unsigned int GCT,unsigned int GCS>
  38. friend class Defragmenter;
  39. public:
  40. ZT_ALWAYS_INLINE Path(const int64_t l,const InetAddress &r) noexcept :
  41. _localSocket(l),
  42. _lastIn(0),
  43. _lastOut(0),
  44. _addr(r)
  45. {
  46. }
  47. /**
  48. * Send a packet via this path (last out time is also updated)
  49. *
  50. * @param RR Runtime environment
  51. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  52. * @param data Packet data
  53. * @param len Packet length
  54. * @param now Current time
  55. * @return True if transport reported success
  56. */
  57. bool send(const RuntimeEnvironment *RR,void *tPtr,const void *data,unsigned int len,int64_t now) noexcept;
  58. /**
  59. * Explicitly update last sent time
  60. *
  61. * @param t Time of send
  62. */
  63. ZT_ALWAYS_INLINE void sent(const int64_t t) noexcept { _lastOut = t; }
  64. /**
  65. * Called when a packet is received from this remote path, regardless of content
  66. *
  67. * @param t Time of receive
  68. */
  69. ZT_ALWAYS_INLINE void received(const int64_t t) noexcept { _lastIn = t; }
  70. /**
  71. * Check path aliveness
  72. *
  73. * @param now Current time
  74. */
  75. ZT_ALWAYS_INLINE bool alive(const int64_t now) const noexcept { return ((now - _lastIn) < ZT_PATH_ALIVE_TIMEOUT); }
  76. /**
  77. * Check if path is considered active
  78. *
  79. * @param now Current time
  80. */
  81. ZT_ALWAYS_INLINE bool active(const int64_t now) const noexcept { return ((now - _lastIn) < ZT_PATH_ACTIVITY_TIMEOUT); }
  82. /**
  83. * @return Physical address
  84. */
  85. ZT_ALWAYS_INLINE const InetAddress &address() const noexcept { return _addr; }
  86. /**
  87. * @return Local socket as specified by external code
  88. */
  89. ZT_ALWAYS_INLINE int64_t localSocket() const noexcept { return _localSocket; }
  90. /**
  91. * @return Last time we received anything
  92. */
  93. ZT_ALWAYS_INLINE int64_t lastIn() const noexcept { return _lastIn; }
  94. /**
  95. * @return Last time we sent something
  96. */
  97. ZT_ALWAYS_INLINE int64_t lastOut() const noexcept { return _lastOut; }
  98. private:
  99. int64_t _localSocket;
  100. int64_t _lastIn;
  101. int64_t _lastOut;
  102. InetAddress _addr;
  103. // These fields belong to Defragmenter but are kept in Path for performance
  104. // as it's much faster this way than having Defragmenter maintain another
  105. // mapping from paths to inbound message IDs.
  106. std::set<uint64_t> _inboundFragmentedMessages;
  107. Mutex _inboundFragmentedMessages_l;
  108. std::atomic<int> __refCount;
  109. };
  110. } // namespace ZeroTier
  111. #endif