36a88e162c294577cdf4bd69463f1f1b31948e37
[oweals/gnunet.git] / src / include / gnunet_set_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2013, 2014 GNUnet e.V.
4
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.
9
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.
14  */
15
16 /**
17  * @author Florian Dold
18  * @author Christian Grothoff
19  *
20  * @file
21  * Two-peer set operations
22  *
23  * @defgroup set  Set service
24  * Two-peer set operations
25  *
26  * @see [Documentation](https://gnunet.org/set-subsystem)
27  *
28  * @{
29  */
30
31 #ifndef GNUNET_SET_SERVICE_H
32 #define GNUNET_SET_SERVICE_H
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42 #include "gnunet_common.h"
43 #include "gnunet_time_lib.h"
44 #include "gnunet_configuration_lib.h"
45
46
47 /**
48  * Maximum size of a context message for set operation requests.
49  */
50 #define GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE ((1<<16) - 1024)
51
52
53 /**
54  * Opaque handle to a set.
55  */
56 struct GNUNET_SET_Handle;
57
58 /**
59  * Opaque handle to a set operation request from another peer.
60  */
61 struct GNUNET_SET_Request;
62
63 /**
64  * Opaque handle to a listen operation.
65  */
66 struct GNUNET_SET_ListenHandle;
67
68 /**
69  * Opaque handle to a set operation.
70  */
71 struct GNUNET_SET_OperationHandle;
72
73
74 /**
75  * The operation that a set set supports.
76  */
77 enum GNUNET_SET_OperationType
78 {
79   /**
80    * A purely local set that does not support any operation.
81    */
82   GNUNET_SET_OPERATION_NONE,
83
84   /**
85    * Set intersection, only return elements that are in both sets.
86    */
87   GNUNET_SET_OPERATION_INTERSECTION,
88
89   /**
90    * Set union, return all elements that are in at least one of the sets.
91    */
92   GNUNET_SET_OPERATION_UNION
93 };
94
95
96 /**
97  * Status for the result callback
98  */
99 enum GNUNET_SET_Status
100 {
101   /**
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`).
105    *
106    * Only applies to
107    * #GNUNET_SET_RESULT_FULL,
108    * #GNUNET_SET_RESULT_ADDED,
109    * #GNUNET_SET_RESULT_REMOVED,
110    */
111   GNUNET_SET_STATUS_OK,
112
113   /**
114    * Element should be added to the result set
115    * of the local peer, i.e. the local peer is
116    * missing an element.
117    *
118    * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
119    */
120   GNUNET_SET_STATUS_ADD_LOCAL,
121
122   /**
123    * Element should be added to the result set
124    * of the remote peer, i.e. the remote peer is
125    * missing an element.
126    *
127    * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
128    */
129   GNUNET_SET_STATUS_ADD_REMOTE,
130
131   /**
132    * The other peer refused to to the operation with us,
133    * or something went wrong.
134    */
135   GNUNET_SET_STATUS_FAILURE,
136
137   /**
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.
141    */
142   GNUNET_SET_STATUS_HALF_DONE,
143
144   /**
145    * Success, all elements have been sent (and received).
146    */
147   GNUNET_SET_STATUS_DONE
148 };
149
150
151
152 /**
153  * The way results are given to the client.
154  */
155 enum GNUNET_SET_ResultMode
156 {
157   /**
158    * Client gets every element in the resulting set.
159    *
160    * Only supported for set intersection.
161    */
162   GNUNET_SET_RESULT_FULL,
163
164   /**
165    * Client gets notified of the required changes
166    * for both the local and the remote set.
167    *
168    * Only supported for set
169    */
170   GNUNET_SET_RESULT_SYMMETRIC,
171
172   /**
173    * Client gets only elements that have been removed from the set.
174    *
175    * Only supported for set intersection.
176    */
177   GNUNET_SET_RESULT_REMOVED,
178
179   /**
180    * Client gets only elements that have been added to the set.
181    *
182    * Only supported for set union.
183    */
184   GNUNET_SET_RESULT_ADDED
185 };
186
187
188 /**
189  * Element stored in a set.
190  */
191 struct GNUNET_SET_Element
192 {
193   /**
194    * Number of bytes in the buffer pointed to by data.
195    */
196   uint16_t size;
197
198   /**
199    * Application-specific element type.
200    */
201   uint16_t element_type;
202
203   /**
204    * Actual data of the element
205    */
206   const void *data;
207 };
208
209
210 /**
211  * Possible options to pass to a set operation.
212  *
213  * Used as tag for struct #GNUNET_SET_Option.
214  */
215 enum GNUNET_SET_OptionType
216 {
217   /**
218    * List terminator.
219    */
220   GNUNET_SET_OPTION_END=0,
221   /**
222    * Fail set operations when the other peer shows weird behavior
223    * that might by a Byzantine fault.
224    *
225    * For set union, 'v.num' is a lower bound on elements
226    * that the other peer must have in common with us.
227    */
228   GNUNET_SET_OPTION_BYZANTINE=1,
229   /**
230    * Do not use the optimized set operation, but send full sets.
231    * Might trigger Byzantine fault detection.
232    */
233   GNUNET_SET_OPTION_FORCE_FULL=2,
234   /**
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.
238    */
239   GNUNET_SET_OPTION_FORCE_DELTA=4,
240 };
241
242
243 /**
244  * Option for set operations.
245  */
246 struct GNUNET_SET_Option
247 {
248   /**
249    * Type of the option.
250    */
251   enum GNUNET_SET_OptionType type;
252
253   /**
254    * Value for the option, only used with some options.
255    */
256   union {
257     uint64_t num;
258   } v;
259 };
260
261
262 /**
263  * Continuation used for some of the set operations
264  *
265  * @param cls closure
266  */
267 typedef void
268 (*GNUNET_SET_Continuation) (void *cls);
269
270
271 /**
272  * Callback for set operation results. Called for each element
273  * in the result set.
274  *
275  * @param cls closure
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`
279  */
280 typedef void
281 (*GNUNET_SET_ResultIterator) (void *cls,
282                               const struct GNUNET_SET_Element *element,
283                               uint64_t current_size,
284                               enum GNUNET_SET_Status status);
285
286 /**
287  * Iterator for set elements.
288  *
289  * @param cls closure
290  * @param element the current element, NULL if all elements have been
291  *        iterated over
292  * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop.
293  */
294 typedef int
295 (*GNUNET_SET_ElementIterator) (void *cls,
296                                const struct GNUNET_SET_Element *element);
297
298
299 /**
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.
302  *
303  * @param cls closure
304  * @param other_peer the other peer
305  * @param context_msg message with application specific information from
306  *        the other peer
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.
313  */
314 typedef void
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);
319
320
321
322 typedef void
323 (*GNUNET_SET_CopyReadyCallback) (void *cls,
324                                  struct GNUNET_SET_Handle *copy);
325
326
327 /**
328  * Create an empty set, supporting the specified operation.
329  *
330  * @param cfg configuration to use for connecting to the
331  *        set service
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
337  */
338 struct GNUNET_SET_Handle *
339 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
340                    enum GNUNET_SET_OperationType op);
341
342
343 /**
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
348  *
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)
355  */
356 int
357 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
358                         const struct GNUNET_SET_Element *element,
359                         GNUNET_SET_Continuation cont,
360                         void *cont_cls);
361
362
363 /**
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
368  *
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)
375  */
376 int
377 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
378                            const struct GNUNET_SET_Element *element,
379                            GNUNET_SET_Continuation cont,
380                            void *cont_cls);
381
382
383 void
384 GNUNET_SET_copy_lazy (struct GNUNET_SET_Handle *set,
385                       GNUNET_SET_CopyReadyCallback cb,
386                       void *cls);
387
388
389 /**
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.
394  *
395  * @param set set to destroy
396  */
397 void
398 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
399
400
401 /**
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().
405  *
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
414  */
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,
422                     void *result_cls);
423
424
425 /**
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.
429  *
430  * @param cfg configuration to use for connecting to
431  *            the set service
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
435  *                  and application id
436  * @param listen_cls handle for @a listen_cb
437  * @return a handle that can be used to cancel the listen operation
438  */
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,
444                    void *listen_cls);
445
446
447 /**
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.
452  *
453  * @param lh handle for the listen operation
454  */
455 void
456 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
457
458
459 /**
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
462  * afterwards.
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.
465  *
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
472  */
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,
478                    void *result_cls);
479
480
481 /**
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.
488  *
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)
493  */
494 int
495 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
496                    struct GNUNET_SET_Handle *set);
497
498
499 /**
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.
503  *
504  * @param oh set operation to cancel
505  */
506 void
507 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
508
509
510 /**
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.
515  *
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)
522  */
523 int
524 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
525                     GNUNET_SET_ElementIterator iter,
526                     void *iter_cls);
527
528
529 /**
530  * Stop iteration over all elements in the given set.  Can only
531  * be called before the iteration has "naturally" completed its
532  * turn.
533  *
534  * @param set the set to stop iterating over
535  */
536 void
537 GNUNET_SET_iterate_cancel (struct GNUNET_SET_Handle *set);
538
539
540 /**
541  * Create a copy of an element.  The copy
542  * must be GNUNET_free-d by the caller.
543  *
544  * @param element the element to copy
545  * @return the copied element
546  */
547 struct GNUNET_SET_Element *
548 GNUNET_SET_element_dup (const struct GNUNET_SET_Element *element);
549
550
551 /**
552  * Hash a set element.
553  *
554  * @param element the element that should be hashed
555  * @param ret_hash a pointer to where the hash of @a element
556  *        should be stored
557  */
558 void
559 GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element,
560                          struct GNUNET_HashCode *ret_hash);
561
562
563 #if 0                           /* keep Emacsens' auto-indent happy */
564 {
565 #endif
566 #ifdef __cplusplus
567 }
568 #endif
569
570 #endif
571
572 /** @} */  /* end of group */