SelfAwareness.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <cstdlib>
  14. #include <cstring>
  15. #include <set>
  16. #include "Constants.hpp"
  17. #include "SelfAwareness.hpp"
  18. #include "RuntimeEnvironment.hpp"
  19. #include "Topology.hpp"
  20. #include "Peer.hpp"
  21. #include "Switch.hpp"
  22. #include "Trace.hpp"
  23. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  24. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 300000
  25. namespace ZeroTier {
  26. class _ResetWithinScope
  27. {
  28. public:
  29. ZT_ALWAYS_INLINE _ResetWithinScope(void *tPtr,int64_t now,int inetAddressFamily,InetAddress::IpScope scope) :
  30. _now(now),
  31. _tPtr(tPtr),
  32. _family(inetAddressFamily),
  33. _scope(scope) {}
  34. ZT_ALWAYS_INLINE void operator()(const SharedPtr<Peer> &p) { p->resetWithinScope(_tPtr,_scope,_family,_now); }
  35. private:
  36. int64_t _now;
  37. void *_tPtr;
  38. int _family;
  39. InetAddress::IpScope _scope;
  40. };
  41. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  42. RR(renv),
  43. _phy(256)
  44. {
  45. }
  46. void SelfAwareness::iam(void *tPtr,const Identity &reporter,const int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now)
  47. {
  48. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  49. if ((scope != reporterPhysicalAddress.ipScope())||(scope == InetAddress::IP_SCOPE_NONE)||(scope == InetAddress::IP_SCOPE_LOOPBACK)||(scope == InetAddress::IP_SCOPE_MULTICAST))
  50. return;
  51. Mutex::Lock l(_phy_l);
  52. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter.address(),receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  53. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  54. // Changes to external surface reported by trusted peers causes path reset in this scope
  55. entry.mySurface = myPhysicalAddress;
  56. entry.ts = now;
  57. entry.trusted = trusted;
  58. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  59. // due to multiple reports of endpoint change.
  60. // Don't use 'entry' after this since hash table gets modified.
  61. {
  62. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  63. PhySurfaceKey *k = nullptr;
  64. PhySurfaceEntry *e = nullptr;
  65. while (i.next(k,e)) {
  66. if ((k->scope == scope)&&(k->reporterPhysicalAddress != reporterPhysicalAddress))
  67. _phy.erase(*k);
  68. }
  69. }
  70. // Reset all paths within this scope and address family
  71. _ResetWithinScope rset(tPtr,now,myPhysicalAddress.ss_family,(InetAddress::IpScope)scope);
  72. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  73. RR->t->resettingPathsInScope(tPtr,reporter,reporterPhysicalAddress,entry.mySurface,myPhysicalAddress,scope);
  74. } else {
  75. // Otherwise just update DB to use to determine external surface info
  76. entry.mySurface = myPhysicalAddress;
  77. entry.ts = now;
  78. entry.trusted = trusted;
  79. }
  80. }
  81. void SelfAwareness::clean(int64_t now)
  82. {
  83. Mutex::Lock l(_phy_l);
  84. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  85. PhySurfaceKey *k = nullptr;
  86. PhySurfaceEntry *e = nullptr;
  87. while (i.next(k,e)) {
  88. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  89. _phy.erase(*k);
  90. }
  91. }
  92. std::multimap<unsigned long,InetAddress> SelfAwareness::externalAddresses(const int64_t now) const
  93. {
  94. std::multimap<unsigned long,InetAddress> r;
  95. Hashtable<InetAddress,unsigned long> counts(16);
  96. {
  97. Mutex::Lock l(_phy_l);
  98. Hashtable<PhySurfaceKey,PhySurfaceEntry>::Iterator i(const_cast<SelfAwareness *>(this)->_phy);
  99. PhySurfaceKey *k = nullptr;
  100. PhySurfaceEntry *e = nullptr;
  101. while (i.next(k,e)) {
  102. if ((now - e->ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  103. ++counts[e->mySurface];
  104. }
  105. }
  106. Hashtable<InetAddress,unsigned long>::Iterator i(counts);
  107. InetAddress *k = nullptr;
  108. unsigned long *c = nullptr;
  109. while (i.next(k,c))
  110. r.insert(std::pair<unsigned long,InetAddress>(*c,*k));
  111. return r;
  112. }
  113. } // namespace ZeroTier