Path.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "Constants.hpp"
  31. #include "InetAddress.hpp"
  32. #include "Utils.hpp"
  33. #include <string>
  34. namespace ZeroTier {
  35. /**
  36. * WAN address and protocol for reaching a peer
  37. */
  38. class Path
  39. {
  40. public:
  41. Path() :
  42. _lastSent(0),
  43. _lastReceived(0),
  44. _lastFirewallOpener(0),
  45. _lastPing(0),
  46. _addr(),
  47. _tcp(false),
  48. _fixed(false) {}
  49. Path(const InetAddress &addr,bool tcp,bool fixed) :
  50. _lastSent(0),
  51. _lastReceived(0),
  52. _lastFirewallOpener(0),
  53. _lastPing(0),
  54. _addr(addr),
  55. _tcp(tcp),
  56. _fixed(fixed) {}
  57. inline const InetAddress &address() const throw() { return _addr; }
  58. inline bool tcp() const throw() { return _tcp; }
  59. inline uint64_t lastSent() const throw() { return _lastSent; }
  60. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  61. inline uint64_t lastFirewallOpener() const throw() { return _lastFirewallOpener; }
  62. inline uint64_t lastPing() const throw() { return _lastPing; }
  63. inline bool fixed() const throw() { return _fixed; }
  64. inline void sent(uint64_t t) throw() { _lastSent = t; }
  65. inline void received(uint64_t t) throw() { _lastReceived = t; }
  66. inline void firewallOpenerSent(uint64_t t) throw() { _lastFirewallOpener = t; }
  67. inline void pinged(uint64_t t) throw() { _lastPing = t; }
  68. inline bool active(uint64_t now) const
  69. throw()
  70. {
  71. return ((_addr)&&((_fixed)||((now - _lastReceived) < ZT_PEER_PATH_ACTIVITY_TIMEOUT)));
  72. }
  73. /**
  74. * @return Human-readable address and other information about this path, some computed as of current time
  75. */
  76. inline std::string toString() const
  77. {
  78. uint64_t now = Utils::now();
  79. char lsago[32],lrago[32],lfoago[32],lpago[32];
  80. Utils::snprintf(lsago,sizeof(lsago),"%lld",(long long)((_lastSent != 0) ? (now - _lastSent) : -1));
  81. Utils::snprintf(lrago,sizeof(lrago),"%lld",(long long)((_lastReceived != 0) ? (now - _lastReceived) : -1));
  82. Utils::snprintf(lfoago,sizeof(lfoago),"%lld",(long long)((_lastFirewallOpener != 0) ? (now - _lastFirewallOpener) : -1));
  83. Utils::snprintf(lpago,sizeof(lfoago),"%lld",(long long)((_lastPing != 0) ? (now - _lastPing) : -1));
  84. return (std::string(_tcp ? "tcp:" : "udp:") + _addr.toString() + "[" + lsago + "," lrago + "," + lpago + "," + lfoago + "," + (active(now) ? "active" : "inactive") + "," + (_fixed ? "fixed" : "learned") + "]");
  85. }
  86. inline operator==(const Path &p) const throw() { return ((_addr == p._addr)&&(_tcp == p._tcp)); }
  87. inline operator!=(const Path &p) const throw() { return ((_addr != p._addr)||(_tcp != p._tcp)); }
  88. inline operator<(const Path &p) const
  89. throw()
  90. {
  91. if (_addr == p._addr) {
  92. if (!_tcp) // UDP < TCP
  93. return p._tcp;
  94. return false;
  95. } else return (_addr < p._addr);
  96. }
  97. inline bool operator>(const Path &p) const throw() { return (p < *this); }
  98. inline bool operator<=(const Path &p) const throw() { return !(p < *this); }
  99. inline bool operator>=(const Path &p) const throw() { return !(*this < p); }
  100. private:
  101. volatile uint64_t _lastSent;
  102. volatile uint64_t _lastReceived;
  103. volatile uint64_t _lastFirewallOpener;
  104. volatile uint64_t _lastPing;
  105. InetAddress _addr;
  106. bool _tcp;
  107. bool _fixed;
  108. };
  109. } // namespace ZeroTier
  110. #endif