rehash-api.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
  3. This file is part of GNU Guix.
  4. GNU Guix is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Guix is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  14. /**
  15. * @brief Client-side API to the rehash service
  16. * @author Maxime Devos
  17. */
  18. #include <stdio.h>
  19. #include <gnunet/gnunet_config.h>
  20. #include <gnunet/gnunet_service_lib.h>
  21. #include <gnunet/gnunet_dht_service.h>
  22. #include <gnunet/gnunet_mq_lib.h>
  23. #include "rehash_service.h"
  24. #include "extra_gnunet_protocols.h"
  25. #include "rehash.h"
  26. struct GNUNET_REHASH_Handle
  27. {
  28. const struct GNUNET_CONFIGURATION_Handle *cfg;
  29. struct GNUNET_MQ_Handle *mq;
  30. };
  31. static int
  32. check_client_result(void *cls, const struct GNUNET_REHASH_ResultMessage *msg)
  33. {
  34. if (msg->output_length == msg->header.size - sizeof(*msg))
  35. return GNUNET_OK;
  36. else
  37. return GNUNET_SYSERR;
  38. };
  39. static void
  40. handle_client_result(void *cls, const struct GNUNET_REHASH_ResultMessage *msg)
  41. {
  42. /* TODO call callbacks */
  43. GNUNET_assert (0);
  44. };
  45. static void
  46. mq_error_handler (void *h, enum GNUNET_MQ_Error e)
  47. {
  48. /* FIXME! */
  49. GNUNET_assert (0);
  50. }
  51. struct GNUNET_REHASH_Handle *
  52. GNUNET_REHASH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  53. {
  54. struct GNUNET_REHASH_Handle *handle;
  55. handle = GNUNET_new (struct GNUNET_REHASH_Handle);
  56. struct GNUNET_MQ_MessageHandler handlers[] = {
  57. GNUNET_MQ_hd_var_size (client_result,
  58. GNUNET_MESSAGE_TYPE_REHASH_CLIENT_RESULT,
  59. struct GNUNET_REHASH_ResultMessage,
  60. handle),
  61. GNUNET_MQ_handler_end ()
  62. };
  63. handle->cfg = cfg;
  64. handle->mq = GNUNET_CLIENT_connect (cfg,
  65. "rehash",
  66. handlers,
  67. &mq_error_handler,
  68. handle);
  69. if (NULL == handle->mq)
  70. {
  71. GNUNET_free (handle);
  72. return NULL;
  73. }
  74. return handle;
  75. }
  76. void
  77. GNUNET_REHASH_disconnect (struct GNUNET_REHASH_Handle *h)
  78. {
  79. GNUNET_MQ_destroy (h->mq);
  80. GNUNET_free (h);
  81. }
  82. void
  83. GNUNET_REHASH_store_abort (struct GNUNET_REHASH_StoreContext *cls)
  84. {
  85. /* TODO stub */
  86. }
  87. struct GNUNET_REHASH_QueryContext *
  88. GNUNET_REHASH_query_start (struct GNUNET_REHASH_Handle *h,
  89. enum GNUNET_FS_SearchOptions options,
  90. uint32_t anonymity,
  91. enum GNUNET_REHASH_Hash_Type in_type,
  92. enum GNUNET_REHASH_Hash_Type out_type,
  93. const char *input,
  94. size_t input_length,
  95. GNUNET_REHASH_QueryContinuation cont,
  96. void *cls)
  97. {
  98. /* TODO stub */
  99. struct GNUNET_REHASH_GetMessage *msg;
  100. struct GNUNET_MQ_Envelope *ev;
  101. char *msg_input;
  102. /* TODO proper error messages, less magic */
  103. /* Prevent buffer overflows! */
  104. GNUNET_assert (input_length <= 64);
  105. ev = GNUNET_MQ_msg_extra (msg, input_length, GNUNET_MESSAGE_TYPE_REHASH_CLIENT_GET);
  106. msg->options = htonl (options);
  107. msg->anonymity_level = htonl (anonymity);
  108. msg->in_type = htonl (out_type);
  109. msg->out_type = htonl (out_type);
  110. msg->input_length = htonl ((uint32_t) input_length);
  111. msg_input = (char *) &msg[1];
  112. memcpy (msg_input, input, input_length);
  113. GNUNET_MQ_send (h->mq, ev);
  114. /* TODO allow abort */
  115. return NULL;
  116. }
  117. struct GNUNET_REHASH_StoreContext *
  118. GNUNET_REHASH_store_start (struct GNUNET_REHASH_Handle *h,
  119. const struct GNUNET_FS_BlockOptions *options,
  120. enum GNUNET_REHASH_Hash_Type in_type,
  121. enum GNUNET_REHASH_Hash_Type out_type,
  122. const char *input,
  123. size_t input_length,
  124. const char *output,
  125. size_t output_length,
  126. GNUNET_REHASH_StoreContinuation cont,
  127. void *cls)
  128. {
  129. struct GNUNET_REHASH_PutMessage *msg;
  130. struct GNUNET_MQ_Envelope *ev;
  131. /* TODO proper error messages, less magic */
  132. /* Prevent buffer overflows! */
  133. GNUNET_assert (input_length <= 64);
  134. GNUNET_assert (output_length <= 64);
  135. ev = GNUNET_MQ_msg_extra (msg, input_length + output_length, GNUNET_MESSAGE_TYPE_REHASH_CLIENT_PUT);
  136. msg->expiration_time = GNUNET_TIME_absolute_hton(options->expiration_time);
  137. msg->anonymity_level = htonl (options->anonymity_level);
  138. msg->content_priority = htonl (options->content_priority);
  139. msg->replication_level = htonl (options->replication_level);
  140. msg->in_type = htonl (in_type);
  141. msg->in_type = htonl (out_type);
  142. msg->input_length = htonl ((uint32_t) input_length);
  143. msg->output_length = htonl ((uint32_t) output_length);
  144. GNUNET_MQ_send (h->mq, ev);
  145. /* TODO: check memory allocation / freeing */
  146. /* TODO: progress / abort / ... */
  147. return NULL;
  148. }