GNUnet  0.20.0
transport_api_cmd_backchannel_check.c
Go to the documentation of this file.
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 
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_ng_lib.h"
32 #include "gnunet_hello_lib.h"
34 #include "transport-testing-cmds.h"
35 
39 #define LOG(kind, ...) GNUNET_log_from (kind, "udp-backchannel",__VA_ARGS__)
40 
41 #define UDP "udp"
42 
46 #define MAX_LINE_LENGTH 1024
47 
52 struct CheckState
53 {
58 
62  unsigned int node_n;
63 
67  unsigned int namespace_n;
68 
73 
74  // Label of the cmd which started the test system.
75  const char *create_label;
76 
81  uint32_t num;
82 
87  const char *start_peer_label;
88 
93 
98 
102  unsigned int con_num;
103 
108 
113 
118 
123 
127  FILE *stream;
128 };
129 
134 static void
135 read_from_log (void *cls)
136 {
137  struct CheckState *cs = cls;
138  char line[MAX_LINE_LENGTH + 1];
139  char *search_string;
140 
141 
143  "read_from_log\n");
144 
145  cs->fh = GNUNET_DISK_file_open ("test.out",
148 
149  cs->task = NULL;
150 
151  /* read message from line and handle it */
152  cs->stream = fdopen (cs->fh->fd, "r");
153  memset (line, 0, MAX_LINE_LENGTH + 1);
154 
155  // fgets (line, MAX_LINE_LENGTH, cs->stream);
156  // while (NULL != line && 0 != strcmp (line, ""))// '\0' != line[0])
157  while (NULL != fgets (line, MAX_LINE_LENGTH, cs->stream))
158  {
159  /*LOG (GNUNET_ERROR_TYPE_DEBUG,
160  "cs->received_backchannel_msgs: %u\n",
161  cs->received_backchannel_msgs);*/
162  /*if (NULL == strstr (line, "line"))
163  LOG (GNUNET_ERROR_TYPE_DEBUG,
164  "line: %s",
165  line);*/
166 
167 
168  for (int i = 0; i < cs->con_num; i++)
169  {
170  search_string = cs->search_string[i];
171  /*LOG (GNUNET_ERROR_TYPE_DEBUG,
172  "search %u %u: %s %p\n",
173  i,
174  cs->con_num,
175  cs->search_string[i],
176  cs->search_string);
177  fprintf (stderr,
178  line);*/
179  if (NULL != strstr (line,
180  search_string))
181  // "Delivering backchannel message from 4TTC to F7B5 of type 1460 to udp"))
182  // cs->search_string[i]))
183  {
186  "received_backchannel_msgs %u con_num %u\n",
188  cs->con_num);
189  if (cs->received_backchannel_msgs == cs->con_num)
190  {
192  "search finished %lu %lu %u\n",
193  strlen (cs->search_string[i]),
194  strlen (
195  "Delivering backchannel message from 4TTC to F7B5 of type 1460 to udp"),
196  strcmp (
197  "Delivering backchannel message from 4TTC to F7B5 of type 1460 to udp",
198  cs->search_string[i]));
200  fclose (cs->stream);
201  return;
202  }
203  }
204  }
205  }
207  "read_from_log end\n");
208  fclose (cs->stream);
210  &read_from_log,
211  cs);
212  /*if (NULL == fgets (line, MAX_LINE_LENGTH, cs->stream))
213  {
214  LOG (GNUNET_ERROR_TYPE_DEBUG,
215  "read null\n");
216  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
217  &read_from_log,
218  cs);
219  return;
220  }*/
221  /*else {
222  cs->task =
223  GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
224  cs->fh,
225  &read_from_log,
226  cs);
227 
228 
229  }*/
230 }
231 
232 
233 static enum GNUNET_GenericReturnValue
235  struct CheckState *cs,
236  const struct GNUNET_TESTING_NetjailNode *node)
237 // struct GNUNET_TESTING_NodeConnection *connection)
238 {
239  // struct GNUNET_TESTING_NetjailTopology *topology = cs->topology;
240  // unsigned int node_n = connection->node_n;
241  // unsigned int namespace_n = connection->namespace_n;
242  // struct GNUNET_HashCode hc;
243  // struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
244  // struct GNUNET_HashCode hc_namespace;
245  /*struct GNUNET_ShortHashCode *key_namespace = GNUNET_new (struct
246  GNUNET_ShortHashCode);*/
247  // struct GNUNET_TESTING_NetjailNode *node;
248  struct GNUNET_TESTING_NodeConnection *pos_connection;
249  struct GNUNET_TESTING_AddressPrefix *pos_prefix;
250  // struct GNUNET_TESTING_NetjailNamespace *namespace;
251  // struct GNUNET_CONTAINER_MultiShortmap *map;
252 
253  /* if (0 == connection->namespace_n) */
254  /* { */
255  /* map = topology->map_globals; */
256  /* } */
257  /* else */
258  /* { */
259  /* GNUNET_CRYPTO_hash (&namespace_n, sizeof(namespace_n), &hc_namespace); */
260  /* memcpy (key_namespace, */
261  /* &hc_namespace, */
262  /* sizeof (*key_namespace)); */
263  /* if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( */
264  /* topology->map_namespaces, */
265  /* key_namespace)) */
266  /* { */
267  /* namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces, */
268  /* key_namespace); */
269  /* map = namespace->nodes; */
270  /* } */
271  /* else */
272  /* GNUNET_assert (0); */
273  /* } */
274 
275  /* GNUNET_CRYPTO_hash (&node_n, sizeof(node_n), &hc); */
276  /* memcpy (key, */
277  /* &hc, */
278  /* sizeof (*key)); */
279  /* if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( */
280  /* map, */
281  /* key)) */
282  /* { */
283  /* node = GNUNET_CONTAINER_multishortmap_get (cs->topology->map_globals, */
284  /* key); */
285  /* for (pos_connection = node->node_connections_head; NULL != pos_connection; */
286  /* pos_connection = pos_connection->next) */
287  /* { */
288  /* if ((node->namespace_n == pos_connection->namespace_n) && */
289  /* (node->node_n == pos_connection->node_n) ) */
290  /* { */
291  /* for (pos_prefix = pos_connection->address_prefixes_head; NULL != */
292  /* pos_prefix; */
293  /* pos_prefix = */
294  /* pos_prefix->next) */
295  /* { */
296  /* if (0 == strcmp (UDP, pos_prefix->address_prefix)) */
297  /* { */
298  /* return GNUNET_YES; */
299  /* } */
300  /* } */
301  /* } */
302  /* } */
303  /* } */
304 
305  for (pos_connection = node->node_connections_head; NULL != pos_connection;
306  pos_connection = pos_connection->next)
307  {
309  "connect via udp %u %u %u %u\n",
310  node->namespace_n,
311  cs->namespace_n,
312  node->node_n,
313  cs->node_n);
314  if ((pos_connection->namespace_n == cs->namespace_n) &&
315  (pos_connection->node_n == cs->node_n) )
316  {
317  for (pos_prefix = pos_connection->address_prefixes_head; NULL !=
318  pos_prefix;
319  pos_prefix =
320  pos_prefix->next)
321  {
322  if (0 == strcmp (UDP, pos_prefix->address_prefix))
323  {
324  return GNUNET_YES;
325  }
326  }
327  }
328  }
329 
330  return GNUNET_NO;
331 }
332 
333 
334 static void
335 add_search_string (struct CheckState *cs, const struct
337 {
338  unsigned int num;
339  struct GNUNET_PeerIdentity *peer;
340  struct GNUNET_PeerIdentity *us;
341  char *buf;
342  char *part_one = "Delivering backchannel message from ";
343  char *part_two = " to ";
344  char *part_three = " of type 1460 to udp";
345  char *peer_id;
346  char *us_id;
347 
348  if (0 == node->namespace_n)
349  num = node->node_n;
350  else
351  num = (node->namespace_n - 1) * cs->topology->nodes_m + node->node_n
352  + cs->topology->nodes_x;
353 
354  // num = GNUNET_TESTING_calculate_num (pos_connection, cs->topology);
357  "peer: %s num %u\n",
358  GNUNET_i2s (peer),
359  num);
360  us = GNUNET_TESTING_get_peer (cs->num, cs->tl_system);
362  "us: %s cs->num %d\n",
363  GNUNET_i2s (us),
364  cs->num);
365 
367  "%s",
368  GNUNET_i2s (peer));
369  GNUNET_asprintf (&us_id,
370  "%s",
371  GNUNET_i2s (us));
372 
373  if (0 < GNUNET_asprintf (&buf,
374  "%s%s%s%s%s",
375  part_one,
376  us_id,
377  part_two,
378  peer_id,
379  part_three))
380  {
382  cs->con_num,
383  buf);
384  /*LOG (GNUNET_ERROR_TYPE_DEBUG,
385  "con_num: %u search: %s %p\n",
386  cs->con_num,
387  cs->search_string[cs->con_num - 1],
388  cs->search_string);*/
389  }
390  else
391  GNUNET_assert (0);
392  GNUNET_free (peer);
393  GNUNET_free (us);
394 }
395 
396 
401 static void
404 {
405  struct CheckState *cs = cls;
406  const struct GNUNET_TESTING_Command *system_cmd;
407  const struct GNUNET_TESTING_System *tl_system;
408  const struct GNUNET_TESTING_Command *peer1_cmd;
411  struct GNUNET_CONTAINER_MultiShortmapIterator *namespace_it;
412  struct GNUNET_ShortHashCode node_key;
413  struct GNUNET_ShortHashCode namespace_key;
414  const struct GNUNET_TESTING_NetjailNode *node;
415  const struct GNUNET_TESTING_NetjailNamespace *namespace;
416 
418  "check run 1\n");
419 
421  cs->start_peer_label);
423  &ah);
424 
426  cs->create_label);
428  &tl_system);
429 
430  cs->tl_system = tl_system;
431 
433  cs->topology);
434 
436  "check run 2\n");
437 
438 
440  cs->topology->map_globals);
441 
443  &node_key,
444  (const
445  void**) &
446  node))
447  {
449  "namespace_n %u node_n %u\n",
450  node->namespace_n,
451  node->node_n);
453  {
454  add_search_string (cs, node);
455  }
456  }
457  GNUNET_free (node_it);
459  cs->topology->map_namespaces);
461  namespace_it,
462  &namespace_key,
463  (const
464  void**) &namespace))
465  {
467  "namespace_n %u\n",
468  node->namespace_n);
470  namespace->nodes);
472  &node_key,
473  (const
474  void**)
475  &node))
476  {
478  "namespace_n %u node_n %u\n",
479  node->namespace_n,
480  node->node_n);
482  {
483  add_search_string (cs, node);
484  }
485  }
486  GNUNET_free (node_it);
487  }
488 
489  if (0 != cs->con_num)
490  {
491  cs->task =
493  cs);
494  }
495  else
497 
498  GNUNET_free (namespace_it);
499 }
500 
501 
506 static int
508  const void **ret,
509  const char *trait,
510  unsigned int index)
511 {
512  return GNUNET_OK;
513 }
514 
515 
520 static void
522 {
523  struct ConnectPeersState *cs = cls;
524 
525  GNUNET_free (cs);
526 }
527 
528 
531  const char *start_peer_label,
532  const char *create_label,
533  uint32_t num,
534  unsigned int node_n,
535  unsigned int namespace_n,
537  topology)
538 {
539  struct CheckState *cs;
540 
541  cs = GNUNET_new (struct CheckState);
543  cs->num = num;
545  cs->topology = topology;
546  cs->node_n = node_n;
547  cs->namespace_n = namespace_n;
548 
549  return GNUNET_TESTING_command_new (cs,
550  label,
554  &cs->ac);
555 }
enum GNUNET_TESTBED_TopologyOption topology
The topology to generate.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
struct GNUNET_TESTING_Interpreter * is
static char * line
Desired phone line (string to be converted to a hash).
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
static char buf[2048]
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Helper library for handling HELLOs.
struct GNUNET_TESTING_NodeConnection * GNUNET_TESTING_get_connections(unsigned int num, const struct GNUNET_TESTING_NetjailTopology *topology)
Get the connections to other nodes for a specific node.
Definition: testing.c:2251
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait_test_system(const struct GNUNET_TESTING_Command *cmd, const struct GNUNET_TESTING_System **ret)
struct GNUNET_PeerIdentity * GNUNET_TESTING_get_peer(unsigned int num, const struct GNUNET_TESTING_System *tl_system)
Retrieve peer identity from the test system with the unique node id.
Definition: testing.c:2276
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new(void *cls, const char *label, GNUNET_TESTING_CommandRunRoutine run, GNUNET_TESTING_CommandCleanupRoutine cleanup, GNUNET_TESTING_CommandGetTraits traits, struct GNUNET_TESTING_AsyncContext *ac)
Create a new command.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(struct GNUNET_TESTING_Interpreter *is, const char *label)
Lookup command by label.
Bandwidth allocation API for applications to interact with.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1237
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
struct GNUNET_CONTAINER_MultiShortmapIterator * GNUNET_CONTAINER_multishortmap_iterator_create(const struct GNUNET_CONTAINER_MultiShortmap *map)
Create an iterator for a multihashmap.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multishortmap_iterator_next(struct GNUNET_CONTAINER_MultiShortmapIterator *iter, struct GNUNET_ShortHashCode *key, const void **value)
Retrieve the next element from the hash map at the iterator's position.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1299
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1272
#define GNUNET_TIME_UNIT_MINUTES
One minute.
Struct to store information needed in callbacks.
unsigned int con_num
Number of connections.
char ** search_string
Array with search strings.
FILE * stream
Stream to read log file lines.
const char * start_peer_label
Label of the cmd to start a peer.
struct GNUNET_TESTING_NodeConnection * node_connections_head
Connections to other peers.
unsigned int received_backchannel_msgs
Number of received backchannel messages.
uint32_t num
Number globally identifying the node.
const struct GNUNET_TESTING_System * tl_system
The testing system of this node.
struct GNUNET_TESTING_AsyncContext ac
Context for our asynchronous completion.
unsigned int node_n
The number of the node in a network namespace.
struct GNUNET_SCHEDULER_Task * task
Task which handles the reading.
unsigned int namespace_n
The number of the network namespace.
struct GNUNET_DISK_FileHandle * fh
File handle for log file.
struct GNUNET_TESTING_NetjailTopology * topology
The topology of the test setup.
Struct to store information needed in callbacks.
Handle used to access files (and pipes).
int fd
File handle on Unix-like systems.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition: scheduler.c:136
A 256-bit hashcode.
Protocol address prefix für a connection between nodes.
struct GNUNET_TESTING_AddressPrefix * next
Pointer to the next prefix in the DLL.
char * address_prefix
The address prefix.
State each asynchronous command must have in its closure.
A command to be run by the interpreter.
char label[127+1]
Label for the command.
Global state of the interpreter, used by a command to access information about other commands.
Node in the netjail topology.
unsigned int namespace_n
The number of the subnet this node is running in.
struct GNUNET_TESTING_NodeConnection * node_connections_head
Head of the DLL with the connections which shall be established to other nodes.
unsigned int node_n
The number of this node in the subnet.
struct GNUNET_CONTAINER_MultiShortmap * map_globals
Hash map containing the global known nodes which are not natted.
unsigned int nodes_m
Number of nodes per subnet.
unsigned int nodes_x
Number of global known nodes.
struct GNUNET_CONTAINER_MultiShortmap * map_namespaces
Hash map containing the subnets (for natted nodes) of the topology.
struct GNUNET_TESTING_NodeConnection * next
Pointer to the next connection in the DLL.
unsigned int node_n
The number of the node this connection points to.
unsigned int namespace_n
The number of the subnet of the node this connection points to.
struct GNUNET_TESTING_AddressPrefix * address_prefixes_head
Head of the DLL with the address prefixes for the protocolls this node is reachable.
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testing.c:122
Handle to the TRANSPORT subsystem for application management.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
enum GNUNET_GenericReturnValue GNUNET_TRANSPORT_get_trait_application_handle(const struct GNUNET_TESTING_Command *cmd, const struct GNUNET_TRANSPORT_ApplicationHandle **ret)
static void add_search_string(struct CheckState *cs, const struct GNUNET_TESTING_NetjailNode *node)
static enum GNUNET_GenericReturnValue will_the_other_node_connect_via_udp(struct CheckState *cs, const struct GNUNET_TESTING_NetjailNode *node)
static void backchannel_check_cleanup(void *cls)
The cleanup function of this cmd frees resources the cmd allocated.
static void read_from_log(void *cls)
struct GNUNET_TESTING_Command GNUNET_TRANSPORT_cmd_backchannel_check(const char *label, const char *start_peer_label, const char *create_label, uint32_t num, unsigned int node_n, unsigned int namespace_n, struct GNUNET_TESTING_NetjailTopology *topology)
Create command.
static void backchannel_check_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
The run method of this cmd will connect to peers.
#define LOG(kind,...)
Generic logging shortcut.
static int backchannel_check_traits(void *cls, const void **ret, const char *trait, unsigned int index)
Trait function of this cmd does nothing.
#define MAX_LINE_LENGTH
Maximum length allowed for line input.