b382851234917c26db3af93e40acb87f6b06099c
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-receiver.c
1 /*
2  This file is part of GNUnet
3  (C) 2012 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  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  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.
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], argv[0]);
50     return 1;
51   }
52
53   /* Setup communication pipeline first */
54   if (pipe (commpipe))
55   {
56     fprintf (stderr,
57              "Failed to create pipe: %s\n",
58              STRERROR (errno));
59     exit (1);
60   }
61
62   /* Attempt to fork and check for errors */
63   if ((pid = fork ()) == -1)
64   {
65     fprintf (stderr, "Failed to fork: %s\n",
66              STRERROR (errno));
67     exit (1);
68   }
69
70   if (pid)
71   {
72     /* A positive (non-negative) PID indicates the parent process */
73     if (0 != close (commpipe[1]))        /* Close unused side of pipe (in side) */
74       fprintf (stderr,
75                "Failed to close fd: %s\n",
76                strerror (errno));
77     start = time (NULL);
78     count = 0;
79     while (1)
80     {
81       ret = read (commpipe[0], msg_buf, sizeof (msg_buf));
82       if (0 > ret)
83       {
84         fprintf (stderr, "read failed: %s\n", strerror (errno));
85         break;
86       }
87       count += ret;
88       akt = time (NULL);
89       if (akt - start > 30)
90       {
91         bytes_per_s = count / (akt - start);
92         bytes_per_s /= 1024;
93         printf ("recv %f kb/s\n", bytes_per_s);
94         start = akt;
95         count = 0;
96       }
97     }
98   }
99   else
100   {
101     /* A zero PID indicates that this is the child process */
102     (void) close (1);
103     if (-1 == dup2 (commpipe[1], 1))    /* Replace stdin with the in side of the pipe */
104       fprintf (stderr, "dup2 failed: %s\n", strerror (errno));
105     (void) close (commpipe[0]); /* Close unused side of pipe (in side) */
106     /* Replace the child fork with a new process */
107     if (execlp
108         ("gnunet-helper-transport-wlan", "gnunet-helper-transport-wlan",
109          argv[1], NULL) == -1)
110     {
111       fprintf (stderr, "Could not start gnunet-helper-transport-wlan!");
112       _exit (1);
113     }
114   }
115   return 0;
116 }