Peer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. _id(),
  33. _ipv4p(),
  34. _ipv6p(),
  35. _lastUsed(0),
  36. _lastUnicastFrame(0),
  37. _lastMulticastFrame(0),
  38. _lastAnnouncedTo(0),
  39. _vMajor(0),
  40. _vMinor(0),
  41. _vRevision(0),
  42. _latency(0)
  43. {
  44. }
  45. Peer::Peer(const Identity &myIdentity,const Identity &peerIdentity)
  46. throw(std::runtime_error) :
  47. _id(peerIdentity),
  48. _ipv4p(),
  49. _ipv6p(),
  50. _lastUsed(0),
  51. _lastUnicastFrame(0),
  52. _lastMulticastFrame(0),
  53. _lastAnnouncedTo(0),
  54. _vMajor(0),
  55. _vMinor(0),
  56. _vRevision(0),
  57. _latency(0)
  58. {
  59. if (!myIdentity.agree(peerIdentity,_key,ZT_PEER_SECRET_KEY_LENGTH))
  60. throw std::runtime_error("new peer identity key agreement failed");
  61. }
  62. void Peer::onReceive(
  63. const RuntimeEnvironment *_r,
  64. const InetAddress &remoteAddr,
  65. unsigned int hops,
  66. uint64_t packetId,
  67. Packet::Verb verb,
  68. uint64_t inRePacketId,
  69. Packet::Verb inReVerb,
  70. uint64_t now)
  71. {
  72. if (!hops) { // direct packet
  73. // Update last receive info for our direct path
  74. WanPath *const wp = (remoteAddr.isV4() ? &_ipv4p : &_ipv6p);
  75. wp->lastReceive = now;
  76. if (!wp->fixed)
  77. wp->addr = remoteAddr;
  78. // Announce multicast LIKEs to peers to whom we have a direct link
  79. if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {
  80. _lastAnnouncedTo = now;
  81. _r->sw->announceMulticastGroups(SharedPtr<Peer>(this));
  82. }
  83. }
  84. if (verb == Packet::VERB_FRAME) {
  85. _lastUnicastFrame = now;
  86. } else if (verb == Packet::VERB_MULTICAST_FRAME) {
  87. _lastMulticastFrame = now;
  88. }
  89. }
  90. bool Peer::send(const RuntimeEnvironment *_r,const void *data,unsigned int len,uint64_t now)
  91. {
  92. if ((_ipv6p.isActive(now))||((!(_ipv4p.addr))&&(_ipv6p.addr))) {
  93. if (_r->sm->send(_ipv6p.addr,false,data,len)) {
  94. _ipv6p.lastSend = now;
  95. return true;
  96. }
  97. }
  98. if (_ipv4p.addr) {
  99. if (_r->sm->send(_ipv4p.addr,false,data,len)) {
  100. _ipv4p.lastSend = now;
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. bool Peer::sendFirewallOpener(const RuntimeEnvironment *_r,uint64_t now)
  107. {
  108. bool sent = false;
  109. if (_ipv4p.addr) {
  110. if (_r->sm->sendFirewallOpener(_ipv4p.addr,ZT_FIREWALL_OPENER_HOPS)) {
  111. _ipv4p.lastFirewallOpener = now;
  112. sent = true;
  113. }
  114. }
  115. if (_ipv6p.addr) {
  116. if (_r->sm->sendFirewallOpener(_ipv6p.addr,ZT_FIREWALL_OPENER_HOPS)) {
  117. _ipv6p.lastFirewallOpener = now;
  118. sent = true;
  119. }
  120. }
  121. return sent;
  122. }
  123. bool Peer::sendPing(const RuntimeEnvironment *_r,uint64_t now)
  124. {
  125. bool sent = false;
  126. if (_ipv4p.addr) {
  127. TRACE("PING %s(%s)",_id.address().toString().c_str(),_ipv4p.addr.toString().c_str());
  128. if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv4p.addr,false)) {
  129. _ipv4p.lastSend = now;
  130. sent = true;
  131. }
  132. }
  133. if (_ipv6p.addr) {
  134. TRACE("PING %s(%s)",_id.address().toString().c_str(),_ipv6p.addr.toString().c_str());
  135. if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv6p.addr,false)) {
  136. _ipv6p.lastSend = now;
  137. sent = true;
  138. }
  139. }
  140. return sent;
  141. }
  142. void Peer::setPathAddress(const InetAddress &addr,bool fixed)
  143. {
  144. if (addr.isV4()) {
  145. _ipv4p.addr = addr;
  146. _ipv4p.fixed = fixed;
  147. } else if (addr.isV6()) {
  148. _ipv6p.addr = addr;
  149. _ipv6p.fixed = fixed;
  150. }
  151. }
  152. void Peer::clearFixedFlag(InetAddress::AddressType t)
  153. {
  154. switch(t) {
  155. case InetAddress::TYPE_NULL:
  156. _ipv4p.fixed = false;
  157. _ipv6p.fixed = false;
  158. break;
  159. case InetAddress::TYPE_IPV4:
  160. _ipv4p.fixed = false;
  161. break;
  162. case InetAddress::TYPE_IPV6:
  163. _ipv6p.fixed = false;
  164. break;
  165. }
  166. }
  167. } // namespace ZeroTier