- fix doxygen
[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 //Not taken until now
40 #define GNUNET_BLOCK_TYPE_GNS_NAMERECORD 11
41
42 /**
43  * Function called to validate a reply or a request.  For
44  * request evaluation, simply pass "NULL" for the reply_block.
45  * Note that it is assumed that the reply has already been
46  * matched to the key (and signatures checked) as it would
47  * be done with the "get_key" function.
48  *
49  * @param cls closure
50  * @param type block type
51  * @param query original query (hash)
52  * @param bf pointer to bloom filter associated with query; possibly updated (!)
53  * @param bf_mutator mutation value for bf
54  * @param xquery extrended query data (can be NULL, depending on type)
55  * @param xquery_size number of bytes in xquery
56  * @param reply_block response to validate
57  * @param reply_block_size number of bytes in reply block
58  * @return characterization of result
59  */
60 static enum GNUNET_BLOCK_EvaluationResult
61 block_plugin_gns_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
62                           const GNUNET_HashCode * query,
63                           struct GNUNET_CONTAINER_BloomFilter **bf,
64                           int32_t bf_mutator, const void *xquery,
65                           size_t xquery_size, const void *reply_block,
66                           size_t reply_block_size)
67 {
68   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
69     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
70   
71   char* name;
72   GNUNET_HashCode pkey_hash;
73   GNUNET_HashCode query_pkey;
74   GNUNET_HashCode name_hash;
75   GNUNET_HashCode mhash;
76   GNUNET_HashCode chash;
77   struct GNSNameRecordBlock *nrb;
78   struct GNSRecordBlock *rb;
79   uint32_t rd_count;
80
81   nrb = (struct GNSNameRecordBlock *)reply_block;
82
83   name = (char*)&nrb[1];
84
85   GNUNET_CRYPTO_hash(&nrb->public_key,
86                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
87                      &pkey_hash);
88
89   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
90
91   GNUNET_CRYPTO_hash_xor(query, &name_hash, &query_pkey);
92   
93   //Check query key against public key
94   if (0 != GNUNET_CRYPTO_hash_cmp(&query_pkey, &pkey_hash))
95     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
96
97   rd_count = ntohl(nrb->rd_count);
98
99   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
100   int i = 0;
101   int record_match = 0;
102   rb = (struct GNSRecordBlock*)(&nrb[1] + strlen(name) + 1);
103
104   for (i=0; i<rd_count; i++)
105   {
106     rd[i].record_type = ntohl(rb->type);
107     rd[i].expiration =
108       GNUNET_TIME_absolute_ntoh(rb->expiration);
109     rd[i].data_size = ntohl(rb->data_length);
110     rd[i].flags = ntohl(rb->flags);
111     rd[i].data = (char*)&rb[1];
112     rb = &rb[1] + rd[i].data_size;
113     if (xquery_size > 0 && (rd[i].record_type == *((uint32_t*)xquery)))
114       record_match++;
115   }
116   
117   //No record matches query
118   if (xquery_size > 0 && (record_match == 0))
119     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
120
121   if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&nrb->public_key,
122                                                       name,
123                                                       nrb->rd_count,
124                                                       rd,
125                                                       &nrb->signature))
126   {
127     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Signature invalid\n");
128     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
129   }
130
131   //FIXME do bf check before or after crypto??
132   if (NULL != bf)
133   {
134     GNUNET_CRYPTO_hash(reply_block, reply_block_size, &chash);
135     GNUNET_BLOCK_mingle_hash(&chash, bf_mutator, &mhash);
136     if (NULL != *bf)
137     {
138       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(*bf, &mhash))
139         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
140     }
141     else
142     {
143       *bf = GNUNET_CONTAINER_bloomfilter_init(NULL, 8, BLOOMFILTER_K);
144     }
145     GNUNET_CONTAINER_bloomfilter_add(*bf, &mhash);
146   }
147
148   return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
149 }
150
151
152 /**
153  * Function called to obtain the key for a block.
154  *
155  * @param cls closure
156  * @param type block type
157  * @param block block to get the key for
158  * @param block_size number of bytes in block
159  * @param key set to the key (query) for the given block
160  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
161  *         (or if extracting a key from a block of this type does not work)
162  */
163 static int
164 block_plugin_gns_get_key (void *cls, enum GNUNET_BLOCK_Type type,
165                          const void *block, size_t block_size,
166                          GNUNET_HashCode * key)
167 {
168   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
169     return GNUNET_NO;
170   GNUNET_HashCode name_hash;
171   GNUNET_HashCode pkey_hash;
172   struct GNSNameRecordBlock *nrb = (struct GNSNameRecordBlock *)block;
173
174   GNUNET_CRYPTO_hash(&nrb[1], strlen((char*)&nrb[1]), &name_hash);
175   GNUNET_CRYPTO_hash(&nrb->public_key,
176                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
177                      &pkey_hash);
178
179   GNUNET_CRYPTO_hash_xor(&name_hash, &pkey_hash, key);
180   
181   //FIXME calculate key from name and hash(pkey) here
182   return GNUNET_OK;
183 }
184
185
186 /**
187  * Entry point for the plugin.
188  */
189 void *
190 libgnunet_plugin_block_gns_init (void *cls)
191 {
192   static enum GNUNET_BLOCK_Type types[] =
193   {
194     GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
195     GNUNET_BLOCK_TYPE_ANY       /* end of list */
196   };
197   struct GNUNET_BLOCK_PluginFunctions *api;
198
199   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
200   api->evaluate = &block_plugin_gns_evaluate;
201   api->get_key = &block_plugin_gns_get_key;
202   api->types = types;
203   return api;
204 }
205
206
207 /**
208  * Exit point from the plugin.
209  */
210 void *
211 libgnunet_plugin_block_gns_done (void *cls)
212 {
213   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
214
215   GNUNET_free (api);
216   return NULL;
217 }
218
219 /* end of plugin_block_gns.c */