renaming stats and cfg
[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
35
36 /**
37  * Struct to store information about an experiment issuer
38  */
39 struct Issuer
40 {
41         struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey;
42 };
43
44
45 /**
46  * Hashmap containing valid experiment issuer
47  */
48 static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
49
50
51 /**
52  * Hashmap containing valid experiments
53  */
54 static struct GNUNET_CONTAINER_MultiHashMap *experiments;
55
56
57 uint32_t GSE_my_issuer_count;
58
59 /**
60  * Valid experiment issuer for this daemon
61  *
62  * Array Experimentation_Issuer with GSE_my_issuer_count elements
63  */
64 struct Experimentation_Issuer *GSE_my_issuer;
65
66
67 /**
68  * Verify experiment signature
69  *
70  * @param i issuer
71  * @param e experiment
72  * @return GNUNET_OK or GNUNET_SYSERR
73  */
74 int
75 experiment_verify (struct Issuer *i, struct Experiment *e)
76 {
77         GNUNET_assert (NULL != i);
78         GNUNET_assert (NULL != e);
79
80         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Verification: to be implemented\n");
81         return GNUNET_OK;
82 }
83
84 int free_experiment (void *cls,
85                                                                                  const struct GNUNET_HashCode * key,
86                                                                                  void *value)
87 {
88         struct Experiment *e = value;
89         GNUNET_CONTAINER_multihashmap_remove (experiments, key, value);
90         GNUNET_free_non_null (e->description);
91         GNUNET_free_non_null (e->name);
92         GNUNET_free (e);
93         return GNUNET_OK;
94 }
95
96
97 /**
98  * Free issuer element
99  *
100  * @param cls unused
101  * @param key the key
102  * @param value the issuer element to free
103  * @return GNUNET_OK to continue
104  */
105 int free_issuer (void *cls,
106                                                                  const struct GNUNET_HashCode * key,
107                                                                  void *value)
108 {
109         struct Issuer *i = value;
110         GNUNET_CONTAINER_multihashmap_remove (valid_issuers, key, value);
111         GNUNET_free (i);
112         return GNUNET_OK;
113 }
114
115 int create_issuer (void *cls,
116                                                                  const struct GNUNET_HashCode * key,
117                                                                  void *value)
118 {
119         static int i = 0;
120         GNUNET_assert (i < GSE_my_issuer_count);
121         GSE_my_issuer[i].issuer_id.hashPubKey = *key;
122
123         i++;
124         return GNUNET_OK;
125
126 }
127
128
129
130 /**
131  * Is peer a valid issuer
132  *
133  * @return GNUNET_YES or GNUNET_NO
134  */
135 int
136 GED_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
137 {
138         if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &issuer_ID->hashPubKey))
139                 return GNUNET_YES;
140         else
141                 return GNUNET_NO;
142 }
143
144 struct GetCtx
145 {
146         struct Node *n;
147         GNUNET_EXPERIMENTATION_experiments_get_cb get_cb;
148 };
149
150 static int
151 get_it (void *cls,
152                                 const struct GNUNET_HashCode * key,
153                                 void *value)
154 {
155         struct GetCtx *get_ctx = cls;
156         struct Experiment *e = value;
157
158         get_ctx->get_cb (get_ctx->n, e);
159
160         return GNUNET_OK;
161 }
162
163
164
165
166 void
167 GED_experiments_get (struct Node *n,
168                                                                                                                                                                 struct GNUNET_PeerIdentity *issuer,
169                                                                                                                                                                 GNUNET_EXPERIMENTATION_experiments_get_cb get_cb)
170 {
171         struct GetCtx get_ctx;
172
173         GNUNET_assert (NULL != n);
174         GNUNET_assert (NULL != experiments);
175         GNUNET_assert (NULL != get_cb);
176
177         get_ctx.n = n;
178         get_ctx.get_cb = get_cb;
179
180         GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
181                         &issuer->hashPubKey, &get_it, &get_ctx);
182
183         get_cb (n, NULL);
184 }
185
186 /**
187  * Add a new experiment
188  */
189 int GNUNET_EXPERIMENTATION_experiments_add (struct Issuer *i,
190                                                                                                                                                                                 const char *name,
191                                                                                                                                                                                 struct GNUNET_PeerIdentity issuer_id,
192                                                                                                                                                                                 struct GNUNET_TIME_Absolute version,
193                                                                                                                                                                                 char *description,
194                                                                                                                                                                                 uint32_t required_capabilities,
195                                                                                                                                                                                 struct GNUNET_TIME_Absolute start,
196                                                                                                                                                                                 struct GNUNET_TIME_Relative frequency,
197                                                                                                                                                                                 struct GNUNET_TIME_Relative duration,
198                                                                                                                                                                                 struct GNUNET_TIME_Absolute stop)
199 {
200         struct Experiment *e;
201         e = GNUNET_malloc (sizeof (struct Experiment));
202
203         e->name = GNUNET_strdup (name);
204         e->issuer = issuer_id;
205         e->version = version;
206         if (NULL != description)
207                 e->description = GNUNET_strdup (description);
208         e->required_capabilities = required_capabilities;
209         e->start = start;
210         e->frequency = frequency;
211         e->duration = duration;
212         e->stop = stop;
213
214         /* verify experiment */
215         if (GNUNET_SYSERR == experiment_verify (i, e))
216         {
217                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Experiment signature is invalid\n"), name);
218                         GNUNET_free (e);
219                         GNUNET_free_non_null (e->name);
220                         GNUNET_free_non_null (e->description);
221                         return GNUNET_SYSERR;
222         }
223
224         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Adding experiment `%s' running from `%s' to `%s' every %llu sec. for %llu sec. \n"),
225                         e->name,
226                         GNUNET_STRINGS_absolute_time_to_string (start),
227                         GNUNET_STRINGS_absolute_time_to_string (stop),
228                         (long long unsigned int) frequency.rel_value / 1000,
229                         (long long unsigned int) duration.rel_value / 1000);
230         GNUNET_CONTAINER_multihashmap_put (experiments, &e->issuer.hashPubKey, e, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
231   GNUNET_STATISTICS_set (GED_stats, "# experiments", GNUNET_CONTAINER_multihashmap_size (experiments), GNUNET_NO);
232
233         return GNUNET_OK;
234 }
235
236
237 /**
238  * Parse a configuration section containing experiments
239  *
240  * @param cls configuration handle
241  * @param name section name
242  */
243 void exp_file_iterator (void *cls,
244                                                                                                 const char *name)
245 {
246         struct GNUNET_CONFIGURATION_Handle *exp = cls;
247         struct Issuer *i;
248
249         char *val;
250         unsigned long long number;
251
252         /* Experiment values */
253         struct GNUNET_PeerIdentity issuer;
254         struct GNUNET_TIME_Absolute version;
255         char *description;
256         uint32_t required_capabilities;
257         struct GNUNET_TIME_Absolute start ;
258         struct GNUNET_TIME_Absolute stop;
259         struct GNUNET_TIME_Relative frequency;
260         struct GNUNET_TIME_Relative duration;
261
262         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing section `%s'\n", name);
263
264         /* Mandatory fields */
265
266         /* Issuer */
267         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "ISSUER", &val))
268         {
269                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer missing\n"), name);
270                         return;
271         }
272         if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &issuer.hashPubKey))
273         {
274                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer invalid\n"), name);
275                         GNUNET_free (val);
276                         return;
277         }
278         if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &issuer.hashPubKey)))
279         {
280                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer not accepted!\n"), name);
281                 GNUNET_free (val);
282                 return;
283         }
284         GNUNET_free (val);
285
286         /* Version */
287         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "VERSION", &number))
288         {
289                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Version missing or invalid \n"), name);
290                         return;
291         }
292         version.abs_value = number;
293
294         /* Required capabilities */
295         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "CAPABILITIES", &number))
296         {
297                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities missing \n"), name);
298                         return;
299         }
300         if (number > UINT32_MAX)
301         {
302                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities invalid \n"), name);
303                 return;
304         }
305         required_capabilities = number;
306
307         /* Optional fields */
308
309         /* Description */
310         GNUNET_CONFIGURATION_get_value_string (exp, name, "DESCRIPTION", &description);
311
312
313
314         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "START", (long long unsigned int *) &start.abs_value))
315                         start = GNUNET_TIME_UNIT_ZERO_ABS;
316
317         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "FREQUENCY", &frequency))
318                         frequency = EXP_DEFAULT_EXP_FREQ;
319         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "DURATION", &duration))
320                         duration = EXP_DEFAULT_EXP_DUR;
321         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "STOP", (long long unsigned int *)&stop.abs_value))
322                         stop = GNUNET_TIME_UNIT_FOREVER_ABS;
323
324         GNUNET_EXPERIMENTATION_experiments_add (i, name, issuer, version,
325                                                                                                                                                                         description, required_capabilities,
326                                                                                                                                                                         start, frequency, duration, stop);
327         GNUNET_free_non_null (description);
328 }
329
330
331 /**
332  * Load experiments from file
333  *
334  * @param file source file
335  */
336 static void
337 load_file (const char * file)
338 {
339         struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
340         if (NULL == exp)
341                 return;
342
343         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
344         {
345                 GNUNET_CONFIGURATION_destroy (exp);
346                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
347                 return;
348         }
349         GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
350         GNUNET_CONFIGURATION_destroy (exp);
351 }
352
353
354 /**
355  * Start experiments management
356  */
357 int
358 GED_experiments_start ()
359 {
360         struct Issuer *i;
361         char *issuers;
362         char *file;
363         char *pubkey;
364         char *pos;
365         struct GNUNET_PeerIdentity issuer_ID;
366         struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
367         struct GNUNET_HashCode hash;
368
369         /* Load valid issuer */
370         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
371         {
372                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
373                         return GNUNET_SYSERR;
374         }
375
376         valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
377   for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
378   {
379
380                 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
381                 {
382                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
383                 }
384                 else
385                 {
386                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
387                                 i = GNUNET_malloc (sizeof (struct Issuer));
388                                 GNUNET_CONTAINER_multihashmap_put (valid_issuers, &issuer_ID.hashPubKey,
389                                                 i, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
390                                 i = NULL;
391                 }
392   }
393   GNUNET_free (issuers);
394
395   if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
396   {
397                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
398                 GED_experiments_stop ();
399                 return GNUNET_SYSERR;
400   }
401   GNUNET_STATISTICS_set (GED_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);
402
403         if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
404         {
405                         if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_key_from_string(pubkey, strlen (pubkey), &pub))
406                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid public key `%s'\n"), pubkey);
407                         else
408                         {
409                                 GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
410                                 if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
411                                 {
412                                                 i->pubkey = pub;
413                                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
414                                 }
415                                 else
416                                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
417                         }
418                         GNUNET_free (pubkey);
419         }
420
421         GSE_my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
422         GSE_my_issuer = GNUNET_malloc (GSE_my_issuer_count * sizeof (struct Experimentation_Issuer));
423         GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &create_issuer, GSE_my_issuer);
424         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Daemon has %u issuers\n"), GSE_my_issuer_count);
425
426   experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
427   /* Load experiments from file */
428         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "EXPERIMENTS", &file))
429                 return GNUNET_OK;
430
431         if (GNUNET_YES != GNUNET_DISK_file_test (file))
432         {
433                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
434                 GNUNET_free (file);
435                         return GNUNET_OK;
436         }
437         load_file (file);
438         GNUNET_free (file);
439         return GNUNET_OK;
440 }
441
442
443 /**
444  * Stop experiments management
445  */
446 void
447 GED_experiments_stop ()
448 {
449         if (NULL != GSE_my_issuer)
450         {
451                 GNUNET_free (GSE_my_issuer);
452                 GSE_my_issuer = NULL;
453                 GSE_my_issuer_count = 0;
454         }
455         if (NULL != valid_issuers)
456         {
457                 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
458                 GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
459         }
460         valid_issuers = NULL;
461         if (NULL != experiments)
462         {
463                 GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
464                 GNUNET_CONTAINER_multihashmap_destroy (experiments);
465         }
466         experiments = NULL;
467 }
468
469 /* end of gnunet-daemon-experimentation_experiments.c */