fix warnings
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-receiver.c
1 /*
2    This file is part of GNUnet
3    Copyright (C) 2012 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 transport/gnunet-transport-wlan-receiver.c
23  * @brief program to send via WLAN as much as possible (to test physical/theoretical throughput)
24  * @author David Brodski
25  */
26 #include "platform.h"
27 #include "gnunet_protocols.h"
28 #include "plugin_transport_wlan.h"
29
30 int
31 main (int argc, char *argv[])
32 {
33   char msg_buf[65536];
34   unsigned long long count;
35   double bytes_per_s;
36   time_t start;
37   time_t akt;
38   ssize_t ret;
39   pid_t pid;
40   int commpipe[2]; /* This holds the fd for the input & output of the pipe */
41
42   if (2 != argc)
43   {
44     fprintf (stderr,
45              "This program must be started with the interface name as argument.\n");
46     fprintf (stderr,
47              "Usage: %s interface-name\n"
48              "e.g. %s mon0\n",
49              argv[0],
50              argv[0]);
51     return 1;
52   }
53
54   /* Setup communication pipeline first */
55   if (pipe (commpipe))
56   {
57     fprintf (stderr, "Failed to create pipe: %s\n", strerror (errno));
58     exit (1);
59   }
60
61   /* Attempt to fork and check for errors */
62   if ((pid = fork ()) == -1)
63   {
64     fprintf (stderr, "Failed to fork: %s\n", strerror (errno));
65     exit (1);
66   }
67
68   if (pid)
69   {
70     /* A positive (non-negative) PID indicates the parent process */
71     if (0 != close (commpipe[1]))  /* Close unused side of pipe (in side) */
72       fprintf (stderr, "Failed to close fd: %s\n", strerror (errno));
73     start = time (NULL);
74     count = 0;
75     while (1)
76     {
77       ret = read (commpipe[0], msg_buf, sizeof(msg_buf));
78       if (0 > ret)
79       {
80         fprintf (stderr, "read failed: %s\n", strerror (errno));
81         break;
82       }
83       count += ret;
84       akt = time (NULL);
85       if (akt - start > 30)
86       {
87         bytes_per_s = count / (akt - start);
88         bytes_per_s /= 1024;
89         printf ("recv %f kb/s\n", bytes_per_s);
90         start = akt;
91         count = 0;
92       }
93     }
94   }
95   else
96   {
97     /* A zero PID indicates that this is the child process */
98     (void) close (1);
99     if (-1 ==
100         dup2 (commpipe[1], 1))  /* Replace stdin with the in side of the pipe */
101       fprintf (stderr, "dup2 failed: %s\n", strerror (errno));
102     (void) close (commpipe[0]); /* Close unused side of pipe (in side) */
103     /* Replace the child fork with a new process */
104     if (execlp ("gnunet-helper-transport-wlan",
105                 "gnunet-helper-transport-wlan",
106                 argv[1],
107                 NULL) == -1)
108     {
109       fprintf (stderr, "Could not start gnunet-helper-transport-wlan!");
110       _exit (1);
111     }
112   }
113   return 0;
114 }