Path.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. _lastSend(0),
  52. _lastReceived(0),
  53. _addr(),
  54. _lastReceiveDesperation(0),
  55. _fixed(false) {}
  56. Path(const Path &p) throw() { memcpy(this,&p,sizeof(Path)); }
  57. Path(const InetAddress &addr,bool fixed) :
  58. _lastSend(0),
  59. _lastReceived(0),
  60. _addr(addr),
  61. _lastReceiveDesperation(0),
  62. _fixed(fixed) {}
  63. inline void init(const InetAddress &addr,bool fixed)
  64. {
  65. _lastSend = 0;
  66. _lastReceived = 0;
  67. _addr = addr;
  68. _lastReceiveDesperation = 0;
  69. _fixed = fixed;
  70. }
  71. inline Path &operator=(const Path &p)
  72. {
  73. if (this != &p)
  74. memcpy(this,&p,sizeof(Path));
  75. return *this;
  76. }
  77. inline const InetAddress &address() const throw() { return _addr; }
  78. inline uint64_t lastSend() const throw() { return _lastSend; }
  79. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  80. inline int lastReceiveDesperation() const throw() { return _lastReceiveDesperation; }
  81. /**
  82. * Called when a packet is sent to this path
  83. *
  84. * This is called automatically by Path::send().
  85. *
  86. * @param t Time of send
  87. */
  88. inline void sent(uint64_t t) throw() { _lastSend = t; }
  89. /**
  90. * Called when a packet is received from this path
  91. *
  92. * @param t Time of receive
  93. * @param d Link desperation of receive
  94. */
  95. inline void received(uint64_t t,int d) throw() { _lastReceived = t; _lastReceiveDesperation = d; }
  96. /**
  97. * @return Is this a fixed path?
  98. */
  99. inline bool fixed() const throw() { return _fixed; }
  100. /**
  101. * @param f New value of fixed path flag
  102. */
  103. inline void setFixed(bool f) throw() { _fixed = f; }
  104. /**
  105. * Compute path desperation
  106. *
  107. * Path desperation affects escalation to less efficient fallback
  108. * transports such as TCP or HTTP relaying.
  109. *
  110. * Right now we only escalate desperation for fixed paths, which
  111. * are paths to supernodes. This causes our fallback tunneling
  112. * mechanisms to kick in.
  113. *
  114. * @param now Current time
  115. * @return Path desperation, starting at 0
  116. */
  117. inline int desperation(uint64_t now) const
  118. {
  119. if ((_fixed)&&(_lastSend > _lastReceived))
  120. return std::max(_lastReceiveDesperation,(int)((_lastSend - _lastReceived) / ZT_DESPERATION_INCREMENT));
  121. return _lastReceiveDesperation;
  122. }
  123. /**
  124. * @param now Current time
  125. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  126. */
  127. inline bool active(uint64_t now) const
  128. throw()
  129. {
  130. return ( (_fixed) || ((now - _lastReceived) < ZT_PEER_PATH_ACTIVITY_TIMEOUT) );
  131. }
  132. /**
  133. * Send a packet via this path
  134. *
  135. * @param RR Runtime environment
  136. * @param data Packet data
  137. * @param len Packet length
  138. * @param now Current time
  139. * @return True if transport reported success
  140. */
  141. inline bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now)
  142. {
  143. if (RR->node->putPacket(_addr,data,len,desperation(now))) {
  144. sent(now);
  145. RR->antiRec->logOutgoingZT(data,len);
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * @param now Current time
  152. * @return Human-readable address and other information about this path
  153. */
  154. inline std::string toString(uint64_t now) const
  155. {
  156. char tmp[1024];
  157. Utils::snprintf(tmp,sizeof(tmp),"%s(%s)",
  158. _addr.toString().c_str(),
  159. ((_fixed) ? "fixed" : (active(now) ? "active" : "inactive"))
  160. );
  161. return std::string(tmp);
  162. }
  163. inline operator bool() const throw() { return (_addr); }
  164. inline bool operator==(const Path &p) const throw() { return (_addr == p._addr); }
  165. inline bool operator!=(const Path &p) const throw() { return (_addr != p._addr); }
  166. inline bool operator<(const Path &p) const throw() { return (_addr < p._addr); }
  167. inline bool operator>(const Path &p) const throw() { return (_addr > p._addr); }
  168. inline bool operator<=(const Path &p) const throw() { return (_addr <= p._addr); }
  169. inline bool operator>=(const Path &p) const throw() { return (_addr >= p._addr); }
  170. private:
  171. uint64_t _lastSend;
  172. uint64_t _lastReceived;
  173. InetAddress _addr;
  174. int _lastReceiveDesperation;
  175. bool _fixed;
  176. };
  177. } // namespace ZeroTier
  178. #endif