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 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
142 fprintf(stderr, "Error at mknode1 \n");
145 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
148 fprintf(stderr, "Error at mknode2 \n");
156 if (0 != stat(FIFO_FILE2, &st))
158 fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
164 if (strstr(argv[2], "1"))
166 //fprintf(stderr, "First\n");
168 fpin = fopen(FIFO_FILE1, "r");
171 fprintf(stderr, "fopen of read FIFO_FILE1\n");
174 fpout = fopen(FIFO_FILE2, "w");
177 fprintf(stderr, "fopen of write FIFO_FILE2\n");
185 //fprintf(stderr, "Second\n");
186 fpout = fopen(FIFO_FILE1, "w");
189 fprintf(stderr, "fopen of write FIFO_FILE1\n");
192 fpin = fopen(FIFO_FILE2, "r");
195 fprintf(stderr, "fopen of read FIFO_FILE2\n");
201 fdpin = fileno(fpin);
202 GNUNET_assert(fpin >= 0);
204 if (fdpin >= FD_SETSIZE)
206 fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin,
207 (unsigned int) FD_SETSIZE);
211 fdpout = fileno(fpout);
212 GNUNET_assert(fdpout >= 0 );
214 if (fdpout >= FD_SETSIZE)
216 fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout,
217 (unsigned int) FD_SETSIZE);
222 signal(SIGINT, &sigfunc);
223 signal(SIGTERM, &sigfunc);
225 char readbuf[MAXLINE];
227 struct sendbuf write_std;
231 struct sendbuf write_pout;
243 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
244 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
246 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
247 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
251 struct MacAddress macaddr;
253 //Send random mac address
254 macaddr.mac[0] = 0x13;
255 macaddr.mac[1] = 0x22;
256 macaddr.mac[2] = 0x33;
257 macaddr.mac[3] = 0x44;
258 macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256);
259 macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256);
261 write_std.size = send_mac_to_plugin((char *) write_std.buf, macaddr.mac);
267 retval = select(0, NULL, NULL, NULL, &tv);
272 // if there is something to write
274 FD_SET(STDOUT_FILENO, &wfds);
276 retval = select(STDOUT_FILENO + 1, NULL, &wfds, NULL, &tv);
278 if (FD_ISSET(STDOUT_FILENO, &wfds))
280 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos, write_std.size
286 fprintf(stderr, "Write ERROR to STDOUT");
291 write_std.pos += ret;
293 if (write_std.pos == write_std.size)
301 memcpy(&write_std.buf, &macmsg, sizeof(struct Wlan_Helper_Control_Message));
302 write_std.size = sizeof(struct Wlan_Helper_Control_Message);
308 select(0, NULL, NULL, NULL, &tv);
310 while (0 == closeprog)
320 // if output queue is empty
321 if (0 == write_pout.size)
323 FD_SET(STDIN_FILENO, &rfds);
326 if (0 == write_std.size)
328 FD_SET(fdpin, &rfds);
332 // if there is something to write
333 if (0 < write_std.size)
335 FD_SET(STDOUT_FILENO, &wfds);
336 maxfd = MAX(maxfd, STDOUT_FILENO);
339 if (0 < write_pout.size)
341 FD_SET(fdpout, &wfds);
342 maxfd = MAX(maxfd, fdpout);
345 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
347 if (-1 == retval && EINTR == errno)
353 fprintf(stderr, "select failed: %s\n", strerror(errno));
358 if (FD_ISSET(STDOUT_FILENO, &wfds))
360 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
361 write_std.size - write_std.pos);
366 fprintf(stderr, "Write ERROR to STDOUT\n");
371 write_std.pos += ret;
373 if (write_std.pos == write_std.size)
381 if (FD_ISSET(fdpout, &wfds))
383 ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
389 fprintf(stderr, "Write ERROR to fdpout\n");
393 write_pout.pos += ret;
395 if (write_pout.pos == write_pout.size)
403 if (FD_ISSET(STDIN_FILENO, &rfds))
405 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
410 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
412 else if (0 < readsize)
414 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
415 GNUNET_NO, GNUNET_NO);
425 if (FD_ISSET(fdpin, &rfds))
427 readsize = read(fdpin, readbuf, sizeof(readbuf));
432 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
435 else if (0 < readsize)
437 GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize,
438 GNUNET_NO, GNUNET_NO);
452 GNUNET_SERVER_mst_destroy(stdin_mst);
453 GNUNET_SERVER_mst_destroy(file_in_mst);
455 end: if (fpout != NULL)