More API function tests...
[oweals/gnunet.git] / src / dht / plugin_block_dht.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
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  * How big is the BF we use for DHT blocks?
38  */
39 #define DHT_BF_SIZE 8
40
41
42 /**
43  * Create a new block group.
44  *
45  * @param ctx block context in which the block group is created
46  * @param type type of the block for which we are creating the group
47  * @param nonce random value used to seed the group creation
48  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
49  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
50  * @param va variable arguments specific to @a type
51  * @return block group handle, NULL if block groups are not supported
52  *         by this @a type of block (this is not an error)
53  */
54 static struct GNUNET_BLOCK_Group *
55 block_plugin_dht_create_group (void *cls,
56                                enum GNUNET_BLOCK_Type type,
57                                uint32_t nonce,
58                                const void *raw_data,
59                                size_t raw_data_size,
60                                va_list va)
61 {
62   return GNUNET_BLOCK_GROUP_bf_create (cls,
63                                        DHT_BF_SIZE,
64                                        GNUNET_CONSTANTS_BLOOMFILTER_K,
65                                        type,
66                                        nonce,
67                                        raw_data,
68                                        raw_data_size);
69 }
70
71
72 /**
73  * Function called to validate a reply or a request.  For
74  * request evaluation, simply pass "NULL" for the @a reply_block.
75  *
76  * @param cls closure
77  * @param type block type
78  * @param group block group to check against
79  * @param eo control flags
80  * @param query original query (hash)
81  * @param xquery extended query data (can be NULL, depending on type)
82  * @param xquery_size number of bytes in @a xquery
83  * @param reply_block response to validate
84  * @param reply_block_size number of bytes in @a reply_block
85  * @return characterization of result
86  */
87 static enum GNUNET_BLOCK_EvaluationResult
88 block_plugin_dht_evaluate (void *cls,
89                            enum GNUNET_BLOCK_Type type,
90                            struct GNUNET_BLOCK_Group *group,
91                            enum GNUNET_BLOCK_EvaluationOptions eo,
92                            const struct GNUNET_HashCode *query,
93                            const void *xquery,
94                            size_t xquery_size,
95                            const void *reply_block,
96                            size_t reply_block_size)
97 {
98   const struct GNUNET_HELLO_Message *hello;
99   struct GNUNET_PeerIdentity pid;
100   const struct GNUNET_MessageHeader *msg;
101   struct GNUNET_HashCode phash;
102
103   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
104     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
105   if (0 != xquery_size)
106   {
107     GNUNET_break_op (0);
108     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
109   }
110   if (NULL == reply_block)
111     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
112   if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
113   {
114     GNUNET_break_op (0);
115     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
116   }
117   msg = reply_block;
118   if (reply_block_size != ntohs (msg->size))
119   {
120     GNUNET_break_op (0);
121     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
122   }
123   hello = reply_block;
124   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
125   {
126     GNUNET_break_op (0);
127     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
128   }
129   GNUNET_CRYPTO_hash (&pid,
130                       sizeof (pid),
131                       &phash);
132   if (GNUNET_YES ==
133       GNUNET_BLOCK_GROUP_bf_test_and_set (group,
134                                           &phash))
135     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
136   return GNUNET_BLOCK_EVALUATION_OK_MORE;
137 }
138
139
140 /**
141  * Function called to obtain the key for a block.
142  *
143  * @param cls closure
144  * @param type block type
145  * @param block block to get the key for
146  * @param block_size number of bytes @a block
147  * @param[out] key set to the key (query) for the given block
148  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
149  *         (or if extracting a key from a block of this type does not work)
150  */
151 static int
152 block_plugin_dht_get_key (void *cls,
153                           enum GNUNET_BLOCK_Type type,
154                           const void *block,
155                           size_t block_size,
156                           struct GNUNET_HashCode *key)
157 {
158   const struct GNUNET_MessageHeader *msg;
159   const struct GNUNET_HELLO_Message *hello;
160   struct GNUNET_PeerIdentity *pid;
161
162   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
163     return GNUNET_SYSERR;
164   if (block_size < sizeof (struct GNUNET_MessageHeader))
165   {
166     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
167                      _("Block not of type %u\n"), GNUNET_BLOCK_TYPE_DHT_HELLO);
168     return GNUNET_NO;
169   }
170   msg = block;
171   if (block_size != ntohs (msg->size))
172   {
173     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
174                      _("Size mismatch for block\n"),
175                      GNUNET_BLOCK_TYPE_DHT_HELLO);
176     return GNUNET_NO;
177   }
178   hello = block;
179   memset (key, 0, sizeof (*key));
180   pid = (struct GNUNET_PeerIdentity *) key;
181   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
182   {
183     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
184                      _("Block of type %u is malformed\n"),
185                      GNUNET_BLOCK_TYPE_DHT_HELLO);
186     return GNUNET_NO;
187   }
188   return GNUNET_OK;
189 }
190
191
192 /**
193  * Entry point for the plugin.
194  */
195 void *
196 libgnunet_plugin_block_dht_init (void *cls)
197 {
198   static enum GNUNET_BLOCK_Type types[] =
199   {
200     GNUNET_BLOCK_TYPE_DHT_HELLO,
201     GNUNET_BLOCK_TYPE_ANY       /* end of list */
202   };
203   struct GNUNET_BLOCK_PluginFunctions *api;
204
205   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
206   api->evaluate = &block_plugin_dht_evaluate;
207   api->get_key = &block_plugin_dht_get_key;
208   api->create_group = &block_plugin_dht_create_group;
209   api->types = types;
210   return api;
211 }
212
213
214 /**
215  * Exit point from the plugin.
216  */
217 void *
218 libgnunet_plugin_block_dht_done (void *cls)
219 {
220   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
221
222   GNUNET_free (api);
223   return NULL;
224 }
225
226 /* end of plugin_block_dht.c */