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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 GNUNET_memcpy (&macmsg.mac,
108 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
109 macmsg.hdr.size = htons (sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
110 macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
111 GNUNET_memcpy (buffer,
113 sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
114 return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
119 * We got a message from the FIFO, check it, convert the message
120 * type to the output forward and copy it to the buffer for stdout.
122 * @param cls the 'struct SendBuffer' to copy the converted message to
123 * @param hdr inbound message from the FIFO
124 * @return #GNUNET_OK on success,
125 * #GNUNET_NO to stop further processing (no error)
126 * #GNUNET_SYSERR to stop further processing with error
129 stdin_send (void *cls,
130 const struct GNUNET_MessageHeader *hdr)
132 struct SendBuffer *write_pout = cls;
133 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *in;
135 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage newheader;
138 sendsize = ntohs (hdr->size);
139 in = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
140 if ( (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ||
141 (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) > sendsize) )
143 FPRINTF (stderr, "%s", "Received malformed message\n");
146 payload_size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage);
147 if ((payload_size + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) + write_pout->size) > MAXLINE * 2)
149 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
152 memset (&newheader, 0, sizeof (newheader));
153 newheader.header.size = htons (payload_size + sizeof (newheader));
154 newheader.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
155 newheader.frame = in->frame;
156 GNUNET_memcpy (write_pout->buf + write_pout->size,
159 write_pout->size += sizeof (newheader);
160 GNUNET_memcpy (write_pout->buf + write_pout->size,
163 write_pout->size += payload_size;
169 * We read a full message from stdin. Copy it to our send buffer.
171 * @param cls the 'struct SendBuffer' to copy to
172 * @param hdr the message we received to copy to the buffer
173 * @return #GNUNET_OK on success,
174 * #GNUNET_NO to stop further processing (no error)
175 * #GNUNET_SYSERR to stop further processing with error
178 file_in_send (void *cls,
179 const struct GNUNET_MessageHeader *hdr)
181 struct SendBuffer *write_std = cls;
184 sendsize = ntohs (hdr->size);
185 if ((sendsize + write_std->size) > MAXLINE * 2)
187 FPRINTF (stderr, "%s", "Packet too big for buffer\n");
190 GNUNET_memcpy (write_std->buf + write_std->size, hdr, sendsize);
191 write_std->size += sendsize;
197 * Main function of a program that pretends to be a WLAN card.
199 * @param argc should be 2
200 * @param argv either '1' or '2', depending on which of the two cards this dummy is to emulate
201 * @return 1 on error, 0 if terminated normally via signal
204 main (int argc, char *argv[])
212 char readbuf[MAXLINE];
214 struct SendBuffer write_std;
215 struct SendBuffer write_pout;
222 struct GNUNET_MessageStreamTokenizer *stdin_mst = NULL;
223 struct GNUNET_MessageStreamTokenizer *file_in_mst = NULL;
224 struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
228 ((0 != strcmp (argv[1], "1")) && (0 != strcmp (argv[1], "2"))) )
232 "This program must be started with the operating mode (1 or 2) as the only argument.\n");
236 /* make the fifos if needed */
238 if ( (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE1)) ||
239 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE2)) )
242 "Failed to create directory for file `%s'\n",
246 if (0 == strcmp (argv[1], "1") )
248 if (0 != stat (FIFO_FILE1, &st))
250 erg = mkfifo (FIFO_FILE1, 0666);
251 if ( (0 != erg) && (EEXIST != errno) )
252 FPRINTF (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
258 if (0 != stat (FIFO_FILE2, &st))
260 GNUNET_break (0 == (erg = mkfifo (FIFO_FILE2, 0666)));
261 if ( (0 != erg) && (EEXIST != errno) )
263 "Error in mkfifo(%s): %s\n",
269 if (0 == strcmp (argv[1], "1"))
272 fpin = fopen (FIFO_FILE1, "r");
276 "fopen of read FIFO_FILE1 failed: %s\n",
280 if (NULL == (fpout = fopen (FIFO_FILE2, "w")))
282 GNUNET_break (0 == mkfifo (FIFO_FILE2, 0666));
283 fpout = fopen (FIFO_FILE2, "w");
288 "fopen of write FIFO_FILE2 failed: %s\n",
296 if (NULL == (fpout = fopen (FIFO_FILE1, "w")))
298 GNUNET_break (0 == mkfifo (FIFO_FILE1, 0666));
299 fpout = fopen (FIFO_FILE1, "w");
304 "fopen of write FIFO_FILE1 failed: %s\n",
308 fpin = fopen (FIFO_FILE2, "r");
312 "fopen of read FIFO_FILE2 failed: %s\n",
318 fdpin = fileno (fpin);
319 GNUNET_assert (fpin >= 0);
320 if (fdpin >= FD_SETSIZE)
323 "File fdpin number too large (%d > %u)\n",
325 (unsigned int) FD_SETSIZE);
329 fdpout = fileno (fpout);
330 GNUNET_assert (fdpout >= 0);
332 if (fdpout >= FD_SETSIZE)
335 "File fdpout number too large (%d > %u)\n",
337 (unsigned int) FD_SETSIZE);
341 signal (SIGINT, &sigfunc);
342 signal (SIGTERM, &sigfunc);
343 signal (GNUNET_TERM_SIG, &sigfunc);
349 stdin_mst = GNUNET_MST_create (&stdin_send, &write_pout);
350 file_in_mst = GNUNET_MST_create (&file_in_send, &write_std);
352 /* Send 'random' mac address */
353 macaddr.mac[0] = 0x13;
354 macaddr.mac[1] = 0x22;
355 macaddr.mac[2] = 0x33;
356 macaddr.mac[3] = 0x44;
357 macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
358 macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
359 write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
361 while (0 == closeprog)
369 /* if output queue is empty, read */
370 if (0 == write_pout.size)
372 FD_SET (STDIN_FILENO, &rfds);
373 maxfd = MAX (STDIN_FILENO, maxfd);
375 if (0 == write_std.size)
377 FD_SET (fdpin, &rfds);
378 maxfd = MAX (fdpin, maxfd);
381 /* if there is something to write, try to write */
382 if (0 < write_std.size)
384 FD_SET (STDOUT_FILENO, &wfds);
385 maxfd = MAX (maxfd, STDOUT_FILENO);
387 if (0 < write_pout.size)
389 FD_SET (fdpout, &wfds);
390 maxfd = MAX (maxfd, fdpout);
393 retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
394 if ((-1 == retval) && (EINTR == errno))
398 FPRINTF (stderr, "select failed: %s\n", STRERROR (errno));
403 if (FD_ISSET (STDOUT_FILENO, &wfds))
406 write (STDOUT_FILENO, write_std.buf + write_std.pos,
407 write_std.size - write_std.pos);
411 FPRINTF (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
417 write_std.pos += ret;
418 /* check if finished writing */
419 if (write_std.pos == write_std.size)
427 if (FD_ISSET (fdpout, &wfds))
430 write (fdpout, write_pout.buf + write_pout.pos,
431 write_pout.size - write_pout.pos);
436 FPRINTF (stderr, "Write ERROR to fdpout failed: %s\n", STRERROR (errno));
440 write_pout.pos += ret;
441 /* check if finished writing */
442 if (write_pout.pos == write_pout.size)
450 if (FD_ISSET (STDIN_FILENO, &rfds))
452 readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
457 FPRINTF (stderr, "Error reading from STDIN_FILENO: %s\n",
460 else if (0 < readsize)
462 GNUNET_MST_from_buffer (stdin_mst,
464 GNUNET_NO, GNUNET_NO);
474 if (FD_ISSET (fdpin, &rfds))
476 readsize = read (fdpin, readbuf, sizeof (readbuf));
480 FPRINTF (stderr, "Error reading from fdpin: %s\n", STRERROR (errno));
483 else if (0 < readsize)
485 GNUNET_MST_from_buffer (file_in_mst,
487 GNUNET_NO, GNUNET_NO);
499 if (NULL != stdin_mst)
500 GNUNET_MST_destroy (stdin_mst);
501 if (NULL != file_in_mst)
502 GNUNET_MST_destroy (file_in_mst);
510 (void) unlink (FIFO_FILE1);
511 (void) unlink (FIFO_FILE2);