SelfAwareness.cpp 3.9 KB

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