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