3 #include <sys/socket.h>
19 #include "gnunet_constants.h"
20 #include "gnunet_os_lib.h"
21 #include "gnunet_transport_plugin.h"
22 #include "transport.h"
23 #include "gnunet_util_lib.h"
24 #include "plugin_transport_wlan.h"
25 #include "gnunet_common.h"
26 #include "gnunet-transport-wlan-helper.h"
27 #include "gnunet_crypto_lib.h"
28 #include "loopback_helper.h"
29 #include "helper_common.h"
42 stdin_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
44 struct sendbuf *write_pout = cls;
46 struct GNUNET_MessageHeader newheader;
50 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send);
52 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
54 fprintf(stderr, "Function stdin_send: wrong packet type\n");
57 if ((sendsize + write_pout->size) > MAXLINE * 2)
59 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
63 newheader.size = htons(sendsize);
64 newheader.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
66 to = write_pout->buf + write_pout->size;
67 memcpy(to, &newheader, sizeof(struct GNUNET_MessageHeader));
68 write_pout->size += sizeof(struct GNUNET_MessageHeader);
70 from = ((unsigned char *) hdr) + sizeof(struct Radiotap_Send)
71 + sizeof(struct GNUNET_MessageHeader);
72 to = write_pout->buf + write_pout->size;
73 memcpy(to, from, sendsize - sizeof(struct GNUNET_MessageHeader));
74 write_pout->size += sendsize - sizeof(struct GNUNET_MessageHeader);
78 file_in_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
80 struct sendbuf * write_std = cls;
83 sendsize = ntohs(hdr->size);
85 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
87 fprintf(stderr, "Function file_in_send: wrong packet type\n");
90 if ((sendsize + write_std->size) > MAXLINE * 2)
92 fprintf(stderr, "Function file_in_send: Packet too big for buffer\n");
96 memcpy(write_std->buf + write_std->size, hdr, sendsize);
97 write_std->size += sendsize;
101 testmode(int argc, char *argv[])
112 //make the fifos if needed
113 if (0 != stat(FIFO_FILE1, &st))
115 if (0 == stat(FIFO_FILE2, &st))
117 fprintf(stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n");
122 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
123 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
129 if (0 != stat(FIFO_FILE2, &st))
131 fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n");
137 if (strstr(argv[2], "1"))
139 //fprintf(stderr, "First\n");
141 fpin = fopen(FIFO_FILE1, "r");
144 fprintf(stderr, "fopen of read FIFO_FILE1\n");
147 if (NULL == (fpout = fopen(FIFO_FILE2, "w")))
149 fprintf(stderr, "fopen of write FIFO_FILE2\n");
157 //fprintf(stderr, "Second\n");
158 if (NULL == (fpout = fopen(FIFO_FILE1, "w")))
160 fprintf(stderr, "fopen of write FIFO_FILE1\n");
163 if (NULL == (fpin = fopen(FIFO_FILE2, "r")))
165 fprintf(stderr, "fopen of read FIFO_FILE2\n");
171 fdpin = fileno(fpin);
172 if (fdpin >= FD_SETSIZE)
174 fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin,
175 (unsigned int) FD_SETSIZE);
180 fdpout = fileno(fpout);
181 if (fdpout >= FD_SETSIZE)
183 fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout,
184 (unsigned int) FD_SETSIZE);
190 signal(SIGINT, &sigfunc);
191 signal(SIGTERM, &sigfunc);
193 char readbuf[MAXLINE];
195 struct sendbuf write_std;
199 struct sendbuf write_pout;
211 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
212 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
214 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
215 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
219 struct MacAddress macaddr;
221 //Send random mac address
222 macaddr.mac[0] = 0x13;
223 macaddr.mac[1] = 0x22;
224 macaddr.mac[2] = 0x33;
225 macaddr.mac[3] = 0x44;
226 macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256);
227 macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256);
229 write_std.size = send_mac_to_plugin((char *) write_std.buf, macaddr.mac);
235 retval = select(0, NULL, NULL, NULL, &tv);
240 // if there is something to write
242 FD_SET(STDOUT_FILENO, &wfds);
244 retval = select(STDOUT_FILENO + 1, NULL, &wfds, NULL, &tv);
246 if (FD_ISSET(STDOUT_FILENO, &wfds))
248 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos, write_std.size
254 fprintf(stderr, "Write ERROR to STDOUT");
259 write_std.pos += ret;
261 if (write_std.pos == write_std.size)
269 memcpy(&write_std.buf, &macmsg, sizeof(struct Wlan_Helper_Control_Message));
270 write_std.size = sizeof(struct Wlan_Helper_Control_Message);
276 retval = select(0, NULL, NULL, NULL, &tv);
278 while (0 == closeprog)
288 // if output queue is empty
289 if (0 == write_pout.size)
291 FD_SET(STDIN_FILENO, &rfds);
294 if (0 == write_std.size)
296 FD_SET(fdpin, &rfds);
300 // if there is something to write
301 if (0 < write_std.size)
303 FD_SET(STDOUT_FILENO, &wfds);
304 maxfd = MAX(maxfd, STDOUT_FILENO);
307 if (0 < write_pout.size)
309 FD_SET(fdpout, &wfds);
310 maxfd = MAX(maxfd, fdpout);
313 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
315 if (-1 == retval && EINTR == errno)
321 fprintf(stderr, "select failed: %s\n", strerror(errno));
325 if (FD_ISSET(STDOUT_FILENO, &wfds))
327 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
328 write_std.size - write_std.pos);
333 fprintf(stderr, "Write ERROR to STDOUT\n");
338 write_std.pos += ret;
340 if (write_std.pos == write_std.size)
348 if (FD_ISSET(fdpout, &wfds))
350 ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
356 fprintf(stderr, "Write ERROR to fdpout\n");
360 write_pout.pos += ret;
362 if (write_pout.pos == write_pout.size)
370 if (FD_ISSET(STDIN_FILENO, &rfds))
372 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
377 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
379 else if (0 < readsize)
381 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
382 GNUNET_NO, GNUNET_NO);
392 if (FD_ISSET(fdpin, &rfds))
394 readsize = read(fdpin, readbuf, sizeof(readbuf));
399 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
402 else if (0 < readsize)
404 GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize,
405 GNUNET_NO, GNUNET_NO);