SelfAwareness.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c)2019 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: 2026-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 "Address.hpp"
  16. #include "Constants.hpp"
  17. #include "Hashtable.hpp"
  18. #include "InetAddress.hpp"
  19. #include "Mutex.hpp"
  20. namespace ZeroTier {
  21. class RuntimeEnvironment;
  22. /**
  23. * Tracks changes to this peer's real world addresses
  24. */
  25. class SelfAwareness {
  26. public:
  27. SelfAwareness(const RuntimeEnvironment* renv);
  28. /**
  29. * Called when a trusted remote peer informs us of our external network address
  30. *
  31. * @param reporter ZeroTier address of reporting peer
  32. * @param receivedOnLocalAddress Local address on which report was received
  33. * @param reporterPhysicalAddress Physical address that reporting peer seems to have
  34. * @param myPhysicalAddress Physical address that peer says we have
  35. * @param trusted True if this peer is trusted as an authority to inform us of external address changes
  36. * @param now Current time
  37. */
  38. void iam(void* tPtr, const Address& reporter, const int64_t receivedOnLocalSocket, const InetAddress& reporterPhysicalAddress, const InetAddress& myPhysicalAddress, bool trusted, int64_t now);
  39. /**
  40. * Return all known external surface addresses reported by peers
  41. *
  42. * @return A vector of InetAddress(es)
  43. */
  44. std::vector<InetAddress> whoami();
  45. /**
  46. * Clean up database periodically
  47. *
  48. * @param now Current time
  49. */
  50. void clean(int64_t now);
  51. private:
  52. struct PhySurfaceKey {
  53. Address reporter;
  54. int64_t receivedOnLocalSocket;
  55. InetAddress reporterPhysicalAddress;
  56. InetAddress::IpScope scope;
  57. PhySurfaceKey() : reporter(), scope(InetAddress::IP_SCOPE_NONE)
  58. {
  59. }
  60. PhySurfaceKey(const Address& r, const int64_t rol, const InetAddress& ra, InetAddress::IpScope s) : reporter(r), receivedOnLocalSocket(rol), reporterPhysicalAddress(ra), scope(s)
  61. {
  62. }
  63. inline unsigned long hashCode() const
  64. {
  65. return ((unsigned long)reporter.toInt() + (unsigned long)scope);
  66. }
  67. inline bool operator==(const PhySurfaceKey& k) const
  68. {
  69. return ((reporter == k.reporter) && (receivedOnLocalSocket == k.receivedOnLocalSocket) && (reporterPhysicalAddress == k.reporterPhysicalAddress) && (scope == k.scope));
  70. }
  71. };
  72. struct PhySurfaceEntry {
  73. InetAddress mySurface;
  74. uint64_t ts;
  75. bool trusted;
  76. PhySurfaceEntry() : mySurface(), ts(0), trusted(false)
  77. {
  78. }
  79. PhySurfaceEntry(const InetAddress& a, const uint64_t t) : mySurface(a), ts(t), trusted(false)
  80. {
  81. }
  82. };
  83. const RuntimeEnvironment* RR;
  84. Hashtable<PhySurfaceKey, PhySurfaceEntry> _phy;
  85. Mutex _phy_m;
  86. };
  87. } // namespace ZeroTier
  88. #endif