SelfAwareness.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <set>
  30. #include <vector>
  31. #include "Constants.hpp"
  32. #include "SelfAwareness.hpp"
  33. #include "RuntimeEnvironment.hpp"
  34. #include "Node.hpp"
  35. #include "Topology.hpp"
  36. #include "Packet.hpp"
  37. #include "Peer.hpp"
  38. #include "Switch.hpp"
  39. #include "Trace.hpp"
  40. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  41. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 600000
  42. namespace ZeroTier {
  43. class _ResetWithinScope
  44. {
  45. public:
  46. _ResetWithinScope(void *tPtr,int64_t now,int inetAddressFamily,InetAddress::IpScope scope) :
  47. _now(now),
  48. _tPtr(tPtr),
  49. _family(inetAddressFamily),
  50. _scope(scope) {}
  51. inline void operator()(Topology &t,const SharedPtr<Peer> &p) { p->resetWithinScope(_tPtr,_scope,_family,_now); }
  52. private:
  53. uint64_t _now;
  54. void *_tPtr;
  55. int _family;
  56. InetAddress::IpScope _scope;
  57. };
  58. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  59. RR(renv),
  60. _phy(128)
  61. {
  62. }
  63. void SelfAwareness::iam(void *tPtr,const Address &reporter,const int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now)
  64. {
  65. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  66. if ((scope != reporterPhysicalAddress.ipScope())||(scope == InetAddress::IP_SCOPE_NONE)||(scope == InetAddress::IP_SCOPE_LOOPBACK)||(scope == InetAddress::IP_SCOPE_MULTICAST))
  67. return;
  68. Mutex::Lock _l(_phy_m);
  69. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,receivedOnLocalSocket,reporterPhysicalAddress,scope)];
  70. if ( (trusted) && ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  71. // Changes to external surface reported by trusted peers causes path reset in this scope
  72. RR->t->resettingPathsInScope(tPtr,reporter,reporterPhysicalAddress,myPhysicalAddress,scope);
  73. entry.mySurface = myPhysicalAddress;
  74. entry.ts = now;
  75. entry.trusted = trusted;
  76. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  77. // due to multiple reports of endpoint change.
  78. // Don't use 'entry' after this since hash table gets modified.
  79. {
  80. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  81. PhySurfaceKey *k = (PhySurfaceKey *)0;
  82. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  83. while (i.next(k,e)) {
  84. if ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope))
  85. _phy.erase(*k);
  86. }
  87. }
  88. // Reset all paths within this scope and address family
  89. _ResetWithinScope rset(tPtr,now,myPhysicalAddress.ss_family,(InetAddress::IpScope)scope);
  90. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  91. } else {
  92. // Otherwise just update DB to use to determine external surface info
  93. entry.mySurface = myPhysicalAddress;
  94. entry.ts = now;
  95. entry.trusted = trusted;
  96. }
  97. }
  98. void SelfAwareness::clean(int64_t now)
  99. {
  100. Mutex::Lock _l(_phy_m);
  101. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  102. PhySurfaceKey *k = (PhySurfaceKey *)0;
  103. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  104. while (i.next(k,e)) {
  105. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  106. _phy.erase(*k);
  107. }
  108. }
  109. } // namespace ZeroTier