gnunet-transport-wlan-receiver.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file transport/gnunet-transport-wlan-receiver.c
  18. * @brief program to send via WLAN as much as possible (to test physical/theoretical throughput)
  19. * @author David Brodski
  20. */
  21. #include "platform.h"
  22. #include "gnunet_protocols.h"
  23. #include "plugin_transport_wlan.h"
  24. int
  25. main (int argc, char *argv[])
  26. {
  27. char msg_buf[65536];
  28. unsigned long long count;
  29. double bytes_per_s;
  30. time_t start;
  31. time_t akt;
  32. ssize_t ret;
  33. pid_t pid;
  34. int commpipe[2]; /* This holds the fd for the input & output of the pipe */
  35. if (2 != argc)
  36. {
  37. fprintf (stderr,
  38. "This program must be started with the interface name as argument.\n");
  39. fprintf (stderr,
  40. "Usage: %s interface-name\n"
  41. "e.g. %s mon0\n",
  42. argv[0],
  43. argv[0]);
  44. return 1;
  45. }
  46. /* Setup communication pipeline first */
  47. if (pipe (commpipe))
  48. {
  49. fprintf (stderr, "Failed to create pipe: %s\n", strerror (errno));
  50. exit (1);
  51. }
  52. /* Attempt to fork and check for errors */
  53. if ((pid = fork ()) == -1)
  54. {
  55. fprintf (stderr, "Failed to fork: %s\n", strerror (errno));
  56. exit (1);
  57. }
  58. if (pid)
  59. {
  60. /* A positive (non-negative) PID indicates the parent process */
  61. if (0 != close (commpipe[1])) /* Close unused side of pipe (in side) */
  62. fprintf (stderr, "Failed to close fd: %s\n", strerror (errno));
  63. start = time (NULL);
  64. count = 0;
  65. while (1)
  66. {
  67. ret = read (commpipe[0], msg_buf, sizeof(msg_buf));
  68. if (0 > ret)
  69. {
  70. fprintf (stderr, "read failed: %s\n", strerror (errno));
  71. break;
  72. }
  73. count += ret;
  74. akt = time (NULL);
  75. if (akt - start > 30)
  76. {
  77. bytes_per_s = count / (akt - start);
  78. bytes_per_s /= 1024;
  79. printf ("recv %f kb/s\n", bytes_per_s);
  80. start = akt;
  81. count = 0;
  82. }
  83. }
  84. }
  85. else
  86. {
  87. /* A zero PID indicates that this is the child process */
  88. (void) close (1);
  89. if (-1 ==
  90. dup2 (commpipe[1], 1)) /* Replace stdin with the in side of the pipe */
  91. fprintf (stderr, "dup2 failed: %s\n", strerror (errno));
  92. (void) close (commpipe[0]); /* Close unused side of pipe (in side) */
  93. /* Replace the child fork with a new process */
  94. if (execlp ("gnunet-helper-transport-wlan",
  95. "gnunet-helper-transport-wlan",
  96. argv[1],
  97. NULL) == -1)
  98. {
  99. fprintf (stderr, "Could not start gnunet-helper-transport-wlan!");
  100. _exit (1);
  101. }
  102. }
  103. return 0;
  104. }