SelfAwareness.cpp 3.9 KB

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