Path.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 True if it appears that a ping has gone unanswered
  79. */
  80. inline bool pingUnanswered(uint64_t now) const
  81. throw()
  82. {
  83. uint64_t lp = _lastPing;
  84. uint64_t lr = _lastReceived;
  85. if (lp)
  86. return ((lr < lp)&&((lp - lr) > ZT_PING_UNANSWERED_AFTER));
  87. return false;
  88. }
  89. /**
  90. * @return Human-readable address and other information about this path, some computed as of current time
  91. */
  92. inline std::string toString() const
  93. {
  94. uint64_t now = Utils::now();
  95. char lsago[32],lrago[32],lfoago[32],lpago[32];
  96. Utils::snprintf(lsago,sizeof(lsago),"%lld",(long long)((_lastSend != 0) ? (now - _lastSend) : -1));
  97. Utils::snprintf(lrago,sizeof(lrago),"%lld",(long long)((_lastReceived != 0) ? (now - _lastReceived) : -1));
  98. Utils::snprintf(lfoago,sizeof(lfoago),"%lld",(long long)((_lastFirewallOpener != 0) ? (now - _lastFirewallOpener) : -1));
  99. Utils::snprintf(lpago,sizeof(lfoago),"%lld",(long long)((_lastPing != 0) ? (now - _lastPing) : -1));
  100. return (_addr.toString() + "[" + (_tcp ? "tcp" : "udp") + ";" + lsago + ";" + lrago + ";" + lpago + ";" + lfoago + ";" + (active(now) ? "active" : "inactive") + ";" + (_fixed ? "fixed" : "learned") + "]");
  101. }
  102. inline bool operator==(const Path &p) const throw() { return ((_addr == p._addr)&&(_tcp == p._tcp)); }
  103. inline bool operator!=(const Path &p) const throw() { return ((_addr != p._addr)||(_tcp != p._tcp)); }
  104. inline bool operator<(const Path &p) const
  105. throw()
  106. {
  107. if (_addr == p._addr) {
  108. if (!_tcp) // UDP < TCP
  109. return p._tcp;
  110. return false;
  111. } else return (_addr < p._addr);
  112. }
  113. inline bool operator>(const Path &p) const throw() { return (p < *this); }
  114. inline bool operator<=(const Path &p) const throw() { return !(p < *this); }
  115. inline bool operator>=(const Path &p) const throw() { return !(*this < p); }
  116. template<unsigned int C>
  117. inline void serialize(Buffer<C> &b) const
  118. {
  119. b.append((unsigned char)ZT_PATH_SERIALIZATION_VERSION);
  120. b.append(_lastSend);
  121. b.append(_lastReceived);
  122. b.append(_lastFirewallOpener);
  123. b.append(_lastPing);
  124. b.append((unsigned char)_addr.type());
  125. switch(_addr.type()) {
  126. case InetAddress::TYPE_NULL:
  127. break;
  128. case InetAddress::TYPE_IPV4:
  129. b.append(_addr.rawIpData(),4);
  130. b.append((uint16_t)_addr.port());
  131. break;
  132. case InetAddress::TYPE_IPV6:
  133. b.append(_addr.rawIpData(),16);
  134. b.append((uint16_t)_addr.port());
  135. break;
  136. }
  137. b.append(_tcp ? (unsigned char)1 : (unsigned char)0);
  138. b.append(_fixed ? (unsigned char)1 : (unsigned char)0);
  139. }
  140. template<unsigned int C>
  141. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  142. {
  143. unsigned int p = startAt;
  144. if (b[p++] != ZT_PATH_SERIALIZATION_VERSION)
  145. throw std::invalid_argument("Path: deserialize(): version mismatch");
  146. _lastSend = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  147. _lastReceived = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  148. _lastFirewallOpener = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  149. _lastPing = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  150. switch((InetAddress::AddressType)b[p++]) {
  151. case InetAddress::TYPE_IPV4:
  152. _addr.set(b.field(p,4),4,b.template at<uint16_t>(p + 4));
  153. p += 4 + sizeof(uint16_t);
  154. break;
  155. case InetAddress::TYPE_IPV6:
  156. _addr.set(b.field(p,16),16,b.template at<uint16_t>(p + 16));
  157. p += 16 + sizeof(uint16_t);
  158. break;
  159. default:
  160. _addr.zero();
  161. break;
  162. }
  163. _tcp = (b[p++] != 0);
  164. _fixed = (b[p++] != 0);
  165. return (p - startAt);
  166. }
  167. private:
  168. volatile uint64_t _lastSend;
  169. volatile uint64_t _lastReceived;
  170. volatile uint64_t _lastFirewallOpener;
  171. volatile uint64_t _lastPing;
  172. InetAddress _addr;
  173. bool _tcp;
  174. bool _fixed;
  175. };
  176. } // namespace ZeroTier
  177. #endif