Path.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_PATH_HPP
  28. #define ZT_PATH_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdexcept>
  32. #include <string>
  33. #include <algorithm>
  34. #include "Constants.hpp"
  35. #include "Node.hpp"
  36. #include "InetAddress.hpp"
  37. #include "Utils.hpp"
  38. #include "AntiRecursion.hpp"
  39. #include "RuntimeEnvironment.hpp"
  40. namespace ZeroTier {
  41. /**
  42. * WAN address and protocol for reaching a peer
  43. *
  44. * This structure is volatile and memcpy-able, and depends on
  45. * InetAddress being similarly safe.
  46. */
  47. class Path
  48. {
  49. public:
  50. Path() :
  51. _addr(),
  52. _lastSend(0),
  53. _lastReceived(0),
  54. _fixed(false) {}
  55. Path(const Path &p) throw() { memcpy(this,&p,sizeof(Path)); }
  56. Path(const InetAddress &addr,bool fixed) :
  57. _addr(addr),
  58. _lastSend(0),
  59. _lastReceived(0),
  60. _fixed(fixed) {}
  61. inline void init(const InetAddress &addr,bool fixed)
  62. {
  63. _addr = addr;
  64. _lastSend = 0;
  65. _lastReceived = 0;
  66. _fixed = fixed;
  67. }
  68. inline Path &operator=(const Path &p)
  69. {
  70. if (this != &p)
  71. memcpy(this,&p,sizeof(Path));
  72. return *this;
  73. }
  74. inline const InetAddress &address() const throw() { return _addr; }
  75. inline uint64_t lastSend() const throw() { return _lastSend; }
  76. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  77. /**
  78. * Called when a packet is sent to this path
  79. *
  80. * This is called automatically by Path::send().
  81. *
  82. * @param t Time of send
  83. */
  84. inline void sent(uint64_t t)
  85. throw()
  86. {
  87. _lastSend = t;
  88. }
  89. /**
  90. * Called when a packet is received from this path
  91. *
  92. * @param t Time of receive
  93. */
  94. inline void received(uint64_t t)
  95. throw()
  96. {
  97. _lastReceived = t;
  98. }
  99. /**
  100. * @return Is this a fixed path?
  101. */
  102. inline bool fixed() const throw() { return _fixed; }
  103. /**
  104. * @param f New value of fixed path flag
  105. */
  106. inline void setFixed(bool f) throw() { _fixed = f; }
  107. /**
  108. * @param now Current time
  109. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  110. */
  111. inline bool active(uint64_t now) const
  112. throw()
  113. {
  114. return ( (_fixed) || ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT) );
  115. }
  116. /**
  117. * Send a packet via this path
  118. *
  119. * @param RR Runtime environment
  120. * @param data Packet data
  121. * @param len Packet length
  122. * @param now Current time
  123. * @return True if transport reported success
  124. */
  125. inline bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now)
  126. {
  127. if (RR->node->putPacket(_addr,data,len)) {
  128. sent(now);
  129. RR->antiRec->logOutgoingZT(data,len);
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * @param now Current time
  136. * @return Human-readable address and other information about this path
  137. */
  138. inline std::string toString(uint64_t now) const
  139. {
  140. char tmp[1024];
  141. Utils::snprintf(tmp,sizeof(tmp),"%s(%s)",
  142. _addr.toString().c_str(),
  143. ((_fixed) ? "fixed" : (active(now) ? "active" : "inactive"))
  144. );
  145. return std::string(tmp);
  146. }
  147. inline operator bool() const throw() { return (_addr); }
  148. inline bool operator==(const Path &p) const throw() { return (_addr == p._addr); }
  149. inline bool operator!=(const Path &p) const throw() { return (_addr != p._addr); }
  150. inline bool operator<(const Path &p) const throw() { return (_addr < p._addr); }
  151. inline bool operator>(const Path &p) const throw() { return (_addr > p._addr); }
  152. inline bool operator<=(const Path &p) const throw() { return (_addr <= p._addr); }
  153. inline bool operator>=(const Path &p) const throw() { return (_addr >= p._addr); }
  154. private:
  155. InetAddress _addr;
  156. uint64_t _lastSend;
  157. uint64_t _lastReceived;
  158. bool _fixed;
  159. };
  160. } // namespace ZeroTier
  161. #endif