cleanup testing_group.c, fix bad shutdown in test_testing testcase
[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 OLD 1
40
41 #define USE_SEND_HELLOS GNUNET_NO
42
43 #define TOPOLOGY_HACK GNUNET_YES
44
45
46 /**
47  * Lowest port used for GNUnet testing.  Should be high enough to not
48  * conflict with other applications running on the hosts but be low
49  * enough to not conflict with client-ports (typically starting around
50  * 32k).
51  */
52 #define LOW_PORT 12000
53
54 /**
55  * Highest port used for GNUnet testing.  Should be low enough to not
56  * conflict with the port range for "local" ports (client apps; see
57  * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
58  */
59 #define HIGH_PORT 56000
60
61 /* Maximum time to delay connect attempt */
62 #define MAX_CONNECT_DELAY 300
63
64 /**
65  * Which list of peers do we need to modify?
66  */
67 enum PeerLists
68 {
69   /** Modify allowed peers */
70   ALLOWED,
71
72   /** Modify connect peers */
73   CONNECT,
74
75   /** Modify blacklist peers */
76   BLACKLIST,
77
78   /** Modify workingset peers */
79   WORKING_SET
80 };
81
82 /**
83  * Prototype of a function called whenever two peers would be connected
84  * in a certain topology.
85  */
86 typedef unsigned int
87 (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup * pg,
88                                       unsigned int first, unsigned int second,
89                                       enum PeerLists list, unsigned int check);
90
91 /**
92  * Context for handling churning a peer group
93  */
94 struct ChurnContext
95 {
96   /**
97    * The peergroup we are dealing with.
98    */
99   struct GNUNET_TESTING_PeerGroup *pg;
100
101   /**
102    * Callback used to notify of churning finished
103    */
104   GNUNET_TESTING_NotifyCompletion cb;
105
106   /**
107    * Closure for callback
108    */
109   void *cb_cls;
110
111   /**
112    * Number of peers that still need to be started
113    */
114   unsigned int num_to_start;
115
116   /**
117    * Number of peers that still need to be stopped
118    */
119   unsigned int num_to_stop;
120
121   /**
122    * Number of peers that failed to start
123    */
124   unsigned int num_failed_start;
125
126   /**
127    * Number of peers that failed to stop
128    */
129   unsigned int num_failed_stop;
130 };
131
132 struct RestartContext
133 {
134   /**
135    * The group of peers being restarted
136    */
137   struct GNUNET_TESTING_PeerGroup *peer_group;
138
139   /**
140    * How many peers have been restarted thus far
141    */
142   unsigned int peers_restarted;
143
144   /**
145    * How many peers got an error when restarting
146    */
147   unsigned int peers_restart_failed;
148
149   /**
150    * The function to call once all peers have been restarted
151    */
152   GNUNET_TESTING_NotifyCompletion callback;
153
154   /**
155    * Closure for callback function
156    */
157   void *callback_cls;
158
159 };
160
161 struct SendHelloContext
162 {
163   /**
164    * Global handle to the peer group.
165    */
166   struct GNUNET_TESTING_PeerGroup *pg;
167
168   /**
169    * The data about this specific peer.
170    */
171   struct PeerData *peer;
172
173   /**
174    * The next HELLO that needs sent to this peer.
175    */
176   struct PeerConnection *peer_pos;
177
178   /**
179    * Are we connected to CORE yet?
180    */
181   unsigned int core_ready;
182
183   /**
184    * How many attempts should we make for failed connections?
185    */
186   unsigned int connect_attempts;
187
188   /**
189    * Task for scheduling core connect requests to be sent.
190    */
191   GNUNET_SCHEDULER_TaskIdentifier core_connect_task;
192 };
193
194 struct ShutdownContext
195 {
196   struct GNUNET_TESTING_PeerGroup *pg;
197   /**
198    * Total peers to wait for
199    */
200   unsigned int total_peers;
201
202   /**
203    * Number of peers successfully shut down
204    */
205   unsigned int peers_down;
206
207   /**
208    * Number of peers failed to shut down
209    */
210   unsigned int peers_failed;
211
212   /**
213    * Number of peers we have started shutting
214    * down.  If too many, wait on them.
215    */
216   unsigned int outstanding;
217
218   /**
219    * Timeout for shutdown.
220    */
221   struct GNUNET_TIME_Relative timeout;
222
223   /**
224    * Callback to call when all peers either
225    * shutdown or failed to shutdown
226    */
227   GNUNET_TESTING_NotifyCompletion cb;
228
229   /**
230    * Closure for cb
231    */
232   void *cb_cls;
233 };
234
235 /**
236  * Individual shutdown context for a particular peer.
237  */
238 struct PeerShutdownContext
239 {
240   /**
241    * Pointer to the high level shutdown context.
242    */
243   struct ShutdownContext *shutdown_ctx;
244
245   /**
246    * The daemon handle for the peer to shut down.
247    */
248   struct GNUNET_TESTING_Daemon *daemon;
249 };
250
251 /**
252  * Individual shutdown context for a particular peer.
253  */
254 struct PeerRestartContext
255 {
256   /**
257    * Pointer to the high level restart context.
258    */
259   struct ChurnRestartContext *churn_restart_ctx;
260
261   /**
262    * The daemon handle for the peer to shut down.
263    */
264   struct GNUNET_TESTING_Daemon *daemon;
265 };
266
267 struct CreateTopologyContext
268 {
269
270   /**
271    * Function to call with number of connections
272    */
273   GNUNET_TESTING_NotifyConnections cont;
274
275   /**
276    * Closure for connection notification
277    */
278   void *cls;
279 };
280
281 enum States
282 {
283   /** Waiting to read number of peers */
284   NUM_PEERS,
285
286   /** Should find next peer index */
287   PEER_INDEX,
288
289   /** Should find colon */
290   COLON,
291
292   /** Should read other peer index, space, or endline */
293   OTHER_PEER_INDEX
294 };
295
296 #if OLD
297 struct PeerConnection
298 {
299   /**
300    * Doubly Linked list
301    */
302   struct PeerConnection *prev;
303
304   /*
305    * Doubly Linked list
306    */
307   struct PeerConnection *next;
308
309   /*
310    * Index of daemon in pg->peers
311    */
312   uint32_t index;
313
314 };
315 #endif
316
317 struct InternalStartContext
318 {
319   /**
320    * Pointer to peerdata
321    */
322   struct PeerData *peer;
323
324   /**
325    * Timeout for peer startup
326    */
327   struct GNUNET_TIME_Relative timeout;
328
329   /**
330    * Client callback for hostkey notification
331    */
332   GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
333
334   /**
335    * Closure for hostkey_callback
336    */
337   void *hostkey_cls;
338
339   /**
340    * Client callback for peer start notification
341    */
342   GNUNET_TESTING_NotifyDaemonRunning start_cb;
343
344   /**
345    * Closure for cb
346    */
347   void *start_cb_cls;
348
349   /**
350    * Hostname, where to start the peer
351    */
352   const char *hostname;
353
354   /**
355    * Username to use when connecting to the
356    * host via ssh.
357    */
358   const char *username;
359
360   /**
361    * Pointer to starting memory location of a hostkey
362    */
363   const char *hostkey;
364
365   /**
366    * Port to use for ssh.
367    */
368   uint16_t sshport;
369
370 };
371
372 struct ChurnRestartContext
373 {
374   /**
375    * PeerGroup that we are working with.
376    */
377   struct GNUNET_TESTING_PeerGroup *pg;
378
379   /**
380    * Number of restarts currently in flight.
381    */
382   unsigned int outstanding;
383
384   /**
385    * Handle to the underlying churn context.
386    */
387   struct ChurnContext *churn_ctx;
388
389   /**
390    * How long to allow the operation to take.
391    */
392   struct GNUNET_TIME_Relative timeout;
393 };
394
395 struct OutstandingSSH
396 {
397   struct OutstandingSSH *next;
398
399   struct OutstandingSSH *prev;
400
401   /**
402    * Number of current ssh connections.
403    */
404   uint32_t outstanding;
405
406   /**
407    * The hostname of this peer.
408    */
409   const char *hostname;
410 };
411
412 /**
413  * Data we keep per peer.
414  */
415 struct PeerData
416 {
417   /**
418    * (Initial) configuration of the host.
419    * (initial because clients could change
420    *  it and we would not know about those
421    *  updates).
422    */
423   struct GNUNET_CONFIGURATION_Handle *cfg;
424
425   /**
426    * Handle for controlling the daemon.
427    */
428   struct GNUNET_TESTING_Daemon *daemon;
429
430   /**
431    * The peergroup this peer belongs to.
432    */
433   struct GNUNET_TESTING_PeerGroup *pg;
434
435 #if OLD
436   /**
437    * Linked list of allowed peer connections.
438    */
439   struct PeerConnection *allowed_peers_head;
440
441   /**
442    * Linked list of allowed peer connections.
443    */
444   struct PeerConnection *allowed_peers_tail;
445
446   /**
447    * Linked list of blacklisted peer connections.
448    */
449   struct PeerConnection *blacklisted_peers_head;
450
451   /**
452    * Linked list of blacklisted peer connections.
453    */
454   struct PeerConnection *blacklisted_peers_tail;
455
456   /**
457    * Linked list of connect peer connections.
458    */
459   struct PeerConnection *connect_peers_head;
460
461   /**
462    * Linked list of connect peer connections.
463    */
464   struct PeerConnection *connect_peers_tail;
465
466   /**
467    * Linked list of connect peer connections.
468    */
469   struct PeerConnection *connect_peers_working_set_head;
470
471   /**
472    * Linked list of connect peer connections.
473    */
474   struct PeerConnection *connect_peers_working_set_tail;
475
476 #else
477   /**
478    * Hash map of allowed peer connections (F2F created topology)
479    */
480   struct GNUNET_CONTAINER_MultiHashMap *allowed_peers;
481
482   /**
483    * Hash map of blacklisted peers
484    */
485   struct GNUNET_CONTAINER_MultiHashMap *blacklisted_peers;
486
487   /**
488    * Hash map of peer connections
489    */
490   struct GNUNET_CONTAINER_MultiHashMap *connect_peers;
491
492   /**
493    * Temporary hash map of peer connections
494    */
495   struct GNUNET_CONTAINER_MultiHashMap *connect_peers_working_set;
496 #endif
497
498   /**
499    * Temporary variable for topology creation, should be reset before
500    * creating any topology so the count is valid once finished.
501    */
502   int num_connections;
503
504   /**
505    * Context to keep track of peers being started, to
506    * stagger hostkey generation and peer startup.
507    */
508   struct InternalStartContext internal_context;
509 };
510
511 /**
512  * Linked list of per-host data.
513  */
514 struct HostData
515 {
516   /**
517    * Name of the host.
518    */
519   char *hostname;
520
521   /**
522    * SSH username to use when connecting to this host.
523    */
524   char *username;
525
526   /**
527    * SSH port to use when connecting to this host.
528    */
529   uint16_t sshport;
530
531   /**
532    * Lowest port that we have not yet used
533    * for GNUnet.
534    */
535   uint16_t minport;
536 };
537
538 struct TopologyIterateContext
539 {
540   /**
541    * The peergroup we are working with.
542    */
543   struct GNUNET_TESTING_PeerGroup *pg;
544
545   /**
546    * Callback for notifying of two connected peers.
547    */
548   GNUNET_TESTING_NotifyTopology topology_cb;
549
550   /**
551    * Closure for topology_cb
552    */
553   void *cls;
554
555   /**
556    * Number of peers currently connected to.
557    */
558   unsigned int connected;
559
560   /**
561    * Number of peers we have finished iterating.
562    */
563   unsigned int completed;
564
565   /**
566    * Number of peers total.
567    */
568   unsigned int total;
569 };
570
571 struct StatsIterateContext
572 {
573   /**
574    * The peergroup that we are dealing with.
575    */
576   struct GNUNET_TESTING_PeerGroup *pg;
577
578   /**
579    * Continuation to call once all stats information has been retrieved.
580    */
581   GNUNET_STATISTICS_Callback cont;
582
583   /**
584    * Proc function to call on each value received.
585    */
586   GNUNET_TESTING_STATISTICS_Iterator proc;
587
588   /**
589    * Closure for topology_cb
590    */
591   void *cls;
592
593   /**
594    * Number of peers currently connected to.
595    */
596   unsigned int connected;
597
598   /**
599    * Number of peers we have finished iterating.
600    */
601   unsigned int completed;
602
603   /**
604    * Number of peers total.
605    */
606   unsigned int total;
607 };
608
609 struct CoreContext
610 {
611   void *iter_context;
612   struct GNUNET_TESTING_Daemon *daemon;
613 };
614
615 struct StatsCoreContext
616 {
617   void *iter_context;
618   struct GNUNET_TESTING_Daemon *daemon;
619   /**
620    * Handle to the statistics service.
621    */
622   struct GNUNET_STATISTICS_Handle *stats_handle;
623
624   /**
625    * Handle for getting statistics.
626    */
627   struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
628 };
629
630 struct ConnectTopologyContext
631 {
632   /**
633    * How many connections are left to create.
634    */
635   unsigned int remaining_connections;
636
637   /**
638    * Handle to group of peers.
639    */
640   struct GNUNET_TESTING_PeerGroup *pg;
641
642   /**
643    * How long to try this connection before timing out.
644    */
645   struct GNUNET_TIME_Relative connect_timeout;
646
647   /**
648    * How many times to retry connecting the two peers.
649    */
650   unsigned int connect_attempts;
651
652   /**
653    * Temp value set for each iteration.
654    */
655   //struct PeerData *first;
656
657   /**
658    * Notification that all peers are connected.
659    */
660   GNUNET_TESTING_NotifyCompletion notify_connections_done;
661
662   /**
663    * Closure for notify.
664    */
665   void *notify_cls;
666 };
667
668 /**
669  * Handle to a group of GNUnet peers.
670  */
671 struct GNUNET_TESTING_PeerGroup
672 {
673   /**
674    * Configuration template.
675    */
676   const struct GNUNET_CONFIGURATION_Handle *cfg;
677
678   /**
679    * Function to call on each started daemon.
680    */
681   //GNUNET_TESTING_NotifyDaemonRunning cb;
682
683   /**
684    * Closure for cb.
685    */
686   //void *cb_cls;
687
688   /*
689    * Function to call on each topology connection created
690    */
691   GNUNET_TESTING_NotifyConnection notify_connection;
692
693   /*
694    * Callback for notify_connection
695    */
696   void *notify_connection_cls;
697
698   /**
699    * Array of information about hosts.
700    */
701   struct HostData *hosts;
702
703   /**
704    * Number of hosts (size of HostData)
705    */
706   unsigned int num_hosts;
707
708   /**
709    * Array of "total" peers.
710    */
711   struct PeerData *peers;
712
713   /**
714    * Number of peers in this group.
715    */
716   unsigned int total;
717
718   /**
719    * At what time should we fail the peer startup process?
720    */
721   struct GNUNET_TIME_Absolute max_timeout;
722
723   /**
724    * How many peers are being started right now?
725    */
726   unsigned int starting;
727
728   /**
729    * How many peers have already been started?
730    */
731   unsigned int started;
732
733   /**
734    * Number of possible connections to peers
735    * at a time.
736    */
737   unsigned int max_outstanding_connections;
738
739   /**
740    * Number of ssh connections to peers (max).
741    */
742   unsigned int max_concurrent_ssh;
743
744   /**
745    * Number of connects we are waiting on, allows us to rate limit
746    * connect attempts.
747    */
748   unsigned int outstanding_connects;
749
750   /**
751    * Number of HELLOs we have yet to send.
752    */
753   unsigned int remaining_hellos;
754
755   /**
756    * How many connects have already been scheduled?
757    */
758   unsigned int total_connects_scheduled;
759
760   /**
761    * Hostkeys loaded from a file.
762    */
763   char *hostkey_data;
764
765   /**
766    * Head of DLL to keep track of the number of outstanding
767    * ssh connections per peer.
768    */
769   struct OutstandingSSH *ssh_head;
770
771   /**
772    * Tail of DLL to keep track of the number of outstanding
773    * ssh connections per peer.
774    */
775   struct OutstandingSSH *ssh_tail;
776
777   /**
778    * Stop scheduling peers connecting.
779    */
780   unsigned int stop_connects;
781
782   /**
783    * Connection context for peer group.
784    */
785   struct ConnectTopologyContext ct_ctx;
786 };
787
788 struct UpdateContext
789 {
790   /**
791    * The altered configuration.
792    */
793   struct GNUNET_CONFIGURATION_Handle *ret;
794
795   /**
796    * The original configuration to alter.
797    */
798   const struct GNUNET_CONFIGURATION_Handle *orig;
799
800   /**
801    * The hostname that this peer will run on.
802    */
803   const char *hostname;
804
805   /**
806    * The next possible port to assign.
807    */
808   unsigned int nport;
809
810   /**
811    * Unique number for unix domain sockets.
812    */
813   unsigned int upnum;
814
815   /**
816    * Unique number for this peer/host to offset
817    * things that are grouped by host.
818    */
819   unsigned int fdnum;
820 };
821
822 struct ConnectContext
823 {
824   /**
825    * Index of peer to connect second to.
826    */
827   uint32_t first_index;
828
829   /**
830    * Index of peer to connect first to.
831    */
832   uint32_t second_index;
833
834   /**
835    * Higher level topology connection context.
836    */
837   struct ConnectTopologyContext *ct_ctx;
838
839   /**
840    * Whether this connection has been accounted for in the schedule_connect call.
841    */
842   int counted;
843 };
844
845 struct UnblacklistContext
846 {
847   /**
848    * The peergroup
849    */
850   struct GNUNET_TESTING_PeerGroup *pg;
851
852   /**
853    * uid of the first peer
854    */
855   uint32_t first_uid;
856 };
857
858 struct RandomContext
859 {
860   /**
861    * The peergroup
862    */
863   struct GNUNET_TESTING_PeerGroup *pg;
864
865   /**
866    * uid of the first peer
867    */
868   uint32_t first_uid;
869
870   /**
871    * Peer data for first peer.
872    */
873   struct PeerData *first;
874
875   /**
876    * Random percentage to use
877    */
878   double percentage;
879 };
880
881 struct MinimumContext
882 {
883   /**
884    * The peergroup
885    */
886   struct GNUNET_TESTING_PeerGroup *pg;
887
888   /**
889    * uid of the first peer
890    */
891   uint32_t first_uid;
892
893   /**
894    * Peer data for first peer.
895    */
896   struct PeerData *first;
897
898   /**
899    * Number of conns per peer
900    */
901   unsigned int num_to_add;
902
903   /**
904    * Permuted array of all possible connections.  Only add the Nth
905    * peer if it's in the Nth position.
906    */
907   unsigned int *pg_array;
908
909   /**
910    * What number is the current element we are iterating over?
911    */
912   unsigned int current;
913 };
914
915 struct DFSContext
916 {
917   /**
918    * The peergroup
919    */
920   struct GNUNET_TESTING_PeerGroup *pg;
921
922   /**
923    * uid of the first peer
924    */
925   uint32_t first_uid;
926
927   /**
928    * uid of the second peer
929    */
930   uint32_t second_uid;
931
932   /**
933    * Peer data for first peer.
934    */
935   struct PeerData *first;
936
937   /**
938    * Which peer has been chosen as the one to add?
939    */
940   unsigned int chosen;
941
942   /**
943    * What number is the current element we are iterating over?
944    */
945   unsigned int current;
946 };
947
948 /**
949  * Simple struct to keep track of progress, and print a
950  * nice little percentage meter for long running tasks.
951  */
952 struct ProgressMeter
953 {
954   unsigned int total;
955
956   unsigned int modnum;
957
958   unsigned int dotnum;
959
960   unsigned int completed;
961
962   int print;
963
964   char *startup_string;
965 };
966
967 #if !OLD
968 /**
969  * Convert unique ID to hash code.
970  *
971  * @param uid unique ID to convert
972  * @param hash set to uid (extended with zeros)
973  */
974 static void
975 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
976   {
977     memset (hash, 0, sizeof (GNUNET_HashCode));
978     *((uint32_t *) hash) = uid;
979   }
980
981 /**
982  * Convert hash code to unique ID.
983  *
984  * @param uid unique ID to convert
985  * @param hash set to uid (extended with zeros)
986  */
987 static void
988 uid_from_hash (const GNUNET_HashCode * hash, uint32_t * uid)
989   {
990     memcpy (uid, hash, sizeof (uint32_t));
991   }
992 #endif
993
994 #if USE_SEND_HELLOS
995 static struct GNUNET_CORE_MessageHandler no_handlers[] =
996   {
997       { NULL, 0, 0}};
998 #endif
999
1000 /**
1001  * Create a meter to keep track of the progress of some task.
1002  *
1003  * @param total the total number of items to complete
1004  * @param start_string a string to prefix the meter with (if printing)
1005  * @param print GNUNET_YES to print the meter, GNUNET_NO to count
1006  *              internally only
1007  *
1008  * @return the progress meter
1009  */
1010 static struct ProgressMeter *
1011 create_meter(unsigned int total, char * start_string, int print)
1012 {
1013   struct ProgressMeter *ret;
1014   ret = GNUNET_malloc(sizeof(struct ProgressMeter));
1015   ret->print = print;
1016   ret->total = total;
1017   ret->modnum = total / 4;
1018   if (ret->modnum == 0) /* Divide by zero check */
1019     ret->modnum = 1;
1020   ret->dotnum = (total / 50) + 1;
1021   if (start_string != NULL)
1022     ret->startup_string = GNUNET_strdup(start_string);
1023   else
1024     ret->startup_string = GNUNET_strdup("");
1025
1026   return ret;
1027 }
1028
1029 /**
1030  * Update progress meter (increment by one).
1031  *
1032  * @param meter the meter to update and print info for
1033  *
1034  * @return GNUNET_YES if called the total requested,
1035  *         GNUNET_NO if more items expected
1036  */
1037 static int
1038 update_meter(struct ProgressMeter *meter)
1039 {
1040   if (meter->print == GNUNET_YES)
1041     {
1042       if (meter->completed % meter->modnum == 0)
1043         {
1044           if (meter->completed == 0)
1045             {
1046               fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
1047             }
1048           else
1049             fprintf (stdout, "%d%%", (int) (((float) meter->completed
1050                 / meter->total) * 100));
1051         }
1052       else if (meter->completed % meter->dotnum == 0)
1053         fprintf (stdout, ".");
1054
1055       if (meter->completed + 1 == meter->total)
1056         fprintf (stdout, "%d%%]\n", 100);
1057       fflush (stdout);
1058     }
1059   meter->completed++;
1060
1061   if (meter->completed == meter->total)
1062     return GNUNET_YES;
1063   if (meter->completed > meter->total)
1064     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
1065   return GNUNET_NO;
1066 }
1067
1068 /**
1069  * Reset progress meter.
1070  *
1071  * @param meter the meter to reset
1072  *
1073  * @return GNUNET_YES if meter reset,
1074  *         GNUNET_SYSERR on error
1075  */
1076 static int
1077 reset_meter(struct ProgressMeter *meter)
1078 {
1079   if (meter == NULL)
1080     return GNUNET_SYSERR;
1081
1082   meter->completed = 0;
1083   return GNUNET_YES;
1084 }
1085
1086 /**
1087  * Release resources for meter
1088  *
1089  * @param meter the meter to free
1090  */
1091 static void
1092 free_meter(struct ProgressMeter *meter)
1093 {
1094   GNUNET_free_non_null (meter->startup_string);
1095   GNUNET_free (meter);
1096 }
1097
1098 /**
1099  * Get a topology from a string input.
1100  *
1101  * @param topology where to write the retrieved topology
1102  * @param topology_string The string to attempt to
1103  *        get a configuration value from
1104  * @return GNUNET_YES if topology string matched a
1105  *         known topology, GNUNET_NO if not
1106  */
1107 int
1108 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology,
1109                             const char *topology_string)
1110 {
1111   /**
1112    * Strings representing topologies in enum
1113    */
1114   static const char *topology_strings[] =
1115     {
1116     /**
1117      * A clique (everyone connected to everyone else).
1118      */
1119     "CLIQUE",
1120
1121     /**
1122      * Small-world network (2d torus plus random links).
1123      */
1124     "SMALL_WORLD",
1125
1126     /**
1127      * Small-world network (ring plus random links).
1128      */
1129     "SMALL_WORLD_RING",
1130
1131     /**
1132      * Ring topology.
1133      */
1134     "RING",
1135
1136     /**
1137      * 2-d torus.
1138      */
1139     "2D_TORUS",
1140
1141     /**
1142      * Random graph.
1143      */
1144     "ERDOS_RENYI",
1145
1146     /**
1147      * Certain percentage of peers are unable to communicate directly
1148      * replicating NAT conditions
1149      */
1150     "INTERNAT",
1151
1152     /**
1153      * Scale free topology.
1154      */
1155     "SCALE_FREE",
1156
1157     /**
1158      * Straight line topology.
1159      */
1160     "LINE",
1161
1162     /**
1163      * All peers are disconnected.
1164      */
1165     "NONE",
1166
1167     /**
1168      * Read the topology from a file.
1169      */
1170     "FROM_FILE",
1171
1172     NULL };
1173
1174   int curr = 0;
1175   if (topology_string == NULL)
1176     return GNUNET_NO;
1177   while (topology_strings[curr] != NULL)
1178     {
1179       if (strcasecmp (topology_strings[curr], topology_string) == 0)
1180         {
1181           *topology = curr;
1182           return GNUNET_YES;
1183         }
1184       curr++;
1185     }
1186   *topology = GNUNET_TESTING_TOPOLOGY_NONE;
1187   return GNUNET_NO;
1188 }
1189
1190 /**
1191  * Get connect topology option from string input.
1192  *
1193  * @param topology_option where to write the retrieved topology
1194  * @param topology_string The string to attempt to
1195  *        get a configuration value from
1196  * @return GNUNET_YES if string matched a known
1197  *         topology option, GNUNET_NO if not
1198  */
1199 int
1200 GNUNET_TESTING_topology_option_get(
1201                                    enum GNUNET_TESTING_TopologyOption *topology_option,
1202                                    const char *topology_string)
1203 {
1204   /**
1205    * Options for connecting a topology as strings.
1206    */
1207   static const char *topology_option_strings[] =
1208     {
1209     /**
1210      * Try to connect all peers specified in the topology.
1211      */
1212     "CONNECT_ALL",
1213
1214     /**
1215      * Choose a random subset of connections to create.
1216      */
1217     "CONNECT_RANDOM_SUBSET",
1218
1219     /**
1220      * Create at least X connections for each peer.
1221      */
1222     "CONNECT_MINIMUM",
1223
1224     /**
1225      * Using a depth first search, create one connection
1226      * per peer.  If any are missed (graph disconnected)
1227      * start over at those peers until all have at least one
1228      * connection.
1229      */
1230     "CONNECT_DFS",
1231
1232     /**
1233      * Find the N closest peers to each allowed peer in the
1234      * topology and make sure a connection to those peers
1235      * exists in the connect topology.
1236      */
1237     "CONNECT_CLOSEST",
1238
1239     /**
1240      * No options specified.
1241      */
1242     "CONNECT_NONE",
1243
1244     NULL };
1245   int curr = 0;
1246
1247   if (topology_string == NULL)
1248     return GNUNET_NO;
1249   while (NULL != topology_option_strings[curr])
1250     {
1251       if (strcasecmp (topology_option_strings[curr], topology_string) == 0)
1252         {
1253           *topology_option = curr;
1254           return GNUNET_YES;
1255         }
1256       curr++;
1257     }
1258   *topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_NONE;
1259   return GNUNET_NO;
1260 }
1261
1262 /**
1263  * Function to iterate over options.  Copies
1264  * the options to the target configuration,
1265  * updating PORT values as needed.
1266  *
1267  * @param cls closure
1268  * @param section name of the section
1269  * @param option name of the option
1270  * @param value value of the option
1271  */
1272 static void
1273 update_config(void *cls, const char *section, const char *option,
1274               const char *value)
1275 {
1276   struct UpdateContext *ctx = cls;
1277   unsigned int ival;
1278   char cval[12];
1279   char uval[128];
1280   char *single_variable;
1281   char *per_host_variable;
1282   unsigned long long num_per_host;
1283
1284   GNUNET_asprintf (&single_variable, "single_%s_per_host", section);
1285   GNUNET_asprintf (&per_host_variable, "num_%s_per_host", section);
1286
1287   if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
1288     {
1289       if ((ival != 0) && (GNUNET_YES
1290           != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1291                                                    single_variable)))
1292         {
1293           GNUNET_snprintf (cval, sizeof(cval), "%u", ctx->nport++);
1294           value = cval;
1295         }
1296       else if ((ival != 0) && (GNUNET_YES
1297           == GNUNET_CONFIGURATION_get_value_yesno (ctx->orig, "testing",
1298                                                    single_variable))
1299           && GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1300                                                     per_host_variable,
1301                                                     &num_per_host))
1302         {
1303           GNUNET_snprintf (cval, sizeof(cval), "%u", ival + ctx->fdnum
1304               % num_per_host);
1305           value = cval;
1306         }
1307     }
1308
1309   if (0 == strcmp (option, "UNIXPATH"))
1310     {
1311       if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno (ctx->orig,
1312                                                               "testing",
1313                                                               single_variable))
1314         {
1315           GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1316                            section, ctx->upnum++);
1317           value = uval;
1318         }
1319       else if ((GNUNET_YES
1320           == GNUNET_CONFIGURATION_get_value_number (ctx->orig, "testing",
1321                                                     per_host_variable,
1322                                                     &num_per_host))
1323           && (num_per_host > 0))
1324
1325         {
1326           GNUNET_snprintf (uval, sizeof(uval), "/tmp/test-service-%s-%u",
1327                            section, ctx->fdnum % num_per_host);
1328           value = uval;
1329         }
1330     }
1331
1332   if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
1333     {
1334       value = ctx->hostname;
1335     }
1336   GNUNET_free (single_variable);
1337   GNUNET_free (per_host_variable);
1338   GNUNET_CONFIGURATION_set_value_string (ctx->ret, section, option, value);
1339 }
1340
1341 /**
1342  * Create a new configuration using the given configuration
1343  * as a template; however, each PORT in the existing cfg
1344  * must be renumbered by incrementing "*port".  If we run
1345  * out of "*port" numbers, return NULL.
1346  *
1347  * @param cfg template configuration
1348  * @param off the current peer offset
1349  * @param port port numbers to use, update to reflect
1350  *             port numbers that were used
1351  * @param upnum number to make unix domain socket names unique
1352  * @param hostname hostname of the controlling host, to allow control connections from
1353  * @param fdnum number used to offset the unix domain socket for grouped processes
1354  *              (such as statistics or peerinfo, which can be shared among others)
1355  *
1356  * @return new configuration, NULL on error
1357  */
1358 static struct GNUNET_CONFIGURATION_Handle *
1359 make_config(const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
1360             uint16_t * port, uint32_t * upnum, const char *hostname,
1361             uint32_t * fdnum)
1362 {
1363   struct UpdateContext uc;
1364   uint16_t orig;
1365   char *control_host;
1366   char *allowed_hosts;
1367
1368   orig = *port;
1369   uc.nport = *port;
1370   uc.upnum = *upnum;
1371   uc.fdnum = *fdnum;
1372   uc.ret = GNUNET_CONFIGURATION_create ();
1373   uc.hostname = hostname;
1374   uc.orig = cfg;
1375
1376   GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
1377   if (uc.nport >= HIGH_PORT)
1378     {
1379       *port = orig;
1380       GNUNET_CONFIGURATION_destroy (uc.ret);
1381       return NULL;
1382     }
1383
1384   if (GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "control_host",
1385                                              &control_host) == GNUNET_OK)
1386     {
1387       if (hostname != NULL)
1388         GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1; %s;", control_host,
1389                          hostname);
1390       else
1391         GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", control_host);
1392
1393       GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "ACCEPT_FROM",
1394                                              allowed_hosts);
1395       GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport",
1396                                              "ACCEPT_FROM", allowed_hosts);
1397       GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "ACCEPT_FROM",
1398                                              allowed_hosts);
1399       GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics",
1400                                              "ACCEPT_FROM", allowed_hosts);
1401
1402       GNUNET_CONFIGURATION_set_value_string (uc.ret, "core", "UNIXPATH", "");
1403       GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport", "UNIXPATH",
1404                                              "");
1405       GNUNET_CONFIGURATION_set_value_string (uc.ret, "dht", "UNIXPATH", "");
1406       GNUNET_CONFIGURATION_set_value_string (uc.ret, "statistics", "UNIXPATH",
1407                                              "");
1408
1409       GNUNET_free_non_null (control_host);
1410       GNUNET_free (allowed_hosts);
1411     }
1412
1413   /* arm needs to know to allow connections from the host on which it is running,
1414    * otherwise gnunet-arm is unable to connect to it in some instances */
1415   if (hostname != NULL)
1416     {
1417       GNUNET_asprintf (&allowed_hosts, "%s; 127.0.0.1;", hostname);
1418       GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1419                                              hostname);
1420       GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1421                                              hostname);
1422       GNUNET_CONFIGURATION_set_value_string (uc.ret, "arm", "ACCEPT_FROM",
1423                                              allowed_hosts);
1424       GNUNET_free (allowed_hosts);
1425     }
1426   else
1427     {
1428       GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-tcp", "BINDTO",
1429                                              "127.0.0.1");
1430       GNUNET_CONFIGURATION_set_value_string (uc.ret, "transport-udp", "BINDTO",
1431                                              "127.0.0.1");
1432     }
1433
1434   *port = (uint16_t) uc.nport;
1435   *upnum = uc.upnum;
1436   uc.fdnum++;
1437   *fdnum = uc.fdnum;
1438   return uc.ret;
1439 }
1440
1441 /*
1442  * Remove entries from the peer connection list
1443  *
1444  * @param pg the peer group we are working with
1445  * @param first index of the first peer
1446  * @param second index of the second peer
1447  * @param list the peer list to use
1448  * @param check UNUSED
1449  *
1450  * @return the number of connections added (can be 0, 1 or 2)
1451  *
1452  */
1453 static unsigned int
1454 remove_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1455                    unsigned int second, enum PeerLists list, unsigned int check)
1456 {
1457   int removed;
1458 #if OLD
1459   struct PeerConnection **first_list;
1460   struct PeerConnection **second_list;
1461   struct PeerConnection *first_iter;
1462   struct PeerConnection *second_iter;
1463   struct PeerConnection **first_tail;
1464   struct PeerConnection **second_tail;
1465
1466 #else
1467   GNUNET_HashCode hash_first;
1468   GNUNET_HashCode hash_second;
1469
1470   hash_from_uid (first, &hash_first);
1471   hash_from_uid (second, &hash_second);
1472 #endif
1473
1474   removed = 0;
1475 #if OLD
1476   switch (list)
1477     {
1478   case ALLOWED:
1479     first_list = &pg->peers[first].allowed_peers_head;
1480     second_list = &pg->peers[second].allowed_peers_head;
1481     first_tail = &pg->peers[first].allowed_peers_tail;
1482     second_tail = &pg->peers[second].allowed_peers_tail;
1483     break;
1484   case CONNECT:
1485     first_list = &pg->peers[first].connect_peers_head;
1486     second_list = &pg->peers[second].connect_peers_head;
1487     first_tail = &pg->peers[first].connect_peers_tail;
1488     second_tail = &pg->peers[second].connect_peers_tail;
1489     break;
1490   case BLACKLIST:
1491     first_list = &pg->peers[first].blacklisted_peers_head;
1492     second_list = &pg->peers[second].blacklisted_peers_head;
1493     first_tail = &pg->peers[first].blacklisted_peers_tail;
1494     second_tail = &pg->peers[second].blacklisted_peers_tail;
1495     break;
1496   case WORKING_SET:
1497     first_list = &pg->peers[first].connect_peers_working_set_head;
1498     second_list = &pg->peers[second].connect_peers_working_set_head;
1499     first_tail = &pg->peers[first].connect_peers_working_set_tail;
1500     second_tail = &pg->peers[second].connect_peers_working_set_tail;
1501     break;
1502   default:
1503     GNUNET_break(0);
1504     return 0;
1505     }
1506
1507   first_iter = *first_list;
1508   while (first_iter != NULL)
1509     {
1510       if (first_iter->index == second)
1511         {
1512           GNUNET_CONTAINER_DLL_remove(*first_list, *first_tail, first_iter);
1513           GNUNET_free(first_iter);
1514           removed++;
1515           break;
1516         }
1517       first_iter = first_iter->next;
1518     }
1519
1520   second_iter = *second_list;
1521   while (second_iter != NULL)
1522     {
1523       if (second_iter->index == first)
1524         {
1525           GNUNET_CONTAINER_DLL_remove(*second_list, *second_tail, second_iter);
1526           GNUNET_free(second_iter);
1527           removed++;
1528           break;
1529         }
1530       second_iter = second_iter->next;
1531     }
1532 #else
1533   if (GNUNET_YES ==
1534       GNUNET_CONTAINER_multihashmap_contains (pg->peers[first].blacklisted_peers,
1535           &hash_second))
1536     {
1537       GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[first].blacklisted_peers,
1538           &hash_second);
1539     }
1540
1541   if (GNUNET_YES ==
1542       GNUNET_CONTAINER_multihashmap_contains (pg->peers[second].blacklisted_peers,
1543           &hash_first))
1544     {
1545       GNUNET_CONTAINER_multihashmap_remove_all (pg->peers[second].blacklisted_peers,
1546           &hash_first);
1547     }
1548 #endif
1549
1550   return removed;
1551 }
1552
1553 /*
1554  * Add entries to the some list
1555  *
1556  * @param pg the peer group we are working with
1557  * @param first index of the first peer
1558  * @param second index of the second peer
1559  * @param list the list type that we should modify
1560  * @param check GNUNET_YES to check lists before adding
1561  *              GNUNET_NO to force add
1562  *
1563  * @return the number of connections added (can be 0, 1 or 2)
1564  *
1565  */
1566 static unsigned int
1567 add_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first,
1568                 unsigned int second, enum PeerLists list, unsigned int check)
1569 {
1570   int added;
1571   int add_first;
1572   int add_second;
1573
1574   struct PeerConnection **first_list;
1575   struct PeerConnection **second_list;
1576   struct PeerConnection *first_iter;
1577   struct PeerConnection *second_iter;
1578   struct PeerConnection *new_first;
1579   struct PeerConnection *new_second;
1580   struct PeerConnection **first_tail;
1581   struct PeerConnection **second_tail;
1582
1583   switch (list)
1584     {
1585   case ALLOWED:
1586     first_list = &pg->peers[first].allowed_peers_head;
1587     second_list = &pg->peers[second].allowed_peers_head;
1588     first_tail = &pg->peers[first].allowed_peers_tail;
1589     second_tail = &pg->peers[second].allowed_peers_tail;
1590     break;
1591   case CONNECT:
1592     first_list = &pg->peers[first].connect_peers_head;
1593     second_list = &pg->peers[second].connect_peers_head;
1594     first_tail = &pg->peers[first].connect_peers_tail;
1595     second_tail = &pg->peers[second].connect_peers_tail;
1596     break;
1597   case BLACKLIST:
1598     first_list = &pg->peers[first].blacklisted_peers_head;
1599     second_list = &pg->peers[second].blacklisted_peers_head;
1600     first_tail = &pg->peers[first].blacklisted_peers_tail;
1601     second_tail = &pg->peers[second].blacklisted_peers_tail;
1602     break;
1603   case WORKING_SET:
1604     first_list = &pg->peers[first].connect_peers_working_set_head;
1605     second_list = &pg->peers[second].connect_peers_working_set_head;
1606     first_tail = &pg->peers[first].connect_peers_working_set_tail;
1607     second_tail = &pg->peers[second].connect_peers_working_set_tail;
1608     break;
1609   default:
1610     GNUNET_break(0);
1611     return 0;
1612     }
1613
1614   add_first = GNUNET_YES;
1615   add_second = GNUNET_YES;
1616
1617   if (check == GNUNET_YES)
1618     {
1619       first_iter = *first_list;
1620       while (first_iter != NULL)
1621         {
1622           if (first_iter->index == second)
1623             {
1624               add_first = GNUNET_NO;
1625               break;
1626             }
1627           first_iter = first_iter->next;
1628         }
1629
1630       second_iter = *second_list;
1631       while (second_iter != NULL)
1632         {
1633           if (second_iter->index == first)
1634             {
1635               add_second = GNUNET_NO;
1636               break;
1637             }
1638           second_iter = second_iter->next;
1639         }
1640     }
1641
1642   added = 0;
1643   if (add_first)
1644     {
1645       new_first = GNUNET_malloc (sizeof (struct PeerConnection));
1646       new_first->index = second;
1647       GNUNET_CONTAINER_DLL_insert(*first_list, *first_tail, new_first);
1648       pg->peers[first].num_connections++;
1649       added++;
1650     }
1651
1652   if (add_second)
1653     {
1654       new_second = GNUNET_malloc (sizeof (struct PeerConnection));
1655       new_second->index = first;
1656       GNUNET_CONTAINER_DLL_insert(*second_list, *second_tail, new_second);
1657       pg->peers[second].num_connections++;
1658       added++;
1659     }
1660
1661   return added;
1662 }
1663
1664 /**
1665  * Scale free network construction as described in:
1666  *
1667  * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
1668  *
1669  * Start with a network of "one" peer, then progressively add
1670  * peers up to the total number.  At each step, iterate over
1671  * all possible peers and connect new peer based on number of
1672  * existing connections of the target peer.
1673  *
1674  * @param pg the peer group we are dealing with
1675  * @param proc the connection processor to use
1676  * @param list the peer list to use
1677  *
1678  * @return the number of connections created
1679  */
1680 static unsigned int
1681 create_scale_free(struct GNUNET_TESTING_PeerGroup *pg,
1682                   GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
1683 {
1684
1685   unsigned int total_connections;
1686   unsigned int outer_count;
1687   unsigned int i;
1688   unsigned int previous_total_connections;
1689   double random;
1690   double probability;
1691
1692   GNUNET_assert (pg->total > 1);
1693
1694   /* Add a connection between the first two nodes */
1695   total_connections = proc (pg, 0, 1, list, GNUNET_YES);
1696
1697   for (outer_count = 1; outer_count < pg->total; outer_count++)
1698     {
1699       previous_total_connections = total_connections;
1700       for (i = 0; i < outer_count; i++)
1701         {
1702           probability = pg->peers[i].num_connections
1703               / (double) previous_total_connections;
1704           random
1705               = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1706                                                     UINT64_MAX))
1707                   / ((double) UINT64_MAX);
1708 #if VERBOSE_TESTING
1709           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710               "Considering connecting peer %d to peer %d\n",
1711               outer_count, i);
1712 #endif
1713           if (random < probability)
1714             {
1715 #if VERBOSE_TESTING
1716               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717                   "Connecting peer %d to peer %d\n", outer_count, i);
1718 #endif
1719               total_connections += proc (pg, outer_count, i, list, GNUNET_YES);
1720             }
1721         }
1722     }
1723
1724   return total_connections;
1725 }
1726
1727 /**
1728  * Create a topology given a peer group (set of running peers)
1729  * and a connection processor.  Creates a small world topology
1730  * according to the rewired ring construction.  The basic
1731  * behavior is that a ring topology is created, but with some
1732  * probability instead of connecting a peer to the next
1733  * neighbor in the ring a connection will be created to a peer
1734  * selected uniformly at random.   We use the TESTING
1735  * PERCENTAGE option to specify what number of
1736  * connections each peer should have.  Default is 2,
1737  * which makes the ring, any given number is multiplied by
1738  * the log of the network size; i.e. a PERCENTAGE of 2 makes
1739  * each peer have on average 2logn connections.  The additional
1740  * connections are made at increasing distance around the ring
1741  * from the original peer, or to random peers based on the re-
1742  * wiring probability. The TESTING
1743  * PROBABILITY option is used as the probability that a given
1744  * connection is rewired.
1745  *
1746  * @param pg the peergroup to create the topology on
1747  * @param proc the connection processor to call to actually set
1748  *        up connections between two peers
1749  * @param list the peer list to use
1750  *
1751  * @return the number of connections that were set up
1752  *
1753  */
1754 static unsigned int
1755 create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg,
1756                         GNUNET_TESTING_ConnectionProcessor proc,
1757                         enum PeerLists list)
1758 {
1759   unsigned int i, j;
1760   int nodeToConnect;
1761   unsigned int natLog;
1762   unsigned int randomPeer;
1763   double random, logNModifier, probability;
1764   unsigned int smallWorldConnections;
1765   int connsPerPeer;
1766   char *p_string;
1767   int max;
1768   int min;
1769   unsigned int useAnd;
1770   int connect_attempts;
1771
1772   logNModifier = 0.5; /* FIXME: default value? */
1773   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1774                                                           "PERCENTAGE",
1775                                                           &p_string))
1776     {
1777       if (sscanf (p_string, "%lf", &logNModifier) != 1)
1778         GNUNET_log (
1779                     GNUNET_ERROR_TYPE_WARNING,
1780                     _
1781                     ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1782                     p_string, "LOGNMODIFIER", "TESTING");
1783       GNUNET_free (p_string);
1784     }
1785   probability = 0.5; /* FIXME: default percentage? */
1786   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1787                                                           "PROBABILITY",
1788                                                           &p_string))
1789     {
1790       if (sscanf (p_string, "%lf", &probability) != 1)
1791         GNUNET_log (
1792                     GNUNET_ERROR_TYPE_WARNING,
1793                     _
1794                     ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1795                     p_string, "PERCENTAGE", "TESTING");
1796       GNUNET_free (p_string);
1797     }
1798   natLog = log (pg->total);
1799   connsPerPeer = ceil (natLog * logNModifier);
1800
1801   if (connsPerPeer % 2 == 1)
1802     connsPerPeer += 1;
1803
1804   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Target is %d connections per peer."),
1805               connsPerPeer);
1806
1807   smallWorldConnections = 0;
1808   connect_attempts = 0;
1809   for (i = 0; i < pg->total; i++)
1810     {
1811       useAnd = 0;
1812       max = i + connsPerPeer / 2;
1813       min = i - connsPerPeer / 2;
1814
1815       if (max > pg->total - 1)
1816         {
1817           max = max - pg->total;
1818           useAnd = 1;
1819         }
1820
1821       if (min < 0)
1822         {
1823           min = pg->total - 1 + min;
1824           useAnd = 1;
1825         }
1826
1827       for (j = 0; j < connsPerPeer / 2; j++)
1828         {
1829           random
1830               = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1831                                                     UINT64_MAX)
1832                   / ((double) UINT64_MAX));
1833           if (random < probability)
1834             {
1835               /* Connect to uniformly selected random peer */
1836               randomPeer
1837                   = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1838                                               pg->total);
1839               while ((((randomPeer < max) && (randomPeer > min)) && (useAnd
1840                   == 0)) || (((randomPeer > min) || (randomPeer < max))
1841                   && (useAnd == 1)))
1842                 {
1843                   randomPeer
1844                       = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1845                                                   pg->total);
1846                 }
1847               smallWorldConnections += proc (pg, i, randomPeer, list,
1848                                              GNUNET_YES);
1849             }
1850           else
1851             {
1852               nodeToConnect = i + j + 1;
1853               if (nodeToConnect > pg->total - 1)
1854                 {
1855                   nodeToConnect = nodeToConnect - pg->total;
1856                 }
1857               connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
1858             }
1859         }
1860
1861     }
1862
1863   connect_attempts += smallWorldConnections;
1864
1865   return connect_attempts;
1866 }
1867
1868 /**
1869  * Create a topology given a peer group (set of running peers)
1870  * and a connection processor.
1871  *
1872  * @param pg the peergroup to create the topology on
1873  * @param proc the connection processor to call to actually set
1874  *        up connections between two peers
1875  * @param list the peer list to use
1876  *
1877  * @return the number of connections that were set up
1878  *
1879  */
1880 static unsigned int
1881 create_nated_internet(struct GNUNET_TESTING_PeerGroup *pg,
1882                       GNUNET_TESTING_ConnectionProcessor proc,
1883                       enum PeerLists list)
1884 {
1885   unsigned int outer_count, inner_count;
1886   unsigned int cutoff;
1887   int connect_attempts;
1888   double nat_percentage;
1889   char *p_string;
1890
1891   nat_percentage = 0.6; /* FIXME: default percentage? */
1892   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1893                                                           "PERCENTAGE",
1894                                                           &p_string))
1895     {
1896       if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1897         GNUNET_log (
1898                     GNUNET_ERROR_TYPE_WARNING,
1899                     _
1900                     ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1901                     p_string, "PERCENTAGE", "TESTING");
1902       GNUNET_free (p_string);
1903     }
1904
1905   cutoff = (unsigned int) (nat_percentage * pg->total);
1906   connect_attempts = 0;
1907   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1908     {
1909       for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1910         {
1911           if ((outer_count > cutoff) || (inner_count > cutoff))
1912             {
1913 #if VERBOSE_TESTING
1914               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915                   "Connecting peer %d to peer %d\n",
1916                   outer_count, inner_count);
1917 #endif
1918               connect_attempts += proc (pg, outer_count, inner_count, list,
1919                                         GNUNET_YES);
1920             }
1921         }
1922     }
1923   return connect_attempts;
1924 }
1925
1926 #if TOPOLOGY_HACK
1927 /**
1928  * Create a topology given a peer group (set of running peers)
1929  * and a connection processor.
1930  *
1931  * @param pg the peergroup to create the topology on
1932  * @param proc the connection processor to call to actually set
1933  *        up connections between two peers
1934  * @param list the peer list to use
1935  *
1936  * @return the number of connections that were set up
1937  *
1938  */
1939 static unsigned int
1940 create_nated_internet_copy(struct GNUNET_TESTING_PeerGroup *pg,
1941                            GNUNET_TESTING_ConnectionProcessor proc,
1942                            enum PeerLists list)
1943 {
1944   unsigned int outer_count, inner_count;
1945   unsigned int cutoff;
1946   int connect_attempts;
1947   double nat_percentage;
1948   char *p_string;
1949   unsigned int count;
1950   struct ProgressMeter *conn_meter;
1951
1952   nat_percentage = 0.6; /* FIXME: default percentage? */
1953   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
1954                                                           "PERCENTAGE",
1955                                                           &p_string))
1956     {
1957       if (sscanf (p_string, "%lf", &nat_percentage) != 1)
1958         GNUNET_log (
1959                     GNUNET_ERROR_TYPE_WARNING,
1960                     _
1961                     ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1962                     p_string, "PERCENTAGE", "TESTING");
1963       GNUNET_free (p_string);
1964     }
1965
1966   cutoff = (unsigned int) (nat_percentage * pg->total);
1967   count = 0;
1968   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1969     {
1970       for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1971         {
1972           if ((outer_count > cutoff) || (inner_count > cutoff))
1973             {
1974               count++;
1975             }
1976         }
1977     }
1978   conn_meter = create_meter (count, "NAT COPY", GNUNET_YES);
1979   connect_attempts = 0;
1980   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
1981     {
1982       for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
1983         {
1984           if ((outer_count > cutoff) || (inner_count > cutoff))
1985             {
1986 #if VERBOSE_TESTING
1987               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1988                   "Connecting peer %d to peer %d\n",
1989                   outer_count, inner_count);
1990 #endif
1991               connect_attempts += proc (pg, outer_count, inner_count, list,
1992                                         GNUNET_YES);
1993               add_connections (pg, outer_count, inner_count, ALLOWED, GNUNET_NO);
1994               update_meter (conn_meter);
1995             }
1996         }
1997     }
1998   free_meter (conn_meter);
1999
2000   return connect_attempts;
2001 }
2002 #endif
2003
2004 /**
2005  * Create a topology given a peer group (set of running peers)
2006  * and a connection processor.
2007  *
2008  * @param pg the peergroup to create the topology on
2009  * @param proc the connection processor to call to actually set
2010  *        up connections between two peers
2011  * @param list the peer list to use
2012  *
2013  * @return the number of connections that were set up
2014  *
2015  */
2016 static unsigned int
2017 create_small_world(struct GNUNET_TESTING_PeerGroup *pg,
2018                    GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2019 {
2020   unsigned int i, j, k;
2021   unsigned int square;
2022   unsigned int rows;
2023   unsigned int cols;
2024   unsigned int toggle = 1;
2025   unsigned int nodeToConnect;
2026   unsigned int natLog;
2027   unsigned int node1Row;
2028   unsigned int node1Col;
2029   unsigned int node2Row;
2030   unsigned int node2Col;
2031   unsigned int distance;
2032   double probability, random, percentage;
2033   unsigned int smallWorldConnections;
2034   unsigned int small_world_it;
2035   char *p_string;
2036   int connect_attempts;
2037   square = floor (sqrt (pg->total));
2038   rows = square;
2039   cols = square;
2040
2041   percentage = 0.5; /* FIXME: default percentage? */
2042   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2043                                                           "PERCENTAGE",
2044                                                           &p_string))
2045     {
2046       if (sscanf (p_string, "%lf", &percentage) != 1)
2047         GNUNET_log (
2048                     GNUNET_ERROR_TYPE_WARNING,
2049                     _
2050                     ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2051                     p_string, "PERCENTAGE", "TESTING");
2052       GNUNET_free (p_string);
2053     }
2054   if (percentage < 0.0)
2055     {
2056       GNUNET_log (
2057                   GNUNET_ERROR_TYPE_WARNING,
2058                   _
2059                   ("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
2060                   "PERCENTAGE", "TESTING", percentage);
2061       percentage = 0.5;
2062     }
2063   probability = 0.5; /* FIXME: default percentage? */
2064   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2065                                                           "PROBABILITY",
2066                                                           &p_string))
2067     {
2068       if (sscanf (p_string, "%lf", &probability) != 1)
2069         GNUNET_log (
2070                     GNUNET_ERROR_TYPE_WARNING,
2071                     _
2072                     ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2073                     p_string, "PROBABILITY", "TESTING");
2074       GNUNET_free (p_string);
2075     }
2076   if (square * square != pg->total)
2077     {
2078       while (rows * cols < pg->total)
2079         {
2080           if (toggle % 2 == 0)
2081             rows++;
2082           else
2083             cols++;
2084
2085           toggle++;
2086         }
2087     }
2088 #if VERBOSE_TESTING
2089   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090       _
2091       ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2092       rows, cols);
2093 #endif
2094
2095   connect_attempts = 0;
2096   /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2097    * to the node to its right and above.  Once this is over, we'll have our torus!
2098    * Special case for the last node (if the rows and columns are not equal), connect
2099    * to the first in the row to maintain topology.
2100    */
2101   for (i = 0; i < pg->total; i++)
2102     {
2103       /* First connect to the node to the right */
2104       if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2105         nodeToConnect = i + 1;
2106       else if (i + 1 == pg->total)
2107         nodeToConnect = rows * cols - cols;
2108       else
2109         nodeToConnect = i - cols + 1;
2110
2111       connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2112
2113       if (i < cols)
2114         {
2115           nodeToConnect = (rows * cols) - cols + i;
2116           if (nodeToConnect >= pg->total)
2117             nodeToConnect -= cols;
2118         }
2119       else
2120         nodeToConnect = i - cols;
2121
2122       if (nodeToConnect < pg->total)
2123         connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2124     }
2125   natLog = log (pg->total);
2126 #if VERBOSE_TESTING > 2
2127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128       _("natural log of %d is %d, will run %d iterations\n"),
2129       pg->total, natLog, (int) (natLog * percentage));
2130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131       _("Total connections added thus far: %u!\n"), connect_attempts);
2132 #endif
2133   smallWorldConnections = 0;
2134   small_world_it = (unsigned int) (natLog * percentage);
2135   if (small_world_it < 1)
2136     small_world_it = 1;
2137   GNUNET_assert (small_world_it > 0 && small_world_it < (unsigned int) -1);
2138   for (i = 0; i < small_world_it; i++)
2139     {
2140       for (j = 0; j < pg->total; j++)
2141         {
2142           /* Determine the row and column of node at position j on the 2d torus */
2143           node1Row = j / cols;
2144           node1Col = j - (node1Row * cols);
2145           for (k = 0; k < pg->total; k++)
2146             {
2147               /* Determine the row and column of node at position k on the 2d torus */
2148               node2Row = k / cols;
2149               node2Col = k - (node2Row * cols);
2150               /* Simple Cartesian distance */
2151               distance = abs (node1Row - node2Row) + abs (node1Col - node2Col);
2152               if (distance > 1)
2153                 {
2154                   /* Calculate probability as 1 over the square of the distance */
2155                   probability = 1.0 / (distance * distance);
2156                   /* Choose a random value between 0 and 1 */
2157                   random
2158                       = ((double) GNUNET_CRYPTO_random_u64 (
2159                                                             GNUNET_CRYPTO_QUALITY_WEAK,
2160                                                             UINT64_MAX))
2161                           / ((double) UINT64_MAX);
2162                   /* If random < probability, then connect the two nodes */
2163                   if (random < probability)
2164                     smallWorldConnections += proc (pg, j, k, list, GNUNET_YES);
2165
2166                 }
2167             }
2168         }
2169     }
2170   connect_attempts += smallWorldConnections;
2171 #if VERBOSE_TESTING > 2
2172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2173       _("Total connections added for small world: %d!\n"),
2174       smallWorldConnections);
2175 #endif
2176   return connect_attempts;
2177 }
2178
2179 /**
2180  * Create a topology given a peer group (set of running peers)
2181  * and a connection processor.
2182  *
2183  * @param pg the peergroup to create the topology on
2184  * @param proc the connection processor to call to actually set
2185  *        up connections between two peers
2186  * @param list the peer list to use
2187  *
2188  * @return the number of connections that were set up
2189  *
2190  */
2191 static unsigned int
2192 create_erdos_renyi(struct GNUNET_TESTING_PeerGroup *pg,
2193                    GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2194 {
2195   double temp_rand;
2196   unsigned int outer_count;
2197   unsigned int inner_count;
2198   int connect_attempts;
2199   double probability;
2200   char *p_string;
2201
2202   probability = 0.5; /* FIXME: default percentage? */
2203   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (pg->cfg, "TESTING",
2204                                                           "PROBABILITY",
2205                                                           &p_string))
2206     {
2207       if (sscanf (p_string, "%lf", &probability) != 1)
2208         GNUNET_log (
2209                     GNUNET_ERROR_TYPE_WARNING,
2210                     _
2211                     ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
2212                     p_string, "PROBABILITY", "TESTING");
2213       GNUNET_free (p_string);
2214     }
2215   connect_attempts = 0;
2216   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2217     {
2218       for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2219         {
2220           temp_rand
2221               = ((double) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
2222                                                     UINT64_MAX))
2223                   / ((double) UINT64_MAX);
2224 #if VERBOSE_TESTING
2225           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2226               _("rand is %f probability is %f\n"), temp_rand,
2227               probability);
2228 #endif
2229           if (temp_rand < probability)
2230             {
2231               connect_attempts += proc (pg, outer_count, inner_count, list,
2232                                         GNUNET_YES);
2233             }
2234         }
2235     }
2236
2237   return connect_attempts;
2238 }
2239
2240 /**
2241  * Create a topology given a peer group (set of running peers)
2242  * and a connection processor.  This particular function creates
2243  * the connections for a 2d-torus, plus additional "closest"
2244  * connections per peer.
2245  *
2246  * @param pg the peergroup to create the topology on
2247  * @param proc the connection processor to call to actually set
2248  *        up connections between two peers
2249  * @param list the peer list to use
2250  *
2251  * @return the number of connections that were set up
2252  *
2253  */
2254 static unsigned int
2255 create_2d_torus(struct GNUNET_TESTING_PeerGroup *pg,
2256                 GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2257 {
2258   unsigned int i;
2259   unsigned int square;
2260   unsigned int rows;
2261   unsigned int cols;
2262   unsigned int toggle = 1;
2263   unsigned int nodeToConnect;
2264   int connect_attempts;
2265
2266   connect_attempts = 0;
2267
2268   square = floor (sqrt (pg->total));
2269   rows = square;
2270   cols = square;
2271
2272   if (square * square != pg->total)
2273     {
2274       while (rows * cols < pg->total)
2275         {
2276           if (toggle % 2 == 0)
2277             rows++;
2278           else
2279             cols++;
2280
2281           toggle++;
2282         }
2283     }
2284 #if VERBOSE_TESTING
2285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2286       _
2287       ("Connecting nodes in 2d torus topology: %u rows %u columns\n"),
2288       rows, cols);
2289 #endif
2290   /* Rows and columns are all sorted out, now iterate over all nodes and connect each
2291    * to the node to its right and above.  Once this is over, we'll have our torus!
2292    * Special case for the last node (if the rows and columns are not equal), connect
2293    * to the first in the row to maintain topology.
2294    */
2295   for (i = 0; i < pg->total; i++)
2296     {
2297       /* First connect to the node to the right */
2298       if (((i + 1) % cols != 0) && (i + 1 != pg->total))
2299         nodeToConnect = i + 1;
2300       else if (i + 1 == pg->total)
2301         nodeToConnect = rows * cols - cols;
2302       else
2303         nodeToConnect = i - cols + 1;
2304 #if VERBOSE_TESTING
2305       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306           "Connecting peer %d to peer %d\n", i, nodeToConnect);
2307 #endif
2308       connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2309
2310       /* Second connect to the node immediately above */
2311       if (i < cols)
2312         {
2313           nodeToConnect = (rows * cols) - cols + i;
2314           if (nodeToConnect >= pg->total)
2315             nodeToConnect -= cols;
2316         }
2317       else
2318         nodeToConnect = i - cols;
2319
2320       if (nodeToConnect < pg->total)
2321         {
2322 #if VERBOSE_TESTING
2323           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324               "Connecting peer %d to peer %d\n", i, nodeToConnect);
2325 #endif
2326           connect_attempts += proc (pg, i, nodeToConnect, list, GNUNET_YES);
2327         }
2328
2329     }
2330
2331   return connect_attempts;
2332 }
2333
2334 /**
2335  * Create a topology given a peer group (set of running peers)
2336  * and a connection processor.
2337  *
2338  * @param pg the peergroup to create the topology on
2339  * @param proc the connection processor to call to actually set
2340  *        up connections between two peers
2341  * @param list the peer list to use
2342  * @param check does the connection processor need to check before
2343  *              performing an action on the list?
2344  *
2345  * @return the number of connections that were set up
2346  *
2347  */
2348 static unsigned int
2349 create_clique(struct GNUNET_TESTING_PeerGroup *pg,
2350               GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list,
2351               unsigned int check)
2352 {
2353   unsigned int outer_count;
2354   unsigned int inner_count;
2355   int connect_attempts;
2356   struct ProgressMeter *conn_meter;
2357   connect_attempts = 0;
2358
2359   conn_meter = create_meter ((((pg->total * pg->total) + pg->total) / 2)
2360       - pg->total, "Create Clique ", GNUNET_YES);
2361   for (outer_count = 0; outer_count < pg->total - 1; outer_count++)
2362     {
2363       for (inner_count = outer_count + 1; inner_count < pg->total; inner_count++)
2364         {
2365 #if VERBOSE_TESTING
2366           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367               "Connecting peer %d to peer %d\n",
2368               outer_count, inner_count);
2369 #endif
2370           connect_attempts += proc (pg, outer_count, inner_count, list, check);
2371           update_meter (conn_meter);
2372         }
2373     }
2374   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Meter has %d left\n",
2375               conn_meter->total - conn_meter->completed);
2376   reset_meter (conn_meter);
2377   free_meter (conn_meter);
2378   return connect_attempts;
2379 }
2380
2381 #if !OLD
2382 /**
2383  * Iterator over hash map entries.
2384  *
2385  * @param cls closure the peer group
2386  * @param key the key stored in the hashmap is the
2387  *            index of the peer to connect to
2388  * @param value value in the hash map, handle to the peer daemon
2389  * @return GNUNET_YES if we should continue to
2390  *         iterate,
2391  *         GNUNET_NO if not.
2392  */
2393 static int
2394 unblacklist_iterator (void *cls,
2395     const GNUNET_HashCode * key,
2396     void *value)
2397   {
2398     struct UnblacklistContext *un_ctx = cls;
2399     uint32_t second_pos;
2400
2401     uid_from_hash (key, &second_pos);
2402
2403     unblacklist_connections(un_ctx->pg, un_ctx->first_uid, second_pos);
2404
2405     return GNUNET_YES;
2406   }
2407 #endif
2408
2409 /**
2410  * Create a blacklist topology based on the allowed topology
2411  * which disallows any connections not in the allowed topology
2412  * at the transport level.
2413  *
2414  * @param pg the peergroup to create the topology on
2415  * @param proc the connection processor to call to allow
2416  *        up connections between two peers
2417  *
2418  * @return the number of connections that were set up
2419  *
2420  */
2421 static unsigned int
2422 copy_allowed(struct GNUNET_TESTING_PeerGroup *pg,
2423              GNUNET_TESTING_ConnectionProcessor proc)
2424 {
2425   struct UnblacklistContext un_ctx;
2426   unsigned int count;
2427   unsigned int total;
2428   struct PeerConnection *iter;
2429
2430   un_ctx.pg = pg;
2431   total = 0;
2432   for (count = 0; count < pg->total - 1; count++)
2433     {
2434       un_ctx.first_uid = count;
2435 #if OLD
2436       iter = pg->peers[count].allowed_peers_head;
2437       while (iter != NULL)
2438         {
2439           remove_connections (pg, count, iter->index, BLACKLIST, GNUNET_YES);
2440           //unblacklist_connections(pg, count, iter->index);
2441           iter = iter->next;
2442         }
2443 #else
2444       total += GNUNET_CONTAINER_multihashmap_iterate(pg->peers[count].allowed_peers, &unblacklist_iterator, &un_ctx);
2445 #endif
2446     }
2447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unblacklisted %u peers\n", total);
2448   return total;
2449 }
2450
2451 /**
2452  * Create a topology given a peer group (set of running peers)
2453  * and a connection processor.
2454  *
2455  * @param pg the peergroup to create the topology on
2456  * @param proc the connection processor to call to actually set
2457  *        up connections between two peers
2458  * @param list which list should be modified
2459  *
2460  * @return the number of connections that were set up
2461  *
2462  */
2463 static unsigned int
2464 create_line(struct GNUNET_TESTING_PeerGroup *pg,
2465             GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2466 {
2467   unsigned int count;
2468   int connect_attempts;
2469
2470   connect_attempts = 0;
2471
2472   /* Connect each peer to the next highest numbered peer */
2473   for (count = 0; count < pg->total - 1; count++)
2474     {
2475 #if VERBOSE_TESTING
2476       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477           "Connecting peer %d to peer %d\n", count, count + 1);
2478 #endif
2479       connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2480     }
2481
2482   return connect_attempts;
2483 }
2484
2485 /**
2486  * Create a topology given a peer group (set of running peers)
2487  * and a connection processor.
2488  *
2489  * @param pg the peergroup to create the topology on
2490  * @param filename the file to read topology information from
2491  * @param proc the connection processor to call to actually set
2492  *        up connections between two peers
2493  * @param list the peer list to use
2494  *
2495  * @return the number of connections that were set up
2496  *
2497  */
2498 static unsigned int
2499 create_from_file(struct GNUNET_TESTING_PeerGroup *pg, char *filename,
2500                  GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2501 {
2502   int connect_attempts;
2503   unsigned int first_peer_index;
2504   unsigned int second_peer_index;
2505   connect_attempts = 0;
2506   struct stat frstat;
2507   int count;
2508   char *data;
2509   char *buf;
2510   unsigned int total_peers;
2511
2512   enum States curr_state;
2513
2514   if (GNUNET_OK != GNUNET_DISK_file_test (filename))
2515     GNUNET_DISK_fn_write (filename, NULL, 0, GNUNET_DISK_PERM_USER_READ);
2516
2517   if ((0 != STAT (filename, &frstat)) || (frstat.st_size == 0))
2518     {
2519       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2520                   "Could not open file `%s' specified for topology!", filename);
2521       return connect_attempts;
2522     }
2523
2524   data = GNUNET_malloc_large (frstat.st_size);
2525   GNUNET_assert(data != NULL);
2526   if (frstat.st_size != GNUNET_DISK_fn_read (filename, data, frstat.st_size))
2527     {
2528       GNUNET_log (
2529                   GNUNET_ERROR_TYPE_ERROR,
2530                   "Could not read file %s specified for host list, ending test!",
2531                   filename);
2532       GNUNET_free (data);
2533       return connect_attempts;
2534     }
2535
2536   buf = data;
2537   count = 0;
2538   first_peer_index = 0;
2539   /* First line should contain a single integer, specifying the number of peers */
2540   /* Each subsequent line should contain this format PEER_INDEX:OTHER_PEER_INDEX[,...] */
2541   curr_state = NUM_PEERS;
2542   while (count < frstat.st_size - 1)
2543     {
2544       if ((buf[count] == '\n') || (buf[count] == ' '))
2545         {
2546           count++;
2547           continue;
2548         }
2549
2550       switch (curr_state)
2551         {
2552       case NUM_PEERS:
2553         errno = 0;
2554         total_peers = strtoul(&buf[count], NULL, 10);
2555         if (errno != 0)
2556           {
2557             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2558                         "Failed to read number of peers from topology file!\n");
2559             GNUNET_free_non_null(data);
2560             return connect_attempts;
2561           }
2562         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2563                     "Read %u total peers in topology\n", total_peers);
2564         GNUNET_assert(total_peers == pg->total);
2565         curr_state = PEER_INDEX;
2566         while ((buf[count] != '\n') && (count < frstat.st_size - 1))
2567           count++;
2568         count++;
2569         break;
2570       case PEER_INDEX:
2571         errno = 0;
2572         first_peer_index = strtoul(&buf[count], NULL, 10);
2573         if (errno != 0)
2574           {
2575             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2576                         "Failed to read peer index from topology file!\n");
2577             GNUNET_free_non_null(data);
2578             return connect_attempts;
2579           }
2580         while ((buf[count] != ':') && (count < frstat.st_size - 1))
2581           count++;
2582         count++;
2583         curr_state = OTHER_PEER_INDEX;
2584         break;
2585       case COLON:
2586         if (1 == sscanf (&buf[count], ":"))
2587           curr_state = OTHER_PEER_INDEX;
2588         count++;
2589         break;
2590       case OTHER_PEER_INDEX:
2591         errno = 0;
2592         second_peer_index = strtoul(&buf[count], NULL, 10);
2593         if (errno != 0)
2594           {
2595             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2596                         "Failed to peer index from topology file!\n");
2597             GNUNET_free_non_null(data);
2598             return connect_attempts;
2599           }
2600         /* Assume file is written with first peer 1, but array index is 0 */
2601         connect_attempts += proc (pg, first_peer_index - 1, second_peer_index
2602                                   - 1, list, GNUNET_YES);
2603         while ((buf[count] != '\n') && (buf[count] != ',') && (count
2604             < frstat.st_size - 1))
2605           count++;
2606         if (buf[count] == '\n')
2607           {
2608             curr_state = PEER_INDEX;
2609           }
2610         else if (buf[count] != ',')
2611           {
2612             curr_state = OTHER_PEER_INDEX;
2613           }
2614         count++;
2615         break;
2616       default:
2617         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2618                     "Found bad data in topology file while in state %d!\n",
2619                     curr_state);
2620         GNUNET_break(0);
2621         return connect_attempts;
2622         }
2623
2624     }
2625   return connect_attempts;
2626 }
2627
2628 /**
2629  * Create a topology given a peer group (set of running peers)
2630  * and a connection processor.
2631  *
2632  * @param pg the peergroup to create the topology on
2633  * @param proc the connection processor to call to actually set
2634  *        up connections between two peers
2635  * @param list the peer list to use
2636  *
2637  * @return the number of connections that were set up
2638  *
2639  */
2640 static unsigned int
2641 create_ring(struct GNUNET_TESTING_PeerGroup *pg,
2642             GNUNET_TESTING_ConnectionProcessor proc, enum PeerLists list)
2643 {
2644   unsigned int count;
2645   int connect_attempts;
2646
2647   connect_attempts = 0;
2648
2649   /* Connect each peer to the next highest numbered peer */
2650   for (count = 0; count < pg->total - 1; count++)
2651     {
2652 #if VERBOSE_TESTING
2653       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654           "Connecting peer %d to peer %d\n", count, count + 1);
2655 #endif
2656       connect_attempts += proc (pg, count, count + 1, list, GNUNET_YES);
2657     }
2658
2659   /* Connect the last peer to the first peer */
2660   connect_attempts += proc (pg, pg->total - 1, 0, list, GNUNET_YES);
2661
2662   return connect_attempts;
2663 }
2664
2665 #if !OLD
2666 /**
2667  * Iterator for writing friends of a peer to a file.
2668  *
2669  * @param cls closure, an open writable file handle
2670  * @param key the key the daemon was stored under
2671  * @param value the GNUNET_TESTING_Daemon that needs to be written.
2672  *
2673  * @return GNUNET_YES to continue iteration
2674  *
2675  * TODO: Could replace friend_file_iterator and blacklist_file_iterator
2676  *       with a single file_iterator that takes a closure which contains
2677  *       the prefix to write before the peer.  Then this could be used
2678  *       for blacklisting multiple transports and writing the friend
2679  *       file.  I'm sure *someone* will complain loudly about other
2680  *       things that negate these functions even existing so no point in
2681  *       "fixing" now.
2682  */
2683 static int
2684 friend_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2685   {
2686     FILE *temp_friend_handle = cls;
2687     struct GNUNET_TESTING_Daemon *peer = value;
2688     struct GNUNET_PeerIdentity *temppeer;
2689     struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2690
2691     temppeer = &peer->id;
2692     GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2693     fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2694
2695     return GNUNET_YES;
2696   }
2697
2698 struct BlacklistContext
2699   {
2700     /*
2701      * The (open) file handle to write to
2702      */
2703     FILE *temp_file_handle;
2704
2705     /*
2706      * The transport that this peer will be blacklisted on.
2707      */
2708     char *transport;
2709   };
2710
2711 /**
2712  * Iterator for writing blacklist data to appropriate files.
2713  *
2714  * @param cls closure, an open writable file handle
2715  * @param key the key the daemon was stored under
2716  * @param value the GNUNET_TESTING_Daemon that needs to be written.
2717  *
2718  * @return GNUNET_YES to continue iteration
2719  */
2720 static int
2721 blacklist_file_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2722   {
2723     struct BlacklistContext *blacklist_ctx = cls;
2724     struct GNUNET_TESTING_Daemon *peer = value;
2725     struct GNUNET_PeerIdentity *temppeer;
2726     struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2727
2728     temppeer = &peer->id;
2729     GNUNET_CRYPTO_hash_to_enc (&temppeer->hashPubKey, &peer_enc);
2730     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Writing entry %s:%s to file\n", blacklist_ctx->transport, (char *) &peer_enc);
2731     fprintf (blacklist_ctx->temp_file_handle, "%s:%s\n",
2732         blacklist_ctx->transport, (char *) &peer_enc);
2733
2734     return GNUNET_YES;
2735   }
2736 #endif
2737
2738 /*
2739  * Create the friend files based on the PeerConnection's
2740  * of each peer in the peer group, and copy the files
2741  * to the appropriate place
2742  *
2743  * @param pg the peer group we are dealing with
2744  */
2745 static int
2746 create_and_copy_friend_files(struct GNUNET_TESTING_PeerGroup *pg)
2747 {
2748   FILE *temp_friend_handle;
2749   unsigned int pg_iter;
2750   char *temp_service_path;
2751   struct GNUNET_OS_Process **procarr;
2752   char *arg;
2753   char *mytemp;
2754 #if NOT_STUPID
2755   enum GNUNET_OS_ProcessStatusType type;
2756   unsigned long return_code;
2757   int count;
2758   int max_wait = 10;
2759 #endif
2760   int ret;
2761
2762   ret = GNUNET_OK;
2763 #if OLD
2764   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2765   struct PeerConnection *conn_iter;
2766 #endif
2767   procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2768   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2769     {
2770       mytemp = GNUNET_DISK_mktemp ("friends");
2771       GNUNET_assert (mytemp != NULL);
2772       temp_friend_handle = fopen (mytemp, "wt");
2773       GNUNET_assert (temp_friend_handle != NULL);
2774 #if OLD
2775       conn_iter = pg->peers[pg_iter].allowed_peers_head;
2776       while (conn_iter != NULL)
2777         {
2778           GNUNET_CRYPTO_hash_to_enc (
2779                                      &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2780                                      &peer_enc);
2781           fprintf (temp_friend_handle, "%s\n", (char *) &peer_enc);
2782           conn_iter = conn_iter->next;
2783         }
2784 #else
2785       GNUNET_CONTAINER_multihashmap_iterate (pg->peers[pg_iter].allowed_peers,
2786           &friend_file_iterator,
2787           temp_friend_handle);
2788 #endif
2789       fclose (temp_friend_handle);
2790
2791       if (GNUNET_OK
2792           != GNUNET_CONFIGURATION_get_value_string (
2793                                                     pg->peers[pg_iter]. daemon->cfg,
2794                                                     "PATHS", "SERVICEHOME",
2795                                                     &temp_service_path))
2796         {
2797           GNUNET_log (
2798                       GNUNET_ERROR_TYPE_WARNING,
2799                       _
2800                       ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
2801                       "SERVICEHOME", "PATHS");
2802           if (UNLINK (mytemp) != 0)
2803             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2804                 mytemp);
2805           GNUNET_free (mytemp);
2806           break;
2807         }
2808
2809       if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
2810         {
2811           GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
2812           procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
2813                                                       mytemp, arg, NULL);
2814 #if VERBOSE_TESTING
2815           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2816               _("Copying file with command cp %s %s\n"), mytemp, arg);
2817 #endif
2818           ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2819           GNUNET_OS_process_close (procarr[pg_iter]);
2820           GNUNET_free (arg);
2821         }
2822       else /* Remote, scp the file to the correct place */
2823         {
2824           if (NULL != pg->peers[pg_iter].daemon->username)
2825             GNUNET_asprintf (&arg, "%s@%s:%s/friends",
2826                              pg->peers[pg_iter].daemon->username,
2827                              pg->peers[pg_iter].daemon->hostname,
2828                              temp_service_path);
2829           else
2830             GNUNET_asprintf (&arg, "%s:%s/friends",
2831                              pg->peers[pg_iter].daemon->hostname,
2832                              temp_service_path);
2833           procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
2834                                                       mytemp, arg, NULL);
2835
2836           ret = GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: schedule this, throttle! */
2837           GNUNET_OS_process_close (procarr[pg_iter]);
2838           if (ret != GNUNET_OK)
2839             return ret;
2840           procarr[pg_iter] = NULL;
2841 #if VERBOSE_TESTING
2842           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843               _("Copying file with command scp %s %s\n"), mytemp,
2844               arg);
2845 #endif
2846           GNUNET_free (arg);
2847         }
2848       GNUNET_free (temp_service_path);
2849       GNUNET_free (mytemp);
2850     }
2851
2852 #if NOT_STUPID
2853   count = 0;
2854   ret = GNUNET_SYSERR;
2855   while ((count < max_wait) && (ret != GNUNET_OK))
2856     {
2857       ret = GNUNET_OK;
2858       for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2859         {
2860 #if VERBOSE_TESTING
2861           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2862               _("Checking copy status of file %d\n"), pg_iter);
2863 #endif
2864           if (procarr[pg_iter] != NULL) /* Check for already completed! */
2865             {
2866               if (GNUNET_OS_process_status
2867                   (procarr[pg_iter], &type, &return_code) != GNUNET_OK)
2868                 {
2869                   ret = GNUNET_SYSERR;
2870                 }
2871               else if ((type != GNUNET_OS_PROCESS_EXITED)
2872                   || (return_code != 0))
2873                 {
2874                   ret = GNUNET_SYSERR;
2875                 }
2876               else
2877                 {
2878                   GNUNET_OS_process_close (procarr[pg_iter]);
2879                   procarr[pg_iter] = NULL;
2880 #if VERBOSE_TESTING
2881                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2882                       _("File %d copied\n"), pg_iter);
2883 #endif
2884                 }
2885             }
2886         }
2887       count++;
2888       if (ret == GNUNET_SYSERR)
2889         {
2890           /* FIXME: why sleep here? -CG */
2891           sleep (1);
2892         }
2893     }
2894
2895 #if VERBOSE_TESTING
2896   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2897       _("Finished copying all friend files!\n"));
2898 #endif
2899 #endif
2900   GNUNET_free (procarr);
2901   return ret;
2902 }
2903
2904 /*
2905  * Create the blacklist files based on the PeerConnection's
2906  * of each peer in the peer group, and copy the files
2907  * to the appropriate place.
2908  *
2909  * @param pg the peer group we are dealing with
2910  * @param transports space delimited list of transports to blacklist
2911  */
2912 static int
2913 create_and_copy_blacklist_files(struct GNUNET_TESTING_PeerGroup *pg,
2914                                 const char *transports)
2915 {
2916   FILE *temp_file_handle;
2917   unsigned int pg_iter;
2918   char *temp_service_path;
2919   struct GNUNET_OS_Process **procarr;
2920   char *arg;
2921   char *mytemp;
2922   enum GNUNET_OS_ProcessStatusType type;
2923   unsigned long return_code;
2924   int count;
2925   int ret;
2926   int max_wait = 10;
2927   int transport_len;
2928   unsigned int i;
2929   char *pos;
2930   char *temp_transports;
2931   int entry_count;
2932 #if OLD
2933   struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
2934   struct PeerConnection *conn_iter;
2935 #else
2936   static struct BlacklistContext blacklist_ctx;
2937 #endif
2938
2939   procarr = GNUNET_malloc (sizeof (struct GNUNET_OS_Process *) * pg->total);
2940   for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
2941     {
2942       mytemp = GNUNET_DISK_mktemp ("blacklist");
2943       GNUNET_assert (mytemp != NULL);
2944       temp_file_handle = fopen (mytemp, "wt");
2945       GNUNET_assert (temp_file_handle != NULL);
2946       temp_transports = GNUNET_strdup (transports);
2947 #if !OLD
2948       blacklist_ctx.temp_file_handle = temp_file_handle;
2949 #endif
2950       transport_len = strlen (temp_transports) + 1;
2951       pos = NULL;
2952
2953       for (i = 0; i < transport_len; i++)
2954         {
2955           if ((temp_transports[i] == ' ') && (pos == NULL))
2956             continue; /* At start of string (whitespace) */
2957           else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
2958             {
2959               temp_transports[i] = '\0';
2960 #if OLD
2961               conn_iter = pg->peers[pg_iter].blacklisted_peers_head;
2962               while (conn_iter != NULL)
2963                 {
2964                   GNUNET_CRYPTO_hash_to_enc (
2965                                              &pg->peers[conn_iter->index].daemon->id.hashPubKey,
2966                                              &peer_enc);
2967                   fprintf (temp_file_handle, "%s:%s\n", pos, (char *) &peer_enc);
2968                   conn_iter = conn_iter->next;
2969                   entry_count++;
2970                 }
2971 #else
2972               blacklist_ctx.transport = pos;
2973               entry_count = GNUNET_CONTAINER_multihashmap_iterate (pg->
2974                   peers
2975                   [pg_iter].blacklisted_peers,
2976                   &blacklist_file_iterator,
2977                   &blacklist_ctx);
2978 #endif
2979               pos = NULL;
2980             } /* At beginning of actual string */
2981           else if (pos == NULL)
2982             {
2983               pos = &temp_transports[i];
2984             }
2985         }
2986
2987       GNUNET_free (temp_transports);
2988       fclose (temp_file_handle);
2989
2990       if (GNUNET_OK
2991           != GNUNET_CONFIGURATION_get_value_string (
2992                                                     pg->peers[pg_iter]. daemon->cfg,
2993                                                     "PATHS", "SERVICEHOME",
2994                                                     &temp_service_path))
2995         {
2996           GNUNET_log (
2997                       GNUNET_ERROR_TYPE_WARNING,
2998                       _
2999                       ("No `%s' specified in peer configuration in section `%s', cannot copy friends file!\n"),
3000                       "SERVICEHOME", "PATHS");
3001           if (UNLINK (mytemp) != 0)
3002             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
3003                 mytemp);
3004           GNUNET_free (mytemp);
3005           break;
3006         }
3007
3008       if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
3009         {
3010           GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
3011           procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv", "mv",
3012                                                       mytemp, arg, NULL);
3013 #if VERBOSE_TESTING
3014           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3015               _("Copying file with command cp %s %s\n"), mytemp, arg);
3016 #endif
3017
3018           GNUNET_free (arg);
3019         }
3020       else /* Remote, scp the file to the correct place */
3021         {
3022           if (NULL != pg->peers[pg_iter].daemon->username)
3023             GNUNET_asprintf (&arg, "%s@%s:%s/blacklist",
3024                              pg->peers[pg_iter].daemon->username,
3025                              pg->peers[pg_iter].daemon->hostname,
3026                              temp_service_path);
3027           else
3028             GNUNET_asprintf (&arg, "%s:%s/blacklist",
3029                              pg->peers[pg_iter].daemon->hostname,
3030                              temp_service_path);
3031           procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
3032                                                       mytemp, arg, NULL);
3033
3034           GNUNET_OS_process_wait (procarr[pg_iter]); /* FIXME: add scheduled blacklist file copy that parallelizes file copying! */
3035
3036 #if VERBOSE_TESTING
3037           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038               _("Copying file with command scp %s %s\n"), mytemp,
3039               arg);
3040 #endif
3041           GNUNET_free (arg);
3042         }
3043       GNUNET_free (temp_service_path);
3044       GNUNET_free (mytemp);
3045     }
3046
3047   count = 0;
3048   ret = GNUNET_SYSERR;
3049   while ((count < max_wait) && (ret != GNUNET_OK))
3050     {
3051       ret = GNUNET_OK;
3052       for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
3053         {
3054 #if VERBOSE_TESTING
3055           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3056               _("Checking copy status of file %d\n"), pg_iter);
3057 #endif
3058           if (procarr[pg_iter] != NULL) /* Check for already completed! */
3059             {
3060               if (GNUNET_OS_process_status (procarr[pg_iter], &type,
3061                                             &return_code) != GNUNET_OK)
3062                 {
3063                   ret = GNUNET_SYSERR;
3064                 }
3065               else if ((type != GNUNET_OS_PROCESS_EXITED) || (return_code != 0))
3066                 {
3067                   ret = GNUNET_SYSERR;
3068                 }
3069               else
3070                 {
3071                   GNUNET_OS_process_close (procarr[pg_iter]);
3072                   procarr[pg_iter] = NULL;
3073 #if VERBOSE_TESTING
3074                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3075                       _("File %d copied\n"), pg_iter);
3076 #endif
3077                 }
3078             }
3079         }
3080       count++;
3081       if (ret == GNUNET_SYSERR)
3082         {
3083           /* FIXME: why sleep here? -CG */
3084           sleep (1);
3085         }
3086     }
3087
3088 #if VERBOSE_TESTING
3089   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090       _("Finished copying all blacklist files!\n"));
3091 #endif
3092   GNUNET_free (procarr);
3093   return ret;
3094 }
3095
3096 /* Forward Declaration */
3097 static void
3098 schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3099
3100 /**
3101  * Choose a random peer's next connection to create, and
3102  * call schedule_connect to set up the connect task.
3103  *
3104  * @param ct_ctx the overall connection context
3105  */
3106 static void
3107 preschedule_connect(struct GNUNET_TESTING_PeerGroup *pg)
3108 {
3109   struct ConnectTopologyContext *ct_ctx = &pg->ct_ctx;
3110   struct PeerConnection *connection_iter;
3111   struct ConnectContext *connect_context;
3112   uint32_t random_peer;
3113
3114   if (ct_ctx->remaining_connections == 0)
3115     return;
3116   random_peer
3117       = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, pg->total);
3118   while (pg->peers[random_peer].connect_peers_head == NULL)
3119     random_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3120                                             pg->total);
3121
3122   connection_iter = pg->peers[random_peer].connect_peers_head;
3123   connect_context = GNUNET_malloc (sizeof (struct ConnectContext));
3124   connect_context->first_index = random_peer;
3125   connect_context->second_index = connection_iter->index;
3126   connect_context->ct_ctx = ct_ctx;
3127   GNUNET_SCHEDULER_add_now (&schedule_connect, connect_context);
3128   GNUNET_CONTAINER_DLL_remove(pg->peers[random_peer].connect_peers_head, pg->peers[random_peer].connect_peers_tail, connection_iter);
3129   GNUNET_free(connection_iter);
3130   ct_ctx->remaining_connections--;
3131 }
3132
3133 #if USE_SEND_HELLOS
3134 /* Forward declaration */
3135 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3136
3137 /**
3138  * Close connections and free the hello context.
3139  *
3140  * @param cls the 'struct SendHelloContext *'
3141  * @param tc scheduler context
3142  */
3143 static void
3144 free_hello_context (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3145   {
3146     struct SendHelloContext *send_hello_context = cls;
3147     if (send_hello_context->peer->daemon->server != NULL)
3148       {
3149         GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3150         send_hello_context->peer->daemon->server = NULL;
3151       }
3152     if (send_hello_context->peer->daemon->th != NULL)
3153       {
3154         GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3155         send_hello_context->peer->daemon->th = NULL;
3156       }
3157     if (send_hello_context->core_connect_task != GNUNET_SCHEDULER_NO_TASK)
3158       {
3159         GNUNET_SCHEDULER_cancel(send_hello_context->core_connect_task);
3160         send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3161       }
3162     send_hello_context->pg->outstanding_connects--;
3163     GNUNET_free(send_hello_context);
3164   }
3165
3166 /**
3167  * For peers that haven't yet connected, notify
3168  * the caller that they have failed (timeout).
3169  *
3170  * @param cls the 'struct SendHelloContext *'
3171  * @param tc scheduler context
3172  */
3173 static void
3174 notify_remaining_connections_failed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3175   {
3176     struct SendHelloContext *send_hello_context = cls;
3177     struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3178     struct PeerConnection *connection;
3179
3180     GNUNET_CORE_disconnect(send_hello_context->peer->daemon->server);
3181     send_hello_context->peer->daemon->server = NULL;
3182
3183     connection = send_hello_context->peer->connect_peers_head;
3184
3185     while (connection != NULL)
3186       {
3187         if (pg->notify_connection != NULL)
3188           {
3189             pg->notify_connection(pg->notify_connection_cls,
3190                 &send_hello_context->peer->daemon->id,
3191                 &pg->peers[connection->index].daemon->id,
3192                 0, /* FIXME */
3193                 send_hello_context->peer->daemon->cfg,
3194                 pg->peers[connection->index].daemon->cfg,
3195                 send_hello_context->peer->daemon,
3196                 pg->peers[connection->index].daemon,
3197                 "Peers failed to connect (timeout)");
3198           }
3199         GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3200         GNUNET_free(connection);
3201         connection = connection->next;
3202       }
3203     GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3204 #if BAD
3205     other_peer = &pg->peers[connection->index];
3206 #endif
3207   }
3208
3209 /**
3210  * For peers that haven't yet connected, send
3211  * CORE connect requests.
3212  *
3213  * @param cls the 'struct SendHelloContext *'
3214  * @param tc scheduler context
3215  */
3216 static void
3217 send_core_connect_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3218   {
3219     struct SendHelloContext *send_hello_context = cls;
3220     struct PeerConnection *conn;
3221     GNUNET_assert(send_hello_context->peer->daemon->server != NULL);
3222
3223     send_hello_context->core_connect_task = GNUNET_SCHEDULER_NO_TASK;
3224
3225     send_hello_context->connect_attempts++;
3226     if (send_hello_context->connect_attempts < send_hello_context->pg->ct_ctx.connect_attempts)
3227       {
3228         conn = send_hello_context->peer->connect_peers_head;
3229         while (conn != NULL)
3230           {
3231             GNUNET_CORE_peer_request_connect(send_hello_context->peer->daemon->server,
3232                 GNUNET_TIME_relative_get_forever(),
3233                 &send_hello_context->pg->peers[conn->index].daemon->id,
3234                 NULL,
3235                 NULL);
3236             conn = conn->next;
3237           }
3238         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) ,
3239             &send_core_connect_requests,
3240             send_hello_context);
3241       }
3242     else
3243       {
3244         GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Timeout before all connections created, marking rest as failed!\n");
3245         GNUNET_SCHEDULER_add_now(&notify_remaining_connections_failed, send_hello_context);
3246       }
3247
3248   }
3249
3250 /**
3251  * Success, connection is up.  Signal client our success.
3252  *
3253  * @param cls our "struct SendHelloContext"
3254  * @param peer identity of the peer that has connected
3255  * @param atsi performance information
3256  *
3257  * FIXME: remove peers from BOTH lists, call notify twice, should
3258  * double the speed of connections as long as the list iteration
3259  * doesn't take too long!
3260  */
3261 static void
3262 core_connect_notify (void *cls,
3263     const struct GNUNET_PeerIdentity *peer,
3264     const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3265   {
3266     struct SendHelloContext *send_hello_context = cls;
3267     struct PeerConnection *connection;
3268     struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3269 #if BAD
3270     struct PeerData *other_peer;
3271 #endif
3272 #if DEBUG_TESTING
3273     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3274         "Connected peer %s to peer %s\n",
3275         ctx->d1->shortname, GNUNET_i2s(peer));
3276 #endif
3277
3278     if (0 == memcmp(&send_hello_context->peer->daemon->id, peer, sizeof(struct GNUNET_PeerIdentity)))
3279     return;
3280
3281     connection = send_hello_context->peer->connect_peers_head;
3282 #if BAD
3283     other_peer = NULL;
3284 #endif
3285
3286     while ((connection != NULL) &&
3287         (0 != memcmp(&pg->peers[connection->index].daemon->id, peer, sizeof(struct GNUNET_PeerIdentity))))
3288       {
3289         connection = connection->next;
3290       }
3291
3292     if (connection == NULL)
3293       {
3294         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);
3295       }
3296     else
3297       {
3298 #if BAD
3299         other_peer = &pg->peers[connection->index];
3300 #endif
3301         if (pg->notify_connection != NULL)
3302           {
3303             pg->notify_connection(pg->notify_connection_cls,
3304                 &send_hello_context->peer->daemon->id,
3305                 peer,
3306                 0, /* FIXME */
3307                 send_hello_context->peer->daemon->cfg,
3308                 pg->peers[connection->index].daemon->cfg,
3309                 send_hello_context->peer->daemon,
3310                 pg->peers[connection->index].daemon,
3311                 NULL);
3312           }
3313         GNUNET_CONTAINER_DLL_remove(send_hello_context->peer->connect_peers_head, send_hello_context->peer->connect_peers_tail, connection);
3314         GNUNET_free(connection);
3315       }
3316
3317 #if BAD
3318     /* Notify of reverse connection and remove from other peers list of outstanding */
3319     if (other_peer != NULL)
3320       {
3321         connection = other_peer->connect_peers_head;
3322         while ((connection != NULL) &&
3323             (0 != memcmp(&send_hello_context->peer->daemon->id, &pg->peers[connection->index].daemon->id, sizeof(struct GNUNET_PeerIdentity))))
3324           {
3325             connection = connection->next;
3326           }
3327         if (connection != NULL)
3328           {
3329             if (pg->notify_connection != NULL)
3330               {
3331                 pg->notify_connection(pg->notify_connection_cls,
3332                     peer,
3333                     &send_hello_context->peer->daemon->id,
3334                     0, /* FIXME */
3335                     pg->peers[connection->index].daemon->cfg,
3336                     send_hello_context->peer->daemon->cfg,
3337                     pg->peers[connection->index].daemon,
3338                     send_hello_context->peer->daemon,
3339                     NULL);
3340               }
3341
3342             GNUNET_CONTAINER_DLL_remove(other_peer->connect_peers_head, other_peer->connect_peers_tail, connection);
3343             GNUNET_free(connection);
3344           }
3345       }
3346 #endif
3347
3348     if (send_hello_context->peer->connect_peers_head == NULL)
3349       {
3350         GNUNET_SCHEDULER_add_now(&free_hello_context, send_hello_context);
3351       }
3352   }
3353
3354 /**
3355  * Notify of a successful connection to the core service.
3356  *
3357  * @param cls a struct SendHelloContext *
3358  * @param server handle to the core service
3359  * @param my_identity the peer identity of this peer
3360  * @param publicKey the public key of the peer
3361  */
3362 void
3363 core_init (void *cls,
3364     struct GNUNET_CORE_Handle * server,
3365     const struct GNUNET_PeerIdentity *
3366     my_identity,
3367     const struct
3368     GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
3369     publicKey)
3370   {
3371     struct SendHelloContext *send_hello_context = cls;
3372     send_hello_context->core_ready = GNUNET_YES;
3373   }
3374
3375 /**
3376  * Function called once a hello has been sent
3377  * to the transport, move on to the next one
3378  * or go away forever.
3379  *
3380  * @param cls the 'struct SendHelloContext *'
3381  * @param tc scheduler context
3382  */
3383 static void
3384 hello_sent_callback (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3385   {
3386     struct SendHelloContext *send_hello_context = cls;
3387     //unsigned int pg_iter;
3388     if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3389       {
3390         GNUNET_free(send_hello_context);
3391         return;
3392       }
3393
3394     send_hello_context->pg->remaining_hellos--;
3395 #if DEBUG_TESTING
3396     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sent HELLO, have %d remaining!\n", send_hello_context->pg->remaining_hellos);
3397 #endif
3398     if (send_hello_context->peer_pos == NULL) /* All HELLOs (for this peer!) have been transmitted! */
3399       {
3400 #if DEBUG_TESTING
3401         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All hellos for this peer sent, disconnecting transport!\n");
3402 #endif
3403         GNUNET_assert(send_hello_context->peer->daemon->th != NULL);
3404         GNUNET_TRANSPORT_disconnect(send_hello_context->peer->daemon->th);
3405         send_hello_context->peer->daemon->th = NULL;
3406
3407         /*if (send_hello_context->pg->remaining_hellos == 0)
3408          {
3409          for (pg_iter = 0; pg_iter < send_hello_context->pg->max_outstanding_connections; pg_iter++)
3410          {
3411          preschedule_connect(&send_hello_context->pg->ct_ctx);
3412          }
3413          }
3414          */
3415         GNUNET_assert (send_hello_context->peer->daemon->server == NULL);
3416         send_hello_context->peer->daemon->server = GNUNET_CORE_connect(send_hello_context->peer->cfg,
3417             1,
3418             send_hello_context,
3419             &core_init,
3420             &core_connect_notify,
3421             NULL,
3422             NULL,
3423             NULL, GNUNET_NO,
3424             NULL, GNUNET_NO,
3425             no_handlers);
3426
3427         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),
3428             &send_core_connect_requests,
3429             send_hello_context);
3430       }
3431     else
3432     GNUNET_SCHEDULER_add_now(&schedule_send_hellos, send_hello_context);
3433   }
3434
3435 /**
3436  * Connect to a peer, give it all the HELLO's of those peers
3437  * we will later ask it to connect to.
3438  *
3439  * @param ct_ctx the overall connection context
3440  */
3441 static void schedule_send_hellos (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3442   {
3443     struct SendHelloContext *send_hello_context = cls;
3444     struct GNUNET_TESTING_PeerGroup *pg = send_hello_context->pg;
3445
3446     if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3447       {
3448         GNUNET_free(send_hello_context);
3449         return;
3450       }
3451
3452     GNUNET_assert(send_hello_context->peer_pos != NULL); /* All of the HELLO sends to be scheduled have been scheduled! */
3453
3454     if (((send_hello_context->peer->daemon->th == NULL) &&
3455             (pg->outstanding_connects > pg->max_outstanding_connections)) ||
3456         (pg->stop_connects == GNUNET_YES))
3457       {
3458 #if VERBOSE_TESTING > 2
3459         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3460             _
3461             ("Delaying connect, we have too many outstanding connections!\n"));
3462 #endif
3463         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3464             (GNUNET_TIME_UNIT_MILLISECONDS, 100),
3465             &schedule_send_hellos, send_hello_context);
3466       }
3467     else
3468       {
3469 #if VERBOSE_TESTING > 2
3470         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3471             _("Creating connection, outstanding_connections is %d\n"),
3472             outstanding_connects);
3473 #endif
3474         if (send_hello_context->peer->daemon->th == NULL)
3475           {
3476             pg->outstanding_connects++; /* Actual TRANSPORT, CORE connections! */
3477             send_hello_context->peer->daemon->th = GNUNET_TRANSPORT_connect(send_hello_context->peer->cfg,
3478                 NULL,
3479                 send_hello_context,
3480                 NULL,
3481                 NULL,
3482                 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 static void
5536 internal_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5537 {
5538   struct InternalStartContext *internal_context = cls;
5539
5540   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
5541     {
5542       return;
5543     }
5544
5545   if ((internal_context->peer->pg->starting
5546       < internal_context->peer->pg->max_concurrent_ssh)
5547       || ((internal_context->hostname != NULL)
5548           && (count_outstanding_at_host (internal_context->hostname,
5549                                          internal_context->peer->pg)
5550               < internal_context->peer->pg->max_concurrent_ssh)))
5551     {
5552       if (internal_context->hostname != NULL)
5553         increment_outstanding_at_host (internal_context->hostname,
5554                                        internal_context->peer->pg);
5555       internal_context->peer->pg->starting++;
5556       internal_context->peer->daemon
5557           = GNUNET_TESTING_daemon_start (internal_context->peer->cfg,
5558                                          internal_context->timeout,
5559                                          internal_context->hostname,
5560                                          internal_context->username,
5561                                          internal_context->sshport,
5562                                          internal_context->hostkey,
5563                                          &internal_hostkey_callback,
5564                                          internal_context,
5565                                          &internal_startup_callback,
5566                                          internal_context);
5567     }
5568   else
5569     {
5570       GNUNET_SCHEDULER_add_delayed (
5571                                     GNUNET_TIME_relative_multiply (
5572                                                                    GNUNET_TIME_UNIT_MILLISECONDS,
5573                                                                    100),
5574                                     &internal_start, internal_context);
5575     }
5576 }
5577
5578 /**
5579  * Function which continues a peer group starting up
5580  * after successfully generating hostkeys for each peer.
5581  *
5582  * @param pg the peer group to continue starting
5583  *
5584  */
5585 void
5586 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg)
5587 {
5588   unsigned int i;
5589
5590   pg->starting = 0;
5591   for (i = 0; i < pg->total; i++)
5592     {
5593       GNUNET_SCHEDULER_add_now (&internal_continue_startup,
5594                                 &pg->peers[i].internal_context);
5595       //GNUNET_TESTING_daemon_continue_startup(pg->peers[i].daemon);
5596     }
5597 }
5598
5599 /**
5600  * Start count gnunet instances with the same set of transports and
5601  * applications.  The port numbers (any option called "PORT") will be
5602  * adjusted to ensure that no two peers running on the same system
5603  * have the same port(s) in their respective configurations.
5604  *
5605  * @param cfg configuration template to use
5606  * @param total number of daemons to start
5607  * @param max_concurrent_connections for testing, how many peers can
5608  *                                   we connect to simultaneously
5609  * @param max_concurrent_ssh when starting with ssh, how many ssh
5610  *        connections will we allow at once (based on remote hosts allowed!)
5611  * @param timeout total time allowed for peers to start
5612  * @param hostkey_callback function to call on each peers hostkey generation
5613  *        if NULL, peers will be started by this call, if non-null,
5614  *        GNUNET_TESTING_daemons_continue_startup must be called after
5615  *        successful hostkey generation
5616  * @param hostkey_cls closure for hostkey callback
5617  * @param cb function to call on each daemon that was started
5618  * @param cb_cls closure for cb
5619  * @param connect_callback function to call each time two hosts are connected
5620  * @param connect_callback_cls closure for connect_callback
5621  * @param hostnames linked list of host structs to use to start peers on
5622  *                  (NULL to run on localhost only)
5623  *
5624  * @return NULL on error, otherwise handle to control peer group
5625  */
5626 struct GNUNET_TESTING_PeerGroup *
5627 GNUNET_TESTING_daemons_start(
5628                              const struct GNUNET_CONFIGURATION_Handle *cfg,
5629                              unsigned int total,
5630                              unsigned int max_concurrent_connections,
5631                              unsigned int max_concurrent_ssh,
5632                              struct GNUNET_TIME_Relative timeout,
5633                              GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
5634                              void *hostkey_cls,
5635                              GNUNET_TESTING_NotifyDaemonRunning cb,
5636                              void *cb_cls,
5637                              GNUNET_TESTING_NotifyConnection connect_callback,
5638                              void *connect_callback_cls,
5639                              const struct GNUNET_TESTING_Host *hostnames)
5640 {
5641   struct GNUNET_TESTING_PeerGroup *pg;
5642   const struct GNUNET_TESTING_Host *hostpos;
5643 #if 0
5644   char *pos;
5645   const char *rpos;
5646   char *start;
5647 #endif
5648   const char *hostname;
5649   const char *username;
5650   char *baseservicehome;
5651   char *newservicehome;
5652   char *tmpdir;
5653   char *hostkeys_file;
5654   char *arg;
5655   char *ssh_port_str;
5656   struct GNUNET_DISK_FileHandle *fd;
5657   struct GNUNET_CONFIGURATION_Handle *pcfg;
5658   unsigned int off;
5659   struct OutstandingSSH *ssh_entry;
5660   unsigned int hostcnt;
5661   unsigned int i;
5662   uint16_t minport;
5663   uint16_t sshport;
5664   uint32_t upnum;
5665   uint32_t fdnum;
5666   uint64_t fs;
5667   uint64_t total_hostkeys;
5668   struct GNUNET_OS_Process *proc;
5669
5670   if (0 == total)
5671     {
5672       GNUNET_break (0);
5673       return NULL;
5674     }
5675
5676   upnum = 0;
5677   fdnum = 0;
5678   pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
5679   pg->cfg = cfg;
5680   pg->notify_connection = connect_callback;
5681   pg->notify_connection_cls = connect_callback_cls;
5682   pg->total = total;
5683   pg->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
5684   pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
5685   pg->max_outstanding_connections = max_concurrent_connections;
5686   pg->max_concurrent_ssh = max_concurrent_ssh;
5687   if (NULL != hostnames)
5688     {
5689       off = 0;
5690       hostpos = hostnames;
5691       while (hostpos != NULL)
5692         {
5693           hostpos = hostpos->next;
5694           off++;
5695         }
5696       pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5697       off = 0;
5698
5699       hostpos = hostnames;
5700       while (hostpos != NULL)
5701         {
5702           pg->hosts[off].minport = LOW_PORT;
5703           pg->hosts[off].hostname = GNUNET_strdup (hostpos->hostname);
5704           if (hostpos->username != NULL)
5705             pg->hosts[off].username = GNUNET_strdup (hostpos->username);
5706           pg->hosts[off].sshport = hostpos->port;
5707           hostpos = hostpos->next;
5708           off++;
5709         }
5710
5711       if (off == 0)
5712         {
5713           pg->hosts = NULL;
5714         }
5715       hostcnt = off;
5716       minport = 0;
5717       pg->num_hosts = off;
5718
5719 #if NO_LL
5720       off = 2;
5721       /* skip leading spaces */
5722       while ((0 != *hostnames) && (isspace ((unsigned char) *hostnames)))
5723       hostnames++;
5724       rpos = hostnames;
5725       while ('\0' != *rpos)
5726         {
5727           if (isspace ((unsigned char) *rpos))
5728           off++;
5729           rpos++;
5730         }
5731       pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
5732       off = 0;
5733       start = GNUNET_strdup (hostnames);
5734       pos = start;
5735       while ('\0' != *pos)
5736         {
5737           if (isspace ((unsigned char) *pos))
5738             {
5739               *pos = '\0';
5740               if (strlen (start) > 0)
5741                 {
5742                   pg->hosts[off].minport = LOW_PORT;
5743                   pg->hosts[off++].hostname = start;
5744                 }
5745               start = pos + 1;
5746             }
5747           pos++;
5748         }
5749       if (strlen (start) > 0)
5750         {
5751           pg->hosts[off].minport = LOW_PORT;
5752           pg->hosts[off++].hostname = start;
5753         }
5754       if (off == 0)
5755         {
5756           GNUNET_free (start);
5757           GNUNET_free (pg->hosts);
5758           pg->hosts = NULL;
5759         }
5760       hostcnt = off;
5761       minport = 0; /* make gcc happy */
5762 #endif
5763     }
5764   else
5765     {
5766       hostcnt = 0;
5767       minport = LOW_PORT;
5768     }
5769
5770   /* Create the servicehome directory for each remote peer */
5771   GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
5772                                                                     &baseservicehome));
5773   for (i = 0; i < pg->num_hosts; i++)
5774     {
5775       ssh_entry = GNUNET_malloc(sizeof(struct OutstandingSSH));
5776       ssh_entry->hostname = pg->hosts[i].hostname; /* Don't free! */
5777       GNUNET_CONTAINER_DLL_insert(pg->ssh_head, pg->ssh_tail, ssh_entry);
5778       GNUNET_asprintf(&tmpdir, "%s/%s", baseservicehome, pg->hosts[i].hostname);
5779       if (NULL != pg->hosts[i].username)
5780         GNUNET_asprintf (&arg, "%s@%s", pg->hosts[i].username,
5781                          pg->hosts[i].hostname);
5782       else
5783         GNUNET_asprintf (&arg, "%s", pg->hosts[i].hostname);
5784       if (pg->hosts[i].sshport != 0)
5785         {
5786           GNUNET_asprintf (&ssh_port_str, "%d", pg->hosts[i].sshport);
5787           proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-P",
5788                                           ssh_port_str,
5789 #if !DEBUG_TESTING
5790                                           "-q",
5791 #endif
5792                                           arg, "mkdir -p", tmpdir,
5793                                           NULL);
5794         }
5795       else
5796         proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", arg,
5797                                         "mkdir -p", tmpdir, NULL);
5798       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5799                   "Creating remote dir with command ssh %s %s %s\n", arg,
5800                   " mkdir -p ", tmpdir);
5801       GNUNET_free(tmpdir);
5802       GNUNET_free(arg);
5803       GNUNET_OS_process_wait (proc);
5804       GNUNET_OS_process_close(proc);
5805     }
5806   GNUNET_free(baseservicehome);
5807
5808   if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING",
5809                                                            "HOSTKEYSFILE",
5810                                                            &hostkeys_file))
5811     {
5812       if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
5813         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!\n");
5814       else
5815         {
5816           /* Check hostkey file size, read entire thing into memory */
5817           fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
5818                                       GNUNET_DISK_PERM_NONE);
5819           if (NULL == fd)
5820             {
5821               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
5822               return NULL;
5823             }
5824
5825           if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs,
5826                                                    GNUNET_YES))
5827             fs = 0;
5828
5829           GNUNET_log (
5830                       GNUNET_ERROR_TYPE_WARNING,
5831                       "Found file size %llu for hostkeys, expect hostkeys to be size %d\n",
5832                       fs, HOSTKEYFILESIZE);
5833
5834           if (fs % HOSTKEYFILESIZE != 0)
5835             {
5836               GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5837                           "File size %llu seems incorrect for hostkeys...\n",
5838                           fs);
5839             }
5840           else
5841             {
5842               total_hostkeys = fs / HOSTKEYFILESIZE;
5843               GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5844                           "Will read %llu hostkeys from file\n", total_hostkeys);
5845               pg->hostkey_data = GNUNET_malloc_large (fs);
5846               GNUNET_assert (fs == GNUNET_DISK_file_read (fd, pg->hostkey_data, fs));
5847               GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
5848             }
5849         }
5850       GNUNET_free(hostkeys_file);
5851     }
5852
5853   for (off = 0; off < total; off++)
5854     {
5855       if (hostcnt > 0)
5856         {
5857           hostname = pg->hosts[off % hostcnt].hostname;
5858           username = pg->hosts[off % hostcnt].username;
5859           sshport = pg->hosts[off % hostcnt].sshport;
5860           pcfg = make_config (cfg, off, &pg->hosts[off % hostcnt].minport,
5861                               &upnum, hostname, &fdnum);
5862         }
5863       else
5864         {
5865           hostname = NULL;
5866           username = NULL;
5867           sshport = 0;
5868           pcfg = make_config (cfg, off, &minport, &upnum, hostname, &fdnum);
5869         }
5870
5871       if (NULL == pcfg)
5872         {
5873           GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _
5874           ("Could not create configuration for peer number %u on `%s'!\n"),
5875                       off, hostname == NULL ? "localhost" : hostname);
5876           continue;
5877         }
5878
5879       if (GNUNET_YES
5880           == GNUNET_CONFIGURATION_get_value_string (pcfg, "PATHS",
5881                                                     "SERVICEHOME",
5882                                                     &baseservicehome))
5883         {
5884           if (hostname != NULL)
5885             GNUNET_asprintf (&newservicehome, "%s/%s/%d/", baseservicehome, hostname, off);
5886           else
5887             GNUNET_asprintf (&newservicehome, "%s/%d/", baseservicehome, off);
5888           GNUNET_free (baseservicehome);
5889         }
5890       else
5891         {
5892           tmpdir = getenv ("TMPDIR");
5893           tmpdir = tmpdir ? tmpdir : "/tmp";
5894           if (hostname != NULL)
5895             GNUNET_asprintf (&newservicehome, "%s/%s/%s/%d/", tmpdir, hostname,
5896                              "gnunet-testing-test-test", off);
5897           else
5898             GNUNET_asprintf (&newservicehome, "%s/%s/%d/", tmpdir,
5899                              "gnunet-testing-test-test", off);
5900         }
5901       GNUNET_CONFIGURATION_set_value_string (pcfg, "PATHS", "SERVICEHOME",
5902                                              newservicehome);
5903       GNUNET_free (newservicehome);
5904       pg->peers[off].cfg = pcfg;
5905 #if DEFER
5906       /* Can we do this later? */
5907       pg->peers[off].allowed_peers =
5908       GNUNET_CONTAINER_multihashmap_create (total);
5909       pg->peers[off].connect_peers =
5910       GNUNET_CONTAINER_multihashmap_create (total);
5911       pg->peers[off].blacklisted_peers =
5912       GNUNET_CONTAINER_multihashmap_create (total);
5913
5914 #endif
5915       pg->peers[off].pg = pg;
5916       pg->peers[off].internal_context.peer = &pg->peers[off];
5917       pg->peers[off].internal_context.timeout = timeout;
5918       pg->peers[off].internal_context.hostname = hostname;
5919       pg->peers[off].internal_context.username = username;
5920       pg->peers[off].internal_context.sshport = sshport;
5921       if (pg->hostkey_data != NULL)
5922         pg->peers[off].internal_context.hostkey = &pg->hostkey_data[off
5923             * HOSTKEYFILESIZE];
5924       pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
5925       pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
5926       pg->peers[off].internal_context.start_cb = cb;
5927       pg->peers[off].internal_context.start_cb_cls = cb_cls;
5928
5929       GNUNET_SCHEDULER_add_now (&internal_start,
5930                                 &pg->peers[off].internal_context);
5931
5932     }
5933   return pg;
5934 }
5935
5936 /*
5937  * Get a daemon by number, so callers don't have to do nasty
5938  * offsetting operation.
5939  */
5940 struct GNUNET_TESTING_Daemon *
5941 GNUNET_TESTING_daemon_get(struct GNUNET_TESTING_PeerGroup *pg,
5942                           unsigned int position)
5943 {
5944   if (position < pg->total)
5945     return pg->peers[position].daemon;
5946   else
5947     return NULL;
5948 }
5949
5950 /*
5951  * Get a daemon by peer identity, so callers can
5952  * retrieve the daemon without knowing it's offset.
5953  *
5954  * @param pg the peer group to retrieve the daemon from
5955  * @param peer_id the peer identity of the daemon to retrieve
5956  *
5957  * @return the daemon on success, or NULL if no such peer identity is found
5958  */
5959 struct GNUNET_TESTING_Daemon *
5960 GNUNET_TESTING_daemon_get_by_id(struct GNUNET_TESTING_PeerGroup *pg,
5961                                 struct GNUNET_PeerIdentity *peer_id)
5962 {
5963   unsigned int i;
5964
5965   for (i = 0; i < pg->total; i++)
5966     {
5967       if (0 == memcmp (&pg->peers[i].daemon->id, peer_id,
5968                        sizeof(struct GNUNET_PeerIdentity)))
5969         return pg->peers[i].daemon;
5970     }
5971
5972   return NULL;
5973 }
5974
5975 /**
5976  * Prototype of a function that will be called when a
5977  * particular operation was completed the testing library.
5978  *
5979  * @param cls closure (a struct RestartContext)
5980  * @param id id of the peer that was restarted
5981  * @param cfg handle to the configuration of the peer
5982  * @param d handle to the daemon that was restarted
5983  * @param emsg NULL on success
5984  */
5985 void
5986 restart_callback(void *cls, const struct GNUNET_PeerIdentity *id,
5987                  const struct GNUNET_CONFIGURATION_Handle *cfg,
5988                  struct GNUNET_TESTING_Daemon *d, const char *emsg)
5989 {
5990   struct RestartContext *restart_context = cls;
5991
5992   if (emsg == NULL)
5993     {
5994       restart_context->peers_restarted++;
5995     }
5996   else
5997     {
5998       restart_context->peers_restart_failed++;
5999     }
6000
6001   if (restart_context->peers_restarted == restart_context->peer_group->total)
6002     {
6003       restart_context->callback (restart_context->callback_cls, NULL);
6004       GNUNET_free (restart_context);
6005     }
6006   else if (restart_context->peers_restart_failed
6007       + restart_context->peers_restarted == restart_context->peer_group->total)
6008     {
6009       restart_context->callback (restart_context->callback_cls,
6010                                  "Failed to restart peers!");
6011       GNUNET_free (restart_context);
6012     }
6013
6014 }
6015
6016 /**
6017  * Callback for informing us about a successful
6018  * or unsuccessful churn stop call.
6019  *
6020  * @param cls a ChurnContext
6021  * @param emsg NULL on success, non-NULL on failure
6022  *
6023  */
6024 void
6025 churn_stop_callback(void *cls, const char *emsg)
6026 {
6027   struct ShutdownContext *shutdown_ctx = cls;
6028   struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
6029   unsigned int total_left;
6030   char *error_message;
6031
6032   error_message = NULL;
6033   shutdown_ctx->outstanding--;
6034
6035   if (emsg != NULL)
6036     {
6037       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6038                   "Churn stop callback failed with error `%s'\n", emsg);
6039       churn_ctx->num_failed_stop++;
6040     }
6041   else
6042     {
6043       churn_ctx->num_to_stop--;
6044     }
6045
6046 #if DEBUG_CHURN
6047   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6048       "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
6049 #endif
6050   total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop)
6051       + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
6052
6053   if (total_left == 0)
6054     {
6055       if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
6056         {
6057           GNUNET_asprintf (
6058                            &error_message,
6059                            "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
6060                            churn_ctx->num_failed_start,
6061                            churn_ctx->num_failed_stop);
6062         }
6063       churn_ctx->cb (churn_ctx->cb_cls, error_message);
6064       GNUNET_free_non_null (error_message);
6065       GNUNET_free (churn_ctx);
6066       GNUNET_free (shutdown_ctx);
6067     }
6068 }
6069
6070 /**
6071  * Count the number of running peers.
6072  *
6073  * @param pg handle for the peer group
6074  *
6075  * @return the number of currently running peers in the peer group
6076  */
6077 unsigned int
6078 GNUNET_TESTING_daemons_running(struct GNUNET_TESTING_PeerGroup *pg)
6079 {
6080   unsigned int i;
6081   unsigned int running = 0;
6082   for (i = 0; i < pg->total; i++)
6083     {
6084       if (pg->peers[i].daemon->running == GNUNET_YES)
6085         {
6086           GNUNET_assert (running != -1);
6087           running++;
6088         }
6089     }
6090   return running;
6091 }
6092
6093 /**
6094  * Task to rate limit the number of outstanding peer shutdown
6095  * requests.  This is necessary for making sure we don't do
6096  * too many ssh connections at once, but is generally nicer
6097  * to any system as well (graduated task starts, as opposed
6098  * to calling gnunet-arm N times all at once).
6099  */
6100 static void
6101 schedule_churn_shutdown_task(void *cls,
6102                              const struct GNUNET_SCHEDULER_TaskContext *tc)
6103 {
6104   struct PeerShutdownContext *peer_shutdown_ctx = cls;
6105   struct ShutdownContext *shutdown_ctx;
6106   struct ChurnContext *churn_ctx;
6107   GNUNET_assert (peer_shutdown_ctx != NULL);
6108   shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6109   GNUNET_assert (shutdown_ctx != NULL);
6110   churn_ctx = (struct ChurnContext *) shutdown_ctx->cb_cls;
6111   if (shutdown_ctx->outstanding > churn_ctx->pg->max_concurrent_ssh)
6112     GNUNET_SCHEDULER_add_delayed (
6113                                   GNUNET_TIME_relative_multiply (
6114                                                                  GNUNET_TIME_UNIT_MILLISECONDS,
6115                                                                  100),
6116                                   &schedule_churn_shutdown_task,
6117                                   peer_shutdown_ctx);
6118   else
6119     {
6120       shutdown_ctx->outstanding++;
6121       GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6122                                   shutdown_ctx->timeout, shutdown_ctx->cb,
6123                                   shutdown_ctx, GNUNET_NO, GNUNET_YES);
6124       GNUNET_free (peer_shutdown_ctx);
6125     }
6126 }
6127
6128 /**
6129  * Simulate churn by stopping some peers (and possibly
6130  * re-starting others if churn is called multiple times).  This
6131  * function can only be used to create leave-join churn (peers "never"
6132  * leave for good).  First "voff" random peers that are currently
6133  * online will be taken offline; then "von" random peers that are then
6134  * offline will be put back online.  No notifications will be
6135  * generated for any of these operations except for the callback upon
6136  * completion.
6137  *
6138  * @param pg handle for the peer group
6139  * @param voff number of peers that should go offline
6140  * @param von number of peers that should come back online;
6141  *            must be zero on first call (since "testbed_start"
6142  *            always starts all of the peers)
6143  * @param timeout how long to wait for operations to finish before
6144  *        giving up
6145  * @param cb function to call at the end
6146  * @param cb_cls closure for cb
6147  */
6148 void
6149 GNUNET_TESTING_daemons_churn(struct GNUNET_TESTING_PeerGroup *pg,
6150                              unsigned int voff, unsigned int von,
6151                              struct GNUNET_TIME_Relative timeout,
6152                              GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6153 {
6154   struct ChurnContext *churn_ctx;
6155   struct ShutdownContext *shutdown_ctx;
6156   struct PeerShutdownContext *peer_shutdown_ctx;
6157   struct PeerRestartContext *peer_restart_ctx;
6158   struct ChurnRestartContext *churn_startup_ctx;
6159
6160   unsigned int running;
6161   unsigned int stopped;
6162   unsigned int total_running;
6163   unsigned int total_stopped;
6164   unsigned int i;
6165   unsigned int *running_arr;
6166   unsigned int *stopped_arr;
6167   unsigned int *running_permute;
6168   unsigned int *stopped_permute;
6169
6170   running = 0;
6171   stopped = 0;
6172
6173   if ((von == 0) && (voff == 0)) /* No peers at all? */
6174     {
6175       cb (cb_cls, NULL);
6176       return;
6177     }
6178
6179   for (i = 0; i < pg->total; i++)
6180     {
6181       if (pg->peers[i].daemon->running == GNUNET_YES)
6182         {
6183           GNUNET_assert (running != -1);
6184           running++;
6185         }
6186       else
6187         {
6188           GNUNET_assert (stopped != -1);
6189           stopped++;
6190         }
6191     }
6192
6193   if (voff > running)
6194     {
6195       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6196                   "Trying to stop more peers than are currently running!\n");
6197       cb (cb_cls, "Trying to stop more peers than are currently running!");
6198       return;
6199     }
6200
6201   if (von > stopped)
6202     {
6203       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6204                   "Trying to start more peers than are currently stopped!\n");
6205       cb (cb_cls, "Trying to start more peers than are currently stopped!");
6206       return;
6207     }
6208
6209   churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6210
6211   running_arr = NULL;
6212   if (running > 0)
6213     running_arr = GNUNET_malloc (running * sizeof (unsigned int));
6214
6215   stopped_arr = NULL;
6216   if (stopped > 0)
6217     stopped_arr = GNUNET_malloc (stopped * sizeof (unsigned int));
6218
6219   running_permute = NULL;
6220   stopped_permute = NULL;
6221
6222   if (running > 0)
6223     running_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6224                                                     running);
6225   if (stopped > 0)
6226     stopped_permute = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
6227                                                     stopped);
6228
6229   total_running = running;
6230   total_stopped = stopped;
6231   running = 0;
6232   stopped = 0;
6233
6234   churn_ctx->num_to_start = von;
6235   churn_ctx->num_to_stop = voff;
6236   churn_ctx->cb = cb;
6237   churn_ctx->cb_cls = cb_cls;
6238   churn_ctx->pg = pg;
6239
6240   for (i = 0; i < pg->total; i++)
6241     {
6242       if (pg->peers[i].daemon->running == GNUNET_YES)
6243         {
6244           GNUNET_assert ((running_arr != NULL) && (total_running > running));
6245           running_arr[running] = i;
6246           running++;
6247         }
6248       else
6249         {
6250           GNUNET_assert ((stopped_arr != NULL) && (total_stopped > stopped));
6251           stopped_arr[stopped] = i;
6252           stopped++;
6253         }
6254     }
6255
6256   GNUNET_assert (running >= voff);
6257   if (voff > 0)
6258     {
6259       shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6260       shutdown_ctx->cb = &churn_stop_callback;
6261       shutdown_ctx->cb_cls = churn_ctx;
6262       shutdown_ctx->total_peers = voff;
6263       shutdown_ctx->timeout = timeout;
6264     }
6265
6266   for (i = 0; i < voff; i++)
6267     {
6268 #if DEBUG_CHURN
6269       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n",
6270           running_permute[i]);
6271 #endif
6272       GNUNET_assert (running_arr != NULL);
6273       peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6274       peer_shutdown_ctx->daemon
6275           = pg->peers[running_arr[running_permute[i]]].daemon;
6276       peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6277       GNUNET_SCHEDULER_add_now (&schedule_churn_shutdown_task,
6278                                 peer_shutdown_ctx);
6279
6280       /*
6281        GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
6282        timeout,
6283        &churn_stop_callback, churn_ctx,
6284        GNUNET_NO, GNUNET_YES); */
6285     }
6286
6287   GNUNET_assert (stopped >= von);
6288   if (von > 0)
6289     {
6290       churn_startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6291       churn_startup_ctx->churn_ctx = churn_ctx;
6292       churn_startup_ctx->timeout = timeout;
6293       churn_startup_ctx->pg = pg;
6294     }
6295   for (i = 0; i < von; i++)
6296     {
6297 #if DEBUG_CHURN
6298       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n",
6299           stopped_permute[i]);
6300 #endif
6301       GNUNET_assert (stopped_arr != NULL);
6302       peer_restart_ctx = GNUNET_malloc (sizeof (struct PeerRestartContext));
6303       peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
6304       peer_restart_ctx->daemon
6305           = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
6306       GNUNET_SCHEDULER_add_now (&schedule_churn_restart, peer_restart_ctx);
6307       /*
6308        GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
6309        timeout, &churn_start_callback, churn_ctx); */
6310     }
6311
6312   GNUNET_free_non_null (running_arr);
6313   GNUNET_free_non_null (stopped_arr);
6314   GNUNET_free_non_null (running_permute);
6315   GNUNET_free_non_null (stopped_permute);
6316 }
6317
6318 /**
6319  * Restart all peers in the given group.
6320  *
6321  * @param pg the handle to the peer group
6322  * @param callback function to call on completion (or failure)
6323  * @param callback_cls closure for the callback function
6324  */
6325 void
6326 GNUNET_TESTING_daemons_restart(struct GNUNET_TESTING_PeerGroup *pg,
6327                                GNUNET_TESTING_NotifyCompletion callback,
6328                                void *callback_cls)
6329 {
6330   struct RestartContext *restart_context;
6331   unsigned int off;
6332
6333   if (pg->total > 0)
6334     {
6335       restart_context = GNUNET_malloc (sizeof (struct RestartContext));
6336       restart_context->peer_group = pg;
6337       restart_context->peers_restarted = 0;
6338       restart_context->callback = callback;
6339       restart_context->callback_cls = callback_cls;
6340
6341       for (off = 0; off < pg->total; off++)
6342         {
6343           GNUNET_TESTING_daemon_restart (pg->peers[off].daemon,
6344                                          &restart_callback, restart_context);
6345         }
6346     }
6347 }
6348
6349 /**
6350  * Start or stop an individual peer from the given group.
6351  *
6352  * @param pg handle to the peer group
6353  * @param offset which peer to start or stop
6354  * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
6355  * @param timeout how long to wait for shutdown
6356  * @param cb function to call at the end
6357  * @param cb_cls closure for cb
6358  */
6359 void
6360 GNUNET_TESTING_daemons_vary(struct GNUNET_TESTING_PeerGroup *pg,
6361                             unsigned int offset, int desired_status,
6362                             struct GNUNET_TIME_Relative timeout,
6363                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6364 {
6365   struct ShutdownContext *shutdown_ctx;
6366   struct ChurnRestartContext *startup_ctx;
6367   struct ChurnContext *churn_ctx;
6368
6369   if (GNUNET_NO == desired_status)
6370     {
6371       if (NULL != pg->peers[offset].daemon)
6372         {
6373           shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6374           churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6375           churn_ctx->num_to_start = 0;
6376           churn_ctx->num_to_stop = 1;
6377           churn_ctx->cb = cb;
6378           churn_ctx->cb_cls = cb_cls;
6379           shutdown_ctx->cb_cls = churn_ctx;
6380           GNUNET_TESTING_daemon_stop (pg->peers[offset].daemon, timeout,
6381                                       &churn_stop_callback, shutdown_ctx,
6382                                       GNUNET_NO, GNUNET_YES);
6383         }
6384     }
6385   else if (GNUNET_YES == desired_status)
6386     {
6387       if (NULL == pg->peers[offset].daemon)
6388         {
6389           startup_ctx = GNUNET_malloc (sizeof (struct ChurnRestartContext));
6390           churn_ctx = GNUNET_malloc (sizeof (struct ChurnContext));
6391           churn_ctx->num_to_start = 1;
6392           churn_ctx->num_to_stop = 0;
6393           churn_ctx->cb = cb;
6394           churn_ctx->cb_cls = cb_cls;
6395           startup_ctx->churn_ctx = churn_ctx;
6396           GNUNET_TESTING_daemon_start_stopped (pg->peers[offset].daemon,
6397                                                timeout, &churn_start_callback,
6398                                                startup_ctx);
6399         }
6400     }
6401   else
6402     GNUNET_break (0);
6403 }
6404
6405 /**
6406  * Callback for shutting down peers in a peer group.
6407  *
6408  * @param cls closure (struct ShutdownContext)
6409  * @param emsg NULL on success
6410  */
6411 void
6412 internal_shutdown_callback(void *cls, const char *emsg)
6413 {
6414   struct PeerShutdownContext *peer_shutdown_ctx = cls;
6415   struct ShutdownContext *shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6416   unsigned int off;
6417   struct OutstandingSSH *ssh_pos;
6418
6419   shutdown_ctx->outstanding--;
6420   if (peer_shutdown_ctx->daemon->hostname != NULL)
6421     decrement_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6422                                    shutdown_ctx->pg);
6423
6424   if (emsg == NULL)
6425     {
6426       shutdown_ctx->peers_down++;
6427     }
6428   else
6429     {
6430       shutdown_ctx->peers_failed++;
6431     }
6432
6433   if ((shutdown_ctx->cb != NULL) && (shutdown_ctx->peers_down
6434       + shutdown_ctx->peers_failed == shutdown_ctx->total_peers))
6435     {
6436       if (shutdown_ctx->peers_failed > 0)
6437         shutdown_ctx->cb (shutdown_ctx->cb_cls,
6438                           "Not all peers successfully shut down!");
6439       else
6440         shutdown_ctx->cb (shutdown_ctx->cb_cls, NULL);
6441
6442       GNUNET_free (shutdown_ctx->pg->peers);
6443       GNUNET_free_non_null(shutdown_ctx->pg->hostkey_data);
6444       for (off = 0; off < shutdown_ctx->pg->num_hosts; off++)
6445         {
6446           GNUNET_free (shutdown_ctx->pg->hosts[off].hostname);
6447           GNUNET_free_non_null (shutdown_ctx->pg->hosts[off].username);
6448         }
6449       GNUNET_free_non_null (shutdown_ctx->pg->hosts);
6450       while (NULL != (ssh_pos = shutdown_ctx->pg->ssh_head))
6451         {
6452           GNUNET_CONTAINER_DLL_remove(shutdown_ctx->pg->ssh_head, shutdown_ctx->pg->ssh_tail, ssh_pos);
6453           GNUNET_free(ssh_pos);
6454         }
6455       GNUNET_free (shutdown_ctx->pg);
6456       GNUNET_free (shutdown_ctx);
6457     }
6458   GNUNET_free(peer_shutdown_ctx);
6459 }
6460
6461 /**
6462  * Task to rate limit the number of outstanding peer shutdown
6463  * requests.  This is necessary for making sure we don't do
6464  * too many ssh connections at once, but is generally nicer
6465  * to any system as well (graduated task starts, as opposed
6466  * to calling gnunet-arm N times all at once).
6467  */
6468 static void
6469 schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6470 {
6471   struct PeerShutdownContext *peer_shutdown_ctx = cls;
6472   struct ShutdownContext *shutdown_ctx;
6473
6474   GNUNET_assert (peer_shutdown_ctx != NULL);
6475   shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
6476   GNUNET_assert (shutdown_ctx != NULL);
6477
6478   if ((shutdown_ctx->outstanding < shutdown_ctx->pg->max_concurrent_ssh)
6479       || ((peer_shutdown_ctx->daemon->hostname != NULL)
6480           && (count_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6481                                          shutdown_ctx->pg)
6482               < shutdown_ctx->pg->max_concurrent_ssh)))
6483     {
6484       if (peer_shutdown_ctx->daemon->hostname != NULL)
6485         increment_outstanding_at_host (peer_shutdown_ctx->daemon->hostname,
6486                                        shutdown_ctx->pg);
6487       shutdown_ctx->outstanding++;
6488       GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon,
6489                                   shutdown_ctx->timeout,
6490                                   &internal_shutdown_callback, peer_shutdown_ctx,
6491                                   GNUNET_YES, GNUNET_NO);
6492     }
6493   else
6494     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
6495                                                                  100),
6496                                   &schedule_shutdown_task, peer_shutdown_ctx);
6497
6498 }
6499
6500 /**
6501  * Shutdown all peers started in the given group.
6502  *
6503  * @param pg handle to the peer group
6504  * @param timeout how long to wait for shutdown
6505  * @param cb callback to notify upon success or failure
6506  * @param cb_cls closure for cb
6507  */
6508 void
6509 GNUNET_TESTING_daemons_stop(struct GNUNET_TESTING_PeerGroup *pg,
6510                             struct GNUNET_TIME_Relative timeout,
6511                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
6512 {
6513   unsigned int off;
6514   struct ShutdownContext *shutdown_ctx;
6515   struct PeerShutdownContext *peer_shutdown_ctx;
6516 #if OLD
6517   struct PeerConnection *conn_iter;
6518   struct PeerConnection *temp_conn;
6519 #endif
6520
6521   GNUNET_assert (pg->total > 0);
6522
6523   shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
6524   shutdown_ctx->cb = cb;
6525   shutdown_ctx->cb_cls = cb_cls;
6526   shutdown_ctx->total_peers = pg->total;
6527   shutdown_ctx->timeout = timeout;
6528   shutdown_ctx->pg = pg;
6529   /* shtudown_ctx->outstanding = 0; */
6530
6531   for (off = 0; off < pg->total; off++)
6532     {
6533       GNUNET_assert (NULL != pg->peers[off].daemon);
6534       peer_shutdown_ctx = GNUNET_malloc (sizeof (struct PeerShutdownContext));
6535       peer_shutdown_ctx->daemon = pg->peers[off].daemon;
6536       peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
6537       GNUNET_SCHEDULER_add_now (&schedule_shutdown_task, peer_shutdown_ctx);
6538
6539       if (NULL != pg->peers[off].cfg)
6540         GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
6541 #if OLD
6542       conn_iter = pg->peers[off].allowed_peers_head;
6543       while (conn_iter != NULL)
6544         {
6545           temp_conn = conn_iter->next;
6546           GNUNET_free(conn_iter);
6547           conn_iter = temp_conn;
6548         }
6549
6550       conn_iter = pg->peers[off].connect_peers_head;
6551       while (conn_iter != NULL)
6552         {
6553           temp_conn = conn_iter->next;
6554           GNUNET_free(conn_iter);
6555           conn_iter = temp_conn;
6556         }
6557
6558       conn_iter = pg->peers[off].blacklisted_peers_head;
6559       while (conn_iter != NULL)
6560         {
6561           temp_conn = conn_iter->next;
6562           GNUNET_free(conn_iter);
6563           conn_iter = temp_conn;
6564         }
6565
6566       conn_iter = pg->peers[off].connect_peers_working_set_head;
6567       while (conn_iter != NULL)
6568         {
6569           temp_conn = conn_iter->next;
6570           GNUNET_free(conn_iter);
6571           conn_iter = temp_conn;
6572         }
6573 #else
6574       if (pg->peers[off].allowed_peers != NULL)
6575       GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].allowed_peers);
6576       if (pg->peers[off].connect_peers != NULL)
6577       GNUNET_CONTAINER_multihashmap_destroy (pg->peers[off].connect_peers);
6578       if (pg->peers[off].blacklisted_peers != NULL)
6579       GNUNET_CONTAINER_multihashmap_destroy (pg->
6580           peers[off].blacklisted_peers);
6581 #endif
6582     }
6583 }
6584
6585 /* end of testing_group.c */