Merge branch 'master' of ssh://gnunet.org/gnunet
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file dht/plugin_block_dht.c
21  * @brief block plugin for DHT internals (right now, find-peer requests only);
22  *        other plugins should be used to store "useful" data in the
23  *        DHT (see fs block plugin)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_block_plugin.h"
30 #include "gnunet_block_group_lib.h"
31
32 #define DEBUG_DHT GNUNET_EXTRA_LOGGING
33
34 /**
35  * Number of bits we set per entry in the bloomfilter.
36  * Do not change!
37  */
38 #define BLOOMFILTER_K 16
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_dht_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,
68                                                                    unsigned int),
69                                                            BLOOMFILTER_K);
70   else if (0 == strcmp (guard,
71                         "filter-size"))
72     bf_size = va_arg (va, unsigned int);
73   else
74   {
75     GNUNET_break (0);
76     bf_size = 8;
77   }
78   GNUNET_break (NULL == va_arg (va, const char *));
79   return GNUNET_BLOCK_GROUP_bf_create (cls,
80                                        bf_size,
81                                        BLOOMFILTER_K,
82                                        type,
83                                        nonce,
84                                        raw_data,
85                                        raw_data_size);
86 }
87
88
89 /**
90  * Function called to validate a reply or a request.  For
91  * request evaluation, simply pass "NULL" for the @a reply_block.
92  *
93  * @param cls closure
94  * @param ctx context
95  * @param type block type
96  * @param group block group to check against
97  * @param eo control flags
98  * @param query original query (hash)
99  * @param xquery extended query data (can be NULL, depending on type)
100  * @param xquery_size number of bytes in @a xquery
101  * @param reply_block response to validate
102  * @param reply_block_size number of bytes in @a reply_block
103  * @return characterization of result
104  */
105 static enum GNUNET_BLOCK_EvaluationResult
106 block_plugin_dht_evaluate (void *cls,
107                            struct GNUNET_BLOCK_Context *ctx,
108                            enum GNUNET_BLOCK_Type type,
109                            struct GNUNET_BLOCK_Group *group,
110                            enum GNUNET_BLOCK_EvaluationOptions eo,
111                            const struct GNUNET_HashCode *query,
112                            const void *xquery,
113                            size_t xquery_size,
114                            const void *reply_block,
115                            size_t reply_block_size)
116 {
117   const struct GNUNET_HELLO_Message *hello;
118   struct GNUNET_PeerIdentity pid;
119   const struct GNUNET_MessageHeader *msg;
120   struct GNUNET_HashCode phash;
121
122   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
123     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
124   if (0 != xquery_size)
125   {
126     GNUNET_break_op (0);
127     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
128   }
129   if (NULL == reply_block)
130     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
131   if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
132   {
133     GNUNET_break_op (0);
134     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
135   }
136   msg = reply_block;
137   if (reply_block_size != ntohs (msg->size))
138   {
139     GNUNET_break_op (0);
140     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
141   }
142   hello = reply_block;
143   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
144   {
145     GNUNET_break_op (0);
146     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
147   }
148   GNUNET_CRYPTO_hash (&pid,
149                       sizeof (pid),
150                       &phash);
151   if (GNUNET_YES ==
152       GNUNET_BLOCK_GROUP_bf_test_and_set (group,
153                                           &phash))
154     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
155   return GNUNET_BLOCK_EVALUATION_OK_MORE;
156 }
157
158
159 /**
160  * Function called to obtain the key for a block.
161  *
162  * @param cls closure
163  * @param type block type
164  * @param block block to get the key for
165  * @param block_size number of bytes @a block
166  * @param[out] key set to the key (query) for the given block
167  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
168  *         (or if extracting a key from a block of this type does not work)
169  */
170 static int
171 block_plugin_dht_get_key (void *cls,
172                           enum GNUNET_BLOCK_Type type,
173                           const void *block,
174                           size_t block_size,
175                           struct GNUNET_HashCode *key)
176 {
177   const struct GNUNET_MessageHeader *msg;
178   const struct GNUNET_HELLO_Message *hello;
179   struct GNUNET_PeerIdentity *pid;
180
181   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
182     return GNUNET_SYSERR;
183   if (block_size < sizeof (struct GNUNET_MessageHeader))
184   {
185     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
186                      "block-dht",
187                      _("Block not of type %u\n"),
188                      GNUNET_BLOCK_TYPE_DHT_HELLO);
189     return GNUNET_NO;
190   }
191   msg = block;
192   if (block_size != ntohs (msg->size))
193   {
194     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
195                      "block-dht",
196                      _("Size mismatch for block\n"),
197                      GNUNET_BLOCK_TYPE_DHT_HELLO);
198     return GNUNET_NO;
199   }
200   hello = block;
201   memset (key, 0, sizeof (*key));
202   pid = (struct GNUNET_PeerIdentity *) key;
203   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
204   {
205     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
206                      "block-dht",
207                      _("Block of type %u is malformed\n"),
208                      GNUNET_BLOCK_TYPE_DHT_HELLO);
209     return GNUNET_NO;
210   }
211   return GNUNET_OK;
212 }
213
214
215 /**
216  * Entry point for the plugin.
217  */
218 void *
219 libgnunet_plugin_block_dht_init (void *cls)
220 {
221   static enum GNUNET_BLOCK_Type types[] =
222   {
223     GNUNET_BLOCK_TYPE_DHT_HELLO,
224     GNUNET_BLOCK_TYPE_ANY       /* end of list */
225   };
226   struct GNUNET_BLOCK_PluginFunctions *api;
227
228   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
229   api->evaluate = &block_plugin_dht_evaluate;
230   api->get_key = &block_plugin_dht_get_key;
231   api->create_group = &block_plugin_dht_create_group;
232   api->types = types;
233   return api;
234 }
235
236
237 /**
238  * Exit point from the plugin.
239  */
240 void *
241 libgnunet_plugin_block_dht_done (void *cls)
242 {
243   struct GNUNET_BLOCK_PluginFunctions *api = cls;
244
245   GNUNET_free (api);
246   return NULL;
247 }
248
249 /* end of plugin_block_dht.c */