OneService.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_ONESERVICE_HPP
  27. #define ZT_ONESERVICE_HPP
  28. #include <string>
  29. #include <vector>
  30. #include "../node/InetAddress.hpp"
  31. #ifdef ZT_SDK
  32. #include "../node/Node.hpp"
  33. // Use the virtual netcon endpoint instead of a tun/tap port driver
  34. #include "../src/SocketTap.hpp"
  35. namespace ZeroTier { typedef SocketTap EthernetTap; }
  36. #endif
  37. namespace ZeroTier {
  38. /**
  39. * Local service for ZeroTier One as system VPN/NFV provider
  40. */
  41. class OneService
  42. {
  43. public:
  44. /**
  45. * Returned by node main if/when it terminates
  46. */
  47. enum ReasonForTermination
  48. {
  49. /**
  50. * Instance is still running
  51. */
  52. ONE_STILL_RUNNING = 0,
  53. /**
  54. * Normal shutdown
  55. */
  56. ONE_NORMAL_TERMINATION = 1,
  57. /**
  58. * A serious unrecoverable error has occurred
  59. */
  60. ONE_UNRECOVERABLE_ERROR = 2,
  61. /**
  62. * Your identity has collided with another
  63. */
  64. ONE_IDENTITY_COLLISION = 3
  65. };
  66. /**
  67. * Local settings for each network
  68. */
  69. struct NetworkSettings
  70. {
  71. /**
  72. * Allow this network to configure IP addresses and routes?
  73. */
  74. bool allowManaged;
  75. /**
  76. * Whitelist of addresses that can be configured by this network.
  77. * If empty and allowManaged is true, allow all private/pseudoprivate addresses.
  78. */
  79. std::vector<InetAddress> allowManagedWhitelist;
  80. /**
  81. * Allow configuration of IPs and routes within global (Internet) IP space?
  82. */
  83. bool allowGlobal;
  84. /**
  85. * Allow overriding of system default routes for "full tunnel" operation?
  86. */
  87. bool allowDefault;
  88. };
  89. /**
  90. * @return Platform default home path or empty string if this platform doesn't have one
  91. */
  92. static std::string platformDefaultHomePath();
  93. /**
  94. * Create a new instance of the service
  95. *
  96. * Once created, you must call the run() method to actually start
  97. * processing.
  98. *
  99. * The port is saved to a file in the home path called zerotier-one.port,
  100. * which is used by the CLI and can be used to see which port was chosen if
  101. * 0 (random port) is picked.
  102. *
  103. * @param hp Home path
  104. * @param port TCP and UDP port for packets and HTTP control (if 0, pick random port)
  105. */
  106. static OneService *newInstance(const char *hp,unsigned int port);
  107. virtual ~OneService();
  108. /**
  109. * Execute the service main I/O loop until terminated
  110. *
  111. * The terminate() method may be called from a signal handler or another
  112. * thread to terminate execution. Otherwise this will not return unless
  113. * another condition terminates execution such as a fatal error.
  114. */
  115. virtual ReasonForTermination run() = 0;
  116. /**
  117. * @return Reason for terminating or ONE_STILL_RUNNING if running
  118. */
  119. virtual ReasonForTermination reasonForTermination() const = 0;
  120. /**
  121. * @return Fatal error message or empty string if none
  122. */
  123. virtual std::string fatalErrorMessage() const = 0;
  124. /**
  125. * @return System device name corresponding with a given ZeroTier network ID or empty string if not opened yet or network ID not found
  126. */
  127. virtual std::string portDeviceName(uint64_t nwid) const = 0;
  128. #ifdef ZT_SDK
  129. /**
  130. * Leaves a network
  131. */
  132. virtual void leave(const char *hp) = 0;
  133. /**
  134. * Joins a network
  135. */
  136. virtual void join(const char *hp) = 0;
  137. /**
  138. * Returns the homePath given by the client application
  139. */
  140. virtual std::string givenHomePath() = 0;
  141. /*
  142. * Returns a SocketTap that is associated with the given nwid
  143. */
  144. virtual EthernetTap * getTap(uint64_t nwid) = 0;
  145. /*
  146. * Returns a SocketTap that cant function as a route to the specified host
  147. */
  148. virtual EthernetTap * getTap(InetAddress &addr) = 0;
  149. /*
  150. * Returns a pointer to the Node
  151. */
  152. virtual Node * getNode() = 0;
  153. /*
  154. * Delete all SocketTap interfaces
  155. */
  156. virtual void removeNets() = 0;
  157. #endif
  158. /**
  159. * Terminate background service (can be called from other threads)
  160. */
  161. virtual void terminate() = 0;
  162. /**
  163. * Get local settings for a network
  164. *
  165. * @param nwid Network ID
  166. * @param settings Buffer to fill with local network settings
  167. * @return True if network was found and settings is filled
  168. */
  169. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const = 0;
  170. /**
  171. * Set local settings for a network
  172. *
  173. * @param nwid Network ID
  174. * @param settings New network local settings
  175. * @return True if network was found and setting modified
  176. */
  177. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings) = 0;
  178. /**
  179. * @return True if service is still running
  180. */
  181. inline bool isRunning() const { return (this->reasonForTermination() == ONE_STILL_RUNNING); }
  182. protected:
  183. OneService() {}
  184. private:
  185. OneService(const OneService &one) {}
  186. inline OneService &operator=(const OneService &one) { return *this; }
  187. };
  188. } // namespace ZeroTier
  189. #endif