OneService.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ONESERVICE_HPP
  28. #define ZT_ONESERVICE_HPP
  29. #include <string>
  30. namespace ZeroTier {
  31. /**
  32. * Local service for ZeroTier One as system VPN/NFV provider
  33. *
  34. * If built with ZT_ENABLE_NETWORK_CONTROLLER defined, this includes and
  35. * runs controller/SqliteNetworkController with a database called
  36. * controller.db in the specified home directory.
  37. *
  38. * If built with ZT_AUTO_UPDATE, an official ZeroTier update URL is
  39. * periodically checked and updates are automatically downloaded, verified
  40. * against a built-in list of update signing keys, and installed. This is
  41. * only supported for certain platforms.
  42. *
  43. * If built with ZT_ENABLE_CLUSTER, a 'cluster' file is checked and if
  44. * present is read to determine the identity of other cluster members.
  45. */
  46. class OneService
  47. {
  48. public:
  49. /**
  50. * Returned by node main if/when it terminates
  51. */
  52. enum ReasonForTermination
  53. {
  54. /**
  55. * Instance is still running
  56. */
  57. ONE_STILL_RUNNING = 0,
  58. /**
  59. * Normal shutdown
  60. */
  61. ONE_NORMAL_TERMINATION = 1,
  62. /**
  63. * A serious unrecoverable error has occurred
  64. */
  65. ONE_UNRECOVERABLE_ERROR = 2,
  66. /**
  67. * Your identity has collided with another
  68. */
  69. ONE_IDENTITY_COLLISION = 3
  70. };
  71. /**
  72. * @return Platform default home path or empty string if this platform doesn't have one
  73. */
  74. static std::string platformDefaultHomePath();
  75. /**
  76. * @return Auto-update URL or empty string if auto-updates unsupported or not enabled
  77. */
  78. static std::string autoUpdateUrl();
  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(
  93. const char *hp,
  94. 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. /**
  117. * @return True if TCP fallback is currently active
  118. */
  119. virtual bool tcpFallbackActive() const = 0;
  120. /**
  121. * Terminate background service (can be called from other threads)
  122. */
  123. virtual void terminate() = 0;
  124. /**
  125. * @return True if service is still running
  126. */
  127. inline bool isRunning() const { return (this->reasonForTermination() == ONE_STILL_RUNNING); }
  128. protected:
  129. OneService() {}
  130. private:
  131. OneService(const OneService &one) {}
  132. inline OneService &operator=(const OneService &one) { return *this; }
  133. };
  134. } // namespace ZeroTier
  135. #endif