SelfAwareness.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // Entry timeout -- make it fairly long since this is just to prevent stale buildup
  38. #define ZT_SELFAWARENESS_ENTRY_TIMEOUT 3600000
  39. namespace ZeroTier {
  40. class _ResetWithinScope
  41. {
  42. public:
  43. _ResetWithinScope(const RuntimeEnvironment *renv,uint64_t now,InetAddress::IpScope scope) :
  44. RR(renv),
  45. _now(now),
  46. _scope(scope) {}
  47. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  48. {
  49. if (p->resetWithinScope(RR,_scope,_now))
  50. peersReset.push_back(p);
  51. }
  52. std::vector< SharedPtr<Peer> > peersReset;
  53. private:
  54. const RuntimeEnvironment *RR;
  55. uint64_t _now;
  56. InetAddress::IpScope _scope;
  57. };
  58. SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
  59. RR(renv)
  60. {
  61. }
  62. SelfAwareness::~SelfAwareness()
  63. {
  64. }
  65. void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,uint64_t now)
  66. {
  67. const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
  68. switch(scope) {
  69. case InetAddress::IP_SCOPE_NONE:
  70. case InetAddress::IP_SCOPE_LOOPBACK:
  71. case InetAddress::IP_SCOPE_MULTICAST:
  72. return;
  73. case InetAddress::IP_SCOPE_GLOBAL:
  74. if ((!trusted)||(scope != reporterPhysicalAddress.ipScope()))
  75. return;
  76. break;
  77. default:
  78. if (scope != reporterPhysicalAddress.ipScope())
  79. return;
  80. break;
  81. }
  82. Mutex::Lock _l(_phy_m);
  83. PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,reporterPhysicalAddress,scope)];
  84. if ((now - entry.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT) {
  85. entry.mySurface = myPhysicalAddress;
  86. entry.ts = now;
  87. TRACE("learned physical address %s for scope %u as seen from %s(%s) (replaced <null>)",myPhysicalAddress.toString().c_str(),(unsigned int)scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str());
  88. } else if (entry.mySurface != myPhysicalAddress) {
  89. entry.mySurface = myPhysicalAddress;
  90. entry.ts = now;
  91. TRACE("learned physical address %s for scope %u as seen from %s(%s) (replaced %s, resetting all in scope)",myPhysicalAddress.toString().c_str(),(unsigned int)scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str(),entry.mySurface.toString().c_str());
  92. // Erase all entries in this scope that were not reported by this remote address to prevent 'thrashing'
  93. // due to multiple reports of endpoint change.
  94. // Don't use 'entry' after this since hash table gets modified.
  95. {
  96. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  97. PhySurfaceKey *k = (PhySurfaceKey *)0;
  98. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  99. while (i.next(k,e)) {
  100. if ((k->reporterPhysicalAddress != reporterPhysicalAddress)&&(k->scope == scope))
  101. _phy.erase(*k);
  102. }
  103. }
  104. _ResetWithinScope rset(RR,now,(InetAddress::IpScope)scope);
  105. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  106. // For all peers for whom we forgot an address, send a packet indirectly if
  107. // they are still considered alive so that we will re-establish direct links.
  108. SharedPtr<Peer> sn(RR->topology->getBestRoot());
  109. if (sn) {
  110. RemotePath *snp = sn->getBestPath(now);
  111. if (snp) {
  112. for(std::vector< SharedPtr<Peer> >::const_iterator p(rset.peersReset.begin());p!=rset.peersReset.end();++p) {
  113. if ((*p)->alive(now)) {
  114. TRACE("sending indirect NOP to %s via %s(%s) to re-establish link",(*p)->address().toString().c_str(),sn->address().toString().c_str(),snp->address().toString().c_str());
  115. Packet outp((*p)->address(),RR->identity.address(),Packet::VERB_NOP);
  116. outp.armor((*p)->key(),true);
  117. snp->send(RR,outp.data(),outp.size(),now);
  118. }
  119. }
  120. }
  121. }
  122. } else {
  123. entry.ts = now;
  124. }
  125. }
  126. void SelfAwareness::clean(uint64_t now)
  127. {
  128. Mutex::Lock _l(_phy_m);
  129. Hashtable< PhySurfaceKey,PhySurfaceEntry >::Iterator i(_phy);
  130. PhySurfaceKey *k = (PhySurfaceKey *)0;
  131. PhySurfaceEntry *e = (PhySurfaceEntry *)0;
  132. while (i.next(k,e)) {
  133. if ((now - e->ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  134. _phy.erase(*k);
  135. }
  136. }
  137. } // namespace ZeroTier