aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-transport-wlan-helper.c
diff options
context:
space:
mode:
authorDavid Brodski <david@brodski.eu>2011-01-11 23:38:54 +0000
committerDavid Brodski <david@brodski.eu>2011-01-11 23:38:54 +0000
commitbe11962997493fb0cee7a8480d6c90da016f6b7d (patch)
treec4db7d56e51d172bda20e88fa5b88477f4b0b876 /src/transport/gnunet-transport-wlan-helper.c
parent977024fee8ec4405d536f837fc805e84f6b886ca (diff)
downloadgnunet-be11962997493fb0cee7a8480d6c90da016f6b7d.tar.gz
gnunet-be11962997493fb0cee7a8480d6c90da016f6b7d.zip
Diffstat (limited to 'src/transport/gnunet-transport-wlan-helper.c')
-rw-r--r--src/transport/gnunet-transport-wlan-helper.c353
1 files changed, 219 insertions, 134 deletions
diff --git a/src/transport/gnunet-transport-wlan-helper.c b/src/transport/gnunet-transport-wlan-helper.c
index 52469b93e..feb30deb6 100644
--- a/src/transport/gnunet-transport-wlan-helper.c
+++ b/src/transport/gnunet-transport-wlan-helper.c
@@ -415,7 +415,33 @@ void sigfunc(int sig)
415 } 415 }
416} 416}
417 417
418struct sendbuf{
419 int pos;
420 int size;
421 char buf[MAXLINE * 2];
422};
423
424static void
425stdin_send (void *cls,
426 void *client,
427 const struct GNUNET_MessageHeader *hdr)
428{
429 struct sendbuf *write_pout = cls;
430 GNUNET_assert (hdr->size + write_pout->size < MAXLINE *2);
431 memcpy(write_pout->buf + write_pout->size, hdr, hdr->size);
432 write_pout->size += hdr->size;
433}
418 434
435static void
436file_in_send (void *cls,
437 void *client,
438 const struct GNUNET_MessageHeader *hdr)
439{
440 struct sendbuf * write_std = cls;
441 GNUNET_assert (hdr->size + write_std->size < MAXLINE *2);
442 memcpy(write_std->buf + write_std->size, hdr, hdr->size);
443 write_std->size += hdr->size;
444}
419 445
420int 446int
421testmode(int argc, char *argv[]) 447testmode(int argc, char *argv[])
@@ -423,10 +449,11 @@ testmode(int argc, char *argv[])
423 struct stat st; 449 struct stat st;
424 int erg; 450 int erg;
425 451
426 int fd[2];
427 FILE *fpin; 452 FILE *fpin;
428 FILE *fpout; 453 FILE *fpout;
429 pid_t pid; 454
455 int fdpin;
456 int fdpout;
430 457
431 458
432 459
@@ -438,188 +465,242 @@ testmode(int argc, char *argv[])
438 perror("FIFO 2 exists, but FIFO 1 not"); 465 perror("FIFO 2 exists, but FIFO 1 not");
439 exit(1); 466 exit(1);
440 } 467 }
441 first = 1; 468
442 umask(0); 469 umask(0);
443 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0); 470 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
444 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0); 471 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
445 472
446 if ((fpin = fopen(FIFO_FILE1, "r")) == NULL) 473
447 {
448 perror("fopen");
449 exit(1);
450 }
451 if ((fpout = fopen(FIFO_FILE2, "w")) == NULL)
452 {
453 perror("fopen");
454 exit(1);
455 }
456 } 474 }
457 else 475 else
458 { 476 {
459 first = 0; 477
460 if (stat(FIFO_FILE2, &st) != 0) 478 if (stat(FIFO_FILE2, &st) != 0)
461 { 479 {
462 perror("FIFO 1 exists, but FIFO 2 not"); 480 perror("FIFO 1 exists, but FIFO 2 not");
463 exit(1); 481 exit(1);
464 } 482 }
465 if ((fpout = fopen(FIFO_FILE1, "w")) == NULL) 483
466 {
467 perror("fopen");
468 exit(1);
469 }
470 if ((fpin = fopen(FIFO_FILE2, "r")) == NULL)
471 {
472 perror("fopen");
473 exit(1);
474 }
475 484
476 } 485 }
477 486
478 // fork 487 if (strstr(argv[2],"1")) {
488 first = 1;
489 fpin = fopen(FIFO_FILE1, "r");
490 if (fpin == NULL)
491 {
492 perror("fopen");
493 exit(1);
494 }
495 if ((fpout = fopen(FIFO_FILE2, "w")) == NULL)
496 {
497 perror("fopen");
498 exit(1);
499 }
479 500
480 fd[0] = STDIN_FILENO; 501 } else {
481 fd[1] = STDOUT_FILENO; 502 first = 0;
503 if ((fpout = fopen(FIFO_FILE1, "w")) == NULL)
504 {
505 perror("fopen");
506 exit(1);
507 }
508 if ((fpin = fopen(FIFO_FILE2, "r")) == NULL)
509 {
510 perror("fopen");
511 exit(1);
512 }
482 513
483 if ((pid = fork()) < 0)
484 {
485 perror("FORK ERROR");
486 514
487 //clean up 515 }
488 if (first == 1)
489 {
490 unlink(FIFO_FILE1);
491 unlink(FIFO_FILE2);
492 }
493 fclose(fpin);
494 fclose(fpout);
495 return -3;
496 }
497 else if (pid == 0) // CHILD PROCESS
498 {
499 516
500 signal(SIGINT, sigfunc); 517 fdpin = fileno(fpin);
501 signal(SIGTERM, sigfunc); 518 fdpout = fileno(fpout);
502 signal(SIGKILL, sigfunc);
503 int rv = 0;
504 int readc = 0;
505 int pos = 0;
506 char line[MAXLINE];
507 int ret = 0;
508 519
509 while (closeprog == 0) 520 signal(SIGINT, sigfunc);
510 { 521 signal(SIGTERM, sigfunc);
511 readc = 0; 522 signal(SIGKILL, sigfunc);
512 523
513 //while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){ 524 char readbuf[MAXLINE];
514 if ((rv = read(fd[0], line, MAXLINE)) < 0) 525 int readsize = 0;
515 { 526 struct sendbuf write_std;
516 perror("READ ERROR FROM STDIN"); 527 write_std.size = 0;
517 } 528 write_std.pos = 0;
518 readc += rv; 529
519 //} 530 struct sendbuf write_pout;
531 write_pout.size = 0;
532 write_pout.pos = 0;
533
534 int ret = 0;
535 int maxfd = 0;
536 int to_write_stdout = 0;
537 int to_write_fpout = 0;
520 538
521 if (closeprog == 1){ 539 fd_set rfds;
522 break; 540 fd_set wfds;
523 } 541 struct timeval tv;
542 int retval;
524 543
525 pos = 0;
526 544
527 545
528 perror("writing blub"); 546 maxfd = MAX(fdpout, fdpin);
529 //fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout); 547 maxfd = MAX(maxfd, STDOUT_FILENO);
548 maxfd++;
530 549
531 //pos += sizeof(struct GNUNET_Mes#include "gnunet_util_lib.h"sageHeader); 550 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
551 struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst;
532 552
533 //do not send radiotap header 553 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout);
534 //pos += sizeof( struct RadiotapHeader); 554 file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std);
535 555
536 while (pos < readc) 556 //send mac first
557
558 struct Wlan_Helper_Control_Message macmsg;
559
560 //Send random mac address
561 macmsg.mac.mac[0] = 0x13;
562 macmsg.mac.mac[1] = 0x22;
563 macmsg.mac.mac[2] = 0x33;
564 macmsg.mac.mac[3] = 0x44;
565 macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
566 macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
567 macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message);
568 macmsg.hdr.type = GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL;
569
570 memcpy(&write_std.buf, &macmsg, macmsg.hdr.size);
571 write_std.size = macmsg.hdr.size;
572
573
574 while (closeprog == 0)
575 {
576
577 //set timeout
578 tv.tv_sec = 5;
579 tv.tv_usec = 0;
580
581 FD_ZERO(&rfds);
582 // if output queue is emty
583 if (to_write_fpout == 0)
584 FD_SET(STDIN_FILENO, &rfds);
585 if (to_write_stdout == 0)
586 FD_SET(fdpin, &rfds);
587
588 FD_ZERO(&wfds);
589 // if there is something to write
590 if (to_write_stdout > 0)
591 FD_SET(STDOUT_FILENO, &wfds);
592 if (to_write_fpout > 0)
593 FD_SET(fdpout, &wfds);
594
595 retval = select(maxfd, &rfds, &wfds, NULL, &tv);
596
597
598 if (retval == -1 && errno == EINTR){
599 continue;
600 }
601 if (retval < 0){
602 perror("select()");
603 exit(1);
604 }
605
606 if (FD_ISSET(STDOUT_FILENO, &wfds))
607 {
608 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
609 write_std.size - write_std.pos);
610
611 if (ret < 0)
537 { 612 {
538 ret = fwrite(&line[pos], 1, readc - pos, fpout); 613 closeprog = 1;
539 if (ret < 0) 614 perror("Write ERROR to STDOUT");
540 { 615 exit(1);
541 closeprog = 1; 616 }
542 perror("Write ERROR FROM fpout"); 617 else
543 break; 618 {
544 } 619 write_std.pos += ret;
545 else 620 // check if finished
621 if (write_std.pos == write_std.size)
546 { 622 {
547 pos += ret; 623 write_std.pos = 0;
624 write_std.size = 0;
548 } 625 }
549 } 626 }
550 } 627 }
551 628
552 629 if (FD_ISSET(fdpout, &wfds))
553 //clean up
554 fclose(fpout);
555 }
556 else // PARENT PROCESS
557 {
558 signal(SIGINT, sigfunc);
559 signal(SIGTERM, sigfunc);
560 signal(SIGKILL, sigfunc);
561 int rv = 0;
562 int ret = 0;
563 ssize_t pos = 0;
564 char line[MAXLINE];
565 struct Wlan_Helper_Control_Message macmsg;
566
567 //Send random mac address
568 macmsg.mac.mac[0] = 0x13;
569 macmsg.mac.mac[1] = 0x22;
570 macmsg.mac.mac[2] = 0x33;
571 macmsg.mac.mac[3] = 0x44;
572 macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
573 macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
574 macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message);
575 macmsg.hdr.type = GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL;
576
577 pos = 0;
578
579 while (pos < sizeof(struct Wlan_Helper_Control_Message))
580 { 630 {
581 pos += write(fd[1], &macmsg + pos, sizeof(struct Wlan_Helper_Control_Message) - pos); 631 ret = write(fdpout, write_pout.buf + write_pout.pos,
582 } 632 write_pout.size - write_pout.pos);
583 633
584 while (closeprog == 0) 634 if (ret < 0)
585 {
586 if ((rv = fread(line, 1, MAXLINE, fpin)) < 0)
587 { 635 {
588 perror("READ ERROR FROM fpin"); 636 closeprog = 1;
637 perror("Write ERROR to fdpout");
638 exit(1);
589 } 639 }
590 if (closeprog == 1){ 640 else
591 break;
592 }
593 pos = 0;
594 while (pos < rv)
595 { 641 {
596 642 write_pout.pos += ret;
597 ret= write(fd[1], &line[pos], rv - pos); 643 // check if finished
598 if (ret < 0) 644 if (write_pout.pos == write_pout.size)
599 {
600 closeprog = 1;
601 perror("Write ERROR FROM STDout");
602 break;
603 }
604 else
605 { 645 {
606 pos += ret; 646 write_pout.pos = 0;
647 write_pout.size = 0;
607 } 648 }
608 } 649 }
609 } 650 }
610 651
652 if (FD_ISSET(STDIN_FILENO, &rfds))
653 {
654 readsize = read(STDIN_FILENO, readbuf,
655 sizeof(readbuf));
656
657 if (readsize < 0)
658 {
659 closeprog = 1;
660 perror("Write ERROR to STDIN_FILENO");
661 exit(1);
662 }
663 else
664 {
665 GNUNET_SERVER_mst_receive(stdin_mst, NULL,
666 readbuf, readsize, GNUNET_NO, GNUNET_NO);
667
668 }
669 }
670
671 if (FD_ISSET(fdpin, &rfds))
672 {
673 readsize = read(fdpin, readbuf,
674 sizeof(readbuf));
675
676 if (readsize < 0)
677 {
678 closeprog = 1;
679 perror("Write ERROR to STDIN_FILENO");
680 exit(1);
681 }
682 else
683 {
684 GNUNET_SERVER_mst_receive(file_in_mst, NULL,
685 readbuf, readsize, GNUNET_NO, GNUNET_NO);
686
687 }
688 }
689
690
691 }
611 692
612 //clean up 693 //clean up
694 fclose(fpout);
613 fclose(fpin); 695 fclose(fpin);
696 perror("Write ERROR to STDIN_FILENO");
614 697
615 if (first == 1) 698 if (first == 1)
616 { 699 {
617 unlink(FIFO_FILE1); 700 unlink(FIFO_FILE1);
618 unlink(FIFO_FILE2); 701 unlink(FIFO_FILE2);
619 } 702 }
620 }
621 703
622 // Write the input to the output
623 704
624 return (0); 705 return (0);
625} 706}
@@ -628,10 +709,14 @@ testmode(int argc, char *argv[])
628int 709int
629main(int argc, char *argv[]) 710main(int argc, char *argv[])
630{ 711{
631 if ((argc==2) && (strstr(argv[1],"1"))) 712 if (argc == 3)
632 { 713 {
633 return testmode(argc, argv); 714 if (strstr(argv[2], "1") || strstr(argv[2], "2"))
634 } 715 {
716
717 return testmode(argc, argv);
718 }
719 }
635#if 0 720#if 0
636 u8 u8aSendBuffer[500]; 721 u8 u8aSendBuffer[500];
637 char szErrbuf[PCAP_ERRBUF_SIZE]; 722 char szErrbuf[PCAP_ERRBUF_SIZE];