OneService.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. class NetworkController;
  32. /**
  33. * Local service for ZeroTier One as system VPN/NFV provider
  34. */
  35. class OneService
  36. {
  37. public:
  38. /**
  39. * Returned by node main if/when it terminates
  40. */
  41. enum ReasonForTermination
  42. {
  43. /**
  44. * Instance is still running
  45. */
  46. ONE_STILL_RUNNING = 0,
  47. /**
  48. * Normal shutdown
  49. */
  50. ONE_NORMAL_TERMINATION = 1,
  51. /**
  52. * A serious unrecoverable error has occurred
  53. */
  54. ONE_UNRECOVERABLE_ERROR = 2,
  55. /**
  56. * Your identity has collided with another
  57. */
  58. ONE_IDENTITY_COLLISION = 3
  59. };
  60. /**
  61. * @return Platform default home path or empty string if this platform doesn't have one
  62. */
  63. static std::string platformDefaultHomePath();
  64. /**
  65. * Create a new instance of the service
  66. *
  67. * Once created, you must call the run() method to actually start
  68. * processing.
  69. *
  70. * @param hp Home path
  71. * @param port TCP and UDP port for packets and HTTP control
  72. * @param master Instance of network config master if this instance is to act as one (default: NULL)
  73. * @param overrideRootTopology String-serialized root topology (for testing, default: NULL)
  74. */
  75. static OneService *newInstance(
  76. const char *hp,
  77. unsigned int port,
  78. NetworkController *master = (NetworkController *)0,
  79. const char *overrideRootTopology = (const char *)0);
  80. virtual ~OneService();
  81. /**
  82. * Execute the service main I/O loop until terminated
  83. *
  84. * The terminate() method may be called from a signal handler or another
  85. * thread to terminate execution. Otherwise this will not return unless
  86. * another condition terminates execution such as a fatal error.
  87. *
  88. * @param
  89. */
  90. virtual ReasonForTermination run() = 0;
  91. /**
  92. * @return Reason for terminating or ONE_STILL_RUNNING if running
  93. */
  94. virtual ReasonForTermination reasonForTermination() const = 0;
  95. /**
  96. * @return Fatal error message or empty string if none
  97. */
  98. virtual std::string fatalErrorMessage() const = 0;
  99. /**
  100. * @return System device name corresponding with a given ZeroTier network ID or empty string if not opened yet or network ID not found
  101. */
  102. virtual std::string portDeviceName(uint64_t nwid) const = 0;
  103. /**
  104. * Terminate background service (can be called from other threads)
  105. */
  106. virtual void terminate() = 0;
  107. /**
  108. * @return True if service is still running
  109. */
  110. inline bool isRunning() const { return (this->reasonForTermination() == ONE_STILL_RUNNING); }
  111. protected:
  112. OneService() {}
  113. private:
  114. OneService(const OneService &one) {}
  115. inline OneService &operator=(const OneService &one) { return *this; }
  116. };
  117. } // namespace ZeroTier
  118. #endif