From eef168bdbf40609b944dada415fc536e15cac494 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 20 Jun 2012 09:36:46 +0000 Subject: [PATCH] -towards block plugin for mesh --- src/block/plugin_block_test.c | 6 +- src/fs/plugin_block_fs.c | 1 - src/include/gnunet_block_lib.h | 7 ++- src/mesh/plugin_block_mesh.c | 77 ++++++++++++++++++++---- src/namestore/gnunet-service-namestore.c | 37 +++++++----- 5 files changed, 95 insertions(+), 33 deletions(-) diff --git a/src/block/plugin_block_test.c b/src/block/plugin_block_test.c index e6b4f4960..dacf04535 100644 --- a/src/block/plugin_block_test.c +++ b/src/block/plugin_block_test.c @@ -28,8 +28,6 @@ #include "platform.h" #include "gnunet_block_plugin.h" -#define DEBUG_TEST GNUNET_EXTRA_LOGGING - /** * Number of bits we set per entry in the bloomfilter. @@ -63,9 +61,9 @@ block_plugin_test_evaluate (void *cls, enum GNUNET_BLOCK_Type type, struct GNUNET_HashCode chash; struct GNUNET_HashCode mhash; - if (type != GNUNET_BLOCK_TYPE_TEST) + if ( GNUNET_BLOCK_TYPE_TEST != type) return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; - if (xquery_size != 0) + if (0 != xquery_size) { GNUNET_break_op (0); return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; diff --git a/src/fs/plugin_block_fs.c b/src/fs/plugin_block_fs.c index c43a5de5e..2e4f7ef87 100644 --- a/src/fs/plugin_block_fs.c +++ b/src/fs/plugin_block_fs.c @@ -29,7 +29,6 @@ #include "block_fs.h" #include "gnunet_signatures.h" -#define DEBUG_FS_BLOCK GNUNET_EXTRA_LOGGING /** * Number of bits we set per entry in the bloomfilter. diff --git a/src/include/gnunet_block_lib.h b/src/include/gnunet_block_lib.h index d651476a8..5498fe74d 100644 --- a/src/include/gnunet_block_lib.h +++ b/src/include/gnunet_block_lib.h @@ -103,7 +103,12 @@ enum GNUNET_BLOCK_Type /** * Block for storing mesh peers */ - GNUNET_BLOCK_TYPE_MESH_PEER = 20 + GNUNET_BLOCK_TYPE_MESH_PEER = 20, + + /** + * Block for finding peers by type + */ + GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE = 21 }; diff --git a/src/mesh/plugin_block_mesh.c b/src/mesh/plugin_block_mesh.c index c5bb458bd..8b601652f 100644 --- a/src/mesh/plugin_block_mesh.c +++ b/src/mesh/plugin_block_mesh.c @@ -26,8 +26,14 @@ #include "platform.h" #include "gnunet_block_plugin.h" +#include "block_mesh.h" + +/** + * Number of bits we set per entry in the bloomfilter. + * Do not change! + */ +#define BLOOMFILTER_K 16 -#define DEBUG_MESH_BLOCK GNUNET_EXTRA_LOGGING /** * Function called to validate a reply or a request. For @@ -55,16 +61,52 @@ block_plugin_mesh_evaluate (void *cls, enum GNUNET_BLOCK_Type type, size_t xquery_size, const void *reply_block, size_t reply_block_size) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Evaluate called\n"); - if (GNUNET_BLOCK_TYPE_MESH_PEER == type) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Type MESH PEER\n"); - } - else + struct GNUNET_HashCode chash; + struct GNUNET_HashCode mhash; + + switch (type) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Other type\n"); + case GNUNET_BLOCK_TYPE_MESH_PEER: + if (0 != xquery_size) + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; + } + if (NULL == reply_block) + return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; + if (sizeof (struct PBlock) != reply_block_size) + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + return GNUNET_BLOCK_EVALUATION_OK_LAST; + case GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE: + /* FIXME: have an xquery? not sure */ + if (0 != xquery_size) + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; + } + if (NULL == reply_block) + return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; + if (sizeof (struct PBlock) != reply_block_size) + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + if (NULL != bf) + { + GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash); + GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash); + if (NULL != *bf) + { + if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash)) + return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; + } + else + { + *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K); + } + GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); + } + return GNUNET_BLOCK_EVALUATION_OK_MORE; + default: + return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; } - return GNUNET_BLOCK_EVALUATION_OK_LAST; } @@ -84,8 +126,20 @@ block_plugin_mesh_get_key (void *cls, enum GNUNET_BLOCK_Type type, const void *block, size_t block_size, struct GNUNET_HashCode * key) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get key called\n"); - return GNUNET_SYSERR; + const struct PBlock *pb; + + switch (type) + { + case GNUNET_BLOCK_TYPE_MESH_PEER: + if (sizeof (struct PBlock) != block_size) + return GNUNET_SYSERR; + pb = block; + *key = pb->id.hashPubKey; + return GNUNET_OK; + // FIXME: other types... + default: + return GNUNET_SYSERR; + } } @@ -98,6 +152,7 @@ libgnunet_plugin_block_mesh_init (void *cls) static enum GNUNET_BLOCK_Type types[] = { GNUNET_BLOCK_TYPE_MESH_PEER, + GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE, GNUNET_BLOCK_TYPE_ANY /* end of list */ }; struct GNUNET_BLOCK_PluginFunctions *api; diff --git a/src/namestore/gnunet-service-namestore.c b/src/namestore/gnunet-service-namestore.c index e67ffaa65..250e2b812 100644 --- a/src/namestore/gnunet-service-namestore.c +++ b/src/namestore/gnunet-service-namestore.c @@ -1137,31 +1137,34 @@ handle_record_remove_it (void *cls, rrc->op_res = 0; } -static void handle_record_remove (void *cls, - struct GNUNET_SERVER_Client * client, - const struct GNUNET_MessageHeader * message) + +static void +handle_record_remove (void *cls, + struct GNUNET_SERVER_Client * client, + const struct GNUNET_MessageHeader * message) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE"); struct GNUNET_NAMESTORE_Client *nc; + const struct RecordRemoveMessage * rr_msg; struct RecordRemoveResponseMessage rrr_msg; struct GNUNET_CRYPTO_RsaPrivateKey *pkey; - struct GNUNET_NAMESTORE_CryptoContainer *cc = NULL; + struct GNUNET_NAMESTORE_CryptoContainer *cc; struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub; struct GNUNET_CRYPTO_ShortHashCode pubkey_hash; struct GNUNET_HashCode long_hash; - char * pkey_tmp = NULL; - char * name_tmp = NULL; - char * rd_ser = NULL; - size_t key_len = 0; - size_t name_len = 0; - size_t rd_ser_len = 0; - size_t msg_size = 0; + const char * pkey_tmp; + const char * name_tmp; + const char * rd_ser; + size_t key_len; + size_t name_len; + size_t rd_ser_len; + size_t msg_size; size_t msg_size_exp = 0; uint32_t rd_count; - uint32_t rid = 0; + uint32_t rid; int res = GNUNET_SYSERR; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE"); if (ntohs (message->size) < sizeof (struct RecordRemoveMessage)) { GNUNET_break_op (0); @@ -1177,7 +1180,7 @@ static void handle_record_remove (void *cls, return; } - struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message; + rr_msg = (const struct RecordRemoveMessage *) message; rid = ntohl (rr_msg->gns_header.r_id); name_len = ntohs (rr_msg->name_len); rd_ser_len = ntohs (rr_msg->rd_len); @@ -1208,7 +1211,7 @@ static void handle_record_remove (void *cls, return; } - pkey_tmp = (char *) &rr_msg[1]; + pkey_tmp = (const char *) &rr_msg[1]; name_tmp = &pkey_tmp[key_len]; rd_ser = &name_tmp[name_len]; @@ -1236,7 +1239,9 @@ static void handle_record_remove (void *cls, if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(zonekeys, &long_hash)) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received new private key for zone `%s'\n",GNUNET_short_h2s(&pubkey_hash)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received new private key for zone `%s'\n", + GNUNET_short_h2s(&pubkey_hash)); cc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer)); cc->privkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len); cc->pubkey = GNUNET_malloc(sizeof (pub)); -- 2.25.1