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