diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-11-25 10:23:02 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-11-25 10:23:02 +0000 |
commit | 492a5f0297306aa28ce07062129d6044b26a587c (patch) | |
tree | 231dd04a07b0bc1d90d107c7bc7e310f2919f205 /src/transport | |
parent | f20dc7a1d1d4c480c126dcfe992d16a3620069f4 (diff) | |
download | gnunet-492a5f0297306aa28ce07062129d6044b26a587c.tar.gz gnunet-492a5f0297306aa28ce07062129d6044b26a587c.zip |
-some more wlan code cleanup
Diffstat (limited to 'src/transport')
-rw-r--r-- | src/transport/gnunet-transport-wlan-helper-dummy.c | 139 |
1 files changed, 61 insertions, 78 deletions
diff --git a/src/transport/gnunet-transport-wlan-helper-dummy.c b/src/transport/gnunet-transport-wlan-helper-dummy.c index 6a884efc1..4e1635451 100644 --- a/src/transport/gnunet-transport-wlan-helper-dummy.c +++ b/src/transport/gnunet-transport-wlan-helper-dummy.c | |||
@@ -56,13 +56,14 @@ sigfunc (int sig) | |||
56 | (void) unlink (FIFO_FILE2); | 56 | (void) unlink (FIFO_FILE2); |
57 | } | 57 | } |
58 | 58 | ||
59 | |||
59 | /** | 60 | /** |
60 | * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin | 61 | * function to create GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL message for plugin |
61 | * @param buffer pointer to buffer for the message | 62 | * @param buffer pointer to buffer for the message |
62 | * @param mac pointer to the mac address | 63 | * @param mac pointer to the mac address |
63 | * @return number of bytes written | 64 | * @return number of bytes written |
64 | */ | 65 | */ |
65 | int | 66 | static int |
66 | send_mac_to_plugin (char *buffer, struct MacAddress *mac) | 67 | send_mac_to_plugin (char *buffer, struct MacAddress *mac) |
67 | { | 68 | { |
68 | 69 | ||
@@ -123,6 +124,7 @@ stdin_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) | |||
123 | sizeof (struct GNUNET_MessageHeader); | 124 | sizeof (struct GNUNET_MessageHeader); |
124 | } | 125 | } |
125 | 126 | ||
127 | |||
126 | static void | 128 | static void |
127 | file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) | 129 | file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) |
128 | { | 130 | { |
@@ -147,18 +149,37 @@ file_in_send (void *cls, void *client, const struct GNUNET_MessageHeader *hdr) | |||
147 | } | 149 | } |
148 | 150 | ||
149 | 151 | ||
150 | |||
151 | int | 152 | int |
152 | testmode (int argc, char *argv[]) | 153 | main (int argc, char *argv[]) |
153 | { | 154 | { |
154 | struct stat st; | 155 | struct stat st; |
155 | int erg; | 156 | int erg; |
156 | |||
157 | FILE *fpin = NULL; | 157 | FILE *fpin = NULL; |
158 | FILE *fpout = NULL; | 158 | FILE *fpout = NULL; |
159 | |||
160 | int fdpin; | 159 | int fdpin; |
161 | int fdpout; | 160 | int fdpout; |
161 | char readbuf[MAXLINE]; | ||
162 | int readsize = 0; | ||
163 | struct sendbuf write_std; | ||
164 | struct sendbuf write_pout; | ||
165 | int ret = 0; | ||
166 | int maxfd = 0; | ||
167 | fd_set rfds; | ||
168 | fd_set wfds; | ||
169 | struct timeval tv; | ||
170 | int retval; | ||
171 | struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst; | ||
172 | struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst; | ||
173 | struct MacAddress macaddr; | ||
174 | |||
175 | if (2 != argc) | ||
176 | { | ||
177 | fprintf (stderr, | ||
178 | "This program must be started with the operating mode (1 or 2) as the only argument.\n"); | ||
179 | return 1; | ||
180 | } | ||
181 | if ((0 != strstr (argv[1], "1")) && (0 != strstr (argv[1], "2"))) | ||
182 | return 1; | ||
162 | 183 | ||
163 | //make the fifos if needed | 184 | //make the fifos if needed |
164 | if (0 != stat (FIFO_FILE1, &st)) | 185 | if (0 != stat (FIFO_FILE1, &st)) |
@@ -168,34 +189,34 @@ testmode (int argc, char *argv[]) | |||
168 | fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n"); | 189 | fprintf (stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n"); |
169 | exit (1); | 190 | exit (1); |
170 | } | 191 | } |
171 | |||
172 | umask (0); | 192 | umask (0); |
173 | //unlink(FIFO_FILE1); | ||
174 | //unlink(FIFO_FILE2); | ||
175 | // FIXME: use mkfifo! | ||
176 | erg = mkfifo (FIFO_FILE1, 0666); | 193 | erg = mkfifo (FIFO_FILE1, 0666); |
177 | if (0 != erg) | 194 | if (0 != erg) |
178 | { | 195 | { |
179 | fprintf (stderr, "Error at mkfifo1: %s\n", strerror (errno)); | 196 | fprintf (stderr, |
197 | "Error in mkfifo(%s): %s\n", | ||
198 | FIFO_FILE1, | ||
199 | strerror (errno)); | ||
180 | //exit(1); | 200 | //exit(1); |
181 | } | 201 | } |
182 | erg = mkfifo (FIFO_FILE2, 0666); | 202 | erg = mkfifo (FIFO_FILE2, 0666); |
183 | if (0 != erg) | 203 | if (0 != erg) |
184 | { | 204 | { |
185 | fprintf (stderr, "Error at mkfifo2: %s\n", strerror (errno)); | 205 | fprintf (stderr, |
206 | "Error in mkfifo(%s): %s\n", | ||
207 | FIFO_FILE2, | ||
208 | strerror (errno)); | ||
186 | //exit(1); | 209 | //exit(1); |
187 | } | 210 | } |
188 | 211 | ||
189 | } | 212 | } |
190 | else | 213 | else |
191 | { | 214 | { |
192 | |||
193 | if (0 != stat (FIFO_FILE2, &st)) | 215 | if (0 != stat (FIFO_FILE2, &st)) |
194 | { | 216 | { |
195 | fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n"); | 217 | fprintf (stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n"); |
196 | exit (1); | 218 | exit (1); |
197 | } | 219 | } |
198 | |||
199 | } | 220 | } |
200 | 221 | ||
201 | if (strstr (argv[1], "1")) | 222 | if (strstr (argv[1], "1")) |
@@ -240,7 +261,9 @@ testmode (int argc, char *argv[]) | |||
240 | 261 | ||
241 | if (fdpin >= FD_SETSIZE) | 262 | if (fdpin >= FD_SETSIZE) |
242 | { | 263 | { |
243 | fprintf (stderr, "File fdpin number too large (%d > %u)\n", fdpin, | 264 | fprintf (stderr, |
265 | "File fdpin number too large (%d > %u)\n", | ||
266 | fdpin, | ||
244 | (unsigned int) FD_SETSIZE); | 267 | (unsigned int) FD_SETSIZE); |
245 | goto end; | 268 | goto end; |
246 | } | 269 | } |
@@ -250,7 +273,9 @@ testmode (int argc, char *argv[]) | |||
250 | 273 | ||
251 | if (fdpout >= FD_SETSIZE) | 274 | if (fdpout >= FD_SETSIZE) |
252 | { | 275 | { |
253 | fprintf (stderr, "File fdpout number too large (%d > %u)\n", fdpout, | 276 | fprintf (stderr, |
277 | "File fdpout number too large (%d > %u)\n", | ||
278 | fdpout, | ||
254 | (unsigned int) FD_SETSIZE); | 279 | (unsigned int) FD_SETSIZE); |
255 | goto end; | 280 | goto end; |
256 | 281 | ||
@@ -259,36 +284,13 @@ testmode (int argc, char *argv[]) | |||
259 | signal (SIGINT, &sigfunc); | 284 | signal (SIGINT, &sigfunc); |
260 | signal (SIGTERM, &sigfunc); | 285 | signal (SIGTERM, &sigfunc); |
261 | 286 | ||
262 | char readbuf[MAXLINE]; | ||
263 | int readsize = 0; | ||
264 | struct sendbuf write_std; | ||
265 | |||
266 | write_std.size = 0; | 287 | write_std.size = 0; |
267 | write_std.pos = 0; | 288 | write_std.pos = 0; |
268 | |||
269 | struct sendbuf write_pout; | ||
270 | |||
271 | write_pout.size = 0; | 289 | write_pout.size = 0; |
272 | write_pout.pos = 0; | 290 | write_pout.pos = 0; |
273 | |||
274 | int ret = 0; | ||
275 | int maxfd = 0; | ||
276 | |||
277 | fd_set rfds; | ||
278 | fd_set wfds; | ||
279 | struct timeval tv; | ||
280 | int retval; | ||
281 | |||
282 | struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst; | ||
283 | struct GNUNET_SERVER_MessageStreamTokenizer *file_in_mst; | ||
284 | |||
285 | stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout); | 291 | stdin_mst = GNUNET_SERVER_mst_create (&stdin_send, &write_pout); |
286 | file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std); | 292 | file_in_mst = GNUNET_SERVER_mst_create (&file_in_send, &write_std); |
287 | 293 | ||
288 | //send mac first | ||
289 | |||
290 | struct MacAddress macaddr; | ||
291 | |||
292 | //Send random mac address | 294 | //Send random mac address |
293 | macaddr.mac[0] = 0x13; | 295 | macaddr.mac[0] = 0x13; |
294 | macaddr.mac[1] = 0x22; | 296 | macaddr.mac[1] = 0x22; |
@@ -296,14 +298,11 @@ testmode (int argc, char *argv[]) | |||
296 | macaddr.mac[3] = 0x44; | 298 | macaddr.mac[3] = 0x44; |
297 | macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256); | 299 | macaddr.mac[4] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 256); |
298 | macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256); | 300 | macaddr.mac[5] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 256); |
299 | |||
300 | write_std.size = send_mac_to_plugin (write_std.buf, &macaddr); | 301 | write_std.size = send_mac_to_plugin (write_std.buf, &macaddr); |
301 | 302 | ||
302 | while (0 == closeprog) | 303 | while (0 == closeprog) |
303 | { | 304 | { |
304 | 305 | maxfd = -1; | |
305 | maxfd = 0; | ||
306 | |||
307 | //set timeout | 306 | //set timeout |
308 | tv.tv_sec = 5; | 307 | tv.tv_sec = 5; |
309 | tv.tv_usec = 0; | 308 | tv.tv_usec = 0; |
@@ -313,12 +312,12 @@ testmode (int argc, char *argv[]) | |||
313 | if (0 == write_pout.size) | 312 | if (0 == write_pout.size) |
314 | { | 313 | { |
315 | FD_SET (STDIN_FILENO, &rfds); | 314 | FD_SET (STDIN_FILENO, &rfds); |
316 | 315 | maxfd = MAX (STDIN_FILENO, maxfd); | |
317 | } | 316 | } |
318 | if (0 == write_std.size) | 317 | if (0 == write_std.size) |
319 | { | 318 | { |
320 | FD_SET (fdpin, &rfds); | 319 | FD_SET (fdpin, &rfds); |
321 | maxfd = fdpin; | 320 | maxfd = MAX (fdpin, maxfd); |
322 | } | 321 | } |
323 | FD_ZERO (&wfds); | 322 | FD_ZERO (&wfds); |
324 | // if there is something to write | 323 | // if there is something to write |
@@ -327,7 +326,6 @@ testmode (int argc, char *argv[]) | |||
327 | FD_SET (STDOUT_FILENO, &wfds); | 326 | FD_SET (STDOUT_FILENO, &wfds); |
328 | maxfd = MAX (maxfd, STDOUT_FILENO); | 327 | maxfd = MAX (maxfd, STDOUT_FILENO); |
329 | } | 328 | } |
330 | |||
331 | if (0 < write_pout.size) | 329 | if (0 < write_pout.size) |
332 | { | 330 | { |
333 | FD_SET (fdpout, &wfds); | 331 | FD_SET (fdpout, &wfds); |
@@ -335,14 +333,13 @@ testmode (int argc, char *argv[]) | |||
335 | } | 333 | } |
336 | 334 | ||
337 | retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv); | 335 | retval = select (maxfd + 1, &rfds, &wfds, NULL, &tv); |
338 | 336 | if ( (-1 == retval) && (EINTR == errno) ) | |
339 | if (-1 == retval && EINTR == errno) | 337 | continue; |
340 | { | ||
341 | continue; | ||
342 | } | ||
343 | if (0 > retval) | 338 | if (0 > retval) |
344 | { | 339 | { |
345 | fprintf (stderr, "select failed: %s\n", strerror (errno)); | 340 | fprintf (stderr, |
341 | "select failed: %s\n", | ||
342 | strerror (errno)); | ||
346 | closeprog = 1; | 343 | closeprog = 1; |
347 | break; | 344 | break; |
348 | } | 345 | } |
@@ -355,7 +352,9 @@ testmode (int argc, char *argv[]) | |||
355 | if (0 > ret) | 352 | if (0 > ret) |
356 | { | 353 | { |
357 | closeprog = 1; | 354 | closeprog = 1; |
358 | fprintf (stderr, "Write ERROR to STDOUT\n"); | 355 | fprintf (stderr, |
356 | "Write ERROR to STDOUT_FILENO: %s\n", | ||
357 | strerror (errno)); | ||
359 | break; | 358 | break; |
360 | } | 359 | } |
361 | else | 360 | else |
@@ -379,7 +378,9 @@ testmode (int argc, char *argv[]) | |||
379 | if (0 > ret) | 378 | if (0 > ret) |
380 | { | 379 | { |
381 | closeprog = 1; | 380 | closeprog = 1; |
382 | fprintf (stderr, "Write ERROR to fdpout\n"); | 381 | fprintf (stderr, |
382 | "Write ERROR to fdpout: %s\n", | ||
383 | strerror (errno)); | ||
383 | } | 384 | } |
384 | else | 385 | else |
385 | { | 386 | { |
@@ -400,7 +401,9 @@ testmode (int argc, char *argv[]) | |||
400 | if (0 > readsize) | 401 | if (0 > readsize) |
401 | { | 402 | { |
402 | closeprog = 1; | 403 | closeprog = 1; |
403 | fprintf (stderr, "Read ERROR to STDIN_FILENO\n"); | 404 | fprintf (stderr, |
405 | "Error reading from STDIN_FILENO: %s\n", | ||
406 | strerror (errno)); | ||
404 | } | 407 | } |
405 | else if (0 < readsize) | 408 | else if (0 < readsize) |
406 | { | 409 | { |
@@ -418,18 +421,18 @@ testmode (int argc, char *argv[]) | |||
418 | if (FD_ISSET (fdpin, &rfds)) | 421 | if (FD_ISSET (fdpin, &rfds)) |
419 | { | 422 | { |
420 | readsize = read (fdpin, readbuf, sizeof (readbuf)); | 423 | readsize = read (fdpin, readbuf, sizeof (readbuf)); |
421 | |||
422 | if (0 > readsize) | 424 | if (0 > readsize) |
423 | { | 425 | { |
424 | closeprog = 1; | 426 | closeprog = 1; |
425 | fprintf (stderr, "Read ERROR to fdpin: %s\n", strerror (errno)); | 427 | fprintf (stderr, |
428 | "Error reading from fdpin: %s\n", | ||
429 | strerror (errno)); | ||
426 | break; | 430 | break; |
427 | } | 431 | } |
428 | else if (0 < readsize) | 432 | else if (0 < readsize) |
429 | { | 433 | { |
430 | GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize, | 434 | GNUNET_SERVER_mst_receive (file_in_mst, NULL, readbuf, readsize, |
431 | GNUNET_NO, GNUNET_NO); | 435 | GNUNET_NO, GNUNET_NO); |
432 | |||
433 | } | 436 | } |
434 | else | 437 | else |
435 | { | 438 | { |
@@ -437,11 +440,9 @@ testmode (int argc, char *argv[]) | |||
437 | closeprog = 1; | 440 | closeprog = 1; |
438 | } | 441 | } |
439 | } | 442 | } |
440 | |||
441 | } | 443 | } |
442 | 444 | ||
443 | //clean up | 445 | //clean up |
444 | |||
445 | GNUNET_SERVER_mst_destroy (stdin_mst); | 446 | GNUNET_SERVER_mst_destroy (stdin_mst); |
446 | GNUNET_SERVER_mst_destroy (file_in_mst); | 447 | GNUNET_SERVER_mst_destroy (file_in_mst); |
447 | 448 | ||
@@ -450,29 +451,11 @@ end: | |||
450 | fclose (fpout); | 451 | fclose (fpout); |
451 | if (fpin != NULL) | 452 | if (fpin != NULL) |
452 | fclose (fpin); | 453 | fclose (fpin); |
453 | |||
454 | if (1 == first) | 454 | if (1 == first) |
455 | { | 455 | { |
456 | (void) unlink (FIFO_FILE1); | 456 | (void) unlink (FIFO_FILE1); |
457 | (void) unlink (FIFO_FILE2); | 457 | (void) unlink (FIFO_FILE2); |
458 | } | 458 | } |
459 | 459 | return 0; | |
460 | return (0); | ||
461 | } | 460 | } |
462 | 461 | ||
463 | int | ||
464 | main (int argc, char *argv[]) | ||
465 | { | ||
466 | if (2 != argc) | ||
467 | { | ||
468 | fprintf (stderr, | ||
469 | "This program must be started with the operating mode as argument.\n"); | ||
470 | fprintf (stderr, | ||
471 | "Usage: options\n" "options:\n" "1 = first loopback file\n" | ||
472 | "2 = second loopback file\n" "\n"); | ||
473 | return 1; | ||
474 | } | ||
475 | if (strstr (argv[1], "1") || strstr (argv[1], "2")) | ||
476 | return testmode (argc, argv); | ||
477 | return 1; | ||
478 | } | ||