first half of new BLOCK API to generalize duplicate detection beyond BFs
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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  * Serialize state of a block group.
44  *
45  * @param bg group to serialize
46  * @param[out] raw_data set to the serialized state
47  * @param[out] raw_data_size set to the number of bytes in @a raw_data
48  * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
49  *         supported, #GNUNET_SYSERR on error
50  */
51 typedef int
52 (*GNUNET_BLOCK_GroupSerializeFunction)(struct GNUNET_BLOCK_Group *bg,
53                                        void **raw_data,
54                                        size_t *raw_data_size);
55
56
57 /**
58  * Destroy resources used by a block group.
59  *
60  * @param bg group to destroy, NULL is allowed
61  */
62 typedef void
63 (*GNUNET_BLOCK_GroupDestroyFunction)(struct GNUNET_BLOCK_Group *bg);
64
65
66 /**
67  * Block group data.  The plugin must initialize the callbacks
68  * and can use the @e internal_cls as it likes.
69  */
70 struct GNUNET_BLOCK_Group
71 {
72
73   /**
74    * Context owning the block group. Set by the main block library.
75    */
76   struct GNUENT_BLOCK_Context *ctx;
77
78   /**
79    * Type for the block group.  Set by the main block library.
80    */
81   enum GNUNET_BLOCK_Type type;
82
83   /**
84    * Serialize the block group data, can be NULL if
85    * not supported.
86    */
87   GNUNET_BLOCK_GroupSerializeFunction serialize_cb;
88
89   /**
90    * Function to call to destroy the block group.
91    * Must not be NULL.
92    */
93   GNUNET_BLOCK_GroupDestroyFunction destroy_cb;
94
95   /**
96    * Internal data structure of the plugin.
97    */
98   void *internal_cls;
99
100 };
101
102
103 /**
104  * Create a new block group.
105  *
106  * @param ctx block context in which the block group is created
107  * @param type type of the block for which we are creating the group
108  * @param nonce random value used to seed the group creation
109  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
110  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
111  * @return block group handle, NULL if block groups are not supported
112  *         by this @a type of block (this is not an error)
113  */
114 typedef struct GNUNET_BLOCK_Group *
115 (*GNUNET_BLOCK_GroupCreateFunction)(void *cls,
116                                     enum GNUNET_BLOCK_Type type,
117                                     uint32_t nonce,
118                                     const void *raw_data,
119                                     size_t raw_data_size);
120
121
122 /**
123  * Function called to validate a reply or a request.  For
124  * request evaluation, simply pass "NULL" for the @a reply_block.
125  * Note that it is assumed that the reply has already been
126  * matched to the key (and signatures checked) as it would
127  * be done with the "get_key" function.
128  *
129  * @param cls closure
130  * @param type block type
131  * @param eo evaluation options to control evaluation
132  * @param query original query (hash)
133  * @param bf pointer to bloom filter associated with query; possibly updated (!)
134  * @param bf_mutator mutation value for @a bf
135  * @param xquery extrended query data (can be NULL, depending on type)
136  * @param xquery_size number of bytes in @a xquery
137  * @param reply_block response to validate
138  * @param reply_block_size number of bytes in @a reply_block
139  * @return characterization of result
140  */
141 typedef enum GNUNET_BLOCK_EvaluationResult
142 (*GNUNET_BLOCK_EvaluationFunction) (void *cls,
143                                     enum GNUNET_BLOCK_Type type,
144                                     enum GNUNET_BLOCK_EvaluationOptions eo,
145                                     const struct GNUNET_HashCode *query,
146                                     struct GNUNET_CONTAINER_BloomFilter **bf,
147                                     int32_t bf_mutator,
148                                     const void *xquery,
149                                     size_t xquery_size,
150                                     const void *reply_block,
151                                     size_t reply_block_size);
152
153
154 /**
155  * Function called to obtain the key for a block.
156  *
157  * @param cls closure
158  * @param type block type
159  * @param block block to get the key for
160  * @param block_size number of bytes in @a block
161  * @param key set to the key (query) for the given block
162  * @return #GNUNET_YES on success,
163  *         #GNUNET_NO if the block is malformed
164  *         #GNUNET_SYSERR if type not supported
165  *         (or if extracting a key from a block of this type does not work)
166  */
167 typedef int
168 (*GNUNET_BLOCK_GetKeyFunction) (void *cls,
169                                 enum GNUNET_BLOCK_Type type,
170                                 const void *block,
171                                 size_t block_size,
172                                 struct GNUNET_HashCode *key);
173
174
175
176 /**
177  * Each plugin is required to return a pointer to a struct of this
178  * type as the return value from its entry point.
179  */
180 struct GNUNET_BLOCK_PluginFunctions
181 {
182
183   /**
184    * Closure for all of the callbacks.
185    */
186   void *cls;
187
188   /**
189    * 0-terminated array of block types supported by this plugin.
190    */
191   const enum GNUNET_BLOCK_Type *types;
192
193   /**
194    * Main function of a block plugin.  Allows us to check if a
195    * block matches a query.
196    */
197   GNUNET_BLOCK_EvaluationFunction evaluate;
198
199   /**
200    * Obtain the key for a given block (if possible).
201    */
202   GNUNET_BLOCK_GetKeyFunction get_key;
203
204   /**
205    * Create a block group to process a bunch of blocks in a shared
206    * context (i.e. to detect duplicates).
207    */
208   GNUNET_BLOCK_GroupCreateFunction create_group;
209 };
210
211 #endif
212
213 /** @} */  /* end of group */