aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-helper-vpn-windows.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vpn/gnunet-helper-vpn-windows.c')
-rw-r--r--src/vpn/gnunet-helper-vpn-windows.c234
1 files changed, 117 insertions, 117 deletions
diff --git a/src/vpn/gnunet-helper-vpn-windows.c b/src/vpn/gnunet-helper-vpn-windows.c
index 15dc5a667..3f4920a6f 100644
--- a/src/vpn/gnunet-helper-vpn-windows.c
+++ b/src/vpn/gnunet-helper-vpn-windows.c
@@ -19,8 +19,8 @@
19 */ 19 */
20/** 20/**
21 * @file vpn/gnunet-helper-vpn-windows.c 21 * @file vpn/gnunet-helper-vpn-windows.c
22 * @brief the helper for the VPN service in win32 builds. 22 * @brief the helper for the VPN service in win32 builds.
23 * Opens a virtual network-interface, sends data received on the if to stdout, 23 * Opens a virtual network-interface, sends data received on the if to stdout,
24 * sends data received on stdin to the interface 24 * sends data received on stdin to the interface
25 * @author Christian M. Fuchs 25 * @author Christian M. Fuchs
26 * 26 *
@@ -68,7 +68,7 @@
68#endif 68#endif
69 69
70/** 70/**
71 * Will this binary be run in permissions testing mode? 71 * Will this binary be run in permissions testing mode?
72 */ 72 */
73static boolean privilege_testing = FALSE; 73static boolean privilege_testing = FALSE;
74 74
@@ -90,7 +90,7 @@ static boolean privilege_testing = FALSE;
90#define INF_FILE64 "share/gnunet/openvpn-tap32/tapw64/OemWin2k.inf" 90#define INF_FILE64 "share/gnunet/openvpn-tap32/tapw64/OemWin2k.inf"
91 91
92/** 92/**
93 * Hardware ID used in the inf-file. 93 * Hardware ID used in the inf-file.
94 * This might change over time, as openvpn advances their driver 94 * This might change over time, as openvpn advances their driver
95 */ 95 */
96#define HARDWARE_ID "tap0901" 96#define HARDWARE_ID "tap0901"
@@ -101,7 +101,7 @@ static boolean privilege_testing = FALSE;
101#define TAP_WIN_MIN_MAJOR 9 101#define TAP_WIN_MIN_MAJOR 9
102 102
103/** 103/**
104 * Minimum minor-id of the driver version we can work with. 104 * Minimum minor-id of the driver version we can work with.
105 * v <= 7 has buggy IPv6. 105 * v <= 7 has buggy IPv6.
106 * v == 8 is broken for small IPv4 Packets 106 * v == 8 is broken for small IPv4 Packets
107 */ 107 */
@@ -109,7 +109,7 @@ static boolean privilege_testing = FALSE;
109 109
110/** 110/**
111 * Time in seconds to wait for our virtual device to go up after telling it to do so. 111 * Time in seconds to wait for our virtual device to go up after telling it to do so.
112 * 112 *
113 * openvpn doesn't specify a value, 4 seems sane for testing, even for openwrt 113 * openvpn doesn't specify a value, 4 seems sane for testing, even for openwrt
114 * (in fact, 4 was chosen by a fair dice roll...) 114 * (in fact, 4 was chosen by a fair dice roll...)
115 */ 115 */
@@ -121,7 +121,7 @@ static boolean privilege_testing = FALSE;
121#define INTERFACE_REGISTRY_LOCATION "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 121#define INTERFACE_REGISTRY_LOCATION "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
122 122
123/** 123/**
124 * Our local process' PID. Used for creating a sufficiently unique additional 124 * Our local process' PID. Used for creating a sufficiently unique additional
125 * hardware ID for our device. 125 * hardware ID for our device.
126 */ 126 */
127static char secondary_hwid[LINE_LEN / 2]; 127static char secondary_hwid[LINE_LEN / 2];
@@ -132,13 +132,13 @@ static char secondary_hwid[LINE_LEN / 2];
132 */ 132 */
133static char device_visible_name[256]; 133static char device_visible_name[256];
134 134
135/** 135/**
136 * This is our own local instance of a virtual network interface 136 * This is our own local instance of a virtual network interface
137 * It is (somewhat) equivalent to using tun/tap in unixoid systems 137 * It is (somewhat) equivalent to using tun/tap in unixoid systems
138 * 138 *
139 * Upon initialization, we create such an device node. 139 * Upon initialization, we create such an device node.
140 * Upon termination, we remove it again. 140 * Upon termination, we remove it again.
141 * 141 *
142 * If we crash this device might stay around. 142 * If we crash this device might stay around.
143 */ 143 */
144static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE; 144static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE;
@@ -149,7 +149,7 @@ static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE;
149static SP_DEVINFO_DATA DeviceNode; 149static SP_DEVINFO_DATA DeviceNode;
150 150
151/** 151/**
152 * GUID of our virtual device in the form of 152 * GUID of our virtual device in the form of
153 * {12345678-1234-1234-1234-123456789abc} - in hex 153 * {12345678-1234-1234-1234-123456789abc} - in hex
154 */ 154 */
155static char device_guid[256]; 155static char device_guid[256];
@@ -161,36 +161,36 @@ static char device_guid[256];
161enum IO_State 161enum IO_State
162{ 162{
163 163
164 /** 164 /**
165 * overlapped I/O is ready for work 165 * overlapped I/O is ready for work
166 */ 166 */
167 IOSTATE_READY = 0, 167 IOSTATE_READY = 0,
168 168
169 /** 169 /**
170 * overlapped I/O has been queued 170 * overlapped I/O has been queued
171 */ 171 */
172 IOSTATE_QUEUED, 172 IOSTATE_QUEUED,
173 173
174 /** 174 /**
175 * overlapped I/O has finished, but is waiting for it's write-partner 175 * overlapped I/O has finished, but is waiting for it's write-partner
176 */ 176 */
177 IOSTATE_WAITING, 177 IOSTATE_WAITING,
178 178
179 /** 179 /**
180 * there is a full buffer waiting 180 * there is a full buffer waiting
181 */ 181 */
182 IOSTATE_RESUME, 182 IOSTATE_RESUME,
183 183
184 /** 184 /**
185 * Operlapped IO states for facility objects 185 * Operlapped IO states for facility objects
186 * overlapped I/O has failed, stop processing 186 * overlapped I/O has failed, stop processing
187 */ 187 */
188 IOSTATE_FAILED 188 IOSTATE_FAILED
189 189
190}; 190};
191 191
192 192
193/** 193/**
194 * A IO Object + read/writebuffer + buffer-size for windows asynchronous IO handling 194 * A IO Object + read/writebuffer + buffer-size for windows asynchronous IO handling
195 */ 195 */
196struct io_facility 196struct io_facility
@@ -229,7 +229,7 @@ struct io_facility
229 * Amount of data actually written or read by readfile/writefile. 229 * Amount of data actually written or read by readfile/writefile.
230 */ 230 */
231 DWORD buffer_size_processed; 231 DWORD buffer_size_processed;
232 232
233 /** 233 /**
234 * How much of this buffer we have writte in total 234 * How much of this buffer we have writte in total
235 */ 235 */
@@ -248,32 +248,32 @@ typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
248 248
249/** 249/**
250 * Determines if the host OS is win32 or win64 250 * Determines if the host OS is win32 or win64
251 * 251 *
252 * @return true if 252 * @return true if
253 */ 253 */
254BOOL 254BOOL
255is_win64 () 255is_win64 ()
256{ 256{
257#if defined(_WIN64) 257#if defined(_WIN64)
258 //this is a win64 binary, 258 //this is a win64 binary,
259 return TRUE; 259 return TRUE;
260#elif defined(_WIN32) 260#elif defined(_WIN32)
261 //this is a 32bit binary, and we need to check if we are running in WOW64 261 //this is a 32bit binary, and we need to check if we are running in WOW64
262 BOOL success = FALSE; 262 BOOL success = FALSE;
263 BOOL on_wow64 = FALSE; 263 BOOL on_wow64 = FALSE;
264 LPFN_ISWOW64PROCESS IsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress (GetModuleHandle ("kernel32"), "IsWow64Process"); 264 LPFN_ISWOW64PROCESS IsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress (GetModuleHandle ("kernel32"), "IsWow64Process");
265 265
266 if (NULL != IsWow64Process) 266 if (NULL != IsWow64Process)
267 success = IsWow64Process (GetCurrentProcess (), &on_wow64); 267 success = IsWow64Process (GetCurrentProcess (), &on_wow64);
268 268
269 return success && on_wow64; 269 return success && on_wow64;
270#endif 270#endif
271} 271}
272/** 272/**
273 * Wrapper for executing a shellcommand in windows. 273 * Wrapper for executing a shellcommand in windows.
274 * 274 *
275 * @param command - the command + parameters to execute 275 * @param command - the command + parameters to execute
276 * @return * exitcode of the program executed, 276 * @return * exitcode of the program executed,
277 * * EINVAL (cmd/file not found) 277 * * EINVAL (cmd/file not found)
278 * * EPIPE (could not read STDOUT) 278 * * EPIPE (could not read STDOUT)
279 */ 279 */
@@ -365,7 +365,7 @@ remove_address6 (const char *address)
365 365
366 /* Did it work?*/ 366 /* Did it work?*/
367 if (0 != ret) 367 if (0 != ret)
368 fprintf (stderr, 368 fprintf (stderr,
369 "FATAL: removing IPv6 address failed: %s\n", 369 "FATAL: removing IPv6 address failed: %s\n",
370 strerror (ret)); 370 strerror (ret));
371} 371}
@@ -410,8 +410,8 @@ set_address4 (const char *address, const char *mask)
410 410
411 /* Did it work?*/ 411 /* Did it work?*/
412 if (0 != ret) 412 if (0 != ret)
413 fprintf (stderr, 413 fprintf (stderr,
414 "FATAL: Setting IPv4 address failed: %s\n", 414 "FATAL: Setting IPv4 address failed: %s\n",
415 strerror (ret)); 415 strerror (ret));
416 return ret; 416 return ret;
417} 417}
@@ -448,8 +448,8 @@ remove_address4 (const char *address)
448 448
449 449
450/** 450/**
451 * Setup a new virtual interface to use for tunneling. 451 * Setup a new virtual interface to use for tunneling.
452 * 452 *
453 * @return: TRUE if setup was successful, else FALSE 453 * @return: TRUE if setup was successful, else FALSE
454 */ 454 */
455static BOOL 455static BOOL
@@ -457,7 +457,7 @@ setup_interface ()
457{ 457{
458 /* 458 /*
459 * where to find our inf-file. (+ the "full" path, after windows found") 459 * where to find our inf-file. (+ the "full" path, after windows found")
460 * 460 *
461 * We do not directly input all the props here, because openvpn will update 461 * We do not directly input all the props here, because openvpn will update
462 * these details over time. 462 * these details over time.
463 */ 463 */
@@ -468,22 +468,22 @@ setup_interface ()
468 GUID class_guid; 468 GUID class_guid;
469 int str_length = 0; 469 int str_length = 0;
470 470
471 /** 471 /**
472 * Set the device's hardware ID and add it to a list. 472 * Set the device's hardware ID and add it to a list.
473 * This information will later on identify this device in registry. 473 * This information will later on identify this device in registry.
474 */ 474 */
475 strncpy (hwidlist, HARDWARE_ID, LINE_LEN); 475 strncpy (hwidlist, HARDWARE_ID, LINE_LEN);
476 /** 476 /**
477 * this is kind of over-complicated, but allows keeps things independent of 477 * this is kind of over-complicated, but allows keeps things independent of
478 * how the openvpn-hwid is actually stored. 478 * how the openvpn-hwid is actually stored.
479 * 479 *
480 * A HWID list is double-\0 terminated and \0 separated 480 * A HWID list is double-\0 terminated and \0 separated
481 */ 481 */
482 str_length = strlen (hwidlist) + 1; 482 str_length = strlen (hwidlist) + 1;
483 strncpy (&hwidlist[str_length], secondary_hwid, LINE_LEN); 483 strncpy (&hwidlist[str_length], secondary_hwid, LINE_LEN);
484 str_length += strlen (&hwidlist[str_length]) + 1; 484 str_length += strlen (&hwidlist[str_length]) + 1;
485 485
486 /** 486 /**
487 * Locate the inf-file, we need to store it somewhere where the system can 487 * Locate the inf-file, we need to store it somewhere where the system can
488 * find it. We need to pick the correct driver for win32/win64. 488 * find it. We need to pick the correct driver for win32/win64.
489 */ 489 */
@@ -493,7 +493,7 @@ setup_interface ()
493 GetFullPathNameA (INF_FILE, MAX_PATH, inf_file_path, &temp_inf_filename); 493 GetFullPathNameA (INF_FILE, MAX_PATH, inf_file_path, &temp_inf_filename);
494 494
495 fprintf (stderr, "INFO: Located our driver's .inf file at %s\n", inf_file_path); 495 fprintf (stderr, "INFO: Located our driver's .inf file at %s\n", inf_file_path);
496 /** 496 /**
497 * Bootstrap our device info using the drivers inf-file 497 * Bootstrap our device info using the drivers inf-file
498 */ 498 */
499 if ( ! SetupDiGetINFClassA (inf_file_path, 499 if ( ! SetupDiGetINFClassA (inf_file_path,
@@ -502,9 +502,9 @@ setup_interface ()
502 NULL)) 502 NULL))
503 return FALSE; 503 return FALSE;
504 504
505 /** 505 /**
506 * Collect all the other needed information... 506 * Collect all the other needed information...
507 * let the system fill our this form 507 * let the system fill our this form
508 */ 508 */
509 DeviceInfo = SetupDiCreateDeviceInfoList (&class_guid, NULL); 509 DeviceInfo = SetupDiCreateDeviceInfoList (&class_guid, NULL);
510 if (DeviceInfo == INVALID_HANDLE_VALUE) 510 if (DeviceInfo == INVALID_HANDLE_VALUE)
@@ -549,9 +549,9 @@ setup_interface ()
549 549
550 550
551/** 551/**
552 * Remove our new virtual interface to use for tunneling. 552 * Remove our new virtual interface to use for tunneling.
553 * This function must be called AFTER setup_interface! 553 * This function must be called AFTER setup_interface!
554 * 554 *
555 * @return: TRUE if destruction was successful, else FALSE 555 * @return: TRUE if destruction was successful, else FALSE
556 */ 556 */
557static BOOL 557static BOOL
@@ -567,7 +567,7 @@ remove_interface ()
567 remove.Scope = DI_REMOVEDEVICE_GLOBAL; 567 remove.Scope = DI_REMOVEDEVICE_GLOBAL;
568 remove.ClassInstallHeader.InstallFunction = DIF_REMOVE; 568 remove.ClassInstallHeader.InstallFunction = DIF_REMOVE;
569 /* 569 /*
570 * 1. Prepare our existing device information set, and place the 570 * 1. Prepare our existing device information set, and place the
571 * uninstall related information into the structure 571 * uninstall related information into the structure
572 */ 572 */
573 if ( ! SetupDiSetClassInstallParamsA (DeviceInfo, 573 if ( ! SetupDiSetClassInstallParamsA (DeviceInfo,
@@ -584,7 +584,7 @@ remove_interface ()
584 return FALSE; 584 return FALSE;
585 585
586 SetupDiDestroyDeviceInfoList (DeviceInfo); 586 SetupDiDestroyDeviceInfoList (DeviceInfo);
587 587
588 fprintf (stderr, "DEBUG: removed interface successfully\n"); 588 fprintf (stderr, "DEBUG: removed interface successfully\n");
589 589
590 return TRUE; 590 return TRUE;
@@ -593,8 +593,8 @@ remove_interface ()
593 593
594/** 594/**
595 * Do all the lookup necessary to retrieve the inteface's actual name 595 * Do all the lookup necessary to retrieve the inteface's actual name
596 * off the registry. 596 * off the registry.
597 * 597 *
598 * @return: TRUE if we were able to lookup the interface's name, else FALSE 598 * @return: TRUE if we were able to lookup the interface's name, else FALSE
599 */ 599 */
600static BOOL 600static BOOL
@@ -618,7 +618,7 @@ resolve_interface_name ()
618 0, //must be 0 618 0, //must be 0
619 NULL)) //hMachine, we are local 619 NULL)) //hMachine, we are local
620 return FALSE; 620 return FALSE;
621 621
622 fprintf (stderr, "DEBUG: Resolving interface name for network device %s\n",pnp_instance_id); 622 fprintf (stderr, "DEBUG: Resolving interface name for network device %s\n",pnp_instance_id);
623 623
624 /* Registry is incredibly slow, retry for up to 30 seconds to allow registry to refresh */ 624 /* Registry is incredibly slow, retry for up to 30 seconds to allow registry to refresh */
@@ -636,7 +636,7 @@ resolve_interface_name ()
636 &adapter_key_handle)) 636 &adapter_key_handle))
637 return FALSE; 637 return FALSE;
638 638
639 /* Of course there is a multitude of entries here, with arbitrary names, 639 /* Of course there is a multitude of entries here, with arbitrary names,
640 * thus we need to iterate through there. 640 * thus we need to iterate through there.
641 */ 641 */
642 while (!retval) 642 while (!retval)
@@ -661,7 +661,7 @@ resolve_interface_name ()
661 NULL, 661 NULL,
662 NULL); 662 NULL);
663 663
664 /* this may fail due to one of two reasons: 664 /* this may fail due to one of two reasons:
665 * we are at the end of the list*/ 665 * we are at the end of the list*/
666 if (ERROR_NO_MORE_ITEMS == status) 666 if (ERROR_NO_MORE_ITEMS == status)
667 break; 667 break;
@@ -712,8 +712,8 @@ resolve_interface_name ()
712 if (status != ERROR_SUCCESS || data_type != REG_SZ) 712 if (status != ERROR_SUCCESS || data_type != REG_SZ)
713 goto cleanup; 713 goto cleanup;
714 714
715 /* 715 /*
716 * we have successfully found OUR instance, 716 * we have successfully found OUR instance,
717 * save the device GUID before exiting 717 * save the device GUID before exiting
718 */ 718 */
719 719
@@ -735,7 +735,7 @@ cleanup:
735 735
736/** 736/**
737 * Determines the version of the installed TAP32 driver and checks if it's sufficiently new for GNUNET 737 * Determines the version of the installed TAP32 driver and checks if it's sufficiently new for GNUNET
738 * 738 *
739 * @param handle the handle to our tap device 739 * @param handle the handle to our tap device
740 * @return TRUE if the version is sufficient, else FALSE 740 * @return TRUE if the version is sufficient, else FALSE
741 */ 741 */
@@ -761,7 +761,7 @@ check_tapw32_version (HANDLE handle)
761 TAP_WIN_MIN_MINOR); 761 TAP_WIN_MIN_MINOR);
762 return FALSE; 762 return FALSE;
763 } 763 }
764 764
765 return TRUE; 765 return TRUE;
766} 766}
767 767
@@ -827,8 +827,8 @@ init_tun ()
827 827
828/** 828/**
829 * Brings a TAP device up and sets it to connected state. 829 * Brings a TAP device up and sets it to connected state.
830 * 830 *
831 * @param handle the handle to our TAP device 831 * @param handle the handle to our TAP device
832 * @return True if the operation succeeded, else false 832 * @return True if the operation succeeded, else false
833 */ 833 */
834static BOOL 834static BOOL
@@ -854,25 +854,25 @@ tun_up (HANDLE handle)
854 854
855/** 855/**
856 * Attempts to read off an input facility (tap or named pipe) in overlapped mode. 856 * Attempts to read off an input facility (tap or named pipe) in overlapped mode.
857 * 857 *
858 * 1. 858 * 1.
859 * If the input facility is in IOSTATE_READY, it will issue a new read operation to the 859 * If the input facility is in IOSTATE_READY, it will issue a new read operation to the
860 * input handle. Then it goes into IOSTATE_QUEUED state. 860 * input handle. Then it goes into IOSTATE_QUEUED state.
861 * In case the read succeeded instantly the input facility enters 3. 861 * In case the read succeeded instantly the input facility enters 3.
862 * 862 *
863 * 2. 863 * 2.
864 * If the input facility is in IOSTATE_QUEUED state, it will check if the queued read has finished already. 864 * If the input facility is in IOSTATE_QUEUED state, it will check if the queued read has finished already.
865 * If it has finished, go to state 3. 865 * If it has finished, go to state 3.
866 * If it has failed, set IOSTATE_FAILED 866 * If it has failed, set IOSTATE_FAILED
867 * 867 *
868 * 3. 868 * 3.
869 * If the output facility is in state IOSTATE_READY, the read-buffer is copied to the output buffer. 869 * If the output facility is in state IOSTATE_READY, the read-buffer is copied to the output buffer.
870 * The input facility enters state IOSTATE_READY 870 * The input facility enters state IOSTATE_READY
871 * The output facility enters state IOSTATE_READY 871 * The output facility enters state IOSTATE_READY
872 * If the output facility is in state IOSTATE_QUEUED, the input facility enters IOSTATE_WAITING 872 * If the output facility is in state IOSTATE_QUEUED, the input facility enters IOSTATE_WAITING
873 * 873 *
874 * IOSTATE_WAITING is reset by the output facility, once it has completed. 874 * IOSTATE_WAITING is reset by the output facility, once it has completed.
875 * 875 *
876 * @param input_facility input named pipe or file to work with. 876 * @param input_facility input named pipe or file to work with.
877 * @param output_facility output pipe or file to hand over data to. 877 * @param output_facility output pipe or file to hand over data to.
878 * @return false if an event reset was impossible (OS error), else true 878 * @return false if an event reset was impossible (OS error), else true
@@ -883,11 +883,11 @@ attempt_read_tap (struct io_facility * input_facility,
883{ 883{
884 struct GNUNET_MessageHeader * hdr; 884 struct GNUNET_MessageHeader * hdr;
885 unsigned short size; 885 unsigned short size;
886 886
887 switch (input_facility->facility_state) 887 switch (input_facility->facility_state)
888 { 888 {
889 case IOSTATE_READY: 889 case IOSTATE_READY:
890 { 890 {
891 if (! ResetEvent (input_facility->overlapped.hEvent)) 891 if (! ResetEvent (input_facility->overlapped.hEvent))
892 { 892 {
893 return FALSE; 893 return FALSE;
@@ -906,9 +906,9 @@ attempt_read_tap (struct io_facility * input_facility,
906 /* reset event manually*/ 906 /* reset event manually*/
907 if (! SetEvent (input_facility->overlapped.hEvent)) 907 if (! SetEvent (input_facility->overlapped.hEvent))
908 return FALSE; 908 return FALSE;
909 909
910 fprintf (stderr, "DEBUG: tap read succeeded immediately\n"); 910 fprintf (stderr, "DEBUG: tap read succeeded immediately\n");
911 911
912 /* we successfully read something from the TAP and now need to 912 /* we successfully read something from the TAP and now need to
913 * send it our via STDOUT. Is that possible at the moment? */ 913 * send it our via STDOUT. Is that possible at the moment? */
914 if ((IOSTATE_READY == output_facility->facility_state || 914 if ((IOSTATE_READY == output_facility->facility_state ||
@@ -917,7 +917,7 @@ attempt_read_tap (struct io_facility * input_facility,
917 { /* hand over this buffers content and apply message header for gnunet */ 917 { /* hand over this buffers content and apply message header for gnunet */
918 hdr = (struct GNUNET_MessageHeader *) output_facility->buffer; 918 hdr = (struct GNUNET_MessageHeader *) output_facility->buffer;
919 size = input_facility->buffer_size + sizeof (struct GNUNET_MessageHeader); 919 size = input_facility->buffer_size + sizeof (struct GNUNET_MessageHeader);
920 920
921 memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader), 921 memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader),
922 input_facility->buffer, 922 input_facility->buffer,
923 input_facility->buffer_size); 923 input_facility->buffer_size);
@@ -964,7 +964,7 @@ attempt_read_tap (struct io_facility * input_facility,
964 return FALSE; 964 return FALSE;
965 965
966 fprintf (stderr, "DEBUG: tap read succeeded delayed\n"); 966 fprintf (stderr, "DEBUG: tap read succeeded delayed\n");
967 967
968 /* we successfully read something from the TAP and now need to 968 /* we successfully read something from the TAP and now need to
969 * send it our via STDOUT. Is that possible at the moment? */ 969 * send it our via STDOUT. Is that possible at the moment? */
970 if ((IOSTATE_READY == output_facility->facility_state || 970 if ((IOSTATE_READY == output_facility->facility_state ||
@@ -973,7 +973,7 @@ attempt_read_tap (struct io_facility * input_facility,
973 { /* hand over this buffers content and apply message header for gnunet */ 973 { /* hand over this buffers content and apply message header for gnunet */
974 hdr = (struct GNUNET_MessageHeader *) output_facility->buffer; 974 hdr = (struct GNUNET_MessageHeader *) output_facility->buffer;
975 size = input_facility->buffer_size + sizeof (struct GNUNET_MessageHeader); 975 size = input_facility->buffer_size + sizeof (struct GNUNET_MessageHeader);
976 976
977 memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader), 977 memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader),
978 input_facility->buffer, 978 input_facility->buffer,
979 input_facility->buffer_size); 979 input_facility->buffer_size);
@@ -1026,26 +1026,26 @@ attempt_read_tap (struct io_facility * input_facility,
1026 1026
1027/** 1027/**
1028 * Attempts to read off an input facility (tap or named pipe) in overlapped mode. 1028 * Attempts to read off an input facility (tap or named pipe) in overlapped mode.
1029 * 1029 *
1030 * 1. 1030 * 1.
1031 * If the input facility is in IOSTATE_READY, it will issue a new read operation to the 1031 * If the input facility is in IOSTATE_READY, it will issue a new read operation to the
1032 * input handle. Then it goes into IOSTATE_QUEUED state. 1032 * input handle. Then it goes into IOSTATE_QUEUED state.
1033 * In case the read succeeded instantly the input facility enters 3. 1033 * In case the read succeeded instantly the input facility enters 3.
1034 * 1034 *
1035 * 2. 1035 * 2.
1036 * If the input facility is in IOSTATE_QUEUED state, it will check if the queued read has finished already. 1036 * If the input facility is in IOSTATE_QUEUED state, it will check if the queued read has finished already.
1037 * If it has finished, go to state 3. 1037 * If it has finished, go to state 3.
1038 * If it has failed, set IOSTATE_FAILED 1038 * If it has failed, set IOSTATE_FAILED
1039 * 1039 *
1040 * 3. 1040 * 3.
1041 * If the facility is finished with ready 1041 * If the facility is finished with ready
1042 * The read-buffer is copied to the output buffer, except for the GNUNET_MessageHeader. 1042 * The read-buffer is copied to the output buffer, except for the GNUNET_MessageHeader.
1043 * The input facility enters state IOSTATE_READY 1043 * The input facility enters state IOSTATE_READY
1044 * The output facility enters state IOSTATE_READY 1044 * The output facility enters state IOSTATE_READY
1045 * If the output facility is in state IOSTATE_QUEUED, the input facility enters IOSTATE_WAITING 1045 * If the output facility is in state IOSTATE_QUEUED, the input facility enters IOSTATE_WAITING
1046 * 1046 *
1047 * IOSTATE_WAITING is reset by the output facility, once it has completed. 1047 * IOSTATE_WAITING is reset by the output facility, once it has completed.
1048 * 1048 *
1049 * @param input_facility input named pipe or file to work with. 1049 * @param input_facility input named pipe or file to work with.
1050 * @param output_facility output pipe or file to hand over data to. 1050 * @param output_facility output pipe or file to hand over data to.
1051 * @return false if an event reset was impossible (OS error), else true 1051 * @return false if an event reset was impossible (OS error), else true
@@ -1055,17 +1055,17 @@ attempt_read_stdin (struct io_facility * input_facility,
1055 struct io_facility * output_facility) 1055 struct io_facility * output_facility)
1056{ 1056{
1057 struct GNUNET_MessageHeader * hdr; 1057 struct GNUNET_MessageHeader * hdr;
1058 1058
1059 switch (input_facility->facility_state) 1059 switch (input_facility->facility_state)
1060 { 1060 {
1061 case IOSTATE_READY: 1061 case IOSTATE_READY:
1062 { 1062 {
1063 input_facility->buffer_size = 0; 1063 input_facility->buffer_size = 0;
1064 1064
1065partial_read_iostate_ready: 1065partial_read_iostate_ready:
1066 if (! ResetEvent (input_facility->overlapped.hEvent)) 1066 if (! ResetEvent (input_facility->overlapped.hEvent))
1067 return FALSE; 1067 return FALSE;
1068 1068
1069 /* Check how the task is handled */ 1069 /* Check how the task is handled */
1070 if (ReadFile (input_facility->handle, 1070 if (ReadFile (input_facility->handle,
1071 input_facility->buffer + input_facility->buffer_size, 1071 input_facility->buffer + input_facility->buffer_size,
@@ -1113,7 +1113,7 @@ partial_read_iostate_ready:
1113 input_facility->facility_state = IOSTATE_WAITING; 1113 input_facility->facility_state = IOSTATE_WAITING;
1114 else /* we read nothing */ 1114 else /* we read nothing */
1115 input_facility->facility_state = IOSTATE_READY; 1115 input_facility->facility_state = IOSTATE_READY;
1116 } 1116 }
1117 else /* operation was either queued or failed*/ 1117 else /* operation was either queued or failed*/
1118 { 1118 {
1119 int err = GetLastError (); 1119 int err = GetLastError ();
@@ -1141,13 +1141,13 @@ partial_read_iostate_ready:
1141 FALSE)) 1141 FALSE))
1142 {/* successful return for a queued operation */ 1142 {/* successful return for a queued operation */
1143 hdr = (struct GNUNET_MessageHeader *) input_facility->buffer; 1143 hdr = (struct GNUNET_MessageHeader *) input_facility->buffer;
1144 1144
1145 if (! ResetEvent (input_facility->overlapped.hEvent)) 1145 if (! ResetEvent (input_facility->overlapped.hEvent))
1146 return FALSE; 1146 return FALSE;
1147 1147
1148 fprintf (stderr, "DEBUG: stdin read succeeded delayed\n"); 1148 fprintf (stderr, "DEBUG: stdin read succeeded delayed\n");
1149 input_facility->buffer_size += input_facility->buffer_size_processed; 1149 input_facility->buffer_size += input_facility->buffer_size_processed;
1150 1150
1151 if ((ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) || 1151 if ((ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) ||
1152 (ntohs (hdr->size) > sizeof (input_facility->buffer))) 1152 (ntohs (hdr->size) > sizeof (input_facility->buffer)))
1153 { 1153 {
@@ -1209,7 +1209,7 @@ partial_read_iostate_ready:
1209 * Attempts to write to an output facility (tap or named pipe) in overlapped mode. 1209 * Attempts to write to an output facility (tap or named pipe) in overlapped mode.
1210 * 1210 *
1211 * TODO: high level description 1211 * TODO: high level description
1212 * 1212 *
1213 * @param output_facility output pipe or file to hand over data to. 1213 * @param output_facility output pipe or file to hand over data to.
1214 * @param input_facility input named pipe or file to work with. 1214 * @param input_facility input named pipe or file to work with.
1215 * @return false if an event reset was impossible (OS error), else true 1215 * @return false if an event reset was impossible (OS error), else true
@@ -1222,7 +1222,7 @@ attempt_write (struct io_facility * output_facility,
1222 { 1222 {
1223 case IOSTATE_READY: 1223 case IOSTATE_READY:
1224 output_facility->buffer_size_written = 0; 1224 output_facility->buffer_size_written = 0;
1225 1225
1226continue_partial_write: 1226continue_partial_write:
1227 if (! ResetEvent (output_facility->overlapped.hEvent)) 1227 if (! ResetEvent (output_facility->overlapped.hEvent))
1228 return FALSE; 1228 return FALSE;
@@ -1237,7 +1237,7 @@ continue_partial_write:
1237 1237
1238 fprintf (stderr, "DEBUG: write succeeded immediately\n"); 1238 fprintf (stderr, "DEBUG: write succeeded immediately\n");
1239 output_facility->buffer_size_written += output_facility->buffer_size_processed; 1239 output_facility->buffer_size_written += output_facility->buffer_size_processed;
1240 1240
1241 /* reset event manually*/ 1241 /* reset event manually*/
1242 if (! SetEvent (output_facility->overlapped.hEvent)) 1242 if (! SetEvent (output_facility->overlapped.hEvent))
1243 return FALSE; 1243 return FALSE;
@@ -1245,7 +1245,7 @@ continue_partial_write:
1245 /* partial write */ 1245 /* partial write */
1246 if (output_facility->buffer_size_written < output_facility->buffer_size) 1246 if (output_facility->buffer_size_written < output_facility->buffer_size)
1247 goto continue_partial_write; 1247 goto continue_partial_write;
1248 1248
1249 /* we are now waiting for our buffer to be filled*/ 1249 /* we are now waiting for our buffer to be filled*/
1250 output_facility->facility_state = IOSTATE_WAITING; 1250 output_facility->facility_state = IOSTATE_WAITING;
1251 1251
@@ -1272,7 +1272,7 @@ continue_partial_write:
1272 return TRUE; 1272 return TRUE;
1273 case IOSTATE_QUEUED: 1273 case IOSTATE_QUEUED:
1274 // there was an operation going on already, check if that has completed now. 1274 // there was an operation going on already, check if that has completed now.
1275 1275
1276 if (GetOverlappedResult (output_facility->handle, 1276 if (GetOverlappedResult (output_facility->handle,
1277 &output_facility->overlapped, 1277 &output_facility->overlapped,
1278 &output_facility->buffer_size_processed, 1278 &output_facility->buffer_size_processed,
@@ -1280,17 +1280,17 @@ continue_partial_write:
1280 {/* successful return for a queued operation */ 1280 {/* successful return for a queued operation */
1281 if (! ResetEvent (output_facility->overlapped.hEvent)) 1281 if (! ResetEvent (output_facility->overlapped.hEvent))
1282 return FALSE; 1282 return FALSE;
1283 1283
1284 fprintf (stderr, "DEBUG: write succeeded delayed\n"); 1284 fprintf (stderr, "DEBUG: write succeeded delayed\n");
1285 output_facility->buffer_size_written += output_facility->buffer_size_processed; 1285 output_facility->buffer_size_written += output_facility->buffer_size_processed;
1286 1286
1287 /* partial write */ 1287 /* partial write */
1288 if (output_facility->buffer_size_written < output_facility->buffer_size) 1288 if (output_facility->buffer_size_written < output_facility->buffer_size)
1289 goto continue_partial_write; 1289 goto continue_partial_write;
1290 1290
1291 /* we are now waiting for our buffer to be filled*/ 1291 /* we are now waiting for our buffer to be filled*/
1292 output_facility->facility_state = IOSTATE_WAITING; 1292 output_facility->facility_state = IOSTATE_WAITING;
1293 1293
1294 /* we successfully wrote something and now need to reset our reader */ 1294 /* we successfully wrote something and now need to reset our reader */
1295 if (IOSTATE_WAITING == input_facility->facility_state) 1295 if (IOSTATE_WAITING == input_facility->facility_state)
1296 input_facility->facility_state = IOSTATE_RESUME; 1296 input_facility->facility_state = IOSTATE_RESUME;
@@ -1307,7 +1307,7 @@ continue_partial_write:
1307 fprintf (stderr, "FATAL: Write to handle failed, exiting\n"); 1307 fprintf (stderr, "FATAL: Write to handle failed, exiting\n");
1308 } 1308 }
1309 } 1309 }
1310 default: 1310 default:
1311 return TRUE; 1311 return TRUE;
1312 } 1312 }
1313} 1313}
@@ -1315,7 +1315,7 @@ continue_partial_write:
1315 1315
1316/** 1316/**
1317 * Initialize a overlapped structure 1317 * Initialize a overlapped structure
1318 * 1318 *
1319 * @param elem the element to initilize 1319 * @param elem the element to initilize
1320 * @param initial_state the initial state for this instance 1320 * @param initial_state the initial state for this instance
1321 * @param signaled if the hEvent created should default to signaled or not 1321 * @param signaled if the hEvent created should default to signaled or not
@@ -1361,7 +1361,7 @@ run (HANDLE tap_handle)
1361 /* tun up: */ 1361 /* tun up: */
1362 /* we do this HERE and not beforehand (in init_tun()), in contrast to openvpn 1362 /* we do this HERE and not beforehand (in init_tun()), in contrast to openvpn
1363 * to remove the need to flush the arp cache, handle DHCP and wrong IPs. 1363 * to remove the need to flush the arp cache, handle DHCP and wrong IPs.
1364 * 1364 *
1365 * DHCP and such are all features we will never use in gnunet afaik. 1365 * DHCP and such are all features we will never use in gnunet afaik.
1366 * But for openvpn those are essential. 1366 * But for openvpn those are essential.
1367 */ 1367 */
@@ -1383,12 +1383,12 @@ run (HANDLE tap_handle)
1383 /* Debug output to console STDIN/STDOUT*/ 1383 /* Debug output to console STDIN/STDOUT*/
1384 std_in.handle = parent_std_in_handle; 1384 std_in.handle = parent_std_in_handle;
1385 std_out.handle = parent_std_out_handle; 1385 std_out.handle = parent_std_out_handle;
1386 1386
1387#else 1387#else
1388 fprintf (stderr, "DEBUG: reopening stdin/out for overlapped IO\n"); 1388 fprintf (stderr, "DEBUG: reopening stdin/out for overlapped IO\n");
1389 /* 1389 /*
1390 * Find out the types of our handles. 1390 * Find out the types of our handles.
1391 * This part is a problem, because in windows we need to handle files, 1391 * This part is a problem, because in windows we need to handle files,
1392 * pipes and the console differently. 1392 * pipes and the console differently.
1393 */ 1393 */
1394 if ((FILE_TYPE_PIPE != GetFileType (parent_std_in_handle)) || 1394 if ((FILE_TYPE_PIPE != GetFileType (parent_std_in_handle)) ||
@@ -1472,16 +1472,16 @@ main (int argc, char **argv)
1472 int global_ret = 0; 1472 int global_ret = 0;
1473 BOOL have_ip4 = FALSE; 1473 BOOL have_ip4 = FALSE;
1474 BOOL have_ip6 = FALSE; 1474 BOOL have_ip6 = FALSE;
1475 1475
1476 if (argc > 1 && 0 != strcmp (argv[1], "-d")){ 1476 if (argc > 1 && 0 != strcmp (argv[1], "-d")){
1477 privilege_testing = TRUE; 1477 privilege_testing = TRUE;
1478 fprintf (stderr, 1478 fprintf (stderr,
1479 "%s", 1479 "%s",
1480 "DEBUG: Running binary in privilege testing mode."); 1480 "DEBUG: Running binary in privilege testing mode.");
1481 argv++; 1481 argv++;
1482 argc--; 1482 argc--;
1483 } 1483 }
1484 1484
1485 if (6 != argc) 1485 if (6 != argc)
1486 { 1486 {
1487 fprintf (stderr, 1487 fprintf (stderr,
@@ -1493,9 +1493,9 @@ main (int argc, char **argv)
1493 strncpy (hwid, argv[1], LINE_LEN); 1493 strncpy (hwid, argv[1], LINE_LEN);
1494 hwid[LINE_LEN - 1] = '\0'; 1494 hwid[LINE_LEN - 1] = '\0';
1495 1495
1496 /* 1496 /*
1497 * We use our PID for finding/resolving the control-panel name of our virtual 1497 * We use our PID for finding/resolving the control-panel name of our virtual
1498 * device. PIDs are (of course) unique at runtime, thus we can safely use it 1498 * device. PIDs are (of course) unique at runtime, thus we can safely use it
1499 * as additional hardware-id for our device. 1499 * as additional hardware-id for our device.
1500 */ 1500 */
1501 snprintf (secondary_hwid, LINE_LEN / 2, "%s-%d", 1501 snprintf (secondary_hwid, LINE_LEN / 2, "%s-%d",