AdminClient.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef AdminClient_H
  16. #define AdminClient_H
  17. #include "interface/addressable/AddrIface.h"
  18. #include "benc/String.h"
  19. #include "benc/Dict.h"
  20. #include "memory/Allocator.h"
  21. #include "util/log/Log.h"
  22. #include "util/events/EventBase.h"
  23. #include "util/platform/Sockaddr.h"
  24. #include "util/Linker.h"
  25. Linker_require("client/AdminClient.c")
  26. enum AdminClient_Error
  27. {
  28. /** Success. */
  29. AdminClient_Error_NONE = 0,
  30. /** Response was longer than max message size. */
  31. AdminClient_Error_OVERLONG_RESPONSE,
  32. /** Error on recv(), causes result.errno to be set. */
  33. AdminClient_Error_ERROR_READING_FROM_SOCKET,
  34. /** No data on socket. */
  35. AdminClient_Error_SOCKET_NOT_READY,
  36. /** Failed to deserialize response. */
  37. AdminClient_Error_DESERIALIZATION_FAILED,
  38. /** Failed to serialize request. */
  39. AdminClient_Error_SERIALIZATION_FAILED,
  40. /** Timeout waiting for response. */
  41. AdminClient_Error_TIMEOUT,
  42. /** Requested cookie and response did not contain cookie. */
  43. AdminClient_Error_NO_COOKIE,
  44. };
  45. /** The biggest message that can be sent or received. */
  46. #define AdminClient_MAX_MESSAGE_SIZE 1023
  47. /** The amount of message padding. */
  48. #define AdminClient_Result_PADDING_SIZE (sizeof(struct Sockaddr_storage))
  49. struct AdminClient_Result
  50. {
  51. /** The error type of AdminClient_Error_NONE if there was no error. */
  52. enum AdminClient_Error err;
  53. /** Space to put the address of the node which the response is being sent to. */
  54. uint8_t padding[AdminClient_Result_PADDING_SIZE];
  55. /**
  56. * When the request is made, this will hold the request bytes,
  57. * after it will hold the response bytes. If there is an error
  58. * during the sending of the request, it will still have the request bytes.
  59. * 1 byte extra to alow for a null terminator.
  60. */
  61. uint8_t messageBytes[AdminClient_MAX_MESSAGE_SIZE + 1];
  62. /** The deserialized response. */
  63. Dict* responseDict;
  64. };
  65. struct AdminClient_Promise;
  66. typedef void (* AdminClient_ResultHandler)(struct AdminClient_Promise* p,
  67. struct AdminClient_Result* res);
  68. struct AdminClient_Promise
  69. {
  70. AdminClient_ResultHandler callback;
  71. void* userData;
  72. struct Allocator* alloc;
  73. };
  74. struct AdminClient
  75. {
  76. /** How long to wait for a response from the cjdns node. */
  77. uint32_t millisecondsToWait;
  78. };
  79. char* AdminClient_errorString(enum AdminClient_Error err);
  80. struct AdminClient_Promise* AdminClient_rpcCall(String* function,
  81. Dict* args,
  82. struct AdminClient* client,
  83. struct Allocator* requestAlloc);
  84. struct AdminClient* AdminClient_new(AddrIface_t* ai,
  85. struct Sockaddr* connectToAddress,
  86. String* adminPassword,
  87. struct EventBase* eventBase,
  88. struct Log* logger,
  89. struct Allocator* alloc);
  90. #endif