SelfAwareness.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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: 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. #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 "Containers.hpp"
  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. {}
  32. ZT_INLINE void operator()(const SharedPtr< Peer > &p)
  33. { 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(m_phy_l);
  50. p_PhySurfaceEntry &entry = m_phy[p_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< p_PhySurfaceKey, p_PhySurfaceEntry >::iterator i(m_phy.begin()); i != m_phy.end();) {
  60. if ((i->first.scope == scope) && (i->first.reporterPhysicalAddress != reporterPhysicalAddress))
  61. m_phy.erase(i++);
  62. else ++i;
  63. }
  64. // Reset all paths within this scope and address family
  65. Vector< SharedPtr< Peer > > peers, rootPeers;
  66. RR->topology->allPeers(peers, rootPeers);
  67. for(Vector< SharedPtr< Peer > >::const_iterator p(peers.begin());p!=peers.end();++p)
  68. (*p)->resetWithinScope(tPtr, (InetAddress::IpScope)scope, myPhysicalAddress.family(), now);
  69. RR->t->resettingPathsInScope(tPtr, 0x9afff100, reporter, reporterPhysicalAddress, entry.mySurface, myPhysicalAddress, scope);
  70. } else {
  71. // Otherwise just update DB to use to determine external surface info
  72. entry.mySurface = myPhysicalAddress;
  73. entry.ts = now;
  74. entry.trusted = trusted;
  75. }
  76. }
  77. void SelfAwareness::clean(int64_t now)
  78. {
  79. Mutex::Lock l(m_phy_l);
  80. for (Map< p_PhySurfaceKey, p_PhySurfaceEntry >::iterator i(m_phy.begin()); i != m_phy.end();) {
  81. if ((now - i->second.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  82. m_phy.erase(i++);
  83. else ++i;
  84. }
  85. }
  86. MultiMap< unsigned int, InetAddress > SelfAwareness::externalAddresses(const int64_t now) const
  87. {
  88. MultiMap< unsigned int, InetAddress > r;
  89. // Count endpoints reporting each IP/port combo
  90. Map< InetAddress, unsigned long > counts;
  91. {
  92. Mutex::Lock l(m_phy_l);
  93. for (Map< p_PhySurfaceKey, p_PhySurfaceEntry >::const_iterator i(m_phy.begin()); i != m_phy.end(); ++i) {
  94. if ((now - i->second.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  95. ++counts[i->second.mySurface];
  96. }
  97. }
  98. // Invert to create a map from count to address
  99. for (Map< InetAddress, unsigned long >::iterator i(counts.begin()); i != counts.end(); ++i)
  100. r.insert(std::pair< unsigned long, InetAddress >(i->second, i->first));
  101. return r;
  102. }
  103. } // namespace ZeroTier