commented out wrong message type
[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
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  * @file dht/plugin_block_dht.c
23  * @brief block plugin for DHT internals (right now, find-peer requests only);
24  *        other plugins should be used to store "useful" data in the
25  *        DHT (see fs block plugin)
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_block_plugin.h"
32 #include "gnunet_block_group_lib.h"
33
34 #define DEBUG_DHT GNUNET_EXTRA_LOGGING
35
36 /**
37  * Number of bits we set per entry in the bloomfilter.
38  * Do not change!
39  */
40 #define BLOOMFILTER_K 16
41
42
43 /**
44  * Create a new block group.
45  *
46  * @param ctx block context in which the block group is created
47  * @param type type of the block for which we are creating the group
48  * @param nonce random value used to seed the group creation
49  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
50  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
51  * @param va variable arguments specific to @a type
52  * @return block group handle, NULL if block groups are not supported
53  *         by this @a type of block (this is not an error)
54  */
55 static struct GNUNET_BLOCK_Group *
56 block_plugin_dht_create_group (void *cls,
57                                enum GNUNET_BLOCK_Type type,
58                                uint32_t nonce,
59                                const void *raw_data,
60                                size_t raw_data_size,
61                                va_list va)
62 {
63   unsigned int bf_size;
64   const char *guard;
65
66   guard = va_arg (va, const char *);
67   if (0 == strcmp (guard,
68                    "seen-set-size"))
69     bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
70                                                            BLOOMFILTER_K);
71   else if (0 == strcmp (guard,
72                         "filter-size"))
73     bf_size = va_arg (va, unsigned int);
74   else
75   {
76     GNUNET_break (0);
77     bf_size = 8;
78   }
79   GNUNET_break (NULL == va_arg (va, const char *));
80   return GNUNET_BLOCK_GROUP_bf_create (cls,
81                                        bf_size,
82                                        BLOOMFILTER_K,
83                                        type,
84                                        nonce,
85                                        raw_data,
86                                        raw_data_size);
87 }
88
89
90 /**
91  * Function called to validate a reply or a request.  For
92  * request evaluation, simply pass "NULL" for the @a reply_block.
93  *
94  * @param cls closure
95  * @param ctx context
96  * @param type block type
97  * @param group block group to check against
98  * @param eo control flags
99  * @param query original query (hash)
100  * @param xquery extended query data (can be NULL, depending on type)
101  * @param xquery_size number of bytes in @a xquery
102  * @param reply_block response to validate
103  * @param reply_block_size number of bytes in @a reply_block
104  * @return characterization of result
105  */
106 static enum GNUNET_BLOCK_EvaluationResult
107 block_plugin_dht_evaluate (void *cls,
108                            struct GNUNET_BLOCK_Context *ctx,
109                            enum GNUNET_BLOCK_Type type,
110                            struct GNUNET_BLOCK_Group *group,
111                            enum GNUNET_BLOCK_EvaluationOptions eo,
112                            const struct GNUNET_HashCode *query,
113                            const void *xquery,
114                            size_t xquery_size,
115                            const void *reply_block,
116                            size_t reply_block_size)
117 {
118   const struct GNUNET_HELLO_Message *hello;
119   struct GNUNET_PeerIdentity pid;
120   const struct GNUNET_MessageHeader *msg;
121   struct GNUNET_HashCode phash;
122
123   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
124     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
125   if (0 != xquery_size)
126   {
127     GNUNET_break_op (0);
128     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
129   }
130   if (NULL == reply_block)
131     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
132   if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
133   {
134     GNUNET_break_op (0);
135     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
136   }
137   msg = reply_block;
138   if (reply_block_size != ntohs (msg->size))
139   {
140     GNUNET_break_op (0);
141     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
142   }
143   hello = reply_block;
144   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
145   {
146     GNUNET_break_op (0);
147     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
148   }
149   GNUNET_CRYPTO_hash (&pid,
150                       sizeof (pid),
151                       &phash);
152   if (GNUNET_YES ==
153       GNUNET_BLOCK_GROUP_bf_test_and_set (group,
154                                           &phash))
155     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
156   return GNUNET_BLOCK_EVALUATION_OK_MORE;
157 }
158
159
160 /**
161  * Function called to obtain the key for a block.
162  *
163  * @param cls closure
164  * @param type block type
165  * @param block block to get the key for
166  * @param block_size number of bytes @a block
167  * @param[out] key set to the key (query) for the given block
168  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
169  *         (or if extracting a key from a block of this type does not work)
170  */
171 static int
172 block_plugin_dht_get_key (void *cls,
173                           enum GNUNET_BLOCK_Type type,
174                           const void *block,
175                           size_t block_size,
176                           struct GNUNET_HashCode *key)
177 {
178   const struct GNUNET_MessageHeader *msg;
179   const struct GNUNET_HELLO_Message *hello;
180   struct GNUNET_PeerIdentity *pid;
181
182   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
183     return GNUNET_SYSERR;
184   if (block_size < sizeof (struct GNUNET_MessageHeader))
185   {
186     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
187                      _("Block not of type %u\n"), GNUNET_BLOCK_TYPE_DHT_HELLO);
188     return GNUNET_NO;
189   }
190   msg = block;
191   if (block_size != ntohs (msg->size))
192   {
193     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
194                      _("Size mismatch for block\n"),
195                      GNUNET_BLOCK_TYPE_DHT_HELLO);
196     return GNUNET_NO;
197   }
198   hello = block;
199   memset (key, 0, sizeof (*key));
200   pid = (struct GNUNET_PeerIdentity *) key;
201   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
202   {
203     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "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 */