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, const struct GNUNET_MessageHeader *hdr)
68 struct sendbuf *write_pout = cls;
70 struct GNUNET_MessageHeader newheader;
71 unsigned char * from_data;
72 unsigned char * to_data;
73 //unsigned char * from_radiotap;
74 unsigned char * to_radiotap;
75 //unsigned char * from_start;
76 unsigned char * to_start;
78 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
79 + sizeof(struct Radiotap_rx);
81 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
83 fprintf(stderr, "Function stdin_send: wrong packet type\n");
86 if ((sendsize + write_pout->size) > MAXLINE * 2)
88 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
92 newheader.size = htons(sendsize);
93 newheader.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
95 to_start = write_pout->buf + write_pout->size;
96 to_radiotap = to_start + sizeof(struct GNUNET_MessageHeader);
97 to_data = to_radiotap + sizeof(struct Radiotap_rx);
99 from_data = ((unsigned char *) hdr) + sizeof(struct Radiotap_Send)
100 + sizeof(struct GNUNET_MessageHeader);
102 memcpy(to_start, &newheader, sizeof(struct GNUNET_MessageHeader));
103 write_pout->size += sizeof(struct GNUNET_MessageHeader);
105 write_pout->size += sizeof(struct Radiotap_rx);
107 memcpy(to_data, from_data, ntohs(hdr->size) - sizeof(struct Radiotap_Send)
108 - sizeof(struct GNUNET_MessageHeader));
109 write_pout->size += ntohs(hdr->size) - sizeof(struct Radiotap_Send)
110 - sizeof(struct GNUNET_MessageHeader);
114 file_in_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
116 struct sendbuf * write_std = cls;
119 sendsize = ntohs(hdr->size);
121 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
123 fprintf(stderr, "Function file_in_send: wrong packet type\n");
126 if ((sendsize + write_std->size) > MAXLINE * 2)
128 fprintf(stderr, "Function file_in_send: Packet too big for buffer\n");
132 memcpy(write_std->buf + write_std->size, hdr, sendsize);
133 write_std->size += sendsize;
140 testmode(int argc, char *argv[])
151 //make the fifos if needed
152 if (0 != stat(FIFO_FILE1, &st))
154 if (0 == stat(FIFO_FILE2, &st))
156 fprintf(stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
161 //unlink(FIFO_FILE1);
162 //unlink(FIFO_FILE2);
163 // FIXME: use mkfifo!
164 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
167 fprintf(stderr, "Error at mknode1 \n");
170 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
173 fprintf(stderr, "Error at mknode2 \n");
181 if (0 != stat(FIFO_FILE2, &st))
183 fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
189 if (strstr(argv[1], "1"))
191 //fprintf(stderr, "First\n");
193 fpin = fopen(FIFO_FILE1, "r");
196 fprintf(stderr, "fopen of read FIFO_FILE1\n");
199 fpout = fopen(FIFO_FILE2, "w");
202 fprintf(stderr, "fopen of write FIFO_FILE2\n");
210 //fprintf(stderr, "Second\n");
211 fpout = fopen(FIFO_FILE1, "w");
214 fprintf(stderr, "fopen of write FIFO_FILE1\n");
217 fpin = fopen(FIFO_FILE2, "r");
220 fprintf(stderr, "fopen of read FIFO_FILE2\n");
226 fdpin = fileno(fpin);
227 GNUNET_assert(fpin >= 0);
229 if (fdpin >= FD_SETSIZE)
231 fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin,
232 (unsigned int) FD_SETSIZE);
236 fdpout = fileno(fpout);
237 GNUNET_assert(fdpout >= 0 );
239 if (fdpout >= FD_SETSIZE)
241 fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout,
242 (unsigned int) FD_SETSIZE);
247 signal(SIGINT, &sigfunc);
248 signal(SIGTERM, &sigfunc);
250 char readbuf[MAXLINE];
252 struct sendbuf write_std;
256 struct sendbuf write_pout;
268 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
269 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
271 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
272 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
276 struct MacAddress macaddr;
278 //Send random mac address
279 macaddr.mac[0] = 0x13;
280 macaddr.mac[1] = 0x22;
281 macaddr.mac[2] = 0x33;
282 macaddr.mac[3] = 0x44;
283 macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256);
284 macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256);
286 write_std.size = send_mac_to_plugin((char *) write_std.buf, macaddr.mac);
288 while (0 == closeprog)
298 // if output queue is empty
299 if (0 == write_pout.size)
301 FD_SET(STDIN_FILENO, &rfds);
304 if (0 == write_std.size)
306 FD_SET(fdpin, &rfds);
310 // if there is something to write
311 if (0 < write_std.size)
313 FD_SET(STDOUT_FILENO, &wfds);
314 maxfd = MAX(maxfd, STDOUT_FILENO);
317 if (0 < write_pout.size)
319 FD_SET(fdpout, &wfds);
320 maxfd = MAX(maxfd, fdpout);
323 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
325 if (-1 == retval && EINTR == errno)
331 fprintf(stderr, "select failed: %s\n", strerror(errno));
336 if (FD_ISSET(STDOUT_FILENO, &wfds))
338 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
339 write_std.size - write_std.pos);
344 fprintf(stderr, "Write ERROR to STDOUT\n");
349 write_std.pos += ret;
351 if (write_std.pos == write_std.size)
359 if (FD_ISSET(fdpout, &wfds))
361 ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
367 fprintf(stderr, "Write ERROR to fdpout\n");
371 write_pout.pos += ret;
373 if (write_pout.pos == write_pout.size)
381 if (FD_ISSET(STDIN_FILENO, &rfds))
383 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
388 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
390 else if (0 < readsize)
392 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
393 GNUNET_NO, GNUNET_NO);
403 if (FD_ISSET(fdpin, &rfds))
405 readsize = read(fdpin, readbuf, sizeof(readbuf));
410 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
413 else if (0 < readsize)
415 GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize,
416 GNUNET_NO, GNUNET_NO);
430 GNUNET_SERVER_mst_destroy(stdin_mst);
431 GNUNET_SERVER_mst_destroy(file_in_mst);
433 end: if (fpout != NULL)
448 main(int argc, char *argv[])
453 "This program must be started with the operating mode as argument.\n");
457 "1 = first loopback file\n"
458 "2 = second loopback file\n"
462 if (strstr(argv[1], "1") || strstr(argv[1], "2"))
463 return testmode(argc, argv);