diff options
Diffstat (limited to 'src/cadet/test_cadet.c')
-rw-r--r-- | src/cadet/test_cadet.c | 1605 |
1 files changed, 0 insertions, 1605 deletions
diff --git a/src/cadet/test_cadet.c b/src/cadet/test_cadet.c deleted file mode 100644 index bdf2ac986..000000000 --- a/src/cadet/test_cadet.c +++ /dev/null | |||
@@ -1,1605 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file cadet/test_cadet.c | ||
22 | * @author Bart Polot | ||
23 | * @author Christian Grothoff | ||
24 | * @brief Test for the cadet service using mq API. | ||
25 | */ | ||
26 | #include <stdio.h> | ||
27 | #include "platform.h" | ||
28 | #include "cadet.h" | ||
29 | #include "cadet_test_lib.h" | ||
30 | #include "gnunet_cadet_service.h" | ||
31 | #include "gnunet_statistics_service.h" | ||
32 | #include <gauger.h> | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Ugly workaround to unify data handlers on incoming and outgoing channels. | ||
37 | */ | ||
38 | struct CadetTestChannelWrapper | ||
39 | { | ||
40 | /** | ||
41 | * Channel pointer. | ||
42 | */ | ||
43 | struct GNUNET_CADET_Channel *ch; | ||
44 | }; | ||
45 | |||
46 | /** | ||
47 | * How many messages to send by default. | ||
48 | */ | ||
49 | #define TOTAL_PACKETS 500 /* Cannot exceed 64k! */ | ||
50 | |||
51 | /** | ||
52 | * How long until we give up on connecting the peers? | ||
53 | */ | ||
54 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) | ||
55 | |||
56 | /** | ||
57 | * Time to wait by default for stuff that should be rather fast. | ||
58 | */ | ||
59 | #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20) | ||
60 | |||
61 | /** | ||
62 | * How fast do we send messages? | ||
63 | */ | ||
64 | #define SEND_INTERVAL GNUNET_TIME_relative_multiply ( \ | ||
65 | GNUNET_TIME_UNIT_MILLISECONDS, 10) | ||
66 | |||
67 | /** | ||
68 | * DIFFERENT TESTS TO RUN | ||
69 | */ | ||
70 | #define SETUP 0 | ||
71 | #define FORWARD 1 | ||
72 | #define KEEPALIVE 2 | ||
73 | #define SPEED 3 | ||
74 | #define SPEED_ACK 4 | ||
75 | #define SPEED_REL 8 | ||
76 | #define P2P_SIGNAL 10 | ||
77 | #define REOPEN 11 | ||
78 | #define DESTROY 12 | ||
79 | |||
80 | /** | ||
81 | * Active peer listing operation. | ||
82 | */ | ||
83 | static struct GNUNET_CADET_PeersLister *plo; | ||
84 | |||
85 | /* | ||
86 | * Task called to check for existing tunnel and depending on that reopen channel | ||
87 | */ | ||
88 | static struct GNUNET_SCHEDULER_Task *get_peers_task; | ||
89 | |||
90 | /** | ||
91 | * Which test are we running? | ||
92 | */ | ||
93 | static int test; | ||
94 | |||
95 | /** | ||
96 | * String with test name | ||
97 | */ | ||
98 | static char *test_name; | ||
99 | |||
100 | /** | ||
101 | * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic. | ||
102 | */ | ||
103 | static int test_backwards = GNUNET_NO; | ||
104 | |||
105 | /** | ||
106 | * How many packets to send. | ||
107 | */ | ||
108 | static unsigned int total_packets; | ||
109 | |||
110 | /** | ||
111 | * Time to wait for fast operations. | ||
112 | */ | ||
113 | static struct GNUNET_TIME_Relative short_time; | ||
114 | |||
115 | /** | ||
116 | * How many events have happened | ||
117 | */ | ||
118 | static int ok; | ||
119 | |||
120 | /** | ||
121 | * Number of events expected to conclude the test successfully. | ||
122 | */ | ||
123 | static int ok_goal; | ||
124 | |||
125 | /** | ||
126 | * Size of each test packet's payload | ||
127 | */ | ||
128 | static size_t size_payload = sizeof(uint32_t); | ||
129 | |||
130 | /** | ||
131 | * Operation to get peer ids. | ||
132 | */ | ||
133 | static struct GNUNET_TESTBED_Operation *t_op[2]; | ||
134 | |||
135 | /** | ||
136 | * Peer ids. | ||
137 | */ | ||
138 | static struct GNUNET_PeerIdentity *testpeer_id[2]; | ||
139 | |||
140 | /** | ||
141 | * Peer ids. | ||
142 | */ | ||
143 | static struct GNUNET_CONFIGURATION_Handle *p_cfg[2]; | ||
144 | |||
145 | /** | ||
146 | * Port ID | ||
147 | */ | ||
148 | static struct GNUNET_HashCode port; | ||
149 | |||
150 | /** | ||
151 | * Peer ids counter. | ||
152 | */ | ||
153 | static unsigned int peerinfo_task_cnt; | ||
154 | |||
155 | /** | ||
156 | * Is the setup initialized? | ||
157 | */ | ||
158 | static int initialized; | ||
159 | |||
160 | /** | ||
161 | * Number of payload packes sent. | ||
162 | */ | ||
163 | static int data_sent; | ||
164 | |||
165 | /** | ||
166 | * Number of payload packets received. | ||
167 | */ | ||
168 | static int data_received; | ||
169 | |||
170 | /** | ||
171 | * Number of payload packed acknowledgements sent. | ||
172 | */ | ||
173 | static int ack_sent; | ||
174 | |||
175 | /** | ||
176 | * Number of payload packed explicitly (app level) acknowledged. | ||
177 | */ | ||
178 | static int ack_received; | ||
179 | |||
180 | /** | ||
181 | * Total number of peers asked to run. | ||
182 | */ | ||
183 | static unsigned long long peers_requested; | ||
184 | |||
185 | /** | ||
186 | * Number of currently running peers (should be same as @c peers_requested). | ||
187 | */ | ||
188 | static unsigned long long peers_running; | ||
189 | |||
190 | /** | ||
191 | * Test context (to shut down). | ||
192 | */ | ||
193 | struct GNUNET_CADET_TEST_Context *test_ctx; | ||
194 | |||
195 | /** | ||
196 | * Task called to disconnect peers. | ||
197 | */ | ||
198 | static struct GNUNET_SCHEDULER_Task *disconnect_task; | ||
199 | |||
200 | /** | ||
201 | * Task called to reconnect peers. | ||
202 | */ | ||
203 | static struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
204 | |||
205 | /** | ||
206 | * Task To perform tests | ||
207 | */ | ||
208 | static struct GNUNET_SCHEDULER_Task *test_task; | ||
209 | |||
210 | /** | ||
211 | * Task runnining #send_next_msg(). | ||
212 | */ | ||
213 | static struct GNUNET_SCHEDULER_Task *send_next_msg_task; | ||
214 | |||
215 | /** | ||
216 | * Channel handle for the root peer | ||
217 | */ | ||
218 | static struct GNUNET_CADET_Channel *outgoing_ch; | ||
219 | |||
220 | /** | ||
221 | * Channel handle for the dest peer | ||
222 | */ | ||
223 | static struct GNUNET_CADET_Channel *incoming_ch; | ||
224 | |||
225 | /** | ||
226 | * Time we started the data transmission (after channel has been established | ||
227 | * and initialized). | ||
228 | */ | ||
229 | static struct GNUNET_TIME_Absolute start_time; | ||
230 | |||
231 | /** | ||
232 | * Peers handle. | ||
233 | */ | ||
234 | static struct GNUNET_TESTBED_Peer **testbed_peers; | ||
235 | |||
236 | |||
237 | struct GNUNET_CADET_Handle **cadets_running; | ||
238 | |||
239 | /** | ||
240 | * Statistics operation handle. | ||
241 | */ | ||
242 | static struct GNUNET_TESTBED_Operation *stats_op; | ||
243 | |||
244 | /** | ||
245 | * Keepalives sent. | ||
246 | */ | ||
247 | static unsigned int ka_sent; | ||
248 | |||
249 | /** | ||
250 | * Keepalives received. | ||
251 | */ | ||
252 | static unsigned int ka_received; | ||
253 | |||
254 | /** | ||
255 | * How many messages were dropped by CADET because of full buffers? | ||
256 | */ | ||
257 | static unsigned int msg_dropped; | ||
258 | |||
259 | /** | ||
260 | * Drop the next cadet message of a given type.. | ||
261 | * | ||
262 | * @param mq message queue | ||
263 | * @param ccn client channel number. | ||
264 | * @param type of cadet message to be dropped. | ||
265 | */ | ||
266 | void | ||
267 | GNUNET_CADET_drop_message (struct GNUNET_MQ_Handle *mq, | ||
268 | struct GNUNET_CADET_ClientChannelNumber ccn, | ||
269 | uint16_t type); | ||
270 | |||
271 | /******************************************************************************/ | ||
272 | |||
273 | |||
274 | /******************************************************************************/ | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Get the channel considered as the "target" or "receiver", depending on | ||
279 | * the test type and size. | ||
280 | * | ||
281 | * @return Channel handle of the target client, either 0 (for backward tests) | ||
282 | * or the last peer in the line (for other tests). | ||
283 | */ | ||
284 | static struct GNUNET_CADET_Channel * | ||
285 | get_target_channel () | ||
286 | { | ||
287 | if ((SPEED == test) && (GNUNET_YES == test_backwards)) | ||
288 | return outgoing_ch; | ||
289 | else | ||
290 | return incoming_ch; | ||
291 | } | ||
292 | |||
293 | |||
294 | /** | ||
295 | * Show the results of the test (banwidth achieved) and log them to GAUGER | ||
296 | */ | ||
297 | static void | ||
298 | show_end_data (void) | ||
299 | { | ||
300 | static struct GNUNET_TIME_Absolute end_time; | ||
301 | static struct GNUNET_TIME_Relative total_time; | ||
302 | |||
303 | end_time = GNUNET_TIME_absolute_get (); | ||
304 | total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time); | ||
305 | fprintf (stderr, | ||
306 | "\nResults of test \"%s\"\n", | ||
307 | test_name); | ||
308 | fprintf (stderr, | ||
309 | "Test time %s\n", | ||
310 | GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES)); | ||
311 | fprintf (stderr, | ||
312 | "Test bandwidth: %f kb/s\n", | ||
313 | 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms | ||
314 | fprintf (stderr, | ||
315 | "Test throughput: %f packets/s\n\n", | ||
316 | total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms | ||
317 | GAUGER ("CADET", | ||
318 | test_name, | ||
319 | total_packets * 1000.0 / (total_time.rel_value_us / 1000), | ||
320 | "packets/s"); | ||
321 | } | ||
322 | |||
323 | |||
324 | /** | ||
325 | * Disconnect from cadet services af all peers, call shutdown. | ||
326 | * | ||
327 | * @param cls Closure (line number from which termination was requested). | ||
328 | * @param tc Task Context. | ||
329 | */ | ||
330 | static void | ||
331 | disconnect_cadet_peers (void *cls) | ||
332 | { | ||
333 | long line = (long) cls; | ||
334 | |||
335 | disconnect_task = NULL; | ||
336 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
337 | "disconnecting cadet service of peers, called from line %ld\n", | ||
338 | line); | ||
339 | for (unsigned int i = 0; i < 2; i++) | ||
340 | { | ||
341 | GNUNET_TESTBED_operation_done (t_op[i]); | ||
342 | } | ||
343 | if (NULL != outgoing_ch) | ||
344 | { | ||
345 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
346 | outgoing_ch = NULL; | ||
347 | } | ||
348 | if (NULL != incoming_ch) | ||
349 | { | ||
350 | GNUNET_CADET_channel_destroy (incoming_ch); | ||
351 | incoming_ch = NULL; | ||
352 | } | ||
353 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
354 | GNUNET_SCHEDULER_shutdown (); | ||
355 | } | ||
356 | |||
357 | |||
358 | /** | ||
359 | * Shut down peergroup, clean up. | ||
360 | * | ||
361 | * @param cls Closure (unused). | ||
362 | * @param tc Task Context. | ||
363 | */ | ||
364 | static void | ||
365 | shutdown_task (void *cls) | ||
366 | { | ||
367 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
368 | "Ending test.\n"); | ||
369 | if (NULL != send_next_msg_task) | ||
370 | { | ||
371 | GNUNET_SCHEDULER_cancel (send_next_msg_task); | ||
372 | send_next_msg_task = NULL; | ||
373 | } | ||
374 | if (NULL != test_task) | ||
375 | { | ||
376 | GNUNET_SCHEDULER_cancel (test_task); | ||
377 | test_task = NULL; | ||
378 | } | ||
379 | if (NULL != disconnect_task) | ||
380 | { | ||
381 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
382 | disconnect_task = | ||
383 | GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
384 | (void *) __LINE__); | ||
385 | } | ||
386 | } | ||
387 | |||
388 | |||
389 | /** | ||
390 | * Stats callback. Finish the stats testbed operation and when all stats have | ||
391 | * been iterated, shutdown the test. | ||
392 | * | ||
393 | * @param cls Closure (line number from which termination was requested). | ||
394 | * @param op the operation that has been finished | ||
395 | * @param emsg error message in case the operation has failed; will be NULL if | ||
396 | * operation has executed successfully. | ||
397 | */ | ||
398 | static void | ||
399 | stats_cont (void *cls, | ||
400 | struct GNUNET_TESTBED_Operation *op, | ||
401 | const char *emsg) | ||
402 | { | ||
403 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
404 | "KA sent: %u, KA received: %u\n", | ||
405 | ka_sent, | ||
406 | ka_received); | ||
407 | if (((KEEPALIVE == test) || (REOPEN == test)) && | ||
408 | ((ka_sent < 2) || (ka_sent > ka_received + 1))) | ||
409 | { | ||
410 | GNUNET_break (0); | ||
411 | ok--; | ||
412 | } | ||
413 | GNUNET_TESTBED_operation_done (stats_op); | ||
414 | |||
415 | if (NULL != disconnect_task) | ||
416 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
417 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
418 | cls); | ||
419 | } | ||
420 | |||
421 | |||
422 | /** | ||
423 | * Process statistic values. | ||
424 | * | ||
425 | * @param cls closure (line number, unused) | ||
426 | * @param peer the peer the statistic belong to | ||
427 | * @param subsystem name of subsystem that created the statistic | ||
428 | * @param name the name of the datum | ||
429 | * @param value the current value | ||
430 | * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not | ||
431 | * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration | ||
432 | */ | ||
433 | static int | ||
434 | stats_iterator (void *cls, | ||
435 | const struct GNUNET_TESTBED_Peer *peer, | ||
436 | const char *subsystem, | ||
437 | const char *name, | ||
438 | uint64_t value, | ||
439 | int is_persistent) | ||
440 | { | ||
441 | static const char *s_sent = "# keepalives sent"; | ||
442 | static const char *s_recv = "# keepalives received"; | ||
443 | static const char *rdrops = "# messages dropped due to full buffer"; | ||
444 | static const char *cdrops = "# messages dropped due to slow client"; | ||
445 | uint32_t i; | ||
446 | |||
447 | i = GNUNET_TESTBED_get_index (peer); | ||
448 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i, | ||
449 | subsystem, name, (unsigned long long) value); | ||
450 | if ((0 == strncmp (s_sent, name, strlen (s_sent))) && (0 == i)) | ||
451 | ka_sent = value; | ||
452 | if ((0 == strncmp (s_recv, name, strlen (s_recv))) && (peers_requested - 1 == | ||
453 | i) ) | ||
454 | ka_received = value; | ||
455 | if (0 == strncmp (rdrops, name, strlen (rdrops))) | ||
456 | msg_dropped += value; | ||
457 | if (0 == strncmp (cdrops, name, strlen (cdrops))) | ||
458 | msg_dropped += value; | ||
459 | |||
460 | return GNUNET_OK; | ||
461 | } | ||
462 | |||
463 | |||
464 | /** | ||
465 | * Task to gather all statistics. | ||
466 | * | ||
467 | * @param cls Closure (line from which the task was scheduled). | ||
468 | */ | ||
469 | static void | ||
470 | gather_stats_and_exit (void *cls) | ||
471 | { | ||
472 | long l = (long) cls; | ||
473 | |||
474 | disconnect_task = NULL; | ||
475 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
476 | "gathering statistics from line %ld\n", | ||
477 | l); | ||
478 | if (NULL != outgoing_ch) | ||
479 | { | ||
480 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
481 | outgoing_ch = NULL; | ||
482 | } | ||
483 | stats_op = GNUNET_TESTBED_get_statistics (peers_running, | ||
484 | testbed_peers, | ||
485 | "cadet", | ||
486 | NULL, | ||
487 | &stats_iterator, | ||
488 | stats_cont, | ||
489 | cls); | ||
490 | } | ||
491 | |||
492 | |||
493 | /** | ||
494 | * Send a message on the channel with the appropriate size and payload. | ||
495 | * | ||
496 | * Update the appropriate *_sent counter. | ||
497 | * | ||
498 | * @param channel Channel to send the message on. | ||
499 | */ | ||
500 | static void | ||
501 | send_test_message (struct GNUNET_CADET_Channel *channel); | ||
502 | |||
503 | /** | ||
504 | * Check if payload is sane (size contains payload). | ||
505 | * | ||
506 | * @param cls should match #ch | ||
507 | * @param message The actual message. | ||
508 | * @return #GNUNET_OK to keep the channel open, | ||
509 | * #GNUNET_SYSERR to close it (signal serious error). | ||
510 | */ | ||
511 | static int | ||
512 | check_data (void *cls, | ||
513 | const struct GNUNET_MessageHeader *message); | ||
514 | |||
515 | /** | ||
516 | * Function is called whenever a message is received. | ||
517 | * | ||
518 | * @param cls closure (set from GNUNET_CADET_connect(), peer number) | ||
519 | * @param message the actual message | ||
520 | */ | ||
521 | static void | ||
522 | handle_data (void *cls, | ||
523 | const struct GNUNET_MessageHeader *message); | ||
524 | |||
525 | /** | ||
526 | * Function called whenever an MQ-channel is destroyed, unless the destruction | ||
527 | * was requested by #GNUNET_CADET_channel_destroy. | ||
528 | * It must NOT call #GNUNET_CADET_channel_destroy on the channel. | ||
529 | * | ||
530 | * It should clean up any associated state, including cancelling any pending | ||
531 | * transmission on this channel. | ||
532 | * | ||
533 | * @param cls Channel closure (channel wrapper). | ||
534 | * @param channel Connection to the other end (henceforth invalid). | ||
535 | */ | ||
536 | static void | ||
537 | disconnect_handler (void *cls, | ||
538 | const struct GNUNET_CADET_Channel *channel); | ||
539 | |||
540 | static struct GNUNET_PeerIdentity * | ||
541 | get_from_p_ids () | ||
542 | { | ||
543 | if (0 < GNUNET_memcmp (testpeer_id[0], testpeer_id[1])) | ||
544 | { | ||
545 | return testpeer_id[1]; | ||
546 | } | ||
547 | else | ||
548 | { | ||
549 | return testpeer_id[0]; | ||
550 | } | ||
551 | } | ||
552 | |||
553 | |||
554 | static struct GNUNET_CADET_Handle * | ||
555 | get_from_cadets () | ||
556 | { | ||
557 | |||
558 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "1\n"); | ||
559 | if (0 < GNUNET_memcmp (testpeer_id[0], testpeer_id[1])) | ||
560 | { | ||
561 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "standard peer\n"); | ||
562 | return cadets_running[0]; | ||
563 | } | ||
564 | else | ||
565 | { | ||
566 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "the other peer\n"); | ||
567 | return cadets_running[peers_running - 1]; | ||
568 | } | ||
569 | |||
570 | } | ||
571 | |||
572 | |||
573 | static unsigned int | ||
574 | get_peer_nr (int outgoing) | ||
575 | { | ||
576 | if (0 < GNUNET_memcmp (testpeer_id[0], testpeer_id[1])) | ||
577 | { | ||
578 | return GNUNET_YES == outgoing ? 0 : peers_running - 1; | ||
579 | } | ||
580 | else | ||
581 | { | ||
582 | return GNUNET_YES == outgoing ? peers_running - 1 : 0; | ||
583 | } | ||
584 | } | ||
585 | |||
586 | |||
587 | /** | ||
588 | * Task to reconnect to other peer. | ||
589 | * | ||
590 | * @param cls Closure (line from which the task was scheduled). | ||
591 | */ | ||
592 | static void | ||
593 | reconnect_op (void *cls) | ||
594 | { | ||
595 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
596 | GNUNET_MQ_hd_var_size (data, | ||
597 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
598 | struct GNUNET_MessageHeader, | ||
599 | NULL), | ||
600 | GNUNET_MQ_handler_end () | ||
601 | }; | ||
602 | long l = (long) cls; | ||
603 | struct CadetTestChannelWrapper *ch; | ||
604 | static struct GNUNET_PeerIdentity *p_id; | ||
605 | static struct GNUNET_CADET_Handle *h1; | ||
606 | |||
607 | reconnect_task = NULL; | ||
608 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
609 | "reconnecting from line %ld\n", | ||
610 | l); | ||
611 | if (NULL != outgoing_ch) | ||
612 | { | ||
613 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
614 | outgoing_ch = NULL; | ||
615 | } | ||
616 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
617 | |||
618 | p_id = get_from_p_ids (); | ||
619 | h1 = get_from_cadets (); | ||
620 | |||
621 | outgoing_ch = GNUNET_CADET_channel_create (h1, | ||
622 | ch, | ||
623 | p_id, | ||
624 | &port, | ||
625 | NULL, | ||
626 | &disconnect_handler, | ||
627 | handlers); | ||
628 | ch->ch = outgoing_ch; | ||
629 | send_test_message (outgoing_ch); | ||
630 | } | ||
631 | |||
632 | |||
633 | void | ||
634 | reopen_channel () | ||
635 | { | ||
636 | struct CadetTestChannelWrapper *ch; | ||
637 | static struct GNUNET_CADET_Handle *h1; | ||
638 | static struct GNUNET_PeerIdentity *p_id; | ||
639 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
640 | GNUNET_MQ_hd_var_size (data, | ||
641 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
642 | struct GNUNET_MessageHeader, | ||
643 | NULL), | ||
644 | GNUNET_MQ_handler_end () | ||
645 | }; | ||
646 | |||
647 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "creating channel again\n"); | ||
648 | p_id = get_from_p_ids (); | ||
649 | h1 = get_from_cadets (); | ||
650 | |||
651 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
652 | outgoing_ch = GNUNET_CADET_channel_create (h1, | ||
653 | ch, | ||
654 | p_id, | ||
655 | &port, | ||
656 | NULL, | ||
657 | &disconnect_handler, | ||
658 | handlers); | ||
659 | ch->ch = outgoing_ch; | ||
660 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
661 | "Sending second test data (after destroying the channel) on channel %p...\n", | ||
662 | outgoing_ch); | ||
663 | send_test_message (outgoing_ch); | ||
664 | } | ||
665 | |||
666 | |||
667 | static void | ||
668 | peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple); | ||
669 | |||
670 | /** | ||
671 | * We ask the monitoring api for all the peers. | ||
672 | */ | ||
673 | static void | ||
674 | get_peers (void *cls) | ||
675 | { | ||
676 | |||
677 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
678 | "requesting peers info!\n"); | ||
679 | plo = GNUNET_CADET_list_peers (p_cfg[get_peer_nr (GNUNET_YES)], | ||
680 | &peers_callback, NULL); | ||
681 | |||
682 | } | ||
683 | |||
684 | |||
685 | /** | ||
686 | * Method called to retrieve information about all peers in CADET, called | ||
687 | * once per peer. | ||
688 | * | ||
689 | * After last peer has been reported, an additional call with NULL is done. | ||
690 | * | ||
691 | * We check the peer we are interested in, if we have a tunnel. If not, we | ||
692 | * reopen the channel | ||
693 | * | ||
694 | * @param cls Closure. | ||
695 | * @param ple information about peer, or NULL on "EOF". | ||
696 | */ | ||
697 | static void | ||
698 | peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple) | ||
699 | { | ||
700 | |||
701 | const struct GNUNET_PeerIdentity *p_id; | ||
702 | const struct GNUNET_PeerIdentity *peer; | ||
703 | |||
704 | |||
705 | peer = &ple->peer; | ||
706 | |||
707 | if (NULL == ple) | ||
708 | { | ||
709 | plo = NULL; | ||
710 | return; | ||
711 | } | ||
712 | p_id = get_from_p_ids (); | ||
713 | |||
714 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
715 | "ple->peer %s\n", | ||
716 | GNUNET_i2s_full (&ple->peer)); | ||
717 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
718 | "p_id %s\n", | ||
719 | GNUNET_i2s_full (p_id)); | ||
720 | |||
721 | if ((0 == GNUNET_memcmp (&ple->peer, p_id)) && ple->have_tunnel) | ||
722 | { | ||
723 | |||
724 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
725 | "schedule get_peers again?\n"); | ||
726 | get_peers_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
727 | &get_peers, | ||
728 | NULL); | ||
729 | |||
730 | } | ||
731 | else if (0 == GNUNET_memcmp (&ple->peer, p_id) ) | ||
732 | { | ||
733 | |||
734 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
735 | "reopen channel\n"); | ||
736 | |||
737 | reopen_channel (); | ||
738 | |||
739 | } | ||
740 | } | ||
741 | |||
742 | |||
743 | /** | ||
744 | * Function called whenever an MQ-channel is destroyed, unless the destruction | ||
745 | * was requested by #GNUNET_CADET_channel_destroy. | ||
746 | * It must NOT call #GNUNET_CADET_channel_destroy on the channel. | ||
747 | * | ||
748 | * It should clean up any associated state, including cancelling any pending | ||
749 | * transmission on this channel. | ||
750 | * | ||
751 | * @param cls Channel closure (channel wrapper). | ||
752 | * @param channel Connection to the other end (henceforth invalid). | ||
753 | */ | ||
754 | static void | ||
755 | disconnect_handler (void *cls, | ||
756 | const struct GNUNET_CADET_Channel *channel) | ||
757 | { | ||
758 | struct CadetTestChannelWrapper *ch_w = cls; | ||
759 | |||
760 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
761 | "Channel disconnected at ok=%d\n", | ||
762 | ok); | ||
763 | GNUNET_assert (ch_w->ch == channel); | ||
764 | |||
765 | if ((DESTROY == test) && (3 == ok)) | ||
766 | { | ||
767 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
768 | "Reopen channel task!\n"); | ||
769 | if (NULL == get_peers_task) | ||
770 | { | ||
771 | get_peers_task = GNUNET_SCHEDULER_add_now (&get_peers, | ||
772 | NULL); | ||
773 | } | ||
774 | return; | ||
775 | } | ||
776 | |||
777 | if (channel == incoming_ch) | ||
778 | { | ||
779 | ok++; | ||
780 | incoming_ch = NULL; | ||
781 | } | ||
782 | else if (outgoing_ch == channel) | ||
783 | { | ||
784 | if (P2P_SIGNAL == test) | ||
785 | { | ||
786 | ok++; | ||
787 | } | ||
788 | outgoing_ch = NULL; | ||
789 | } | ||
790 | else | ||
791 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
792 | "Unknown channel! %p\n", | ||
793 | channel); | ||
794 | if ((NULL != disconnect_task) && (REOPEN != test)) | ||
795 | { | ||
796 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
797 | disconnect_task = | ||
798 | GNUNET_SCHEDULER_add_now (&gather_stats_and_exit, | ||
799 | (void *) __LINE__); | ||
800 | } | ||
801 | else if ((NULL != reconnect_task) && (REOPEN == test)) | ||
802 | { | ||
803 | GNUNET_SCHEDULER_cancel (reconnect_task); | ||
804 | reconnect_task = | ||
805 | GNUNET_SCHEDULER_add_now (&reconnect_op, | ||
806 | (void *) __LINE__); | ||
807 | } | ||
808 | GNUNET_free (ch_w); | ||
809 | } | ||
810 | |||
811 | |||
812 | /** | ||
813 | * Abort test: schedule disconnect and shutdown immediately | ||
814 | * | ||
815 | * @param line Line in the code the abort is requested from (__LINE__). | ||
816 | */ | ||
817 | static void | ||
818 | abort_test (long line) | ||
819 | { | ||
820 | if (NULL != disconnect_task) | ||
821 | { | ||
822 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
823 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
824 | "Aborting test from %ld\n", | ||
825 | line); | ||
826 | disconnect_task = | ||
827 | GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
828 | (void *) line); | ||
829 | } | ||
830 | } | ||
831 | |||
832 | |||
833 | /** | ||
834 | * Send a message on the channel with the appropriate size and payload. | ||
835 | * | ||
836 | * Update the appropriate *_sent counter. | ||
837 | * | ||
838 | * @param channel Channel to send the message on. | ||
839 | */ | ||
840 | static void | ||
841 | send_test_message (struct GNUNET_CADET_Channel *channel) | ||
842 | { | ||
843 | struct GNUNET_MQ_Envelope *env; | ||
844 | struct GNUNET_MessageHeader *msg; | ||
845 | uint32_t *data; | ||
846 | int payload; | ||
847 | int size; | ||
848 | |||
849 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
850 | "Sending test message on channel %u\n", | ||
851 | channel->ccn.channel_of_client); | ||
852 | size = size_payload; | ||
853 | if (GNUNET_NO == initialized) | ||
854 | { | ||
855 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n"); | ||
856 | size += 1000; | ||
857 | payload = data_sent; | ||
858 | if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer | ||
859 | data_sent++; | ||
860 | } | ||
861 | else if ((SPEED == test) || (SPEED_ACK == test)) | ||
862 | { | ||
863 | if (get_target_channel () == channel) | ||
864 | { | ||
865 | payload = ack_sent; | ||
866 | size += ack_sent; | ||
867 | ack_sent++; | ||
868 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
869 | "Sending ACK %u [%d bytes]\n", | ||
870 | payload, size); | ||
871 | } | ||
872 | else | ||
873 | { | ||
874 | payload = data_sent; | ||
875 | size += data_sent; | ||
876 | data_sent++; | ||
877 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
878 | "Sending DATA %u [%d bytes]\n", | ||
879 | data_sent, size); | ||
880 | } | ||
881 | } | ||
882 | else if (FORWARD == test) | ||
883 | { | ||
884 | payload = ack_sent; | ||
885 | } | ||
886 | else if (P2P_SIGNAL == test) | ||
887 | { | ||
888 | payload = data_sent; | ||
889 | } | ||
890 | else if (REOPEN == test) | ||
891 | { | ||
892 | payload = data_sent; | ||
893 | data_sent++; | ||
894 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
895 | "Sending DATA %u [%d bytes]\n", | ||
896 | data_sent, size); | ||
897 | } | ||
898 | else if (DESTROY == test) | ||
899 | { | ||
900 | payload = data_sent; | ||
901 | } | ||
902 | else | ||
903 | { | ||
904 | GNUNET_assert (0); | ||
905 | } | ||
906 | env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY); | ||
907 | |||
908 | data = (uint32_t *) &msg[1]; | ||
909 | *data = htonl (payload); | ||
910 | GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env); | ||
911 | } | ||
912 | |||
913 | |||
914 | /** | ||
915 | * Task to request a new data transmission in a SPEED test, without waiting | ||
916 | * for previous messages to be sent/arrrive. | ||
917 | * | ||
918 | * @param cls Closure (unused). | ||
919 | */ | ||
920 | static void | ||
921 | send_next_msg (void *cls) | ||
922 | { | ||
923 | struct GNUNET_CADET_Channel *channel; | ||
924 | |||
925 | send_next_msg_task = NULL; | ||
926 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
927 | "Sending next message: %d\n", | ||
928 | data_sent); | ||
929 | |||
930 | channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch; | ||
931 | GNUNET_assert (NULL != channel); | ||
932 | GNUNET_assert (SPEED == test); | ||
933 | send_test_message (channel); | ||
934 | if (data_sent < total_packets) | ||
935 | { | ||
936 | /* SPEED test: Send all messages as soon as possible */ | ||
937 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
938 | "Scheduling message %d\n", | ||
939 | data_sent + 1); | ||
940 | send_next_msg_task = | ||
941 | GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL, | ||
942 | &send_next_msg, | ||
943 | NULL); | ||
944 | } | ||
945 | } | ||
946 | |||
947 | |||
948 | /** | ||
949 | * Every few messages cancel the timeout task and re-schedule it again, to | ||
950 | * avoid timing out when traffic keeps coming. | ||
951 | * | ||
952 | * @param line Code line number to log if a timeout occurs. | ||
953 | */ | ||
954 | static void | ||
955 | reschedule_timeout_task (long line) | ||
956 | { | ||
957 | if ((ok % 10) == 0) | ||
958 | { | ||
959 | if (NULL != disconnect_task) | ||
960 | { | ||
961 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
962 | "reschedule timeout every 10 messages\n"); | ||
963 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
964 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
965 | &gather_stats_and_exit, | ||
966 | (void *) line); | ||
967 | } | ||
968 | } | ||
969 | } | ||
970 | |||
971 | |||
972 | /** | ||
973 | * Check if payload is sane (size contains payload). | ||
974 | * | ||
975 | * @param cls should match #ch | ||
976 | * @param message The actual message. | ||
977 | * @return #GNUNET_OK to keep the channel open, | ||
978 | * #GNUNET_SYSERR to close it (signal serious error). | ||
979 | */ | ||
980 | static int | ||
981 | check_data (void *cls, | ||
982 | const struct GNUNET_MessageHeader *message) | ||
983 | { | ||
984 | return GNUNET_OK; /* all is well-formed */ | ||
985 | } | ||
986 | |||
987 | |||
988 | /** | ||
989 | * Function is called whenever a message is received. | ||
990 | * | ||
991 | * @param cls closure (set from GNUNET_CADET_connect(), peer number) | ||
992 | * @param message the actual message | ||
993 | */ | ||
994 | static void | ||
995 | handle_data (void *cls, | ||
996 | const struct GNUNET_MessageHeader *message) | ||
997 | { | ||
998 | struct CadetTestChannelWrapper *ch = cls; | ||
999 | struct GNUNET_CADET_Channel *channel = ch->ch; | ||
1000 | uint32_t *data; | ||
1001 | uint32_t payload; | ||
1002 | int *counter; | ||
1003 | |||
1004 | ok++; | ||
1005 | GNUNET_CADET_receive_done (channel); | ||
1006 | counter = get_target_channel () == channel ? &data_received : &ack_received; | ||
1007 | |||
1008 | reschedule_timeout_task ((long) __LINE__); | ||
1009 | |||
1010 | if (channel == outgoing_ch) | ||
1011 | { | ||
1012 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1013 | "Root client got a message.\n"); | ||
1014 | } | ||
1015 | else if (channel == incoming_ch) | ||
1016 | { | ||
1017 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1018 | "Leaf client got a message.\n"); | ||
1019 | } | ||
1020 | else | ||
1021 | { | ||
1022 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1023 | "Unknown channel %p.\n", | ||
1024 | channel); | ||
1025 | GNUNET_assert (0); | ||
1026 | } | ||
1027 | |||
1028 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1029 | "handle_data ok: (%d/%d)\n", | ||
1030 | ok, | ||
1031 | ok_goal); | ||
1032 | data = (uint32_t *) &message[1]; | ||
1033 | payload = ntohl (*data); | ||
1034 | if (payload == *counter) | ||
1035 | { | ||
1036 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1037 | " payload as expected: %u\n", | ||
1038 | payload); | ||
1039 | } | ||
1040 | else | ||
1041 | { | ||
1042 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1043 | " payload %u, expected: %u\n", | ||
1044 | payload, *counter); | ||
1045 | } | ||
1046 | |||
1047 | if (DESTROY == test) | ||
1048 | { | ||
1049 | if (2 == ok) | ||
1050 | { | ||
1051 | ok++; | ||
1052 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1053 | "dropping message ok: (%d/%d)\n", | ||
1054 | ok, | ||
1055 | ok_goal); | ||
1056 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1057 | "TEST ID 0: %s\n", | ||
1058 | GNUNET_i2s (testpeer_id[0])); | ||
1059 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1060 | "TEST ID 1: %s\n", | ||
1061 | GNUNET_i2s (testpeer_id[1])); | ||
1062 | |||
1063 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "dropping message\n"); | ||
1064 | GNUNET_CADET_drop_message (GNUNET_CADET_get_mq (outgoing_ch), | ||
1065 | outgoing_ch->ccn, | ||
1066 | GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); | ||
1067 | if (NULL != outgoing_ch) | ||
1068 | { | ||
1069 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1070 | "Destroying channel %p...\n", | ||
1071 | outgoing_ch); | ||
1072 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
1073 | outgoing_ch = NULL; | ||
1074 | } | ||
1075 | } | ||
1076 | else if (5 == ok) | ||
1077 | { | ||
1078 | ok++; | ||
1079 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1080 | "destroy test finished ok: (%d/%d)\n", | ||
1081 | ok, | ||
1082 | ok_goal); | ||
1083 | disconnect_task = | ||
1084 | GNUNET_SCHEDULER_add_now (&gather_stats_and_exit, | ||
1085 | (void *) __LINE__); | ||
1086 | // End of DESTROY test. | ||
1087 | } | ||
1088 | } | ||
1089 | |||
1090 | if (GNUNET_NO == initialized) | ||
1091 | { | ||
1092 | initialized = GNUNET_YES; | ||
1093 | start_time = GNUNET_TIME_absolute_get (); | ||
1094 | if (SPEED == test) | ||
1095 | { | ||
1096 | GNUNET_assert (incoming_ch == channel); | ||
1097 | send_next_msg_task = GNUNET_SCHEDULER_add_now (&send_next_msg, | ||
1098 | NULL); | ||
1099 | return; | ||
1100 | } | ||
1101 | } | ||
1102 | |||
1103 | (*counter)++; | ||
1104 | if (get_target_channel () == channel) /* Got "data" */ | ||
1105 | { | ||
1106 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received); | ||
1107 | if ((DESTROY != test) && ((SPEED != test) || ( (ok_goal - 2) == ok)) ) | ||
1108 | { | ||
1109 | /* Send ACK */ | ||
1110 | send_test_message (channel); | ||
1111 | return; | ||
1112 | } | ||
1113 | else | ||
1114 | { | ||
1115 | if (data_received < total_packets) | ||
1116 | return; | ||
1117 | } | ||
1118 | } | ||
1119 | else /* Got "ack" */ | ||
1120 | { | ||
1121 | if ((SPEED_ACK == test) || (SPEED == test) ) | ||
1122 | { | ||
1123 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received); | ||
1124 | /* Send more data */ | ||
1125 | send_test_message (channel); | ||
1126 | if ((ack_received < total_packets) && (SPEED != test) ) | ||
1127 | return; | ||
1128 | if ((ok == 2) && (SPEED == test) ) | ||
1129 | return; | ||
1130 | show_end_data (); | ||
1131 | } | ||
1132 | if (test == P2P_SIGNAL) | ||
1133 | { | ||
1134 | GNUNET_CADET_channel_destroy (incoming_ch); | ||
1135 | incoming_ch = NULL; | ||
1136 | } | ||
1137 | else | ||
1138 | { | ||
1139 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
1140 | outgoing_ch = NULL; | ||
1141 | } | ||
1142 | } | ||
1143 | } | ||
1144 | |||
1145 | |||
1146 | /** | ||
1147 | * Method called whenever a peer connects to a port in MQ-based CADET. | ||
1148 | * | ||
1149 | * @param cls Closure from #GNUNET_CADET_open_port (peer # as long). | ||
1150 | * @param channel New handle to the channel. | ||
1151 | * @param source Peer that started this channel. | ||
1152 | * @return Closure for the incoming @a channel. It's given to: | ||
1153 | * - The #GNUNET_CADET_DisconnectEventHandler (given to | ||
1154 | * #GNUNET_CADET_open_port) when the channel dies. | ||
1155 | * - Each the #GNUNET_MQ_MessageCallback handlers for each message | ||
1156 | * received on the @a channel. | ||
1157 | */ | ||
1158 | static void * | ||
1159 | connect_handler (void *cls, | ||
1160 | struct GNUNET_CADET_Channel *channel, | ||
1161 | const struct GNUNET_PeerIdentity *source) | ||
1162 | { | ||
1163 | struct CadetTestChannelWrapper *ch; | ||
1164 | long peer = (long) cls; | ||
1165 | |||
1166 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1167 | "Incoming channel from %s to %ld: %p\n", | ||
1168 | GNUNET_i2s (source), | ||
1169 | peer, | ||
1170 | channel); | ||
1171 | ok++; | ||
1172 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1173 | "connect_handler ok: (%d/%d)\n", | ||
1174 | ok, | ||
1175 | ok_goal); | ||
1176 | |||
1177 | if (peer == get_peer_nr (GNUNET_NO)) | ||
1178 | { | ||
1179 | if ((DESTROY != test) && (NULL != incoming_ch)) | ||
1180 | { | ||
1181 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1182 | "Duplicate incoming channel for client %lu\n", | ||
1183 | (long) cls); | ||
1184 | GNUNET_assert (0); | ||
1185 | } | ||
1186 | incoming_ch = channel; | ||
1187 | } | ||
1188 | else | ||
1189 | { | ||
1190 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1191 | "Incoming channel for unexpected peer #%lu\n", | ||
1192 | (long) cls); | ||
1193 | GNUNET_assert (0); | ||
1194 | } | ||
1195 | if ((NULL != disconnect_task) && (REOPEN != test) && (DESTROY != test)) | ||
1196 | { | ||
1197 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1198 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1199 | &gather_stats_and_exit, | ||
1200 | (void *) __LINE__); | ||
1201 | } | ||
1202 | else if ((NULL != disconnect_task) && (REOPEN == test)) | ||
1203 | { | ||
1204 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1205 | disconnect_task = GNUNET_SCHEDULER_add_delayed ( | ||
1206 | GNUNET_TIME_relative_multiply (short_time, 2), | ||
1207 | &gather_stats_and_exit, | ||
1208 | (void *) __LINE__); | ||
1209 | } | ||
1210 | |||
1211 | if ((NULL != reconnect_task) && (REOPEN == test)) | ||
1212 | { | ||
1213 | GNUNET_SCHEDULER_cancel (reconnect_task); | ||
1214 | reconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1215 | &reconnect_op, | ||
1216 | (void *) __LINE__); | ||
1217 | } | ||
1218 | |||
1219 | |||
1220 | /* TODO: cannot return channel as-is, in order to unify the data handlers */ | ||
1221 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
1222 | ch->ch = channel; | ||
1223 | |||
1224 | return ch; | ||
1225 | } | ||
1226 | |||
1227 | |||
1228 | /** | ||
1229 | * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES. | ||
1230 | * | ||
1231 | * Testcase continues when the root receives confirmation of connected peers, | ||
1232 | * on callback function ch. | ||
1233 | * | ||
1234 | * @param cls Closure (unused). | ||
1235 | */ | ||
1236 | static void | ||
1237 | start_test (void *cls) | ||
1238 | { | ||
1239 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
1240 | GNUNET_MQ_hd_var_size (data, | ||
1241 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
1242 | struct GNUNET_MessageHeader, | ||
1243 | NULL), | ||
1244 | GNUNET_MQ_handler_end () | ||
1245 | }; | ||
1246 | struct CadetTestChannelWrapper *ch; | ||
1247 | static struct GNUNET_CADET_Handle *h1; | ||
1248 | static struct GNUNET_PeerIdentity *p_id; | ||
1249 | |||
1250 | test_task = NULL; | ||
1251 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "start_test: %s\n", test_name); | ||
1252 | if (NULL != disconnect_task) | ||
1253 | { | ||
1254 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1255 | disconnect_task = NULL; | ||
1256 | } | ||
1257 | |||
1258 | if (SPEED_REL == test) | ||
1259 | { | ||
1260 | test = SPEED; | ||
1261 | } | ||
1262 | |||
1263 | p_id = get_from_p_ids (); | ||
1264 | h1 = get_from_cadets (); | ||
1265 | |||
1266 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
1267 | outgoing_ch = GNUNET_CADET_channel_create (h1, | ||
1268 | ch, | ||
1269 | p_id, | ||
1270 | &port, | ||
1271 | NULL, | ||
1272 | &disconnect_handler, | ||
1273 | handlers); | ||
1274 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "channel created\n"); | ||
1275 | |||
1276 | ch->ch = outgoing_ch; | ||
1277 | |||
1278 | if (DESTROY != test) | ||
1279 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1280 | &gather_stats_and_exit, | ||
1281 | (void *) __LINE__); | ||
1282 | if (KEEPALIVE == test) | ||
1283 | return; /* Don't send any data. */ | ||
1284 | |||
1285 | data_received = 0; | ||
1286 | data_sent = 0; | ||
1287 | ack_received = 0; | ||
1288 | ack_sent = 0; | ||
1289 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1290 | "Sending data initializer on channel %p...\n", | ||
1291 | outgoing_ch); | ||
1292 | send_test_message (outgoing_ch); | ||
1293 | if (REOPEN == test) | ||
1294 | { | ||
1295 | reconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1296 | &reconnect_op, | ||
1297 | (void *) __LINE__); | ||
1298 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1299 | disconnect_task = GNUNET_SCHEDULER_add_delayed ( | ||
1300 | GNUNET_TIME_relative_multiply (short_time, 2), | ||
1301 | &gather_stats_and_exit, | ||
1302 | (void *) __LINE__); | ||
1303 | } | ||
1304 | } | ||
1305 | |||
1306 | |||
1307 | /** | ||
1308 | * Callback to be called when the requested peer information is available | ||
1309 | * | ||
1310 | * @param cls the closure from GNUNET_TESTBED_peer_getinformation() | ||
1311 | * @param op the operation this callback corresponds to | ||
1312 | * @param pinfo the result; will be NULL if the operation has failed | ||
1313 | * @param emsg error message if the operation has failed; | ||
1314 | * NULL if the operation is successful | ||
1315 | */ | ||
1316 | static void | ||
1317 | pi_cb (void *cls, | ||
1318 | struct GNUNET_TESTBED_Operation *op, | ||
1319 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
1320 | const char *emsg) | ||
1321 | { | ||
1322 | long i = (long) cls; | ||
1323 | |||
1324 | if ((NULL == pinfo) || | ||
1325 | (NULL != emsg)) | ||
1326 | { | ||
1327 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1328 | "pi_cb: %s\n", | ||
1329 | emsg); | ||
1330 | abort_test (__LINE__); | ||
1331 | return; | ||
1332 | } | ||
1333 | |||
1334 | if (GNUNET_TESTBED_PIT_IDENTITY == pinfo->pit) | ||
1335 | { | ||
1336 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1337 | "ID callback for %ld\n", | ||
1338 | i); | ||
1339 | testpeer_id[i] = pinfo->result.id; | ||
1340 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1341 | "id: %s\n", | ||
1342 | GNUNET_i2s (testpeer_id[i])); | ||
1343 | } | ||
1344 | else if (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit) | ||
1345 | { | ||
1346 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1347 | "CFG callback for %ld\n", | ||
1348 | i); | ||
1349 | p_cfg[i] = pinfo->result.cfg; | ||
1350 | } | ||
1351 | else | ||
1352 | { | ||
1353 | GNUNET_break (0); | ||
1354 | } | ||
1355 | |||
1356 | peerinfo_task_cnt++; | ||
1357 | if (peerinfo_task_cnt < 4) | ||
1358 | return; | ||
1359 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1360 | "Got all peer information, starting test\n"); | ||
1361 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1362 | "TEST ID 0: %s\n", | ||
1363 | GNUNET_i2s (testpeer_id[0])); | ||
1364 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1365 | "TEST ID 1: %s\n", | ||
1366 | GNUNET_i2s (testpeer_id[1])); | ||
1367 | test_task = GNUNET_SCHEDULER_add_now (&start_test, NULL); | ||
1368 | } | ||
1369 | |||
1370 | |||
1371 | /** | ||
1372 | * test main: start test when all peers are connected | ||
1373 | * | ||
1374 | * @param cls Closure. | ||
1375 | * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end. | ||
1376 | * @param num_peers Number of peers that are running. | ||
1377 | * @param peers Array of peers. | ||
1378 | * @param cadets Handle to each of the CADETs of the peers. | ||
1379 | */ | ||
1380 | static void | ||
1381 | tmain (void *cls, | ||
1382 | struct GNUNET_CADET_TEST_Context *ctx, | ||
1383 | unsigned int num_peers, | ||
1384 | struct GNUNET_TESTBED_Peer **peers, | ||
1385 | struct GNUNET_CADET_Handle **cadets) | ||
1386 | { | ||
1387 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n"); | ||
1388 | ok = 0; | ||
1389 | test_ctx = ctx; | ||
1390 | peers_running = num_peers; | ||
1391 | GNUNET_assert (peers_running == peers_requested); | ||
1392 | testbed_peers = peers; | ||
1393 | cadets_running = cadets; | ||
1394 | |||
1395 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1396 | &disconnect_cadet_peers, | ||
1397 | (void *) __LINE__); | ||
1398 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | ||
1399 | NULL); | ||
1400 | t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], | ||
1401 | GNUNET_TESTBED_PIT_IDENTITY, | ||
1402 | &pi_cb, | ||
1403 | (void *) 0L); | ||
1404 | t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], | ||
1405 | GNUNET_TESTBED_PIT_IDENTITY, | ||
1406 | &pi_cb, | ||
1407 | (void *) 1L); | ||
1408 | t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], | ||
1409 | GNUNET_TESTBED_PIT_CONFIGURATION, | ||
1410 | &pi_cb, | ||
1411 | (void *) 0L); | ||
1412 | t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], | ||
1413 | GNUNET_TESTBED_PIT_CONFIGURATION, | ||
1414 | &pi_cb, | ||
1415 | (void *) 1L); | ||
1416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n"); | ||
1417 | } | ||
1418 | |||
1419 | |||
1420 | /** | ||
1421 | * Main: start test | ||
1422 | */ | ||
1423 | int | ||
1424 | main (int argc, char *argv[]) | ||
1425 | { | ||
1426 | static const struct GNUNET_HashCode *ports[2]; | ||
1427 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
1428 | GNUNET_MQ_hd_var_size (data, | ||
1429 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
1430 | struct GNUNET_MessageHeader, | ||
1431 | NULL), | ||
1432 | GNUNET_MQ_handler_end () | ||
1433 | }; | ||
1434 | const char *config_file; | ||
1435 | char port_id[] = "test port"; | ||
1436 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
1437 | GNUNET_GETOPT_option_relative_time ('t', | ||
1438 | "time", | ||
1439 | "short_time", | ||
1440 | gettext_noop ("set short timeout"), | ||
1441 | &short_time), | ||
1442 | GNUNET_GETOPT_option_uint ('m', | ||
1443 | "messages", | ||
1444 | "NUM_MESSAGES", | ||
1445 | gettext_noop ("set number of messages to send"), | ||
1446 | &total_packets), | ||
1447 | |||
1448 | GNUNET_GETOPT_OPTION_END | ||
1449 | }; | ||
1450 | |||
1451 | |||
1452 | initialized = GNUNET_NO; | ||
1453 | GNUNET_log_setup ("test", "DEBUG", NULL); | ||
1454 | |||
1455 | total_packets = TOTAL_PACKETS; | ||
1456 | short_time = SHORT_TIME; | ||
1457 | if (-1 == GNUNET_GETOPT_run (argv[0], options, argc, argv)) | ||
1458 | { | ||
1459 | fprintf (stderr, "test failed: problem with CLI parameters\n"); | ||
1460 | exit (1); | ||
1461 | } | ||
1462 | |||
1463 | config_file = "test_cadet.conf"; | ||
1464 | GNUNET_CRYPTO_hash (port_id, sizeof(port_id), &port); | ||
1465 | |||
1466 | /* Find out requested size */ | ||
1467 | if (strstr (argv[0], "_2_") != NULL) | ||
1468 | { | ||
1469 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n"); | ||
1470 | peers_requested = 2; | ||
1471 | } | ||
1472 | else if (strstr (argv[0], "_5_") != NULL) | ||
1473 | { | ||
1474 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n"); | ||
1475 | peers_requested = 5; | ||
1476 | } | ||
1477 | else if (strstr (argv[0], "_6_") != NULL) | ||
1478 | { | ||
1479 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "6 PEER LINE\n"); | ||
1480 | peers_requested = 6; | ||
1481 | } | ||
1482 | else | ||
1483 | { | ||
1484 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n"); | ||
1485 | peers_requested = 2; | ||
1486 | } | ||
1487 | |||
1488 | /* Find out requested test */ | ||
1489 | if (strstr (argv[0], "_forward") != NULL) | ||
1490 | { | ||
1491 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n"); | ||
1492 | test = FORWARD; | ||
1493 | test_name = "unicast"; | ||
1494 | ok_goal = 4; | ||
1495 | } | ||
1496 | else if (strstr (argv[0], "_signal") != NULL) | ||
1497 | { | ||
1498 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n"); | ||
1499 | test = P2P_SIGNAL; | ||
1500 | test_name = "signal"; | ||
1501 | ok_goal = 4; | ||
1502 | } | ||
1503 | else if (strstr (argv[0], "_speed_ack") != NULL) | ||
1504 | { | ||
1505 | /* Test is supposed to generate the following callbacks: | ||
1506 | * 1 incoming channel (@dest) | ||
1507 | * total_packets received data packet (@dest) | ||
1508 | * total_packets received data packet (@orig) | ||
1509 | * 1 received channel destroy (@dest) FIXME #5818 | ||
1510 | */ok_goal = total_packets * 2 + 2; | ||
1511 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n"); | ||
1512 | test = SPEED_ACK; | ||
1513 | test_name = "speed ack"; | ||
1514 | } | ||
1515 | else if (strstr (argv[0], "_speed") != NULL) | ||
1516 | { | ||
1517 | /* Test is supposed to generate the following callbacks: | ||
1518 | * 1 incoming channel (@dest) | ||
1519 | * 1 initial packet (@dest) | ||
1520 | * total_packets received data packet (@dest) | ||
1521 | * 1 received data packet (@orig) | ||
1522 | * 1 received channel destroy (@dest) FIXME #5818 | ||
1523 | */ok_goal = total_packets + 4; | ||
1524 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n"); | ||
1525 | if (strstr (argv[0], "_reliable") != NULL) | ||
1526 | { | ||
1527 | test = SPEED_REL; | ||
1528 | test_name = "speed reliable"; | ||
1529 | config_file = "test_cadet_drop.conf"; | ||
1530 | } | ||
1531 | else | ||
1532 | { | ||
1533 | test = SPEED; | ||
1534 | test_name = "speed"; | ||
1535 | } | ||
1536 | } | ||
1537 | else if (strstr (argv[0], "_keepalive") != NULL) | ||
1538 | { | ||
1539 | test = KEEPALIVE; | ||
1540 | test_name = "keepalive"; | ||
1541 | /* Test is supposed to generate the following callbacks: | ||
1542 | * 1 incoming channel (@dest) | ||
1543 | * [wait] | ||
1544 | * 1 received channel destroy (@dest) FIXME #5818 | ||
1545 | */ok_goal = 1; | ||
1546 | } | ||
1547 | else if (strstr (argv[0], "_reopen") != NULL) | ||
1548 | { | ||
1549 | test = REOPEN; | ||
1550 | test_name = "reopen"; | ||
1551 | ///* Test is supposed to generate the following callbacks: | ||
1552 | // * 1 incoming channel (@dest) | ||
1553 | // * [wait] | ||
1554 | // * 1 received channel destroy (@dest) FIXME #5818 | ||
1555 | // */ | ||
1556 | ok_goal = 6; | ||
1557 | } | ||
1558 | else if (strstr (argv[0], "_destroy") != NULL) | ||
1559 | { | ||
1560 | test = DESTROY; | ||
1561 | test_name = "destroy"; | ||
1562 | ok_goal = 6; | ||
1563 | short_time = GNUNET_TIME_relative_multiply (short_time, 5); | ||
1564 | } | ||
1565 | else | ||
1566 | { | ||
1567 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n"); | ||
1568 | test = SETUP; | ||
1569 | ok_goal = 0; | ||
1570 | } | ||
1571 | |||
1572 | if (strstr (argv[0], "backwards") != NULL) | ||
1573 | { | ||
1574 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n"); | ||
1575 | test_backwards = GNUNET_YES; | ||
1576 | GNUNET_asprintf (&test_name, "backwards %s", test_name); | ||
1577 | } | ||
1578 | |||
1579 | peerinfo_task_cnt = 0; | ||
1580 | ports[0] = &port; | ||
1581 | ports[1] = NULL; | ||
1582 | GNUNET_CADET_TEST_ruN ("test_cadet_small", | ||
1583 | config_file, | ||
1584 | peers_requested, | ||
1585 | &tmain, | ||
1586 | NULL, /* tmain cls */ | ||
1587 | &connect_handler, | ||
1588 | NULL, | ||
1589 | &disconnect_handler, | ||
1590 | handlers, | ||
1591 | ports); | ||
1592 | if (NULL != strstr (argv[0], "_reliable")) | ||
1593 | msg_dropped = 0; /* dropped should be retransmitted */ | ||
1594 | |||
1595 | if (ok_goal > ok - msg_dropped) | ||
1596 | { | ||
1597 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "FAILED! (%d/%d)\n", ok, ok_goal); | ||
1598 | return 1; | ||
1599 | } | ||
1600 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n"); | ||
1601 | return 0; | ||
1602 | } | ||
1603 | |||
1604 | |||
1605 | /* end of test_cadet.c */ | ||