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