SelfAwareness.hpp 2.8 KB

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