Node.hpp 5.9 KB

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