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/test_transport_wlan_dummy.c
22 * @brief helper for the testcases for plugin_transport_wlan.c
23 * @author David Brodski
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
43 #include "gnunet_constants.h"
44 #include "gnunet_os_lib.h"
45 #include "gnunet_transport_plugin.h"
46 #include "transport.h"
47 #include "gnunet_util_lib.h"
48 #include "plugin_transport_wlan.h"
49 #include "gnunet_common.h"
50 #include "gnunet-transport-wlan-helper.h"
51 #include "gnunet_crypto_lib.h"
52 #include "wlan/loopback_helper.h"
53 #include "wlan/helper_common.h"
66 stdin_send(void *cls, void *client,
67 const struct GNUNET_MessageHeader *hdr)
69 struct sendbuf *write_pout = cls;
71 struct GNUNET_MessageHeader newheader;
76 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
77 + sizeof(struct Radiotap_rx);
79 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
81 fprintf(stderr, "Function stdin_send: wrong packet type\n");
84 if ((sendsize + write_pout->size) > MAXLINE * 2)
86 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
90 newheader.size = htons(sendsize);
91 newheader.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
93 to_start = write_pout->buf + write_pout->size;
94 memcpy(to_start, &newheader, sizeof(struct GNUNET_MessageHeader));
95 write_pout->size += sizeof(struct GNUNET_MessageHeader);
97 to_radiotap = to_start + sizeof(struct GNUNET_MessageHeader);
98 memset(to_radiotap, 0, sizeof (struct Radiotap_rx));
99 write_pout->size += sizeof(struct Radiotap_rx);
101 to_data = to_radiotap + sizeof(struct Radiotap_rx);
103 ((char *) hdr) + sizeof(struct Radiotap_Send) + sizeof(struct GNUNET_MessageHeader),
104 ntohs(hdr->size) - sizeof(struct Radiotap_Send) - sizeof(struct GNUNET_MessageHeader));
105 write_pout->size += ntohs(hdr->size) - sizeof(struct Radiotap_Send) - sizeof(struct GNUNET_MessageHeader);
109 file_in_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
111 struct sendbuf * write_std = cls;
114 sendsize = ntohs(hdr->size);
116 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
118 fprintf(stderr, "Function file_in_send: wrong packet type\n");
121 if ((sendsize + write_std->size) > MAXLINE * 2)
123 fprintf(stderr, "Function file_in_send: Packet too big for buffer\n");
127 memcpy(write_std->buf + write_std->size, hdr, sendsize);
128 write_std->size += sendsize;
135 testmode(int argc, char *argv[])
146 //make the fifos if needed
147 if (0 != stat(FIFO_FILE1, &st))
149 if (0 == stat(FIFO_FILE2, &st))
151 fprintf(stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
156 //unlink(FIFO_FILE1);
157 //unlink(FIFO_FILE2);
158 // FIXME: use mkfifo!
159 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
162 fprintf(stderr, "Error at mknode1 \n");
165 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
168 fprintf(stderr, "Error at mknode2 \n");
176 if (0 != stat(FIFO_FILE2, &st))
178 fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
184 if (strstr(argv[1], "1"))
186 //fprintf(stderr, "First\n");
188 fpin = fopen(FIFO_FILE1, "r");
191 fprintf(stderr, "fopen of read FIFO_FILE1\n");
194 fpout = fopen(FIFO_FILE2, "w");
197 fprintf(stderr, "fopen of write FIFO_FILE2\n");
205 //fprintf(stderr, "Second\n");
206 fpout = fopen(FIFO_FILE1, "w");
209 fprintf(stderr, "fopen of write FIFO_FILE1\n");
212 fpin = fopen(FIFO_FILE2, "r");
215 fprintf(stderr, "fopen of read FIFO_FILE2\n");
221 fdpin = fileno(fpin);
222 GNUNET_assert(fpin >= 0);
224 if (fdpin >= FD_SETSIZE)
226 fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin,
227 (unsigned int) FD_SETSIZE);
231 fdpout = fileno(fpout);
232 GNUNET_assert(fdpout >= 0 );
234 if (fdpout >= FD_SETSIZE)
236 fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout,
237 (unsigned int) FD_SETSIZE);
242 signal(SIGINT, &sigfunc);
243 signal(SIGTERM, &sigfunc);
245 char readbuf[MAXLINE];
247 struct sendbuf write_std;
251 struct sendbuf write_pout;
263 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
264 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
266 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
267 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
271 struct MacAddress macaddr;
273 //Send random mac address
274 macaddr.mac[0] = 0x13;
275 macaddr.mac[1] = 0x22;
276 macaddr.mac[2] = 0x33;
277 macaddr.mac[3] = 0x44;
278 macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256);
279 macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256);
281 write_std.size = send_mac_to_plugin(write_std.buf, macaddr.mac);
283 while (0 == closeprog)
293 // if output queue is empty
294 if (0 == write_pout.size)
296 FD_SET(STDIN_FILENO, &rfds);
299 if (0 == write_std.size)
301 FD_SET(fdpin, &rfds);
305 // if there is something to write
306 if (0 < write_std.size)
308 FD_SET(STDOUT_FILENO, &wfds);
309 maxfd = MAX(maxfd, STDOUT_FILENO);
312 if (0 < write_pout.size)
314 FD_SET(fdpout, &wfds);
315 maxfd = MAX(maxfd, fdpout);
318 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
320 if (-1 == retval && EINTR == errno)
326 fprintf(stderr, "select failed: %s\n", strerror(errno));
331 if (FD_ISSET(STDOUT_FILENO, &wfds))
333 ret = write(STDOUT_FILENO,
334 write_std.buf + write_std.pos,
335 write_std.size - write_std.pos);
339 fprintf(stderr, "Write ERROR to STDOUT\n");
344 write_std.pos += ret;
346 if (write_std.pos == write_std.size)
354 if (FD_ISSET(fdpout, &wfds))
357 write_pout.buf + write_pout.pos,
358 write_pout.size - write_pout.pos);
363 fprintf(stderr, "Write ERROR to fdpout\n");
367 write_pout.pos += ret;
369 if (write_pout.pos == write_pout.size)
377 if (FD_ISSET(STDIN_FILENO, &rfds))
379 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
384 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
386 else if (0 < readsize)
388 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
389 GNUNET_NO, GNUNET_NO);
399 if (FD_ISSET(fdpin, &rfds))
401 readsize = read(fdpin, readbuf, sizeof(readbuf));
406 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
409 else if (0 < readsize)
411 GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize,
412 GNUNET_NO, GNUNET_NO);
426 GNUNET_SERVER_mst_destroy(stdin_mst);
427 GNUNET_SERVER_mst_destroy(file_in_mst);
429 end: if (fpout != NULL)
444 main(int argc, char *argv[])
449 "This program must be started with the operating mode as argument.\n");
453 "1 = first loopback file\n"
454 "2 = second loopback file\n"
458 if (strstr(argv[1], "1") || strstr(argv[1], "2"))
459 return testmode(argc, argv);