AdminClient.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef AdminClient_H
  16. #define AdminClient_H
  17. #include "benc/String.h"
  18. #include "benc/Dict.h"
  19. #include "memory/Allocator.h"
  20. #include "util/log/Log.h"
  21. #include "util/events/EventBase.h"
  22. #include "util/platform/Sockaddr.h"
  23. #include "util/Linker.h"
  24. Linker_require("admin/AdminClient.c")
  25. enum AdminClient_Error
  26. {
  27. /** Success. */
  28. AdminClient_Error_NONE = 0,
  29. /** Response was longer than max message size. */
  30. AdminClient_Error_OVERLONG_RESPONSE,
  31. /** Error on recv(), causes result.errno to be set. */
  32. AdminClient_Error_ERROR_READING_FROM_SOCKET,
  33. /** No data on socket. */
  34. AdminClient_Error_SOCKET_NOT_READY,
  35. /** Failed to deserialize response. */
  36. AdminClient_Error_DESERIALIZATION_FAILED,
  37. /** Failed to serialize request. */
  38. AdminClient_Error_SERIALIZATION_FAILED,
  39. /** Timeout waiting for response. */
  40. AdminClient_Error_TIMEOUT,
  41. /** Requested cookie and response did not contain cookie. */
  42. AdminClient_Error_NO_COOKIE,
  43. };
  44. /** The biggest message that can be sent or received. */
  45. #define AdminClient_MAX_MESSAGE_SIZE 1023
  46. /** The amount of message padding. */
  47. #define AdminClient_Result_PADDING_SIZE (sizeof(struct Sockaddr_storage))
  48. struct AdminClient_Result
  49. {
  50. /** The error type of AdminClient_Error_NONE if there was no error. */
  51. enum AdminClient_Error err;
  52. /** Space to put the address of the node which the response is being sent to. */
  53. uint8_t padding[AdminClient_Result_PADDING_SIZE];
  54. /**
  55. * When the request is made, this will hold the request bytes,
  56. * after it will hold the response bytes. If there is an error
  57. * during the sending of the request, it will still have the request bytes.
  58. * 1 byte extra to alow for a null terminator.
  59. */
  60. uint8_t messageBytes[AdminClient_MAX_MESSAGE_SIZE + 1];
  61. /** The deserialized response. */
  62. Dict* responseDict;
  63. };
  64. struct AdminClient_Promise;
  65. typedef void (* AdminClient_ResultHandler)(struct AdminClient_Promise* p,
  66. struct AdminClient_Result* res);
  67. struct AdminClient_Promise
  68. {
  69. AdminClient_ResultHandler callback;
  70. void* userData;
  71. struct Allocator* alloc;
  72. };
  73. struct AdminClient
  74. {
  75. /** How long to wait for a response from the cjdns node. */
  76. uint32_t millisecondsToWait;
  77. };
  78. char* AdminClient_errorString(enum AdminClient_Error err);
  79. struct AdminClient_Promise* AdminClient_rpcCall(String* function,
  80. Dict* args,
  81. struct AdminClient* client,
  82. struct Allocator* requestAlloc);
  83. struct AdminClient* AdminClient_new(struct Sockaddr* addr,
  84. String* adminPassword,
  85. struct EventBase* eventBase,
  86. struct Log* logger,
  87. struct Allocator* alloc);
  88. #endif