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"
39 /* Experiment issuer */
40 struct GNUNET_PeerIdentity issuer;
42 /* Experiment version as timestamp of creation */
43 struct GNUNET_TIME_Absolute version;
48 /* Required capabilities */
49 uint32_t required_capabilities;
51 /* Experiment itself */
58 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey;
62 * Hashmap containing valid experiment issuer
64 static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
67 * Hashmap containing valid experiments
69 static struct GNUNET_CONTAINER_MultiHashMap *experiments;
72 * Verify experiment signature
76 * @return GNUNET_OK or GNUNET_SYSERR
80 experiment_verify (struct Issuer *i, struct Experiment *e)
82 GNUNET_assert (NULL != i);
83 GNUNET_assert (NULL != e);
85 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Verification: to be implemented\n");
89 int free_experiment (void *cls,
90 const struct GNUNET_HashCode * key,
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);
101 int free_issuer (void *cls,
102 const struct GNUNET_HashCode * key,
105 struct Issuer *i = value;
106 GNUNET_CONTAINER_multihashmap_remove (valid_issuers, key, value);
112 * Is peer a valid issuer
114 * @return GNUNET_YES or GNUNET_NO
117 GNUNET_EXPERIMENTATION_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
119 if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &issuer_ID->hashPubKey))
126 void exp_file_iterator (void *cls,
129 struct GNUNET_CONFIGURATION_Handle *exp = cls;
130 struct Experiment *e;
134 unsigned long long number;
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing section `%s'\n", section);
138 e = GNUNET_malloc (sizeof (struct Experiment));
139 /* Mandatory fields */
142 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, section, "ISSUER", &val))
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer missing\n"), section);
148 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &e->issuer.hashPubKey))
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer invalid\n"), section);
155 if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &e->issuer.hashPubKey)))
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer not accepted!\n"), section);
165 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, section, "VERSION", &number))
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Version missing or invalid \n"), section);
171 e->version.abs_value = number;
173 /* Required capabilities */
174 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, section, "CAPABILITIES", &number))
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities missing \n"), section);
180 if (number > UINT32_MAX)
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities invalid \n"), section);
186 e->required_capabilities = number;
187 e->name = GNUNET_strdup (section);
189 /* Optional fields */
191 GNUNET_CONFIGURATION_get_value_string (exp, section, "DESCRIPTION", &e->description);
193 /* verify experiment */
194 if (GNUNET_SYSERR == experiment_verify (i, e))
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Experiment signature is invalid\n"), section);
198 GNUNET_free_non_null (e->name);
199 GNUNET_free_non_null (e->description);
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);
208 * Load experiments from file
210 * @param file source file
214 load_file (const char * file)
216 struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
220 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
222 GNUNET_CONFIGURATION_destroy (exp);
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
227 GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
229 GNUNET_CONFIGURATION_destroy (exp);
233 * Start experiments management
236 GNUNET_EXPERIMENTATION_experiments_start ()
243 struct GNUNET_PeerIdentity issuer_ID;
244 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
245 struct GNUNET_HashCode hash;
247 /* Load valid issuer */
248 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
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;
254 valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
255 for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
258 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
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);
271 GNUNET_free (issuers);
273 if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
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;
279 GNUNET_STATISTICS_set (GSE_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);
281 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
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);
287 GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
288 if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
296 GNUNET_free (pubkey);
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))
304 if (GNUNET_YES != GNUNET_DISK_file_test (file))
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
319 * Stop experiments management
322 GNUNET_EXPERIMENTATION_experiments_stop ()
324 if (NULL != valid_issuers)
326 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
327 GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
329 valid_issuers = NULL;
330 if (NULL != experiments)
332 GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
333 GNUNET_CONTAINER_multihashmap_destroy (experiments);
338 /* end of gnunet-daemon-experimentation_experiments.c */