SelfAwareness.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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: 2025-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. * Clean up database periodically
  42. *
  43. * @param now Current time
  44. */
  45. void clean(int64_t now);
  46. private:
  47. struct PhySurfaceKey
  48. {
  49. Address reporter;
  50. int64_t receivedOnLocalSocket;
  51. InetAddress reporterPhysicalAddress;
  52. InetAddress::IpScope scope;
  53. PhySurfaceKey() : reporter(),scope(InetAddress::IP_SCOPE_NONE) {}
  54. PhySurfaceKey(const Address &r,const int64_t rol,const InetAddress &ra,InetAddress::IpScope s) : reporter(r),receivedOnLocalSocket(rol),reporterPhysicalAddress(ra),scope(s) {}
  55. inline unsigned long hashCode() const { return ((unsigned long)reporter.toInt() + (unsigned long)scope); }
  56. inline bool operator==(const PhySurfaceKey &k) const { return ((reporter == k.reporter)&&(receivedOnLocalSocket == k.receivedOnLocalSocket)&&(reporterPhysicalAddress == k.reporterPhysicalAddress)&&(scope == k.scope)); }
  57. };
  58. struct PhySurfaceEntry
  59. {
  60. InetAddress mySurface;
  61. uint64_t ts;
  62. bool trusted;
  63. PhySurfaceEntry() : mySurface(),ts(0),trusted(false) {}
  64. PhySurfaceEntry(const InetAddress &a,const uint64_t t) : mySurface(a),ts(t),trusted(false) {}
  65. };
  66. const RuntimeEnvironment *RR;
  67. Hashtable< PhySurfaceKey,PhySurfaceEntry > _phy;
  68. Mutex _phy_m;
  69. };
  70. } // namespace ZeroTier
  71. #endif