Node.cpp 9.4 KB

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