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