diff options
author | Christian Fuchs <christian.fuchs@cfuchs.net> | 2013-01-15 14:45:15 +0000 |
---|---|---|
committer | Christian Fuchs <christian.fuchs@cfuchs.net> | 2013-01-15 14:45:15 +0000 |
commit | 687c0c114b4bc5238334f0ab632d8cbb81b4db94 (patch) | |
tree | cd2b7859839d66f75c85313e1186cb0826847507 /src/vpn | |
parent | 0ae03d819843945cb3b2b54755ddeeec231a1ad4 (diff) | |
download | gnunet-687c0c114b4bc5238334f0ab632d8cbb81b4db94.tar.gz gnunet-687c0c114b4bc5238334f0ab632d8cbb81b4db94.zip |
final logics for run implemented
added teardown functionality.
now: testing...
Diffstat (limited to 'src/vpn')
-rw-r--r-- | src/vpn/gnunet-helper-vpn-windows.c | 182 |
1 files changed, 129 insertions, 53 deletions
diff --git a/src/vpn/gnunet-helper-vpn-windows.c b/src/vpn/gnunet-helper-vpn-windows.c index c67e654e2..50137322d 100644 --- a/src/vpn/gnunet-helper-vpn-windows.c +++ b/src/vpn/gnunet-helper-vpn-windows.c | |||
@@ -152,6 +152,7 @@ struct io_facility | |||
152 | 152 | ||
153 | OVERLAPPED overlapped; | 153 | OVERLAPPED overlapped; |
154 | DWORD buffer_size; | 154 | DWORD buffer_size; |
155 | DWORD buffer_size_written; | ||
155 | unsigned char buffer[MAX_SIZE]; | 156 | unsigned char buffer[MAX_SIZE]; |
156 | }; | 157 | }; |
157 | 158 | ||
@@ -307,6 +308,10 @@ set_address4 (const char *address, const char *mask) | |||
307 | exit (1); | 308 | exit (1); |
308 | } | 309 | } |
309 | 310 | ||
311 | // Set Device to Subnet-Mode? | ||
312 | // do we really need tun.c:2925 ? | ||
313 | |||
314 | |||
310 | /* | 315 | /* |
311 | * prepare the command | 316 | * prepare the command |
312 | */ | 317 | */ |
@@ -678,6 +683,7 @@ init_tun () | |||
678 | 683 | ||
679 | return handle; | 684 | return handle; |
680 | } | 685 | } |
686 | |||
681 | /** | 687 | /** |
682 | * Brings a TAP device up and sets it to connected state. | 688 | * Brings a TAP device up and sets it to connected state. |
683 | * | 689 | * |
@@ -741,10 +747,10 @@ attempt_read (struct io_facility * input_facility, | |||
741 | return FALSE; | 747 | return FALSE; |
742 | } | 748 | } |
743 | input_facility->status = ReadFile (input_facility->handle, | 749 | input_facility->status = ReadFile (input_facility->handle, |
744 | &input_facility->buffer[MAX_SIZE], | 750 | input_facility->buffer, |
745 | MAX_SIZE, | 751 | MAX_SIZE, |
746 | &input_facility->buffer_size, | 752 | &input_facility->buffer_size, |
747 | &input_facility->overlapped); | 753 | &input_facility->overlapped); |
748 | 754 | ||
749 | /* Check how the task is handled */ | 755 | /* Check how the task is handled */ |
750 | if (input_facility->status) | 756 | if (input_facility->status) |
@@ -756,7 +762,9 @@ attempt_read (struct io_facility * input_facility, | |||
756 | 762 | ||
757 | /* we successfully read something from the TAP and now need to | 763 | /* we successfully read something from the TAP and now need to |
758 | * send it our via STDOUT. Is that possible at the moment? */ | 764 | * send it our via STDOUT. Is that possible at the moment? */ |
759 | if (IOSTATE_READY == output_facility->facility_state && 0 < input_facility->buffer_size) | 765 | if ((IOSTATE_READY == output_facility->facility_state || |
766 | IOSTATE_WAITING == output_facility->facility_state) | ||
767 | && 0 < input_facility->buffer_size) | ||
760 | { /* hand over this buffers content */ | 768 | { /* hand over this buffers content */ |
761 | memcpy (output_facility->buffer, | 769 | memcpy (output_facility->buffer, |
762 | input_facility->buffer, | 770 | input_facility->buffer, |
@@ -781,6 +789,10 @@ attempt_read (struct io_facility * input_facility, | |||
781 | { /* error occurred, let the rest of the elements finish */ | 789 | { /* error occurred, let the rest of the elements finish */ |
782 | input_facility->path_open = FALSE; | 790 | input_facility->path_open = FALSE; |
783 | input_facility->facility_state = IOSTATE_FAILED; | 791 | input_facility->facility_state = IOSTATE_FAILED; |
792 | if (IOSTATE_WAITING == output_facility->facility_state) | ||
793 | output_facility->path_open = FALSE; | ||
794 | |||
795 | fprintf (stderr, "Fatal: Read from handle failed, allowing write to finish!\n"); | ||
784 | } | 796 | } |
785 | } | 797 | } |
786 | } | 798 | } |
@@ -789,9 +801,9 @@ attempt_read (struct io_facility * input_facility, | |||
789 | { | 801 | { |
790 | // there was an operation going on already, check if that has completed now. | 802 | // there was an operation going on already, check if that has completed now. |
791 | input_facility->status = GetOverlappedResult (input_facility->handle, | 803 | input_facility->status = GetOverlappedResult (input_facility->handle, |
792 | &input_facility->overlapped, | 804 | &input_facility->overlapped, |
793 | &input_facility->buffer_size, | 805 | &input_facility->buffer_size, |
794 | FALSE); | 806 | FALSE); |
795 | if (input_facility->status) | 807 | if (input_facility->status) |
796 | {/* successful return for a queued operation */ | 808 | {/* successful return for a queued operation */ |
797 | if (!ResetEvent (input_facility->overlapped.hEvent)) | 809 | if (!ResetEvent (input_facility->overlapped.hEvent)) |
@@ -799,7 +811,9 @@ attempt_read (struct io_facility * input_facility, | |||
799 | 811 | ||
800 | /* we successfully read something from the TAP and now need to | 812 | /* we successfully read something from the TAP and now need to |
801 | * send it our via STDOUT. Is that possible at the moment? */ | 813 | * send it our via STDOUT. Is that possible at the moment? */ |
802 | if (IOSTATE_READY == output_facility->facility_state && 0 < input_facility->buffer_size) | 814 | if ((IOSTATE_READY == output_facility->facility_state || |
815 | IOSTATE_WAITING == output_facility->facility_state) | ||
816 | && 0 < input_facility->buffer_size) | ||
803 | { /* hand over this buffers content */ | 817 | { /* hand over this buffers content */ |
804 | memcpy (output_facility->buffer, | 818 | memcpy (output_facility->buffer, |
805 | input_facility->buffer, | 819 | input_facility->buffer, |
@@ -821,6 +835,9 @@ attempt_read (struct io_facility * input_facility, | |||
821 | { /* error occurred, let the rest of the elements finish */ | 835 | { /* error occurred, let the rest of the elements finish */ |
822 | input_facility->path_open = FALSE; | 836 | input_facility->path_open = FALSE; |
823 | input_facility->facility_state = IOSTATE_FAILED; | 837 | input_facility->facility_state = IOSTATE_FAILED; |
838 | if (IOSTATE_WAITING == output_facility->facility_state) | ||
839 | output_facility->path_open = FALSE; | ||
840 | fprintf (stderr, "Fatal: Read from handle failed, allowing write to finish!\n"); | ||
824 | } | 841 | } |
825 | } | 842 | } |
826 | } | 843 | } |
@@ -840,11 +857,90 @@ static boolean | |||
840 | attempt_write (struct io_facility * output_facility, | 857 | attempt_write (struct io_facility * output_facility, |
841 | struct io_facility * input_facility) | 858 | struct io_facility * input_facility) |
842 | { | 859 | { |
843 | if (IOSTATE_READY == output_facility->facility_state && output_facility->buffer_size > 0 ){ | 860 | if (IOSTATE_READY == output_facility->facility_state |
844 | 861 | && output_facility->buffer_size > 0) | |
862 | { | ||
863 | if (!ResetEvent (output_facility->overlapped.hEvent)) | ||
864 | { | ||
865 | return FALSE; | ||
866 | } | ||
867 | |||
868 | output_facility->status = WriteFile (output_facility->handle, | ||
869 | output_facility->buffer, | ||
870 | output_facility->buffer_size, | ||
871 | &output_facility->buffer_size_written, | ||
872 | &output_facility->overlapped); | ||
873 | |||
874 | /* Check how the task is handled */ | ||
875 | if (output_facility->status && | ||
876 | output_facility->buffer_size_written == output_facility->buffer_size) | ||
877 | {/* async event processed immediately*/ | ||
878 | |||
879 | /* reset event manually*/ | ||
880 | if (!SetEvent (output_facility->overlapped.hEvent)) | ||
881 | return FALSE; | ||
882 | |||
883 | /* we are now waiting for our buffer to be filled*/ | ||
884 | output_facility->facility_state = IOSTATE_WAITING; | ||
885 | output_facility->buffer_size = 0; | ||
886 | output_facility->buffer_size_written = 0; | ||
887 | |||
888 | /* we successfully wrote something and now need to reset our reader */ | ||
889 | if (IOSTATE_WAITING == input_facility->facility_state) | ||
890 | input_facility->facility_state = IOSTATE_READY; | ||
891 | else if (IOSTATE_FAILED == input_facility->facility_state) | ||
892 | output_facility->path_open = FALSE; | ||
893 | } | ||
894 | else /* operation was either queued or failed*/ | ||
895 | { | ||
896 | int err = GetLastError (); | ||
897 | if (ERROR_IO_PENDING == err) | ||
898 | { /* operation queued */ | ||
899 | output_facility->facility_state = IOSTATE_QUEUED; | ||
900 | } | ||
901 | else | ||
902 | { /* error occurred, close this path */ | ||
903 | output_facility->path_open = FALSE; | ||
904 | output_facility->facility_state = IOSTATE_FAILED; | ||
905 | fprintf (stderr, "Fatal: Write to handle failed, exiting!\n"); | ||
906 | } | ||
907 | } | ||
908 | |||
845 | } | 909 | } |
846 | else if (IOSTATE_QUEUED == output_facility->facility_state){ | 910 | else if (IOSTATE_QUEUED == output_facility->facility_state) |
847 | 911 | { | |
912 | // there was an operation going on already, check if that has completed now. | ||
913 | output_facility->status = GetOverlappedResult (output_facility->handle, | ||
914 | &output_facility->overlapped, | ||
915 | &output_facility->buffer_size_written, | ||
916 | FALSE); | ||
917 | if (output_facility->status && | ||
918 | output_facility->buffer_size_written == output_facility->buffer_size) | ||
919 | {/* successful return for a queued operation */ | ||
920 | if (!ResetEvent (output_facility->overlapped.hEvent)) | ||
921 | return FALSE; | ||
922 | |||
923 | /* we are now waiting for our buffer to be filled*/ | ||
924 | output_facility->facility_state = IOSTATE_WAITING; | ||
925 | output_facility->buffer_size = 0; | ||
926 | output_facility->buffer_size_written = 0; | ||
927 | |||
928 | /* we successfully wrote something and now need to reset our reader */ | ||
929 | if (IOSTATE_WAITING == input_facility->facility_state) | ||
930 | input_facility->facility_state = IOSTATE_READY; | ||
931 | else if (IOSTATE_FAILED == input_facility->facility_state) | ||
932 | output_facility->path_open = FALSE; | ||
933 | } | ||
934 | else | ||
935 | { /* operation still pending/queued or failed? */ | ||
936 | int err = GetLastError (); | ||
937 | if (ERROR_IO_INCOMPLETE != err && ERROR_IO_PENDING != err) | ||
938 | { /* error occurred, close this path */ | ||
939 | output_facility->path_open = FALSE; | ||
940 | output_facility->facility_state = IOSTATE_FAILED; | ||
941 | fprintf (stderr, "Fatal: Write to handle failed, exiting!\n"); | ||
942 | } | ||
943 | } | ||
848 | } | 944 | } |
849 | 945 | ||
850 | return TRUE; | 946 | return TRUE; |
@@ -904,14 +1000,14 @@ run (HANDLE tap_handle) | |||
904 | * But for openvpn those are essential. | 1000 | * But for openvpn those are essential. |
905 | */ | 1001 | */ |
906 | if (!tun_up (tap_handle)) | 1002 | if (!tun_up (tap_handle)) |
907 | goto teardown; | 1003 | return; |
908 | 1004 | ||
909 | /* Initialize our overlapped IO structures*/ | 1005 | /* Initialize our overlapped IO structures*/ |
910 | if (!(initialize_io_facility (&tap_read, TRUE, FALSE) | 1006 | if (!(initialize_io_facility (&tap_read, TRUE, FALSE) |
911 | && initialize_io_facility (&tap_write, FALSE, TRUE) | 1007 | && initialize_io_facility (&tap_write, FALSE, TRUE) |
912 | && initialize_io_facility (&std_in, TRUE, FALSE) | 1008 | && initialize_io_facility (&std_in, TRUE, FALSE) |
913 | && initialize_io_facility (&std_out, FALSE, TRUE))) | 1009 | && initialize_io_facility (&std_out, FALSE, TRUE))) |
914 | goto teardown; | 1010 | goto teardown_final; |
915 | 1011 | ||
916 | /* Handles for STDIN and STDOUT */ | 1012 | /* Handles for STDIN and STDOUT */ |
917 | tap_read.handle = tap_handle; | 1013 | tap_read.handle = tap_handle; |
@@ -951,50 +1047,34 @@ run (HANDLE tap_handle) | |||
951 | goto teardown; | 1047 | goto teardown; |
952 | } | 1048 | } |
953 | 1049 | ||
954 | //openvpn | 1050 | while (std_out.path_open || tap_write.path_open) |
955 | // Set Device to Subnet-Mode? | ||
956 | // do we really need tun.c:2925 ? | ||
957 | // Why does openvpn assign IPv4's there??? Foobar?? | ||
958 | |||
959 | // Setup should be complete here. | ||
960 | // If something is missing, check init.c:3400+ | ||
961 | |||
962 | // mainloop: | ||
963 | // tunnel_point_to_point | ||
964 | // openvpn.c:62 | ||
965 | |||
966 | while (std_in.path_open | ||
967 | || std_out.path_open | ||
968 | || tap_read.path_open | ||
969 | || tap_write.path_open) | ||
970 | { | 1051 | { |
971 | /* perform READ from stdin if possible */ | 1052 | /* perform READ from stdin if possible */ |
972 | if ((std_in.path_open && tap_write.path_open) | 1053 | if (std_in.path_open && tap_write.path_open && !attempt_read (&std_in, &tap_write)) |
973 | || IOSTATE_QUEUED == std_in.facility_state) | 1054 | break; |
974 | if (!attempt_read (&std_in, &tap_write)) | ||
975 | break; | ||
976 | 1055 | ||
977 | /* perform READ from tap if possible */ | 1056 | /* perform READ from tap if possible */ |
978 | if ((tap_read.path_open && std_out.path_open) | 1057 | if (tap_read.path_open && std_out.path_open && !attempt_read (&tap_read, &std_out)) |
979 | || IOSTATE_QUEUED == tap_read.facility_state) | 1058 | break; |
980 | if (!attempt_read (&tap_read, &std_out)) | ||
981 | break; | ||
982 | 1059 | ||
983 | /* perform WRITE to tap if possible */ | 1060 | /* perform WRITE to tap if possible */ |
984 | if (IOSTATE_READY == tap_write.facility_state && tap_write.path_open) | 1061 | if (tap_write.path_open && !attempt_write (&tap_write, &std_in)) |
985 | if (!attempt_write (&tap_write, &std_in)) | 1062 | break; |
986 | break; | ||
987 | 1063 | ||
988 | /* perform WRITE to STDOUT if possible */ | 1064 | /* perform WRITE to STDOUT if possible */ |
989 | if (IOSTATE_READY == std_out.facility_state && std_out.path_open) | 1065 | if (std_out.path_open && !attempt_write (&std_out, &tap_read)) |
990 | if (!attempt_write (&std_out, &tap_read)) | 1066 | break; |
991 | break; | ||
992 | |||
993 | // check if any path is blocked | ||
994 | } | 1067 | } |
1068 | |||
995 | teardown: | 1069 | teardown: |
996 | ; | 1070 | |
997 | //init.c:3472 | 1071 | CancelIo (tap_handle); |
1072 | CancelIo (std_in.handle); | ||
1073 | CancelIo (std_out.handle); | ||
1074 | |||
1075 | teardown_final: | ||
1076 | |||
1077 | CloseHandle (tap_handle); | ||
998 | } | 1078 | } |
999 | 1079 | ||
1000 | /** | 1080 | /** |
@@ -1067,10 +1147,6 @@ main (int argc, char **argv) | |||
1067 | set_address4 (address, mask); | 1147 | set_address4 (address, mask); |
1068 | } | 1148 | } |
1069 | 1149 | ||
1070 | //eventuell: | ||
1071 | // tap_allow_nonadmin_access | ||
1072 | //tun.c:2023 | ||
1073 | |||
1074 | run (handle); | 1150 | run (handle); |
1075 | global_ret = 0; | 1151 | global_ret = 0; |
1076 | cleanup: | 1152 | cleanup: |