01d8fa8169af425eb06416b4250a2c3d37a6ee75
[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_EccPublicKey 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_break (0 == 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_break (0 == 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 FindCtx
145 {
146         const char *name;
147         struct GNUNET_TIME_Absolute version;
148         struct Experiment *res;
149 };
150
151 static int
152 find_it (void *cls,
153                                 const struct GNUNET_HashCode * key,
154                                 void *value)
155 {
156         struct FindCtx *find_ctx = cls;
157         struct Experiment *e = (struct Experiment *) value;
158
159         if (0 != strcmp(e->name, find_ctx->name))
160                 return GNUNET_OK;
161         if (e->version.abs_value_us != find_ctx->version.abs_value_us)
162                 return GNUNET_OK;
163
164         find_ctx->res = e;
165         return GNUNET_NO;
166 }
167
168
169 /*
170  * Find an experiment based on issuer name and version
171  *
172  * @param issuer the issuer
173  * @param name experiment name
174  * @param version experiment version
175  * @return the experiment or NULL if not found
176  */
177 struct Experiment *
178 GED_experiments_find (const struct GNUNET_PeerIdentity *issuer,
179                                                                                         const char *name,
180                                                                                         const struct GNUNET_TIME_Absolute version)
181 {
182         struct FindCtx find_ctx;
183
184         find_ctx.name = name;
185         find_ctx.version = version;
186         find_ctx.res = NULL;
187
188         GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
189                         &issuer->hashPubKey, &find_it, &find_ctx);
190         return find_ctx.res;
191 }
192
193 struct GetCtx
194 {
195         struct Node *n;
196         GNUNET_EXPERIMENTATION_experiments_get_cb get_cb;
197 };
198
199
200 static int
201 get_it (void *cls,
202                                 const struct GNUNET_HashCode * key,
203                                 void *value)
204 {
205         struct GetCtx *get_ctx = cls;
206         struct Experiment *e = value;
207
208         get_ctx->get_cb (get_ctx->n, e);
209
210         return GNUNET_OK;
211 }
212
213 void
214 GED_experiments_get (struct Node *n,
215                                                                                  struct GNUNET_PeerIdentity *issuer,
216                                                                                  GNUNET_EXPERIMENTATION_experiments_get_cb get_cb)
217 {
218         struct GetCtx get_ctx;
219
220         GNUNET_assert (NULL != n);
221         GNUNET_assert (NULL != experiments);
222         GNUNET_assert (NULL != get_cb);
223
224         get_ctx.n = n;
225         get_ctx.get_cb = get_cb;
226
227         GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
228                         &issuer->hashPubKey, &get_it, &get_ctx);
229
230         get_cb (n, NULL);
231 }
232
233 /**
234  * Add a new experiment
235  */
236 int GNUNET_EXPERIMENTATION_experiments_add (struct Issuer *i,
237                                                                                                                                                                                 const char *name,
238                                                                                                                                                                                 struct GNUNET_PeerIdentity issuer_id,
239                                                                                                                                                                                 struct GNUNET_TIME_Absolute version,
240                                                                                                                                                                                 char *description,
241                                                                                                                                                                                 uint32_t required_capabilities,
242                                                                                                                                                                                 struct GNUNET_TIME_Absolute start,
243                                                                                                                                                                                 struct GNUNET_TIME_Relative frequency,
244                                                                                                                                                                                 struct GNUNET_TIME_Relative duration,
245                                                                                                                                                                                 struct GNUNET_TIME_Absolute stop)
246 {
247         struct Experiment *e;
248         e = GNUNET_malloc (sizeof (struct Experiment));
249
250         e->name = GNUNET_strdup (name);
251         e->issuer = issuer_id;
252         e->version = version;
253         if (NULL != description)
254                 e->description = GNUNET_strdup (description);
255         e->required_capabilities = required_capabilities;
256         e->start = start;
257         e->frequency = frequency;
258         e->duration = duration;
259         e->stop = stop;
260
261         /* verify experiment */
262         if (GNUNET_SYSERR == experiment_verify (i, e))
263         {
264                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Experiment signature is invalid\n"), name);
265                         GNUNET_free (e);
266                         GNUNET_free_non_null (e->name);
267                         GNUNET_free_non_null (e->description);
268                         return GNUNET_SYSERR;
269         }
270
271         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Adding experiment `%s' running from `%s' to `%s' every %llu sec. for %llu sec. \n"),
272                         e->name,
273                         GNUNET_STRINGS_absolute_time_to_string (start),
274                         GNUNET_STRINGS_absolute_time_to_string (stop),
275                         (long long unsigned int) frequency.rel_value_us / 1000000LL,
276                         (long long unsigned int) duration.rel_value_us / 1000000LL);
277         GNUNET_CONTAINER_multihashmap_put (experiments, &e->issuer.hashPubKey, e, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
278   GNUNET_STATISTICS_set (GED_stats, "# experiments", GNUNET_CONTAINER_multihashmap_size (experiments), GNUNET_NO);
279
280         return GNUNET_OK;
281 }
282
283
284 /**
285  * Parse a configuration section containing experiments
286  *
287  * @param cls configuration handle
288  * @param name section name
289  */
290 void exp_file_iterator (void *cls,
291                                                                                                 const char *name)
292 {
293         struct GNUNET_CONFIGURATION_Handle *exp = cls;
294         struct Issuer *i;
295
296         char *val;
297         unsigned long long number;
298
299         /* Experiment values */
300         struct GNUNET_PeerIdentity issuer;
301         struct GNUNET_TIME_Absolute version;
302         char *description;
303         uint32_t required_capabilities;
304         struct GNUNET_TIME_Absolute start ;
305         struct GNUNET_TIME_Absolute stop;
306         struct GNUNET_TIME_Relative frequency;
307         struct GNUNET_TIME_Relative duration;
308
309         //GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing section `%s'\n", name);
310
311         /* Mandatory fields */
312
313         /* Issuer */
314         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "ISSUER", &val))
315         {
316                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer missing\n"), name);
317                         return;
318         }
319         if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &issuer.hashPubKey))
320         {
321                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer invalid\n"), name);
322                         GNUNET_free (val);
323                         return;
324         }
325         if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &issuer.hashPubKey)))
326         {
327                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer not accepted!\n"), name);
328                 GNUNET_free (val);
329                 return;
330         }
331         GNUNET_free (val);
332
333         /* Version */
334         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "VERSION", &number))
335         {
336                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Version missing or invalid \n"), name);
337                         return;
338         }
339         version.abs_value_us = number; // FIXME: what is this supposed to be? Version != TIME!???
340
341         /* Required capabilities */
342         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "CAPABILITIES", &number))
343         {
344                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities missing \n"), name);
345                         return;
346         }
347         if (number > UINT32_MAX)
348         {
349                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities invalid \n"), name);
350                 return;
351         }
352         required_capabilities = number;
353
354         /* Optional fields */
355
356         /* Description */
357         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "DESCRIPTION", &description))
358                 description = NULL;
359
360         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "START", (long long unsigned int *) &start.abs_value_us))
361                         start = GNUNET_TIME_UNIT_ZERO_ABS;
362
363         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "FREQUENCY", &frequency))
364                         frequency = EXP_DEFAULT_EXP_FREQ;
365         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "DURATION", &duration))
366                         duration = EXP_DEFAULT_EXP_DUR;
367         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "STOP", (long long unsigned int *)&stop.abs_value_us))
368                         stop = GNUNET_TIME_UNIT_FOREVER_ABS;
369
370         GNUNET_EXPERIMENTATION_experiments_add (i, name, issuer, version,
371                                                                                                                                                                         description, required_capabilities,
372                                                                                                                                                                         start, frequency, duration, stop);
373         GNUNET_free_non_null (description);
374 }
375
376
377 /**
378  * Load experiments from file
379  *
380  * @param file source file
381  */
382 static void
383 load_file (const char * file)
384 {
385         struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
386         if (NULL == exp)
387                 return;
388
389         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
390         {
391                 GNUNET_CONFIGURATION_destroy (exp);
392                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
393                 return;
394         }
395         GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
396         GNUNET_CONFIGURATION_destroy (exp);
397 }
398
399
400 /**
401  * Start experiments management
402  */
403 int
404 GED_experiments_start ()
405 {
406         struct Issuer *i;
407         char *issuers;
408         char *file;
409         char *pubkey;
410         char *pos;
411         struct GNUNET_PeerIdentity issuer_ID;
412         struct GNUNET_CRYPTO_EccPublicKey pub;
413         struct GNUNET_HashCode hash;
414
415         /* Load valid issuer */
416         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
417         {
418                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
419                         return GNUNET_SYSERR;
420         }
421
422         valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
423   for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
424   {
425
426                 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
427                 {
428                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
429                 }
430                 else
431                 {
432                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
433                                 i = GNUNET_malloc (sizeof (struct Issuer));
434                                 GNUNET_CONTAINER_multihashmap_put (valid_issuers, &issuer_ID.hashPubKey,
435                                                 i, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
436                                 i = NULL;
437                 }
438   }
439   GNUNET_free (issuers);
440
441   if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
442   {
443                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
444                 GED_experiments_stop ();
445                 return GNUNET_SYSERR;
446   }
447   GNUNET_STATISTICS_set (GED_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);
448
449         if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
450         {
451                         if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_key_from_string(pubkey, strlen (pubkey), &pub))
452                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid public key `%s'\n"), pubkey);
453                         else
454                         {
455                                 GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
456                                 if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
457                                 {
458                                                 i->pubkey = pub;
459                                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
460                                 }
461                                 else
462                                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
463                         }
464                         GNUNET_free (pubkey);
465         }
466
467         GSE_my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
468         GSE_my_issuer = GNUNET_malloc (GSE_my_issuer_count * sizeof (struct Experimentation_Issuer));
469         GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &create_issuer, GSE_my_issuer);
470         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Daemon has %u issuers\n", GSE_my_issuer_count);
471
472   experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
473   /* Load experiments from file */
474         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "EXPERIMENTS", &file))
475                 return GNUNET_OK;
476
477         if (GNUNET_YES != GNUNET_DISK_file_test (file))
478         {
479                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
480                 GNUNET_free (file);
481                         return GNUNET_OK;
482         }
483         load_file (file);
484         GNUNET_free (file);
485         return GNUNET_OK;
486 }
487
488
489 /**
490  * Stop experiments management
491  */
492 void
493 GED_experiments_stop ()
494 {
495         if (NULL != GSE_my_issuer)
496         {
497                 GNUNET_free (GSE_my_issuer);
498                 GSE_my_issuer = NULL;
499                 GSE_my_issuer_count = 0;
500         }
501         if (NULL != valid_issuers)
502         {
503                 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
504                 GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
505         }
506         valid_issuers = NULL;
507         if (NULL != experiments)
508         {
509                 GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
510                 GNUNET_CONTAINER_multihashmap_destroy (experiments);
511         }
512         experiments = NULL;
513 }
514
515 /* end of gnunet-daemon-experimentation_experiments.c */