Path.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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() + "[" + (_tcp ? "tcp" : "udp") + ";" + lsago + ";" + lrago + ";" + lpago + ";" + lfoago + ";" + (active(now) ? "active" : "inactive") + ";" + (_fixed ? "fixed" : "learned") + "]");
  89. }
  90. inline bool operator==(const Path &p) const throw() { return ((_addr == p._addr)&&(_tcp == p._tcp)); }
  91. inline bool operator!=(const Path &p) const throw() { return ((_addr != p._addr)||(_tcp != p._tcp)); }
  92. inline bool operator<(const Path &p) const
  93. throw()
  94. {
  95. if (_addr == p._addr) {
  96. if (!_tcp) // UDP < TCP
  97. return p._tcp;
  98. return false;
  99. } else return (_addr < p._addr);
  100. }
  101. inline bool operator>(const Path &p) const throw() { return (p < *this); }
  102. inline bool operator<=(const Path &p) const throw() { return !(p < *this); }
  103. inline bool operator>=(const Path &p) const throw() { return !(*this < p); }
  104. template<unsigned int C>
  105. inline void serialize(Buffer<C> &b) const
  106. {
  107. b.append((unsigned char)ZT_PATH_SERIALIZATION_VERSION);
  108. b.append(_lastSend);
  109. b.append(_lastReceived);
  110. b.append(_lastFirewallOpener);
  111. b.append(_lastPing);
  112. b.append((unsigned char)_addr.type());
  113. switch(_addr.type()) {
  114. case InetAddress::TYPE_NULL:
  115. break;
  116. case InetAddress::TYPE_IPV4:
  117. b.append(_addr.rawIpData(),4);
  118. b.append((uint16_t)_addr.port());
  119. break;
  120. case InetAddress::TYPE_IPV6:
  121. b.append(_addr.rawIpData(),16);
  122. b.append((uint16_t)_addr.port());
  123. break;
  124. }
  125. b.append(_tcp ? (unsigned char)1 : (unsigned char)0);
  126. b.append(_fixed ? (unsigned char)1 : (unsigned char)0);
  127. }
  128. template<unsigned int C>
  129. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  130. {
  131. unsigned int p = startAt;
  132. if (b[p++] != ZT_PATH_SERIALIZATION_VERSION)
  133. throw std::invalid_argument("Path: deserialize(): version mismatch");
  134. _lastSend = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  135. _lastReceived = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  136. _lastFirewallOpener = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  137. _lastPing = b.template at<uint64_t>(p); p += sizeof(uint64_t);
  138. switch((InetAddress::AddressType)b[p++]) {
  139. case InetAddress::TYPE_IPV4:
  140. _addr.set(b.field(p,4),4,b.template at<uint16_t>(p + 4));
  141. p += 4 + sizeof(uint16_t);
  142. break;
  143. case InetAddress::TYPE_IPV6:
  144. _addr.set(b.field(p,16),16,b.template at<uint16_t>(p + 16));
  145. p += 16 + sizeof(uint16_t);
  146. break;
  147. default:
  148. _addr.zero();
  149. break;
  150. }
  151. _tcp = (b[p++] != 0);
  152. _fixed = (b[p++] != 0);
  153. return (p - startAt);
  154. }
  155. private:
  156. volatile uint64_t _lastSend;
  157. volatile uint64_t _lastReceived;
  158. volatile uint64_t _lastFirewallOpener;
  159. volatile uint64_t _lastPing;
  160. InetAddress _addr;
  161. bool _tcp;
  162. bool _fixed;
  163. };
  164. } // namespace ZeroTier
  165. #endif