Node.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. char buf[16384];
  192. std::string r;
  193. unsigned long olen = 0;
  194. do {
  195. long n = _dataStoreGetFunction(reinterpret_cast<ZT1_Node *>(this),name,buf,sizeof(buf),r.length(),&olen);
  196. if (n <= 0)
  197. return std::string();
  198. r.append(buf,n);
  199. } while (r.length() < olen);
  200. return r;
  201. }
  202. void Node::postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev)
  203. {
  204. if (Peer::compareVersion(major,minor,rev,_newestVersionSeen[0],_newestVersionSeen[1],_newestVersionSeen[2]) > 0) {
  205. _newestVersionSeen[0] = major;
  206. _newestVersionSeen[1] = minor;
  207. _newestVersionSeen[2] = rev;
  208. this->postEvent(ZT1_EVENT_SAW_MORE_RECENT_VERSION);
  209. }
  210. }
  211. } // namespace ZeroTier
  212. /****************************************************************************/
  213. /* CAPI bindings */
  214. /****************************************************************************/
  215. extern "C" {
  216. enum ZT1_ResultCode ZT1_Node_new(
  217. ZT1_Node **node,
  218. uint64_t now,
  219. ZT1_DataStoreGetFunction dataStoreGetFunction,
  220. ZT1_DataStorePutFunction dataStorePutFunction,
  221. ZT1_WirePacketSendFunction wirePacketSendFunction,
  222. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  223. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  224. ZT1_StatusCallback statusCallback)
  225. {
  226. *node = (ZT1_Node *)0;
  227. try {
  228. *node = reinterpret_cast<ZT1_Node *>(new ZeroTier::Node(now,dataStoreGetFunction,dataStorePutFunction,wirePacketSendFunction,virtualNetworkFrameFunction,virtualNetworkConfigFunction,statusCallback));
  229. return ZT1_RESULT_OK;
  230. } catch (std::bad_alloc &exc) {
  231. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  232. } catch (std::runtime_error &exc) {
  233. return ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  234. } catch ( ... ) {
  235. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  236. }
  237. }
  238. void ZT1_Node_delete(ZT1_Node *node)
  239. {
  240. try {
  241. delete (reinterpret_cast<ZeroTier::Node *>(node));
  242. } catch ( ... ) {}
  243. }
  244. enum ZT1_ResultCode ZT1_Node_processWirePacket(
  245. ZT1_Node *node,
  246. uint64_t now,
  247. const struct sockaddr_storage *remoteAddress,
  248. unsigned int linkDesperation,
  249. const void *packetData,
  250. unsigned int packetLength,
  251. uint64_t *nextCallDeadline)
  252. {
  253. try {
  254. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(now,remoteAddress,linkDesperation,packetData,packetLength,nextCallDeadline);
  255. } catch (std::bad_alloc &exc) {
  256. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  257. } catch ( ... ) {
  258. return ZT1_RESULT_ERROR_PACKET_INVALID;
  259. }
  260. }
  261. enum ZT1_ResultCode ZT1_Node_processVirtualNetworkFrame(
  262. ZT1_Node *node,
  263. uint64_t now,
  264. uint64_t nwid,
  265. uint64_t sourceMac,
  266. uint64_t destMac,
  267. unsigned int etherType,
  268. unsigned int vlanId,
  269. const void *frameData,
  270. unsigned int frameLength,
  271. uint64_t *nextCallDeadline)
  272. {
  273. try {
  274. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,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_processNothing(ZT1_Node *node,uint64_t now,uint64_t *nextCallDeadline)
  282. {
  283. try {
  284. return reinterpret_cast<ZeroTier::Node *>(node)->processNothing(now,nextCallDeadline);
  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_join(ZT1_Node *node,uint64_t nwid)
  292. {
  293. try {
  294. return reinterpret_cast<ZeroTier::Node *>(node)->join(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_leave(ZT1_Node *node,uint64_t nwid)
  302. {
  303. try {
  304. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid);
  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_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  312. {
  313. try {
  314. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(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. enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  322. {
  323. try {
  324. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  325. } catch (std::bad_alloc &exc) {
  326. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  327. } catch ( ... ) {
  328. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  329. }
  330. }
  331. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
  332. {
  333. try {
  334. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  335. } catch ( ... ) {}
  336. }
  337. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node)
  338. {
  339. try {
  340. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  341. } catch ( ... ) {
  342. return (ZT1_PeerList *)0;
  343. }
  344. }
  345. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid)
  346. {
  347. try {
  348. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  349. } catch ( ... ) {
  350. return (ZT1_VirtualNetworkConfig *)0;
  351. }
  352. }
  353. ZT1_VirtualNetworkList *ZT1_Node_networks(ZT1_Node *node)
  354. {
  355. try {
  356. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  357. } catch ( ... ) {
  358. return (ZT1_VirtualNetworkList *)0;
  359. }
  360. }
  361. void ZT1_Node_freeQueryResult(ZT1_Node *node,void *qr)
  362. {
  363. try {
  364. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  365. } catch ( ... ) {}
  366. }
  367. void ZT1_Node_setNetconfMaster(ZT1_Node *node,void *networkConfigMasterInstance)
  368. {
  369. try {
  370. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkConfigMasterInstance);
  371. } catch ( ... ) {}
  372. }
  373. void ZT1_version(int *major,int *minor,int *revision,unsigned long *featureFlags)
  374. {
  375. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  376. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  377. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  378. if (featureFlags) {
  379. *featureFlags = (
  380. ZT1_FEATURE_FLAG_THREAD_SAFE
  381. #ifdef ZT_OFFICIAL_BUILD
  382. | ZT1_FEATURE_FLAG_OFFICIAL
  383. #endif
  384. );
  385. }
  386. }
  387. } // extern "C"