OneService.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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: 2023-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. /**
  78. * @return Platform default home path or empty string if this platform doesn't have one
  79. */
  80. static std::string platformDefaultHomePath();
  81. /**
  82. * Create a new instance of the service
  83. *
  84. * Once created, you must call the run() method to actually start
  85. * processing.
  86. *
  87. * The port is saved to a file in the home path called zerotier-one.port,
  88. * which is used by the CLI and can be used to see which port was chosen if
  89. * 0 (random port) is picked.
  90. *
  91. * @param hp Home path
  92. * @param port TCP and UDP port for packets and HTTP control (if 0, pick random port)
  93. */
  94. static OneService *newInstance(const char *hp,unsigned int port);
  95. virtual ~OneService();
  96. /**
  97. * Execute the service main I/O loop until terminated
  98. *
  99. * The terminate() method may be called from a signal handler or another
  100. * thread to terminate execution. Otherwise this will not return unless
  101. * another condition terminates execution such as a fatal error.
  102. */
  103. virtual ReasonForTermination run() = 0;
  104. /**
  105. * @return Reason for terminating or ONE_STILL_RUNNING if running
  106. */
  107. virtual ReasonForTermination reasonForTermination() const = 0;
  108. /**
  109. * @return Fatal error message or empty string if none
  110. */
  111. virtual std::string fatalErrorMessage() const = 0;
  112. /**
  113. * @return System device name corresponding with a given ZeroTier network ID or empty string if not opened yet or network ID not found
  114. */
  115. virtual std::string portDeviceName(uint64_t nwid) const = 0;
  116. #ifdef ZT_SDK
  117. /**
  118. * Whether we allow access to the service via local HTTP requests (disabled by default in libzt)
  119. */
  120. bool allowHttpBackplaneManagement = false;
  121. /**
  122. * @return Reference to the Node
  123. */
  124. virtual Node * getNode() = 0;
  125. /**
  126. * Fills out a structure with network-specific route information
  127. */
  128. virtual void getRoutes(uint64_t nwid, void *routeArray, unsigned int *numRoutes) = 0;
  129. #endif
  130. /**
  131. * Terminate background service (can be called from other threads)
  132. */
  133. virtual void terminate() = 0;
  134. /**
  135. * Get local settings for a network
  136. *
  137. * @param nwid Network ID
  138. * @param settings Buffer to fill with local network settings
  139. * @return True if network was found and settings is filled
  140. */
  141. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const = 0;
  142. /**
  143. * Set local settings for a network
  144. *
  145. * @param nwid Network ID
  146. * @param settings New network local settings
  147. * @return True if network was found and setting modified
  148. */
  149. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings) = 0;
  150. /**
  151. * @return True if service is still running
  152. */
  153. inline bool isRunning() const { return (this->reasonForTermination() == ONE_STILL_RUNNING); }
  154. protected:
  155. OneService() {}
  156. private:
  157. OneService(const OneService &one) {}
  158. inline OneService &operator=(const OneService &one) { return *this; }
  159. };
  160. } // namespace ZeroTier
  161. #endif