779aff88d6969b239f0dac3a89112886de2cb83e
[oweals/gnunet.git] / src / dht / test_dht_multipeer.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/test_dht_multipeer.c
22  * @brief testcase for testing DHT service with
23  *        multiple peers.
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_dht_service.h"
29
30 /* DEFINES */
31 #define VERBOSE GNUNET_NO
32
33 /* Timeout for entire testcase */
34 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
35
36 /* Timeout for waiting for replies to get requests */
37 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
38
39 /* Timeout for waiting for gets to complete */
40 #define GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
41
42 /* Timeout for waiting for puts to complete */
43 #define PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
44
45 #define SECONDS_PER_PEER_START 45
46
47 /* If number of peers not in config file, use this number */
48 #define DEFAULT_NUM_PEERS 5
49
50 #define TEST_DATA_SIZE 8
51
52 #define MAX_OUTSTANDING_PUTS 10
53
54 #define MAX_OUTSTANDING_GETS 10
55
56 #define PATH_TRACKING GNUNET_YES
57
58 /* Structs */
59
60 struct TestPutContext
61 {
62   /**
63    * This is a linked list 
64    */
65   struct TestPutContext *next;
66
67   /**
68    * Handle to the first peers DHT service (via the API)
69    */
70   struct GNUNET_DHT_Handle *dht_handle;
71
72   /**
73    *  Handle to the PUT peer daemon
74    */
75   struct GNUNET_TESTING_Daemon *daemon;
76
77   /**
78    *  Identifier for this PUT
79    */
80   uint32_t uid;
81
82   /**
83    * Task for disconnecting DHT handles
84    */
85   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
86 };
87
88 struct TestGetContext
89 {
90   /* This is a linked list */
91   struct TestGetContext *next;
92
93   /**
94    * Handle to the first peers DHT service (via the API)
95    */
96   struct GNUNET_DHT_Handle *dht_handle;
97
98   /**
99    * Handle for the DHT get request
100    */
101   struct GNUNET_DHT_GetHandle *get_handle;
102
103   /**
104    *  Handle to the GET peer daemon
105    */
106   struct GNUNET_TESTING_Daemon *daemon;
107
108   /**
109    *  Identifier for this GET
110    */
111   uint32_t uid;
112
113   /**
114    * Task for disconnecting DHT handles (and stopping GET)
115    */
116   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
117
118   /**
119    * Whether or not this request has been fulfilled already.
120    */
121   int succeeded;
122 };
123
124 /* Globals */
125
126 /**
127  * List of GETS to perform
128  */
129 struct TestGetContext *all_gets;
130
131 /**
132  * List of PUTS to perform
133  */
134 struct TestPutContext *all_puts;
135
136 /**
137  * Directory to store temp data in, defined in config file
138  */
139 static char *test_directory;
140
141 /**
142  * Variable used to store the number of connections we should wait for.
143  */
144 static unsigned int expected_connections;
145
146 /**
147  * Variable used to keep track of how many peers aren't yet started.
148  */
149 static unsigned long long peers_left;
150
151 /**
152  * Handle to the set of all peers run for this test.
153  */
154 static struct GNUNET_TESTING_PeerGroup *pg;
155
156
157 /**
158  * Total number of peers to run, set based on config file.
159  */
160 static unsigned long long num_peers;
161
162 /**
163  * Total number of items to insert.
164  */
165 static unsigned long long num_puts;
166
167 /**
168  * Total number of items to attempt to get.
169  */
170 static unsigned long long num_gets;
171
172 /**
173  * How many puts do we currently have in flight?
174  */
175 static unsigned long long outstanding_puts;
176
177 /**
178  * How many puts are done?
179  */
180 static unsigned long long puts_completed;
181
182 /**
183  * How many puts do we currently have in flight?
184  */
185 static unsigned long long outstanding_gets;
186
187 /**
188  * How many gets are done?
189  */
190 static unsigned long long gets_completed;
191
192 /**
193  * How many gets failed?
194  */
195 static unsigned long long gets_failed;
196
197 /**
198  * Global used to count how many connections we have currently
199  * been notified about (how many times has topology_callback been called
200  * with success?)
201  */
202 static unsigned int total_connections;
203
204 /**
205  * Global used to count how many failed connections we have
206  * been notified about (how many times has topology_callback
207  * been called with failure?)
208  */
209 static unsigned int failed_connections;
210
211 enum GNUNET_DHT_RouteOption route_option;
212
213 /* Task handle to use to schedule test failure */
214 GNUNET_SCHEDULER_TaskIdentifier die_task;
215
216 static char *blacklist_transports;
217
218 static enum GNUNET_TESTING_Topology topology;
219
220 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE;  /* Don't do any blacklisting */
221
222 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
223
224 static enum GNUNET_TESTING_TopologyOption connect_topology_option =
225     GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
226
227 static double connect_topology_option_modifier = 0.0;
228
229 /* Global return value (0 for success, anything else for failure) */
230 static int ok;
231
232 /**
233  * Check whether peers successfully shut down.
234  */
235 void
236 shutdown_callback (void *cls, const char *emsg)
237 {
238   if (emsg != NULL)
239   {
240     if (ok == 0)
241       ok = 2;
242   }
243 }
244
245 /**
246  * Task to release DHT handles for PUT
247  */
248 static void
249 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 {
251   struct TestPutContext *test_put = cls;
252
253   test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
254   GNUNET_DHT_disconnect (test_put->dht_handle);
255   test_put->dht_handle = NULL;
256 }
257
258 /**
259  * Function scheduled to be run on the successful completion of this
260  * testcase.
261  */
262 static void
263 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
264 {
265   GNUNET_assert (pg != NULL);
266   struct TestPutContext *test_put = all_puts;
267   struct TestGetContext *test_get = all_gets;
268
269   while (test_put != NULL)
270   {
271     if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
272       GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
273     if (test_put->dht_handle != NULL)
274       GNUNET_DHT_disconnect (test_put->dht_handle);
275     test_put = test_put->next;
276   }
277
278   while (test_get != NULL)
279   {
280     if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
281       GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
282     if (test_get->get_handle != NULL)
283       GNUNET_DHT_get_stop (test_get->get_handle);
284     if (test_get->dht_handle != NULL)
285       GNUNET_DHT_disconnect (test_get->dht_handle);
286     test_get = test_get->next;
287   }
288
289   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
290   ok = 0;
291 }
292
293
294 /**
295  * Check if the get_handle is being used, if so stop the request.  Either
296  * way, schedule the end_badly_cont function which actually shuts down the
297  * test.
298  */
299 static void
300 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
301 {
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n",
303               (char *) cls);
304
305   struct TestPutContext *test_put = all_puts;
306   struct TestGetContext *test_get = all_gets;
307
308   while (test_put != NULL)
309   {
310     if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
311       GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
312     if (test_put->dht_handle != NULL)
313       GNUNET_DHT_disconnect (test_put->dht_handle);
314     test_put = test_put->next;
315   }
316
317   while (test_get != NULL)
318   {
319     if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
320       GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
321     if (test_get->get_handle != NULL)
322       GNUNET_DHT_get_stop (test_get->get_handle);
323     if (test_get->dht_handle != NULL)
324       GNUNET_DHT_disconnect (test_get->dht_handle);
325     test_get = test_get->next;
326   }
327
328   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
329   ok = 1;
330 }
331
332 /**
333  * Task to release DHT handle associated with GET request.
334  */
335 static void
336 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 {
338   struct TestGetContext *test_get = cls;
339
340   outstanding_gets--;           /* GET is really finished */
341   GNUNET_DHT_disconnect (test_get->dht_handle);
342   test_get->dht_handle = NULL;
343
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n",
345               gets_completed, gets_failed);
346   if ((gets_completed == num_gets) && (outstanding_gets == 0))  /* All gets successful */
347   {
348     GNUNET_SCHEDULER_cancel (die_task);
349     //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &get_topology, NULL);
350     GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
351   }
352   else if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0))       /* Had some failures */
353   {
354     GNUNET_SCHEDULER_cancel (die_task);
355     GNUNET_SCHEDULER_add_now (&end_badly, "not all gets succeeded!\n");
356   }
357 }
358
359 /**
360  * Task to release get handle.
361  */
362 static void
363 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
364 {
365   struct TestGetContext *test_get = cls;
366   GNUNET_HashCode search_key;   /* Key stored under */
367   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
368
369   memset (original_data, test_get->uid, sizeof (original_data));
370   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
371
372   if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
373   {
374     gets_failed++;
375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376                 "Get from peer %s for key %s failed!\n",
377                 test_get->daemon->shortname, GNUNET_h2s (&search_key));
378   }
379   GNUNET_assert (test_get->get_handle != NULL);
380   GNUNET_DHT_get_stop (test_get->get_handle);
381   GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
382   test_get->get_handle = NULL;
383   test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
384 }
385
386 /**
387  * Iterator called if the GET request initiated returns a response.
388  *
389  * @param cls closure
390  * @param exp when will this value expire
391  * @param key key of the result
392  * @param type type of the result
393  * @param size number of bytes in data
394  * @param data pointer to the result data
395  */
396 void
397 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
398                      const GNUNET_HashCode * key,
399                      const struct GNUNET_PeerIdentity *const *get_path,
400                      const struct GNUNET_PeerIdentity *const *put_path,
401                      enum GNUNET_BLOCK_Type type, size_t size, const void *data)
402 {
403   struct TestGetContext *test_get = cls;
404   GNUNET_HashCode search_key;   /* Key stored under */
405   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
406   unsigned int i;
407
408   memset (original_data, test_get->uid, sizeof (original_data));
409   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
410
411   if (test_get->succeeded == GNUNET_YES)
412     return;                     /* Get has already been successful, probably ending now */
413
414 #if PATH_TRACKING
415   if (put_path != NULL)
416   {
417     fprintf (stderr, "PUT Path: ");
418     for (i = 0; put_path[i] != NULL; i++)
419       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (put_path[i]));
420     fprintf (stderr, "\n");
421   }
422   if (get_path != NULL)
423   {
424     fprintf (stderr, "GET Path: ");
425     for (i = 0; get_path[i] != NULL; i++)
426       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (get_path[i]));
427     fprintf (stderr, "\n");
428   }
429 #endif
430
431   if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
432       (0 != memcmp (original_data, data, sizeof (original_data))))
433   {
434     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435                 "Key or data is not the same as was inserted!\n");
436   }
437   else
438   {
439     gets_completed++;
440     test_get->succeeded = GNUNET_YES;
441   }
442
443   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
444   GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
445   GNUNET_SCHEDULER_add_continuation (&get_stop_task, test_get,
446                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
447 }
448
449
450 /**
451  * Set up some data, and call API PUT function
452  */
453 static void
454 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
455 {
456   struct TestGetContext *test_get = cls;
457   GNUNET_HashCode key;          /* Made up key to store data under */
458   char data[TEST_DATA_SIZE];    /* Made up data to store */
459
460   if (test_get == NULL)
461     return;                     /* End of the list */
462   memset (data, test_get->uid, sizeof (data));
463   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
464
465   if (outstanding_gets > MAX_OUTSTANDING_GETS)
466   {
467     GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
468     return;
469   }
470
471   test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
472   /* Insert the data at the first peer */
473   GNUNET_assert (test_get->dht_handle != NULL);
474   outstanding_gets++;
475   test_get->get_handle =
476       GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
477                             GNUNET_BLOCK_TYPE_TEST, &key,
478                             DEFAULT_GET_REPLICATION, route_option, NULL, 0,
479                             NULL, 0, &get_result_iterator, test_get);
480 #if VERBOSE
481   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
482               test_get->uid, test_get->daemon->shortname);
483 #endif
484   test_get->disconnect_task =
485       GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
486   GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
487 }
488
489 /**
490  * Called when the PUT request has been transmitted to the DHT service.
491  * Schedule the GET request for some time in the future.
492  */
493 static void
494 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
495 {
496   struct TestPutContext *test_put = cls;
497
498   outstanding_puts--;
499   puts_completed++;
500
501   GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
502   test_put->disconnect_task =
503       GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
504   if (puts_completed == num_puts)
505   {
506     GNUNET_assert (outstanding_puts == 0);
507     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
508                                   (GNUNET_TIME_UNIT_SECONDS, 10), &do_get,
509                                   all_gets);
510     return;
511   }
512 }
513
514 /**
515  * Set up some data, and call API PUT function
516  */
517 static void
518 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
519 {
520   struct TestPutContext *test_put = cls;
521   GNUNET_HashCode key;          /* Made up key to store data under */
522   char data[TEST_DATA_SIZE];    /* Made up data to store */
523
524   if (test_put == NULL)
525     return;                     /* End of list */
526
527   memset (data, test_put->uid, sizeof (data));
528   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
529
530   if (outstanding_puts > MAX_OUTSTANDING_PUTS)
531   {
532     GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
533     return;
534   }
535
536 #if VERBOSE
537   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
538               test_put->uid, test_put->daemon->shortname);
539 #endif
540   test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
541
542   GNUNET_assert (test_put->dht_handle != NULL);
543   outstanding_puts++;
544   GNUNET_DHT_put (test_put->dht_handle, &key, DEFAULT_PUT_REPLICATION,
545                   route_option, GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
546                   GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
547                   &put_finished, test_put);
548   test_put->disconnect_task =
549       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
550                                     &put_disconnect_task, test_put);
551   GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
552 }
553
554
555 /**
556  * Set up some all of the put and get operations we want
557  * to do.  Allocate data structure for each, add to list,
558  * then call actual insert functions.
559  */
560 static void
561 setup_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
562 {
563   int i;
564   uint32_t temp_daemon;
565   struct TestPutContext *test_put;
566   struct TestGetContext *test_get;
567   int remember[num_puts][num_peers];
568
569   for (i = 0; i < num_puts; i++)
570   {
571     test_put = GNUNET_malloc (sizeof (struct TestPutContext));
572     test_put->uid = i;
573     temp_daemon =
574         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
575     test_put->daemon = GNUNET_TESTING_daemon_get (pg, temp_daemon);
576     test_put->next = all_puts;
577     all_puts = test_put;
578   }
579
580   for (i = 0; i < num_gets; i++)
581   {
582     test_get = GNUNET_malloc (sizeof (struct TestGetContext));
583     test_get->uid =
584         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
585     temp_daemon =
586         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
587     while (remember[test_get->uid][temp_daemon] == 1)
588       temp_daemon =
589           GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
590     test_get->daemon = GNUNET_TESTING_daemon_get (pg, temp_daemon);
591     remember[test_get->uid][temp_daemon] = 1;
592     test_get->next = all_gets;
593     all_gets = test_get;
594   }
595
596   GNUNET_SCHEDULER_add_now (&do_put, all_puts);
597 }
598
599
600 /**
601  * This function is called whenever a connection attempt is finished between two of
602  * the started peers (started with GNUNET_TESTING_daemons_start).  The total
603  * number of times this function is called should equal the number returned
604  * from the GNUNET_TESTING_connect_topology call.
605  *
606  * The emsg variable is NULL on success (peers connected), and non-NULL on
607  * failure (peers failed to connect).
608  */
609 void
610 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
611                    const struct GNUNET_PeerIdentity *second, uint32_t distance,
612                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
613                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
614                    struct GNUNET_TESTING_Daemon *first_daemon,
615                    struct GNUNET_TESTING_Daemon *second_daemon,
616                    const char *emsg)
617 {
618   if (emsg == NULL)
619   {
620     total_connections++;
621 #if VERBOSE > 1
622     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623                 "connected peer %s to peer %s, distance %u\n",
624                 first_daemon->shortname, second_daemon->shortname, distance);
625 #endif
626   }
627 #if VERBOSE
628   else
629   {
630     failed_connections++;
631     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632                 "Failed to connect peer %s to peer %s with error :\n%s\n",
633                 first_daemon->shortname, second_daemon->shortname, emsg);
634   }
635 #endif
636
637   if (total_connections == expected_connections)
638   {
639 #if VERBOSE
640     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641                 "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
642                 total_connections);
643 #endif
644     GNUNET_SCHEDULER_cancel (die_task);
645     die_task =
646         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
647                                       "from setup puts/gets");
648
649     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
650                                   (GNUNET_TIME_UNIT_SECONDS, 2),
651                                   &setup_puts_and_gets, NULL);
652   }
653   else if (total_connections + failed_connections == expected_connections)
654   {
655     GNUNET_SCHEDULER_cancel (die_task);
656     die_task =
657         GNUNET_SCHEDULER_add_now (&end_badly,
658                                   "from topology_callback (too many failed connections)");
659   }
660 }
661
662 static void
663 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
664                         const struct GNUNET_CONFIGURATION_Handle *cfg,
665                         struct GNUNET_TESTING_Daemon *d, const char *emsg)
666 {
667   if (emsg != NULL)
668   {
669     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670                 "Failed to start daemon with error: `%s'\n", emsg);
671     return;
672   }
673   GNUNET_assert (id != NULL);
674
675 #if VERBOSE
676   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
677               (num_peers - peers_left) + 1, num_peers);
678 #endif
679
680   peers_left--;
681   if (peers_left == 0)
682   {
683
684 #if VERBOSE
685     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686                 "All %d daemons started, now connecting peers!\n", num_peers);
687 #endif
688     GNUNET_SCHEDULER_cancel (die_task);
689
690     expected_connections = -1;
691     if ((pg != NULL) && (peers_left == 0))
692     {
693       expected_connections =
694           GNUNET_TESTING_connect_topology (pg, connection_topology,
695                                            connect_topology_option,
696                                            connect_topology_option_modifier,
697                                            TIMEOUT, num_peers, NULL, NULL);
698 #if VERBOSE
699       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
700                   expected_connections);
701 #endif
702     }
703
704     if (expected_connections == GNUNET_SYSERR)
705     {
706       die_task =
707           GNUNET_SCHEDULER_add_now (&end_badly,
708                                     "from connect topology (bad return)");
709     }
710
711     die_task =
712         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
713                                       "from connect topology (timeout)");
714
715     ok = 0;
716   }
717 }
718
719 static void
720 create_topology ()
721 {
722   peers_left = num_peers;       /* Reset counter */
723   if (GNUNET_TESTING_create_topology
724       (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
725   {
726 #if VERBOSE
727     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728                 "Topology set up, now starting peers!\n");
729 #endif
730     GNUNET_TESTING_daemons_continue_startup (pg);
731   }
732   else
733   {
734     GNUNET_SCHEDULER_cancel (die_task);
735     die_task =
736         GNUNET_SCHEDULER_add_now (&end_badly,
737                                   "from create topology (bad return)");
738   }
739   GNUNET_SCHEDULER_cancel (die_task);
740   die_task =
741       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
742                                     "from continue startup (timeout)");
743 }
744
745 /**
746  * Callback indicating that the hostkey was created for a peer.
747  *
748  * @param cls NULL
749  * @param id the peer identity
750  * @param d the daemon handle (pretty useless at this point, remove?)
751  * @param emsg non-null on failure
752  */
753 void
754 hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
755                   struct GNUNET_TESTING_Daemon *d, const char *emsg)
756 {
757   if (emsg != NULL)
758   {
759     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
760                 "Hostkey callback received error: %s\n", emsg);
761   }
762
763 #if VERBOSE > 1
764   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765               "Hostkey (%d/%d) created for peer `%s'\n", num_peers - peers_left,
766               num_peers, GNUNET_i2s (id));
767 #endif
768
769
770   peers_left--;
771   if (peers_left == 0)
772   {
773 #if VERBOSE
774     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775                 "All %d hostkeys created, now creating topology!\n", num_peers);
776 #endif
777     GNUNET_SCHEDULER_cancel (die_task);
778     /* Set up task in case topology creation doesn't finish
779      * within a reasonable amount of time */
780     die_task =
781         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
782                                       "from create_topology");
783     GNUNET_SCHEDULER_add_now (&create_topology, NULL);
784     ok = 0;
785   }
786 }
787
788
789 static void
790 run (void *cls, char *const *args, const char *cfgfile,
791      const struct GNUNET_CONFIGURATION_Handle *cfg)
792 {
793   char *topology_str;
794   char *connect_topology_str;
795   char *blacklist_topology_str;
796   char *connect_topology_option_str;
797   char *connect_topology_option_modifier_string;
798
799 #if PATH_TRACKING
800   route_option = GNUNET_DHT_RO_RECORD_ROUTE;
801 #else
802   route_option = GNUNET_DHT_RO_NONE;
803 #endif
804
805   /* Get path from configuration file */
806   if (GNUNET_YES !=
807       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
808                                              &test_directory))
809   {
810     ok = 404;
811     return;
812   }
813
814   if ((GNUNET_YES ==
815        GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
816                                               &topology_str)) &&
817       (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
818   {
819     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
820                 "Invalid topology `%s' given for section %s option %s\n",
821                 topology_str, "TESTING", "TOPOLOGY");
822     topology = GNUNET_TESTING_TOPOLOGY_CLIQUE;  /* Defaults to NONE, so set better default here */
823   }
824
825   if ((GNUNET_YES ==
826        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
827                                               "connect_topology",
828                                               &connect_topology_str)) &&
829       (GNUNET_NO ==
830        GNUNET_TESTING_topology_get (&connection_topology,
831                                     connect_topology_str)))
832   {
833     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
834                 "Invalid connect topology `%s' given for section %s option %s\n",
835                 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
836   }
837   GNUNET_free_non_null (connect_topology_str);
838   if ((GNUNET_YES ==
839        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
840                                               "connect_topology_option",
841                                               &connect_topology_option_str)) &&
842       (GNUNET_NO ==
843        GNUNET_TESTING_topology_option_get (&connect_topology_option,
844                                            connect_topology_option_str)))
845   {
846     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
847                 "Invalid connect topology option `%s' given for section %s option %s\n",
848                 connect_topology_option_str, "TESTING",
849                 "CONNECT_TOPOLOGY_OPTION");
850     connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;       /* Defaults to NONE, set to ALL */
851   }
852   GNUNET_free_non_null (connect_topology_option_str);
853   if (GNUNET_YES ==
854       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
855                                              "connect_topology_option_modifier",
856                                              &connect_topology_option_modifier_string))
857   {
858     if (sscanf
859         (connect_topology_option_modifier_string, "%lf",
860          &connect_topology_option_modifier) != 1)
861     {
862       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
863                   _
864                   ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
865                   connect_topology_option_modifier_string,
866                   "connect_topology_option_modifier", "TESTING");
867     }
868     GNUNET_free (connect_topology_option_modifier_string);
869   }
870
871   if (GNUNET_YES !=
872       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
873                                              "blacklist_transports",
874                                              &blacklist_transports))
875     blacklist_transports = NULL;
876
877   if ((GNUNET_YES ==
878        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
879                                               "blacklist_topology",
880                                               &blacklist_topology_str)) &&
881       (GNUNET_NO ==
882        GNUNET_TESTING_topology_get (&blacklist_topology,
883                                     blacklist_topology_str)))
884   {
885     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
886                 "Invalid topology `%s' given for section %s option %s\n",
887                 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
888   }
889   GNUNET_free_non_null (topology_str);
890   GNUNET_free_non_null (blacklist_topology_str);
891
892   /* Get number of peers to start from configuration */
893   if (GNUNET_SYSERR ==
894       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
895                                              &num_peers))
896     num_peers = DEFAULT_NUM_PEERS;
897
898   if (GNUNET_SYSERR ==
899       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_puts",
900                                              &num_puts))
901     num_puts = DEFAULT_NUM_PEERS;
902
903   if (GNUNET_SYSERR ==
904       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_gets",
905                                              &num_gets))
906     num_gets = DEFAULT_NUM_PEERS;
907
908   /* Set peers_left so we know when all peers started */
909   peers_left = num_peers;
910
911   /* Set up a task to end testing if peer start fails */
912   die_task =
913       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
914                                     (GNUNET_TIME_UNIT_SECONDS,
915                                      SECONDS_PER_PEER_START * num_peers),
916                                     &end_badly,
917                                     "didn't generate all hostkeys within a reasonable amount of time!!!");
918
919   pg = GNUNET_TESTING_daemons_start (cfg, peers_left,   /* Total number of peers */
920                                      peers_left,        /* Number of outstanding connections */
921                                      peers_left,        /* Number of parallel ssh connections, or peers being started at once */
922                                      GNUNET_TIME_relative_multiply
923                                      (GNUNET_TIME_UNIT_SECONDS,
924                                       SECONDS_PER_PEER_START * num_peers),
925                                      &hostkey_callback, NULL,
926                                      &peers_started_callback, NULL,
927                                      &topology_callback, NULL, NULL);
928
929 }
930
931 static int
932 check ()
933 {
934   int ret;
935
936   /* Arguments for GNUNET_PROGRAM_run */
937   char *const argv[] = { "test-dht-multipeer",  /* Name to give running binary */
938     "-c",
939     "test_dht_multipeer_data.conf",     /* Config file to use */
940 #if VERBOSE
941     "-L", "DEBUG",
942 #endif
943     NULL
944   };
945   struct GNUNET_GETOPT_CommandLineOption options[] = {
946     GNUNET_GETOPT_OPTION_END
947   };
948   /* Run the run function as a new program */
949   ret =
950       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
951                           "test-dht-multipeer", "nohelp", options, &run, &ok);
952   if (ret != GNUNET_OK)
953   {
954     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
955                 "`test-dht-multipeer': Failed with error code %d\n", ret);
956   }
957   return ok;
958 }
959
960 int
961 main (int argc, char *argv[])
962 {
963   int ret;
964
965   GNUNET_log_setup ("test-dht-multipeer",
966 #if VERBOSE
967                     "DEBUG",
968 #else
969                     "WARNING",
970 #endif
971                     NULL);
972   ret = check ();
973   /**
974    * Need to remove base directory, subdirectories taken care
975    * of by the testing framework.
976    */
977   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
978   {
979     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
980                 "Failed to remove testing directory %s\n", test_directory);
981   }
982   return ret;
983 }
984
985 /* end of test_dht_twopeer_put_get.c */