Path.hpp 6.5 KB

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