RemotePath.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_REMOTEPATH_HPP
  28. #define ZT_REMOTEPATH_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdexcept>
  32. #include <algorithm>
  33. #include "Path.hpp"
  34. #include "Node.hpp"
  35. #include "AntiRecursion.hpp"
  36. #include "RuntimeEnvironment.hpp"
  37. #define ZT_REMOTEPATH_FLAG_FIXED 0x0001
  38. namespace ZeroTier {
  39. /**
  40. * Path to a remote peer
  41. *
  42. * This extends Path to include status information about path activity.
  43. */
  44. class RemotePath : public Path
  45. {
  46. public:
  47. RemotePath() :
  48. Path(),
  49. _lastSend(0),
  50. _lastReceived(0),
  51. _localAddress(),
  52. _flags(0) {}
  53. RemotePath(const InetAddress &localAddress,const InetAddress &addr,bool fixed) :
  54. Path(addr,0,TRUST_NORMAL),
  55. _lastSend(0),
  56. _lastReceived(0),
  57. _localAddress(localAddress),
  58. _flags(fixed ? ZT_REMOTEPATH_FLAG_FIXED : 0) {}
  59. inline const InetAddress &localAddress() const throw() { return _localAddress; }
  60. inline uint64_t lastSend() const throw() { return _lastSend; }
  61. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  62. /**
  63. * @return Is this a fixed path?
  64. */
  65. inline bool fixed() const throw() { return ((_flags & ZT_REMOTEPATH_FLAG_FIXED) != 0); }
  66. /**
  67. * @param f New value of fixed flag
  68. */
  69. inline void setFixed(const bool f)
  70. throw()
  71. {
  72. if (f)
  73. _flags |= ZT_REMOTEPATH_FLAG_FIXED;
  74. else _flags &= ~ZT_REMOTEPATH_FLAG_FIXED;
  75. }
  76. /**
  77. * Called when a packet is sent to this remote path
  78. *
  79. * This is called automatically by RemotePath::send().
  80. *
  81. * @param t Time of send
  82. */
  83. inline void sent(uint64_t t)
  84. throw()
  85. {
  86. _lastSend = t;
  87. }
  88. /**
  89. * Called when a packet is received from this remote path
  90. *
  91. * @param t Time of receive
  92. */
  93. inline void received(uint64_t t)
  94. throw()
  95. {
  96. _lastReceived = t;
  97. }
  98. /**
  99. * @param now Current time
  100. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  101. */
  102. inline bool active(uint64_t now) const
  103. throw()
  104. {
  105. return ( ((_flags & ZT_REMOTEPATH_FLAG_FIXED) != 0) || ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT) );
  106. }
  107. /**
  108. * Send a packet via this path
  109. *
  110. * @param RR Runtime environment
  111. * @param data Packet data
  112. * @param len Packet length
  113. * @param now Current time
  114. * @return True if transport reported success
  115. */
  116. inline bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now)
  117. {
  118. if (RR->node->putPacket(_localAddress,address(),data,len)) {
  119. sent(now);
  120. RR->antiRec->logOutgoingZT(data,len);
  121. return true;
  122. }
  123. return false;
  124. }
  125. template<unsigned int C>
  126. inline void serialize(Buffer<C> &b) const
  127. {
  128. b.append((uint8_t)1); // version
  129. _addr.serialize(b);
  130. b.append((uint8_t)_trust);
  131. b.append((uint64_t)_lastSend);
  132. b.append((uint64_t)_lastReceived);
  133. _localAddress.serialize(b);
  134. b.append((uint16_t)_flags);
  135. }
  136. template<unsigned int C>
  137. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  138. {
  139. unsigned int p = startAt;
  140. if (b[p++] != 1)
  141. throw std::invalid_argument("invalid serialized RemotePath");
  142. p += _addr.deserialize(b,p);
  143. _ipScope = _addr.ipScope();
  144. _trust = (Path::Trust)b[p++];
  145. _lastSend = b.template at<uint64_t>(p); p += 8;
  146. _lastReceived = b.template at<uint64_t>(p); p += 8;
  147. p += _localAddress.deserialize(b,p);
  148. _flags = b.template at<uint16_t>(p); p += 2;
  149. return (p - startAt);
  150. }
  151. protected:
  152. uint64_t _lastSend;
  153. uint64_t _lastReceived;
  154. InetAddress _localAddress;
  155. uint16_t _flags;
  156. };
  157. } // namespace ZeroTier
  158. #endif