NodeControlClient.hpp 3.4 KB

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