2 This file is part of GNUnet.
3 Copyright (C) 2010, 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file transport/gnunet-helper-transport-wlan-dummy.c
20 * @brief helper for the testcases for plugin_transport_wlan.c
21 * @author David Brodski
24 #include "gnunet_protocols.h"
25 #include "gnunet_util_lib.h"
26 #include "plugin_transport_wlan.h"
29 * Name of the fifo to use for IPC with the other dummy process.
31 #define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in"
34 * Name of the fifo to use for IPC with the other dummy process.
36 #define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out"
39 * Maximum size of a message allowed in either direction
40 * (used for our receive and sent buffers).
46 * IO buffer used for buffering data in transit.
52 * How many bytes that were stored in 'buf' did we already write to the
53 * destination? Always smaller than 'size'.
58 * How many bytes of data are stored in 'buf' for transmission right now?
59 * Data always starts at offset 0 and extends to 'size'.
64 * Buffered data; twice the maximum allowed message size as we add some
67 char buf[MAXLINE * 2];
72 * Flag set to 1 if we are to terminate, otherwise 0.
78 * We're being killed, clean up.
80 * @param sig killing signal
86 (void) unlink (FIFO_FILE1);
87 (void) unlink (FIFO_FILE2);
92 * Create control message for plugin
94 * @param buffer pointer to buffer for the message
95 * @param mac pointer to the mac address
96 * @return number of bytes written
99 send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
102 struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
104 GNUNET_memcpy (&macmsg.mac,
106 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 GNUNET_memcpy (buffer,
111 sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
112 return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
117 * We got a message from the FIFO, check it, convert the message
118 * type to the output forward and copy it to the buffer for stdout.
120 * @param cls the 'struct SendBuffer' to copy the converted message to
121 * @param hdr inbound message from the FIFO
122 * @return #GNUNET_OK on success,
123 * #GNUNET_NO to stop further processing (no error)
124 * #GNUNET_SYSERR to stop further processing with error
127 stdin_send (void *cls,
128 const struct GNUNET_MessageHeader *hdr)
130 struct SendBuffer *write_pout = cls;
131 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in;
133 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader;
136 sendsize = ntohs (hdr->size);
137 in = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
138 if ( (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ||
139 (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) > sendsize) )
141 FPRINTF (stderr, "%s", "Received malformed message\n");
144 payload_size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage);
145 if ((payload_size + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) + write_pout->size) > MAXLINE * 2)
147 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
150 memset (&newheader, 0, sizeof (newheader));
151 newheader.header.size = htons (payload_size + sizeof (newheader));
152 newheader.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
153 newheader.frame = in->frame;
154 GNUNET_memcpy (write_pout->buf + write_pout->size,
157 write_pout->size += sizeof (newheader);
158 GNUNET_memcpy (write_pout->buf + write_pout->size,
161 write_pout->size += payload_size;
167 * We read a full message from stdin. Copy it to our send buffer.
169 * @param cls the 'struct SendBuffer' to copy to
170 * @param hdr the message we received to copy to the buffer
171 * @return #GNUNET_OK on success,
172 * #GNUNET_NO to stop further processing (no error)
173 * #GNUNET_SYSERR to stop further processing with error
176 file_in_send (void *cls,
177 const struct GNUNET_MessageHeader *hdr)
179 struct SendBuffer *write_std = cls;
182 sendsize = ntohs (hdr->size);
183 if ((sendsize + write_std->size) > MAXLINE * 2)
185 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
188 GNUNET_memcpy (write_std->buf + write_std->size, hdr, sendsize);
189 write_std->size += sendsize;
195 * Main function of a program that pretends to be a WLAN card.
197 * @param argc should be 2
198 * @param argv either '1' or '2', depending on which of the two cards this dummy is to emulate
199 * @return 1 on error, 0 if terminated normally via signal
202 main (int argc, char *argv[])
210 char readbuf[MAXLINE];
212 struct SendBuffer write_std;
213 struct SendBuffer write_pout;
220 struct GNUNET_MessageStreamTokenizer *stdin_mst = NULL;
221 struct GNUNET_MessageStreamTokenizer *file_in_mst = NULL;
222 struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
226 ((0 != strcmp (argv[1], "1")) && (0 != strcmp (argv[1], "2"))) )
230 "This program must be started with the operating mode (1 or 2) as the only argument.\n");
234 /* make the fifos if needed */
236 if ( (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE1)) ||
237 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE2)) )
240 "Failed to create directory for file `%s'\n",
244 if (0 == strcmp (argv[1], "1") )
246 if (0 != stat (FIFO_FILE1, &st))
248 erg = mkfifo (FIFO_FILE1, 0666);
249 if ( (0 != erg) && (EEXIST != errno) )
250 FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
256 if (0 != stat (FIFO_FILE2, &st))
258 GNUNET_break (0 == (erg = mkfifo (FIFO_FILE2, 0666)));
259 if ( (0 != erg) && (EEXIST != errno) )
261 "Error in mkfifo(%s): %s\n",
267 if (0 == strcmp (argv[1], "1"))
270 fpin = fopen (FIFO_FILE1, "r");
274 "fopen of read FIFO_FILE1 failed: %s\n",
278 if (NULL == (fpout = fopen (FIFO_FILE2, "w")))
280 GNUNET_break (0 == mkfifo (FIFO_FILE2, 0666));
281 fpout = fopen (FIFO_FILE2, "w");
286 "fopen of write FIFO_FILE2 failed: %s\n",
294 if (NULL == (fpout = fopen (FIFO_FILE1, "w")))
296 GNUNET_break (0 == mkfifo (FIFO_FILE1, 0666));
297 fpout = fopen (FIFO_FILE1, "w");
302 "fopen of write FIFO_FILE1 failed: %s\n",
306 fpin = fopen (FIFO_FILE2, "r");
310 "fopen of read FIFO_FILE2 failed: %s\n",
316 fdpin = fileno (fpin);
317 GNUNET_assert (fpin >= 0);
318 if (fdpin >= FD_SETSIZE)
321 "File fdpin number too large (%d > %u)\n",
323 (unsigned int) FD_SETSIZE);
327 fdpout = fileno (fpout);
328 GNUNET_assert (fdpout >= 0);
330 if (fdpout >= FD_SETSIZE)
333 "File fdpout number too large (%d > %u)\n",
335 (unsigned int) FD_SETSIZE);
339 signal (SIGINT, &sigfunc);
340 signal (SIGTERM, &sigfunc);
341 signal (GNUNET_TERM_SIG, &sigfunc);
347 stdin_mst = GNUNET_MST_create (&stdin_send, &write_pout);
348 file_in_mst = GNUNET_MST_create (&file_in_send, &write_std);
350 /* Send 'random' mac address */
351 macaddr.mac[0] = 0x13;
352 macaddr.mac[1] = 0x22;
353 macaddr.mac[2] = 0x33;
354 macaddr.mac[3] = 0x44;
355 macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
356 macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
357 write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
359 while (0 == closeprog)
367 /* if output queue is empty, read */
368 if (0 == write_pout.size)
370 FD_SET (STDIN_FILENO, &rfds);
371 maxfd = MAX (STDIN_FILENO, maxfd);
373 if (0 == write_std.size)
375 FD_SET (fdpin, &rfds);
376 maxfd = MAX (fdpin, maxfd);
379 /* if there is something to write, try to write */
380 if (0 < write_std.size)
382 FD_SET (STDOUT_FILENO, &wfds);
383 maxfd = MAX (maxfd, STDOUT_FILENO);
385 if (0 < write_pout.size)
387 FD_SET (fdpout, &wfds);
388 maxfd = MAX (maxfd, fdpout);
391 retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
392 if ((-1 == retval) && (EINTR == errno))
396 FPRINTF (stderr, "select failed: %s\n", STRERROR (errno));
401 if (FD_ISSET (STDOUT_FILENO, &wfds))
404 write (STDOUT_FILENO, write_std.buf + write_std.pos,
405 write_std.size - write_std.pos);
409 FPRINTF (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
415 write_std.pos += ret;
416 /* check if finished writing */
417 if (write_std.pos == write_std.size)
425 if (FD_ISSET (fdpout, &wfds))
428 write (fdpout, write_pout.buf + write_pout.pos,
429 write_pout.size - write_pout.pos);
434 FPRINTF (stderr, "Write ERROR to fdpout failed: %s\n", STRERROR (errno));
438 write_pout.pos += ret;
439 /* check if finished writing */
440 if (write_pout.pos == write_pout.size)
448 if (FD_ISSET (STDIN_FILENO, &rfds))
450 readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
455 FPRINTF (stderr, "Error reading from STDIN_FILENO: %s\n",
458 else if (0 < readsize)
460 GNUNET_MST_from_buffer (stdin_mst,
462 GNUNET_NO, GNUNET_NO);
472 if (FD_ISSET (fdpin, &rfds))
474 readsize = read (fdpin, readbuf, sizeof (readbuf));
478 FPRINTF (stderr, "Error reading from fdpin: %s\n", STRERROR (errno));
481 else if (0 < readsize)
483 GNUNET_MST_from_buffer (file_in_mst,
485 GNUNET_NO, GNUNET_NO);
497 if (NULL != stdin_mst)
498 GNUNET_MST_destroy (stdin_mst);
499 if (NULL != file_in_mst)
500 GNUNET_MST_destroy (file_in_mst);
508 (void) unlink (FIFO_FILE1);
509 (void) unlink (FIFO_FILE2);