- fix error messages
[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 int
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   return GNUNET_OK;
158 }
159
160
161 /**
162  * We read a full message from stdin.  Copy it to our send buffer.
163  *
164  * @param cls the 'struct SendBuffer' to copy to
165  * @param client unused
166  * @param hdr the message we received to copy to the buffer
167  */
168 static int
169 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
170 {
171   struct SendBuffer *write_std = cls;
172   uint16_t sendsize;
173
174   sendsize = ntohs (hdr->size);
175   if ((sendsize + write_std->size) > MAXLINE * 2)
176   {
177     FPRINTF (stderr, "%s", "Packet too big for buffer\n");
178     exit (1);
179   }
180   memcpy (write_std->buf + write_std->size, hdr, sendsize);
181   write_std->size += sendsize;
182   return GNUNET_OK;
183 }
184
185
186 /**
187  * Main function of a program that pretends to be a WLAN card.
188  *
189  * @param argc should be 2
190  * @param argv either '1' or '2', depending on which of the two cards this dummy is to emulate
191  * @return 1 on error, 0 if terminated normally via signal
192  */
193 int
194 main (int argc, char *argv[])
195 {
196   struct stat st;
197   int erg;
198   FILE *fpin = NULL;
199   FILE *fpout = NULL;
200   int fdpin;
201   int fdpout;
202   char readbuf[MAXLINE];
203   int readsize;
204   struct SendBuffer write_std;
205   struct SendBuffer write_pout;
206   int ret;
207   int maxfd;
208   fd_set rfds;
209   fd_set wfds;
210   struct timeval tv;
211   int retval;
212   struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst = NULL;
213   struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst = NULL;
214   struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
215   int first;
216
217   if ( (2 != argc) ||
218        ((0 != strcmp (argv[1], "1")) && (0 != strcmp (argv[1], "2"))) )
219   {
220     FPRINTF (stderr,
221              "%s",
222              "This program must be started with the operating mode (1 or 2) as the only argument.\n");
223     return 1;
224   }
225
226   /* make the fifos if needed */
227   umask (0);
228   if ( (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE1)) ||
229        (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE2)) )
230   {
231     FPRINTF (stderr, "Failed to create directory for file `%s'\n", FIFO_FILE1);
232     return 1;
233   }
234   if (0 == strcmp (argv[1], "1") )
235   {
236     if (0 != stat (FIFO_FILE1, &st))
237     {
238       erg = mkfifo (FIFO_FILE1, 0666);
239       if ( (0 != erg) && (EEXIST != errno) )
240         FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
241                  strerror (errno));
242     }
243   }
244   else
245   {
246     if (0 != stat (FIFO_FILE2, &st))
247     {
248         GNUNET_break (0 == (erg = mkfifo (FIFO_FILE2, 0666)));
249       if ( (0 != erg) && (EEXIST != errno) )
250         FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
251                  strerror (errno));
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     if (NULL == (fpout = fopen (FIFO_FILE2, "w")))
265     {
266       GNUNET_break (0 == mkfifo (FIFO_FILE2, 0666));
267       fpout = fopen (FIFO_FILE2, "w");
268     }
269     if (NULL == fpout)
270     {
271       FPRINTF (stderr, "fopen of write FIFO_FILE2 failed: %s\n", STRERROR (errno));
272       goto end;
273     }
274   }
275   else
276   {
277     first = 0;
278     if (NULL == (fpout = fopen (FIFO_FILE1, "w")))
279     {
280         GNUNET_break (0 == mkfifo (FIFO_FILE1, 0666));
281       fpout = fopen (FIFO_FILE1, "w");
282     }
283     if (NULL == fpout)
284     {
285       FPRINTF (stderr, "fopen of write FIFO_FILE1 failed: %s\n", STRERROR (errno));
286       goto end;
287     }
288     fpin = fopen (FIFO_FILE2, "r");
289     if (NULL == fpin)
290     {
291       FPRINTF (stderr, "fopen of read FIFO_FILE2 failed: %s\n", STRERROR (errno));
292       goto end;
293     }
294   }
295
296   fdpin = fileno (fpin);
297   GNUNET_assert (fpin >= 0);
298   if (fdpin >= FD_SETSIZE)
299   {
300     FPRINTF (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
301              (unsigned int) FD_SETSIZE);
302     goto end;
303   }
304
305   fdpout = fileno (fpout);
306   GNUNET_assert (fdpout >= 0);
307
308   if (fdpout >= FD_SETSIZE)
309   {
310     FPRINTF (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
311              (unsigned int) FD_SETSIZE);
312     goto end;
313   }
314
315   signal (SIGINT, &sigfunc);
316   signal (SIGTERM, &sigfunc);
317   signal (GNUNET_TERM_SIG, &sigfunc);
318
319   write_std.size = 0;
320   write_std.pos = 0;
321   write_pout.size = 0;
322   write_pout.pos = 0;
323   stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
324   file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
325
326   /* Send 'random' mac address */
327   macaddr.mac[0] = 0x13;
328   macaddr.mac[1] = 0x22;
329   macaddr.mac[2] = 0x33;
330   macaddr.mac[3] = 0x44;
331   macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
332   macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
333   write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
334
335   while (0 == closeprog)
336   {
337     maxfd = -1;
338     tv.tv_sec = 5;
339     tv.tv_usec = 0;
340
341     FD_ZERO (&rfds);
342     FD_ZERO (&wfds);
343     /* if output queue is empty, read */
344     if (0 == write_pout.size)
345     {
346       FD_SET (STDIN_FILENO, &rfds);
347       maxfd = MAX (STDIN_FILENO, maxfd);
348     }
349     if (0 == write_std.size)
350     {
351       FD_SET (fdpin, &rfds);
352       maxfd = MAX (fdpin, maxfd);
353     }
354
355     /* if there is something to write, try to write */
356     if (0 < write_std.size)
357     {
358       FD_SET (STDOUT_FILENO, &wfds);
359       maxfd = MAX (maxfd, STDOUT_FILENO);
360     }
361     if (0 < write_pout.size)
362     {
363       FD_SET (fdpout, &wfds);
364       maxfd = MAX (maxfd, fdpout);
365     }
366
367     retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
368     if ((-1 == retval) && (EINTR == errno))
369       continue;
370     if (0 > retval)
371     {
372       FPRINTF (stderr, "select failed: %s\n", STRERROR (errno));
373       closeprog = 1;
374       break;
375     }
376
377     if (FD_ISSET (STDOUT_FILENO, &wfds))
378     {
379       ret =
380           write (STDOUT_FILENO, write_std.buf + write_std.pos,
381                  write_std.size - write_std.pos);
382       if (0 > ret)
383       {
384         closeprog = 1;
385         FPRINTF (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
386                  STRERROR (errno));
387         break;
388       }
389       else
390       {
391         write_std.pos += ret;
392         /* check if finished writing */
393         if (write_std.pos == write_std.size)
394         {
395           write_std.pos = 0;
396           write_std.size = 0;
397         }
398       }
399     }
400
401     if (FD_ISSET (fdpout, &wfds))
402     {
403       ret =
404           write (fdpout, write_pout.buf + write_pout.pos,
405                  write_pout.size - write_pout.pos);
406
407       if (0 > ret)
408       {
409         closeprog = 1;
410         FPRINTF (stderr, "Write ERROR to fdpout failed: %s\n", STRERROR (errno));
411       }
412       else
413       {
414         write_pout.pos += ret;
415         /* check if finished writing */
416         if (write_pout.pos == write_pout.size)
417         {
418           write_pout.pos = 0;
419           write_pout.size = 0;
420         }
421       }
422     }
423
424     if (FD_ISSET (STDIN_FILENO, &rfds))
425     {
426       readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
427
428       if (0 > readsize)
429       {
430         closeprog = 1;
431         FPRINTF (stderr, "Error reading from STDIN_FILENO: %s\n",
432                  STRERROR (errno));
433       }
434       else if (0 < readsize)
435       {
436         GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
437                                    GNUNET_NO, GNUNET_NO);
438
439       }
440       else
441       {
442         /* eof */
443         closeprog = 1;
444       }
445     }
446
447     if (FD_ISSET (fdpin, &rfds))
448     {
449       readsize = read (fdpin, readbuf, sizeof (readbuf));
450       if (0 > readsize)
451       {
452         closeprog = 1;
453         FPRINTF (stderr, "Error reading from fdpin: %s\n", STRERROR (errno));
454         break;
455       }
456       else if (0 < readsize)
457       {
458         GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
459                                    GNUNET_NO, GNUNET_NO);
460       }
461       else
462       {
463         /* eof */
464         closeprog = 1;
465       }
466     }
467   }
468
469 end:
470   /* clean up */
471   if (NULL != stdin_mst)
472     GNUNET_SERVER_mst_destroy (stdin_mst);
473   if (NULL != file_in_mst)
474     GNUNET_SERVER_mst_destroy (file_in_mst);
475
476   if (NULL != fpout)
477     fclose (fpout);
478   if (NULL != fpin)
479     fclose (fpin);
480   if (1 == first)
481   {
482     (void) unlink (FIFO_FILE1);
483     (void) unlink (FIFO_FILE2);
484   }
485   return 0;
486 }