SelfAwareness.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_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_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. {
  43. }
  44. void SelfAwareness::iam(void *tPtr,const Identity &reporter,const int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now)
  45. {
  46. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  47. if ((scope != reporterPhysicalAddress.ipScope())||(scope == InetAddress::IP_SCOPE_NONE)||(scope == InetAddress::IP_SCOPE_LOOPBACK)||(scope == InetAddress::IP_SCOPE_MULTICAST))
  48. return;
  49. Mutex::Lock l(_phy_l);
  50. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter.address(),receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  51. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  52. // Changes to external surface reported by trusted peers causes path reset in this scope
  53. entry.mySurface = myPhysicalAddress;
  54. entry.ts = now;
  55. entry.trusted = trusted;
  56. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  57. // due to multiple reports of endpoint change.
  58. // Don't use 'entry' after this since hash table gets modified.
  59. for(Map<PhySurfaceKey,PhySurfaceEntry>::iterator i(_phy.begin());i!=_phy.end();) {
  60. if ((i->first.scope == scope)&&(i->first.reporterPhysicalAddress != reporterPhysicalAddress))
  61. _phy.erase(i++);
  62. else ++i;
  63. }
  64. // Reset all paths within this scope and address family
  65. _ResetWithinScope rset(tPtr,now,myPhysicalAddress.family(),(InetAddress::IpScope)scope);
  66. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  67. RR->t->resettingPathsInScope(tPtr,0x9afff100,reporter,reporterPhysicalAddress,entry.mySurface,myPhysicalAddress,scope);
  68. } else {
  69. // Otherwise just update DB to use to determine external surface info
  70. entry.mySurface = myPhysicalAddress;
  71. entry.ts = now;
  72. entry.trusted = trusted;
  73. }
  74. }
  75. void SelfAwareness::clean(int64_t now)
  76. {
  77. Mutex::Lock l(_phy_l);
  78. for(Map<PhySurfaceKey,PhySurfaceEntry>::iterator i(_phy.begin());i!=_phy.end();) {
  79. if ((now - i->second.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  80. _phy.erase(i++);
  81. else ++i;
  82. }
  83. }
  84. std::multimap<unsigned long,InetAddress> SelfAwareness::externalAddresses(const int64_t now) const
  85. {
  86. std::multimap<unsigned long,InetAddress> r;
  87. Map<InetAddress,unsigned long> counts;
  88. {
  89. Mutex::Lock l(_phy_l);
  90. for(Map<PhySurfaceKey,PhySurfaceEntry>::const_iterator i(_phy.begin());i!=_phy.end();++i) {
  91. if ((now - i->second.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  92. ++counts[i->second.mySurface];
  93. }
  94. }
  95. for(Map<InetAddress,unsigned long>::iterator i(counts.begin());i!=counts.end();++i)
  96. r.insert(std::pair<unsigned long,InetAddress>(i->second,i->first));
  97. return r;
  98. }
  99. } // namespace ZeroTier