GNUnet  0.11.x
gnunet-cmds-helper.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 
39 #include "platform.h"
40 #include "gnunet_util_lib.h"
41 #include "gnunet_testing_lib.h"
42 #include "gnunet_testing_ng_lib.h"
43 #include "testing_cmds.h"
44 #include "gnunet_testing_plugin.h"
45 #include <zlib.h>
46 
47 
51 #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
52 
56 #define LOG_DEBUG(...) LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
57 
58 #define NODE_BASE_IP "192.168.15."
59 
60 #define KNOWN_BASE_IP "92.68.151."
61 
62 #define ROUTER_BASE_IP "92.68.150."
63 
67 struct Plugin
68 {
72  char *library_name;
73 
78 
83  char *node_ip;
84 
89  char *plugin_name;
90 
95  char *global_n;
96 
101  char *local_m;
102 
107  char *n;
108 
113  char *m;
114 };
115 
121 {
126  char *n;
127 
132  char *m;
133 
138  char *global_n;
139 
144  char *local_m;
145 };
146 
150 struct WriteContext
151 {
155  void *data;
156 
160  size_t length;
161 
165  size_t pos;
166 };
167 
176 struct Plugin *plugin;
177 
182 
187 
192 
197 
202 
207 
212 
216 static int done_reading;
217 
221 static int status;
222 
223 
229 static void
230 shutdown_task (void *cls)
231 {
232 
233  LOG_DEBUG ("Shutting down.\n");
234 
235  if (NULL != read_task_id)
236  {
238  read_task_id = NULL;
239  }
240  if (NULL != write_task_id)
241  {
242  struct WriteContext *wc;
243 
245  write_task_id = NULL;
246  GNUNET_free (wc->data);
247  GNUNET_free (wc);
248  }
249  if (NULL != stdin_fd)
251  if (NULL != stdout_fd)
254  tokenizer = NULL;
255 
256  if (NULL != test_system)
257  {
259  test_system = NULL;
260  }
261 }
262 
263 
269 static void
270 write_task (void *cls)
271 {
272  struct WriteContext *wc = cls;
273  ssize_t bytes_wrote;
274 
275  GNUNET_assert (NULL != wc);
276  write_task_id = NULL;
277  bytes_wrote = GNUNET_DISK_file_write (stdout_fd,
278  wc->data + wc->pos,
279  wc->length - wc->pos);
280  if (GNUNET_SYSERR == bytes_wrote)
281  {
283  "Cannot reply back successful initialization\n");
284  GNUNET_free (wc->data);
285  GNUNET_free (wc);
286  return;
287  }
288  wc->pos += bytes_wrote;
289  if (wc->pos == wc->length)
290  {
291  GNUNET_free (wc->data);
292  GNUNET_free (wc);
293  return;
294  }
296  stdout_fd,
297  &write_task,
298  wc);
299 }
300 
301 
306 static void
307 write_message (struct GNUNET_MessageHeader *message, size_t msg_length)
308 {
309  struct WriteContext *wc;
310 
311  wc = GNUNET_new (struct WriteContext);
312  wc->length = msg_length;
313  wc->data = message;
316  stdout_fd,
317  &write_task,
318  wc);
319 }
320 
321 
334 static int
335 tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message)
336 {
337 
338  struct NodeIdentifier *ni = cls;
339  const struct GNUNET_CMDS_HelperInit *msg;
340  struct GNUNET_CMDS_HelperReply *reply;
341  char *binary;
342  char *plugin_name;
343  size_t plugin_name_size;
344  uint16_t msize;
345  size_t msg_length;
346  char *router_ip;
347  char *node_ip;
348  unsigned int namespace_n;
349 
350  msize = ntohs (message->size);
351  if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_INIT == ntohs (message->type))
352  {
353  msg = (const struct GNUNET_CMDS_HelperInit *) message;
354  plugin_name_size = ntohs (msg->plugin_name_size);
355  if ((sizeof(struct GNUNET_CMDS_HelperInit) + plugin_name_size) > msize)
356  {
357  GNUNET_break (0);
359  "Received unexpected message -- exiting\n");
360  goto error;
361  }
364  ((char *) &msg[1]),
365  plugin_name_size + 1);
366 
367  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-cmd");
368 
369  plugin = GNUNET_new (struct Plugin);
371  NULL);
373 
374  plugin->global_n = ni->global_n;
375  plugin->local_m = ni->local_m;
376  plugin->n = ni->n;
377  plugin->m = ni->m;
378 
379  router_ip = GNUNET_malloc (strlen (ROUTER_BASE_IP) + strlen (plugin->n)
380  + 1);
381  strcpy (router_ip, ROUTER_BASE_IP);
382  strcat (router_ip, plugin->n);
383 
384  sscanf (plugin->n, "%u", &namespace_n);
385 
386  if (0 == namespace_n)
387  {
389  "known node n: %s\n",
390  plugin->n);
391  node_ip = GNUNET_malloc (strlen (KNOWN_BASE_IP) + strlen (plugin->m) + 1);
392  strcat (node_ip, KNOWN_BASE_IP);
393  }
394  else
395  {
397  "subnet node n: %s\n",
398  plugin->n);
399  node_ip = GNUNET_malloc (strlen (NODE_BASE_IP) + strlen (plugin->m) + 1);
400  strcat (node_ip, NODE_BASE_IP);
401  }
402  strcat (node_ip, plugin->m);
403 
404  plugin->api->start_testcase (&write_message, router_ip, node_ip, plugin->m,
405  plugin->n, plugin->local_m);
406 
407  GNUNET_free (binary);
408 
409  msg_length = sizeof(struct GNUNET_CMDS_HelperReply);
410  reply = GNUNET_new (struct GNUNET_CMDS_HelperReply);
412  reply->header.size = htons ((uint16_t) msg_length);
413 
414  write_message ((struct GNUNET_MessageHeader *) reply, msg_length);
415 
416  return GNUNET_OK;
417  }
419  message->type))
420  {
421  plugin->api->all_peers_started ();
422  return GNUNET_OK;
423  }
424  else
425  {
426  LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
427  goto error;
428  }
429 
430 
431  error:
434  "tokenizer shutting down!\n");
436  return GNUNET_SYSERR;
437 }
438 
439 
445 static void
446 read_task (void *cls)
447 {
449  ssize_t sread;
450 
451  read_task_id = NULL;
452  sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof(buf));
453  if ((GNUNET_SYSERR == sread) || (0 == sread))
454  {
455  LOG_DEBUG ("STDIN closed\n");
457  return;
458  }
459  if (GNUNET_YES == done_reading)
460  {
461  /* didn't expect any more data! */
462  GNUNET_break_op (0);
464  "tokenizer shutting down during reading, didn't expect any more data!\n");
466  return;
467  }
468  LOG_DEBUG ("Read %u bytes\n", (unsigned int) sread);
469  /* FIXME: could introduce a GNUNET_MST_read2 to read
470  directly from 'stdin_fd' and save a memcpy() here */
471  if (GNUNET_OK !=
473  {
474  GNUNET_break (0);
476  "tokenizer shutting down during reading, writing to buffer failed!\n");
478  return;
479  }
480  read_task_id /* No timeout while reading */
482  stdin_fd,
483  &read_task,
484  NULL);
485 }
486 
487 
496 static void
497 run (void *cls,
498  char *const *args,
499  const char *cfgfile,
500  const struct GNUNET_CONFIGURATION_Handle *cfg)
501 {
502  struct NodeIdentifier *ni = cls;
503 
504  LOG_DEBUG ("Starting interpreter loop helper...\n");
505 
510  stdin_fd,
511  &read_task,
512  NULL);
514 }
515 
516 
520 static void
522 {
523  static char c;
524  int old_errno; /* back-up errno */
525 
526  old_errno = errno;
527  GNUNET_break (
528  1 ==
531  &c,
532  sizeof(c)));
533  errno = old_errno;
534 }
535 
536 
544 int
545 main (int argc, char **argv)
546 {
547  struct NodeIdentifier *ni;
551  int ret;
552 
553  GNUNET_log_setup ("gnunet-cmds-helper",
554  "DEBUG",
555  NULL);
556  ni = GNUNET_new (struct NodeIdentifier);
557  ni->global_n = argv[1];
558  ni->local_m = argv[2];
559  ni->m = argv[3];
560  ni->n = argv[4];
561 
562  status = GNUNET_OK;
563  if (NULL ==
565  {
566  GNUNET_break (0);
567  return 1;
568  }
569  shc_chld =
571  ret = GNUNET_PROGRAM_run (argc,
572  argv,
573  "gnunet-cmds-helper",
574  "Helper for starting a local interpreter loop",
575  options,
576  &run,
577  ni);
578 
580  shc_chld = NULL;
582  GNUNET_free (ni);
583  if (GNUNET_OK != ret)
584  return 1;
585  return (GNUNET_OK == status) ? 0 : 1;
586 }
587 
588 
589 /* end of gnunet-cmds-helper.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static struct GNUNET_SIGNAL_Context * shc_chld
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static void write_message(struct GNUNET_MessageHeader *message, size_t msg_length)
Callback to write a message to the master loop.
struct GNUNET_MessageStreamTokenizer * tokenizer
Our message stream tokenizer.
static void read_task(void *cls)
Task to read from stdin.
int main(int argc, char **argv)
Main function.
static struct GNUNET_DISK_FileHandle * stdout_fd
Disk handle for stdout.
static void shutdown_task(void *cls)
Task to shut down cleanly.
static int status
Result to return in case we fail.
static int tokenizer_cb(void *cls, const struct GNUNET_MessageHeader *message)
Functions with this signature are called whenever a complete message is received by the tokenizer.
#define LOG_DEBUG(...)
Debug logging shorthand.
static struct GNUNET_DISK_PipeHandle * sigpipe
Pipe used to communicate shutdown via signal.
static void sighandler_child_death()
Signal handler called for SIGCHLD.
#define NODE_BASE_IP
struct Plugin * plugin
The process handle to the testbed service.
static struct GNUNET_TESTING_System * test_system
Handle to the testing system.
static struct GNUNET_SCHEDULER_Task * read_task_id
Task identifier for the read task.
#define ROUTER_BASE_IP
static void write_task(void *cls)
Task to write to the standard out.
#define KNOWN_BASE_IP
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run.
#define LOG(kind,...)
Generic logging shortcut testing_api_cmd_block_until_all_peers_started.c.
static struct GNUNET_SCHEDULER_Task * write_task_id
Task identifier for the write task.
static struct GNUNET_DISK_FileHandle * stdin_fd
Disk handle from stdin.
static int done_reading
Are we done reading messages from stdin?
static char * plugin_name
Name of our plugin.
static char buf[2048]
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_native(FILE *fd)
Get a handle from a native FD.
Definition: disk.c:1344
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:686
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1585
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:622
const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
Get the handle to a particular pipe end.
Definition: disk.c:1616
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1442
@ GNUNET_DISK_PF_NONE
No special options, use non-blocking read/write operations.
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_OS_get_libexec_binary_path(const char *progname)
Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary, try to prefix it with the ...
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:217
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:364
#define GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED
#define GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY
The reply message from gnunet-cmds-helper.
#define GNUNET_MESSAGE_TYPE_CMDS_HELPER_INIT
The initialization message towards gnunet-cmds-helper.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:531
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1667
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1700
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1331
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:411
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:85
int GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst, const char *buf, size_t size, int purge, int one_shot)
Add incoming data to the receive buffer and call the callback for all complete messages.
Definition: mst.c:114
struct GNUNET_SIGNAL_Context * GNUNET_SIGNAL_handler_install(int signal, GNUNET_SIGNAL_Handler handler)
Install a signal handler that will be run if the given signal is received.
Definition: signal.c:51
void GNUNET_SIGNAL_handler_uninstall(struct GNUNET_SIGNAL_Context *ctx)
Uninstall a previously installed signal handler.
Definition: signal.c:77
size_t GNUNET_strlcpy(char *dst, const char *src, size_t n)
Like strlcpy but portable.
Definition: strings.c:162
void GNUNET_TESTING_system_destroy(struct GNUNET_TESTING_System *system, int remove_paths)
Free system resources.
Definition: testing.c:529
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_SIGCHLD
Definition: platform.h:42
Initialization message for gnunet-cmds-testbed to start cmd binary.
Definition: testing_cmds.h:38
Reply message from cmds helper process.
Definition: testing_cmds.h:57
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY.
Definition: testing_cmds.h:61
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition: disk.c:69
Definition of a command line option.
Header for all communications.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
Handle to a message stream tokenizer.
Definition: mst.c:44
Entry in list of pending tasks.
Definition: scheduler.c:135
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testing.c:94
Struct with information about a specific node and the whole network namespace setup.
char * m
The number of the node in the namespace.
char * n
The number of the namespace this node is in.
char * local_m
The number of local nodes per namespace.
char * global_n
The number of namespaces.
Handle for a plugin.
Definition: block.c:38
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
char * node_ip
IP address of the specific node the helper is running for.
char * m
The number of the node in the namespace.
char * n
The number of the namespace this node is in.
char * global_n
The number of namespaces.
struct GNUNET_TESTING_PluginFunctions * api
Plugin API.
char * plugin_name
Name of the test case plugin.
char * local_m
The number of local nodes per namespace.
char * library_name
Name of the shared library.
Definition: block.c:42
Context for a single write on a chunk of memory.
size_t pos
The current position from where the write operation should begin.
size_t length
The length of the data.
void * data
The data to write.
Message formats for communication between testing cmds helper and testcase plugins.