Path.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. #include "Buffer.hpp"
  37. #define ZT_PATH_SERIALIZATION_VERSION 2
  38. namespace ZeroTier {
  39. /**
  40. * WAN address and protocol for reaching a peer
  41. */
  42. class Path
  43. {
  44. public:
  45. enum Type
  46. {
  47. PATH_TYPE_NULL = 0,
  48. PATH_TYPE_UDP = 1,
  49. PATH_TYPE_TCP_OUT = 2,
  50. PATH_TYPE_TCP_IN = 3
  51. };
  52. Path() :
  53. _lastSend(0),
  54. _lastReceived(0),
  55. _lastFirewallOpener(0),
  56. _lastPing(0),
  57. _addr(),
  58. _type(PATH_TYPE_NULL),
  59. _fixed(false) {}
  60. Path(const Path &p)
  61. {
  62. // InetAddress is memcpy'able
  63. memcpy(this,&p,sizeof(Path));
  64. }
  65. Path(const InetAddress &addr,Type t,bool fixed = false) :
  66. _lastSend(0),
  67. _lastReceived(0),
  68. _lastFirewallOpener(0),
  69. _lastPing(0),
  70. _addr(addr),
  71. _type(t),
  72. _fixed(fixed) {}
  73. inline Path &operator=(const Path &p)
  74. {
  75. if (this != &p)
  76. memcpy(this,&p,sizeof(Path));
  77. return *this;
  78. }
  79. inline const InetAddress &address() const throw() { return _addr; }
  80. inline Type type() const throw() { return _type; }
  81. inline bool tcp() const throw() { return ((_type == PATH_TYPE_TCP_IN)||(_type == PATH_TYPE_TCP_OUT)); }
  82. inline uint64_t lastSend() const throw() { return _lastSend; }
  83. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  84. inline uint64_t lastFirewallOpener() const throw() { return _lastFirewallOpener; }
  85. inline uint64_t lastPing() const throw() { return _lastPing; }
  86. inline bool fixed() const throw() { return _fixed; }
  87. inline void setFixed(bool f) throw() { _fixed = f; }
  88. inline void sent(uint64_t t) throw() { _lastSend = t; }
  89. inline void received(uint64_t t) throw() { _lastReceived = t; }
  90. inline void firewallOpenerSent(uint64_t t) throw() { _lastFirewallOpener = t; }
  91. inline void pinged(uint64_t t) throw() { _lastPing = t; }
  92. /**
  93. * @param now Current time
  94. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  95. */
  96. inline bool active(uint64_t now) const
  97. throw()
  98. {
  99. return ((_addr)&&((_fixed)||((now - _lastReceived) < ZT_PEER_PATH_ACTIVITY_TIMEOUT)));
  100. }
  101. /**
  102. * @return Human-readable address and other information about this path, some computed as of current time
  103. */
  104. inline std::string toString() const
  105. {
  106. uint64_t now = Utils::now();
  107. char tmp[1024];
  108. const char *t = "";
  109. switch(_type) {
  110. case PATH_TYPE_NULL: t = "null"; break;
  111. case PATH_TYPE_UDP: t = "udp"; break;
  112. case PATH_TYPE_TCP_OUT: t = "tcp_out"; break;
  113. case PATH_TYPE_TCP_IN: t = "tcp_in"; break;
  114. }
  115. Utils::snprintf(tmp,sizeof(tmp),"%s;%s;%lld;%lld;%lld;%lld;%s",
  116. t,
  117. _addr.toString().c_str(),
  118. (long long)((_lastSend != 0) ? ((now - _lastSend) / 1000LL) : -1),
  119. (long long)((_lastReceived != 0) ? ((now - _lastReceived) / 1000LL) : -1),
  120. (long long)((_lastFirewallOpener != 0) ? ((now - _lastFirewallOpener) / 1000LL) : -1),
  121. (long long)((_lastPing != 0) ? ((now - _lastPing) / 1000LL) : -1),
  122. ((_fixed) ? "fixed" : (active(now) ? "active" : "inactive"))
  123. );
  124. return std::string(tmp);
  125. }
  126. inline bool operator==(const Path &p) const throw() { return ((_addr == p._addr)&&(_type == p._type)); }
  127. inline bool operator!=(const Path &p) const throw() { return ((_addr != p._addr)||(_type != p._type)); }
  128. inline bool operator<(const Path &p) const
  129. throw()
  130. {
  131. if (_addr == p._addr)
  132. return ((int)_type < (int)p._type);
  133. else return (_addr < p._addr);
  134. }
  135. inline bool operator>(const Path &p) const throw() { return (p < *this); }
  136. inline bool operator<=(const Path &p) const throw() { return !(p < *this); }
  137. inline bool operator>=(const Path &p) const throw() { return !(*this < p); }
  138. template<unsigned int C>
  139. inline void serialize(Buffer<C> &b) const
  140. {
  141. b.append((unsigned char)ZT_PATH_SERIALIZATION_VERSION);
  142. b.append(_lastSend);
  143. b.append(_lastReceived);
  144. b.append(_lastFirewallOpener);
  145. b.append(_lastPing);
  146. b.append((unsigned char)_addr.type());
  147. switch(_addr.type()) {
  148. case InetAddress::TYPE_NULL:
  149. break;
  150. case InetAddress::TYPE_IPV4:
  151. b.append(_addr.rawIpData(),4);
  152. b.append((uint16_t)_addr.port());
  153. break;
  154. case InetAddress::TYPE_IPV6:
  155. b.append(_addr.rawIpData(),16);
  156. b.append((uint16_t)_addr.port());
  157. break;
  158. }
  159. b.append((unsigned char)_type);
  160. b.append(_fixed ? (unsigned char)1 : (unsigned char)0);
  161. }
  162. template<unsigned int C>
  163. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  164. {
  165. unsigned int p = startAt;
  166. if (b[p++] != ZT_PATH_SERIALIZATION_VERSION)
  167. throw std::invalid_argument("Path: deserialize(): version mismatch");
  168. _lastSend = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  169. _lastReceived = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  170. _lastFirewallOpener = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  171. _lastPing = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  172. switch((InetAddress::AddressType)b[p++]) {
  173. case InetAddress::TYPE_IPV4:
  174. _addr.set(b.field(p,4),4,b.template at<uint16_t>(p + 4));
  175. p += 4 + sizeof(uint16_t);
  176. break;
  177. case InetAddress::TYPE_IPV6:
  178. _addr.set(b.field(p,16),16,b.template at<uint16_t>(p + 16));
  179. p += 16 + sizeof(uint16_t);
  180. break;
  181. default:
  182. _addr.zero();
  183. break;
  184. }
  185. _type = (Type)b[p++];
  186. _fixed = (b[p++] != 0);
  187. return (p - startAt);
  188. }
  189. private:
  190. volatile uint64_t _lastSend;
  191. volatile uint64_t _lastReceived;
  192. volatile uint64_t _lastFirewallOpener;
  193. volatile uint64_t _lastPing;
  194. InetAddress _addr;
  195. Type _type;
  196. bool _fixed;
  197. };
  198. } // namespace ZeroTier
  199. #endif