Node.hpp 6.4 KB

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