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