SelfAwareness.cpp 4.1 KB

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