Path.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. _lastReceiveDesperation(0),
  55. _fixed(false) {}
  56. Path(const Path &p) throw() { memcpy(this,&p,sizeof(Path)); }
  57. Path(const InetAddress &addr,bool fixed) :
  58. _addr(addr),
  59. _lastSend(0),
  60. _lastReceived(0),
  61. _lastReceiveDesperation(0),
  62. _fixed(fixed) {}
  63. inline void init(const InetAddress &addr,bool fixed)
  64. {
  65. _addr = addr;
  66. _lastSend = 0;
  67. _lastReceived = 0;
  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. /**
  81. * Called when a packet is sent to this path
  82. *
  83. * This is called automatically by Path::send().
  84. *
  85. * @param t Time of send
  86. */
  87. inline void sent(uint64_t t)
  88. throw()
  89. {
  90. _lastSend = t;
  91. }
  92. /**
  93. * Called when a packet is received from this path
  94. *
  95. * @param t Time of receive
  96. * @param d Link desperation of receive
  97. */
  98. inline void received(uint64_t t,unsigned int d)
  99. throw()
  100. {
  101. _lastReceived = t;
  102. _lastReceiveDesperation = d;
  103. }
  104. /**
  105. * @return Is this a fixed path?
  106. */
  107. inline bool fixed() const throw() { return _fixed; }
  108. /**
  109. * @param f New value of fixed path flag
  110. */
  111. inline void setFixed(bool f) throw() { _fixed = f; }
  112. /**
  113. * @return Last desperation reported via incoming link
  114. */
  115. inline unsigned int lastReceiveDesperation() const throw() { return _lastReceiveDesperation; }
  116. /**
  117. * @param now Current time
  118. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  119. */
  120. inline bool active(uint64_t now) const
  121. throw()
  122. {
  123. return ( (_fixed) || ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT) );
  124. }
  125. /**
  126. * Send a packet via this path
  127. *
  128. * @param RR Runtime environment
  129. * @param data Packet data
  130. * @param len Packet length
  131. * @param now Current time
  132. * @return True if transport reported success
  133. */
  134. inline bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now)
  135. {
  136. if (RR->node->putPacket(_addr,data,len,std::max(RR->node->coreDesperation(),_lastReceiveDesperation))) {
  137. sent(now);
  138. RR->antiRec->logOutgoingZT(data,len);
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * @param now Current time
  145. * @return Human-readable address and other information about this path
  146. */
  147. inline std::string toString(uint64_t now) const
  148. {
  149. char tmp[1024];
  150. Utils::snprintf(tmp,sizeof(tmp),"%s(%s)",
  151. _addr.toString().c_str(),
  152. ((_fixed) ? "fixed" : (active(now) ? "active" : "inactive"))
  153. );
  154. return std::string(tmp);
  155. }
  156. inline operator bool() const throw() { return (_addr); }
  157. inline bool operator==(const Path &p) const throw() { return (_addr == p._addr); }
  158. inline bool operator!=(const Path &p) const throw() { return (_addr != p._addr); }
  159. inline bool operator<(const Path &p) const throw() { return (_addr < p._addr); }
  160. inline bool operator>(const Path &p) const throw() { return (_addr > p._addr); }
  161. inline bool operator<=(const Path &p) const throw() { return (_addr <= p._addr); }
  162. inline bool operator>=(const Path &p) const throw() { return (_addr >= p._addr); }
  163. private:
  164. InetAddress _addr;
  165. uint64_t _lastSend;
  166. uint64_t _lastReceived;
  167. unsigned int _lastReceiveDesperation;
  168. bool _fixed;
  169. };
  170. } // namespace ZeroTier
  171. #endif