Path.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  34. * Base class for paths
  35. *
  36. * The base Path class is an immutable value.
  37. */
  38. class Path
  39. {
  40. public:
  41. /**
  42. * Path trust category
  43. *
  44. * Note that this is NOT peer trust and has nothing to do with root server
  45. * designations or other trust metrics. This indicates how much we trust
  46. * this path to be secure and/or private. A trust level of normal means
  47. * encrypt and authenticate all traffic. Privacy trust means we can send
  48. * traffic in the clear. Ultimate trust means we don't even need
  49. * authentication. Generally a private path would be a hard-wired local
  50. * LAN, while an ultimate trust path would be a physically isolated private
  51. * server backplane.
  52. *
  53. * Nearly all paths will be normal trust. The other levels are for high
  54. * performance local SDN use only.
  55. *
  56. * These values MUST match ZT1_LocalInterfaceAddressTrust in ZeroTierOne.h
  57. */
  58. enum Trust
  59. {
  60. TRUST_NORMAL = 0,
  61. TRUST_PRIVACY = 1,
  62. TRUST_ULTIMATE = 2
  63. };
  64. Path() :
  65. _addr(),
  66. _ipScope(InetAddress::IP_SCOPE_NONE),
  67. _trust(TRUST_NORMAL)
  68. {
  69. }
  70. Path(const InetAddress &addr,int metric,Trust trust) :
  71. _addr(addr),
  72. _ipScope(addr.ipScope()),
  73. _trust(trust)
  74. {
  75. }
  76. /**
  77. * @return Physical address
  78. */
  79. inline const InetAddress &address() const throw() { return _addr; }
  80. /**
  81. * @return IP scope -- faster shortcut for address().ipScope()
  82. */
  83. inline InetAddress::IpScope ipScope() const throw() { return _ipScope; }
  84. /**
  85. * @return Preference rank, higher == better
  86. */
  87. inline int preferenceRank() const throw() { return (int)_ipScope; } // IP scopes are in ascending rank order in InetAddress.hpp
  88. /**
  89. * @return Path trust level
  90. */
  91. inline Trust trust() const throw() { return _trust; }
  92. /**
  93. * @return True if path is considered reliable (no NAT keepalives etc. are needed)
  94. */
  95. inline bool reliable() const throw()
  96. {
  97. return ((_ipScope != InetAddress::IP_SCOPE_GLOBAL)&&(_ipScope != InetAddress::IP_SCOPE_PSEUDOPRIVATE));
  98. }
  99. /**
  100. * @return True if address is non-NULL
  101. */
  102. inline operator bool() const throw() { return (_addr); }
  103. // Comparisons are by address only
  104. inline bool operator==(const Path &p) const throw() { return (_addr == p._addr); }
  105. inline bool operator!=(const Path &p) const throw() { return (_addr != p._addr); }
  106. inline bool operator<(const Path &p) const throw() { return (_addr < p._addr); }
  107. inline bool operator>(const Path &p) const throw() { return (_addr > p._addr); }
  108. inline bool operator<=(const Path &p) const throw() { return (_addr <= p._addr); }
  109. inline bool operator>=(const Path &p) const throw() { return (_addr >= p._addr); }
  110. /**
  111. * Check whether this address is valid for a ZeroTier path
  112. *
  113. * This checks the address type and scope against address types and scopes
  114. * that we currently support for ZeroTier communication.
  115. *
  116. * @param a Address to check
  117. * @return True if address is good for ZeroTier path use
  118. */
  119. static inline bool isAddressValidForPath(const InetAddress &a)
  120. throw()
  121. {
  122. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  123. switch(a.ipScope()) {
  124. /* Note: we don't do link-local at the moment. Unfortunately these
  125. * cause several issues. The first is that they usually require a
  126. * device qualifier, which we don't handle yet and can't portably
  127. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  128. * these very ephemerally or otherwise strangely. So we'll use
  129. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  130. * global IP addresses. */
  131. case InetAddress::IP_SCOPE_PRIVATE:
  132. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  133. case InetAddress::IP_SCOPE_SHARED:
  134. case InetAddress::IP_SCOPE_GLOBAL:
  135. return true;
  136. default:
  137. return false;
  138. }
  139. }
  140. return false;
  141. }
  142. private:
  143. InetAddress _addr;
  144. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  145. Trust _trust;
  146. };
  147. } // namespace ZeroTier
  148. #endif