1
0

Node.hpp 5.6 KB

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