SelfAwareness.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "Constants.hpp"
  31. #include "SelfAwareness.hpp"
  32. #include "RuntimeEnvironment.hpp"
  33. #include "Node.hpp"
  34. #include "Topology.hpp"
  35. #include "Packet.hpp"
  36. #include "Peer.hpp"
  37. #include "Switch.hpp"
  38. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  39. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 3600000
  40. namespace ZeroTier {
  41. class _ResetWithinScope
  42. {
  43. public:
  44. _ResetWithinScope(const RuntimeEnvironment *renv,uint64_t now,InetAddress::IpScope scope) :
  45. RR(renv),
  46. _now(now),
  47. _scope(scope) {}
  48. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  49. {
  50. if (p->resetWithinScope(RR,_scope,_now))
  51. peersReset.push_back(p);
  52. }
  53. std::vector< SharedPtr<Peer> > peersReset;
  54. private:
  55. const RuntimeEnvironment *RR;
  56. uint64_t _now;
  57. InetAddress::IpScope _scope;
  58. };
  59. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  60. RR(renv),
  61. _phy(32)
  62. {
  63. }
  64. SelfAwareness::~SelfAwareness()
  65. {
  66. }
  67. void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,uint64_t now)
  68. {
  69. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  70. // This would be weird, e.g. a public IP talking to 10.0.0.1, so just ignore it.
  71. // If your network is this weird it's probably not reliable information.
  72. if (scope != reporterPhysicalAddress.ipScope())
  73. return;
  74. // Some scopes we ignore, and global scope IPs are only used for this
  75. // mechanism if they come from someone we trust (e.g. a root).
  76. switch(scope) {
  77. case InetAddress::IP_SCOPE_NONE:
  78. case InetAddress::IP_SCOPE_LOOPBACK:
  79. case InetAddress::IP_SCOPE_MULTICAST:
  80. return;
  81. case InetAddress::IP_SCOPE_GLOBAL:
  82. if (!trusted)
  83. return;
  84. break;
  85. default:
  86. break;
  87. }
  88. Mutex::Lock _l(_phy_m);
  89. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,reporterPhysicalAddress,scope)];
  90. if ( ((now - entry.ts) < ZT_SELFAWARENESS_ENTRY_TIMEOUT) && (!entry.mySurface.ipsEqual(myPhysicalAddress)) ) {
  91. entry.mySurface = myPhysicalAddress;
  92. entry.ts = now;
  93. TRACE("physical address %s for scope %u as seen from %s(%s) differs from %s, resetting paths in scope",myPhysicalAddress.toString().c_str(),(unsigned int)scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str(),entry.mySurface.toString().c_str());
  94. // Erase all entries in this scope that were not reported from this remote address to prevent 'thrashing'
  95. // due to multiple reports of endpoint change.
  96. // Don't use 'entry' after this since hash table gets modified.
  97. {
  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 ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope))
  103. _phy.erase(*k);
  104. }
  105. }
  106. // Reset all paths within this scope
  107. _ResetWithinScope rset(RR,now,(InetAddress::IpScope)scope);
  108. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  109. // Send a NOP to all peers for whom we forgot a path. This will cause direct
  110. // links to be re-established if possible, possibly using a root server or some
  111. // other relay.
  112. for(std::vector< SharedPtr<Peer> >::const_iterator p(rset.peersReset.begin());p!=rset.peersReset.end();++p) {
  113. if ((*p)->alive(now)) {
  114. Packet outp((*p)->address(),RR->identity.address(),Packet::VERB_NOP);
  115. RR->sw->send(outp,true,0);
  116. }
  117. }
  118. } else {
  119. entry.mySurface = myPhysicalAddress;
  120. entry.ts = now;
  121. }
  122. }
  123. void SelfAwareness::clean(uint64_t now)
  124. {
  125. Mutex::Lock _l(_phy_m);
  126. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  127. PhySurfaceKey *k = (PhySurfaceKey *)0;
  128. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  129. while (i.next(k,e)) {
  130. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  131. _phy.erase(*k);
  132. }
  133. }
  134. } // namespace ZeroTier