Node.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 sm Socket manager for physical network I/O
  79. * @param nm Network configuration master or NULL for none
  80. * @param resetIdentity If true, delete identity before starting and regenerate
  81. * @param overrideRootTopology Override root topology with this dictionary (in string serialized format) and do not update (default: NULL for none)
  82. */
  83. Node(
  84. const char *hp,
  85. EthernetTapFactory *tf,
  86. SocketManager *sm,
  87. NetworkConfigMaster *nm,
  88. bool resetIdentity,
  89. const char *overrideRootTopology = (const char *)0) throw();
  90. ~Node();
  91. /**
  92. * Execute node in current thread, return on shutdown
  93. *
  94. * @return Reason for termination
  95. */
  96. ReasonForTermination run()
  97. throw();
  98. /**
  99. * Obtain a human-readable reason for node termination
  100. *
  101. * @return Reason for node termination or NULL if run() has not returned
  102. */
  103. const char *terminationMessage() const
  104. throw();
  105. /**
  106. * Terminate this node, causing run() to return
  107. *
  108. * @param reason Reason for termination
  109. * @param reasonText Text to be returned by terminationMessage()
  110. */
  111. void terminate(ReasonForTermination reason,const char *reasonText)
  112. throw();
  113. /**
  114. * Forget p2p links now and resynchronize with peers
  115. *
  116. * This can be used if the containing application knows its network environment has
  117. * changed. ZeroTier itself tries to detect such changes, but is not always successful.
  118. */
  119. void resync()
  120. throw();
  121. /**
  122. * @return True if we appear to be online in some viable capacity
  123. */
  124. bool online()
  125. throw();
  126. /**
  127. * @return True if run() has been called
  128. */
  129. bool started()
  130. throw();
  131. /**
  132. * @return True if run() has not yet returned
  133. */
  134. bool running()
  135. throw();
  136. /**
  137. * @return True if initialization phase of startup is complete
  138. */
  139. bool initialized()
  140. throw();
  141. /**
  142. * @return This node's address (in least significant 40 bits of 64-bit int) or 0 if not yet initialized
  143. */
  144. uint64_t address()
  145. throw();
  146. /**
  147. * Join a network
  148. *
  149. * Use getNetworkStatus() to check the network's status after joining. If you
  150. * are already a member of the network, this does nothing.
  151. *
  152. * @param nwid 64-bit network ID
  153. */
  154. void join(uint64_t nwid)
  155. throw();
  156. /**
  157. * Leave a network (if a member)
  158. *
  159. * @param nwid 64-bit network ID
  160. */
  161. void leave(uint64_t nwid)
  162. throw();
  163. /**
  164. * Get the status of this node
  165. *
  166. * @param status Buffer to fill with status information
  167. */
  168. void status(ZT1_Node_Status *status)
  169. throw();
  170. /**
  171. * @return List of known peers or NULL on failure
  172. */
  173. ZT1_Node_PeerList *listPeers()
  174. throw();
  175. /**
  176. * @param nwid 64-bit network ID
  177. * @return Network status or NULL if we are not a member of this network
  178. */
  179. ZT1_Node_Network *getNetworkStatus(uint64_t nwid)
  180. throw();
  181. /**
  182. * @return List of networks we've joined or NULL on failure
  183. */
  184. ZT1_Node_NetworkList *listNetworks()
  185. throw();
  186. /**
  187. * Free a query result buffer
  188. *
  189. * Use this to free the return values of listNetworks(), listPeers(), etc.
  190. *
  191. * @param qr Query result buffer
  192. */
  193. void freeQueryResult(void *qr)
  194. throw();
  195. /**
  196. * Check for software updates (if enabled) (updates will eventually get factored out of node/)
  197. */
  198. bool updateCheck()
  199. throw();
  200. static const char *versionString() throw();
  201. static unsigned int versionMajor() throw();
  202. static unsigned int versionMinor() throw();
  203. static unsigned int versionRevision() throw();
  204. private:
  205. // Nodes are not copyable
  206. Node(const Node&);
  207. const Node& operator=(const Node&);
  208. void *const _impl; // private implementation
  209. };
  210. } // namespace ZeroTier
  211. #endif