change testing library to use timeout for peer and peergroup startup so testcase...
[oweals/gnunet.git] / src / testing / testing_group.c
1 /*
2       This file is part of GNUnet
3       (C) 2008, 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 2, 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 /**
22  * @file testing/testing_group.c
23  * @brief convenience API for writing testcases for GNUnet
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_testing_lib.h"
29
30 #define VERBOSE_TESTING GNUNET_YES
31
32 /**
33  * Lowest port used for GNUnet testing.  Should be high enough to not
34  * conflict with other applications running on the hosts but be low
35  * enough to not conflict with client-ports (typically starting around
36  * 32k).
37  */
38 #define LOW_PORT 10000
39
40 /**
41  * Highest port used for GNUnet testing.  Should be low enough to not
42  * conflict with the port range for "local" ports (client apps; see
43  * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
44  */
45 #define HIGH_PORT 32000
46
47 #define MAX_OUTSTANDING_CONNECTIONS 50
48
49 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 160)
50
51 #define CONNECT_ATTEMPTS 8
52
53 /**
54  * Prototype of a function called whenever two peers would be connected
55  * in a certain topology.
56  */
57 typedef int (*GNUNET_TESTING_ConnectionProcessor)
58 (struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second);
59
60 struct RestartContext
61 {
62   /**
63    * The group of peers being restarted
64    */
65   struct GNUNET_TESTING_PeerGroup *peer_group;
66
67   /**
68    * How many peers have been restarted thus far
69    */
70   unsigned int peers_restarted;
71
72   /**
73    * How many peers got an error when restarting
74    */
75   unsigned int peers_restart_failed;
76
77   /**
78    * The function to call once all peers have been restarted
79    */
80   GNUNET_TESTING_NotifyCompletion callback;
81
82   /**
83    * Closure for callback function
84    */
85   void *callback_cls;
86
87 };
88
89 struct CreateTopologyContext
90 {
91
92   /**
93    * Function to call with number of connections
94    */
95   GNUNET_TESTING_NotifyConnections cont;
96
97   /**
98    * Closure for connection notification
99    */
100   void *cls;
101 };
102
103 #if OLD
104 struct PeerConnection
105 {
106   /*
107    * Linked list
108    */
109   struct PeerConnection *next;
110
111   /*
112    * Pointer to daemon handle
113    */
114   struct GNUNET_TESTING_Daemon *daemon;
115
116 };
117 #endif
118
119 /**
120  * Data we keep per peer.
121  */
122 struct PeerData
123 {
124   /**
125    * (Initial) configuration of the host.
126    * (initial because clients could change
127    *  it and we would not know about those
128    *  updates).
129    */
130   struct GNUNET_CONFIGURATION_Handle *cfg;
131
132   /**
133    * Handle for controlling the daemon.
134    */
135   struct GNUNET_TESTING_Daemon *daemon;
136
137   /**
138    * The peergroup this peer belongs to.
139    */
140   struct GNUNET_TESTING_PeerGroup *pg;
141
142   /**
143    * Linked list of peer connections (pointers)
144    */
145   //struct PeerConnection *connected_peers;
146   /**
147    * Hash map of allowed peer connections (F2F created topology)
148    */
149   struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
150
151   /**
152    * Hash map of blacklisted peers
153    */
154   struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
155
156   /**
157    * Hash map of peer connections
158    */
159   struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
160
161   /**
162    * Temporary hash map of peer connections
163    */
164   struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
165
166   /**
167    * Temporary variable for topology creation, should be reset before
168    * creating any topology so the count is valid once finished.
169    */
170   int num_connections;
171 };
172
173
174 /**
175  * Data we keep per host.
176  */
177 struct HostData
178 {
179   /**
180    * Name of the host.
181    */
182   char *hostname;
183
184   /**
185    * Lowest port that we have not yet used
186    * for GNUnet.
187    */
188   uint16_t minport;
189 };
190
191
192 /**
193  * Handle to a group of GNUnet peers.
194  */
195 struct GNUNET_TESTING_PeerGroup
196 {
197   /**
198    * Our scheduler.
199    */
200   struct GNUNET_SCHEDULER_Handle *sched;
201
202   /**
203    * Configuration template.
204    */
205   const struct GNUNET_CONFIGURATION_Handle *cfg;
206
207   /**
208    * Function to call on each started daemon.
209    */
210   GNUNET_TESTING_NotifyDaemonRunning cb;
211
212   /**
213    * Closure for cb.
214    */
215   void *cb_cls;
216
217   /*
218    * Function to call on each topology connection created
219    */
220   GNUNET_TESTING_NotifyConnection notify_connection;
221
222   /*
223    * Callback for notify_connection
224    */
225   void *notify_connection_cls;
226
227   /**
228    * NULL-terminated array of information about
229    * hosts.
230    */
231   struct HostData *hosts;
232
233   /**
234    * Array of "total" peers.
235    */
236   struct PeerData *peers;
237
238   /**
239    * Number of peers in this group.
240    */
241   unsigned int total;
242
243   /**
244    * At what time should we fail the peer startup process?
245    */
246   struct GNUNET_TIME_Absolute max_timeout;
247 };
248
249 /**
250  * Convert unique ID to hash code.
251  *
252  * @param uid unique ID to convert
253  * @param hash set to uid (extended with zeros)
254  */
255 static void
256 hash_from_uid (uint32_t uid,
257                GNUNET_HashCode *hash)
258 {
259   memset (hash, 0, sizeof(GNUNET_HashCode));
260   *((uint32_t*)hash) = uid;
261 }
262
263 /**
264  * Convert hash code to unique ID.
265  *
266  * @param uid unique ID to convert
267  * @param hash set to uid (extended with zeros)
268  */
269 static void
270 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
271 {
272   memcpy (uid, hash, sizeof(uint32_t));
273 }
274
275 struct UpdateContext
276 {
277   struct GNUNET_CONFIGURATION_Handle *ret;
278   unsigned int nport;
279   const char *hostname;
280 };
281
282
283 struct ConnectContext
284 {
285   struct GNUNET_TESTING_Daemon *first;
286
287   struct GNUNET_TESTING_Daemon *second;
288
289   struct GNUNET_TESTING_PeerGroup *pg;
290 };
291
292 /**
293  * Number of connects we are waiting on, allows us to rate limit
294  * connect attempts.
295  */
296 static int outstanding_connects;
297
298
299 /**
300  * Function to iterate over options.  Copies
301  * the options to the target configuration,
302  * updating PORT values as needed.
303  *
304  * @param cls closure
305  * @param section name of the section
306  * @param option name of the option
307  * @param value value of the option
308  */
309 static void
310 update_config (void *cls,
311                const char *section, const char *option, const char *value)
312 {
313   struct UpdateContext *ctx = cls;
314   unsigned int ival;
315   char cval[12];
316
317   if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
318     {
319       GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
320       value = cval;
321     }
322
323   if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
324     {
325       value = ctx->hostname;
326     }
327
328   GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
329 }
330
331
332 /**
333  * Create a new configuration using the given configuration
334  * as a template; however, each PORT in the existing cfg
335  * must be renumbered by incrementing "*port".  If we run
336  * out of "*port" numbers, return NULL.
337  *
338  * @param cfg template configuration
339  * @param port port numbers to use, update to reflect
340  *             port numbers that were used
341  * @param hostname hostname of the controlling host, to allow control connections from
342  *
343  * @return new configuration, NULL on error
344  */
345 static struct GNUNET_CONFIGURATION_Handle *
346 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t * port, const char *hostname)
347 {
348   struct UpdateContext uc;
349   uint16_t orig;
350   char *control_host;
351   char *allowed_hosts;
352
353   orig = *port;
354   uc.nport = *port;
355   uc.ret = GNUNET_CONFIGURATION_create ();
356   uc.hostname = hostname;
357
358   GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
359   if (uc.nport >= HIGH_PORT)
360     {
361       *port = orig;
362       GNUNET_CONFIGURATION_destroy (uc.ret);
363       return NULL;
364     }
365
366   if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
367     {
368       GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
369       GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
370       GNUNET_free_non_null(control_host);
371       GNUNET_free(allowed_hosts);
372     }
373
374
375   /* arm needs to know to allow connections from the host on which it is running,
376    * otherwise gnunet-arm is unable to connect to it in some instances */
377   if (hostname != NULL)
378     {
379       GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
380       GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
381       GNUNET_free(allowed_hosts);
382     }
383
384   *port = (uint16_t) uc.nport;
385   return uc.ret;
386 }
387
388
389 /*
390  * Add entries to the peers connect list
391  *
392  * @param pg the peer group we are working with
393  * @param first index of the first peer
394  * @param second index of the second peer
395  *
396  * @return the number of connections added (can be 0, 1 or 2)
397  *         technically should only be 0 or 2, but the small price
398  *         of iterating over the lists (hashmaps in the future)
399  *         for being sure doesn't bother me!
400  *
401  */
402 static int
403 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
404 {
405   int added;
406   int add_first;
407   int add_second;
408
409   GNUNET_HashCode hash_first;
410   GNUNET_HashCode hash_second;
411
412   hash_from_uid(first, &hash_first);
413   hash_from_uid(second, &hash_second);
414
415   add_first = GNUNET_NO;
416   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
417     {
418       add_first = GNUNET_YES;
419     }
420
421   add_second = GNUNET_NO;
422   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
423     {
424       add_second = GNUNET_YES;
425     }
426
427   added = 0;
428   if (add_first)
429     {
430       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
431       pg->peers[first].num_connections++;
432       added++;
433     }
434
435   if (add_second)
436     {
437       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
438       pg->peers[second].num_connections++;
439       added++;
440     }
441
442   return added;
443 }
444
445
446 /*
447  * Add entries to the peers allowed connections list
448  *
449  * @param pg the peer group we are working with
450  * @param first index of the first peer
451  * @param second index of the second peer
452  *
453  * @return the number of connections added (can be 0, 1 or 2)
454  *         technically should only be 0 or 2, but the small price
455  *         of iterating over the lists (hashmaps in the future)
456  *         for being sure doesn't bother me!
457  *
458  */
459 static int
460 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
461 {
462   int added;
463 #if OLD
464   struct PeerConnection *first_iter;
465   struct PeerConnection *second_iter;
466   struct PeerConnection *new_first;
467   struct PeerConnection *new_second;
468 #endif
469   int add_first;
470   int add_second;
471
472   GNUNET_HashCode hash_first;
473   GNUNET_HashCode hash_second;
474
475   hash_from_uid(first, &hash_first);
476   hash_from_uid(second, &hash_second);
477
478   add_first = GNUNET_NO;
479   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
480     {
481       add_first = GNUNET_YES;
482     }
483
484   add_second = GNUNET_NO;
485   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
486     {
487       add_second = GNUNET_YES;
488     }
489 #if OLD
490   first_iter = pg->peers[first].connected_peers;
491   while (first_iter != NULL)
492     {
493       if (first_iter->daemon == pg->peers[second].daemon)
494         add_first = GNUNET_NO;
495       first_iter = first_iter->next;
496     }
497
498   second_iter = pg->peers[second].connected_peers;
499   add_second = GNUNET_YES;
500   while (second_iter != NULL)
501     {
502       if (second_iter->daemon == pg->peers[first].daemon)
503         add_second = GNUNET_NO;
504       second_iter = second_iter->next;
505     }
506 #endif
507
508   added = 0;
509   if (add_first)
510     {
511       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
512 #if OLD
513       new_first = GNUNET_malloc(sizeof(struct PeerConnection));
514       new_first->daemon = pg->peers[second].daemon;
515       new_first->next = pg->peers[first].connected_peers;
516       pg->peers[first].connected_peers = new_first;
517 #endif
518       pg->peers[first].num_connections++;
519       added++;
520     }
521
522   if (add_second)
523     {
524       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
525 #if OLD
526       new_second = GNUNET_malloc(sizeof(struct PeerConnection));
527       new_second->daemon = pg->peers[first].daemon;
528       new_second->next = pg->peers[second].connected_peers;
529       pg->peers[second].connected_peers = new_second;
530       pg->peers[first].num_connections++;
531 #endif
532       pg->peers[second].num_connections++;
533       added++;
534     }
535
536   return added;
537 }
538
539 /*
540  * Add entries to the peers blacklisted list
541  *
542  * @param pg the peer group we are working with
543  * @param first index of the first peer
544  * @param second index of the second peer
545  *
546  * @return the number of connections added (can be 0, 1 or 2)
547  *
548  */
549 static int
550 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
551 {
552   int added;
553   int add_first;
554   int add_second;
555   GNUNET_HashCode hash_first;
556   GNUNET_HashCode hash_second;
557
558   hash_from_uid(first, &hash_first);
559   hash_from_uid(second, &hash_second);
560
561   add_first = GNUNET_NO;
562   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
563     {
564       add_first = GNUNET_YES;
565     }
566
567   add_second = GNUNET_NO;
568   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
569     {
570       add_second = GNUNET_YES;
571     }
572
573   added = 0;
574   if (add_first)
575     {
576       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
577       pg->peers[first].num_connections++;
578       added++;
579     }
580
581   if (add_second)
582     {
583       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
584       pg->peers[second].num_connections++;
585       added++;
586     }
587
588   return added;
589 }
590
591 /*
592  * Remove entries from the peers blacklisted list
593  *
594  * @param pg the peer group we are working with
595  * @param first index of the first peer
596  * @param second index of the second peer
597  *
598  * @return the number of connections removed (can be 0, 1 or 2)
599  *
600  */
601 static int
602 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
603 {
604   int removed;
605   int remove_first;
606   int remove_second;
607   GNUNET_HashCode hash_first;
608   GNUNET_HashCode hash_second;
609
610   hash_from_uid(first, &hash_first);
611   hash_from_uid(second, &hash_second);
612
613   remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
614   remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
615
616   removed = 0;
617   if (remove_first)
618     {
619       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
620       removed++;
621     }
622
623   if (remove_second)
624     {
625       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
626       removed++;
627     }
628
629   return removed;
630 }
631
632 /**
633  * Scale free network construction as described in:
634  *
635  * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
636  *
637  * Start with a network of "one" peer, then progressively add
638  * peers up to the total number.  At each step, iterate over
639  * all possible peers and connect new peer based on number of
640  * existing connections of the target peer.
641  *
642  * @param pg the peer group we are dealing with
643  *
644  * @return the number of connections created
645  */
646 static int
647 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
648 {
649
650   unsigned int total_connections;
651   unsigned int outer_count;
652   unsigned int i;
653   unsigned int previous_total_connections;
654   double random;
655   double probability;
656
657   GNUNET_assert(pg->total > 1);
658
659   /* Add a connection between the first two nodes */
660   total_connections = proc(pg, 0, 1);
661
662   for (outer_count = 1; outer_count < pg->total; outer_count++)
663     {
664       previous_total_connections = total_connections;
665       for (i = 0; i < outer_count; i++)
666         {
667           probability = pg->peers[i].num_connections / (double)previous_total_connections;
668           random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
669                                                       (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
670 #if VERBOSE_TESTING
671           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672                       "Considering connecting peer %d to peer %d\n",
673                       outer_count, i);
674 #endif
675           if (random < probability)
676             {
677 #if VERBOSE_TESTING
678               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679                           "Connecting peer %d to peer %d\n",
680                           outer_count, i);
681 #endif
682               total_connections += proc(pg, outer_count, i);
683             }
684         }
685     }
686
687   return total_connections;
688 }
689
690 int
691 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
692 {
693   unsigned int i, j;
694   int nodeToConnect;
695   unsigned int natLog;
696   unsigned int randomPeer;
697   double random, logNModifier, percentage;
698   unsigned int smallWorldConnections;
699   int connsPerPeer;
700   char *p_string;
701   int max;
702   int min;
703   unsigned int useAnd;
704   int connect_attempts;
705
706   logNModifier = 0.5; /* FIXME: default value? */
707   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
708                                                          "TESTING",
709                                                          "LOGNMODIFIER",
710                                                          &p_string))
711     {
712       if (sscanf(p_string, "%lf", &logNModifier) != 1)
713         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
714                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
715                     p_string,
716                     "LOGNMODIFIER",
717                     "TESTING");
718       GNUNET_free (p_string);
719     }
720   percentage = 0.5; /* FIXME: default percentage? */
721   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
722                                                          "TESTING",
723                                                          "PERCENTAGE",
724                                                          &p_string))
725     {
726       if (sscanf(p_string, "%lf", &percentage) != 1)
727         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
728                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
729                     p_string,
730                     "PERCENTAGE",
731                     "TESTING");
732       GNUNET_free (p_string);
733     }
734   natLog = log (pg->total);
735   connsPerPeer = ceil (natLog * logNModifier);
736
737   if (connsPerPeer % 2 == 1)
738     connsPerPeer += 1;
739
740   smallWorldConnections = 0;
741   connect_attempts = 0;
742   for (i = 0; i < pg->total; i++)
743     {
744       useAnd = 0;
745       max = i + connsPerPeer / 2;
746       min = i - connsPerPeer / 2;
747
748       if (max > pg->total - 1)
749         {
750           max = max - pg->total;
751           useAnd = 1;
752         }
753
754       if (min < 0)
755         {
756           min = pg->total - 1 + min;
757           useAnd = 1;
758         }
759
760       for (j = 0; j < connsPerPeer / 2; j++)
761         {
762           random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
763                                                       (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
764           if (random < percentage)
765             {
766               /* Connect to uniformly selected random peer */
767               randomPeer =
768                 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
769                                    pg->total);
770               while ((((randomPeer < max) && (randomPeer > min))
771                       && (useAnd == 0)) || (((randomPeer > min)
772                                              || (randomPeer < max))
773                                             && (useAnd == 1)))
774                 {
775                   randomPeer =
776                       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
777                                                          pg->total);
778                 }
779               smallWorldConnections +=
780                 proc (pg, i, randomPeer);
781             }
782           else
783             {
784               nodeToConnect = i + j + 1;
785               if (nodeToConnect > pg->total - 1)
786                 {
787                   nodeToConnect = nodeToConnect - pg->total;
788                 }
789               connect_attempts +=
790                 proc (pg, i, nodeToConnect);
791             }
792         }
793
794     }
795
796   connect_attempts += smallWorldConnections;
797
798   return connect_attempts;
799 }
800
801
802 static int
803 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
804 {
805   unsigned int outer_count, inner_count;
806   unsigned int cutoff;
807   int connect_attempts;
808   double nat_percentage;
809   char *p_string;
810
811   nat_percentage = 0.6; /* FIXME: default percentage? */
812   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
813                                                          "TESTING",
814                                                          "NATPERCENTAGE",
815                                                          &p_string))
816     {
817       if (sscanf(p_string, "%lf", &nat_percentage) != 1)
818         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
819                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
820                     p_string,
821                     "NATPERCENTAGE",
822                     "TESTING");
823       GNUNET_free (p_string);
824     }
825
826
827
828   cutoff = (unsigned int) (nat_percentage * pg->total);
829
830   connect_attempts = 0;
831
832   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
833     {
834       for (inner_count = outer_count + 1; inner_count < pg->total;
835            inner_count++)
836         {
837           if ((outer_count > cutoff) || (inner_count > cutoff))
838             {
839 #if VERBOSE_TESTING
840               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841                           "Connecting peer %d to peer %d\n",
842                           outer_count, inner_count);
843 #endif
844               connect_attempts += proc(pg, outer_count, inner_count);
845             }
846         }
847     }
848
849   return connect_attempts;
850
851 }
852
853
854
855 static int
856 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
857 {
858   unsigned int i, j, k;
859   unsigned int square;
860   unsigned int rows;
861   unsigned int cols;
862   unsigned int toggle = 1;
863   unsigned int nodeToConnect;
864   unsigned int natLog;
865   unsigned int node1Row;
866   unsigned int node1Col;
867   unsigned int node2Row;
868   unsigned int node2Col;
869   unsigned int distance;
870   double probability, random, percentage;
871   unsigned int smallWorldConnections;
872   char *p_string;
873   int connect_attempts;
874   square = floor (sqrt (pg->total));
875   rows = square;
876   cols = square;
877
878   percentage = 0.5; /* FIXME: default percentage? */
879   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
880                                                          "TESTING",
881                                                          "PERCENTAGE",
882                                                          &p_string))
883     {
884       if (sscanf(p_string, "%lf", &percentage) != 1)
885         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
886                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
887                     p_string,
888                     "PERCENTAGE",
889                     "TESTING");
890       GNUNET_free (p_string);
891     }
892   probability = 0.5; /* FIXME: default percentage? */
893   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
894                                                          "TESTING",
895                                                          "PROBABILITY",
896                                                          &p_string))
897     {
898       if (sscanf(p_string, "%lf", &probability) != 1)
899         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
900                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
901                     p_string,
902                     "PROBABILITY",
903                     "TESTING");
904       GNUNET_free (p_string);
905     }
906   if (square * square != pg->total)
907     {
908       while (rows * cols < pg->total)
909         {
910           if (toggle % 2 == 0)
911             rows++;
912           else
913             cols++;
914
915           toggle++;
916         }
917     }
918 #if VERBOSE_TESTING
919       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
920                   _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
921                   rows, cols);
922 #endif
923
924   connect_attempts = 0;
925   /* Rows and columns are all sorted out, now iterate over all nodes and connect each
926    * to the node to its right and above.  Once this is over, we'll have our torus!
927    * Special case for the last node (if the rows and columns are not equal), connect
928    * to the first in the row to maintain topology.
929    */
930   for (i = 0; i < pg->total; i++)
931     {
932       /* First connect to the node to the right */
933       if (((i + 1) % cols != 0) && (i + 1 != pg->total))
934         nodeToConnect = i + 1;
935       else if (i + 1 == pg->total)
936         nodeToConnect = rows * cols - cols;
937       else
938         nodeToConnect = i - cols + 1;
939
940       connect_attempts += proc (pg, i, nodeToConnect);
941
942       if (i < cols)
943         nodeToConnect = (rows * cols) - cols + i;
944       else
945         nodeToConnect = i - cols;
946
947       if (nodeToConnect < pg->total)
948         connect_attempts += proc (pg, i, nodeToConnect);
949     }
950   natLog = log (pg->total);
951 #if VERBOSE_TESTING > 2
952   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953               _("natural log of %d is %d, will run %d iterations\n"),
954              pg->total, natLog, (int) (natLog * percentage));
955   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
956 #endif
957   smallWorldConnections = 0;
958   for (i = 0; i < (int) (natLog * percentage); i++)
959     {
960       for (j = 0; j < pg->total; j++)
961         {
962           /* Determine the row and column of node at position j on the 2d torus */
963           node1Row = j / cols;
964           node1Col = j - (node1Row * cols);
965           for (k = 0; k < pg->total; k++)
966             {
967               /* Determine the row and column of node at position k on the 2d torus */
968               node2Row = k / cols;
969               node2Col = k - (node2Row * cols);
970               /* Simple Cartesian distance */
971               distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
972               if (distance > 1)
973                 {
974                   /* Calculate probability as 1 over the square of the distance */
975                   probability = 1.0 / (distance * distance);
976                   /* Choose a random value between 0 and 1 */
977                   random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
978                                                               (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
979                   /* If random < probability, then connect the two nodes */
980                   if (random < probability)
981                     smallWorldConnections += proc (pg, j, k);
982
983                 }
984             }
985         }
986     }
987   connect_attempts += smallWorldConnections;
988 #if VERBOSE_TESTING > 2
989           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990                       _("Total connections added for small world: %d!\n"),
991                       smallWorldConnections);
992 #endif
993   return connect_attempts;
994 }
995
996
997
998 static int
999 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1000 {
1001   double temp_rand;
1002   unsigned int outer_count;
1003   unsigned int inner_count;
1004   int connect_attempts;
1005   double probability;
1006   char *p_string;
1007
1008   probability = 0.5; /* FIXME: default percentage? */
1009   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1010                                                          "TESTING",
1011                                                          "PROBABILITY",
1012                                                          &p_string))
1013     {
1014       if (sscanf(p_string, "%lf", &probability) != 1)
1015         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1016                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1017                     p_string,
1018                     "PROBABILITY",
1019                     "TESTING");
1020       GNUNET_free (p_string);
1021     }
1022   connect_attempts = 0;
1023   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1024     {
1025       for (inner_count = outer_count + 1; inner_count < pg->total;
1026            inner_count++)
1027         {
1028           temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1029                                                          (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
1030 #if VERBOSE_TESTING
1031           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032                       _("rand is %f probability is %f\n"), temp_rand,
1033                       probability);
1034 #endif
1035           if (temp_rand < probability)
1036             {
1037               connect_attempts += proc (pg, outer_count, inner_count);
1038             }
1039         }
1040     }
1041
1042   return connect_attempts;
1043 }
1044
1045 static int
1046 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1047 {
1048   unsigned int i;
1049   unsigned int square;
1050   unsigned int rows;
1051   unsigned int cols;
1052   unsigned int toggle = 1;
1053   unsigned int nodeToConnect;
1054   int connect_attempts;
1055
1056   connect_attempts = 0;
1057
1058   square = floor (sqrt (pg->total));
1059   rows = square;
1060   cols = square;
1061
1062   if (square * square != pg->total)
1063     {
1064       while (rows * cols < pg->total)
1065         {
1066           if (toggle % 2 == 0)
1067             rows++;
1068           else
1069             cols++;
1070
1071           toggle++;
1072         }
1073     }
1074 #if VERBOSE_TESTING
1075       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076                   _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1077                   rows, cols);
1078 #endif
1079   /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1080    * to the node to its right and above.  Once this is over, we'll have our torus!
1081    * Special case for the last node (if the rows and columns are not equal), connect
1082    * to the first in the row to maintain topology.
1083    */
1084   for (i = 0; i < pg->total; i++)
1085     {
1086       /* First connect to the node to the right */
1087       if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1088         nodeToConnect = i + 1;
1089       else if (i + 1 == pg->total)
1090         nodeToConnect = rows * cols - cols;
1091       else
1092         nodeToConnect = i - cols + 1;
1093 #if VERBOSE_TESTING
1094           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095                       "Connecting peer %d to peer %d\n",
1096                       i, nodeToConnect);
1097 #endif
1098       connect_attempts += proc(pg, i, nodeToConnect);
1099
1100       /* Second connect to the node immediately above */
1101       if (i < cols)
1102         nodeToConnect = (rows * cols) - cols + i;
1103       else
1104         nodeToConnect = i - cols;
1105
1106       if (nodeToConnect < pg->total)
1107         {
1108 #if VERBOSE_TESTING
1109           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110                       "Connecting peer %d to peer %d\n",
1111                       i, nodeToConnect);
1112 #endif
1113           connect_attempts += proc(pg, i, nodeToConnect);
1114         }
1115
1116     }
1117
1118   return connect_attempts;
1119 }
1120
1121
1122
1123 static int
1124 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1125 {
1126   unsigned int outer_count;
1127   unsigned int inner_count;
1128   int connect_attempts;
1129
1130   connect_attempts = 0;
1131
1132   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1133     {
1134       for (inner_count = outer_count + 1; inner_count < pg->total;
1135            inner_count++)
1136         {
1137 #if VERBOSE_TESTING
1138           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139                       "Connecting peer %d to peer %d\n",
1140                       outer_count, inner_count);
1141 #endif
1142           connect_attempts += proc(pg, outer_count, inner_count);
1143         }
1144     }
1145
1146   return connect_attempts;
1147 }
1148
1149
1150 static int
1151 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1152 {
1153   unsigned int count;
1154   int connect_attempts;
1155
1156   connect_attempts = 0;
1157
1158   /* Connect each peer to the next highest numbered peer */
1159   for (count = 0; count < pg->total - 1; count++)
1160     {
1161 #if VERBOSE_TESTING
1162           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163                       "Connecting peer %d to peer %d\n",
1164                       count, count + 1);
1165 #endif
1166       connect_attempts += proc(pg, count, count + 1);
1167     }
1168
1169   /* Connect the last peer to the first peer */
1170   connect_attempts += proc(pg, pg->total - 1, 0);
1171
1172   return connect_attempts;
1173 }
1174
1175
1176 /**
1177  * Iterator for writing friends of a peer to a file.
1178  *
1179  * @param cls closure, an open writable file handle
1180  * @param key the key the daemon was stored under
1181  * @param value the GNUNET_TESTING_Daemon that needs to be written.
1182  *
1183  * @return GNUNET_YES to continue iteration
1184  *
1185  * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1186  *       with a single file_iterator that takes a closure which contains
1187  *       the prefix to write before the peer.  Then this could be used
1188  *       for blacklisting multiple transports and writing the friend
1189  *       file.  I'm sure *someone* will complain loudly about other
1190  *       things that negate these functions even existing so no point in
1191  *       "fixing" now.
1192  */
1193 static int
1194 friend_file_iterator (void *cls,
1195                   const GNUNET_HashCode * key,
1196                   void *value)
1197 {
1198   FILE *temp_friend_handle = cls;
1199   struct GNUNET_TESTING_Daemon *peer = value;
1200   struct GNUNET_PeerIdentity *temppeer;
1201   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1202
1203   temppeer = &peer->id;
1204   GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1205   fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1206
1207   return GNUNET_YES;
1208 }
1209
1210
1211 /**
1212  * Iterator for writing blacklist data to appropriate files.
1213  *
1214  * @param cls closure, an open writable file handle
1215  * @param key the key the daemon was stored under
1216  * @param value the GNUNET_TESTING_Daemon that needs to be written.
1217  *
1218  * @return GNUNET_YES to continue iteration
1219  */
1220 static int
1221 blacklist_file_iterator (void *cls,
1222                          const GNUNET_HashCode * key,
1223                          void *value)
1224 {
1225   FILE *temp_blacklist_handle = cls;
1226   struct GNUNET_TESTING_Daemon *peer = value;
1227   struct GNUNET_PeerIdentity *temppeer;
1228   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1229
1230   temppeer = &peer->id;
1231   GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1232   fprintf(temp_blacklist_handle, "tcp:%s\n", (char *)&peer_enc);
1233
1234   return GNUNET_YES;
1235 }
1236
1237 /*
1238  * Create the friend files based on the PeerConnection's
1239  * of each peer in the peer group, and copy the files
1240  * to the appropriate place
1241  *
1242  * @param pg the peer group we are dealing with
1243  */
1244 static int
1245 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1246 {
1247   FILE *temp_friend_handle;
1248   unsigned int pg_iter;
1249   char *temp_service_path;
1250   pid_t *pidarr;
1251   char *arg;
1252   char * mytemp;
1253   enum GNUNET_OS_ProcessStatusType type;
1254   unsigned long return_code;
1255   int count;
1256   int ret;
1257   int max_wait = 10;
1258
1259   pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1260   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1261     {
1262       mytemp = GNUNET_DISK_mktemp("friends");
1263       GNUNET_assert(mytemp != NULL);
1264       temp_friend_handle = fopen (mytemp, "wt");
1265       GNUNET_assert(temp_friend_handle != NULL);
1266       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1267       fclose(temp_friend_handle);
1268
1269       if (GNUNET_OK !=
1270           GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1271         {
1272           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1273                       _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1274                       "SERVICEHOME",
1275                       "PATHS");
1276           if (UNLINK (mytemp) != 0)
1277             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1278           GNUNET_free (mytemp);
1279           break;
1280         }
1281
1282       if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1283         {
1284           GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1285           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1286                                          "mv", mytemp, arg, NULL);
1287 #if VERBOSE_TESTING
1288           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289                       _("Copying file with command cp %s %s\n"), mytemp, arg);
1290 #endif
1291
1292           GNUNET_free(arg);
1293         }
1294       else /* Remote, scp the file to the correct place */
1295         {
1296           if (NULL != pg->peers[pg_iter].daemon->username)
1297             GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1298           else
1299             GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1300           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1301                                          "scp", mytemp, arg, NULL);
1302
1303 #if VERBOSE_TESTING
1304           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305                       _("Copying file with command scp %s %s\n"), mytemp, arg);
1306 #endif
1307           GNUNET_free(arg);
1308         }
1309       GNUNET_free (temp_service_path);
1310       GNUNET_free (mytemp);
1311     }
1312
1313   count = 0;
1314   ret = GNUNET_SYSERR;
1315   while ((count < max_wait) && (ret != GNUNET_OK))
1316     {
1317       ret = GNUNET_OK;
1318       for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1319         {
1320 #if VERBOSE_TESTING
1321           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322                       _("Checking copy status of file %d\n"), pg_iter);
1323 #endif
1324           if (pidarr[pg_iter] != 0) /* Check for already completed! */
1325             {
1326               if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1327                 {
1328                   ret = GNUNET_SYSERR;
1329                 }
1330               else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1331                 {
1332                   ret = GNUNET_SYSERR;
1333                 }
1334               else
1335                 {
1336                   pidarr[pg_iter] = 0;
1337 #if VERBOSE_TESTING
1338             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1339                       _("File %d copied\n"), pg_iter);
1340 #endif
1341                 }
1342             }
1343         }
1344       count++;
1345       if (ret == GNUNET_SYSERR)
1346         {
1347           sleep(1);
1348         }
1349     }
1350
1351 #if VERBOSE_TESTING
1352     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353                 _("Finished copying all friend files!\n"));
1354 #endif
1355   GNUNET_free(pidarr);
1356   return ret;
1357 }
1358
1359
1360 /*
1361  * Create the blacklist files based on the PeerConnection's
1362  * of each peer in the peer group, and copy the files
1363  * to the appropriate place.
1364  *
1365  * @param pg the peer group we are dealing with
1366  */
1367 static int
1368 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg)
1369 {
1370   FILE *temp_friend_handle;
1371   unsigned int pg_iter;
1372   char *temp_service_path;
1373   pid_t *pidarr;
1374   char *arg;
1375   char *mytemp;
1376   enum GNUNET_OS_ProcessStatusType type;
1377   unsigned long return_code;
1378   int count;
1379   int ret;
1380   int max_wait = 10;
1381
1382   pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1383   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1384     {
1385       mytemp = GNUNET_DISK_mktemp("blacklist");
1386       GNUNET_assert(mytemp != NULL);
1387       temp_friend_handle = fopen (mytemp, "wt");
1388       GNUNET_assert(temp_friend_handle != NULL);
1389       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, temp_friend_handle);
1390       fclose(temp_friend_handle);
1391
1392       if (GNUNET_OK !=
1393           GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1394         {
1395           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1396                       _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1397                       "SERVICEHOME",
1398                       "PATHS");
1399           if (UNLINK (mytemp) != 0)
1400             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1401           GNUNET_free (mytemp);
1402           break;
1403         }
1404
1405       if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1406         {
1407           GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
1408           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1409                                          "mv", mytemp, arg, NULL);
1410 #if VERBOSE_TESTING
1411           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412                       _("Copying file with command cp %s %s\n"), mytemp, arg);
1413 #endif
1414
1415           GNUNET_free(arg);
1416         }
1417       else /* Remote, scp the file to the correct place */
1418         {
1419           if (NULL != pg->peers[pg_iter].daemon->username)
1420             GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1421           else
1422             GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1423           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1424                                          "scp", mytemp, arg, NULL);
1425
1426 #if VERBOSE_TESTING
1427           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428                       _("Copying file with command scp %s %s\n"), mytemp, arg);
1429 #endif
1430           GNUNET_free(arg);
1431         }
1432       GNUNET_free (temp_service_path);
1433       GNUNET_free (mytemp);
1434     }
1435
1436   count = 0;
1437   ret = GNUNET_SYSERR;
1438   while ((count < max_wait) && (ret != GNUNET_OK))
1439     {
1440       ret = GNUNET_OK;
1441       for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1442         {
1443 #if VERBOSE_TESTING
1444           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445                       _("Checking copy status of file %d\n"), pg_iter);
1446 #endif
1447           if (pidarr[pg_iter] != 0) /* Check for already completed! */
1448             {
1449               if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1450                 {
1451                   ret = GNUNET_SYSERR;
1452                 }
1453               else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1454                 {
1455                   ret = GNUNET_SYSERR;
1456                 }
1457               else
1458                 {
1459                   pidarr[pg_iter] = 0;
1460 #if VERBOSE_TESTING
1461             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462                       _("File %d copied\n"), pg_iter);
1463 #endif
1464                 }
1465             }
1466         }
1467       count++;
1468       if (ret == GNUNET_SYSERR)
1469         {
1470           sleep(1);
1471         }
1472     }
1473
1474 #if VERBOSE_TESTING
1475     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476                 _("Finished copying all blacklist files!\n"));
1477 #endif
1478   GNUNET_free(pidarr);
1479   return ret;
1480 }
1481
1482
1483 /**
1484  * Internal notification of a connection, kept so that we can ensure some connections
1485  * happen instead of flooding all testing daemons with requests to connect.
1486  */
1487 static void internal_connect_notify (void *cls,
1488                                      const struct GNUNET_PeerIdentity *first,
1489                                      const struct GNUNET_PeerIdentity *second,
1490                                      const struct GNUNET_CONFIGURATION_Handle *first_cfg,
1491                                      const struct GNUNET_CONFIGURATION_Handle *second_cfg,
1492                                      struct GNUNET_TESTING_Daemon *first_daemon,
1493                                      struct GNUNET_TESTING_Daemon *second_daemon,
1494                                      const char *emsg)
1495 {
1496   struct GNUNET_TESTING_PeerGroup *pg = cls;
1497   outstanding_connects--;
1498
1499   pg->notify_connection(pg->notify_connection_cls, first, second, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
1500
1501 }
1502
1503 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1504 {
1505   struct ConnectContext *connect_context = cls;
1506
1507   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1508     return;
1509
1510   if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
1511     {
1512 #if VERBOSE_TESTING > 2
1513           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514                       _("Delaying connect, we have too many outstanding connections!\n"));
1515 #endif
1516       GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &schedule_connect, connect_context);
1517     }
1518   else
1519     {
1520 #if VERBOSE_TESTING > 2
1521           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522                       _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
1523 #endif
1524       outstanding_connects++;
1525       GNUNET_TESTING_daemons_connect (connect_context->first,
1526                                       connect_context->second,
1527                                       CONNECT_TIMEOUT,
1528                                       CONNECT_ATTEMPTS,
1529                                       &internal_connect_notify,
1530                                       connect_context->pg);
1531       GNUNET_free(connect_context);
1532     }
1533 }
1534
1535 /**
1536  * Iterator for actually scheduling connections to be created
1537  * between two peers.
1538  *
1539  * @param cls closure, a GNUNET_TESTING_Daemon
1540  * @param key the key the second Daemon was stored under
1541  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1542  *
1543  * @return GNUNET_YES to continue iteration
1544  */
1545 static int
1546 connect_iterator (void *cls,
1547                   const GNUNET_HashCode * key,
1548                   void *value)
1549 {
1550   struct PeerData *first = cls;
1551   struct GNUNET_TESTING_Daemon *second = value;
1552   struct ConnectContext *connect_context;
1553
1554   connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1555   connect_context->pg = first->pg;
1556   connect_context->first = first->daemon;
1557   connect_context->second = second;
1558   GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
1559
1560   return GNUNET_YES;
1561 }
1562
1563 /**
1564  * Iterator for copying all entries in the allowed hashmap to the
1565  * connect hashmap.
1566  *
1567  * @param cls closure, a GNUNET_TESTING_Daemon
1568  * @param key the key the second Daemon was stored under
1569  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1570  *
1571  * @return GNUNET_YES to continue iteration
1572  */
1573 static int
1574 copy_topology_iterator (void *cls,
1575                   const GNUNET_HashCode * key,
1576                   void *value)
1577 {
1578   struct PeerData *first = cls;
1579
1580   GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1581
1582   return GNUNET_YES;
1583 }
1584
1585 /**
1586  * Make the peers to connect the same as those that are allowed to be
1587  * connected.
1588  *
1589  * @param pg the peer group
1590  */
1591 static int
1592 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
1593 {
1594   unsigned int pg_iter;
1595   int ret;
1596   int total;
1597
1598   total = 0;
1599   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1600     {
1601       ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &copy_topology_iterator, &pg->peers[pg_iter]);
1602       if (GNUNET_SYSERR == ret)
1603         return GNUNET_SYSERR;
1604
1605       total = total + ret;
1606     }
1607
1608   return total;
1609 }
1610
1611
1612 /*
1613  * Connect the topology as specified by the PeerConnection's
1614  * of each peer in the peer group
1615  *
1616  * @param pg the peer group we are dealing with
1617  *
1618  * @return the number of connections that will be attempted
1619  */
1620 static int
1621 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
1622 {
1623   unsigned int pg_iter;
1624   int ret;
1625   int total;
1626 #if OLD
1627   struct PeerConnection *connection_iter;
1628   struct ConnectContext *connect_context;
1629 #endif
1630
1631   total = 0;
1632   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1633     {
1634       ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
1635       if (GNUNET_SYSERR == ret)
1636         return GNUNET_SYSERR;
1637
1638       total = total + ret;
1639
1640 #if OLD
1641       connection_iter = ;
1642       while (connection_iter != NULL)
1643         {
1644           connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
1645           connect_context->pg = pg;
1646           connect_context->first = ;
1647           connect_context->second = connection_iter->daemon;
1648           GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
1649           connection_iter = connection_iter->next;
1650         }
1651 #endif
1652     }
1653   return total;
1654 }
1655
1656
1657 /*
1658  * Takes a peer group and creates a topology based on the
1659  * one specified.  Creates a topology means generates friend
1660  * files for the peers so they can only connect to those allowed
1661  * by the topology.  This will only have an effect once peers
1662  * are started if the FRIENDS_ONLY option is set in the base
1663  * config.  Also takes an optional restrict topology which
1664  * disallows direct TCP connections UNLESS they are specified in
1665  * the restricted topology.
1666  *
1667  * @param pg the peer group struct representing the running peers
1668  * @param topology which topology to connect the peers in
1669  * @param restrict_topology allow only direct TCP connections in this topology
1670  *                          use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
1671  *
1672  * @return the maximum number of connections were all allowed peers
1673  *         connected to each other
1674  */
1675 int
1676 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
1677                                 enum GNUNET_TESTING_Topology topology,
1678                                 enum GNUNET_TESTING_Topology restrict_topology)
1679 {
1680   int ret;
1681   int num_connections;
1682   int unblacklisted_connections;
1683
1684   GNUNET_assert (pg->notify_connection != NULL);
1685   ret = GNUNET_OK;
1686
1687   switch (topology)
1688     {
1689     case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1690 #if VERBOSE_TESTING
1691       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692                   _("Creating clique topology\n"));
1693 #endif
1694       num_connections = create_clique (pg, &add_allowed_connections);
1695       break;
1696     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1697 #if VERBOSE_TESTING
1698       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699                   _("Creating small world (ring) topology\n"));
1700 #endif
1701       num_connections = create_small_world_ring (pg, &add_allowed_connections);
1702       break;
1703     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1704 #if VERBOSE_TESTING
1705       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706                   _("Creating small world (2d-torus) topology\n"));
1707 #endif
1708       num_connections = create_small_world (pg, &add_allowed_connections);
1709       break;
1710     case GNUNET_TESTING_TOPOLOGY_RING:
1711 #if VERBOSE_TESTING
1712       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1713                   _("Creating ring topology\n"));
1714 #endif
1715       num_connections = create_ring (pg, &add_allowed_connections);
1716       break;
1717     case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1718 #if VERBOSE_TESTING
1719       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1720                   _("Creating 2d torus topology\n"));
1721 #endif
1722       num_connections = create_2d_torus (pg, &add_allowed_connections);
1723       break;
1724     case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1725 #if VERBOSE_TESTING
1726       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1727                   _("Creating Erdos-Renyi topology\n"));
1728 #endif
1729       num_connections = create_erdos_renyi (pg, &add_allowed_connections);
1730       break;
1731     case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1732 #if VERBOSE_TESTING
1733       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734                   _("Creating InterNAT topology\n"));
1735 #endif
1736       num_connections = create_nated_internet (pg, &add_allowed_connections);
1737       break;
1738     case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
1739 #if VERBOSE_TESTING
1740       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1741                   _("Creating Scale Free topology\n"));
1742 #endif
1743       num_connections = create_scale_free (pg, &add_allowed_connections);
1744       break;
1745     case GNUNET_TESTING_TOPOLOGY_NONE:
1746       num_connections = 0;
1747       break;
1748     default:
1749       num_connections = 0;
1750       break;
1751     }
1752   if (num_connections < 1)
1753     return GNUNET_SYSERR;
1754
1755   if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
1756     {
1757       ret = create_and_copy_friend_files(pg);
1758     }
1759
1760   if (ret != GNUNET_OK)
1761     {
1762 #if VERBOSE_TESTING
1763       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1764                   _("Failed during friend file copying!\n"));
1765 #endif
1766       return GNUNET_SYSERR;
1767     }
1768   else
1769     {
1770 #if VERBOSE_TESTING
1771           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772                       _("Friend files created/copied successfully!\n"));
1773 #endif
1774     }
1775
1776   /**
1777    * Use the create clique method to initially set all connections
1778    * as blacklisted.
1779    */
1780   create_clique (pg, &blacklist_connections);
1781   unblacklisted_connections = 0;
1782   /**
1783    * Un-blacklist connections as per the topology specified
1784    */
1785   switch (restrict_topology)
1786     {
1787     case GNUNET_TESTING_TOPOLOGY_CLIQUE:
1788 #if VERBOSE_TESTING
1789       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790                   _("Blacklisting all but clique topology\n"));
1791 #endif
1792       unblacklisted_connections = create_clique (pg, &unblacklist_connections);
1793       break;
1794     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
1795 #if VERBOSE_TESTING
1796       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1797                   _("Blacklisting all but small world (ring) topology\n"));
1798 #endif
1799       unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
1800       break;
1801     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
1802 #if VERBOSE_TESTING
1803       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804                   _("Blacklisting all but small world (2d-torus) topology\n"));
1805 #endif
1806       unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
1807       break;
1808     case GNUNET_TESTING_TOPOLOGY_RING:
1809 #if VERBOSE_TESTING
1810       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1811                   _("Blacklisting all but ring topology\n"));
1812 #endif
1813       unblacklisted_connections = create_ring (pg, &unblacklist_connections);
1814       break;
1815     case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
1816 #if VERBOSE_TESTING
1817       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818                   _("Blacklisting all but 2d torus topology\n"));
1819 #endif
1820       unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
1821       break;
1822     case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
1823 #if VERBOSE_TESTING
1824       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825                   _("Blacklisting all but Erdos-Renyi topology\n"));
1826 #endif
1827       unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
1828       break;
1829     case GNUNET_TESTING_TOPOLOGY_INTERNAT:
1830 #if VERBOSE_TESTING
1831       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832                   _("Blacklisting all but InterNAT topology\n"));
1833 #endif
1834       unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
1835       break;
1836     case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
1837 #if VERBOSE_TESTING
1838       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839                   _("Blacklisting all but Scale Free topology\n"));
1840 #endif
1841       unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
1842       break;
1843     case GNUNET_TESTING_TOPOLOGY_NONE:
1844       /* Fall through */
1845     default:
1846       break;
1847     }
1848
1849   if (unblacklisted_connections > 0)
1850   {
1851     ret = create_and_copy_blacklist_files(pg);
1852     if (ret != GNUNET_OK)
1853       {
1854 #if VERBOSE_TESTING
1855         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856                     _("Failed during blacklist file copying!\n"));
1857 #endif
1858         return GNUNET_SYSERR;
1859       }
1860     else
1861       {
1862 #if VERBOSE_TESTING
1863         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864                     _("Blacklist files created/copied successfully!\n"));
1865 #endif
1866       }
1867   }
1868
1869
1870   return num_connections;
1871 }
1872
1873 struct RandomContext
1874 {
1875   /**
1876    * The peergroup
1877    */
1878   struct GNUNET_TESTING_PeerGroup *pg;
1879
1880   /**
1881    * uid of the first peer
1882    */
1883   uint32_t first_uid;
1884
1885   /**
1886    * Peer data for first peer.
1887    */
1888   struct PeerData *first;
1889
1890   /**
1891    * Random percentage to use
1892    */
1893   double percentage;
1894 };
1895
1896 struct MinimumContext
1897 {
1898   /**
1899    * The peergroup
1900    */
1901   struct GNUNET_TESTING_PeerGroup *pg;
1902
1903   /**
1904    * uid of the first peer
1905    */
1906   uint32_t first_uid;
1907
1908   /**
1909    * Peer data for first peer.
1910    */
1911   struct PeerData *first;
1912
1913   /**
1914    * Number of conns per peer
1915    */
1916   unsigned int num_to_add;
1917
1918   /**
1919    * Permuted array of all possible connections.  Only add the Nth
1920    * peer if it's in the Nth position.
1921    */
1922   unsigned int *pg_array;
1923
1924   /**
1925    * What number is the current element we are iterating over?
1926    */
1927   unsigned int current;
1928 };
1929
1930 struct DFSContext
1931 {
1932   /**
1933    * The peergroup
1934    */
1935   struct GNUNET_TESTING_PeerGroup *pg;
1936
1937   /**
1938    * uid of the first peer
1939    */
1940   uint32_t first_uid;
1941
1942   /**
1943    * uid of the second peer
1944    */
1945   uint32_t second_uid;
1946
1947   /**
1948    * Peer data for first peer.
1949    */
1950   struct PeerData *first;
1951
1952   /**
1953    * Which peer has been chosen as the one to add?
1954    */
1955   unsigned int chosen;
1956
1957   /**
1958    * What number is the current element we are iterating over?
1959    */
1960   unsigned int current;
1961 };
1962
1963 /**
1964  * Iterator for choosing random peers to connect.
1965  *
1966  * @param cls closure, a RandomContext
1967  * @param key the key the second Daemon was stored under
1968  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
1969  *
1970  * @return GNUNET_YES to continue iteration
1971  */
1972 static int
1973 random_connect_iterator (void *cls,
1974                   const GNUNET_HashCode * key,
1975                   void *value)
1976 {
1977   struct RandomContext *random_ctx = cls;
1978   double random_number;
1979   uint32_t second_pos;
1980   GNUNET_HashCode first_hash;
1981   random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1982                    (uint64_t)-1LL)) / ( (double) (uint64_t) -1LL);
1983   if (random_number < random_ctx->percentage)
1984   {
1985     GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1986   }
1987   /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
1988   uid_from_hash(key, &second_pos);
1989   hash_from_uid(random_ctx->first_uid, &first_hash);
1990   GNUNET_assert(random_ctx->pg->total > second_pos);
1991   GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
1992
1993   return GNUNET_YES;
1994 }
1995
1996 /**
1997  * Iterator for adding at least X peers to a peers connection set.
1998  *
1999  * @param cls closure, MinimumContext
2000  * @param key the key the second Daemon was stored under
2001  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2002  *
2003  * @return GNUNET_YES to continue iteration
2004  */
2005 static int
2006 minimum_connect_iterator (void *cls,
2007                   const GNUNET_HashCode * key,
2008                   void *value)
2009 {
2010   struct MinimumContext *min_ctx = cls;
2011   uint32_t second_pos;
2012   GNUNET_HashCode first_hash;
2013   unsigned int i;
2014
2015   if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2016   {
2017     for (i = 0; i < min_ctx->num_to_add; i++)
2018     {
2019       if (min_ctx->pg_array[i] == min_ctx->current)
2020       {
2021         GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2022         uid_from_hash(key, &second_pos);
2023         hash_from_uid(min_ctx->first_uid, &first_hash);
2024         GNUNET_assert(min_ctx->pg->total > second_pos);
2025         GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->pg->peers[second_pos].connect_peers_working_set, &first_hash, min_ctx->first->daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2026         /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2027         GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2028       }
2029     }
2030     min_ctx->current++;
2031     return GNUNET_YES;
2032   }
2033   else
2034     return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2035
2036 }
2037
2038
2039 /**
2040  * Iterator for adding peers to a connection set based on a depth first search.
2041  *
2042  * @param cls closure, MinimumContext
2043  * @param key the key the second daemon was stored under
2044  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2045  *
2046  * @return GNUNET_YES to continue iteration
2047  */
2048 static int
2049 dfs_connect_iterator (void *cls,
2050                   const GNUNET_HashCode * key,
2051                   void *value)
2052 {
2053   struct DFSContext *dfs_ctx = cls;
2054   GNUNET_HashCode first_hash;
2055
2056   if (dfs_ctx->current == dfs_ctx->chosen)
2057     {
2058       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2059       uid_from_hash(key, &dfs_ctx->second_uid);
2060       hash_from_uid(dfs_ctx->first_uid, &first_hash);
2061       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers_working_set, &first_hash, dfs_ctx->first->daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2062       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2063       /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2064       return GNUNET_NO; /* We have found our peer, don't iterate more */
2065     }
2066
2067   dfs_ctx->current++;
2068   return GNUNET_YES;
2069 }
2070
2071
2072 /**
2073  * From the set of connections possible, choose percentage percent of connections
2074  * to actually connect.
2075  *
2076  * @param pg the peergroup we are dealing with
2077  * @param percentage what percent of total connections to make
2078  */
2079 void
2080 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2081 {
2082   struct RandomContext random_ctx;
2083   uint32_t pg_iter;
2084
2085   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2086     {
2087       random_ctx.first_uid = pg_iter;
2088       random_ctx.first = &pg->peers[pg_iter];
2089       random_ctx.percentage = percentage;
2090       random_ctx.pg = pg;
2091       pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2092       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2093       /* Now remove the old connections */
2094       GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2095       /* And replace with the random set */
2096       pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2097     }
2098 }
2099
2100 /**
2101  * From the set of connections possible, choose at least num connections per
2102  * peer.
2103  *
2104  * @param pg the peergroup we are dealing with
2105  * @param num how many connections at least should each peer have (if possible)?
2106  */
2107 void
2108 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2109 {
2110   struct MinimumContext minimum_ctx;
2111   uint32_t pg_iter;
2112
2113   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2114     {
2115       pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2116     }
2117
2118   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2119     {
2120       minimum_ctx.first_uid = pg_iter;
2121       minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2122       minimum_ctx.first = &pg->peers[pg_iter];
2123       minimum_ctx.pg = pg;
2124       minimum_ctx.num_to_add = num;
2125       minimum_ctx.current = 0;
2126       pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2127       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &minimum_connect_iterator, &minimum_ctx);
2128     }
2129
2130   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2131     {
2132       /* Remove the "old" connections */
2133       GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2134       /* And replace with the working set */
2135       pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2136       fprintf(stderr, "Finished! Hashmap size %u\n", GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2137     }
2138
2139 }
2140
2141
2142 static unsigned int count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2143 {
2144   unsigned int count;
2145   unsigned int pg_iter;
2146
2147   count = 0;
2148
2149   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2150     {
2151       count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2152     }
2153
2154   return count;
2155 }
2156
2157
2158 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2159 {
2160   unsigned int count;
2161   unsigned int pg_iter;
2162
2163   count = 0;
2164
2165   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2166     {
2167       count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2168     }
2169
2170   return count;
2171 }
2172
2173 /**
2174  * From the set of connections possible, choose at least num connections per
2175  * peer based on depth first traversal of peer connections.  If DFS leaves
2176  * peers unconnected, ensure those peers get connections.
2177  *
2178  * @param pg the peergroup we are dealing with
2179  * @param num how many connections at least should each peer have (if possible)?
2180  */
2181 void
2182 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2183 {
2184   struct DFSContext dfs_ctx;
2185   uint32_t pg_iter;
2186   uint32_t dfs_count;
2187   uint32_t starting_peer;
2188   uint32_t least_connections;
2189   GNUNET_HashCode second_hash;
2190
2191   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2192     {
2193       pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2194     }
2195
2196   starting_peer = 0;
2197   dfs_count = 0;
2198   while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
2199     {
2200       if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
2201         {
2202           least_connections = -1; /* Set to very high number */
2203           for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2204             {
2205               if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
2206                 {
2207                   starting_peer = pg_iter;
2208                   least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2209                 }
2210             }
2211         }
2212
2213       if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0)  /* Ensure there is at least one peer left to connect! */
2214         {
2215           dfs_count = 0;
2216           continue;
2217         }
2218
2219       /* Choose a random peer from the chosen peers set of connections to add */
2220       dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
2221       dfs_ctx.first_uid = starting_peer;
2222       dfs_ctx.first = &pg->peers[starting_peer];
2223       dfs_ctx.pg = pg;
2224       dfs_ctx.current = 0;
2225
2226       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
2227       /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
2228       hash_from_uid(dfs_ctx.second_uid, &second_hash);
2229       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
2230       starting_peer = dfs_ctx.second_uid;
2231     }
2232
2233   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2234     {
2235
2236     }
2237
2238   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2239     {
2240       /* Remove the "old" connections */
2241       GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2242       /* And replace with the working set */
2243       pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2244       fprintf(stderr, "Finished! Hashmap size %u\n", GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2245     }
2246
2247 }
2248
2249 /*
2250  * @param pg the peer group struct representing the running peers
2251  * @param topology which topology to connect the peers in
2252  * @param options options for connecting the topology
2253  * @param option_modifier modifier for options that take a parameter
2254  *
2255  * There are many ways to connect peers that are supported by this function.
2256  * To connect peers in the same topology that was created via the
2257  * GNUNET_TESTING_create_topology, the topology variable must be set to
2258  * GNUNET_TESTING_TOPOLOGY_NONE.  If the topology variable is specified,
2259  * a new instance of that topology will be generated and attempted to be
2260  * connected.  This could result in some connections being impossible,
2261  * because some topologies are non-deterministic.
2262  *
2263  */
2264 int
2265 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
2266                                  enum GNUNET_TESTING_Topology topology,
2267                                  enum GNUNET_TESTING_TopologyOption options,
2268                                  double option_modifier)
2269 {
2270   int num_connections;
2271
2272   switch (topology)
2273       {
2274       case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2275   #if VERBOSE_TESTING
2276         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2277                     _("Creating clique topology\n"));
2278   #endif
2279         num_connections = create_clique (pg, &add_actual_connections);
2280         break;
2281       case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2282   #if VERBOSE_TESTING
2283         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2284                     _("Creating small world (ring) topology\n"));
2285   #endif
2286         num_connections = create_small_world_ring (pg, &add_actual_connections);
2287         break;
2288       case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2289   #if VERBOSE_TESTING
2290         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2291                     _("Creating small world (2d-torus) topology\n"));
2292   #endif
2293         num_connections = create_small_world (pg, &add_actual_connections);
2294         break;
2295       case GNUNET_TESTING_TOPOLOGY_RING:
2296   #if VERBOSE_TESTING
2297         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298                     _("Creating ring topology\n"));
2299   #endif
2300         num_connections = create_ring (pg, &add_actual_connections);
2301         break;
2302       case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2303   #if VERBOSE_TESTING
2304         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2305                     _("Creating 2d torus topology\n"));
2306   #endif
2307         num_connections = create_2d_torus (pg, &add_actual_connections);
2308         break;
2309       case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2310   #if VERBOSE_TESTING
2311         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312                     _("Creating Erdos-Renyi topology\n"));
2313   #endif
2314         num_connections = create_erdos_renyi (pg, &add_actual_connections);
2315         break;
2316       case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2317   #if VERBOSE_TESTING
2318         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319                     _("Creating InterNAT topology\n"));
2320   #endif
2321         num_connections = create_nated_internet (pg, &add_actual_connections);
2322         break;
2323       case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2324   #if VERBOSE_TESTING
2325         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326                     _("Creating Scale Free topology\n"));
2327   #endif
2328         num_connections = create_scale_free (pg, &add_actual_connections);
2329         break;
2330       case GNUNET_TESTING_TOPOLOGY_NONE:
2331         num_connections = copy_allowed_topology(pg);
2332         break;
2333       default:
2334         GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unknown topology specification, can't connect peers!\n");
2335         return GNUNET_SYSERR;
2336       }
2337
2338   switch (options)
2339     {
2340     case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM: /* Create a random subset of total connections based on parameter */
2341       choose_random_connections(pg, option_modifier);
2342       break;
2343     case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM: /* Create at least X connections per peer (if possible!) */
2344       choose_minimum(pg, (unsigned int)option_modifier);
2345       break;
2346     case GNUNET_TESTING_TOPOLOGY_OPTION_DFS: /* Choose a random starting point, randomly walk graph, try to get each peer X connections */
2347       perform_dfs(pg, (int)option_modifier);
2348       break;
2349     case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
2350       /* Fall through */
2351     case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
2352       /* Fall through */
2353     default:
2354       break;
2355     }
2356
2357   return connect_topology(pg);
2358 }
2359
2360 /**
2361  * Function which continues a peer group starting up
2362  * after successfully generating hostkeys for each peer.
2363  *
2364  * @param pg the peer group to continue starting
2365  *
2366  */
2367 void
2368 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
2369 {
2370   unsigned int i;
2371
2372   for (i = 0; i < pg->total; i++)
2373     {
2374       GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
2375     }
2376 }
2377
2378 /**
2379  * Start count gnunetd processes with the same set of transports and
2380  * applications.  The port numbers (any option called "PORT") will be
2381  * adjusted to ensure that no two peers running on the same system
2382  * have the same port(s) in their respective configurations.
2383  *
2384  * @param sched scheduler to use
2385  * @param cfg configuration template to use
2386  * @param total number of daemons to start
2387  * @param timeout total time allowed for peers to start
2388  * @param hostkey_callback function to call on each peers hostkey generation
2389  *        if NULL, peers will be started by this call, if non-null,
2390  *        GNUNET_TESTING_daemons_continue_startup must be called after
2391  *        successful hostkey generation
2392  * @param hostkey_cls closure for hostkey callback
2393  * @param cb function to call on each daemon that was started
2394  * @param cb_cls closure for cb
2395  * @param connect_callback function to call each time two hosts are connected
2396  * @param connect_callback_cls closure for connect_callback
2397  * @param hostnames space-separated list of hostnames to use; can be NULL (to run
2398  *        everything on localhost).
2399  * @return NULL on error, otherwise handle to control peer group
2400  */
2401 struct GNUNET_TESTING_PeerGroup *
2402 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
2403                               const struct GNUNET_CONFIGURATION_Handle *cfg,
2404                               unsigned int total,
2405                               struct GNUNET_TIME_Relative timeout,
2406                               GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
2407                               void *hostkey_cls,
2408                               GNUNET_TESTING_NotifyDaemonRunning cb,
2409                               void *cb_cls,
2410                               GNUNET_TESTING_NotifyConnection
2411                               connect_callback, void *connect_callback_cls,
2412                               const char *hostnames)
2413 {
2414   struct GNUNET_TESTING_PeerGroup *pg;
2415   const char *rpos;
2416   char *pos;
2417   char *start;
2418   const char *hostname;
2419   char *baseservicehome;
2420   char *newservicehome;
2421   char *tmpdir;
2422   struct GNUNET_CONFIGURATION_Handle *pcfg;
2423   unsigned int off;
2424   unsigned int hostcnt;
2425   uint16_t minport;
2426
2427   if (0 == total)
2428     {
2429       GNUNET_break (0);
2430       return NULL;
2431     }
2432
2433   pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
2434   pg->sched = sched;
2435   pg->cfg = cfg;
2436   pg->cb = cb;
2437   pg->cb_cls = cb_cls;
2438   pg->notify_connection = connect_callback;
2439   pg->notify_connection_cls = connect_callback_cls;
2440   pg->total = total;
2441   pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
2442   pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
2443   if (NULL != hostnames)
2444     {
2445       off = 2;
2446       /* skip leading spaces */
2447       while ((0 != *hostnames) && (isspace (*hostnames)))
2448         hostnames++;
2449       rpos = hostnames;
2450       while ('\0' != *rpos)
2451         {
2452           if (isspace (*rpos))
2453             off++;
2454           rpos++;
2455         }
2456       pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
2457       off = 0;
2458       start = GNUNET_strdup (hostnames);
2459       pos = start;
2460       while ('\0' != *pos)
2461         {
2462           if (isspace (*pos))
2463             {
2464               *pos = '\0';
2465               if (strlen (start) > 0)
2466                 {
2467                   pg->hosts[off].minport = LOW_PORT;
2468                   pg->hosts[off++].hostname = start;
2469                 }
2470               start = pos + 1;
2471             }
2472           pos++;
2473         }
2474       if (strlen (start) > 0)
2475         {
2476           pg->hosts[off].minport = LOW_PORT;
2477           pg->hosts[off++].hostname = start;
2478         }
2479       if (off == 0)
2480         {
2481           GNUNET_free (start);
2482           GNUNET_free (pg->hosts);
2483           pg->hosts = NULL;
2484         }
2485       hostcnt = off;
2486       minport = 0;              /* make gcc happy */
2487     }
2488   else
2489     {
2490       hostcnt = 0;
2491       minport = LOW_PORT;
2492     }
2493   for (off = 0; off < total; off++)
2494     {
2495       if (hostcnt > 0)
2496         {
2497           hostname = pg->hosts[off % hostcnt].hostname;
2498           pcfg = make_config (cfg, &pg->hosts[off % hostcnt].minport, hostname);
2499         }
2500       else
2501         {
2502           hostname = NULL;
2503           pcfg = make_config (cfg, &minport, hostname);
2504         }
2505
2506       if (NULL == pcfg)
2507         {
2508           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2509                       _
2510                       ("Could not create configuration for peer number %u on `%s'!\n"),
2511                       off, hostname == NULL ? "localhost" : hostname);
2512           continue;
2513         }
2514
2515       if (GNUNET_YES ==
2516           GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
2517                                                  &baseservicehome))
2518         {
2519           GNUNET_asprintf (&newservicehome,
2520                            "%s/%d/", baseservicehome, off);
2521           GNUNET_free (baseservicehome);
2522         }
2523       else
2524         {
2525           tmpdir = getenv ("TMPDIR");
2526           tmpdir = tmpdir ? tmpdir : "/tmp";
2527           GNUNET_asprintf (&newservicehome,
2528                            "%s/%s/%d/",
2529                            tmpdir,
2530                            "gnunet-testing-test-test", off);
2531         }
2532       GNUNET_CONFIGURATION_set_value_string (pcfg,
2533                                              "PATHS",
2534                                              "SERVICEHOME", newservicehome);
2535       GNUNET_free (newservicehome);
2536       pg->peers[off].cfg = pcfg;
2537       pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
2538       pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
2539       pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
2540       pg->peers[off].pg = pg;
2541       pg->peers[off].daemon = GNUNET_TESTING_daemon_start (sched,
2542                                                            pcfg,
2543                                                            timeout,
2544                                                            hostname,
2545                                                            hostkey_callback,
2546                                                            hostkey_cls,
2547                                                            cb, cb_cls);
2548       if (NULL == pg->peers[off].daemon)
2549         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2550                     _("Could not start peer number %u!\n"), off);
2551
2552     }
2553   return pg;
2554 }
2555
2556 /*
2557  * Get a daemon by number, so callers don't have to do nasty
2558  * offsetting operation.
2559  */
2560 struct GNUNET_TESTING_Daemon *
2561 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
2562 {
2563   if (position < pg->total)
2564     return pg->peers[position].daemon;
2565   else
2566     return NULL;
2567 }
2568
2569 /**
2570  * Prototype of a function that will be called when a
2571  * particular operation was completed the testing library.
2572  *
2573  * @param cls closure
2574  * @param emsg NULL on success
2575  */
2576 void restart_callback (void *cls,
2577                        const struct GNUNET_PeerIdentity *id,
2578                        const struct GNUNET_CONFIGURATION_Handle *cfg,
2579                        struct GNUNET_TESTING_Daemon *d,
2580                        const char *emsg)
2581 {
2582   struct RestartContext *restart_context = cls;
2583
2584   if (emsg == NULL)
2585     {
2586       restart_context->peers_restarted++;
2587     }
2588   else
2589     {
2590       restart_context->peers_restart_failed++;
2591     }
2592
2593   if (restart_context->peers_restarted == restart_context->peer_group->total)
2594     {
2595       restart_context->callback(restart_context->callback_cls, NULL);
2596       GNUNET_free(restart_context);
2597     }
2598   else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
2599     {
2600       restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
2601       GNUNET_free(restart_context);
2602     }
2603
2604 }
2605
2606 /**
2607  * Restart all peers in the given group.
2608  *
2609  * @param pg the handle to the peer group
2610  * @param timeout how long to wait on failure
2611  * @param callback function to call on completion (or failure)
2612  * @param callback_cls closure for the callback function
2613  */
2614 void
2615 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
2616 {
2617   struct RestartContext *restart_context;
2618   unsigned int off;
2619
2620   if (pg->total > 0)
2621     {
2622       restart_context = GNUNET_malloc(sizeof(struct RestartContext));
2623       restart_context->peer_group = pg;
2624       restart_context->peers_restarted = 0;
2625       restart_context->callback = callback;
2626       restart_context->callback_cls = callback_cls;
2627
2628       for (off = 0; off < pg->total; off++)
2629         {
2630           GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
2631         }
2632     }
2633 }
2634
2635 /**
2636  * Shutdown all peers started in the given group.
2637  *
2638  * @param pg handle to the peer group
2639  * @param timeout how long to wait for shutdown
2640  *
2641  */
2642 void
2643 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_TIME_Relative timeout)
2644 {
2645   unsigned int off;
2646
2647   for (off = 0; off < pg->total; off++)
2648     {
2649       /* FIXME: should we wait for our
2650          continuations to be called here? This
2651          would require us to take a continuation
2652          as well... */
2653
2654       if (NULL != pg->peers[off].daemon)
2655         GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES);
2656       if (NULL != pg->peers[off].cfg)
2657         GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
2658
2659       if (pg->peers[off].allowed_peers != NULL)
2660         GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
2661       if (pg->peers[off].connect_peers != NULL)
2662         GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
2663       if (pg->peers[off].blacklisted_peers != NULL)
2664         GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
2665
2666     }
2667   GNUNET_free (pg->peers);
2668   if (NULL != pg->hosts)
2669     {
2670       GNUNET_free (pg->hosts[0].hostname);
2671       GNUNET_free (pg->hosts);
2672     }
2673   GNUNET_free (pg);
2674 }
2675
2676
2677 /* end of testing_group.c */