Node.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. namespace ZeroTier {
  40. class RuntimeEnvironment;
  41. /**
  42. * Implementation of Node object as defined in CAPI
  43. *
  44. * The pointer returned by ZT1_Node_new() is an instance of this class.
  45. */
  46. class Node
  47. {
  48. public:
  49. Node(
  50. uint64_t now,
  51. ZT1_DataStoreGetFunction dataStoreGetFunction,
  52. ZT1_DataStorePutFunction dataStorePutFunction,
  53. ZT1_WirePacketSendFunction wirePacketSendFunction,
  54. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  55. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  56. ZT1_StatusCallback statusCallback,
  57. const char *overrideRootTopology);
  58. ~Node();
  59. // Public API Functions ----------------------------------------------------
  60. ZT1_ResultCode processWirePacket(
  61. uint64_t now,
  62. const struct sockaddr_storage *remoteAddress,
  63. unsigned int linkDesperation,
  64. const void *packetData,
  65. unsigned int packetLength,
  66. uint64_t *nextBackgroundTaskDeadline);
  67. ZT1_ResultCode processVirtualNetworkFrame(
  68. uint64_t now,
  69. uint64_t nwid,
  70. uint64_t sourceMac,
  71. uint64_t destMac,
  72. unsigned int etherType,
  73. unsigned int vlanId,
  74. const void *frameData,
  75. unsigned int frameLength,
  76. uint64_t *nextBackgroundTaskDeadline);
  77. ZT1_ResultCode processBackgroundTasks(uint64_t now,uint64_t *nextBackgroundTaskDeadline);
  78. ZT1_ResultCode join(uint64_t nwid);
  79. ZT1_ResultCode leave(uint64_t nwid);
  80. ZT1_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  81. ZT1_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  82. void status(ZT1_NodeStatus *status);
  83. ZT1_PeerList *peers();
  84. ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid);
  85. ZT1_VirtualNetworkList *networks();
  86. void freeQueryResult(void *qr);
  87. void setNetconfMaster(void *networkConfigMasterInstance);
  88. // Internal functions ------------------------------------------------------
  89. /**
  90. * @return Time as of last call to run()
  91. */
  92. inline uint64_t now() const throw() { return _now; }
  93. /**
  94. * Enqueue a ZeroTier message to be sent
  95. *
  96. * @param addr Destination address
  97. * @param data Packet data
  98. * @param len Packet length
  99. * @param desperation Link desperation for reaching this address
  100. * @return True if packet appears to have been sent
  101. */
  102. inline bool putPacket(const InetAddress &addr,const void *data,unsigned int len,unsigned int desperation)
  103. {
  104. return (_wirePacketSendFunction(
  105. reinterpret_cast<ZT1_Node *>(this),
  106. reinterpret_cast<const struct sockaddr_storage *>(&addr),
  107. desperation,
  108. data,
  109. len) == 0);
  110. }
  111. /**
  112. * Enqueue a frame to be injected into a tap device (port)
  113. *
  114. * @param nwid Network ID
  115. * @param source Source MAC
  116. * @param dest Destination MAC
  117. * @param etherType 16-bit ethernet type
  118. * @param vlanId VLAN ID or 0 if none
  119. * @param data Frame data
  120. * @param len Frame length
  121. */
  122. inline void putFrame(uint64_t nwid,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  123. {
  124. _virtualNetworkFrameFunction(
  125. reinterpret_cast<ZT1_Node *>(this),
  126. nwid,
  127. source.toInt(),
  128. dest.toInt(),
  129. etherType,
  130. vlanId,
  131. data,
  132. len);
  133. }
  134. inline SharedPtr<Network> network(uint64_t nwid)
  135. {
  136. Mutex::Lock _l(_networks_m);
  137. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  138. return ((nw == _networks.end()) ? SharedPtr<Network>() : nw->second);
  139. }
  140. inline std::vector< SharedPtr<Network> > allNetworks() const
  141. {
  142. Mutex::Lock _l(_networks_m);
  143. std::vector< SharedPtr<Network> > nw;
  144. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  145. nw.push_back(n->second);
  146. return nw;
  147. }
  148. inline unsigned int coreDesperation() const throw() { return _coreDesperation; }
  149. inline bool dataStorePut(const char *name,const void *data,unsigned int len,bool secure) { return (_dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),name,data,len,(int)secure) == 0); }
  150. inline bool dataStorePut(const char *name,const std::string &data,bool secure) { return dataStorePut(name,(const void *)data.data(),(unsigned int)data.length(),secure); }
  151. inline void dataStoreDelete(const char *name) { _dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),name,(const void *)0,0,0); }
  152. std::string dataStoreGet(const char *name);
  153. inline void postEvent(ZT1_Event ev) { _statusCallback(reinterpret_cast<ZT1_Node *>(this),ev); }
  154. inline int configureVirtualNetworkPort(uint64_t nwid,ZT1_VirtualNetworkConfigOperation op,const ZT1_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT1_Node *>(this),nwid,op,nc); }
  155. void postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev);
  156. private:
  157. RuntimeEnvironment *RR;
  158. ZT1_DataStoreGetFunction _dataStoreGetFunction;
  159. ZT1_DataStorePutFunction _dataStorePutFunction;
  160. ZT1_WirePacketSendFunction _wirePacketSendFunction;
  161. ZT1_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
  162. ZT1_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
  163. ZT1_StatusCallback _statusCallback;
  164. //Dictionary _localConfig; // persisted as local.conf
  165. //Mutex _localConfig_m;
  166. std::map< uint64_t,SharedPtr<Network> > _networks;
  167. Mutex _networks_m;
  168. Mutex _backgroundTasksLock;
  169. uint64_t _now;
  170. uint64_t _startTimeAfterInactivity;
  171. uint64_t _lastPingCheck;
  172. uint64_t _lastHousekeepingRun;
  173. unsigned int _coreDesperation;
  174. unsigned int _newestVersionSeen[3]; // major, minor, revision
  175. };
  176. } // namespace ZeroTier
  177. #endif