Peer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #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. _requestHistoryPtr(0)
  44. {
  45. }
  46. Peer::Peer(const Identity &myIdentity,const Identity &peerIdentity)
  47. throw(std::runtime_error) :
  48. _id(peerIdentity),
  49. _ipv4p(),
  50. _ipv6p(),
  51. _lastUsed(0),
  52. _lastUnicastFrame(0),
  53. _lastMulticastFrame(0),
  54. _lastAnnouncedTo(0),
  55. _vMajor(0),
  56. _vMinor(0),
  57. _vRevision(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. Demarc::Port localPort,
  65. const InetAddress &remoteAddr,
  66. unsigned int hops,
  67. uint64_t packetId,
  68. Packet::Verb verb,
  69. uint64_t inRePacketId,
  70. Packet::Verb inReVerb,
  71. uint64_t now)
  72. {
  73. if (!hops) { // direct packet
  74. // Announce multicast LIKEs to peers to whom we have a direct link
  75. if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {
  76. _lastAnnouncedTo = now;
  77. _r->sw->announceMulticastGroups(SharedPtr<Peer>(this));
  78. }
  79. // Update last receive info for our direct path
  80. WanPath *const wp = (remoteAddr.isV4() ? &_ipv4p : &_ipv6p);
  81. wp->lastReceive = now;
  82. wp->localPort = ((localPort) ? localPort : Demarc::ANY_PORT);
  83. // Do things like learn latency or endpoints on OK or ERROR replies
  84. if (inReVerb != Packet::VERB_NOP) {
  85. for(unsigned int p=0;p<ZT_PEER_REQUEST_HISTORY_LENGTH;++p) {
  86. if ((_requestHistory[p].timestamp)&&(_requestHistory[p].packetId == inRePacketId)&&(_requestHistory[p].verb == inReVerb)) {
  87. _latency = std::min((unsigned int)(now - _requestHistory[p].timestamp),(unsigned int)0xffff);
  88. // Only learn paths on replies to packets we have sent, otherwise
  89. // this introduces both an asymmetry problem in NAT-t and a potential
  90. // reply DOS attack.
  91. if (!wp->fixed) {
  92. wp->addr = remoteAddr;
  93. TRACE("peer %s learned endpoint %s from %s(%s)",address().toString().c_str(),remoteAddr.toString().c_str(),Packet::verbString(verb),Packet::verbString(inReVerb));
  94. }
  95. _requestHistory[p].timestamp = 0;
  96. break;
  97. }
  98. }
  99. }
  100. // If we get a valid packet with a different address that is not a response
  101. // to a request, send a PROBE to authenticate this endpoint and determine if
  102. // it is reachable.
  103. if ((!wp->fixed)&&(wp->addr != remoteAddr))
  104. _r->sw->sendPROBE(SharedPtr<Peer>(this),localPort,remoteAddr);
  105. }
  106. if (verb == Packet::VERB_FRAME) {
  107. _lastUnicastFrame = now;
  108. } else if (verb == Packet::VERB_MULTICAST_FRAME) {
  109. _lastMulticastFrame = now;
  110. }
  111. }
  112. Demarc::Port Peer::send(const RuntimeEnvironment *_r,const void *data,unsigned int len,uint64_t now)
  113. {
  114. if ((_ipv6p.isActive(now))||((!(_ipv4p.addr))&&(_ipv6p.addr))) {
  115. if (_r->demarc->send(_ipv6p.localPort,_ipv6p.addr,data,len,-1)) {
  116. _ipv6p.lastSend = now;
  117. return _ipv6p.localPort;
  118. }
  119. }
  120. if (_ipv4p.addr) {
  121. if (_r->demarc->send(_ipv4p.localPort,_ipv4p.addr,data,len,-1)) {
  122. _ipv4p.lastSend = now;
  123. return _ipv4p.localPort;
  124. }
  125. }
  126. return Demarc::NULL_PORT;
  127. }
  128. bool Peer::sendFirewallOpener(const RuntimeEnvironment *_r,uint64_t now)
  129. {
  130. bool sent = false;
  131. if (_ipv4p.addr) {
  132. if (_r->demarc->send(_ipv4p.localPort,_ipv4p.addr,"\0",1,ZT_FIREWALL_OPENER_HOPS)) {
  133. _ipv4p.lastFirewallOpener = now;
  134. sent = true;
  135. }
  136. }
  137. if (_ipv6p.addr) {
  138. if (_r->demarc->send(_ipv6p.localPort,_ipv6p.addr,"\0",1,ZT_FIREWALL_OPENER_HOPS)) {
  139. _ipv6p.lastFirewallOpener = now;
  140. sent = true;
  141. }
  142. }
  143. return sent;
  144. }
  145. bool Peer::sendPing(const RuntimeEnvironment *_r,uint64_t now)
  146. {
  147. bool sent = false;
  148. if (_ipv4p.addr) {
  149. if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv4p.localPort,_ipv4p.addr)) {
  150. _ipv4p.lastSend = now;
  151. sent = true;
  152. }
  153. }
  154. if (_ipv6p.addr) {
  155. if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv6p.localPort,_ipv6p.addr)) {
  156. _ipv6p.lastSend = now;
  157. sent = true;
  158. }
  159. }
  160. return sent;
  161. }
  162. void Peer::setPathAddress(const InetAddress &addr,bool fixed)
  163. {
  164. if (addr.isV4()) {
  165. _ipv4p.addr = addr;
  166. _ipv4p.fixed = fixed;
  167. } else if (addr.isV6()) {
  168. _ipv6p.addr = addr;
  169. _ipv6p.fixed = fixed;
  170. }
  171. }
  172. void Peer::clearFixedFlag(InetAddress::AddressType t)
  173. {
  174. switch(t) {
  175. case InetAddress::TYPE_NULL:
  176. _ipv4p.fixed = false;
  177. _ipv6p.fixed = false;
  178. break;
  179. case InetAddress::TYPE_IPV4:
  180. _ipv4p.fixed = false;
  181. break;
  182. case InetAddress::TYPE_IPV6:
  183. _ipv6p.fixed = false;
  184. break;
  185. }
  186. }
  187. } // namespace ZeroTier