2 This file is part of GNUnet
3 Copyright (C) 2010,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
22 * @author Christian Grothoff
25 * API for block plugins.
27 * @defgroup block-plugin Block plugin API
28 * To be implemented by applications storing data in the DHT.
30 * Each block plugin must conform to the API specified by this header.
35 #ifndef PLUGIN_BLOCK_H
36 #define PLUGIN_BLOCK_H
38 #include "gnunet_util_lib.h"
39 #include "gnunet_block_lib.h"
43 * Mark elements as "seen" using a hash of the element. Not supported
44 * by all block plugins.
46 * @param bg group to update
47 * @param seen_results results already seen
48 * @param seen_results_count number of entries in @a seen_results
51 (*GNUNET_BLOCK_GroupMarkSeenFunction)(struct GNUNET_BLOCK_Group *bg,
53 GNUNET_HashCode *seen_results,
54 unsigned int seen_results_count);
58 * Merge two groups, if possible. Not supported by all block plugins,
59 * can also fail if the nonces were different.
61 * @param bg1 group to update
62 * @param bg2 group to merge into @a bg1
63 * @return #GNUNET_OK on success, #GNUNET_NO if the nonces were different and thus
67 (*GNUNET_BLOCK_GroupMergeFunction)(struct GNUNET_BLOCK_Group *bg1,
68 const struct GNUNET_BLOCK_Group *bg2);
72 * Serialize state of a block group.
74 * @param bg group to serialize
75 * @param[out] nonce set to the nonce of the @a bg
76 * @param[out] raw_data set to the serialized state
77 * @param[out] raw_data_size set to the number of bytes in @a raw_data
78 * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
79 * supported, #GNUNET_SYSERR on error
82 (*GNUNET_BLOCK_GroupSerializeFunction)(struct GNUNET_BLOCK_Group *bg,
85 size_t *raw_data_size);
89 * Destroy resources used by a block group.
91 * @param bg group to destroy, NULL is allowed
94 (*GNUNET_BLOCK_GroupDestroyFunction)(struct GNUNET_BLOCK_Group *bg);
98 * Block group data. The plugin must initialize the callbacks
99 * and can use the @e internal_cls as it likes.
101 struct GNUNET_BLOCK_Group
104 * Context owning the block group. Set by the main block library.
106 struct GNUENT_BLOCK_Context *ctx;
109 * Type for the block group. Set by the main block library.
111 enum GNUNET_BLOCK_Type type;
114 * Serialize the block group data, can be NULL if
117 GNUNET_BLOCK_GroupSerializeFunction serialize_cb;
120 * Function to call to mark elements as seen in the group.
121 * Can be NULL if not supported.
123 GNUNET_BLOCK_GroupMarkSeenFunction mark_seen_cb;
126 * Function to call to merge two groups.
127 * Can be NULL if not supported.
129 GNUNET_BLOCK_GroupMergeFunction merge_cb;
132 * Function to call to destroy the block group.
135 GNUNET_BLOCK_GroupDestroyFunction destroy_cb;
138 * Internal data structure of the plugin.
145 * Create a new block group.
147 * @param ctx block context in which the block group is created
148 * @param type type of the block for which we are creating the group
149 * @param nonce random value used to seed the group creation
150 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
151 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
152 * @param va variable arguments specific to @a type
153 * @return block group handle, NULL if block groups are not supported
154 * by this @a type of block (this is not an error)
156 typedef struct GNUNET_BLOCK_Group *
157 (*GNUNET_BLOCK_GroupCreateFunction)(void *cls,
158 enum GNUNET_BLOCK_Type type,
160 const void *raw_data,
161 size_t raw_data_size,
166 * Function called to validate a reply or a request. For
167 * request evaluation, simply pass "NULL" for the @a reply_block.
168 * Note that it is assumed that the reply has already been
169 * matched to the key (and signatures checked) as it would
170 * be done with the "get_key" function.
173 * @param ctx block context
174 * @param type block type
175 * @param group which block group to use for evaluation
176 * @param eo evaluation options to control evaluation
177 * @param query original query (hash)
178 * @param xquery extrended query data (can be NULL, depending on type)
179 * @param xquery_size number of bytes in @a xquery
180 * @param reply_block response to validate
181 * @param reply_block_size number of bytes in @a reply_block
182 * @return characterization of result
184 typedef enum GNUNET_BLOCK_EvaluationResult
185 (*GNUNET_BLOCK_EvaluationFunction)(void *cls,
186 struct GNUNET_BLOCK_Context *ctx,
187 enum GNUNET_BLOCK_Type type,
188 struct GNUNET_BLOCK_Group *group,
189 enum GNUNET_BLOCK_EvaluationOptions eo,
190 const struct GNUNET_HashCode *query,
193 const void *reply_block,
194 size_t reply_block_size);
198 * Function called to obtain the key for a block.
201 * @param type block type
202 * @param block block to get the key for
203 * @param block_size number of bytes in @a block
204 * @param key set to the key (query) for the given block
205 * @return #GNUNET_YES on success,
206 * #GNUNET_NO if the block is malformed
207 * #GNUNET_SYSERR if type not supported
208 * (or if extracting a key from a block of this type does not work)
211 (*GNUNET_BLOCK_GetKeyFunction) (void *cls,
212 enum GNUNET_BLOCK_Type type,
215 struct GNUNET_HashCode *key);
219 * Each plugin is required to return a pointer to a struct of this
220 * type as the return value from its entry point.
222 struct GNUNET_BLOCK_PluginFunctions
225 * Closure for all of the callbacks.
230 * 0-terminated array of block types supported by this plugin.
232 const enum GNUNET_BLOCK_Type *types;
235 * Main function of a block plugin. Allows us to check if a
236 * block matches a query.
238 GNUNET_BLOCK_EvaluationFunction evaluate;
241 * Obtain the key for a given block (if possible).
243 GNUNET_BLOCK_GetKeyFunction get_key;
246 * Create a block group to process a bunch of blocks in a shared
247 * context (i.e. to detect duplicates).
249 GNUNET_BLOCK_GroupCreateFunction create_group;
254 /** @} */ /* end of group */