gnunet_seti_service.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013, 2014, 2020 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. * @author Florian Dold
  18. * @author Christian Grothoff
  19. *
  20. * @file
  21. * Two-peer set intersection operations
  22. *
  23. * @defgroup set Set intersection service
  24. * Two-peer set operations
  25. *
  26. * @{
  27. */
  28. #ifndef GNUNET_SETI_SERVICE_H
  29. #define GNUNET_SETI_SERVICE_H
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #if 0 /* keep Emacsens' auto-indent happy */
  34. }
  35. #endif
  36. #endif
  37. #include "gnunet_common.h"
  38. #include "gnunet_time_lib.h"
  39. #include "gnunet_configuration_lib.h"
  40. /**
  41. * Maximum size of a context message for set operation requests.
  42. */
  43. #define GNUNET_SETI_CONTEXT_MESSAGE_MAX_SIZE ((1 << 16) - 1024)
  44. /**
  45. * Opaque handle to a set.
  46. */
  47. struct GNUNET_SETI_Handle;
  48. /**
  49. * Opaque handle to a set operation request from another peer.
  50. */
  51. struct GNUNET_SETI_Request;
  52. /**
  53. * Opaque handle to a listen operation.
  54. */
  55. struct GNUNET_SETI_ListenHandle;
  56. /**
  57. * Opaque handle to a set operation.
  58. */
  59. struct GNUNET_SETI_OperationHandle;
  60. /**
  61. * Status for the result callback
  62. */
  63. enum GNUNET_SETI_Status
  64. {
  65. /**
  66. * Element should be added to the result set of the local peer, i.e. the
  67. * element is in the intersection.
  68. */
  69. GNUNET_SETI_STATUS_ADD_LOCAL,
  70. /**
  71. * Element should be delete from the result set of the local peer, i.e. the
  72. * local peer is having an element that is not in the intersection.
  73. */
  74. GNUNET_SETI_STATUS_DEL_LOCAL,
  75. /**
  76. * The other peer refused to do the operation with us, or something went
  77. * wrong.
  78. */
  79. GNUNET_SETI_STATUS_FAILURE,
  80. /**
  81. * Success, all elements have been sent (and received).
  82. */
  83. GNUNET_SETI_STATUS_DONE
  84. };
  85. /**
  86. * Element stored in a set.
  87. */
  88. struct GNUNET_SETI_Element
  89. {
  90. /**
  91. * Number of bytes in the buffer pointed to by data.
  92. */
  93. uint16_t size;
  94. /**
  95. * Application-specific element type.
  96. */
  97. uint16_t element_type;
  98. /**
  99. * Actual data of the element
  100. */
  101. const void *data;
  102. };
  103. /**
  104. * Possible options to pass to a set operation.
  105. *
  106. * Used as tag for struct #GNUNET_SETI_Option.
  107. */
  108. enum GNUNET_SETI_OptionType
  109. {
  110. /**
  111. * List terminator.
  112. */
  113. GNUNET_SETI_OPTION_END = 0,
  114. /**
  115. * Return the elements remaining in the intersection
  116. * (#GNUNET_SETI_STATUS_ADD_LOCAL). If not given, the default is to return a
  117. * list of the elements to be removed (#GNUNET_SETI_STATUS_DEL_LOCAL).
  118. */
  119. GNUNET_SETI_OPTION_RETURN_INTERSECTION = 1,
  120. };
  121. /**
  122. * Option for set operations.
  123. */
  124. struct GNUNET_SETI_Option
  125. {
  126. /**
  127. * Type of the option.
  128. */
  129. enum GNUNET_SETI_OptionType type;
  130. /**
  131. * Value for the option, only used with some options.
  132. */
  133. union
  134. {
  135. uint64_t num;
  136. } v;
  137. };
  138. /**
  139. * Callback for set union operation results. Called for each element
  140. * in the result set.
  141. *
  142. * @param cls closure
  143. * @param element a result element, only valid if status is #GNUNET_SETI_STATUS_OK
  144. * @param current_size current set size
  145. * @param status see `enum GNUNET_SETI_Status`
  146. */
  147. typedef void
  148. (*GNUNET_SETI_ResultIterator) (void *cls,
  149. const struct GNUNET_SETI_Element *element,
  150. uint64_t current_size,
  151. enum GNUNET_SETI_Status status);
  152. /**
  153. * Called when another peer wants to do a set operation with the
  154. * local peer. If a listen error occurs, the @a request is NULL.
  155. *
  156. * @param cls closure
  157. * @param other_peer the other peer
  158. * @param context_msg message with application specific information from
  159. * the other peer
  160. * @param request request from the other peer (never NULL), use GNUNET_SETI_accept()
  161. * to accept it, otherwise the request will be refused
  162. * Note that we can't just return value from the listen callback,
  163. * as it is also necessary to specify the set we want to do the
  164. * operation with, with sometimes can be derived from the context
  165. * message. It's necessary to specify the timeout.
  166. */
  167. typedef void
  168. (*GNUNET_SETI_ListenCallback) (void *cls,
  169. const struct GNUNET_PeerIdentity *other_peer,
  170. const struct GNUNET_MessageHeader *context_msg,
  171. struct GNUNET_SETI_Request *request);
  172. /**
  173. * Create an empty set, supporting the specified operation.
  174. *
  175. * @param cfg configuration to use for connecting to the
  176. * set service
  177. * @return a handle to the set
  178. */
  179. struct GNUNET_SETI_Handle *
  180. GNUNET_SETI_create (const struct GNUNET_CONFIGURATION_Handle *cfg);
  181. /**
  182. * Add an element to the given set.
  183. *
  184. * @param set set to add element to
  185. * @param element element to add to the set
  186. * @param cb function to call when finished, can be NULL
  187. * @param cb_cls closure for @a cb
  188. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
  189. * set is invalid (e.g. the set service crashed)
  190. */
  191. int
  192. GNUNET_SETI_add_element (struct GNUNET_SETI_Handle *set,
  193. const struct GNUNET_SETI_Element *element,
  194. GNUNET_SCHEDULER_TaskCallback cb,
  195. void *cb_cls);
  196. /**
  197. * Destroy the set handle, and free all associated resources. Operations may
  198. * still be pending when a set is destroyed (and will be allowed to complete).
  199. *
  200. * @param set set to destroy
  201. */
  202. void
  203. GNUNET_SETI_destroy (struct GNUNET_SETI_Handle *set);
  204. /**
  205. * Prepare a set operation to be evaluated with another peer. The evaluation
  206. * will not start until the client provides a local set with
  207. * GNUNET_SETI_commit().
  208. *
  209. * @param other_peer peer with the other set
  210. * @param app_id hash for the application using the set
  211. * @param context_msg additional information for the request
  212. * @param options options to use when processing the request
  213. * @param result_cb called on error or success
  214. * @param result_cls closure for @a result_cb
  215. * @return a handle to cancel the operation
  216. */
  217. struct GNUNET_SETI_OperationHandle *
  218. GNUNET_SETI_prepare (const struct GNUNET_PeerIdentity *other_peer,
  219. const struct GNUNET_HashCode *app_id,
  220. const struct GNUNET_MessageHeader *context_msg,
  221. const struct GNUNET_SETI_Option options[],
  222. GNUNET_SETI_ResultIterator result_cb,
  223. void *result_cls);
  224. /**
  225. * Wait for set operation requests for the given application ID.
  226. * If the connection to the set service is lost, the listener is
  227. * re-created transparently with exponential backoff.
  228. *
  229. * @param cfg configuration to use for connecting to
  230. * the set service
  231. * @param app_id id of the application that handles set operation requests
  232. * @param listen_cb called for each incoming request matching the operation
  233. * and application id
  234. * @param listen_cls handle for @a listen_cb
  235. * @return a handle that can be used to cancel the listen operation
  236. */
  237. struct GNUNET_SETI_ListenHandle *
  238. GNUNET_SETI_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
  239. const struct GNUNET_HashCode *app_id,
  240. GNUNET_SETI_ListenCallback listen_cb,
  241. void *listen_cls);
  242. /**
  243. * Cancel the given listen operation. After calling cancel, the
  244. * listen callback for this listen handle will not be called again.
  245. * Note that cancelling a listen operation will automatically reject
  246. * all operations that have not yet been accepted.
  247. *
  248. * @param lh handle for the listen operation
  249. */
  250. void
  251. GNUNET_SETI_listen_cancel (struct GNUNET_SETI_ListenHandle *lh);
  252. /**
  253. * Accept a request we got via GNUNET_SETI_listen(). Must be called during
  254. * GNUNET_SETI_listen(), as the `struct GNUNET_SETI_Request` becomes invalid
  255. * afterwards.
  256. * Call GNUNET_SETI_commit() to provide the local set to use for the operation,
  257. * and to begin the exchange with the remote peer.
  258. *
  259. * @param request request to accept
  260. * @param options options to use when processing the request
  261. * @param result_cb callback for the results
  262. * @param result_cls closure for @a result_cb
  263. * @return a handle to cancel the operation
  264. */
  265. struct GNUNET_SETI_OperationHandle *
  266. GNUNET_SETI_accept (struct GNUNET_SETI_Request *request,
  267. const struct GNUNET_SETI_Option options[],
  268. GNUNET_SETI_ResultIterator result_cb,
  269. void *result_cls);
  270. /**
  271. * Commit a set to be used with a set operation.
  272. * This function is called once we have fully constructed
  273. * the set that we want to use for the operation. At this
  274. * time, the P2P protocol can then begin to exchange the
  275. * set information and call the result callback with the
  276. * result information.
  277. *
  278. * @param oh handle to the set operation
  279. * @param set the set to use for the operation
  280. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
  281. * set is invalid (e.g. the set service crashed)
  282. */
  283. int
  284. GNUNET_SETI_commit (struct GNUNET_SETI_OperationHandle *oh,
  285. struct GNUNET_SETI_Handle *set);
  286. /**
  287. * Cancel the given set operation. May not be called after the operation's
  288. * `GNUNET_SETI_ResultIterator` has been called with a status of
  289. * #GNUNET_SETI_STATUS_FAILURE or #GNUNET_SETI_STATUS_DONE.
  290. *
  291. * @param oh set operation to cancel
  292. */
  293. void
  294. GNUNET_SETI_operation_cancel (struct GNUNET_SETI_OperationHandle *oh);
  295. /**
  296. * Hash a set element.
  297. *
  298. * @param element the element that should be hashed
  299. * @param[out] ret_hash a pointer to where the hash of @a element
  300. * should be stored
  301. */
  302. void
  303. GNUNET_SETI_element_hash (const struct GNUNET_SETI_Element *element,
  304. struct GNUNET_HashCode *ret_hash);
  305. #if 0 /* keep Emacsens' auto-indent happy */
  306. {
  307. #endif
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #endif
  312. /** @} */ /* end of group */