Path.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ZT_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()
  88. {
  89. // First, since the scope enum values in InetAddress.hpp are in order of
  90. // use preference rank, we take that. Then we multiple by two, yielding
  91. // a sequence like 0, 2, 4, 6, etc. Then if it's IPv6 we add one. This
  92. // makes IPv6 addresses of a given scope outrank IPv4 addresses of the
  93. // same scope -- e.g. 1 outranks 0. This makes us prefer IPv6, but not
  94. // if the address scope/class is of a fundamentally lower rank.
  95. return ( ((int)_ipScope * 2) + ((_addr.ss_family == AF_INET6) ? 1 : 0) );
  96. }
  97. /**
  98. * @return Path trust level
  99. */
  100. inline Trust trust() const throw() { return _trust; }
  101. /**
  102. * @return True if path is considered reliable (no NAT keepalives etc. are needed)
  103. */
  104. inline bool reliable() const throw()
  105. {
  106. return ( (_addr.ss_family == AF_INET6) || ((_ipScope != InetAddress::IP_SCOPE_GLOBAL)&&(_ipScope != InetAddress::IP_SCOPE_PSEUDOPRIVATE)) );
  107. }
  108. /**
  109. * @return True if address is non-NULL
  110. */
  111. inline operator bool() const throw() { return (_addr); }
  112. // Comparisons are by address only
  113. inline bool operator==(const Path &p) const throw() { return (_addr == p._addr); }
  114. inline bool operator!=(const Path &p) const throw() { return (_addr != p._addr); }
  115. inline bool operator<(const Path &p) const throw() { return (_addr < p._addr); }
  116. inline bool operator>(const Path &p) const throw() { return (_addr > p._addr); }
  117. inline bool operator<=(const Path &p) const throw() { return (_addr <= p._addr); }
  118. inline bool operator>=(const Path &p) const throw() { return (_addr >= p._addr); }
  119. /**
  120. * Check whether this address is valid for a ZeroTier path
  121. *
  122. * This checks the address type and scope against address types and scopes
  123. * that we currently support for ZeroTier communication.
  124. *
  125. * @param a Address to check
  126. * @return True if address is good for ZeroTier path use
  127. */
  128. static inline bool isAddressValidForPath(const InetAddress &a)
  129. throw()
  130. {
  131. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  132. switch(a.ipScope()) {
  133. /* Note: we don't do link-local at the moment. Unfortunately these
  134. * cause several issues. The first is that they usually require a
  135. * device qualifier, which we don't handle yet and can't portably
  136. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  137. * these very ephemerally or otherwise strangely. So we'll use
  138. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  139. * global IP addresses. */
  140. case InetAddress::IP_SCOPE_PRIVATE:
  141. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  142. case InetAddress::IP_SCOPE_SHARED:
  143. case InetAddress::IP_SCOPE_GLOBAL:
  144. return true;
  145. default:
  146. return false;
  147. }
  148. }
  149. return false;
  150. }
  151. private:
  152. InetAddress _addr;
  153. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  154. Trust _trust;
  155. };
  156. } // namespace ZeroTier
  157. #endif