Node.cpp 11 KB

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