OneService.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 { typedef VirtualTap EthernetTap; }
  22. #endif
  23. // Forward declaration so we can avoid dragging everything in
  24. struct InetAddress;
  25. class Node;
  26. /**
  27. * Local service for ZeroTier One as system VPN/NFV provider
  28. */
  29. class OneService
  30. {
  31. public:
  32. /**
  33. * Returned by node main if/when it terminates
  34. */
  35. enum ReasonForTermination
  36. {
  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. /**
  60. * Allow this network to configure IP addresses and routes?
  61. */
  62. bool allowManaged;
  63. /**
  64. * Whitelist of addresses that can be configured by this network.
  65. * If empty and allowManaged is true, allow all private/pseudoprivate addresses.
  66. */
  67. std::vector<InetAddress> allowManagedWhitelist;
  68. /**
  69. * Allow configuration of IPs and routes within global (Internet) IP space?
  70. */
  71. bool allowGlobal;
  72. /**
  73. * Allow overriding of system default routes for "full tunnel" operation?
  74. */
  75. bool allowDefault;
  76. /**
  77. * Allow configuration of DNS for the network
  78. */
  79. bool allowDNS;
  80. };
  81. /**
  82. * @return Platform default home path or empty string if this platform doesn't have one
  83. */
  84. static std::string platformDefaultHomePath();
  85. /**
  86. * Create a new instance of the service
  87. *
  88. * Once created, you must call the run() method to actually start
  89. * processing.
  90. *
  91. * The port is saved to a file in the home path called zerotier-one.port,
  92. * which is used by the CLI and can be used to see which port was chosen if
  93. * 0 (random port) is picked.
  94. *
  95. * @param hp Home path
  96. * @param port TCP and UDP port for packets and HTTP control (if 0, pick random port)
  97. */
  98. static OneService *newInstance(const char *hp,unsigned int port);
  99. virtual ~OneService();
  100. /**
  101. * Execute the service main I/O loop until terminated
  102. *
  103. * The terminate() method may be called from a signal handler or another
  104. * thread to terminate execution. Otherwise this will not return unless
  105. * another condition terminates execution such as a fatal error.
  106. */
  107. virtual ReasonForTermination run() = 0;
  108. /**
  109. * @return Reason for terminating or ONE_STILL_RUNNING if running
  110. */
  111. virtual ReasonForTermination reasonForTermination() const = 0;
  112. /**
  113. * @return Fatal error message or empty string if none
  114. */
  115. virtual std::string fatalErrorMessage() const = 0;
  116. /**
  117. * @return System device name corresponding with a given ZeroTier network ID or empty string if not opened yet or network ID not found
  118. */
  119. virtual std::string portDeviceName(uint64_t nwid) const = 0;
  120. #ifdef ZT_SDK
  121. /**
  122. * Whether we allow access to the service via local HTTP requests (disabled by default in libzt)
  123. */
  124. bool allowHttpBackplaneManagement = false;
  125. /**
  126. * @return Reference to the Node
  127. */
  128. virtual Node * getNode() = 0;
  129. /**
  130. * Fills out a structure with network-specific route information
  131. */
  132. virtual void getRoutes(uint64_t nwid, void *routeArray, unsigned int *numRoutes) = 0;
  133. #endif
  134. /**
  135. * Terminate background service (can be called from other threads)
  136. */
  137. virtual void terminate() = 0;
  138. /**
  139. * Get local settings for a network
  140. *
  141. * @param nwid Network ID
  142. * @param settings Buffer to fill with local network settings
  143. * @return True if network was found and settings is filled
  144. */
  145. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const = 0;
  146. /**
  147. * Set local settings for a network
  148. *
  149. * @param nwid Network ID
  150. * @param settings New network local settings
  151. * @return True if network was found and setting modified
  152. */
  153. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings) = 0;
  154. /**
  155. * @return True if service is still running
  156. */
  157. inline bool isRunning() const { return (this->reasonForTermination() == ONE_STILL_RUNNING); }
  158. protected:
  159. OneService() {}
  160. private:
  161. OneService(const OneService &one) {}
  162. inline OneService &operator=(const OneService &one) { return *this; }
  163. };
  164. } // namespace ZeroTier
  165. #endif