1
0

Node.cpp 12 KB

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