Node.hpp 5.5 KB

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