Peer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Constants.hpp"
  14. #include "Peer.hpp"
  15. #include "Node.hpp"
  16. #include "Switch.hpp"
  17. #include "Network.hpp"
  18. #include "SelfAwareness.hpp"
  19. #include "Packet.hpp"
  20. #include "Trace.hpp"
  21. #include "InetAddress.hpp"
  22. namespace ZeroTier {
  23. Peer::Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity) :
  24. RR(renv),
  25. _lastReceive(0),
  26. _lastWhoisRequestReceived(0),
  27. _lastEchoRequestReceived(0),
  28. _lastPushDirectPathsReceived(0),
  29. _lastTriedStaticPath(0),
  30. _latency(0xffff),
  31. _pathCount(0),
  32. _id(peerIdentity),
  33. _vProto(0),
  34. _vMajor(0),
  35. _vMinor(0),
  36. _vRevision(0)
  37. {
  38. if (!myIdentity.agree(peerIdentity,_key))
  39. throw ZT_EXCEPTION_INVALID_ARGUMENT;
  40. }
  41. void Peer::received(
  42. void *tPtr,
  43. const SharedPtr<Path> &path,
  44. const unsigned int hops,
  45. const uint64_t packetId,
  46. const unsigned int payloadLength,
  47. const Packet::Verb verb,
  48. const uint64_t inRePacketId,
  49. const Packet::Verb inReVerb,
  50. const uint64_t networkId)
  51. {
  52. const int64_t now = RR->node->now();
  53. _lastReceive = now;
  54. /*
  55. if (hops == 0) {
  56. // If this is a direct packet (no hops), update existing paths or learn new ones
  57. bool havePath = false;
  58. {
  59. Mutex::Lock _l(_paths_m);
  60. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  61. if (_paths[i]) {
  62. if (_paths[i] == path) {
  63. havePath = true;
  64. break;
  65. }
  66. } else break;
  67. }
  68. }
  69. bool attemptToContact = false;
  70. if ((!havePath)&&(RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id.address(),path->localSocket(),path->address()))) {
  71. Mutex::Lock _l(_paths_m);
  72. // Paths are redundant if they duplicate an alive path to the same IP or
  73. // with the same local socket and address family.
  74. bool redundant = false;
  75. unsigned int replacePath = ZT_MAX_PEER_NETWORK_PATHS;
  76. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  77. if (_paths[i]) {
  78. if ( (_paths[i]->alive(now)) && ( ((_paths[i]->localSocket() == path->localSocket())&&(_paths[i]->address().ss_family == path->address().ss_family)) || (_paths[i]->address().ipsEqual2(path->address())) ) ) {
  79. redundant = true;
  80. break;
  81. }
  82. // If the path is the same address and port, simply assume this is a replacement
  83. if ( (_paths[i]->address().ipsEqual2(path->address()))) {
  84. replacePath = i;
  85. break;
  86. }
  87. } else break;
  88. }
  89. // If the path isn't a duplicate of the same localSocket AND we haven't already determined a replacePath,
  90. // then find the worst path and replace it.
  91. if (!redundant && replacePath == ZT_MAX_PEER_NETWORK_PATHS) {
  92. int replacePathQuality = 0;
  93. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  94. if (_paths[i]) {
  95. const int q = _paths[i]->quality(now);
  96. if (q > replacePathQuality) {
  97. replacePathQuality = q;
  98. replacePath = i;
  99. }
  100. } else {
  101. replacePath = i;
  102. break;
  103. }
  104. }
  105. }
  106. if (replacePath != ZT_MAX_PEER_NETWORK_PATHS) {
  107. if (verb == Packet::VERB_OK) {
  108. RR->t->peerLearnedNewPath(tPtr,networkId,*this,path,packetId);
  109. _paths[replacePath] = path;
  110. } else {
  111. attemptToContact = true;
  112. }
  113. }
  114. }
  115. if (attemptToContact) {
  116. sendHELLO(tPtr,path->localSocket(),path->address(),now);
  117. path->sent(now);
  118. RR->t->peerConfirmingUnknownPath(tPtr,networkId,*this,path,packetId,verb);
  119. }
  120. }
  121. */
  122. // Periodically push direct paths to the peer, doing so more often if we do not
  123. // currently have a direct path.
  124. /*
  125. const int64_t sinceLastPush = now - _lastDirectPathPushSent;
  126. if (sinceLastPush >= ((hops == 0) ? ZT_DIRECT_PATH_PUSH_INTERVAL_HAVEPATH : ZT_DIRECT_PATH_PUSH_INTERVAL)) {
  127. _lastDirectPathPushSent = now;
  128. std::vector<ZT_InterfaceAddress> pathsToPush(RR->node->directPaths());
  129. if (pathsToPush.size() > 0) {
  130. std::vector<ZT_InterfaceAddress>::const_iterator p(pathsToPush.begin());
  131. while (p != pathsToPush.end()) {
  132. ScopedPtr<Packet> outp(new Packet(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS));
  133. outp->addSize(2); // leave room for count
  134. unsigned int count = 0;
  135. while ((p != pathsToPush.end())&&((outp->size() + 24) < 1200)) {
  136. uint8_t addressType = 4;
  137. uint8_t addressLength = 6;
  138. unsigned int ipLength = 4;
  139. const void *rawIpData;
  140. const void *rawIpPort;
  141. switch(p->address.ss_family) {
  142. case AF_INET:
  143. rawIpData = &(reinterpret_cast<const struct sockaddr_in *>(&(p->address))->sin_addr.s_addr);
  144. rawIpPort = &(reinterpret_cast<const struct sockaddr_in *>(&(p->address))->sin_port);
  145. break;
  146. case AF_INET6:
  147. rawIpData = reinterpret_cast<const struct sockaddr_in6 *>(&(p->address))->sin6_addr.s6_addr;
  148. rawIpPort = &(reinterpret_cast<const struct sockaddr_in6 *>(&(p->address))->sin6_port);
  149. addressType = 6;
  150. addressLength = 18;
  151. ipLength = 16;
  152. break;
  153. default: // we currently only push IP addresses
  154. ++p;
  155. continue;
  156. }
  157. outp->append((uint8_t)0); // no flags
  158. outp->append((uint16_t)0); // no extensions
  159. outp->append(addressType);
  160. outp->append(addressLength);
  161. outp->append(rawIpData,ipLength);
  162. outp->append(rawIpPort,2);
  163. ++count;
  164. ++p;
  165. }
  166. if (count) {
  167. outp->setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  168. outp->compress();
  169. outp->armor(_key,true);
  170. path->send(RR,tPtr,outp->data(),outp->size(),now);
  171. }
  172. }
  173. }
  174. }
  175. */
  176. }
  177. bool Peer::hasActivePathTo(int64_t now,const InetAddress &addr) const
  178. {
  179. // TODO
  180. return false;
  181. }
  182. void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now)
  183. {
  184. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  185. outp.append((unsigned char)ZT_PROTO_VERSION);
  186. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  187. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  188. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  189. outp.append(now);
  190. RR->identity.serialize(outp,false);
  191. atAddress.serialize(outp);
  192. RR->node->expectReplyTo(outp.packetId());
  193. if (atAddress) {
  194. outp.armor(_key,false); // false == don't encrypt full payload, but add MAC
  195. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  196. } else {
  197. RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
  198. }
  199. }
  200. void Peer::ping(void *tPtr,int64_t now,unsigned int &v4SendCount,unsigned int &v6SendCount)
  201. {
  202. /*
  203. Mutex::Lock _l(_paths_m);
  204. unsigned int j = 0;
  205. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  206. if ((_paths[i])&&(_paths[i]->alive(now))) {
  207. sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now);
  208. _paths[i]->sent(now);
  209. if (_paths[i]->address().isV4())
  210. ++v4SendCount;
  211. else if (_paths[i]->address().isV6())
  212. ++v6SendCount;
  213. if (i != j)
  214. _paths[j] = _paths[i];
  215. ++j;
  216. }
  217. }
  218. while(j < ZT_MAX_PEER_NETWORK_PATHS) {
  219. _paths[j].zero();
  220. ++j;
  221. }
  222. */
  223. }
  224. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  225. {
  226. /*
  227. Mutex::Lock _l(_paths_m);
  228. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  229. if (_paths[i]) {
  230. if ((_paths[i]->address().ss_family == inetAddressFamily)&&(_paths[i]->ipScope() == scope)) {
  231. sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now);
  232. _paths[i]->sent(now);
  233. }
  234. } else break;
  235. }
  236. */
  237. }
  238. void Peer::getAllPaths(std::vector< SharedPtr<Path> > &paths)
  239. {
  240. RWMutex::RLock l(_paths_l);
  241. paths.clear();
  242. paths.assign(_paths,_paths + _pathCount);
  243. }
  244. } // namespace ZeroTier