NodeControlClient.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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_NODECONTROLCLIENT_HPP
  28. #define ZT_NODECONTROLCLIENT_HPP
  29. #include <string>
  30. #include <vector>
  31. #include "../node/Constants.hpp"
  32. #ifdef __WINDOWS__
  33. #define ZT_IPC_ENDPOINT_BASE "\\\\.\\pipe\\ZeroTierOne-"
  34. #else
  35. #define ZT_IPC_ENDPOINT_BASE "/tmp/.ZeroTierOne-"
  36. #endif
  37. namespace ZeroTier {
  38. /**
  39. * Client for controlling a local ZeroTier One node
  40. */
  41. class NodeControlClient
  42. {
  43. public:
  44. /**
  45. * Create a new node config client
  46. *
  47. * Initialization may fail. Call error() to check.
  48. *
  49. * @param ep Endpoint to connect to (OS-dependent)
  50. * @param resultHandler Function to call when commands provide results
  51. * @param arg First argument to result handler
  52. */
  53. NodeControlClient(const char *ep,const char *authToken,void (*resultHandler)(void *,const char *),void *arg)
  54. throw();
  55. ~NodeControlClient();
  56. /**
  57. * @return Initialization error or NULL if none
  58. */
  59. const char *error() const
  60. throw();
  61. /**
  62. * Send a command to the local node
  63. *
  64. * Note that the returned conversation ID will never be 0. A return value
  65. * of 0 indicates a fatal error such as failure to bind to any local UDP
  66. * port.
  67. *
  68. * @param command
  69. * @return Conversation ID that will be provided to result handler when/if results are sent back
  70. */
  71. void send(const char *command)
  72. throw();
  73. inline void send(const std::string &command)
  74. throw() { return send(command.c_str()); }
  75. /**
  76. * Split a line of results
  77. *
  78. * @param line Line to split
  79. * @return Vector of fields
  80. */
  81. static std::vector<std::string> splitLine(const char *line);
  82. static inline std::vector<std::string> splitLine(const std::string &line) { return splitLine(line.c_str()); }
  83. /**
  84. * @return Default path for current user's authtoken.secret or ~/.zeroTierOneAuthToken (location is platform-dependent)
  85. */
  86. static const char *authTokenDefaultUserPath();
  87. /**
  88. * Load (or generate) the authentication token
  89. *
  90. * @param path Full path to authtoken.secret
  91. * @param generateIfNotFound If true, generate and save if not found or readable (requires appropriate privileges, returns empty on failure)
  92. * @return Authentication token or empty string on failure
  93. */
  94. static std::string getAuthToken(const char *path,bool generateIfNotFound);
  95. private:
  96. // NodeControlClient is not copyable
  97. NodeControlClient(const NodeControlClient&);
  98. const NodeControlClient& operator=(const NodeControlClient&);
  99. void *_impl;
  100. };
  101. } // namespace ZeroTier
  102. #endif