SelfAwareness.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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: 2023-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. ZT_ALWAYS_INLINE _ResetWithinScope(void *tPtr,int64_t now,int inetAddressFamily,InetAddress::IpScope scope) :
  34. _now(now),
  35. _tPtr(tPtr),
  36. _family(inetAddressFamily),
  37. _scope(scope) {}
  38. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Peer> &p)
  39. {
  40. p->resetWithinScope(_tPtr,_scope,_family,_now);
  41. return true;
  42. }
  43. private:
  44. uint64_t _now;
  45. void *_tPtr;
  46. int _family;
  47. InetAddress::IpScope _scope;
  48. };
  49. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  50. RR(renv),
  51. _phy(128) {}
  52. void SelfAwareness::iam(void *tPtr,const Address &reporter,const int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now)
  53. {
  54. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  55. if ((scope != reporterPhysicalAddress.ipScope())||(scope == InetAddress::IP_SCOPE_NONE)||(scope == InetAddress::IP_SCOPE_LOOPBACK)||(scope == InetAddress::IP_SCOPE_MULTICAST))
  56. return;
  57. Mutex::Lock _l(_phy_m);
  58. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  59. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  60. // Changes to external surface reported by trusted peers causes path reset in this scope
  61. RR->t->resettingPathsInScope(tPtr,reporter,reporterPhysicalAddress,myPhysicalAddress,scope);
  62. entry.mySurface = myPhysicalAddress;
  63. entry.ts = now;
  64. entry.trusted = trusted;
  65. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  66. // due to multiple reports of endpoint change.
  67. // Don't use 'entry' after this since hash table gets modified.
  68. {
  69. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  70. PhySurfaceKey *k = (PhySurfaceKey *)0;
  71. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  72. while (i.next(k,e)) {
  73. if ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope))
  74. _phy.erase(*k);
  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. void SelfAwareness::clean(int64_t now)
  88. {
  89. Mutex::Lock _l(_phy_m);
  90. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  91. PhySurfaceKey *k = (PhySurfaceKey *)0;
  92. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  93. while (i.next(k,e)) {
  94. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  95. _phy.erase(*k);
  96. }
  97. }
  98. } // namespace ZeroTier