Merge branch 'master' of gnunet.org:gnunet
[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 GNUNET_HashCode *seen_results,
53                                       unsigned int seen_results_count);
54
55
56 /**
57  * Merge two groups, if possible. Not supported by all block plugins,
58  * can also fail if the nonces were different.
59  *
60  * @param bg1 group to update
61  * @param bg2 group to merge into @a bg1
62  * @return #GNUNET_OK on success, #GNUNET_NO if the nonces were different and thus
63  *         we failed.
64  */
65 typedef int
66 (*GNUNET_BLOCK_GroupMergeFunction)(struct GNUNET_BLOCK_Group *bg1,
67                                    const struct GNUNET_BLOCK_Group *bg2);
68
69
70 /**
71  * Serialize state of a block group.
72  *
73  * @param bg group to serialize
74  * @param[out] nonce set to the nonce of the @a bg
75  * @param[out] raw_data set to the serialized state
76  * @param[out] raw_data_size set to the number of bytes in @a raw_data
77  * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
78  *         supported, #GNUNET_SYSERR on error
79  */
80 typedef int
81 (*GNUNET_BLOCK_GroupSerializeFunction)(struct GNUNET_BLOCK_Group *bg,
82                                        uint32_t *nonce,
83                                        void **raw_data,
84                                        size_t *raw_data_size);
85
86
87 /**
88  * Destroy resources used by a block group.
89  *
90  * @param bg group to destroy, NULL is allowed
91  */
92 typedef void
93 (*GNUNET_BLOCK_GroupDestroyFunction)(struct GNUNET_BLOCK_Group *bg);
94
95
96 /**
97  * Block group data.  The plugin must initialize the callbacks
98  * and can use the @e internal_cls as it likes.
99  */
100 struct GNUNET_BLOCK_Group
101 {
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 /**
146  * Create a new block group.
147  *
148  * @param ctx block context in which the block group is created
149  * @param type type of the block for which we are creating the group
150  * @param nonce random value used to seed the group creation
151  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
152  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
153  * @param va variable arguments specific to @a type
154  * @return block group handle, NULL if block groups are not supported
155  *         by this @a type of block (this is not an error)
156  */
157 typedef struct GNUNET_BLOCK_Group *
158 (*GNUNET_BLOCK_GroupCreateFunction)(void *cls,
159                                     enum GNUNET_BLOCK_Type type,
160                                     uint32_t nonce,
161                                     const void *raw_data,
162                                     size_t raw_data_size,
163                                     va_list va);
164
165
166 /**
167  * Function called to validate a reply or a request.  For
168  * request evaluation, simply pass "NULL" for the @a reply_block.
169  * Note that it is assumed that the reply has already been
170  * matched to the key (and signatures checked) as it would
171  * be done with the "get_key" function.
172  *
173  * @param cls closure
174  * @param ctx block context
175  * @param type block type
176  * @param group which block group to use for evaluation
177  * @param eo evaluation options to control evaluation
178  * @param query original query (hash)
179  * @param xquery extrended query data (can be NULL, depending on type)
180  * @param xquery_size number of bytes in @a xquery
181  * @param reply_block response to validate
182  * @param reply_block_size number of bytes in @a reply_block
183  * @return characterization of result
184  */
185 typedef enum GNUNET_BLOCK_EvaluationResult
186 (*GNUNET_BLOCK_EvaluationFunction) (void *cls,
187                                     struct GNUNET_BLOCK_Context *ctx,
188                                     enum GNUNET_BLOCK_Type type,
189                                     struct GNUNET_BLOCK_Group *group,
190                                     enum GNUNET_BLOCK_EvaluationOptions eo,
191                                     const struct GNUNET_HashCode *query,
192                                     const void *xquery,
193                                     size_t xquery_size,
194                                     const void *reply_block,
195                                     size_t reply_block_size);
196
197
198 /**
199  * Function called to obtain the key for a block.
200  *
201  * @param cls closure
202  * @param type block type
203  * @param block block to get the key for
204  * @param block_size number of bytes in @a block
205  * @param key set to the key (query) for the given block
206  * @return #GNUNET_YES on success,
207  *         #GNUNET_NO if the block is malformed
208  *         #GNUNET_SYSERR if type not supported
209  *         (or if extracting a key from a block of this type does not work)
210  */
211 typedef int
212 (*GNUNET_BLOCK_GetKeyFunction) (void *cls,
213                                 enum GNUNET_BLOCK_Type type,
214                                 const void *block,
215                                 size_t block_size,
216                                 struct GNUNET_HashCode *key);
217
218
219
220 /**
221  * Each plugin is required to return a pointer to a struct of this
222  * type as the return value from its entry point.
223  */
224 struct GNUNET_BLOCK_PluginFunctions
225 {
226
227   /**
228    * Closure for all of the callbacks.
229    */
230   void *cls;
231
232   /**
233    * 0-terminated array of block types supported by this plugin.
234    */
235   const enum GNUNET_BLOCK_Type *types;
236
237   /**
238    * Main function of a block plugin.  Allows us to check if a
239    * block matches a query.
240    */
241   GNUNET_BLOCK_EvaluationFunction evaluate;
242
243   /**
244    * Obtain the key for a given block (if possible).
245    */
246   GNUNET_BLOCK_GetKeyFunction get_key;
247
248   /**
249    * Create a block group to process a bunch of blocks in a shared
250    * context (i.e. to detect duplicates).
251    */
252   GNUNET_BLOCK_GroupCreateFunction create_group;
253 };
254
255 #endif
256
257 /** @} */  /* end of group */