message parsing on receive
[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 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 != find_ctx->version.abs_value)
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 / 1000,
276                         (long long unsigned int) duration.rel_value / 1000);
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 = number;
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         GNUNET_CONFIGURATION_get_value_string (exp, name, "DESCRIPTION", &description);
358
359
360
361         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "START", (long long unsigned int *) &start.abs_value))
362                         start = GNUNET_TIME_UNIT_ZERO_ABS;
363
364         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "FREQUENCY", &frequency))
365                         frequency = EXP_DEFAULT_EXP_FREQ;
366         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "DURATION", &duration))
367                         duration = EXP_DEFAULT_EXP_DUR;
368         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "STOP", (long long unsigned int *)&stop.abs_value))
369                         stop = GNUNET_TIME_UNIT_FOREVER_ABS;
370
371         GNUNET_EXPERIMENTATION_experiments_add (i, name, issuer, version,
372                                                                                                                                                                         description, required_capabilities,
373                                                                                                                                                                         start, frequency, duration, stop);
374         GNUNET_free_non_null (description);
375 }
376
377
378 /**
379  * Load experiments from file
380  *
381  * @param file source file
382  */
383 static void
384 load_file (const char * file)
385 {
386         struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
387         if (NULL == exp)
388                 return;
389
390         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
391         {
392                 GNUNET_CONFIGURATION_destroy (exp);
393                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
394                 return;
395         }
396         GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
397         GNUNET_CONFIGURATION_destroy (exp);
398 }
399
400
401 /**
402  * Start experiments management
403  */
404 int
405 GED_experiments_start ()
406 {
407         struct Issuer *i;
408         char *issuers;
409         char *file;
410         char *pubkey;
411         char *pos;
412         struct GNUNET_PeerIdentity issuer_ID;
413         struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
414         struct GNUNET_HashCode hash;
415
416         /* Load valid issuer */
417         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
418         {
419                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
420                         return GNUNET_SYSERR;
421         }
422
423         valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
424   for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
425   {
426
427                 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
428                 {
429                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
430                 }
431                 else
432                 {
433                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
434                                 i = GNUNET_malloc (sizeof (struct Issuer));
435                                 GNUNET_CONTAINER_multihashmap_put (valid_issuers, &issuer_ID.hashPubKey,
436                                                 i, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
437                                 i = NULL;
438                 }
439   }
440   GNUNET_free (issuers);
441
442   if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
443   {
444                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
445                 GED_experiments_stop ();
446                 return GNUNET_SYSERR;
447   }
448   GNUNET_STATISTICS_set (GED_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);
449
450         if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
451         {
452                         if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_key_from_string(pubkey, strlen (pubkey), &pub))
453                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid public key `%s'\n"), pubkey);
454                         else
455                         {
456                                 GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
457                                 if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
458                                 {
459                                                 i->pubkey = pub;
460                                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
461                                 }
462                                 else
463                                         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
464                         }
465                         GNUNET_free (pubkey);
466         }
467
468         GSE_my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
469         GSE_my_issuer = GNUNET_malloc (GSE_my_issuer_count * sizeof (struct Experimentation_Issuer));
470         GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &create_issuer, GSE_my_issuer);
471         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Daemon has %u issuers\n"), GSE_my_issuer_count);
472
473   experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
474   /* Load experiments from file */
475         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "EXPERIMENTS", &file))
476                 return GNUNET_OK;
477
478         if (GNUNET_YES != GNUNET_DISK_file_test (file))
479         {
480                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
481                 GNUNET_free (file);
482                         return GNUNET_OK;
483         }
484         load_file (file);
485         GNUNET_free (file);
486         return GNUNET_OK;
487 }
488
489
490 /**
491  * Stop experiments management
492  */
493 void
494 GED_experiments_stop ()
495 {
496         if (NULL != GSE_my_issuer)
497         {
498                 GNUNET_free (GSE_my_issuer);
499                 GSE_my_issuer = NULL;
500                 GSE_my_issuer_count = 0;
501         }
502         if (NULL != valid_issuers)
503         {
504                 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
505                 GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
506         }
507         valid_issuers = NULL;
508         if (NULL != experiments)
509         {
510                 GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
511                 GNUNET_CONTAINER_multihashmap_destroy (experiments);
512         }
513         experiments = NULL;
514 }
515
516 /* end of gnunet-daemon-experimentation_experiments.c */