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