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
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.
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.
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.
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
26 #ifndef GNUNET_SERVICE_SET_H_PRIVATE
27 #define GNUNET_SERVICE_SET_H_PRIVATE
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"
40 * Implementation-specific set state. Used as opaque pointer, and
41 * specified further in the respective implementation.
46 * Implementation-specific set operation. Used as opaque pointer, and
47 * specified further in the respective implementation.
49 struct OperationState;
52 * A set that supports a specific operation with other peers.
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
65 * Operation context used to execute a set operation.
71 * Detail information about an operation.
73 struct OperationSpecification
77 * The remove peer we evaluate the operation with.
79 struct GNUNET_PeerIdentity peer;
82 * Application ID for the operation, used to distinguish
83 * multiple operations of the same type with the same peer.
85 struct GNUNET_HashCode app_id;
88 * Context message, may be NULL.
90 struct GNUNET_MessageHeader *context_msg;
93 * Set associated with the operation, NULL until the spec has been
94 * associated with a set.
99 * Salt to use for the operation.
104 * Remote peers element count
106 uint32_t remote_element_count;
109 * ID used to identify an operation between service and client
111 uint32_t client_request_id;
114 * The type of the operation.
116 enum GNUNET_SET_OperationType operation;
119 * When are elements sent to the client, and which elements are sent?
121 enum GNUNET_SET_ResultMode result_mode;
124 * Always use delta operation instead of sending full sets,
125 * even it it's less efficient.
130 * Always send full sets, even if delta operations would
136 * #GNUNET_YES to fail operations where Byzantine faults
142 * Lower bound for the set size, used only when
143 * byzantine mode is enabled.
145 int byzantine_lower_bound;
150 * Signature of functions that create the implementation-specific
151 * state for a set supporting a specific operation.
153 * @return a set state specific to the supported operation, NULL on error
155 typedef struct SetState *
156 (*CreateImpl) (void);
160 * Signature of functions that implement the add/remove functionality
161 * for a set supporting a specific operation.
163 * @param set implementation-specific set state
164 * @param ee element message from the client
167 (*AddRemoveImpl) (struct SetState *state,
168 struct ElementEntry *ee);
172 * Signature of functions that handle disconnection of the remote
175 * @param op the set operation, contains implementation-specific data
178 (*PeerDisconnectImpl) (struct Operation *op);
182 * Signature of functions that implement the destruction of the
183 * implementation-specific set state.
185 * @param state the set state, contains implementation-specific data
188 (*DestroySetImpl) (struct SetState *state);
192 * Signature of functions that implement accepting a set operation.
194 * @param op operation that is created by accepting the operation,
195 * should be initialized by the implementation
198 (*OpAcceptImpl) (struct Operation *op);
202 * Signature of functions that implement starting the evaluation of
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
211 (*OpEvaluateImpl) (struct Operation *op,
212 const struct GNUNET_MessageHeader *opaque_context);
216 * Signature of functions that implement operation cancellation
218 * @param op operation state
221 (*CancelImpl) (struct Operation *op);
224 typedef struct SetState *
225 (*CopyStateImpl) (struct Set *op);
229 * Dispatch table for a specific set operation. Every set operation
230 * has to implement the callback in this struct.
235 * Callback for the set creation.
240 * Callback for element insertion
245 * Callback for element removal.
247 AddRemoveImpl remove;
250 * Callback for accepting a set operation request
255 * Callback for starting evaluation with a remote peer.
257 OpEvaluateImpl evaluate;
260 * Callback for destruction of the set state.
262 DestroySetImpl destroy_set;
265 * Callback for handling the remote peer's disconnect.
267 PeerDisconnectImpl peer_disconnect;
270 * Callback for canceling an operation by its ID.
274 CopyStateImpl copy_state;
279 * MutationEvent gives information about changes
280 * to an element (removal / addition) in a set content.
285 * First generation affected by this mutation event.
287 * If @a generation is 0, this mutation event is a list
290 unsigned int generation;
293 * If @a added is #GNUNET_YES, then this is a
294 * `remove` event, otherwise it is an `add` event.
301 * Information about an element element in the set. All elements are
302 * stored in a hash-table from their hash-code to their `struct
303 * Element`, so that the remove and add operations are reasonably
309 * The actual element. The data for the element
310 * should be allocated at the end of this struct.
312 struct GNUNET_SET_Element element;
315 * Hash of the element. For set union: Will be used to derive the
316 * different IBF keys for different salts.
318 struct GNUNET_HashCode element_hash;
321 * If @a mutations is not NULL, it contains
322 * a list of mutations, ordered by increasing generation.
323 * The list is terminated by a sentinel event with `generation`
326 * If @a mutations is NULL, then this element exists in all generations
327 * of the respective set content this element belongs to.
329 struct MutationEvent *mutations;
332 * Number of elements in the array @a mutations.
334 unsigned int mutations_size;
337 * #GNUNET_YES if the element is a remote element, and does not belong
338 * to the operation's set.
348 * Operation context used to execute a set operation.
353 * V-Table for the operation belonging to the tunnel contest.
355 * Used for all operation specific operations after receiving the ops request
357 const struct SetVT *vt;
360 * Channel to the peer.
362 struct GNUNET_CADET_Channel *channel;
365 * Port this operation runs on.
367 struct Listener *listener;
370 * Message queue for the channel.
372 struct GNUNET_MQ_Handle *mq;
375 * Detail information about the set operation, including the set to
376 * use. When 'spec' is NULL, the operation is not yet entirely
379 struct OperationSpecification *spec;
382 * Operation-specific operation state. Note that the exact
383 * type depends on this being a union or intersection operation
384 * (and thus on @e vt).
386 struct OperationState *state;
389 * Evaluate operations are held in a linked list.
391 struct Operation *next;
394 * Evaluate operations are held in a linked list.
396 struct Operation *prev;
399 * The identity of the requesting peer. Needs to
400 * be stored here as the op spec might not have been created yet.
402 struct GNUNET_PeerIdentity peer;
405 * Timeout task, if the incoming peer has not been accepted
406 * after the timeout, it will be disconnected.
408 struct GNUNET_SCHEDULER_Task *timeout_task;
411 * The type of the operation.
413 enum GNUNET_SET_OperationType operation;
416 * Unique request id for the request from a remote peer, sent to the
417 * client, which will accept or reject the request. Set to '0' iff
418 * the request has not been suggested yet.
423 * #GNUNET_YES if this is not a "real" set operation yet, and we still
424 * need to wait for the other peer to give us more details.
429 * Generation in which the operation handle
432 unsigned int generation_created;
435 * Incremented whenever (during shutdown) some component still
436 * needs to do something with this before the operation is freed.
437 * (Used as a reference counter, but only during termination.)
444 * SetContent stores the actual set elements,
445 * which may be shared by multiple generations derived
451 * Number of references to the content.
453 unsigned int refcount;
456 * Maps `struct GNUNET_HashCode *` to `struct ElementEntry *`.
458 struct GNUNET_CONTAINER_MultiHashMap *elements;
460 unsigned int latest_generation;
463 * Mutations requested by the client that we're
464 * unable to execute right now because we're iterating
465 * over the underlying hash map of elements.
467 struct PendingMutation *pending_mutations_head;
470 * Mutations requested by the client that we're
471 * unable to execute right now because we're iterating
472 * over the underlying hash map of elements.
474 struct PendingMutation *pending_mutations_tail;
477 * Number of concurrently active iterators.
483 struct GenerationRange
486 * First generation that is excluded.
491 * Generation after the last excluded generation.
497 struct PendingMutation
499 struct PendingMutation *prev;
500 struct PendingMutation *next;
505 * Message that describes the desired mutation.
506 * May only be a #GNUNET_MESSAGE_TYPE_SET_ADD or
507 * #GNUNET_MESSAGE_TYPE_SET_REMOVE.
509 struct GNUNET_MessageHeader *mutation_message;
514 * A set that supports a specific operation with other peers.
520 * Sets are held in a doubly linked list (in `sets_head` and `sets_tail`).
525 * Sets are held in a doubly linked list.
530 * Client that owns the set. Only one client may own a set,
531 * and there can only be one set per client.
533 struct GNUNET_SERVICE_Client *client;
536 * Message queue for the client.
538 struct GNUNET_MQ_Handle *client_mq;
541 * Virtual table for this set. Determined by the operation type of
544 * Used only for Add/remove of elements and when receiving an incoming
545 * operation from a remote peer.
547 const struct SetVT *vt;
550 * Implementation-specific state.
552 struct SetState *state;
555 * Current state of iterating elements for the client.
556 * NULL if we are not currently iterating.
558 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
561 * Evaluate operations are held in a linked list.
563 struct Operation *ops_head;
566 * Evaluate operations are held in a linked list.
568 struct Operation *ops_tail;
571 * Current generation, that is, number of previously executed
572 * operations and lazy copies on the underlying set content.
574 unsigned int current_generation;
577 * List of generations we have to exclude, due to lazy copies.
579 struct GenerationRange *excluded_generations;
582 * Number of elements in array @a excluded_generations.
584 unsigned int excluded_generations_size;
587 * Type of operation supported for this set
589 enum GNUNET_SET_OperationType operation;
592 * Each @e iter is assigned a unique number, so that the client
593 * can distinguish iterations.
595 uint16_t iteration_id;
598 * Generation we're currently iteration over.
600 unsigned int iter_generation;
603 * Content, possibly shared by multiple sets,
604 * and thus reference counted.
606 struct SetContent *content;
610 extern struct GNUNET_STATISTICS_Handle *_GSS_statistics;
614 * Destroy the given operation. Call the implementation-specific
615 * cancel function of the operation. Disconnects from the remote
616 * peer. Does not disconnect the client, as there may be multiple
617 * operations per set.
619 * @param op operation to destroy
620 * @param gc #GNUNET_YES to perform garbage collection on the set
623 _GSS_operation_destroy (struct Operation *op,
628 * Get the table with implementing functions for set union.
630 * @return the operation specific VTable
633 _GSS_union_vt (void);
637 * Get the table with implementing functions for set intersection.
639 * @return the operation specific VTable
642 _GSS_intersection_vt (void);
646 _GSS_is_element_of_set (struct ElementEntry *ee,
650 _GSS_is_element_of_operation (struct ElementEntry *ee,
651 struct Operation *op);