aboutsummaryrefslogtreecommitdiff
path: root/src/experimentation/gnunet-daemon-experimentation_experiments.c
blob: 01d8fa8169af425eb06416b4250a2c3d37a6ee75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file experimentation/gnunet-daemon-experimentation_experiments.c
 * @brief experimentation daemon: experiment management
 * @author Christian Grothoff
 * @author Matthias Wachs
 */
#include "platform.h"
#include "gnunet_getopt_lib.h"
#include "gnunet_util_lib.h"
#include "gnunet_core_service.h"
#include "gnunet_statistics_service.h"
#include "gnunet-daemon-experimentation.h"



/**
 * Struct to store information about an experiment issuer
 */
struct Issuer
{
	struct GNUNET_CRYPTO_EccPublicKey pubkey;
};


/**
 * Hashmap containing valid experiment issuer
 */
static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;


/**
 * Hashmap containing valid experiments
 */
static struct GNUNET_CONTAINER_MultiHashMap *experiments;


uint32_t GSE_my_issuer_count;

/**
 * Valid experiment issuer for this daemon
 *
 * Array Experimentation_Issuer with GSE_my_issuer_count elements
 */
struct Experimentation_Issuer *GSE_my_issuer;


/**
 * Verify experiment signature
 *
 * @param i issuer
 * @param e experiment
 * @return GNUNET_OK or GNUNET_SYSERR
 */
int
experiment_verify (struct Issuer *i, struct Experiment *e)
{
	GNUNET_assert (NULL != i);
	GNUNET_assert (NULL != e);

	GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Verification: to be implemented\n");
	return GNUNET_OK;
}

int free_experiment (void *cls,
										 const struct GNUNET_HashCode * key,
										 void *value)
{
	struct Experiment *e = value;
	GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (experiments, key, value));
	GNUNET_free_non_null (e->description);
	GNUNET_free_non_null (e->name);
	GNUNET_free (e);
	return GNUNET_OK;
}


/**
 * Free issuer element
 *
 * @param cls unused
 * @param key the key
 * @param value the issuer element to free
 * @return GNUNET_OK to continue
 */
int free_issuer (void *cls,
								 const struct GNUNET_HashCode * key,
								 void *value)
{
	struct Issuer *i = value;
	GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (valid_issuers, key, value));
	GNUNET_free (i);
	return GNUNET_OK;
}

int create_issuer (void *cls,
								 const struct GNUNET_HashCode * key,
								 void *value)
{
	static int i = 0;
	GNUNET_assert (i < GSE_my_issuer_count);
	GSE_my_issuer[i].issuer_id.hashPubKey = *key;

	i++;
	return GNUNET_OK;

}



/**
 * Is peer a valid issuer
 *
 * @return GNUNET_YES or GNUNET_NO
 */
int
GED_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
{
	if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &issuer_ID->hashPubKey))
		return GNUNET_YES;
	else
		return GNUNET_NO;
}

struct FindCtx
{
	const char *name;
	struct GNUNET_TIME_Absolute version;
	struct Experiment *res;
};

static int
find_it (void *cls,
				const struct GNUNET_HashCode * key,
				void *value)
{
	struct FindCtx *find_ctx = cls;
	struct Experiment *e = (struct Experiment *) value;

	if (0 != strcmp(e->name, find_ctx->name))
		return GNUNET_OK;
	if (e->version.abs_value_us != find_ctx->version.abs_value_us)
		return GNUNET_OK;

	find_ctx->res = e;
	return GNUNET_NO;
}


/*
 * Find an experiment based on issuer name and version
 *
 * @param issuer the issuer
 * @param name experiment name
 * @param version experiment version
 * @return the experiment or NULL if not found
 */
struct Experiment *
GED_experiments_find (const struct GNUNET_PeerIdentity *issuer,
											const char *name,
											const struct GNUNET_TIME_Absolute version)
{
	struct FindCtx find_ctx;

	find_ctx.name = name;
	find_ctx.version = version;
	find_ctx.res = NULL;

	GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
			&issuer->hashPubKey, &find_it, &find_ctx);
	return find_ctx.res;
}

struct GetCtx
{
	struct Node *n;
	GNUNET_EXPERIMENTATION_experiments_get_cb get_cb;
};


