doxygen
[oweals/gnunet.git] / src / transport / test_plugin_transport_wlan_dummy.c
1 /*\r
2  This file is part of GNUnet.\r
3  (C) 2010 Christian Grothoff (and other contributing authors)
4 \r
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.
9 \r
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.
14 \r
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
19  */\r
20 /**\r
21  * @file transport/test_transport_wlan_dummy.c\r
22  * @brief helper for the testcase for plugin_transport_wlan.c\r
23  * @author David Brodski\r
24  */\r
25 \r
26 #include "platform.h"\r
27 #include "gnunet_constants.h"\r
28 #include "gnunet_os_lib.h"\r
29 #include "gnunet_transport_plugin.h"\r
30 #include "transport.h"\r
31 #include "plugin_transport_wlan.h"\r
32 #include "gnunet_common.h"\r
33 #include "gnunet-transport-wlan-helper.h"\r
34 \r
35 #include <stdio.h>\r
36 #include <stdlib.h>\r
37 #include <sys/stat.h>\r
38 \r
39 #define FIFO_FILE1       "MYFIFOin"\r
40 #define FIFO_FILE2       "MYFIFOout"\r
41 #define MAXLINE         5000\r
42 \r
43 int closeprog = 0;\r
44 \r
45 void sigfunc(int sig)\r
46 {\r
47 \r
48  if(sig != SIGINT || sig != SIGTERM || sig != SIGKILL)\r
49    return;\r
50  else\r
51   {\r
52    closeprog = 1;\r
53    exit(0);\r
54    }\r
55 }\r
56 \r
57 \r
58 \r
59 int\r
60 main(int argc, char *argv[])\r
61 {\r
62   struct stat st;\r
63   struct stat st2;\r
64   int erg;\r
65   int first;\r
66   FILE *fpin;\r
67   FILE *fpout;\r
68   pid_t pid;\r
69 \r
70   perror("Test");\r
71 \r
72 \r
73   //make the fifos if needed\r
74   if (stat(FIFO_FILE1, &st) != 0)\r
75     {\r
76       if (stat(FIFO_FILE2, &st2) == 0)\r
77         {\r
78           perror("FIFO 2 exists, but FIFO 1 not, blub");\r
79           exit(1);\r
80         }\r
81       first = 1;\r
82       perror("First");\r
83       umask(0);\r
84       erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);\r
85       erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);\r
86 \r
87       if ((fpin = fopen(FIFO_FILE1, "r")) == NULL)\r
88         {\r
89           perror("fopen");\r
90           exit(1);\r
91         }\r
92       if ((fpout = fopen(FIFO_FILE2, "w")) == NULL)\r
93         {\r
94           perror("fopen");\r
95           exit(1);\r
96         }\r
97     }\r
98   else\r
99     {\r
100       first = 0;\r
101       perror("Second");\r
102       if (stat(FIFO_FILE2, &st2) != 0)\r
103         {\r
104           perror("FIFO 1 exists, but FIFO 2 not, mäh");\r
105           exit(1);\r
106         }\r
107       if ((fpout = fopen(FIFO_FILE1, "w")) == NULL)\r
108         {\r
109           perror("fopen");\r
110           exit(1);\r
111         }\r
112       if ((fpin = fopen(FIFO_FILE2, "r")) == NULL)\r
113         {\r
114           perror("fopen");\r
115           exit(1);\r
116         }\r
117 \r
118     }\r
119 \r
120   // fork\r
121 \r
122   if ((pid = fork()) < 0)\r
123     {\r
124       perror("FORK ERROR");\r
125 \r
126       //clean up\r
127       if (first == 1)\r
128               {\r
129                 unlink(FIFO_FILE1);\r
130                 unlink(FIFO_FILE2);\r
131               }\r
132       fclose(fpin);\r
133       fclose(fpout);\r
134       return -3;\r
135     }\r
136   else if (pid == 0) // CHILD PROCESS\r
137     {\r
138     perror("Child");\r
139       signal(SIGINT, sigfunc);\r
140       signal(SIGTERM, sigfunc);\r
141       signal(SIGKILL, sigfunc);\r
142       int rv = 0;\r
143       int readc = 0;\r
144       int pos = 0;\r
145       char line[MAXLINE];\r
146 \r
147       while (closeprog == 0)\r
148         {\r
149           readc = 0;\r
150 \r
151           while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){\r
152             if ((rv = read(STDIN_FILENO, line, MAXLINE)) < 0)\r
153               {\r
154                 perror("READ ERROR FROM STDIN");\r
155               }\r
156             readc += rv;\r
157           }\r
158 \r
159           pos = 0;\r
160 \r
161           //fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout);\r
162 \r
163           //pos += sizeof(struct GNUNET_MessageHeader);\r
164 \r
165           //do not send radiotap header\r
166           pos += sizeof( struct RadiotapHeader);\r
167 \r
168           while (pos < readc)\r
169             {\r
170               pos += fwrite(&line[pos], 1, readc - pos, fpout);\r
171             }\r
172         }\r
173 \r
174 \r
175       //clean up\r
176       fclose(fpout);\r
177     }\r
178   else // PARENT PROCESS\r
179     {\r
180     perror("Parent");\r
181       signal(SIGINT, sigfunc);\r
182       signal(SIGTERM, sigfunc);\r
183       signal(SIGKILL, sigfunc);\r
184       int rv = 0;\r
185       ssize_t pos = 0;\r
186       char line[MAXLINE];\r
187       struct Wlan_Helper_Control_Message macmsg;\r
188 \r
189 \r
190       //Send random mac address\r
191       macmsg.mac.mac[0] = 0x13;\r
192       macmsg.mac.mac[1] = 0x22;\r
193       macmsg.mac.mac[2] = 0x33;\r
194       macmsg.mac.mac[3] = 0x44;\r
195       macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);\r
196       macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);\r
197       macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message);\r
198 \r
199       pos = 0;\r
200       /*\r
201       while (pos < sizeof(struct Wlan_Helper_Control_Message))\r
202         {\r
203           pos += write(STDOUT_FILENO, &macmsg + pos, sizeof(struct Wlan_Helper_Control_Message) - pos);\r
204         }\r
205       */\r
206       while (closeprog == 0)\r
207         {\r
208           if ((rv = fread(line, 1, MAXLINE, fpin)) < 0)\r
209             {\r
210               perror("READ ERROR FROM fpin");\r
211             }\r
212 \r
213           pos = 0;\r
214           while (pos < rv)\r
215             {\r
216               pos += write(STDOUT_FILENO, &line[pos], rv - pos);\r
217             }\r
218         }\r
219 \r
220 \r
221       //clean up\r
222       fclose(fpin);\r
223 \r
224       if (first == 1)\r
225         {\r
226           unlink(FIFO_FILE1);\r
227           unlink(FIFO_FILE2);\r
228         }\r
229     }\r
230 \r
231   // Write the input to the output\r
232 \r
233   return (0);\r
234 }\r
235 \r