Node.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #ifndef ZT_NODE_HPP
  28. #define ZT_NODE_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <map>
  33. #include "Constants.hpp"
  34. #include "../include/ZeroTierOne.h"
  35. #include "RuntimeEnvironment.hpp"
  36. #include "InetAddress.hpp"
  37. #include "Mutex.hpp"
  38. #include "MAC.hpp"
  39. #include "Network.hpp"
  40. #include "Path.hpp"
  41. #include "Salsa20.hpp"
  42. #undef TRACE
  43. #ifdef ZT_TRACE
  44. #define TRACE(f,...) RR->node->postTrace(__FILE__,__LINE__,f,##__VA_ARGS__)
  45. #else
  46. #define TRACE(f,...) {}
  47. #endif
  48. namespace ZeroTier {
  49. /**
  50. * Implementation of Node object as defined in CAPI
  51. *
  52. * The pointer returned by ZT1_Node_new() is an instance of this class.
  53. */
  54. class Node
  55. {
  56. public:
  57. Node(
  58. uint64_t now,
  59. void *uptr,
  60. ZT1_DataStoreGetFunction dataStoreGetFunction,
  61. ZT1_DataStorePutFunction dataStorePutFunction,
  62. ZT1_WirePacketSendFunction wirePacketSendFunction,
  63. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  64. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  65. ZT1_EventCallback eventCallback,
  66. const char *overrideRootTopology);
  67. ~Node();
  68. // Public API Functions ----------------------------------------------------
  69. ZT1_ResultCode processWirePacket(
  70. uint64_t now,
  71. const struct sockaddr_storage *remoteAddress,
  72. const void *packetData,
  73. unsigned int packetLength,
  74. volatile uint64_t *nextBackgroundTaskDeadline);
  75. ZT1_ResultCode processVirtualNetworkFrame(
  76. uint64_t now,
  77. uint64_t nwid,
  78. uint64_t sourceMac,
  79. uint64_t destMac,
  80. unsigned int etherType,
  81. unsigned int vlanId,
  82. const void *frameData,
  83. unsigned int frameLength,
  84. volatile uint64_t *nextBackgroundTaskDeadline);
  85. ZT1_ResultCode processBackgroundTasks(uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
  86. ZT1_ResultCode join(uint64_t nwid);
  87. ZT1_ResultCode leave(uint64_t nwid);
  88. ZT1_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  89. ZT1_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  90. uint64_t address() const;
  91. void status(ZT1_NodeStatus *status) const;
  92. ZT1_PeerList *peers() const;
  93. ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid) const;
  94. ZT1_VirtualNetworkList *networks() const;
  95. void freeQueryResult(void *qr);
  96. int addLocalInterfaceAddress(const struct sockaddr_storage *addr,int metric,ZT1_LocalInterfaceAddressTrust trust);
  97. void clearLocalInterfaceAddresses();
  98. void setNetconfMaster(void *networkControllerInstance);
  99. // Internal functions ------------------------------------------------------
  100. /**
  101. * @return Time as of last call to run()
  102. */
  103. inline uint64_t now() const throw() { return _now; }
  104. /**
  105. * Enqueue a ZeroTier message to be sent
  106. *
  107. * @param addr Destination address
  108. * @param data Packet data
  109. * @param len Packet length
  110. * @return True if packet appears to have been sent
  111. */
  112. inline bool putPacket(const InetAddress &addr,const void *data,unsigned int len)
  113. {
  114. return (_wirePacketSendFunction(
  115. reinterpret_cast<ZT1_Node *>(this),
  116. _uPtr,
  117. reinterpret_cast<const struct sockaddr_storage *>(&addr),
  118. data,
  119. len) == 0);
  120. }
  121. /**
  122. * Enqueue a frame to be injected into a tap device (port)
  123. *
  124. * @param nwid Network ID
  125. * @param source Source MAC
  126. * @param dest Destination MAC
  127. * @param etherType 16-bit ethernet type
  128. * @param vlanId VLAN ID or 0 if none
  129. * @param data Frame data
  130. * @param len Frame length
  131. */
  132. inline void putFrame(uint64_t nwid,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  133. {
  134. _virtualNetworkFrameFunction(
  135. reinterpret_cast<ZT1_Node *>(this),
  136. _uPtr,
  137. nwid,
  138. source.toInt(),
  139. dest.toInt(),
  140. etherType,
  141. vlanId,
  142. data,
  143. len);
  144. }
  145. inline SharedPtr<Network> network(uint64_t nwid) const
  146. {
  147. Mutex::Lock _l(_networks_m);
  148. return _network(nwid);
  149. }
  150. inline std::vector< SharedPtr<Network> > allNetworks() const
  151. {
  152. std::vector< SharedPtr<Network> > nw;
  153. Mutex::Lock _l(_networks_m);
  154. nw.reserve(_networks.size());
  155. for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i)
  156. nw.push_back(i->second);
  157. return nw;
  158. }
  159. /**
  160. * @return Potential direct paths to me a.k.a. local interface addresses
  161. */
  162. inline std::vector<Path> directPaths() const
  163. {
  164. Mutex::Lock _l(_directPaths_m);
  165. return _directPaths;
  166. }
  167. inline bool dataStorePut(const char *name,const void *data,unsigned int len,bool secure) { return (_dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,name,data,len,(int)secure) == 0); }
  168. inline bool dataStorePut(const char *name,const std::string &data,bool secure) { return dataStorePut(name,(const void *)data.data(),(unsigned int)data.length(),secure); }
  169. inline void dataStoreDelete(const char *name) { _dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,name,(const void *)0,0,0); }
  170. std::string dataStoreGet(const char *name);
  171. /**
  172. * Post an event to the external user
  173. *
  174. * @param ev Event type
  175. * @param md Meta-data (default: NULL/none)
  176. */
  177. inline void postEvent(ZT1_Event ev,const void *md = (const void *)0) { _eventCallback(reinterpret_cast<ZT1_Node *>(this),_uPtr,ev,md); }
  178. /**
  179. * Update virtual network port configuration
  180. *
  181. * @param nwid Network ID
  182. * @param op Configuration operation
  183. * @param nc Network configuration
  184. */
  185. inline int configureVirtualNetworkPort(uint64_t nwid,ZT1_VirtualNetworkConfigOperation op,const ZT1_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,nwid,op,nc); }
  186. /**
  187. * @return True if we appear to be online
  188. */
  189. inline bool online() const throw() { return _online; }
  190. /**
  191. * If this version is newer than the newest we've seen, post a new version seen event
  192. */
  193. void postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev);
  194. #ifdef ZT_TRACE
  195. void postTrace(const char *module,unsigned int line,const char *fmt,...);
  196. #endif
  197. /**
  198. * @return Next 64-bit random number (not for cryptographic use)
  199. */
  200. uint64_t prng();
  201. private:
  202. inline SharedPtr<Network> _network(uint64_t nwid) const
  203. {
  204. // assumes _networks_m is locked
  205. for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i) {
  206. if (i->first == nwid)
  207. return i->second;
  208. }
  209. return SharedPtr<Network>();
  210. }
  211. RuntimeEnvironment _RR;
  212. RuntimeEnvironment *RR;
  213. void *_uPtr; // _uptr (lower case) is reserved in Visual Studio :P
  214. ZT1_DataStoreGetFunction _dataStoreGetFunction;
  215. ZT1_DataStorePutFunction _dataStorePutFunction;
  216. ZT1_WirePacketSendFunction _wirePacketSendFunction;
  217. ZT1_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
  218. ZT1_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
  219. ZT1_EventCallback _eventCallback;
  220. std::vector< std::pair< uint64_t, SharedPtr<Network> > > _networks;
  221. Mutex _networks_m;
  222. std::vector<Path> _directPaths;
  223. Mutex _directPaths_m;
  224. Mutex _backgroundTasksLock;
  225. unsigned int _prngStreamPtr;
  226. Salsa20 _prng;
  227. uint64_t _prngStream[16]; // repeatedly encrypted with _prng to yield a high-quality non-crypto PRNG stream
  228. uint64_t _now;
  229. uint64_t _lastPingCheck;
  230. uint64_t _lastHousekeepingRun;
  231. unsigned int _newestVersionSeen[3]; // major, minor, revision
  232. bool _online;
  233. };
  234. } // namespace ZeroTier
  235. #endif