Clean up and renaming
[oweals/gnunet.git] / src / include / gnunet_block_plugin.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010,2013,2017 GNUnet e.V.
4
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.
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      Affero General Public License for more details.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * API for block plugins.
26  *
27  * @defgroup block-plugin  Block plugin API
28  * To be implemented by applications storing data in the DHT.
29  *
30  * Each block plugin must conform to the API specified by this header.
31  *
32  * @{
33  */
34
35 #ifndef PLUGIN_BLOCK_H
36 #define PLUGIN_BLOCK_H
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_block_lib.h"
40
41
42 /**
43  * Mark elements as "seen" using a hash of the element. Not supported
44  * by all block plugins.
45  *
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
49  */
50 typedef void
51 (*GNUNET_BLOCK_GroupMarkSeenFunction)(struct GNUNET_BLOCK_Group *bg,
52                                       const struct
53                                       GNUNET_HashCode *seen_results,
54                                       unsigned int seen_results_count);
55
56
57 /**
58  * Merge two groups, if possible. Not supported by all block plugins,
59  * can also fail if the nonces were different.
60  *
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
64  *         we failed.
65  */
66 typedef int
67 (*GNUNET_BLOCK_GroupMergeFunction)(struct GNUNET_BLOCK_Group *bg1,
68                                    const struct GNUNET_BLOCK_Group *bg2);
69
70
71 /**
72  * Serialize state of a block group.
73  *
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
80  */
81 typedef int
82 (*GNUNET_BLOCK_GroupSerializeFunction)(struct GNUNET_BLOCK_Group *bg,
83                                        uint32_t *nonce,
84                                        void **raw_data,
85                                        size_t *raw_data_size);
86
87
88 /**
89  * Destroy resources used by a block group.
90  *
91  * @param bg group to destroy, NULL is allowed
92  */
93 typedef void
94 (*GNUNET_BLOCK_GroupDestroyFunction)(struct GNUNET_BLOCK_Group *bg);
95
96
97 /**
98  * Block group data.  The plugin must initialize the callbacks
99  * and can use the @e internal_cls as it likes.
100  */
101 struct GNUNET_BLOCK_Group
102 {
103   /**
104    * Context owning the block group. Set by the main block library.
105    */
106   struct GNUENT_BLOCK_Context *ctx;
107
108   /**
109    * Type for the block group.  Set by the main block library.
110    */
111   enum GNUNET_BLOCK_Type type;
112
113   /**
114    * Serialize the block group data, can be NULL if
115    * not supported.
116    */
117   GNUNET_BLOCK_GroupSerializeFunction serialize_cb;
118
119   /**
120    * Function to call to mark elements as seen in the group.
121    * Can be NULL if not supported.
122    */
123   GNUNET_BLOCK_GroupMarkSeenFunction mark_seen_cb;
124
125   /**
126    * Function to call to merge two groups.
127    * Can be NULL if not supported.
128    */
129   GNUNET_BLOCK_GroupMergeFunction merge_cb;
130
131   /**
132    * Function to call to destroy the block group.
133    * Must not be NULL.
134    */
135   GNUNET_BLOCK_GroupDestroyFunction destroy_cb;
136
137   /**
138    * Internal data structure of the plugin.
139    */
140   void *internal_cls;
141 };
142
143
144 /**
145  * Create a new block group.
146  *
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)
155  */
156 typedef struct GNUNET_BLOCK_Group *
157 (*GNUNET_BLOCK_GroupCreateFunction)(void *cls,
158                                     enum GNUNET_BLOCK_Type type,
159                                     uint32_t nonce,
160                                     const void *raw_data,
161                                     size_t raw_data_size,
162                                     va_list va);
163
164
165 /**
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.
171  *
172  * @param cls closure
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
183  */
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,
191                                    const void *xquery,
192                                    size_t xquery_size,
193                                    const void *reply_block,
194                                    size_t reply_block_size);
195
196
197 /**
198  * Function called to obtain the key for a block.
199  *
200  * @param cls closure
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)
209  */
210 typedef int
211 (*GNUNET_BLOCK_GetKeyFunction) (void *cls,
212                                 enum GNUNET_BLOCK_Type type,
213                                 const void *block,
214                                 size_t block_size,
215                                 struct GNUNET_HashCode *key);
216
217
218
219 /**
220  * Each plugin is required to return a pointer to a struct of this
221  * type as the return value from its entry point.
222  */
223 struct GNUNET_BLOCK_PluginFunctions
224 {
225   /**
226    * Closure for all of the callbacks.
227    */
228   void *cls;
229
230   /**
231    * 0-terminated array of block types supported by this plugin.
232    */
233   const enum GNUNET_BLOCK_Type *types;
234
235   /**
236    * Main function of a block plugin.  Allows us to check if a
237    * block matches a query.
238    */
239   GNUNET_BLOCK_EvaluationFunction evaluate;
240
241   /**
242    * Obtain the key for a given block (if possible).
243    */
244   GNUNET_BLOCK_GetKeyFunction get_key;
245
246   /**
247    * Create a block group to process a bunch of blocks in a shared
248    * context (i.e. to detect duplicates).
249    */
250   GNUNET_BLOCK_GroupCreateFunction create_group;
251 };
252
253 #endif
254
255 /** @} */  /* end of group */