glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / block / plugin_block_test.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010 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  * @file block/plugin_block_test.c
18  * @brief block plugin to test the DHT as a simple key-value store;
19  *        this plugin simply accepts any (new) response for any key
20  * @author Christian Grothoff
21  */
22
23 #include "platform.h"
24 #include "gnunet_block_plugin.h"
25 #include "gnunet_block_group_lib.h"
26
27 /**
28  * Number of bits we set per entry in the bloomfilter.
29  * Do not change!
30  */
31 #define BLOOMFILTER_K 16
32
33 /**
34  * How big is the BF we use for DHT blocks?
35  */
36 #define TEST_BF_SIZE 8
37
38
39 /**
40  * Create a new block group.
41  *
42  * @param ctx block context in which the block group is created
43  * @param type type of the block for which we are creating the group
44  * @param nonce random value used to seed the group creation
45  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
46  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
47  * @param va variable arguments specific to @a type
48  * @return block group handle, NULL if block groups are not supported
49  *         by this @a type of block (this is not an error)
50  */
51 static struct GNUNET_BLOCK_Group *
52 block_plugin_test_create_group (void *cls,
53                                 enum GNUNET_BLOCK_Type type,
54                                 uint32_t nonce,
55                                 const void *raw_data,
56                                 size_t raw_data_size,
57                                 va_list va)
58 {
59   unsigned int bf_size;
60   const char *guard;
61
62   guard = va_arg (va, const char *);
63   if (0 == strcmp (guard,
64                    "seen-set-size"))
65     bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
66                                                            BLOOMFILTER_K);
67   else if (0 == strcmp (guard,
68                         "filter-size"))
69     bf_size = va_arg (va, unsigned int);
70   else
71   {
72     GNUNET_break (0);
73     bf_size = TEST_BF_SIZE;
74   }
75   GNUNET_break (NULL == va_arg (va, const char *));
76   return GNUNET_BLOCK_GROUP_bf_create (cls,
77                                        bf_size,
78                                        BLOOMFILTER_K,
79                                        type,
80                                        nonce,
81                                        raw_data,
82                                        raw_data_size);
83 }
84
85
86 /**
87  * Function called to validate a reply or a request.  For
88  * request evaluation, simply pass "NULL" for the reply_block.
89  *
90  * @param cls closure
91  * @param ctx block context
92  * @param type block type
93  * @param group group to check against
94  * @param eo control flags
95  * @param query original query (hash)
96  * @param xquery extrended query data (can be NULL, depending on type)
97  * @param xquery_size number of bytes in @a xquery
98  * @param reply_block response to validate
99  * @param reply_block_size number of bytes in @a reply_block
100  * @return characterization of result
101  */
102 static enum GNUNET_BLOCK_EvaluationResult
103 block_plugin_test_evaluate (void *cls,
104                             struct GNUNET_BLOCK_Context *ctx,
105                             enum GNUNET_BLOCK_Type type,
106                             struct GNUNET_BLOCK_Group *group,
107                             enum GNUNET_BLOCK_EvaluationOptions eo,
108                             const struct GNUNET_HashCode *query,
109                             const void *xquery,
110                             size_t xquery_size,
111                             const void *reply_block,
112                             size_t reply_block_size)
113 {
114   struct GNUNET_HashCode chash;
115
116   if ( GNUNET_BLOCK_TYPE_TEST != type)
117   {
118     GNUNET_break (0);
119     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
120   }
121   if (0 != xquery_size)
122   {
123     GNUNET_break_op (0);
124     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
125   }
126   if (NULL == reply_block)
127     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
128   GNUNET_CRYPTO_hash (reply_block,
129                       reply_block_size,
130                       &chash);
131   if (GNUNET_YES ==
132       GNUNET_BLOCK_GROUP_bf_test_and_set (group,
133                                           &chash))
134     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
135   return GNUNET_BLOCK_EVALUATION_OK_MORE;
136 }
137
138
139 /**
140  * Function called to obtain the key for a block.
141  *
142  * @param cls closure
143  * @param type block type
144  * @param block block to get the key for
145  * @param block_size number of bytes in @a block
146  * @param key set to the key (query) for the given block
147  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
148  *         (or if extracting a key from a block of this type does not work)
149  */
150 static int
151 block_plugin_test_get_key (void *cls,
152                            enum GNUNET_BLOCK_Type type,
153                            const void *block,
154                            size_t block_size,
155                            struct GNUNET_HashCode *key)
156 {
157   /* always fails since there is no fixed relationship between
158    * keys and values for test values */
159   return GNUNET_SYSERR;
160 }
161
162
163 /**
164  * Entry point for the plugin.
165  *
166  * @param cls NULL
167  * @return the exported block API
168  */
169 void *
170 libgnunet_plugin_block_test_init (void *cls)
171 {
172   static enum GNUNET_BLOCK_Type types[] =
173   {
174     GNUNET_BLOCK_TYPE_TEST,
175     GNUNET_BLOCK_TYPE_ANY       /* end of list */
176   };
177   struct GNUNET_BLOCK_PluginFunctions *api;
178
179   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
180   api->evaluate = &block_plugin_test_evaluate;
181   api->get_key = &block_plugin_test_get_key;
182   api->create_group = &block_plugin_test_create_group;
183   api->types = types;
184   return api;
185 }
186
187
188 /**
189  * Exit point from the plugin.
190  *
191  * @param cls the return value from #libgnunet_plugin_block_test_init
192  * @return NULL
193  */
194 void *
195 libgnunet_plugin_block_test_done (void *cls)
196 {
197   struct GNUNET_BLOCK_PluginFunctions *api = cls;
198
199   GNUNET_free (api);
200   return NULL;
201 }
202
203 /* end of plugin_block_test.c */