2 This file is part of GNUnet.
3 (C) 2010, 2012 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"
31 * Name of the fifo to use for IPC with the other dummy process.
33 #define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in"
36 * Name of the fifo to use for IPC with the other dummy process.
38 #define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out"
41 * Maximum size of a message allowed in either direction
42 * (used for our receive and sent buffers).
48 * IO buffer used for buffering data in transit.
54 * How many bytes that were stored in 'buf' did we already write to the
55 * destination? Always smaller than 'size'.
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'.
66 * Buffered data; twice the maximum allowed message size as we add some
69 char buf[MAXLINE * 2];
74 * Flag set to 1 if we are to terminate, otherwise 0.
80 * We're being killed, clean up.
82 * @param sig killing signal
88 (void) unlink (FIFO_FILE1);
89 (void) unlink (FIFO_FILE2);
94 * Create control message for plugin
96 * @param buffer pointer to buffer for the message
97 * @param mac pointer to the mac address
98 * @return number of bytes written
101 send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
104 struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
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);
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.
118 * @param cls the 'struct SendBuffer' to copy the converted message to
119 * @param client unused
120 * @param hdr inbound message from the FIFO
123 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
125 struct SendBuffer *write_pout = cls;
126 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in;
128 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader;
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) )
136 FPRINTF (stderr, "%s", "Received malformed message\n");
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)
142 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
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,
152 write_pout->size += sizeof (newheader);
153 memcpy (write_pout->buf + write_pout->size,
156 write_pout->size += payload_size;
161 * We read a full message from stdin. Copy it to our send buffer.
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
168 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
170 struct SendBuffer *write_std = cls;
173 sendsize = ntohs (hdr->size);
174 if ((sendsize + write_std->size) > MAXLINE * 2)
176 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
179 memcpy (write_std->buf + write_std->size, hdr, sendsize);
180 write_std->size += sendsize;
185 * Main function of a program that pretends to be a WLAN card.
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
192 main (int argc, char *argv[])
200 char readbuf[MAXLINE];
202 struct SendBuffer write_std;
203 struct SendBuffer write_pout;
210 struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst = NULL;
211 struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst = NULL;
212 struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
216 ((0 != strcmp (argv[1], "1")) && (0 != strcmp (argv[1], "2"))) )
220 "This program must be started with the operating mode (1 or 2) as the only argument.\n");
224 /* make the fifos if needed */
226 if ( (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE1)) ||
227 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE2)) )
229 FPRINTF (stderr, "Failed to create directory for file `%s'\n", FIFO_FILE1);
232 if (0 == strcmp (argv[1], "1") )
234 if (0 != stat (FIFO_FILE1, &st))
236 erg = mkfifo (FIFO_FILE1, 0666);
237 if ( (0 != erg) && (EEXIST != errno) )
238 FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
244 if (0 != stat (FIFO_FILE2, &st))
246 erg = mkfifo (FIFO_FILE2, 0666);
247 if ( (0 != erg) && (EEXIST != errno) )
248 FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
253 if (0 == strcmp (argv[1], "1"))
256 fpin = fopen (FIFO_FILE1, "r");
259 FPRINTF (stderr, "fopen of read FIFO_FILE1 failed: %s\n", STRERROR (errno));
262 if (NULL == (fpout = fopen (FIFO_FILE2, "w")))
264 erg = mkfifo (FIFO_FILE2, 0666);
265 fpout = fopen (FIFO_FILE2, "w");
269 FPRINTF (stderr, "fopen of write FIFO_FILE2 failed: %s\n", STRERROR (errno));
276 if (NULL == (fpout = fopen (FIFO_FILE1, "w")))
278 erg = mkfifo (FIFO_FILE1, 0666);
279 fpout = fopen (FIFO_FILE1, "w");
283 FPRINTF (stderr, "fopen of write FIFO_FILE1 failed: %s\n", STRERROR (errno));
286 fpin = fopen (FIFO_FILE2, "r");
289 FPRINTF (stderr, "fopen of read FIFO_FILE2 failed: %s\n", STRERROR (errno));
294 fdpin = fileno (fpin);
295 GNUNET_assert (fpin >= 0);
296 if (fdpin >= FD_SETSIZE)
298 FPRINTF (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
299 (unsigned int) FD_SETSIZE);
303 fdpout = fileno (fpout);
304 GNUNET_assert (fdpout >= 0);
306 if (fdpout >= FD_SETSIZE)
308 FPRINTF (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
309 (unsigned int) FD_SETSIZE);
313 signal (SIGINT, &sigfunc);
314 signal (SIGTERM, &sigfunc);
320 stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
321 file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
323 /* Send 'random' mac address */
324 macaddr.mac[0] = 0x13;
325 macaddr.mac[1] = 0x22;
326 macaddr.mac[2] = 0x33;
327 macaddr.mac[3] = 0x44;
328 macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
329 macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
330 write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
332 while (0 == closeprog)
340 /* if output queue is empty, read */
341 if (0 == write_pout.size)
343 FD_SET (STDIN_FILENO, &rfds);
344 maxfd = MAX (STDIN_FILENO, maxfd);
346 if (0 == write_std.size)
348 FD_SET (fdpin, &rfds);
349 maxfd = MAX (fdpin, maxfd);
352 /* if there is something to write, try to write */
353 if (0 < write_std.size)
355 FD_SET (STDOUT_FILENO, &wfds);
356 maxfd = MAX (maxfd, STDOUT_FILENO);
358 if (0 < write_pout.size)
360 FD_SET (fdpout, &wfds);
361 maxfd = MAX (maxfd, fdpout);
364 retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
365 if ((-1 == retval) && (EINTR == errno))
369 FPRINTF (stderr, "select failed: %s\n", STRERROR (errno));
374 if (FD_ISSET (STDOUT_FILENO, &wfds))
377 write (STDOUT_FILENO, write_std.buf + write_std.pos,
378 write_std.size - write_std.pos);
382 FPRINTF (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
388 write_std.pos += ret;
389 /* check if finished writing */
390 if (write_std.pos == write_std.size)
398 if (FD_ISSET (fdpout, &wfds))
401 write (fdpout, write_pout.buf + write_pout.pos,
402 write_pout.size - write_pout.pos);
407 FPRINTF (stderr, "Write ERROR to fdpout failed: %s\n", STRERROR (errno));
411 write_pout.pos += ret;
412 /* check if finished writing */
413 if (write_pout.pos == write_pout.size)
421 if (FD_ISSET (STDIN_FILENO, &rfds))
423 readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
428 FPRINTF (stderr, "Error reading from STDIN_FILENO: %s\n",
431 else if (0 < readsize)
433 GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
434 GNUNET_NO, GNUNET_NO);
444 if (FD_ISSET (fdpin, &rfds))
446 readsize = read (fdpin, readbuf, sizeof (readbuf));
450 FPRINTF (stderr, "Error reading from fdpin: %s\n", STRERROR (errno));
453 else if (0 < readsize)
455 GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
456 GNUNET_NO, GNUNET_NO);
468 if (NULL != stdin_mst)
469 GNUNET_SERVER_mst_destroy (stdin_mst);
470 if (NULL != file_in_mst)
471 GNUNET_SERVER_mst_destroy (file_in_mst);
479 (void) unlink (FIFO_FILE1);
480 (void) unlink (FIFO_FILE2);