SelfAwareness.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "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. _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(m_phy_l);
  78. for (Map< p_PhySurfaceKey, p_PhySurfaceEntry >::iterator i(m_phy.begin()); i != m_phy.end();) {
  79. if ((now - i->second.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  80. m_phy.erase(i++);
  81. else ++i;
  82. }
  83. }
  84. MultiMap< unsigned int, InetAddress > SelfAwareness::externalAddresses(const int64_t now) const
  85. {
  86. MultiMap< unsigned int, InetAddress > r;
  87. // Count endpoints reporting each IP/port combo
  88. Map< InetAddress, unsigned long > counts;
  89. {
  90. Mutex::Lock l(m_phy_l);
  91. for (Map< p_PhySurfaceKey, p_PhySurfaceEntry >::const_iterator i(m_phy.begin()); i != m_phy.end(); ++i) {
  92. if ((now - i->second.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  93. ++counts[i->second.mySurface];
  94. }
  95. }
  96. // Invert to create a map from count to address
  97. for (Map< InetAddress, unsigned long >::iterator i(counts.begin()); i != counts.end(); ++i)
  98. r.insert(std::pair< unsigned long, InetAddress >(i->second, i->first));
  99. return r;
  100. }
  101. } // namespace ZeroTier