Node.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "InetAddress.hpp"
  36. #include "Mutex.hpp"
  37. #include "MAC.hpp"
  38. #include "Network.hpp"
  39. #undef TRACE
  40. #ifdef ZT_TRACE
  41. #define TRACE(f,...) RR->node->postTrace(__FILE__,__LINE__,f,##__VA_ARGS__)
  42. #else
  43. #define TRACE(f,...) {}
  44. #endif
  45. namespace ZeroTier {
  46. class RuntimeEnvironment;
  47. /**
  48. * Implementation of Node object as defined in CAPI
  49. *
  50. * The pointer returned by ZT1_Node_new() is an instance of this class.
  51. */
  52. class Node
  53. {
  54. public:
  55. Node(
  56. uint64_t now,
  57. void *uptr,
  58. ZT1_DataStoreGetFunction dataStoreGetFunction,
  59. ZT1_DataStorePutFunction dataStorePutFunction,
  60. ZT1_WirePacketSendFunction wirePacketSendFunction,
  61. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  62. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  63. ZT1_EventCallback eventCallback,
  64. const char *overrideRootTopology);
  65. ~Node();
  66. // Public API Functions ----------------------------------------------------
  67. ZT1_ResultCode processWirePacket(
  68. uint64_t now,
  69. const struct sockaddr_storage *remoteAddress,
  70. unsigned int linkDesperation,
  71. const void *packetData,
  72. unsigned int packetLength,
  73. volatile uint64_t *nextBackgroundTaskDeadline);
  74. ZT1_ResultCode processVirtualNetworkFrame(
  75. uint64_t now,
  76. uint64_t nwid,
  77. uint64_t sourceMac,
  78. uint64_t destMac,
  79. unsigned int etherType,
  80. unsigned int vlanId,
  81. const void *frameData,
  82. unsigned int frameLength,
  83. volatile uint64_t *nextBackgroundTaskDeadline);
  84. ZT1_ResultCode processBackgroundTasks(uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
  85. ZT1_ResultCode join(uint64_t nwid);
  86. ZT1_ResultCode leave(uint64_t nwid);
  87. ZT1_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  88. ZT1_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  89. uint64_t address() const;
  90. void status(ZT1_NodeStatus *status) const;
  91. ZT1_PeerList *peers() const;
  92. ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid) const;
  93. ZT1_VirtualNetworkList *networks() const;
  94. void freeQueryResult(void *qr);
  95. void setNetconfMaster(void *networkControllerInstance);
  96. // Internal functions ------------------------------------------------------
  97. /**
  98. * @return Time as of last call to run()
  99. */
  100. inline uint64_t now() const throw() { return _now; }
  101. /**
  102. * Enqueue a ZeroTier message to be sent
  103. *
  104. * @param addr Destination address
  105. * @param data Packet data
  106. * @param len Packet length
  107. * @param desperation Link desperation for reaching this address
  108. * @return True if packet appears to have been sent
  109. */
  110. inline bool putPacket(const InetAddress &addr,const void *data,unsigned int len,unsigned int desperation)
  111. {
  112. return (_wirePacketSendFunction(
  113. reinterpret_cast<ZT1_Node *>(this),
  114. _uPtr,
  115. reinterpret_cast<const struct sockaddr_storage *>(&addr),
  116. desperation,
  117. data,
  118. len) == 0);
  119. }
  120. /**
  121. * Enqueue a frame to be injected into a tap device (port)
  122. *
  123. * @param nwid Network ID
  124. * @param source Source MAC
  125. * @param dest Destination MAC
  126. * @param etherType 16-bit ethernet type
  127. * @param vlanId VLAN ID or 0 if none
  128. * @param data Frame data
  129. * @param len Frame length
  130. */
  131. inline void putFrame(uint64_t nwid,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  132. {
  133. _virtualNetworkFrameFunction(
  134. reinterpret_cast<ZT1_Node *>(this),
  135. _uPtr,
  136. nwid,
  137. source.toInt(),
  138. dest.toInt(),
  139. etherType,
  140. vlanId,
  141. data,
  142. len);
  143. }
  144. inline SharedPtr<Network> network(uint64_t nwid)
  145. {
  146. Mutex::Lock _l(_networks_m);
  147. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  148. return ((nw == _networks.end()) ? SharedPtr<Network>() : nw->second);
  149. }
  150. inline std::vector< SharedPtr<Network> > allNetworks() const
  151. {
  152. Mutex::Lock _l(_networks_m);
  153. std::vector< SharedPtr<Network> > nw;
  154. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  155. nw.push_back(n->second);
  156. return nw;
  157. }
  158. /**
  159. * Get an overall current level of desperation
  160. *
  161. * The current level of desperation is based on how recently an upstream
  162. * (a.k.a. supernode) peer has spoken to us. As such, it will change and
  163. * return to 0 once something like tunneling (higher desperation link) is
  164. * active. As a result, actual link desperation for outgoing messages
  165. * should be the max of either this or the most recent link desperation
  166. * for an incoming message from a given address. See Path.hpp and Peer.hpp.
  167. *
  168. * In other words think of this as 'the desperation we should try to
  169. * escalate to right now.'
  170. *
  171. * @return Overall system level of desperation
  172. */
  173. inline unsigned int coreDesperation() const throw() { return _coreDesperation; }
  174. 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); }
  175. inline bool dataStorePut(const char *name,const std::string &data,bool secure) { return dataStorePut(name,(const void *)data.data(),(unsigned int)data.length(),secure); }
  176. inline void dataStoreDelete(const char *name) { _dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,name,(const void *)0,0,0); }
  177. std::string dataStoreGet(const char *name);
  178. /**
  179. * Post an event to the external user
  180. *
  181. * @param ev Event type
  182. * @param md Meta-data (default: NULL/none)
  183. */
  184. inline void postEvent(ZT1_Event ev,const void *md = (const void *)0) { _eventCallback(reinterpret_cast<ZT1_Node *>(this),_uPtr,ev,md); }
  185. /**
  186. * Update virtual network port configuration
  187. *
  188. * @param nwid Network ID
  189. * @param op Configuration operation
  190. * @param nc Network configuration
  191. */
  192. inline int configureVirtualNetworkPort(uint64_t nwid,ZT1_VirtualNetworkConfigOperation op,const ZT1_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,nwid,op,nc); }
  193. /**
  194. * @return True if we appear to be online
  195. */
  196. inline bool online() const throw() { return _online; }
  197. /**
  198. * If this version is newer than the newest we've seen, post a new version seen event
  199. */
  200. void postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev);
  201. #ifdef ZT_TRACE
  202. void postTrace(const char *module,unsigned int line,const char *fmt,...);
  203. #endif
  204. private:
  205. RuntimeEnvironment *RR;
  206. void *_uPtr; // _uptr (lower case) is reserved in Visual Studio :P
  207. ZT1_DataStoreGetFunction _dataStoreGetFunction;
  208. ZT1_DataStorePutFunction _dataStorePutFunction;
  209. ZT1_WirePacketSendFunction _wirePacketSendFunction;
  210. ZT1_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
  211. ZT1_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
  212. ZT1_EventCallback _eventCallback;
  213. //Dictionary _localConfig; // persisted as local.conf
  214. //Mutex _localConfig_m;
  215. std::map< uint64_t,SharedPtr<Network> > _networks;
  216. Mutex _networks_m;
  217. Mutex _backgroundTasksLock;
  218. uint64_t _now;
  219. uint64_t _startTimeAfterInactivity;
  220. uint64_t _lastPingCheck;
  221. uint64_t _lastHousekeepingRun;
  222. uint64_t _lastBeacon;
  223. unsigned int _coreDesperation;
  224. unsigned int _newestVersionSeen[3]; // major, minor, revision
  225. bool _online;
  226. };
  227. } // namespace ZeroTier
  228. #endif