aboutsummaryrefslogtreecommitdiff
path: root/src/service/transport/transport_api_cmd_backchannel_check.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/transport/transport_api_cmd_backchannel_check.c')
-rw-r--r--src/service/transport/transport_api_cmd_backchannel_check.c553
1 files changed, 553 insertions, 0 deletions
diff --git a/src/service/transport/transport_api_cmd_backchannel_check.c b/src/service/transport/transport_api_cmd_backchannel_check.c
new file mode 100644
index 000000000..f23230798
--- /dev/null
+++ b/src/service/transport/transport_api_cmd_backchannel_check.c
@@ -0,0 +1,553 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 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/**
22 * @file testing_api_cmd_backchannel_check.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_common.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_testing_ng_lib.h"
30#include "gnunet_testing_netjail_lib.h"
31#include "gnunet_transport_application_service.h"
32#include "transport-testing-cmds.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log_from (kind, "udp-backchannel",__VA_ARGS__)
38
39#define UDP "udp"
40
41/**
42 * Maximum length allowed for line input.
43 */
44#define MAX_LINE_LENGTH 1024
45
46/**
47 * Struct to store information needed in callbacks.
48 *
49 */
50struct CheckState
51{
52 /**
53 * Context for our asynchronous completion.
54 */
55 struct GNUNET_TESTING_AsyncContext ac;
56
57 /**
58 * The number of the node in a network namespace.
59 */
60 unsigned int node_n;
61
62 /**
63 * The number of the network namespace.
64 */
65 unsigned int namespace_n;
66
67 /**
68 * The testing system of this node.
69 */
70 const struct GNUNET_TESTING_System *tl_system;
71
72 // Label of the cmd which started the test system.
73 const char *create_label;
74
75 /**
76 * Number globally identifying the node.
77 *
78 */
79 uint32_t num;
80
81 /**
82 * Label of the cmd to start a peer.
83 *
84 */
85 const char *start_peer_label;
86
87 /**
88 * The topology of the test setup.
89 */
90 struct GNUNET_TESTING_NetjailTopology *topology;
91
92 /**
93 * Connections to other peers.
94 */
95 struct GNUNET_TESTING_NodeConnection *node_connections_head;
96
97 /**
98 * Number of connections.
99 */
100 unsigned int con_num;
101
102 /**
103 * Number of received backchannel messages.
104 */
105 unsigned int received_backchannel_msgs;
106
107 /**
108 * Array with search strings.
109 */
110 char **search_string;
111
112 /**
113 * File handle for log file.
114 */
115 struct GNUNET_DISK_FileHandle *fh;
116
117 /**
118 * Task which handles the reading
119 */
120 struct GNUNET_SCHEDULER_Task *task;
121
122 /**
123 * Stream to read log file lines.
124 */
125 FILE *stream;
126};
127
128/**
129 *
130 * @param cls The cmd state CheckState.
131 */
132static void
133read_from_log (void *cls)
134{
135 struct CheckState *cs = cls;
136 char line[MAX_LINE_LENGTH + 1];
137 char *search_string;
138
139
140 LOG (GNUNET_ERROR_TYPE_DEBUG,
141 "read_from_log\n");
142
143 cs->fh = GNUNET_DISK_file_open ("test.out",
144 GNUNET_DISK_OPEN_READ,
145 GNUNET_DISK_PERM_USER_READ);
146
147 cs->task = NULL;
148
149 /* read message from line and handle it */
150 cs->stream = fdopen (cs->fh->fd, "r");
151 memset (line, 0, MAX_LINE_LENGTH + 1);
152
153 // fgets (line, MAX_LINE_LENGTH, cs->stream);
154 // while (NULL != line && 0 != strcmp (line, ""))// '\0' != line[0])
155 while (NULL != fgets (line, MAX_LINE_LENGTH, cs->stream))
156 {
157 /*LOG (GNUNET_ERROR_TYPE_DEBUG,
158 "cs->received_backchannel_msgs: %u\n",
159 cs->received_backchannel_msgs);*/
160 /*if (NULL == strstr (line, "line"))
161 LOG (GNUNET_ERROR_TYPE_DEBUG,
162 "line: %s",
163 line);*/
164
165
166 for (int i = 0; i < cs->con_num; i++)
167 {
168 search_string = cs->search_string[i];
169 /*LOG (GNUNET_ERROR_TYPE_DEBUG,
170 "search %u %u: %s %p\n",
171 i,
172 cs->con_num,
173 cs->search_string[i],
174 cs->search_string);
175 fprintf (stderr,
176 line);*/
177 if (NULL != strstr (line,
178 search_string))
179 // "Delivering backchannel message from 4TTC to F7B5 of type 1460 to udp"))
180 // cs->search_string[i]))
181 {
182 cs->received_backchannel_msgs++;
183 LOG (GNUNET_ERROR_TYPE_DEBUG,
184 "received_backchannel_msgs %u con_num %u\n",
185 cs->received_backchannel_msgs,
186 cs->con_num);
187 if (cs->received_backchannel_msgs == cs->con_num)
188 {
189 LOG (GNUNET_ERROR_TYPE_DEBUG,
190 "search finished %lu %lu %u\n",
191 strlen (cs->search_string[i]),
192 strlen (
193 "Delivering backchannel message from 4TTC to F7B5 of type 1460 to udp"),
194 strcmp (
195 "Delivering backchannel message from 4TTC to F7B5 of type 1460 to udp",
196 cs->search_string[i]));
197 GNUNET_TESTING_async_finish (&cs->ac);
198 fclose (cs->stream);
199 return;
200 }
201 }
202 }
203 }
204 LOG (GNUNET_ERROR_TYPE_DEBUG,
205 "read_from_log end\n");
206 fclose (cs->stream);
207 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
208 &read_from_log,
209 cs);
210 /*if (NULL == fgets (line, MAX_LINE_LENGTH, cs->stream))
211 {
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 "read null\n");
214 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
215 &read_from_log,
216 cs);
217 return;
218 }*/
219 /*else {
220 cs->task =
221 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
222 cs->fh,
223 &read_from_log,
224 cs);
225
226
227 }*/
228}
229
230
231static enum GNUNET_GenericReturnValue
232will_the_other_node_connect_via_udp (
233 struct CheckState *cs,
234 const struct GNUNET_TESTING_NetjailNode *node)
235// struct GNUNET_TESTING_NodeConnection *connection)
236{
237 // struct GNUNET_TESTING_NetjailTopology *topology = cs->topology;
238 // unsigned int node_n = connection->node_n;
239 // unsigned int namespace_n = connection->namespace_n;
240 // struct GNUNET_HashCode hc;
241 // struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
242 // struct GNUNET_HashCode hc_namespace;
243 /*struct GNUNET_ShortHashCode *key_namespace = GNUNET_new (struct
244 GNUNET_ShortHashCode);*/
245 // struct GNUNET_TESTING_NetjailNode *node;
246 struct GNUNET_TESTING_NodeConnection *pos_connection;
247 struct GNUNET_TESTING_AddressPrefix *pos_prefix;
248 // struct GNUNET_TESTING_NetjailNamespace *namespace;
249 // struct GNUNET_CONTAINER_MultiShortmap *map;
250
251 /* if (0 == connection->namespace_n) */
252 /* { */
253 /* map = topology->map_globals; */
254 /* } */
255 /* else */
256 /* { */
257 /* GNUNET_CRYPTO_hash (&namespace_n, sizeof(namespace_n), &hc_namespace); */
258 /* memcpy (key_namespace, */
259 /* &hc_namespace, */
260 /* sizeof (*key_namespace)); */
261 /* if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( */
262 /* topology->map_namespaces, */
263 /* key_namespace)) */
264 /* { */
265 /* namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces, */
266 /* key_namespace); */
267 /* map = namespace->nodes; */
268 /* } */
269 /* else */
270 /* GNUNET_assert (0); */
271 /* } */
272
273 /* GNUNET_CRYPTO_hash (&node_n, sizeof(node_n), &hc); */
274 /* memcpy (key, */
275 /* &hc, */
276 /* sizeof (*key)); */
277 /* if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( */
278 /* map, */
279 /* key)) */
280 /* { */
281 /* node = GNUNET_CONTAINER_multishortmap_get (cs->topology->map_globals, */
282 /* key); */
283 /* for (pos_connection = node->node_connections_head; NULL != pos_connection; */
284 /* pos_connection = pos_connection->next) */
285 /* { */
286 /* if ((node->namespace_n == pos_connection->namespace_n) && */
287 /* (node->node_n == pos_connection->node_n) ) */
288 /* { */
289 /* for (pos_prefix = pos_connection->address_prefixes_head; NULL != */
290 /* pos_prefix; */
291 /* pos_prefix = */
292 /* pos_prefix->next) */
293 /* { */
294 /* if (0 == strcmp (UDP, pos_prefix->address_prefix)) */
295 /* { */
296 /* return GNUNET_YES; */
297 /* } */
298 /* } */
299 /* } */
300 /* } */
301 /* } */
302
303 for (pos_connection = node->node_connections_head; NULL != pos_connection;
304 pos_connection = pos_connection->next)
305 {
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "connect via udp %u %u %u %u\n",
308 node->namespace_n,
309 cs->namespace_n,
310 node->node_n,
311 cs->node_n);
312 if ((pos_connection->namespace_n == cs->namespace_n) &&
313 (pos_connection->node_n == cs->node_n) )
314 {
315 for (pos_prefix = pos_connection->address_prefixes_head; NULL !=
316 pos_prefix;
317 pos_prefix =
318 pos_prefix->next)
319 {
320 if (0 == strcmp (UDP, pos_prefix->address_prefix))
321 {
322 return GNUNET_YES;
323 }
324 }
325 }
326 }
327
328 return GNUNET_NO;
329}
330
331
332static void
333add_search_string (struct CheckState *cs, const struct
334 GNUNET_TESTING_NetjailNode *node)
335{
336 unsigned int num;
337 struct GNUNET_PeerIdentity *peer;
338 struct GNUNET_PeerIdentity *us;
339 char *buf;
340 char *part_one = "Delivering backchannel message from ";
341 char *part_two = " to ";
342 char *part_three = " of type 1460 to udp";
343 char *peer_id;
344 char *us_id;
345
346 if (0 == node->namespace_n)
347 num = node->node_n;
348 else
349 num = (node->namespace_n - 1) * cs->topology->nodes_m + node->node_n
350 + cs->topology->nodes_x;
351
352 // num = GNUNET_TESTING_calculate_num (pos_connection, cs->topology);
353 peer = GNUNET_TESTING_get_peer (num, cs->tl_system);
354 LOG (GNUNET_ERROR_TYPE_DEBUG,
355 "peer: %s num %u\n",
356 GNUNET_i2s (peer),
357 num);
358 us = GNUNET_TESTING_get_peer (cs->num, cs->tl_system);
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 "us: %s cs->num %d\n",
361 GNUNET_i2s (us),
362 cs->num);
363
364 GNUNET_asprintf (&peer_id,
365 "%s",
366 GNUNET_i2s (peer));
367 GNUNET_asprintf (&us_id,
368 "%s",
369 GNUNET_i2s (us));
370
371 if (0 < GNUNET_asprintf (&buf,
372 "%s%s%s%s%s",
373 part_one,
374 us_id,
375 part_two,
376 peer_id,
377 part_three))
378 {
379 GNUNET_array_append (cs->search_string,
380 cs->con_num,
381 buf);
382 /*LOG (GNUNET_ERROR_TYPE_DEBUG,
383 "con_num: %u search: %s %p\n",
384 cs->con_num,
385 cs->search_string[cs->con_num - 1],
386 cs->search_string);*/
387 }
388 else
389 GNUNET_assert (0);
390 GNUNET_free (peer);
391 GNUNET_free (us);
392}
393
394
395/**
396 * The run method of this cmd will connect to peers.
397 *
398 */
399static void
400backchannel_check_run (void *cls,
401 struct GNUNET_TESTING_Interpreter *is)
402{
403 struct CheckState *cs = cls;
404 const struct GNUNET_TESTING_Command *system_cmd;
405 const struct GNUNET_TESTING_System *tl_system;
406 const struct GNUNET_TESTING_Command *peer1_cmd;
407 const struct GNUNET_TRANSPORT_ApplicationHandle *ah;
408 struct GNUNET_CONTAINER_MultiShortmapIterator *node_it;
409 struct GNUNET_CONTAINER_MultiShortmapIterator *namespace_it;
410 struct GNUNET_ShortHashCode node_key;
411 struct GNUNET_ShortHashCode namespace_key;
412 const struct GNUNET_TESTING_NetjailNode *node;
413 const struct GNUNET_TESTING_NetjailNamespace *namespace;
414
415 LOG (GNUNET_ERROR_TYPE_DEBUG,
416 "check run 1\n");
417
418 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
419 cs->start_peer_label);
420 GNUNET_TRANSPORT_TESTING_get_trait_application_handle (peer1_cmd,
421 &ah);
422
423 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
424 cs->create_label);
425 GNUNET_TESTING_get_trait_test_system (system_cmd,
426 &tl_system);
427
428 cs->tl_system = tl_system;
429
430 cs->node_connections_head = GNUNET_TESTING_get_connections (cs->num,
431 cs->topology);
432
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "check run 2\n");
435
436
437 node_it = GNUNET_CONTAINER_multishortmap_iterator_create (
438 cs->topology->map_globals);
439
440 while (GNUNET_YES == GNUNET_CONTAINER_multishortmap_iterator_next (node_it,
441 &node_key,
442 (const
443 void**) &
444 node))
445 {
446 LOG (GNUNET_ERROR_TYPE_DEBUG,
447 "namespace_n %u node_n %u\n",
448 node->namespace_n,
449 node->node_n);
450 if (GNUNET_YES == will_the_other_node_connect_via_udp (cs, node))
451 {
452 add_search_string (cs, node);
453 }
454 }
455 GNUNET_free (node_it);
456 namespace_it = GNUNET_CONTAINER_multishortmap_iterator_create (
457 cs->topology->map_namespaces);
458 while (GNUNET_YES == GNUNET_CONTAINER_multishortmap_iterator_next (
459 namespace_it,
460 &namespace_key,
461 (const
462 void**) &namespace))
463 {
464 LOG (GNUNET_ERROR_TYPE_DEBUG,
465 "namespace_n %u\n",
466 node->namespace_n);
467 node_it = GNUNET_CONTAINER_multishortmap_iterator_create (
468 namespace->nodes);
469 while (GNUNET_YES == GNUNET_CONTAINER_multishortmap_iterator_next (node_it,
470 &node_key,
471 (const
472 void**)
473 &node))
474 {
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "namespace_n %u node_n %u\n",
477 node->namespace_n,
478 node->node_n);
479 if (GNUNET_YES == will_the_other_node_connect_via_udp (cs, node))
480 {
481 add_search_string (cs, node);
482 }
483 }
484 GNUNET_free (node_it);
485 }
486
487 if (0 != cs->con_num)
488 {
489 cs->task =
490 GNUNET_SCHEDULER_add_now (&read_from_log,
491 cs);
492 }
493 else
494 GNUNET_TESTING_async_finish (&cs->ac);
495
496 GNUNET_free (namespace_it);
497}
498
499
500/**
501 * Trait function of this cmd does nothing.
502 *
503 */
504static int
505backchannel_check_traits (void *cls,
506 const void **ret,
507 const char *trait,
508 unsigned int index)
509{
510 return GNUNET_OK;
511}
512
513
514/**
515 * The cleanup function of this cmd frees resources the cmd allocated.
516 *
517 */
518static void
519backchannel_check_cleanup (void *cls)
520{
521 struct ConnectPeersState *cs = cls;
522
523 GNUNET_free (cs);
524}
525
526
527struct GNUNET_TESTING_Command
528GNUNET_TRANSPORT_cmd_backchannel_check (const char *label,
529 const char *start_peer_label,
530 const char *create_label,
531 uint32_t num,
532 unsigned int node_n,
533 unsigned int namespace_n,
534 struct GNUNET_TESTING_NetjailTopology *
535 topology)
536{
537 struct CheckState *cs;
538
539 cs = GNUNET_new (struct CheckState);
540 cs->start_peer_label = start_peer_label;
541 cs->num = num;
542 cs->create_label = create_label;
543 cs->topology = topology;
544 cs->node_n = node_n;
545 cs->namespace_n = namespace_n;
546
547 return GNUNET_TESTING_command_new (cs,
548 label,
549 &backchannel_check_run,
550 &backchannel_check_cleanup,
551 &backchannel_check_traits,
552 &cs->ac);
553}