Node.hpp 6.6 KB

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