diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-12-11 15:39:53 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-12-11 15:39:53 +0000 |
commit | a70fbf63796b1b9c88209fd4400e6303b51d21ac (patch) | |
tree | af9e1ab111543d86f003d73e4adb518b1a1c8666 /src/transport/gnunet-helper-transport-wlan-dummy.c | |
parent | 2a80e31499ae4b7d2dcdbdcac7a3604c42b357f7 (diff) | |
download | gnunet-a70fbf63796b1b9c88209fd4400e6303b51d21ac.tar.gz gnunet-a70fbf63796b1b9c88209fd4400e6303b51d21ac.zip |
renaming WLAN helper process to gnunet-helper-transport-wlan in order to satisfy naming conventions
Diffstat (limited to 'src/transport/gnunet-helper-transport-wlan-dummy.c')
-rw-r--r-- | src/transport/gnunet-helper-transport-wlan-dummy.c | 439 |
1 files changed, 439 insertions, 0 deletions
diff --git a/src/transport/gnunet-helper-transport-wlan-dummy.c b/src/transport/gnunet-helper-transport-wlan-dummy.c new file mode 100644 index 000000000..c18216b1c --- /dev/null +++ b/src/transport/gnunet-helper-transport-wlan-dummy.c | |||
@@ -0,0 +1,439 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010 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 | * @file transport/gnunet-helper-transport-wlan-dummy.c | ||
22 | * @brief helper for the testcases for plugin_transport_wlan.c | ||
23 | * @author David Brodski | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_protocols.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "plugin_transport_wlan.h" | ||
29 | |||
30 | #define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in" | ||
31 | #define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out" | ||
32 | |||
33 | #define MAXLINE 4096 | ||
34 | |||
35 | struct sendbuf | ||
36 | { | ||
37 | unsigned int pos; | ||
38 | unsigned int size; | ||
39 | char buf[MAXLINE * 2]; | ||
40 | }; | ||
41 | |||
42 | static int first; | ||
43 | |||
44 | static int closeprog; | ||
45 | |||
46 | static void | ||
47 | sigfunc (int sig) | ||
48 | { | ||
49 | closeprog = 1; | ||
50 | (void) unlink (FIFO_FILE1); | ||
51 | (void) unlink (FIFO_FILE2); | ||
52 | } | ||
53 | |||
54 | |||
55 | /** | ||
56 | * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin | ||
57 | * @param buffer pointer to buffer for the message | ||
58 | * @param mac pointer to the mac address | ||
59 | * @return number of bytes written | ||
60 | */ | ||
61 | static int | ||
62 | send_mac_to_plugin (char *buffer, struct MacAddress *mac) | ||
63 | { | ||
64 | |||
65 | struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg; | ||
66 | |||
67 | memcpy (&macmsg.mac, (char *) mac, sizeof (struct MacAddress)); | ||
68 | macmsg.hdr.size = htons (sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage)); | ||
69 | macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL); | ||
70 | |||
71 | memcpy (buffer, &macmsg, sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage)); | ||
72 | return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage); | ||
73 | } | ||
74 | |||
75 | static void | ||
76 | stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) | ||
77 | { | ||
78 | struct sendbuf *write_pout = cls; | ||
79 | int sendsize; | ||
80 | struct GNUNET_MessageHeader newheader; | ||
81 | char *to_data; | ||
82 | char *to_radiotap; | ||
83 | char *to_start; | ||
84 | |||
85 | sendsize = | ||
86 | ntohs (hdr->size) - sizeof (struct Radiotap_Send) + | ||
87 | sizeof (struct Radiotap_rx); | ||
88 | |||
89 | if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type)) | ||
90 | { | ||
91 | fprintf (stderr, "Function stdin_send: wrong packet type\n"); | ||
92 | exit (1); | ||
93 | } | ||
94 | if ((sendsize + write_pout->size) > MAXLINE * 2) | ||
95 | { | ||
96 | fprintf (stderr, "Function stdin_send: Packet too big for buffer\n"); | ||
97 | exit (1); | ||
98 | } | ||
99 | |||
100 | newheader.size = htons (sendsize); | ||
101 | newheader.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA); | ||
102 | |||
103 | to_start = write_pout->buf + write_pout->size; | ||
104 | memcpy (to_start, &newheader, sizeof (struct GNUNET_MessageHeader)); | ||
105 | write_pout->size += sizeof (struct GNUNET_MessageHeader); | ||
106 | |||
107 | to_radiotap = to_start + sizeof (struct GNUNET_MessageHeader); | ||
108 | memset (to_radiotap, 0, sizeof (struct Radiotap_rx)); | ||
109 | write_pout->size += sizeof (struct Radiotap_rx); | ||
110 | |||
111 | to_data = to_radiotap + sizeof (struct Radiotap_rx); | ||
112 | memcpy (to_data, | ||
113 | ((char *) hdr) + sizeof (struct Radiotap_Send) + | ||
114 | sizeof (struct GNUNET_MessageHeader), | ||
115 | ntohs (hdr->size) - sizeof (struct Radiotap_Send) - | ||
116 | sizeof (struct GNUNET_MessageHeader)); | ||
117 | write_pout->size += | ||
118 | ntohs (hdr->size) - sizeof (struct Radiotap_Send) - | ||
119 | sizeof (struct GNUNET_MessageHeader); | ||
120 | } | ||
121 | |||
122 | |||
123 | static void | ||
124 | file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) | ||
125 | { | ||
126 | struct sendbuf *write_std = cls; | ||
127 | uint16_t sendsize; | ||
128 | |||
129 | sendsize = ntohs (hdr->size); | ||
130 | |||
131 | if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type)) | ||
132 | { | ||
133 | fprintf (stderr, "Function file_in_send: wrong packet type\n"); | ||
134 | exit (1); | ||
135 | } | ||
136 | if ((sendsize + write_std->size) > MAXLINE * 2) | ||
137 | { | ||
138 | fprintf (stderr, "Function file_in_send: Packet too big for buffer\n"); | ||
139 | exit (1); | ||
140 | } | ||
141 | |||
142 | memcpy (write_std->buf + write_std->size, hdr, sendsize); | ||
143 | write_std->size += sendsize; | ||
144 | } | ||
145 | |||
146 | |||
147 | int | ||
148 | main (int argc, char *argv[]) | ||
149 | { | ||
150 | struct stat st; | ||
151 | int erg; | ||
152 | FILE *fpin = NULL; | ||
153 | FILE *fpout = NULL; | ||
154 | int fdpin; | ||
155 | int fdpout; | ||
156 | char readbuf[MAXLINE]; | ||
157 | int readsize = 0; | ||
158 | struct sendbuf write_std; | ||
159 | struct sendbuf write_pout; | ||
160 | int ret = 0; | ||
161 | int maxfd = 0; | ||
162 | fd_set rfds; | ||
163 | fd_set wfds; | ||
164 | struct timeval tv; | ||
165 | int retval; | ||
166 | struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst; | ||
167 | struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst; | ||
168 | struct MacAddress macaddr; | ||
169 | |||
170 | if (2 != argc) | ||
171 | { | ||
172 | fprintf (stderr, | ||
173 | "This program must be started with the operating mode (1 or 2) as the only argument.\n"); | ||
174 | return 1; | ||
175 | } | ||
176 | if ((0 != strstr (argv[1], "1")) && (0 != strstr (argv[1], "2"))) | ||
177 | return 1; | ||
178 | |||
179 | //make the fifos if needed | ||
180 | if (0 != stat (FIFO_FILE1, &st)) | ||
181 | { | ||
182 | if (0 == stat (FIFO_FILE2, &st)) | ||
183 | { | ||
184 | fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n"); | ||
185 | exit (1); | ||
186 | } | ||
187 | umask (0); | ||
188 | erg = mkfifo (FIFO_FILE1, 0666); | ||
189 | if (0 != erg) | ||
190 | { | ||
191 | fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1, | ||
192 | strerror (errno)); | ||
193 | //exit(1); | ||
194 | } | ||
195 | erg = mkfifo (FIFO_FILE2, 0666); | ||
196 | if (0 != erg) | ||
197 | { | ||
198 | fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2, | ||
199 | strerror (errno)); | ||
200 | //exit(1); | ||
201 | } | ||
202 | |||
203 | } | ||
204 | else | ||
205 | { | ||
206 | if (0 != stat (FIFO_FILE2, &st)) | ||
207 | { | ||
208 | fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n"); | ||
209 | exit (1); | ||
210 | } | ||
211 | } | ||
212 | |||
213 | if (strstr (argv[1], "1")) | ||
214 | { | ||
215 | //fprintf(stderr, "First\n"); | ||
216 | first = 1; | ||
217 | fpin = fopen (FIFO_FILE1, "r"); | ||
218 | if (NULL == fpin) | ||
219 | { | ||
220 | fprintf (stderr, "fopen of read FIFO_FILE1\n"); | ||
221 | goto end; | ||
222 | } | ||
223 | fpout = fopen (FIFO_FILE2, "w"); | ||
224 | if (NULL == fpout) | ||
225 | { | ||
226 | fprintf (stderr, "fopen of write FIFO_FILE2\n"); | ||
227 | goto end; | ||
228 | } | ||
229 | |||
230 | } | ||
231 | else | ||
232 | { | ||
233 | first = 0; | ||
234 | //fprintf(stderr, "Second\n"); | ||
235 | fpout = fopen (FIFO_FILE1, "w"); | ||
236 | if (NULL == fpout) | ||
237 | { | ||
238 | fprintf (stderr, "fopen of write FIFO_FILE1\n"); | ||
239 | goto end; | ||
240 | } | ||
241 | fpin = fopen (FIFO_FILE2, "r"); | ||
242 | if (NULL == fpin) | ||
243 | { | ||
244 | fprintf (stderr, "fopen of read FIFO_FILE2\n"); | ||
245 | goto end; | ||
246 | } | ||
247 | |||
248 | } | ||
249 | |||
250 | fdpin = fileno (fpin); | ||
251 | GNUNET_assert (fpin >= 0); | ||
252 | |||
253 | if (fdpin >= FD_SETSIZE) | ||
254 | { | ||
255 | fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin, | ||
256 | (unsigned int) FD_SETSIZE); | ||
257 | goto end; | ||
258 | } | ||
259 | |||
260 | fdpout = fileno (fpout); | ||
261 | GNUNET_assert (fdpout >= 0); | ||
262 | |||
263 | if (fdpout >= FD_SETSIZE) | ||
264 | { | ||
265 | fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout, | ||
266 | (unsigned int) FD_SETSIZE); | ||
267 | goto end; | ||
268 | |||
269 | } | ||
270 | |||
271 | signal (SIGINT, &sigfunc); | ||
272 | signal (SIGTERM, &sigfunc); | ||
273 | |||
274 | write_std.size = 0; | ||
275 | write_std.pos = 0; | ||
276 | write_pout.size = 0; | ||
277 | write_pout.pos = 0; | ||
278 | stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout); | ||
279 | file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std); | ||
280 | |||
281 | //Send random mac address | ||
282 | macaddr.mac[0] = 0x13; | ||
283 | macaddr.mac[1] = 0x22; | ||
284 | macaddr.mac[2] = 0x33; | ||
285 | macaddr.mac[3] = 0x44; | ||
286 | macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256); | ||
287 | macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256); | ||
288 | write_std.size = send_mac_to_plugin (write_std.buf, &macaddr); | ||
289 | |||
290 | while (0 == closeprog) | ||
291 | { | ||
292 | maxfd = -1; | ||
293 | //set timeout | ||
294 | tv.tv_sec = 5; | ||
295 | tv.tv_usec = 0; | ||
296 | |||
297 | FD_ZERO (&rfds); | ||
298 | // if output queue is empty | ||
299 | if (0 == write_pout.size) | ||
300 | { | ||
301 | FD_SET (STDIN_FILENO, &rfds); | ||
302 | maxfd = MAX (STDIN_FILENO, maxfd); | ||
303 | } | ||
304 | if (0 == write_std.size) | ||
305 | { | ||
306 | FD_SET (fdpin, &rfds); | ||
307 | maxfd = MAX (fdpin, maxfd); | ||
308 | } | ||
309 | FD_ZERO (&wfds); | ||
310 | // if there is something to write | ||
311 | if (0 < write_std.size) | ||
312 | { | ||
313 | FD_SET (STDOUT_FILENO, &wfds); | ||
314 | maxfd = MAX (maxfd, STDOUT_FILENO); | ||
315 | } | ||
316 | if (0 < write_pout.size) | ||
317 | { | ||
318 | FD_SET (fdpout, &wfds); | ||
319 | maxfd = MAX (maxfd, fdpout); | ||
320 | } | ||
321 | |||
322 | retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv); | ||
323 | if ((-1 == retval) && (EINTR == errno)) | ||
324 | continue; | ||
325 | if (0 > retval) | ||
326 | { | ||
327 | fprintf (stderr, "select failed: %s\n", strerror (errno)); | ||
328 | closeprog = 1; | ||
329 | break; | ||
330 | } | ||
331 | |||
332 | if (FD_ISSET (STDOUT_FILENO, &wfds)) | ||
333 | { | ||
334 | ret = | ||
335 | write (STDOUT_FILENO, write_std.buf + write_std.pos, | ||
336 | write_std.size - write_std.pos); | ||
337 | if (0 > ret) | ||
338 | { | ||
339 | closeprog = 1; | ||
340 | fprintf (stderr, "Write ERROR to STDOUT_FILENO: %s\n", | ||
341 | strerror (errno)); | ||
342 | break; | ||
343 | } | ||
344 | else | ||
345 | { | ||
346 | write_std.pos += ret; | ||
347 | // check if finished | ||
348 | if (write_std.pos == write_std.size) | ||
349 | { | ||
350 | write_std.pos = 0; | ||
351 | write_std.size = 0; | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | |||
356 | if (FD_ISSET (fdpout, &wfds)) | ||
357 | { | ||
358 | ret = | ||
359 | write (fdpout, write_pout.buf + write_pout.pos, | ||
360 | write_pout.size - write_pout.pos); | ||
361 | |||
362 | if (0 > ret) | ||
363 | { | ||
364 | closeprog = 1; | ||
365 | fprintf (stderr, "Write ERROR to fdpout: %s\n", strerror (errno)); | ||
366 | } | ||
367 | else | ||
368 | { | ||
369 | write_pout.pos += ret; | ||
370 | // check if finished | ||
371 | if (write_pout.pos == write_pout.size) | ||
372 | { | ||
373 | write_pout.pos = 0; | ||
374 | write_pout.size = 0; | ||
375 | } | ||
376 | } | ||
377 | } | ||
378 | |||
379 | if (FD_ISSET (STDIN_FILENO, &rfds)) | ||
380 | { | ||
381 | readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf)); | ||
382 | |||
383 | if (0 > readsize) | ||
384 | { | ||
385 | closeprog = 1; | ||
386 | fprintf (stderr, "Error reading from STDIN_FILENO: %s\n", | ||
387 | strerror (errno)); | ||
388 | } | ||
389 | else if (0 < readsize) | ||
390 | { | ||
391 | GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize, | ||
392 | GNUNET_NO, GNUNET_NO); | ||
393 | |||
394 | } | ||
395 | else | ||
396 | { | ||
397 | //eof | ||
398 | closeprog = 1; | ||
399 | } | ||
400 | } | ||
401 | |||
402 | if (FD_ISSET (fdpin, &rfds)) | ||
403 | { | ||
404 | readsize = read (fdpin, readbuf, sizeof (readbuf)); | ||
405 | if (0 > readsize) | ||
406 | { | ||
407 | closeprog = 1; | ||
408 | fprintf (stderr, "Error reading from fdpin: %s\n", strerror (errno)); | ||
409 | break; | ||
410 | } | ||
411 | else if (0 < readsize) | ||
412 | { | ||
413 | GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize, | ||
414 | GNUNET_NO, GNUNET_NO); | ||
415 | } | ||
416 | else | ||
417 | { | ||
418 | //eof | ||
419 | closeprog = 1; | ||
420 | } | ||
421 | } | ||
422 | } | ||
423 | |||
424 | //clean up | ||
425 | GNUNET_SERVER_mst_destroy (stdin_mst); | ||
426 | GNUNET_SERVER_mst_destroy (file_in_mst); | ||
427 | |||
428 | end: | ||
429 | if (fpout != NULL) | ||
430 | fclose (fpout); | ||
431 | if (fpin != NULL) | ||
432 | fclose (fpin); | ||
433 | if (1 == first) | ||
434 | { | ||
435 | (void) unlink (FIFO_FILE1); | ||
436 | (void) unlink (FIFO_FILE2); | ||
437 | } | ||
438 | return 0; | ||
439 | } | ||