additional topology creation option, for adding closest underlay capable peers to...
[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 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file testing/testing_group.c
23  * @brief convenience API for writing testcases for GNUnet
24  * @author Nathan Evans
25  * @author Christian Grothoff
26  *
27  */
28 #include "platform.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_core_service.h"
32
33 #define VERBOSE_TESTING GNUNET_NO
34
35 #define VERBOSE_TOPOLOGY GNUNET_YES
36
37 #define DEBUG_CHURN GNUNET_NO
38
39 /**
40  * Lowest port used for GNUnet testing.  Should be high enough to not
41  * conflict with other applications running on the hosts but be low
42  * enough to not conflict with client-ports (typically starting around
43  * 32k).
44  */
45 #define LOW_PORT 10000
46
47 /**
48  * Highest port used for GNUnet testing.  Should be low enough to not
49  * conflict with the port range for "local" ports (client apps; see
50  * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
51  */
52 #define HIGH_PORT 56000
53
54 #define MAX_OUTSTANDING_CONNECTIONS 10
55
56 #define MAX_CONCURRENT_HOSTKEYS 10
57
58 #define MAX_CONCURRENT_STARTING 10
59
60 #define MAX_CONCURRENT_SHUTDOWN 10
61
62 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
63
64 #define CONNECT_ATTEMPTS 8
65
66 /**
67  * Prototype of a function called whenever two peers would be connected
68  * in a certain topology.
69  */
70 typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg, 
71                                                   unsigned int first,
72                                                   unsigned int second);
73
74
75 /**
76  * Context for handling churning a peer group
77  */
78 struct ChurnContext
79 {
80   /**
81    * Callback used to notify of churning finished
82    */
83   GNUNET_TESTING_NotifyCompletion cb;
84
85   /**
86    * Closure for callback
87    */
88   void *cb_cls;
89
90   /**
91    * Number of peers that still need to be started
92    */
93   unsigned int num_to_start;
94
95   /**
96    * Number of peers that still need to be stopped
97    */
98   unsigned int num_to_stop;
99  
100   /**
101    * Number of peers that failed to start
102    */
103   unsigned int num_failed_start;
104
105   /**
106    * Number of peers that failed to stop
107    */
108   unsigned int num_failed_stop;
109 };
110
111 struct RestartContext
112 {
113   /**
114    * The group of peers being restarted
115    */
116   struct GNUNET_TESTING_PeerGroup *peer_group;
117
118   /**
119    * How many peers have been restarted thus far
120    */
121   unsigned int peers_restarted;
122
123   /**
124    * How many peers got an error when restarting
125    */
126   unsigned int peers_restart_failed;
127
128   /**
129    * The function to call once all peers have been restarted
130    */
131   GNUNET_TESTING_NotifyCompletion callback;
132
133   /**
134    * Closure for callback function
135    */
136   void *callback_cls;
137
138 };
139
140
141 struct ShutdownContext
142 {
143   /**
144    * Total peers to wait for
145    */
146   unsigned int total_peers;
147
148   /**
149    * Number of peers successfully shut down
150    */
151   unsigned int peers_down;
152
153   /**
154    * Number of peers failed to shut down
155    */
156   unsigned int peers_failed;
157
158   /**
159    * Number of peers we have started shutting
160    * down.  If too many, wait on them.
161    */
162   unsigned int outstanding;
163
164   /**
165    * Timeout for shutdown.
166    */
167   struct GNUNET_TIME_Relative timeout;
168
169   /**
170    * Callback to call when all peers either
171    * shutdown or failed to shutdown
172    */
173   GNUNET_TESTING_NotifyCompletion cb;
174
175   /**
176    * Closure for cb
177    */
178   void *cb_cls;
179 };
180
181 /**
182  * Individual shutdown context for a particular peer.
183  */
184 struct PeerShutdownContext
185 {
186   /**
187    * Pointer to the high level shutdown context.
188    */
189   struct ShutdownContext *shutdown_ctx;
190
191   /**
192    * The daemon handle for the peer to shut down.
193    */
194   struct GNUNET_TESTING_Daemon *daemon;
195 };
196
197 /**
198  * Individual shutdown context for a particular peer.
199  */
200 struct PeerRestartContext
201 {
202   /**
203    * Pointer to the high level restart context.
204    */
205   struct ChurnRestartContext *churn_restart_ctx;
206
207   /**
208    * The daemon handle for the peer to shut down.
209    */
210   struct GNUNET_TESTING_Daemon *daemon;
211 };
212
213
214 struct CreateTopologyContext
215 {
216
217   /**
218    * Function to call with number of connections
219    */
220   GNUNET_TESTING_NotifyConnections cont;
221
222   /**
223    * Closure for connection notification
224    */
225   void *cls;
226 };
227
228 #if OLD
229 struct PeerConnection
230 {
231   /*
232    * Linked list
233    */
234   struct PeerConnection *next;
235
236   /*
237    * Pointer to daemon handle
238    */
239   struct GNUNET_TESTING_Daemon *daemon;
240
241 };
242 #endif
243
244 struct InternalStartContext
245 {
246   /**
247    * Pointer to peerdata
248    */
249   struct PeerData *peer;
250
251   /**
252    * Timeout for peer startup
253    */
254   struct GNUNET_TIME_Relative timeout;
255
256   /**
257    * Client callback for hostkey notification
258    */
259   GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
260
261   /**
262    * Closure for hostkey_callback
263    */
264   void *hostkey_cls;
265
266   /**
267    * Client callback for peer start notification
268    */
269   GNUNET_TESTING_NotifyDaemonRunning start_cb;
270
271   /**
272    * Closure for cb
273    */
274   void *start_cb_cls;
275
276   /**
277    * Hostname, where to start the peer
278    */
279   const char *hostname;
280
281   /**
282    * Username to use when connecting to the
283    * host via ssh.
284    */
285   const char *username;
286
287   /**
288    * Port to use for ssh.
289    */
290   uint16_t sshport;
291
292 };
293
294 struct ChurnRestartContext
295 {
296   /**
297    * Number of restarts currently in flight.
298    */
299   unsigned int outstanding;
300
301   /**
302    * Handle to the underlying churn context.
303    */
304   struct ChurnContext *churn_ctx;
305
306   /**
307    * How long to allow the operation to take.
308    */
309   struct GNUNET_TIME_Relative timeout;
310 };
311
312 /**
313  * Data we keep per peer.
314  */
315 struct PeerData
316 {
317   /**
318    * (Initial) configuration of the host.
319    * (initial because clients could change
320    *  it and we would not know about those
321    *  updates).
322    */
323   struct GNUNET_CONFIGURATION_Handle *cfg;
324
325   /**
326    * Handle for controlling the daemon.
327    */
328   struct GNUNET_TESTING_Daemon *daemon;
329
330   /**
331    * The peergroup this peer belongs to.
332    */
333   struct GNUNET_TESTING_PeerGroup *pg;
334
335   /**
336    * Hash map of allowed peer connections (F2F created topology)
337    */
338   struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
339
340   /**
341    * Hash map of blacklisted peers
342    */
343   struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
344
345   /**
346    * Hash map of peer connections
347    */
348   struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
349
350   /**
351    * Temporary hash map of peer connections
352    */
353   struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
354
355   /**
356    * Temporary variable for topology creation, should be reset before
357    * creating any topology so the count is valid once finished.
358    */
359   int num_connections;
360
361   /**
362    * Context to keep track of peers being started, to
363    * stagger hostkey generation and peer startup.
364    */
365   struct InternalStartContext internal_context;
366 };
367
368
369 /**
370  * Linked list of per-host data.
371  */
372 struct HostData
373 {
374   /**
375    * Name of the host.
376    */
377   char *hostname;
378
379   /**
380    * SSH username to use when connecting to this host.
381    */
382   char *username;
383
384   /**
385    * SSH port to use when connecting to this host.
386    */
387   uint16_t sshport;
388
389   /**
390    * Lowest port that we have not yet used
391    * for GNUnet.
392    */
393   uint16_t minport;
394 };
395
396 struct TopologyIterateContext
397 {
398   /**
399    * Callback for notifying of two connected peers.
400    */
401   GNUNET_TESTING_NotifyTopology topology_cb;
402
403   /**
404    * Closure for topology_cb
405    */
406   void *cls;
407
408   /**
409    * Number of peers currently connected to.
410    */
411   unsigned int connected;
412
413   /**
414    * Number of peers we have finished iterating.
415    */
416   unsigned int completed;
417
418   /**
419    * Number of peers total.
420    */
421   unsigned int total;
422 };
423
424 struct StatsIterateContext
425 {
426   /**
427    * Continuation to call once all stats information has been retrieved.
428    */
429   GNUNET_STATISTICS_Callback cont;
430
431   /**
432    * Proc function to call on each value received.
433    */
434   GNUNET_TESTING_STATISTICS_Iterator proc;
435
436   /**
437    * Closure for topology_cb
438    */
439   void *cls;
440
441   /**
442    * Number of peers currently connected to.
443    */
444   unsigned int connected;
445
446   /**
447    * Number of peers we have finished iterating.
448    */
449   unsigned int completed;
450
451   /**
452    * Number of peers total.
453    */
454   unsigned int total;
455 };
456
457 struct CoreContext
458 {
459   void *iter_context;
460   struct GNUNET_TESTING_Daemon *daemon;
461 };
462
463 struct StatsCoreContext
464 {
465   void *iter_context;
466   struct GNUNET_TESTING_Daemon *daemon;
467   /**
468    * Handle to the statistics service.
469    */
470   struct GNUNET_STATISTICS_Handle *stats_handle;
471
472   /**
473    * Handle for getting statistics.
474    */
475   struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
476 };
477
478 /**
479  * Handle to a group of GNUnet peers.
480  */
481 struct GNUNET_TESTING_PeerGroup
482 {
483   /**
484    * Our scheduler.
485    */
486   struct GNUNET_SCHEDULER_Handle *sched;
487
488   /**
489    * Configuration template.
490    */
491   const struct GNUNET_CONFIGURATION_Handle *cfg;
492
493   /**
494    * Function to call on each started daemon.
495    */
496   //GNUNET_TESTING_NotifyDaemonRunning cb;
497
498   /**
499    * Closure for cb.
500    */
501   //void *cb_cls;
502
503   /*
504    * Function to call on each topology connection created
505    */
506   GNUNET_TESTING_NotifyConnection notify_connection;
507
508   /*
509    * Callback for notify_connection
510    */
511   void *notify_connection_cls;
512
513   /**
514    * Array of information about hosts.
515    */
516   struct HostData *hosts;
517
518   /**
519    * Number of hosts (size of HostData)
520    */
521   unsigned int num_hosts;
522
523   /**
524    * Array of "total" peers.
525    */
526   struct PeerData *peers;
527
528   /**
529    * Number of peers in this group.
530    */
531   unsigned int total;
532
533   /**
534    * At what time should we fail the peer startup process?
535    */
536   struct GNUNET_TIME_Absolute max_timeout;
537
538   /**
539    * How many peers are being started right now?
540    */
541   unsigned int starting;
542
543   /**
544    * How many peers have already been started?
545    */
546   unsigned int started;
547 };
548
549 struct UpdateContext
550 {
551   struct GNUNET_CONFIGURATION_Handle *ret;
552   const struct GNUNET_CONFIGURATION_Handle *orig;
553   const char *hostname;
554   unsigned int nport;
555   unsigned int upnum;
556   unsigned int fdnum;
557 };
558
559
560 struct ConnectContext
561 {
562   struct GNUNET_TESTING_Daemon *first;
563
564   struct GNUNET_TESTING_Daemon *second;
565
566   struct GNUNET_TESTING_PeerGroup *pg;
567 };
568
569 /**
570  * Convert unique ID to hash code.
571  *
572  * @param uid unique ID to convert
573  * @param hash set to uid (extended with zeros)
574  */
575 static void
576 hash_from_uid (uint32_t uid,
577                GNUNET_HashCode *hash)
578 {
579   memset (hash, 0, sizeof(GNUNET_HashCode));
580   *((uint32_t*)hash) = uid;
581 }
582
583 /**
584  * Convert hash code to unique ID.
585  *
586  * @param uid unique ID to convert
587  * @param hash set to uid (extended with zeros)
588  */
589 static void
590 uid_from_hash (const GNUNET_HashCode *hash, uint32_t *uid)
591 {
592   memcpy (uid, hash, sizeof(uint32_t));
593 }
594
595 /**
596  * Number of connects we are waiting on, allows us to rate limit
597  * connect attempts.
598  */
599 static int outstanding_connects;
600
601 /**
602  * Get a topology from a string input.
603  *
604  * @param topology where to write the retrieved topology
605  * @param topology_string The string to attempt to
606  *        get a configuration value from
607  * @return GNUNET_YES if topology string matched a
608  *         known topology, GNUNET_NO if not
609  */
610 int
611 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
612 {
613   /**
614    * Strings representing topologies in enum
615    */
616   static const char * topology_strings[] =
617     {
618       /**
619        * A clique (everyone connected to everyone else).
620        */
621       "CLIQUE",
622       
623       /**
624        * Small-world network (2d torus plus random links).
625        */
626       "SMALL_WORLD",
627       
628       /**
629        * Small-world network (ring plus random links).
630        */
631       "SMALL_WORLD_RING",
632       
633       /**
634        * Ring topology.
635        */
636       "RING",
637       
638       /**
639        * 2-d torus.
640        */
641       "2D_TORUS",
642       
643       /**
644        * Random graph.
645        */
646       "ERDOS_RENYI",
647       
648       /**
649        * Certain percentage of peers are unable to communicate directly
650        * replicating NAT conditions
651        */
652       "INTERNAT",
653       
654       /**
655        * Scale free topology.
656        */
657       "SCALE_FREE",
658       
659       /**
660        * Straight line topology.
661        */
662       "LINE",
663       
664       /**
665        * All peers are disconnected.
666        */
667       "NONE",
668
669       NULL
670     };
671
672   int curr = 0;
673   if (topology_string == NULL)
674     return GNUNET_NO;
675   while (topology_strings[curr] != NULL)
676     {
677       if (strcasecmp(topology_strings[curr], topology_string) == 0)
678         {
679           *topology = curr;
680           return GNUNET_YES;
681         }
682       curr++;
683     }
684   *topology = GNUNET_TESTING_TOPOLOGY_NONE;
685   return GNUNET_NO;
686 }
687
688
689 /**
690  * Get connect topology option from string input.
691  *
692  * @param topology_option where to write the retrieved topology
693  * @param topology_string The string to attempt to
694  *        get a configuration value from
695  * @return GNUNET_YES if string matched a known
696  *         topology option, GNUNET_NO if not
697  */
698 int
699 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
700                                     char * topology_string)
701 {
702   /**
703    * Options for connecting a topology as strings.
704    */
705   static const char * topology_option_strings[] =
706     {
707       /**
708        * Try to connect all peers specified in the topology.
709        */
710       "CONNECT_ALL",
711       
712       /**
713        * Choose a random subset of connections to create.
714        */
715       "CONNECT_RANDOM_SUBSET",
716       
717       /**
718        * Create at least X connections for each peer.
719        */
720       "CONNECT_MINIMUM",
721       
722       /**
723        * Using a depth first search, create one connection
724        * per peer.  If any are missed (graph disconnected)
725        * start over at those peers until all have at least one
726        * connection.
727        */
728       "CONNECT_DFS",
729       
730       /**
731        * Find the N closest peers to each allowed peer in the
732        * topology and make sure a connection to those peers
733        * exists in the connect topology.
734        */
735       "CONNECT_CLOSEST",
736
737       /**
738        * No options specified.
739        */
740       "CONNECT_NONE",
741
742       NULL
743     };
744   int curr = 0;
745
746   if (topology_string == NULL)
747     return GNUNET_NO;
748   while (NULL != topology_option_strings[curr])
749     {
750       if (strcasecmp(topology_option_strings[curr], topology_string) == 0)
751         {
752           *topology_option = curr;
753           return GNUNET_YES;
754         }
755       curr++;
756     } 
757   *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
758   return GNUNET_NO;
759 }
760
761 /**
762  * Function to iterate over options.  Copies
763  * the options to the target configuration,
764  * updating PORT values as needed.
765  *
766  * @param cls closure
767  * @param section name of the section
768  * @param option name of the option
769  * @param value value of the option
770  */
771 static void
772 update_config (void *cls,
773                const char *section, const char *option, const char *value)
774 {
775   struct UpdateContext *ctx = cls;
776   unsigned int ival;
777   char cval[12];
778   char uval[128];
779   char *single_variable;
780   char *per_host_variable;
781   unsigned long long num_per_host;
782
783   if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
784     {
785       GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
786       if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
787         {
788           GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
789           value = cval;
790         }
791
792       GNUNET_free(single_variable);
793     }
794
795   if (0 == strcmp (option, "UNIXPATH"))
796     {
797       GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
798       GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
799       if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
800         {
801           GNUNET_snprintf (uval,
802                            sizeof (uval),
803                            "/tmp/test-service-%s-%u",
804                            section,
805                            ctx->upnum++);
806           value = uval;
807         }
808       else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
809         {
810           GNUNET_snprintf (uval,
811                            sizeof (uval),
812                            "/tmp/test-service-%s-%u",
813                            section,
814                            ctx->fdnum % num_per_host);
815           value = uval;
816         }
817       GNUNET_free(single_variable);
818       GNUNET_free(per_host_variable);
819
820     }
821
822   if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
823     {
824       value = ctx->hostname;
825     }
826
827   GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
828 }
829
830
831 /**
832  * Create a new configuration using the given configuration
833  * as a template; however, each PORT in the existing cfg
834  * must be renumbered by incrementing "*port".  If we run
835  * out of "*port" numbers, return NULL.
836  *
837  * @param cfg template configuration
838  * @param port port numbers to use, update to reflect
839  *             port numbers that were used
840  * @param upnum number to make unix domain socket names unique
841  * @param hostname hostname of the controlling host, to allow control connections from
842  * @param fdnum number used to offset the unix domain socket for grouped processes
843  *              (such as statistics or peerinfo, which can be shared among others)
844  *
845  * @return new configuration, NULL on error
846  */
847 static struct GNUNET_CONFIGURATION_Handle *
848 make_config (const struct GNUNET_CONFIGURATION_Handle *cfg, 
849              uint16_t * port,
850              uint32_t * upnum,
851              const char *hostname, uint32_t * fdnum)
852 {
853   struct UpdateContext uc;
854   uint16_t orig;
855   char *control_host;
856   char *allowed_hosts;
857
858   orig = *port;
859   uc.nport = *port;
860   uc.upnum = *upnum;
861   uc.fdnum = *fdnum;
862   uc.ret = GNUNET_CONFIGURATION_create ();
863   uc.hostname = hostname;
864   uc.orig = cfg;
865
866   GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
867   if (uc.nport >= HIGH_PORT)
868     {
869       *port = orig;
870       GNUNET_CONFIGURATION_destroy (uc.ret);
871       return NULL;
872     }
873
874   if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
875     {
876       if (hostname != NULL)
877         GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
878       else
879         GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
880
881       GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
882       GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
883       GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
884       GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
885       GNUNET_free_non_null(control_host);
886       GNUNET_free(allowed_hosts);
887     }
888
889
890   /* arm needs to know to allow connections from the host on which it is running,
891    * otherwise gnunet-arm is unable to connect to it in some instances */
892   if (hostname != NULL)
893     {
894       GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
895       GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
896       GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
897       GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
898       GNUNET_free(allowed_hosts);
899     }
900   else
901     {
902       GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
903       GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
904     }
905
906
907   *port = (uint16_t) uc.nport;
908   *upnum = uc.upnum;
909   uc.fdnum++;
910   *fdnum = uc.fdnum;
911   return uc.ret;
912 }
913
914
915 /*
916  * Add entries to the peers connect list
917  *
918  * @param pg the peer group we are working with
919  * @param first index of the first peer
920  * @param second index of the second peer
921  *
922  * @return the number of connections added
923  *         technically should only be 0 or 2
924  *
925  */
926 static int
927 add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
928 {
929   int added;
930   int add_first;
931   int add_second;
932
933   GNUNET_HashCode hash_first;
934   GNUNET_HashCode hash_second;
935
936   hash_from_uid(first, &hash_first);
937   hash_from_uid(second, &hash_second);
938
939   add_first = GNUNET_NO;
940   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].connect_peers, &hash_second))
941     {
942       add_first = GNUNET_YES;
943     }
944
945   add_second = GNUNET_NO;
946   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].connect_peers, &hash_first))
947     {
948       add_second = GNUNET_YES;
949     }
950
951   added = 0;
952   if (add_first)
953     {
954       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].connect_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
955       pg->peers[first].num_connections++;
956       added++;
957     }
958
959   if (add_second)
960     {
961       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].connect_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
962       pg->peers[second].num_connections++;
963       added++;
964     }
965
966   return added;
967 }
968
969
970 /*
971  * Add entries to the peers allowed connections list
972  *
973  * @param pg the peer group we are working with
974  * @param first index of the first peer
975  * @param second index of the second peer
976  *
977  * @return the number of connections added (can be 0, 1 or 2)
978  *         technically should only be 0 or 2, but the small price
979  *         of iterating over the lists (hashmaps in the future)
980  *         for being sure doesn't bother me!
981  *
982  */
983 static int
984 add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
985 {
986   int added;
987 #if OLD
988   struct PeerConnection *first_iter;
989   struct PeerConnection *second_iter;
990   struct PeerConnection *new_first;
991   struct PeerConnection *new_second;
992 #endif
993   int add_first;
994   int add_second;
995
996   GNUNET_HashCode hash_first;
997   GNUNET_HashCode hash_second;
998
999   hash_from_uid(first, &hash_first);
1000   hash_from_uid(second, &hash_second);
1001
1002   add_first = GNUNET_NO;
1003   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].allowed_peers, &hash_second))
1004     {
1005       add_first = GNUNET_YES;
1006     }
1007
1008   add_second = GNUNET_NO;
1009   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].allowed_peers, &hash_first))
1010     {
1011       add_second = GNUNET_YES;
1012     }
1013 #if OLD
1014   first_iter = pg->peers[first].connected_peers;
1015   while (first_iter != NULL)
1016     {
1017       if (first_iter->daemon == pg->peers[second].daemon)
1018         add_first = GNUNET_NO;
1019       first_iter = first_iter->next;
1020     }
1021
1022   second_iter = pg->peers[second].connected_peers;
1023   add_second = GNUNET_YES;
1024   while (second_iter != NULL)
1025     {
1026       if (second_iter->daemon == pg->peers[first].daemon)
1027         add_second = GNUNET_NO;
1028       second_iter = second_iter->next;
1029     }
1030 #endif
1031
1032   added = 0;
1033   if (add_first)
1034     {
1035       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].allowed_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1036 #if OLD
1037       new_first = GNUNET_malloc(sizeof(struct PeerConnection));
1038       new_first->daemon = pg->peers[second].daemon;
1039       new_first->next = pg->peers[first].connected_peers;
1040       pg->peers[first].connected_peers = new_first;
1041 #endif
1042       pg->peers[first].num_connections++;
1043       added++;
1044     }
1045
1046   if (add_second)
1047     {
1048       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].allowed_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1049 #if OLD
1050       new_second = GNUNET_malloc(sizeof(struct PeerConnection));
1051       new_second->daemon = pg->peers[first].daemon;
1052       new_second->next = pg->peers[second].connected_peers;
1053       pg->peers[second].connected_peers = new_second;
1054       pg->peers[first].num_connections++;
1055 #endif
1056       pg->peers[second].num_connections++;
1057       added++;
1058     }
1059
1060   return added;
1061 }
1062
1063 /*
1064  * Add entries to the peers blacklisted list
1065  *
1066  * @param pg the peer group we are working with
1067  * @param first index of the first peer
1068  * @param second index of the second peer
1069  *
1070  * @return the number of connections added (can be 0, 1 or 2)
1071  *
1072  */
1073 static int
1074 blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1075 {
1076   int added;
1077   int add_first;
1078   int add_second;
1079   GNUNET_HashCode hash_first;
1080   GNUNET_HashCode hash_second;
1081
1082   hash_from_uid(first, &hash_first);
1083   hash_from_uid(second, &hash_second);
1084
1085   add_first = GNUNET_NO;
1086   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second))
1087     {
1088       add_first = GNUNET_YES;
1089     }
1090
1091   add_second = GNUNET_NO;
1092   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first))
1093     {
1094       add_second = GNUNET_YES;
1095     }
1096
1097   added = 0;
1098   if (add_first)
1099     {
1100       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1101       pg->peers[first].num_connections++;
1102       added++;
1103     }
1104
1105   if (add_second)
1106     {
1107       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1108       pg->peers[second].num_connections++;
1109       added++;
1110     }
1111
1112   return added;
1113 }
1114
1115 /*
1116  * Remove entries from the peers blacklisted list
1117  *
1118  * @param pg the peer group we are working with
1119  * @param first index of the first peer
1120  * @param second index of the second peer
1121  *
1122  * @return the number of connections removed (can be 0, 1 or 2)
1123  *
1124  */
1125 static int
1126 unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
1127 {
1128   int removed;
1129   int remove_first;
1130   int remove_second;
1131   GNUNET_HashCode hash_first;
1132   GNUNET_HashCode hash_second;
1133
1134   hash_from_uid(first, &hash_first);
1135   hash_from_uid(second, &hash_second);
1136
1137   remove_first = GNUNET_CONTAINER_multihashmap_contains(pg->peers[first].blacklisted_peers, &hash_second);
1138   remove_second = GNUNET_CONTAINER_multihashmap_contains(pg->peers[second].blacklisted_peers, &hash_first);
1139
1140   removed = 0;
1141   if (remove_first)
1142     {
1143       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[first].blacklisted_peers, &hash_second, pg->peers[second].daemon));
1144       removed++;
1145     }
1146
1147   if (remove_second)
1148     {
1149       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[second].blacklisted_peers, &hash_first, pg->peers[first].daemon));
1150       removed++;
1151     }
1152
1153   return removed;
1154 }
1155
1156 /**
1157  * Scale free network construction as described in:
1158  *
1159  * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1160  *
1161  * Start with a network of "one" peer, then progressively add
1162  * peers up to the total number.  At each step, iterate over
1163  * all possible peers and connect new peer based on number of
1164  * existing connections of the target peer.
1165  *
1166  * @param pg the peer group we are dealing with
1167  * @param proc the connection processor to use
1168  *
1169  * @return the number of connections created
1170  */
1171 static int
1172 create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1173 {
1174
1175   unsigned int total_connections;
1176   unsigned int outer_count;
1177   unsigned int i;
1178   unsigned int previous_total_connections;
1179   double random;
1180   double probability;
1181
1182   GNUNET_assert(pg->total > 1);
1183
1184   /* Add a connection between the first two nodes */
1185   total_connections = proc(pg, 0, 1);
1186
1187   for (outer_count = 1; outer_count < pg->total; outer_count++)
1188     {
1189       previous_total_connections = total_connections;
1190       for (i = 0; i < outer_count; i++)
1191         {
1192           probability = pg->peers[i].num_connections / (double)previous_total_connections;
1193           random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1194                                                       UINT64_MAX)) / ( (double) UINT64_MAX);
1195 #if VERBOSE_TESTING
1196           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197                       "Considering connecting peer %d to peer %d\n",
1198                       outer_count, i);
1199 #endif
1200           if (random < probability)
1201             {
1202 #if VERBOSE_TESTING
1203               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204                           "Connecting peer %d to peer %d\n",
1205                           outer_count, i);
1206 #endif
1207               total_connections += proc(pg, outer_count, i);
1208             }
1209         }
1210     }
1211
1212   return total_connections;
1213 }
1214
1215 /**
1216  * Create a topology given a peer group (set of running peers)
1217  * and a connection processor.
1218  *
1219  * @param pg the peergroup to create the topology on
1220  * @param proc the connection processor to call to actually set
1221  *        up connections between two peers
1222  *
1223  * @return the number of connections that were set up
1224  *
1225  */
1226 int
1227 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1228 {
1229   unsigned int i, j;
1230   int nodeToConnect;
1231   unsigned int natLog;
1232   unsigned int randomPeer;
1233   double random, logNModifier, percentage;
1234   unsigned int smallWorldConnections;
1235   int connsPerPeer;
1236   char *p_string;
1237   int max;
1238   int min;
1239   unsigned int useAnd;
1240   int connect_attempts;
1241
1242   logNModifier = 0.5; /* FIXME: default value? */
1243   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1244                                                          "TESTING",
1245                                                          "LOGNMODIFIER",
1246                                                          &p_string))
1247     {
1248       if (sscanf(p_string, "%lf", &logNModifier) != 1)
1249         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1250                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1251                     p_string,
1252                     "LOGNMODIFIER",
1253                     "TESTING");
1254       GNUNET_free (p_string);
1255     }
1256   percentage = 0.5; /* FIXME: default percentage? */
1257   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1258                                                          "TESTING",
1259                                                          "PERCENTAGE",
1260                                                          &p_string))
1261     {
1262       if (sscanf(p_string, "%lf", &percentage) != 1)
1263         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1264                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1265                     p_string,
1266                     "PERCENTAGE",
1267                     "TESTING");
1268       GNUNET_free (p_string);
1269     }
1270   natLog = log (pg->total);
1271   connsPerPeer = ceil (natLog * logNModifier);
1272
1273   if (connsPerPeer % 2 == 1)
1274     connsPerPeer += 1;
1275
1276   smallWorldConnections = 0;
1277   connect_attempts = 0;
1278   for (i = 0; i < pg->total; i++)
1279     {
1280       useAnd = 0;
1281       max = i + connsPerPeer / 2;
1282       min = i - connsPerPeer / 2;
1283
1284       if (max > pg->total - 1)
1285         {
1286           max = max - pg->total;
1287           useAnd = 1;
1288         }
1289
1290       if (min < 0)
1291         {
1292           min = pg->total - 1 + min;
1293           useAnd = 1;
1294         }
1295
1296       for (j = 0; j < connsPerPeer / 2; j++)
1297         {
1298           random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1299                                                       UINT64_MAX) / ( (double) UINT64_MAX));
1300           if (random < percentage)
1301             {
1302               /* Connect to uniformly selected random peer */
1303               randomPeer =
1304                 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1305                                    pg->total);
1306               while ((((randomPeer < max) && (randomPeer > min))
1307                       && (useAnd == 0)) || (((randomPeer > min)
1308                                              || (randomPeer < max))
1309                                             && (useAnd == 1)))
1310                 {
1311                   randomPeer =
1312                       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1313                                                          pg->total);
1314                 }
1315               smallWorldConnections +=
1316                 proc (pg, i, randomPeer);
1317             }
1318           else
1319             {
1320               nodeToConnect = i + j + 1;
1321               if (nodeToConnect > pg->total - 1)
1322                 {
1323                   nodeToConnect = nodeToConnect - pg->total;
1324                 }
1325               connect_attempts +=
1326                 proc (pg, i, nodeToConnect);
1327             }
1328         }
1329
1330     }
1331
1332   connect_attempts += smallWorldConnections;
1333
1334   return connect_attempts;
1335 }
1336
1337 /**
1338  * Create a topology given a peer group (set of running peers)
1339  * and a connection processor.
1340  *
1341  * @param pg the peergroup to create the topology on
1342  * @param proc the connection processor to call to actually set
1343  *        up connections between two peers
1344  *
1345  * @return the number of connections that were set up
1346  *
1347  */
1348 static int
1349 create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1350 {
1351   unsigned int outer_count, inner_count;
1352   unsigned int cutoff;
1353   int connect_attempts;
1354   double nat_percentage;
1355   char *p_string;
1356
1357   nat_percentage = 0.6; /* FIXME: default percentage? */
1358   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1359                                                          "TESTING",
1360                                                          "NATPERCENTAGE",
1361                                                          &p_string))
1362     {
1363       if (sscanf(p_string, "%lf", &nat_percentage) != 1)
1364         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1365                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1366                     p_string,
1367                     "NATPERCENTAGE",
1368                     "TESTING");
1369       GNUNET_free (p_string);
1370     }
1371
1372
1373
1374   cutoff = (unsigned int) (nat_percentage * pg->total);
1375
1376   connect_attempts = 0;
1377
1378   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1379     {
1380       for (inner_count = outer_count + 1; inner_count < pg->total;
1381            inner_count++)
1382         {
1383           if ((outer_count > cutoff) || (inner_count > cutoff))
1384             {
1385 #if VERBOSE_TESTING
1386               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387                           "Connecting peer %d to peer %d\n",
1388                           outer_count, inner_count);
1389 #endif
1390               connect_attempts += proc(pg, outer_count, inner_count);
1391             }
1392         }
1393     }
1394
1395   return connect_attempts;
1396
1397 }
1398
1399 /**
1400  * Create a topology given a peer group (set of running peers)
1401  * and a connection processor.
1402  *
1403  * @param pg the peergroup to create the topology on
1404  * @param proc the connection processor to call to actually set
1405  *        up connections between two peers
1406  *
1407  * @return the number of connections that were set up
1408  *
1409  */
1410 static int
1411 create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1412 {
1413   unsigned int i, j, k;
1414   unsigned int square;
1415   unsigned int rows;
1416   unsigned int cols;
1417   unsigned int toggle = 1;
1418   unsigned int nodeToConnect;
1419   unsigned int natLog;
1420   unsigned int node1Row;
1421   unsigned int node1Col;
1422   unsigned int node2Row;
1423   unsigned int node2Col;
1424   unsigned int distance;
1425   double probability, random, percentage;
1426   unsigned int smallWorldConnections;
1427   unsigned int small_world_it;
1428   char *p_string;
1429   int connect_attempts;
1430   square = floor (sqrt (pg->total));
1431   rows = square;
1432   cols = square;
1433
1434   percentage = 0.5; /* FIXME: default percentage? */
1435   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1436                                                          "TESTING",
1437                                                          "PERCENTAGE",
1438                                                          &p_string))
1439     {
1440       if (sscanf(p_string, "%lf", &percentage) != 1)
1441         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1442                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1443                     p_string,
1444                     "PERCENTAGE",
1445                     "TESTING");
1446       GNUNET_free (p_string);
1447     }
1448   if (percentage < 0.0)
1449     {
1450       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1451                   _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
1452                   "PERCENTAGE", "TESTING", percentage);
1453       percentage = 0.5;
1454     }
1455   probability = 0.5; /* FIXME: default percentage? */
1456   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1457                                                          "TESTING",
1458                                                          "PROBABILITY",
1459                                                          &p_string))
1460     {
1461       if (sscanf(p_string, "%lf", &probability) != 1)
1462         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1463                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1464                     p_string,
1465                     "PROBABILITY",
1466                     "TESTING");
1467       GNUNET_free (p_string);
1468     }
1469   if (square * square != pg->total)
1470     {
1471       while (rows * cols < pg->total)
1472         {
1473           if (toggle % 2 == 0)
1474             rows++;
1475           else
1476             cols++;
1477
1478           toggle++;
1479         }
1480     }
1481 #if VERBOSE_TESTING
1482       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1483                   _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1484                   rows, cols);
1485 #endif
1486
1487   connect_attempts = 0;
1488   /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1489    * to the node to its right and above.  Once this is over, we'll have our torus!
1490    * Special case for the last node (if the rows and columns are not equal), connect
1491    * to the first in the row to maintain topology.
1492    */
1493   for (i = 0; i < pg->total; i++)
1494     {
1495       /* First connect to the node to the right */
1496       if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1497         nodeToConnect = i + 1;
1498       else if (i + 1 == pg->total)
1499         nodeToConnect = rows * cols - cols;
1500       else
1501         nodeToConnect = i - cols + 1;
1502
1503       connect_attempts += proc (pg, i, nodeToConnect);
1504
1505       if (i < cols)
1506         nodeToConnect = (rows * cols) - cols + i;
1507       else
1508         nodeToConnect = i - cols;
1509
1510       if (nodeToConnect < pg->total)
1511         connect_attempts += proc (pg, i, nodeToConnect);
1512     }
1513   natLog = log (pg->total);
1514 #if VERBOSE_TESTING > 2
1515   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516               _("natural log of %d is %d, will run %d iterations\n"),
1517              pg->total, natLog, (int) (natLog * percentage));
1518   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
1519 #endif
1520   smallWorldConnections = 0;
1521   small_world_it = (unsigned int)(natLog * percentage);
1522   GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
1523   for (i = 0; i < small_world_it; i++)
1524     {
1525       for (j = 0; j < pg->total; j++)
1526         {
1527           /* Determine the row and column of node at position j on the 2d torus */
1528           node1Row = j / cols;
1529           node1Col = j - (node1Row * cols);
1530           for (k = 0; k < pg->total; k++)
1531             {
1532               /* Determine the row and column of node at position k on the 2d torus */
1533               node2Row = k / cols;
1534               node2Col = k - (node2Row * cols);
1535               /* Simple Cartesian distance */
1536               distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
1537               if (distance > 1)
1538                 {
1539                   /* Calculate probability as 1 over the square of the distance */
1540                   probability = 1.0 / (distance * distance);
1541                   /* Choose a random value between 0 and 1 */
1542                   random = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1543                                                               UINT64_MAX)) / ( (double) UINT64_MAX);
1544                   /* If random < probability, then connect the two nodes */
1545                   if (random < probability)
1546                     smallWorldConnections += proc (pg, j, k);
1547
1548                 }
1549             }
1550         }
1551     }
1552   connect_attempts += smallWorldConnections;
1553 #if VERBOSE_TESTING > 2
1554           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555                       _("Total connections added for small world: %d!\n"),
1556                       smallWorldConnections);
1557 #endif
1558   return connect_attempts;
1559 }
1560
1561 /**
1562  * Create a topology given a peer group (set of running peers)
1563  * and a connection processor.
1564  *
1565  * @param pg the peergroup to create the topology on
1566  * @param proc the connection processor to call to actually set
1567  *        up connections between two peers
1568  *
1569  * @return the number of connections that were set up
1570  *
1571  */
1572 static int
1573 create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1574 {
1575   double temp_rand;
1576   unsigned int outer_count;
1577   unsigned int inner_count;
1578   int connect_attempts;
1579   double probability;
1580   char *p_string;
1581
1582   probability = 0.5; /* FIXME: default percentage? */
1583   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
1584                                                          "TESTING",
1585                                                          "PROBABILITY",
1586                                                          &p_string))
1587     {
1588       if (sscanf(p_string, "%lf", &probability) != 1)
1589         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1590                     _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1591                     p_string,
1592                     "PROBABILITY",
1593                     "TESTING");
1594       GNUNET_free (p_string);
1595     }
1596   connect_attempts = 0;
1597   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1598     {
1599       for (inner_count = outer_count + 1; inner_count < pg->total;
1600            inner_count++)
1601         {
1602           temp_rand = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
1603                                                          UINT64_MAX)) / ( (double) UINT64_MAX);
1604 #if VERBOSE_TESTING
1605           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606                       _("rand is %f probability is %f\n"), temp_rand,
1607                       probability);
1608 #endif
1609           if (temp_rand < probability)
1610             {
1611               connect_attempts += proc (pg, outer_count, inner_count);
1612             }
1613         }
1614     }
1615
1616   return connect_attempts;
1617 }
1618
1619 /**
1620  * Create a topology given a peer group (set of running peers)
1621  * and a connection processor.  This particular function creates
1622  * the connections for a 2d-torus, plus additional "closest"
1623  * connections per peer.
1624  *
1625  * @param pg the peergroup to create the topology on
1626  * @param proc the connection processor to call to actually set
1627  *        up connections between two peers
1628  *
1629  * @return the number of connections that were set up
1630  *
1631  */
1632 static int
1633 create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1634 {
1635   unsigned int i;
1636   unsigned int square;
1637   unsigned int rows;
1638   unsigned int cols;
1639   unsigned int toggle = 1;
1640   unsigned int nodeToConnect;
1641   int connect_attempts;
1642
1643   connect_attempts = 0;
1644
1645   square = floor (sqrt (pg->total));
1646   rows = square;
1647   cols = square;
1648
1649   if (square * square != pg->total)
1650     {
1651       while (rows * cols < pg->total)
1652         {
1653           if (toggle % 2 == 0)
1654             rows++;
1655           else
1656             cols++;
1657
1658           toggle++;
1659         }
1660     }
1661 #if VERBOSE_TESTING
1662       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663                   _("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
1664                   rows, cols);
1665 #endif
1666   /* Rows and columns are all sorted out, now iterate over all nodes and connect each
1667    * to the node to its right and above.  Once this is over, we'll have our torus!
1668    * Special case for the last node (if the rows and columns are not equal), connect
1669    * to the first in the row to maintain topology.
1670    */
1671   for (i = 0; i < pg->total; i++)
1672     {
1673       /* First connect to the node to the right */
1674       if (((i + 1) % cols != 0) && (i + 1 != pg->total))
1675         nodeToConnect = i + 1;
1676       else if (i + 1 == pg->total)
1677         nodeToConnect = rows * cols - cols;
1678       else
1679         nodeToConnect = i - cols + 1;
1680 #if VERBOSE_TESTING
1681           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1682                       "Connecting peer %d to peer %d\n",
1683                       i, nodeToConnect);
1684 #endif
1685       connect_attempts += proc(pg, i, nodeToConnect);
1686
1687       /* Second connect to the node immediately above */
1688       if (i < cols)
1689         nodeToConnect = (rows * cols) - cols + i;
1690       else
1691         nodeToConnect = i - cols;
1692
1693       if (nodeToConnect < pg->total)
1694         {
1695 #if VERBOSE_TESTING
1696           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697                       "Connecting peer %d to peer %d\n",
1698                       i, nodeToConnect);
1699 #endif
1700           connect_attempts += proc(pg, i, nodeToConnect);
1701         }
1702
1703     }
1704
1705   return connect_attempts;
1706 }
1707
1708
1709 /**
1710  * Create a topology given a peer group (set of running peers)
1711  * and a connection processor.
1712  *
1713  * @param pg the peergroup to create the topology on
1714  * @param proc the connection processor to call to actually set
1715  *        up connections between two peers
1716  *
1717  * @return the number of connections that were set up
1718  *
1719  */
1720 static int
1721 create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1722 {
1723   unsigned int outer_count;
1724   unsigned int inner_count;
1725   int connect_attempts;
1726
1727   connect_attempts = 0;
1728
1729   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1730     {
1731       for (inner_count = outer_count + 1; inner_count < pg->total;
1732            inner_count++)
1733         {
1734 #if VERBOSE_TESTING
1735           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736                       "Connecting peer %d to peer %d\n",
1737                       outer_count, inner_count);
1738 #endif
1739           connect_attempts += proc(pg, outer_count, inner_count);
1740         }
1741     }
1742
1743   return connect_attempts;
1744 }
1745
1746 /**
1747  * Create a topology given a peer group (set of running peers)
1748  * and a connection processor.
1749  *
1750  * @param pg the peergroup to create the topology on
1751  * @param proc the connection processor to call to actually set
1752  *        up connections between two peers
1753  *
1754  * @return the number of connections that were set up
1755  *
1756  */
1757 static int
1758 create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1759 {
1760   unsigned int count;
1761   int connect_attempts;
1762
1763   connect_attempts = 0;
1764
1765   /* Connect each peer to the next highest numbered peer */
1766   for (count = 0; count < pg->total - 1; count++)
1767     {
1768 #if VERBOSE_TESTING
1769           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770                       "Connecting peer %d to peer %d\n",
1771                       count, count + 1);
1772 #endif
1773       connect_attempts += proc(pg, count, count + 1);
1774     }
1775
1776   return connect_attempts;
1777 }
1778
1779 /**
1780  * Create a topology given a peer group (set of running peers)
1781  * and a connection processor.
1782  *
1783  * @param pg the peergroup to create the topology on
1784  * @param proc the connection processor to call to actually set
1785  *        up connections between two peers
1786  *
1787  * @return the number of connections that were set up
1788  *
1789  */
1790 static int
1791 create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
1792 {
1793   unsigned int count;
1794   int connect_attempts;
1795
1796   connect_attempts = 0;
1797
1798   /* Connect each peer to the next highest numbered peer */
1799   for (count = 0; count < pg->total - 1; count++)
1800     {
1801 #if VERBOSE_TESTING
1802           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803                       "Connecting peer %d to peer %d\n",
1804                       count, count + 1);
1805 #endif
1806       connect_attempts += proc(pg, count, count + 1);
1807     }
1808
1809   /* Connect the last peer to the first peer */
1810   connect_attempts += proc(pg, pg->total - 1, 0);
1811
1812   return connect_attempts;
1813 }
1814
1815
1816 /**
1817  * Iterator for writing friends of a peer to a file.
1818  *
1819  * @param cls closure, an open writable file handle
1820  * @param key the key the daemon was stored under
1821  * @param value the GNUNET_TESTING_Daemon that needs to be written.
1822  *
1823  * @return GNUNET_YES to continue iteration
1824  *
1825  * TODO: Could replace friend_file_iterator and blacklist_file_iterator
1826  *       with a single file_iterator that takes a closure which contains
1827  *       the prefix to write before the peer.  Then this could be used
1828  *       for blacklisting multiple transports and writing the friend
1829  *       file.  I'm sure *someone* will complain loudly about other
1830  *       things that negate these functions even existing so no point in
1831  *       "fixing" now.
1832  */
1833 static int
1834 friend_file_iterator (void *cls,
1835                   const GNUNET_HashCode * key,
1836                   void *value)
1837 {
1838   FILE *temp_friend_handle = cls;
1839   struct GNUNET_TESTING_Daemon *peer = value;
1840   struct GNUNET_PeerIdentity *temppeer;
1841   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1842
1843   temppeer = &peer->id;
1844   GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1845   fprintf(temp_friend_handle, "%s\n", (char *)&peer_enc);
1846
1847   return GNUNET_YES;
1848 }
1849
1850 struct BlacklistContext
1851 {
1852   /*
1853    * The (open) file handle to write to
1854    */
1855   FILE *temp_file_handle;
1856
1857   /*
1858    * The transport that this peer will be blacklisted on.
1859    */
1860   char *transport;
1861 };
1862
1863 /**
1864  * Iterator for writing blacklist data to appropriate files.
1865  *
1866  * @param cls closure, an open writable file handle
1867  * @param key the key the daemon was stored under
1868  * @param value the GNUNET_TESTING_Daemon that needs to be written.
1869  *
1870  * @return GNUNET_YES to continue iteration
1871  */
1872 static int
1873 blacklist_file_iterator (void *cls,
1874                          const GNUNET_HashCode * key,
1875                          void *value)
1876 {
1877   struct BlacklistContext *blacklist_ctx = cls;
1878   //FILE *temp_blacklist_handle = cls;
1879   struct GNUNET_TESTING_Daemon *peer = value;
1880   struct GNUNET_PeerIdentity *temppeer;
1881   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
1882
1883   temppeer = &peer->id;
1884   GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
1885   fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
1886
1887   return GNUNET_YES;
1888 }
1889
1890 /*
1891  * Create the friend files based on the PeerConnection's
1892  * of each peer in the peer group, and copy the files
1893  * to the appropriate place
1894  *
1895  * @param pg the peer group we are dealing with
1896  */
1897 static int
1898 create_and_copy_friend_files (struct GNUNET_TESTING_PeerGroup *pg)
1899 {
1900   FILE *temp_friend_handle;
1901   unsigned int pg_iter;
1902   char *temp_service_path;
1903   pid_t *pidarr;
1904   char *arg;
1905   char * mytemp;
1906   enum GNUNET_OS_ProcessStatusType type;
1907   unsigned long return_code;
1908   int count;
1909   int ret;
1910   int max_wait = 10;
1911
1912   pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
1913   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1914     {
1915       mytemp = GNUNET_DISK_mktemp("friends");
1916       GNUNET_assert(mytemp != NULL);
1917       temp_friend_handle = fopen (mytemp, "wt");
1918       GNUNET_assert(temp_friend_handle != NULL);
1919       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &friend_file_iterator, temp_friend_handle);
1920       fclose(temp_friend_handle);
1921
1922       if (GNUNET_OK !=
1923           GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
1924         {
1925           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1926                       _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
1927                       "SERVICEHOME",
1928                       "PATHS");
1929           if (UNLINK (mytemp) != 0)
1930             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
1931           GNUNET_free (mytemp);
1932           break;
1933         }
1934
1935       if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
1936         {
1937           GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
1938           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
1939                                          "mv", mytemp, arg, NULL);
1940 #if VERBOSE_TESTING
1941           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942                       _("Copying file with command cp %s %s\n"), mytemp, arg);
1943 #endif
1944
1945           GNUNET_free(arg);
1946         }
1947       else /* Remote, scp the file to the correct place */
1948         {
1949           if (NULL != pg->peers[pg_iter].daemon->username)
1950             GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
1951           else
1952             GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
1953           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
1954                                          "scp", mytemp, arg, NULL);
1955
1956 #if VERBOSE_TESTING
1957           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958                       _("Copying file with command scp %s %s\n"), mytemp, arg);
1959 #endif
1960           GNUNET_free(arg);
1961         }
1962       GNUNET_free (temp_service_path);
1963       GNUNET_free (mytemp);
1964     }
1965
1966   count = 0;
1967   ret = GNUNET_SYSERR;
1968   while ((count < max_wait) && (ret != GNUNET_OK))
1969     {
1970       ret = GNUNET_OK;
1971       for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
1972         {
1973 #if VERBOSE_TESTING
1974           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975                       _("Checking copy status of file %d\n"), pg_iter);
1976 #endif
1977           if (pidarr[pg_iter] != 0) /* Check for already completed! */
1978             {
1979               if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
1980                 {
1981                   ret = GNUNET_SYSERR;
1982                 }
1983               else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
1984                 {
1985                   ret = GNUNET_SYSERR;
1986                 }
1987               else
1988                 {
1989                   pidarr[pg_iter] = 0;
1990 #if VERBOSE_TESTING
1991             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992                       _("File %d copied\n"), pg_iter);
1993 #endif
1994                 }
1995             }
1996         }
1997       count++;
1998       if (ret == GNUNET_SYSERR)
1999         {
2000           /* FIXME: why sleep here? -CG */
2001           sleep(1);
2002         }
2003     }
2004
2005 #if VERBOSE_TESTING
2006     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007                 _("Finished copying all friend files!\n"));
2008 #endif
2009   GNUNET_free(pidarr);
2010   return ret;
2011 }
2012
2013
2014 /*
2015  * Create the blacklist files based on the PeerConnection's
2016  * of each peer in the peer group, and copy the files
2017  * to the appropriate place.
2018  *
2019  * @param pg the peer group we are dealing with
2020  * @param transports space delimited list of transports to blacklist
2021  */
2022 static int
2023 create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
2024 {
2025   FILE *temp_file_handle;
2026   static struct BlacklistContext blacklist_ctx;
2027   unsigned int pg_iter;
2028   char *temp_service_path;
2029   pid_t *pidarr;
2030   char *arg;
2031   char *mytemp;
2032   enum GNUNET_OS_ProcessStatusType type;
2033   unsigned long return_code;
2034   int count;
2035   int ret;
2036   int max_wait = 10;
2037   int transport_len;
2038   unsigned int i;
2039   char *pos;
2040   char *temp_transports;
2041
2042   pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
2043   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2044     {
2045       mytemp = GNUNET_DISK_mktemp("blacklist");
2046       GNUNET_assert(mytemp != NULL);
2047       temp_file_handle = fopen (mytemp, "wt");
2048       GNUNET_assert(temp_file_handle != NULL);
2049       temp_transports = GNUNET_strdup(transports);
2050       blacklist_ctx.temp_file_handle = temp_file_handle;
2051       transport_len = strlen(temp_transports) + 1;
2052       pos = NULL;
2053
2054       for (i = 0; i < transport_len; i++)
2055       {
2056         if ((temp_transports[i] == ' ') && (pos == NULL))
2057           continue; /* At start of string (whitespace) */
2058         else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2059         {
2060           temp_transports[i] = '\0';
2061           blacklist_ctx.transport = pos;
2062           GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
2063           pos = NULL;
2064         } /* At beginning of actual string */
2065         else if (pos == NULL)
2066         {
2067           pos = &temp_transports[i];
2068         }
2069       }
2070
2071       GNUNET_free (temp_transports);
2072       fclose(temp_file_handle);
2073
2074       if (GNUNET_OK !=
2075           GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
2076         {
2077           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2078                       _("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2079                       "SERVICEHOME",
2080                       "PATHS");
2081           if (UNLINK (mytemp) != 0)
2082             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", mytemp);
2083           GNUNET_free (mytemp);
2084           break;
2085         }
2086
2087       if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2088         {
2089           GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
2090           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
2091                                          "mv", mytemp, arg, NULL);
2092 #if VERBOSE_TESTING
2093           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094                       _("Copying file with command cp %s %s\n"), mytemp, arg);
2095 #endif
2096
2097           GNUNET_free(arg);
2098         }
2099       else /* Remote, scp the file to the correct place */
2100         {
2101           if (NULL != pg->peers[pg_iter].daemon->username)
2102             GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
2103           else
2104             GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
2105           pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
2106                                          "scp", mytemp, arg, NULL);
2107
2108 #if VERBOSE_TESTING
2109           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110                       _("Copying file with command scp %s %s\n"), mytemp, arg);
2111 #endif
2112           GNUNET_free(arg);
2113         }
2114       GNUNET_free (temp_service_path);
2115       GNUNET_free (mytemp);
2116     }
2117
2118   count = 0;
2119   ret = GNUNET_SYSERR;
2120   while ((count < max_wait) && (ret != GNUNET_OK))
2121     {
2122       ret = GNUNET_OK;
2123       for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2124         {
2125 #if VERBOSE_TESTING
2126           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127                       _("Checking copy status of file %d\n"), pg_iter);
2128 #endif
2129           if (pidarr[pg_iter] != 0) /* Check for already completed! */
2130             {
2131               if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
2132                 {
2133                   ret = GNUNET_SYSERR;
2134                 }
2135               else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
2136                 {
2137                   ret = GNUNET_SYSERR;
2138                 }
2139               else
2140                 {
2141                   pidarr[pg_iter] = 0;
2142 #if VERBOSE_TESTING
2143             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144                       _("File %d copied\n"), pg_iter);
2145 #endif
2146                 }
2147             }
2148         }
2149       count++;
2150       if (ret == GNUNET_SYSERR)
2151         {
2152           /* FIXME: why sleep here? -CG */
2153           sleep(1);
2154         }
2155     }
2156
2157 #if VERBOSE_TESTING
2158     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159                 _("Finished copying all blacklist files!\n"));
2160 #endif
2161   GNUNET_free(pidarr);
2162   return ret;
2163 }
2164
2165
2166 /**
2167  * Internal notification of a connection, kept so that we can ensure some connections
2168  * happen instead of flooding all testing daemons with requests to connect.
2169  */
2170 static void internal_connect_notify (void *cls,
2171                                      const struct GNUNET_PeerIdentity *first,
2172                                      const struct GNUNET_PeerIdentity *second,
2173                                      uint32_t distance,
2174                                      const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2175                                      const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2176                                      struct GNUNET_TESTING_Daemon *first_daemon,
2177                                      struct GNUNET_TESTING_Daemon *second_daemon,
2178                                      const char *emsg)
2179 {
2180   struct GNUNET_TESTING_PeerGroup *pg = cls;
2181   outstanding_connects--;
2182
2183   pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
2184 }
2185
2186
2187 /**
2188  * Either delay a connection (because there are too many outstanding)
2189  * or schedule it for right now.
2190  *
2191  * @param cls a connection context
2192  * @param tc the task runtime context
2193  */
2194 static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2195 {
2196   struct ConnectContext *connect_context = cls;
2197
2198   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2199     return;
2200
2201   if (outstanding_connects > MAX_OUTSTANDING_CONNECTIONS)
2202     {
2203 #if VERBOSE_TESTING > 2
2204           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205                       _("Delaying connect, we have too many outstanding connections!\n"));
2206 #endif
2207       GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
2208     }
2209   else
2210     {
2211 #if VERBOSE_TESTING > 2
2212           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2213                       _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
2214 #endif
2215       outstanding_connects++;
2216       GNUNET_TESTING_daemons_connect (connect_context->first,
2217                                       connect_context->second,
2218                                       CONNECT_TIMEOUT,
2219                                       CONNECT_ATTEMPTS,
2220                                       &internal_connect_notify,
2221                                       connect_context->pg);
2222       GNUNET_free(connect_context);
2223     }
2224 }
2225
2226
2227 /**
2228  * Iterator for actually scheduling connections to be created
2229  * between two peers.
2230  *
2231  * @param cls closure, a GNUNET_TESTING_Daemon
2232  * @param key the key the second Daemon was stored under
2233  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2234  *
2235  * @return GNUNET_YES to continue iteration
2236  */
2237 static int
2238 connect_iterator (void *cls,
2239                   const GNUNET_HashCode * key,
2240                   void *value)
2241 {
2242   struct PeerData *first = cls;
2243   struct GNUNET_TESTING_Daemon *second = value;
2244   struct ConnectContext *connect_context;
2245
2246   connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2247   connect_context->pg = first->pg;
2248   connect_context->first = first->daemon;
2249   connect_context->second = second;
2250   GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
2251
2252   return GNUNET_YES;
2253 }
2254
2255
2256 /**
2257  * Iterator for copying all entries in the allowed hashmap to the
2258  * connect hashmap.
2259  *
2260  * @param cls closure, a GNUNET_TESTING_Daemon
2261  * @param key the key the second Daemon was stored under
2262  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2263  *
2264  * @return GNUNET_YES to continue iteration
2265  */
2266 static int
2267 copy_topology_iterator (void *cls,
2268                   const GNUNET_HashCode * key,
2269                   void *value)
2270 {
2271   struct PeerData *first = cls;
2272
2273   GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(first->connect_peers, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2274
2275   return GNUNET_YES;
2276 }
2277
2278 /**
2279  * Make the peers to connect the same as those that are allowed to be
2280  * connected.
2281  *
2282  * @param pg the peer group
2283  */
2284 static int
2285 copy_allowed_topology (struct GNUNET_TESTING_PeerGroup *pg)
2286 {
2287   unsigned int pg_iter;
2288   int ret;
2289   int total;
2290
2291   total = 0;
2292   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2293     {
2294       ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &copy_topology_iterator, &pg->peers[pg_iter]);
2295       if (GNUNET_SYSERR == ret)
2296         return GNUNET_SYSERR;
2297
2298       total = total + ret;
2299     }
2300
2301   return total;
2302 }
2303
2304
2305 /**
2306  * Connect the topology as specified by the PeerConnection's
2307  * of each peer in the peer group
2308  *
2309  * @param pg the peer group we are dealing with
2310  * @return the number of connections that will be attempted
2311  */
2312 static int
2313 connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
2314 {
2315   unsigned int pg_iter;
2316   int ret;
2317   int total;
2318 #if OLD
2319   struct PeerConnection *connection_iter;
2320   struct ConnectContext *connect_context;
2321 #endif
2322
2323   total = 0;
2324   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2325     {
2326       ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
2327       if (GNUNET_SYSERR == ret)
2328         return GNUNET_SYSERR;
2329
2330       total = total + ret;
2331
2332 #if OLD
2333       connection_iter = ;
2334       while (connection_iter != NULL)
2335         {
2336           connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
2337           connect_context->pg = pg;
2338           connect_context->first = ;
2339           connect_context->second = connection_iter->daemon;
2340           GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
2341           connection_iter = connection_iter->next;
2342         }
2343 #endif
2344     }
2345   return total;
2346 }
2347
2348
2349 /**
2350  * Takes a peer group and creates a topology based on the
2351  * one specified.  Creates a topology means generates friend
2352  * files for the peers so they can only connect to those allowed
2353  * by the topology.  This will only have an effect once peers
2354  * are started if the FRIENDS_ONLY option is set in the base
2355  * config.  Also takes an optional restrict topology which
2356  * disallows connections based on a particular transport
2357  * UNLESS they are specified in the restricted topology.
2358  *
2359  * @param pg the peer group struct representing the running peers
2360  * @param topology which topology to connect the peers in
2361  * @param restrict_topology allow only direct TCP connections in this topology
2362  *                          use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
2363  * @param restrict_transports space delimited list of transports to blacklist
2364  *                            to create restricted topology
2365  *
2366  * @return the maximum number of connections were all allowed peers
2367  *         connected to each other
2368  */
2369 int
2370 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
2371                                 enum GNUNET_TESTING_Topology topology,
2372                                 enum GNUNET_TESTING_Topology restrict_topology,
2373                                 char *restrict_transports)
2374 {
2375   int ret;
2376   int num_connections;
2377   int unblacklisted_connections;
2378
2379   GNUNET_assert (pg->notify_connection != NULL);
2380
2381   switch (topology)
2382     {
2383     case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2384 #if VERBOSE_TESTING
2385       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2386                   _("Creating clique topology\n"));
2387 #endif
2388       num_connections = create_clique (pg, &add_allowed_connections);
2389       break;
2390     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2391 #if VERBOSE_TESTING
2392       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393                   _("Creating small world (ring) topology\n"));
2394 #endif
2395       num_connections = create_small_world_ring (pg, &add_allowed_connections);
2396       break;
2397     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2398 #if VERBOSE_TESTING
2399       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400                   _("Creating small world (2d-torus) topology\n"));
2401 #endif
2402       num_connections = create_small_world (pg, &add_allowed_connections);
2403       break;
2404     case GNUNET_TESTING_TOPOLOGY_RING:
2405 #if VERBOSE_TESTING
2406       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2407                   _("Creating ring topology\n"));
2408 #endif
2409       num_connections = create_ring (pg, &add_allowed_connections);
2410       break;
2411     case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2412 #if VERBOSE_TESTING
2413       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2414                   _("Creating 2d torus topology\n"));
2415 #endif
2416       num_connections = create_2d_torus (pg, &add_allowed_connections);
2417       break;
2418     case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2419 #if VERBOSE_TESTING
2420       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2421                   _("Creating Erdos-Renyi topology\n"));
2422 #endif
2423       num_connections = create_erdos_renyi (pg, &add_allowed_connections);
2424       break;
2425     case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2426 #if VERBOSE_TESTING
2427       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428                   _("Creating InterNAT topology\n"));
2429 #endif
2430       num_connections = create_nated_internet (pg, &add_allowed_connections);
2431       break;
2432     case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2433 #if VERBOSE_TESTING
2434       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2435                   _("Creating Scale Free topology\n"));
2436 #endif
2437       num_connections = create_scale_free (pg, &add_allowed_connections);
2438       break;
2439     case GNUNET_TESTING_TOPOLOGY_LINE:
2440 #if VERBOSE_TESTING
2441       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442                   _("Creating straight line topology\n"));
2443 #endif
2444       num_connections = create_line (pg, &add_allowed_connections);
2445       break;
2446     case GNUNET_TESTING_TOPOLOGY_NONE:
2447 #if VERBOSE_TESTING
2448       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2449                   _("Creating no allowed topology (all peers can connect at core level)\n"));
2450 #endif
2451       num_connections = 0;
2452       break;
2453     default:
2454       num_connections = 0;
2455       break;
2456     }
2457
2458   if (num_connections < 0)
2459     return GNUNET_SYSERR;
2460
2461   if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pg->cfg, "TESTING", "F2F"))
2462     {
2463       ret = create_and_copy_friend_files(pg);
2464       if (ret != GNUNET_OK)
2465         {
2466 #if VERBOSE_TESTING
2467           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468                       _("Failed during friend file copying!\n"));
2469 #endif
2470           return GNUNET_SYSERR;
2471         }
2472       else
2473         {
2474 #if VERBOSE_TESTING
2475               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476                           _("Friend files created/copied successfully!\n"));
2477 #endif
2478         }
2479     }
2480
2481   /* Use the create clique method to initially set all connections as blacklisted. */
2482   if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
2483     create_clique (pg, &blacklist_connections);
2484   unblacklisted_connections = 0;
2485   /* Un-blacklist connections as per the topology specified */
2486   switch (restrict_topology)
2487     {
2488     case GNUNET_TESTING_TOPOLOGY_CLIQUE:
2489 #if VERBOSE_TESTING
2490       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2491                   _("Blacklisting all but clique topology\n"));
2492 #endif
2493       unblacklisted_connections = create_clique (pg, &unblacklist_connections);
2494       break;
2495     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
2496 #if VERBOSE_TESTING
2497       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2498                   _("Blacklisting all but small world (ring) topology\n"));
2499 #endif
2500       unblacklisted_connections = create_small_world_ring (pg, &unblacklist_connections);
2501       break;
2502     case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
2503 #if VERBOSE_TESTING
2504       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505                   _("Blacklisting all but small world (2d-torus) topology\n"));
2506 #endif
2507       unblacklisted_connections = create_small_world (pg, &unblacklist_connections);
2508       break;
2509     case GNUNET_TESTING_TOPOLOGY_RING:
2510 #if VERBOSE_TESTING
2511       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512                   _("Blacklisting all but ring topology\n"));
2513 #endif
2514       unblacklisted_connections = create_ring (pg, &unblacklist_connections);
2515       break;
2516     case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
2517 #if VERBOSE_TESTING
2518       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519                   _("Blacklisting all but 2d torus topology\n"));
2520 #endif
2521       unblacklisted_connections = create_2d_torus (pg, &unblacklist_connections);
2522       break;
2523     case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
2524 #if VERBOSE_TESTING
2525       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2526                   _("Blacklisting all but Erdos-Renyi topology\n"));
2527 #endif
2528       unblacklisted_connections = create_erdos_renyi (pg, &unblacklist_connections);
2529       break;
2530     case GNUNET_TESTING_TOPOLOGY_INTERNAT:
2531 #if VERBOSE_TESTING
2532       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533                   _("Blacklisting all but InterNAT topology\n"));
2534 #endif
2535       unblacklisted_connections = create_nated_internet (pg, &unblacklist_connections);
2536       break;
2537     case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
2538 #if VERBOSE_TESTING
2539       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540                   _("Blacklisting all but Scale Free topology\n"));
2541 #endif
2542       unblacklisted_connections = create_scale_free (pg, &unblacklist_connections);
2543       break;
2544     case GNUNET_TESTING_TOPOLOGY_LINE:
2545 #if VERBOSE_TESTING
2546       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547                   _("Blacklisting all but straight line topology\n"));
2548 #endif
2549       unblacklisted_connections = create_line (pg, &unblacklist_connections);
2550       break;
2551     case GNUNET_TESTING_TOPOLOGY_NONE:
2552 #if VERBOSE_TESTING
2553       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554                   _("Creating no blacklist topology (all peers can connect at transport level)\n"));
2555 #endif
2556     default:
2557       break;
2558     }
2559
2560   if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
2561   {
2562     ret = create_and_copy_blacklist_files(pg, restrict_transports);
2563     if (ret != GNUNET_OK)
2564       {
2565 #if VERBOSE_TESTING
2566         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2567                     _("Failed during blacklist file copying!\n"));
2568 #endif
2569         return GNUNET_SYSERR;
2570       }
2571     else
2572       {
2573 #if VERBOSE_TESTING
2574         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2575                     _("Blacklist files created/copied successfully!\n"));
2576 #endif
2577       }
2578   }
2579   return num_connections;
2580 }
2581
2582 struct RandomContext
2583 {
2584   /**
2585    * The peergroup
2586    */
2587   struct GNUNET_TESTING_PeerGroup *pg;
2588
2589   /**
2590    * uid of the first peer
2591    */
2592   uint32_t first_uid;
2593
2594   /**
2595    * Peer data for first peer.
2596    */
2597   struct PeerData *first;
2598
2599   /**
2600    * Random percentage to use
2601    */
2602   double percentage;
2603 };
2604
2605 struct MinimumContext
2606 {
2607   /**
2608    * The peergroup
2609    */
2610   struct GNUNET_TESTING_PeerGroup *pg;
2611
2612   /**
2613    * uid of the first peer
2614    */
2615   uint32_t first_uid;
2616
2617   /**
2618    * Peer data for first peer.
2619    */
2620   struct PeerData *first;
2621
2622   /**
2623    * Number of conns per peer
2624    */
2625   unsigned int num_to_add;
2626
2627   /**
2628    * Permuted array of all possible connections.  Only add the Nth
2629    * peer if it's in the Nth position.
2630    */
2631   unsigned int *pg_array;
2632
2633   /**
2634    * What number is the current element we are iterating over?
2635    */
2636   unsigned int current;
2637 };
2638
2639 struct DFSContext
2640 {
2641   /**
2642    * The peergroup
2643    */
2644   struct GNUNET_TESTING_PeerGroup *pg;
2645
2646   /**
2647    * uid of the first peer
2648    */
2649   uint32_t first_uid;
2650
2651   /**
2652    * uid of the second peer
2653    */
2654   uint32_t second_uid;
2655
2656   /**
2657    * Peer data for first peer.
2658    */
2659   struct PeerData *first;
2660
2661   /**
2662    * Which peer has been chosen as the one to add?
2663    */
2664   unsigned int chosen;
2665
2666   /**
2667    * What number is the current element we are iterating over?
2668    */
2669   unsigned int current;
2670 };
2671
2672 /**
2673  * Iterator for choosing random peers to connect.
2674  *
2675  * @param cls closure, a RandomContext
2676  * @param key the key the second Daemon was stored under
2677  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2678  *
2679  * @return GNUNET_YES to continue iteration
2680  */
2681 static int
2682 random_connect_iterator (void *cls,
2683                          const GNUNET_HashCode * key,
2684                          void *value)
2685 {
2686   struct RandomContext *random_ctx = cls;
2687   double random_number;
2688   uint32_t second_pos;
2689   GNUNET_HashCode first_hash;
2690   random_number = ((double) GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
2691                                                      UINT64_MAX)) / ( (double) UINT64_MAX);
2692   if (random_number < random_ctx->percentage)
2693   {
2694     GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(random_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2695   }
2696   /* Now we have considered this particular connection, remove it from the second peer so it's not double counted */
2697   uid_from_hash(key, &second_pos);
2698   hash_from_uid(random_ctx->first_uid, &first_hash);
2699   GNUNET_assert(random_ctx->pg->total > second_pos);
2700   GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(random_ctx->pg->peers[second_pos].connect_peers, &first_hash, random_ctx->first->daemon));
2701
2702   return GNUNET_YES;
2703 }
2704
2705 /**
2706  * Iterator for adding at least X peers to a peers connection set.
2707  *
2708  * @param cls closure, MinimumContext
2709  * @param key the key the second Daemon was stored under
2710  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2711  *
2712  * @return GNUNET_YES to continue iteration
2713  */
2714 static int
2715 minimum_connect_iterator (void *cls,
2716                   const GNUNET_HashCode * key,
2717                   void *value)
2718 {
2719   struct MinimumContext *min_ctx = cls;
2720   uint32_t second_pos;
2721   GNUNET_HashCode first_hash;
2722   unsigned int i;
2723
2724   if (GNUNET_CONTAINER_multihashmap_size(min_ctx->first->connect_peers_working_set) < min_ctx->num_to_add)
2725   {
2726     for (i = 0; i < min_ctx->num_to_add; i++)
2727     {
2728       if (min_ctx->pg_array[i] == min_ctx->current)
2729       {
2730         GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(min_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2731         uid_from_hash(key, &second_pos);
2732         hash_from_uid(min_ctx->first_uid, &first_hash);
2733         GNUNET_assert(min_ctx->pg->total > second_pos);
2734         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));
2735         /* Now we have added this particular connection, remove it from the second peer's map so it's not double counted */
2736         GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(min_ctx->pg->peers[second_pos].connect_peers, &first_hash, min_ctx->first->daemon));
2737       }
2738     }
2739     min_ctx->current++;
2740     return GNUNET_YES;
2741   }
2742   else
2743     return GNUNET_NO; /* We can stop iterating, we have enough peers! */
2744
2745 }
2746
2747
2748 /**
2749  * Iterator for adding peers to a connection set based on a depth first search.
2750  *
2751  * @param cls closure, MinimumContext
2752  * @param key the key the second daemon was stored under
2753  * @param value the GNUNET_TESTING_Daemon that the first is to connect to
2754  *
2755  * @return GNUNET_YES to continue iteration
2756  */
2757 static int
2758 dfs_connect_iterator (void *cls,
2759                   const GNUNET_HashCode * key,
2760                   void *value)
2761 {
2762   struct DFSContext *dfs_ctx = cls;
2763   GNUNET_HashCode first_hash;
2764
2765   if (dfs_ctx->current == dfs_ctx->chosen)
2766     {
2767       GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(dfs_ctx->first->connect_peers_working_set, key, value, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2768       uid_from_hash(key, &dfs_ctx->second_uid);
2769       hash_from_uid(dfs_ctx->first_uid, &first_hash);
2770       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));
2771       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(dfs_ctx->pg->peers[dfs_ctx->second_uid].connect_peers, &first_hash, dfs_ctx->first->daemon));
2772       /* Can't remove second from first yet because we are currently iterating, hence the return value in the DFSContext! */
2773       return GNUNET_NO; /* We have found our peer, don't iterate more */
2774     }
2775
2776   dfs_ctx->current++;
2777   return GNUNET_YES;
2778 }
2779
2780
2781 /**
2782  * From the set of connections possible, choose percentage percent of connections
2783  * to actually connect.
2784  *
2785  * @param pg the peergroup we are dealing with
2786  * @param percentage what percent of total connections to make
2787  */
2788 void
2789 choose_random_connections(struct GNUNET_TESTING_PeerGroup *pg, double percentage)
2790 {
2791   struct RandomContext random_ctx;
2792   uint32_t pg_iter;
2793
2794   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2795     {
2796       random_ctx.first_uid = pg_iter;
2797       random_ctx.first = &pg->peers[pg_iter];
2798       random_ctx.percentage = percentage;
2799       random_ctx.pg = pg;
2800       pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(pg->total);
2801       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &random_connect_iterator, &random_ctx);
2802       /* Now remove the old connections */
2803       GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2804       /* And replace with the random set */
2805       pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2806     }
2807 }
2808
2809 /**
2810  * From the set of connections possible, choose at least num connections per
2811  * peer.
2812  *
2813  * @param pg the peergroup we are dealing with
2814  * @param num how many connections at least should each peer have (if possible)?
2815  */
2816 static void
2817 choose_minimum(struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2818 {
2819   struct MinimumContext minimum_ctx;
2820   uint32_t pg_iter;
2821
2822   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2823    {
2824      pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2825    }
2826
2827   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2828     {
2829       minimum_ctx.first_uid = pg_iter;
2830       minimum_ctx.pg_array = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, 
2831                                                           GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers));
2832       minimum_ctx.first = &pg->peers[pg_iter];
2833       minimum_ctx.pg = pg;
2834       minimum_ctx.num_to_add = num;
2835       minimum_ctx.current = 0;
2836       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers,
2837                                             &minimum_connect_iterator, 
2838                                             &minimum_ctx);
2839     }
2840
2841   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2842     {
2843       /* Remove the "old" connections */
2844       GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
2845       /* And replace with the working set */
2846       pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
2847     }
2848
2849 }
2850
2851
2852 static unsigned int
2853 count_workingset_connections(struct GNUNET_TESTING_PeerGroup *pg)
2854 {
2855   unsigned int count;
2856   unsigned int pg_iter;
2857
2858   count = 0;
2859
2860   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2861     {
2862       count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
2863     }
2864
2865   return count;
2866 }
2867
2868
2869 static unsigned int count_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg)
2870 {
2871   unsigned int count;
2872   unsigned int pg_iter;
2873
2874   count = 0;
2875
2876   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2877     {
2878       count += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
2879     }
2880
2881   return count;
2882 }
2883
2884
2885 struct FindClosestContext
2886 {
2887   /**
2888    * The currently known closest peer.
2889    */
2890   struct GNUNET_TESTING_Daemon *closest;
2891
2892   /**
2893    * The info for the peer we are adding connections for.
2894    */
2895   struct PeerData *curr_peer;
2896
2897   /**
2898    * The distance (bits) between the current
2899    * peer and the currently known closest.
2900    */
2901   unsigned int closest_dist;
2902
2903   /**
2904    * The offset of the closest known peer in
2905    * the peer group.
2906    */
2907   unsigned int closest_num;
2908 };
2909
2910 /**
2911  * Iterator over hash map entries of the allowed
2912  * peer connections.  Find the closest, not already
2913  * connected peer and return it.
2914  *
2915  * @param cls closure (struct FindClosestContext)
2916  * @param key current key code (hash of offset in pg)
2917  * @param value value in the hash map - a GNUNET_TESTING_Daemon
2918  * @return GNUNET_YES if we should continue to
2919  *         iterate,
2920  *         GNUNET_NO if not.
2921  */
2922 static
2923 int find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
2924 {
2925   struct FindClosestContext *closest_ctx = cls;
2926   struct GNUNET_TESTING_Daemon *daemon = value;
2927
2928   if (((closest_ctx->closest == NULL) ||
2929        (GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey) > closest_ctx->closest_dist))
2930       && (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(closest_ctx->curr_peer->connect_peers, key)))
2931     {
2932       closest_ctx->closest_dist = GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey);
2933       closest_ctx->closest = daemon;
2934       uid_from_hash(key, &closest_ctx->closest_num);
2935     }
2936   return GNUNET_YES;
2937 }
2938
2939 /**
2940  * From the set of connections possible, choose at num connections per
2941  * peer based on depth which are closest out of those allowed.  Guaranteed
2942  * to add num peers to connect to, provided there are that many peers
2943  * in the underlay topology to connect to.
2944  *
2945  * @param pg the peergroup we are dealing with
2946  * @param num how many connections at least should each peer have (if possible)?
2947  * @param proc processor to actually add the connections
2948  */
2949 void
2950 add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num, GNUNET_TESTING_ConnectionProcessor proc)
2951 {
2952   struct FindClosestContext closest_ctx;
2953   uint32_t pg_iter;
2954   uint32_t i;
2955
2956   for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
2957     {
2958       for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2959         {
2960           closest_ctx.curr_peer = &pg->peers[pg_iter];
2961           closest_ctx.closest = NULL;
2962           closest_ctx.closest_dist = 0;
2963           closest_ctx.closest_num = 0;
2964           GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &find_closest_peers, &closest_ctx);
2965           if (closest_ctx.closest != NULL)
2966             {
2967               GNUNET_assert((0 <= closest_ctx.closest_num) && (closest_ctx.closest_num < pg->total));
2968               proc(pg, pg_iter, closest_ctx.closest_num);
2969             }
2970         }
2971     }
2972 }
2973
2974 /**
2975  * From the set of connections possible, choose at least num connections per
2976  * peer based on depth first traversal of peer connections.  If DFS leaves
2977  * peers unconnected, ensure those peers get connections.
2978  *
2979  * @param pg the peergroup we are dealing with
2980  * @param num how many connections at least should each peer have (if possible)?
2981  */
2982 void
2983 perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num)
2984 {
2985   struct DFSContext dfs_ctx;
2986   uint32_t pg_iter;
2987   uint32_t dfs_count;
2988   uint32_t starting_peer;
2989   uint32_t least_connections;
2990   GNUNET_HashCode second_hash;
2991
2992   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2993     {
2994       pg->peers[pg_iter].connect_peers_working_set = GNUNET_CONTAINER_multihashmap_create(num);
2995     }
2996
2997   starting_peer = 0;
2998   dfs_count = 0;
2999   while ((count_workingset_connections(pg) < num * pg->total) && (count_allowed_connections(pg) > 0))
3000     {
3001       if (dfs_count % pg->total == 0) /* Restart the DFS at some weakly connected peer */
3002         {
3003           least_connections = -1; /* Set to very high number */
3004           for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3005             {
3006               if (GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set) < least_connections)
3007                 {
3008                   starting_peer = pg_iter;
3009                   least_connections = GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers_working_set);
3010                 }
3011             }
3012         }
3013
3014       if (GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers) == 0)  /* Ensure there is at least one peer left to connect! */
3015         {
3016           dfs_count = 0;
3017           continue;
3018         }
3019
3020       /* Choose a random peer from the chosen peers set of connections to add */
3021       dfs_ctx.chosen = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CONTAINER_multihashmap_size(pg->peers[starting_peer].connect_peers));
3022       dfs_ctx.first_uid = starting_peer;
3023       dfs_ctx.first = &pg->peers[starting_peer];
3024       dfs_ctx.pg = pg;
3025       dfs_ctx.current = 0;
3026
3027       GNUNET_CONTAINER_multihashmap_iterate(pg->peers[starting_peer].connect_peers, &dfs_connect_iterator, &dfs_ctx);
3028       /* Remove the second from the first, since we will be continuing the search and may encounter the first peer again! */
3029       hash_from_uid(dfs_ctx.second_uid, &second_hash);
3030       GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(pg->peers[starting_peer].connect_peers, &second_hash, pg->peers[dfs_ctx.second_uid].daemon));
3031       starting_peer = dfs_ctx.second_uid;
3032     }
3033
3034   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3035     {
3036       /* Remove the "old" connections */
3037       GNUNET_CONTAINER_multihashmap_destroy(pg->peers[pg_iter].connect_peers);
3038       /* And replace with the working set */
3039       pg->peers[pg_iter].connect_peers = pg->peers[pg_iter].connect_peers_working_set;
3040     }
3041 }
3042
3043 /**
3044  * Internal callback for topology information for a particular peer.
3045  */
3046 static void
3047 internal_topology_callback(void *cls,
3048                            const struct GNUNET_PeerIdentity *peer,
3049                            struct GNUNET_TIME_Relative latency, uint32_t distance)
3050 {
3051   struct CoreContext *core_ctx = cls;
3052   struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
3053
3054   if (peer == NULL) /* Either finished, or something went wrong */
3055     {
3056       iter_ctx->completed++;
3057       iter_ctx->connected--;
3058       /* One core context allocated per iteration, must free! */
3059       GNUNET_free(core_ctx);
3060     }
3061   else
3062     {
3063       iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
3064     }
3065
3066   if (iter_ctx->completed == iter_ctx->total)
3067     {
3068       iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
3069       /* Once all are done, free the iteration context */
3070       GNUNET_free(iter_ctx);
3071     }
3072 }
3073
3074
3075 /**
3076  * Check running topology iteration tasks, if below max start a new one, otherwise
3077  * schedule for some time in the future.
3078  */
3079 static void
3080 schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3081 {
3082   struct CoreContext *core_context = cls;
3083   struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
3084   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3085     return;
3086
3087   if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3088     {
3089 #if VERBOSE_TESTING > 2
3090           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091                       _("Delaying connect, we have too many outstanding connections!\n"));
3092 #endif
3093       GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
3094     }
3095   else
3096     {
3097 #if VERBOSE_TESTING > 2
3098           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3099                       _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3100 #endif
3101       topology_context->connected++;
3102       if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
3103         internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
3104
3105     }
3106 }
3107
3108 /**
3109  * Iterate over all (running) peers in the peer group, retrieve
3110  * all connections that each currently has.
3111  */
3112 void
3113 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
3114 {
3115   struct TopologyIterateContext *topology_context;
3116   struct CoreContext *core_ctx;
3117   unsigned int i;
3118   unsigned int total_count;
3119
3120   /* Allocate a single topology iteration context */
3121   topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
3122   topology_context->topology_cb = cb;
3123   topology_context->cls = cls;
3124   total_count = 0;
3125   for (i = 0; i < pg->total; i++)
3126     {
3127       if (pg->peers[i].daemon->running == GNUNET_YES)
3128         {
3129           /* Allocate one core context per core we need to connect to */
3130           core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
3131           core_ctx->daemon = pg->peers[i].daemon;
3132           /* Set back pointer to topology iteration context */
3133           core_ctx->iter_context = topology_context;
3134           GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
3135           total_count++;
3136         }
3137     }
3138   topology_context->total = total_count;
3139   return;
3140 }
3141
3142 /**
3143  * Callback function to process statistic values.
3144  * This handler is here only really to insert a peer
3145  * identity (or daemon) so the statistics can be uniquely
3146  * tied to a single running peer.
3147  *
3148  * @param cls closure
3149  * @param subsystem name of subsystem that created the statistic
3150  * @param name the name of the datum
3151  * @param value the current value
3152  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
3153  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
3154  */
3155 static int internal_stats_callback (void *cls,
3156                                     const char *subsystem,
3157                                     const char *name,
3158                                     uint64_t value,
3159                                     int is_persistent)
3160 {
3161   struct StatsCoreContext *core_context = cls;
3162   struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3163
3164   return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
3165 }
3166
3167 /**
3168  * Internal continuation call for statistics iteration.
3169  *
3170  * @param cls closure, the CoreContext for this iteration
3171  * @param success whether or not the statistics iterations
3172  *        was canceled or not (we don't care)
3173  */
3174 static void internal_stats_cont (void *cls, int success)
3175 {
3176   struct StatsCoreContext *core_context = cls;
3177   struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3178
3179   stats_context->connected--;
3180   stats_context->completed++;
3181
3182   if (stats_context->completed == stats_context->total)
3183     {
3184       stats_context->cont(stats_context->cls, GNUNET_YES);
3185       GNUNET_free(stats_context);
3186     }
3187
3188   if (core_context->stats_handle != NULL)
3189     GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
3190
3191   GNUNET_free(core_context);
3192 }
3193
3194 /**
3195  * Check running topology iteration tasks, if below max start a new one, otherwise
3196  * schedule for some time in the future.
3197  */
3198 static void
3199 schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3200 {
3201   struct StatsCoreContext *core_context = cls;
3202   struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
3203
3204   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3205     return;
3206
3207   if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
3208     {
3209 #if VERBOSE_TESTING > 2
3210           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3211                       _("Delaying connect, we have too many outstanding connections!\n"));
3212 #endif
3213       GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
3214     }
3215   else
3216     {
3217 #if VERBOSE_TESTING > 2
3218           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3219                       _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
3220 #endif
3221
3222       stats_context->connected++;
3223       core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
3224       if (core_context->stats_handle == NULL)
3225         {
3226           internal_stats_cont (core_context, GNUNET_NO);
3227           return;
3228         }
3229
3230       core_context->stats_get_handle = GNUNET_STATISTICS_get(core_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
3231       if (core_context->stats_get_handle == NULL)
3232         internal_stats_cont (core_context, GNUNET_NO);
3233
3234     }
3235 }
3236
3237
3238 /**
3239  * Iterate over all (running) peers in the peer group, retrieve
3240  * all statistics from each.
3241  */
3242 void
3243 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
3244                                GNUNET_STATISTICS_Callback cont,
3245                                GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
3246 {
3247   struct StatsIterateContext *stats_context;
3248   struct StatsCoreContext *core_ctx;
3249   unsigned int i;
3250   unsigned int total_count;
3251
3252   /* Allocate a single stats iteration context */
3253   stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
3254   stats_context->cont = cont;
3255   stats_context->proc = proc;
3256   stats_context->cls = cls;
3257   total_count = 0;
3258   for (i = 0; i < pg->total; i++)
3259     {
3260       if (pg->peers[i].daemon->running == GNUNET_YES)
3261         {
3262           /* Allocate one core context per core we need to connect to */
3263           core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
3264           core_ctx->daemon = pg->peers[i].daemon;
3265           /* Set back pointer to topology iteration context */
3266           core_ctx->iter_context = stats_context;
3267           GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
3268           total_count++;
3269         }
3270     }
3271   stats_context->total = total_count;
3272   return;
3273 }
3274
3275 /**
3276  * There are many ways to connect peers that are supported by this function.
3277  * To connect peers in the same topology that was created via the
3278  * GNUNET_TESTING_create_topology, the topology variable must be set to
3279  * GNUNET_TESTING_TOPOLOGY_NONE.  If the topology variable is specified,
3280  * a new instance of that topology will be generated and attempted to be
3281  * connected.  This could result in some connections being impossible,
3282  * because some topologies are non-deterministic.
3283  *
3284  * @param pg the peer group struct representing the running peers
3285  * @param topology which topology to connect the peers in
3286  * @param options options for connecting the topology
3287  * @param option_modifier modifier for options that take a parameter
3288  * @return the number of connections that will be attempted, GNUNET_SYSERR on error
3289  */
3290 int
3291 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
3292                                  enum GNUNET_TESTING_Topology topology,
3293                                  enum GNUNET_TESTING_TopologyOption options,
3294                                  double option_modifier)
3295 {
3296   switch (topology)
3297       {
3298       case GNUNET_TESTING_TOPOLOGY_CLIQUE:
3299 #if VERBOSE_TOPOLOGY
3300       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3301                   _("Creating clique CONNECT topology\n"));
3302 #endif
3303         create_clique (pg, &add_actual_connections);
3304         break;
3305       case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
3306 #if VERBOSE_TOPOLOGY
3307       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308                   _("Creating small world (ring) CONNECT topology\n"));
3309 #endif
3310         create_small_world_ring (pg, &add_actual_connections);
3311         break;
3312       case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
3313 #if VERBOSE_TOPOLOGY
3314       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3315                   _("Creating small world (2d-torus) CONNECT topology\n"));
3316 #endif
3317         create_small_world (pg, &add_actual_connections);
3318         break;
3319       case GNUNET_TESTING_TOPOLOGY_RING:
3320 #if VERBOSE_TOPOLOGY
3321       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322                   _("Creating ring CONNECT topology\n"));
3323 #endif
3324         create_ring (pg, &add_actual_connections);
3325         break;
3326       case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
3327 #if VERBOSE_TOPOLOGY
3328       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3329                   _("Creating 2d torus CONNECT topology\n"));
3330 #endif
3331         create_2d_torus (pg, &add_actual_connections);
3332         break;
3333       case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
3334 #if VERBOSE_TOPOLOGY
3335       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336                   _("Creating Erdos-Renyi CONNECT topology\n"));
3337 #endif
3338         create_erdos_renyi (pg, &add_actual_connections);
3339         break;
3340       case GNUNET_TESTING_TOPOLOGY_INTERNAT:
3341 #if VERBOSE_TOPOLOGY
3342       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3343                   _("Creating InterNAT CONNECT topology\n"));
3344 #endif
3345         create_nated_internet (pg, &add_actual_connections);
3346         break;
3347       case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
3348 #if VERBOSE_TOPOLOGY
3349       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3350                   _("Creating Scale Free CONNECT topology\n"));
3351 #endif
3352         create_scale_free (pg, &add_actual_connections);
3353         break;
3354       case GNUNET_TESTING_TOPOLOGY_LINE:
3355 #if VERBOSE_TOPOLOGY
3356       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3357                   _("Creating straight line CONNECT topology\n"));
3358 #endif
3359         create_line (pg, &add_actual_connections);
3360         break;
3361       case GNUNET_TESTING_TOPOLOGY_NONE:
3362 #if VERBOSE_TOPOLOGY
3363         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3364                   _("Creating no CONNECT topology\n"));
3365 #endif
3366         copy_allowed_topology(pg);
3367         break;
3368       default:
3369         GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
3370                    _("Unknown topology specification, can't connect peers!\n"));
3371         return GNUNET_SYSERR;
3372       }
3373
3374   switch (options)
3375     {
3376     case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
3377 #if VERBOSE_TOPOLOGY
3378       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3379                   _("Connecting random subset (%'.2f percent) of possible peers\n"), 100 * option_modifier);
3380 #endif
3381       choose_random_connections(pg, option_modifier);
3382       break;
3383     case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
3384 #if VERBOSE_TOPOLOGY
3385       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3386                   _("Connecting a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3387 #endif
3388       choose_minimum(pg, (unsigned int)option_modifier);
3389       break;
3390     case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
3391 #if VERBOSE_TOPOLOGY
3392       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3393                   _("Using DFS to connect a minimum of %u peers each (if possible)\n"), (unsigned int)option_modifier);
3394 #endif
3395       perform_dfs(pg, (int)option_modifier);
3396       break;
3397     case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
3398 #if VERBOSE_TOPOLOGY
3399       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3400                   _("Finding additional %u closest peers each (if possible)\n"), (unsigned int)option_modifier);
3401 #endif
3402       add_closest(pg, (unsigned int)option_modifier, &add_actual_connections);
3403       break;
3404     case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
3405       break;
3406     case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
3407       break;
3408     default:
3409       break;
3410     }
3411
3412   return connect_topology(pg);
3413 }
3414
3415 /**
3416  * Callback that is called whenever a hostkey is generated
3417  * for a peer.  Call the real callback and decrement the
3418  * starting counter for the peergroup.
3419  *
3420  * @param cls closure
3421  * @param id identifier for the daemon, NULL on error
3422  * @param d handle for the daemon
3423  * @param emsg error message (NULL on success)
3424  */
3425 static void internal_hostkey_callback (void *cls,
3426                                        const struct GNUNET_PeerIdentity *id,
3427                                        struct GNUNET_TESTING_Daemon *d,
3428                                        const char *emsg)
3429 {
3430   struct InternalStartContext *internal_context = cls;
3431   internal_context->peer->pg->starting--;
3432   internal_context->peer->pg->started++;
3433   if (internal_context->hostkey_callback != NULL)
3434     internal_context->hostkey_callback(internal_context->hostkey_cls, id, d, emsg);
3435   else if (internal_context->peer->pg->started == internal_context->peer->pg->total)
3436     {
3437       internal_context->peer->pg->started = 0; /* Internal startup may use this counter! */
3438       GNUNET_TESTING_daemons_continue_startup(internal_context->peer->pg);
3439     }
3440 }
3441
3442 /**
3443  * Callback that is called whenever a peer has finished starting.
3444  * Call the real callback and decrement the starting counter
3445  * for the peergroup.
3446  *
3447  * @param cls closure
3448  * @param id identifier for the daemon, NULL on error
3449  * @param d handle for the daemon
3450  * @param emsg error message (NULL on success)
3451  */
3452 static void internal_startup_callback (void *cls,
3453                                        const struct GNUNET_PeerIdentity *id,
3454                                        const struct GNUNET_CONFIGURATION_Handle *cfg,
3455                                        struct GNUNET_TESTING_Daemon *d,
3456                                        const char *emsg)
3457 {
3458   struct InternalStartContext *internal_context = cls;
3459   internal_context->peer->pg->starting--;
3460   if (internal_context->start_cb != NULL)
3461     internal_context->start_cb(internal_context->start_cb_cls, id, cfg, d, emsg);
3462 }
3463
3464 static void
3465 internal_continue_startup (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3466 {
3467   struct InternalStartContext *internal_context = cls;
3468
3469   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3470     {
3471       return;
3472     }
3473
3474   if (internal_context->peer->pg->starting < MAX_CONCURRENT_STARTING)
3475     {
3476       internal_context->peer->pg->starting++;
3477       GNUNET_TESTING_daemon_continue_startup (internal_context->peer->daemon);
3478     }
3479   else
3480     {
3481       GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_continue_startup, internal_context);
3482     }
3483 }
3484
3485
3486 /**
3487  * Callback for informing us about a successful
3488  * or unsuccessful churn start call.
3489  *
3490  * @param cls a ChurnContext
3491  * @param id the peer identity of the started peer
3492  * @param cfg the handle to the configuration of the peer
3493  * @param d handle to the daemon for the peer
3494  * @param emsg NULL on success, non-NULL on failure
3495  *
3496  */
3497 void
3498 churn_start_callback (void *cls,
3499                       const struct GNUNET_PeerIdentity *id,
3500                       const struct GNUNET_CONFIGURATION_Handle *cfg,
3501                       struct GNUNET_TESTING_Daemon *d,
3502                       const char *emsg)
3503 {
3504   struct ChurnRestartContext *startup_ctx = cls;
3505   struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
3506
3507   unsigned int total_left;
3508   char *error_message;
3509
3510   error_message = NULL;
3511   if (emsg != NULL)
3512     {
3513       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3514                   "Churn stop callback failed with error `%s'\n",
3515                   emsg);
3516       churn_ctx->num_failed_start++;
3517     }
3518   else
3519     {
3520       churn_ctx->num_to_start--;
3521     }
3522
3523 #if DEBUG_CHURN
3524   GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
3525              "Started peer, %d left.\n",
3526              churn_ctx->num_to_start);
3527 #endif
3528
3529   total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3530
3531   if (total_left == 0)
3532   {
3533     if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3534       GNUNET_asprintf(&error_message,
3535                       "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
3536                       churn_ctx->num_failed_start,
3537                       churn_ctx->num_failed_stop);
3538     churn_ctx->cb(churn_ctx->cb_cls, error_message);
3539     GNUNET_free_non_null(error_message);
3540     GNUNET_free(churn_ctx);
3541     GNUNET_free(startup_ctx);
3542   }
3543 }
3544
3545
3546 static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3547 {
3548   struct PeerRestartContext *peer_restart_ctx = cls;
3549   struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
3550
3551   if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
3552     GNUNET_SCHEDULER_add_delayed(peer_restart_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
3553   else
3554     {
3555       GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
3556                                           startup_ctx->timeout,
3557                                           &churn_start_callback,
3558                                           startup_ctx);
3559       GNUNET_free(peer_restart_ctx);
3560     }
3561 }
3562
3563 static void
3564 internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
3565 {
3566   struct InternalStartContext *internal_context = cls;
3567
3568   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3569     {
3570       return;
3571     }
3572
3573   if (internal_context->peer->pg->starting < MAX_CONCURRENT_HOSTKEYS)
3574     {
3575       internal_context->peer->pg->starting++;
3576       internal_context->peer->daemon = GNUNET_TESTING_daemon_start (internal_context->peer->pg->sched,
3577                                                                     internal_context->peer->cfg,
3578                                                                     internal_context->timeout,
3579                                                                     internal_context->hostname,
3580                                                                     internal_context->username,
3581                                                                     internal_context->sshport,
3582                                                                     &internal_hostkey_callback,
3583                                                                     internal_context,
3584                                                                     &internal_startup_callback,
3585                                                                     internal_context);
3586     }
3587   else
3588     {
3589       GNUNET_SCHEDULER_add_delayed(internal_context->peer->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &internal_start, internal_context);
3590     }
3591 }
3592
3593 /**
3594  * Function which continues a peer group starting up
3595  * after successfully generating hostkeys for each peer.
3596  *
3597  * @param pg the peer group to continue starting
3598  *
3599  */
3600 void
3601 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
3602 {
3603   unsigned int i;
3604
3605   pg->starting = 0;
3606   for (i = 0; i < pg->total; i++)
3607     {
3608       GNUNET_SCHEDULER_add_now (pg->sched, &internal_continue_startup, &pg->peers[i].internal_context);
3609       //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
3610     }
3611 }
3612
3613 /**
3614  * Start count gnunet instances with the same set of transports and
3615  * applications.  The port numbers (any option called "PORT") will be
3616  * adjusted to ensure that no two peers running on the same system
3617  * have the same port(s) in their respective configurations.
3618  *
3619  * @param sched scheduler to use
3620  * @param cfg configuration template to use
3621  * @param total number of daemons to start
3622  * @param timeout total time allowed for peers to start
3623  * @param hostkey_callback function to call on each peers hostkey generation
3624  *        if NULL, peers will be started by this call, if non-null,
3625  *        GNUNET_TESTING_daemons_continue_startup must be called after
3626  *        successful hostkey generation
3627  * @param hostkey_cls closure for hostkey callback
3628  * @param cb function to call on each daemon that was started
3629  * @param cb_cls closure for cb
3630  * @param connect_callback function to call each time two hosts are connected
3631  * @param connect_callback_cls closure for connect_callback
3632  * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
3633  *
3634  * @return NULL on error, otherwise handle to control peer group
3635  */
3636 struct GNUNET_TESTING_PeerGroup *
3637 GNUNET_TESTING_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
3638                               const struct GNUNET_CONFIGURATION_Handle *cfg,
3639                               unsigned int total,
3640                               struct GNUNET_TIME_Relative timeout,
3641                               GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
3642                               void *hostkey_cls,
3643                               GNUNET_TESTING_NotifyDaemonRunning cb,
3644                               void *cb_cls,
3645                               GNUNET_TESTING_NotifyConnection
3646                               connect_callback, void *connect_callback_cls,
3647                               const struct GNUNET_TESTING_Host *hostnames)
3648 {
3649   struct GNUNET_TESTING_PeerGroup *pg;
3650   const struct GNUNET_TESTING_Host *hostpos;
3651 #if 0
3652   char *pos;
3653   const char *rpos;
3654   char *start;
3655 #endif
3656   const char *hostname;
3657   const char *username;
3658   char *baseservicehome;
3659   char *newservicehome;
3660   char *tmpdir;
3661   struct GNUNET_CONFIGURATION_Handle *pcfg;
3662   unsigned int off;
3663   unsigned int hostcnt;
3664   uint16_t minport;
3665   uint16_t sshport;
3666   uint32_t upnum;
3667   uint32_t fdnum;
3668
3669   if (0 == total)
3670     {
3671       GNUNET_break (0);
3672       return NULL;
3673     }
3674   upnum = 0;
3675   fdnum = 0;
3676   pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
3677   pg->sched = sched;
3678   pg->cfg = cfg;
3679   pg->notify_connection = connect_callback;
3680   pg->notify_connection_cls = connect_callback_cls;
3681   pg->total = total;
3682   pg->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
3683   pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
3684   if (NULL != hostnames)
3685     {
3686       off = 0;
3687       hostpos = hostnames;
3688       while (hostpos != NULL)
3689         {
3690           hostpos = hostpos->next;
3691           off++;
3692         }
3693       pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3694       off = 0;
3695
3696       hostpos = hostnames;
3697       while (hostpos != NULL)
3698         {
3699           pg->hosts[off].minport = LOW_PORT;
3700           pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
3701           if (hostpos->username != NULL)
3702             pg->hosts[off].username = GNUNET_strdup(hostpos->username);
3703           pg->hosts[off].sshport = hostpos->port;
3704           hostpos = hostpos->next;
3705           off++;
3706         }
3707
3708       if (off == 0)
3709         {
3710           pg->hosts = NULL;
3711         }
3712       hostcnt = off;
3713       minport = 0;
3714       pg->num_hosts = off;
3715
3716 #if NO_LL
3717       off = 2;
3718       /* skip leading spaces */
3719       while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
3720         hostnames++;
3721       rpos = hostnames;
3722       while ('\0' != *rpos)
3723         {
3724           if (isspace ( (unsigned char) *rpos))
3725             off++;
3726           rpos++;
3727         }
3728       pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
3729       off = 0;
3730       start = GNUNET_strdup (hostnames);
3731       pos = start;
3732       while ('\0' != *pos)
3733         {
3734           if (isspace ( (unsigned char) *pos))
3735             {
3736               *pos = '\0';
3737               if (strlen (start) > 0)
3738                 {
3739                   pg->hosts[off].minport = LOW_PORT;
3740                   pg->hosts[off++].hostname = start;
3741                 }
3742               start = pos + 1;
3743             }
3744           pos++;
3745         }
3746       if (strlen (start) > 0)
3747         {
3748           pg->hosts[off].minport = LOW_PORT;
3749           pg->hosts[off++].hostname = start;
3750         }
3751       if (off == 0)
3752         {
3753           GNUNET_free (start);
3754           GNUNET_free (pg->hosts);
3755           pg->hosts = NULL;
3756         }
3757       hostcnt = off;
3758       minport = 0;              /* make gcc happy */
3759 #endif
3760     }
3761   else
3762     {
3763       hostcnt = 0;
3764       minport = LOW_PORT;
3765     }
3766   for (off = 0; off < total; off++)
3767     {
3768       if (hostcnt > 0)
3769         {
3770           hostname = pg->hosts[off % hostcnt].hostname;
3771           username = pg->hosts[off % hostcnt].username;
3772           sshport = pg->hosts[off % hostcnt].sshport;
3773           pcfg = make_config (cfg, 
3774                               &pg->hosts[off % hostcnt].minport,
3775                               &upnum,
3776                               hostname, &fdnum);
3777         }
3778       else
3779         {
3780           hostname = NULL;
3781           username = NULL;
3782           sshport = 0;
3783           pcfg = make_config (cfg,
3784                               &minport,
3785                               &upnum,
3786                               hostname, &fdnum);
3787         }
3788
3789       if (NULL == pcfg)
3790         {
3791           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3792                       _
3793                       ("Could not create configuration for peer number %u on `%s'!\n"),
3794                       off, hostname == NULL ? "localhost" : hostname);
3795           continue;
3796         }
3797
3798       if (GNUNET_YES ==
3799           GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS", "SERVICEHOME",
3800                                                  &baseservicehome))
3801         {
3802           GNUNET_asprintf (&newservicehome,
3803                            "%s/%d/", baseservicehome, off);
3804           GNUNET_free (baseservicehome);
3805         }
3806       else
3807         {
3808           tmpdir = getenv ("TMPDIR");
3809           tmpdir = tmpdir ? tmpdir : "/tmp";
3810           GNUNET_asprintf (&newservicehome,
3811                            "%s/%s/%d/",
3812                            tmpdir,
3813                            "gnunet-testing-test-test", off);
3814         }
3815       GNUNET_CONFIGURATION_set_value_string (pcfg,
3816                                              "PATHS",
3817                                              "SERVICEHOME", newservicehome);
3818       GNUNET_free (newservicehome);
3819       pg->peers[off].cfg = pcfg;
3820       pg->peers[off].allowed_peers = GNUNET_CONTAINER_multihashmap_create(total);
3821       pg->peers[off].connect_peers = GNUNET_CONTAINER_multihashmap_create(total);
3822       pg->peers[off].blacklisted_peers = GNUNET_CONTAINER_multihashmap_create(total);
3823       pg->peers[off].pg = pg;
3824
3825       pg->peers[off].internal_context.peer = &pg->peers[off];
3826       pg->peers[off].internal_context.timeout = timeout;
3827       pg->peers[off].internal_context.hostname = hostname;
3828       pg->peers[off].internal_context.username = username;
3829       pg->peers[off].internal_context.sshport = sshport;
3830       pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
3831       pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
3832       pg->peers[off].internal_context.start_cb = cb;
3833       pg->peers[off].internal_context.start_cb_cls = cb_cls;
3834
3835       GNUNET_SCHEDULER_add_now (sched, &internal_start, &pg->peers[off].internal_context);
3836
3837     }
3838   return pg;
3839 }
3840
3841 /*
3842  * Get a daemon by number, so callers don't have to do nasty
3843  * offsetting operation.
3844  */
3845 struct GNUNET_TESTING_Daemon *
3846 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, unsigned int position)
3847 {
3848   if (position < pg->total)
3849     return pg->peers[position].daemon;
3850   else
3851     return NULL;
3852 }
3853
3854 /*
3855  * Get a daemon by peer identity, so callers can
3856  * retrieve the daemon without knowing it's offset.
3857  *
3858  * @param pg the peer group to retrieve the daemon from
3859  * @param peer_id the peer identity of the daemon to retrieve
3860  *
3861  * @return the daemon on success, or NULL if no such peer identity is found
3862  */
3863 struct GNUNET_TESTING_Daemon *
3864 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
3865 {
3866   unsigned int i;
3867
3868   for (i = 0; i < pg->total; i ++)
3869     {
3870       if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
3871         return pg->peers[i].daemon;
3872     }
3873
3874   return NULL;
3875 }
3876
3877 /**
3878  * Prototype of a function that will be called when a
3879  * particular operation was completed the testing library.
3880  *
3881  * @param cls closure (a struct RestartContext)
3882  * @param id id of the peer that was restarted
3883  * @param cfg handle to the configuration of the peer
3884  * @param d handle to the daemon that was restarted
3885  * @param emsg NULL on success
3886  */
3887 void restart_callback (void *cls,
3888                        const struct GNUNET_PeerIdentity *id,
3889                        const struct GNUNET_CONFIGURATION_Handle *cfg,
3890                        struct GNUNET_TESTING_Daemon *d,
3891                        const char *emsg)
3892 {
3893   struct RestartContext *restart_context = cls;
3894
3895   if (emsg == NULL)
3896     {
3897       restart_context->peers_restarted++;
3898     }
3899   else
3900     {
3901       restart_context->peers_restart_failed++;
3902     }
3903
3904   if (restart_context->peers_restarted == restart_context->peer_group->total)
3905     {
3906       restart_context->callback(restart_context->callback_cls, NULL);
3907       GNUNET_free(restart_context);
3908     }
3909   else if (restart_context->peers_restart_failed + restart_context->peers_restarted == restart_context->peer_group->total)
3910     {
3911       restart_context->callback(restart_context->callback_cls, "Failed to restart peers!");
3912       GNUNET_free(restart_context);
3913     }
3914
3915 }
3916
3917 /**
3918  * Callback for informing us about a successful
3919  * or unsuccessful churn stop call.
3920  *
3921  * @param cls a ChurnContext
3922  * @param emsg NULL on success, non-NULL on failure
3923  *
3924  */
3925 void
3926 churn_stop_callback (void *cls, const char *emsg)
3927 {
3928   struct ShutdownContext *shutdown_ctx = cls;
3929   struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
3930   unsigned int total_left;
3931   char *error_message;
3932
3933   error_message = NULL;
3934   shutdown_ctx->outstanding--;
3935
3936   if (emsg != NULL)
3937     {
3938       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
3939                  "Churn stop callback failed with error `%s'\n", emsg);
3940       churn_ctx->num_failed_stop++;
3941     }
3942   else
3943     {
3944       churn_ctx->num_to_stop--;
3945     }
3946
3947 #if DEBUG_CHURN
3948   GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
3949              "Stopped peer, %d left.\n", 
3950              churn_ctx->num_to_stop);
3951 #endif
3952   total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
3953
3954   if (total_left == 0)
3955   {
3956     if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
3957       {
3958         GNUNET_asprintf(&error_message, 
3959                         "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!", 
3960                         churn_ctx->num_failed_start, 
3961                         churn_ctx->num_failed_stop);
3962       }
3963     churn_ctx->cb(churn_ctx->cb_cls, error_message);
3964     GNUNET_free_non_null(error_message);
3965     GNUNET_free(churn_ctx);
3966     GNUNET_free(shutdown_ctx);
3967   }
3968 }
3969
3970 /**
3971  * Count the number of running peers.
3972  *
3973  * @param pg handle for the peer group
3974  *
3975  * @return the number of currently running peers in the peer group
3976  */
3977 unsigned int
3978 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
3979 {
3980   unsigned int i;
3981   unsigned int running = 0;
3982   for (i = 0; i < pg->total; i++)
3983   {
3984     if (pg->peers[i].daemon->running == GNUNET_YES)
3985     {
3986       GNUNET_assert(running != -1);
3987       running++;
3988     }
3989   }
3990   return running;
3991 }
3992
3993 /**
3994  * Task to rate limit the number of outstanding peer shutdown
3995  * requests.  This is necessary for making sure we don't do
3996  * too many ssh connections at once, but is generally nicer
3997  * to any system as well (graduated task starts, as opposed
3998  * to calling gnunet-arm N times all at once).
3999  */
4000 static void
4001 schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4002 {
4003   struct PeerShutdownContext *peer_shutdown_ctx = cls;
4004   struct ShutdownContext *shutdown_ctx;
4005
4006   GNUNET_assert(peer_shutdown_ctx != NULL);
4007   shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4008   GNUNET_assert(shutdown_ctx != NULL);
4009
4010   if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4011     GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_shutdown_task, peer_shutdown_ctx);
4012   else
4013     {
4014       shutdown_ctx->outstanding++;
4015       GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
4016       GNUNET_free(peer_shutdown_ctx);
4017     }
4018 }
4019
4020 /**
4021  * Simulate churn by stopping some peers (and possibly
4022  * re-starting others if churn is called multiple times).  This
4023  * function can only be used to create leave-join churn (peers "never"
4024  * leave for good).  First "voff" random peers that are currently
4025  * online will be taken offline; then "von" random peers that are then
4026  * offline will be put back online.  No notifications will be
4027  * generated for any of these operations except for the callback upon
4028  * completion.
4029  *
4030  * @param pg handle for the peer group
4031  * @param voff number of peers that should go offline
4032  * @param von number of peers that should come back online;
4033  *            must be zero on first call (since "testbed_start"
4034  *            always starts all of the peers)
4035  * @param timeout how long to wait for operations to finish before
4036  *        giving up
4037  * @param cb function to call at the end
4038  * @param cb_cls closure for cb
4039  */
4040 void
4041 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
4042                               unsigned int voff,
4043                               unsigned int von,
4044                               struct GNUNET_TIME_Relative timeout,
4045                               GNUNET_TESTING_NotifyCompletion cb,
4046                               void *cb_cls)
4047 {
4048   struct ChurnContext *churn_ctx;
4049   struct ShutdownContext *shutdown_ctx;
4050   struct PeerShutdownContext *peer_shutdown_ctx;
4051   struct PeerRestartContext *peer_restart_ctx;
4052   struct ChurnRestartContext *churn_startup_ctx;
4053
4054   unsigned int running;
4055   unsigned int stopped;
4056   unsigned int total_running;
4057   unsigned int total_stopped;
4058   unsigned int i;
4059   unsigned int *running_arr;
4060   unsigned int *stopped_arr;
4061   unsigned int *running_permute;
4062   unsigned int *stopped_permute;
4063
4064   running = 0;
4065   stopped = 0;
4066
4067   if ((von == 0) && (voff == 0)) /* No peers at all? */
4068     {
4069       cb(cb_cls, NULL);
4070       return;
4071     }
4072
4073   for (i = 0; i < pg->total; i++)
4074   {
4075     if (pg->peers[i].daemon->running == GNUNET_YES)
4076     {
4077       GNUNET_assert(running != -1);
4078       running++;
4079     }
4080     else
4081     {
4082       GNUNET_assert(stopped != -1);
4083       stopped++;
4084     }
4085   }
4086
4087   if (voff > running)
4088   {
4089     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
4090     cb(cb_cls, "Trying to stop more peers than are currently running!");
4091     return;
4092   }
4093
4094   if (von > stopped)
4095   {
4096     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
4097     cb(cb_cls, "Trying to start more peers than are currently stopped!");
4098     return;
4099   }
4100
4101   churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4102
4103   running_arr = NULL;
4104   if (running > 0)
4105     running_arr = GNUNET_malloc(running * sizeof(unsigned int));
4106
4107   stopped_arr = NULL;
4108   if (stopped > 0)
4109     stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
4110
4111   running_permute = NULL;
4112   stopped_permute = NULL;
4113
4114   if (running > 0)
4115     running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
4116   if (stopped > 0)
4117     stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
4118
4119   total_running = running;
4120   total_stopped = stopped;
4121   running = 0;
4122   stopped = 0;
4123
4124   churn_ctx->num_to_start = von;
4125   churn_ctx->num_to_stop = voff;
4126   churn_ctx->cb = cb;
4127   churn_ctx->cb_cls = cb_cls;  
4128
4129   for (i = 0; i < pg->total; i++)
4130   {
4131     if (pg->peers[i].daemon->running == GNUNET_YES)
4132     {
4133       GNUNET_assert((running_arr != NULL) && (total_running > running));
4134       running_arr[running] = i;
4135       running++;
4136     }
4137     else
4138     {
4139       GNUNET_assert((stopped_arr != NULL) && (total_stopped > stopped));
4140       stopped_arr[stopped] = i;
4141       stopped++;
4142     }
4143   }
4144
4145   GNUNET_assert(running >= voff);
4146   if (voff > 0)
4147     {
4148       shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4149       shutdown_ctx->cb = &churn_stop_callback;
4150       shutdown_ctx->cb_cls = churn_ctx;
4151       shutdown_ctx->total_peers = voff;
4152       shutdown_ctx->timeout = timeout;
4153     }
4154
4155   for (i = 0; i < voff; i++)
4156   {
4157 #if DEBUG_CHURN
4158     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
4159 #endif
4160     GNUNET_assert(running_arr != NULL);
4161     peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4162     peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
4163     peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4164     GNUNET_SCHEDULER_add_now(peer_shutdown_ctx->daemon->sched, &schedule_churn_shutdown_task, peer_shutdown_ctx);
4165
4166     /*
4167     GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
4168                                 timeout, 
4169                                 &churn_stop_callback, churn_ctx, 
4170                                 GNUNET_NO, GNUNET_YES); */
4171   }
4172
4173   GNUNET_assert(stopped >= von);
4174   if (von > 0)
4175     {
4176       churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4177       churn_startup_ctx->churn_ctx = churn_ctx;
4178       churn_startup_ctx->timeout = timeout;
4179     }
4180   for (i = 0; i < von; i++)
4181     {
4182 #if DEBUG_CHURN
4183       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
4184 #endif
4185       GNUNET_assert(stopped_arr != NULL);
4186       peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
4187       peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
4188       peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
4189       GNUNET_SCHEDULER_add_now(peer_restart_ctx->daemon->sched, &schedule_churn_restart, peer_restart_ctx);
4190       /*
4191       GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon, 
4192                                           timeout, &churn_start_callback, churn_ctx);*/
4193   }
4194
4195   GNUNET_free_non_null(running_arr);
4196   GNUNET_free_non_null(stopped_arr);
4197   GNUNET_free_non_null(running_permute);
4198   GNUNET_free_non_null(stopped_permute);
4199 }
4200
4201
4202 /**
4203  * Restart all peers in the given group.
4204  *
4205  * @param pg the handle to the peer group
4206  * @param callback function to call on completion (or failure)
4207  * @param callback_cls closure for the callback function
4208  */
4209 void
4210 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
4211                                 GNUNET_TESTING_NotifyCompletion callback,
4212                                 void *callback_cls)
4213 {
4214   struct RestartContext *restart_context;
4215   unsigned int off;
4216
4217   if (pg->total > 0)
4218     {
4219       restart_context = GNUNET_malloc(sizeof(struct RestartContext));
4220       restart_context->peer_group = pg;
4221       restart_context->peers_restarted = 0;
4222       restart_context->callback = callback;
4223       restart_context->callback_cls = callback_cls;
4224
4225       for (off = 0; off < pg->total; off++)
4226         {
4227           GNUNET_TESTING_daemon_restart (pg->peers[off].daemon, &restart_callback, restart_context);
4228         }
4229     }
4230 }
4231
4232 /**
4233  * Start or stop an individual peer from the given group.
4234  *
4235  * @param pg handle to the peer group
4236  * @param offset which peer to start or stop
4237  * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
4238  * @param timeout how long to wait for shutdown
4239  * @param cb function to call at the end
4240  * @param cb_cls closure for cb
4241  */
4242 void
4243 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg, 
4244                              unsigned int offset,
4245                              int desired_status,
4246                              struct GNUNET_TIME_Relative timeout,
4247                              GNUNET_TESTING_NotifyCompletion cb,
4248                              void *cb_cls)
4249 {
4250   struct ShutdownContext *shutdown_ctx;
4251   struct ChurnRestartContext *startup_ctx;
4252   struct ChurnContext *churn_ctx;
4253
4254   if (GNUNET_NO == desired_status)
4255     {
4256       if (NULL != pg->peers[offset].daemon)
4257         {
4258           shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4259           churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4260           churn_ctx->num_to_start = 0;
4261           churn_ctx->num_to_stop = 1;
4262           churn_ctx->cb = cb;
4263           churn_ctx->cb_cls = cb_cls;
4264           shutdown_ctx->cb_cls = churn_ctx;
4265           GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon, 
4266                                      timeout, &churn_stop_callback, shutdown_ctx,
4267                                      GNUNET_NO, GNUNET_YES);     
4268         }
4269     }
4270   else if (GNUNET_YES == desired_status)
4271     {
4272       if (NULL == pg->peers[offset].daemon)
4273         {
4274           startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
4275           churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
4276           churn_ctx->num_to_start = 1;
4277           churn_ctx->num_to_stop = 0;
4278           churn_ctx->cb = cb;
4279           churn_ctx->cb_cls = cb_cls;  
4280           startup_ctx->churn_ctx = churn_ctx;
4281           GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon, 
4282                                               timeout, &churn_start_callback, startup_ctx);
4283         }
4284     }
4285   else
4286     GNUNET_break (0);
4287 }
4288
4289
4290 /**
4291  * Callback for shutting down peers in a peer group.
4292  *
4293  * @param cls closure (struct ShutdownContext)
4294  * @param emsg NULL on success
4295  */
4296 void internal_shutdown_callback (void *cls,
4297                                  const char *emsg)
4298 {
4299   struct ShutdownContext *shutdown_ctx = cls;
4300
4301   shutdown_ctx->outstanding--;
4302   if (emsg == NULL)
4303     {
4304       shutdown_ctx->peers_down++;
4305     }
4306   else
4307     {
4308       shutdown_ctx->peers_failed++;
4309     }
4310
4311   if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
4312     {
4313       if (shutdown_ctx->peers_failed > 0)
4314         shutdown_ctx->cb(shutdown_ctx->cb_cls, "Not all peers successfully shut down!");
4315       else
4316         shutdown_ctx->cb(shutdown_ctx->cb_cls, NULL);
4317       GNUNET_free(shutdown_ctx);
4318     }
4319 }
4320
4321
4322 /**
4323  * Task to rate limit the number of outstanding peer shutdown
4324  * requests.  This is necessary for making sure we don't do
4325  * too many ssh connections at once, but is generally nicer
4326  * to any system as well (graduated task starts, as opposed
4327  * to calling gnunet-arm N times all at once).
4328  */
4329 static void
4330 schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
4331 {
4332   struct PeerShutdownContext *peer_shutdown_ctx = cls;
4333   struct ShutdownContext *shutdown_ctx;
4334
4335   GNUNET_assert(peer_shutdown_ctx != NULL);
4336   shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
4337   GNUNET_assert(shutdown_ctx != NULL);
4338
4339   if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
4340     GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
4341   else
4342     {
4343       shutdown_ctx->outstanding++;
4344       GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4345       GNUNET_free(peer_shutdown_ctx);
4346     }
4347 }
4348
4349 /**
4350  * Shutdown all peers started in the given group.
4351  *
4352  * @param pg handle to the peer group
4353  * @param timeout how long to wait for shutdown
4354  * @param cb callback to notify upon success or failure
4355  * @param cb_cls closure for cb
4356  */
4357 void
4358 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg, 
4359                              struct GNUNET_TIME_Relative timeout,
4360                              GNUNET_TESTING_NotifyCompletion cb,
4361                              void *cb_cls)
4362 {
4363   unsigned int off;
4364   struct ShutdownContext *shutdown_ctx;
4365   struct PeerShutdownContext *peer_shutdown_ctx;
4366
4367   GNUNET_assert(pg->total > 0);
4368
4369   shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
4370   shutdown_ctx->cb = cb;
4371   shutdown_ctx->cb_cls = cb_cls;
4372   shutdown_ctx->total_peers = pg->total;
4373   shutdown_ctx->timeout = timeout;
4374   /* shtudown_ctx->outstanding = 0; */
4375
4376   for (off = 0; off < pg->total; off++)
4377     {
4378       GNUNET_assert(NULL != pg->peers[off].daemon);
4379       peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
4380       peer_shutdown_ctx->daemon = pg->peers[off].daemon;
4381       peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
4382       GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
4383       //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
4384       if (NULL != pg->peers[off].cfg)
4385         GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
4386       if (pg->peers[off].allowed_peers != NULL)
4387         GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].allowed_peers);
4388       if (pg->peers[off].connect_peers != NULL)
4389         GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].connect_peers);
4390       if (pg->peers[off].blacklisted_peers != NULL)
4391         GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
4392     }
4393   GNUNET_free (pg->peers);
4394   for (off = 0; off < pg->num_hosts; off++)
4395     {
4396       GNUNET_free (pg->hosts[off].hostname);
4397       GNUNET_free_non_null (pg->hosts[off].username);
4398     }
4399   GNUNET_free_non_null (pg->hosts);
4400   GNUNET_free (pg);
4401 }
4402
4403
4404 /* end of testing_group.c */