2
0

OneService.hpp 4.6 KB

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