-large cleanup and bugfixes
[oweals/gnunet.git] / src / gns / plugin_block_gns.c
1 /*
2      This file is part of GNUnet
3      (C) 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file gns/plugin_block_gns.c
23  * @brief blocks used for GNS records
24  * @author Martin Schanzenbach
25  */
26
27 #include "platform.h"
28 #include "gnunet_block_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "block_gns.h"
31 #include "gnunet_signatures.h"
32
33 /**
34  * Number of bits we set per entry in the bloomfilter.
35  * Do not change! -from fs
36  */
37 #define BLOOMFILTER_K 16
38
39 /**
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.
45  *
46  * @param cls closure
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
56  */
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)
64 {
65   char* name;
66   GNUNET_HashCode pkey_hash;
67   GNUNET_HashCode query_key;
68   GNUNET_HashCode name_hash;
69   GNUNET_HashCode mhash;
70   GNUNET_HashCode chash;
71   struct GNSNameRecordBlock *nrb;
72   uint32_t rd_count;
73   char* rd_data = NULL;
74   int rd_len;
75   uint32_t record_xquery;
76   unsigned int record_match;
77
78   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
79              "BLOCK_TEST\n");
80
81   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
82     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
83   if (reply_block_size == 0)
84     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
85     
86   nrb = (struct GNSNameRecordBlock *)reply_block;
87   name = (char*)&nrb[1];
88   GNUNET_CRYPTO_hash(&nrb->public_key,
89                      sizeof(nrb->public_key),
90                      &pkey_hash);
91
92   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
93
94   GNUNET_CRYPTO_hash_xor(&pkey_hash, &name_hash, &query_key);
95   
96   struct GNUNET_CRYPTO_HashAsciiEncoded xor_exp;
97   struct GNUNET_CRYPTO_HashAsciiEncoded xor_got;
98   GNUNET_CRYPTO_hash_to_enc (&query_key, &xor_exp);
99   GNUNET_CRYPTO_hash_to_enc (query, &xor_got);
100
101   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
102              "BLOCK_TEST for %s got %s expected %s\n",
103              name, (char*) &xor_got, (char*) &xor_exp);
104
105   /* Check query key against public key */
106   if (0 != GNUNET_CRYPTO_hash_cmp(query, &query_key))
107     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
108   
109   record_match = 0;
110   rd_count = ntohl(nrb->rd_count);
111   rd_data = (char*)&nrb[1];
112   rd_data += strlen(name) + 1;
113   rd_len = reply_block_size - (strlen(name) + 1
114                                + sizeof(struct GNSNameRecordBlock));
115   {
116     struct GNUNET_NAMESTORE_RecordData rd[rd_count];
117     unsigned int i;
118     struct GNUNET_TIME_Absolute exp = GNUNET_TIME_absolute_get_forever();
119     
120     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_len,
121                                                                rd_data,
122                                                                rd_count,
123                                                                rd))
124     {
125       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
126                  "Data invalid (%d bytes, %d records)\n", rd_len, rd_count);
127       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
128     }
129
130     if (xquery_size < sizeof(uint32_t))
131       record_xquery = 0;
132     else
133       record_xquery = ntohl(*((uint32_t*)xquery));
134     
135     for (i=0; i<rd_count; i++)
136     {
137       
138       exp = GNUNET_TIME_absolute_min (exp, rd[i].expiration);
139       
140       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
141                  "Got record of size %d\n", rd[i].data_size);
142
143       if ((record_xquery != 0)
144           && (rd[i].record_type == record_xquery))
145       {
146         record_match++;
147       }
148     }
149     
150     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
151                "Verifying signature of %d records for name %s\n",
152                rd_count, name);
153
154     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&nrb->public_key,
155                                                         exp,
156                                                         name,
157                                                         rd_count,
158                                                         rd,
159                                                         &nrb->signature))
160     {
161       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Signature invalid\n");
162       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
163     }
164   }
165   
166   if ((record_xquery != 0) && (record_match == 0))
167   {
168     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
169                "No record matches query!\n");
170     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
171   }
172
173   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Records match\n");
174   
175   if (NULL != bf)
176   {
177     GNUNET_CRYPTO_hash(reply_block, reply_block_size, &chash);
178     GNUNET_BLOCK_mingle_hash(&chash, bf_mutator, &mhash);
179     if (NULL != *bf)
180     {
181       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Check BF\n");
182       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(*bf, &mhash))
183         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
184     }
185     else
186     {
187       *bf = GNUNET_CONTAINER_bloomfilter_init(NULL, 8, BLOOMFILTER_K);
188     }
189     GNUNET_CONTAINER_bloomfilter_add(*bf, &mhash);
190   }
191   return GNUNET_BLOCK_EVALUATION_OK_MORE;
192 }
193
194
195 /**
196  * Function called to obtain the key for a block.
197  *
198  * @param cls closure
199  * @param type block type
200  * @param block block to get the key for
201  * @param block_size number of bytes in block
202  * @param key set to the key (query) for the given block
203  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
204  *         (or if extracting a key from a block of this type does not work)
205  */
206 static int
207 block_plugin_gns_get_key (void *cls, enum GNUNET_BLOCK_Type type,
208                          const void *block, size_t block_size,
209                          GNUNET_HashCode * key)
210 {
211   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
212     return GNUNET_SYSERR;
213   GNUNET_HashCode name_hash;
214   GNUNET_HashCode pkey_hash;
215   struct GNSNameRecordBlock *nrb = (struct GNSNameRecordBlock *)block;
216
217   GNUNET_CRYPTO_hash(&nrb[1], strlen((char*)&nrb[1]), &name_hash);
218   GNUNET_CRYPTO_hash(&nrb->public_key,
219                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
220                      &pkey_hash);
221
222   GNUNET_CRYPTO_hash_xor(&name_hash, &pkey_hash, key);
223   
224   //FIXME calculate key from name and hash(pkey) here
225   return GNUNET_OK;
226 }
227
228
229 /**
230  * Entry point for the plugin.
231  */
232 void *
233 libgnunet_plugin_block_gns_init (void *cls)
234 {
235   static enum GNUNET_BLOCK_Type types[] =
236   {
237     GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
238     GNUNET_BLOCK_TYPE_ANY       /* end of list */
239   };
240   struct GNUNET_BLOCK_PluginFunctions *api;
241
242   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
243   api->evaluate = &block_plugin_gns_evaluate;
244   api->get_key = &block_plugin_gns_get_key;
245   api->types = types;
246   return api;
247 }
248
249
250 /**
251  * Exit point from the plugin.
252  */
253 void *
254 libgnunet_plugin_block_gns_done (void *cls)
255 {
256   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
257
258   GNUNET_free (api);
259   return NULL;
260 }
261
262 /* end of plugin_block_gns.c */