Path.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 // NOTE: max 255
  59. {
  60. TRUST_NORMAL = 0,
  61. TRUST_PRIVACY = 10,
  62. TRUST_ULTIMATE = 20
  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. /**
  113. * Check whether this address is valid for a ZeroTier path
  114. *
  115. * This checks the address type and scope against address types and scopes
  116. * that we currently support for ZeroTier communication.
  117. *
  118. * @param a Address to check
  119. * @return True if address is good for ZeroTier path use
  120. */
  121. static inline bool isAddressValidForPath(const InetAddress &a)
  122. throw()
  123. {
  124. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  125. switch(a.ipScope()) {
  126. /* Note: we don't do link-local at the moment. Unfortunately these
  127. * cause several issues. The first is that they usually require a
  128. * device qualifier, which we don't handle yet and can't portably
  129. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  130. * these very ephemerally or otherwise strangely. So we'll use
  131. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  132. * global IP addresses. */
  133. case InetAddress::IP_SCOPE_PRIVATE:
  134. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  135. case InetAddress::IP_SCOPE_SHARED:
  136. case InetAddress::IP_SCOPE_GLOBAL:
  137. return true;
  138. default:
  139. return false;
  140. }
  141. }
  142. return false;
  143. }
  144. protected:
  145. InetAddress _addr;
  146. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  147. Trust _trust;
  148. };
  149. } // namespace ZeroTier
  150. #endif