RemotePath.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. namespace ZeroTier {
  38. /**
  39. * Path to a remote peer
  40. *
  41. * This extends Path to include status information about path activity.
  42. */
  43. class RemotePath : public Path
  44. {
  45. public:
  46. RemotePath() :
  47. Path(),
  48. _lastSend(0),
  49. _lastReceived(0),
  50. _localAddress(),
  51. _flags(0) {}
  52. RemotePath(const InetAddress &localAddress,const InetAddress &addr) :
  53. Path(addr,0,TRUST_NORMAL),
  54. _lastSend(0),
  55. _lastReceived(0),
  56. _localAddress(localAddress),
  57. _flags(0) {}
  58. inline const InetAddress &localAddress() const throw() { return _localAddress; }
  59. inline uint64_t lastSend() const throw() { return _lastSend; }
  60. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  61. /**
  62. * Called when a packet is sent to this remote path
  63. *
  64. * This is called automatically by RemotePath::send().
  65. *
  66. * @param t Time of send
  67. */
  68. inline void sent(uint64_t t)
  69. throw()
  70. {
  71. _lastSend = t;
  72. }
  73. /**
  74. * Called when a packet is received from this remote path
  75. *
  76. * @param t Time of receive
  77. */
  78. inline void received(uint64_t t)
  79. throw()
  80. {
  81. _lastReceived = t;
  82. }
  83. /**
  84. * @param now Current time
  85. * @return True if this path appears active
  86. */
  87. inline bool active(uint64_t now) const
  88. throw()
  89. {
  90. return ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT);
  91. }
  92. /**
  93. * Send a packet via this path
  94. *
  95. * @param RR Runtime environment
  96. * @param data Packet data
  97. * @param len Packet length
  98. * @param now Current time
  99. * @return True if transport reported success
  100. */
  101. inline bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now)
  102. {
  103. if (RR->node->putPacket(_localAddress,address(),data,len)) {
  104. sent(now);
  105. RR->antiRec->logOutgoingZT(data,len);
  106. return true;
  107. }
  108. return false;
  109. }
  110. template<unsigned int C>
  111. inline void serialize(Buffer<C> &b) const
  112. {
  113. b.append((uint8_t)1); // version
  114. _addr.serialize(b);
  115. b.append((uint8_t)_trust);
  116. b.append((uint64_t)_lastSend);
  117. b.append((uint64_t)_lastReceived);
  118. _localAddress.serialize(b);
  119. b.append((uint16_t)_flags);
  120. }
  121. template<unsigned int C>
  122. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  123. {
  124. unsigned int p = startAt;
  125. if (b[p++] != 1)
  126. throw std::invalid_argument("invalid serialized RemotePath");
  127. p += _addr.deserialize(b,p);
  128. _ipScope = _addr.ipScope();
  129. _trust = (Path::Trust)b[p++];
  130. _lastSend = b.template at<uint64_t>(p); p += 8;
  131. _lastReceived = b.template at<uint64_t>(p); p += 8;
  132. p += _localAddress.deserialize(b,p);
  133. _flags = b.template at<uint16_t>(p); p += 2;
  134. return (p - startAt);
  135. }
  136. protected:
  137. uint64_t _lastSend;
  138. uint64_t _lastReceived;
  139. InetAddress _localAddress;
  140. uint16_t _flags;
  141. };
  142. } // namespace ZeroTier
  143. #endif