4bdaec8411f02672fa87f14e43be5441ee6fb534
[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
19 /**
20  * @author Christian Grothoff
21  *
22  * @file
23  * API for block plugins.
24  *
25  * @defgroup block-plugin  Block plugin API
26  * To be implemented by applications storing data in the DHT.
27  *
28  * Each block plugin must conform to the API specified by this header.
29  *
30  * @{
31  */
32
33 #ifndef PLUGIN_BLOCK_H
34 #define PLUGIN_BLOCK_H
35
36 #include "gnunet_util_lib.h"
37 #include "gnunet_block_lib.h"
38
39
40 /**
41  * Mark elements as "seen" using a hash of the element. Not supported
42  * by all block plugins.
43  *
44  * @param bg group to update
45  * @param seen_results results already seen
46  * @param seen_results_count number of entries in @a seen_results
47  */
48 typedef void
49 (*GNUNET_BLOCK_GroupMarkSeenFunction)(struct GNUNET_BLOCK_Group *bg,
50                                       const struct GNUNET_HashCode *seen_results,
51                                       unsigned int seen_results_count);
52
53
54 /**
55  * Merge two groups, if possible. Not supported by all block plugins,
56  * can also fail if the nonces were different.
57  *
58  * @param bg1 group to update
59  * @param bg2 group to merge into @a bg1
60  * @return #GNUNET_OK on success, #GNUNET_NO if the nonces were different and thus
61  *         we failed.
62  */
63 typedef int
64 (*GNUNET_BLOCK_GroupMergeFunction)(struct GNUNET_BLOCK_Group *bg1,
65                                    const struct GNUNET_BLOCK_Group *bg2);
66
67
68 /**
69  * Serialize state of a block group.
70  *
71  * @param bg group to serialize
72  * @param[out] nonce set to the nonce of the @a bg
73  * @param[out] raw_data set to the serialized state
74  * @param[out] raw_data_size set to the number of bytes in @a raw_data
75  * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
76  *         supported, #GNUNET_SYSERR on error
77  */
78 typedef int
79 (*GNUNET_BLOCK_GroupSerializeFunction)(struct GNUNET_BLOCK_Group *bg,
80                                        uint32_t *nonce,
81                                        void **raw_data,
82                                        size_t *raw_data_size);
83
84
85 /**
86  * Destroy resources used by a block group.
87  *
88  * @param bg group to destroy, NULL is allowed
89  */
90 typedef void
91 (*GNUNET_BLOCK_GroupDestroyFunction)(struct GNUNET_BLOCK_Group *bg);
92
93
94 /**
95  * Block group data.  The plugin must initialize the callbacks
96  * and can use the @e internal_cls as it likes.
97  */
98 struct GNUNET_BLOCK_Group
99 {
100
101   /**
102    * Context owning the block group. Set by the main block library.
103    */
104   struct GNUENT_BLOCK_Context *ctx;
105
106   /**
107    * Type for the block group.  Set by the main block library.
108    */
109   enum GNUNET_BLOCK_Type type;
110
111   /**
112    * Serialize the block group data, can be NULL if
113    * not supported.
114    */
115   GNUNET_BLOCK_GroupSerializeFunction serialize_cb;
116
117   /**
118    * Function to call to mark elements as seen in the group.
119    * Can be NULL if not supported.
120    */
121   GNUNET_BLOCK_GroupMarkSeenFunction mark_seen_cb;
122
123   /**
124    * Function to call to merge two groups.
125    * Can be NULL if not supported.
126    */
127   GNUNET_BLOCK_GroupMergeFunction merge_cb;
128
129   /**
130    * Function to call to destroy the block group.
131    * Must not be NULL.
132    */
133   GNUNET_BLOCK_GroupDestroyFunction destroy_cb;
134
135   /**
136    * Internal data structure of the plugin.
137    */
138   void *internal_cls;
139
140 };
141
142
143 /**
144  * Create a new block group.
145  *
146  * @param ctx block context in which the block group is created
147  * @param type type of the block for which we are creating the group
148  * @param nonce random value used to seed the group creation
149  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
150  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
151  * @param va variable arguments specific to @a type
152  * @return block group handle, NULL if block groups are not supported
153  *         by this @a type of block (this is not an error)
154  */
155 typedef struct GNUNET_BLOCK_Group *
156 (*GNUNET_BLOCK_GroupCreateFunction)(void *cls,
157                                     enum GNUNET_BLOCK_Type type,
158                                     uint32_t nonce,
159                                     const void *raw_data,
160                                     size_t raw_data_size,
161                                     va_list va);
162
163
164 /**
165  * Function called to validate a reply or a request.  For
166  * request evaluation, simply pass "NULL" for the @a reply_block.
167  * Note that it is assumed that the reply has already been
168  * matched to the key (and signatures checked) as it would
169  * be done with the "get_key" function.
170  *
171  * @param cls closure
172  * @param ctx block context
173  * @param type block type
174  * @param group which block group to use for evaluation
175  * @param eo evaluation options to control evaluation
176  * @param query original query (hash)
177  * @param xquery extrended query data (can be NULL, depending on type)
178  * @param xquery_size number of bytes in @a xquery
179  * @param reply_block response to validate
180  * @param reply_block_size number of bytes in @a reply_block
181  * @return characterization of result
182  */
183 typedef enum GNUNET_BLOCK_EvaluationResult
184 (*GNUNET_BLOCK_EvaluationFunction) (void *cls,
185                                     struct GNUNET_BLOCK_Context *ctx,
186                                     enum GNUNET_BLOCK_Type type,
187                                     struct GNUNET_BLOCK_Group *group,
188                                     enum GNUNET_BLOCK_EvaluationOptions eo,
189                                     const struct GNUNET_HashCode *query,
190                                     const void *xquery,
191                                     size_t xquery_size,
192                                     const void *reply_block,
193                                     size_t reply_block_size);
194
195
196 /**
197  * Function called to obtain the key for a block.
198  *
199  * @param cls closure
200  * @param type block type
201  * @param block block to get the key for
202  * @param block_size number of bytes in @a block
203  * @param key set to the key (query) for the given block
204  * @return #GNUNET_YES on success,
205  *         #GNUNET_NO if the block is malformed
206  *         #GNUNET_SYSERR if type not supported
207  *         (or if extracting a key from a block of this type does not work)
208  */
209 typedef int
210 (*GNUNET_BLOCK_GetKeyFunction) (void *cls,
211                                 enum GNUNET_BLOCK_Type type,
212                                 const void *block,
213                                 size_t block_size,
214                                 struct GNUNET_HashCode *key);
215
216
217
218 /**
219  * Each plugin is required to return a pointer to a struct of this
220  * type as the return value from its entry point.
221  */
222 struct GNUNET_BLOCK_PluginFunctions
223 {
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 */