Node.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. Node::Node(
  44. uint64_t now,
  45. ZT1_DataStoreGetFunction *dataStoreGetFunction,
  46. ZT1_DataStorePutFunction *dataStorePutFunction,
  47. ZT1_WirePacketSendFunction *wirePacketSendFunction,
  48. ZT1_VirtualNetworkFrameFunction *virtualNetworkFrameFunction,
  49. ZT1_VirtualNetworkConfigCallback *virtualNetworkConfigCallback,
  50. ZT1_StatusCallback *statusCallback) :
  51. RR(new RuntimeEnvironment(this)),
  52. _dataStoreGetFunction(dataStoreGetFunction),
  53. _dataStorePutFunction(dataStorePutFunction),
  54. _wirePacketSendFunction(wirePacketSendFunction),
  55. _virtualNetworkFrameFunction(virtualNetworkFrameFunction),
  56. _virtualNetworkConfigCallback(virtualNetworkConfigCallback),
  57. _statusCallback(statusCallback),
  58. _networks(),
  59. _networks_m(),
  60. _now(now)
  61. {
  62. try {
  63. RR->prng = new CMWC4096();
  64. RR->sw = new Switch(RR);
  65. RR->mc = new Multicaster(RR);
  66. RR->antiRec = new AntiRecursion(RR);
  67. RR->topology = new Topology(RR);
  68. } catch ( ... ) {
  69. delete RR->topology;
  70. delete RR->antiRec;
  71. delete RR->mc;
  72. delete RR->sw;
  73. delete RR->prng;
  74. delete RR->log;
  75. delete RR;
  76. throw;
  77. }
  78. }
  79. Node::~Node()
  80. {
  81. delete RR->topology;
  82. delete RR->antiRec;
  83. delete RR->mc;
  84. delete RR->sw;
  85. delete RR->prng;
  86. delete RR->log;
  87. delete RR;
  88. }
  89. ZT1_ResultCode Node::processWirePacket(
  90. uint64_t now,
  91. const struct sockaddr_storage *remoteAddress,
  92. int linkDesperation,
  93. const void *packetData,
  94. unsigned int packetLength,
  95. uint64_t *nextCallDeadline)
  96. {
  97. _now = now;
  98. }
  99. ZT1_ResultCode Node::processVirtualNetworkFrame(
  100. uint64_t now,
  101. uint64_t nwid,
  102. uint64_t sourceMac,
  103. uint64_t destMac,
  104. unsigned int etherType,
  105. unsigned int vlanId,
  106. const void *frameData,
  107. unsigned int frameLength,
  108. uint64_t *nextCallDeadline)
  109. {
  110. _now = now;
  111. }
  112. ZT1_Resultcode Node::processNothing(uint64_t now,uint64_t *nextCallDeadline)
  113. {
  114. _now = now;
  115. }
  116. ZT1_ResultCode Node::join(uint64_t nwid)
  117. {
  118. Mutex::Lock _l(_networks_m);
  119. SharedPtr<Network> &nw = _networks[nwid];
  120. if (!nw)
  121. nw = SharedPtr<Network>(new Network(RR,nwid));
  122. return ZT1_RESULT_OK;
  123. }
  124. ZT1_ResultCode Node::leave(uint64_t nwid)
  125. {
  126. Mutex::Lock _l(_networks_m);
  127. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  128. if (nw != _networks.end()) {
  129. nw->second->destroy();
  130. _networks.erase(nw);
  131. }
  132. }
  133. ZT1_ResultCode Node::multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  134. {
  135. }
  136. ZT1_ResultCode Node::multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  137. {
  138. }
  139. void Node::status(ZT1_NodeStatus *status)
  140. {
  141. }
  142. ZT1_PeerList *Node::peers()
  143. {
  144. }
  145. ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid)
  146. {
  147. }
  148. ZT1_VirtualNetworkList *Node::listNetworks()
  149. {
  150. }
  151. void Node::freeQueryResult(void *qr)
  152. {
  153. if (qr)
  154. ::free(qr);
  155. }
  156. void Node::setNetconfMaster(void *networkConfigMasterInstance)
  157. {
  158. RR->netconfMaster = reinterpret_cast<NetworkConfigMaster *>(networkConfigMasterInstance);
  159. }
  160. } // namespace ZeroTier
  161. extern "C" {
  162. enum ZT1_ResultCode ZT1_Node_new(
  163. ZT1_Node **node,
  164. uint64_t now,
  165. ZT1_DataStoreGetFunction *dataStoreGetFunction,
  166. ZT1_DataStorePutFunction *dataStorePutFunction,
  167. ZT1_WirePacketSendFunction *wirePacketSendFunction,
  168. ZT1_VirtualNetworkFrameFunction *virtualNetworkFrameFunction,
  169. ZT1_VirtualNetworkConfigCallback *virtualNetworkConfigCallback,
  170. ZT1_StatusCallback *statusCallback)
  171. {
  172. *node = (ZT1_Node *)0;
  173. try {
  174. *node = reinterpret_cast<ZT1_Node *>(new ZeroTier::Node(now,dataStoreGetFunction,dataStorePutFunction,wirePacketSendFunction,virtualNetworkFrameFunction,virtualNetworkConfigCallback,statusCallback));
  175. return ZT1_RESULT_OK;
  176. } catch (std::bad_alloc &exc) {
  177. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  178. } catch (std::runtime_error &exc) {
  179. return ZT1_RESULT_ERROR_DATA_STORE_FAILED;
  180. } catch ( ... ) {
  181. return ZT1_RESULT_ERROR_INTERNAL;
  182. }
  183. }
  184. enum ZT1_ResultCode ZT1_Node_processWirePacket(
  185. ZT1_Node *node,
  186. uint64_t now,
  187. const struct sockaddr_storage *remoteAddress,
  188. int linkDesperation,
  189. const void *packetData,
  190. unsigned int packetLength,
  191. uint64_t *nextCallDeadline)
  192. {
  193. try {
  194. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(now,remoteAddress,linkDesperation,packetData,packetLength,nextCallDeadline);
  195. } catch (std::bad_alloc &exc) {
  196. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  197. } catch ( ... ) {
  198. return ZT1_RESULT_PACKET_INVALID;
  199. }
  200. }
  201. enum ZT1_ResultCode ZT1_Node_processVirtualNetworkFrame(
  202. ZT1_Node *node,
  203. uint64_t now,
  204. uint64_t nwid,
  205. uint64_t sourceMac,
  206. uint64_t destMac,
  207. unsigned int etherType,
  208. unsigned int vlanId,
  209. const void *frameData,
  210. unsigned int frameLength,
  211. uint64_t *nextCallDeadline)
  212. {
  213. try {
  214. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextCallDeadline);
  215. } catch (std::bad_alloc &exc) {
  216. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  217. } catch ( ... ) {
  218. return ZT1_RESULT_ERROR_INTERNAL;
  219. }
  220. }
  221. enum ZT1_Resultcode ZT1_Node_processNothing(ZT1_Node *node,uint64_t now,uint64_t *nextCallDeadline)
  222. {
  223. try {
  224. return reinterpret_cast<ZeroTier::Node *>(node)->processNothing(now,nextCallDeadline);
  225. } catch (std::bad_alloc &exc) {
  226. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  227. } catch ( ... ) {
  228. return ZT1_RESULT_ERROR_INTERNAL;
  229. }
  230. }
  231. enum ZT1_ResultCode ZT1_Node_join(ZT1_Node *node,uint64_t nwid)
  232. {
  233. try {
  234. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid);
  235. } catch (std::bad_alloc &exc) {
  236. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  237. } catch ( ... ) {
  238. return ZT1_RESULT_ERROR_INTERNAL;
  239. }
  240. }
  241. enum ZT1_ResultCode ZT1_Node_leave(ZT1_Node *node,uint64_t nwid)
  242. {
  243. try {
  244. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid);
  245. } catch (std::bad_alloc &exc) {
  246. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  247. } catch ( ... ) {
  248. return ZT1_RESULT_ERROR_INTERNAL;
  249. }
  250. }
  251. enum ZT1_ResultCode ZT1_Node_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  252. {
  253. try {
  254. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(nwid,multicastGroup,multicastAdi);
  255. } catch (std::bad_alloc &exc) {
  256. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  257. } catch ( ... ) {
  258. return ZT1_RESULT_ERROR_INTERNAL;
  259. }
  260. }
  261. enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  262. {
  263. try {
  264. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  265. } catch (std::bad_alloc &exc) {
  266. return ZT1_RESULT_ERROR_OUT_OF_MEMORY;
  267. } catch ( ... ) {
  268. return ZT1_RESULT_ERROR_INTERNAL;
  269. }
  270. }
  271. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
  272. {
  273. try {
  274. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  275. } catch ( ... ) {}
  276. }
  277. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node)
  278. {
  279. try {
  280. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  281. } catch ( ... ) {
  282. return (ZT1_PeerList *)0;
  283. }
  284. }
  285. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid)
  286. {
  287. try {
  288. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  289. } catch ( ... ) {
  290. return (ZT1_VirtualNetworkConfig *)0;
  291. }
  292. }
  293. ZT1_VirtualNetworkList *ZT1_Node_listNetworks(ZT1_Node *node)
  294. {
  295. try {
  296. return reinterpret_cast<ZeroTier::Node *>(node)->listNetworks();
  297. } catch ( ... ) {
  298. return (ZT1_VirtualNetworkList *)0;
  299. }
  300. }
  301. void ZT1_Node_freeQueryResult(ZT1_Node *node,void *qr)
  302. {
  303. try {
  304. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  305. } catch ( ... ) {}
  306. }
  307. void ZT1_Node_setNetconfMaster(ZT1_Node *node,void *networkConfigMasterInstance)
  308. {
  309. try {
  310. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkConfigMasterInstance);
  311. } catch ( ... ) {}
  312. }
  313. void ZT1_version(int *major,int *minor,int *revision,unsigned long *featureFlags)
  314. {
  315. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  316. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  317. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  318. if (featureFlags) {
  319. *featureFlags =
  320. ZT1_FEATURE_FLAG_THREAD_SAFE |
  321. #ifdef ZT_OFFICIAL_BUILD
  322. ZT1_FEATURE_FLAG_OFFICIAL
  323. #endif
  324. ;
  325. }
  326. }
  327. } // extern "C"