Node.hpp 5.2 KB

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