Peer.cpp 4.0 KB

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