Peer.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. */
  101. if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {
  102. _lastAnnouncedTo = now;
  103. const bool isSupernode = RR->topology->isSupernode(_id.address());
  104. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
  105. const std::vector< SharedPtr<Network> > networks(RR->node->allNetworks());
  106. for(std::vector< SharedPtr<Network> >::const_iterator n(networks.begin());n!=networks.end();++n) {
  107. if ( (isSupernode) || ((*n)->isAllowed(_id.address())) ) {
  108. const std::vector<MulticastGroup> mgs((*n)->allMulticastGroups());
  109. for(std::vector<MulticastGroup>::const_iterator mg(mgs.begin());mg!=mgs.end();++mg) {
  110. if ((outp.size() + 18) > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
  111. outp.armor(_key,true);
  112. RR->node->putPacket(remoteAddr,outp.data(),outp.size(),linkDesperation);
  113. outp.reset(_id.address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
  114. }
  115. // network ID, MAC, ADI
  116. outp.append((uint64_t)(*n)->id());
  117. mg->mac().appendTo(outp);
  118. outp.append((uint32_t)mg->adi());
  119. }
  120. }
  121. }
  122. if (outp.size() > ZT_PROTO_MIN_PACKET_LENGTH) {
  123. outp.armor(_key,true);
  124. RR->node->putPacket(remoteAddr,outp.data(),outp.size(),linkDesperation);
  125. }
  126. }
  127. }
  128. if ((verb == Packet::VERB_FRAME)||(verb == Packet::VERB_EXT_FRAME))
  129. _lastUnicastFrame = now;
  130. else if (verb == Packet::VERB_MULTICAST_FRAME)
  131. _lastMulticastFrame = now;
  132. }
  133. void Peer::addPath(const Path &newp)
  134. {
  135. unsigned int np = _numPaths;
  136. for(unsigned int p=0;p<np;++p) {
  137. if (_paths[p].address() == newp.address()) {
  138. _paths[p].setFixed(newp.fixed());
  139. return;
  140. }
  141. }
  142. Path *slot = (Path *)0;
  143. if (np < ZT_PEER_MAX_PATHS) {
  144. // Add new path
  145. slot = &(_paths[np++]);
  146. } else {
  147. // Replace oldest non-fixed path
  148. uint64_t slotLRmin = 0xffffffffffffffffULL;
  149. for(unsigned int p=0;p<ZT_PEER_MAX_PATHS;++p) {
  150. if ((!_paths[p].fixed())&&(_paths[p].lastReceived() <= slotLRmin)) {
  151. slotLRmin = _paths[p].lastReceived();
  152. slot = &(_paths[p]);
  153. }
  154. }
  155. }
  156. if (slot) {
  157. *slot = newp;
  158. _numPaths = np;
  159. }
  160. }
  161. void Peer::clearPaths(bool fixedToo)
  162. {
  163. if (fixedToo) {
  164. _numPaths = 0;
  165. } else {
  166. unsigned int np = _numPaths;
  167. unsigned int x = 0;
  168. unsigned int y = 0;
  169. while (x < np) {
  170. if (_paths[x].fixed())
  171. _paths[y++] = _paths[x];
  172. ++x;
  173. }
  174. _numPaths = y;
  175. }
  176. }
  177. void Peer::resetWithinScope(const RuntimeEnvironment *RR,InetAddress::IpScope scope,uint64_t now)
  178. {
  179. unsigned int np = _numPaths;
  180. unsigned int x = 0;
  181. unsigned int y = 0;
  182. while (x < np) {
  183. if (_paths[x].address().ipScope() == scope) {
  184. if (_paths[x].fixed()) {
  185. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_NOP);
  186. outp.armor(_key,false);
  187. RR->node->putPacket(_paths[x].address(),outp.data(),outp.size(),_paths[x].desperation(now));
  188. _paths[y++] = _paths[x]; // keep fixed paths
  189. }
  190. } else {
  191. _paths[y++] = _paths[x]; // keep paths not in this scope
  192. }
  193. ++x;
  194. }
  195. _numPaths = y;
  196. if ((y < np)&&(alive(now))) {
  197. // Try to re-establish direct connectivity to this peer if it's alive
  198. // and we have forgotten paths to it.
  199. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_NOP);
  200. RR->sw->send(outp,true);
  201. }
  202. }
  203. void Peer::getBestActiveAddresses(uint64_t now,InetAddress &v4,InetAddress &v6) const
  204. {
  205. uint64_t bestV4 = 0,bestV6 = 0;
  206. for(unsigned int p=0,np=_numPaths;p<np;++p) {
  207. if (_paths[p].active(now)) {
  208. uint64_t lr = _paths[p].lastReceived();
  209. if (lr) {
  210. if (_paths[p].address().isV4()) {
  211. if (lr >= bestV4) {
  212. bestV4 = lr;
  213. v4 = _paths[p].address();
  214. }
  215. } else if (_paths[p].address().isV6()) {
  216. if (lr >= bestV6) {
  217. bestV6 = lr;
  218. v6 = _paths[p].address();
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. } // namespace ZeroTier