Path.hpp 6.1 KB

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