SelfAwareness.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: 2026-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. }
  56. Mutex::Lock _l(_phy_m);
  57. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  58. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  59. // Changes to external surface reported by trusted peers causes path reset in this scope
  60. RR->t->resettingPathsInScope(tPtr,reporter,reporterPhysicalAddress,myPhysicalAddress,scope);
  61. entry.mySurface = myPhysicalAddress;
  62. entry.ts = now;
  63. entry.trusted = trusted;
  64. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  65. // due to multiple reports of endpoint change.
  66. // Don't use 'entry' after this since hash table gets modified.
  67. {
  68. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  69. PhySurfaceKey *k = (PhySurfaceKey *)0;
  70. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  71. while (i.next(k,e)) {
  72. if ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope)) {
  73. _phy.erase(*k);
  74. }
  75. }
  76. }
  77. // Reset all paths within this scope and address family
  78. _ResetWithinScope rset(tPtr,now,myPhysicalAddress.ss_family,(InetAddress::IpScope)scope);
  79. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  80. } else {
  81. // Otherwise just update DB to use to determine external surface info
  82. entry.mySurface = myPhysicalAddress;
  83. entry.ts = now;
  84. entry.trusted = trusted;
  85. }
  86. }
  87. std::vector<InetAddress> SelfAwareness::whoami()
  88. {
  89. std::vector<InetAddress> surfaceAddresses;
  90. Mutex::Lock _l(_phy_m);
  91. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  92. PhySurfaceKey *k = (PhySurfaceKey *)0;
  93. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  94. while (i.next(k,e)) {
  95. if (std::find(surfaceAddresses.begin(), surfaceAddresses.end(), e->mySurface) == surfaceAddresses.end()) {
  96. surfaceAddresses.push_back(e->mySurface);
  97. }
  98. }
  99. return surfaceAddresses;
  100. }
  101. void SelfAwareness::clean(int64_t now)
  102. {
  103. Mutex::Lock _l(_phy_m);
  104. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  105. PhySurfaceKey *k = (PhySurfaceKey *)0;
  106. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  107. while (i.next(k,e)) {
  108. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT) {
  109. _phy.erase(*k);
  110. }
  111. }
  112. }
  113. } // namespace ZeroTier