-regex
[oweals/gnunet.git] / src / transport / gnunet-helper-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/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 GNUNET_TRANSPORT_WLAN_MacAddress *mac)
63 {
64
65   struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
66
67   memcpy (&macmsg.mac, (char *) mac, sizeof (struct GNUNET_TRANSPORT_WLAN_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
76 static void
77 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
78 {
79   struct sendbuf *write_pout = cls;
80   const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in;
81   size_t payload_size;
82   struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader;
83
84   in = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
85   if ( (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ||
86        (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) < ntohs (hdr->size)) )
87   {
88     fprintf (stderr, "Function stdin_send: wrong packet type or size\n");
89     exit (1);
90   }
91   payload_size = ntohs (hdr->size) - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage);
92   if ((payload_size + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) + write_pout->size) > MAXLINE * 2)
93   {
94     fprintf (stderr, "Function stdin_send: Packet too big for buffer\n");
95     exit (1);
96   }
97   memset (&newheader, 0, sizeof (newheader));
98   newheader.header.size = htons (payload_size + sizeof (newheader));
99   newheader.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
100   newheader.frame = in->frame;
101   memcpy (write_pout->buf + write_pout->size,
102           &newheader,
103           sizeof (newheader));
104   write_pout->size += sizeof (newheader);
105   memcpy (write_pout->buf + write_pout->size,
106           &in[1],
107           payload_size);
108   write_pout->size += payload_size;
109 }
110
111
112 static void
113 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
114 {
115   struct sendbuf *write_std = cls;
116   uint16_t sendsize;
117
118   sendsize = ntohs (hdr->size);
119   if ((sendsize + write_std->size) > MAXLINE * 2)
120   {
121     fprintf (stderr, "Function file_in_send: Packet too big for buffer\n");
122     exit (1);
123   }
124
125   memcpy (write_std->buf + write_std->size, hdr, sendsize);
126   write_std->size += sendsize;
127 }
128
129
130 int
131 main (int argc, char *argv[])
132 {
133   struct stat st;
134   int erg;
135   FILE *fpin = NULL;
136   FILE *fpout = NULL;
137   int fdpin;
138   int fdpout;
139   char readbuf[MAXLINE];
140   int readsize = 0;
141   struct sendbuf write_std;
142   struct sendbuf write_pout;
143   int ret = 0;
144   int maxfd = 0;
145   fd_set rfds;
146   fd_set wfds;
147   struct timeval tv;
148   int retval;
149   struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
150   struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst;
151   struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
152
153   if (2 != argc)
154   {
155     fprintf (stderr,
156              "This program must be started with the operating mode (1 or 2) as the only argument.\n");
157     return 1;
158   }
159   if ((0 != strstr (argv[1], "1")) && (0 != strstr (argv[1], "2")))
160     return 1;
161
162   //make the fifos if needed
163   if (0 != stat (FIFO_FILE1, &st))
164   {
165     if (0 == stat (FIFO_FILE2, &st))
166     {
167       fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
168       exit (1);
169     }
170     umask (0);
171     erg = mkfifo (FIFO_FILE1, 0666);
172     if (0 != erg)
173     {
174       fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
175                strerror (errno));
176       //exit(1);
177     }
178     erg = mkfifo (FIFO_FILE2, 0666);
179     if (0 != erg)
180     {
181       fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
182                strerror (errno));
183       //exit(1);
184     }
185
186   }
187   else
188   {
189     if (0 != stat (FIFO_FILE2, &st))
190     {
191       fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
192       exit (1);
193     }
194   }
195
196   if (strstr (argv[1], "1"))
197   {
198     //fprintf(stderr, "First\n");
199     first = 1;
200     fpin = fopen (FIFO_FILE1, "r");
201     if (NULL == fpin)
202     {
203       fprintf (stderr, "fopen of read FIFO_FILE1\n");
204       goto end;
205     }
206     fpout = fopen (FIFO_FILE2, "w");
207     if (NULL == fpout)
208     {
209       fprintf (stderr, "fopen of write FIFO_FILE2\n");
210       goto end;
211     }
212
213   }
214   else
215   {
216     first = 0;
217     //fprintf(stderr, "Second\n");
218     fpout = fopen (FIFO_FILE1, "w");
219     if (NULL == fpout)
220     {
221       fprintf (stderr, "fopen of write FIFO_FILE1\n");
222       goto end;
223     }
224     fpin = fopen (FIFO_FILE2, "r");
225     if (NULL == fpin)
226     {
227       fprintf (stderr, "fopen of read FIFO_FILE2\n");
228       goto end;
229     }
230
231   }
232
233   fdpin = fileno (fpin);
234   GNUNET_assert (fpin >= 0);
235
236   if (fdpin >= FD_SETSIZE)
237   {
238     fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
239              (unsigned int) FD_SETSIZE);
240     goto end;
241   }
242
243   fdpout = fileno (fpout);
244   GNUNET_assert (fdpout >= 0);
245
246   if (fdpout >= FD_SETSIZE)
247   {
248     fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
249              (unsigned int) FD_SETSIZE);
250     goto end;
251
252   }
253
254   signal (SIGINT, &sigfunc);
255   signal (SIGTERM, &sigfunc);
256
257   write_std.size = 0;
258   write_std.pos = 0;
259   write_pout.size = 0;
260   write_pout.pos = 0;
261   stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
262   file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
263
264   //Send random mac address
265   macaddr.mac[0] = 0x13;
266   macaddr.mac[1] = 0x22;
267   macaddr.mac[2] = 0x33;
268   macaddr.mac[3] = 0x44;
269   macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
270   macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
271   write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
272
273   while (0 == closeprog)
274   {
275     maxfd = -1;
276     //set timeout
277     tv.tv_sec = 5;
278     tv.tv_usec = 0;
279
280     FD_ZERO (&rfds);
281     // if output queue is empty
282     if (0 == write_pout.size)
283     {
284       FD_SET (STDIN_FILENO, &rfds);
285       maxfd = MAX (STDIN_FILENO, maxfd);
286     }
287     if (0 == write_std.size)
288     {
289       FD_SET (fdpin, &rfds);
290       maxfd = MAX (fdpin, maxfd);
291     }
292     FD_ZERO (&wfds);
293     // if there is something to write
294     if (0 < write_std.size)
295     {
296       FD_SET (STDOUT_FILENO, &wfds);
297       maxfd = MAX (maxfd, STDOUT_FILENO);
298     }
299     if (0 < write_pout.size)
300     {
301       FD_SET (fdpout, &wfds);
302       maxfd = MAX (maxfd, fdpout);
303     }
304
305     retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
306     if ((-1 == retval) && (EINTR == errno))
307       continue;
308     if (0 > retval)
309     {
310       fprintf (stderr, "select failed: %s\n", strerror (errno));
311       closeprog = 1;
312       break;
313     }
314
315     if (FD_ISSET (STDOUT_FILENO, &wfds))
316     {
317       ret =
318           write (STDOUT_FILENO, write_std.buf + write_std.pos,
319                  write_std.size - write_std.pos);
320       if (0 > ret)
321       {
322         closeprog = 1;
323         fprintf (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
324                  strerror (errno));
325         break;
326       }
327       else
328       {
329         write_std.pos += ret;
330         // check if finished
331         if (write_std.pos == write_std.size)
332         {
333           write_std.pos = 0;
334           write_std.size = 0;
335         }
336       }
337     }
338
339     if (FD_ISSET (fdpout, &wfds))
340     {
341       ret =
342           write (fdpout, write_pout.buf + write_pout.pos,
343                  write_pout.size - write_pout.pos);
344
345       if (0 > ret)
346       {
347         closeprog = 1;
348         fprintf (stderr, "Write ERROR to fdpout: %s\n", strerror (errno));
349       }
350       else
351       {
352         write_pout.pos += ret;
353         // check if finished
354         if (write_pout.pos == write_pout.size)
355         {
356           write_pout.pos = 0;
357           write_pout.size = 0;
358         }
359       }
360     }
361
362     if (FD_ISSET (STDIN_FILENO, &rfds))
363     {
364       readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
365
366       if (0 > readsize)
367       {
368         closeprog = 1;
369         fprintf (stderr, "Error reading from STDIN_FILENO: %s\n",
370                  strerror (errno));
371       }
372       else if (0 < readsize)
373       {
374         GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
375                                    GNUNET_NO, GNUNET_NO);
376
377       }
378       else
379       {
380         //eof
381         closeprog = 1;
382       }
383     }
384
385     if (FD_ISSET (fdpin, &rfds))
386     {
387       readsize = read (fdpin, readbuf, sizeof (readbuf));
388       if (0 > readsize)
389       {
390         closeprog = 1;
391         fprintf (stderr, "Error reading from fdpin: %s\n", strerror (errno));
392         break;
393       }
394       else if (0 < readsize)
395       {
396         GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
397                                    GNUNET_NO, GNUNET_NO);
398       }
399       else
400       {
401         //eof
402         closeprog = 1;
403       }
404     }
405   }
406
407   //clean up
408   GNUNET_SERVER_mst_destroy (stdin_mst);
409   GNUNET_SERVER_mst_destroy (file_in_mst);
410
411 end:
412   if (fpout != NULL)
413     fclose (fpout);
414   if (fpin != NULL)
415     fclose (fpin);
416   if (1 == first)
417   {
418     (void) unlink (FIFO_FILE1);
419     (void) unlink (FIFO_FILE2);
420   }
421   return 0;
422 }