Path.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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 "Constants.hpp"
  34. #include "InetAddress.hpp"
  35. #include "Utils.hpp"
  36. namespace ZeroTier {
  37. /**
  38. * WAN address and protocol for reaching a peer
  39. *
  40. * This structure is volatile and memcpy-able, and depends on
  41. * InetAddress being similarly safe.
  42. */
  43. class Path
  44. {
  45. public:
  46. enum Type
  47. {
  48. PATH_TYPE_NULL = 0,
  49. PATH_TYPE_UDP = 1,
  50. PATH_TYPE_TCP_OUT = 2,
  51. PATH_TYPE_TCP_IN = 3
  52. };
  53. Path() :
  54. _lastSend(0),
  55. _lastReceived(0),
  56. _lastPing(0),
  57. _addr(),
  58. _type(PATH_TYPE_NULL),
  59. _fixed(false) {}
  60. Path(const Path &p)
  61. {
  62. memcpy(this,&p,sizeof(Path));
  63. }
  64. Path(const InetAddress &addr,Type t,bool fixed = false) :
  65. _lastSend(0),
  66. _lastReceived(0),
  67. _lastPing(0),
  68. _addr(addr),
  69. _type(t),
  70. _fixed(fixed) {}
  71. inline void init(const InetAddress &addr,Type t,bool fixed = false)
  72. {
  73. _lastSend = 0;
  74. _lastReceived = 0;
  75. _lastPing = 0;
  76. _addr = addr;
  77. _type = t;
  78. _fixed = fixed;
  79. }
  80. inline Path &operator=(const Path &p)
  81. {
  82. if (this != &p)
  83. memcpy(this,&p,sizeof(Path));
  84. return *this;
  85. }
  86. inline const InetAddress &address() const throw() { return _addr; }
  87. inline Type type() const throw() { return _type; }
  88. inline bool tcp() const throw() { return ((_type == PATH_TYPE_TCP_IN)||(_type == PATH_TYPE_TCP_OUT)); }
  89. inline uint64_t lastSend() const throw() { return _lastSend; }
  90. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  91. inline uint64_t lastPing() const throw() { return _lastPing; }
  92. inline bool fixed() const throw() { return _fixed; }
  93. inline void setFixed(bool f) throw() { _fixed = f; }
  94. inline void sent(uint64_t t) throw() { _lastSend = t; }
  95. inline void received(uint64_t t) throw() { _lastReceived = t; }
  96. inline void pinged(uint64_t t) throw() { _lastPing = t; }
  97. /**
  98. * @param now Current time
  99. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  100. */
  101. inline bool active(uint64_t now) const
  102. throw()
  103. {
  104. return ((_addr)&&((_fixed)||((now - _lastReceived) < ZT_PEER_PATH_ACTIVITY_TIMEOUT)));
  105. }
  106. /**
  107. * @return Human-readable address and other information about this path, some computed as of current time
  108. */
  109. inline std::string toString() const
  110. {
  111. uint64_t now = Utils::now();
  112. char tmp[1024];
  113. const char *t = "";
  114. switch(_type) {
  115. case PATH_TYPE_NULL: t = "null"; break;
  116. case PATH_TYPE_UDP: t = "udp"; break;
  117. case PATH_TYPE_TCP_OUT: t = "tcp_out"; break;
  118. case PATH_TYPE_TCP_IN: t = "tcp_in"; break;
  119. }
  120. Utils::snprintf(tmp,sizeof(tmp),"%s;%s;%lld;%lld;%lld;%s",
  121. t,
  122. _addr.toString().c_str(),
  123. (long long)((_lastSend != 0) ? ((now - _lastSend) / 1000LL) : -1),
  124. (long long)((_lastReceived != 0) ? ((now - _lastReceived) / 1000LL) : -1),
  125. (long long)((_lastPing != 0) ? ((now - _lastPing) / 1000LL) : -1),
  126. ((_fixed) ? "fixed" : (active(now) ? "active" : "inactive"))
  127. );
  128. return std::string(tmp);
  129. }
  130. inline bool operator==(const Path &p) const throw() { return ((_addr == p._addr)&&(_type == p._type)); }
  131. inline bool operator!=(const Path &p) const throw() { return ((_addr != p._addr)||(_type != p._type)); }
  132. inline bool operator<(const Path &p) const
  133. throw()
  134. {
  135. if (_addr == p._addr)
  136. return ((int)_type < (int)p._type);
  137. else return (_addr < p._addr);
  138. }
  139. inline bool operator>(const Path &p) const throw() { return (p < *this); }
  140. inline bool operator<=(const Path &p) const throw() { return !(p < *this); }
  141. inline bool operator>=(const Path &p) const throw() { return !(*this < p); }
  142. private:
  143. volatile uint64_t _lastSend;
  144. volatile uint64_t _lastReceived;
  145. volatile uint64_t _lastPing;
  146. InetAddress _addr;
  147. Type _type;
  148. bool _fixed;
  149. };
  150. } // namespace ZeroTier
  151. #endif