3 #include <sys/socket.h>
20 #include "gnunet_constants.h"
21 #include "gnunet_os_lib.h"
22 #include "gnunet_transport_plugin.h"
23 #include "transport.h"
24 #include "gnunet_util_lib.h"
25 #include "plugin_transport_wlan.h"
26 #include "gnunet_common.h"
27 #include "gnunet-transport-wlan-helper.h"
28 #include "gnunet_crypto_lib.h"
29 #include "loopback_helper.h"
30 #include "helper_common.h"
43 stdin_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
45 struct sendbuf *write_pout = cls;
47 struct GNUNET_MessageHeader newheader;
51 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send);
53 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
55 fprintf(stderr, "Function stdin_send: wrong packet type\n");
58 if ((sendsize + write_pout->size) > MAXLINE * 2)
60 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
64 newheader.size = htons(sendsize);
65 newheader.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
67 to = write_pout->buf + write_pout->size;
68 memcpy(to, &newheader, sizeof(struct GNUNET_MessageHeader));
69 write_pout->size += sizeof(struct GNUNET_MessageHeader);
71 from = ((unsigned char *) hdr) + sizeof(struct Radiotap_Send)
72 + sizeof(struct GNUNET_MessageHeader);
73 to = write_pout->buf + write_pout->size;
74 memcpy(to, from, sendsize - sizeof(struct GNUNET_MessageHeader));
75 write_pout->size += sendsize - sizeof(struct GNUNET_MessageHeader);
79 file_in_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
81 struct sendbuf * write_std = cls;
84 sendsize = ntohs(hdr->size);
86 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
88 fprintf(stderr, "Function file_in_send: wrong packet type\n");
91 if ((sendsize + write_std->size) > MAXLINE * 2)
93 fprintf(stderr, "Function file_in_send: Packet too big for buffer\n");
97 memcpy(write_std->buf + write_std->size, hdr, sendsize);
98 write_std->size += sendsize;
102 testmode(int argc, char *argv[])
113 //make the fifos if needed
114 if (0 != stat(FIFO_FILE1, &st))
116 if (0 == stat(FIFO_FILE2, &st))
118 fprintf(stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
123 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
124 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
130 if (0 != stat(FIFO_FILE2, &st))
132 fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
138 if (strstr(argv[2], "1"))
140 //fprintf(stderr, "First\n");
142 fpin = fopen(FIFO_FILE1, "r");
145 fprintf(stderr, "fopen of read FIFO_FILE1\n");
148 if (NULL == (fpout = fopen(FIFO_FILE2, "w")))
150 fprintf(stderr, "fopen of write FIFO_FILE2\n");
158 //fprintf(stderr, "Second\n");
159 if (NULL == (fpout = fopen(FIFO_FILE1, "w")))
161 fprintf(stderr, "fopen of write FIFO_FILE1\n");
164 if (NULL == (fpin = fopen(FIFO_FILE2, "r")))
166 fprintf(stderr, "fopen of read FIFO_FILE2\n");
172 fdpin = fileno(fpin);
173 if (fdpin >= FD_SETSIZE)
175 fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin,
176 (unsigned int) FD_SETSIZE);
181 fdpout = fileno(fpout);
182 if (fdpout >= FD_SETSIZE)
184 fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout,
185 (unsigned int) FD_SETSIZE);
191 signal(SIGINT, &sigfunc);
192 signal(SIGTERM, &sigfunc);
194 char readbuf[MAXLINE];
196 struct sendbuf write_std;
200 struct sendbuf write_pout;
212 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
213 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
215 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
216 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
220 struct MacAddress macaddr;
222 //Send random mac address
223 macaddr.mac[0] = 0x13;
224 macaddr.mac[1] = 0x22;
225 macaddr.mac[2] = 0x33;
226 macaddr.mac[3] = 0x44;
227 macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256);
228 macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256);
230 write_std.size = send_mac_to_plugin((char *) write_std.buf, macaddr.mac);
236 retval = select(0, NULL, NULL, NULL, &tv);
241 // if there is something to write
243 FD_SET(STDOUT_FILENO, &wfds);
245 retval = select(STDOUT_FILENO + 1, NULL, &wfds, NULL, &tv);
247 if (FD_ISSET(STDOUT_FILENO, &wfds))
249 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos, write_std.size
255 fprintf(stderr, "Write ERROR to STDOUT");
260 write_std.pos += ret;
262 if (write_std.pos == write_std.size)
270 memcpy(&write_std.buf, &macmsg, sizeof(struct Wlan_Helper_Control_Message));
271 write_std.size = sizeof(struct Wlan_Helper_Control_Message);
277 retval = select(0, NULL, NULL, NULL, &tv);
279 while (0 == closeprog)
289 // if output queue is empty
290 if (0 == write_pout.size)
292 FD_SET(STDIN_FILENO, &rfds);
295 if (0 == write_std.size)
297 FD_SET(fdpin, &rfds);
301 // if there is something to write
302 if (0 < write_std.size)
304 FD_SET(STDOUT_FILENO, &wfds);
305 maxfd = MAX(maxfd, STDOUT_FILENO);
308 if (0 < write_pout.size)
310 FD_SET(fdpout, &wfds);
311 maxfd = MAX(maxfd, fdpout);
314 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
316 if (-1 == retval && EINTR == errno)
322 fprintf(stderr, "select failed: %s\n", strerror(errno));
326 if (FD_ISSET(STDOUT_FILENO, &wfds))
328 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
329 write_std.size - write_std.pos);
334 fprintf(stderr, "Write ERROR to STDOUT\n");
339 write_std.pos += ret;
341 if (write_std.pos == write_std.size)
349 if (FD_ISSET(fdpout, &wfds))
351 ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
357 fprintf(stderr, "Write ERROR to fdpout\n");
361 write_pout.pos += ret;
363 if (write_pout.pos == write_pout.size)
371 if (FD_ISSET(STDIN_FILENO, &rfds))
373 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
378 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
380 else if (0 < readsize)
382 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
383 GNUNET_NO, GNUNET_NO);
393 if (FD_ISSET(fdpin, &rfds))
395 readsize = read(fdpin, readbuf, sizeof(readbuf));
400 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
403 else if (0 < readsize)
405 GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize,
406 GNUNET_NO, GNUNET_NO);