fix: function and variable with same name
[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    }\r
54 }\r
55 \r
56 \r
57 \r
58 int\r
59 main(int argc, char *argv[])\r
60 {\r
61   struct stat st;\r
62   int erg;\r
63   int first;\r
64   FILE *fpin;\r
65   FILE *fpout;\r
66   pid_t pid;\r
67 \r
68   signal(SIGINT,sigfunc);\r
69   signal(SIGTERM,sigfunc);\r
70   signal(SIGKILL,sigfunc);\r
71 \r
72   //make the fifos if needed\r
73   if (stat(FIFO_FILE1, &st) != 0)\r
74     {\r
75       if (stat(FIFO_FILE2, &st) != 0)\r
76         {\r
77           perror("FIFO 2 exists, but FIFO 1 not");\r
78           exit(1);\r
79         }\r
80       first = 1;\r
81       umask(0);\r
82       erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);\r
83       erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);\r
84 \r
85       if ((fpin = fopen(FIFO_FILE1, "r")) == NULL)\r
86         {\r
87           perror("fopen");\r
88           exit(1);\r
89         }\r
90       if ((fpout = fopen(FIFO_FILE2, "w")) == NULL)\r
91         {\r
92           perror("fopen");\r
93           exit(1);\r
94         }\r
95     }\r
96   else\r
97     {\r
98       first = 0;\r
99       if (stat(FIFO_FILE2, &st) == 0)\r
100         {\r
101           perror("FIFO 1 exists, but FIFO 2 not");\r
102           exit(1);\r
103         }\r
104       if ((fpout = fopen(FIFO_FILE1, "w")) == NULL)\r
105         {\r
106           perror("fopen");\r
107           exit(1);\r
108         }\r
109       if ((fpin = fopen(FIFO_FILE2, "r")) == NULL)\r
110         {\r
111           perror("fopen");\r
112           exit(1);\r
113         }\r
114 \r
115     }\r
116 \r
117   // fork\r
118 \r
119   if ((pid = fork()) < 0)\r
120     {\r
121       perror("FORK ERROR");\r
122       return -3;\r
123     }\r
124   else if (pid == 0) // CHILD PROCESS\r
125     {\r
126       int rv = 0;\r
127       int readc = 0;\r
128       int pos = 0;\r
129       char line[MAXLINE];\r
130 \r
131       while (closeprog == 0)\r
132         {\r
133           readc = 0;\r
134 \r
135           while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){\r
136             if ((rv = read(STDIN_FILENO, line, MAXLINE)) < 0)\r
137               {\r
138                 perror("READ ERROR FROM STDIN");\r
139               }\r
140             readc += rv;\r
141           }\r
142 \r
143           pos = 0;\r
144 \r
145           fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout);\r
146 \r
147           pos += sizeof(struct GNUNET_MessageHeader);\r
148 \r
149           //do not send radiotap header\r
150           pos += sizeof( struct RadiotapHeader);\r
151 \r
152           while (pos < readc)\r
153             {\r
154               pos += fwrite(&line[pos], 1, readc - pos, fpout);\r
155             }\r
156         }\r
157 \r
158 \r
159       //clean up\r
160       fclose(fpout);\r
161     }\r
162   else // PARENT PROCESS\r
163     {\r
164       int rv = 0;\r
165       ssize_t pos = 0;\r
166       char line[MAXLINE];\r
167 \r
168       while (closeprog == 0)\r
169         {\r
170           if ((rv = fread(line, 1, MAXLINE, fpin)) < 0)\r
171             {\r
172               perror("READ ERROR FROM STDIN");\r
173             }\r
174 \r
175           pos = 0;\r
176           while (pos < rv)\r
177             {\r
178               pos += write(STDOUT_FILENO, &line[pos], rv - pos);\r
179             }\r
180         }\r
181 \r
182 \r
183       //clean up\r
184       fclose(fpin);\r
185 \r
186       if (first == 1)\r
187         {\r
188           unlink(FIFO_FILE1);\r
189           unlink(FIFO_FILE2);\r
190         }\r
191     }\r
192 \r
193   // Write the input to the output\r
194 \r
195   return (0);\r
196 }\r
197 \r