OneService.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_ONESERVICE_HPP
  19. #define ZT_ONESERVICE_HPP
  20. #include <string>
  21. namespace ZeroTier {
  22. /**
  23. * Local service for ZeroTier One as system VPN/NFV provider
  24. */
  25. class OneService
  26. {
  27. public:
  28. /**
  29. * Returned by node main if/when it terminates
  30. */
  31. enum ReasonForTermination
  32. {
  33. /**
  34. * Instance is still running
  35. */
  36. ONE_STILL_RUNNING = 0,
  37. /**
  38. * Normal shutdown
  39. */
  40. ONE_NORMAL_TERMINATION = 1,
  41. /**
  42. * A serious unrecoverable error has occurred
  43. */
  44. ONE_UNRECOVERABLE_ERROR = 2,
  45. /**
  46. * Your identity has collided with another
  47. */
  48. ONE_IDENTITY_COLLISION = 3
  49. };
  50. /**
  51. * Local settings for each network
  52. */
  53. struct NetworkSettings
  54. {
  55. /**
  56. * Allow this network to configure IP addresses and routes?
  57. */
  58. bool allowManaged;
  59. /**
  60. * Allow configuration of IPs and routes within global (Internet) IP space?
  61. */
  62. bool allowGlobal;
  63. /**
  64. * Allow overriding of system default routes for "full tunnel" operation?
  65. */
  66. bool allowDefault;
  67. };
  68. /**
  69. * @return Platform default home path or empty string if this platform doesn't have one
  70. */
  71. static std::string platformDefaultHomePath();
  72. /**
  73. * @return Auto-update URL or empty string if auto-updates unsupported or not enabled
  74. */
  75. static std::string autoUpdateUrl();
  76. /**
  77. * Create a new instance of the service
  78. *
  79. * Once created, you must call the run() method to actually start
  80. * processing.
  81. *
  82. * The port is saved to a file in the home path called zerotier-one.port,
  83. * which is used by the CLI and can be used to see which port was chosen if
  84. * 0 (random port) is picked.
  85. *
  86. * @param hp Home path
  87. * @param port TCP and UDP port for packets and HTTP control (if 0, pick random port)
  88. */
  89. static OneService *newInstance(
  90. const char *hp,
  91. unsigned int port);
  92. virtual ~OneService();
  93. /**
  94. * Execute the service main I/O loop until terminated
  95. *
  96. * The terminate() method may be called from a signal handler or another
  97. * thread to terminate execution. Otherwise this will not return unless
  98. * another condition terminates execution such as a fatal error.
  99. */
  100. virtual ReasonForTermination run() = 0;
  101. /**
  102. * @return Reason for terminating or ONE_STILL_RUNNING if running
  103. */
  104. virtual ReasonForTermination reasonForTermination() const = 0;
  105. /**
  106. * @return Fatal error message or empty string if none
  107. */
  108. virtual std::string fatalErrorMessage() const = 0;
  109. /**
  110. * @return System device name corresponding with a given ZeroTier network ID or empty string if not opened yet or network ID not found
  111. */
  112. virtual std::string portDeviceName(uint64_t nwid) const = 0;
  113. /**
  114. * @return True if TCP fallback is currently active
  115. */
  116. virtual bool tcpFallbackActive() const = 0;
  117. /**
  118. * Terminate background service (can be called from other threads)
  119. */
  120. virtual void terminate() = 0;
  121. /**
  122. * Get local settings for a network
  123. *
  124. * @param nwid Network ID
  125. * @param settings Buffer to fill with local network settings
  126. * @return True if network was found and settings is filled
  127. */
  128. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const = 0;
  129. /**
  130. * Set local settings for a network
  131. *
  132. * @param nwid Network ID
  133. * @param settings New network local settings
  134. * @return True if network was found and setting modified
  135. */
  136. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings) = 0;
  137. /**
  138. * @return True if service is still running
  139. */
  140. inline bool isRunning() const { return (this->reasonForTermination() == ONE_STILL_RUNNING); }
  141. protected:
  142. OneService() {}
  143. private:
  144. OneService(const OneService &one) {}
  145. inline OneService &operator=(const OneService &one) { return *this; }
  146. };
  147. } // namespace ZeroTier
  148. #endif