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