Path.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <stdexcept>
  31. #include <string>
  32. #include "Constants.hpp"
  33. #include "InetAddress.hpp"
  34. #include "Utils.hpp"
  35. #include "Buffer.hpp"
  36. #define ZT_PATH_SERIALIZATION_VERSION 1
  37. namespace ZeroTier {
  38. /**
  39. * WAN address and protocol for reaching a peer
  40. */
  41. class Path
  42. {
  43. public:
  44. Path() :
  45. _lastSend(0),
  46. _lastReceived(0),
  47. _lastFirewallOpener(0),
  48. _lastPing(0),
  49. _addr(),
  50. _tcp(false),
  51. _fixed(false) {}
  52. Path(const InetAddress &addr,bool tcp,bool fixed = false) :
  53. _lastSend(0),
  54. _lastReceived(0),
  55. _lastFirewallOpener(0),
  56. _lastPing(0),
  57. _addr(addr),
  58. _tcp(tcp),
  59. _fixed(fixed) {}
  60. inline const InetAddress &address() const throw() { return _addr; }
  61. inline bool tcp() const throw() { return _tcp; }
  62. inline uint64_t lastSend() const throw() { return _lastSend; }
  63. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  64. inline uint64_t lastFirewallOpener() const throw() { return _lastFirewallOpener; }
  65. inline uint64_t lastPing() const throw() { return _lastPing; }
  66. inline bool fixed() const throw() { return _fixed; }
  67. inline void setFixed(bool f) throw() { _fixed = f; }
  68. inline void sent(uint64_t t) throw() { _lastSend = t; }
  69. inline void received(uint64_t t) throw() { _lastReceived = t; }
  70. inline void firewallOpenerSent(uint64_t t) throw() { _lastFirewallOpener = t; }
  71. inline void pinged(uint64_t t) throw() { _lastPing = t; }
  72. inline bool active(uint64_t now) const
  73. throw()
  74. {
  75. return ((_addr)&&((_fixed)||((now - _lastReceived) < ZT_PEER_PATH_ACTIVITY_TIMEOUT)));
  76. }
  77. /**
  78. * @return Human-readable address and other information about this path, some computed as of current time
  79. */
  80. inline std::string toString() const
  81. {
  82. uint64_t now = Utils::now();
  83. char lsago[32],lrago[32],lfoago[32],lpago[32];
  84. Utils::snprintf(lsago,sizeof(lsago),"%lld",(long long)((_lastSend != 0) ? (now - _lastSend) : -1));
  85. Utils::snprintf(lrago,sizeof(lrago),"%lld",(long long)((_lastReceived != 0) ? (now - _lastReceived) : -1));
  86. Utils::snprintf(lfoago,sizeof(lfoago),"%lld",(long long)((_lastFirewallOpener != 0) ? (now - _lastFirewallOpener) : -1));
  87. Utils::snprintf(lpago,sizeof(lfoago),"%lld",(long long)((_lastPing != 0) ? (now - _lastPing) : -1));
  88. return ( _addr.toString() +
  89. "[" +
  90. (_tcp ? "tcp" : "udp") +
  91. ";" +
  92. lsago +
  93. ";" +
  94. lrago +
  95. ";" +
  96. lpago +
  97. ";" +
  98. lfoago +
  99. ";" +
  100. (active(now) ? "active" : "inactive") +
  101. ";" +
  102. (_fixed ? "fixed" : "learned") +
  103. "]"
  104. );
  105. }
  106. inline bool operator==(const Path &p) const throw() { return ((_addr == p._addr)&&(_tcp == p._tcp)); }
  107. inline bool operator!=(const Path &p) const throw() { return ((_addr != p._addr)||(_tcp != p._tcp)); }
  108. inline bool operator<(const Path &p) const
  109. throw()
  110. {
  111. if (_addr == p._addr) {
  112. if (!_tcp) // UDP < TCP
  113. return p._tcp;
  114. return false;
  115. } else return (_addr < p._addr);
  116. }
  117. inline bool operator>(const Path &p) const throw() { return (p < *this); }
  118. inline bool operator<=(const Path &p) const throw() { return !(p < *this); }
  119. inline bool operator>=(const Path &p) const throw() { return !(*this < p); }
  120. template<unsigned int C>
  121. inline void serialize(Buffer<C> &b) const
  122. {
  123. b.append((unsigned char)ZT_PATH_SERIALIZATION_VERSION);
  124. b.append(_lastSend);
  125. b.append(_lastReceived);
  126. b.append(_lastFirewallOpener);
  127. b.append(_lastPing);
  128. b.append((unsigned char)_addr.type());
  129. switch(_addr.type()) {
  130. case InetAddress::TYPE_NULL:
  131. break;
  132. case InetAddress::TYPE_IPV4:
  133. b.append(_addr.rawIpData(),4);
  134. b.append((uint16_t)_addr.port());
  135. break;
  136. case InetAddress::TYPE_IPV6:
  137. b.append(_addr.rawIpData(),16);
  138. b.append((uint16_t)_addr.port());
  139. break;
  140. }
  141. b.append(_tcp ? (unsigned char)1 : (unsigned char)0);
  142. b.append(_fixed ? (unsigned char)1 : (unsigned char)0);
  143. }
  144. template<unsigned int C>
  145. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  146. {
  147. unsigned int p = startAt;
  148. if (b[p++] != ZT_PATH_SERIALIZATION_VERSION)
  149. throw std::invalid_argument("Path: deserialize(): version mismatch");
  150. _lastSend = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  151. _lastReceived = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  152. _lastFirewallOpener = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  153. _lastPing = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  154. switch((InetAddress::AddressType)b[p++]) {
  155. case InetAddress::TYPE_IPV4:
  156. _addr.set(b.field(p,4),4,b.template at<uint16_t>(p + 4));
  157. p += 4 + sizeof(uint16_t);
  158. break;
  159. case InetAddress::TYPE_IPV6:
  160. _addr.set(b.field(p,16),16,b.template at<uint16_t>(p + 16));
  161. p += 16 + sizeof(uint16_t);
  162. break;
  163. default:
  164. _addr.zero();
  165. break;
  166. }
  167. _tcp = (b[p++] != 0);
  168. _fixed = (b[p++] != 0);
  169. return (p - startAt);
  170. }
  171. private:
  172. volatile uint64_t _lastSend;
  173. volatile uint64_t _lastReceived;
  174. volatile uint64_t _lastFirewallOpener;
  175. volatile uint64_t _lastPing;
  176. InetAddress _addr;
  177. bool _tcp;
  178. bool _fixed;
  179. };
  180. } // namespace ZeroTier
  181. #endif