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