2 This file is part of GNUnet
3 (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/plugin_block_gns.c
23 * @brief blocks used for GNS records
24 * @author Martin Schanzenbach
28 #include "gnunet_block_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "block_gns.h"
31 #include "gnunet_signatures.h"
34 * Number of bits we set per entry in the bloomfilter.
35 * Do not change! -from fs
37 #define BLOOMFILTER_K 16
40 * Function called to validate a reply or a request. For
41 * request evaluation, simply pass "NULL" for the reply_block.
42 * Note that it is assumed that the reply has already been
43 * matched to the key (and signatures checked) as it would
44 * be done with the "get_key" function.
47 * @param type block type
48 * @param query original query (hash)
49 * @param bf pointer to bloom filter associated with query; possibly updated (!)
50 * @param bf_mutator mutation value for bf
51 * @param xquery extrended query data (can be NULL, depending on type)
52 * @param xquery_size number of bytes in xquery
53 * @param reply_block response to validate
54 * @param reply_block_size number of bytes in reply block
55 * @return characterization of result
57 static enum GNUNET_BLOCK_EvaluationResult
58 block_plugin_gns_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
59 const GNUNET_HashCode * query,
60 struct GNUNET_CONTAINER_BloomFilter **bf,
61 int32_t bf_mutator, const void *xquery,
62 size_t xquery_size, const void *reply_block,
63 size_t reply_block_size)
65 if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
66 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
67 if (reply_block_size == 0)
68 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
71 GNUNET_HashCode pkey_hash;
72 GNUNET_HashCode query_key;
73 GNUNET_HashCode name_hash;
74 GNUNET_HashCode mhash;
75 GNUNET_HashCode chash;
76 struct GNSNameRecordBlock *nrb;
77 struct GNSRecordBlock *rb;
80 nrb = (struct GNSNameRecordBlock *)reply_block;
81 name = (char*)&nrb[1];
82 GNUNET_CRYPTO_hash(&nrb->public_key,
83 sizeof(nrb->public_key),
86 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
88 GNUNET_CRYPTO_hash_xor(&pkey_hash, &name_hash, &query_key);
90 /* Check query key against public key */
91 if (0 != GNUNET_CRYPTO_hash_cmp(query, &query_key))
92 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
94 rd_count = ntohl(nrb->rd_count);
96 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
99 uint32_t record_xquery = ntohl(*((uint32_t*)xquery));
100 rb = (struct GNSRecordBlock*)(&name[strlen(name) + 1]);
102 for (i=0; i<rd_count; i++)
104 rd[i].record_type = ntohl(rb->type);
106 GNUNET_TIME_absolute_ntoh(rb->expiration);
107 rd[i].data_size = ntohl(rb->data_length);
108 rd[i].flags = ntohl(rb->flags);
109 rd[i].data = (char*)&rb[1];
110 rb = (struct GNSRecordBlock *)((char*)&rb[1] + rd[i].data_size);
112 if (xquery_size == 0)
115 if (rd[i].record_type == record_xquery)
122 /*if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&nrb->public_key,
128 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Signature invalid\n");
129 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
132 //No record matches query
133 if ((xquery_size > 0) && (record_match == 0))
134 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
136 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Records match\n");
137 //FIXME do bf check before or after crypto??
140 GNUNET_CRYPTO_hash(reply_block, reply_block_size, &chash);
141 GNUNET_BLOCK_mingle_hash(&chash, bf_mutator, &mhash);
144 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Check BF\n");
145 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(*bf, &mhash))
146 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
150 *bf = GNUNET_CONTAINER_bloomfilter_init(NULL, 8, BLOOMFILTER_K);
152 GNUNET_CONTAINER_bloomfilter_add(*bf, &mhash);
154 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No dup\n");
155 return GNUNET_BLOCK_EVALUATION_OK_MORE;
160 * Function called to obtain the key for a block.
163 * @param type block type
164 * @param block block to get the key for
165 * @param block_size number of bytes in block
166 * @param 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)
171 block_plugin_gns_get_key (void *cls, enum GNUNET_BLOCK_Type type,
172 const void *block, size_t block_size,
173 GNUNET_HashCode * key)
175 if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
176 return GNUNET_SYSERR;
177 GNUNET_HashCode name_hash;
178 GNUNET_HashCode pkey_hash;
179 struct GNSNameRecordBlock *nrb = (struct GNSNameRecordBlock *)block;
181 GNUNET_CRYPTO_hash(&nrb[1], strlen((char*)&nrb[1]), &name_hash);
182 GNUNET_CRYPTO_hash(&nrb->public_key,
183 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
186 GNUNET_CRYPTO_hash_xor(&name_hash, &pkey_hash, key);
188 //FIXME calculate key from name and hash(pkey) here
194 * Entry point for the plugin.
197 libgnunet_plugin_block_gns_init (void *cls)
199 static enum GNUNET_BLOCK_Type types[] =
201 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
202 GNUNET_BLOCK_TYPE_ANY /* end of list */
204 struct GNUNET_BLOCK_PluginFunctions *api;
206 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
207 api->evaluate = &block_plugin_gns_evaluate;
208 api->get_key = &block_plugin_gns_get_key;
215 * Exit point from the plugin.
218 libgnunet_plugin_block_gns_done (void *cls)
220 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
226 /* end of plugin_block_gns.c */