2 This file is part of GNUnet.
\r
3 (C) 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
\r
6 it under the terms of the GNU General Public License as published
\r
7 by the Free Software Foundation; either version 3, or (at your
\r
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
\r
11 WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
\r
16 along with GNUnet; see the file COPYING. If not, write to the
\r
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
\r
18 Boston, MA 02111-1307, USA.
\r
21 * @file transport/test_transport_wlan_dummy.c
\r
22 * @brief helper for the testcases for plugin_transport_wlan.c
\r
23 * @author David Brodski
\r
26 #include <sys/socket.h>
\r
27 #include <sys/ioctl.h>
\r
28 #include <sys/types.h>
\r
29 #include <sys/wait.h>
\r
30 #include <sys/time.h>
\r
31 #include <sys/stat.h>
\r
42 #include "platform.h"
\r
43 #include "gnunet_constants.h"
\r
44 #include "gnunet_os_lib.h"
\r
45 #include "gnunet_transport_plugin.h"
\r
46 #include "transport.h"
\r
47 #include "gnunet_util_lib.h"
\r
48 #include "plugin_transport_wlan.h"
\r
49 #include "gnunet_common.h"
\r
50 #include "gnunet-transport-wlan-helper.h"
\r
51 #include "gnunet_crypto_lib.h"
\r
52 #include "wlan/loopback_helper.h"
\r
53 #include "wlan/helper_common.h"
\r
66 stdin_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
\r
68 struct sendbuf *write_pout = cls;
\r
70 struct GNUNET_MessageHeader newheader;
\r
71 unsigned char * from_data;
\r
72 unsigned char * to_data;
\r
73 //unsigned char * from_radiotap;
\r
74 unsigned char * to_radiotap;
\r
75 //unsigned char * from_start;
\r
76 unsigned char * to_start;
\r
78 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
\r
79 + sizeof(struct Radiotap_rx);
\r
81 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
\r
83 fprintf(stderr, "Function stdin_send: wrong packet type\n");
\r
86 if ((sendsize + write_pout->size) > MAXLINE * 2)
\r
88 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
\r
92 newheader.size = htons(sendsize);
\r
93 newheader.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
\r
95 to_start = write_pout->buf + write_pout->size;
\r
96 to_radiotap = to_start + sizeof(struct GNUNET_MessageHeader);
\r
97 to_data = to_radiotap + sizeof(struct Radiotap_rx);
\r
99 from_data = ((unsigned char *) hdr) + sizeof(struct Radiotap_Send)
\r
100 + sizeof(struct GNUNET_MessageHeader);
\r
102 memcpy(to_start, &newheader, sizeof(struct GNUNET_MessageHeader));
\r
103 write_pout->size += sizeof(struct GNUNET_MessageHeader);
\r
105 write_pout->size += sizeof(struct Radiotap_rx);
\r
107 memcpy(to_data, from_data, ntohs(hdr->size) - sizeof(struct Radiotap_Send)
\r
108 - sizeof(struct GNUNET_MessageHeader));
\r
109 write_pout->size += ntohs(hdr->size) - sizeof(struct Radiotap_Send)
\r
110 - sizeof(struct GNUNET_MessageHeader);
\r
114 file_in_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
\r
116 struct sendbuf * write_std = cls;
\r
119 sendsize = ntohs(hdr->size);
\r
121 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
\r
123 fprintf(stderr, "Function file_in_send: wrong packet type\n");
\r
126 if ((sendsize + write_std->size) > MAXLINE * 2)
\r
128 fprintf(stderr, "Function file_in_send: Packet too big for buffer\n");
\r
132 memcpy(write_std->buf + write_std->size, hdr, sendsize);
\r
133 write_std->size += sendsize;
\r
140 testmode(int argc, char *argv[])
\r
146 FILE *fpout = NULL;
\r
151 //make the fifos if needed
\r
152 if (0 != stat(FIFO_FILE1, &st))
\r
154 if (0 == stat(FIFO_FILE2, &st))
\r
156 fprintf(stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
\r
161 //unlink(FIFO_FILE1);
\r
162 //unlink(FIFO_FILE2);
\r
163 // FIXME: use mkfifo!
\r
164 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
\r
167 fprintf(stderr, "Error at mknode1 \n");
\r
170 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
\r
173 fprintf(stderr, "Error at mknode2 \n");
\r
181 if (0 != stat(FIFO_FILE2, &st))
\r
183 fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
\r
189 if (strstr(argv[1], "1"))
\r
191 //fprintf(stderr, "First\n");
\r
193 fpin = fopen(FIFO_FILE1, "r");
\r
196 fprintf(stderr, "fopen of read FIFO_FILE1\n");
\r
199 fpout = fopen(FIFO_FILE2, "w");
\r
202 fprintf(stderr, "fopen of write FIFO_FILE2\n");
\r
210 //fprintf(stderr, "Second\n");
\r
211 fpout = fopen(FIFO_FILE1, "w");
\r
214 fprintf(stderr, "fopen of write FIFO_FILE1\n");
\r
217 fpin = fopen(FIFO_FILE2, "r");
\r
220 fprintf(stderr, "fopen of read FIFO_FILE2\n");
\r
226 fdpin = fileno(fpin);
\r
227 GNUNET_assert(fpin >= 0);
\r
229 if (fdpin >= FD_SETSIZE)
\r
231 fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin,
\r
232 (unsigned int) FD_SETSIZE);
\r
236 fdpout = fileno(fpout);
\r
237 GNUNET_assert(fdpout >= 0 );
\r
239 if (fdpout >= FD_SETSIZE)
\r
241 fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout,
\r
242 (unsigned int) FD_SETSIZE);
\r
247 signal(SIGINT, &sigfunc);
\r
248 signal(SIGTERM, &sigfunc);
\r
250 char readbuf[MAXLINE];
\r
252 struct sendbuf write_std;
\r
253 write_std.size = 0;
\r
256 struct sendbuf write_pout;
\r
257 write_pout.size = 0;
\r
258 write_pout.pos = 0;
\r
268 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
\r
269 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
\r
271 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
\r
272 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
\r
276 struct MacAddress macaddr;
\r
278 //Send random mac address
\r
279 macaddr.mac[0] = 0x13;
\r
280 macaddr.mac[1] = 0x22;
\r
281 macaddr.mac[2] = 0x33;
\r
282 macaddr.mac[3] = 0x44;
\r
283 macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256);
\r
284 macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256);
\r
286 write_std.size = send_mac_to_plugin((char *) write_std.buf, macaddr.mac);
\r
288 while (0 == closeprog)
\r
298 // if output queue is empty
\r
299 if (0 == write_pout.size)
\r
301 FD_SET(STDIN_FILENO, &rfds);
\r
304 if (0 == write_std.size)
\r
306 FD_SET(fdpin, &rfds);
\r
310 // if there is something to write
\r
311 if (0 < write_std.size)
\r
313 FD_SET(STDOUT_FILENO, &wfds);
\r
314 maxfd = MAX(maxfd, STDOUT_FILENO);
\r
317 if (0 < write_pout.size)
\r
319 FD_SET(fdpout, &wfds);
\r
320 maxfd = MAX(maxfd, fdpout);
\r
323 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
\r
325 if (-1 == retval && EINTR == errno)
\r
331 fprintf(stderr, "select failed: %s\n", strerror(errno));
\r
336 if (FD_ISSET(STDOUT_FILENO, &wfds))
\r
338 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
\r
339 write_std.size - write_std.pos);
\r
344 fprintf(stderr, "Write ERROR to STDOUT\n");
\r
349 write_std.pos += ret;
\r
350 // check if finished
\r
351 if (write_std.pos == write_std.size)
\r
354 write_std.size = 0;
\r
359 if (FD_ISSET(fdpout, &wfds))
\r
361 ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
\r
367 fprintf(stderr, "Write ERROR to fdpout\n");
\r
371 write_pout.pos += ret;
\r
372 // check if finished
\r
373 if (write_pout.pos == write_pout.size)
\r
375 write_pout.pos = 0;
\r
376 write_pout.size = 0;
\r
381 if (FD_ISSET(STDIN_FILENO, &rfds))
\r
383 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
\r
388 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
\r
390 else if (0 < readsize)
\r
392 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
\r
393 GNUNET_NO, GNUNET_NO);
\r
403 if (FD_ISSET(fdpin, &rfds))
\r
405 readsize = read(fdpin, readbuf, sizeof(readbuf));
\r
410 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
\r
413 else if (0 < readsize)
\r
415 GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize,
\r
416 GNUNET_NO, GNUNET_NO);
\r
430 GNUNET_SERVER_mst_destroy(stdin_mst);
\r
431 GNUNET_SERVER_mst_destroy(file_in_mst);
\r
433 end: if (fpout != NULL)
\r
440 unlink(FIFO_FILE1);
\r
441 unlink(FIFO_FILE2);
\r
448 main(int argc, char *argv[])
\r
453 "This program must be started with the operating mode as argument.\n");
\r
457 "1 = first loopback file\n"
\r
458 "2 = second loopback file\n"
\r
462 if (strstr(argv[1], "1") || strstr(argv[1], "2"))
\r
463 return testmode(argc, argv);
\r