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"
36 * Struct to store information about a specific experiment
43 /* Experiment issuer */
44 struct GNUNET_PeerIdentity issuer;
46 /* Experiment version as timestamp of creation */
47 struct GNUNET_TIME_Absolute version;
52 /* Required capabilities */
53 uint32_t required_capabilities;
55 /* Experiment itself */
62 * Struct to store information about an experiment issuer
66 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey;
71 * Hashmap containing valid experiment issuer
73 static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
77 * Hashmap containing valid experiments
79 static struct GNUNET_CONTAINER_MultiHashMap *experiments;
83 * Verify experiment signature
87 * @return GNUNET_OK or GNUNET_SYSERR
90 experiment_verify (struct Issuer *i, struct Experiment *e)
92 GNUNET_assert (NULL != i);
93 GNUNET_assert (NULL != e);
95 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Verification: to be implemented\n");
99 int free_experiment (void *cls,
100 const struct GNUNET_HashCode * key,
103 struct Experiment *e = value;
104 GNUNET_CONTAINER_multihashmap_remove (experiments, key, value);
105 GNUNET_free_non_null (e->description);
106 GNUNET_free_non_null (e->name);
113 * Free issuer element
117 * @param value the issuer element to free
118 * @return GNUNET_OK to continue
120 int free_issuer (void *cls,
121 const struct GNUNET_HashCode * key,
124 struct Issuer *i = value;
125 GNUNET_CONTAINER_multihashmap_remove (valid_issuers, key, value);
132 * Is peer a valid issuer
134 * @return GNUNET_YES or GNUNET_NO
137 GNUNET_EXPERIMENTATION_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
139 if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &issuer_ID->hashPubKey))
147 * Parse a configuration section containing experiments
149 * @param cls configuration handle
150 * @param section section name
152 void exp_file_iterator (void *cls,
155 struct GNUNET_CONFIGURATION_Handle *exp = cls;
156 struct Experiment *e;
160 unsigned long long number;
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing section `%s'\n", section);
164 e = GNUNET_malloc (sizeof (struct Experiment));
165 /* Mandatory fields */
168 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, section, "ISSUER", &val))
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer missing\n"), section);
174 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &e->issuer.hashPubKey))
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer invalid\n"), section);
181 if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &e->issuer.hashPubKey)))
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer not accepted!\n"), section);
191 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, section, "VERSION", &number))
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Version missing or invalid \n"), section);
197 e->version.abs_value = number;
199 /* Required capabilities */
200 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, section, "CAPABILITIES", &number))
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities missing \n"), section);
206 if (number > UINT32_MAX)
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities invalid \n"), section);
212 e->required_capabilities = number;
213 e->name = GNUNET_strdup (section);
215 /* Optional fields */
217 GNUNET_CONFIGURATION_get_value_string (exp, section, "DESCRIPTION", &e->description);
219 /* verify experiment */
220 if (GNUNET_SYSERR == experiment_verify (i, e))
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Experiment signature is invalid\n"), section);
224 GNUNET_free_non_null (e->name);
225 GNUNET_free_non_null (e->description);
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Adding experiment `%s'\n"), e->name);
229 GNUNET_CONTAINER_multihashmap_put (experiments, &e->issuer.hashPubKey, e, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
230 GNUNET_STATISTICS_set (GSE_stats, "# experiments", GNUNET_CONTAINER_multihashmap_size (experiments), GNUNET_NO);
235 * Load experiments from file
237 * @param file source file
240 load_file (const char * file)
242 struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
246 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
248 GNUNET_CONFIGURATION_destroy (exp);
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
252 GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
253 GNUNET_CONFIGURATION_destroy (exp);
258 * Start experiments management
261 GNUNET_EXPERIMENTATION_experiments_start ()
268 struct GNUNET_PeerIdentity issuer_ID;
269 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
270 struct GNUNET_HashCode hash;
272 /* Load valid issuer */
273 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
276 return GNUNET_SYSERR;
279 valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
280 for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
283 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
290 i = GNUNET_malloc (sizeof (struct Issuer));
291 GNUNET_CONTAINER_multihashmap_put (valid_issuers, &issuer_ID.hashPubKey,
292 i, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
296 GNUNET_free (issuers);
298 if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
301 GNUNET_EXPERIMENTATION_experiments_stop ();
302 return GNUNET_SYSERR;
304 GNUNET_STATISTICS_set (GSE_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);
306 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
308 if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_key_from_string(pubkey, strlen (pubkey), &pub))
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid public key `%s'\n"), pubkey);
312 GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
313 if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
321 GNUNET_free (pubkey);
324 experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
325 /* Load experiments from file */
326 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GSE_cfg, "EXPERIMENTATION", "EXPERIMENTS", &file))
329 if (GNUNET_YES != GNUNET_DISK_file_test (file))
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
342 * Stop experiments management
345 GNUNET_EXPERIMENTATION_experiments_stop ()
347 if (NULL != valid_issuers)
349 GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
350 GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
352 valid_issuers = NULL;
353 if (NULL != experiments)
355 GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
356 GNUNET_CONTAINER_multihashmap_destroy (experiments);
361 /* end of gnunet-daemon-experimentation_experiments.c */