2 This file is part of GNUnet
3 Copyright (C) 2013, 2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @author Florian Dold
18 * @author Christian Grothoff
21 * Two-peer set operations
23 * @defgroup set Set service
24 * Two-peer set operations
26 * @see [Documentation](https://gnunet.org/set-subsystem)
31 #ifndef GNUNET_SET_SERVICE_H
32 #define GNUNET_SET_SERVICE_H
37 #if 0 /* keep Emacsens' auto-indent happy */
42 #include "gnunet_common.h"
43 #include "gnunet_time_lib.h"
44 #include "gnunet_configuration_lib.h"
48 * Maximum size of a context message for set operation requests.
50 #define GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE ((1<<16) - 1024)
54 * Opaque handle to a set.
56 struct GNUNET_SET_Handle;
59 * Opaque handle to a set operation request from another peer.
61 struct GNUNET_SET_Request;
64 * Opaque handle to a listen operation.
66 struct GNUNET_SET_ListenHandle;
69 * Opaque handle to a set operation.
71 struct GNUNET_SET_OperationHandle;
75 * The operation that a set set supports.
77 enum GNUNET_SET_OperationType
80 * A purely local set that does not support any operation.
82 GNUNET_SET_OPERATION_NONE,
85 * Set intersection, only return elements that are in both sets.
87 GNUNET_SET_OPERATION_INTERSECTION,
90 * Set union, return all elements that are in at least one of the sets.
92 GNUNET_SET_OPERATION_UNION
97 * Status for the result callback
99 enum GNUNET_SET_Status
102 * Everything went ok, we are transmitting an element of the
103 * result (in set, or to be removed from set, depending on
104 * the `enum GNUNET_SET_ResultMode`).
107 * #GNUNET_SET_RESULT_FULL,
108 * #GNUNET_SET_RESULT_ADDED,
109 * #GNUNET_SET_RESULT_REMOVED,
111 GNUNET_SET_STATUS_OK,
114 * Element should be added to the result set
115 * of the local peer, i.e. the local peer is
116 * missing an element.
118 * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
120 GNUNET_SET_STATUS_ADD_LOCAL,
123 * Element should be added to the result set
124 * of the remote peer, i.e. the remote peer is
125 * missing an element.
127 * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
129 GNUNET_SET_STATUS_ADD_REMOTE,
132 * The other peer refused to to the operation with us,
133 * or something went wrong.
135 GNUNET_SET_STATUS_FAILURE,
138 * Success, all elements have been returned (but the other peer
139 * might still be receiving some from us, so we are not done). Only
140 * used during UNION operation.
142 GNUNET_SET_STATUS_HALF_DONE,
145 * Success, all elements have been sent (and received).
147 GNUNET_SET_STATUS_DONE
153 * The way results are given to the client.
155 enum GNUNET_SET_ResultMode
158 * Client gets every element in the resulting set.
160 * Only supported for set intersection.
162 GNUNET_SET_RESULT_FULL,
165 * Client gets notified of the required changes
166 * for both the local and the remote set.
168 * Only supported for set
170 GNUNET_SET_RESULT_SYMMETRIC,
173 * Client gets only elements that have been removed from the set.
175 * Only supported for set intersection.
177 GNUNET_SET_RESULT_REMOVED,
180 * Client gets only elements that have been added to the set.
182 * Only supported for set union.
184 GNUNET_SET_RESULT_ADDED
189 * Element stored in a set.
191 struct GNUNET_SET_Element
194 * Number of bytes in the buffer pointed to by data.
199 * Application-specific element type.
201 uint16_t element_type;
204 * Actual data of the element
211 * Possible options to pass to a set operation.
213 * Used as tag for struct #GNUNET_SET_Option.
215 enum GNUNET_SET_OptionType
220 GNUNET_SET_OPTION_END=0,
222 * Fail set operations when the other peer shows weird behavior
223 * that might by a Byzantine fault.
225 * For set union, 'v.num' is a lower bound on elements
226 * that the other peer must have in common with us.
228 GNUNET_SET_OPTION_BYZANTINE=1,
230 * Do not use the optimized set operation, but send full sets.
231 * Might trigger Byzantine fault detection.
233 GNUNET_SET_OPTION_FORCE_FULL=2,
235 * Only use optimized set operations, even though for this
236 * particular set operation they might be much slower.
237 * Might trigger Byzantine fault detection.
239 GNUNET_SET_OPTION_FORCE_DELTA=4,
244 * Option for set operations.
246 struct GNUNET_SET_Option
249 * Type of the option.
251 enum GNUNET_SET_OptionType type;
254 * Value for the option, only used with some options.
263 * Continuation used for some of the set operations
268 (*GNUNET_SET_Continuation) (void *cls);
272 * Callback for set operation results. Called for each element
276 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
277 * @param current_size current set size
278 * @param status see `enum GNUNET_SET_Status`
281 (*GNUNET_SET_ResultIterator) (void *cls,
282 const struct GNUNET_SET_Element *element,
283 uint64_t current_size,
284 enum GNUNET_SET_Status status);
287 * Iterator for set elements.
290 * @param element the current element, NULL if all elements have been
292 * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop.
295 (*GNUNET_SET_ElementIterator) (void *cls,
296 const struct GNUNET_SET_Element *element);
300 * Called when another peer wants to do a set operation with the
301 * local peer. If a listen error occurs, the @a request is NULL.
304 * @param other_peer the other peer
305 * @param context_msg message with application specific information from
307 * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
308 * to accept it, otherwise the request will be refused
309 * Note that we can't just return value from the listen callback,
310 * as it is also necessary to specify the set we want to do the
311 * operation with, whith sometimes can be derived from the context
312 * message. It's necessary to specify the timeout.
315 (*GNUNET_SET_ListenCallback) (void *cls,
316 const struct GNUNET_PeerIdentity *other_peer,
317 const struct GNUNET_MessageHeader *context_msg,
318 struct GNUNET_SET_Request *request);
323 (*GNUNET_SET_CopyReadyCallback) (void *cls,
324 struct GNUNET_SET_Handle *copy);
328 * Create an empty set, supporting the specified operation.
330 * @param cfg configuration to use for connecting to the
332 * @param op operation supported by the set
333 * Note that the operation has to be specified
334 * beforehand, as certain set operations need to maintain
335 * data structures spefific to the operation
336 * @return a handle to the set
338 struct GNUNET_SET_Handle *
339 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
340 enum GNUNET_SET_OperationType op);
344 * Add an element to the given set.
345 * After the element has been added (in the sense of being
346 * transmitted to the set service), @a cont will be called.
347 * Calls to #GNUNET_SET_add_element can be queued
349 * @param set set to add element to
350 * @param element element to add to the set
351 * @param cont continuation called after the element has been added
352 * @param cont_cls closure for @a cont
353 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
354 * set is invalid (e.g. the set service crashed)
357 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
358 const struct GNUNET_SET_Element *element,
359 GNUNET_SET_Continuation cont,
364 * Remove an element to the given set.
365 * After the element has been removed (in the sense of the
366 * request being transmitted to the set service), cont will be called.
367 * Calls to remove_element can be queued
369 * @param set set to remove element from
370 * @param element element to remove from the set
371 * @param cont continuation called after the element has been removed
372 * @param cont_cls closure for @a cont
373 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
374 * set is invalid (e.g. the set service crashed)
377 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
378 const struct GNUNET_SET_Element *element,
379 GNUNET_SET_Continuation cont,
384 GNUNET_SET_copy_lazy (struct GNUNET_SET_Handle *set,
385 GNUNET_SET_CopyReadyCallback cb,
390 * Destroy the set handle, and free all associated resources.
391 * Iterations must have completed (or be explicitly canceled)
392 * before destroying the corresponding set. Operations may
393 * still be pending when a set is destroyed.
395 * @param set set to destroy
398 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
402 * Prepare a set operation to be evaluated with another peer.
403 * The evaluation will not start until the client provides
404 * a local set with GNUNET_SET_commit().
406 * @param other_peer peer with the other set
407 * @param app_id hash for the application using the set
408 * @param context_msg additional information for the request
409 * @param result_mode specified how results will be returned,
410 * see `enum GNUNET_SET_ResultMode`.
411 * @param result_cb called on error or success
412 * @param result_cls closure for @a result_cb
413 * @return a handle to cancel the operation
415 struct GNUNET_SET_OperationHandle *
416 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
417 const struct GNUNET_HashCode *app_id,
418 const struct GNUNET_MessageHeader *context_msg,
419 enum GNUNET_SET_ResultMode result_mode,
420 struct GNUNET_SET_Option options[],
421 GNUNET_SET_ResultIterator result_cb,
426 * Wait for set operation requests for the given application ID.
427 * If the connection to the set service is lost, the listener is
428 * re-created transparently with exponential backoff.
430 * @param cfg configuration to use for connecting to
432 * @param operation operation we want to listen for
433 * @param app_id id of the application that handles set operation requests
434 * @param listen_cb called for each incoming request matching the operation
436 * @param listen_cls handle for @a listen_cb
437 * @return a handle that can be used to cancel the listen operation
439 struct GNUNET_SET_ListenHandle *
440 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
441 enum GNUNET_SET_OperationType op_type,
442 const struct GNUNET_HashCode *app_id,
443 GNUNET_SET_ListenCallback listen_cb,
448 * Cancel the given listen operation. After calling cancel, the
449 * listen callback for this listen handle will not be called again.
450 * Note that cancelling a listen operation will automatically reject
451 * all operations that have not yet been accepted.
453 * @param lh handle for the listen operation
456 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
460 * Accept a request we got via GNUNET_SET_listen(). Must be called during
461 * GNUNET_SET_listen(), as the `struct GNUNET_SET_Request` becomes invalid
463 * Call GNUNET_SET_commit() to provide the local set to use for the operation,
464 * and to begin the exchange with the remote peer.
466 * @param request request to accept
467 * @param result_mode specified how results will be returned,
468 * see `enum GNUNET_SET_ResultMode`.
469 * @param result_cb callback for the results
470 * @param result_cls closure for @a result_cb
471 * @return a handle to cancel the operation
473 struct GNUNET_SET_OperationHandle *
474 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
475 enum GNUNET_SET_ResultMode result_mode,
476 struct GNUNET_SET_Option options[],
477 GNUNET_SET_ResultIterator result_cb,
482 * Commit a set to be used with a set operation.
483 * This function is called once we have fully constructed
484 * the set that we want to use for the operation. At this
485 * time, the P2P protocol can then begin to exchange the
486 * set information and call the result callback with the
487 * result information.
489 * @param oh handle to the set operation
490 * @param set the set to use for the operation
491 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
492 * set is invalid (e.g. the set service crashed)
495 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
496 struct GNUNET_SET_Handle *set);
500 * Cancel the given set operation. May not be called after the
501 * operation's `GNUNET_SET_ResultIterator` has been called with a
502 * status that indicates error, timeout or done.
504 * @param oh set operation to cancel
507 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
511 * Iterate over all elements in the given set.
512 * Note that this operation involves transferring every element of the set
513 * from the service to the client, and is thus costly.
514 * Only one iteration per set may be active at the same time.
516 * @param set the set to iterate over
517 * @param iter the iterator to call for each element
518 * @param iter_cls closure for @a iter
519 * @return #GNUNET_YES if the iteration started successfuly,
520 * #GNUNET_NO if another iteration was still active,
521 * #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
524 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
525 GNUNET_SET_ElementIterator iter,
530 * Stop iteration over all elements in the given set. Can only
531 * be called before the iteration has "naturally" completed its
534 * @param set the set to stop iterating over
537 GNUNET_SET_iterate_cancel (struct GNUNET_SET_Handle *set);
541 * Create a copy of an element. The copy
542 * must be GNUNET_free-d by the caller.
544 * @param element the element to copy
545 * @return the copied element
547 struct GNUNET_SET_Element *
548 GNUNET_SET_element_dup (const struct GNUNET_SET_Element *element);
552 * Hash a set element.
554 * @param element the element that should be hashed
555 * @param ret_hash a pointer to where the hash of @a element
559 GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element,
560 struct GNUNET_HashCode *ret_hash);
563 #if 0 /* keep Emacsens' auto-indent happy */
572 /** @} */ /* end of group */