stuff
[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 GNUNET_PeerIdentity *id,
93                                                     struct GNUNET_TESTING_Daemon *d,
94                                                     const char *emsg);
95
96 /**
97  * Prototype of a function that will be called whenever
98  * a daemon was started by the testing library.
99  *
100  * @param cls closure
101  * @param id identifier for the daemon, NULL on error
102  * @param cfg configuration used by this daemon
103  * @param d handle for the daemon
104  * @param emsg error message (NULL on success)
105  */
106 typedef void (*GNUNET_TESTING_NotifyDaemonRunning)(void *cls,
107                                                    const struct GNUNET_PeerIdentity *id,
108                                                    const struct GNUNET_CONFIGURATION_Handle *cfg,
109                                                    struct GNUNET_TESTING_Daemon *d,
110                                                    const char *emsg);
111
112
113 /**
114  * Handle to an entire testbed of GNUnet peers.
115  */
116 struct GNUNET_TESTING_Testbed;
117
118 /**
119  * Phases of starting GNUnet on a system.
120  */
121 enum GNUNET_TESTING_StartPhase
122 {
123   /**
124    * Copy the configuration file to the target system.
125    */
126   SP_COPYING,
127
128   /**
129    * Configuration file has been copied, generate hostkey.
130    */
131   SP_COPIED,
132
133   /**
134    * Create the hostkey for the peer.
135    */
136   SP_HOSTKEY_CREATE,
137
138   /**
139    * Hostkey generated, wait for topology to be finished.
140    */
141   SP_HOSTKEY_CREATED,
142
143   /**
144    * Topology has been created, now start ARM.
145    */
146   SP_TOPOLOGY_SETUP,
147
148   /**
149    * ARM has been started, check that it has properly daemonized and
150    * then try to connect to the CORE service (which should be
151    * auto-started by ARM).
152    */
153   SP_START_ARMING,
154
155   /**
156    * We're waiting for CORE to start.
157    */
158   SP_START_CORE,
159
160   /**
161    * Core has notified us that we've established a connection to the service.
162    * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
163    */
164   SP_START_DONE,
165
166   /**
167    * We've been asked to terminate the instance and are now waiting for
168    * the remote command to stop the gnunet-arm process and delete temporary
169    * files.
170    */
171   SP_SHUTDOWN_START,
172
173   /**
174    * We've received a configuration update and are currently waiting for
175    * the copy process for the update to complete.  Once it is, we will
176    * return to "SP_START_DONE" (and rely on ARM to restart all affected
177    * services).
178    */
179   SP_CONFIG_UPDATE
180 };
181
182 /**
183  * Prototype of a function that will be called when a
184  * particular operation was completed the testing library.
185  *
186  * @param cls closure
187  * @param emsg NULL on success
188  */
189 typedef void (*GNUNET_TESTING_NotifyCompletion)(void *cls,
190                                                 const char *emsg);
191
192 /**
193  * Prototype of a function that will be called with the
194  * number of connections created for a particular topology.
195  *
196  * @param cls closure
197  * @param num_connections the number of connections created
198  */
199 typedef void (*GNUNET_TESTING_NotifyConnections)(void *cls,
200                                                 unsigned int num_connections);
201
202 /**
203  * Handle for a GNUnet daemon (technically a set of
204  * daemons; the handle is really for the master ARM
205  * daemon) started by the testing library.
206  */
207 struct GNUNET_TESTING_Daemon
208 {
209   /**
210    * Our configuration.
211    */
212   struct GNUNET_CONFIGURATION_Handle *cfg;
213
214   /**
215    * At what time to give up starting the peer
216    */
217   struct GNUNET_TIME_Absolute max_timeout;
218
219   /**
220    * Host to run GNUnet on.
221    */
222   char *hostname;
223
224   /**
225    * Port to use for ssh, NULL to let system choose default.
226    */
227   char *ssh_port_str;
228
229   /**
230    * Result of GNUNET_i2s of this peer,
231    * for printing
232    */
233   char *shortname;
234
235   /**
236    * Username we are using.
237    */
238   char *username;
239
240   /**
241    * Name of the configuration file
242    */
243   char *cfgfile;
244
245   /**
246    * Callback to inform initiator that the peer's
247    * hostkey has been created.
248    */
249   GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
250
251   /**
252    * Closure for hostkey creation callback.
253    */
254   void *hostkey_cls;
255
256   /**
257    * Function to call when the peer is running.
258    */
259   GNUNET_TESTING_NotifyDaemonRunning cb;
260
261   /**
262    * Closure for cb.
263    */
264   void *cb_cls;
265
266   /**
267    * Arguments from "daemon_stop" call.
268    */
269   GNUNET_TESTING_NotifyCompletion dead_cb;
270
271   /**
272    * Closure for 'dead_cb'.
273    */
274   void *dead_cb_cls;
275
276   /**
277    * Arguments from "daemon_stop" call.
278    */
279   GNUNET_TESTING_NotifyCompletion update_cb;
280
281   /**
282    * Closure for 'update_cb'.
283    */
284   void *update_cb_cls;
285
286   /**
287    * Identity of this peer (once started).
288    */
289   struct GNUNET_PeerIdentity id;
290
291   /**
292    * Flag to indicate that we've already been asked
293    * to terminate (but could not because some action
294    * was still pending).
295    */
296   int dead;
297
298   /**
299    * PID of the process that we started last.
300    */
301   struct GNUNET_OS_Process *proc;
302
303   /**
304    * In which phase are we during the start of
305    * this process?
306    */
307   enum GNUNET_TESTING_StartPhase phase;
308
309   /**
310    * ID of the current task.
311    */
312   GNUNET_SCHEDULER_TaskIdentifier task;
313
314   /**
315    * Handle to the server.
316    */
317   struct GNUNET_CORE_Handle *server;
318
319   /**
320    * Handle to the transport service of this peer
321    */
322   struct GNUNET_TRANSPORT_Handle *th;
323
324   /**
325    * HELLO message for this peer
326    */
327   struct GNUNET_HELLO_Message *hello;
328
329   /**
330    * Handle to a pipe for reading the hostkey.
331    */
332   struct GNUNET_DISK_PipeHandle *pipe_stdout;
333
334   /**
335    * Output from gnunet-peerinfo is read into this buffer.
336    */
337   char hostkeybuf[105];
338
339   /**
340    * Current position in 'hostkeybuf' (for reading from gnunet-peerinfo)
341    */
342   unsigned int hostkeybufpos;
343
344   /**
345    * Set to GNUNET_YES once the peer is up.
346    */
347   int running;
348
349   /**
350    * Used to tell shutdown not to remove configuration for the peer
351    * (if it's going to be restarted later)
352    */
353   int churn;
354 };
355
356
357 /**
358  * Handle to a group of GNUnet peers.
359  */
360 struct GNUNET_TESTING_PeerGroup;
361
362
363 /**
364  * Prototype of a function that will be called whenever
365  * two daemons are connected by the testing library.
366  *
367  * @param cls closure
368  * @param first peer id for first daemon
369  * @param second peer id for the second daemon
370  * @param distance distance between the connected peers
371  * @param first_cfg config for the first daemon
372  * @param second_cfg config for the second daemon
373  * @param first_daemon handle for the first daemon
374  * @param second_daemon handle for the second daemon
375  * @param emsg error message (NULL on success)
376  */
377 typedef void (*GNUNET_TESTING_NotifyConnection)(void *cls,
378                                                 const struct GNUNET_PeerIdentity *first,
379                                                 const struct GNUNET_PeerIdentity *second,
380                                                 uint32_t distance,
381                                                 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
382                                                 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
383                                                 struct GNUNET_TESTING_Daemon *first_daemon,
384                                                 struct GNUNET_TESTING_Daemon *second_daemon,
385                                                 const char *emsg);
386
387 /**
388  * Prototype of a callback function indicating that two peers
389  * are currently connected.
390  *
391  * @param cls closure
392  * @param first peer id for first daemon
393  * @param second peer id for the second daemon
394  * @param distance distance between the connected peers
395  * @param emsg error message (NULL on success)
396  */
397 typedef void (*GNUNET_TESTING_NotifyTopology)(void *cls,
398                                               const struct GNUNET_PeerIdentity *first,
399                                               const struct GNUNET_PeerIdentity *second,
400                                               const char *emsg);
401
402 /**
403  * Starts a GNUnet daemon.  GNUnet must be installed on the target
404  * system and available in the PATH.  The machine must furthermore be
405  * reachable via "ssh" (unless the hostname is "NULL") without the
406  * need to enter a password.
407  *
408  * @param cfg configuration to use
409  * @param timeout how long to wait starting up peers
410  * @param hostname name of the machine where to run GNUnet
411  *        (use NULL for localhost).
412  * @param ssh_username ssh username to use when connecting to hostname
413  * @param sshport port to pass to ssh process when connecting to hostname
414  * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
415  * @param hostkey_callback function to call once the hostkey has been
416  *        generated for this peer, but it hasn't yet been started
417  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
418  * @param hostkey_cls closure for hostkey callback
419  * @param cb function to call once peer is up, or failed to start
420  * @param cb_cls closure for cb
421  * @return handle to the daemon (actual start will be completed asynchronously)
422  */
423 struct GNUNET_TESTING_Daemon *
424 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
425                              struct GNUNET_TIME_Relative timeout,
426                              const char *hostname,
427                              const char *ssh_username,
428                              uint16_t sshport,
429                              const char *hostkey,
430                              GNUNET_TESTING_NotifyHostkeyCreated
431                              hostkey_callback, void *hostkey_cls,
432                              GNUNET_TESTING_NotifyDaemonRunning cb,
433                              void *cb_cls);
434
435 /**
436  * Continues GNUnet daemon startup when user wanted to be notified
437  * once a hostkey was generated (for creating friends files, blacklists,
438  * etc.).
439  *
440  * @param daemon the daemon to finish starting
441  */
442 void
443 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon);
444
445 /**
446  * Check whether the given daemon is running.
447  *
448  * @param daemon the daemon to check
449  *
450  * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
451  *         daemon is down, GNUNET_SYSERR on error.
452  */
453 int
454 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon);
455
456 /**
457  * Restart (stop and start) a GNUnet daemon.
458  *
459  * @param d the daemon that should be restarted
460  * @param cb function called once the daemon is (re)started
461  * @param cb_cls closure for cb
462  */
463 void
464 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
465                                GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls);
466
467 /**
468  * Start a peer that has previously been stopped using the daemon_stop
469  * call (and files weren't deleted and the allow restart flag)
470  *
471  * @param daemon the daemon to start (has been previously stopped)
472  * @param timeout how long to wait for restart
473  * @param cb the callback for notification when the peer is running
474  * @param cb_cls closure for the callback
475  */
476 void
477 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
478                                      struct GNUNET_TIME_Relative timeout,
479                                      GNUNET_TESTING_NotifyDaemonRunning cb,
480                                      void *cb_cls);
481
482 /**
483  * Get a certain testing daemon handle.
484  *
485  * @param pg handle to the set of running peers
486  * @param position the number of the peer to return
487  */
488 struct GNUNET_TESTING_Daemon *
489 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg, 
490                            unsigned int position);
491
492 /*
493  * Get a daemon by peer identity, so callers can
494  * retrieve the daemon without knowing it's offset.
495  *
496  * @param pg the peer group to retrieve the daemon from
497  * @param peer_id the peer identity of the daemon to retrieve
498  *
499  * @return the daemon on success, or NULL if no such peer identity is found
500  */
501 struct GNUNET_TESTING_Daemon *
502 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
503                                  struct GNUNET_PeerIdentity *peer_id);
504
505 /**
506  * Stops a GNUnet daemon.
507  *
508  * @param d the daemon that should be stopped
509  * @param timeout how long to wait for process for shutdown to complete
510  * @param cb function called once the daemon was stopped
511  * @param cb_cls closure for cb
512  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
513  *        to leave them (i.e. for restarting at a later time,
514  *        or logfile inspection once finished)
515  * @param allow_restart GNUNET_YES to restart peer later (using this API)
516  *        GNUNET_NO to kill off and clean up for good
517  */
518 void
519 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
520                             struct GNUNET_TIME_Relative timeout,
521                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
522                             int delete_files, int allow_restart);
523
524
525 /**
526  * Changes the configuration of a GNUnet daemon.
527  *
528  * @param d the daemon that should be modified
529  * @param cfg the new configuration for the daemon
530  * @param cb function called once the configuration was changed
531  * @param cb_cls closure for cb
532  */
533 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
534                                         struct GNUNET_CONFIGURATION_Handle *cfg,
535                                         GNUNET_TESTING_NotifyCompletion cb,
536                                         void * cb_cls);
537
538
539 /**
540  * Establish a connection between two GNUnet daemons.
541  *
542  * @param d1 handle for the first daemon
543  * @param d2 handle for the second daemon
544  * @param timeout how long is the connection attempt
545  *        allowed to take?
546  * @param max_connect_attempts how many times should we try to reconnect
547  *        (within timeout)
548  * @param cb function to call at the end
549  * @param cb_cls closure for cb
550  */
551 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
552                                      struct GNUNET_TESTING_Daemon *d2,
553                                      struct GNUNET_TIME_Relative timeout,
554                                      unsigned int max_connect_attempts,
555                                      GNUNET_TESTING_NotifyConnection cb,
556                                      void *cb_cls);
557
558
559
560
561 /**
562  * Start count gnunet instances with the same set of transports and
563  * applications.  The port numbers (any option called "PORT") will be
564  * adjusted to ensure that no two peers running on the same system
565  * have the same port(s) in their respective configurations.
566  *
567  * @param cfg configuration template to use
568  * @param total number of daemons to start
569  * @param max_concurrent_connections for testing, how many peers can
570  *                                   we connect to simultaneously
571  * @param timeout total time allowed for peers to start
572  * @param hostkey_callback function to call on each peers hostkey generation
573  *        if NULL, peers will be started by this call, if non-null,
574  *        GNUNET_TESTING_daemons_continue_startup must be called after
575  *        successful hostkey generation
576  * @param hostkey_cls closure for hostkey callback
577  * @param cb function to call on each daemon that was started
578  * @param cb_cls closure for cb
579  * @param connect_callback function to call each time two hosts are connected
580  * @param connect_callback_cls closure for connect_callback
581  * @param hostnames linked list of host structs to use to start peers on
582  *                  (NULL to run on localhost only)
583  *
584  * @return NULL on error, otherwise handle to control peer group
585  */
586 struct GNUNET_TESTING_PeerGroup *
587 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
588                               unsigned int total,
589                               unsigned int max_concurrent_connections,
590                               struct GNUNET_TIME_Relative timeout,
591                               GNUNET_TESTING_NotifyHostkeyCreated
592                               hostkey_callback, void *hostkey_cls,
593                               GNUNET_TESTING_NotifyDaemonRunning cb,
594                               void *cb_cls,
595                               GNUNET_TESTING_NotifyConnection
596                               connect_callback, void *connect_callback_cls,
597                               const struct GNUNET_TESTING_Host *hostnames);
598
599 /**
600  * Function which continues a peer group starting up
601  * after successfully generating hostkeys for each peer.
602  *
603  * @param pg the peer group to continue starting
604  */
605 void
606 GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg);
607
608 /**
609  * Restart all peers in the given group.
610  *
611  * @param pg the handle to the peer group
612  * @param callback function to call on completion (or failure)
613  * @param callback_cls closure for the callback function
614  */
615 void
616 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
617                                 GNUNET_TESTING_NotifyCompletion callback,
618                                 void *callback_cls);
619
620
621 /**
622  * Shutdown all peers started in the given group.
623  *
624  * @param pg handle to the peer group
625  * @param timeout how long to wait for shutdown
626  * @param cb callback to notify upon success or failure
627  * @param cb_cls closure for cb
628  */
629 void
630 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg, 
631                              struct GNUNET_TIME_Relative timeout,
632                              GNUNET_TESTING_NotifyCompletion cb,
633                              void *cb_cls);
634
635
636 /**
637  * Count the number of running peers.
638  *
639  * @param pg handle for the peer group
640  *
641  * @return the number of currently running peers in the peer group
642  */
643 unsigned int
644 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg);
645
646 /**
647  * Simulate churn by stopping some peers (and possibly
648  * re-starting others if churn is called multiple times).  This
649  * function can only be used to create leave-join churn (peers "never"
650  * leave for good).  First "voff" random peers that are currently
651  * online will be taken offline; then "von" random peers that are then
652  * offline will be put back online.  No notifications will be
653  * generated for any of these operations except for the callback upon
654  * completion.  Note that the implementation is at liberty to keep
655  * the ARM service itself (but none of the other services or daemons)
656  * running even though the "peer" is being varied offline.
657  *
658  * @param pg handle for the peer group
659  * @param voff number of peers that should go offline
660  * @param von number of peers that should come back online;
661  *            must be zero on first call (since "testbed_start"
662  *            always starts all of the peers)
663  * @param timeout how long to wait for operations to finish before
664  *        giving up
665  * @param cb function to call at the end
666  * @param cb_cls closure for cb
667  */
668 void
669 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
670                               unsigned int voff,
671                               unsigned int von,
672                               struct GNUNET_TIME_Relative timeout,
673                               GNUNET_TESTING_NotifyCompletion cb,
674                               void *cb_cls);
675
676 /**
677  * Callback function to process statistic values.
678  *
679  * @param cls closure
680  * @param peer the peer the statistics belong to
681  * @param subsystem name of subsystem that created the statistic
682  * @param name the name of the datum
683  * @param value the current value
684  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
685  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
686  */
687 typedef int (*GNUNET_TESTING_STATISTICS_Iterator) (void *cls,
688                                                    const struct GNUNET_PeerIdentity *peer,
689                                                    const char *subsystem,
690                                                    const char *name,
691                                                    uint64_t value,
692                                                    int is_persistent);
693
694 /**
695  * Iterate over all (running) peers in the peer group, retrieve
696  * all statistics from each.
697  */
698 void
699 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
700                                GNUNET_STATISTICS_Callback cont,
701                                GNUNET_TESTING_STATISTICS_Iterator proc, void *cls);
702
703 /**
704  * Topologies supported for testbeds.
705  */
706 enum GNUNET_TESTING_Topology
707 {
708   /**
709    * A clique (everyone connected to everyone else).
710    */
711   GNUNET_TESTING_TOPOLOGY_CLIQUE,
712
713   /**
714    * Small-world network (2d torus plus random links).
715    */
716   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
717
718   /**
719    * Small-world network (ring plus random links).
720    */
721   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
722
723   /**
724    * Ring topology.
725    */
726   GNUNET_TESTING_TOPOLOGY_RING,
727
728   /**
729    * 2-d torus.
730    */
731   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
732
733   /**
734    * Random graph.
735    */
736   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
737
738   /**
739    * Certain percentage of peers are unable to communicate directly
740    * replicating NAT conditions
741    */
742   GNUNET_TESTING_TOPOLOGY_INTERNAT,
743
744   /**
745    * Scale free topology.
746    */
747   GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
748
749   /**
750    * Straight line topology.
751    */
752   GNUNET_TESTING_TOPOLOGY_LINE,
753
754   /**
755    * All peers are disconnected.
756    */
757   GNUNET_TESTING_TOPOLOGY_NONE,
758
759   /**
760    * Read a topology from a given file.
761    */
762   GNUNET_TESTING_TOPOLOGY_FROM_FILE
763 };
764
765 /**
766  * Options for connecting a topology.
767  */
768 enum GNUNET_TESTING_TopologyOption
769 {
770   /**
771    * Try to connect all peers specified in the topology.
772    */
773   GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
774
775   /**
776    * Choose a random subset of connections to create.
777    */
778   GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
779
780   /**
781    * Create at least X connections for each peer.
782    */
783   GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
784
785   /**
786    * Using a depth first search, create one connection
787    * per peer.  If any are missed (graph disconnected)
788    * start over at those peers until all have at least one
789    * connection.
790    */
791   GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
792
793   /**
794    * Find the N closest peers to each allowed peer in the
795    * topology and make sure a connection to those peers
796    * exists in the connect topology.
797    */
798   GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST,
799
800   /**
801    * No options specified.
802    */
803   GNUNET_TESTING_TOPOLOGY_OPTION_NONE
804 };
805
806
807 /**
808  * Get a topology from a string input.
809  *
810  * @param topology where to write the retrieved topology
811  * @param topology_string The string to attempt to
812  *        get a configuration value from
813  * @return GNUNET_YES if topology string matched a
814  *         known topology, GNUNET_NO if not
815  */
816 int
817 GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, 
818                             const char * topology_string);
819
820 /**
821  * Get connect topology option from string input.
822  *
823  * @param topology_option where to write the retrieved topology
824  * @param topology_string The string to attempt to
825  *        get a configuration value from
826  * @return GNUNET_YES if topology string matched a
827  *         known topology, GNUNET_NO if not
828  */
829 int
830 GNUNET_TESTING_topology_option_get(enum GNUNET_TESTING_TopologyOption *topology_option,
831                                    const char * topology_string);
832
833
834 /**
835  * Takes a peer group and creates a topology based on the
836  * one specified.  Creates a topology means generates friend
837  * files for the peers so they can only connect to those allowed
838  * by the topology.  This will only have an effect once peers
839  * are started if the FRIENDS_ONLY option is set in the base
840  * config.
841  *
842  * Also takes an optional restrict topology which
843  * disallows direct connections UNLESS they are specified in
844  * the restricted topology.
845  *
846  * A simple example; if the topology option is set to LINE
847  * peers can ONLY connect in a LINE.  However, if the topology
848  * option is set to 2D-torus and the restrict option is set to
849  * line with restrict_transports equal to "tcp udp", then peers
850  * may connect in a 2D-torus, but will be restricted to tcp and
851  * udp connections only in a LINE.  Generally it only makes
852  * sense to do this if restrict_topology is a subset of topology.
853  *
854  * For testing peer discovery, etc. it is generally better to
855  * leave restrict_topology as GNUNET_TESTING_TOPOLOGY_NONE and
856  * then use the connect_topology function to restrict the initial
857  * connection set.
858  *
859  * @param pg the peer group struct representing the running peers
860  * @param topology which topology to connect the peers in
861  * @param restrict_topology allow only direct connections in this topology,
862  *        based on those listed in restrict_transports, set to
863  *        GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
864  * @param restrict_transports space delimited list of transports to blacklist
865  *                            to create restricted topology, NULL for none
866  *
867  * @return the maximum number of connections were all allowed peers
868  *         connected to each other
869  */
870 unsigned int
871 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
872                                 enum GNUNET_TESTING_Topology topology,
873                                 enum GNUNET_TESTING_Topology restrict_topology,
874                                 const char *restrict_transports);
875
876 /**
877  * Iterate over all (running) peers in the peer group, retrieve
878  * all connections that each currently has.
879  *
880  * @param pg the peer group we are concerned with
881  * @param cb callback for topology information
882  * @param cls closure for callback
883  */
884 void
885 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
886                              GNUNET_TESTING_NotifyTopology cb, void *cls);
887
888 /**
889  * There are many ways to connect peers that are supported by this function.
890  * To connect peers in the same topology that was created via the
891  * GNUNET_TESTING_create_topology, the topology variable must be set to
892  * GNUNET_TESTING_TOPOLOGY_NONE.  If the topology variable is specified,
893  * a new instance of that topology will be generated and attempted to be
894  * connected.  This could result in some connections being impossible,
895  * because some topologies are non-deterministic.
896  *
897  * @param pg the peer group struct representing the running peers
898  * @param topology which topology to connect the peers in
899  * @param options options for connecting the topology
900  * @param option_modifier modifier for options that take a parameter
901  * @param connect_timeout how long to wait before giving up on connecting
902  *                        two peers
903  * @param connect_attempts how many times to attempt to connect two peers
904  *                         over the connect_timeout duration
905  * @param notify_callback notification to be called once all connections completed
906  * @param notify_cls closure for notification callback
907  *
908  * @return the number of connections that will be attempted, GNUNET_SYSERR on error
909  */
910 int
911 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
912                                  enum GNUNET_TESTING_Topology topology,
913                                  enum GNUNET_TESTING_TopologyOption options,
914                                  double option_modifier,
915                                  struct GNUNET_TIME_Relative connect_timeout,
916                                  unsigned int connect_attempts,
917                                  GNUNET_TESTING_NotifyCompletion
918                                  notify_callback, void *notify_cls);
919
920 /**
921  * Start or stop an individual peer from the given group.
922  *
923  * @param pg handle to the peer group
924  * @param offset which peer to start or stop
925  * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
926  * @param timeout how long to wait for shutdown
927  * @param cb function to call at the end
928  * @param cb_cls closure for cb
929  */
930 void
931 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg, 
932                              unsigned int offset,
933                              int desired_status,
934                              struct GNUNET_TIME_Relative timeout,
935                              GNUNET_TESTING_NotifyCompletion cb,
936                              void *cb_cls);
937
938
939 #if 0                           /* keep Emacsens' auto-indent happy */
940 {
941 #endif
942 #ifdef __cplusplus
943 }
944 #endif
945
946 #endif