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.
53 * How many bytes that were stored in 'buf' did we already write to the
54 * destination? Always smaller than 'size'.
59 * How many bytes of data are stored in 'buf' for transmission right now?
60 * Data always starts at offset 0 and extends to 'size'.
65 * Buffered data; twice the maximum allowed message size as we add some
68 char buf[MAXLINE * 2];
73 * Flag set to 1 if we are to terminate, otherwise 0.
79 * We're being killed, clean up.
81 * @param sig killing signal
87 (void) unlink (FIFO_FILE1);
88 (void) unlink (FIFO_FILE2);
93 * Create control message for plugin
95 * @param buffer pointer to buffer for the message
96 * @param mac pointer to the mac address
97 * @return number of bytes written
100 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));
108 htons (sizeof(struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
109 macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
110 GNUNET_memcpy (buffer,
112 sizeof(struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
113 return sizeof(struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
118 * We got a message from the FIFO, check it, convert the message
119 * type to the output forward and copy it to the buffer for stdout.
121 * @param cls the 'struct SendBuffer' to copy the converted message to
122 * @param hdr inbound message from the FIFO
123 * @return #GNUNET_OK on success,
124 * #GNUNET_NO to stop further processing (no error)
125 * #GNUNET_SYSERR to stop further processing with error
128 stdin_send (void *cls, 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");
145 sendsize - sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage);
147 + sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
148 + write_pout->size) > MAXLINE * 2)
150 fprintf (stderr, "%s", "Packet too big for buffer\n");
153 memset (&newheader, 0, sizeof(newheader));
154 newheader.header.size = htons (payload_size + sizeof(newheader));
155 newheader.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
156 newheader.frame = in->frame;
157 GNUNET_memcpy (write_pout->buf + write_pout->size,
160 write_pout->size += sizeof(newheader);
161 GNUNET_memcpy (write_pout->buf + write_pout->size, &in[1], payload_size);
162 write_pout->size += payload_size;
168 * We read a full message from stdin. Copy it to our send buffer.
170 * @param cls the 'struct SendBuffer' to copy to
171 * @param hdr the message we received to copy to the buffer
172 * @return #GNUNET_OK on success,
173 * #GNUNET_NO to stop further processing (no error)
174 * #GNUNET_SYSERR to stop further processing with error
177 file_in_send (void *cls, 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"))))
231 "This program must be started with the operating mode (1 or 2) as the only argument.\n");
235 /* make the fifos if needed */
237 if ((GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE1)) ||
238 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (FIFO_FILE2)))
240 fprintf (stderr, "Failed to create directory for file `%s'\n", FIFO_FILE1);
243 if (0 == strcmp (argv[1], "1"))
245 if (0 != stat (FIFO_FILE1, &st))
247 erg = mkfifo (FIFO_FILE1, 0666);
248 if ((0 != erg) && (EEXIST != errno))
250 "Error in mkfifo(%s): %s\n",
257 if (0 != stat (FIFO_FILE2, &st))
259 GNUNET_break (0 == (erg = mkfifo (FIFO_FILE2, 0666)));
260 if ((0 != erg) && (EEXIST != errno))
262 "Error in mkfifo(%s): %s\n",
268 if (0 == strcmp (argv[1], "1"))
271 fpin = fopen (FIFO_FILE1, "r");
275 "fopen of read FIFO_FILE1 failed: %s\n",
279 if (NULL == (fpout = fopen (FIFO_FILE2, "w")))
281 GNUNET_break (0 == mkfifo (FIFO_FILE2, 0666));
282 fpout = fopen (FIFO_FILE2, "w");
287 "fopen of write FIFO_FILE2 failed: %s\n",
295 if (NULL == (fpout = fopen (FIFO_FILE1, "w")))
297 GNUNET_break (0 == mkfifo (FIFO_FILE1, 0666));
298 fpout = fopen (FIFO_FILE1, "w");
303 "fopen of write FIFO_FILE1 failed: %s\n",
307 fpin = fopen (FIFO_FILE2, "r");
311 "fopen of read FIFO_FILE2 failed: %s\n",
317 fdpin = fileno (fpin);
318 GNUNET_assert (fpin >= 0);
319 if (fdpin >= FD_SETSIZE)
322 "File fdpin number too large (%d > %u)\n",
324 (unsigned int) FD_SETSIZE);
328 fdpout = fileno (fpout);
329 GNUNET_assert (fdpout >= 0);
331 if (fdpout >= FD_SETSIZE)
334 "File fdpout number too large (%d > %u)\n",
336 (unsigned int) FD_SETSIZE);
340 signal (SIGINT, &sigfunc);
341 signal (SIGTERM, &sigfunc);
342 signal (GNUNET_TERM_SIG, &sigfunc);
348 stdin_mst = GNUNET_MST_create (&stdin_send, &write_pout);
349 file_in_mst = GNUNET_MST_create (&file_in_send, &write_std);
351 /* Send 'random' mac address */
352 macaddr.mac[0] = 0x13;
353 macaddr.mac[1] = 0x22;
354 macaddr.mac[2] = 0x33;
355 macaddr.mac[3] = 0x44;
356 macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
357 macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
358 write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
360 while (0 == closeprog)
368 /* if output queue is empty, read */
369 if (0 == write_pout.size)
371 FD_SET (STDIN_FILENO, &rfds);
372 maxfd = MAX (STDIN_FILENO, maxfd);
374 if (0 == write_std.size)
376 FD_SET (fdpin, &rfds);
377 maxfd = MAX (fdpin, maxfd);
380 /* if there is something to write, try to write */
381 if (0 < write_std.size)
383 FD_SET (STDOUT_FILENO, &wfds);
384 maxfd = MAX (maxfd, STDOUT_FILENO);
386 if (0 < write_pout.size)
388 FD_SET (fdpout, &wfds);
389 maxfd = MAX (maxfd, fdpout);
392 retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
393 if ((-1 == retval) && (EINTR == errno))
397 fprintf (stderr, "select failed: %s\n", strerror (errno));
402 if (FD_ISSET (STDOUT_FILENO, &wfds))
404 ret = write (STDOUT_FILENO,
405 write_std.buf + write_std.pos,
406 write_std.size - write_std.pos);
411 "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_pout.buf + write_pout.pos,
431 write_pout.size - write_pout.pos);
437 "Write ERROR to fdpout failed: %s\n",
442 write_pout.pos += ret;
443 /* check if finished writing */
444 if (write_pout.pos == write_pout.size)
452 if (FD_ISSET (STDIN_FILENO, &rfds))
454 readsize = read (STDIN_FILENO, readbuf, sizeof(readbuf));
460 "Error reading from STDIN_FILENO: %s\n",
463 else if (0 < readsize)
465 GNUNET_MST_from_buffer (stdin_mst,
478 if (FD_ISSET (fdpin, &rfds))
480 readsize = read (fdpin, readbuf, sizeof(readbuf));
484 fprintf (stderr, "Error reading from fdpin: %s\n", strerror (errno));
487 else if (0 < readsize)
489 GNUNET_MST_from_buffer (file_in_mst,
505 if (NULL != stdin_mst)
506 GNUNET_MST_destroy (stdin_mst);
507 if (NULL != file_in_mst)
508 GNUNET_MST_destroy (file_in_mst);
516 (void) unlink (FIFO_FILE1);
517 (void) unlink (FIFO_FILE2);