indentation
[oweals/gnunet.git] / src / transport / test_plugin_transport_wlan_dummy.c
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/test_transport_wlan_dummy.c
22  * @brief helper for the testcases for plugin_transport_wlan.c
23  * @author David Brodski
24  */
25
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/time.h>
31 #include <sys/stat.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include <errno.h>
36 #include <resolv.h>
37 #include <string.h>
38 #include <utime.h>
39 #include <unistd.h>
40 #include <getopt.h>
41
42 #include "platform.h"
43 #include "gnunet_constants.h"
44 #include "gnunet_os_lib.h"
45 #include "gnunet_transport_plugin.h"
46 #include "transport.h"
47 #include "gnunet_util_lib.h"
48 #include "plugin_transport_wlan.h"
49 #include "gnunet_common.h"
50 #include "gnunet-transport-wlan-helper.h"
51 #include "gnunet_crypto_lib.h"
52 #include "wlan/loopback_helper.h"
53 #include "wlan/helper_common.h"
54
55 static int first;
56
57 static void
58 sigfunc (int sig)
59 {
60   closeprog = 1;
61   unlink (FIFO_FILE1);
62   unlink (FIFO_FILE2);
63 }
64
65 static void
66 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
67 {
68   struct sendbuf *write_pout = cls;
69   int sendsize;
70   struct GNUNET_MessageHeader newheader;
71   char *to_data;
72   char *to_radiotap;
73   char *to_start;
74
75   sendsize = ntohs (hdr->size) - sizeof (struct Radiotap_Send)
76       + sizeof (struct Radiotap_rx);
77
78   if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
79   {
80     fprintf (stderr, "Function stdin_send: wrong packet type\n");
81     exit (1);
82   }
83   if ((sendsize + write_pout->size) > MAXLINE * 2)
84   {
85     fprintf (stderr, "Function stdin_send: Packet too big for buffer\n");
86     exit (1);
87   }
88
89   newheader.size = htons (sendsize);
90   newheader.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
91
92   to_start = write_pout->buf + write_pout->size;
93   memcpy (to_start, &newheader, sizeof (struct GNUNET_MessageHeader));
94   write_pout->size += sizeof (struct GNUNET_MessageHeader);
95
96   to_radiotap = to_start + sizeof (struct GNUNET_MessageHeader);
97   memset (to_radiotap, 0, sizeof (struct Radiotap_rx));
98   write_pout->size += sizeof (struct Radiotap_rx);
99
100   to_data = to_radiotap + sizeof (struct Radiotap_rx);
101   memcpy (to_data,
102           ((char *) hdr) + sizeof (struct Radiotap_Send) +
103           sizeof (struct GNUNET_MessageHeader),
104           ntohs (hdr->size) - sizeof (struct Radiotap_Send) -
105           sizeof (struct GNUNET_MessageHeader));
106   write_pout->size +=
107       ntohs (hdr->size) - sizeof (struct Radiotap_Send) -
108       sizeof (struct GNUNET_MessageHeader);
109 }
110
111 static void
112 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
113 {
114   struct sendbuf *write_std = cls;
115   uint16_t sendsize;
116
117   sendsize = ntohs (hdr->size);
118
119   if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
120   {
121     fprintf (stderr, "Function file_in_send: wrong packet type\n");
122     exit (1);
123   }
124   if ((sendsize + write_std->size) > MAXLINE * 2)
125   {
126     fprintf (stderr, "Function file_in_send: Packet too big for buffer\n");
127     exit (1);
128   }
129
130   memcpy (write_std->buf + write_std->size, hdr, sendsize);
131   write_std->size += sendsize;
132 }
133
134 int closeprog;
135
136
137 int
138 testmode (int argc, char *argv[])
139 {
140   struct stat st;
141   int erg;
142
143   FILE *fpin = NULL;
144   FILE *fpout = NULL;
145
146   int fdpin;
147   int fdpout;
148
149   //make the fifos if needed
150   if (0 != stat (FIFO_FILE1, &st))
151   {
152     if (0 == stat (FIFO_FILE2, &st))
153     {
154       fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
155       exit (1);
156     }
157
158     umask (0);
159     //unlink(FIFO_FILE1);
160     //unlink(FIFO_FILE2);
161     // FIXME: use mkfifo!
162     erg = mknod (FIFO_FILE1, S_IFIFO | 0666, 0);
163     if (0 != erg)
164     {
165       fprintf (stderr, "Error at mknode1 \n");
166       //exit(1);
167     }
168     erg = mknod (FIFO_FILE2, S_IFIFO | 0666, 0);
169     if (0 != erg)
170     {
171       fprintf (stderr, "Error at mknode2 \n");
172       //exit(1);
173     }
174
175   }
176   else
177   {
178
179     if (0 != stat (FIFO_FILE2, &st))
180     {
181       fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
182       exit (1);
183     }
184
185   }
186
187   if (strstr (argv[1], "1"))
188   {
189     //fprintf(stderr, "First\n");
190     first = 1;
191     fpin = fopen (FIFO_FILE1, "r");
192     if (NULL == fpin)
193     {
194       fprintf (stderr, "fopen of read FIFO_FILE1\n");
195       goto end;
196     }
197     fpout = fopen (FIFO_FILE2, "w");
198     if (NULL == fpout)
199     {
200       fprintf (stderr, "fopen of write FIFO_FILE2\n");
201       goto end;
202     }
203
204   }
205   else
206   {
207     first = 0;
208     //fprintf(stderr, "Second\n");
209     fpout = fopen (FIFO_FILE1, "w");
210     if (NULL == fpout)
211     {
212       fprintf (stderr, "fopen of write FIFO_FILE1\n");
213       goto end;
214     }
215     fpin = fopen (FIFO_FILE2, "r");
216     if (NULL == fpin)
217     {
218       fprintf (stderr, "fopen of read FIFO_FILE2\n");
219       goto end;
220     }
221
222   }
223
224   fdpin = fileno (fpin);
225   GNUNET_assert (fpin >= 0);
226
227   if (fdpin >= FD_SETSIZE)
228   {
229     fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
230              (unsigned int) FD_SETSIZE);
231     goto end;
232   }
233
234   fdpout = fileno (fpout);
235   GNUNET_assert (fdpout >= 0);
236
237   if (fdpout >= FD_SETSIZE)
238   {
239     fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
240              (unsigned int) FD_SETSIZE);
241     goto end;
242
243   }
244
245   signal (SIGINT, &sigfunc);
246   signal (SIGTERM, &sigfunc);
247
248   char readbuf[MAXLINE];
249   int readsize = 0;
250   struct sendbuf write_std;
251
252   write_std.size = 0;
253   write_std.pos = 0;
254
255   struct sendbuf write_pout;
256
257   write_pout.size = 0;
258   write_pout.pos = 0;
259
260   int ret = 0;
261   int maxfd = 0;
262
263   fd_set rfds;
264   fd_set wfds;
265   struct timeval tv;
266   int retval;
267
268   struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
269   struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst;
270
271   stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
272   file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
273
274   //send mac first
275
276   struct MacAddress macaddr;
277
278   //Send random mac address
279   macaddr.mac[0] = 0x13;
280   macaddr.mac[1] = 0x22;
281   macaddr.mac[2] = 0x33;
282   macaddr.mac[3] = 0x44;
283   macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
284   macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
285
286   write_std.size = send_mac_to_plugin (write_std.buf, macaddr.mac);
287
288   while (0 == closeprog)
289   {
290
291     maxfd = 0;
292
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
303     }
304     if (0 == write_std.size)
305     {
306       FD_SET (fdpin, &rfds);
307       maxfd = fdpin;
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
317     if (0 < write_pout.size)
318     {
319       FD_SET (fdpout, &wfds);
320       maxfd = MAX (maxfd, fdpout);
321     }
322
323     retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
324
325     if (-1 == retval && EINTR == errno)
326     {
327       continue;
328     }
329     if (0 > retval)
330     {
331       fprintf (stderr, "select failed: %s\n", strerror (errno));
332       closeprog = 1;
333       break;
334     }
335
336     if (FD_ISSET (STDOUT_FILENO, &wfds))
337     {
338       ret = write (STDOUT_FILENO,
339                    write_std.buf + write_std.pos,
340                    write_std.size - write_std.pos);
341       if (0 > ret)
342       {
343         closeprog = 1;
344         fprintf (stderr, "Write ERROR to STDOUT\n");
345         break;
346       }
347       else
348       {
349         write_std.pos += ret;
350         // check if finished
351         if (write_std.pos == write_std.size)
352         {
353           write_std.pos = 0;
354           write_std.size = 0;
355         }
356       }
357     }
358
359     if (FD_ISSET (fdpout, &wfds))
360     {
361       ret = write (fdpout,
362                    write_pout.buf + write_pout.pos,
363                    write_pout.size - write_pout.pos);
364
365       if (0 > ret)
366       {
367         closeprog = 1;
368         fprintf (stderr, "Write ERROR to fdpout\n");
369       }
370       else
371       {
372         write_pout.pos += ret;
373         // check if finished
374         if (write_pout.pos == write_pout.size)
375         {
376           write_pout.pos = 0;
377           write_pout.size = 0;
378         }
379       }
380     }
381
382     if (FD_ISSET (STDIN_FILENO, &rfds))
383     {
384       readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
385
386       if (0 > readsize)
387       {
388         closeprog = 1;
389         fprintf (stderr, "Read ERROR to STDIN_FILENO\n");
390       }
391       else if (0 < readsize)
392       {
393         GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
394                                    GNUNET_NO, GNUNET_NO);
395
396       }
397       else
398       {
399         //eof
400         closeprog = 1;
401       }
402     }
403
404     if (FD_ISSET (fdpin, &rfds))
405     {
406       readsize = read (fdpin, readbuf, sizeof (readbuf));
407
408       if (0 > readsize)
409       {
410         closeprog = 1;
411         fprintf (stderr, "Read ERROR to fdpin: %s\n", strerror (errno));
412         break;
413       }
414       else if (0 < readsize)
415       {
416         GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
417                                    GNUNET_NO, GNUNET_NO);
418
419       }
420       else
421       {
422         //eof
423         closeprog = 1;
424       }
425     }
426
427   }
428
429   //clean up
430
431   GNUNET_SERVER_mst_destroy (stdin_mst);
432   GNUNET_SERVER_mst_destroy (file_in_mst);
433
434 end:if (fpout != NULL)
435     fclose (fpout);
436   if (fpin != NULL)
437     fclose (fpin);
438
439   if (1 == first)
440   {
441     unlink (FIFO_FILE1);
442     unlink (FIFO_FILE2);
443   }
444
445   return (0);
446 }
447
448 int
449 main (int argc, char *argv[])
450 {
451   if (2 != argc)
452   {
453     fprintf (stderr,
454              "This program must be started with the operating mode as argument.\n");
455     fprintf (stderr,
456              "Usage: options\n"
457              "options:\n"
458              "1 = first loopback file\n" "2 = second loopback file\n" "\n");
459     return 1;
460   }
461   if (strstr (argv[1], "1") || strstr (argv[1], "2"))
462     return testmode (argc, argv);
463   return 1;
464 }