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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 * Signature of functions that create the implementation-specific
72 * state for a set supporting a specific operation.
74 * @return a set state specific to the supported operation, NULL on error
76 typedef struct SetState *
77 (*SetCreateImpl) (void);
81 * Signature of functions that implement the add/remove functionality
82 * for a set supporting a specific operation.
84 * @param set implementation-specific set state
85 * @param ee element message from the client
88 (*SetAddRemoveImpl) (struct SetState *state,
89 struct ElementEntry *ee);
93 * Make a copy of a set's internal state.
95 * @param state set state to copy
96 * @return copy of the internal state
98 typedef struct SetState *
99 (*SetCopyStateImpl) (struct SetState *state);
103 * Signature of functions that implement the destruction of the
104 * implementation-specific set state.
106 * @param state the set state, contains implementation-specific data
109 (*SetDestroyImpl) (struct SetState *state);
113 * Signature of functions that implement accepting a set operation.
115 * @param op operation that is created by accepting the operation,
116 * should be initialized by the implementation
117 * @return operation-specific state to keep in @a op
119 typedef struct OperationState *
120 (*OpAcceptImpl) (struct Operation *op);
124 * Signature of functions that implement starting the evaluation of
127 * @param op operation that is created, should be initialized to
128 * begin the evaluation
129 * @param opaque_context message to be transmitted to the listener
130 * to convince it to accept, may be NULL
131 * @return operation-specific state to keep in @a op
133 typedef struct OperationState *
134 (*OpEvaluateImpl) (struct Operation *op,
135 const struct GNUNET_MessageHeader *opaque_context);
138 * Signature of functions that implement operation cancelation.
139 * This includes notifying the client about the operation's final
142 * @param op operation state
145 (*OpCancelImpl) (struct Operation *op);
149 * Signature of functions called when the CADET channel died.
151 * @param op operation state
154 (*OpChannelDeathImpl) (struct Operation *op);
159 * Dispatch table for a specific set operation. Every set operation
160 * has to implement the callback in this struct.
164 * Callback for the set creation.
166 SetCreateImpl create;
169 * Callback for element insertion
171 SetAddRemoveImpl add;
174 * Callback for element removal.
176 SetAddRemoveImpl remove;
179 * Callback for making a copy of a set's internal state.
181 SetCopyStateImpl copy_state;
184 * Callback for destruction of the set state.
186 SetDestroyImpl destroy_set;
189 * Callback for accepting a set operation request
194 * Callback for starting evaluation with a remote peer.
196 OpEvaluateImpl evaluate;
199 * Callback for canceling an operation.
204 * Callback called in case the CADET channel died.
206 OpChannelDeathImpl channel_death;
211 * MutationEvent gives information about changes
212 * to an element (removal / addition) in a set content.
214 struct MutationEvent {
216 * First generation affected by this mutation event.
218 * If @a generation is 0, this mutation event is a list
221 unsigned int generation;
224 * If @a added is #GNUNET_YES, then this is a
225 * `remove` event, otherwise it is an `add` event.
232 * Information about an element element in the set. All elements are
233 * stored in a hash-table from their hash-code to their `struct
234 * Element`, so that the remove and add operations are reasonably
237 struct ElementEntry {
239 * The actual element. The data for the element
240 * should be allocated at the end of this struct.
242 struct GNUNET_SET_Element element;
245 * Hash of the element. For set union: Will be used to derive the
246 * different IBF keys for different salts.
248 struct GNUNET_HashCode element_hash;
251 * If @a mutations is not NULL, it contains
252 * a list of mutations, ordered by increasing generation.
253 * The list is terminated by a sentinel event with `generation`
256 * If @a mutations is NULL, then this element exists in all generations
257 * of the respective set content this element belongs to.
259 struct MutationEvent *mutations;
262 * Number of elements in the array @a mutations.
264 unsigned int mutations_size;
267 * #GNUNET_YES if the element is a remote element, and does not belong
268 * to the operation's set.
275 * A listener is inhabited by a client, and waits for evaluation
276 * requests from remote peers.
282 * 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;
308 * Operation context used to execute a set operation.
312 * Kept in a DLL of the listener, if @e listener is non-NULL.
314 struct Operation *next;
317 * Kept in a DLL of the listener, if @e listener is non-NULL.
319 struct Operation *prev;
322 * Channel to the peer.
324 struct GNUNET_CADET_Channel *channel;
327 * Port this operation runs on.
329 struct Listener *listener;
332 * Message queue for the channel.
334 struct GNUNET_MQ_Handle *mq;
337 * Context message, may be NULL.
339 struct GNUNET_MessageHeader *context_msg;
342 * Set associated with the operation, NULL until the spec has been
343 * associated with a set.
348 * Operation-specific operation state. Note that the exact
349 * type depends on this being a union or intersection operation
350 * (and thus on @e vt).
352 struct OperationState *state;
355 * The identity of the requesting peer. Needs to
356 * be stored here as the op spec might not have been created yet.
358 struct GNUNET_PeerIdentity peer;
361 * Timeout task, if the incoming peer has not been accepted
362 * after the timeout, it will be disconnected.
364 struct GNUNET_SCHEDULER_Task *timeout_task;
367 * Salt to use for the operation.
372 * Remote peers element count
374 uint32_t remote_element_count;
377 * ID used to identify an operation between service and client
379 uint32_t client_request_id;
382 * When are elements sent to the client, and which elements are sent?
384 enum GNUNET_SET_ResultMode result_mode;
387 * Always use delta operation instead of sending full sets,
388 * even it it's less efficient.
393 * Always send full sets, even if delta operations would
399 * #GNUNET_YES to fail operations where Byzantine faults
405 * Lower bound for the set size, used only when
406 * byzantine mode is enabled.
408 int byzantine_lower_bound;
411 * Unique request id for the request from a remote peer, sent to the
412 * client, which will accept or reject the request. Set to '0' iff
413 * the request has not been suggested yet.
418 * Generation in which the operation handle
421 unsigned int generation_created;
426 * SetContent stores the actual set elements, which may be shared by
427 * multiple generations derived from one set.
431 * Maps `struct GNUNET_HashCode *` to `struct ElementEntry *`.
433 struct GNUNET_CONTAINER_MultiHashMap *elements;
436 * Mutations requested by the client that we're
437 * unable to execute right now because we're iterating
438 * over the underlying hash map of elements.
440 struct PendingMutation *pending_mutations_head;
443 * Mutations requested by the client that we're
444 * unable to execute right now because we're iterating
445 * over the underlying hash map of elements.
447 struct PendingMutation *pending_mutations_tail;
450 * Number of references to the content.
452 unsigned int refcount;
457 unsigned int latest_generation;
460 * Number of concurrently active iterators.
466 struct GenerationRange {
468 * First generation that is excluded.
473 * Generation after the last excluded generation.
480 * Information about a mutation to apply to a set.
482 struct PendingMutation {
484 * Mutations are kept in a DLL.
486 struct PendingMutation *prev;
489 * Mutations are kept in a DLL.
491 struct PendingMutation *next;
494 * Set this mutation is about.
499 * Message that describes the desired mutation.
500 * May only be a #GNUNET_MESSAGE_TYPE_SET_ADD or
501 * #GNUNET_MESSAGE_TYPE_SET_REMOVE.
503 struct GNUNET_SET_ElementMessage *msg;
508 * A set that supports a specific operation with other peers.
512 * Sets are held in a doubly linked list (in `sets_head` and `sets_tail`).
517 * Sets are held in a doubly linked list.
522 * Client that owns the set. Only one client may own a set,
523 * and there can only be one set per client.
525 struct ClientState *cs;
528 * Content, possibly shared by multiple sets,
529 * and thus reference counted.
531 struct SetContent *content;
534 * Virtual table for this set. Determined by the operation type of
537 * Used only for Add/remove of elements and when receiving an incoming
538 * operation from a remote peer.
540 const struct SetVT *vt;
543 * Implementation-specific state.
545 struct SetState *state;
548 * Current state of iterating elements for the client.
549 * NULL if we are not currently iterating.
551 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
554 * Evaluate operations are held in a linked list.
556 struct Operation *ops_head;
559 * Evaluate operations are held in a linked list.
561 struct Operation *ops_tail;
564 * List of generations we have to exclude, due to lazy copies.
566 struct GenerationRange *excluded_generations;
569 * Current generation, that is, number of previously executed
570 * operations and lazy copies on the underlying set content.
572 unsigned int current_generation;
575 * Number of elements in array @a excluded_generations.
577 unsigned int excluded_generations_size;
580 * Type of operation supported for this set
582 enum GNUNET_SET_OperationType operation;
585 * Generation we're currently iteration over.
587 unsigned int iter_generation;
590 * Each @e iter is assigned a unique number, so that the client
591 * can distinguish iterations.
593 uint16_t iteration_id;
597 extern struct GNUNET_STATISTICS_Handle *_GSS_statistics;
601 * Destroy the given operation. Used for any operation where both
602 * peers were known and that thus actually had a vt and channel. Must
603 * not be used for operations where 'listener' is still set and we do
604 * not know the other peer.
606 * Call the implementation-specific cancel function of the operation.
607 * Disconnects from the remote peer. Does not disconnect the client,
608 * as there may be multiple operations per set.
610 * @param op operation to destroy
611 * @param gc #GNUNET_YES to perform garbage collection on the set
614 _GSS_operation_destroy(struct Operation *op,
619 * This function probably should not exist
620 * and be replaced by inlining more specific
621 * logic in the various places where it is called.
624 _GSS_operation_destroy2(struct Operation *op);
628 * Get the table with implementing functions for set union.
630 * @return the operation specific VTable
637 * Get the table with implementing functions for set intersection.
639 * @return the operation specific VTable
642 _GSS_intersection_vt(void);
646 * Is element @a ee part of the set used by @a op?
648 * @param ee element to test
649 * @param op operation the defines the set and its generation
650 * @return #GNUNET_YES if the element is in the set, #GNUNET_NO if not
653 _GSS_is_element_of_operation(struct ElementEntry *ee,
654 struct Operation *op);