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.c554
1 files changed, 554 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..68bdae69c
--- /dev/null
+++ b/src/service/transport/transport_api_cmd_backchannel_check.c
@@ -0,0 +1,554 @@
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 "gnunet_hello_lib.h"
33#include "transport-testing-cmds.h"
34
35/**
36 * Generic logging shortcut
37 */
38#define LOG(kind, ...) GNUNET_log_from (kind, "udp-backchannel",__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 * Context for our asynchronous completion.
55 */
56 struct GNUNET_TESTING_AsyncContext ac;
57
58 /**
59 * The number of the node in a network namespace.
60 */
61 unsigned int node_n;
62
63 /**
64 * The number of the network namespace.
65 */
66 unsigned int namespace_n;
67
68 /**
69 * The testing system of this node.
70 */
71 const struct GNUNET_TESTING_System *tl_system;
72
73 // Label of the cmd which started the test system.
74 const char *create_label;
75
76 /**
77 * Number globally identifying the node.
78 *
79 */
80 uint32_t num;
81
82 /**
83 * Label of the cmd to start a peer.
84 *
85 */
86 const char *start_peer_label;
87
88 /**
89 * The topology of the test setup.
90 */
91 struct GNUNET_TESTING_NetjailTopology *topology;
92
93 /**
94 * Connections to other peers.
95 */
96 struct GNUNET_TESTING_NodeConnection *node_connections_head;
97
98 /**
99 * Number of connections.
100 */
101 unsigned int con_num;
102
103 /**
104 * Number of received backchannel messages.
105 */
106 unsigned int received_backchannel_msgs;
107
108 /**
109 * Array with search strings.
110 */
111 char **search_string;
112
113 /**
114 * File handle for log file.
115 */
116 struct GNUNET_DISK_FileHandle *fh;
117
118 /**
119 * Task which handles the reading
120 */
121 struct GNUNET_SCHEDULER_Task *task;
122
123 /**
124 * Stream to read log file lines.
125 */
126 FILE *stream;
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 GNUNET_TESTING_async_finish (&cs->ac);
199 fclose (cs->stream);
200 return;
201 }
202 }
203 }
204 }
205 LOG (GNUNET_ERROR_TYPE_DEBUG,
206 "read_from_log end\n");
207 fclose (cs->stream);
208 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
209 &read_from_log,
210 cs);
211 /*if (NULL == fgets (line, MAX_LINE_LENGTH, cs->stream))
212 {
213 LOG (GNUNET_ERROR_TYPE_DEBUG,
214 "read null\n");
215 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
216 &read_from_log,
217 cs);
218 return;
219 }*/
220 /*else {
221 cs->task =
222 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
223 cs->fh,
224 &read_from_log,
225 cs);
226
227
228 }*/
229}
230
231
232static enum GNUNET_GenericReturnValue
233will_the_other_node_connect_via_udp (
234 struct CheckState *cs,
235 const struct GNUNET_TESTING_NetjailNode *node)
236// struct GNUNET_TESTING_NodeConnection *connection)
237{
238 // struct GNUNET_TESTING_NetjailTopology *topology = cs->topology;
239 // unsigned int node_n = connection->node_n;
240 // unsigned int namespace_n = connection->namespace_n;
241 // struct GNUNET_HashCode hc;
242 // struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
243 // struct GNUNET_HashCode hc_namespace;
244 /*struct GNUNET_ShortHashCode *key_namespace = GNUNET_new (struct
245 GNUNET_ShortHashCode);*/
246 // struct GNUNET_TESTING_NetjailNode *node;
247 struct GNUNET_TESTING_NodeConnection *pos_connection;
248 struct GNUNET_TESTING_AddressPrefix *pos_prefix;
249 // struct GNUNET_TESTING_NetjailNamespace *namespace;
250 // struct GNUNET_CONTAINER_MultiShortmap *map;
251
252 /* if (0 == connection->namespace_n) */
253 /* { */
254 /* map = topology->map_globals; */
255 /* } */
256 /* else */
257 /* { */
258 /* GNUNET_CRYPTO_hash (&namespace_n, sizeof(namespace_n), &hc_namespace); */
259 /* memcpy (key_namespace, */
260 /* &hc_namespace, */
261 /* sizeof (*key_namespace)); */
262 /* if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( */
263 /* topology->map_namespaces, */
264 /* key_namespace)) */
265 /* { */
266 /* namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces, */
267 /* key_namespace); */
268 /* map = namespace->nodes; */
269 /* } */
270 /* else */
271 /* GNUNET_assert (0); */
272 /* } */
273
274 /* GNUNET_CRYPTO_hash (&node_n, sizeof(node_n), &hc); */
275 /* memcpy (key, */
276 /* &hc, */
277 /* sizeof (*key)); */
278 /* if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( */
279 /* map, */
280 /* key)) */
281 /* { */
282 /* node = GNUNET_CONTAINER_multishortmap_get (cs->topology->map_globals, */
283 /* key); */
284 /* for (pos_connection = node->node_connections_head; NULL != pos_connection; */
285 /* pos_connection = pos_connection->next) */
286 /* { */
287 /* if ((node->namespace_n == pos_connection->namespace_n) && */
288 /* (node->node_n == pos_connection->node_n) ) */
289 /* { */
290 /* for (pos_prefix = pos_connection->address_prefixes_head; NULL != */
291 /* pos_prefix; */
292 /* pos_prefix = */
293 /* pos_prefix->next) */
294 /* { */
295 /* if (0 == strcmp (UDP, pos_prefix->address_prefix)) */
296 /* { */
297 /* return GNUNET_YES; */
298 /* } */
299 /* } */
300 /* } */
301 /* } */
302 /* } */
303
304 for (pos_connection = node->node_connections_head; NULL != pos_connection;
305 pos_connection = pos_connection->next)
306 {
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "connect via udp %u %u %u %u\n",
309 node->namespace_n,
310 cs->namespace_n,
311 node->node_n,
312 cs->node_n);
313 if ((pos_connection->namespace_n == cs->namespace_n) &&
314 (pos_connection->node_n == cs->node_n) )
315 {
316 for (pos_prefix = pos_connection->address_prefixes_head; NULL !=
317 pos_prefix;
318 pos_prefix =
319 pos_prefix->next)
320 {
321 if (0 == strcmp (UDP, pos_prefix->address_prefix))
322 {
323 return GNUNET_YES;
324 }
325 }
326 }
327 }
328
329 return GNUNET_NO;
330}
331
332
333static void
334add_search_string (struct CheckState *cs, const struct
335 GNUNET_TESTING_NetjailNode *node)
336{
337 unsigned int num;
338 struct GNUNET_PeerIdentity *peer;
339 struct GNUNET_PeerIdentity *us;
340 char *buf;
341 char *part_one = "Delivering backchannel message from ";
342 char *part_two = " to ";
343 char *part_three = " of type 1460 to udp";
344 char *peer_id;
345 char *us_id;
346
347 if (0 == node->namespace_n)
348 num = node->node_n;
349 else
350 num = (node->namespace_n - 1) * cs->topology->nodes_m + node->node_n
351 + cs->topology->nodes_x;
352
353 // num = GNUNET_TESTING_calculate_num (pos_connection, cs->topology);
354 peer = GNUNET_TESTING_get_peer (num, cs->tl_system);
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "peer: %s num %u\n",
357 GNUNET_i2s (peer),
358 num);
359 us = GNUNET_TESTING_get_peer (cs->num, cs->tl_system);
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "us: %s cs->num %d\n",
362 GNUNET_i2s (us),
363 cs->num);
364
365 GNUNET_asprintf (&peer_id,
366 "%s",
367 GNUNET_i2s (peer));
368 GNUNET_asprintf (&us_id,
369 "%s",
370 GNUNET_i2s (us));
371
372 if (0 < GNUNET_asprintf (&buf,
373 "%s%s%s%s%s",
374 part_one,
375 us_id,
376 part_two,
377 peer_id,
378 part_three))
379 {
380 GNUNET_array_append (cs->search_string,
381 cs->con_num,
382 buf);
383 /*LOG (GNUNET_ERROR_TYPE_DEBUG,
384 "con_num: %u search: %s %p\n",
385 cs->con_num,
386 cs->search_string[cs->con_num - 1],
387 cs->search_string);*/
388 }
389 else
390 GNUNET_assert (0);
391 GNUNET_free (peer);
392 GNUNET_free (us);
393}
394
395
396/**
397 * The run method of this cmd will connect to peers.
398 *
399 */
400static void
401backchannel_check_run (void *cls,
402 struct GNUNET_TESTING_Interpreter *is)
403{
404 struct CheckState *cs = cls;
405 const struct GNUNET_TESTING_Command *system_cmd;
406 const struct GNUNET_TESTING_System *tl_system;
407 const struct GNUNET_TESTING_Command *peer1_cmd;
408 const struct GNUNET_TRANSPORT_ApplicationHandle *ah;
409 struct GNUNET_CONTAINER_MultiShortmapIterator *node_it;
410 struct GNUNET_CONTAINER_MultiShortmapIterator *namespace_it;
411 struct GNUNET_ShortHashCode node_key;
412 struct GNUNET_ShortHashCode namespace_key;
413 const struct GNUNET_TESTING_NetjailNode *node;
414 const struct GNUNET_TESTING_NetjailNamespace *namespace;
415
416 LOG (GNUNET_ERROR_TYPE_DEBUG,
417 "check run 1\n");
418
419 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
420 cs->start_peer_label);
421 GNUNET_TRANSPORT_get_trait_application_handle (peer1_cmd,
422 &ah);
423
424 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
425 cs->create_label);
426 GNUNET_TESTING_get_trait_test_system (system_cmd,
427 &tl_system);
428
429 cs->tl_system = tl_system;
430
431 cs->node_connections_head = GNUNET_TESTING_get_connections (cs->num,
432 cs->topology);
433
434 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 "check run 2\n");
436
437
438 node_it = GNUNET_CONTAINER_multishortmap_iterator_create (
439 cs->topology->map_globals);
440
441 while (GNUNET_YES == GNUNET_CONTAINER_multishortmap_iterator_next (node_it,
442 &node_key,
443 (const
444 void**) &
445 node))
446 {
447 LOG (GNUNET_ERROR_TYPE_DEBUG,
448 "namespace_n %u node_n %u\n",
449 node->namespace_n,
450 node->node_n);
451 if (GNUNET_YES == will_the_other_node_connect_via_udp (cs, node))
452 {
453 add_search_string (cs, node);
454 }
455 }
456 GNUNET_free (node_it);
457 namespace_it = GNUNET_CONTAINER_multishortmap_iterator_create (
458 cs->topology->map_namespaces);
459 while (GNUNET_YES == GNUNET_CONTAINER_multishortmap_iterator_next (
460 namespace_it,
461 &namespace_key,
462 (const
463 void**) &namespace))
464 {
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "namespace_n %u\n",
467 node->namespace_n);
468 node_it = GNUNET_CONTAINER_multishortmap_iterator_create (
469 namespace->nodes);
470 while (GNUNET_YES == GNUNET_CONTAINER_multishortmap_iterator_next (node_it,
471 &node_key,
472 (const
473 void**)
474 &node))
475 {
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "namespace_n %u node_n %u\n",
478 node->namespace_n,
479 node->node_n);
480 if (GNUNET_YES == will_the_other_node_connect_via_udp (cs, node))
481 {
482 add_search_string (cs, node);
483 }
484 }
485 GNUNET_free (node_it);
486 }
487
488 if (0 != cs->con_num)
489 {
490 cs->task =
491 GNUNET_SCHEDULER_add_now (&read_from_log,
492 cs);
493 }
494 else
495 GNUNET_TESTING_async_finish (&cs->ac);
496
497 GNUNET_free (namespace_it);
498}
499
500
501/**
502 * Trait function of this cmd does nothing.
503 *
504 */
505static int
506backchannel_check_traits (void *cls,
507 const void **ret,
508 const char *trait,
509 unsigned int index)
510{
511 return GNUNET_OK;
512}
513
514
515/**
516 * The cleanup function of this cmd frees resources the cmd allocated.
517 *
518 */
519static void
520backchannel_check_cleanup (void *cls)
521{
522 struct ConnectPeersState *cs = cls;
523
524 GNUNET_free (cs);
525}
526
527
528struct GNUNET_TESTING_Command
529GNUNET_TRANSPORT_cmd_backchannel_check (const char *label,
530 const char *start_peer_label,
531 const char *create_label,
532 uint32_t num,
533 unsigned int node_n,
534 unsigned int namespace_n,
535 struct GNUNET_TESTING_NetjailTopology *
536 topology)
537{
538 struct CheckState *cs;
539
540 cs = GNUNET_new (struct CheckState);
541 cs->start_peer_label = start_peer_label;
542 cs->num = num;
543 cs->create_label = create_label;
544 cs->topology = topology;
545 cs->node_n = node_n;
546 cs->namespace_n = namespace_n;
547
548 return GNUNET_TESTING_command_new (cs,
549 label,
550 &backchannel_check_run,
551 &backchannel_check_cleanup,
552 &backchannel_check_traits,
553 &cs->ac);
554}