2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/gnunet-helper-transport-wlan-dummy.c
22 * @brief helper for the testcases for plugin_transport_wlan.c
23 * @author David Brodski
26 #include "gnunet_protocols.h"
27 #include "gnunet_util_lib.h"
28 #include "plugin_transport_wlan.h"
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"
39 char buf[MAXLINE * 2];
50 (void) unlink (FIFO_FILE1);
51 (void) unlink (FIFO_FILE2);
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
62 send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
65 struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
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);
71 memcpy (buffer, &macmsg, sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
72 return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
77 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
79 struct sendbuf *write_pout = cls;
80 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in;
82 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader;
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)) )
88 fprintf (stderr, "Function stdin_send: wrong packet type or size\n");
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)
94 fprintf (stderr, "Function stdin_send: Packet too big for buffer\n");
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,
104 write_pout->size += sizeof (newheader);
105 memcpy (write_pout->buf + write_pout->size,
108 write_pout->size += payload_size;
113 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
115 struct sendbuf *write_std = cls;
118 sendsize = ntohs (hdr->size);
120 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
122 fprintf (stderr, "Function file_in_send: wrong packet type\n");
125 if ((sendsize + write_std->size) > MAXLINE * 2)
127 fprintf (stderr, "Function file_in_send: Packet too big for buffer\n");
131 memcpy (write_std->buf + write_std->size, hdr, sendsize);
132 write_std->size += sendsize;
137 main (int argc, char *argv[])
145 char readbuf[MAXLINE];
147 struct sendbuf write_std;
148 struct sendbuf write_pout;
155 struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
156 struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst;
157 struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
162 "This program must be started with the operating mode (1 or 2) as the only argument.\n");
165 if ((0 != strstr (argv[1], "1")) && (0 != strstr (argv[1], "2")))
168 //make the fifos if needed
169 if (0 != stat (FIFO_FILE1, &st))
171 if (0 == stat (FIFO_FILE2, &st))
173 fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
177 erg = mkfifo (FIFO_FILE1, 0666);
180 fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
184 erg = mkfifo (FIFO_FILE2, 0666);
187 fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
195 if (0 != stat (FIFO_FILE2, &st))
197 fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
202 if (strstr (argv[1], "1"))
204 //fprintf(stderr, "First\n");
206 fpin = fopen (FIFO_FILE1, "r");
209 fprintf (stderr, "fopen of read FIFO_FILE1\n");
212 fpout = fopen (FIFO_FILE2, "w");
215 fprintf (stderr, "fopen of write FIFO_FILE2\n");
223 //fprintf(stderr, "Second\n");
224 fpout = fopen (FIFO_FILE1, "w");
227 fprintf (stderr, "fopen of write FIFO_FILE1\n");
230 fpin = fopen (FIFO_FILE2, "r");
233 fprintf (stderr, "fopen of read FIFO_FILE2\n");
239 fdpin = fileno (fpin);
240 GNUNET_assert (fpin >= 0);
242 if (fdpin >= FD_SETSIZE)
244 fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
245 (unsigned int) FD_SETSIZE);
249 fdpout = fileno (fpout);
250 GNUNET_assert (fdpout >= 0);
252 if (fdpout >= FD_SETSIZE)
254 fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
255 (unsigned int) FD_SETSIZE);
260 signal (SIGINT, &sigfunc);
261 signal (SIGTERM, &sigfunc);
267 stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
268 file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
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);
279 while (0 == closeprog)
287 // if output queue is empty
288 if (0 == write_pout.size)
290 FD_SET (STDIN_FILENO, &rfds);
291 maxfd = MAX (STDIN_FILENO, maxfd);
293 if (0 == write_std.size)
295 FD_SET (fdpin, &rfds);
296 maxfd = MAX (fdpin, maxfd);
299 // if there is something to write
300 if (0 < write_std.size)
302 FD_SET (STDOUT_FILENO, &wfds);
303 maxfd = MAX (maxfd, STDOUT_FILENO);
305 if (0 < write_pout.size)
307 FD_SET (fdpout, &wfds);
308 maxfd = MAX (maxfd, fdpout);
311 retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
312 if ((-1 == retval) && (EINTR == errno))
316 fprintf (stderr, "select failed: %s\n", strerror (errno));
321 if (FD_ISSET (STDOUT_FILENO, &wfds))
324 write (STDOUT_FILENO, write_std.buf + write_std.pos,
325 write_std.size - write_std.pos);
329 fprintf (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
335 write_std.pos += ret;
337 if (write_std.pos == write_std.size)
345 if (FD_ISSET (fdpout, &wfds))
348 write (fdpout, write_pout.buf + write_pout.pos,
349 write_pout.size - write_pout.pos);
354 fprintf (stderr, "Write ERROR to fdpout: %s\n", strerror (errno));
358 write_pout.pos += ret;
360 if (write_pout.pos == write_pout.size)
368 if (FD_ISSET (STDIN_FILENO, &rfds))
370 readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
375 fprintf (stderr, "Error reading from STDIN_FILENO: %s\n",
378 else if (0 < readsize)
380 GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
381 GNUNET_NO, GNUNET_NO);
391 if (FD_ISSET (fdpin, &rfds))
393 readsize = read (fdpin, readbuf, sizeof (readbuf));
397 fprintf (stderr, "Error reading from fdpin: %s\n", strerror (errno));
400 else if (0 < readsize)
402 GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
403 GNUNET_NO, GNUNET_NO);
414 GNUNET_SERVER_mst_destroy (stdin_mst);
415 GNUNET_SERVER_mst_destroy (file_in_mst);
424 (void) unlink (FIFO_FILE1);
425 (void) unlink (FIFO_FILE2);