-controller startup, connect and duals
[oweals/gnunet.git] / src / include / gnunet_testbed_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009, 2012 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 include/gnunet_testbed_service.h
23  * @brief API for writing tests and creating large-scale
24  *        emulation testbeds for GNUnet.
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_TESTBED_SERVICE_H
29 #define GNUNET_TESTBED_SERVICE_H
30
31 #include "gnunet_util_lib.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #if 0                           /* keep Emacsens' auto-indent happy */
37 }
38 #endif
39 #endif
40
41
42 /**
43  * Opaque handle to a host running experiments managed by the testbed framework.
44  * The master process must be able to SSH to this host without password (via
45  * ssh-agent).
46  */
47 struct GNUNET_TESTBED_Host;
48
49 /**
50  * Opaque handle to a peer controlled by the testbed framework.  A peer runs
51  * at a particular host.
52  */ 
53 struct GNUNET_TESTBED_Peer;
54
55 /**
56  * Opaque handle to an abstract operation to be executed by the testbed framework.
57  */
58 struct GNUNET_TESTBED_Operation;
59
60 /**
61  * Handle to interact with a GNUnet testbed controller.  Each
62  * controller has at least one master handle which is created when the
63  * controller is created; this master handle interacts with the
64  * controller process, destroying it destroys the controller (by
65  * closing stdin of the controller process).  Additionally,
66  * controllers can interact with each other (in a P2P fashion); those
67  * links are established via TCP/IP on the controller's service port.
68  */
69 struct GNUNET_TESTBED_Controller;
70
71 /**
72  * Handle to a large-scale testbed that is managed at a high level.
73  */
74 struct GNUNET_TESTBED_Testbed;
75
76
77 /**
78  * Create a host to run peers and controllers on.
79  * 
80  * @param hostname name of the host, use "NULL" for localhost
81  * @param username username to use for the login; may be NULL
82  * @param port port number to use for ssh; use 0 to let ssh decide
83  * @return handle to the host, NULL on error
84  */
85 struct GNUNET_TESTBED_Host *
86 GNUNET_TESTBED_host_create (const char *hostname,
87                             const char *username,
88                             uint16_t port);
89
90
91
92 /**
93  * Create a host to run peers and controllers on.  This function is used
94  * if a peer learns about a host via IPC between controllers (and thus 
95  * some higher-level controller has already determined the unique IDs).
96  * 
97  * @param id global host ID assigned to the host; 0 is
98  *        reserved to always mean 'localhost'
99  * @param hostname name of the host, use "NULL" for localhost
100  * @param username username to use for the login; may be NULL
101  * @param port port number to use for ssh; use 0 to let ssh decide
102  * @return handle to the host, NULL on error
103  */
104 struct GNUNET_TESTBED_Host *
105 GNUNET_TESTBED_host_create_with_id (uint32_t id,
106                                     const char *hostname,
107                                     const char *username,
108                                     uint16_t port);
109
110
111 /**
112  * Load a set of hosts from a configuration file.
113  *
114  * @param filename file with the host specification
115  * @param hosts set to the hosts found in the file
116  * @return number of hosts returned in 'hosts', 0 on error
117  */
118 unsigned int
119 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
120                                      struct GNUNET_TESTBED_Host **hosts);
121
122
123 /**
124  * Destroy a host handle.  Must only be called once everything
125  * running on that host has been stopped.
126  *
127  * @param host handle to destroy
128  */
129 void
130 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host);
131
132
133 /**
134  * Enumeration with (at most 64) possible event types that
135  * can be monitored using the testbed framework.
136  */
137 enum GNUNET_TESTBED_EventType
138 {
139   /**
140    * A peer has been started.
141    */
142   GNUNET_TESTBED_ET_PEER_START = 0,
143
144   /**
145    * A peer has been stopped.
146    */
147   GNUNET_TESTBED_ET_PEER_STOP = 1,
148
149   /**
150    * A connection between two peers was established.
151    */
152   GNUNET_TESTBED_ET_CONNECT = 2,
153
154   /**
155    * A connection between two peers was torn down.
156    */
157   GNUNET_TESTBED_ET_DISCONNECT = 3,
158
159   /**
160    * A requested testbed operation has been completed.
161    */
162   GNUNET_TESTBED_ET_OPERATION_FINISHED = 4,
163
164   /**
165    * The 'GNUNET_TESTBED_run' operation has been completed
166    */
167   GNUNET_TESTBED_ET_TESTBED_ONLINE = 5
168
169 };
170
171
172 /**
173  * Types of information that can be requested about a peer.
174  */
175 enum GNUNET_TESTBED_PeerInformationType
176 {
177
178   /**
179    * Special value (not valid for requesting information)
180    * that is used in the event struct if a 'generic' pointer
181    * is returned (for other operations not related to this
182    * enumeration).
183    */
184   GNUNET_TESTBED_PIT_GENERIC = 0,
185
186   /**
187    * What host is the peer running on?  Returns a 'const struct
188    * GNUNET_TESTBED_Host *'.  Valid until
189    * 'GNUNET_TESTBED_operation_done' is called.
190    */
191   GNUNET_TESTBED_PIT_HOST,
192
193   /**
194    * What configuration is the peer using?  Returns a 'const struct
195    * GNUNET_CONFIGURATION_Handle *'.  Valid until
196    * 'GNUNET_TESTNIG_operation_done' is called.  However, the
197    * values may be inaccurate if the peer is reconfigured in
198    * the meantime.
199    */
200   GNUNET_TESTBED_PIT_CONFIGURATION,
201
202   /**
203    * What is the identity of the peer?  Returns a
204    * 'const struct GNUNET_PeerIdentity *'.  Valid until
205    * 'GNUNET_TESTNIG_operation_done' is called.
206    */
207   GNUNET_TESTBED_PIT_IDENTITY
208
209 };
210
211
212 /**
213  * Argument to GNUNET_TESTBED_ControllerCallback with details about
214  * the event.
215  */
216 struct GNUNET_TESTBED_EventInformation
217 {
218   
219   /**
220    * Type of the event.
221    */
222   enum GNUNET_TESTBED_EventType type;
223
224   /**
225    * Details about the event.
226    */
227   union
228   {
229     
230     /**
231      * Details about peer start event.
232      */ 
233     struct
234     {
235       /**
236        * Handle for the host where the peer
237        * was started.
238        */
239       struct GNUNET_TESTBED_Host *host;
240
241       /**
242        * Handle for the peer that was started.
243        */
244       struct GNUNET_TESTBED_Peer *peer;
245       
246     } peer_start;
247
248     /**
249      * Details about peer stop event.
250      */ 
251     struct
252     {
253
254       /**
255        * Handle for the peer that was started.
256        */
257       struct GNUNET_TESTBED_Peer *peer;
258       
259     } peer_stop;
260
261     /**
262      * Details about connect event.
263      */ 
264     struct
265     {
266       /**
267        * Handle for one of the connected peers.
268        */
269       struct GNUNET_TESTBED_Peer *peer1;
270
271       /**
272        * Handle for one of the connected peers.
273        */
274       struct GNUNET_TESTBED_Peer *peer2;
275
276     } peer_connect;
277
278     /**
279      * Details about disconnect event.
280      */ 
281     struct
282     {
283       /**
284        * Handle for one of the disconnected peers.
285        */
286       struct GNUNET_TESTBED_Peer *peer1;
287
288       /**
289        * Handle for one of the disconnected peers.
290        */
291       struct GNUNET_TESTBED_Peer *peer2;
292       
293     } peer_disconnect;
294
295     /**
296      * Details about an operation finished event.
297      */ 
298     struct 
299     {
300
301       /**
302        * Handle for the operation that was finished.
303        */
304       struct GNUNET_TESTBED_Operation *operation;
305
306       /**
307        * Closure that was passed in when the event was
308        * requested.
309        */
310       void *op_cls;
311
312       /**
313        * Error message for the operation, NULL on success.
314        */ 
315       const char *emsg;
316
317       /**
318        * Peer information type; captures which of the types
319        * in the 'op_result' is actually in use.
320        */
321       enum GNUNET_TESTBED_PeerInformationType pit;
322
323       /**
324        * Pointer to an operation-specific return value; NULL on error;
325        * can be NULL for certain operations.  Valid until
326        * 'GNUNET_TESTBED_operation_done' is called.
327        */
328       union
329       {
330         /**
331          * No result (NULL pointer) or generic result
332          * (whatever the GNUNET_TESTBED_ConnectAdapter returned).
333          */
334         void *generic;
335
336         /**
337          * Identity of host running the peer.
338          */
339         struct GNUNET_TESTBED_Host *host;
340
341         /**
342          * Identity of the peer.
343          */
344         const struct GNUNET_PeerIdentity *pid;
345
346         /**
347          * Configuration of the peer.
348          */
349         const struct GNUNET_CONFIGURATION_Handle *cfg;
350
351       } op_result;
352
353     } operation_finished;   
354
355
356     /**
357      * Details about an testbed run completed event.
358      */ 
359     struct 
360     {
361
362       /**
363        * Error message for the operation, NULL on success.
364        */ 
365       const char *emsg;
366
367       /**
368        * Array of peers now running (valid until
369        * 'GNUNET_TESTBED_testbed_stop' is called).  Note that it is
370        * not allowed to call 'GNUNET_TESTBED_peer_destroy' on peers
371        * from this array.
372        */
373       struct GNUNET_TESTBED_Peer **peers;
374
375       /**
376        * Size of the 'peers' array.
377        */
378       unsigned int num_peers;
379       
380     } testbed_run_finished;   
381
382   } details;
383
384 };
385
386
387 /**
388  * Signature of the event handler function called by the
389  * respective event controller.
390  *
391  * @param cls closure
392  * @param event information about the event
393  */
394 typedef void (*GNUNET_TESTBED_ControllerCallback)(void *cls,
395                                                   const struct GNUNET_TESTBED_EventInformation *event);
396
397
398 /**
399  * Opaque Handle for Controller process
400  */
401 struct GNUNET_TESTBED_ControllerProc;
402
403
404 /**
405  * Starts a controller process at the host
406  *
407  * @param host the host where the controller has to be started; NULL for localhost
408  * @return the controller process handle
409  */
410 struct GNUNET_TESTBED_ControllerProc *
411 GNUNET_TESTBED_controller_start (struct GNUNET_TESTBED_Host *host);
412
413
414 /**
415  * Stop the controller process (also will terminate all peers and controllers
416  * dependent on this controller).  This function blocks until the testbed has
417  * been fully terminated (!).
418  *
419  * @param cproc the controller process handle
420  */
421 void
422 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc);
423
424
425 /**
426  * Connect to a controller process using the given configuration at the
427  * given host.
428  *
429  * @param cfg configuration to use
430  * @param host host to run the controller on; This should be the same host if
431  *          the controller was previously started with
432  *          GNUNET_TESTBED_controller_start; NULL for localhost
433  * @param host host where this controller is being run;
434  * @param event_mask bit mask with set of events to call 'cc' for;
435  *                   or-ed values of "1LL" shifted by the
436  *                   respective 'enum GNUNET_TESTBED_EventType'
437  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
438  * @param cc controller callback to invoke on events
439  * @param cc_cls closure for cc
440  * @return handle to the controller
441  */
442 struct GNUNET_TESTBED_Controller *
443 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
444                                    struct GNUNET_TESTBED_Host *host,
445                                    uint64_t event_mask,
446                                    GNUNET_TESTBED_ControllerCallback cc,
447                                    void *cc_cls);
448
449
450 /**
451  * Configure shared services at a controller.  Using this function,
452  * you can specify that certain services (such as "resolver")
453  * should not be run for each peer but instead be shared
454  * across N peers on the specified host.  This function
455  * must be called before any peers are created at the host.
456  * 
457  * @param controller controller to configure
458  * @param service_name name of the service to share
459  * @param num_peers number of peers that should share one instance
460  *        of the specified service (1 for no sharing is the default),
461  *        use 0 to disable the service
462  */
463 void
464 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
465                                              const char *service_name,
466                                              uint32_t num_peers);
467
468
469 /**
470  * Stop the given controller (also will terminate all peers and
471  * controllers dependent on this controller).  This function 
472  * blocks until the testbed has been fully terminated (!).
473  *
474  * @param controller handle to controller to stop
475  */
476 void
477 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller);
478
479
480 /**
481  * Opaque handle for host registration
482  */
483 struct GNUNET_TESTBED_HostRegistrationHandle;
484
485
486 /**
487  * Callback which will be called to after a host registration succeeded or failed
488  *
489  * @param cls the closure
490  * @param emsg the error message; NULL if host registration is successful
491  */
492 typedef void (* GNUNET_TESTBED_HostRegistrationCompletion) (void *cls, 
493                                                             const char *emsg);
494
495
496 /**
497  * Register a host with the controller
498  *
499  * @param controller the controller handle
500  * @param host the host to register
501  * @param cc the completion callback to call to inform the status of
502  *          registration. After calling this callback the registration handle
503  *          will be invalid. Cannot be NULL
504  * @param cc_cls the closure for the cc
505  * @return handle to the host registration which can be used to cancel the
506  *           registration; NULL if another registration handle is present and
507  *           is not cancelled
508  */
509 struct GNUNET_TESTBED_HostRegistrationHandle *
510 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
511                               struct GNUNET_TESTBED_Host *host,
512                               GNUNET_TESTBED_HostRegistrationCompletion cc,
513                               void *cc_cls);
514
515
516 /**
517  * Cancel the pending registration. Note that the registration message will
518  * already be queued to be sent to the service, cancellation has only the
519  * effect that the registration completion callback for the registration is
520  * never called and from our perspective the host is not registered until the
521  * completion callback is called.
522  *
523  * @param handle the registration handle to cancel
524  */
525 void
526 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
527                                     *handle);
528
529
530 /**
531  * Create a link from a 'master' controller to a slave controller.
532  * Whenever the master controller is asked to start a peer at the
533  * given 'delegated_host', it will delegate the request to the
534  * specified slave controller.  Note that the slave controller runs at
535  * the 'slave_host', which may or may not be the same host as the
536  * 'delegated_host' (for hierarchical delegations).  The configuration
537  * of the slave controller is given and to be used to either create
538  * the slave controller or to connect to an existing slave controller
539  * process.  'is_subordinate' specifies if the given slave controller
540  * should be started and managed by the master controller, or if the
541  * slave already has a master and this is just a secondary master that
542  * is also allowed to use the existing slave.
543  *
544  * @param master handle to the master controller who creates the association
545  * @param delegated_host requests to which host should be delegated
546  * @param slave_host which host is used to run the slave controller 
547  * @param slave_cfg configuration to use for the slave controller
548  * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
549  *                       by the master controller; GNUNET_NO if we are just
550  *                       allowed to use the slave via TCP/IP
551  */
552 void
553 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
554                                 struct GNUNET_TESTBED_Host *delegated_host,
555                                 struct GNUNET_TESTBED_Host *slave_host,
556                                 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
557                                 int is_subordinate);
558
559
560 /**
561  * Create the given peer at the specified host using the given
562  * controller.  If the given controller is not running on the target
563  * host, it should find or create a controller at the target host and
564  * delegate creating the peer.  Explicit delegation paths can be setup
565  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
566  * path exists, a direct link with a subordinate controller is setup
567  * for the first delegated peer to a particular host; the subordinate
568  * controller is then destroyed once the last peer that was delegated
569  * to the remote host is stopped.
570  *
571  * Creating the peer only creates the handle to manipulate and further
572  * configure the peer; use "GNUNET_TESTBED_peer_start" and
573  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
574  * processes.
575  *
576  * Note that the given configuration will be adjusted by the
577  * controller to avoid port/path conflicts with other peers.
578  * The "final" configuration can be obtained using
579  * 'GNUNET_TESTBED_peer_get_information'.
580  *
581  * @param controller controller process to use
582  * @param host host to run the peer on
583  * @param cfg configuration to use for the peer
584  * @return handle to the peer (actual startup will happen asynchronously)
585  */
586 struct GNUNET_TESTBED_Peer *
587 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
588                             struct GNUNET_TESTBED_Host *host,
589                             const struct GNUNET_CONFIGURATION_Handle *cfg);
590
591
592 /**
593  * Start the given peer.
594  *
595  * @param peer peer to start
596  * @return handle to the operation
597  */
598 struct GNUNET_TESTBED_Operation *
599 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer);
600
601
602 /**
603  * Stop the given peer.  The handle remains valid (use
604  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
605  * state of the peer).
606  *
607  * @param peer peer to stop
608  * @return handle to the operation
609  */
610 struct GNUNET_TESTBED_Operation *
611 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer);
612
613
614 /**
615  * Request information about a peer.
616  *
617  * @param peer peer to request information about
618  * @param pit desired information
619  * @return handle to the operation
620  */
621 struct GNUNET_TESTBED_Operation *
622 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
623                                      enum GNUNET_TESTBED_PeerInformationType pit);
624
625
626 /**
627  * Change peer configuration.  Must only be called while the
628  * peer is stopped.  Ports and paths cannot be changed this
629  * way.
630  *
631  * @param peer peer to change configuration for
632  * @param cfg new configuration (differences to existing
633  *            configuration only)
634  * @return handle to the operation
635  */
636 struct GNUNET_TESTBED_Operation *
637 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
638                                           const struct GNUNET_CONFIGURATION_Handle *cfg);
639
640
641 /**
642  * Destroy the given peer; the peer should have been
643  * stopped first (if it was started).
644  *
645  * @param peer peer to stop
646  * @return handle to the operation
647  */
648 struct GNUNET_TESTBED_Operation *
649 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer);
650
651
652 /**
653  * Options for peer connections.
654  */
655 enum GNUNET_TESTBED_ConnectOption
656 {
657   /**
658    * No option (not valid as an argument).
659    */
660   GNUNET_TESTBED_CO_NONE = 0,
661   
662   /**
663    * Allow or disallow a connection between the specified peers.  
664    * Followed by GNUNET_NO (int) if a connection is disallowed
665    * or GNUNET_YES if a connection is allowed.  Note that the
666    * default (all connections allowed or disallowed) is
667    * specified in the configuration of the controller.
668    */
669   GNUNET_TESTBED_CO_ALLOW = 1,
670   
671   /**
672    * FIXME: add (and implement) options to limit connection to
673    * particular transports, force simulation of particular latencies
674    * or message loss rates, or set bandwidth limitations.
675    */
676   
677 };
678
679
680 /**
681  * Manipulate the P2P underlay topology by configuring a link
682  * between two peers.  
683  *
684  * @param op_cls closure argument to give with the operation event
685  * @param p1 first peer
686  * @param p2 second peer
687  * @param co option to change
688  * @param ap option-specific values
689  * @return handle to the operation, NULL if configuring the link at this
690  *         time is not allowed
691  */
692 struct GNUNET_TESTBED_Operation *
693 GNUNET_TESTBED_underlay_configure_link_va (void *op_cls,
694                                            struct GNUNET_TESTBED_Peer *p1,
695                                            struct GNUNET_TESTBED_Peer *p2,
696                                            enum GNUNET_TESTBED_ConnectOption co,
697                                            va_list ap);
698
699
700 /**
701  * Manipulate the P2P underlay topology by configuring a link
702  * between two peers.  
703  *
704  * @param op_cls closure argument to give with the operation event
705  * @param p1 first peer
706  * @param p2 second peer
707  * @param co option to change
708  * @param ... option-specific values
709  * @return handle to the operation, NULL if configuring the link at this
710  *         time is not allowed
711  */
712 struct GNUNET_TESTBED_Operation *
713 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
714                                         struct GNUNET_TESTBED_Peer *p1,
715                                         struct GNUNET_TESTBED_Peer *p2,
716                                         enum GNUNET_TESTBED_ConnectOption co, ...);
717
718
719
720 /**
721  * Topologies supported for testbeds.
722  */
723 enum GNUNET_TESTBED_TopologyOption
724 {
725   /**
726    * A clique (everyone connected to everyone else).  No options.
727    */
728   GNUNET_TESTBED_TOPOLOGY_CLIQUE,
729
730   /**
731    * Small-world network (2d torus plus random links).  Followed
732    * by the number of random links to add (unsigned int).
733    */
734   GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD,
735
736   /**
737    * Small-world network (ring plus random links).  Followed
738    * by the number of random links to add (unsigned int).
739    */
740   GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING,
741
742   /**
743    * Ring topology.  No options.
744    */
745   GNUNET_TESTBED_TOPOLOGY_RING,
746
747   /**
748    * 2-d torus.  No options.
749    */
750   GNUNET_TESTBED_TOPOLOGY_2D_TORUS,
751
752   /**
753    * Random graph.  Followed by the link density, that is the
754    * percentage of links present in relation to a clique
755    * (float).
756    */
757   GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
758
759   /**
760    * Certain percentage of peers are unable to communicate directly
761    * replicating NAT conditions.  Followed by the fraction of
762    * NAT'ed peers (float).
763    */
764   GNUNET_TESTBED_TOPOLOGY_INTERNAT,
765
766   /**
767    * Scale free topology.   FIXME: options?
768    */
769   GNUNET_TESTBED_TOPOLOGY_SCALE_FREE,
770
771   /**
772    * Straight line topology.  No options.
773    */
774   GNUNET_TESTBED_TOPOLOGY_LINE,
775
776   /**
777    * All peers are disconnected.  No options.
778    */
779   GNUNET_TESTBED_TOPOLOGY_NONE,
780
781   /**
782    * Read a topology from a given file.  Followed by the name of the file (const char *).
783    */
784   GNUNET_TESTBED_TOPOLOGY_FROM_FILE
785 };
786
787
788 /**
789  * Configure overall network topology to have a particular shape.
790  *
791  * @param op_cls closure argument to give with the operation event
792  * @param num_peers number of peers in 'peers'
793  * @param peers array of 'num_peers' with the peers to configure
794  * @param topo desired underlay topology to use
795  * @param ap topology-specific options
796  * @return handle to the operation, NULL if configuring the topology
797  *         is not allowed at this time
798  */
799 struct GNUNET_TESTBED_Operation *
800 GNUNET_TESTBED_underlay_configure_topology_va (void *op_cls,
801                                                unsigned int num_peers,
802                                                struct GNUNET_TESTBED_Peer **peers,
803                                                enum GNUNET_TESTBED_TopologyOption topo,
804                                                va_list ap);
805
806
807 /**
808  * Configure overall network topology to have a particular shape.
809  *
810  * @param op_cls closure argument to give with the operation event
811  * @param num_peers number of peers in 'peers'
812  * @param peers array of 'num_peers' with the peers to configure
813  * @param topo desired underlay topology to use
814  * @param ... topology-specific options
815  * @return handle to the operation, NULL if configuring the topology
816  *         is not allowed at this time
817  */
818 struct GNUNET_TESTBED_Operation *
819 GNUNET_TESTBED_underlay_configure_topology (void *op_cls,
820                                             unsigned int num_peers,
821                                             struct GNUNET_TESTBED_Peer **peers,
822                                             enum GNUNET_TESTBED_TopologyOption topo,
823                                             ...);
824
825
826 /**
827  * Both peers must have been started before calling this function.
828  * This function then obtains a HELLO from 'p1', gives it to 'p2'
829  * and asks 'p2' to connect to 'p1'.
830  *
831  * @param op_cls closure argument to give with the operation event
832  * @param p1 first peer
833  * @param p2 second peer
834  * @return handle to the operation, NULL if connecting these two
835  *         peers is fundamentally not possible at this time (peers
836  *         not running or underlay disallows)
837  */
838 struct GNUNET_TESTBED_Operation *
839 GNUNET_TESTBED_overlay_connect (void *op_cls,
840                                 struct GNUNET_TESTBED_Peer *p1,
841                                 struct GNUNET_TESTBED_Peer *p2);
842
843
844 /**
845  * All peers must have been started before calling this function.
846  * This function then connects the given peers in the P2P overlay
847  * using the given topology.
848  *
849  * @param op_cls closure argument to give with the operation event
850  * @param num_peers number of peers in 'peers'
851  * @param peers array of 'num_peers' with the peers to configure
852  * @param topo desired underlay topology to use
853  * @param va topology-specific options
854  * @return handle to the operation, NULL if connecting these 
855  *         peers is fundamentally not possible at this time (peers
856  *         not running or underlay disallows)
857  */
858 struct GNUNET_TESTBED_Operation *
859 GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls,
860                                               unsigned int num_peers,
861                                               struct GNUNET_TESTBED_Peer *peers,
862                                               enum GNUNET_TESTBED_TopologyOption topo,
863                                               va_list va);
864
865
866 /**
867  * All peers must have been started before calling this function.
868  * This function then connects the given peers in the P2P overlay
869  * using the given topology.
870  *
871  * @param op_cls closure argument to give with the operation event
872  * @param num_peers number of peers in 'peers'
873  * @param peers array of 'num_peers' with the peers to configure
874  * @param topo desired underlay topology to use
875  * @param ... topology-specific options
876  * @return handle to the operation, NULL if connecting these 
877  *         peers is fundamentally not possible at this time (peers
878  *         not running or underlay disallows)
879  */
880 struct GNUNET_TESTBED_Operation *
881 GNUNET_TESTBED_overlay_configure_topology (void *op_cls,
882                                            unsigned int num_peers,
883                                            struct GNUNET_TESTBED_Peer *peers,
884                                            enum GNUNET_TESTBED_TopologyOption topo,
885                                            ...);
886
887
888 /**
889  * Ask the testbed controller to write the current overlay topology to
890  * a file.  Naturally, the file will only contain a snapshot as the
891  * topology may evolve all the time.
892  *
893  * @param controller overlay controller to inspect
894  * @param filename name of the file the topology should
895  *        be written to.
896  */
897 void
898 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
899                                                const char *filename);
900
901
902 /**
903  * Adapter function called to establish a connection to
904  * a service.
905  * 
906  * @param cls closure
907  * @param cfg configuration of the peer to connect to
908  * @return service handle to return in 'op_result', NULL on error
909  */
910 typedef void * (*GNUNET_TESTBED_ConnectAdapter)(void *cls,
911                                                 const struct GNUNET_CONFIGURATION_Handle *cfg);
912
913
914 /**
915  * Adapter function called to destroy a connection to
916  * a service.
917  * 
918  * @param cls closure
919  * @param op_result service handle returned from the connect adapter
920  */
921 typedef void (*GNUNET_TESTBED_DisconnectAdapter)(void *cls,
922                                                  void *op_result);
923
924
925 /**
926  * Connect to a service offered by the given peer.  Will ensure that
927  * the request is queued to not overwhelm our ability to create and
928  * maintain connections with other systems.  The actual service
929  * handle is then returned via the 'op_result' member in the event
930  * callback.  The 'ca' callback is used to create the connection
931  * when the time is right; the 'da' callback will be used to 
932  * destroy the connection (upon 'GNUNET_TESTBED_operation_done').
933  * 'GNUNET_TESTBED_operation_cancel' can be used to abort this
934  * operation until the event callback has been called.
935  *
936  * @param op_cls closure to pass in operation event
937  * @param peer peer that runs the service
938  * @param service_name name of the service to connect to
939  * @param ca helper function to establish the connection
940  * @param da helper function to close the connection
941  * @param cada_cls closure for ca and da
942  * @return handle for the operation
943  */
944 struct GNUNET_TESTBED_Operation *
945 GNUNET_TESTBED_service_connect (void *op_cls,
946                                 struct GNUNET_TESTBED_Peer *peer,
947                                 const char *service_name,
948                                 GNUNET_TESTBED_ConnectAdapter ca,
949                                 GNUNET_TESTBED_DisconnectAdapter da,
950                                 void *cada_cls);
951
952
953 /**
954  * Cancel a pending operation.  Releases all resources
955  * of the operation and will ensure that no event
956  * is generated for the operation.  Does NOT guarantee
957  * that the operation will be fully undone (or that
958  * nothing ever happened).  
959  * 
960  * @param operation operation to cancel
961  */
962 void
963 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation);
964
965
966 /**
967  * Signal that the information from an operation has been fully
968  * processed.  This function MUST be called for each event
969  * of type 'operation_finished' to fully remove the operation
970  * from the operation queue.  After calling this function, the
971  * 'op_result' becomes invalid (!).
972  * 
973  * @param operation operation to signal completion for
974  */
975 void
976 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation);
977
978
979 /**
980  * Configure and run a testbed using the given
981  * master controller on 'num_hosts' starting
982  * 'num_peers' using the given peer configuration.
983  *
984  * @param controller master controller for the testbed
985  *                   (must not be destroyed until after the
986  *                    testbed is destroyed).
987  * @param num_hosts number of hosts in 'hosts', 0 to only
988  *        use 'localhost'
989  * @param hosts list of hosts to use for the testbed
990  * @param num_peers number of peers to start
991  * @param peer_cfg peer configuration template to use
992  * @param underlay_topology underlay topology to create
993  * @param va topology-specific options
994  * @return handle to the testbed
995  */
996 struct GNUNET_TESTBED_Testbed *
997 GNUNET_TESTBED_create_va (struct GNUNET_TESTBED_Controller *controller,
998                           unsigned int num_hosts,
999                           struct GNUNET_TESTBED_Host **hosts,
1000                           unsigned int num_peers,
1001                           const struct GNUNET_CONFIGURATION_Handle *peer_cfg,
1002                           enum GNUNET_TESTBED_TopologyOption underlay_topology,
1003                           va_list va);
1004
1005
1006 /**
1007  * Configure and run a testbed using the given
1008  * master controller on 'num_hosts' starting
1009  * 'num_peers' using the given peer configuration.
1010  *
1011  * @param controller master controller for the testbed
1012  *                   (must not be destroyed until after the
1013  *                    testbed is destroyed).
1014  * @param num_hosts number of hosts in 'hosts', 0 to only
1015  *        use 'localhost'
1016  * @param hosts list of hosts to use for the testbed
1017  * @param num_peers number of peers to start
1018  * @param peer_cfg peer configuration template to use
1019  * @param underlay_topology underlay topology to create
1020  * @param ... topology-specific options
1021  */
1022 struct GNUNET_TESTBED_Testbed *
1023 GNUNET_TESTBED_create (struct GNUNET_TESTBED_Controller *controller,
1024                        unsigned int num_hosts,
1025                        struct GNUNET_TESTBED_Host **hosts,
1026                        unsigned int num_peers,
1027                        const struct GNUNET_CONFIGURATION_Handle *peer_cfg,
1028                        enum GNUNET_TESTBED_TopologyOption underlay_topology,
1029                        ...);
1030
1031
1032 /**
1033  * Destroy a testbed.  Stops all running peers and then
1034  * destroys all peers.  Does NOT destroy the master controller.
1035  *
1036  * @param testbed testbed to destroy
1037  */
1038 void
1039 GNUNET_TESTBED_destroy (struct GNUNET_TESTBED_Testbed *testbed);
1040
1041
1042 /**
1043  * Convenience method for running a testbed with
1044  * a single call.  Underlay and overlay topology
1045  * are configured using the "UNDERLAY" and "OVERLAY"
1046  * options in the "[testbed]" section of the configuration\
1047  * (with possible options given in "UNDERLAY_XXX" and/or
1048  * "OVERLAY_XXX").
1049  *
1050  * The testbed is to be terminated using a call to
1051  * "GNUNET_SCHEDULER_shutdown".
1052  *
1053  * @param host_filename name of the file with the 'hosts', NULL
1054  *        to run everything on 'localhost'
1055  * @param cfg configuration to use (for testbed, controller and peers)
1056  * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1057  * @param event_mask bit mask with set of events to call 'cc' for;
1058  *                   or-ed values of "1LL" shifted by the
1059  *                   respective 'enum GNUNET_TESTBED_EventType'
1060  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1061  * @param cc controller callback to invoke on events
1062  * @param cc_cls closure for cc
1063  * @param master task to run once the testbed is ready
1064  * @param master_cls closure for 'task'.
1065  */
1066 void
1067 GNUNET_TESTBED_run (const char *host_filename,
1068                     const struct GNUNET_CONFIGURATION_Handle *cfg,
1069                     unsigned int num_peers,
1070                     uint64_t event_mask,
1071                     GNUNET_TESTBED_ControllerCallback cc,
1072                     void *cc_cls,
1073                     GNUNET_SCHEDULER_Task master,
1074                     void *master_cls);
1075
1076
1077 /**
1078  * Signature of a main function for a testcase.
1079  * 
1080  * @param cls closure
1081  * @param num_peers number of peers in 'peers'
1082  * @param peers handle to peers run in the testbed
1083  */
1084 typedef void (*GNUNET_TESTBED_TestMaster)(void *cls,
1085                                           unsigned int num_peers,
1086                                           struct GNUNET_TESTBED_Peer **peers);
1087                                           
1088
1089 /**
1090  * Convenience method for running a "simple" test on the local system
1091  * with a single call from 'main'.  Underlay and overlay topology are
1092  * configured using the "UNDERLAY" and "OVERLAY" options in the
1093  * "[testbed]" section of the configuration (with possible options
1094  * given in "UNDERLAY_XXX" and/or "OVERLAY_XXX").
1095  *
1096  * The test is to be terminated using a call to
1097  * "GNUNET_SCHEDULER_shutdown".  If starting the test fails,
1098  * the program is stopped without 'master' ever being run.
1099  *
1100  * NOTE: this function should be called from 'main', NOT from
1101  * within a GNUNET_SCHEDULER-loop.  This function will initialze
1102  * the scheduler loop, the testbed and then pass control to
1103  * 'master'.
1104  *
1105  * @param testname name of the testcase (to configure logging, etc.)
1106  * @param cfg_filename configuration filename to use
1107  *              (for testbed, controller and peers)
1108  * @param num_peers number of peers to start
1109  * @param test_master task to run once the test is ready
1110  * @param test_master_cls closure for 'task'.
1111  */
1112 void
1113 GNUNET_TESTBED_test_run (const char *testname,
1114                          const char *cfg_filename,
1115                          unsigned int num_peers,
1116                          GNUNET_TESTBED_TestMaster test_master,
1117                          void *test_master_cls);
1118
1119
1120 #if 0                           /* keep Emacsens' auto-indent happy */
1121 {
1122 #endif
1123
1124
1125 #ifdef __cplusplus
1126 }
1127 #endif
1128
1129 #endif