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