1
0

Node.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 "../version.h"
  28. #include "Constants.hpp"
  29. #include "Node.hpp"
  30. #include "RuntimeEnvironment.hpp"
  31. #include "NetworkConfigMaster.hpp"
  32. #include "CMWC4096.hpp"
  33. #include "Switch.hpp"
  34. #include "Multicaster.hpp"
  35. #include "AntiRecursion.hpp"
  36. #include "Topology.hpp"
  37. #include "Buffer.hpp"
  38. #include "Packet.hpp"
  39. #include "Logger.hpp"
  40. #include "Address.hpp"
  41. #include "Identity.hpp"
  42. #include "SelfAwareness.hpp"
  43. namespace ZeroTier {
  44. /****************************************************************************/
  45. /* Public Node interface (C++, exposed via CAPI bindings) */
  46. /****************************************************************************/
  47. Node::Node(
  48. uint64_t now,
  49. ZT1_DataStoreGetFunction dataStoreGetFunction,
  50. ZT1_DataStorePutFunction dataStorePutFunction,
  51. ZT1_WirePacketSendFunction wirePacketSendFunction,
  52. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  53. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  54. ZT1_StatusCallback statusCallback) :
  55. RR(new RuntimeEnvironment(this)),
  56. _dataStoreGetFunction(dataStoreGetFunction),
  57. _dataStorePutFunction(dataStorePutFunction),
  58. _wirePacketSendFunction(wirePacketSendFunction),
  59. _virtualNetworkFrameFunction(virtualNetworkFrameFunction),
  60. _virtualNetworkConfigFunction(virtualNetworkConfigFunction),
  61. _statusCallback(statusCallback),
  62. _networks(),
  63. _networks_m(),
  64. _now(now)
  65. {
  66. _newestVersionSeen[0] = ZEROTIER_ONE_VERSION_MAJOR;
  67. _newestVersionSeen[1] = ZEROTIER_ONE_VERSION_MINOR;
  68. _newestVersionSeen[2] = ZEROTIER_ONE_VERSION_REVISION;
  69. std::string idtmp(dataStoreGet("identity.secret"));
  70. if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
  71. RR->identity.generate();
  72. idtmp = RR->identity.toString(true);
  73. if (!dataStorePut("identity.secret",idtmp,true)) {
  74. delete RR;
  75. throw std::runtime_error("unable to write identity.secret");
  76. }
  77. idtmp = RR->identity.toString(false);
  78. if (!dataStorePut("identity.public",idtmp,false)) {
  79. delete RR;
  80. throw std::runtime_error("unable to write identity.public");
  81. }
  82. }
  83. try {
  84. RR->prng = new CMWC4096();
  85. RR->sw = new Switch(RR);
  86. RR->mc = new Multicaster(RR);
  87. RR->antiRec = new AntiRecursion();
  88. RR->topology = new Topology(RR);
  89. RR->sa = new SelfAwareness(RR);
  90. } catch ( ... ) {
  91. delete RR->sa;
  92. delete RR->topology;
  93. delete RR->antiRec;
  94. delete RR->mc;
  95. delete RR->sw;
  96. delete RR->prng;
  97. delete RR->log;
  98. delete RR;
  99. throw;
  100. }
  101. postEvent(ZT1_EVENT_UP);
  102. }
  103. Node::~Node()
  104. {
  105. delete RR->sa;
  106. delete RR->topology;
  107. delete RR->antiRec;
  108. delete RR->mc;
  109. delete RR->sw;
  110. delete RR->prng;
  111. delete RR->log;
  112. delete RR;
  113. }
  114. ZT1_ResultCode Node::processWirePacket(
  115. uint64_t now,
  116. const struct sockaddr_storage *remoteAddress,
  117. unsigned int linkDesperation,
  118. const void *packetData,
  119. unsigned int packetLength,
  120. uint64_t *nextCallDeadline)
  121. {
  122. processBackgroundTasks(now,nextCallDeadline);
  123. }
  124. ZT1_ResultCode Node::processVirtualNetworkFrame(
  125. uint64_t now,
  126. uint64_t nwid,
  127. uint64_t sourceMac,
  128. uint64_t destMac,
  129. unsigned int etherType,
  130. unsigned int vlanId,
  131. const void *frameData,
  132. unsigned int frameLength,
  133. uint64_t *nextCallDeadline)
  134. {
  135. processBackgroundTasks(now,nextCallDeadline);
  136. }
  137. ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,uint64_t *nextCallDeadline)
  138. {
  139. _now = now;
  140. }
  141. ZT1_ResultCode Node::join(uint64_t nwid)
  142. {
  143. Mutex::Lock _l(_networks_m);
  144. SharedPtr<Network> &nw = _networks[nwid];
  145. if (!nw)
  146. nw = SharedPtr<Network>(new Network(RR,nwid));
  147. return ZT1_RESULT_OK;
  148. }
  149. ZT1_ResultCode Node::leave(uint64_t nwid)
  150. {
  151. Mutex::Lock _l(_networks_m);
  152. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  153. if (nw != _networks.end()) {
  154. nw->second->destroy();
  155. _networks.erase(nw);
  156. }
  157. }
  158. ZT1_ResultCode Node::multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  159. {
  160. Mutex::Lock _l(_networks_m);
  161. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  162. if (nw != _networks.end())
  163. nw->second->multicastSubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  164. }
  165. ZT1_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  166. {
  167. Mutex::Lock _l(_networks_m);
  168. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  169. if (nw != _networks.end())
  170. nw->second->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  171. }
  172. void Node::status(ZT1_NodeStatus *status)
  173. {
  174. }
  175. ZT1_PeerList *Node::peers()
  176. {
  177. }
  178. ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid)
  179. {
  180. Mutex::Lock _l(_networks_m);
  181. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  182. if (nw != _networks.end()) {
  183. ZT1_VirtualNetworkConfig *nc = (ZT1_VirtualNetworkConfig *)::malloc(sizeof(ZT1_VirtualNetworkConfig));
  184. nw->second->externalConfig(nc);
  185. return nc;
  186. }
  187. return (ZT1_VirtualNetworkConfig *)0;
  188. }
  189. ZT1_VirtualNetworkList *Node::networks()
  190. {
  191. }
  192. void Node::freeQueryResult(void *qr)
  193. {
  194. if (qr)
  195. ::free(qr);
  196. }
  197. void Node::setNetconfMaster(void *networkConfigMasterInstance)
  198. {
  199. RR->netconfMaster = reinterpret_cast<NetworkConfigMaster *>(networkConfigMasterInstance);
  200. }
  201. /****************************************************************************/
  202. /* Node methods used only within node/ */
  203. /****************************************************************************/
  204. std::string Node::dataStoreGet(const char *name)
  205. {
  206. char buf[16384];
  207. std::string r;
  208. unsigned long olen = 0;
  209. do {
  210. long n = _dataStoreGetFunction(reinterpret_cast<ZT1_Node *>(this),name,buf,sizeof(buf),r.length(),&olen);
  211. if (n <= 0)
  212. return std::string();
  213. r.append(buf,n);
  214. } while (r.length() < olen);
  215. return r;
  216. }
  217. void Node::postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev)
  218. {
  219. if (Peer::compareVersion(major,minor,rev,_newestVersionSeen[0],_newestVersionSeen[1],_newestVersionSeen[2]) > 0) {
  220. _newestVersionSeen[0] = major;
  221. _newestVersionSeen[1] = minor;
  222. _newestVersionSeen[2] = rev;
  223. this->postEvent(ZT1_EVENT_SAW_MORE_RECENT_VERSION);
  224. }
  225. }
  226. } // namespace ZeroTier
  227. /****************************************************************************/
  228. /* CAPI bindings */
  229. /****************************************************************************/
  230. extern "C" {
  231. enum ZT1_ResultCode ZT1_Node_new(
  232. ZT1_Node **node,
  233. uint64_t now,
  234. ZT1_DataStoreGetFunction dataStoreGetFunction,
  235. ZT1_DataStorePutFunction dataStorePutFunction,
  236. ZT1_WirePacketSendFunction wirePacketSendFunction,
  237. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  238. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  239. ZT1_StatusCallback statusCallback)
  240. {
  241. *node = (ZT1_Node *)0;
  242. try {
  243. *node = reinterpret_cast<ZT1_Node *>(new ZeroTier::Node(now,dataStoreGetFunction,dataStorePutFunction,wirePacketSendFunction,virtualNetworkFrameFunction,virtualNetworkConfigFunction,statusCallback));
  244. return ZT1_RESULT_OK;
  245. } catch (std::bad_alloc &exc) {
  246. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  247. } catch (std::runtime_error &exc) {
  248. return ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  249. } catch ( ... ) {
  250. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  251. }
  252. }
  253. void ZT1_Node_delete(ZT1_Node *node)
  254. {
  255. try {
  256. delete (reinterpret_cast<ZeroTier::Node *>(node));
  257. } catch ( ... ) {}
  258. }
  259. enum ZT1_ResultCode ZT1_Node_processWirePacket(
  260. ZT1_Node *node,
  261. uint64_t now,
  262. const struct sockaddr_storage *remoteAddress,
  263. unsigned int linkDesperation,
  264. const void *packetData,
  265. unsigned int packetLength,
  266. uint64_t *nextCallDeadline)
  267. {
  268. try {
  269. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(now,remoteAddress,linkDesperation,packetData,packetLength,nextCallDeadline);
  270. } catch (std::bad_alloc &exc) {
  271. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  272. } catch ( ... ) {
  273. return ZT1_RESULT_ERROR_PACKET_INVALID;
  274. }
  275. }
  276. enum ZT1_ResultCode ZT1_Node_processVirtualNetworkFrame(
  277. ZT1_Node *node,
  278. uint64_t now,
  279. uint64_t nwid,
  280. uint64_t sourceMac,
  281. uint64_t destMac,
  282. unsigned int etherType,
  283. unsigned int vlanId,
  284. const void *frameData,
  285. unsigned int frameLength,
  286. uint64_t *nextCallDeadline)
  287. {
  288. try {
  289. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextCallDeadline);
  290. } catch (std::bad_alloc &exc) {
  291. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  292. } catch ( ... ) {
  293. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  294. }
  295. }
  296. enum ZT1_ResultCode ZT1_Node_processBackgroundTasks(ZT1_Node *node,uint64_t now,uint64_t *nextCallDeadline)
  297. {
  298. try {
  299. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(now,nextCallDeadline);
  300. } catch (std::bad_alloc &exc) {
  301. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  302. } catch ( ... ) {
  303. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  304. }
  305. }
  306. enum ZT1_ResultCode ZT1_Node_join(ZT1_Node *node,uint64_t nwid)
  307. {
  308. try {
  309. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid);
  310. } catch (std::bad_alloc &exc) {
  311. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  312. } catch ( ... ) {
  313. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  314. }
  315. }
  316. enum ZT1_ResultCode ZT1_Node_leave(ZT1_Node *node,uint64_t nwid)
  317. {
  318. try {
  319. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid);
  320. } catch (std::bad_alloc &exc) {
  321. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  322. } catch ( ... ) {
  323. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  324. }
  325. }
  326. enum ZT1_ResultCode ZT1_Node_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  327. {
  328. try {
  329. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(nwid,multicastGroup,multicastAdi);
  330. } catch (std::bad_alloc &exc) {
  331. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  332. } catch ( ... ) {
  333. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  334. }
  335. }
  336. enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  337. {
  338. try {
  339. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  340. } catch (std::bad_alloc &exc) {
  341. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  342. } catch ( ... ) {
  343. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  344. }
  345. }
  346. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
  347. {
  348. try {
  349. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  350. } catch ( ... ) {}
  351. }
  352. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node)
  353. {
  354. try {
  355. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  356. } catch ( ... ) {
  357. return (ZT1_PeerList *)0;
  358. }
  359. }
  360. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid)
  361. {
  362. try {
  363. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  364. } catch ( ... ) {
  365. return (ZT1_VirtualNetworkConfig *)0;
  366. }
  367. }
  368. ZT1_VirtualNetworkList *ZT1_Node_networks(ZT1_Node *node)
  369. {
  370. try {
  371. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  372. } catch ( ... ) {
  373. return (ZT1_VirtualNetworkList *)0;
  374. }
  375. }
  376. void ZT1_Node_freeQueryResult(ZT1_Node *node,void *qr)
  377. {
  378. try {
  379. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  380. } catch ( ... ) {}
  381. }
  382. void ZT1_Node_setNetconfMaster(ZT1_Node *node,void *networkConfigMasterInstance)
  383. {
  384. try {
  385. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkConfigMasterInstance);
  386. } catch ( ... ) {}
  387. }
  388. void ZT1_version(int *major,int *minor,int *revision,unsigned long *featureFlags)
  389. {
  390. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  391. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  392. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  393. if (featureFlags) {
  394. *featureFlags = (
  395. ZT1_FEATURE_FLAG_THREAD_SAFE
  396. #ifdef ZT_OFFICIAL_BUILD
  397. | ZT1_FEATURE_FLAG_OFFICIAL
  398. #endif
  399. );
  400. }
  401. }
  402. } // extern "C"