ca46d874673cce196020b37fd00394b230393228
[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
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       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       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @author Florian Dold
23  * @author Christian Grothoff
24  *
25  * @file
26  * Two-peer set operations
27  *
28  * @defgroup set  Set service
29  * Two-peer set operations
30  *
31  * @see [Documentation](https://gnunet.org/set-subsystem)
32  *
33  * @{
34  */
35
36 #ifndef GNUNET_SET_SERVICE_H
37 #define GNUNET_SET_SERVICE_H
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #if 0                           /* keep Emacsens' auto-indent happy */
43 }
44 #endif
45 #endif
46
47 #include "gnunet_common.h"
48 #include "gnunet_time_lib.h"
49 #include "gnunet_configuration_lib.h"
50
51
52 /**
53  * Maximum size of a context message for set operation requests.
54  */
55 #define GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE ((1<<16) - 1024)
56
57
58 /**
59  * Opaque handle to a set.
60  */
61 struct GNUNET_SET_Handle;
62
63 /**
64  * Opaque handle to a set operation request from another peer.
65  */
66 struct GNUNET_SET_Request;
67
68 /**
69  * Opaque handle to a listen operation.
70  */
71 struct GNUNET_SET_ListenHandle;
72
73 /**
74  * Opaque handle to a set operation.
75  */
76 struct GNUNET_SET_OperationHandle;
77
78
79 /**
80  * The operation that a set set supports.
81  */
82 enum GNUNET_SET_OperationType
83 {
84   /**
85    * A purely local set that does not support any operation.
86    */
87   GNUNET_SET_OPERATION_NONE,
88
89   /**
90    * Set intersection, only return elements that are in both sets.
91    */
92   GNUNET_SET_OPERATION_INTERSECTION,
93
94   /**
95    * Set union, return all elements that are in at least one of the sets.
96    */
97   GNUNET_SET_OPERATION_UNION
98 };
99
100
101 /**
102  * Status for the result callback
103  */
104 enum GNUNET_SET_Status
105 {
106   /**
107    * Everything went ok, we are transmitting an element of the
108    * result (in set, or to be removed from set, depending on
109    * the `enum GNUNET_SET_ResultMode`).
110    *
111    * Only applies to
112    * #GNUNET_SET_RESULT_FULL,
113    * #GNUNET_SET_RESULT_ADDED,
114    * #GNUNET_SET_RESULT_REMOVED,
115    */
116   GNUNET_SET_STATUS_OK,
117
118   /**
119    * Element should be added to the result set
120    * of the local peer, i.e. the local peer is
121    * missing an element.
122    *
123    * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
124    */
125   GNUNET_SET_STATUS_ADD_LOCAL,
126
127   /**
128    * Element should be added to the result set
129    * of the remote peer, i.e. the remote peer is
130    * missing an element.
131    *
132    * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
133    */
134   GNUNET_SET_STATUS_ADD_REMOTE,
135
136   /**
137    * The other peer refused to to the operation with us,
138    * or something went wrong.
139    */
140   GNUNET_SET_STATUS_FAILURE,
141
142   /**
143    * Success, all elements have been returned (but the other peer
144    * might still be receiving some from us, so we are not done).  Only
145    * used during UNION operation.
146    */
147   GNUNET_SET_STATUS_HALF_DONE,
148
149   /**
150    * Success, all elements have been sent (and received).
151    */
152   GNUNET_SET_STATUS_DONE
153 };
154
155
156
157 /**
158  * The way results are given to the client.
159  */
160 enum GNUNET_SET_ResultMode
161 {
162   /**
163    * Client gets every element in the resulting set.
164    *
165    * Only supported for set intersection.
166    */
167   GNUNET_SET_RESULT_FULL,
168
169   /**
170    * Client gets notified of the required changes
171    * for both the local and the remote set.
172    *
173    * Only supported for set
174    */
175   GNUNET_SET_RESULT_SYMMETRIC,
176
177   /**
178    * Client gets only elements that have been removed from the set.
179    *
180    * Only supported for set intersection.
181    */
182   GNUNET_SET_RESULT_REMOVED,
183
184   /**
185    * Client gets only elements that have been added to the set.
186    *
187    * Only supported for set union.
188    */
189   GNUNET_SET_RESULT_ADDED
190 };
191
192
193 /**
194  * Element stored in a set.
195  */
196 struct GNUNET_SET_Element
197 {
198   /**
199    * Number of bytes in the buffer pointed to by data.
200    */
201   uint16_t size;
202
203   /**
204    * Application-specific element type.
205    */
206   uint16_t element_type;
207
208   /**
209    * Actual data of the element
210    */
211   const void *data;
212 };
213
214
215 /**
216  * Possible options to pass to a set operation.
217  *
218  * Used as tag for struct #GNUNET_SET_Option.
219  */
220 enum GNUNET_SET_OptionType
221 {
222   /**
223    * Fail set operations when the other peer shows weird behavior
224    * that might by a Byzantine fault.
225    *
226    * For set union, 'v.num' is a lower bound on elements
227    * that the other peer must have in common with us.
228    */
229   GNUNET_SET_OPTION_BYZANTINE=1,
230   /**
231    * Do not use the optimized set operation, but send full sets.
232    * Might trigger Byzantine fault detection.
233    */
234   GNUNET_SET_OPTION_FORCE_FULL=2,
235   /**
236    * Only use optimized set operations, even though for this
237    * particular set operation they might be much slower.
238    * Might trigger Byzantine fault detection.
239    */
240   GNUNET_SET_OPTION_FORCE_DELTA=4,
241 };
242
243
244 /**
245  * Option for set operations.
246  */
247 struct GNUNET_SET_Option
248 {
249   /**
250    * Type of the option.
251    */
252   enum GNUNET_SET_OptionType type;
253
254   /**
255    * Value for the option, only used with some options.
256    */
257   union {
258     uint64_t num;
259   } v;
260 };
261
262
263 /**
264  * Continuation used for some of the set operations
265  *
266  * @param cls closure
267  */
268 typedef void
269 (*GNUNET_SET_Continuation) (void *cls);
270
271
272 /**
273  * Callback for set operation results. Called for each element
274  * in the result set.
275  *
276  * @param cls closure
277  * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
278  * @param current_size current set size
279  * @param status see `enum GNUNET_SET_Status`
280  */
281 typedef void
282 (*GNUNET_SET_ResultIterator) (void *cls,
283                               const struct GNUNET_SET_Element *element,
284                               uint64_t current_size,
285                               enum GNUNET_SET_Status status);
286
287 /**
288  * Iterator for set elements.
289  *
290  * @param cls closure
291  * @param element the current element, NULL if all elements have been
292  *        iterated over
293  * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop.
294  */
295 typedef int
296 (*GNUNET_SET_ElementIterator) (void *cls,
297                                const struct GNUNET_SET_Element *element);
298
299
300 /**
301  * Called when another peer wants to do a set operation with the
302  * local peer. If a listen error occurs, the @a request is NULL.
303  *
304  * @param cls closure
305  * @param other_peer the other peer
306  * @param context_msg message with application specific information from
307  *        the other peer
308  * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
309  *        to accept it, otherwise the request will be refused
310  *        Note that we can't just return value from the listen callback,
311  *        as it is also necessary to specify the set we want to do the
312  *        operation with, whith sometimes can be derived from the context
313  *        message. It's necessary to specify the timeout.
314  */
315 typedef void
316 (*GNUNET_SET_ListenCallback) (void *cls,
317                               const struct GNUNET_PeerIdentity *other_peer,
318                               const struct GNUNET_MessageHeader *context_msg,
319                               struct GNUNET_SET_Request *request);
320
321
322
323 typedef void
324 (*GNUNET_SET_CopyReadyCallback) (void *cls,
325                                  struct GNUNET_SET_Handle *copy);
326
327
328 /**
329  * Create an empty set, supporting the specified operation.
330  *
331  * @param cfg configuration to use for connecting to the
332  *        set service
333  * @param op operation supported by the set
334  *        Note that the operation has to be specified
335  *        beforehand, as certain set operations need to maintain
336  *        data structures spefific to the operation
337  * @return a handle to the set
338  */
339 struct GNUNET_SET_Handle *
340 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
341                    enum GNUNET_SET_OperationType op);
342
343
344 /**
345  * Add an element to the given set.
346  * After the element has been added (in the sense of being
347  * transmitted to the set service), @a cont will be called.
348  * Calls to #GNUNET_SET_add_element can be queued
349  *
350  * @param set set to add element to
351  * @param element element to add to the set
352  * @param cont continuation called after the element has been added
353  * @param cont_cls closure for @a cont
354  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
355  *         set is invalid (e.g. the set service crashed)
356  */
357 int
358 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
359                         const struct GNUNET_SET_Element *element,
360                         GNUNET_SET_Continuation cont,
361                         void *cont_cls);
362
363
364 /**
365  * Remove an element to the given set.
366  * After the element has been removed (in the sense of the
367  * request being transmitted to the set service), cont will be called.
368  * Calls to remove_element can be queued
369  *
370  * @param set set to remove element from
371  * @param element element to remove from the set
372  * @param cont continuation called after the element has been removed
373  * @param cont_cls closure for @a cont
374  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
375  *         set is invalid (e.g. the set service crashed)
376  */
377 int
378 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
379                            const struct GNUNET_SET_Element *element,
380                            GNUNET_SET_Continuation cont,
381                            void *cont_cls);
382
383
384 void
385 GNUNET_SET_copy_lazy (struct GNUNET_SET_Handle *set,
386                       GNUNET_SET_CopyReadyCallback cb,
387                       void *cls);
388
389
390 /**
391  * Destroy the set handle, and free all associated resources.
392  * Iterations must have completed (or be explicitly canceled)
393  * before destroying the corresponding set.  Operations may
394  * still be pending when a set is destroyed.
395  *
396  * @param set set to destroy
397  */
398 void
399 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
400
401
402 /**
403  * Prepare a set operation to be evaluated with another peer.
404  * The evaluation will not start until the client provides
405  * a local set with GNUNET_SET_commit().
406  *
407  * @param other_peer peer with the other set
408  * @param app_id hash for the application using the set
409  * @param context_msg additional information for the request
410  * @param result_mode specified how results will be returned,
411  *        see `enum GNUNET_SET_ResultMode`.
412  * @param result_cb called on error or success
413  * @param result_cls closure for @a result_cb
414  * @return a handle to cancel the operation
415  */
416 struct GNUNET_SET_OperationHandle *
417 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
418                     const struct GNUNET_HashCode *app_id,
419                     const struct GNUNET_MessageHeader *context_msg,
420                     enum GNUNET_SET_ResultMode result_mode,
421                     struct GNUNET_SET_Option options[],
422                     GNUNET_SET_ResultIterator result_cb,
423                     void *result_cls);
424
425
426 /**
427  * Wait for set operation requests for the given application ID.
428  * If the connection to the set service is lost, the listener is
429  * re-created transparently with exponential backoff.
430  *
431  * @param cfg configuration to use for connecting to
432  *            the set service
433  * @param operation operation we want to listen for
434  * @param app_id id of the application that handles set operation requests
435  * @param listen_cb called for each incoming request matching the operation
436  *                  and application id
437  * @param listen_cls handle for @a listen_cb
438  * @return a handle that can be used to cancel the listen operation
439  */
440 struct GNUNET_SET_ListenHandle *
441 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
442                    enum GNUNET_SET_OperationType op_type,
443                    const struct GNUNET_HashCode *app_id,
444                    GNUNET_SET_ListenCallback listen_cb,
445                    void *listen_cls);
446
447
448 /**
449  * Cancel the given listen operation.  After calling cancel, the
450  * listen callback for this listen handle will not be called again.
451  *
452  * @param lh handle for the listen operation
453  */
454 void
455 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
456
457
458 /**
459  * Accept a request we got via GNUNET_SET_listen().  Must be called during
460  * GNUNET_SET_listen(), as the `struct GNUNET_SET_Request` becomes invalid
461  * afterwards.
462  * Call GNUNET_SET_commit() to provide the local set to use for the operation,
463  * and to begin the exchange with the remote peer.
464  *
465  * @param request request to accept
466  * @param result_mode specified how results will be returned,
467  *        see `enum GNUNET_SET_ResultMode`.
468  * @param result_cb callback for the results
469  * @param result_cls closure for @a result_cb
470  * @return a handle to cancel the operation
471  */
472 struct GNUNET_SET_OperationHandle *
473 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
474                    enum GNUNET_SET_ResultMode result_mode,
475                    struct GNUNET_SET_Option options[],
476                    GNUNET_SET_ResultIterator result_cb,
477                    void *result_cls);
478
479
480 /**
481  * Commit a set to be used with a set operation.
482  * This function is called once we have fully constructed
483  * the set that we want to use for the operation.  At this
484  * time, the P2P protocol can then begin to exchange the
485  * set information and call the result callback with the
486  * result information.
487  *
488  * @param oh handle to the set operation
489  * @param set the set to use for the operation
490  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
491  *         set is invalid (e.g. the set service crashed)
492  */
493 int
494 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
495                    struct GNUNET_SET_Handle *set);
496
497
498 /**
499  * Cancel the given set operation.  May not be called after the
500  * operation's `GNUNET_SET_ResultIterator` has been called with a
501  * status that indicates error, timeout or done.
502  *
503  * @param oh set operation to cancel
504  */
505 void
506 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
507
508
509 /**
510  * Iterate over all elements in the given set.
511  * Note that this operation involves transferring every element of the set
512  * from the service to the client, and is thus costly.
513  * Only one iteration per set may be active at the same time.
514  *
515  * @param set the set to iterate over
516  * @param iter the iterator to call for each element
517  * @param iter_cls closure for @a iter
518  * @return #GNUNET_YES if the iteration started successfuly,
519  *         #GNUNET_NO if another iteration was still active,
520  *         #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
521  */
522 int
523 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
524                     GNUNET_SET_ElementIterator iter,
525                     void *iter_cls);
526
527 /**
528  * Stop iteration over all elements in the given set.  Can only
529  * be called before the iteration has "naturally" completed its
530  * turn.
531  *
532  * @param set the set to stop iterating over
533  */
534 void
535 GNUNET_SET_iterate_cancel (struct GNUNET_SET_Handle *set);
536
537 /**
538  * Create a copy of an element.  The copy
539  * must be GNUNET_free-d by the caller.
540  *
541  * @param element the element to copy
542  * @return the copied element
543  */
544 struct GNUNET_SET_Element *
545 GNUNET_SET_element_dup (const struct GNUNET_SET_Element *element);
546
547 /**
548  * Hash a set element.
549  *
550  * @param element the element that should be hashed
551  * @param ret_hash a pointer to where the hash of @a element
552  *        should be stored
553  */
554 void
555 GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element, struct GNUNET_HashCode *ret_hash);
556
557
558 #if 0                           /* keep Emacsens' auto-indent happy */
559 {
560 #endif
561 #ifdef __cplusplus
562 }
563 #endif
564
565 #endif
566
567 /** @} */  /* end of group */