1 #include <sys/socket.h>
18 #include "gnunet_constants.h"
19 #include "gnunet_os_lib.h"
20 #include "gnunet_transport_plugin.h"
21 #include "transport.h"
22 #include "gnunet_util_lib.h"
23 #include "plugin_transport_wlan.h"
24 #include "gnunet_common.h"
25 #include "gnunet-transport-wlan-helper.h"
26 #include "gnunet_crypto_lib.h"
27 #include "loopback_helper.h"
28 #include "helper_common.h"
41 stdin_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
43 struct sendbuf *write_pout = cls;
45 struct GNUNET_MessageHeader newheader;
46 unsigned char * from_data;
47 unsigned char * to_data;
48 //unsigned char * from_radiotap;
49 unsigned char * to_radiotap;
50 //unsigned char * from_start;
51 unsigned char * to_start;
53 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
54 + sizeof(struct Radiotap_rx);
56 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
58 fprintf(stderr, "Function stdin_send: wrong packet type\n");
61 if ((sendsize + write_pout->size) > MAXLINE * 2)
63 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
67 newheader.size = htons(sendsize);
68 newheader.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
70 to_start = write_pout->buf + write_pout->size;
71 to_radiotap = to_start + sizeof(struct GNUNET_MessageHeader);
72 to_data = to_radiotap + sizeof(struct Radiotap_rx);
74 from_data = ((unsigned char *) hdr) + sizeof(struct Radiotap_Send)
75 + sizeof(struct GNUNET_MessageHeader);
77 memcpy(to_start, &newheader, sizeof(struct GNUNET_MessageHeader));
78 write_pout->size += sizeof(struct GNUNET_MessageHeader);
80 write_pout->size += sizeof(struct Radiotap_rx);
82 memcpy(to_data, from_data, ntohs(hdr->size) - sizeof(struct Radiotap_Send)
83 - sizeof(struct GNUNET_MessageHeader));
84 write_pout->size += ntohs(hdr->size) - sizeof(struct Radiotap_Send)
85 - sizeof(struct GNUNET_MessageHeader);
89 file_in_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
91 struct sendbuf * write_std = cls;
94 sendsize = ntohs(hdr->size);
96 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
98 fprintf(stderr, "Function file_in_send: wrong packet type\n");
101 if ((sendsize + write_std->size) > MAXLINE * 2)
103 fprintf(stderr, "Function file_in_send: Packet too big for buffer\n");
107 memcpy(write_std->buf + write_std->size, hdr, sendsize);
108 write_std->size += sendsize;
116 testmode(int argc, char *argv[])
127 //make the fifos if needed
128 if (0 != stat(FIFO_FILE1, &st))
130 if (0 == stat(FIFO_FILE2, &st))
132 fprintf(stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
137 //unlink(FIFO_FILE1);
138 //unlink(FIFO_FILE2);
139 // FIXME: use mkfifo!
140 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
143 fprintf(stderr, "Error at mknode1 \n");
146 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
149 fprintf(stderr, "Error at mknode2 \n");
157 if (0 != stat(FIFO_FILE2, &st))
159 fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
165 if (strstr(argv[2], "1"))
167 //fprintf(stderr, "First\n");
169 fpin = fopen(FIFO_FILE1, "r");
172 fprintf(stderr, "fopen of read FIFO_FILE1\n");
175 fpout = fopen(FIFO_FILE2, "w");
178 fprintf(stderr, "fopen of write FIFO_FILE2\n");
186 //fprintf(stderr, "Second\n");
187 fpout = fopen(FIFO_FILE1, "w");
190 fprintf(stderr, "fopen of write FIFO_FILE1\n");
193 fpin = fopen(FIFO_FILE2, "r");
196 fprintf(stderr, "fopen of read FIFO_FILE2\n");
202 fdpin = fileno(fpin);
203 GNUNET_assert(fpin >= 0);
205 if (fdpin >= FD_SETSIZE)
207 fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin,
208 (unsigned int) FD_SETSIZE);
212 fdpout = fileno(fpout);
213 GNUNET_assert(fdpout >= 0 );
215 if (fdpout >= FD_SETSIZE)
217 fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout,
218 (unsigned int) FD_SETSIZE);
223 signal(SIGINT, &sigfunc);
224 signal(SIGTERM, &sigfunc);
226 char readbuf[MAXLINE];
228 struct sendbuf write_std;
232 struct sendbuf write_pout;
244 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
245 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
247 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
248 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
252 struct MacAddress macaddr;
254 //Send random mac address
255 macaddr.mac[0] = 0x13;
256 macaddr.mac[1] = 0x22;
257 macaddr.mac[2] = 0x33;
258 macaddr.mac[3] = 0x44;
259 macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256);
260 macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256);
262 write_std.size = send_mac_to_plugin((char *) write_std.buf, macaddr.mac);
268 retval = select(0, NULL, NULL, NULL, &tv);
273 // if there is something to write
275 FD_SET(STDOUT_FILENO, &wfds);
277 retval = select(STDOUT_FILENO + 1, NULL, &wfds, NULL, &tv);
279 if (FD_ISSET(STDOUT_FILENO, &wfds))
281 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos, write_std.size
287 fprintf(stderr, "Write ERROR to STDOUT");
292 write_std.pos += ret;
294 if (write_std.pos == write_std.size)
302 memcpy(&write_std.buf, &macmsg, sizeof(struct Wlan_Helper_Control_Message));
303 write_std.size = sizeof(struct Wlan_Helper_Control_Message);
309 select(0, NULL, NULL, NULL, &tv);
311 while (0 == closeprog)
321 // if output queue is empty
322 if (0 == write_pout.size)
324 FD_SET(STDIN_FILENO, &rfds);
327 if (0 == write_std.size)
329 FD_SET(fdpin, &rfds);
333 // if there is something to write
334 if (0 < write_std.size)
336 FD_SET(STDOUT_FILENO, &wfds);
337 maxfd = MAX(maxfd, STDOUT_FILENO);
340 if (0 < write_pout.size)
342 FD_SET(fdpout, &wfds);
343 maxfd = MAX(maxfd, fdpout);
346 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
348 if (-1 == retval && EINTR == errno)
354 fprintf(stderr, "select failed: %s\n", strerror(errno));
359 if (FD_ISSET(STDOUT_FILENO, &wfds))
361 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
362 write_std.size - write_std.pos);
367 fprintf(stderr, "Write ERROR to STDOUT\n");
372 write_std.pos += ret;
374 if (write_std.pos == write_std.size)
382 if (FD_ISSET(fdpout, &wfds))
384 ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
390 fprintf(stderr, "Write ERROR to fdpout\n");
394 write_pout.pos += ret;
396 if (write_pout.pos == write_pout.size)
404 if (FD_ISSET(STDIN_FILENO, &rfds))
406 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
411 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
413 else if (0 < readsize)
415 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
416 GNUNET_NO, GNUNET_NO);
426 if (FD_ISSET(fdpin, &rfds))
428 readsize = read(fdpin, readbuf, sizeof(readbuf));
433 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
436 else if (0 < readsize)
438 GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize,
439 GNUNET_NO, GNUNET_NO);
453 GNUNET_SERVER_mst_destroy(stdin_mst);
454 GNUNET_SERVER_mst_destroy(file_in_mst);
456 end: if (fpout != NULL)