Node.cpp 9.2 KB

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