static int
get_it (void *cls,
				const struct GNUNET_HashCode * key,
				void *value)
{
	struct GetCtx *get_ctx = cls;
	struct Experiment *e = value;

	get_ctx->get_cb (get_ctx->n, e);

	return GNUNET_OK;
}

void
GED_experiments_get (struct Node *n,
										 struct GNUNET_PeerIdentity *issuer,
										 GNUNET_EXPERIMENTATION_experiments_get_cb get_cb)
{
	struct GetCtx get_ctx;

	GNUNET_assert (NULL != n);
	GNUNET_assert (NULL != experiments);
	GNUNET_assert (NULL != get_cb);

	get_ctx.n = n;
	get_ctx.get_cb = get_cb;

	GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
			&issuer->hashPubKey, &get_it, &get_ctx);

	get_cb (n, NULL);
}

/**
 * Add a new experiment
 */
int GNUNET_EXPERIMENTATION_experiments_add (struct Issuer *i,
																						const char *name,
																						struct GNUNET_PeerIdentity issuer_id,
																						struct GNUNET_TIME_Absolute version,
																						char *description,
																						uint32_t required_capabilities,
																						struct GNUNET_TIME_Absolute start,
																						struct GNUNET_TIME_Relative frequency,
																						struct GNUNET_TIME_Relative duration,
																						struct GNUNET_TIME_Absolute stop)
{
	struct Experiment *e;
	e = GNUNET_malloc (sizeof (struct Experiment));

	e->name = GNUNET_strdup (name);
	e->issuer = issuer_id;
	e->version = version;
	if (NULL != description)
		e->description = GNUNET_strdup (description);
	e->required_capabilities = required_capabilities;
	e->start = start;
	e->frequency = frequency;
	e->duration = duration;
	e->stop = stop;

	/* verify experiment */
	if (GNUNET_SYSERR == experiment_verify (i, e))
	{
			GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Experiment signature is invalid\n"), name);
			GNUNET_free (e);
			GNUNET_free_non_null (e->name);
			GNUNET_free_non_null (e->description);
			return GNUNET_SYSERR;
	}

	GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Adding experiment `%s' running from `%s' to `%s' every %llu sec. for %llu sec. \n"),
			e->name,
			GNUNET_STRINGS_absolute_time_to_string (start),
			GNUNET_STRINGS_absolute_time_to_string (stop),
			(long long unsigned int) frequency.rel_value_us / 1000000LL,
			(long long unsigned int) duration.rel_value_us / 1000000LL);
	GNUNET_CONTAINER_multihashmap_put (experiments, &e->issuer.hashPubKey, e, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  GNUNET_STATISTICS_set (GED_stats, "# experiments", GNUNET_CONTAINER_multihashmap_size (experiments), GNUNET_NO);

	return GNUNET_OK;
}


/**
 * Parse a configuration section containing experiments
 *
 * @param cls configuration handle
 * @param name section name
 */
void exp_file_iterator (void *cls,
												const char *name)
{
	struct GNUNET_CONFIGURATION_Handle *exp = cls;
	struct Issuer *i;

	char *val;
	unsigned long long number;

	/* Experiment values */
	struct GNUNET_PeerIdentity issuer;
	struct GNUNET_TIME_Absolute version;
	char *description;
	uint32_t required_capabilities;
	struct GNUNET_TIME_Absolute start ;
	struct GNUNET_TIME_Absolute stop;
	struct GNUNET_TIME_Relative frequency;
	struct GNUNET_TIME_Relative duration;

	//GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing section `%s'\n", name);

	/* Mandatory fields */

	/* Issuer */
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "ISSUER", &val))
	{
			GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer missing\n"), name);
			return;
	}
	if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &issuer.hashPubKey))
	{
			GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer invalid\n"), name);
			GNUNET_free (val);
			return;
	}
	if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &issuer.hashPubKey)))
	{
		GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Issuer not accepted!\n"), name);
		GNUNET_free (val);
		return;
	}
	GNUNET_free (val);

	/* Version */
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "VERSION", &number))
	{
			GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Version missing or invalid \n"), name);
			return;
	}
	version.abs_value_us = number; // FIXME: what is this supposed to be? Version != TIME!???

	/* Required capabilities */
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "CAPABILITIES", &number))
	{
			GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities missing \n"), name);
			return;
	}
	if (number > UINT32_MAX)
	{
		GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Experiment `%s': Required capabilities invalid \n"), name);
		return;
	}
	required_capabilities = number;

	/* Optional fields */

	/* Description */
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "DESCRIPTION", &description))
		description = NULL;

	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "START", (long long unsigned int *) &start.abs_value_us))
			start = GNUNET_TIME_UNIT_ZERO_ABS;

	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "FREQUENCY", &frequency))
			frequency = EXP_DEFAULT_EXP_FREQ;
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "DURATION", &duration))
			duration = EXP_DEFAULT_EXP_DUR;
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "STOP", (long long unsigned int *)&stop.abs_value_us))
			stop = GNUNET_TIME_UNIT_FOREVER_ABS;

	GNUNET_EXPERIMENTATION_experiments_add (i, name, issuer, version,
																					description, required_capabilities,
																					start, frequency, duration, stop);
	GNUNET_free_non_null (description);
}


/**
 * Load experiments from file
 *
 * @param file source file
 */
static void
load_file (const char * file)
{
	struct GNUNET_CONFIGURATION_Handle *exp = GNUNET_CONFIGURATION_create();
	if (NULL == exp)
		return;

	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
	{
		GNUNET_CONFIGURATION_destroy (exp);
		GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse file `%s'\n"), file);
		return;
	}
	GNUNET_CONFIGURATION_iterate_sections (exp, &exp_file_iterator, exp);
	GNUNET_CONFIGURATION_destroy (exp);
}


