Peer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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. namespace ZeroTier {
  30. Peer::Peer() :
  31. _id(),
  32. _ipv4p(),
  33. _ipv6p(),
  34. _lastUsed(0),
  35. _lastUnicastFrame(0),
  36. _lastMulticastFrame(0),
  37. _lastAnnouncedTo(0),
  38. _vMajor(0),
  39. _vMinor(0),
  40. _vRevision(0)
  41. {
  42. }
  43. Peer::Peer(const Identity &myIdentity,const Identity &peerIdentity)
  44. throw(std::runtime_error) :
  45. _id(peerIdentity),
  46. _ipv4p(),
  47. _ipv6p(),
  48. _lastUsed(0),
  49. _lastUnicastFrame(0),
  50. _lastMulticastFrame(0),
  51. _lastAnnouncedTo(0),
  52. _vMajor(0),
  53. _vMinor(0),
  54. _vRevision(0)
  55. {
  56. if (!myIdentity.agree(peerIdentity,_key,ZT_PEER_SECRET_KEY_LENGTH))
  57. throw std::runtime_error("new peer identity key agreement failed");
  58. }
  59. void Peer::onReceive(const RuntimeEnvironment *_r,Demarc::Port localPort,const InetAddress &remoteAddr,unsigned int hops,Packet::Verb verb,uint64_t now)
  60. {
  61. if (!hops) { // direct packet
  62. WanPath *wp = (remoteAddr.isV4() ? &_ipv4p : &_ipv6p);
  63. wp->lastReceive = now;
  64. wp->localPort = localPort;
  65. if (!wp->fixed)
  66. wp->addr = remoteAddr;
  67. if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {
  68. _lastAnnouncedTo = now;
  69. _r->sw->announceMulticastGroups(SharedPtr<Peer>(this));
  70. }
  71. }
  72. if (verb == Packet::VERB_FRAME) {
  73. _lastUnicastFrame = now;
  74. } else if (verb == Packet::VERB_MULTICAST_FRAME) {
  75. _lastMulticastFrame = now;
  76. }
  77. }
  78. bool Peer::send(const RuntimeEnvironment *_r,const void *data,unsigned int len,uint64_t now)
  79. {
  80. if ((_ipv6p.isActive(now))||((!(_ipv4p.addr))&&(_ipv6p.addr))) {
  81. if (_r->demarc->send(_ipv6p.localPort,_ipv6p.addr,data,len,-1)) {
  82. _ipv6p.lastSend = now;
  83. return true;
  84. }
  85. }
  86. if (_ipv4p.addr) {
  87. if (_r->demarc->send(_ipv4p.localPort,_ipv4p.addr,data,len,-1)) {
  88. _ipv4p.lastSend = now;
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. bool Peer::sendFirewallOpener(const RuntimeEnvironment *_r,uint64_t now)
  95. {
  96. bool sent = false;
  97. if (_ipv4p.addr) {
  98. if (_r->demarc->send(_ipv4p.localPort,_ipv4p.addr,"\0",1,ZT_FIREWALL_OPENER_HOPS)) {
  99. _ipv4p.lastFirewallOpener = now;
  100. sent = true;
  101. }
  102. }
  103. if (_ipv6p.addr) {
  104. if (_r->demarc->send(_ipv6p.localPort,_ipv6p.addr,"\0",1,ZT_FIREWALL_OPENER_HOPS)) {
  105. _ipv6p.lastFirewallOpener = now;
  106. sent = true;
  107. }
  108. }
  109. return sent;
  110. }
  111. bool Peer::sendPing(const RuntimeEnvironment *_r,uint64_t now)
  112. {
  113. bool sent = false;
  114. if (_ipv4p.addr) {
  115. if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv4p.localPort,_ipv4p.addr)) {
  116. _ipv4p.lastSend = now;
  117. sent = true;
  118. }
  119. }
  120. if (_ipv6p.addr) {
  121. if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv6p.localPort,_ipv6p.addr)) {
  122. _ipv6p.lastSend = now;
  123. sent = true;
  124. }
  125. }
  126. return sent;
  127. }
  128. void Peer::setPathAddress(const InetAddress &addr,bool fixed)
  129. {
  130. if (addr.isV4()) {
  131. _ipv4p.addr = addr;
  132. _ipv4p.fixed = fixed;
  133. } else if (addr.isV6()) {
  134. _ipv6p.addr = addr;
  135. _ipv6p.fixed = fixed;
  136. }
  137. }
  138. void Peer::clearFixedFlag(InetAddress::AddressType t)
  139. {
  140. switch(t) {
  141. case InetAddress::TYPE_NULL:
  142. _ipv4p.fixed = false;
  143. _ipv6p.fixed = false;
  144. break;
  145. case InetAddress::TYPE_IPV4:
  146. _ipv4p.fixed = false;
  147. break;
  148. case InetAddress::TYPE_IPV6:
  149. _ipv6p.fixed = false;
  150. break;
  151. }
  152. }
  153. } // namespace ZeroTier