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