76403e427fc438e41681f2d42aeae0e0024c7ebb
[oweals/gnunet.git] / src / testbed / testbed_api_testbed.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2008--2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18   Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file testbed/testbed_api_testbed.c
23  * @brief high-level testbed management
24  * @author Christian Grothoff
25  * @author Sree Harsha Totakura
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "testbed_api.h"
32 #include "testbed_api_peers.h"
33 #include "testbed_api_hosts.h"
34 #include "testbed_api_topology.h"
35
36 /**
37  * Generic loggins shorthand
38  */
39 #define LOG(kind,...)                                           \
40   GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
41
42 /**
43  * Debug logging shortcut
44  */
45 #define DEBUG(...)                              \
46   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
47
48 /**
49  * The default setup timeout in seconds
50  */
51 #define DEFAULT_SETUP_TIMEOUT 300
52
53
54 /**
55  * Configuration section for testbed
56  */
57 #define TESTBED_CONFIG_SECTION "testbed"
58
59 /**
60  * Option string for the maximum number of edges a peer is permitted to have
61  * while generating scale free topology
62  */
63 #define SCALE_FREE_CAP "SCALE_FREE_TOPOLOGY_CAP"
64
65 /**
66  * Option string for the number of edges to be established when adding a new
67  * node to the scale free network
68  */
69 #define SCALE_FREE_M "SCALE_FREE_TOPOLOGY_M"
70
71 /**
72  * Context information for the operation we start
73  */
74 struct RunContextOperation
75 {
76   /**
77    * The testbed operation handle
78    */
79   struct GNUNET_TESTBED_Operation *op;
80
81   /**
82    * Context information for GNUNET_TESTBED_run()
83    */
84   struct GNUNET_TESTBED_RunHandle *rc;
85
86   /**
87    * Closure
88    */
89   void *cls;
90
91 };
92
93
94 /**
95  * States of RunContext
96  */
97 enum State
98 {
99   /**
100    * Initial state
101    */
102   RC_INIT = 0,
103
104   /**
105    * Controllers on given hosts started and linked
106    */
107   RC_LINKED,
108
109   /**
110    * Peers are created
111    */
112   RC_PEERS_CREATED,
113
114   /**
115    * The testbed run is ready and the master callback can be called now. At this
116    * time the peers are all started and if a topology is provided in the
117    * configuration the topology would have been attempted
118    */
119   RC_READY,
120
121   /* /\** */
122   /*  * Peers are stopped */
123   /*  *\/ */
124   /* RC_PEERS_STOPPED, */
125
126   /* /\** */
127   /*  * Peers are destroyed */
128   /*  *\/ */
129   /* RC_PEERS_DESTROYED */
130
131   /**
132    * All peers shutdown (stopped and destroyed)
133    */
134   RC_PEERS_SHUTDOWN
135 };
136
137
138 /**
139  * Context for host compability checks
140  */
141 struct CompatibilityCheckContext
142 {
143   /**
144    * The run context
145    */
146   struct GNUNET_TESTBED_RunHandle *rc;
147
148   /**
149    * Handle for the compability check
150    */
151   struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
152
153   /**
154    * Index of the host in the run context's hosts array
155    */
156   unsigned int index;
157 };
158
159
160 /**
161  * Testbed Run Handle
162  */
163 struct GNUNET_TESTBED_RunHandle
164 {
165   /**
166    * The controller handle
167    */
168   struct GNUNET_TESTBED_Controller *c;
169
170   /**
171    * The configuration of the controller. This is based on the cfg given to the
172    * function GNUNET_TESTBED_run(). We also use this config as a template while
173    * for peers
174    */
175   struct GNUNET_CONFIGURATION_Handle *cfg;
176
177   /**
178    * Handle to the host on which the controller runs
179    */
180   struct GNUNET_TESTBED_Host *h;
181
182   /**
183    * The handle to the controller process
184    */
185   struct GNUNET_TESTBED_ControllerProc *cproc;
186
187   /**
188    * The callback to use as controller callback
189    */
190   GNUNET_TESTBED_ControllerCallback cc;
191
192   /**
193    * The pointer to the controller callback
194    */
195   void *cc_cls;
196
197   /**
198    * The trusted IP string
199    */
200   char *trusted_ip;
201
202   /**
203    * TestMaster callback to call when testbed initialization is done
204    */
205   GNUNET_TESTBED_TestMaster test_master;
206
207   /**
208    * The closure for the TestMaster callback
209    */
210   void *test_master_cls;
211
212   /**
213    * A hashmap for operations started by us
214    */
215   struct GNUNET_CONTAINER_MultiHashMap32 *rcop_map;
216
217   /**
218    * An array of hosts loaded from the hostkeys file
219    */
220   struct GNUNET_TESTBED_Host **hosts;
221
222   /**
223    * Array of compatibility check contexts
224    */
225   struct CompatibilityCheckContext *hclist;
226
227   /**
228    * Array of peers which we create
229    */
230   struct GNUNET_TESTBED_Peer **peers;
231
232   /**
233    * The topology generation operation. Will be null if no topology is set in
234    * the configuration
235    */
236   struct GNUNET_TESTBED_Operation *topology_operation;
237
238   /**
239    * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
240    */
241   char *topo_file;
242
243   /**
244    * Host registration handle
245    */
246   struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
247
248   /**
249    * Profiling start time
250    */
251   struct GNUNET_TIME_Absolute pstart_time;
252
253   /**
254    * Host registration task
255    */
256   struct GNUNET_SCHEDULER_Task * register_hosts_task;
257
258   /**
259    * Task to be run of a timeout
260    */
261   struct GNUNET_SCHEDULER_Task * timeout_task;
262
263   /**
264    * Task run upon shutdown interrupts
265    */
266   struct GNUNET_SCHEDULER_Task * interrupt_task;
267
268   /**
269    * The event mask for the controller
270    */
271   uint64_t event_mask;
272
273   /**
274    * State of this context
275    */
276   enum State state;
277
278   /**
279    * The topology which has to be achieved with the peers started in this context
280    */
281   enum GNUNET_TESTBED_TopologyOption topology;
282
283   /**
284    * Have we already shutdown
285    */
286   int shutdown;
287
288   /**
289    * Number of hosts in the given host file
290    */
291   unsigned int num_hosts;
292
293   /**
294    * Number of registered hosts. Also used as a counter while checking
295    * habitabillity of hosts
296    */
297   unsigned int reg_hosts;
298
299   /**
300    * Current peer count for an operation; Set this to 0 and increment for each
301    * successful operation on a peer
302    */
303   unsigned int peer_count;
304
305   /**
306    * number of peers to start
307    */
308   unsigned int num_peers;
309
310   /**
311    * Expected overlay connects. Should be zero if no topology is relavant
312    */
313   unsigned int num_oc;
314
315   /**
316    * Number of random links to established
317    */
318   unsigned int random_links;
319
320   /**
321    * the number of overlay link connection attempts that succeeded
322    */
323   unsigned int links_succeeded;
324
325   /**
326    * the number of overlay link connection attempts that failed
327    */
328   unsigned int links_failed;
329
330 };
331
332
333 /**
334  * Return a 32-bit key from a pointer
335  *
336  * @param rcop the pointer
337  * @return 32-bit key
338  */
339 static uint32_t
340 rcop_key (void *rcop)
341 {
342   return * ((uint32_t *) &rcop);
343 }
344
345
346 /**
347  * Context information used for finding a pointer in the rcop_map
348  */
349 struct SearchContext
350 {
351   /**
352    * The operation pointer to look for
353    */
354   struct GNUNET_TESTBED_Operation *query;
355
356   /**
357    * The Run context operation which has the operation being queried
358    */
359   struct RunContextOperation *result;
360 };
361
362
363 /**
364  * Iterator for searching over the elements matching a given query
365  *
366  * @param cls the SearchContext
367  * @param key the 32-bit key
368  * @param value the RunContextOperation element
369  * @return GNUNET_YES to continue iteration; GNUNET_NO to cancel it
370  */
371 static int
372 search_iterator (void *cls, uint32_t key, void *value)
373 {
374   struct RunContextOperation *rcop = value;
375   struct SearchContext *sc = cls;
376
377   GNUNET_assert (NULL != rcop);
378   if (sc->query == rcop->op)
379   {
380     GNUNET_assert (NULL == sc->result);
381     sc->result = rcop;
382     return GNUNET_NO;
383   }
384   return GNUNET_YES;
385 }
386
387
388 /**
389  * Initiate a search for the given operation in the rcop_map
390  *
391  * @param rc the RunContext whose rcop_map will be searched for the given
392  *          operation
393  * @param op the given operation to search for
394  * @return the matching RunContextOperation if found; NULL if not
395  */
396 static struct RunContextOperation *
397 search_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct GNUNET_TESTBED_Operation *op)
398 {
399   struct SearchContext sc;
400
401   sc.query = op;
402   sc.result = NULL;
403   if (GNUNET_SYSERR ==
404       GNUNET_CONTAINER_multihashmap32_get_multiple (rc->rcop_map,
405                                                     rcop_key (op),
406                                                     &search_iterator,
407                                                     &sc))
408   {
409     GNUNET_assert (NULL != sc.result);
410     return sc.result;
411   }
412   return NULL;
413 }
414
415
416 /**
417  * Insert an RunContextOperation into the rcop_map of the given RunContext
418  *
419  * @param rc the RunContext into whose map is to be used for insertion
420  * @param rcop the RunContextOperation to insert
421  */
422 static void
423 insert_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct RunContextOperation *rcop)
424 {
425   GNUNET_assert (GNUNET_OK ==
426                  GNUNET_CONTAINER_multihashmap32_put (rc->rcop_map,
427                                                       rcop_key (rcop->op), rcop,
428                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
429 }
430
431
432 /**
433  * Remove a RunContextOperation from the rcop_map of the given RunContext
434  *
435  * @param rc the RunContext from whose map the given RunContextOperaton has to
436  *          be removed
437  * @param rcop the RunContextOperation
438  */
439 static void
440 remove_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct RunContextOperation *rcop)
441 {
442   GNUNET_assert (GNUNET_YES ==
443                  GNUNET_CONTAINER_multihashmap32_remove (rc->rcop_map,
444                                                          rcop_key (rcop->op),
445                                                          rcop));
446 }
447
448 /**
449  * Assuming all peers have been destroyed cleanup run handle
450  *
451  * @param rc the run context
452  */
453 static void
454 cleanup (struct GNUNET_TESTBED_RunHandle *rc)
455 {
456   unsigned int hid;
457
458   GNUNET_assert (NULL == rc->register_hosts_task);
459   GNUNET_assert (NULL == rc->reg_handle);
460   GNUNET_assert (NULL == rc->peers);
461   GNUNET_assert (NULL == rc->hclist);
462   GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
463   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
464   GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
465   if (NULL != rc->c)
466     GNUNET_TESTBED_controller_disconnect (rc->c);
467   if (NULL != rc->cproc)
468     GNUNET_TESTBED_controller_stop (rc->cproc);
469   if (NULL != rc->h)
470     GNUNET_TESTBED_host_destroy (rc->h);
471   for (hid = 0; hid < rc->num_hosts; hid++)
472     GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
473   GNUNET_free_non_null (rc->hosts);
474   if (NULL != rc->cfg)
475     GNUNET_CONFIGURATION_destroy (rc->cfg);
476   GNUNET_free_non_null (rc->topo_file);
477   GNUNET_free_non_null (rc->trusted_ip);
478   GNUNET_free (rc);
479 }
480
481
482 /**
483  * Iterator for cleaning up elements from rcop_map
484  *
485  * @param cls the RunContext
486  * @param key the 32-bit key
487  * @param value the RunContextOperation element
488  * @return always GNUNET_YES
489  */
490 static int
491 rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
492 {
493   struct GNUNET_TESTBED_RunHandle *rc = cls;
494   struct RunContextOperation *rcop = value;
495
496   GNUNET_assert (rc == rcop->rc);
497   remove_rcop (rc, rcop);
498   GNUNET_TESTBED_operation_done (rcop->op);
499   GNUNET_free (rcop);
500   return GNUNET_YES;
501 }
502
503
504 /**
505  * Cancels operations and tasks which are assigned to the given run context
506  *
507  * @param rc the RunContext
508  */
509 static void
510 rc_cleanup_operations (struct GNUNET_TESTBED_RunHandle *rc)
511 {
512   struct CompatibilityCheckContext *hc;
513   unsigned int nhost;
514
515   if (NULL != rc->hclist)
516   {
517     for (nhost = 0; nhost < rc->num_hosts; nhost++)
518     {
519       hc = &rc->hclist[nhost];
520       if (NULL != hc->h)
521         GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
522     }
523     GNUNET_free (rc->hclist);
524     rc->hclist = NULL;
525   }
526   /* Stop register hosts task if it is running */
527   if (NULL != rc->register_hosts_task)
528   {
529     GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
530     rc->register_hosts_task = NULL;
531   }
532   if (NULL != rc->timeout_task)
533   {
534     GNUNET_SCHEDULER_cancel (rc->timeout_task);
535     rc->timeout_task = NULL;
536   }
537   if (NULL != rc->reg_handle)
538   {
539     GNUNET_TESTBED_cancel_registration (rc->reg_handle);
540     rc->reg_handle = NULL;
541   }
542   if (NULL != rc->topology_operation)
543   {
544     GNUNET_TESTBED_operation_done (rc->topology_operation);
545     rc->topology_operation = NULL;
546   }
547   /* cancel any exiting operations */
548   GNUNET_assert (GNUNET_SYSERR !=
549                  GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
550                                                           &rcop_cleanup_iterator,
551                                                           rc));
552 }
553
554
555 /**
556  * Cancels the scheduled interrupt task
557  *
558  * @param rc the run context
559  */
560 static void
561 cancel_interrupt_task (struct GNUNET_TESTBED_RunHandle *rc)
562 {
563   GNUNET_SCHEDULER_cancel (rc->interrupt_task);
564   rc->interrupt_task = NULL;
565 }
566
567
568 /**
569  * This callback will be called when all the operations are completed
570  * (done/cancelled)
571  *
572  * @param cls run context
573  */
574 static void
575 wait_op_completion (void *cls)
576 {
577   struct GNUNET_TESTBED_RunHandle *rc = cls;
578   struct RunContextOperation *rcop;
579
580   if ( (NULL == rc->cproc)
581        || (NULL == rc->c)
582        || (GNUNET_YES == rc->shutdown) )
583   {
584     if (NULL != rc->peers)
585     {
586       GNUNET_free (rc->peers);
587       rc->peers = NULL;
588     }
589     goto cleanup_;
590   }
591   if (NULL == rc->peers)
592     goto cleanup_;
593   rc->shutdown = GNUNET_YES;
594   rcop = GNUNET_new (struct RunContextOperation);
595   rcop->rc = rc;
596   rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
597   GNUNET_assert (NULL != rcop->op);
598   DEBUG ("Shutting down peers\n");
599   rc->pstart_time = GNUNET_TIME_absolute_get ();
600   insert_rcop (rc, rcop);
601   return;
602
603  cleanup_:
604   rc->state = RC_PEERS_SHUTDOWN;
605   cancel_interrupt_task (rc);
606   cleanup (rc);
607 }
608
609
610 /**
611  * Task run upon interrupts (SIGINT, SIGTERM) and upon scheduler shutdown.
612  *
613  * @param cls the RunContext which has to be acted upon
614  * @param tc the scheduler task context
615  */
616 static void
617 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
618 {
619   struct GNUNET_TESTBED_RunHandle *rc = cls;
620   struct GNUNET_TESTBED_Controller *c = rc->c;
621   unsigned int size;
622
623   /* reschedule */
624   rc->interrupt_task = GNUNET_SCHEDULER_add_delayed
625       (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt, rc);
626   rc_cleanup_operations (rc);
627   if ( (GNUNET_NO == rc->shutdown) &&
628        (NULL != c) &&
629        (NULL != c->opc_map) &&
630        (0 != (size = GNUNET_CONTAINER_multihashmap32_size (c->opc_map))))
631   {
632     LOG (GNUNET_ERROR_TYPE_WARNING,
633          "Shutdown postponed as there are %u operations currently active\n",
634          size);
635     c->opcq_empty_cb = &wait_op_completion;
636     c->opcq_empty_cls = rc;
637     return;
638   }
639   wait_op_completion (rc);
640 }
641
642
643 /**
644  * Function to return the string representation of the duration between current
645  * time and `pstart_time' in `RunContext'
646  *
647  * @param rc the RunContext
648  * @return the representation string; this is NOT reentrant
649  */
650 static const char *
651 prof_time (struct GNUNET_TESTBED_RunHandle *rc)
652 {
653   struct GNUNET_TIME_Relative ptime;
654
655   ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
656   return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
657 }
658
659
660 /**
661  * Task for starting peers
662  *
663  * @param cls the RunHandle
664  * @param tc the task context from scheduler
665  */
666 static void
667 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
668 {
669   struct GNUNET_TESTBED_RunHandle *rc = cls;
670   struct RunContextOperation *rcop;
671   unsigned int peer;
672
673   DEBUG ("Starting Peers\n");
674   rc->pstart_time = GNUNET_TIME_absolute_get ();
675   for (peer = 0; peer < rc->num_peers; peer++)
676   {
677     rcop = GNUNET_new (struct RunContextOperation);
678     rcop->rc = rc;
679     rcop->op  = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
680     GNUNET_assert (NULL != rcop->op);
681     rcop->cls = rc->peers[peer];
682     insert_rcop (rc, rcop);
683   }
684   rc->peer_count = 0;
685 }
686
687
688 /**
689  * Functions of this signature are called when a peer has been successfully
690  * created
691  *
692  * @param cls the closure from GNUNET_TESTBED_peer_create()
693  * @param peer the handle for the created peer; NULL on any error during
694  *          creation
695  * @param emsg NULL if peer is not NULL; else MAY contain the error description
696  */
697 static void
698 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
699 {
700   struct RunContextOperation *rcop = cls;
701   struct GNUNET_TESTBED_RunHandle *rc;
702
703   GNUNET_assert (NULL != rcop);
704   GNUNET_assert (NULL != (rc = rcop->rc));
705   remove_rcop (rc, rcop);
706   GNUNET_TESTBED_operation_done (rcop->op);
707   GNUNET_free (rcop);
708   if (NULL == peer)
709   {
710     if (NULL != emsg)
711       LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
712            emsg);
713     GNUNET_SCHEDULER_shutdown ();
714     return;
715   }
716   rc->peers[rc->peer_count] = peer;
717   rc->peer_count++;
718   if (rc->peer_count < rc->num_peers)
719     return;
720   DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
721   rc->state = RC_PEERS_CREATED;
722   GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
723 }
724
725
726 /**
727  * call test master callback
728  *
729  * @param rc the RunContext
730  */
731 static void
732 call_master (struct GNUNET_TESTBED_RunHandle *rc)
733 {
734   GNUNET_SCHEDULER_cancel (rc->timeout_task);
735   rc->timeout_task = NULL;
736   if (NULL != rc->test_master)
737     rc->test_master (rc->test_master_cls, rc, rc->num_peers, rc->peers,
738                      rc->links_succeeded, rc->links_failed);
739 }
740
741
742 /**
743  * Callbacks of this type are called when topology configuration is completed
744  *
745  * @param cls the operation closure given to
746  *          GNUNET_TESTBED_overlay_configure_topology_va() and
747  *          GNUNET_TESTBED_overlay_configure() calls
748  * @param nsuccess the number of successful overlay connects
749  * @param nfailures the number of overlay connects which failed
750  */
751 static void
752 topology_completion_callback (void *cls, unsigned int nsuccess,
753                               unsigned int nfailures)
754 {
755   struct GNUNET_TESTBED_RunHandle *rc = cls;
756
757   DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
758   GNUNET_TESTBED_operation_done (rc->topology_operation);
759   rc->topology_operation = NULL;
760   rc->links_succeeded = nsuccess;
761   rc->links_failed = nfailures;
762   rc->state = RC_READY;
763   call_master (rc);
764 }
765
766
767 /**
768  * Function to create peers
769  *
770  * @param rc the RunContext
771  */
772 static void
773 create_peers (struct GNUNET_TESTBED_RunHandle *rc)
774 {
775   struct RunContextOperation *rcop;
776   unsigned int peer;
777
778   DEBUG ("Creating peers\n");
779   rc->pstart_time = GNUNET_TIME_absolute_get ();
780   rc->peers =
781       GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
782   GNUNET_assert (NULL != rc->c);
783   rc->peer_count = 0;
784   for (peer = 0; peer < rc->num_peers; peer++)
785   {
786     rcop = GNUNET_new (struct RunContextOperation);
787     rcop->rc = rc;
788     rcop->op =
789         GNUNET_TESTBED_peer_create (rc->c,
790                                     (0 ==
791                                      rc->num_hosts) ? rc->h : rc->hosts[peer %
792                                                                         rc->num_hosts],
793                                     rc->cfg, &peer_create_cb, rcop);
794     GNUNET_assert (NULL != rcop->op);
795     insert_rcop (rc, rcop);
796   }
797 }
798
799
800 /**
801  * Signature of the event handler function called by the
802  * respective event controller.
803  *
804  * @param cls closure
805  * @param event information about the event
806  */
807 static void
808 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
809 {
810   struct GNUNET_TESTBED_RunHandle *rc = cls;
811   struct RunContextOperation *rcop;
812
813   if (RC_INIT == rc->state)
814   {
815     switch (event->type)
816     {
817     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
818       rcop = event->op_cls;
819       if (NULL != event->details.operation_finished.emsg)
820       {
821         LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
822         GNUNET_SCHEDULER_shutdown ();
823       }
824       else
825         rc->reg_hosts++;
826       GNUNET_assert (event->op == rcop->op);
827       remove_rcop (rc, rcop);
828       GNUNET_TESTBED_operation_done (rcop->op);
829       GNUNET_free (rcop);
830       if (rc->reg_hosts == rc->num_hosts)
831       {
832         rc->state = RC_LINKED;
833         create_peers (rc);
834       }
835       return;
836     default:
837       GNUNET_break (0);
838       GNUNET_SCHEDULER_shutdown ();
839       return;
840     }
841   }
842   if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
843     goto call_cc;
844   if (NULL == (rcop = search_rcop (rc, event->op)))
845     goto call_cc;
846   remove_rcop (rc, rcop);
847   GNUNET_TESTBED_operation_done (rcop->op);
848   GNUNET_free (rcop);
849   if ( (GNUNET_NO == rc->shutdown)
850        && (NULL != event->details.operation_finished.emsg) )
851   {
852     LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
853          event->details.operation_finished.emsg);
854     GNUNET_SCHEDULER_shutdown ();
855     return;
856   }
857   GNUNET_assert (GNUNET_YES == rc->shutdown);
858   switch (rc->state)
859   {
860   case RC_LINKED:
861   case RC_PEERS_CREATED:
862   case RC_READY:
863     rc->state = RC_PEERS_SHUTDOWN;
864     GNUNET_free_non_null (rc->peers);
865     rc->peers = NULL;
866     DEBUG ("Peers shut down in %s\n", prof_time (rc));
867     GNUNET_SCHEDULER_shutdown ();
868     break;
869   default:
870     GNUNET_assert (0);
871   }
872   return;
873
874 call_cc:
875   if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
876     rc->cc (rc->cc_cls, event);
877   if (GNUNET_TESTBED_ET_PEER_START != event->type)
878     return;
879   if (NULL == (rcop = search_rcop (rc, event->op))) /* Not our operation */
880     return;
881   remove_rcop (rc, rcop);
882   GNUNET_TESTBED_operation_done (rcop->op);
883   GNUNET_free (rcop);
884   rc->peer_count++;
885   if (rc->peer_count < rc->num_peers)
886     return;
887   DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
888   if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
889   {
890     switch (rc->topology)
891     {
892     case GNUNET_TESTBED_TOPOLOGY_NONE:
893       GNUNET_assert (0);
894     case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
895     case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
896     case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
897       rc->topology_operation =
898           GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
899                                                      rc->peers, &rc->num_oc,
900                                                      &topology_completion_callback,
901                                                      rc,
902                                                      rc->topology,
903                                                      rc->random_links,
904                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
905       break;
906     case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
907       GNUNET_assert (NULL != rc->topo_file);
908       rc->topology_operation =
909           GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
910                                                      rc->peers, &rc->num_oc,
911                                                      &topology_completion_callback,
912                                                      rc,
913                                                      rc->topology,
914                                                      rc->topo_file,
915                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
916       break;
917     case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
918       {
919         unsigned long long number;
920         unsigned int cap;
921         GNUNET_assert (GNUNET_OK ==
922                        GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
923                                                               SCALE_FREE_CAP,
924                                                               &number));
925         cap = (unsigned int) number;
926         GNUNET_assert (GNUNET_OK ==
927                        GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
928                                                               SCALE_FREE_M,
929                                                               &number));
930         rc->topology_operation =
931             GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
932                                                        rc->peers, &rc->num_oc,
933                                                        &topology_completion_callback,
934                                                        rc,
935                                                        rc->topology,
936                                                        cap,    /* uint16_t */
937                                                        (unsigned int) number, /* uint8_t */
938                                                        GNUNET_TESTBED_TOPOLOGY_OPTION_END);
939       }
940       break;
941     default:
942       rc->topology_operation =
943           GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
944                                                      rc->peers, &rc->num_oc,
945                                                      &topology_completion_callback,
946                                                      rc,
947                                                      rc->topology,
948                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
949     }
950     if (NULL == rc->topology_operation)
951       LOG (GNUNET_ERROR_TYPE_WARNING,
952            "Not generating a topology. Check number of peers\n");
953     else
954     {
955       DEBUG ("Creating overlay topology\n");
956       rc->pstart_time = GNUNET_TIME_absolute_get ();
957       return;
958     }
959   }
960   rc->state = RC_READY;
961   call_master (rc);
962 }
963
964
965 /**
966  * Task to register all hosts available in the global host list
967  *
968  * @param cls the RunContext
969  * @param tc the scheduler task context
970  */
971 static void
972 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
973
974
975 /**
976  * Callback which will be called to after a host registration succeeded or failed
977  *
978  * @param cls the closure
979  * @param emsg the error message; NULL if host registration is successful
980  */
981 static void
982 host_registration_completion (void *cls, const char *emsg)
983 {
984   struct GNUNET_TESTBED_RunHandle *rc = cls;
985
986   rc->reg_handle = NULL;
987   if (NULL != emsg)
988   {
989     LOG (GNUNET_ERROR_TYPE_WARNING,
990          _("Host registration failed for a host. Error: %s\n"), emsg);
991     GNUNET_SCHEDULER_shutdown ();
992     return;
993   }
994   rc->register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, rc);
995 }
996
997
998 /**
999  * Task to register all hosts available in the global host list
1000  *
1001  * @param cls RunContext
1002  * @param tc the scheduler task context
1003  */
1004 static void
1005 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1006 {
1007   struct GNUNET_TESTBED_RunHandle *rc = cls;
1008   struct RunContextOperation *rcop;
1009   unsigned int slave;
1010
1011   rc->register_hosts_task = NULL;
1012   if (rc->reg_hosts == rc->num_hosts)
1013   {
1014     DEBUG ("All hosts successfully registered\n");
1015     /* Start slaves */
1016     for (slave = 0; slave < rc->num_hosts; slave++)
1017     {
1018       rcop = GNUNET_new (struct RunContextOperation);
1019       rcop->rc = rc;
1020       rcop->op =
1021           GNUNET_TESTBED_controller_link (rcop, rc->c, rc->hosts[slave],
1022                                           rc->h, GNUNET_YES);
1023       GNUNET_assert (NULL != rcop->op);
1024       insert_rcop (rc, rcop);
1025     }
1026     rc->reg_hosts = 0;
1027     return;
1028   }
1029   rc->reg_handle =
1030       GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
1031                                     host_registration_completion, rc);
1032   rc->reg_hosts++;
1033 }
1034
1035
1036 /**
1037  * Callback to signal successfull startup of the controller process
1038  *
1039  * @param cls the closure from GNUNET_TESTBED_controller_start()
1040  * @param cfg the configuration with which the controller has been started;
1041  *          NULL if status is not GNUNET_OK
1042  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1043  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1044  */
1045 static void
1046 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1047                       int status)
1048 {
1049   struct GNUNET_TESTBED_RunHandle *rc = cls;
1050   uint64_t event_mask;
1051
1052   if (status != GNUNET_OK)
1053   {
1054     rc->cproc = NULL;
1055     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1056                 _("Controller crash detected. Shutting down.\n"));
1057     GNUNET_SCHEDULER_shutdown ();
1058     return;
1059   }
1060   GNUNET_CONFIGURATION_destroy (rc->cfg);
1061   rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1062   event_mask = rc->event_mask;
1063   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1064   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1065   if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
1066     event_mask |= GNUNET_TESTBED_ET_CONNECT;
1067   rc->c =
1068       GNUNET_TESTBED_controller_connect (rc->h, event_mask, &event_cb, rc);
1069   if (0 < rc->num_hosts)
1070   {
1071     rc->reg_hosts = 0;
1072     rc->register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, rc);
1073     return;
1074   }
1075   rc->state = RC_LINKED;
1076   create_peers (rc);
1077 }
1078
1079
1080 /**
1081  * Callback function invoked for each interface found.
1082  *
1083  * @param cls closure
1084  * @param name name of the interface (can be NULL for unknown)
1085  * @param isDefault is this presumably the default interface
1086  * @param addr address of this interface (can be NULL for unknown or unassigned)
1087  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
1088  * @param netmask the network mask (can be NULL for unknown or unassigned))
1089  * @param addrlen length of the address
1090  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
1091  */
1092 static int
1093 netint_proc (void *cls, const char *name, int isDefault,
1094              const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
1095              const struct sockaddr *netmask, socklen_t addrlen)
1096 {
1097   struct GNUNET_TESTBED_RunHandle *rc = cls;
1098   char hostip[NI_MAXHOST];
1099   char *buf;
1100
1101   if (sizeof (struct sockaddr_in) != addrlen)
1102     return GNUNET_OK;           /* Only consider IPv4 for now */
1103   if (0 !=
1104       getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
1105     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
1106   if (NULL == rc->trusted_ip)
1107   {
1108     rc->trusted_ip = GNUNET_strdup (hostip);
1109     return GNUNET_YES;
1110   }
1111   (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
1112   GNUNET_free (rc->trusted_ip);
1113   rc->trusted_ip = buf;
1114   return GNUNET_YES;
1115 }
1116
1117
1118 /**
1119  * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1120  * inform whether the given host is habitable or not. The Handle returned by
1121  * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1122  *
1123  * @param cls NULL
1124  * @param host the host whose status is being reported; will be NULL if the host
1125  *          given to GNUNET_TESTBED_is_host_habitable() is NULL
1126  * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1127  */
1128 static void
1129 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
1130                    int status)
1131 {
1132   struct CompatibilityCheckContext *hc = cls;
1133   struct GNUNET_TESTBED_RunHandle *rc;
1134   struct GNUNET_TESTBED_Host **old_hosts;
1135   unsigned int nhost;
1136
1137   GNUNET_assert (NULL != (rc = hc->rc));
1138   nhost = hc->index;
1139   GNUNET_assert (nhost <= rc->num_hosts);
1140   GNUNET_assert (host == rc->hosts[nhost]);
1141   hc->h = NULL;
1142   if (GNUNET_NO == status)
1143   {
1144     if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1145       LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1146            GNUNET_TESTBED_host_get_hostname (host));
1147     else
1148       LOG (GNUNET_ERROR_TYPE_ERROR,
1149            _("Testbed cannot be started on localhost\n"));
1150     GNUNET_SCHEDULER_shutdown ();
1151     return;
1152   }
1153   rc->reg_hosts++;
1154   if (rc->reg_hosts < rc->num_hosts)
1155     return;
1156   GNUNET_free (rc->hclist);
1157   rc->hclist = NULL;
1158   rc->h = rc->hosts[0];
1159   rc->num_hosts--;
1160   if (0 < rc->num_hosts)
1161   {
1162     old_hosts = rc->hosts;
1163     rc->hosts =
1164         GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
1165     memcpy (rc->hosts, &old_hosts[1],
1166             (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
1167     GNUNET_free (old_hosts);
1168   }
1169   else
1170   {
1171     GNUNET_free (rc->hosts);
1172     rc->hosts = NULL;
1173   }
1174   GNUNET_TESTBED_host_resolve_ (rc->h);
1175   for (nhost = 0; nhost < rc->num_hosts; nhost++)
1176     GNUNET_TESTBED_host_resolve_ (rc->hosts[nhost]);
1177   GNUNET_OS_network_interfaces_list (netint_proc, rc);
1178   if (NULL == rc->trusted_ip)
1179     rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
1180   rc->cproc =
1181       GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h,
1182                                        &controller_status_cb, rc);
1183   GNUNET_free (rc->trusted_ip);
1184   rc->trusted_ip = NULL;
1185   if (NULL == rc->cproc)
1186   {
1187     LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1188     GNUNET_SCHEDULER_shutdown ();
1189   }
1190 }
1191
1192
1193 /**
1194  * Task run upon timeout while setting up the testbed
1195  *
1196  * @param cls the RunContext
1197  * @param tc the task context
1198  */
1199 static void
1200 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1201 {
1202   struct GNUNET_TESTBED_RunHandle *rc = cls;
1203
1204   rc->timeout_task = NULL;
1205   LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1206    GNUNET_SCHEDULER_shutdown ();
1207    if (NULL != rc->test_master)
1208      rc->test_master (rc->test_master_cls, rc, 0, NULL, 0, 0);
1209    rc->test_master = NULL;
1210 }
1211
1212
1213 /**
1214  * Convenience method for running a testbed with
1215  * a single call.  Underlay and overlay topology
1216  * are configured using the "UNDERLAY" and "OVERLAY"
1217  * options in the "[testbed]" section of the configuration\
1218  * (with possible options given in "UNDERLAY_XXX" and/or
1219  * "OVERLAY_XXX").
1220  *
1221  * The testbed is to be terminated using a call to
1222  * "GNUNET_SCHEDULER_shutdown".
1223  *
1224  * @param host_filename name of the file with the 'hosts', NULL
1225  *        to run everything on 'localhost'
1226  * @param cfg configuration to use (for testbed, controller and peers)
1227  * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1228  * @param event_mask bit mask with set of events to call 'cc' for;
1229  *                   or-ed values of "1LL" shifted by the
1230  *                   respective 'enum GNUNET_TESTBED_EventType'
1231  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1232  * @param cc controller callback to invoke on events; This callback is called
1233  *          for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1234  *          set in the event_mask as this is the only way get access to the
1235  *          handle of each peer
1236  * @param cc_cls closure for cc
1237  * @param test_master this callback will be called once the test is ready
1238  * @param test_master_cls closure for 'test_master'.
1239  */
1240 void
1241 GNUNET_TESTBED_run (const char *host_filename,
1242                     const struct GNUNET_CONFIGURATION_Handle *cfg,
1243                     unsigned int num_peers, uint64_t event_mask,
1244                     GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1245                     GNUNET_TESTBED_TestMaster test_master,
1246                     void *test_master_cls)
1247 {
1248   struct GNUNET_TESTBED_RunHandle *rc;
1249   char *topology;
1250   struct CompatibilityCheckContext *hc;
1251   struct GNUNET_TIME_Relative timeout;
1252   unsigned long long number;
1253   unsigned int hid;
1254   unsigned int nhost;
1255
1256   GNUNET_assert (num_peers > 0);
1257   rc = GNUNET_new (struct GNUNET_TESTBED_RunHandle);
1258   rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1259 #if ENABLE_SUPERMUC
1260   rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1261                                                               &rc->hosts);
1262   if (0 == rc->num_hosts)
1263   {
1264     LOG (GNUNET_ERROR_TYPE_WARNING,
1265            _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1266     goto error_cleanup;
1267   }
1268 #else
1269   if (NULL != host_filename)
1270   {
1271     rc->num_hosts =
1272         GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1273                                              &rc->hosts);
1274     if (0 == rc->num_hosts)
1275     {
1276       LOG (GNUNET_ERROR_TYPE_WARNING,
1277            _("No hosts loaded. Need at least one host\n"));
1278       goto error_cleanup;
1279     }
1280   }
1281   else
1282     rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1283 #endif
1284   rc->num_peers = num_peers;
1285   rc->event_mask = event_mask;
1286   rc->cc = cc;
1287   rc->cc_cls = cc_cls;
1288   rc->test_master = test_master;
1289   rc->test_master_cls = test_master_cls;
1290   rc->state = RC_INIT;
1291   rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1292   if (GNUNET_OK ==
1293       GNUNET_CONFIGURATION_get_value_string (rc->cfg, TESTBED_CONFIG_SECTION,
1294                                              "OVERLAY_TOPOLOGY", &topology))
1295   {
1296     if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1297     {
1298       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1299                                  "OVERLAY_TOPLOGY",
1300                                  _
1301                                  ("Specified topology must be supported by testbed"));
1302     }
1303     GNUNET_free (topology);
1304   }
1305   switch (rc->topology)
1306   {
1307   case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1308   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1309   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1310     if (GNUNET_OK !=
1311         GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
1312                                                "OVERLAY_RANDOM_LINKS",
1313                                                &number))
1314     {
1315       /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1316        * option to be set to the number of random links to be established  */
1317       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1318                                  "OVERLAY_RANDOM_LINKS");
1319       goto error_cleanup;
1320     }
1321     if (number > UINT32_MAX)
1322     {
1323       GNUNET_break (0);         /* Too big number */
1324       goto error_cleanup;
1325     }
1326     rc->random_links = (unsigned int) number;
1327     break;
1328   case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1329     if (GNUNET_OK !=
1330         GNUNET_CONFIGURATION_get_value_filename (rc->cfg, TESTBED_CONFIG_SECTION,
1331                                                "OVERLAY_TOPOLOGY_FILE",
1332                                                &rc->topo_file))
1333     {
1334       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1335                                  "OVERLAY_TOPOLOGY_FILE");
1336       goto error_cleanup;
1337     }
1338     goto warn_ignore;
1339   case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
1340     if (GNUNET_OK !=
1341         GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
1342                                                SCALE_FREE_CAP, &number))
1343     {
1344       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1345                                  SCALE_FREE_CAP);
1346       goto error_cleanup;
1347     }
1348     if (UINT16_MAX < number)
1349     {
1350       LOG (GNUNET_ERROR_TYPE_ERROR,
1351            _("Maximum number of edges a peer can have in a scale free topology"
1352              " cannot be more than %u.  Given `%s = %llu'"), UINT16_MAX,
1353            SCALE_FREE_CAP, number);
1354       goto error_cleanup;
1355     }
1356     if (GNUNET_OK !=
1357         GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
1358                                                SCALE_FREE_M, &number))
1359     {
1360       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1361                                  SCALE_FREE_M);
1362       goto error_cleanup;
1363     }
1364     if (UINT8_MAX < number)
1365     {
1366       LOG (GNUNET_ERROR_TYPE_ERROR,
1367            _("The number of edges that can established when adding a new node"
1368              " to scale free topology cannot be more than %u.  Given `%s = %llu'"),
1369            UINT8_MAX, SCALE_FREE_M, number);
1370       goto error_cleanup;
1371     }
1372     goto warn_ignore;
1373   default:
1374   warn_ignore:
1375     /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1376     if (GNUNET_YES ==
1377         GNUNET_CONFIGURATION_have_value (rc->cfg, TESTBED_CONFIG_SECTION,
1378                                          "OVERLAY_RANDOM_LINKS"))
1379       LOG (GNUNET_ERROR_TYPE_WARNING,
1380            "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1381     break;
1382   }
1383   if (0 != rc->num_hosts)
1384   {
1385     rc->hclist = GNUNET_malloc (sizeof (struct CompatibilityCheckContext)
1386                                 * rc->num_hosts);
1387     for (nhost = 0; nhost < rc->num_hosts; nhost++)
1388     {
1389       hc = &rc->hclist[nhost];
1390       hc->index = nhost;
1391       hc->rc = rc;
1392       hc->h = GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1393                                                 &host_habitable_cb, hc);
1394       if (NULL == hc->h)
1395       {
1396         GNUNET_break (0);
1397         for (nhost = 0; nhost < rc->num_hosts; nhost++)
1398         {
1399           hc = &rc->hclist[nhost];
1400           if (NULL != hc->h)
1401             GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
1402         }
1403         GNUNET_free (rc->hclist);
1404         rc->hclist = NULL;
1405         goto error_cleanup;
1406       }
1407     }
1408   }
1409   else
1410     rc->cproc =
1411         GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h,
1412                                          &controller_status_cb, rc);
1413   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, TESTBED_CONFIG_SECTION,
1414                                                         "SETUP_TIMEOUT",
1415                                                         &timeout))
1416   {
1417     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1418                                              DEFAULT_SETUP_TIMEOUT);
1419   }
1420   rc->rcop_map = GNUNET_CONTAINER_multihashmap32_create (256);
1421   rc->timeout_task =
1422       GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1423   rc->interrupt_task =
1424       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1425                                     rc);
1426   return;
1427
1428 error_cleanup:
1429   if (NULL != rc->h)
1430     GNUNET_TESTBED_host_destroy (rc->h);
1431   if (NULL != rc->hosts)
1432   {
1433     for (hid = 0; hid < rc->num_hosts; hid++)
1434       if (NULL != rc->hosts[hid])
1435         GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1436     GNUNET_free (rc->hosts);
1437   }
1438   if (NULL != rc->cfg)
1439     GNUNET_CONFIGURATION_destroy (rc->cfg);
1440   GNUNET_free (rc);
1441 }
1442
1443
1444 /**
1445  * Obtain handle to the master controller from a testbed run.  The handle
1446  * returned should not be disconnected.
1447  *
1448  * @param h the testbed run handle
1449  * @return handle to the master controller
1450  */
1451 struct GNUNET_TESTBED_Controller *
1452 GNUNET_TESTBED_run_get_controller_handle (struct GNUNET_TESTBED_RunHandle *h)
1453 {
1454   return h->c;
1455 }
1456
1457
1458 /* end of testbed_api_testbed.c */