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