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-transport-wlan-helper-dummy.c
22 * @brief helper for the testcases for plugin_transport_wlan.c
23 * @author David Brodski
26 #include "gnunet_constants.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_transport_plugin.h"
29 #include "transport.h"
30 #include "gnunet_util_lib.h"
31 #include "plugin_transport_wlan.h"
32 #include "gnunet_common.h"
33 #include "gnunet_crypto_lib.h"
35 #define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in"
36 #define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out"
44 char buf[MAXLINE * 2];
55 (void) unlink (FIFO_FILE1);
56 (void) unlink (FIFO_FILE2);
60 * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin
61 * @param buffer pointer to buffer for the message
62 * @param mac pointer to the mac address
63 * @return number of bytes written
66 send_mac_to_plugin (char *buffer, struct MacAddress *mac)
69 struct Wlan_Helper_Control_Message macmsg;
71 memcpy (&macmsg.mac, (char *) mac, sizeof (struct MacAddress));
72 macmsg.hdr.size = htons (sizeof (struct Wlan_Helper_Control_Message));
73 macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
75 memcpy (buffer, &macmsg, sizeof (struct Wlan_Helper_Control_Message));
76 return sizeof (struct Wlan_Helper_Control_Message);
80 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
82 struct sendbuf *write_pout = cls;
84 struct GNUNET_MessageHeader newheader;
90 ntohs (hdr->size) - sizeof (struct Radiotap_Send) +
91 sizeof (struct Radiotap_rx);
93 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
95 fprintf (stderr, "Function stdin_send: wrong packet type\n");
98 if ((sendsize + write_pout->size) > MAXLINE * 2)
100 fprintf (stderr, "Function stdin_send: Packet too big for buffer\n");
104 newheader.size = htons (sendsize);
105 newheader.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
107 to_start = write_pout->buf + write_pout->size;
108 memcpy (to_start, &newheader, sizeof (struct GNUNET_MessageHeader));
109 write_pout->size += sizeof (struct GNUNET_MessageHeader);
111 to_radiotap = to_start + sizeof (struct GNUNET_MessageHeader);
112 memset (to_radiotap, 0, sizeof (struct Radiotap_rx));
113 write_pout->size += sizeof (struct Radiotap_rx);
115 to_data = to_radiotap + sizeof (struct Radiotap_rx);
117 ((char *) hdr) + sizeof (struct Radiotap_Send) +
118 sizeof (struct GNUNET_MessageHeader),
119 ntohs (hdr->size) - sizeof (struct Radiotap_Send) -
120 sizeof (struct GNUNET_MessageHeader));
122 ntohs (hdr->size) - sizeof (struct Radiotap_Send) -
123 sizeof (struct GNUNET_MessageHeader);
127 file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
129 struct sendbuf *write_std = cls;
132 sendsize = ntohs (hdr->size);
134 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
136 fprintf (stderr, "Function file_in_send: wrong packet type\n");
139 if ((sendsize + write_std->size) > MAXLINE * 2)
141 fprintf (stderr, "Function file_in_send: Packet too big for buffer\n");
145 memcpy (write_std->buf + write_std->size, hdr, sendsize);
146 write_std->size += sendsize;
152 testmode (int argc, char *argv[])
163 //make the fifos if needed
164 if (0 != stat (FIFO_FILE1, &st))
166 if (0 == stat (FIFO_FILE2, &st))
168 fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
173 //unlink(FIFO_FILE1);
174 //unlink(FIFO_FILE2);
175 // FIXME: use mkfifo!
176 erg = mkfifo (FIFO_FILE1, 0666);
179 fprintf (stderr, "Error at mkfifo1: %s\n", strerror (errno));
182 erg = mkfifo (FIFO_FILE2, 0666);
185 fprintf (stderr, "Error at mkfifo2: %s\n", strerror (errno));
193 if (0 != stat (FIFO_FILE2, &st))
195 fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
201 if (strstr (argv[1], "1"))
203 //fprintf(stderr, "First\n");
205 fpin = fopen (FIFO_FILE1, "r");
208 fprintf (stderr, "fopen of read FIFO_FILE1\n");
211 fpout = fopen (FIFO_FILE2, "w");
214 fprintf (stderr, "fopen of write FIFO_FILE2\n");
222 //fprintf(stderr, "Second\n");
223 fpout = fopen (FIFO_FILE1, "w");
226 fprintf (stderr, "fopen of write FIFO_FILE1\n");
229 fpin = fopen (FIFO_FILE2, "r");
232 fprintf (stderr, "fopen of read FIFO_FILE2\n");
238 fdpin = fileno (fpin);
239 GNUNET_assert (fpin >= 0);
241 if (fdpin >= FD_SETSIZE)
243 fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
244 (unsigned int) FD_SETSIZE);
248 fdpout = fileno (fpout);
249 GNUNET_assert (fdpout >= 0);
251 if (fdpout >= FD_SETSIZE)
253 fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
254 (unsigned int) FD_SETSIZE);
259 signal (SIGINT, &sigfunc);
260 signal (SIGTERM, &sigfunc);
262 char readbuf[MAXLINE];
264 struct sendbuf write_std;
269 struct sendbuf write_pout;
282 struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
283 struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst;
285 stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
286 file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
290 struct MacAddress macaddr;
292 //Send random mac address
293 macaddr.mac[0] = 0x13;
294 macaddr.mac[1] = 0x22;
295 macaddr.mac[2] = 0x33;
296 macaddr.mac[3] = 0x44;
297 macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
298 macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
300 write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
302 while (0 == closeprog)
312 // if output queue is empty
313 if (0 == write_pout.size)
315 FD_SET (STDIN_FILENO, &rfds);
318 if (0 == write_std.size)
320 FD_SET (fdpin, &rfds);
324 // if there is something to write
325 if (0 < write_std.size)
327 FD_SET (STDOUT_FILENO, &wfds);
328 maxfd = MAX (maxfd, STDOUT_FILENO);
331 if (0 < write_pout.size)
333 FD_SET (fdpout, &wfds);
334 maxfd = MAX (maxfd, fdpout);
337 retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
339 if (-1 == retval && EINTR == errno)
345 fprintf (stderr, "select failed: %s\n", strerror (errno));
350 if (FD_ISSET (STDOUT_FILENO, &wfds))
353 write (STDOUT_FILENO, write_std.buf + write_std.pos,
354 write_std.size - write_std.pos);
358 fprintf (stderr, "Write ERROR to STDOUT\n");
363 write_std.pos += ret;
365 if (write_std.pos == write_std.size)
373 if (FD_ISSET (fdpout, &wfds))
376 write (fdpout, write_pout.buf + write_pout.pos,
377 write_pout.size - write_pout.pos);
382 fprintf (stderr, "Write ERROR to fdpout\n");
386 write_pout.pos += ret;
388 if (write_pout.pos == write_pout.size)
396 if (FD_ISSET (STDIN_FILENO, &rfds))
398 readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
403 fprintf (stderr, "Read ERROR to STDIN_FILENO\n");
405 else if (0 < readsize)
407 GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
408 GNUNET_NO, GNUNET_NO);
418 if (FD_ISSET (fdpin, &rfds))
420 readsize = read (fdpin, readbuf, sizeof (readbuf));
425 fprintf (stderr, "Read ERROR to fdpin: %s\n", strerror (errno));
428 else if (0 < readsize)
430 GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
431 GNUNET_NO, GNUNET_NO);
445 GNUNET_SERVER_mst_destroy (stdin_mst);
446 GNUNET_SERVER_mst_destroy (file_in_mst);
456 (void) unlink (FIFO_FILE1);
457 (void) unlink (FIFO_FILE2);
464 main (int argc, char *argv[])
469 "This program must be started with the operating mode as argument.\n");
471 "Usage: options\n" "options:\n" "1 = first loopback file\n"
472 "2 = second loopback file\n" "\n");
475 if (strstr (argv[1], "1") || strstr (argv[1], "2"))
476 return testmode (argc, argv);