diff options
Diffstat (limited to 'src/dht/plugin_block_dht.c')
-rw-r--r-- | src/dht/plugin_block_dht.c | 95 |
1 files changed, 69 insertions, 26 deletions
diff --git a/src/dht/plugin_block_dht.c b/src/dht/plugin_block_dht.c index 8bb533961..24f8b21b2 100644 --- a/src/dht/plugin_block_dht.c +++ b/src/dht/plugin_block_dht.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | Copyright (C) 2010 GNUnet e.V. | 3 | Copyright (C) 2010, 2017 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -25,25 +25,78 @@ | |||
25 | * DHT (see fs block plugin) | 25 | * DHT (see fs block plugin) |
26 | * @author Christian Grothoff | 26 | * @author Christian Grothoff |
27 | */ | 27 | */ |
28 | |||
29 | #include "platform.h" | 28 | #include "platform.h" |
30 | #include "gnunet_constants.h" | 29 | #include "gnunet_constants.h" |
31 | #include "gnunet_hello_lib.h" | 30 | #include "gnunet_hello_lib.h" |
32 | #include "gnunet_block_plugin.h" | 31 | #include "gnunet_block_plugin.h" |
32 | #include "gnunet_block_group_lib.h" | ||
33 | 33 | ||
34 | #define DEBUG_DHT GNUNET_EXTRA_LOGGING | 34 | #define DEBUG_DHT GNUNET_EXTRA_LOGGING |
35 | 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 | |||
36 | 89 | ||
37 | /** | 90 | /** |
38 | * Function called to validate a reply or a request. For | 91 | * Function called to validate a reply or a request. For |
39 | * request evaluation, simply pass "NULL" for the @a reply_block. | 92 | * request evaluation, simply pass "NULL" for the @a reply_block. |
40 | * | 93 | * |
41 | * @param cls closure | 94 | * @param cls closure |
95 | * @param ctx context | ||
42 | * @param type block type | 96 | * @param type block type |
97 | * @param group block group to check against | ||
43 | * @param eo control flags | 98 | * @param eo control flags |
44 | * @param query original query (hash) | 99 | * @param query original query (hash) |
45 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
46 | * @param bf_mutator mutation value for @a bf | ||
47 | * @param xquery extended query data (can be NULL, depending on type) | 100 | * @param xquery extended query data (can be NULL, depending on type) |
48 | * @param xquery_size number of bytes in @a xquery | 101 | * @param xquery_size number of bytes in @a xquery |
49 | * @param reply_block response to validate | 102 | * @param reply_block response to validate |
@@ -52,17 +105,16 @@ | |||
52 | */ | 105 | */ |
53 | static enum GNUNET_BLOCK_EvaluationResult | 106 | static enum GNUNET_BLOCK_EvaluationResult |
54 | block_plugin_dht_evaluate (void *cls, | 107 | block_plugin_dht_evaluate (void *cls, |
108 | struct GNUNET_BLOCK_Context *ctx, | ||
55 | enum GNUNET_BLOCK_Type type, | 109 | enum GNUNET_BLOCK_Type type, |
110 | struct GNUNET_BLOCK_Group *group, | ||
56 | enum GNUNET_BLOCK_EvaluationOptions eo, | 111 | enum GNUNET_BLOCK_EvaluationOptions eo, |
57 | const struct GNUNET_HashCode *query, | 112 | const struct GNUNET_HashCode *query, |
58 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
59 | int32_t bf_mutator, | ||
60 | const void *xquery, | 113 | const void *xquery, |
61 | size_t xquery_size, | 114 | size_t xquery_size, |
62 | const void *reply_block, | 115 | const void *reply_block, |
63 | size_t reply_block_size) | 116 | size_t reply_block_size) |
64 | { | 117 | { |
65 | struct GNUNET_HashCode mhash; | ||
66 | const struct GNUNET_HELLO_Message *hello; | 118 | const struct GNUNET_HELLO_Message *hello; |
67 | struct GNUNET_PeerIdentity pid; | 119 | struct GNUNET_PeerIdentity pid; |
68 | const struct GNUNET_MessageHeader *msg; | 120 | const struct GNUNET_MessageHeader *msg; |
@@ -75,8 +127,7 @@ block_plugin_dht_evaluate (void *cls, | |||
75 | GNUNET_break_op (0); | 127 | GNUNET_break_op (0); |
76 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; | 128 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; |
77 | } | 129 | } |
78 | if ( (NULL == reply_block) || | 130 | if (NULL == reply_block) |
79 | (0 == reply_block_size) ) | ||
80 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | 131 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; |
81 | if (reply_block_size < sizeof (struct GNUNET_MessageHeader)) | 132 | if (reply_block_size < sizeof (struct GNUNET_MessageHeader)) |
82 | { | 133 | { |
@@ -95,22 +146,13 @@ block_plugin_dht_evaluate (void *cls, | |||
95 | GNUNET_break_op (0); | 146 | GNUNET_break_op (0); |
96 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; | 147 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; |
97 | } | 148 | } |
98 | if (NULL != bf) | 149 | GNUNET_CRYPTO_hash (&pid, |
99 | { | 150 | sizeof (pid), |
100 | GNUNET_CRYPTO_hash (&pid, sizeof (pid), &phash); | 151 | &phash); |
101 | GNUNET_BLOCK_mingle_hash (&phash, bf_mutator, &mhash); | 152 | if (GNUNET_YES == |
102 | if (NULL != *bf) | 153 | GNUNET_BLOCK_GROUP_bf_test_and_set (group, |
103 | { | 154 | &phash)) |
104 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash)) | 155 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; |
105 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, | ||
110 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
111 | } | ||
112 | GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); | ||
113 | } | ||
114 | return GNUNET_BLOCK_EVALUATION_OK_MORE; | 156 | return GNUNET_BLOCK_EVALUATION_OK_MORE; |
115 | } | 157 | } |
116 | 158 | ||
@@ -183,6 +225,7 @@ libgnunet_plugin_block_dht_init (void *cls) | |||
183 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | 225 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); |
184 | api->evaluate = &block_plugin_dht_evaluate; | 226 | api->evaluate = &block_plugin_dht_evaluate; |
185 | api->get_key = &block_plugin_dht_get_key; | 227 | api->get_key = &block_plugin_dht_get_key; |
228 | api->create_group = &block_plugin_dht_create_group; | ||
186 | api->types = types; | 229 | api->types = types; |
187 | return api; | 230 | return api; |
188 | } | 231 | } |
@@ -194,7 +237,7 @@ libgnunet_plugin_block_dht_init (void *cls) | |||
194 | void * | 237 | void * |
195 | libgnunet_plugin_block_dht_done (void *cls) | 238 | libgnunet_plugin_block_dht_done (void *cls) |
196 | { | 239 | { |
197 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | 240 | struct GNUNET_BLOCK_PluginFunctions *api = cls; |
198 | 241 | ||
199 | GNUNET_free (api); | 242 | GNUNET_free (api); |
200 | return NULL; | 243 | return NULL; |