-makefile for new test_stream_local (commented)
[oweals/gnunet.git] / src / transport / gnunet-helper-transport-wlan-dummy.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010, 2012 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 /**
31  * Name of the fifo to use for IPC with the other dummy process.
32  */
33 #define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in"
34
35 /**
36  * Name of the fifo to use for IPC with the other dummy process.
37  */
38 #define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out"
39
40 /**
41  * Maximum size of a message allowed in either direction
42  * (used for our receive and sent buffers).
43  */
44 #define MAXLINE 4096
45
46
47 /**
48  * IO buffer used for buffering data in transit.
49  */
50 struct SendBuffer
51 {
52
53   /**
54    * How many bytes that were stored in 'buf' did we already write to the
55    * destination?  Always smaller than 'size'.
56    */
57   size_t pos;
58
59   /**
60    * How many bytes of data are stored in 'buf' for transmission right now?
61    * Data always starts at offset 0 and extends to 'size'.
62    */
63   size_t size;
64
65   /**
66    * Buffered data; twice the maximum allowed message size as we add some
67    * headers.
68    */
69   char buf[MAXLINE * 2];
70 };
71
72
73 /**
74  * Flag set to 1 if we are to terminate, otherwise 0.
75  */
76 static int closeprog;
77
78
79 /**
80  * We're being killed, clean up.
81  *
82  * @param sig killing signal
83  */
84 static void
85 sigfunc (int sig)
86 {
87   closeprog = 1;
88   (void) unlink (FIFO_FILE1);
89   (void) unlink (FIFO_FILE2);
90 }
91
92
93 /**
94  * Create control message for plugin
95  *
96  * @param buffer pointer to buffer for the message
97  * @param mac pointer to the mac address
98  * @return number of bytes written
99  */
100 static int
101 send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
102 {
103
104   struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
105
106   memcpy (&macmsg.mac, (char *) mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
107   macmsg.hdr.size = htons (sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
108   macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
109   memcpy (buffer, &macmsg, sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
110   return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
111 }
112
113
114 /**
115  * We got a message from the FIFO, check it, convert the message
116  * type to the output forward and copy it to the buffer for stdout.
117  *
118  * @param cls the 'struct SendBuffer' to copy the converted message to
119  * @param client unused
120  * @param hdr inbound message from the FIFO
121  */
122 static void
123 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
124 {
125   struct SendBuffer *write_pout = cls;
126   const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in;
127   size_t payload_size;
128   struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader;
129   uint16_t sendsize;
130
131   sendsize = ntohs (hdr->size);
132   in = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
133   if ( (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ||
134        (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) > sendsize) )
135   {
136     FPRINTF (stderr, "%s", "Received malformed message\n");
137     exit (1);
138   }
139   payload_size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage);
140   if ((payload_size + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) + write_pout->size) > MAXLINE * 2)
141   {
142     FPRINTF (stderr, "%s",  "Packet too big for buffer\n");
143     exit (1);
144   }
145   memset (&newheader, 0, sizeof (newheader));
146   newheader.header.size = htons (payload_size + sizeof (newheader));
147   newheader.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
148   newheader.frame = in->frame;
149   memcpy (write_pout->buf + write_pout->size,
150           &newheader,
151           sizeof (newheader));
152   write_pout->size += sizeof (newheader);
153   memcpy (write_pout->buf + write_pout->size,
154           &in[1],
155           payload_size);
156   write_pout->size += payload_size;
157 }
158
159
160 /**
161  * We read a full message from stdin.  Copy it to our send buffer.
162  *
163  * @param cls the 'struct SendBuffer' to copy to
164  * @param client unused
165  * @param hdr the message we received to copy to the buffer
166  */
167 static void
168 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
169 {
170   struct SendBuffer *write_std = cls;
171   uint16_t sendsize;
172
173   sendsize = ntohs (hdr->size);
174   if ((sendsize + write_std->size) > MAXLINE * 2)
175   {
176     FPRINTF (stderr, "%s", "Packet too big for buffer\n");
177     exit (1);
178   }
179   memcpy (write_std->buf + write_std->size, hdr, sendsize);
180   write_std->size += sendsize;
181 }
182
183
184 /**
185  * Main function of a program that pretends to be a WLAN card.
186  *
187  * @param argc should be 2
188  * @param argv either '1' or '2', depending on which of the two cards this dummy is to emulate
189  * @return 1 on error, 0 if terminated normally via signal
190  */
191 int
192 main (int argc, char *argv[])
193 {
194   struct stat st;
195   int erg;
196   FILE *fpin = NULL;
197   FILE *fpout = NULL;
198   int fdpin;
199   int fdpout;
200   char readbuf[MAXLINE];
201   int readsize;
202   struct SendBuffer write_std;
203   struct SendBuffer write_pout;
204   int ret;
205   int maxfd;
206   fd_set rfds;
207   fd_set wfds;
208   struct timeval tv;
209   int retval;
210   struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst = NULL;
211   struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst = NULL;
212   struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
213   int first;
214
215   if ( (2 != argc) ||
216        ((0 != strcmp (argv[1], "1")) && (0 != strcmp (argv[1], "2"))) )
217   {
218     FPRINTF (stderr,
219              "%s",
220              "This program must be started with the operating mode (1 or 2) as the only argument.\n");
221     return 1;
222   }
223
224   /* make the fifos if needed */
225   if (0 != stat (FIFO_FILE1, &st))
226   {
227     if (0 == stat (FIFO_FILE2, &st))
228     {
229       FPRINTF (stderr, "%s", "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
230       exit (1);
231     }
232     umask (0);
233     erg = mkfifo (FIFO_FILE1, 0666);
234     if (0 != erg)
235     {
236       FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
237                strerror (errno));
238     }
239     erg = mkfifo (FIFO_FILE2, 0666);
240     if (0 != erg)
241     {
242       FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
243                strerror (errno));
244     }
245   }
246   else
247   {
248     if (0 != stat (FIFO_FILE2, &st))
249     {
250       FPRINTF (stderr, "%s", "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
251       exit (1);
252     }
253   }
254
255   if (0 != strcmp (argv[1], "1"))
256   {
257     first = 1;
258     fpin = fopen (FIFO_FILE1, "r");
259     if (NULL == fpin)
260     {
261       FPRINTF (stderr, "fopen of read FIFO_FILE1 failed: %s\n", STRERROR (errno));
262       goto end;
263     }
264     fpout = fopen (FIFO_FILE2, "w");
265     if (NULL == fpout)
266     {
267       FPRINTF (stderr, "fopen of write FIFO_FILE2 failed: %s\n", STRERROR (errno));
268       goto end;
269     }
270   }
271   else
272   {
273     first = 0;
274     fpout = fopen (FIFO_FILE1, "w");
275     if (NULL == fpout)
276     {
277       FPRINTF (stderr, "fopen of write FIFO_FILE1 failed: %s\n", STRERROR (errno));
278       goto end;
279     }
280     fpin = fopen (FIFO_FILE2, "r");
281     if (NULL == fpin)
282     {
283       FPRINTF (stderr, "fopen of read FIFO_FILE2 failed: %s\n", STRERROR (errno));
284       goto end;
285     }
286   }
287
288   fdpin = fileno (fpin);
289   GNUNET_assert (fpin >= 0);
290   if (fdpin >= FD_SETSIZE)
291   {
292     FPRINTF (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
293              (unsigned int) FD_SETSIZE);
294     goto end;
295   }
296
297   fdpout = fileno (fpout);
298   GNUNET_assert (fdpout >= 0);
299
300   if (fdpout >= FD_SETSIZE)
301   {
302     FPRINTF (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
303              (unsigned int) FD_SETSIZE);
304     goto end;
305   }
306
307   signal (SIGINT, &sigfunc);
308   signal (SIGTERM, &sigfunc);
309
310   write_std.size = 0;
311   write_std.pos = 0;
312   write_pout.size = 0;
313   write_pout.pos = 0;
314   stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
315   file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
316
317   /* Send 'random' mac address */
318   macaddr.mac[0] = 0x13;
319   macaddr.mac[1] = 0x22;
320   macaddr.mac[2] = 0x33;
321   macaddr.mac[3] = 0x44;
322   macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
323   macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
324   write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
325
326   while (0 == closeprog)
327   {
328     maxfd = -1;
329     tv.tv_sec = 5;
330     tv.tv_usec = 0;
331
332     FD_ZERO (&rfds);
333     FD_ZERO (&wfds);
334     /* if output queue is empty, read */
335     if (0 == write_pout.size)
336     {
337       FD_SET (STDIN_FILENO, &rfds);
338       maxfd = MAX (STDIN_FILENO, maxfd);
339     }
340     if (0 == write_std.size)
341     {
342       FD_SET (fdpin, &rfds);
343       maxfd = MAX (fdpin, maxfd);
344     }
345
346     /* if there is something to write, try to write */
347     if (0 < write_std.size)
348     {
349       FD_SET (STDOUT_FILENO, &wfds);
350       maxfd = MAX (maxfd, STDOUT_FILENO);
351     }
352     if (0 < write_pout.size)
353     {
354       FD_SET (fdpout, &wfds);
355       maxfd = MAX (maxfd, fdpout);
356     }
357
358     retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
359     if ((-1 == retval) && (EINTR == errno))
360       continue;
361     if (0 > retval)
362     {
363       FPRINTF (stderr, "select failed: %s\n", STRERROR (errno));
364       closeprog = 1;
365       break;
366     }
367
368     if (FD_ISSET (STDOUT_FILENO, &wfds))
369     {
370       ret =
371           write (STDOUT_FILENO, write_std.buf + write_std.pos,
372                  write_std.size - write_std.pos);
373       if (0 > ret)
374       {
375         closeprog = 1;
376         FPRINTF (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
377                  STRERROR (errno));
378         break;
379       }
380       else
381       {
382         write_std.pos += ret;
383         /* check if finished writing */
384         if (write_std.pos == write_std.size)
385         {
386           write_std.pos = 0;
387           write_std.size = 0;
388         }
389       }
390     }
391
392     if (FD_ISSET (fdpout, &wfds))
393     {
394       ret =
395           write (fdpout, write_pout.buf + write_pout.pos,
396                  write_pout.size - write_pout.pos);
397
398       if (0 > ret)
399       {
400         closeprog = 1;
401         FPRINTF (stderr, "Write ERROR to fdpout failed: %s\n", STRERROR (errno));
402       }
403       else
404       {
405         write_pout.pos += ret;
406         /* check if finished writing */
407         if (write_pout.pos == write_pout.size)
408         {
409           write_pout.pos = 0;
410           write_pout.size = 0;
411         }
412       }
413     }
414
415     if (FD_ISSET (STDIN_FILENO, &rfds))
416     {
417       readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
418
419       if (0 > readsize)
420       {
421         closeprog = 1;
422         FPRINTF (stderr, "Error reading from STDIN_FILENO: %s\n",
423                  STRERROR (errno));
424       }
425       else if (0 < readsize)
426       {
427         GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
428                                    GNUNET_NO, GNUNET_NO);
429
430       }
431       else
432       {
433         /* eof */
434         closeprog = 1;
435       }
436     }
437
438     if (FD_ISSET (fdpin, &rfds))
439     {
440       readsize = read (fdpin, readbuf, sizeof (readbuf));
441       if (0 > readsize)
442       {
443         closeprog = 1;
444         FPRINTF (stderr, "Error reading from fdpin: %s\n", STRERROR (errno));
445         break;
446       }
447       else if (0 < readsize)
448       {
449         GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
450                                    GNUNET_NO, GNUNET_NO);
451       }
452       else
453       {
454         /* eof */
455         closeprog = 1;
456       }
457     }
458   }
459
460 end:
461   /* clean up */
462   if (NULL != stdin_mst)
463     GNUNET_SERVER_mst_destroy (stdin_mst);
464   if (NULL != file_in_mst)
465     GNUNET_SERVER_mst_destroy (file_in_mst);
466
467   if (NULL != fpout)
468     fclose (fpout);
469   if (NULL != fpin)
470     fclose (fpin);
471   if (1 == first)
472   {
473     (void) unlink (FIFO_FILE1);
474     (void) unlink (FIFO_FILE2);
475   }
476   return 0;
477 }