OneService.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_ONESERVICE_HPP
  14. #define ZT_ONESERVICE_HPP
  15. #include <string>
  16. #include <vector>
  17. namespace ZeroTier {
  18. #ifdef ZT_SDK
  19. class VirtualTap;
  20. // Use the virtual libzt endpoint instead of a tun/tap port driver
  21. namespace ZeroTier {
  22. typedef VirtualTap EthernetTap;
  23. }
  24. #endif
  25. // Forward declaration so we can avoid dragging everything in
  26. struct InetAddress;
  27. class Node;
  28. /**
  29. * Local service for ZeroTier One as system VPN/NFV provider
  30. */
  31. class OneService {
  32. public:
  33. /**
  34. * Returned by node main if/when it terminates
  35. */
  36. enum ReasonForTermination {
  37. /**
  38. * Instance is still running
  39. */
  40. ONE_STILL_RUNNING = 0,
  41. /**
  42. * Normal shutdown
  43. */
  44. ONE_NORMAL_TERMINATION = 1,
  45. /**
  46. * A serious unrecoverable error has occurred
  47. */
  48. ONE_UNRECOVERABLE_ERROR = 2,
  49. /**
  50. * Your identity has collided with another
  51. */
  52. ONE_IDENTITY_COLLISION = 3
  53. };
  54. /**
  55. * Local settings for each network
  56. */
  57. struct NetworkSettings {
  58. /**
  59. * Allow this network to configure IP addresses and routes?
  60. */
  61. bool allowManaged;
  62. /**
  63. * Whitelist of addresses that can be configured by this network.
  64. * If empty and allowManaged is true, allow all private/pseudoprivate addresses.
  65. */
  66. std::vector<InetAddress> allowManagedWhitelist;
  67. /**
  68. * Allow configuration of IPs and routes within global (Internet) IP space?
  69. */
  70. bool allowGlobal;
  71. /**
  72. * Allow overriding of system default routes for "full tunnel" operation?
  73. */
  74. bool allowDefault;
  75. /**
  76. * Allow configuration of DNS for the network
  77. */
  78. bool allowDNS;
  79. };
  80. /**
  81. * @return Platform default home path or empty string if this platform doesn't have one
  82. */
  83. static std::string platformDefaultHomePath();
  84. /**
  85. * Create a new instance of the service
  86. *
  87. * Once created, you must call the run() method to actually start
  88. * processing.
  89. *
  90. * The port is saved to a file in the home path called zerotier-one.port,
  91. * which is used by the CLI and can be used to see which port was chosen if
  92. * 0 (random port) is picked.
  93. *
  94. * @param hp Home path
  95. * @param port TCP and UDP port for packets and HTTP control (if 0, pick random port)
  96. */
  97. static OneService* newInstance(const char* hp, unsigned int port);
  98. virtual ~OneService();
  99. /**
  100. * Execute the service main I/O loop until terminated
  101. *
  102. * The terminate() method may be called from a signal handler or another
  103. * thread to terminate execution. Otherwise this will not return unless
  104. * another condition terminates execution such as a fatal error.
  105. */
  106. virtual ReasonForTermination run() = 0;
  107. /**
  108. * @return Reason for terminating or ONE_STILL_RUNNING if running
  109. */
  110. virtual ReasonForTermination reasonForTermination() const = 0;
  111. /**
  112. * @return Fatal error message or empty string if none
  113. */
  114. virtual std::string fatalErrorMessage() const = 0;
  115. /**
  116. * @return System device name corresponding with a given ZeroTier network ID or empty string if not opened yet or network ID not found
  117. */
  118. virtual std::string portDeviceName(uint64_t nwid) const = 0;
  119. #ifdef ZT_SDK
  120. /**
  121. * Whether we allow access to the service via local HTTP requests (disabled by default in libzt)
  122. */
  123. bool allowHttpBackplaneManagement = false;
  124. /**
  125. * @return Reference to the Node
  126. */
  127. virtual Node* getNode() = 0;
  128. /**
  129. * Fills out a structure with network-specific route information
  130. */
  131. virtual void getRoutes(uint64_t nwid, void* routeArray, unsigned int* numRoutes) = 0;
  132. #endif
  133. /**
  134. * Terminate background service (can be called from other threads)
  135. */
  136. virtual void terminate() = 0;
  137. /**
  138. * Get local settings for a network
  139. *
  140. * @param nwid Network ID
  141. * @param settings Buffer to fill with local network settings
  142. * @return True if network was found and settings is filled
  143. */
  144. virtual bool getNetworkSettings(const uint64_t nwid, NetworkSettings& settings) const = 0;
  145. /**
  146. * Set local settings for a network
  147. *
  148. * @param nwid Network ID
  149. * @param settings New network local settings
  150. * @return True if network was found and setting modified
  151. */
  152. virtual bool setNetworkSettings(const uint64_t nwid, const NetworkSettings& settings) = 0;
  153. /**
  154. * @return True if service is still running
  155. */
  156. inline bool isRunning() const
  157. {
  158. return (this->reasonForTermination() == ONE_STILL_RUNNING);
  159. }
  160. protected:
  161. OneService()
  162. {
  163. }
  164. private:
  165. OneService(const OneService& one)
  166. {
  167. }
  168. inline OneService& operator=(const OneService& one)
  169. {
  170. return *this;
  171. }
  172. };
  173. } // namespace ZeroTier
  174. #endif