Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / set / gnunet-service-set.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  * @file set/gnunet-service-set.h
22  * @brief common components for the implementation the different set operations
23  * @author Florian Dold
24  * @author Christian Grothoff
25  */
26 #ifndef GNUNET_SERVICE_SET_H_PRIVATE
27 #define GNUNET_SERVICE_SET_H_PRIVATE
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_applications.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_cadet_service.h"
35 #include "gnunet_set_service.h"
36 #include "set.h"
37
38
39 /**
40  * Implementation-specific set state.  Used as opaque pointer, and
41  * specified further in the respective implementation.
42  */
43 struct SetState;
44
45 /**
46  * Implementation-specific set operation.  Used as opaque pointer, and
47  * specified further in the respective implementation.
48  */
49 struct OperationState;
50
51 /**
52  * A set that supports a specific operation with other peers.
53  */
54 struct Set;
55
56 /**
57  * Information about an element element in the set.  All elements are
58  * stored in a hash-table from their hash-code to their 'struct
59  * Element', so that the remove and add operations are reasonably
60  * fast.
61  */
62 struct ElementEntry;
63
64 /**
65  * Operation context used to execute a set operation.
66  */
67 struct Operation;
68
69
70 /**
71  * Detail information about an operation.
72  */
73 struct OperationSpecification
74 {
75
76   /**
77    * The remove peer we evaluate the operation with.
78    */
79   struct GNUNET_PeerIdentity peer;
80
81   /**
82    * Application ID for the operation, used to distinguish
83    * multiple operations of the same type with the same peer.
84    */
85   struct GNUNET_HashCode app_id;
86
87   /**
88    * Context message, may be NULL.
89    */
90   struct GNUNET_MessageHeader *context_msg;
91
92   /**
93    * Set associated with the operation, NULL until the spec has been
94    * associated with a set.
95    */
96   struct Set *set;
97
98   /**
99    * Salt to use for the operation.
100    */
101   uint32_t salt;
102
103   /**
104    * Remote peers element count
105    */
106   uint32_t remote_element_count;
107
108   /**
109    * ID used to identify an operation between service and client
110    */
111   uint32_t client_request_id;
112
113   /**
114    * The type of the operation.
115    */
116   enum GNUNET_SET_OperationType operation;
117
118   /**
119    * When are elements sent to the client, and which elements are sent?
120    */
121   enum GNUNET_SET_ResultMode result_mode;
122
123   /**
124    * Always use delta operation instead of sending full sets,
125    * even it it's less efficient.
126    */
127   int force_delta;
128
129   /**
130    * Always send full sets, even if delta operations would
131    * be more efficient.
132    */
133   int force_full;
134
135   /**
136    * #GNUNET_YES to fail operations where Byzantine faults
137    * are suspected
138    */
139   int byzantine;
140
141   /**
142    * Lower bound for the set size, used only when
143    * byzantine mode is enabled.
144    */
145   int byzantine_lower_bound;
146 };
147
148
149 /**
150  * Signature of functions that create the implementation-specific
151  * state for a set supporting a specific operation.
152  *
153  * @return a set state specific to the supported operation, NULL on error
154  */
155 typedef struct SetState *
156 (*CreateImpl) (void);
157
158
159 /**
160  * Signature of functions that implement the add/remove functionality
161  * for a set supporting a specific operation.
162  *
163  * @param set implementation-specific set state
164  * @param ee element message from the client
165  */
166 typedef void
167 (*AddRemoveImpl) (struct SetState *state,
168                   struct ElementEntry *ee);
169
170
171 /**
172  * Signature of functions that handle disconnection of the remote
173  * peer.
174  *
175  * @param op the set operation, contains implementation-specific data
176  */
177 typedef void
178 (*PeerDisconnectImpl) (struct Operation *op);
179
180
181 /**
182  * Signature of functions that implement the destruction of the
183  * implementation-specific set state.
184  *
185  * @param state the set state, contains implementation-specific data
186  */
187 typedef void
188 (*DestroySetImpl) (struct SetState *state);
189
190
191 /**
192  * Signature of functions that implement accepting a set operation.
193  *
194  * @param op operation that is created by accepting the operation,
195  *        should be initialized by the implementation
196  */
197 typedef void
198 (*OpAcceptImpl) (struct Operation *op);
199
200
201 /**
202  * Signature of functions that implement starting the evaluation of
203  * set operations.
204  *
205  * @param op operation that is created, should be initialized to
206  *        begin the evaluation
207  * @param opaque_context message to be transmitted to the listener
208  *        to convince him to accept, may be NULL
209  */
210 typedef void
211 (*OpEvaluateImpl) (struct Operation *op,
212                    const struct GNUNET_MessageHeader *opaque_context);
213
214
215 /**
216  * Signature of functions that implement the message handling for
217  * the different set operations.
218  *
219  * @param op operation state
220  * @param msg received message
221  * @return #GNUNET_OK on success, #GNUNET_SYSERR to
222  *         destroy the operation and the tunnel
223  */
224 typedef int
225 (*MsgHandlerImpl) (struct Operation *op,
226                    const struct GNUNET_MessageHeader *msg);
227
228
229 /**
230  * Signature of functions that implement operation cancellation
231  *
232  * @param op operation state
233  */
234 typedef void
235 (*CancelImpl) (struct Operation *op);
236
237
238 typedef struct SetState *
239 (*CopyStateImpl) (struct Set *op);
240
241
242 /**
243  * Dispatch table for a specific set operation.  Every set operation
244  * has to implement the callback in this struct.
245  */
246 struct SetVT
247 {
248   /**
249    * Callback for the set creation.
250    */
251   CreateImpl create;
252
253   /**
254    * Callback for element insertion
255    */
256   AddRemoveImpl add;
257
258   /**
259    * Callback for element removal.
260    */
261   AddRemoveImpl remove;
262
263   /**
264    * Callback for accepting a set operation request
265    */
266   OpAcceptImpl accept;
267
268   /**
269    * Callback for starting evaluation with a remote peer.
270    */
271   OpEvaluateImpl evaluate;
272
273   /**
274    * Callback for destruction of the set state.
275    */
276   DestroySetImpl destroy_set;
277
278   /**
279    * Callback for handling operation-specific messages.
280    */
281   MsgHandlerImpl msg_handler;
282
283   /**
284    * Callback for handling the remote peer's disconnect.
285    */
286   PeerDisconnectImpl peer_disconnect;
287
288   /**
289    * Callback for canceling an operation by its ID.
290    */
291   CancelImpl cancel;
292
293   CopyStateImpl copy_state;
294 };
295
296
297 /**
298  * MutationEvent gives information about changes
299  * to an element (removal / addition) in a set content.
300  */
301 struct MutationEvent
302 {
303   /**
304    * First generation affected by this mutation event.
305    *
306    * If @a generation is 0, this mutation event is a list
307    * sentinel element.
308    */
309   unsigned int generation;
310
311   /**
312    * If @a added is #GNUNET_YES, then this is a
313    * `remove` event, otherwise it is an `add` event.
314    */
315   int added;
316 };
317
318
319 /**
320  * Information about an element element in the set.  All elements are
321  * stored in a hash-table from their hash-code to their `struct
322  * Element`, so that the remove and add operations are reasonably
323  * fast.
324  */
325 struct ElementEntry
326 {
327   /**
328    * The actual element. The data for the element
329    * should be allocated at the end of this struct.
330    */
331   struct GNUNET_SET_Element element;
332
333   /**
334    * Hash of the element.  For set union: Will be used to derive the
335    * different IBF keys for different salts.
336    */
337   struct GNUNET_HashCode element_hash;
338
339   /**
340    * If @a mutations is not NULL, it contains
341    * a list of mutations, ordered by increasing generation.
342    * The list is terminated by a sentinel event with `generation`
343    * set to 0.
344    *
345    * If @a mutations is NULL, then this element exists in all generations
346    * of the respective set content this element belongs to.
347    */
348   struct MutationEvent *mutations;
349
350   /**
351    * Number of elements in the array @a mutations.
352    */
353   unsigned int mutations_size;
354
355   /**
356    * #GNUNET_YES if the element is a remote element, and does not belong
357    * to the operation's set.
358    */
359   int remote;
360 };
361
362
363 struct Listener;
364
365
366 /**
367  * Operation context used to execute a set operation.
368  */
369 struct Operation
370 {
371   /**
372    * V-Table for the operation belonging to the tunnel contest.
373    *
374    * Used for all operation specific operations after receiving the ops request
375    */
376   const struct SetVT *vt;
377
378   /**
379    * Channel to the peer.
380    */
381   struct GNUNET_CADET_Channel *channel;
382
383   /**
384    * Port this operation runs on.
385    */
386   struct Listener *listener;
387
388   /**
389    * Message queue for the channel.
390    */
391   struct GNUNET_MQ_Handle *mq;
392
393   /**
394    * Detail information about the set operation, including the set to
395    * use.  When 'spec' is NULL, the operation is not yet entirely
396    * initialized.
397    */
398   struct OperationSpecification *spec;
399
400   /**
401    * Operation-specific operation state.  Note that the exact
402    * type depends on this being a union or intersection operation
403    * (and thus on @e vt).
404    */
405   struct OperationState *state;
406
407   /**
408    * Evaluate operations are held in a linked list.
409    */
410   struct Operation *next;
411
412   /**
413    * Evaluate operations are held in a linked list.
414    */
415   struct Operation *prev;
416
417   /**
418    * The identity of the requesting peer.  Needs to
419    * be stored here as the op spec might not have been created yet.
420    */
421   struct GNUNET_PeerIdentity peer;
422
423   /**
424    * Timeout task, if the incoming peer has not been accepted
425    * after the timeout, it will be disconnected.
426    */
427   struct GNUNET_SCHEDULER_Task *timeout_task;
428
429   /**
430    * Unique request id for the request from a remote peer, sent to the
431    * client, which will accept or reject the request.  Set to '0' iff
432    * the request has not been suggested yet.
433    */
434   uint32_t suggest_id;
435
436   /**
437    * #GNUNET_YES if this is not a "real" set operation yet, and we still
438    * need to wait for the other peer to give us more details.
439    */
440   int is_incoming;
441
442   /**
443    * Generation in which the operation handle
444    * was created.
445    */
446   unsigned int generation_created;
447
448   /**
449    * Incremented whenever (during shutdown) some component still
450    * needs to do something with this before the operation is freed.
451    * (Used as a reference counter, but only during termination.)
452    */
453   unsigned int keep;
454 };
455
456
457 /**
458  * SetContent stores the actual set elements,
459  * which may be shared by multiple generations derived
460  * from one set.
461  */
462 struct SetContent
463 {
464   /**
465    * Number of references to the content.
466    */
467   unsigned int refcount;
468
469   /**
470    * Maps `struct GNUNET_HashCode *` to `struct ElementEntry *`.
471    */
472   struct GNUNET_CONTAINER_MultiHashMap *elements;
473
474   unsigned int latest_generation;
475
476   /**
477    * Mutations requested by the client that we're
478    * unable to execute right now because we're iterating
479    * over the underlying hash map of elements.
480    */
481   struct PendingMutation *pending_mutations_head;
482
483   /**
484    * Mutations requested by the client that we're
485    * unable to execute right now because we're iterating
486    * over the underlying hash map of elements.
487    */
488   struct PendingMutation *pending_mutations_tail;
489
490   /**
491    * Number of concurrently active iterators.
492    */
493   int iterator_count;
494 };
495
496
497 struct GenerationRange
498 {
499   /**
500    * First generation that is excluded.
501    */
502   unsigned int start;
503
504   /**
505    * Generation after the last excluded generation.
506    */
507   unsigned int end;
508 };
509
510
511 struct PendingMutation
512 {
513   struct PendingMutation *prev;
514   struct PendingMutation *next;
515
516   struct Set *set;
517
518   /**
519    * Message that describes the desired mutation.
520    * May only be a #GNUNET_MESSAGE_TYPE_SET_ADD or
521    * #GNUNET_MESSAGE_TYPE_SET_REMOVE.
522    */
523   struct GNUNET_MessageHeader *mutation_message;
524 };
525
526
527 /**
528  * A set that supports a specific operation with other peers.
529  */
530 struct Set
531 {
532
533   /**
534    * Sets are held in a doubly linked list (in `sets_head` and `sets_tail`).
535    */
536   struct Set *next;
537
538   /**
539    * Sets are held in a doubly linked list.
540    */
541   struct Set *prev;
542
543   /**
544    * Client that owns the set.  Only one client may own a set,
545    * and there can only be one set per client.
546    */
547   struct GNUNET_SERVICE_Client *client;
548
549   /**
550    * Message queue for the client.
551    */
552   struct GNUNET_MQ_Handle *client_mq;
553
554   /**
555    * Virtual table for this set.  Determined by the operation type of
556    * this set.
557    *
558    * Used only for Add/remove of elements and when receiving an incoming
559    * operation from a remote peer.
560    */
561   const struct SetVT *vt;
562
563   /**
564    * Implementation-specific state.
565    */
566   struct SetState *state;
567
568   /**
569    * Current state of iterating elements for the client.
570    * NULL if we are not currently iterating.
571    */
572   struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
573
574   /**
575    * Evaluate operations are held in a linked list.
576    */
577   struct Operation *ops_head;
578
579   /**
580    * Evaluate operations are held in a linked list.
581    */
582   struct Operation *ops_tail;
583
584   /**
585    * Current generation, that is, number of previously executed
586    * operations and lazy copies on the underlying set content.
587    */
588   unsigned int current_generation;
589
590   /**
591    * List of generations we have to exclude, due to lazy copies.
592    */
593   struct GenerationRange *excluded_generations;
594
595   /**
596    * Number of elements in array @a excluded_generations.
597    */
598   unsigned int excluded_generations_size;
599
600   /**
601    * Type of operation supported for this set
602    */
603   enum GNUNET_SET_OperationType operation;
604
605   /**
606    * Each @e iter is assigned a unique number, so that the client
607    * can distinguish iterations.
608    */
609   uint16_t iteration_id;
610
611   /**
612    * Generation we're currently iteration over.
613    */
614   unsigned int iter_generation;
615
616   /**
617    * Content, possibly shared by multiple sets,
618    * and thus reference counted.
619    */
620   struct SetContent *content;
621 };
622
623
624 extern struct GNUNET_STATISTICS_Handle *_GSS_statistics;
625
626
627 /**
628  * Destroy the given operation.  Call the implementation-specific
629  * cancel function of the operation.  Disconnects from the remote
630  * peer.  Does not disconnect the client, as there may be multiple
631  * operations per set.
632  *
633  * @param op operation to destroy
634  * @param gc #GNUNET_YES to perform garbage collection on the set
635  */
636 void
637 _GSS_operation_destroy (struct Operation *op,
638                         int gc);
639
640
641 /**
642  * Get the table with implementing functions for set union.
643  *
644  * @return the operation specific VTable
645  */
646 const struct SetVT *
647 _GSS_union_vt (void);
648
649
650 /**
651  * Get the table with implementing functions for set intersection.
652  *
653  * @return the operation specific VTable
654  */
655 const struct SetVT *
656 _GSS_intersection_vt (void);
657
658
659 int
660 _GSS_is_element_of_set (struct ElementEntry *ee,
661                         struct Set *set);
662
663 int
664 _GSS_is_element_of_operation (struct ElementEntry *ee,
665                               struct Operation *op);
666
667
668 #endif