glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / gns / plugin_block_gns.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010-2013 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 gns/plugin_block_gns.c
18  * @brief blocks used for GNS records
19  * @author Martin Schanzenbach
20  * @author Christian Grothoff
21  */
22
23 #include "platform.h"
24 #include "gnunet_block_group_lib.h"
25 #include "gnunet_block_plugin.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_signatures.h"
28
29 /**
30  * Number of bits we set per entry in the bloomfilter.
31  * Do not change! -from fs
32  */
33 #define BLOOMFILTER_K 16
34
35 /**
36  * How big is the BF we use for GNS blocks?
37  */
38 #define GNS_BF_SIZE 8
39
40
41 /**
42  * Create a new block group.
43  *
44  * @param ctx block context in which the block group is created
45  * @param type type of the block for which we are creating the group
46  * @param nonce random value used to seed the group creation
47  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
48  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
49  * @param va variable arguments specific to @a type
50  * @return block group handle, NULL if block groups are not supported
51  *         by this @a type of block (this is not an error)
52  */
53 static struct GNUNET_BLOCK_Group *
54 block_plugin_gns_create_group (void *cls,
55                                enum GNUNET_BLOCK_Type type,
56                                uint32_t nonce,
57                                const void *raw_data,
58                                size_t raw_data_size,
59                                va_list va)
60 {
61   unsigned int bf_size;
62   const char *guard;
63
64   guard = va_arg (va, const char *);
65   if (0 == strcmp (guard,
66                    "seen-set-size"))
67     bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
68                                                            BLOOMFILTER_K);
69   else if (0 == strcmp (guard,
70                         "filter-size"))
71     bf_size = va_arg (va, unsigned int);
72   else
73   {
74     GNUNET_break (0);
75     bf_size = GNS_BF_SIZE;
76   }
77   GNUNET_break (NULL == va_arg (va, const char *));
78   return GNUNET_BLOCK_GROUP_bf_create (cls,
79                                        bf_size,
80                                        BLOOMFILTER_K,
81                                        type,
82                                        nonce,
83                                        raw_data,
84                                        raw_data_size);
85 }
86
87
88 /**
89  * Function called to validate a reply or a request.  For
90  * request evaluation, simply pass "NULL" for the reply_block.
91  * Note that it is assumed that the reply has already been
92  * matched to the key (and signatures checked) as it would
93  * be done with the "get_key" function.
94  *
95  * @param cls closure
96  * @param ctx block context
97  * @param type block type
98  * @param bg block group to use for evaluation
99  * @param eo control flags
100  * @param query original query (hash)
101  * @param xquery extrended query data (can be NULL, depending on @a type)
102  * @param xquery_size number of bytes in @a xquery
103  * @param reply_block response to validate
104  * @param reply_block_size number of bytes in @a reply_block
105  * @return characterization of result
106  */
107 static enum GNUNET_BLOCK_EvaluationResult
108 block_plugin_gns_evaluate (void *cls,
109                            struct GNUNET_BLOCK_Context *ctx,
110                            enum GNUNET_BLOCK_Type type,
111                            struct GNUNET_BLOCK_Group *bg,
112                            enum GNUNET_BLOCK_EvaluationOptions eo,
113                            const struct GNUNET_HashCode *query,
114                            const void *xquery,
115                            size_t xquery_size,
116                            const void *reply_block,
117                            size_t reply_block_size)
118 {
119   const struct GNUNET_GNSRECORD_Block *block;
120   struct GNUNET_HashCode h;
121   struct GNUNET_HashCode chash;
122
123   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
124     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
125   if (NULL == reply_block)
126   {
127     if (0 != xquery_size)
128     {
129       GNUNET_break_op (0);
130       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
131     }
132     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
133   }
134
135   /* this is a reply */
136   if (reply_block_size < sizeof (struct GNUNET_GNSRECORD_Block))
137     {
138       GNUNET_break_op (0);
139       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
140     }
141   block = reply_block;
142   if (ntohl (block->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature) + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
143       reply_block_size)
144     {
145       GNUNET_break_op (0);
146       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
147     }
148   GNUNET_CRYPTO_hash (&block->derived_key,
149                       sizeof (block->derived_key),
150                       &h);
151   if (0 != memcmp (&h, query, sizeof (struct GNUNET_HashCode)))
152     {
153       GNUNET_break_op (0);
154       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
155     }
156   if (GNUNET_OK !=
157       GNUNET_GNSRECORD_block_verify (block))
158     {
159       GNUNET_break_op (0);
160       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
161     }
162   GNUNET_CRYPTO_hash (reply_block,
163                       reply_block_size,
164                       &chash);
165   if (GNUNET_YES ==
166       GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
167                                           &chash))
168     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
169   return GNUNET_BLOCK_EVALUATION_OK_MORE;
170 }
171
172
173 /**
174  * Function called to obtain the key for a block.
175  *
176  * @param cls closure
177  * @param type block type
178  * @param reply_block block to get the key for
179  * @param reply_block_size number of bytes in @a reply_block
180  * @param key set to the key (query) for the given block
181  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
182  *         (or if extracting a key from a block of this type does not work)
183  */
184 static int
185 block_plugin_gns_get_key (void *cls,
186                           enum GNUNET_BLOCK_Type type,
187                           const void *reply_block,
188                           size_t reply_block_size,
189                           struct GNUNET_HashCode *key)
190 {
191   const struct GNUNET_GNSRECORD_Block *block;
192
193   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
194     return GNUNET_SYSERR;
195   if (reply_block_size < sizeof (struct GNUNET_GNSRECORD_Block))
196     {
197       GNUNET_break_op (0);
198       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
199     }
200   block = reply_block;
201   GNUNET_CRYPTO_hash (&block->derived_key,
202                       sizeof (block->derived_key),
203                       key);
204   return GNUNET_OK;
205 }
206
207
208 /**
209  * Entry point for the plugin.
210  */
211 void *
212 libgnunet_plugin_block_gns_init (void *cls)
213 {
214   static enum GNUNET_BLOCK_Type types[] =
215   {
216     GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
217     GNUNET_BLOCK_TYPE_ANY       /* end of list */
218   };
219   struct GNUNET_BLOCK_PluginFunctions *api;
220
221   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
222   api->evaluate = &block_plugin_gns_evaluate;
223   api->get_key = &block_plugin_gns_get_key;
224   api->create_group = &block_plugin_gns_create_group;
225   api->types = types;
226   return api;
227 }
228
229
230 /**
231  * Exit point from the plugin.
232  */
233 void *
234 libgnunet_plugin_block_gns_done (void *cls)
235 {
236   struct GNUNET_BLOCK_PluginFunctions *api = cls;
237
238   GNUNET_free (api);
239   return NULL;
240 }
241
242 /* end of plugin_block_gns.c */