diff options
Diffstat (limited to 'src/transport/util_connect_running_peers.c')
-rw-r--r-- | src/transport/util_connect_running_peers.c | 391 |
1 files changed, 391 insertions, 0 deletions
diff --git a/src/transport/util_connect_running_peers.c b/src/transport/util_connect_running_peers.c new file mode 100644 index 000000000..91eb67004 --- /dev/null +++ b/src/transport/util_connect_running_peers.c | |||
@@ -0,0 +1,391 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 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 | * @file transport/util_connect_running_peers.c | ||
22 | * @brief utility to connect running peers | ||
23 | * | ||
24 | * This utility connects to running peers with each other | ||
25 | * The peers have to be started before, for example in the debugger with | ||
26 | * breakpoints set | ||
27 | */ | ||
28 | #include "platform.h" | ||
29 | #include "gnunet_common.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | #include "gnunet_getopt_lib.h" | ||
32 | #include "gnunet_os_lib.h" | ||
33 | #include "gnunet_program_lib.h" | ||
34 | #include "gnunet_scheduler_lib.h" | ||
35 | #include "gnunet_transport_service.h" | ||
36 | #include "transport.h" | ||
37 | #include "transport-testing.h" | ||
38 | |||
39 | #define VERBOSE GNUNET_NO | ||
40 | |||
41 | #define VERBOSE_ARM GNUNET_NO | ||
42 | |||
43 | #define START_ARM GNUNET_YES | ||
44 | |||
45 | /** | ||
46 | * How long until we give up on transmitting the message? | ||
47 | */ | ||
48 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) | ||
49 | |||
50 | /** | ||
51 | * How long until we give up on transmitting the message? | ||
52 | */ | ||
53 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | ||
54 | |||
55 | #define MTYPE 12345 | ||
56 | |||
57 | static int ok; | ||
58 | |||
59 | static GNUNET_SCHEDULER_TaskIdentifier die_task; | ||
60 | |||
61 | static GNUNET_SCHEDULER_TaskIdentifier send_task; | ||
62 | |||
63 | struct PeerContext *p1; | ||
64 | |||
65 | struct PeerContext *p2; | ||
66 | |||
67 | static GNUNET_TRANSPORT_TESTING_ConnectRequest cc; | ||
68 | |||
69 | struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
70 | |||
71 | char *cfg_file_p1; | ||
72 | |||
73 | char *cfg_file_p2; | ||
74 | |||
75 | #if VERBOSE | ||
76 | #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) | ||
77 | #else | ||
78 | #define OKPP do { ok++; } while (0) | ||
79 | #endif | ||
80 | |||
81 | |||
82 | |||
83 | static void | ||
84 | end () | ||
85 | { | ||
86 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n"); | ||
87 | |||
88 | if (send_task != GNUNET_SCHEDULER_NO_TASK) | ||
89 | GNUNET_SCHEDULER_cancel (send_task); | ||
90 | |||
91 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | ||
92 | GNUNET_SCHEDULER_cancel (die_task); | ||
93 | |||
94 | if (th != NULL) | ||
95 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
96 | th = NULL; | ||
97 | |||
98 | if (p1 != NULL) | ||
99 | disconnect_from_peer (p1); | ||
100 | if (p2 != NULL) | ||
101 | disconnect_from_peer (p2); | ||
102 | } | ||
103 | |||
104 | static void | ||
105 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
106 | { | ||
107 | die_task = GNUNET_SCHEDULER_NO_TASK; | ||
108 | |||
109 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n"); | ||
110 | |||
111 | if (send_task != GNUNET_SCHEDULER_NO_TASK) | ||
112 | GNUNET_SCHEDULER_cancel (send_task); | ||
113 | |||
114 | if (cc != NULL) | ||
115 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
116 | |||
117 | if (th != NULL) | ||
118 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
119 | th = NULL; | ||
120 | |||
121 | if (p1 != NULL) | ||
122 | disconnect_from_peer (p1); | ||
123 | if (p2 != NULL) | ||
124 | disconnect_from_peer (p2); | ||
125 | |||
126 | ok = GNUNET_SYSERR; | ||
127 | } | ||
128 | |||
129 | |||
130 | static void | ||
131 | notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
132 | const struct GNUNET_MessageHeader *message, | ||
133 | const struct GNUNET_TRANSPORT_ATS_Information *ats, | ||
134 | uint32_t ats_count) | ||
135 | { | ||
136 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
137 | "Received message of type %d from peer %s!\n", | ||
138 | ntohs (message->type), GNUNET_i2s (peer)); | ||
139 | |||
140 | if ((MTYPE == ntohs (message->type)) && | ||
141 | (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) | ||
142 | { | ||
143 | ok = 0; | ||
144 | end (); | ||
145 | } | ||
146 | else | ||
147 | { | ||
148 | GNUNET_break (0); | ||
149 | ok = 1; | ||
150 | end (); | ||
151 | } | ||
152 | } | ||
153 | |||
154 | |||
155 | static size_t | ||
156 | notify_ready (void *cls, size_t size, void *buf) | ||
157 | { | ||
158 | struct PeerContext *p = cls; | ||
159 | struct GNUNET_MessageHeader *hdr; | ||
160 | |||
161 | th = NULL; | ||
162 | |||
163 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
164 | "Transmitting message with %u bytes to peer %s\n", | ||
165 | sizeof (struct GNUNET_MessageHeader), GNUNET_i2s (&p->id)); | ||
166 | GNUNET_assert (size >= 256); | ||
167 | |||
168 | if (buf != NULL) | ||
169 | { | ||
170 | hdr = buf; | ||
171 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
172 | hdr->type = htons (MTYPE); | ||
173 | } | ||
174 | return sizeof (struct GNUNET_MessageHeader); | ||
175 | } | ||
176 | |||
177 | |||
178 | static void | ||
179 | notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
180 | const struct GNUNET_TRANSPORT_ATS_Information *ats, | ||
181 | uint32_t ats_count) | ||
182 | { | ||
183 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n", | ||
184 | GNUNET_i2s (peer), cls); | ||
185 | } | ||
186 | |||
187 | |||
188 | static void | ||
189 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
190 | { | ||
191 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n", | ||
192 | GNUNET_i2s (peer), cls); | ||
193 | } | ||
194 | |||
195 | |||
196 | void | ||
197 | disconnect_from_peer (struct PeerContext *p) | ||
198 | { | ||
199 | GNUNET_assert (p != NULL); | ||
200 | if (p->th != NULL) | ||
201 | GNUNET_TRANSPORT_disconnect (p->th); | ||
202 | |||
203 | if (p->cfg != NULL) | ||
204 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
205 | GNUNET_free (p); | ||
206 | p = NULL; | ||
207 | } | ||
208 | |||
209 | struct PeerContext * | ||
210 | connect_to_peer (const char *cfgname, GNUNET_TRANSPORT_ReceiveCallback rec, | ||
211 | GNUNET_TRANSPORT_NotifyConnect nc, | ||
212 | GNUNET_TRANSPORT_NotifyDisconnect nd, void *cb_cls) | ||
213 | { | ||
214 | if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO) | ||
215 | { | ||
216 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", cfgname); | ||
217 | return NULL; | ||
218 | } | ||
219 | |||
220 | struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext)); | ||
221 | |||
222 | p->cfg = GNUNET_CONFIGURATION_create (); | ||
223 | |||
224 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | ||
225 | if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME")) | ||
226 | GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", | ||
227 | &p->servicehome); | ||
228 | if (NULL != p->servicehome) | ||
229 | GNUNET_DISK_directory_remove (p->servicehome); | ||
230 | /* | ||
231 | * p->arm_proc = | ||
232 | * GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", | ||
233 | * "gnunet-service-arm", "-c", cfgname, | ||
234 | * #if VERBOSE_PEERS | ||
235 | * "-L", "DEBUG", | ||
236 | * #else | ||
237 | * "-L", "ERROR", | ||
238 | * #endif | ||
239 | * NULL); | ||
240 | */ | ||
241 | p->nc = nc; | ||
242 | p->nd = nd; | ||
243 | p->rec = rec; | ||
244 | if (cb_cls != NULL) | ||
245 | p->cb_cls = cb_cls; | ||
246 | else | ||
247 | p->cb_cls = p; | ||
248 | |||
249 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service `%s'\n", | ||
250 | p->servicehome); | ||
251 | p->th = | ||
252 | GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive, | ||
253 | ¬ify_connect, ¬ify_disconnect); | ||
254 | GNUNET_assert (p->th != NULL); | ||
255 | return p; | ||
256 | } | ||
257 | |||
258 | static void | ||
259 | sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
260 | { | ||
261 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
262 | |||
263 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
264 | return; | ||
265 | |||
266 | th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, 256, 0, TIMEOUT, | ||
267 | ¬ify_ready, &p1); | ||
268 | } | ||
269 | |||
270 | static void | ||
271 | testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls) | ||
272 | { | ||
273 | cc = NULL; | ||
274 | char *p1_c = strdup (GNUNET_i2s (&p1->id)); | ||
275 | |||
276 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers connected: %s <-> %s\n", p1_c, | ||
277 | GNUNET_i2s (&p2->id)); | ||
278 | GNUNET_free (p1_c); | ||
279 | |||
280 | // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG! | ||
281 | send_task = | ||
282 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL); | ||
283 | } | ||
284 | |||
285 | static void | ||
286 | run (void *cls, char *const *args, const char *cfgfile, | ||
287 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
288 | { | ||
289 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | ||
290 | |||
291 | p1 = connect_to_peer (cfg_file_p1, ¬ify_receive, ¬ify_connect, | ||
292 | ¬ify_disconnect, NULL); | ||
293 | p2 = connect_to_peer (cfg_file_p2, ¬ify_receive, ¬ify_connect, | ||
294 | ¬ify_disconnect, NULL); | ||
295 | |||
296 | if ((p1 == NULL) || (p2 == NULL)) | ||
297 | { | ||
298 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n"); | ||
299 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | ||
300 | GNUNET_SCHEDULER_cancel (die_task); | ||
301 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
302 | return; | ||
303 | } | ||
304 | |||
305 | cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, | ||
306 | NULL); | ||
307 | } | ||
308 | |||
309 | |||
310 | static int | ||
311 | check () | ||
312 | { | ||
313 | static char *const argv[] = { "test-transport-api", | ||
314 | "-c", | ||
315 | "test_transport_api_data.conf", | ||
316 | #if VERBOSE | ||
317 | "-L", "DEBUG", | ||
318 | #endif | ||
319 | NULL | ||
320 | }; | ||
321 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
322 | GNUNET_GETOPT_OPTION_END | ||
323 | }; | ||
324 | |||
325 | #if WRITECONFIG | ||
326 | setTransportOptions ("test_transport_api_data.conf"); | ||
327 | #endif | ||
328 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
329 | |||
330 | ok = 1; | ||
331 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, | ||
332 | "util_connect_running_peers", "nohelp", options, &run, | ||
333 | &ok); | ||
334 | |||
335 | return ok; | ||
336 | } | ||
337 | |||
338 | int | ||
339 | main (int argc, char *argv[]) | ||
340 | { | ||
341 | int ret; | ||
342 | |||
343 | GNUNET_log_setup ("util_connect_running_peers", | ||
344 | #if VERBOSE | ||
345 | "DEBUG", | ||
346 | #else | ||
347 | "WARNING", | ||
348 | #endif | ||
349 | NULL); | ||
350 | |||
351 | |||
352 | if (argc < 3) | ||
353 | { | ||
354 | fprintf (stderr, | ||
355 | "usage ./util_connect_running_peers <cfg_peer1> <cfg_peer2>\n"); | ||
356 | return -1; | ||
357 | } | ||
358 | else | ||
359 | { | ||
360 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configuration file 1: `%s' \n", | ||
361 | argv[1]); | ||
362 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configuration file 2: `%s'\n", | ||
363 | argv[2]); | ||
364 | } | ||
365 | |||
366 | if (GNUNET_DISK_file_test (argv[1]) == GNUNET_NO) | ||
367 | { | ||
368 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", argv[1]); | ||
369 | return -1; | ||
370 | } | ||
371 | if (GNUNET_DISK_file_test (argv[2]) == GNUNET_NO) | ||
372 | { | ||
373 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", argv[2]); | ||
374 | return -1; | ||
375 | } | ||
376 | |||
377 | GNUNET_asprintf (&cfg_file_p1, argv[1]); | ||
378 | GNUNET_asprintf (&cfg_file_p2, argv[2]); | ||
379 | |||
380 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
381 | "Trying to connect peers, press control-d to stop... \n", | ||
382 | argv[1]); | ||
383 | |||
384 | ret = check (); | ||
385 | |||
386 | GNUNET_free (cfg_file_p1); | ||
387 | GNUNET_free (cfg_file_p2); | ||
388 | return ret; | ||
389 | } | ||
390 | |||
391 | /* end of test_transport_api.c */ | ||