SelfAwareness.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_SELFAWARENESS_HPP
  14. #define ZT_SELFAWARENESS_HPP
  15. #include "Constants.hpp"
  16. #include "InetAddress.hpp"
  17. #include "Containers.hpp"
  18. #include "Address.hpp"
  19. #include "Mutex.hpp"
  20. #include <map>
  21. namespace ZeroTier {
  22. class Identity;
  23. class RuntimeEnvironment;
  24. /**
  25. * SelfAwareness manages awareness of this peer's external address(es) and NAT situation.
  26. *
  27. * This code should not be capable of achieving sentience and triggering the Terminator wars.
  28. */
  29. class SelfAwareness
  30. {
  31. public:
  32. typedef std::multimap< unsigned long,InetAddress,std::less<unsigned long>,Utils::Mallocator< std::pair<const unsigned long,InetAddress> > > ExternalAddressList;
  33. explicit SelfAwareness(const RuntimeEnvironment *renv);
  34. /**
  35. * Called when a remote peer informs us of our external network address
  36. *
  37. * @param reporter Identity of reporting peer
  38. * @param receivedOnLocalAddress Local address on which report was received
  39. * @param reporterPhysicalAddress Physical address that reporting peer seems to have
  40. * @param myPhysicalAddress Physical address that peer says we have
  41. * @param trusted True if this peer is trusted as an authority to inform us of external address changes
  42. * @param now Current time
  43. */
  44. void iam(void *tPtr,const Identity &reporter,int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now);
  45. /**
  46. * Clean up database periodically
  47. *
  48. * @param now Current time
  49. */
  50. void clean(int64_t now);
  51. /**
  52. * Get external address consensus, which is the statistical "mode" of external addresses.
  53. *
  54. * @param now Current time
  55. * @return Map of count to IP/port representing how many endpoints reported each address
  56. */
  57. ExternalAddressList externalAddresses(int64_t now) const;
  58. private:
  59. struct PhySurfaceKey
  60. {
  61. Address reporter;
  62. int64_t receivedOnLocalSocket;
  63. InetAddress reporterPhysicalAddress;
  64. InetAddress::IpScope scope;
  65. ZT_INLINE PhySurfaceKey() noexcept {} // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init,hicpp-use-equals-default,modernize-use-equals-default)
  66. ZT_INLINE PhySurfaceKey(const Address &r,const int64_t rol,const InetAddress &ra,InetAddress::IpScope s) noexcept : reporter(r),receivedOnLocalSocket(rol),reporterPhysicalAddress(ra),scope(s) {}
  67. ZT_INLINE unsigned long hashCode() const noexcept { return ((unsigned long)reporter.toInt() + (unsigned long)receivedOnLocalSocket + (unsigned long)scope); }
  68. ZT_INLINE bool operator==(const PhySurfaceKey &k) const noexcept { return ((reporter == k.reporter) && (receivedOnLocalSocket == k.receivedOnLocalSocket) && (reporterPhysicalAddress == k.reporterPhysicalAddress) && (scope == k.scope)); }
  69. ZT_INLINE bool operator!=(const PhySurfaceKey &k) const noexcept { return (!(*this == k)); }
  70. ZT_INLINE bool operator<(const PhySurfaceKey &k) const noexcept
  71. {
  72. if (reporter < k.reporter) {
  73. return true;
  74. } else if (reporter == k.reporter) {
  75. if (receivedOnLocalSocket < k.receivedOnLocalSocket) {
  76. return true;
  77. } else if (receivedOnLocalSocket == k.receivedOnLocalSocket) {
  78. if (reporterPhysicalAddress < k.reporterPhysicalAddress) {
  79. return true;
  80. } else if (reporterPhysicalAddress == k.reporterPhysicalAddress) {
  81. return scope < k.scope;
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. };
  88. struct PhySurfaceEntry
  89. {
  90. InetAddress mySurface;
  91. uint64_t ts;
  92. bool trusted;
  93. ZT_INLINE PhySurfaceEntry() noexcept : mySurface(),ts(0),trusted(false) {}
  94. ZT_INLINE PhySurfaceEntry(const InetAddress &a,const uint64_t t) noexcept : mySurface(a),ts(t),trusted(false) {}
  95. };
  96. const RuntimeEnvironment *RR;
  97. Map< PhySurfaceKey,PhySurfaceEntry > _phy;
  98. Mutex _phy_l;
  99. };
  100. } // namespace ZeroTier
  101. #endif