Peer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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 "Constants.hpp"
  28. #include "Peer.hpp"
  29. #include "Node.hpp"
  30. #include "Switch.hpp"
  31. #include "Network.hpp"
  32. #include "AntiRecursion.hpp"
  33. #include <algorithm>
  34. namespace ZeroTier {
  35. Peer::Peer(const Identity &myIdentity,const Identity &peerIdentity)
  36. throw(std::runtime_error) :
  37. _lastUsed(0),
  38. _lastReceive(0),
  39. _lastUnicastFrame(0),
  40. _lastMulticastFrame(0),
  41. _lastAnnouncedTo(0),
  42. _vMajor(0),
  43. _vMinor(0),
  44. _vRevision(0),
  45. _id(peerIdentity),
  46. _numPaths(0),
  47. _latency(0)
  48. {
  49. if (!myIdentity.agree(peerIdentity,_key,ZT_PEER_SECRET_KEY_LENGTH))
  50. throw std::runtime_error("new peer identity key agreement failed");
  51. }
  52. void Peer::received(
  53. const RuntimeEnvironment *RR,
  54. const InetAddress &remoteAddr,
  55. int linkDesperation
  56. unsigned int hops,
  57. uint64_t packetId,
  58. Packet::Verb verb,
  59. uint64_t inRePacketId,
  60. Packet::Verb inReVerb)
  61. {
  62. const uint64_t now = RR->node->now();
  63. _lastReceive = now;
  64. if (!hops) {
  65. /* Learn new paths from direct (hops == 0) packets */
  66. {
  67. unsigned int np = _numPaths;
  68. bool havePath = false;
  69. for(unsigned int p=0;p<np;++p) {
  70. if (_paths[p].address() == remoteAddr) {
  71. _paths[p].received(now,linkDesperation);
  72. havePath = true;
  73. break;
  74. }
  75. }
  76. if (!havePath) {
  77. Path *slot = (Path *)0;
  78. if (np < ZT_PEER_MAX_PATHS) {
  79. // Add new path
  80. slot = &(_paths[np++]);
  81. } else {
  82. // Replace oldest non-fixed path
  83. uint64_t slotLRmin = 0xffffffffffffffffULL;
  84. for(unsigned int p=0;p<ZT_PEER_MAX_PATHS;++p) {
  85. if ((!_paths[p].fixed())&&(_paths[p].lastReceived() <= slotLRmin)) {
  86. slotLRmin = _paths[p].lastReceived();
  87. slot = &(_paths[p]);
  88. }
  89. }
  90. }
  91. if (slot) {
  92. slot->init(remoteAddr,false);
  93. slot->received(now,linkDesperation);
  94. _numPaths = np;
  95. }
  96. }
  97. }
  98. /* Announce multicast groups of interest to direct peers if they are
  99. * considered authorized members of a given network. Also announce to
  100. * supernodes and network controllers. The other place this is done
  101. * is in rescanMulticastGroups() in Network, but that only sends something
  102. * if a network's multicast groups change. */
  103. if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {
  104. _lastAnnouncedTo = now;
  105. bool isSupernode = RR->topology->isSupernode(_id.address());
  106. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
  107. std::vector< SharedPtr<Network> > networks(RR->nc->networks());
  108. for(std::vector< SharedPtr<Network> >::iterator n(networks.begin());n!=networks.end();++n) {
  109. if ( ((*n)->isAllowed(_id.address())) || (isSupernode) ) {
  110. std::set<MulticastGroup> mgs((*n)->multicastGroups());
  111. for(std::set<MulticastGroup>::iterator mg(mgs.begin());mg!=mgs.end();++mg) {
  112. if ((outp.size() + 18) > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
  113. outp.armor(_key,true);
  114. RR->node->putPacket(remoteAddr,outp.data(),outp.size(),linkDesperation,false);
  115. outp.reset(_id.address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
  116. }
  117. // network ID, MAC, ADI
  118. outp.append((uint64_t)(*n)->id());
  119. mg->mac().appendTo(outp);
  120. outp.append((uint32_t)mg->adi());
  121. }
  122. }
  123. }
  124. if (outp.size() > ZT_PROTO_MIN_PACKET_LENGTH) {
  125. outp.armor(_key,true);
  126. RR->node->putPacket(remoteAddr,outp.data(),outp.size(),linkDesperation,false);
  127. }
  128. }
  129. }
  130. if ((verb == Packet::VERB_FRAME)||(verb == Packet::VERB_EXT_FRAME))
  131. _lastUnicastFrame = now;
  132. else if (verb == Packet::VERB_MULTICAST_FRAME)
  133. _lastMulticastFrame = now;
  134. }
  135. void Peer::addPath(const Path &newp)
  136. {
  137. unsigned int np = _numPaths;
  138. for(unsigned int p=0;p<np;++p) {
  139. if (_paths[p].address() == newp.address()) {
  140. _paths[p].setFixed(newp.fixed());
  141. return;
  142. }
  143. }
  144. Path *slot = (Path *)0;
  145. if (np < ZT_PEER_MAX_PATHS) {
  146. // Add new path
  147. slot = &(_paths[np++]);
  148. } else {
  149. // Replace oldest non-fixed path
  150. uint64_t slotLRmin = 0xffffffffffffffffULL;
  151. for(unsigned int p=0;p<ZT_PEER_MAX_PATHS;++p) {
  152. if ((!_paths[p].fixed())&&(_paths[p].lastReceived() <= slotLRmin)) {
  153. slotLRmin = _paths[p].lastReceived();
  154. slot = &(_paths[p]);
  155. }
  156. }
  157. }
  158. if (slot) {
  159. *slot = newp;
  160. _numPaths = np;
  161. }
  162. }
  163. void Peer::clearPaths(bool fixedToo)
  164. {
  165. if (fixedToo) {
  166. _numPaths = 0;
  167. } else {
  168. unsigned int np = _numPaths;
  169. unsigned int x = 0;
  170. unsigned int y = 0;
  171. while (x < np) {
  172. if (_paths[x].fixed())
  173. _paths[y++] = _paths[x];
  174. ++x;
  175. }
  176. _numPaths = y;
  177. }
  178. }
  179. void Peer::getBestActiveAddresses(uint64_t now,InetAddress &v4,InetAddress &v6) const
  180. {
  181. uint64_t bestV4 = 0,bestV6 = 0;
  182. for(unsigned int p=0,np=_numPaths;p<np;++p) {
  183. if (_paths[p].active(now)) {
  184. uint64_t lr = _paths[p].lastReceived();
  185. if (lr) {
  186. if (_paths[p].address().isV4()) {
  187. if (lr >= bestV4) {
  188. bestV4 = lr;
  189. v4 = _paths[p].address();
  190. }
  191. } else if (_paths[p].address().isV6()) {
  192. if (lr >= bestV6) {
  193. bestV6 = lr;
  194. v6 = _paths[p].address();
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. } // namespace ZeroTier