assorted dht changes, fixes, etc.
[oweals/gnunet.git] / src / dht / gnunet-dht-driver.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  * @file dht/gnunet-dht-driver.c
22  * @brief Driver for setting up a group of gnunet peers and
23  *        then issuing GETS and PUTS on the DHT.  Coarse results
24  *        are reported, fine grained results (if requested) are
25  *        logged to a (mysql) database, or to file.
26  *
27  * FIXME: Do churn, enable malicious peers!
28  */
29 #include "platform.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_dht_service.h"
33 #include "dhtlog.h"
34 #include "dht.h"
35
36 /* DEFINES */
37 #define VERBOSE GNUNET_NO
38
39 /* Timeout for entire driver to run */
40 #define DEFAULT_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
41
42 /* Timeout for waiting for (individual) replies to get requests */
43 #define DEFAULT_GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
44
45 #define DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
46
47 /* Timeout for waiting for gets to be sent to the service */
48 #define DEFAULT_GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
49
50 /* Timeout for waiting for puts to be sent to the service */
51 #define DEFAULT_PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
52
53 /* Timeout for waiting for puts to be sent to the service */
54 #define DEFAULT_FIND_PEER_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 40)
55
56 #define DEFAULT_SECONDS_PER_PEER_START GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 45)
57
58 #define DEFAULT_TEST_DATA_SIZE 8
59
60 #define DEFAULT_BUCKET_SIZE 4
61
62 #define FIND_PEER_THRESHOLD DEFAULT_BUCKET_SIZE * 2
63
64 #define DEFAULT_MAX_OUTSTANDING_PUTS 10
65
66 #define DEFAULT_MAX_OUTSTANDING_FIND_PEERS 1
67
68 #define DEFAULT_MAX_OUTSTANDING_GETS 10
69
70 #define DEFAULT_CONNECT_TIMEOUT 60
71
72 #define DEFAULT_TOPOLOGY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
73
74 /*
75  * Default frequency for sending malicious get messages
76  */
77 #define DEFAULT_MALICIOUS_GET_FREQUENCY 1000 /* Number of milliseconds */
78
79 /*
80  * Default frequency for sending malicious put messages
81  */
82 #define DEFAULT_MALICIOUS_PUT_FREQUENCY 1000 /* Default is in milliseconds */
83
84 /* Structs */
85
86 struct MaliciousContext
87 {
88   /**
89    * Handle to DHT service (via the API)
90    */
91   struct GNUNET_DHT_Handle *dht_handle;
92
93   /**
94    *  Handle to the peer daemon
95    */
96   struct GNUNET_TESTING_Daemon *daemon;
97
98   /**
99    * Task for disconnecting DHT handles
100    */
101   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
102
103   /**
104    * What type of malicious to set this peer to.
105    */
106   int malicious_type;
107 };
108
109 struct TestFindPeer
110 {
111   /* This is a linked list */
112   struct TestFindPeer *next;
113
114   /* Handle to the bigger context */
115   struct FindPeerContext *find_peer_context;
116
117   /**
118    * Handle to the peer's DHT service (via the API)
119    */
120   struct GNUNET_DHT_Handle *dht_handle;
121
122   /**
123    *  Handle to the peer daemon
124    */
125   struct GNUNET_TESTING_Daemon *daemon;
126
127   /**
128    * Task for disconnecting DHT handles
129    */
130   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
131 };
132
133 struct TestPutContext
134 {
135   /* This is a linked list */
136   struct TestPutContext *next;
137
138   /**
139    * Handle to the first peers DHT service (via the API)
140    */
141   struct GNUNET_DHT_Handle *dht_handle;
142
143   /**
144    *  Handle to the PUT peer daemon
145    */
146   struct GNUNET_TESTING_Daemon *daemon;
147
148   /**
149    *  Identifier for this PUT
150    */
151   uint32_t uid;
152
153   /**
154    * Task for disconnecting DHT handles
155    */
156   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
157 };
158
159 struct TestGetContext
160 {
161   /* This is a linked list */
162   struct TestGetContext *next;
163
164   /**
165    * Handle to the first peers DHT service (via the API)
166    */
167   struct GNUNET_DHT_Handle *dht_handle;
168
169   /**
170    * Handle for the DHT get request
171    */
172   struct GNUNET_DHT_GetHandle *get_handle;
173
174   /**
175    *  Handle to the GET peer daemon
176    */
177   struct GNUNET_TESTING_Daemon *daemon;
178
179   /**
180    *  Identifier for this GET
181    */
182   uint32_t uid;
183
184   /**
185    * Task for disconnecting DHT handles (and stopping GET)
186    */
187   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
188
189   /**
190    * Whether or not this request has been fulfilled already.
191    */
192   int succeeded;
193 };
194
195 /**
196  * Simple struct to keep track of progress, and print a
197  * nice little percentage meter for long running tasks.
198  */
199 struct ProgressMeter
200 {
201   unsigned int total;
202
203   unsigned int modnum;
204
205   unsigned int dotnum;
206
207   unsigned int completed;
208
209   int print;
210
211   char *startup_string;
212 };
213
214 /**
215  * Linked list of information for populating statistics
216  * before ending trial.
217  */
218 struct StatisticsIteratorContext
219 {
220   const struct GNUNET_PeerIdentity *peer;
221   unsigned int stat_routes;
222   unsigned int stat_route_forwards;
223   unsigned int stat_results;
224   unsigned int stat_results_to_client;
225   unsigned int stat_result_forwards;
226   unsigned int stat_gets;
227   unsigned int stat_puts;
228   unsigned int stat_puts_inserted;
229   unsigned int stat_find_peer;
230   unsigned int stat_find_peer_start;
231   unsigned int stat_get_start;
232   unsigned int stat_put_start;
233   unsigned int stat_find_peer_reply;
234   unsigned int stat_get_reply;
235   unsigned int stat_find_peer_answer;
236   unsigned int stat_get_response_start;
237 };
238
239 /**
240  * Context for getting a topology, logging it, and continuing
241  * on with some next operation.
242  */
243 struct TopologyIteratorContext
244 {
245   unsigned int total_connections;
246   struct GNUNET_PeerIdentity *peer;
247   GNUNET_SCHEDULER_Task cont;
248   void *cls;
249   struct GNUNET_TIME_Relative timeout;
250 };
251
252 /* Globals */
253
254 /**
255  * Timeout to let all get requests happen.
256  */
257 static struct GNUNET_TIME_Relative all_get_timeout;
258
259 /**
260  * Per get timeout
261  */
262 static struct GNUNET_TIME_Relative get_timeout;
263
264 static struct GNUNET_TIME_Relative get_delay;
265
266 static struct GNUNET_TIME_Relative put_delay;
267
268 static struct GNUNET_TIME_Relative find_peer_delay;
269
270 static struct GNUNET_TIME_Relative seconds_per_peer_start;
271
272 static int do_find_peer;
273
274 static unsigned long long test_data_size = DEFAULT_TEST_DATA_SIZE;
275
276 static unsigned long long max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
277
278 static unsigned long long max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
279
280 static unsigned long long malicious_getters;
281
282 static unsigned long long max_outstanding_find_peers;
283
284 static unsigned long long malicious_putters;
285
286 static unsigned long long malicious_droppers;
287
288 static unsigned long long malicious_get_frequency;
289
290 static unsigned long long malicious_put_frequency;
291
292 static unsigned long long settle_time;
293
294 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
295
296 static unsigned long long trialuid;
297
298 /**
299  * Hash map of stats contexts.
300  */
301 struct GNUNET_CONTAINER_MultiHashMap *stats_map;
302
303 /**
304  * LL of malicious settings.
305  */
306 struct MaliciousContext *all_malicious;
307
308 /**
309  * List of GETS to perform
310  */
311 struct TestGetContext *all_gets;
312
313 /**
314  * List of PUTS to perform
315  */
316 struct TestPutContext *all_puts;
317
318 /**
319  * Directory to store temporary data in, defined in config file
320  */
321 static char *test_directory;
322
323 /**
324  * Variable used to store the number of connections we should wait for.
325  */
326 static unsigned int expected_connections;
327
328 /**
329  * Variable used to keep track of how many peers aren't yet started.
330  */
331 static unsigned long long peers_left;
332
333 /**
334  * Handle to the set of all peers run for this test.
335  */
336 static struct GNUNET_TESTING_PeerGroup *pg;
337
338 /**
339  * Global scheduler, used for all GNUNET_SCHEDULER_* functions.
340  */
341 static struct GNUNET_SCHEDULER_Handle *sched;
342
343 /**
344  * Global config handle.
345  */
346 const struct GNUNET_CONFIGURATION_Handle *config;
347
348 /**
349  * Total number of peers to run, set based on config file.
350  */
351 static unsigned long long num_peers;
352
353 /**
354  * Total number of items to insert.
355  */
356 static unsigned long long num_puts;
357
358 /**
359  * How many puts do we currently have in flight?
360  */
361 static unsigned long long outstanding_puts;
362
363 /**
364  * How many puts are done?
365  */
366 static unsigned long long puts_completed;
367
368 /**
369  * Total number of items to attempt to get.
370  */
371 static unsigned long long num_gets;
372
373 /**
374  * How many puts do we currently have in flight?
375  */
376 static unsigned long long outstanding_gets;
377
378 /**
379  * How many gets are done?
380  */
381 static unsigned long long gets_completed;
382
383 /**
384  * How many gets failed?
385  */
386 static unsigned long long gets_failed;
387
388 /**
389  * How many malicious control messages do
390  * we currently have in flight?
391  */
392 static unsigned long long outstanding_malicious;
393
394 /**
395  * How many set malicious peers are done?
396  */
397 static unsigned long long malicious_completed;
398
399 /**
400  * Global used to count how many connections we have currently
401  * been notified about (how many times has topology_callback been called
402  * with success?)
403  */
404 static unsigned int total_connections;
405
406 /**
407  * Global used to count how many failed connections we have
408  * been notified about (how many times has topology_callback
409  * been called with failure?)
410  */
411 static unsigned int failed_connections;
412
413 /* Task handle to use to schedule shutdown if something goes wrong */
414 GNUNET_SCHEDULER_TaskIdentifier die_task;
415
416 static char *blacklist_transports;
417
418 static enum GNUNET_TESTING_Topology topology;
419
420 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
421
422 static enum GNUNET_TESTING_Topology connect_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
423
424 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
425
426 static double connect_topology_option_modifier = 0.0;
427
428 static struct ProgressMeter *hostkey_meter;
429
430 static struct ProgressMeter *peer_start_meter;
431
432 static struct ProgressMeter *peer_connect_meter;
433
434 static struct ProgressMeter *put_meter;
435
436 static struct ProgressMeter *get_meter;
437
438 /* Global return value (0 for success, anything else for failure) */
439 static int ok;
440
441 /**
442  * Create a meter to keep track of the progress of some task.
443  *
444  * @param total the total number of items to complete
445  * @param start_string a string to prefix the meter with (if printing)
446  * @param print GNUNET_YES to print the meter, GNUNET_NO to count
447  *              internally only
448  *
449  * @return the progress meter
450  */
451 static struct ProgressMeter *
452 create_meter(unsigned int total, char * start_string, int print)
453 {
454   struct ProgressMeter *ret;
455   ret = GNUNET_malloc(sizeof(struct ProgressMeter));
456   ret->print = print;
457   ret->total = total;
458   ret->modnum = total / 4;
459   ret->dotnum = (total / 50) + 1;
460   if (start_string != NULL)
461     ret->startup_string = GNUNET_strdup(start_string);
462   else
463     ret->startup_string = GNUNET_strdup("");
464
465   return ret;
466 }
467
468 /**
469  * Update progress meter (increment by one).
470  *
471  * @param meter the meter to update and print info for
472  *
473  * @return GNUNET_YES if called the total requested,
474  *         GNUNET_NO if more items expected
475  */
476 static int
477 update_meter(struct ProgressMeter *meter)
478 {
479   if (meter->print == GNUNET_YES)
480     {
481       if (meter->completed % meter->modnum == 0)
482         {
483           if (meter->completed == 0)
484             {
485               fprintf(stdout, "%sProgress: [0%%", meter->startup_string);
486             }
487           else
488             fprintf(stdout, "%d%%", (int)(((float)meter->completed / meter->total) * 100));
489         }
490       else if (meter->completed % meter->dotnum == 0)
491         fprintf(stdout, ".");
492
493       if (meter->completed + 1 == meter->total)
494         fprintf(stdout, "%d%%]\n", 100);
495       fflush(stdout);
496     }
497   meter->completed++;
498
499   if (meter->completed == meter->total)
500     return GNUNET_YES;
501   return GNUNET_NO;
502 }
503
504 /**
505  * Release resources for meter
506  *
507  * @param meter the meter to free
508  */
509 static void
510 free_meter(struct ProgressMeter *meter)
511 {
512   GNUNET_free_non_null(meter->startup_string);
513   GNUNET_free_non_null(meter);
514 }
515
516 /**
517  * Check whether peers successfully shut down.
518  */
519 void shutdown_callback (void *cls,
520                         const char *emsg)
521 {
522   if (emsg != NULL)
523     {
524       if (ok == 0)
525         ok = 2;
526     }
527 }
528
529 /**
530  * Task to release DHT handles for PUT
531  */
532 static void
533 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
534 {
535   struct TestPutContext *test_put = cls;
536   test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
537   GNUNET_DHT_disconnect(test_put->dht_handle);
538   test_put->dht_handle = NULL;
539 }
540
541 /**
542  * Function scheduled to be run on the successful completion of this
543  * testcase.
544  */
545 static void
546 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
547 {
548   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ending test normally!\n", (char *)cls);
549   GNUNET_assert (pg != NULL);
550   struct TestPutContext *test_put = all_puts;
551   struct TestGetContext *test_get = all_gets;
552
553   while (test_put != NULL)
554     {
555       if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
556         GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
557       if (test_put->dht_handle != NULL)
558         GNUNET_DHT_disconnect(test_put->dht_handle);
559       test_put = test_put->next;
560     }
561
562   while (test_get != NULL)
563     {
564       if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
565         GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
566       if (test_get->get_handle != NULL)
567         GNUNET_DHT_get_stop(test_get->get_handle, NULL, NULL);
568       if (test_get->dht_handle != NULL)
569         GNUNET_DHT_disconnect(test_get->dht_handle);
570       test_get = test_get->next;
571     }
572
573   GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
574
575   if (dhtlog_handle != NULL)
576     {
577       fprintf(stderr, "Update trial endtime\n");
578       dhtlog_handle->update_trial (trialuid, gets_completed);
579       GNUNET_DHTLOG_disconnect(dhtlog_handle);
580       dhtlog_handle = NULL;
581     }
582
583   if (hostkey_meter != NULL)
584     free_meter(hostkey_meter);
585   if (peer_start_meter != NULL)
586     free_meter(peer_start_meter);
587   if (peer_connect_meter != NULL)
588     free_meter(peer_connect_meter);
589   if (put_meter != NULL)
590     free_meter(put_meter);
591   if (get_meter != NULL)
592     free_meter(get_meter);
593
594   ok = 0;
595 }
596
597 /**
598  * Callback for iterating over all the peer connections of a peer group.
599  */
600 void log_topology_cb (void *cls,
601                       const struct GNUNET_PeerIdentity *first,
602                       const struct GNUNET_PeerIdentity *second,
603                       struct GNUNET_TIME_Relative latency,
604                       uint32_t distance,
605                       const char *emsg)
606 {
607   struct TopologyIteratorContext *topo_ctx = cls;
608   if ((first != NULL) && (second != NULL))
609     {
610       topo_ctx->total_connections++;
611       if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(config, "dht_testing", "mysql_logging_extended"))
612         dhtlog_handle->insert_extended_topology(first, second);
613     }
614   else
615     {
616       GNUNET_assert(dhtlog_handle != NULL);
617       fprintf(stderr, "topology iteration finished (%u connections), scheduling continuation\n", topo_ctx->total_connections);
618       dhtlog_handle->update_topology(topo_ctx->total_connections);
619       if (topo_ctx->cont != NULL)
620         GNUNET_SCHEDULER_add_now (sched, topo_ctx->cont, topo_ctx->cls);
621       GNUNET_free(topo_ctx);
622     }
623 }
624
625 /**
626  * Iterator over hash map entries.
627  *
628  * @param cls closure - always NULL
629  * @param key current key code
630  * @param value value in the hash map, a stats context
631  * @return GNUNET_YES if we should continue to
632  *         iterate,
633  *         GNUNET_NO if not.
634  */
635 static int stats_iterate (void *cls,
636                           const GNUNET_HashCode * key,
637                           void *value)
638 {
639   struct StatisticsIteratorContext *stats_ctx;
640   if (value == NULL)
641     return GNUNET_NO;
642   stats_ctx = value;
643   dhtlog_handle->insert_stat(stats_ctx->peer, stats_ctx->stat_routes, stats_ctx->stat_route_forwards, stats_ctx->stat_results,
644                              stats_ctx->stat_results_to_client, stats_ctx->stat_result_forwards, stats_ctx->stat_gets,
645                              stats_ctx->stat_puts, stats_ctx->stat_puts_inserted, stats_ctx->stat_find_peer,
646                              stats_ctx->stat_find_peer_start, stats_ctx->stat_get_start, stats_ctx->stat_put_start,
647                              stats_ctx->stat_find_peer_reply, stats_ctx->stat_get_reply, stats_ctx->stat_find_peer_answer,
648                              stats_ctx->stat_get_response_start);
649   GNUNET_free(stats_ctx);
650   return GNUNET_YES;
651 }
652
653 static void stats_finished (void *cls, int result)
654 {
655   fprintf(stderr, "Finished getting all peers statistics, iterating!\n");
656   GNUNET_CONTAINER_multihashmap_iterate(stats_map, &stats_iterate, NULL);
657   GNUNET_CONTAINER_multihashmap_destroy(stats_map);
658   GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
659 }
660
661 /**
662  * Callback function to process statistic values.
663  *
664  * @param cls closure
665  * @param peer the peer the statistics belong to
666  * @param subsystem name of subsystem that created the statistic
667  * @param name the name of the datum
668  * @param value the current value
669  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
670  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
671  */
672 static int stats_handle  (void *cls,
673                           const struct GNUNET_PeerIdentity *peer,
674                           const char *subsystem,
675                           const char *name,
676                           uint64_t value,
677                           int is_persistent)
678 {
679   struct StatisticsIteratorContext *stats_ctx;
680
681   if (dhtlog_handle != NULL)
682     dhtlog_handle->add_generic_stat(peer, name, subsystem, value);
683   if (GNUNET_CONTAINER_multihashmap_contains(stats_map, &peer->hashPubKey))
684     {
685       stats_ctx = GNUNET_CONTAINER_multihashmap_get(stats_map, &peer->hashPubKey);
686     }
687   else
688     {
689       stats_ctx = GNUNET_malloc(sizeof(struct StatisticsIteratorContext));
690       stats_ctx->peer = peer;
691       GNUNET_CONTAINER_multihashmap_put(stats_map, &peer->hashPubKey, stats_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
692     }
693   GNUNET_assert(stats_ctx != NULL);
694
695   if (strcmp(name, STAT_ROUTES) == 0)
696     stats_ctx->stat_routes = value;
697   else if (strcmp(name, STAT_ROUTE_FORWARDS) == 0)
698     stats_ctx->stat_route_forwards = value;
699   else if (strcmp(name, STAT_RESULTS) == 0)
700     stats_ctx->stat_results = value;
701   else if (strcmp(name, STAT_RESULTS_TO_CLIENT) == 0)
702     stats_ctx->stat_results_to_client = value;
703   else if (strcmp(name, STAT_RESULT_FORWARDS) == 0)
704     stats_ctx->stat_result_forwards = value;
705   else if (strcmp(name, STAT_GETS) == 0)
706     stats_ctx->stat_gets = value;
707   else if (strcmp(name, STAT_PUTS) == 0)
708     stats_ctx->stat_puts = value;
709   else if (strcmp(name, STAT_PUTS_INSERTED) == 0)
710     stats_ctx->stat_puts_inserted = value;
711   else if (strcmp(name, STAT_FIND_PEER) == 0)
712     stats_ctx->stat_find_peer = value;
713   else if (strcmp(name, STAT_FIND_PEER_START) == 0)
714     stats_ctx->stat_find_peer_start = value;
715   else if (strcmp(name, STAT_GET_START) == 0)
716     stats_ctx->stat_get_start = value;
717   else if (strcmp(name, STAT_PUT_START) == 0)
718     stats_ctx->stat_put_start = value;
719   else if (strcmp(name, STAT_FIND_PEER_REPLY) == 0)
720     stats_ctx->stat_find_peer_reply = value;
721   else if (strcmp(name, STAT_GET_REPLY) == 0)
722     stats_ctx->stat_get_reply = value;
723   else if (strcmp(name, STAT_FIND_PEER_ANSWER) == 0)
724     stats_ctx->stat_find_peer_answer = value;
725   else if (strcmp(name, STAT_GET_RESPONSE_START) == 0)
726     stats_ctx->stat_get_response_start = value;
727
728   return GNUNET_OK;
729 }
730
731 /**
732  * Connect to statistics service for each peer and get the appropriate
733  * dht statistics for safe keeping.
734  */
735 static void
736 log_dht_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
737 {
738   stats_map = GNUNET_CONTAINER_multihashmap_create(num_peers);
739   fprintf(stderr, "Starting statistics logging\n");
740   GNUNET_TESTING_get_statistics(pg, &stats_finished, &stats_handle, NULL);
741 }
742
743
744 /**
745  * Connect to all peers in the peer group and iterate over their
746  * connections.
747  */
748 static void
749 capture_current_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
750 {
751   struct TopologyIteratorContext *topo_ctx = cls;
752   dhtlog_handle->insert_topology(0);
753   GNUNET_TESTING_get_topology (pg, &log_topology_cb, topo_ctx);
754 }
755
756
757 /**
758  * Check if the get_handle is being used, if so stop the request.  Either
759  * way, schedule the end_badly_cont function which actually shuts down the
760  * test.
761  */
762 static void
763 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
764 {
765   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failing test with error: `%s'!\n", (char *)cls);
766
767   struct TestPutContext *test_put = all_puts;
768   struct TestGetContext *test_get = all_gets;
769
770   while (test_put != NULL)
771     {
772       if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
773         GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
774       if (test_put->dht_handle != NULL)
775         GNUNET_DHT_disconnect(test_put->dht_handle);
776       test_put = test_put->next;
777     }
778
779   while (test_get != NULL)
780     {
781       if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
782         GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
783       if (test_get->get_handle != NULL)
784         GNUNET_DHT_get_stop(test_get->get_handle, NULL, NULL);
785       if (test_get->dht_handle != NULL)
786         GNUNET_DHT_disconnect(test_get->dht_handle);
787       test_get = test_get->next;
788     }
789
790   GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
791
792   if (dhtlog_handle != NULL)
793     {
794       fprintf(stderr, "Update trial endtime\n");
795       dhtlog_handle->update_trial (trialuid, gets_completed);
796       GNUNET_DHTLOG_disconnect(dhtlog_handle);
797       dhtlog_handle = NULL;
798     }
799
800   if (hostkey_meter != NULL)
801     free_meter(hostkey_meter);
802   if (peer_start_meter != NULL)
803     free_meter(peer_start_meter);
804   if (peer_connect_meter != NULL)
805     free_meter(peer_connect_meter);
806   if (put_meter != NULL)
807     free_meter(put_meter);
808   if (get_meter != NULL)
809     free_meter(get_meter);
810
811   ok = 1;
812 }
813
814 /**
815  * Task to release DHT handle associated with GET request.
816  */
817 static void
818 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
819 {
820   struct TestGetContext *test_get = cls;
821   struct TopologyIteratorContext *topo_ctx;
822   outstanding_gets--; /* GET is really finished */
823   GNUNET_DHT_disconnect(test_get->dht_handle);
824   test_get->dht_handle = NULL;
825
826 #if VERBOSE > 1
827   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n", gets_completed, gets_failed);
828 #endif
829   update_meter(get_meter);
830   if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0))
831     {
832       GNUNET_SCHEDULER_cancel(sched, die_task);
833       //GNUNET_SCHEDULER_add_now(sched, &finish_testing, NULL);
834       if (dhtlog_handle != NULL)
835         {
836           topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
837           topo_ctx->cont = &log_dht_statistics;
838           GNUNET_SCHEDULER_add_now(sched, &capture_current_topology, topo_ctx);
839         }
840       else
841         GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
842     }
843 }
844
845 /**
846  * Task to release get handle.
847  */
848 static void
849 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
850 {
851   struct TestGetContext *test_get = cls;
852
853   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
854     gets_failed++;
855   GNUNET_assert(test_get->get_handle != NULL);
856   GNUNET_DHT_get_stop(test_get->get_handle, &get_stop_finished, test_get);
857   test_get->get_handle = NULL;
858   test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
859 }
860
861 /**
862  * Iterator called if the GET request initiated returns a response.
863  *
864  * @param cls closure
865  * @param exp when will this value expire
866  * @param key key of the result
867  * @param type type of the result
868  * @param size number of bytes in data
869  * @param data pointer to the result data
870  */
871 void get_result_iterator (void *cls,
872                           struct GNUNET_TIME_Absolute exp,
873                           const GNUNET_HashCode * key,
874                           uint32_t type,
875                           uint32_t size,
876                           const void *data)
877 {
878   struct TestGetContext *test_get = cls;
879   GNUNET_HashCode search_key; /* Key stored under */
880   char original_data[test_data_size]; /* Made up data to store */
881
882   memset(original_data, test_get->uid, sizeof(original_data));
883   GNUNET_CRYPTO_hash(original_data, test_data_size, &search_key);
884
885   if (test_get->succeeded == GNUNET_YES)
886     return; /* Get has already been successful, probably ending now */
887
888   if ((0 != memcmp(&search_key, key, sizeof (GNUNET_HashCode))) || (0 != memcmp(original_data, data, sizeof(original_data))))
889     {
890       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key or data is not the same as was inserted!\n");
891     }
892   else
893     {
894       gets_completed++;
895       test_get->succeeded = GNUNET_YES;
896     }
897 #if VERBOSE > 1
898   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
899 #endif
900   GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
901   GNUNET_SCHEDULER_add_continuation(sched, &get_stop_task, test_get, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
902 }
903
904 /**
905  * Continuation telling us GET request was sent.
906  */
907 static void
908 get_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
909 {
910   // Is there something to be done here?
911   if (tc->reason != GNUNET_SCHEDULER_REASON_PREREQ_DONE)
912     return;
913 }
914
915 /**
916  * Set up some data, and call API PUT function
917  */
918 static void
919 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
920 {
921   struct TestGetContext *test_get = cls;
922   GNUNET_HashCode key; /* Made up key to store data under */
923   char data[test_data_size]; /* Made up data to store */
924
925   if (num_gets == 0)
926     {
927       GNUNET_SCHEDULER_cancel(sched, die_task);
928       GNUNET_SCHEDULER_add_now(sched, &finish_testing, NULL);
929     }
930   if (test_get == NULL)
931     return; /* End of the list */
932
933   memset(data, test_get->uid, sizeof(data));
934   GNUNET_CRYPTO_hash(data, test_data_size, &key);
935
936   if (outstanding_gets > max_outstanding_gets)
937     {
938       GNUNET_SCHEDULER_add_delayed (sched, get_delay, &do_get, test_get);
939       return;
940     }
941
942   test_get->dht_handle = GNUNET_DHT_connect(sched, test_get->daemon->cfg, 10);
943   /* Insert the data at the first peer */
944   GNUNET_assert(test_get->dht_handle != NULL);
945   outstanding_gets++;
946   test_get->get_handle = GNUNET_DHT_get_start(test_get->dht_handle,
947                                               GNUNET_TIME_relative_get_forever(),
948                                               1,
949                                               &key,
950                                               &get_result_iterator,
951                                               test_get,
952                                               &get_continuation,
953                                               test_get);
954 #if VERBOSE > 1
955   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
956              test_get->uid,
957              test_get->daemon->shortname);
958 #endif
959   test_get->disconnect_task = GNUNET_SCHEDULER_add_delayed(sched, get_timeout, &get_stop_task, test_get);
960   GNUNET_SCHEDULER_add_now (sched, &do_get, test_get->next);
961 }
962
963 /**
964  * Called when the PUT request has been transmitted to the DHT service.
965  * Schedule the GET request for some time in the future.
966  */
967 static void
968 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
969 {
970   struct TestPutContext *test_put = cls;
971   struct TopologyIteratorContext *topo_ctx;
972   outstanding_puts--;
973   puts_completed++;
974
975   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
976     fprintf(stderr, "PUT Request failed!\n");
977
978   GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
979   test_put->disconnect_task = GNUNET_SCHEDULER_add_now(sched, &put_disconnect_task, test_put);
980   if (GNUNET_YES == update_meter(put_meter))
981     {
982       GNUNET_assert(outstanding_puts == 0);
983       GNUNET_SCHEDULER_cancel (sched, die_task);
984       if (dhtlog_handle != NULL)
985         {
986           topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
987           topo_ctx->cont = &do_get;
988           topo_ctx->cls = all_gets;
989           topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
990           die_task = GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_add(GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout), DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
991                                                    &end_badly, "from do gets");
992           GNUNET_SCHEDULER_add_now(sched, &capture_current_topology, topo_ctx);
993         }
994       else
995         {
996           die_task = GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout),
997                                                        &end_badly, "from do gets");
998           GNUNET_SCHEDULER_add_delayed(sched, DEFAULT_GET_TIMEOUT, &do_get, all_gets);
999           GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
1000         }
1001       return;
1002     }
1003 }
1004
1005 /**
1006  * Set up some data, and call API PUT function
1007  */
1008 static void
1009 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1010 {
1011   struct TestPutContext *test_put = cls;
1012   GNUNET_HashCode key; /* Made up key to store data under */
1013   char data[test_data_size]; /* Made up data to store */
1014   uint32_t rand;
1015
1016   if (test_put == NULL)
1017     return; /* End of list */
1018
1019   memset(data, test_put->uid, sizeof(data));
1020   GNUNET_CRYPTO_hash(data, test_data_size, &key);
1021
1022   if (outstanding_puts > max_outstanding_puts)
1023     {
1024       GNUNET_SCHEDULER_add_delayed (sched, put_delay, &do_put, test_put);
1025       return;
1026     }
1027
1028 #if VERBOSE > 1
1029     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
1030                test_put->uid,
1031                test_put->daemon->shortname);
1032 #endif
1033   test_put->dht_handle = GNUNET_DHT_connect(sched, test_put->daemon->cfg, 10);
1034
1035   GNUNET_assert(test_put->dht_handle != NULL);
1036   outstanding_puts++;
1037   GNUNET_DHT_put(test_put->dht_handle,
1038                  &key,
1039                  1,
1040                  sizeof(data), data,
1041                  GNUNET_TIME_absolute_get_forever(),
1042                  GNUNET_TIME_relative_get_forever(),
1043                  &put_finished, test_put);
1044   test_put->disconnect_task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_get_forever(), &put_disconnect_task, test_put);
1045   rand = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
1046   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, rand), &do_put, test_put->next);
1047 }
1048
1049 /**
1050  * Context for sending out find peer requests.
1051  */
1052 struct FindPeerContext
1053 {
1054   struct GNUNET_DHT_Handle *dht_handle;
1055   struct GNUNET_TIME_Absolute endtime;
1056   unsigned int current_peers;
1057   unsigned int previous_peers;
1058   unsigned int outstanding;
1059   unsigned int total;
1060 };
1061
1062 static void
1063 schedule_find_peer_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1064
1065 /**
1066  * Given a number of total peers and a bucket size, estimate the number of
1067  * connections in a perfect kademlia topology.
1068  */
1069 static unsigned int connection_estimate(unsigned int peer_count, unsigned int bucket_size)
1070 {
1071   unsigned int i;
1072   unsigned int filled;
1073   i = num_peers;
1074
1075   filled = 0;
1076   while (i > bucket_size)
1077     {
1078       filled++;
1079       i = i/2;
1080     }
1081   return filled * bucket_size * peer_count;
1082
1083 }
1084
1085 /**
1086  * Callback for iterating over all the peer connections of a peer group.
1087  */
1088 void count_peers_cb (void *cls,
1089                       const struct GNUNET_PeerIdentity *first,
1090                       const struct GNUNET_PeerIdentity *second,
1091                       struct GNUNET_TIME_Relative latency,
1092                       uint32_t distance,
1093                       const char *emsg)
1094 {
1095   struct FindPeerContext *find_peer_context = cls;
1096   if ((first != NULL) && (second != NULL))
1097     {
1098       find_peer_context->current_peers++;
1099     }
1100   else
1101     {
1102       GNUNET_assert(dhtlog_handle != NULL);
1103       fprintf(stderr, "peer count finished (%u connections), %u new peers, connection estimate %u\n", find_peer_context->current_peers, find_peer_context->current_peers - find_peer_context->previous_peers, connection_estimate(num_peers, DEFAULT_BUCKET_SIZE));
1104       if ((find_peer_context->current_peers - find_peer_context->previous_peers > FIND_PEER_THRESHOLD) &&
1105           (find_peer_context->current_peers < connection_estimate(num_peers, DEFAULT_BUCKET_SIZE)) &&
1106           (GNUNET_TIME_absolute_get_remaining(find_peer_context->endtime).value > 0))
1107         {
1108           fprintf(stderr, "Scheduling another round of find peer requests.\n");
1109           GNUNET_SCHEDULER_add_now(sched, schedule_find_peer_requests, find_peer_context);
1110         }
1111       else
1112         {
1113           fprintf(stderr, "Not sending any more find peer requests.\n");
1114         }
1115     }
1116 }
1117
1118 /**
1119  * Connect to all peers in the peer group and iterate over their
1120  * connections.
1121  */
1122 static void
1123 count_new_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1124 {
1125   struct FindPeerContext *find_peer_context = cls;
1126   find_peer_context->previous_peers = find_peer_context->current_peers;
1127   find_peer_context->current_peers = 0;
1128   GNUNET_TESTING_get_topology (pg, &count_peers_cb, find_peer_context);
1129 }
1130
1131
1132 static void
1133 decrement_find_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1134 {
1135   struct TestFindPeer *test_find_peer = cls;
1136   GNUNET_assert(test_find_peer->find_peer_context->outstanding > 0);
1137   test_find_peer->find_peer_context->outstanding--;
1138   test_find_peer->find_peer_context->total--;
1139   GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%d find_peers remaining\n", test_find_peer->find_peer_context->total);
1140   if ((0 == test_find_peer->find_peer_context->total) &&
1141       (GNUNET_TIME_absolute_get_remaining(test_find_peer->find_peer_context->endtime).value > 0))
1142   {
1143     GNUNET_SCHEDULER_add_now(sched, &count_new_peers, test_find_peer->find_peer_context);
1144   }
1145   GNUNET_free(test_find_peer);
1146 }
1147
1148 /**
1149  * A find peer request has been sent to the server, now we will schedule a task
1150  * to wait the appropriate time to allow the request to go out and back.
1151  *
1152  * @param cls closure - a TestFindPeer struct
1153  * @param tc context the task is being called with
1154  */
1155 static void
1156 handle_find_peer_sent (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1157 {
1158   struct TestFindPeer *test_find_peer = cls;
1159
1160   GNUNET_DHT_disconnect(test_find_peer->dht_handle);
1161   GNUNET_SCHEDULER_add_delayed(sched, find_peer_delay, &decrement_find_peers, test_find_peer);
1162 }
1163
1164 static void
1165 send_find_peer_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1166 {
1167   struct TestFindPeer *test_find_peer = cls;
1168
1169   if (test_find_peer->find_peer_context->outstanding > max_outstanding_find_peers)
1170   {
1171     GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 300), &send_find_peer_request, test_find_peer);
1172     return;
1173   }
1174
1175   test_find_peer->find_peer_context->outstanding++;
1176   if (GNUNET_TIME_absolute_get_remaining(test_find_peer->find_peer_context->endtime).value == 0)
1177   {
1178     GNUNET_SCHEDULER_add_now(sched, &decrement_find_peers, test_find_peer);
1179     return;
1180   }
1181
1182   test_find_peer->dht_handle = GNUNET_DHT_connect(sched, test_find_peer->daemon->cfg, 1);
1183   GNUNET_assert(test_find_peer->dht_handle != NULL);
1184   fprintf(stderr, "calling GNUNET_DHT_find_peers\n");
1185   GNUNET_DHT_find_peers (test_find_peer->dht_handle,
1186                          &handle_find_peer_sent, test_find_peer);
1187 }
1188
1189 /**
1190  * Set up a single find peer request for each peer in the topology.  Do this
1191  * until the settle time is over, limited by the number of outstanding requests
1192  * and the time allowed for each one!
1193  */
1194 static void
1195 schedule_find_peer_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1196 {
1197   struct FindPeerContext *find_peer_ctx = cls;
1198   struct TestFindPeer *test_find_peer;
1199   uint32_t i;
1200   uint32_t random;
1201
1202   for (i = 0; i < max_outstanding_find_peers; i++)
1203     {
1204       test_find_peer = GNUNET_malloc(sizeof(struct TestFindPeer));
1205       random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
1206       test_find_peer->daemon  = GNUNET_TESTING_daemon_get(pg, random);
1207       test_find_peer->find_peer_context = find_peer_ctx;
1208       find_peer_ctx->total++;
1209       GNUNET_SCHEDULER_add_now(sched, &send_find_peer_request, test_find_peer);
1210     }
1211 }
1212
1213 /**
1214  * Set up some all of the put and get operations we want
1215  * to do.  Allocate data structure for each, add to list,
1216  * then call actual insert functions.
1217  */
1218 static void
1219 setup_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1220 {
1221   int i;
1222   uint32_t temp_daemon;
1223   struct TestPutContext *test_put;
1224   struct TestGetContext *test_get;
1225   int remember[num_puts][num_peers];
1226
1227   memset(&remember, 0, sizeof(int) * num_puts * num_peers);
1228   for (i = 0; i < num_puts; i++)
1229     {
1230       test_put = GNUNET_malloc(sizeof(struct TestPutContext));
1231       test_put->uid = i;
1232       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
1233       test_put->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
1234       test_put->next = all_puts;
1235       all_puts = test_put;
1236     }
1237
1238   for (i = 0; i < num_gets; i++)
1239     {
1240       test_get = GNUNET_malloc(sizeof(struct TestGetContext));
1241       test_get->uid = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
1242       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
1243       while (remember[test_get->uid][temp_daemon] == 1)
1244         temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
1245       test_get->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
1246       remember[test_get->uid][temp_daemon] = 1;
1247       test_get->next = all_gets;
1248       all_gets = test_get;
1249     }
1250
1251   /*GNUNET_SCHEDULER_cancel (sched, die_task);*/
1252   die_task = GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, num_puts * 2),
1253                                            &end_badly, "from do puts");
1254   GNUNET_SCHEDULER_add_now (sched, &do_put, all_puts);
1255 }
1256
1257 /**
1258  * Set up some all of the put and get operations we want
1259  * to do.  Allocate data structure for each, add to list,
1260  * then call actual insert functions.
1261  */
1262 static void
1263 continue_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1264 {
1265   int i;
1266   int max;
1267   struct TopologyIteratorContext *topo_ctx;
1268   struct FindPeerContext *find_peer_context;
1269   if (dhtlog_handle != NULL)
1270     {
1271       if (settle_time >= 60 * 2)
1272         max = (settle_time / 60) - 2;
1273       else
1274         max = 1;
1275       for (i = 1; i < max; i++)
1276         {
1277           topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1278           fprintf(stderr, "scheduled topology iteration in %d minutes\n", i);
1279           GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, i), &capture_current_topology, topo_ctx);
1280         }
1281       topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1282       topo_ctx->cont = &setup_puts_and_gets;
1283       GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time), &capture_current_topology, topo_ctx);
1284     }
1285   else
1286     GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time), &setup_puts_and_gets, NULL);
1287
1288   if (GNUNET_YES == do_find_peer)
1289   {
1290     find_peer_context = GNUNET_malloc(sizeof(struct FindPeerContext));
1291     find_peer_context->endtime = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time));
1292     GNUNET_SCHEDULER_add_now(sched, &schedule_find_peer_requests, find_peer_context);
1293   }
1294 }
1295
1296 /**
1297  * Task to release DHT handles
1298  */
1299 static void
1300 malicious_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1301 {
1302   struct MaliciousContext *ctx = cls;
1303   outstanding_malicious--;
1304   malicious_completed++;
1305   ctx->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1306   GNUNET_DHT_disconnect(ctx->dht_handle);
1307   ctx->dht_handle = NULL;
1308   GNUNET_free(ctx);
1309
1310   if (malicious_completed == malicious_getters + malicious_putters + malicious_droppers)
1311     {
1312       GNUNET_SCHEDULER_cancel(sched, die_task);
1313       fprintf(stderr, "Finished setting all malicious peers up, calling continuation!\n");
1314       if (dhtlog_handle != NULL)
1315         GNUNET_SCHEDULER_add_now (sched,
1316                                   &continue_puts_and_gets, NULL);
1317       else
1318         GNUNET_SCHEDULER_add_delayed (sched,
1319                                     GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time),
1320                                     &continue_puts_and_gets, NULL);
1321     }
1322
1323 }
1324
1325 /**
1326  * Task to release DHT handles
1327  */
1328 static void
1329 malicious_done_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1330 {
1331   struct MaliciousContext *ctx = cls;
1332   GNUNET_SCHEDULER_cancel(sched, ctx->disconnect_task);
1333   GNUNET_SCHEDULER_add_now(sched, &malicious_disconnect_task, ctx);
1334 }
1335
1336 /**
1337  * Set up some data, and call API PUT function
1338  */
1339 static void
1340 set_malicious (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1341 {
1342   struct MaliciousContext *ctx = cls;
1343   int ret;
1344
1345   if (outstanding_malicious > DEFAULT_MAX_OUTSTANDING_GETS)
1346     {
1347       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &set_malicious, ctx);
1348       return;
1349     }
1350
1351   if (ctx->dht_handle == NULL)
1352     {
1353       ctx->dht_handle = GNUNET_DHT_connect(sched, ctx->daemon->cfg, 1);
1354       outstanding_malicious++;
1355     }
1356
1357   GNUNET_assert(ctx->dht_handle != NULL);
1358
1359
1360 #if VERBOSE > 1
1361     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting peer %s malicious type %d\n",
1362                 ctx->daemon->shortname, ctx->malicious_type);
1363 #endif
1364
1365   ret = GNUNET_YES;
1366   switch (ctx->malicious_type)
1367   {
1368   case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
1369     ret = GNUNET_DHT_set_malicious_getter(ctx->dht_handle, malicious_get_frequency, &malicious_done_task, ctx);
1370     break;
1371   case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
1372     ret = GNUNET_DHT_set_malicious_putter(ctx->dht_handle, malicious_put_frequency, &malicious_done_task, ctx);
1373     break;
1374   case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
1375     ret = GNUNET_DHT_set_malicious_dropper(ctx->dht_handle, &malicious_done_task, ctx);
1376     break;
1377   default:
1378     break;
1379   }
1380
1381   if (ret == GNUNET_NO)
1382     {
1383       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &set_malicious, ctx);
1384     }
1385   else
1386     ctx->disconnect_task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_get_forever(), &malicious_disconnect_task, ctx);
1387 }
1388
1389 /**
1390  * Select randomly from set of known peers,
1391  * set the desired number of peers to the
1392  * proper malicious types.
1393  */
1394 static void
1395 setup_malicious_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1396 {
1397   struct MaliciousContext *ctx;
1398   int i;
1399   uint32_t temp_daemon;
1400
1401   for (i = 0; i < malicious_getters; i++)
1402     {
1403       ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
1404       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
1405       ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
1406       ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET;
1407       GNUNET_SCHEDULER_add_now (sched, &set_malicious, ctx);
1408
1409     }
1410
1411   for (i = 0; i < malicious_putters; i++)
1412     {
1413       ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
1414       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
1415       ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
1416       ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT;
1417       GNUNET_SCHEDULER_add_now (sched, &set_malicious, ctx);
1418
1419     }
1420
1421   for (i = 0; i < malicious_droppers; i++)
1422     {
1423       ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
1424       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
1425       ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
1426       ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP;
1427       GNUNET_SCHEDULER_add_now (sched, &set_malicious, ctx);
1428     }
1429
1430   if (malicious_getters + malicious_putters + malicious_droppers > 0)
1431     die_task = GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, (malicious_getters + malicious_putters + malicious_droppers) * 2),
1432                                              &end_badly, "from set malicious");
1433   else
1434     {
1435       if (dhtlog_handle != NULL)
1436         GNUNET_SCHEDULER_add_now (sched,
1437                                   &continue_puts_and_gets, NULL);
1438       else
1439         GNUNET_SCHEDULER_add_delayed (sched,
1440                                     GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time),
1441                                     &continue_puts_and_gets, NULL);
1442     }
1443
1444 }
1445
1446 /**
1447  * This function is called whenever a connection attempt is finished between two of
1448  * the started peers (started with GNUNET_TESTING_daemons_start).  The total
1449  * number of times this function is called should equal the number returned
1450  * from the GNUNET_TESTING_connect_topology call.
1451  *
1452  * The emsg variable is NULL on success (peers connected), and non-NULL on
1453  * failure (peers failed to connect).
1454  */
1455 void
1456 topology_callback (void *cls,
1457                    const struct GNUNET_PeerIdentity *first,
1458                    const struct GNUNET_PeerIdentity *second,
1459                    uint32_t distance,
1460                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1461                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1462                    struct GNUNET_TESTING_Daemon *first_daemon,
1463                    struct GNUNET_TESTING_Daemon *second_daemon,
1464                    const char *emsg)
1465 {
1466   struct TopologyIteratorContext *topo_ctx;
1467   if (emsg == NULL)
1468     {
1469       total_connections++;
1470 #if VERBOSE > 1
1471       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
1472                  first_daemon->shortname,
1473                  second_daemon->shortname,
1474                  distance);
1475 #endif
1476     }
1477 #if VERBOSE
1478   else
1479     {
1480       failed_connections++;
1481       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
1482                   first_daemon->shortname,
1483                   second_daemon->shortname, emsg);
1484     }
1485 #endif
1486   GNUNET_assert(peer_connect_meter != NULL);
1487   if (GNUNET_YES == update_meter(peer_connect_meter))
1488     {
1489 #if VERBOSE
1490       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491                   "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
1492                   total_connections);
1493 #endif
1494       if (dhtlog_handle != NULL)
1495         {
1496           dhtlog_handle->update_connections (trialuid, total_connections);
1497           dhtlog_handle->insert_topology(expected_connections);
1498         }
1499
1500       GNUNET_SCHEDULER_cancel (sched, die_task);
1501       /*die_task = GNUNET_SCHEDULER_add_delayed (sched, DEFAULT_TIMEOUT,
1502                                                &end_badly, "from setup puts/gets");*/
1503       if ((dhtlog_handle != NULL) && (settle_time > 0))
1504         {
1505           topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1506           topo_ctx->cont = &setup_malicious_peers;
1507           //topo_ctx->cont = &continue_puts_and_gets;
1508           GNUNET_SCHEDULER_add_now(sched, &capture_current_topology, topo_ctx);
1509         }
1510       else
1511         {
1512           GNUNET_SCHEDULER_add_now(sched, &setup_malicious_peers, NULL);
1513           /*GNUNET_SCHEDULER_add_delayed (sched,
1514                                         GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time),
1515                                         &continue_puts_and_gets, NULL);*/
1516         }
1517     }
1518   else if (total_connections + failed_connections == expected_connections)
1519     {
1520       GNUNET_SCHEDULER_cancel (sched, die_task);
1521       die_task = GNUNET_SCHEDULER_add_now (sched,
1522                                            &end_badly, "from topology_callback (too many failed connections)");
1523     }
1524 }
1525
1526 static void
1527 peers_started_callback (void *cls,
1528        const struct GNUNET_PeerIdentity *id,
1529        const struct GNUNET_CONFIGURATION_Handle *cfg,
1530        struct GNUNET_TESTING_Daemon *d, const char *emsg)
1531 {
1532   if (emsg != NULL)
1533     {
1534       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
1535                   emsg);
1536       return;
1537     }
1538   GNUNET_assert (id != NULL);
1539
1540 #if VERBOSE > 1
1541   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
1542               (num_peers - peers_left) + 1, num_peers);
1543 #endif
1544
1545   peers_left--;
1546
1547   if (GNUNET_YES == update_meter(peer_start_meter))
1548     {
1549 #if VERBOSE
1550       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551                   "All %d daemons started, now connecting peers!\n",
1552                   num_peers);
1553 #endif
1554       GNUNET_SCHEDULER_cancel (sched, die_task);
1555
1556       expected_connections = -1;
1557       if ((pg != NULL) && (peers_left == 0))
1558         {
1559           expected_connections = GNUNET_TESTING_connect_topology (pg, connect_topology, connect_topology_option, connect_topology_option_modifier);
1560
1561           peer_connect_meter = create_meter(expected_connections, "Peer connection ", GNUNET_YES);
1562           fprintf(stderr, "Have %d expected connections\n", expected_connections);
1563         }
1564
1565       if (expected_connections == GNUNET_SYSERR)
1566         {
1567           die_task = GNUNET_SCHEDULER_add_now (sched,
1568                                                &end_badly, "from connect topology (bad return)");
1569         }
1570
1571       die_task = GNUNET_SCHEDULER_add_delayed (sched,
1572                                                GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, DEFAULT_CONNECT_TIMEOUT * expected_connections),
1573                                                &end_badly, "from connect topology (timeout)");
1574
1575       ok = 0;
1576     }
1577 }
1578
1579 static void
1580 create_topology ()
1581 {
1582   peers_left = num_peers; /* Reset counter */
1583   if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
1584     {
1585 #if VERBOSE
1586       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587                   "Topology set up, now starting peers!\n");
1588 #endif
1589       GNUNET_TESTING_daemons_continue_startup(pg);
1590     }
1591   else
1592     {
1593       GNUNET_SCHEDULER_cancel (sched, die_task);
1594       die_task = GNUNET_SCHEDULER_add_now (sched,
1595                                            &end_badly, "from create topology (bad return)");
1596     }
1597   GNUNET_free_non_null(blacklist_transports);
1598   GNUNET_SCHEDULER_cancel (sched, die_task);
1599   die_task = GNUNET_SCHEDULER_add_delayed (sched,
1600                                            GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
1601                                            &end_badly, "from continue startup (timeout)");
1602 }
1603
1604 /**
1605  * Callback indicating that the hostkey was created for a peer.
1606  *
1607  * @param cls NULL
1608  * @param id the peer identity
1609  * @param d the daemon handle (pretty useless at this point, remove?)
1610  * @param emsg non-null on failure
1611  */
1612 void hostkey_callback (void *cls,
1613                        const struct GNUNET_PeerIdentity *id,
1614                        struct GNUNET_TESTING_Daemon *d,
1615                        const char *emsg)
1616 {
1617   if (emsg != NULL)
1618     {
1619       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Hostkey callback received error: %s\n", emsg);
1620     }
1621
1622 #if VERBOSE > 1
1623     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624                 "Hostkey (%d/%d) created for peer `%s'\n",
1625                 num_peers - peers_left, num_peers, GNUNET_i2s(id));
1626 #endif
1627
1628     peers_left--;
1629     if (GNUNET_YES == update_meter(hostkey_meter))
1630       {
1631 #if VERBOSE
1632         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1633                     "All %d hostkeys created, now creating topology!\n",
1634                     num_peers);
1635 #endif
1636         GNUNET_SCHEDULER_cancel (sched, die_task);
1637         /* Set up task in case topology creation doesn't finish
1638          * within a reasonable amount of time */
1639         die_task = GNUNET_SCHEDULER_add_delayed (sched,
1640                                                  DEFAULT_TOPOLOGY_TIMEOUT,
1641                                                  &end_badly, "from create_topology");
1642         GNUNET_SCHEDULER_add_now(sched, &create_topology, NULL);
1643         ok = 0;
1644       }
1645 }
1646
1647
1648 static void
1649 run (void *cls,
1650      struct GNUNET_SCHEDULER_Handle *s,
1651      char *const *args,
1652      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
1653 {
1654   struct stat frstat;
1655   struct GNUNET_TESTING_Host *hosts;
1656   struct GNUNET_TESTING_Host *temphost;
1657   char *topology_str;
1658   char *connect_topology_str;
1659   char *blacklist_topology_str;
1660   char *connect_topology_option_str;
1661   char *connect_topology_option_modifier_string;
1662   char *trialmessage;
1663   char *topology_percentage_str;
1664   float topology_percentage;
1665   char *topology_probability_str;
1666   char *hostfile;
1667   float topology_probability;
1668   unsigned long long temp_config_number;
1669   int stop_closest;
1670   int stop_found;
1671   int strict_kademlia;
1672   char *buf;
1673   char *data;
1674   int count;
1675
1676   sched = s;
1677   config = cfg;
1678   /* Get path from configuration file */
1679   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
1680     {
1681       ok = 404;
1682       return;
1683     }
1684
1685   /**
1686    * Get DHT specific testing options.
1687    */
1688   if ((GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging")) ||
1689       (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging_extended")))
1690     {
1691       dhtlog_handle = GNUNET_DHTLOG_connect(cfg);
1692       if (dhtlog_handle == NULL)
1693         {
1694           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1695                       "Could not connect to mysql server for logging, will NOT log dht operations!");
1696           ok = 3306;
1697           return;
1698         }
1699     }
1700
1701   stop_closest = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", "stop_on_closest");
1702   if (stop_closest == GNUNET_SYSERR)
1703     stop_closest = GNUNET_NO;
1704
1705   stop_found = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", "stop_found");
1706   if (stop_found == GNUNET_SYSERR)
1707     stop_found = GNUNET_NO;
1708
1709   strict_kademlia = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", "strict_kademlia");
1710   if (strict_kademlia == GNUNET_SYSERR)
1711     strict_kademlia = GNUNET_NO;
1712
1713   if (GNUNET_OK !=
1714       GNUNET_CONFIGURATION_get_value_string (cfg, "dht_testing", "comment",
1715                                              &trialmessage))
1716     trialmessage = NULL;
1717
1718   if (GNUNET_OK !=
1719       GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
1720                                              &hostfile))
1721     hostfile = NULL;
1722
1723   hosts = NULL;
1724   temphost = NULL;
1725   if (hostfile != NULL)
1726     {
1727       if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
1728           GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
1729             | GNUNET_DISK_PERM_USER_WRITE);
1730       if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
1731         {
1732           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1733                       "Could not open file specified for host list, ending test!");
1734           ok = 1119;
1735           GNUNET_free_non_null(trialmessage);
1736           GNUNET_free(hostfile);
1737           return;
1738         }
1739
1740     data = GNUNET_malloc_large (frstat.st_size);
1741     GNUNET_assert(data != NULL);
1742     if (frstat.st_size !=
1743         GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
1744       {
1745         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1746                   "Could not read file %s specified for host list, ending test!", hostfile);
1747         GNUNET_free (hostfile);
1748         GNUNET_free (data);
1749         GNUNET_free_non_null(trialmessage);
1750         return;
1751       }
1752
1753     GNUNET_free_non_null(hostfile);
1754
1755     buf = data;
1756     count = 0;
1757     while (count < frstat.st_size)
1758       {
1759         count++;
1760         if (((data[count] == '\n') || (data[count] == '\0')) && (buf != &data[count]))
1761           {
1762             data[count] = '\0';
1763             temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
1764             temphost->hostname = buf;
1765             temphost->next = hosts;
1766             hosts = temphost;
1767             buf = &data[count + 1];
1768           }
1769         else if ((data[count] == '\n') || (data[count] == '\0'))
1770           buf = &data[count + 1];
1771       }
1772     }
1773
1774   if (GNUNET_OK !=
1775           GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_getters",
1776                                                  &malicious_getters))
1777     malicious_getters = 0;
1778
1779   if (GNUNET_OK !=
1780           GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_putters",
1781                                                  &malicious_putters))
1782     malicious_putters = 0;
1783
1784   if (GNUNET_OK !=
1785             GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_droppers",
1786                                                    &malicious_droppers))
1787     malicious_droppers = 0;
1788
1789   if (GNUNET_OK !=
1790       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "settle_time",
1791                                                  &settle_time))
1792     settle_time = 0;
1793
1794   if (GNUNET_SYSERR ==
1795       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_puts",
1796                                              &num_puts))
1797     num_puts = num_peers;
1798
1799   if (GNUNET_SYSERR ==
1800       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_gets",
1801                                              &num_gets))
1802     num_gets = num_peers;
1803
1804   if (GNUNET_OK ==
1805         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "find_peer_delay",
1806                                                &temp_config_number))
1807     find_peer_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1808   else
1809     find_peer_delay = DEFAULT_FIND_PEER_DELAY;
1810
1811   if (GNUNET_OK ==
1812         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_find_peers",
1813                                                &temp_config_number))
1814     max_outstanding_find_peers = temp_config_number;
1815   else
1816     max_outstanding_find_peers = DEFAULT_MAX_OUTSTANDING_FIND_PEERS;
1817
1818   if (GNUNET_OK ==
1819         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "get_timeout",
1820                                                &temp_config_number))
1821     get_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1822   else
1823     get_timeout = DEFAULT_GET_TIMEOUT;
1824
1825   if (GNUNET_OK ==
1826         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_puts",
1827                                                &temp_config_number))
1828     max_outstanding_puts = temp_config_number;
1829   else
1830     max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
1831
1832   if (GNUNET_OK ==
1833         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_gets",
1834                                                &temp_config_number))
1835     max_outstanding_gets = temp_config_number;
1836   else
1837     max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
1838
1839   if (GNUNET_OK ==
1840         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "timeout",
1841                                                &temp_config_number))
1842     all_get_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1843   else
1844     all_get_timeout.value = get_timeout.value * ((num_gets / max_outstanding_gets) + 1);
1845
1846   if (GNUNET_OK ==
1847         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "get_delay",
1848                                                &temp_config_number))
1849     get_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1850   else
1851     get_delay = DEFAULT_GET_DELAY;
1852
1853   if (GNUNET_OK ==
1854         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "put_delay",
1855                                                &temp_config_number))
1856     put_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1857   else
1858     put_delay = DEFAULT_PUT_DELAY;
1859
1860   if (GNUNET_OK ==
1861       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "peer_start_timeout",
1862                                              &temp_config_number))
1863     seconds_per_peer_start = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1864   else
1865     seconds_per_peer_start = DEFAULT_SECONDS_PER_PEER_START;
1866
1867   if (GNUNET_OK ==
1868         GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "data_size",
1869                                                &temp_config_number))
1870     test_data_size = temp_config_number;
1871   else
1872     test_data_size = DEFAULT_TEST_DATA_SIZE;
1873
1874   /**
1875    * Get testing related options.
1876    */
1877
1878   if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
1879                                                           "MALICIOUS_GET_FREQUENCY",
1880                                                           &malicious_get_frequency))
1881     malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
1882
1883
1884   if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
1885                                                           "MALICIOUS_PUT_FREQUENCY",
1886                                                           &malicious_put_frequency))
1887     malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
1888
1889   if (GNUNET_NO ==
1890         GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
1891                                              "find_peers"))
1892     {
1893       do_find_peer = GNUNET_NO;
1894     }
1895   else
1896     do_find_peer = GNUNET_YES;
1897
1898   topology_str = NULL;
1899   if ((GNUNET_YES ==
1900       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "topology",
1901                                             &topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&topology, topology_str)))
1902     {
1903       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1904                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "TOPOLOGY");
1905       topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
1906     }
1907
1908   if (GNUNET_OK !=
1909       GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "percentage",
1910                                                  &topology_percentage_str))
1911     topology_percentage = 0.5;
1912   else
1913     {
1914       topology_percentage = atof (topology_percentage_str);
1915       GNUNET_free(topology_percentage_str);
1916     }
1917
1918   if (GNUNET_OK !=
1919       GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "probability",
1920                                                  &topology_probability_str))
1921     topology_probability = 0.5;
1922   else
1923     {
1924      topology_probability = atof (topology_probability_str);
1925      GNUNET_free(topology_probability_str);
1926     }
1927
1928   if ((GNUNET_YES ==
1929       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology",
1930                                             &connect_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&connect_topology, connect_topology_str)))
1931     {
1932       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1933                   "Invalid connect topology `%s' given for section %s option %s\n", connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
1934     }
1935   GNUNET_free_non_null(connect_topology_str);
1936
1937   if ((GNUNET_YES ==
1938       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology_option",
1939                                             &connect_topology_option_str)) && (GNUNET_NO == GNUNET_TESTING_topology_option_get(&connect_topology_option, connect_topology_option_str)))
1940     {
1941       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1942                   "Invalid connect topology option `%s' given for section %s option %s\n", connect_topology_option_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
1943       connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
1944     }
1945   GNUNET_free_non_null(connect_topology_option_str);
1946
1947   if (GNUNET_YES ==
1948         GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "connect_topology_option_modifier",
1949                                                &connect_topology_option_modifier_string))
1950     {
1951       if (sscanf(connect_topology_option_modifier_string, "%lf", &connect_topology_option_modifier) != 1)
1952       {
1953         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1954         _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1955         connect_topology_option_modifier_string,
1956         "connect_topology_option_modifier",
1957         "TESTING");
1958       }
1959       GNUNET_free (connect_topology_option_modifier_string);
1960     }
1961
1962   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "blacklist_transports",
1963                                          &blacklist_transports))
1964     blacklist_transports = NULL;
1965
1966   if ((GNUNET_YES ==
1967       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "blacklist_topology",
1968                                             &blacklist_topology_str)) &&
1969       (GNUNET_NO == GNUNET_TESTING_topology_get(&blacklist_topology, blacklist_topology_str)))
1970     {
1971       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1972                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
1973     }
1974   GNUNET_free_non_null(topology_str);
1975   GNUNET_free_non_null(blacklist_topology_str);
1976
1977   /* Get number of peers to start from configuration */
1978   if (GNUNET_SYSERR ==
1979       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
1980                                              &num_peers))
1981     {
1982       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1983                   "Number of peers must be specified in section %s option %s\n", topology_str, "TESTING", "NUM_PEERS");
1984     }
1985   GNUNET_assert(num_peers > 0 && num_peers < (unsigned long long)-1);
1986   /* Set peers_left so we know when all peers started */
1987   peers_left = num_peers;
1988
1989   /* Set up a task to end testing if peer start fails */
1990   die_task = GNUNET_SCHEDULER_add_delayed (sched,
1991                                            GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
1992                                            &end_badly, "didn't generate all hostkeys within allowed startup time!");
1993
1994   if (dhtlog_handle == NULL)
1995     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1996                 "dhtlog_handle is NULL!");
1997
1998   if ((trialmessage != NULL) && (dhtlog_handle != NULL))
1999     {
2000       dhtlog_handle->insert_trial (&trialuid, peers_left, topology,
2001                                     blacklist_topology, connect_topology,
2002                                     connect_topology_option,
2003                                     connect_topology_option_modifier, topology_percentage,
2004                                     topology_probability, num_puts, num_gets,
2005                                     max_outstanding_gets, settle_time, 1,
2006                                     malicious_getters, malicious_putters,
2007                                     malicious_droppers, malicious_get_frequency,
2008                                     malicious_put_frequency, stop_closest, stop_found,
2009                                     strict_kademlia, 0, trialmessage);
2010     }
2011   else if (dhtlog_handle != NULL)
2012     {
2013       dhtlog_handle->insert_trial (&trialuid, peers_left, topology,
2014                                     blacklist_topology, connect_topology,
2015                                     connect_topology_option,
2016                                     connect_topology_option_modifier, topology_percentage,
2017                                     topology_probability, num_puts, num_gets,
2018                                     max_outstanding_gets, settle_time, 1,
2019                                     malicious_getters, malicious_putters,
2020                                     malicious_droppers, malicious_get_frequency,
2021                                     malicious_put_frequency, stop_closest, stop_found,
2022                                     strict_kademlia, 0, "");
2023     }
2024
2025   GNUNET_free_non_null(trialmessage);
2026
2027   hostkey_meter = create_meter(peers_left, "Hostkeys created ", GNUNET_YES);
2028   peer_start_meter = create_meter(peers_left, "Peers started ", GNUNET_YES);
2029
2030   put_meter = create_meter(num_puts, "Puts completed ", GNUNET_YES);
2031   get_meter = create_meter(num_gets, "Gets completed ", GNUNET_YES);
2032   pg = GNUNET_TESTING_daemons_start (sched, cfg,
2033                                      peers_left,
2034                                      GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
2035                                      &hostkey_callback, NULL,
2036                                      &peers_started_callback, NULL,
2037                                      &topology_callback, NULL,
2038                                      hosts);
2039
2040   GNUNET_free_non_null(temphost);
2041 }
2042
2043
2044 int
2045 main (int argc, char *argv[])
2046 {
2047   int ret;
2048   struct GNUNET_GETOPT_CommandLineOption options[] = {
2049       GNUNET_GETOPT_OPTION_END
2050     };
2051
2052   ret = GNUNET_PROGRAM_run (argc,
2053                             argv, "gnunet-dht-driver", "nohelp",
2054                             options, &run, &ok);
2055
2056   if (ret != GNUNET_OK)
2057     {
2058       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`gnunet-dht-driver': Failed with error code %d\n", ret);
2059     }
2060
2061   /**
2062    * Need to remove base directory, subdirectories taken care
2063    * of by the testing framework.
2064    */
2065   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
2066     {
2067       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
2068     }
2069   return ret;
2070 }
2071
2072 /* end of test_dht_twopeer_put_get.c */