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-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"
30 #define FIFO_FILE1 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_in"
31 #define FIFO_FILE2 "/tmp/test-transport/api-wlan-p1/WLAN_FIFO_out"
39 char buf[MAXLINE * 2];
50 (void) unlink (FIFO_FILE1);
51 (void) unlink (FIFO_FILE2);
56 * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin
57 * @param buffer pointer to buffer for the message
58 * @param mac pointer to the mac address
59 * @return number of bytes written
62 send_mac_to_plugin (char *buffer, struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
65 struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
67 memcpy (&macmsg.mac, (char *) mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
68 macmsg.hdr.size = htons (sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
69 macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
71 memcpy (buffer, &macmsg, sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage));
72 return sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage);
77 stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
79 struct sendbuf *write_pout = cls;
81 struct GNUNET_MessageHeader newheader;
87 ntohs (hdr->size) - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) +
88 sizeof (struct Radiotap_rx) + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame) +
89 sizeof (struct GNUNET_MessageHeader);
91 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
93 fprintf (stderr, "Function stdin_send: wrong packet type\n");
96 if ((sendsize + write_pout->size) > MAXLINE * 2)
98 fprintf (stderr, "Function stdin_send: Packet too big for buffer\n");
102 newheader.size = htons (sendsize);
103 newheader.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
105 to_start = write_pout->buf + write_pout->size;
106 memcpy (to_start, &newheader, sizeof (struct GNUNET_MessageHeader));
107 write_pout->size += sizeof (struct GNUNET_MessageHeader);
109 to_radiotap = to_start + sizeof (struct GNUNET_MessageHeader);
110 memset (to_radiotap, 0, sizeof (struct Radiotap_rx));
111 write_pout->size += sizeof (struct Radiotap_rx);
113 to_data = to_radiotap + sizeof (struct Radiotap_rx);
116 sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) -
117 - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame),
118 ntohs (hdr->size) - (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)
119 - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)));
121 ntohs (hdr->size) - (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)
122 - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
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;
151 main (int argc, char *argv[])
159 char readbuf[MAXLINE];
161 struct sendbuf write_std;
162 struct sendbuf write_pout;
169 struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
170 struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst;
171 struct GNUNET_TRANSPORT_WLAN_MacAddress macaddr;
176 "This program must be started with the operating mode (1 or 2) as the only argument.\n");
179 if ((0 != strstr (argv[1], "1")) && (0 != strstr (argv[1], "2")))
182 //make the fifos if needed
183 if (0 != stat (FIFO_FILE1, &st))
185 if (0 == stat (FIFO_FILE2, &st))
187 fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
191 erg = mkfifo (FIFO_FILE1, 0666);
194 fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE1,
198 erg = mkfifo (FIFO_FILE2, 0666);
201 fprintf (stderr, "Error in mkfifo(%s): %s\n", FIFO_FILE2,
209 if (0 != stat (FIFO_FILE2, &st))
211 fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
216 if (strstr (argv[1], "1"))
218 //fprintf(stderr, "First\n");
220 fpin = fopen (FIFO_FILE1, "r");
223 fprintf (stderr, "fopen of read FIFO_FILE1\n");
226 fpout = fopen (FIFO_FILE2, "w");
229 fprintf (stderr, "fopen of write FIFO_FILE2\n");
237 //fprintf(stderr, "Second\n");
238 fpout = fopen (FIFO_FILE1, "w");
241 fprintf (stderr, "fopen of write FIFO_FILE1\n");
244 fpin = fopen (FIFO_FILE2, "r");
247 fprintf (stderr, "fopen of read FIFO_FILE2\n");
253 fdpin = fileno (fpin);
254 GNUNET_assert (fpin >= 0);
256 if (fdpin >= FD_SETSIZE)
258 fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin,
259 (unsigned int) FD_SETSIZE);
263 fdpout = fileno (fpout);
264 GNUNET_assert (fdpout >= 0);
266 if (fdpout >= FD_SETSIZE)
268 fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout,
269 (unsigned int) FD_SETSIZE);
274 signal (SIGINT, &sigfunc);
275 signal (SIGTERM, &sigfunc);
281 stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout);
282 file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std);
284 //Send random mac address
285 macaddr.mac[0] = 0x13;
286 macaddr.mac[1] = 0x22;
287 macaddr.mac[2] = 0x33;
288 macaddr.mac[3] = 0x44;
289 macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256);
290 macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256);
291 write_std.size = send_mac_to_plugin (write_std.buf, &macaddr);
293 while (0 == closeprog)
301 // if output queue is empty
302 if (0 == write_pout.size)
304 FD_SET (STDIN_FILENO, &rfds);
305 maxfd = MAX (STDIN_FILENO, maxfd);
307 if (0 == write_std.size)
309 FD_SET (fdpin, &rfds);
310 maxfd = MAX (fdpin, maxfd);
313 // if there is something to write
314 if (0 < write_std.size)
316 FD_SET (STDOUT_FILENO, &wfds);
317 maxfd = MAX (maxfd, STDOUT_FILENO);
319 if (0 < write_pout.size)
321 FD_SET (fdpout, &wfds);
322 maxfd = MAX (maxfd, fdpout);
325 retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv);
326 if ((-1 == retval) && (EINTR == errno))
330 fprintf (stderr, "select failed: %s\n", strerror (errno));
335 if (FD_ISSET (STDOUT_FILENO, &wfds))
338 write (STDOUT_FILENO, write_std.buf + write_std.pos,
339 write_std.size - write_std.pos);
343 fprintf (stderr, "Write ERROR to STDOUT_FILENO: %s\n",
349 write_std.pos += ret;
351 if (write_std.pos == write_std.size)
359 if (FD_ISSET (fdpout, &wfds))
362 write (fdpout, write_pout.buf + write_pout.pos,
363 write_pout.size - write_pout.pos);
368 fprintf (stderr, "Write ERROR to fdpout: %s\n", strerror (errno));
372 write_pout.pos += ret;
374 if (write_pout.pos == write_pout.size)
382 if (FD_ISSET (STDIN_FILENO, &rfds))
384 readsize = read (STDIN_FILENO, readbuf, sizeof (readbuf));
389 fprintf (stderr, "Error reading from STDIN_FILENO: %s\n",
392 else if (0 < readsize)
394 GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, readsize,
395 GNUNET_NO, GNUNET_NO);
405 if (FD_ISSET (fdpin, &rfds))
407 readsize = read (fdpin, readbuf, sizeof (readbuf));
411 fprintf (stderr, "Error reading from fdpin: %s\n", strerror (errno));
414 else if (0 < readsize)
416 GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize,
417 GNUNET_NO, GNUNET_NO);
428 GNUNET_SERVER_mst_destroy (stdin_mst);
429 GNUNET_SERVER_mst_destroy (file_in_mst);
438 (void) unlink (FIFO_FILE1);
439 (void) unlink (FIFO_FILE2);