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