command-socket.dox 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. /**
  7. @page ctrlSocket Control Channel
  8. @section ctrlSocketOverview Control Channel Overview
  9. In many cases it is useful to manage certain aspects of the DHCP servers
  10. while they are running. In Kea, this may be done via the Control Channel.
  11. Control Channel allows an external entity (e.g. a tool run by a sysadmin
  12. or a script) to issue commands to the server which can influence its
  13. behavior or retreive information from it. Several notable examples
  14. envisioned are: reconfiguration, statistics retrieval and manipulation,
  15. and shutdown.
  16. Communication over Control Channel is conducted using JSON structures.
  17. As of Kea 0.9.2, the only supported communication channel is UNIX stream
  18. socket, but additional types may be added in the future.
  19. If configured, Kea will open a socket and will listen for any incoming
  20. connections. A process connecting to this socket is expected to send JSON
  21. commands structured as follows:
  22. @code
  23. {
  24. "command": "foo",
  25. "arguments": {
  26. "param_foo": "value1",
  27. "param_bar": "value2",
  28. ...
  29. }
  30. }
  31. @endcode
  32. - command - is the name of command to execute and is mandatory.
  33. - arguments - contain a single parameter or a map or parameters
  34. required to carry out the given command. The exact content and format is command specific.
  35. The server will process the incoming command and then send a response of the form:
  36. @code
  37. {
  38. "result": 0|1,
  39. "text": "textual description",
  40. "arguments": {
  41. "argument1": "value1",
  42. "argument2": "value2",
  43. ...
  44. }
  45. }
  46. @endcode
  47. - result - indicates the outcome of the command. A value of 0 means a success while
  48. any non-zero value designates an error. Currently 1 is used as a generic error, but additional
  49. error codes may be added in the future.
  50. - text field - typically appears when result is non-zero and contains description of the error
  51. encountered.
  52. - arguments - is a map of additional data values returned by the server, specific to the
  53. command issue. The map is always present, even if it contains no data values.
  54. @section ctrlSocketClient Using Control Channel
  55. Here are two examples of how to access the Control Channel:
  56. 1. Use socat tool, which is available in many Linux and BSD distributions.
  57. See http://www.dest-unreach.org/socat/ for details. To use it:
  58. @code
  59. socat UNIX:/var/run/kea/kea4.sock -
  60. @endcode
  61. You then can type JSON commands and get responses (also in JSON format).
  62. 2. Here's an example C code that connects and gets a list of supported commands:
  63. @code
  64. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  65. //
  66. // This Source Code Form is subject to the terms of the Mozilla Public
  67. // License, v. 2.0. If a copy of the MPL was not distributed with this
  68. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  69. #include <sys/socket.h>
  70. #include <sys/un.h>
  71. #include <stdio.h>
  72. #include <string.h>
  73. #include <unistd.h>
  74. int main(int argc, const char* argv[]) {
  75. if (argc != 2) {
  76. printf("Usage: %s socket_path\n", argv[0]);
  77. return (1);
  78. }
  79. // Create UNIX stream socket.
  80. int socket_fd;
  81. if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  82. {
  83. perror("Failed to create UNIX stream");
  84. return (1);
  85. }
  86. // Specify the address to connect to (unix path)
  87. struct sockaddr_un srv_addr;
  88. memset(&srv_addr, 0, sizeof(struct sockaddr_un));
  89. srv_addr.sun_family = AF_UNIX;
  90. strcpy(srv_addr.sun_path, argv[1]);
  91. socklen_t len = sizeof(srv_addr);
  92. // Try to connect.
  93. if (connect(socket_fd, (struct sockaddr*) &srv_addr, len) == -1) {
  94. perror("Failed to connect");
  95. return (1);
  96. }
  97. // Send a command to list all available commands.
  98. char buf[1024];
  99. sprintf(buf, "{ \"command\": \"list-commands\" }");
  100. int bytes_sent = send(socket_fd, buf, strlen(buf), 0);
  101. printf("%d bytes sent\n", bytes_sent);
  102. // Receive a response (should be JSON formatted list of commands)
  103. int bytes_rcvd = recv(socket_fd, buf, sizeof(buf), 0);
  104. printf("%d bytes received: [%s]\n", bytes_rcvd, buf);
  105. // Close the socket
  106. close(socket_fd);
  107. return 0;
  108. }
  109. @endcode
  110. @section ctrlSocketImpl Control Channel Implementation
  111. Control Channel is implemented in @ref isc::config::CommandMgr. It is a signleton
  112. class that allows registration of callbacks that handle specific commands.
  113. It internally supports a single command: @c list-commands that returns a list
  114. of supported commands. This component is expected to be shared among all daemons.
  115. There are 3 main methods that are expected to be used by developers:
  116. - @ref isc::config::CommandMgr::registerCommand, which allows registration of
  117. additional commands.
  118. - @ref isc::config::CommandMgr::deregisterCommand, which allows removing previously
  119. registered command.
  120. - @ref isc::config::CommandMgr::processCommand, which allows handling specified
  121. command.
  122. There are also two methods for managing control sockets. They are not expected
  123. to be used directly, unless someone implements a new Control Channel (e.g. TCP
  124. or HTTPS connection):
  125. - @ref isc::config::CommandMgr::openCommandSocket that passes structure defined
  126. in the configuration file. Currently only two parameters are supported: socket-type
  127. (which must contain value 'unix') and socket-name (which contains unix path for
  128. the named socket to be created).
  129. - @ref isc::config::CommandMgr::closeCommandSocket() - it is used to close the
  130. socket.
  131. @section ctrlSocketConnections Accepting connections
  132. The @ref isc::config::CommandMgr is implemented using boost ASIO and uses
  133. asynchronous calls to accept new connections and receive commands from the
  134. controlling clients. ASIO uses IO service object to run asynchronous calls.
  135. Thus, before the server can use the @ref isc::config::CommandMgr it must
  136. provide it with a common instance of the @ref isc::asiolink::IOService
  137. object using @ref isc::config::CommandMgr::setIOService. The server's
  138. main loop must contain calls to @ref isc::asiolink::IOService::run or
  139. @ref isc::asiolink::IOService::poll or their variants to invoke Command
  140. Manager's handlers as required for processing control requests.
  141. */