diff options
Diffstat (limited to 'src/transport/gnunet-transport-wlan-receiver.c')
-rw-r--r-- | src/transport/gnunet-transport-wlan-receiver.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/transport/gnunet-transport-wlan-receiver.c b/src/transport/gnunet-transport-wlan-receiver.c new file mode 100644 index 000000000..22ad2e20d --- /dev/null +++ b/src/transport/gnunet-transport-wlan-receiver.c | |||
@@ -0,0 +1,113 @@ | |||
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[1], msg_buf, sizeof (msg_buf)); | ||
82 | if (0 > ret) | ||
83 | break; | ||
84 | count += ret; | ||
85 | akt = time (NULL); | ||
86 | if (akt - start > 30) | ||
87 | { | ||
88 | bytes_per_s = count / (akt - start); | ||
89 | bytes_per_s /= 1024; | ||
90 | printf ("recv %f kb/s\n", bytes_per_s); | ||
91 | start = akt; | ||
92 | count = 0; | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | else | ||
97 | { | ||
98 | /* A zero PID indicates that this is the child process */ | ||
99 | (void) close (1); | ||
100 | if (-1 == 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 (execl | ||
105 | ("gnunet-helper-transport-wlan", "gnunet-helper-transport-wlan", | ||
106 | argv[1], NULL) == -1) | ||
107 | { | ||
108 | fprintf (stderr, "Could not start gnunet-helper-transport-wlan!"); | ||
109 | _exit (1); | ||
110 | } | ||
111 | } | ||
112 | return 0; | ||
113 | } | ||