2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file experimentation/gnunet-daemon-experimentation_experiments.c
23 * @brief experimentation daemon: experiment management
24 * @author Christian Grothoff
25 * @author Matthias Wachs
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"
37 * Struct to store information about an experiment issuer
41 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey;
46 * Hashmap containing valid experiment issuer
48 static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
52 * Hashmap containing valid experiments
54 static struct GNUNET_CONTAINER_MultiHashMap *experiments;
57 uint32_t GSE_my_issuer_count;
59 struct Experimentation_Issuer *GSE_my_issuer;
63 * Verify experiment signature
67 * @return GNUNET_OK or GNUNET_SYSERR
70 experiment_verify (struct Issuer *i, struct Experiment *e)
72 GNUNET_assert (NULL != i);
73 GNUNET_assert (NULL != e);
75 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Verification: to be implemented\n");
79 int free_experiment (void *cls,
80 const struct GNUNET_HashCode * key,
83 struct Experiment *e = value;
84 GNUNET_CONTAINER_multihashmap_remove (experiments, key, value);
85 GNUNET_free_non_null (e->description);
86 GNUNET_free_non_null (e->name);
97 * @param value the issuer element to free
98 * @return GNUNET_OK to continue
100 int free_issuer (void *cls,
101 const struct GNUNET_HashCode * key,
104 struct Issuer *i = value;
105 GNUNET_CONTAINER_multihashmap_remove (valid_issuers, key, value);
110 int create_issuer (void *cls,
111 const struct GNUNET_HashCode * key,
115 GNUNET_assert (i < GSE_my_issuer_count);
116 GSE_my_issuer[i].issuer_id.hashPubKey = *key;
126 * Is peer a valid issuer
128 * @return GNUNET_YES or GNUNET_NO
131 GNUNET_EXPERIMENTATION_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
133 if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &issuer_ID->hashPubKey))
142 GNUNET_EXPERIMENTATION_experiments_get_cb get_cb;
147 const struct GNUNET_HashCode * key,
150 struct GetCtx *get_ctx = cls;
151 struct Experiment *e = value;
153 get_ctx->get_cb (get_ctx->n, e);
162 GNUNET_EXPERIMENTATION_experiments_get (struct Node *n,
163 struct GNUNET_PeerIdentity *issuer,
164 GNUNET_EXPERIMENTATION_experiments_get_cb get_cb)
166 struct GetCtx get_ctx;
168 GNUNET_assert (NULL != n);
169 GNUNET_assert (NULL != experiments);
170 GNUNET_assert (NULL != get_cb);
173 get_ctx.get_cb = get_cb;
175 GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
176 &issuer->hashPubKey, &get_it, &get_ctx);
182 * Add a new experiment
184 int GNUNET_EXPERIMENTATION_experiments_add (struct Issuer *i,
186 struct GNUNET_PeerIdentity issuer_id,
187 struct GNUNET_TIME_Absolute version,
189 uint32_t required_capabilities,
190 struct GNUNET_TIME_Absolute start,
191 struct GNUNET_TIME_Relative frequency,
192 struct GNUNET_TIME_Relative duration,
193 struct GNUNET_TIME_Absolute stop)
195 struct Experiment *e;
196 e = GNUNET_malloc (sizeof (struct Experiment));
198 e->name = GNUNET_strdup (name);
199 e->issuer = issuer_id;
200 e->version = version;
201 if (NULL != description)
202 e->description = GNUNET_strdup (description);
203 e->required_capabilities = required_capabilities;
205 e->frequency = frequency;
206 e->duration = duration;
209 /* verify experiment */
210 if (GNUNET_SYSERR == experiment_verify (i, e))
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Experiment signature is invalid\n"), name);
214 GNUNET_free_non_null (e->name);
215 GNUNET_free_non_null (e->description);
216 return GNUNET_SYSERR;
219 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Adding experiment `%s' running from `%s' to `%s' every %llu sec. for %llu sec. \n"),
221 GNUNET_STRINGS_absolute_time_to_string (start),
222 GNUNET_STRINGS_absolute_time_to_string (stop),
223 (long long unsigned int) frequency.rel_value / 1000,
224 (long long unsigned int) duration.rel_value / 1000);
225 GNUNET_CONTAINER_multihashmap_put (experiments, &e->issuer.hashPubKey, e, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
226 GNUNET_STATISTICS_set (GSE_stats, "# experiments", GNUNET_CONTAINER_multihashmap_size (experiments), GNUNET_NO);
233 * Parse a configuration section containing experiments
235 * @param cls configuration handle
236 * @param name section name
238 void exp_file_iterator (void *cls,
241 struct GNUNET_CONFIGURATION_Handle *exp = cls;
245 unsigned long long number;
247 /* Experiment values */
248 struct GNUNET_PeerIdentity issuer;
249 struct GNUNET_TIME_Absolute version;
251 uint32_t required_capabilities;
252 struct GNUNET_TIME_Absolute start ;
253 struct GNUNET_TIME_Absolute stop;
254 struct GNUNET_TIME_Relative frequency;
255 struct GNUNET_TIME_Relative duration;
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing section `%s'\n", name);
259 /* Mandatory fields */
262 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "ISSUER", &val))
264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer missing\n"), name);
267 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &issuer.hashPubKey))
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer invalid\n"), name);
273 if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &issuer.hashPubKey)))
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer not accepted!\n"), name);
282 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "VERSION", &number))
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Version missing or invalid \n"), name);
287 version.abs_value = number;
289 /* Required capabilities */
290 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "CAPABILITIES", &number))
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities missing \n"), name);
295 if (number > UINT32_MAX)
297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities invalid \n"), name);
300 required_capabilities = number;
302 /* Optional fields */
305 GNUNET_CONFIGURATION_get_value_string (exp, name, "DESCRIPTION", &description);
309 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "START", (long long unsigned int *) &start.abs_value))
310 start = GNUNET_TIME_UNIT_ZERO_ABS;
312 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "FREQUENCY", &frequency))
313 frequency = EXP_DEFAULT_EXP_FREQ;
314 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "DURATION", &duration))
315 duration = EXP_DEFAULT_EXP_DUR;
316 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "STOP", (long long unsigned int *)&stop.abs_value))
317 stop = GNUNET_TIME_UNIT_FOREVER_ABS;
319 GNUNET_EXPERIMENTATION_experiments_add (i, name, issuer, version,
320 description, required_capabilities,
321 start, frequency, duration, stop);
322 GNUNET_free_non_null (description);
327 * Load experiments from file
329 * @param file source file
332 load_file (const char * file)
334 struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
338 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
340 GNUNET_CONFIGURATION_destroy (exp);
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
344 GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
345 GNUNET_CONFIGURATION_destroy (exp);
350 * Start experiments management
353 GNUNET_EXPERIMENTATION_experiments_start ()
360 struct GNUNET_PeerIdentity issuer_ID;
361 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
362 struct GNUNET_HashCode hash;
364 /* Load valid issuer */
365 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
368 return GNUNET_SYSERR;
371 valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
372 for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
375 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
382 i = GNUNET_malloc (sizeof (struct Issuer));
383 GNUNET_CONTAINER_multihashmap_put (valid_issuers, &issuer_ID.hashPubKey,
384 i, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
388 GNUNET_free (issuers);
390 if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
393 GNUNET_EXPERIMENTATION_experiments_stop ();
394 return GNUNET_SYSERR;
396 GNUNET_STATISTICS_set (GSE_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);
398 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
400 if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_key_from_string(pubkey, strlen (pubkey), &pub))
401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid public key `%s'\n"), pubkey);
404 GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
405 if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
413 GNUNET_free (pubkey);
416 GSE_my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
417 GSE_my_issuer = GNUNET_malloc (GSE_my_issuer_count * sizeof (struct Experimentation_Issuer));
418 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &create_issuer, GSE_my_issuer);
419 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Daemon has %u issuers\n"), GSE_my_issuer_count);
421 experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
422 /* Load experiments from file */
423 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "EXPERIMENTS", &file))
426 if (GNUNET_YES != GNUNET_DISK_file_test (file))
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
439 * Stop experiments management
442 GNUNET_EXPERIMENTATION_experiments_stop ()
444 if (NULL != GSE_my_issuer)
446 GNUNET_free (GSE_my_issuer);
447 GSE_my_issuer = NULL;
448 GSE_my_issuer_count = 0;
450 if (NULL != valid_issuers)
452 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
453 GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
455 valid_issuers = NULL;
456 if (NULL != experiments)
458 GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
459 GNUNET_CONTAINER_multihashmap_destroy (experiments);
464 /* end of gnunet-daemon-experimentation_experiments.c */