/**
 * Start experiments management
 */
int
GED_experiments_start ()
{
	struct Issuer *i;
	char *issuers;
	char *file;
	char *pubkey;
	char *pos;
	struct GNUNET_PeerIdentity issuer_ID;
	struct GNUNET_CRYPTO_EccPublicKey pub;
	struct GNUNET_HashCode hash;

	/* Load valid issuer */
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
	{
			GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
			return GNUNET_SYSERR;
	}

	valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
  for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
  {

  		if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (pos, &issuer_ID.hashPubKey))
  		{
  	  		GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid value `%s'\n"), pos);
  		}
  		else
  		{
  				GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is a valid issuer \n", GNUNET_i2s (&issuer_ID));
  				i = GNUNET_malloc (sizeof (struct Issuer));
  				GNUNET_CONTAINER_multihashmap_put (valid_issuers, &issuer_ID.hashPubKey,
  						i, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  				i = NULL;
  		}
  }
  GNUNET_free (issuers);

  if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
  {
		GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
		GED_experiments_stop ();
		return GNUNET_SYSERR;
  }
  GNUNET_STATISTICS_set (GED_stats, "# issuer", GNUNET_CONTAINER_multihashmap_size (valid_issuers), GNUNET_NO);

	if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "PUBKEY", &pubkey))
	{
			if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_key_from_string(pubkey, strlen (pubkey), &pub))
	  		GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid public key `%s'\n"), pubkey);
			else
			{
				GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
				if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
				{
						i->pubkey = pub;
						GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
				}
				else
					GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
			}
			GNUNET_free (pubkey);
	}

	GSE_my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
	GSE_my_issuer = GNUNET_malloc (GSE_my_issuer_count * sizeof (struct Experimentation_Issuer));
	GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &create_issuer, GSE_my_issuer);
	GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Daemon has %u issuers\n", GSE_my_issuer_count);

  experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
  /* Load experiments from file */
	if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "EXPERIMENTS", &file))
		return GNUNET_OK;

	if (GNUNET_YES != GNUNET_DISK_file_test (file))
	{
  		GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot read experiments file `%s'\n"), file);
  		GNUNET_free (file);
			return GNUNET_OK;
	}
	load_file (file);
	GNUNET_free (file);
	return GNUNET_OK;
}


/**
 * Stop experiments management
 */
void
GED_experiments_stop ()
{
	if (NULL != GSE_my_issuer)
	{
		GNUNET_free (GSE_my_issuer);
		GSE_my_issuer = NULL;
		GSE_my_issuer_count = 0;
	}
	if (NULL != valid_issuers)
	{
		GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);
		GNUNET_CONTAINER_multihashmap_destroy (valid_issuers);
	}
	valid_issuers = NULL;
	if (NULL != experiments)
	{
		GNUNET_CONTAINER_multihashmap_iterate (experiments, &free_experiment, NULL);
		GNUNET_CONTAINER_multihashmap_destroy (experiments);
	}
	experiments = NULL;
}

/* end of gnunet-daemon-experimentation_experiments.c */