Node.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <string>
  30. #include <vector>
  31. namespace ZeroTier {
  32. /**
  33. * A ZeroTier One node
  34. *
  35. * This class hides all its implementation details and all other classes in
  36. * preparation for ZeroTier One being made available in library form for
  37. * embedding in mobile apps.
  38. */
  39. class Node
  40. {
  41. public:
  42. /**
  43. * Client for controlling a local ZeroTier One node
  44. *
  45. * Windows note: be sure you call WSAStartup() before using this,
  46. * otherwise it will be unable to open a local UDP socket to
  47. * communicate with the service.
  48. */
  49. class LocalClient
  50. {
  51. public:
  52. /**
  53. * Create a new node config client
  54. *
  55. * @param authToken Authentication token
  56. * @param controlPort Control port or 0 for 39393 (default)
  57. * @param resultHandler Function to call when commands provide results
  58. */
  59. LocalClient(const char *authToken,unsigned int controlPort,void (*resultHandler)(void *,unsigned long,const char *),void *arg)
  60. throw();
  61. ~LocalClient();
  62. /**
  63. * Send a command to the local node
  64. *
  65. * Note that the returned conversation ID will never be 0. A return value
  66. * of 0 indicates a fatal error such as failure to bind to any local UDP
  67. * port.
  68. *
  69. * @param command
  70. * @return Conversation ID that will be provided to result handler when/if results are sent back
  71. */
  72. unsigned long send(const char *command)
  73. throw();
  74. inline unsigned long send(const std::string &command) throw() { return send(command.c_str()); }
  75. /**
  76. * Split a line of results by space
  77. *
  78. * @param line Line to split
  79. * @return Vector of fields
  80. */
  81. static std::vector<std::string> splitLine(const char *line);
  82. static inline std::vector<std::string> splitLine(const std::string &line) { return splitLine(line.c_str()); }
  83. /**
  84. * @return Default path for user-local authorization token for the current user or empty string if cannot be determined
  85. */
  86. static std::string authTokenDefaultUserPath();
  87. /**
  88. * @return Default system path for auth token on this platform
  89. */
  90. static std::string authTokenDefaultSystemPath();
  91. private:
  92. // LocalClient is not copyable
  93. LocalClient(const LocalClient&);
  94. const LocalClient& operator=(const LocalClient&);
  95. void *_impl;
  96. };
  97. /**
  98. * Returned by node main if/when it terminates
  99. */
  100. enum ReasonForTermination
  101. {
  102. /**
  103. * Node is currently in run()
  104. */
  105. NODE_RUNNING = 0,
  106. /**
  107. * Node is shutting down for normal reasons, including a signal
  108. */
  109. NODE_NORMAL_TERMINATION = 1,
  110. /**
  111. * An upgrade is available. Its path is in reasonForTermination().
  112. */
  113. NODE_RESTART_FOR_UPGRADE = 2,
  114. /**
  115. * A serious unrecoverable error has occurred.
  116. */
  117. NODE_UNRECOVERABLE_ERROR = 3
  118. };
  119. /**
  120. * Create a new node
  121. *
  122. * The node is not executed until run() is called.
  123. *
  124. * @param hp Home directory path
  125. * @param port Port to bind for talking to the ZT1 network or 0 for 9993 (default)
  126. * @param controlPort Port to bind locally for control packets or 0 for 39393 (default)
  127. */
  128. Node(const char *hp,unsigned int port,unsigned int controlPort)
  129. throw();
  130. ~Node();
  131. /**
  132. * Execute node in current thread
  133. *
  134. * This does not return until the node shuts down. Shutdown may be caused
  135. * by an internally detected condition such as a new upgrade being
  136. * available or a fatal error, or it may be signaled externally using
  137. * the terminate() method.
  138. *
  139. * @return Reason for termination
  140. */
  141. ReasonForTermination run()
  142. throw();
  143. /**
  144. * Obtain a human-readable reason for node termination
  145. *
  146. * @return Reason for node termination or NULL if run() has not returned
  147. */
  148. const char *reasonForTermination() const
  149. throw();
  150. /**
  151. * Cause run() to return
  152. *
  153. * This can be called from a signal handler or another thread to signal a
  154. * running node to shut down. Shutdown may take a few seconds, so run()
  155. * may not return instantly. Multiple calls are ignored.
  156. *
  157. * @param reason Reason for termination
  158. * @param reasonText Text to be returned by reasonForTermination()
  159. */
  160. void terminate(ReasonForTermination reason,const char *reasonText)
  161. throw();
  162. /**
  163. * Forget p2p links and resynchronize with peers
  164. */
  165. void resync()
  166. throw();
  167. /**
  168. * Get the ZeroTier version in major.minor.revision string format
  169. *
  170. * @return Version in string form
  171. */
  172. static const char *versionString()
  173. throw();
  174. static unsigned int versionMajor() throw();
  175. static unsigned int versionMinor() throw();
  176. static unsigned int versionRevision() throw();
  177. private:
  178. // Nodes are not copyable
  179. Node(const Node&);
  180. const Node& operator=(const Node&);
  181. void *const _impl; // private implementation
  182. };
  183. } // namespace ZeroTier
  184. extern "C" {
  185. // Functions with C-style linkage for easy DLL symbol table
  186. // lookup. These just create instances of Node and LocalClient.
  187. ZeroTier::Node *zeroTierCreateNode(const char *hp,unsigned int port,unsigned int controlPort);
  188. void zeroTierDeleteNode(ZeroTier::Node *n);
  189. ZeroTier::Node::LocalClient *zeroTierCreateLocalClient(const char *authToken,unsigned int controlPort,void (*resultHandler)(void *,unsigned long,const char *),void *arg);
  190. void zeroTierDeleteLocalClient(ZeroTier::Node::LocalClient *lc);
  191. } // extern "C"
  192. #endif