Node.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 NodeControlClient
  50. {
  51. public:
  52. /**
  53. * Create a new node config client
  54. *
  55. * Initialization may fail. Call error() to check.
  56. *
  57. * @param hp Home path of ZeroTier One instance
  58. * @param resultHandler Function to call when commands provide results
  59. * @param arg First argument to result handler
  60. * @param authToken Authentication token or NULL (default) to read from authtoken.secret in home path
  61. */
  62. NodeControlClient(const char *hp,void (*resultHandler)(void *,const char *),void *arg,const char *authToken = (const char *)0)
  63. throw();
  64. ~NodeControlClient();
  65. /**
  66. * @return Initialization error or NULL if none
  67. */
  68. const char *error() const
  69. throw();
  70. /**
  71. * Send a command to the local node
  72. *
  73. * Note that the returned conversation ID will never be 0. A return value
  74. * of 0 indicates a fatal error such as failure to bind to any local UDP
  75. * port.
  76. *
  77. * @param command
  78. * @return Conversation ID that will be provided to result handler when/if results are sent back
  79. */
  80. void send(const char *command)
  81. throw();
  82. inline void send(const std::string &command)
  83. throw() { return send(command.c_str()); }
  84. /**
  85. * Split a line of results
  86. *
  87. * @param line Line to split
  88. * @return Vector of fields
  89. */
  90. static std::vector<std::string> splitLine(const char *line);
  91. static inline std::vector<std::string> splitLine(const std::string &line) { return splitLine(line.c_str()); }
  92. /**
  93. * @return Default path for current user's authtoken.secret
  94. */
  95. static const char *authTokenDefaultUserPath();
  96. /**
  97. * @return Default path to system authtoken.secret
  98. */
  99. static const char *authTokenDefaultSystemPath();
  100. private:
  101. // NodeControlClient is not copyable
  102. NodeControlClient(const NodeControlClient&);
  103. const NodeControlClient& operator=(const NodeControlClient&);
  104. void *_impl;
  105. };
  106. /**
  107. * Returned by node main if/when it terminates
  108. */
  109. enum ReasonForTermination
  110. {
  111. /**
  112. * Node is currently in run()
  113. */
  114. NODE_RUNNING = 0,
  115. /**
  116. * Node is shutting down for normal reasons, including a signal
  117. */
  118. NODE_NORMAL_TERMINATION = 1,
  119. /**
  120. * An upgrade is available. Its path is in reasonForTermination().
  121. */
  122. NODE_RESTART_FOR_UPGRADE = 2,
  123. /**
  124. * A serious unrecoverable error has occurred.
  125. */
  126. NODE_UNRECOVERABLE_ERROR = 3,
  127. /**
  128. * An address collision occurred (typically this should cause re-invocation with resetIdentity set to true)
  129. */
  130. NODE_ADDRESS_COLLISION = 4
  131. };
  132. /**
  133. * Create a new node
  134. *
  135. * The node is not executed until run() is called.
  136. *
  137. * @param hp Home directory path or NULL for system-wide default for this platform (default: NULL)
  138. * @param udpPort UDP port or 0 to disable (default: 9993)
  139. * @param tcpPort TCP port or 0 to disable (default: 0)
  140. * @param resetIdentity If true, delete identity before starting and regenerate (default: false)
  141. */
  142. Node(const char *hp = (const char *)0,unsigned int udpPort = 9993,unsigned int tcpPort = 0,bool resetIdentity = false)
  143. throw();
  144. ~Node();
  145. /**
  146. * Execute node in current thread
  147. *
  148. * This does not return until the node shuts down. Shutdown may be caused
  149. * by an internally detected condition such as a new upgrade being
  150. * available or a fatal error, or it may be signaled externally using
  151. * the terminate() method.
  152. *
  153. * @return Reason for termination
  154. */
  155. ReasonForTermination run()
  156. throw();
  157. /**
  158. * Obtain a human-readable reason for node termination
  159. *
  160. * @return Reason for node termination or NULL if run() has not returned
  161. */
  162. const char *reasonForTermination() const
  163. throw();
  164. /**
  165. * Cause run() to return
  166. *
  167. * This can be called from a signal handler or another thread to signal a
  168. * running node to shut down. Shutdown may take a few seconds, so run()
  169. * may not return instantly. Multiple calls are ignored.
  170. *
  171. * @param reason Reason for termination
  172. * @param reasonText Text to be returned by reasonForTermination()
  173. */
  174. void terminate(ReasonForTermination reason,const char *reasonText)
  175. throw();
  176. /**
  177. * Forget p2p links now and resynchronize with peers
  178. */
  179. void resync()
  180. throw();
  181. static const char *versionString() throw();
  182. static unsigned int versionMajor() throw();
  183. static unsigned int versionMinor() throw();
  184. static unsigned int versionRevision() throw();
  185. private:
  186. // Nodes are not copyable
  187. Node(const Node&);
  188. const Node& operator=(const Node&);
  189. void *const _impl; // private implementation
  190. };
  191. } // namespace ZeroTier
  192. #endif