- correct port
[oweals/gnunet.git] / src / transport / transport-testing.h
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 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 2, 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 transport-testing.h
23  * @brief testing lib for transport service
24  *
25  * @author Matthias Wachs
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet_transport_service.h"
36
37
38 #define GNUNET_TRANSPORT_TESTING_ConnectRequest void *
39
40
41 /**
42  * Context for a single peer
43  */
44 struct PeerContext;
45
46 /**
47  * Callback when two peers are connected and both have called the connect callback
48  * to notify clients about a new peer
49  */
50 typedef void (*GNUNET_TRANSPORT_TESTING_start_cb) (struct PeerContext * p,
51                                                    void *cls);
52
53 /**
54  * Callback when two peers are connected and both have called the connect callback
55  * to notify clients about a new peer
56  */
57 typedef void (*GNUNET_TRANSPORT_TESTING_connect_cb) (struct PeerContext * p1,
58                                                      struct PeerContext * p2,
59                                                      void *cls);
60
61
62
63 struct GNUNET_TRANSPORT_TESTING_handle;
64
65 /**
66  * Context for a single peer
67  */
68 struct PeerContext
69 {
70   struct PeerContext *next;
71   struct PeerContext *prev;
72
73   struct GNUNET_TRANSPORT_TESTING_handle *tth;
74
75   struct GNUNET_CONFIGURATION_Handle *cfg;
76
77   struct GNUNET_TRANSPORT_Handle *th;
78
79   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
80
81   struct GNUNET_PeerIdentity id;
82
83   struct GNUNET_OS_Process *arm_proc;
84
85   GNUNET_TRANSPORT_ReceiveCallback rec;
86
87   GNUNET_TRANSPORT_NotifyConnect nc;
88
89   GNUNET_TRANSPORT_NotifyDisconnect nd;
90
91   GNUNET_TRANSPORT_TESTING_start_cb start_cb;
92
93   struct GNUNET_HELLO_Message *hello;
94
95   void *cb_cls;
96
97   char *servicehome;
98
99   char *hostkeyfile;
100
101   unsigned int no;
102 };
103
104
105 struct ConnectingContext
106 {
107   struct ConnectingContext *next;
108   struct ConnectingContext *prev;
109   struct PeerContext *p1;
110   struct PeerContext *p2;
111   GNUNET_SCHEDULER_TaskIdentifier tct;
112   GNUNET_TRANSPORT_TESTING_connect_cb cb;
113   void *cb_cls;
114   struct GNUNET_TRANSPORT_Handle *th_p1;
115   struct GNUNET_TRANSPORT_Handle *th_p2;
116   int p1_c;
117   int p2_c;
118 };
119
120 struct GNUNET_TRANSPORT_TESTING_handle
121 {
122   struct ConnectingContext *cc_head;
123   struct ConnectingContext *cc_tail;
124
125   char *hostkey_data;
126   int hostkeys_total;
127   int hostkeys_last;
128
129   struct PeerContext *p_head;
130   struct PeerContext *p_tail;
131 };
132
133
134 /**
135 * Start a peer with the given configuration
136 * @param tth the testing handle
137 * @param cfgname configuration file
138 * @param peer_id the peer_id
139 * @param rec receive callback
140 * @param nc connect callback
141 * @param nd disconnect callback
142 * @param start_cb start callback
143 * @param cb_cls closure for callback
144 * @return the peer context
145 */
146 struct PeerContext *
147 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
148                                      *tth, const char *cfgname, int peer_id,
149                                      GNUNET_TRANSPORT_ReceiveCallback rec,
150                                      GNUNET_TRANSPORT_NotifyConnect nc,
151                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
152                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
153                                      void *cb_cls);
154
155
156 /**
157  * shutdown the given peer
158  * @param tth the testing handle
159  * @param p the peer
160  */
161
162 void
163 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
164                                     struct PeerContext *pc);
165
166
167 /**
168 * Restart the given peer
169 * @param tth testing handle
170 * @param p the peer
171 * @param cfgname the cfg file used to restart
172 * @param restart_cb restart callback
173 * @param cb_cls callback closure
174 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
175 */
176 int
177 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
178                                        *tth, struct PeerContext *p,
179                                        const char *cfgname,
180                                        GNUNET_TRANSPORT_TESTING_start_cb
181                                        restart_cb, void *cb_cls);
182
183 /**
184  * Connect the given peers and call the callback when both peers report the
185  * inbound connection. Remarks: start_peer's notify_connect callback can be called
186  * before.
187  *
188  * @param tth transport testing handle
189  * @param p1 peer 1
190  * @param p2 peer 2
191  * @param cb the callback to call when both peers notified that they are connected
192  * @param cls callback cls
193  * @return a connect request handle
194  */
195 GNUNET_TRANSPORT_TESTING_ConnectRequest
196 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
197                                         struct PeerContext *p1,
198                                         struct PeerContext *p2,
199                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
200                                         void *cls);
201
202 /**
203  * Cancel the request to connect two peers
204  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
205  * @param tth testing
206  * @param ccr a connect request handle
207  */
208 void
209 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
210                                                GNUNET_TRANSPORT_TESTING_handle
211                                                *tth,
212                                                GNUNET_TRANSPORT_TESTING_ConnectRequest
213                                                ccr);
214
215 /**
216  * Clean up the transport testing
217  * @param tth transport testing handle
218  */
219 void
220 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth);
221
222 /**
223  * Initialize the transport testing
224  * @return transport testing handle
225  */
226 struct GNUNET_TRANSPORT_TESTING_handle *
227 GNUNET_TRANSPORT_TESTING_init ();
228
229 /*
230  * Some utility functions
231  */
232
233 /**
234  * Extracts the test filename from an absolute file name and removes the extension
235  * @param file absolute file name
236  * @param dest where to store result
237  */
238 void
239 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest);
240
241 /**
242  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
243  * if existing ".exe"-prefix and adds the peer-number
244  *
245  * @param file filename of the test, e.g. argv[0]
246  * @param dest where to write the filename
247  * @param count peer number
248  */
249 void
250 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
251                                           int count);
252
253
254 /**
255  * Extracts the plugin anme from an absolute file name and the test name
256  * @param file absolute file name
257  * @param test test name
258  * @param dest where to store result
259  */
260 void
261 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
262                                                const char *testname,
263                                                char **pluginname);
264
265
266 /**
267  * Extracts the filename from an absolute file name and removes the extenstion
268  * @param file absolute file name
269  * @param dest where to store result
270  */
271 void
272 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
273                                                char **testname);
274
275 /* end of transport_testing.h */