SelfAwareness.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "Hashtable.hpp"
  18. #include "Address.hpp"
  19. #include "Mutex.hpp"
  20. #include <map>
  21. namespace ZeroTier {
  22. class RuntimeEnvironment;
  23. /**
  24. * SelfAwareness manages awareness of this peer's external address(es) and NAT situation.
  25. *
  26. * This code should not be capable of achieving sentience and triggering the Terminator wars.
  27. */
  28. class SelfAwareness
  29. {
  30. public:
  31. explicit SelfAwareness(const RuntimeEnvironment *renv);
  32. ~SelfAwareness();
  33. /**
  34. * Called when a remote peer informs us of our external network address
  35. *
  36. * @param reporter ZeroTier address of reporting peer
  37. * @param receivedOnLocalAddress Local address on which report was received
  38. * @param reporterPhysicalAddress Physical address that reporting peer seems to have
  39. * @param myPhysicalAddress Physical address that peer says we have
  40. * @param trusted True if this peer is trusted as an authority to inform us of external address changes
  41. * @param now Current time
  42. */
  43. void iam(void *tPtr,const Address &reporter,int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now);
  44. /**
  45. * Clean up database periodically
  46. *
  47. * @param now Current time
  48. */
  49. void clean(int64_t now);
  50. /**
  51. * Check whether this node appears to be behind a symmetric NAT
  52. *
  53. * @param now Current time
  54. * @return True if it looks like we're behind a symmetric NAT
  55. */
  56. bool symmetricNat(int64_t now) const;
  57. /**
  58. * Get external address consensus, which is the statistical "mode" of external addresses.
  59. *
  60. * @param now Current time
  61. * @return Map of count to IP/port representing how many endpoints reported each address
  62. */
  63. std::multimap<unsigned long,InetAddress> externalAddresses(int64_t now) const;
  64. private:
  65. struct PhySurfaceKey
  66. {
  67. Address reporter;
  68. int64_t receivedOnLocalSocket;
  69. InetAddress reporterPhysicalAddress;
  70. InetAddress::IpScope scope;
  71. ZT_ALWAYS_INLINE PhySurfaceKey() {}
  72. ZT_ALWAYS_INLINE PhySurfaceKey(const Address &r,const int64_t rol,const InetAddress &ra,InetAddress::IpScope s) : reporter(r),receivedOnLocalSocket(rol),reporterPhysicalAddress(ra),scope(s) {}
  73. ZT_ALWAYS_INLINE unsigned long hashCode() const { return ((unsigned long)reporter.toInt() + (unsigned long)receivedOnLocalSocket + (unsigned long)scope); }
  74. ZT_ALWAYS_INLINE bool operator==(const PhySurfaceKey &k) const { return ((reporter == k.reporter)&&(receivedOnLocalSocket == k.receivedOnLocalSocket)&&(reporterPhysicalAddress == k.reporterPhysicalAddress)&&(scope == k.scope)); }
  75. ZT_ALWAYS_INLINE bool operator!=(const PhySurfaceKey &k) const { return (!(*this == k)); }
  76. };
  77. struct PhySurfaceEntry
  78. {
  79. InetAddress mySurface;
  80. uint64_t ts;
  81. bool trusted;
  82. ZT_ALWAYS_INLINE PhySurfaceEntry() : mySurface(),ts(0),trusted(false) {}
  83. ZT_ALWAYS_INLINE PhySurfaceEntry(const InetAddress &a,const uint64_t t) : mySurface(a),ts(t),trusted(false) {}
  84. };
  85. const RuntimeEnvironment *RR;
  86. Hashtable< PhySurfaceKey,PhySurfaceEntry > _phy;
  87. Mutex _phy_l;
  88. };
  89. } // namespace ZeroTier
  90. #endif