glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / dht / plugin_block_dht.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010, 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  * @file dht/plugin_block_dht.c
18  * @brief block plugin for DHT internals (right now, find-peer requests only);
19  *        other plugins should be used to store "useful" data in the
20  *        DHT (see fs block plugin)
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_constants.h"
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_block_plugin.h"
27 #include "gnunet_block_group_lib.h"
28
29 #define DEBUG_DHT GNUNET_EXTRA_LOGGING
30
31 /**
32  * Number of bits we set per entry in the bloomfilter.
33  * Do not change!
34  */
35 #define BLOOMFILTER_K 16
36
37
38 /**
39  * Create a new block group.
40  *
41  * @param ctx block context in which the block group is created
42  * @param type type of the block for which we are creating the group
43  * @param nonce random value used to seed the group creation
44  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
45  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
46  * @param va variable arguments specific to @a type
47  * @return block group handle, NULL if block groups are not supported
48  *         by this @a type of block (this is not an error)
49  */
50 static struct GNUNET_BLOCK_Group *
51 block_plugin_dht_create_group (void *cls,
52                                enum GNUNET_BLOCK_Type type,
53                                uint32_t nonce,
54                                const void *raw_data,
55                                size_t raw_data_size,
56                                va_list va)
57 {
58   unsigned int bf_size;
59   const char *guard;
60
61   guard = va_arg (va, const char *);
62   if (0 == strcmp (guard,
63                    "seen-set-size"))
64     bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va,
65                                                                    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 = 8;
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 @a reply_block.
89  *
90  * @param cls closure
91  * @param ctx context
92  * @param type block type
93  * @param group block group to check against
94  * @param eo control flags
95  * @param query original query (hash)
96  * @param xquery extended 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_dht_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   const struct GNUNET_HELLO_Message *hello;
115   struct GNUNET_PeerIdentity pid;
116   const struct GNUNET_MessageHeader *msg;
117   struct GNUNET_HashCode phash;
118
119   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
120     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
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   if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
129   {
130     GNUNET_break_op (0);
131     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
132   }
133   msg = reply_block;
134   if (reply_block_size != ntohs (msg->size))
135   {
136     GNUNET_break_op (0);
137     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
138   }
139   hello = reply_block;
140   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
141   {
142     GNUNET_break_op (0);
143     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
144   }
145   GNUNET_CRYPTO_hash (&pid,
146                       sizeof (pid),
147                       &phash);
148   if (GNUNET_YES ==
149       GNUNET_BLOCK_GROUP_bf_test_and_set (group,
150                                           &phash))
151     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
152   return GNUNET_BLOCK_EVALUATION_OK_MORE;
153 }
154
155
156 /**
157  * Function called to obtain the key for a block.
158  *
159  * @param cls closure
160  * @param type block type
161  * @param block block to get the key for
162  * @param block_size number of bytes @a block
163  * @param[out] key set to the key (query) for the given block
164  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
165  *         (or if extracting a key from a block of this type does not work)
166  */
167 static int
168 block_plugin_dht_get_key (void *cls,
169                           enum GNUNET_BLOCK_Type type,
170                           const void *block,
171                           size_t block_size,
172                           struct GNUNET_HashCode *key)
173 {
174   const struct GNUNET_MessageHeader *msg;
175   const struct GNUNET_HELLO_Message *hello;
176   struct GNUNET_PeerIdentity *pid;
177
178   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
179     return GNUNET_SYSERR;
180   if (block_size < sizeof (struct GNUNET_MessageHeader))
181   {
182     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
183                      "block-dht",
184                      _("Block not of type %u\n"),
185                      GNUNET_BLOCK_TYPE_DHT_HELLO);
186     return GNUNET_NO;
187   }
188   msg = block;
189   if (block_size != ntohs (msg->size))
190   {
191     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
192                      "block-dht",
193                      _("Size mismatch for block\n"),
194                      GNUNET_BLOCK_TYPE_DHT_HELLO);
195     return GNUNET_NO;
196   }
197   hello = block;
198   memset (key, 0, sizeof (*key));
199   pid = (struct GNUNET_PeerIdentity *) key;
200   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
201   {
202     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
203                      "block-dht",
204                      _("Block of type %u is malformed\n"),
205                      GNUNET_BLOCK_TYPE_DHT_HELLO);
206     return GNUNET_NO;
207   }
208   return GNUNET_OK;
209 }
210
211
212 /**
213  * Entry point for the plugin.
214  */
215 void *
216 libgnunet_plugin_block_dht_init (void *cls)
217 {
218   static enum GNUNET_BLOCK_Type types[] =
219   {
220     GNUNET_BLOCK_TYPE_DHT_HELLO,
221     GNUNET_BLOCK_TYPE_ANY       /* end of list */
222   };
223   struct GNUNET_BLOCK_PluginFunctions *api;
224
225   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
226   api->evaluate = &block_plugin_dht_evaluate;
227   api->get_key = &block_plugin_dht_get_key;
228   api->create_group = &block_plugin_dht_create_group;
229   api->types = types;
230   return api;
231 }
232
233
234 /**
235  * Exit point from the plugin.
236  */
237 void *
238 libgnunet_plugin_block_dht_done (void *cls)
239 {
240   struct GNUNET_BLOCK_PluginFunctions *api = cls;
241
242   GNUNET_free (api);
243   return NULL;
244 }
245
246 /* end of plugin_block_dht.c */