2 This file is part of GNUnet
3 Copyright (C) 2013-2017 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.
16 * @file set/gnunet-service-set.h
17 * @brief common components for the implementation the different set operations
18 * @author Florian Dold
19 * @author Christian Grothoff
21 #ifndef GNUNET_SERVICE_SET_H_PRIVATE
22 #define GNUNET_SERVICE_SET_H_PRIVATE
25 #include "gnunet_util_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_applications.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_cadet_service.h"
30 #include "gnunet_set_service.h"
35 * Implementation-specific set state. Used as opaque pointer, and
36 * specified further in the respective implementation.
41 * Implementation-specific set operation. Used as opaque pointer, and
42 * specified further in the respective implementation.
44 struct OperationState;
47 * A set that supports a specific operation with other peers.
52 * Information about an element element in the set. All elements are
53 * stored in a hash-table from their hash-code to their 'struct
54 * Element', so that the remove and add operations are reasonably
60 * Operation context used to execute a set operation.
66 * Signature of functions that create the implementation-specific
67 * state for a set supporting a specific operation.
69 * @return a set state specific to the supported operation, NULL on error
71 typedef struct SetState *
72 (*SetCreateImpl) (void);
76 * Signature of functions that implement the add/remove functionality
77 * for a set supporting a specific operation.
79 * @param set implementation-specific set state
80 * @param ee element message from the client
83 (*SetAddRemoveImpl) (struct SetState *state,
84 struct ElementEntry *ee);
88 * Make a copy of a set's internal state.
90 * @param state set state to copy
91 * @return copy of the internal state
93 typedef struct SetState *
94 (*SetCopyStateImpl) (struct SetState *state);
98 * Signature of functions that implement the destruction of the
99 * implementation-specific set state.
101 * @param state the set state, contains implementation-specific data
104 (*SetDestroyImpl) (struct SetState *state);
108 * Signature of functions that implement accepting a set operation.
110 * @param op operation that is created by accepting the operation,
111 * should be initialized by the implementation
112 * @return operation-specific state to keep in @a op
114 typedef struct OperationState *
115 (*OpAcceptImpl) (struct Operation *op);
119 * Signature of functions that implement starting the evaluation of
122 * @param op operation that is created, should be initialized to
123 * begin the evaluation
124 * @param opaque_context message to be transmitted to the listener
125 * to convince him to accept, may be NULL
126 * @return operation-specific state to keep in @a op
128 typedef struct OperationState *
129 (*OpEvaluateImpl) (struct Operation *op,
130 const struct GNUNET_MessageHeader *opaque_context);
133 * Signature of functions that implement operation cancelation.
134 * This includes notifying the client about the operation's final
137 * @param op operation state
140 (*OpCancelImpl) (struct Operation *op);
144 * Signature of functions called when the CADET channel died.
146 * @param op operation state
149 (*OpChannelDeathImpl) (struct Operation *op);
154 * Dispatch table for a specific set operation. Every set operation
155 * has to implement the callback in this struct.
160 * Callback for the set creation.
162 SetCreateImpl create;
165 * Callback for element insertion
167 SetAddRemoveImpl add;
170 * Callback for element removal.
172 SetAddRemoveImpl remove;
175 * Callback for making a copy of a set's internal state.
177 SetCopyStateImpl copy_state;
180 * Callback for destruction of the set state.
182 SetDestroyImpl destroy_set;
185 * Callback for accepting a set operation request
190 * Callback for starting evaluation with a remote peer.
192 OpEvaluateImpl evaluate;
195 * Callback for canceling an operation.
200 * Callback called in case the CADET channel died.
202 OpChannelDeathImpl channel_death;
208 * MutationEvent gives information about changes
209 * to an element (removal / addition) in a set content.
214 * First generation affected by this mutation event.
216 * If @a generation is 0, this mutation event is a list
219 unsigned int generation;
222 * If @a added is #GNUNET_YES, then this is a
223 * `remove` event, otherwise it is an `add` event.
230 * Information about an element element in the set. All elements are
231 * stored in a hash-table from their hash-code to their `struct
232 * Element`, so that the remove and add operations are reasonably
238 * The actual element. The data for the element
239 * should be allocated at the end of this struct.
241 struct GNUNET_SET_Element element;
244 * Hash of the element. For set union: Will be used to derive the
245 * different IBF keys for different salts.
247 struct GNUNET_HashCode element_hash;
250 * If @a mutations is not NULL, it contains
251 * a list of mutations, ordered by increasing generation.
252 * The list is terminated by a sentinel event with `generation`
255 * If @a mutations is NULL, then this element exists in all generations
256 * of the respective set content this element belongs to.
258 struct MutationEvent *mutations;
261 * Number of elements in the array @a mutations.
263 unsigned int mutations_size;
266 * #GNUNET_YES if the element is a remote element, and does not belong
267 * to the operation's set.
274 * A listener is inhabited by a client, and waits for evaluation
275 * requests from remote peers.
281 * State we keep per client.
286 * Set, if associated with the client, otherwise NULL.
291 * Listener, if associated with the client, otherwise NULL.
293 struct Listener *listener;
298 struct GNUNET_SERVICE_Client *client;
303 struct GNUNET_MQ_Handle *mq;
309 * Operation context used to execute a set operation.
315 * Kept in a DLL of the listener, if @e listener is non-NULL.
317 struct Operation *next;
320 * Kept in a DLL of the listener, if @e listener is non-NULL.
322 struct Operation *prev;
325 * Channel to the peer.
327 struct GNUNET_CADET_Channel *channel;
330 * Port this operation runs on.
332 struct Listener *listener;
335 * Message queue for the channel.
337 struct GNUNET_MQ_Handle *mq;
340 * Context message, may be NULL.
342 struct GNUNET_MessageHeader *context_msg;
345 * Set associated with the operation, NULL until the spec has been
346 * associated with a set.
351 * Operation-specific operation state. Note that the exact
352 * type depends on this being a union or intersection operation
353 * (and thus on @e vt).
355 struct OperationState *state;
358 * The identity of the requesting peer. Needs to
359 * be stored here as the op spec might not have been created yet.
361 struct GNUNET_PeerIdentity peer;
364 * Timeout task, if the incoming peer has not been accepted
365 * after the timeout, it will be disconnected.
367 struct GNUNET_SCHEDULER_Task *timeout_task;
370 * Salt to use for the operation.
375 * Remote peers element count
377 uint32_t remote_element_count;
380 * ID used to identify an operation between service and client
382 uint32_t client_request_id;
385 * When are elements sent to the client, and which elements are sent?
387 enum GNUNET_SET_ResultMode result_mode;
390 * Always use delta operation instead of sending full sets,
391 * even it it's less efficient.
396 * Always send full sets, even if delta operations would
402 * #GNUNET_YES to fail operations where Byzantine faults
408 * Lower bound for the set size, used only when
409 * byzantine mode is enabled.
411 int byzantine_lower_bound;
414 * Unique request id for the request from a remote peer, sent to the
415 * client, which will accept or reject the request. Set to '0' iff
416 * the request has not been suggested yet.
421 * Generation in which the operation handle
424 unsigned int generation_created;
430 * SetContent stores the actual set elements, which may be shared by
431 * multiple generations derived from one set.
437 * Maps `struct GNUNET_HashCode *` to `struct ElementEntry *`.
439 struct GNUNET_CONTAINER_MultiHashMap *elements;
442 * Mutations requested by the client that we're
443 * unable to execute right now because we're iterating
444 * over the underlying hash map of elements.
446 struct PendingMutation *pending_mutations_head;
449 * Mutations requested by the client that we're
450 * unable to execute right now because we're iterating
451 * over the underlying hash map of elements.
453 struct PendingMutation *pending_mutations_tail;
456 * Number of references to the content.
458 unsigned int refcount;
463 unsigned int latest_generation;
466 * Number of concurrently active iterators.
472 struct GenerationRange
475 * First generation that is excluded.
480 * Generation after the last excluded generation.
487 * Information about a mutation to apply to a set.
489 struct PendingMutation
492 * Mutations are kept in a DLL.
494 struct PendingMutation *prev;
497 * Mutations are kept in a DLL.
499 struct PendingMutation *next;
502 * Set this mutation is about.
507 * Message that describes the desired mutation.
508 * May only be a #GNUNET_MESSAGE_TYPE_SET_ADD or
509 * #GNUNET_MESSAGE_TYPE_SET_REMOVE.
511 struct GNUNET_SET_ElementMessage *msg;
516 * A set that supports a specific operation with other peers.
522 * Sets are held in a doubly linked list (in `sets_head` and `sets_tail`).
527 * Sets are held in a doubly linked list.
532 * Client that owns the set. Only one client may own a set,
533 * and there can only be one set per client.
535 struct ClientState *cs;
538 * Content, possibly shared by multiple sets,
539 * and thus reference counted.
541 struct SetContent *content;
544 * Virtual table for this set. Determined by the operation type of
547 * Used only for Add/remove of elements and when receiving an incoming
548 * operation from a remote peer.
550 const struct SetVT *vt;
553 * Implementation-specific state.
555 struct SetState *state;
558 * Current state of iterating elements for the client.
559 * NULL if we are not currently iterating.
561 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
564 * Evaluate operations are held in a linked list.
566 struct Operation *ops_head;
569 * Evaluate operations are held in a linked list.
571 struct Operation *ops_tail;
574 * List of generations we have to exclude, due to lazy copies.
576 struct GenerationRange *excluded_generations;
579 * Current generation, that is, number of previously executed
580 * operations and lazy copies on the underlying set content.
582 unsigned int current_generation;
585 * Number of elements in array @a excluded_generations.
587 unsigned int excluded_generations_size;
590 * Type of operation supported for this set
592 enum GNUNET_SET_OperationType operation;
595 * Generation we're currently iteration over.
597 unsigned int iter_generation;
600 * Each @e iter is assigned a unique number, so that the client
601 * can distinguish iterations.
603 uint16_t iteration_id;
608 extern struct GNUNET_STATISTICS_Handle *_GSS_statistics;
612 * Destroy the given operation. Used for any operation where both
613 * peers were known and that thus actually had a vt and channel. Must
614 * not be used for operations where 'listener' is still set and we do
615 * not know the other peer.
617 * Call the implementation-specific cancel function of the operation.
618 * Disconnects from the remote peer. Does not disconnect the client,
619 * as there may be multiple operations per set.
621 * @param op operation to destroy
622 * @param gc #GNUNET_YES to perform garbage collection on the set
625 _GSS_operation_destroy (struct Operation *op,
630 * Get the table with implementing functions for set union.
632 * @return the operation specific VTable
635 _GSS_union_vt (void);
639 * Get the table with implementing functions for set intersection.
641 * @return the operation specific VTable
644 _GSS_intersection_vt (void);
648 * Is element @a ee part of the set used by @a op?
650 * @param ee element to test
651 * @param op operation the defines the set and its generation
652 * @return #GNUNET_YES if the element is in the set, #GNUNET_NO if not
655 _GSS_is_element_of_operation (struct ElementEntry *ee,
656 struct Operation *op);