aboutsummaryrefslogtreecommitdiff
path: root/src/gns/plugin_block_gns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gns/plugin_block_gns.c')
-rw-r--r--src/gns/plugin_block_gns.c132
1 files changed, 26 insertions, 106 deletions
diff --git a/src/gns/plugin_block_gns.c b/src/gns/plugin_block_gns.c
index 407754a8c..fd9c99cb4 100644
--- a/src/gns/plugin_block_gns.c
+++ b/src/gns/plugin_block_gns.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-2013 GNUnet e.V. 3 Copyright (C) 2010-2013, 2021, 2022 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -92,98 +92,16 @@ block_plugin_gns_create_group (void *cls,
92 92
93 93
94/** 94/**
95 * Function called to validate a reply or a request. For
96 * request evaluation, simply pass "NULL" for the reply_block.
97 * Note that it is assumed that the reply has already been
98 * matched to the key (and signatures checked) as it would
99 * be done with the "get_key" function.
100 *
101 * @param cls closure
102 * @param ctx block context
103 * @param type block type
104 * @param bg block group to use for evaluation
105 * @param eo control flags
106 * @param query original query (hash)
107 * @param xquery extrended query data (can be NULL, depending on @a type)
108 * @param xquery_size number of bytes in @a xquery
109 * @param reply_block response to validate
110 * @param reply_block_size number of bytes in @a reply_block
111 * @return characterization of result
112 */
113static enum GNUNET_BLOCK_EvaluationResult
114block_plugin_gns_evaluate (void *cls,
115 struct GNUNET_BLOCK_Context *ctx,
116 enum GNUNET_BLOCK_Type type,
117 struct GNUNET_BLOCK_Group *bg,
118 enum GNUNET_BLOCK_EvaluationOptions eo,
119 const struct GNUNET_HashCode *query,
120 const void *xquery,
121 size_t xquery_size,
122 const void *reply_block,
123 size_t reply_block_size)
124{
125 const struct GNUNET_GNSRECORD_Block *block;
126 struct GNUNET_HashCode h;
127 struct GNUNET_HashCode chash;
128
129 if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
130 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
131 if (NULL == reply_block)
132 {
133 if (0 != xquery_size)
134 {
135 GNUNET_break_op (0);
136 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137 }
138 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
139 }
140
141 /* this is a reply */
142 if (reply_block_size < sizeof(struct GNUNET_GNSRECORD_Block))
143 {
144 GNUNET_break_op (0);
145 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
146 }
147 block = reply_block;
148 if (GNUNET_GNSRECORD_block_get_size (block) > reply_block_size)
149 {
150 GNUNET_break_op (0);
151 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
152 }
153 GNUNET_GNSRECORD_query_from_block (block,
154 &h);
155 if (0 != GNUNET_memcmp (&h, query))
156 {
157 GNUNET_break_op (0);
158 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
159 }
160 if (GNUNET_OK !=
161 GNUNET_GNSRECORD_block_verify (block))
162 {
163 GNUNET_break_op (0);
164 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
165 }
166 GNUNET_CRYPTO_hash (reply_block,
167 reply_block_size,
168 &chash);
169 if (GNUNET_YES ==
170 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
171 &chash))
172 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
173 return GNUNET_BLOCK_EVALUATION_OK_MORE;
174}
175
176
177/**
178 * Function called to obtain the key for a block. 95 * Function called to obtain the key for a block.
96 * If the @a block is malformed, the function should
97 * zero-out @a key and return #GNUNET_OK.
179 * 98 *
180 * @param cls closure 99 * @param cls closure
181 * @param type block type 100 * @param type block type
182 * @param reply_block block to get the key for 101 * @param reply_block block to get the key for
183 * @param reply_block_size number of bytes in @a reply_block 102 * @param reply_block_size number of bytes in @a reply_block
184 * @param key set to the key (query) for the given block 103 * @param key set to the key (query) for the given block
185 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported 104 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported, #GNUNET_NO if extracting a key from a block of this type does not work
186 * (or if extracting a key from a block of this type does not work)
187 */ 105 */
188static enum GNUNET_GenericReturnValue 106static enum GNUNET_GenericReturnValue
189block_plugin_gns_get_key (void *cls, 107block_plugin_gns_get_key (void *cls,
@@ -194,12 +112,18 @@ block_plugin_gns_get_key (void *cls,
194{ 112{
195 const struct GNUNET_GNSRECORD_Block *block; 113 const struct GNUNET_GNSRECORD_Block *block;
196 114
197 if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD) 115 if (GNUNET_BLOCK_TYPE_GNS_NAMERECORD != type)
116 {
117 GNUNET_break (0);
198 return GNUNET_SYSERR; 118 return GNUNET_SYSERR;
119 }
199 if (reply_block_size < sizeof(struct GNUNET_GNSRECORD_Block)) 120 if (reply_block_size < sizeof(struct GNUNET_GNSRECORD_Block))
200 { 121 {
201 GNUNET_break_op (0); 122 GNUNET_break_op (0);
202 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 123 memset (key,
124 0,
125 sizeof (*key));
126 return GNUNET_OK;
203 } 127 }
204 block = reply_block; 128 block = reply_block;
205 GNUNET_GNSRECORD_query_from_block (block, 129 GNUNET_GNSRECORD_query_from_block (block,
@@ -227,8 +151,11 @@ block_plugin_gns_check_query (void *cls,
227 const void *xquery, 151 const void *xquery,
228 size_t xquery_size) 152 size_t xquery_size)
229{ 153{
230 if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD) 154 if (GNUNET_BLOCK_TYPE_GNS_NAMERECORD != type)
155 {
156 GNUNET_break (0);
231 return GNUNET_SYSERR; 157 return GNUNET_SYSERR;
158 }
232 if (0 != xquery_size) 159 if (0 != xquery_size)
233 { 160 {
234 GNUNET_break_op (0); 161 GNUNET_break_op (0);
@@ -243,7 +170,6 @@ block_plugin_gns_check_query (void *cls,
243 * 170 *
244 * @param cls closure 171 * @param cls closure
245 * @param type block type 172 * @param type block type
246 * @param query key for the block (hash), must match exactly
247 * @param block block data to validate 173 * @param block block data to validate
248 * @param block_size number of bytes in @a block 174 * @param block_size number of bytes in @a block
249 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not 175 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not
@@ -251,14 +177,16 @@ block_plugin_gns_check_query (void *cls,
251static enum GNUNET_GenericReturnValue 177static enum GNUNET_GenericReturnValue
252block_plugin_gns_check_block (void *cls, 178block_plugin_gns_check_block (void *cls,
253 enum GNUNET_BLOCK_Type type, 179 enum GNUNET_BLOCK_Type type,
254 const struct GNUNET_HashCode *query,
255 const void *block, 180 const void *block,
256 size_t block_size) 181 size_t block_size)
257{ 182{
258 const struct GNUNET_GNSRECORD_Block *gblock; 183 const struct GNUNET_GNSRECORD_Block *gblock;
259 184
260 if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD) 185 if (GNUNET_BLOCK_TYPE_GNS_NAMERECORD != type)
186 {
187 GNUNET_break (0);
261 return GNUNET_SYSERR; 188 return GNUNET_SYSERR;
189 }
262 if (block_size < sizeof(struct GNUNET_GNSRECORD_Block)) 190 if (block_size < sizeof(struct GNUNET_GNSRECORD_Block))
263 { 191 {
264 GNUNET_break_op (0); 192 GNUNET_break_op (0);
@@ -306,23 +234,16 @@ block_plugin_gns_check_reply (void *cls,
306 const void *reply_block, 234 const void *reply_block,
307 size_t reply_block_size) 235 size_t reply_block_size)
308{ 236{
309 const struct GNUNET_GNSRECORD_Block *block; 237 const struct GNUNET_GNSRECORD_Block *block = reply_block;
310 struct GNUNET_HashCode chash; 238 struct GNUNET_HashCode chash;
311 239
312 if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD) 240 if (GNUNET_BLOCK_TYPE_GNS_NAMERECORD != type)
313 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
314 /* this is a reply */
315 if (reply_block_size < sizeof(struct GNUNET_GNSRECORD_Block))
316 { 241 {
317 GNUNET_break_op (0); 242 GNUNET_break (0);
318 return GNUNET_BLOCK_REPLY_INVALID; 243 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
319 }
320 block = reply_block;
321 if (GNUNET_GNSRECORD_block_get_size (block) > reply_block_size)
322 {
323 GNUNET_break_op (0);
324 return GNUNET_BLOCK_REPLY_INVALID;
325 } 244 }
245 GNUNET_assert (reply_block_size >= sizeof(struct GNUNET_GNSRECORD_Block));
246 GNUNET_assert (GNUNET_GNSRECORD_block_get_size (block) > reply_block_size);
326 GNUNET_CRYPTO_hash (reply_block, 247 GNUNET_CRYPTO_hash (reply_block,
327 reply_block_size, 248 reply_block_size,
328 &chash); 249 &chash);
@@ -347,7 +268,6 @@ libgnunet_plugin_block_gns_init (void *cls)
347 struct GNUNET_BLOCK_PluginFunctions *api; 268 struct GNUNET_BLOCK_PluginFunctions *api;
348 269
349 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); 270 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
350 api->evaluate = &block_plugin_gns_evaluate;
351 api->get_key = &block_plugin_gns_get_key; 271 api->get_key = &block_plugin_gns_get_key;
352 api->create_group = &block_plugin_gns_create_group; 272 api->create_group = &block_plugin_gns_create_group;
353 api->check_query = &block_plugin_gns_check_query; 273 api->check_query = &block_plugin_gns_check_query;