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