Peer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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 "Peer.hpp"
  28. #include "Switch.hpp"
  29. #include <algorithm>
  30. namespace ZeroTier {
  31. Peer::Peer() :
  32. _lastUsed(0),
  33. _lastUnicastFrame(0),
  34. _lastMulticastFrame(0),
  35. _lastAnnouncedTo(0),
  36. _vMajor(0),
  37. _vMinor(0),
  38. _vRevision(0),
  39. _latency(0) {}
  40. Peer::Peer(const Identity &myIdentity,const Identity &peerIdentity)
  41. throw(std::runtime_error) :
  42. _id(peerIdentity),
  43. _lastUsed(0),
  44. _lastUnicastFrame(0),
  45. _lastMulticastFrame(0),
  46. _lastAnnouncedTo(0),
  47. _vMajor(0),
  48. _vMinor(0),
  49. _vRevision(0),
  50. _latency(0)
  51. {
  52. if (!myIdentity.agree(peerIdentity,_key,ZT_PEER_SECRET_KEY_LENGTH))
  53. throw std::runtime_error("new peer identity key agreement failed");
  54. }
  55. void Peer::onReceive(
  56. const RuntimeEnvironment *_r,
  57. const SharedPtr<Socket> &fromSock,
  58. const InetAddress &remoteAddr,
  59. unsigned int hops,
  60. uint64_t packetId,
  61. Packet::Verb verb,
  62. uint64_t inRePacketId,
  63. Packet::Verb inReVerb,
  64. uint64_t now)
  65. {
  66. Mutex::Lock _l(_lock);
  67. if (!hops) { // direct packet
  68. // Update receive time on known paths
  69. bool havePath = false;
  70. for(std::vector<Path>::iterator p(_paths.begin());p!=_paths.end();++p) {
  71. if ((p->address() == remoteAddr)&&(p->tcp() == (fromSock->type() == Socket::ZT_SOCKET_TYPE_TCP))) {
  72. p->received(now);
  73. havePath = true;
  74. break;
  75. }
  76. }
  77. // Learn new UDP paths (learning TCP would require an explicit mechanism)
  78. if ((!havePath)&&(fromSock->type() != Socket::ZT_SOCKET_TYPE_TCP)) {
  79. _paths.push_back(Path(remoteAddr,false,false));
  80. _paths.back().received(now);
  81. }
  82. // Announce multicast LIKEs to peers to whom we have a direct link
  83. if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {
  84. _lastAnnouncedTo = now;
  85. _r->sw->announceMulticastGroups(SharedPtr<Peer>(this));
  86. }
  87. }
  88. if (verb == Packet::VERB_FRAME) {
  89. _lastUnicastFrame = now;
  90. } else if (verb == Packet::VERB_MULTICAST_FRAME) {
  91. _lastMulticastFrame = now;
  92. }
  93. }
  94. bool Peer::send(const RuntimeEnvironment *_r,const void *data,unsigned int len,uint64_t now)
  95. {
  96. Mutex::Lock _l(_lock);
  97. if (_paths.empty())
  98. return false;
  99. uint64_t bestPathLastReceived = 0;
  100. std::vector<Path>::iterator bestPath;
  101. for(std::vector<Path>::iterator p(_paths.begin());p!=_paths.end();++p) {
  102. uint64_t lr = p->lastRecevied();
  103. if (lr >= bestPathLastReceived) {
  104. bestPathLastReceived = lr;
  105. bestPath = p;
  106. }
  107. }
  108. if (_r->sm->send(bestPath->address(),bestPath->tcp(),data,len)) {
  109. bestPath->sent(now);
  110. return true;
  111. }
  112. return false;
  113. }
  114. bool Peer::sendFirewallOpener(const RuntimeEnvironment *_r,uint64_t now)
  115. {
  116. bool sent = false;
  117. Mutex::Lock _l(_lock);
  118. for(std::vector<Path>::iterator p(_paths.begin());p!=_paths.end();++p) {
  119. if (!p->tcp())
  120. sent |= _r->sm->sendFirewallOpener(p->address(),ZT_FIREWALL_OPENER_HOPS);
  121. }
  122. return sent;
  123. }
  124. bool Peer::sendPing(const RuntimeEnvironment *_r,uint64_t now,bool firstSinceReset)
  125. {
  126. bool sent = false;
  127. SharedPtr<Peer> self(this);
  128. Mutex::Lock _l(_lock);
  129. bool allPingsUnanswered;
  130. if (!firstSinceReset) {
  131. allPingsUnanswered = true;
  132. for(std::vector<Path>::iterator p(_paths.begin());p!=_paths.end();++p) {
  133. if (!p->pingUnanswered(now)) {
  134. allPingsUnanswered = false;
  135. break;
  136. }
  137. }
  138. } else allPingsUnanswered = false;
  139. for(std::vector<Path>::iterator p(_paths.begin());p!=_paths.end();++p) {
  140. if ((allPingsUnanswered)||(!p->tcp())) {
  141. if (_r->sw->sendHELLO(self,p->address(),p->tcp())) {
  142. p->sent(now);
  143. p->pinged(now);
  144. sent = true;
  145. }
  146. }
  147. }
  148. return sent;
  149. }
  150. } // namespace ZeroTier