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