-fixes
[oweals/gnunet.git] / src / include / gnunet_testing_lib.h
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file include/gnunet_testing_lib.h
23  * @brief convenience API for writing testcases for GNUnet
24  *        Many testcases need to start and stop gnunetd,
25  *        and this library is supposed to make that easier
26  *        for TESTCASES.  Normal programs should always
27  *        use functions from gnunet_{util,arm}_lib.h.  This API is
28  *        ONLY for writing testcases!
29  * @author Christian Grothoff
30  */
31
32 #ifndef GNUNET_TESTING_LIB_H
33 #define GNUNET_TESTING_LIB_H
34
35 #include "gnunet_util_lib.h"
36 #include "gnunet_statistics_service.h"
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 #define HOSTKEYFILESIZE 914
47
48 /**
49  * Handle for a GNUnet daemon (technically a set of
50  * daemons; the handle is really for the master ARM
51  * daemon) started by the testing library.
52  */
53 struct GNUNET_TESTING_Daemon;
54
55 /**
56  * Linked list of hostnames and ports to use for starting daemons.
57  */
58 struct GNUNET_TESTING_Host
59 {
60   /**
61    * Pointer to next item in the list.
62    */
63   struct GNUNET_TESTING_Host *next;
64
65   /**
66    * Hostname to connect to.
67    */
68   char *hostname;
69
70   /**
71    * Username to use when connecting (may be null).
72    */
73   char *username;
74
75   /**
76    * Port to use for SSH connection (used for ssh
77    * connection forwarding, 0 to let ssh decide)
78    */
79   uint16_t port;
80 };
81
82 /**
83  * Prototype of a function that will be called whenever
84  * a daemon was started by the testing library.
85  *
86  * @param cls closure
87  * @param id identifier for the daemon, NULL on error
88  * @param d handle for the daemon
89  * @param emsg error message (NULL on success)
90  */
91 typedef void (*GNUNET_TESTING_NotifyHostkeyCreated) (void *cls,
92                                                      const struct
93                                                      GNUNET_PeerIdentity * id,
94                                                      struct
95                                                      GNUNET_TESTING_Daemon * d,
96                                                      const char *emsg);
97
98 /**
99  * Prototype of a function that will be called whenever
100  * a daemon was started by the testing library.
101  *
102  * @param cls closure
103  * @param id identifier for the daemon, NULL on error
104  * @param cfg configuration used by this daemon
105  * @param d handle for the daemon
106  * @param emsg error message (NULL on success)
107  */
108 typedef void (*GNUNET_TESTING_NotifyDaemonRunning) (void *cls,
109                                                     const struct
110                                                     GNUNET_PeerIdentity * id,
111                                                     const struct
112                                                     GNUNET_CONFIGURATION_Handle
113                                                     * cfg,
114                                                     struct GNUNET_TESTING_Daemon
115                                                     * d, const char *emsg);
116
117 /**
118  * Handle to an entire testbed of GNUnet peers.
119  */
120 struct GNUNET_TESTING_Testbed;
121
122 /**
123  * Phases of starting GNUnet on a system.
124  */
125 enum GNUNET_TESTING_StartPhase
126 {
127   /**
128    * Copy the configuration file to the target system.
129    */
130   SP_COPYING,
131
132   /**
133    * Configuration file has been copied, generate hostkey.
134    */
135   SP_COPIED,
136
137   /**
138    * Create the hostkey for the peer.
139    */
140   SP_HOSTKEY_CREATE,
141
142   /**
143    * Hostkey generated, wait for topology to be finished.
144    */
145   SP_HOSTKEY_CREATED,
146
147   /**
148    * Topology has been created, now start ARM.
149    */
150   SP_TOPOLOGY_SETUP,
151
152   /**
153    * ARM has been started, check that it has properly daemonized and
154    * then try to connect to the CORE service (which should be
155    * auto-started by ARM).
156    */
157   SP_START_ARMING,
158
159   /**
160    * We're waiting for CORE to start.
161    */
162   SP_START_CORE,
163
164   /**
165    * CORE is up, now make sure we get the HELLO for this peer.
166    */
167   SP_GET_HELLO,
168
169   /**
170    * Core has notified us that we've established a connection to the service.
171    * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
172    */
173   SP_START_DONE,
174
175   /**
176    * We've been asked to terminate the instance and are now waiting for
177    * the remote command to stop the gnunet-arm process and delete temporary
178    * files.
179    */
180   SP_SHUTDOWN_START,
181
182   /**
183    * We should shutdown a *single* service via gnunet-arm.  Call the dead_cb
184    * upon notification from gnunet-arm that the service has been stopped.
185    */
186   SP_SERVICE_SHUTDOWN_START,
187
188   /**
189    * We should start a *single* service via gnunet-arm.  Call the daemon cb
190    * upon notification from gnunet-arm that the service has been started.
191    */
192   SP_SERVICE_START,
193
194   /**
195    * We've received a configuration update and are currently waiting for
196    * the copy process for the update to complete.  Once it is, we will
197    * return to "SP_START_DONE" (and rely on ARM to restart all affected
198    * services).
199    */
200   SP_CONFIG_UPDATE
201 };
202
203 /**
204  * Prototype of a function that will be called when a
205  * particular operation was completed the testing library.
206  *
207  * @param cls closure
208  * @param emsg NULL on success
209  */
210 typedef void (*GNUNET_TESTING_NotifyCompletion) (void *cls, const char *emsg);
211
212 /**
213  * Prototype of a function that will be called with the
214  * number of connections created for a particular topology.
215  *
216  * @param cls closure
217  * @param num_connections the number of connections created
218  */
219 typedef void (*GNUNET_TESTING_NotifyConnections) (void *cls,
220                                                   unsigned int num_connections);
221
222 /**
223  * Handle for a GNUnet daemon (technically a set of
224  * daemons; the handle is really for the master ARM
225  * daemon) started by the testing library.
226  */
227 struct GNUNET_TESTING_Daemon
228 {
229   /**
230    * Our configuration.
231    */
232   struct GNUNET_CONFIGURATION_Handle *cfg;
233
234   /**
235    * At what time to give up starting the peer
236    */
237   struct GNUNET_TIME_Absolute max_timeout;
238
239   /**
240    * Host to run GNUnet on.
241    */
242   char *hostname;
243
244   /**
245    * Port to use for ssh, NULL to let system choose default.
246    */
247   char *ssh_port_str;
248
249   /**
250    * Result of GNUNET_i2s of this peer,
251    * for printing
252    */
253   char *shortname;
254
255   /**
256    * Username we are using.
257    */
258   char *username;
259
260   /**
261    * Name of the configuration file
262    */
263   char *cfgfile;
264
265   /**
266    * Callback to inform initiator that the peer's
267    * hostkey has been created.
268    */
269   GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
270
271   /**
272    * Closure for hostkey creation callback.
273    */
274   void *hostkey_cls;
275
276   /**
277    * Function to call when the peer is running.
278    */
279   GNUNET_TESTING_NotifyDaemonRunning cb;
280
281   /**
282    * Closure for cb.
283    */
284   void *cb_cls;
285
286   /**
287    * Arguments from "daemon_stop" call.
288    */
289   GNUNET_TESTING_NotifyCompletion dead_cb;
290
291   /**
292    * Closure for 'dead_cb'.
293    */
294   void *dead_cb_cls;
295
296   /**
297    * Arguments from "daemon_stop" call.
298    */
299   GNUNET_TESTING_NotifyCompletion update_cb;
300
301   /**
302    * Closure for 'update_cb'.
303    */
304   void *update_cb_cls;
305
306   /**
307    * PID of the process we used to run gnunet-arm or SSH to start the peer.
308    */
309   struct GNUNET_OS_Process *proc_arm_start;
310
311   /**
312    * PID of the process we used to run gnunet-arm or SSH to stop the peer.
313    */
314   struct GNUNET_OS_Process *proc_arm_stop;
315
316   /**
317    * PID of the process we used to run gnunet-arm or SSH to manage services at the peer.
318    */
319   struct GNUNET_OS_Process *proc_arm_srv_start;
320
321   /**
322    * PID of the process we used to run gnunet-arm or SSH to manage services at the peer.
323    */
324   struct GNUNET_OS_Process *proc_arm_srv_stop;
325
326   /**
327    * PID of the process we used to run copy files
328    */
329   struct GNUNET_OS_Process *proc_arm_copying;
330
331   /**
332    * PID of the process we used to run gnunet-peerinfo.
333    */
334   struct GNUNET_OS_Process *proc_arm_peerinfo;
335
336   /**
337    * Handle to the server.
338    */
339   struct GNUNET_CORE_Handle *server;
340
341   /**
342    * Handle to the transport service of this peer
343    */
344   struct GNUNET_TRANSPORT_Handle *th;
345
346   /**
347    * Handle for getting HELLOs from transport
348    */
349   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
350
351   /**
352    * HELLO message for this peer
353    */
354   struct GNUNET_HELLO_Message *hello;
355
356   /**
357    * Handle to a pipe for reading the hostkey.
358    */
359   struct GNUNET_DISK_PipeHandle *pipe_stdout;
360
361   /**
362    * Currently, a single char * pointing to a service
363    * that has been churned off.
364    *
365    * FIXME: make this a linked list of services that have been churned off!!!
366    */
367   char *churned_services;
368
369   /**
370    * ID of the current task.
371    */
372   GNUNET_SCHEDULER_TaskIdentifier task;
373
374   /**
375    * Identity of this peer (once started).
376    */
377   struct GNUNET_PeerIdentity id;
378
379   /**
380    * Flag to indicate that we've already been asked
381    * to terminate (but could not because some action
382    * was still pending).
383    */
384   int dead;
385
386   /**
387    * GNUNET_YES if the hostkey has been created
388    * for this peer, GNUNET_NO otherwise.
389    */
390   int have_hostkey;
391
392   /**
393    * In which phase are we during the start of
394    * this process?
395    */
396   enum GNUNET_TESTING_StartPhase phase;
397
398   /**
399    * Current position in 'hostkeybuf' (for reading from gnunet-peerinfo)
400    */
401   unsigned int hostkeybufpos;
402
403   /**
404    * Set to GNUNET_YES once the peer is up.
405    */
406   int running;
407
408   /**
409    * Used to tell shutdown not to remove configuration for the peer
410    * (if it's going to be restarted later)
411    */
412   int churn;
413
414   /**
415    * Output from gnunet-peerinfo is read into this buffer.
416    */
417   char hostkeybuf[105];
418
419 };
420
421
422 /**
423  * Handle to a group of GNUnet peers.
424  */
425 struct GNUNET_TESTING_PeerGroup;
426
427
428 /**
429  * Prototype of a function that will be called whenever
430  * two daemons are connected by the testing library.
431  *
432  * @param cls closure
433  * @param first peer id for first daemon
434  * @param second peer id for the second daemon
435  * @param distance distance between the connected peers
436  * @param first_cfg config for the first daemon
437  * @param second_cfg config for the second daemon
438  * @param first_daemon handle for the first daemon
439  * @param second_daemon handle for the second daemon
440  * @param emsg error message (NULL on success)
441  */
442 typedef void (*GNUNET_TESTING_NotifyConnection) (void *cls,
443                                                  const struct
444                                                  GNUNET_PeerIdentity * first,
445                                                  const struct
446                                                  GNUNET_PeerIdentity * second,
447                                                  uint32_t distance,
448                                                  const struct
449                                                  GNUNET_CONFIGURATION_Handle *
450                                                  first_cfg,
451                                                  const struct
452                                                  GNUNET_CONFIGURATION_Handle *
453                                                  second_cfg,
454                                                  struct GNUNET_TESTING_Daemon *
455                                                  first_daemon,
456                                                  struct GNUNET_TESTING_Daemon *
457                                                  second_daemon,
458                                                  const char *emsg);
459
460
461 /**
462  * Prototype of a callback function indicating that two peers
463  * are currently connected.
464  *
465  * @param cls closure
466  * @param first peer id for first daemon
467  * @param second peer id for the second daemon
468  * @param distance distance between the connected peers
469  * @param emsg error message (NULL on success)
470  */
471 typedef void (*GNUNET_TESTING_NotifyTopology) (void *cls,
472                                                const struct GNUNET_PeerIdentity
473                                                * first,
474                                                const struct GNUNET_PeerIdentity
475                                                * second, const char *emsg);
476
477
478 /**
479  * Starts a GNUnet daemon.  GNUnet must be installed on the target
480  * system and available in the PATH.  The machine must furthermore be
481  * reachable via "ssh" (unless the hostname is "NULL") without the
482  * need to enter a password.
483  *
484  * @param cfg configuration to use
485  * @param timeout how long to wait starting up peers
486  * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
487  *                to really start the peer (default)
488  * @param hostname name of the machine where to run GNUnet
489  *        (use NULL for localhost).
490  * @param ssh_username ssh username to use when connecting to hostname
491  * @param sshport port to pass to ssh process when connecting to hostname
492  * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
493  * @param hostkey_callback function to call once the hostkey has been
494  *        generated for this peer, but it hasn't yet been started
495  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
496  * @param hostkey_cls closure for hostkey callback
497  * @param cb function to call once peer is up, or failed to start
498  * @param cb_cls closure for cb
499  * @return handle to the daemon (actual start will be completed asynchronously)
500  */
501 struct GNUNET_TESTING_Daemon *
502 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
503                              struct GNUNET_TIME_Relative timeout, int pretend,
504                              const char *hostname, const char *ssh_username,
505                              uint16_t sshport, const char *hostkey,
506                              GNUNET_TESTING_NotifyHostkeyCreated
507                              hostkey_callback, void *hostkey_cls,
508                              GNUNET_TESTING_NotifyDaemonRunning cb,
509                              void *cb_cls);
510
511 /**
512  * Continues GNUnet daemon startup when user wanted to be notified
513  * once a hostkey was generated (for creating friends files, blacklists,
514  * etc.).
515  *
516  * @param daemon the daemon to finish starting
517  */
518 void
519 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon);
520
521
522 /**
523  * Check whether the given daemon is running.
524  *
525  * @param daemon the daemon to check
526  * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
527  *         daemon is down, GNUNET_SYSERR on error.
528  */
529 int
530 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon);
531
532
533 /**
534  * Obtain the peer identity of the peer with the given configuration
535  * handle.  This function reads the private key of the peer, obtains
536  * the public key and hashes it.
537  *
538  * @param cfg configuration of the peer
539  * @param pid where to store the peer identity
540  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
541  */
542 int
543 GNUNET_TESTING_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
544                                   struct GNUNET_PeerIdentity *pid);
545
546
547 /**
548  * Restart (stop and start) a GNUnet daemon.
549  *
550  * @param d the daemon that should be restarted
551  * @param cb function called once the daemon is (re)started
552  * @param cb_cls closure for cb
553  */
554 void
555 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
556                                GNUNET_TESTING_NotifyDaemonRunning cb,
557                                void *cb_cls);
558
559
560 /**
561  * Start a peer that has previously been stopped using the daemon_stop
562  * call (and files weren't deleted and the allow restart flag)
563  *
564  * @param daemon the daemon to start (has been previously stopped)
565  * @param timeout how long to wait for restart
566  * @param cb the callback for notification when the peer is running
567  * @param cb_cls closure for the callback
568  */
569 void
570 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
571                                      struct GNUNET_TIME_Relative timeout,
572                                      GNUNET_TESTING_NotifyDaemonRunning cb,
573                                      void *cb_cls);
574
575
576 /**
577  * Starts a GNUnet daemon's service.
578  *
579  * @param d the daemon for which the service should be started
580  * @param service the name of the service to start
581  * @param timeout how long to wait for process for startup
582  * @param cb function called once gnunet-arm returns
583  * @param cb_cls closure for cb
584  */
585 void
586 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
587                                      const char *service,
588                                      struct GNUNET_TIME_Relative timeout,
589                                      GNUNET_TESTING_NotifyDaemonRunning cb,
590                                      void *cb_cls);
591
592
593 /**
594  * Starts a GNUnet daemon's service which has been previously turned off.
595  *
596  * @param d the daemon for which the service should be started
597  * @param service the name of the service to start
598  * @param timeout how long to wait for process for startup
599  * @param cb function called once gnunet-arm returns
600  * @param cb_cls closure for cb
601  */
602 void
603 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
604                                              char *service,
605                                              struct GNUNET_TIME_Relative
606                                              timeout,
607                                              GNUNET_TESTING_NotifyDaemonRunning
608                                              cb, void *cb_cls);
609
610
611 /**
612  * Get a certain testing daemon handle.
613  *
614  * @param pg handle to the set of running peers
615  * @param position the number of the peer to return
616  */
617 struct GNUNET_TESTING_Daemon *
618 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
619                            unsigned int position);
620
621
622 /**
623  * Get a daemon by peer identity, so callers can
624  * retrieve the daemon without knowing it's offset.
625  *
626  * @param pg the peer group to retrieve the daemon from
627  * @param peer_id the peer identity of the daemon to retrieve
628  *
629  * @return the daemon on success, or NULL if no such peer identity is found
630  */
631 struct GNUNET_TESTING_Daemon *
632 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
633                                  const struct GNUNET_PeerIdentity *peer_id);
634
635
636 /**
637  * Stops a GNUnet daemon.
638  *
639  * @param d the daemon that should be stopped
640  * @param timeout how long to wait for process for shutdown to complete
641  * @param cb function called once the daemon was stopped
642  * @param cb_cls closure for cb
643  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
644  *        to leave them (i.e. for restarting at a later time,
645  *        or logfile inspection once finished)
646  * @param allow_restart GNUNET_YES to restart peer later (using this API)
647  *        GNUNET_NO to kill off and clean up for good
648  */
649 void
650 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
651                             struct GNUNET_TIME_Relative timeout,
652                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
653                             int delete_files, int allow_restart);
654
655
656
657 /**
658  * Create a new configuration using the given configuration
659  * as a template; however, each PORT in the existing cfg
660  * must be renumbered by incrementing "*port".  If we run
661  * out of "*port" numbers, return NULL.
662  *
663  * @param cfg template configuration
664  * @param off the current peer offset
665  * @param port port numbers to use, update to reflect
666  *             port numbers that were used
667  * @param upnum number to make unix domain socket names unique
668  * @param hostname hostname of the controlling host, to allow control connections from
669  * @param fdnum number used to offset the unix domain socket for grouped processes
670  *              (such as statistics or peerinfo, which can be shared among others)
671  *
672  * @return new configuration, NULL on error
673  */
674 struct GNUNET_CONFIGURATION_Handle *
675 GNUNET_TESTING_create_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
676              uint16_t * port, uint32_t * upnum, const char *hostname,
677              uint32_t * fdnum);
678
679 /**
680  * Changes the configuration of a GNUnet daemon.
681  *
682  * @param d the daemon that should be modified
683  * @param cfg the new configuration for the daemon
684  * @param cb function called once the configuration was changed
685  * @param cb_cls closure for cb
686  */
687 void
688 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
689                                    struct GNUNET_CONFIGURATION_Handle *cfg,
690                                    GNUNET_TESTING_NotifyCompletion cb,
691                                    void *cb_cls);
692
693
694 /**
695  * Stops a single service of a GNUnet daemon.  Used like daemon_stop,
696  * only doesn't stop the entire peer in any case.  If the service
697  * is not currently running, this call is likely to fail after
698  * timeout!
699  *
700  * @param d the daemon that should be stopped
701  * @param service the name of the service to stop
702  * @param timeout how long to wait for process for shutdown to complete
703  * @param cb function called once the service was stopped
704  * @param cb_cls closure for cb
705  */
706 void
707 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
708                                     const char *service,
709                                     struct GNUNET_TIME_Relative timeout,
710                                     GNUNET_TESTING_NotifyCompletion cb,
711                                     void *cb_cls);
712
713
714 /**
715  * Read a testing hosts file based on a configuration.
716  * Returns a DLL of hosts (caller must free!) on success
717  * or NULL on failure.
718  *
719  * @param cfg a configuration with a testing section
720  *
721  * @return DLL of hosts on success, NULL on failure
722  */
723 struct GNUNET_TESTING_Host *
724 GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg);
725
726
727 /**
728  * Start count gnunet instances with the same set of transports and
729  * applications.  The port numbers (any option called "PORT") will be
730  * adjusted to ensure that no two peers running on the same system
731  * have the same port(s) in their respective configurations.
732  *
733  * @param cfg configuration template to use
734  * @param total number of daemons to start
735  * @param max_concurrent_connections for testing, how many peers can
736 *                                   we connect to simultaneously
737  * @param max_concurrent_ssh when starting with ssh, how many ssh
738  *        connections will we allow at once (based on remote hosts allowed!)
739  * @param timeout total time allowed for peers to start
740  * @param hostkey_callback function to call on each peers hostkey generation
741  *        if NULL, peers will be started by this call, if non-null,
742  *        GNUNET_TESTING_daemons_continue_startup must be called after
743  *        successful hostkey generation
744  * @param hostkey_cls closure for hostkey callback
745  * @param cb function to call on each daemon that was started
746  * @param cb_cls closure for cb
747  * @param connect_callback function to call each time two hosts are connected
748  * @param connect_callback_cls closure for connect_callback
749  * @param hostnames linked list of host structs to use to start peers on
750  *                  (NULL to run on localhost only)
751  *
752  * @return NULL on error, otherwise handle to control peer group
753  */
754 struct GNUNET_TESTING_PeerGroup *
755 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
756                               unsigned int total,
757                               unsigned int max_concurrent_connections,
758                               unsigned int max_concurrent_ssh,
759                               struct GNUNET_TIME_Relative timeout,
760                               GNUNET_TESTING_NotifyHostkeyCreated
761                               hostkey_callback, void *hostkey_cls,
762                               GNUNET_TESTING_NotifyDaemonRunning cb,
763                               void *cb_cls,
764                               GNUNET_TESTING_NotifyConnection connect_callback,
765                               void *connect_callback_cls,
766                               const struct GNUNET_TESTING_Host *hostnames);
767
768
769 /**
770  * Function which continues a peer group starting up
771  * after successfully generating hostkeys for each peer.
772  *
773  * @param pg the peer group to continue starting
774  */
775 void
776 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg);
777
778
779 /**
780  * Handle for an active request to connect two peers.
781  */
782 struct GNUNET_TESTING_ConnectContext;
783
784
785 /**
786  * Establish a connection between two GNUnet daemons.  The daemons
787  * must both be running and not be stopped until either the
788  * 'cb' callback is called OR the connection request has been
789  * explicitly cancelled.
790  *
791  * @param d1 handle for the first daemon
792  * @param d2 handle for the second daemon
793  * @param timeout how long is the connection attempt
794  *        allowed to take?
795  * @param max_connect_attempts how many times should we try to reconnect
796  *        (within timeout)
797  * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
798  *                   the HELLO has already been exchanged
799  * @param cb function to call at the end
800  * @param cb_cls closure for cb
801  * @return handle to cancel the request, NULL on error
802  */
803 struct GNUNET_TESTING_ConnectContext *
804 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
805                                 struct GNUNET_TESTING_Daemon *d2,
806                                 struct GNUNET_TIME_Relative timeout,
807                                 unsigned int max_connect_attempts,
808                                 int send_hello,
809                                 GNUNET_TESTING_NotifyConnection cb,
810                                 void *cb_cls);
811
812
813
814 /**
815  * Cancel an attempt to connect two daemons.
816  *
817  * @param cc connect context
818  */
819 void
820 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext
821                                        *cc);
822
823
824
825 /**
826  * Restart all peers in the given group.
827  *
828  * @param pg the handle to the peer group
829  * @param callback function to call on completion (or failure)
830  * @param callback_cls closure for the callback function
831  */
832 void
833 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
834                                 GNUNET_TESTING_NotifyCompletion callback,
835                                 void *callback_cls);
836
837
838 /**
839  * Shutdown all peers started in the given group.
840  *
841  * @param pg handle to the peer group
842  * @param timeout how long to wait for shutdown
843  * @param cb callback to notify upon success or failure
844  * @param cb_cls closure for cb
845  */
846 void
847 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
848                              struct GNUNET_TIME_Relative timeout,
849                              GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
850
851
852 /**
853  * Count the number of running peers.
854  *
855  * @param pg handle for the peer group
856  *
857  * @return the number of currently running peers in the peer group
858  */
859 unsigned int
860 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg);
861
862
863 /**
864  * Simulate churn by stopping some peers (and possibly
865  * re-starting others if churn is called multiple times).  This
866  * function can only be used to create leave-join churn (peers "never"
867  * leave for good).  First "voff" random peers that are currently
868  * online will be taken offline; then "von" random peers that are then
869  * offline will be put back online.  No notifications will be
870  * generated for any of these operations except for the callback upon
871  * completion.  Note that the implementation is at liberty to keep
872  * the ARM service itself (but none of the other services or daemons)
873  * running even though the "peer" is being varied offline.
874  *
875  * @param pg handle for the peer group
876  * @param service the service to churn on/off, NULL for all
877  * @param voff number of peers that should go offline
878  * @param von number of peers that should come back online;
879  *            must be zero on first call (since "testbed_start"
880  *            always starts all of the peers)
881  * @param timeout how long to wait for operations to finish before
882  *        giving up
883  * @param cb function to call at the end
884  * @param cb_cls closure for cb
885  */
886 void
887 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
888                               char *service, unsigned int voff,
889                               unsigned int von,
890                               struct GNUNET_TIME_Relative timeout,
891                               GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
892
893
894 /**
895  * Start a given service for each of the peers in the peer group.
896  *
897  * @param pg handle for the peer group
898  * @param service the service to start
899  * @param timeout how long to wait for operations to finish before
900  *        giving up
901  * @param cb function to call once finished
902  * @param cb_cls closure for cb
903  *
904  */
905 void
906 GNUNET_TESTING_daemons_start_service (struct GNUNET_TESTING_PeerGroup *pg,
907                                       char *service,
908                                       struct GNUNET_TIME_Relative timeout,
909                                       GNUNET_TESTING_NotifyCompletion cb,
910                                       void *cb_cls);
911
912
913 /**
914  * Callback function to process statistic values.
915  *
916  * @param cls closure
917  * @param peer the peer the statistics belong to
918  * @param subsystem name of subsystem that created the statistic
919  * @param name the name of the datum
920  * @param value the current value
921  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
922  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
923  */
924 typedef int (*GNUNET_TESTING_STATISTICS_Iterator) (void *cls,
925                                                    const struct
926                                                    GNUNET_PeerIdentity * peer,
927                                                    const char *subsystem,
928                                                    const char *name,
929                                                    uint64_t value,
930                                                    int is_persistent);
931
932
933 /**
934  * Iterate over all (running) peers in the peer group, retrieve
935  * all statistics from each.
936  *
937  * @param pg the peergroup to iterate statistics of
938  * @param cont continuation to call once call is completed(?)
939  * @param proc processing function for each statistic retrieved
940  * @param cls closure to pass to proc
941  */
942 void
943 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
944                                GNUNET_STATISTICS_Callback cont,
945                                GNUNET_TESTING_STATISTICS_Iterator proc,
946                                void *cls);
947
948
949 /**
950  * Topologies supported for testbeds.
951  */
952 enum GNUNET_TESTING_Topology
953 {
954   /**
955    * A clique (everyone connected to everyone else).
956    */
957   GNUNET_TESTING_TOPOLOGY_CLIQUE,
958
959   /**
960    * Small-world network (2d torus plus random links).
961    */
962   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
963
964   /**
965    * Small-world network (ring plus random links).
966    */
967   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
968
969   /**
970    * Ring topology.
971    */
972   GNUNET_TESTING_TOPOLOGY_RING,
973
974   /**
975    * 2-d torus.
976    */
977   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
978
979   /**
980    * Random graph.
981    */
982   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
983
984   /**
985    * Certain percentage of peers are unable to communicate directly
986    * replicating NAT conditions
987    */
988   GNUNET_TESTING_TOPOLOGY_INTERNAT,
989
990   /**
991    * Scale free topology.
992    */
993   GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
994
995   /**
996    * Straight line topology.
997    */
998   GNUNET_TESTING_TOPOLOGY_LINE,
999
1000   /**
1001    * All peers are disconnected.
1002    */
1003   GNUNET_TESTING_TOPOLOGY_NONE,
1004
1005   /**
1006    * Read a topology from a given file.
1007    */
1008   GNUNET_TESTING_TOPOLOGY_FROM_FILE
1009 };
1010
1011 /**
1012  * Options for connecting a topology.
1013  */
1014 enum GNUNET_TESTING_TopologyOption
1015 {
1016   /**
1017    * Try to connect all peers specified in the topology.
1018    */
1019   GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
1020
1021   /**
1022    * Choose a random subset of connections to create.
1023    */
1024   GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
1025
1026   /**
1027    * Create at least X connections for each peer.
1028    */
1029   GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
1030
1031   /**
1032    * Using a depth first search, create one connection
1033    * per peer.  If any are missed (graph disconnected)
1034    * start over at those peers until all have at least one
1035    * connection.
1036    */
1037   GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
1038
1039   /**
1040    * Find the N closest peers to each allowed peer in the
1041    * topology and make sure a connection to those peers
1042    * exists in the connect topology.
1043    */
1044   GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST,
1045
1046   /**
1047    * No options specified.
1048    */
1049   GNUNET_TESTING_TOPOLOGY_OPTION_NONE
1050 };
1051
1052
1053 /**
1054  * Get a topology from a string input.
1055  *
1056  * @param topology where to write the retrieved topology
1057  * @param topology_string The string to attempt to
1058  *        get a configuration value from
1059  * @return GNUNET_YES if topology string matched a
1060  *         known topology, GNUNET_NO if not
1061  */
1062 int
1063 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
1064                              const char *topology_string);
1065
1066
1067 /**
1068  * Get connect topology option from string input.
1069  *
1070  * @param topology_option where to write the retrieved topology
1071  * @param topology_string The string to attempt to
1072  *        get a configuration value from
1073  * @return GNUNET_YES if topology string matched a
1074  *         known topology, GNUNET_NO if not
1075  */
1076 int
1077 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
1078                                     *topology_option,
1079                                     const char *topology_string);
1080
1081
1082 /**
1083  * Takes a peer group and creates a topology based on the
1084  * one specified.  Creates a topology means generates friend
1085  * files for the peers so they can only connect to those allowed
1086  * by the topology.  This will only have an effect once peers
1087  * are started if the FRIENDS_ONLY option is set in the base
1088  * config.
1089  *
1090  * Also takes an optional restrict topology which
1091  * disallows direct connections UNLESS they are specified in
1092  * the restricted topology.
1093  *
1094  * A simple example; if the topology option is set to LINE
1095  * peers can ONLY connect in a LINE.  However, if the topology
1096  * option is set to 2D-torus and the restrict option is set to
1097  * line with restrict_transports equal to "tcp udp", then peers
1098  * may connect in a 2D-torus, but will be restricted to tcp and
1099  * udp connections only in a LINE.  Generally it only makes
1100  * sense to do this if restrict_topology is a subset of topology.
1101  *
1102  * For testing peer discovery, etc. it is generally better to
1103  * leave restrict_topology as GNUNET_TESTING_TOPOLOGY_NONE and
1104  * then use the connect_topology function to restrict the initial
1105  * connection set.
1106  *
1107  * @param pg the peer group struct representing the running peers
1108  * @param topology which topology to connect the peers in
1109  * @param restrict_topology allow only direct connections in this topology,
1110  *        based on those listed in restrict_transports, set to
1111  *        GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
1112  * @param restrict_transports space delimited list of transports to blacklist
1113  *                            to create restricted topology, NULL for none
1114  *
1115  * @return the maximum number of connections were all allowed peers
1116  *         connected to each other
1117  */
1118 unsigned int
1119 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
1120                                 enum GNUNET_TESTING_Topology topology,
1121                                 enum GNUNET_TESTING_Topology restrict_topology,
1122                                 const char *restrict_transports);
1123
1124
1125 /**
1126  * Iterate over all (running) peers in the peer group, retrieve
1127  * all connections that each currently has.
1128  *
1129  * @param pg the peer group we are concerned with
1130  * @param cb callback for topology information
1131  * @param cls closure for callback
1132  */
1133 void
1134 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
1135                              GNUNET_TESTING_NotifyTopology cb, void *cls);
1136
1137
1138 /**
1139  * Stop the connection process temporarily.
1140  *
1141  * @param pg the peer group to stop connecting
1142  */
1143 void
1144 GNUNET_TESTING_stop_connections (struct GNUNET_TESTING_PeerGroup *pg);
1145
1146
1147 /**
1148  * Resume the connection process.
1149  *
1150  * @param pg the peer group to resume connecting
1151  */
1152 void
1153 GNUNET_TESTING_resume_connections (struct GNUNET_TESTING_PeerGroup *pg);
1154
1155
1156 /**
1157  * There are many ways to connect peers that are supported by this function.
1158  * To connect peers in the same topology that was created via the
1159  * GNUNET_TESTING_create_topology, the topology variable must be set to
1160  * GNUNET_TESTING_TOPOLOGY_NONE.  If the topology variable is specified,
1161  * a new instance of that topology will be generated and attempted to be
1162  * connected.  This could result in some connections being impossible,
1163  * because some topologies are non-deterministic.
1164  *
1165  * @param pg the peer group struct representing the running peers
1166  * @param topology which topology to connect the peers in
1167  * @param options options for connecting the topology
1168  * @param option_modifier modifier for options that take a parameter
1169  * @param connect_timeout how long to wait before giving up on connecting
1170  *                        two peers
1171  * @param connect_attempts how many times to attempt to connect two peers
1172  *                         over the connect_timeout duration
1173  * @param notify_callback notification to be called once all connections completed
1174  * @param notify_cls closure for notification callback
1175  *
1176  * @return the number of connections that will be attempted, GNUNET_SYSERR on error
1177  */
1178 int
1179 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
1180                                  enum GNUNET_TESTING_Topology topology,
1181                                  enum GNUNET_TESTING_TopologyOption options,
1182                                  double option_modifier,
1183                                  struct GNUNET_TIME_Relative connect_timeout,
1184                                  unsigned int connect_attempts,
1185                                  GNUNET_TESTING_NotifyCompletion
1186                                  notify_callback, void *notify_cls);
1187
1188
1189 /**
1190  * Start or stop an individual peer from the given group.
1191  *
1192  * @param pg handle to the peer group
1193  * @param offset which peer to start or stop
1194  * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
1195  * @param timeout how long to wait for shutdown
1196  * @param cb function to call at the end
1197  * @param cb_cls closure for cb
1198  */
1199 void
1200 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
1201                              unsigned int offset, int desired_status,
1202                              struct GNUNET_TIME_Relative timeout,
1203                              GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
1204
1205
1206 /**
1207  * Start a peer group with a given number of peers.  Notify
1208  * on completion of peer startup and connection based on given
1209  * topological constraints.  Optionally notify on each
1210  * established connection.
1211  *
1212  * @param cfg configuration template to use
1213  * @param total number of daemons to start
1214  * @param timeout total time allowed for peers to start
1215  * @param connect_cb function to call each time two daemons are connected
1216  * @param peergroup_cb function to call once all peers are up and connected
1217  * @param peergroup_cls closure for peergroup callbacks
1218  * @param hostnames linked list of host structs to use to start peers on
1219  *                  (NULL to run on localhost only)
1220  *
1221  * @return NULL on error, otherwise handle to control peer group
1222  */
1223 struct GNUNET_TESTING_PeerGroup *
1224 GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1225                                 unsigned int total,
1226                                 struct GNUNET_TIME_Relative timeout,
1227                                 GNUNET_TESTING_NotifyConnection connect_cb,
1228                                 GNUNET_TESTING_NotifyCompletion peergroup_cb,
1229                                 void *peergroup_cls,
1230                                 const struct GNUNET_TESTING_Host *hostnames);
1231
1232
1233 /**
1234  * Print current topology to a graphviz readable file.
1235  *
1236  * @param pg a currently running peergroup to print to file
1237  * @param output_filename the file to write the topology to
1238  * @param notify_cb callback to call upon completion or failure
1239  * @param notify_cb_cls closure for notify_cb
1240  *
1241  */
1242 void
1243 GNUNET_TESTING_peergroup_topology_to_file (struct GNUNET_TESTING_PeerGroup *pg,
1244                                            const char *output_filename,
1245                                            GNUNET_TESTING_NotifyCompletion
1246                                            notify_cb, void *notify_cb_cls);
1247
1248
1249 #if 0                           /* keep Emacsens' auto-indent happy */
1250 {
1251 #endif
1252 #ifdef __cplusplus
1253 }
1254 #endif
1255
1256 #endif