SelfAwareness.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <set>
  20. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  21. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 300000
  22. namespace ZeroTier {
  23. class _ResetWithinScope
  24. {
  25. public:
  26. ZT_INLINE _ResetWithinScope(void *tPtr,int64_t now,int inetAddressFamily,InetAddress::IpScope scope) :
  27. _now(now),
  28. _tPtr(tPtr),
  29. _family(inetAddressFamily),
  30. _scope(scope) {}
  31. ZT_INLINE void operator()(const SharedPtr<Peer> &p) { p->resetWithinScope(_tPtr,_scope,_family,_now); }
  32. private:
  33. int64_t _now;
  34. void *_tPtr;
  35. int _family;
  36. InetAddress::IpScope _scope;
  37. };
  38. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  39. RR(renv)
  40. {
  41. }
  42. void SelfAwareness::iam(void *tPtr,const Identity &reporter,const int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now)
  43. {
  44. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  45. if ((scope != reporterPhysicalAddress.ipScope())||(scope == InetAddress::IP_SCOPE_NONE)||(scope == InetAddress::IP_SCOPE_LOOPBACK)||(scope == InetAddress::IP_SCOPE_MULTICAST))
  46. return;
  47. Mutex::Lock l(_phy_l);
  48. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter.address(),receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  49. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  50. // Changes to external surface reported by trusted peers causes path reset in this scope
  51. entry.mySurface = myPhysicalAddress;
  52. entry.ts = now;
  53. entry.trusted = trusted;
  54. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  55. // due to multiple reports of endpoint change.
  56. // Don't use 'entry' after this since hash table gets modified.
  57. for(Map<PhySurfaceKey,PhySurfaceEntry>::iterator i(_phy.begin());i!=_phy.end();) { // NOLINT(modernize-loop-convert,modernize-use-auto,hicpp-use-auto)
  58. if ((i->first.scope == scope)&&(i->first.reporterPhysicalAddress != reporterPhysicalAddress))
  59. _phy.erase(i++);
  60. else ++i;
  61. }
  62. // Reset all paths within this scope and address family
  63. _ResetWithinScope rset(tPtr,now,myPhysicalAddress.family(),(InetAddress::IpScope)scope);
  64. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  65. RR->t->resettingPathsInScope(tPtr,0x9afff100,reporter,reporterPhysicalAddress,entry.mySurface,myPhysicalAddress,scope);
  66. } else {
  67. // Otherwise just update DB to use to determine external surface info
  68. entry.mySurface = myPhysicalAddress;
  69. entry.ts = now;
  70. entry.trusted = trusted;
  71. }
  72. }
  73. void SelfAwareness::clean(int64_t now)
  74. {
  75. Mutex::Lock l(_phy_l);
  76. for(Map<PhySurfaceKey,PhySurfaceEntry>::iterator i(_phy.begin());i!=_phy.end();) { // NOLINT(modernize-loop-convert,modernize-use-auto,hicpp-use-auto)
  77. if ((now - i->second.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  78. _phy.erase(i++);
  79. else ++i;
  80. }
  81. }
  82. SelfAwareness::ExternalAddressList SelfAwareness::externalAddresses(const int64_t now) const
  83. {
  84. SelfAwareness::ExternalAddressList r;
  85. Map<InetAddress,unsigned long> counts;
  86. {
  87. Mutex::Lock l(_phy_l);
  88. for(Map<PhySurfaceKey,PhySurfaceEntry>::const_iterator i(_phy.begin());i!=_phy.end();++i) { // NOLINT(modernize-loop-convert,modernize-use-auto,hicpp-use-auto)
  89. if ((now - i->second.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  90. ++counts[i->second.mySurface];
  91. }
  92. }
  93. for(Map<InetAddress,unsigned long>::iterator i(counts.begin());i!=counts.end();++i) // NOLINT(modernize-loop-convert,modernize-use-auto,hicpp-use-auto)
  94. r.insert(std::pair<unsigned long,InetAddress>(i->second,i->first));
  95. return r;
  96. }
  97. } // namespace ZeroTier