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