pubkey
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_experiments.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 experimentation/gnunet-daemon-experimentation_experiments.c
23  * @brief experimentation daemon: experiment management
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-daemon-experimentation.h"
33
34 struct Experiment
35 {
36         /* Header */
37         char *name;
38
39         /* Experiment issuer */
40         struct GNUNET_PeerIdentity issuer;
41
42         /* Experiment version as timestamp of creation */
43         struct GNUNET_TIME_Absolute version;
44
45         /* Description */
46         char *description;
47
48         /* Required capabilities  */
49         uint32_t required_capabilities;
50
51         /* Experiment itself */
52
53         /* TBD */
54 };
55
56 struct Issuer
57 {
58         struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey;
59 };
60
61 /**
62  * Hashmap containing valid experiment issuer
63  */
64 static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
65
66 /**
67  * Hashmap containing valid experiments
68  */
69 static struct GNUNET_CONTAINER_MultiHashMap *experiments;
70
71 /**
72  * Verify experiment signature
73  *
74  * @param i issuer
75  * @param e experiment
76  * @return GNUNET_OK or GNUNET_SYSERR
77  */
78
79 int
80 experiment_verify (struct Issuer *i, struct Experiment *e)
81 {
82         GNUNET_assert (NULL != i);
83         GNUNET_assert (NULL != e);
84
85         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Verification: to be implemented\n");
86         return GNUNET_OK;
87 }
88
89 int free_experiment (void *cls,
90                                                                                  const struct GNUNET_HashCode * key,
91                                                                                  void *value)
92 {
93         struct Experiment *e = value;
94         GNUNET_CONTAINER_multihashmap_remove (experiments, key, value);
95         GNUNET_free_non_null (e->description);
96         GNUNET_free_non_null (e->name);
97         GNUNET_free (e);
98         return GNUNET_OK;
99 }
100
101 int free_issuer (void *cls,
102                                                                  const struct GNUNET_HashCode * key,
103                                                                  void *value)
104 {
105         struct Issuer *i = value;
106         GNUNET_CONTAINER_multihashmap_remove (valid_issuers, key, value);
107         GNUNET_free (i);
108         return GNUNET_OK;
109 }
110
111 /**
112  * Is peer a valid issuer
113  *
114  * @return GNUNET_YES or GNUNET_NO
115  */
116 int
117 GNUNET_EXPERIMENTATION_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
118 {
119         if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &issuer_ID->hashPubKey))
120                 return GNUNET_YES;
121         else
122                 return GNUNET_NO;
123 }
124
125
126 void exp_file_iterator (void *cls,
127                                                                                                 const char *section)
128 {
129         struct GNUNET_CONFIGURATION_Handle *exp = cls;
130         struct Experiment *e;
131         struct Issuer *i;
132
133         char *val;
134         unsigned long long number;
135
136         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing section `%s'\n", section);
137
138         e = GNUNET_malloc (sizeof (struct Experiment));
139         /* Mandatory fields */
140
141         /* Issuer */
142         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, section, "ISSUER", &val))
143         {
144                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer missing\n"), section);
145                         GNUNET_free (e);
146                         return;
147         }
148         if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &e->issuer.hashPubKey))
149         {
150                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer invalid\n"), section);
151                         GNUNET_free (val);
152                         GNUNET_free (e);
153                         return;
154         }
155         if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &e->issuer.hashPubKey)))
156         {
157                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer not accepted!\n"), section);
158                 GNUNET_free (val);
159                 GNUNET_free (e);
160                 return;
161         }
162         GNUNET_free (val);
163
164         /* Version */
165         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, section, "VERSION", &number))
166         {
167                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Version missing or invalid \n"), section);
168                         GNUNET_free (e);
169                         return;
170         }
171         e->version.abs_value = number;
172
173         /* Required capabilities */
174         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, section, "CAPABILITIES", &number))
175         {
176                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities missing \n"), section);
177                         GNUNET_free (e);
178                         return;
179         }
180         if (number > UINT32_MAX)
181         {
182                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities invalid \n"), section);
183                 GNUNET_free (e);
184                 return;
185         }
186         e->required_capabilities = number;
187         e->name = GNUNET_strdup (section);
188
189         /* Optional fields */
190         /* Description */
191         GNUNET_CONFIGURATION_get_value_string (exp, section, "DESCRIPTION", &e->description);
192
193         /* verify experiment */
194         if (GNUNET_SYSERR == experiment_verify (i, e))
195         {
196                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Experiment signature is invalid\n"), section);
197                         GNUNET_free (e);
198                         GNUNET_free_non_null (e->name);
199                         GNUNET_free_non_null (e->description);
200                         return;
201         }
202         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Adding experiment `%s'\n"), e->name);
203         GNUNET_CONTAINER_multihashmap_put (experiments, &e->issuer.hashPubKey, e, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
204   GNUNET_STATISTICS_set (GSE_stats, "# experiments", GNUNET_CONTAINER_multihashmap_size (experiments), GNUNET_NO);
205 }
206
207 /**
208  * Load experiments from file
209  *
210  * @param file source file
211  */
212
213 static void
214 load_file (const char * file)
215 {
216         struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
217         if (NULL == exp)
218                 return;
219
220         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
221         {
222                 GNUNET_CONFIGURATION_destroy (exp);
223                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
224                 return;
225         }
226
227         GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
228
229         GNUNET_CONFIGURATION_destroy (exp);
230 }
231
232 /**
233  * Start experiments management
234  */
235 int
236 GNUNET_EXPERIMENTATION_experiments_start ()
237 {
238         struct Issuer *i;
239         char *issuers;
240         char *file;
241         char *pubkey;
242         char *pos;
243         struct GNUNET_PeerIdentity issuer_ID;
244         struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
245         struct GNUNET_HashCode hash;
246
247         /* Load valid issuer */
248         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
249         {
250                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
251                         return GNUNET_SYSERR;
252         }
253
254         valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
255   for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
256   {
257
258                 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
259                 {
260                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
261                 }
262                 else
263                 {
264                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
265                                 i = GNUNET_malloc (sizeof (struct Issuer));
266                                 GNUNET_CONTAINER_multihashmap_put (valid_issuers, &issuer_ID.hashPubKey,
267                                                 i, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
268                                 i = NULL;
269                 }
270   }
271   GNUNET_free (issuers);
272
273   if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
274   {
275                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
276                 GNUNET_EXPERIMENTATION_experiments_stop ();
277                 return GNUNET_SYSERR;
278   }
279   GNUNET_STATISTICS_set (GSE_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);
280
281         if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
282         {
283                         if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_key_from_string(pubkey, strlen (pubkey), &pub))
284                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid public key `%s'\n"), pubkey);
285                         else
286                         {
287                                 GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
288                                 if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
289                                 {
290                                                 i->pubkey = pub;
291                                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
292                                 }
293                                 else
294                                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
295                         }
296                         GNUNET_free (pubkey);
297         }
298
299   experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
300   /* Load experiments from file */
301         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "EXPERIMENTS", &file))
302                 return GNUNET_OK;
303
304         if (GNUNET_YES != GNUNET_DISK_file_test (file))
305         {
306                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
307                 GNUNET_free (file);
308                         return GNUNET_OK;
309         }
310         load_file (file);
311         GNUNET_free (file);
312
313         return GNUNET_OK;
314 }
315
316
317
318 /**
319  * Stop experiments management
320  */
321 void
322 GNUNET_EXPERIMENTATION_experiments_stop ()
323 {
324         if (NULL != valid_issuers)
325         {
326                 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
327                 GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
328         }
329         valid_issuers = NULL;
330         if (NULL != experiments)
331         {
332                 GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
333                 GNUNET_CONTAINER_multihashmap_destroy (experiments);
334         }
335         experiments = NULL;
336 }
337
338 /* end of gnunet-daemon-experimentation_experiments.c */