Path.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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 "Constants.hpp"
  30. #include "InetAddress.hpp"
  31. #include "Utils.hpp"
  32. namespace ZeroTier {
  33. class Path
  34. {
  35. public:
  36. // Must be the same values as ZT1_LocalInterfaceAddressTrust in ZeroTierOne.h
  37. enum Trust
  38. {
  39. TRUST_NORMAL = 0,
  40. TRUST_PRIVACY = 1,
  41. TRUST_ULTIMATE = 2
  42. };
  43. Path() :
  44. _addr(),
  45. _metric(0),
  46. _trust(TRUST_NORMAL),
  47. _reliable(false)
  48. {
  49. }
  50. Path(const InetAddress &addr,int metric,Trust trust,bool reliable) :
  51. _addr(addr),
  52. _metric(metric),
  53. _trust(trust),
  54. _reliable(reliable)
  55. {
  56. }
  57. /**
  58. * @return Physical address
  59. */
  60. inline const InetAddress &address() const throw() { return _addr; }
  61. /**
  62. * @return Metric (higher == worse) or negative if path is blacklisted
  63. */
  64. inline int metric() const throw() { return _metric; }
  65. /**
  66. * @return Path trust level
  67. */
  68. inline Trust trust() const throw() { return _trust; }
  69. /**
  70. * @return True if path is considered reliable (no NAT keepalives etc. are needed)
  71. */
  72. inline bool reliable() const throw() { return _reliable; }
  73. /**
  74. * @return True if address is non-NULL
  75. */
  76. inline operator bool() const throw() { return (_addr); }
  77. // Comparisons are by address only
  78. inline bool operator==(const Path &p) const throw() { return (_addr == p._addr); }
  79. inline bool operator!=(const Path &p) const throw() { return (_addr != p._addr); }
  80. inline bool operator<(const Path &p) const throw() { return (_addr < p._addr); }
  81. inline bool operator>(const Path &p) const throw() { return (_addr > p._addr); }
  82. inline bool operator<=(const Path &p) const throw() { return (_addr <= p._addr); }
  83. inline bool operator>=(const Path &p) const throw() { return (_addr >= p._addr); }
  84. /**
  85. * Check whether this address is valid for a ZeroTier path
  86. *
  87. * This checks the address type and scope against address types and scopes
  88. * that we currently support for ZeroTier communication.
  89. *
  90. * @param a Address to check
  91. * @return True if address is good for ZeroTier path use
  92. */
  93. static inline bool isAddressValidForPath(const InetAddress &a)
  94. throw()
  95. {
  96. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  97. switch(a.ipScope()) {
  98. /* Note: we don't do link-local at the moment. Unfortunately these
  99. * cause several issues. The first is that they usually require a
  100. * device qualifier, which we don't handle yet and can't portably
  101. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  102. * these very ephemerally or otherwise strangely. So we'll use
  103. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  104. * global IP addresses. */
  105. case InetAddress::IP_SCOPE_PRIVATE:
  106. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  107. case InetAddress::IP_SCOPE_SHARED:
  108. case InetAddress::IP_SCOPE_GLOBAL:
  109. return true;
  110. default:
  111. return false;
  112. }
  113. }
  114. return false;
  115. }
  116. protected:
  117. InetAddress _addr;
  118. int _metric; // negative == blacklisted
  119. Trust _trust;
  120. bool _reliable;
  121. };
  122. } // namespace ZeroTier
  123. #endif