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