Peer.cpp 3.6 KB

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