Node.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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 <stdint.h>
  30. #include "../include/ZeroTierOne.h"
  31. namespace ZeroTier {
  32. class EthernetTapFactory;
  33. class RoutingTable;
  34. class SocketManager;
  35. /**
  36. * A ZeroTier One node
  37. */
  38. class Node
  39. {
  40. public:
  41. /**
  42. * Returned by node main if/when it terminates
  43. */
  44. enum ReasonForTermination
  45. {
  46. /**
  47. * Node is currently in run()
  48. */
  49. NODE_RUNNING = 0,
  50. /**
  51. * Node is shutting down for normal reasons, including a signal
  52. */
  53. NODE_NORMAL_TERMINATION = 1,
  54. /**
  55. * An upgrade is available. Its path is in reasonForTermination().
  56. */
  57. NODE_RESTART_FOR_UPGRADE = 2,
  58. /**
  59. * A serious unrecoverable error has occurred.
  60. */
  61. NODE_UNRECOVERABLE_ERROR = 3,
  62. /**
  63. * An address collision occurred (typically this should cause re-invocation with resetIdentity set to true)
  64. */
  65. NODE_ADDRESS_COLLISION = 4
  66. };
  67. /**
  68. * Create a new node
  69. *
  70. * The node is not executed until run() is called. The supplied tap factory
  71. * and routing table must not be freed until the node is no longer
  72. * executing. Node does not delete these objects; the caller still owns
  73. * them.
  74. *
  75. * @param hp Home directory path or NULL for system-wide default for this platform
  76. * @param tf Ethernet tap factory for platform network stack
  77. * @param rt Routing table interface for platform network stack
  78. * @param sm Socket manager for physical network I/O
  79. * @param resetIdentity If true, delete identity before starting and regenerate
  80. * @param overrideRootTopology Override root topology with this dictionary (in string serialized format) and do not update (default: NULL for none)
  81. */
  82. Node(
  83. const char *hp,
  84. EthernetTapFactory *tf,
  85. RoutingTable *rt,
  86. SocketManager *sm,
  87. bool resetIdentity,
  88. const char *overrideRootTopology = (const char *)0) throw();
  89. ~Node();
  90. /**
  91. * Execute node in current thread, return on shutdown
  92. *
  93. * @return Reason for termination
  94. */
  95. ReasonForTermination run()
  96. throw();
  97. /**
  98. * Obtain a human-readable reason for node termination
  99. *
  100. * @return Reason for node termination or NULL if run() has not returned
  101. */
  102. const char *terminationMessage() const
  103. throw();
  104. /**
  105. * Terminate this node, causing run() to return
  106. *
  107. * @param reason Reason for termination
  108. * @param reasonText Text to be returned by terminationMessage()
  109. */
  110. void terminate(ReasonForTermination reason,const char *reasonText)
  111. throw();
  112. /**
  113. * Forget p2p links now and resynchronize with peers
  114. *
  115. * This can be used if the containing application knows its network environment has
  116. * changed. ZeroTier itself tries to detect such changes, but is not always successful.
  117. */
  118. void resync()
  119. throw();
  120. /**
  121. * @return True if we appear to be online in some viable capacity
  122. */
  123. bool online()
  124. throw();
  125. /**
  126. * @return True if run() has been called
  127. */
  128. bool started()
  129. throw();
  130. /**
  131. * @return True if run() has not yet returned
  132. */
  133. bool running()
  134. throw();
  135. /**
  136. * @return True if initialization phase of startup is complete
  137. */
  138. bool initialized()
  139. throw();
  140. /**
  141. * @return This node's address (in least significant 40 bits of 64-bit int) or 0 if not yet initialized
  142. */
  143. uint64_t address()
  144. throw();
  145. /**
  146. * Join a network
  147. *
  148. * Use getNetworkStatus() to check the network's status after joining. If you
  149. * are already a member of the network, this does nothing.
  150. *
  151. * @param nwid 64-bit network ID
  152. */
  153. void join(uint64_t nwid)
  154. throw();
  155. /**
  156. * Leave a network
  157. *
  158. * @param nwid 64-bit network ID
  159. */
  160. void leave(uint64_t nwid)
  161. throw();
  162. /**
  163. * Get the status of this node
  164. *
  165. * @param status Buffer to fill with status information
  166. */
  167. void status(ZT1_Node_Status *status)
  168. throw();
  169. /**
  170. * @return List of known peers or NULL on failure
  171. */
  172. ZT1_Node_PeerList *listPeers()
  173. throw();
  174. /**
  175. * @param nwid 64-bit network ID
  176. * @return Network status or NULL if we are not a member of this network
  177. */
  178. ZT1_Node_Network *getNetworkStatus(uint64_t nwid)
  179. throw();
  180. /**
  181. * @return List of networks we've joined or NULL on failure
  182. */
  183. ZT1_Node_NetworkList *listNetworks()
  184. throw();
  185. /**
  186. * Free a query result buffer
  187. *
  188. * Use this to free the return values of listNetworks(), listPeers(), etc.
  189. *
  190. * @param qr Query result buffer
  191. */
  192. void freeQueryResult(void *qr)
  193. throw();
  194. /**
  195. * Check for software updates (if enabled) (updates will eventually get factored out of node/)
  196. */
  197. bool updateCheck()
  198. throw();
  199. /**
  200. * Inject a packet into a network's tap as if it came from the host
  201. *
  202. * This is primarily for debugging, and at the moment is only supported on
  203. * the test/dummy Ethernet tap implementation. Attempting to use it for real
  204. * devices will fail and return 'false.'
  205. *
  206. * @param nwid Network ID
  207. * @param from Source MAC address (must be 6 bytes in length)
  208. * @param to Destination MAC address (must be 6 bytes in length)
  209. * @param etherType Ethernet frame type
  210. * @param data Frame data
  211. * @param len Length of frame in bytes
  212. * @return True on success; false if not a member of network, injection not supported, or data too large
  213. */
  214. bool injectPacketFromHost(uint64_t nwid,const unsigned char *from,const unsigned char *to,unsigned int etherType,const void *data,unsigned int len);
  215. static const char *versionString() throw();
  216. static unsigned int versionMajor() throw();
  217. static unsigned int versionMinor() throw();
  218. static unsigned int versionRevision() throw();
  219. private:
  220. // Nodes are not copyable
  221. Node(const Node&);
  222. const Node& operator=(const Node&);
  223. void *const _impl; // private implementation
  224. };
  225. } // namespace ZeroTier
  226. #endif