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