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