SelfAwareness.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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,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. _ResetWithinScope rset(RR,now,(InetAddress::IpScope)scope);
  93. RR->topology->eachPeer<_ResetWithinScope &>(rset);
  94. // For all peers for whom we forgot an address, send a packet indirectly if
  95. // they are still considered alive so that we will re-establish direct links.
  96. SharedPtr<Peer> sn(RR->topology->getBestSupernode());
  97. if (sn) {
  98. Path *snp = sn->getBestPath(now);
  99. if (snp) {
  100. for(std::vector< SharedPtr<Peer> >::const_iterator p(rset.peersReset.begin());p!=rset.peersReset.end();++p) {
  101. if ((*p)->alive(now)) {
  102. 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());
  103. Packet outp((*p)->address(),RR->identity.address(),Packet::VERB_NOP);
  104. outp.armor((*p)->key(),true);
  105. snp->send(RR,outp.data(),outp.size(),now);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. void SelfAwareness::clean(uint64_t now)
  113. {
  114. Mutex::Lock _l(_phy_m);
  115. for(std::map< PhySurfaceKey,PhySurfaceEntry >::iterator p(_phy.begin());p!=_phy.end();) {
  116. if ((now - p->second.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
  117. _phy.erase(p++);
  118. else ++p;
  119. }
  120. }
  121. } // namespace ZeroTier