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