fs_publish_ksk.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2012, 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet 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 GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fs/fs_publish_ksk.c
  18. * @brief publish a URI under a keyword in GNUnet
  19. * @see https://gnunet.org/encoding and #2564
  20. * @author Krista Bennett
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_constants.h"
  25. #include "gnunet_signatures.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_fs_service.h"
  28. #include "fs_api.h"
  29. #include "fs_tree.h"
  30. #include "fs_publish_ublock.h"
  31. /**
  32. * Context for the KSK publication.
  33. */
  34. struct GNUNET_FS_PublishKskContext
  35. {
  36. /**
  37. * Keywords to use.
  38. */
  39. struct GNUNET_FS_Uri *ksk_uri;
  40. /**
  41. * URI to publish.
  42. */
  43. struct GNUNET_FS_Uri *uri;
  44. /**
  45. * Metadata to use.
  46. */
  47. struct GNUNET_CONTAINER_MetaData *meta;
  48. /**
  49. * Global FS context.
  50. */
  51. struct GNUNET_FS_Handle *h;
  52. /**
  53. * UBlock publishing operation that is active.
  54. */
  55. struct GNUNET_FS_PublishUblockContext *uc;
  56. /**
  57. * Handle to the datastore, NULL if we are just simulating.
  58. */
  59. struct GNUNET_DATASTORE_Handle *dsh;
  60. /**
  61. * Current task.
  62. */
  63. struct GNUNET_SCHEDULER_Task *ksk_task;
  64. /**
  65. * Function to call once we're done.
  66. */
  67. GNUNET_FS_PublishContinuation cont;
  68. /**
  69. * Closure for cont.
  70. */
  71. void *cont_cls;
  72. /**
  73. * When should the KBlocks expire?
  74. */
  75. struct GNUNET_FS_BlockOptions bo;
  76. /**
  77. * Options to use.
  78. */
  79. enum GNUNET_FS_PublishOptions options;
  80. /**
  81. * Keyword that we are currently processing.
  82. */
  83. unsigned int i;
  84. };
  85. /**
  86. * Continuation of #GNUNET_FS_publish_ksk() that performs
  87. * the actual publishing operation (iterating over all
  88. * of the keywords).
  89. *
  90. * @param cls closure of type `struct PublishKskContext *`
  91. */
  92. static void
  93. publish_ksk_cont (void *cls);
  94. /**
  95. * Function called by the datastore API with
  96. * the result from the PUT request.
  97. *
  98. * @param cls closure of type `struct GNUNET_FS_PublishKskContext *`
  99. * @param msg error message (or NULL)
  100. */
  101. static void
  102. kb_put_cont (void *cls,
  103. const char *msg)
  104. {
  105. struct GNUNET_FS_PublishKskContext *pkc = cls;
  106. pkc->uc = NULL;
  107. if (NULL != msg)
  108. {
  109. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  110. "KBlock PUT operation failed: %s\n", msg);
  111. pkc->cont (pkc->cont_cls, NULL, msg);
  112. GNUNET_FS_publish_ksk_cancel (pkc);
  113. return;
  114. }
  115. pkc->ksk_task = GNUNET_SCHEDULER_add_now (&publish_ksk_cont, pkc);
  116. }
  117. /**
  118. * Continuation of #GNUNET_FS_publish_ksk() that performs the actual
  119. * publishing operation (iterating over all of the keywords).
  120. *
  121. * @param cls closure of type `struct GNUNET_FS_PublishKskContext *`
  122. */
  123. static void
  124. publish_ksk_cont (void *cls)
  125. {
  126. struct GNUNET_FS_PublishKskContext *pkc = cls;
  127. const char *keyword;
  128. pkc->ksk_task = NULL;
  129. if ((pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
  130. (NULL == pkc->dsh))
  131. {
  132. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  133. "KSK PUT operation complete\n");
  134. pkc->cont (pkc->cont_cls, pkc->ksk_uri,
  135. NULL);
  136. GNUNET_FS_publish_ksk_cancel (pkc);
  137. return;
  138. }
  139. keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
  140. pkc->uc = GNUNET_FS_publish_ublock_ (pkc->h,
  141. pkc->dsh,
  142. keyword + 1 /* skip '+' */,
  143. NULL,
  144. GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
  145. pkc->meta,
  146. pkc->uri,
  147. &pkc->bo,
  148. pkc->options,
  149. &kb_put_cont, pkc);
  150. }
  151. /**
  152. * Publish a CHK under various keywords on GNUnet.
  153. *
  154. * @param h handle to the file sharing subsystem
  155. * @param ksk_uri keywords to use
  156. * @param meta metadata to use
  157. * @param uri URI to refer to in the KBlock
  158. * @param bo per-block options
  159. * @param options publication options
  160. * @param cont continuation
  161. * @param cont_cls closure for cont
  162. * @return NULL on error ('cont' will still be called)
  163. */
  164. struct GNUNET_FS_PublishKskContext *
  165. GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
  166. const struct GNUNET_FS_Uri *ksk_uri,
  167. const struct GNUNET_CONTAINER_MetaData *meta,
  168. const struct GNUNET_FS_Uri *uri,
  169. const struct GNUNET_FS_BlockOptions *bo,
  170. enum GNUNET_FS_PublishOptions options,
  171. GNUNET_FS_PublishContinuation cont, void *cont_cls)
  172. {
  173. struct GNUNET_FS_PublishKskContext *pkc;
  174. GNUNET_assert (NULL != uri);
  175. pkc = GNUNET_new (struct GNUNET_FS_PublishKskContext);
  176. pkc->h = h;
  177. pkc->bo = *bo;
  178. pkc->options = options;
  179. pkc->cont = cont;
  180. pkc->cont_cls = cont_cls;
  181. pkc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
  182. if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
  183. {
  184. pkc->dsh = GNUNET_DATASTORE_connect (h->cfg);
  185. if (NULL == pkc->dsh)
  186. {
  187. cont (cont_cls,
  188. NULL,
  189. _ ("Could not connect to datastore."));
  190. GNUNET_free (pkc);
  191. return NULL;
  192. }
  193. }
  194. pkc->uri = GNUNET_FS_uri_dup (uri);
  195. pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
  196. pkc->ksk_task = GNUNET_SCHEDULER_add_now (&publish_ksk_cont, pkc);
  197. return pkc;
  198. }
  199. /**
  200. * Abort the KSK publishing operation.
  201. *
  202. * @param pkc context of the operation to abort.
  203. */
  204. void
  205. GNUNET_FS_publish_ksk_cancel (struct GNUNET_FS_PublishKskContext *pkc)
  206. {
  207. if (NULL != pkc->ksk_task)
  208. {
  209. GNUNET_SCHEDULER_cancel (pkc->ksk_task);
  210. pkc->ksk_task = NULL;
  211. }
  212. if (NULL != pkc->uc)
  213. {
  214. GNUNET_FS_publish_ublock_cancel_ (pkc->uc);
  215. pkc->uc = NULL;
  216. }
  217. if (NULL != pkc->dsh)
  218. {
  219. GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
  220. pkc->dsh = NULL;
  221. }
  222. GNUNET_CONTAINER_meta_data_destroy (pkc->meta);
  223. GNUNET_FS_uri_destroy (pkc->ksk_uri);
  224. GNUNET_FS_uri_destroy (pkc->uri);
  225. GNUNET_free (pkc);
  226. }
  227. /* end of fs_publish_ksk.c */