#include <windows.h>
#include <setupapi.h>
#include <ddk/cfgmgr32.h>
+#include <ddk/newdev.h>
+#include <Winsock2.h>
+#include <time.h>
#include "platform.h"
#include "tap-windows.h"
-#include <Winsock2.h>
-
/**
* Need 'struct GNUNET_MessageHeader'.
*/
* Should we print (interesting|debug) messages that can happen during
* normal operation?
*/
-#define DEBUG GNUNET_NO
+//#define DEBUG GNUNET_YES
/**
* Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
* Name or Path+Name of our driver in Unicode.
* The .sys and .cat files HAVE to be in the same location as this file!
*/
-#define INF_FILE "tapw32.inf"
+#define INF_FILE "OemWin2k.inf"
/**
* Hardware ID used in the inf-file.
* This might change over time, as openvpn advances their driver
*/
-#define HARDWARE_ID "TAP0901"
-
-/**
- * Component ID if our driver
- */
-#define TAP_WIN_COMPONENT_ID "tap0901"
+#define HARDWARE_ID "tap0901"
/**
* Minimum major-id of the driver version we can work with
/**
* Location of the network interface list resides in registry.
- * TODO: is this fixed on all version of windows? Checked with XP and 7
*/
#define INTERFACE_REGISTRY_LOCATION "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
*/
static char device_guid[256];
-/* Overlapped IO Begins here (warning: nasty!) */
-/**
- * A overlapped-IO Object + read/writebuffer + buffer-size for windows asynchronous IO handling
+/**
+ * Possible states of an IO facility.
*/
-struct overlapped_facility
+enum IO_State
{
- int iostate;
- BOOL status; // BOOL is winbool, NOT boolean!
- DWORD flags;
- OVERLAPPED overlapped;
+ /**
+ * overlapped I/O is ready for work
+ */
+ IOSTATE_READY = 0,
+
+ /**
+ * overlapped I/O has been queued
+ */
+ IOSTATE_QUEUED,
+
+ /**
+ * overlapped I/O has finished, but is waiting for it's write-partner
+ */
+ IOSTATE_WAITING,
+
+ /**
+ * there is a full buffer waiting
+ */
+ IOSTATE_RESUME,
+
+ /**
+ * Operlapped IO states for facility objects
+ * overlapped I/O has failed, stop processing
+ */
+ IOSTATE_FAILED
- DWORD buffer_size;
- unsigned char buffer[MAX_SIZE];
};
+
/**
- * Operlapped IO states for its objects
+ * A IO Object + read/writebuffer + buffer-size for windows asynchronous IO handling
*/
-#define IOSTATE_INITIAL 0
-#define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
-#define IOSTATE_IMMEDIATE_RETURN 2 /* I/O function returned immediately without queueing */
+struct io_facility
+{
+ /**
+ * The mode the state machine associated with this object is in.
+ */
+ enum IO_State facility_state;
+
+ /**
+ * If the path is open or blocked in general (used for quickly checking)
+ */
+ BOOL path_open; // BOOL is winbool (int), NOT boolean (unsigned char)!
+
+ /**
+ * Windows Object-Handle (used for accessing TAP and STDIN/STDOUT)
+ */
+ HANDLE handle;
-#if WINVER < 0x0600
+ /**
+ * Overlaped IO structure used for asynchronous IO in windows.
+ */
+ OVERLAPPED overlapped;
+
+ /**
+ * Buffer for reading things to and writing from...
+ */
+ unsigned char buffer[MAX_SIZE];
+
+ /**
+ * How much of this buffer was used when reading or how much data can be written
+ */
+ DWORD buffer_size;
+
+ /**
+ * Amount of data written, is compared to buffer_size.
+ */
+ DWORD buffer_size_written;
+};
/**
- * inet_pton() wrapper for WSAStringToAddress()
- *
- * this is needed as long as we support WinXP, because only Vista+ support
- * inet_pton at all, and mingw does not yet offer inet_pton/ntop at all
- *
- * @param af - IN - the aftype this address is supposed to be (v4/v6)
- * @param src - IN - the presentation form of the address
- * @param dst - OUT - the numerical form of the address
- * @return 0 on success, 1 on failure
+ * ReOpenFile is only available as of XP SP2 and 2003 SP1
*/
-int
-inet_pton (int af, const char *src, void *dst)
-{
- struct sockaddr_storage addr;
- int size = sizeof (addr);
- char local_copy[INET6_ADDRSTRLEN + 1];
+WINBASEAPI HANDLE WINAPI ReOpenFile (HANDLE, DWORD, DWORD, DWORD);
- ZeroMemory (&addr, sizeof (addr));
- /* stupid non-const API */
- strncpy (local_copy, src, INET6_ADDRSTRLEN + 1);
- local_copy[INET6_ADDRSTRLEN] = 0;
-
- if (WSAStringToAddressA (local_copy, af, NULL, (struct sockaddr *) &addr, &size) == 0)
- {
- switch (af)
- {
- case AF_INET:
- *(struct in_addr *) dst = ((struct sockaddr_in *) &addr)->sin_addr;
- return 1;
- case AF_INET6:
- *(struct in6_addr *) dst = ((struct sockaddr_in6 *) &addr)->sin6_addr;
- return 1;
- }
- }
- return 0;
-}
-#endif
/**
* Wrapper for executing a shellcommand in windows.
* * EPIPE (could not read STDOUT)
*/
static int
-execute_shellcommand (char * command)
+execute_shellcommand (const char *command)
{
FILE *pipe;
- if (NULL == command ||
- NULL == (pipe = _popen (command, "rt")))
+ if ( (NULL == command) ||
+ (NULL == (pipe = _popen (command, "rt"))) )
return EINVAL;
-#ifdef TESTING
- {
- char output[LINE_LEN];
-
- printf ("executed command: %s", command);
- while (NULL != fgets (output, sizeof (output), pipe))
- printf (output);
- }
+#ifdef DEBUG
+ fprintf (stderr, "DEBUG: Command output: \n");
+ char output[LINE_LEN];
+ while (NULL != fgets (output, sizeof (output), pipe))
+ fprintf (stderr, "%s", output);
#endif
- if (!feof (pipe))
- return EPIPE;
-
return _pclose (pipe);
}
+
/**
* @brief Sets the IPv6-Address given in address on the interface dev
*
* @param address the IPv6-Address
* @param prefix_len the length of the network-prefix
*/
-static void
+static int
set_address6 (const char *address, unsigned long prefix_len)
{
int ret = EINVAL;
sa6.sin6_family = AF_INET6;
if (1 != inet_pton (AF_INET6, address, &sa6.sin6_addr.s6_addr))
{
- fprintf (stderr, "Failed to parse address `%s': %s\n", address,
+ fprintf (stderr, "ERROR: Failed to parse address `%s': %s\n", address,
strerror (errno));
- exit (1);
+ return -1;
}
/*
* prepare the command
*/
snprintf (command, LINE_LEN,
- "netsh interface ipv6 add address \"%s\" %s/%d",
+ "netsh interface ipv6 add address \"%s\" %s/%d store=active",
device_visible_name, address, prefix_len);
/*
* Set the address
/* Did it work?*/
if (0 != ret)
- {
- fprintf (stderr, "Setting IPv6 address failed: %s\n", strerror (ret));
- exit (1); // FIXME: return error code, shut down interface / unload driver
- }
+ fprintf (stderr, "FATAL: Setting IPv6 address failed: %s\n", strerror (ret));
+ return ret;
}
+
+/**
+ * @brief Removes the IPv6-Address given in address from the interface dev
+ *
+ * @param dev the interface to remove
+ * @param address the IPv4-Address
+ * @param mask the netmask
+ */
+static void
+remove_address6 (const char *address)
+{
+ char command[LINE_LEN];
+ int ret = EINVAL;
+
+ // sanity checking was already done in set_address6
+ /*
+ * prepare the command
+ */
+ snprintf (command, LINE_LEN,
+ "netsh interface ipv6 delete address \"%s\" store=persistent",
+ device_visible_name, address);
+ /*
+ * Set the address
+ */
+ ret = execute_shellcommand (command);
+
+ /* Did it work?*/
+ if (0 != ret)
+ fprintf (stderr, "FATAL: removing IPv6 address failed: %s\n", strerror (ret));
+}
+
+
/**
* @brief Sets the IPv4-Address given in address on the interface dev
*
* @param address the IPv4-Address
* @param mask the netmask
*/
-static void
+static int
set_address4 (const char *address, const char *mask)
{
int ret = EINVAL;
*/
if (1 != inet_pton (AF_INET, address, &addr.sin_addr.s_addr))
{
- fprintf (stderr, "Failed to parse address `%s': %s\n", address,
+ fprintf (stderr, "ERROR: Failed to parse address `%s': %s\n", address,
strerror (errno));
- exit (1);
+ return -1;
}
+ // Set Device to Subnet-Mode?
+ // do we really need tun.c:2925 ?
/*
* prepare the command
*/
snprintf (command, LINE_LEN,
- "netsh interface ipv4 add address \"%s\" %s %s",
+ "netsh interface ipv4 add address \"%s\" %s %s store=active",
device_visible_name, address, mask);
/*
* Set the address
/* Did it work?*/
if (0 != ret)
- {
- fprintf (stderr, "Setting IPv4 address failed: %s\n", strerror (ret));
- exit (1); // FIXME: return error code, shut down interface / unload driver
- }
+ fprintf (stderr, "FATAL: Setting IPv4 address failed: %s\n", strerror (ret));
+ return ret;
+}
+
+
+/**
+ * @brief Removes the IPv4-Address given in address from the interface dev
+ *
+ * @param dev the interface to remove
+ * @param address the IPv4-Address
+ * @param mask the netmask
+ */
+static void
+remove_address4 (const char *address)
+{
+ char command[LINE_LEN];
+ int ret = EINVAL;
+
+ // sanity checking was already done in set_address4
+
+ /*
+ * prepare the command
+ */
+ snprintf (command, LINE_LEN,
+ "netsh interface ipv4 delete address \"%s\" gateway=all store=persistent",
+ device_visible_name, address);
+ /*
+ * Set the address
+ */
+ ret = execute_shellcommand (command);
+
+ /* Did it work?*/
+ if (0 != ret)
+ fprintf (stderr, "FATAL: removing IPv4 address failed: %s\n", strerror (ret));
}
+
/**
* Setup a new virtual interface to use for tunneling.
*
* @return: TRUE if setup was successful, else FALSE
*/
-static boolean
+static BOOL
setup_interface ()
{
/*
* these details over time.
*/
char inf_file_path[MAX_PATH];
+ char * temp_inf_filename;
char hwidlist[LINE_LEN + 4];
char class_name[128];
GUID class_guid;
- int str_lenth = 0;
+ int str_length = 0;
/**
* Set the device's hardware ID and add it to a list.
* This information will later on identify this device in registry.
- *
- * TODO: Currently we just use TAP0901 as HWID,
- * but we might want to add additional information
*/
strncpy (hwidlist, HARDWARE_ID, LINE_LEN);
/**
*
* A HWID list is double-\0 terminated and \0 separated
*/
- str_lenth = strlen (hwidlist) + 1;
- strncpy (&hwidlist[str_lenth], secondary_hwid, LINE_LEN - str_lenth);
+ str_length = strlen (hwidlist) + 1;
+ strncpy (&hwidlist[str_length], secondary_hwid, LINE_LEN);
+ str_length += strlen (&hwidlist[str_length]) + 1;
/**
* Locate the inf-file, we need to store it somewhere where the system can
* TODO: How about win64 in the future?
* We need to use a different driver for amd64/i386 !
*/
- GetFullPathNameA (INF_FILE, MAX_PATH, inf_file_path, NULL);
+ GetFullPathNameA (INF_FILE, MAX_PATH, inf_file_path, &temp_inf_filename);
/**
* Bootstrap our device info using the drivers inf-file
class_name,
&class_guid,
NULL,
- NULL,
+ 0,
DICD_GENERATE_ID,
&DeviceNode))
return FALSE;
&DeviceNode,
SPDRP_HARDWAREID,
(LPBYTE) hwidlist,
- (strlen (hwidlist) + 2) * sizeof (char)))
+ str_length * sizeof (char)))
return FALSE;
/* Install our new class(=device) into the system */
&DeviceNode))
return FALSE;
+ /* This system call tends to take a while (several seconds!) on
+ "modern" Windoze systems */
+ if (!UpdateDriverForPlugAndPlayDevicesA (NULL,
+ secondary_hwid,
+ inf_file_path,
+ INSTALLFLAG_FORCE | INSTALLFLAG_NONINTERACTIVE,
+ NULL)) //reboot required? NEVER!
+ return FALSE;
+
return TRUE;
}
+
/**
* Remove our new virtual interface to use for tunneling.
* This function must be called AFTER setup_interface!
*
* @return: TRUE if destruction was successful, else FALSE
*/
-static boolean
+static BOOL
remove_interface ()
{
SP_REMOVEDEVICE_PARAMS remove;
return TRUE;
}
+
/**
* Do all the lookup necessary to retrieve the inteface's actual name
* off the registry.
*
* @return: TRUE if we were able to lookup the interface's name, else FALSE
*/
-static boolean
+static BOOL
resolve_interface_name ()
{
-
SP_DEVINFO_LIST_DETAIL_DATA device_details;
char pnp_instance_id [MAX_DEVICE_ID_LEN];
HKEY adapter_key_handle;
LONG status;
DWORD len;
int i = 0;
- boolean retval = FALSE;
+ int retrys;
+ BOOL retval = FALSE;
char adapter[] = INTERFACE_REGISTRY_LOCATION;
/* We can obtain the PNP instance ID from our setupapi handle */
NULL)) //hMachine, we are local
return FALSE;
- /* Now we can use this ID to locate the correct networks interface in registry */
- if (ERROR_SUCCESS != RegOpenKeyExA (
- HKEY_LOCAL_MACHINE,
- adapter,
- 0,
- KEY_READ,
- &adapter_key_handle))
- return FALSE;
-
- /* Of course there is a multitude of entries here, with arbitrary names,
- * thus we need to iterate through there.
- */
- while (!retval)
+ /* Registry is incredibly slow, retry for up to 30 seconds to allow registry to refresh */
+ for (retrys = 0; retrys < 120 && !retval; retrys++)
{
- char instance_key[256];
- char query_key [256];
- HKEY instance_key_handle;
- char pnpinstanceid_name[] = "PnpInstanceID";
- char pnpinstanceid_value[256];
- char adaptername_name[] = "Name";
- DWORD data_type;
-
- len = sizeof (adapter_key_handle);
- /* optain a subkey of {4D36E972-E325-11CE-BFC1-08002BE10318} */
- status = RegEnumKeyExA (
- adapter_key_handle,
- i,
- instance_key,
- &len,
- NULL,
- NULL,
- NULL,
- NULL);
-
- /* this may fail due to one of two reasons:
- * we are at the end of the list*/
- if (ERROR_NO_MORE_ITEMS == status)
- break;
- // * we found a broken registry key, continue with the next key.
- if (ERROR_SUCCESS != status)
- goto cleanup;
-
- /* prepare our new query string: */
- snprintf (query_key, 256, "%s\\%s\\Connection",
- INTERFACE_REGISTRY_LOCATION,
- instance_key);
-
- /* look inside instance_key\\Connection */
- status = RegOpenKeyExA (
- HKEY_LOCAL_MACHINE,
- query_key,
- 0,
- KEY_READ,
- &instance_key_handle);
-
- if (status != ERROR_SUCCESS)
- continue;
-
- /* now, read our PnpInstanceID */
- len = sizeof (pnpinstanceid_value);
- status = RegQueryValueExA (instance_key_handle,
- pnpinstanceid_name,
- NULL, //reserved, always NULL according to MSDN
- &data_type,
- (LPBYTE) pnpinstanceid_value,
- &len);
-
- if (status != ERROR_SUCCESS || data_type != REG_SZ)
- goto cleanup;
-
- /* compare the value we got to our devices PNPInstanceID*/
- if (0 != strncmp (pnpinstanceid_value, pnp_instance_id,
- sizeof (pnpinstanceid_value) / sizeof (char)))
- goto cleanup;
-
- len = sizeof (device_visible_name);
- status = RegQueryValueExA (
- instance_key_handle,
- adaptername_name,
- NULL, //reserved, always NULL according to MSDN
- &data_type,
- (LPBYTE) device_visible_name,
- &len);
-
- if (status != ERROR_SUCCESS || data_type != REG_SZ)
- goto cleanup;
+ /* sleep for 250ms*/
+ Sleep (250);
+
+ /* Now we can use this ID to locate the correct networks interface in registry */
+ if (ERROR_SUCCESS != RegOpenKeyExA (
+ HKEY_LOCAL_MACHINE,
+ adapter,
+ 0,
+ KEY_READ,
+ &adapter_key_handle))
+ return FALSE;
- /*
- * we have successfully found OUR instance,
- * save the device GUID before exiting
+ /* Of course there is a multitude of entries here, with arbitrary names,
+ * thus we need to iterate through there.
*/
-
- strncpy (device_guid, instance_key, 256);
- retval = TRUE;
+ while (!retval)
+ {
+ char instance_key[256];
+ char query_key [256];
+ HKEY instance_key_handle;
+ char pnpinstanceid_name[] = "PnpInstanceID";
+ char pnpinstanceid_value[256];
+ char adaptername_name[] = "Name";
+ DWORD data_type;
+
+ len = 256 * sizeof (char);
+ /* optain a subkey of {4D36E972-E325-11CE-BFC1-08002BE10318} */
+ status = RegEnumKeyExA (
+ adapter_key_handle,
+ i,
+ instance_key,
+ &len,
+ NULL,
+ NULL,
+ NULL,
+ NULL);
+
+ /* this may fail due to one of two reasons:
+ * we are at the end of the list*/
+ if (ERROR_NO_MORE_ITEMS == status)
+ break;
+ // * we found a broken registry key, continue with the next key.
+ if (ERROR_SUCCESS != status)
+ goto cleanup;
+
+ /* prepare our new query string: */
+ snprintf (query_key, 256, "%s\\%s\\Connection",
+ adapter,
+ instance_key);
+
+ /* look inside instance_key\\Connection */
+ status = RegOpenKeyExA (
+ HKEY_LOCAL_MACHINE,
+ query_key,
+ 0,
+ KEY_READ,
+ &instance_key_handle);
+
+ if (status != ERROR_SUCCESS)
+ goto cleanup;
+
+ /* now, read our PnpInstanceID */
+ len = sizeof (pnpinstanceid_value);
+ status = RegQueryValueExA (instance_key_handle,
+ pnpinstanceid_name,
+ NULL, //reserved, always NULL according to MSDN
+ &data_type,
+ (LPBYTE) pnpinstanceid_value,
+ &len);
+
+ if (status != ERROR_SUCCESS || data_type != REG_SZ)
+ goto cleanup;
+
+ /* compare the value we got to our devices PNPInstanceID*/
+ if (0 != strncmp (pnpinstanceid_value, pnp_instance_id,
+ sizeof (pnpinstanceid_value) / sizeof (char)))
+ goto cleanup;
+
+ len = sizeof (device_visible_name);
+ status = RegQueryValueExA (
+ instance_key_handle,
+ adaptername_name,
+ NULL, //reserved, always NULL according to MSDN
+ &data_type,
+ (LPBYTE) device_visible_name,
+ &len);
+
+ if (status != ERROR_SUCCESS || data_type != REG_SZ)
+ goto cleanup;
+
+ /*
+ * we have successfully found OUR instance,
+ * save the device GUID before exiting
+ */
+
+ strncpy (device_guid, instance_key, 256);
+ retval = TRUE;
+ fprintf (stderr, "DEBUG: Interface Name lookup succeeded on retry %d\n", retrys);
cleanup:
- RegCloseKey (instance_key_handle);
-
- ++i;
- }
+ RegCloseKey (instance_key_handle);
- RegCloseKey (adapter_key_handle);
+ ++i;
+ }
+ RegCloseKey (adapter_key_handle);
+ }
return retval;
}
-static boolean
+
+/**
+ * Determines the version of the installed TAP32 driver and checks if it's sufficiently new for GNUNET
+ *
+ * @param handle the handle to our tap device
+ * @return TRUE if the version is sufficient, else FALSE
+ */
+static BOOL
check_tapw32_version (HANDLE handle)
{
- {
- ULONG version[3];
- DWORD len;
- memset (&(version), 0, sizeof (version));
+ ULONG version[3];
+ DWORD len;
+ memset (&(version), 0, sizeof (version));
- if (DeviceIoControl (handle, TAP_WIN_IOCTL_GET_VERSION,
- &version, sizeof (version),
- &version, sizeof (version), &len, NULL))
- {
-#ifdef TESTING
- fprintf (stderr, "TAP-Windows Driver Version %d.%d %s",
- (int) version[0],
- (int) version[1],
- (version[2] ? "(DEBUG)" : ""));
-#endif
- }
+ if (DeviceIoControl (handle, TAP_WIN_IOCTL_GET_VERSION,
+ &version, sizeof (version),
+ &version, sizeof (version), &len, NULL))
+ fprintf (stderr, "INFO: TAP-Windows Driver Version %d.%d %s\n",
+ (int) version[0],
+ (int) version[1],
+ (version[2] ? "(DEBUG)" : ""));
- if (version[0] != TAP_WIN_MIN_MAJOR || version[1] < TAP_WIN_MIN_MINOR)
- {
- fprintf (stderr, "ERROR: This version of gnunet requires a TAP-Windows driver that is at least version %d.%d!\n",
- TAP_WIN_MIN_MAJOR,
- TAP_WIN_MIN_MINOR);
- return FALSE;
- }
- return TRUE;
- }
+ if (version[0] != TAP_WIN_MIN_MAJOR || version[1] < TAP_WIN_MIN_MINOR){
+ fprintf (stderr, "FATAL: This version of gnunet requires a TAP-Windows driver that is at least version %d.%d!\n",
+ TAP_WIN_MIN_MAJOR,
+ TAP_WIN_MIN_MINOR);
+ return FALSE;
+ }
+
+ return TRUE;
}
+
/**
* Creates a tun-interface called dev;
*
char device_path[256];
HANDLE handle;
- if (!setup_interface ())
+ if (! setup_interface ())
{
errno = ENODEV;
return INVALID_HANDLE_VALUE;
if (handle == INVALID_HANDLE_VALUE)
{
- fprintf (stderr, "CreateFile failed on TAP device: %s\n", device_path);
+ fprintf (stderr, "FATAL: CreateFile failed on TAP device: %s\n", device_path);
return handle;
}
return handle;
}
-static boolean
+
+/**
+ * Brings a TAP device up and sets it to connected state.
+ *
+ * @param handle the handle to our TAP device
+ * @return True if the operation succeeded, else false
+ */
+static BOOL
tun_up (HANDLE handle)
{
ULONG status = TRUE;
DWORD len;
- if (DeviceIoControl (handle, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
- &status, sizeof (status),
- &status, sizeof (status), &len, NULL))
+ if (!DeviceIoControl (handle, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
+ &status, sizeof (status),
+ &status, sizeof (status), &len, NULL))
{
- fprintf (stderr, "The TAP-Windows driver ignored our request to set the interface UP (TAP_WIN_IOCTL_SET_MEDIA_STATUS DeviceIoControl call)!\n");
+ fprintf (stderr, "FATAL: TAP driver ignored request to UP interface (DeviceIoControl call)!\n");
return FALSE;
}
/* Wait for the device to go UP, might take some time. */
- Sleep ((TAP32_POSTUP_WAITTIME)*1000);
+ Sleep (TAP32_POSTUP_WAITTIME * 1000);
return TRUE;
}
+
+/**
+ * Attempts to read off an input facility (tap or named pipe) in overlapped mode.
+ *
+ * 1.
+ * If the input facility is in IOSTATE_READY, it will issue a new read operation to the
+ * input handle. Then it goes into IOSTATE_QUEUED state.
+ * In case the read succeeded instantly the input facility enters 3.
+ *
+ * 2.
+ * If the input facility is in IOSTATE_QUEUED state, it will check if the queued read has finished already.
+ * If it has finished, go to state 3.
+ * If it has failed, set IOSTATE_FAILED
+ *
+ * 3.
+ * If the output facility is in state IOSTATE_READY, the read-buffer is copied to the output buffer.
+ * The input facility enters state IOSTATE_READY
+ * The output facility enters state IOSTATE_READY
+ * If the output facility is in state IOSTATE_QUEUED, the input facility enters IOSTATE_WAITING
+ *
+ * IOSTATE_WAITING is reset by the output facility, once it has completed.
+ *
+ * @param input_facility input named pipe or file to work with.
+ * @param output_facility output pipe or file to hand over data to.
+ * @return false if an event reset was impossible (OS error), else true
+ */
+static BOOL
+attempt_read_tap (struct io_facility * input_facility,
+ struct io_facility * output_facility)
+{
+ struct GNUNET_MessageHeader * hdr;
+ unsigned short size;
+ BOOL status;
+
+ switch (input_facility->facility_state)
+ {
+ case IOSTATE_READY:
+ {
+ if (!ResetEvent (input_facility->overlapped.hEvent))
+ {
+ return FALSE;
+ }
+
+ input_facility->buffer_size = 0;
+ status = ReadFile (input_facility->handle,
+ input_facility->buffer,
+ sizeof (input_facility->buffer) - sizeof (struct GNUNET_MessageHeader),
+ &input_facility->buffer_size,
+ &input_facility->overlapped);
+
+ /* Check how the task is handled */
+ if (status)
+ {/* async event processed immediately*/
+
+ /* reset event manually*/
+ if (!SetEvent (input_facility->overlapped.hEvent))
+ return FALSE;
+
+ /* we successfully read something from the TAP and now need to
+ * send it our via STDOUT. Is that possible at the moment? */
+ if ((IOSTATE_READY == output_facility->facility_state ||
+ IOSTATE_WAITING == output_facility->facility_state)
+ && 0 < input_facility->buffer_size)
+ { /* hand over this buffers content and apply message header for gnunet */
+ hdr = (struct GNUNET_MessageHeader *) output_facility->buffer;
+ size = input_facility->buffer_size + sizeof (struct GNUNET_MessageHeader);
+
+ memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader),
+ input_facility->buffer,
+ input_facility->buffer_size);
+
+ output_facility->buffer_size = size;
+ hdr->size = htons (size);
+ hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
+ output_facility->facility_state = IOSTATE_READY;
+ }
+ else if (0 < input_facility->buffer_size)
+ { /* If we have have read our buffer, wait for our write-partner*/
+ input_facility->facility_state = IOSTATE_WAITING;
+ // TODO: shall we attempt to fill our buffer or should we wait for our write-partner to finish?
+ }
+ }
+ else /* operation was either queued or failed*/
+ {
+ int err = GetLastError ();
+ if (ERROR_IO_PENDING == err)
+ { /* operation queued */
+ input_facility->facility_state = IOSTATE_QUEUED;
+ }
+ else
+ { /* error occurred, let the rest of the elements finish */
+ input_facility->path_open = FALSE;
+ input_facility->facility_state = IOSTATE_FAILED;
+ if (IOSTATE_WAITING == output_facility->facility_state)
+ output_facility->path_open = FALSE;
+
+ fprintf (stderr, "FATAL: Read from handle failed, allowing write to finish!\n");
+ }
+ }
+ }
+ return TRUE;
+ // We are queued and should check if the read has finished
+ case IOSTATE_QUEUED:
+ {
+ // there was an operation going on already, check if that has completed now.
+ status = GetOverlappedResult (input_facility->handle,
+ &input_facility->overlapped,
+ &input_facility->buffer_size,
+ FALSE);
+ if (status)
+ {/* successful return for a queued operation */
+ if (!ResetEvent (input_facility->overlapped.hEvent))
+ return FALSE;
+
+ /* we successfully read something from the TAP and now need to
+ * send it our via STDOUT. Is that possible at the moment? */
+ if ((IOSTATE_READY == output_facility->facility_state ||
+ IOSTATE_WAITING == output_facility->facility_state)
+ && 0 < input_facility->buffer_size)
+ { /* hand over this buffers content and apply message header for gnunet */
+ hdr = (struct GNUNET_MessageHeader *) output_facility->buffer;
+ size = input_facility->buffer_size + sizeof (struct GNUNET_MessageHeader);
+
+ memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader),
+ input_facility->buffer,
+ input_facility->buffer_size);
+
+ output_facility->buffer_size = size;
+ hdr->size = htons(size);
+ hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
+ output_facility->facility_state = IOSTATE_READY;
+ input_facility->facility_state = IOSTATE_READY;
+ }
+ else if (0 < input_facility->buffer_size)
+ { /* If we have have read our buffer, wait for our write-partner*/
+ input_facility->facility_state = IOSTATE_WAITING;
+ // TODO: shall we attempt to fill our buffer or should we wait for our write-partner to finish?
+ }
+ }
+ else
+ { /* operation still pending/queued or failed? */
+ int err = GetLastError ();
+ if (ERROR_IO_INCOMPLETE != err && ERROR_IO_PENDING != err)
+ { /* error occurred, let the rest of the elements finish */
+ input_facility->path_open = FALSE;
+ input_facility->facility_state = IOSTATE_FAILED;
+ if (IOSTATE_WAITING == output_facility->facility_state)
+ output_facility->path_open = FALSE;
+ fprintf (stderr, "FATAL: Read from handle failed, allowing write to finish!\n");
+ }
+ }
+ }
+ return TRUE;
+ case IOSTATE_RESUME:
+ hdr = (struct GNUNET_MessageHeader *) output_facility->buffer;
+ size = input_facility->buffer_size + sizeof (struct GNUNET_MessageHeader);
+
+ memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader),
+ input_facility->buffer,
+ input_facility->buffer_size);
+
+ output_facility->buffer_size = size;
+ hdr->size = htons (size);
+ hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
+ output_facility->facility_state = IOSTATE_READY;
+ input_facility->facility_state = IOSTATE_READY;
+ return TRUE;
+ default:
+ return TRUE;
+ }
+}
+
+/**
+ * Attempts to read off an input facility (tap or named pipe) in overlapped mode.
+ *
+ * 1.
+ * If the input facility is in IOSTATE_READY, it will issue a new read operation to the
+ * input handle. Then it goes into IOSTATE_QUEUED state.
+ * In case the read succeeded instantly the input facility enters 3.
+ *
+ * 2.
+ * If the input facility is in IOSTATE_QUEUED state, it will check if the queued read has finished already.
+ * If it has finished, go to state 3.
+ * If it has failed, set IOSTATE_FAILED
+ *
+ * 3.
+ * If the facility is finished with ready
+ * The read-buffer is copied to the output buffer, except for the GNUNET_MessageHeader.
+ * The input facility enters state IOSTATE_READY
+ * The output facility enters state IOSTATE_READY
+ * If the output facility is in state IOSTATE_QUEUED, the input facility enters IOSTATE_WAITING
+ *
+ * IOSTATE_WAITING is reset by the output facility, once it has completed.
+ *
+ * @param input_facility input named pipe or file to work with.
+ * @param output_facility output pipe or file to hand over data to.
+ * @return false if an event reset was impossible (OS error), else true
+ */
+static BOOL
+attempt_read_stdin (struct io_facility * input_facility,
+ struct io_facility * output_facility)
+{
+ struct GNUNET_MessageHeader * hdr;
+ BOOL status;
+ switch (input_facility->facility_state)
+ {
+ case IOSTATE_READY:
+ {
+ if (!ResetEvent (input_facility->overlapped.hEvent))
+ return FALSE;
+ input_facility->buffer_size = 0;
+ status = ReadFile (input_facility->handle,
+ input_facility->buffer,
+ sizeof (input_facility->buffer),
+ &input_facility->buffer_size,
+ &input_facility->overlapped);
+
+ /* Check how the task is handled */
+ if (status && (sizeof (struct GNUNET_MessageHeader) < input_facility->buffer_size))
+ {/* async event processed immediately*/
+ hdr = (struct GNUNET_MessageHeader *) input_facility->buffer;
+
+ /* reset event manually*/
+ if (!SetEvent (input_facility->overlapped.hEvent))
+ return FALSE;
+
+ if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER ||
+ ntohs (hdr->size) > sizeof (input_facility->buffer))
+ {
+ fprintf (stderr, "WARNING: Protocol violation, got GNUnet Message type %h, size %h!\n", ntohs (hdr->type), ntohs (hdr->size));
+ input_facility->facility_state = IOSTATE_READY;
+ return TRUE;
+ }
+ if (ntohs (hdr->size) > input_facility->buffer_size);
+ // TODO: add support for partial read
+
+ /* we successfully read something from the TAP and now need to
+ * send it our via STDOUT. Is that possible at the moment? */
+ if (sizeof (struct GNUNET_MessageHeader) < input_facility->buffer_size)
+ {
+ if (IOSTATE_READY == output_facility->facility_state ||
+ IOSTATE_WAITING == output_facility->facility_state)
+ {
+ /* hand over this buffers content and strip gnunet message header */
+ memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader),
+ input_facility->buffer,
+ input_facility->buffer_size - sizeof (struct GNUNET_MessageHeader));
+ output_facility->buffer_size = input_facility->buffer_size - sizeof (struct GNUNET_MessageHeader);
+ output_facility->facility_state = IOSTATE_READY;
+
+ }
+ else if (IOSTATE_QUEUED == output_facility->facility_state)
+ /* If we have have read our buffer, wait for our write-partner*/
+ input_facility->facility_state = IOSTATE_WAITING;
+ // TODO: shall we attempt to fill our buffer or should we wait for our write-partner to finish?
+ }
+ }
+ else if (status && 0 >= input_facility->buffer_size)
+ {
+ if (!SetEvent (input_facility->overlapped.hEvent))
+ return FALSE;
+
+ input_facility->facility_state = IOSTATE_READY;
+ }
+ else /* operation was either queued or failed*/
+ {
+ int err = GetLastError ();
+ if (ERROR_IO_PENDING == err)
+ { /* operation queued */
+ input_facility->facility_state = IOSTATE_QUEUED;
+ }
+ else
+ { /* error occurred, let the rest of the elements finish */
+ input_facility->path_open = FALSE;
+ input_facility->facility_state = IOSTATE_FAILED;
+ if (IOSTATE_WAITING == output_facility->facility_state)
+ output_facility->path_open = FALSE;
+
+ fprintf (stderr, "FATAL: Read from handle failed, allowing write to finish!\n");
+ }
+ }
+ }
+ return TRUE;
+ // We are queued and should check if the read has finished
+ case IOSTATE_QUEUED:
+ {
+ // there was an operation going on already, check if that has completed now.
+ status = GetOverlappedResult (input_facility->handle,
+ &input_facility->overlapped,
+ &input_facility->buffer_size,
+ FALSE);
+ if (status)
+ {/* successful return for a queued operation */
+ hdr = (struct GNUNET_MessageHeader *) input_facility->buffer;
+
+ if (!ResetEvent (input_facility->overlapped.hEvent))
+ return FALSE;
+
+ if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER ||
+ ntohs (hdr->size) > sizeof (input_facility->buffer))
+ {
+ fprintf (stderr, "WARNING: Protocol violation, got GNUnet Message type %h, size %h!\n", ntohs (hdr->type), ntohs (hdr->size));
+ input_facility->facility_state = IOSTATE_READY;
+ return TRUE;
+ }
+ if (ntohs (hdr->size) > input_facility->buffer_size );
+ // TODO: add support for partial read
+
+ /* we successfully read something from the TAP and now need to
+ * send it our via STDOUT. Is that possible at the moment? */
+ if ((IOSTATE_READY == output_facility->facility_state ||
+ IOSTATE_WAITING == output_facility->facility_state)
+ && sizeof(struct GNUNET_MessageHeader) < input_facility->buffer_size)
+ { /* hand over this buffers content and strip gnunet message header */
+ memcpy (output_facility->buffer + sizeof(struct GNUNET_MessageHeader),
+ input_facility->buffer,
+ input_facility->buffer_size - sizeof(struct GNUNET_MessageHeader));
+ output_facility->buffer_size = input_facility->buffer_size - sizeof(struct GNUNET_MessageHeader);
+ output_facility->facility_state = IOSTATE_READY;
+ input_facility->facility_state = IOSTATE_READY;
+ }
+ else if (sizeof(struct GNUNET_MessageHeader) < input_facility->buffer_size)
+ { /* If we have have read our buffer, wait for our write-partner*/
+ input_facility->facility_state = IOSTATE_WAITING;
+ // TODO: shall we attempt to fill our buffer or should we wait for our write-partner to finish?
+ }
+ else if (sizeof(struct GNUNET_MessageHeader) >= input_facility->buffer_size)
+ input_facility->facility_state = IOSTATE_READY;
+ }
+ else
+ { /* operation still pending/queued or failed? */
+ int err = GetLastError ();
+ if (ERROR_IO_INCOMPLETE != err && ERROR_IO_PENDING != err)
+ { /* error occurred, let the rest of the elements finish */
+ input_facility->path_open = FALSE;
+ input_facility->facility_state = IOSTATE_FAILED;
+ if (IOSTATE_WAITING == output_facility->facility_state)
+ output_facility->path_open = FALSE;
+ fprintf (stderr, "FATAL: Read from handle failed, allowing write to finish!\n");
+ }
+ }
+ }
+ return TRUE;
+ case IOSTATE_RESUME: /* Our buffer was filled already but our write facility was busy. */
+ memcpy (output_facility->buffer + sizeof (struct GNUNET_MessageHeader),
+ input_facility->buffer,
+ input_facility->buffer_size - sizeof (struct GNUNET_MessageHeader));
+ output_facility->buffer_size = input_facility->buffer_size - sizeof (struct GNUNET_MessageHeader);
+ output_facility->facility_state = IOSTATE_READY;
+ input_facility->facility_state = IOSTATE_READY;
+ return TRUE;
+ default:
+ return TRUE;
+ }
+}
+
+/**
+ * Attempts to write to an output facility (tap or named pipe) in overlapped mode.
+ *
+ * TODO: high level description
+ *
+ * @param output_facility output pipe or file to hand over data to.
+ * @param input_facility input named pipe or file to work with.
+ * @return false if an event reset was impossible (OS error), else true
+ */
+static BOOL
+attempt_write (struct io_facility * output_facility,
+ struct io_facility * input_facility)
+{
+ BOOL status;
+
+ switch (output_facility->facility_state)
+ {
+ case IOSTATE_READY:
+
+ if (!ResetEvent (output_facility->overlapped.hEvent))
+ return FALSE;
+
+ output_facility->buffer_size_written = 0;
+ status = WriteFile (output_facility->handle,
+ output_facility->buffer,
+ output_facility->buffer_size,
+ &output_facility->buffer_size_written,
+ &output_facility->overlapped);
+
+ /* Check how the task was handled */
+ if (status &&
+ output_facility->buffer_size_written == output_facility->buffer_size)
+ {/* async event processed immediately*/
+
+ /* reset event manually*/
+ if (!SetEvent (output_facility->overlapped.hEvent))
+ return FALSE;
+
+ /* we are now waiting for our buffer to be filled*/
+ output_facility->facility_state = IOSTATE_WAITING;
+ output_facility->buffer_size = 0;
+ output_facility->buffer_size_written = 0;
+
+ /* we successfully wrote something and now need to reset our reader */
+ if (IOSTATE_WAITING == input_facility->facility_state)
+ input_facility->facility_state = IOSTATE_RESUME;
+ else if (IOSTATE_FAILED == input_facility->facility_state)
+ output_facility->path_open = FALSE;
+ }
+ else /* operation was either queued or failed*/
+ {
+ int err = GetLastError ();
+ if (ERROR_IO_PENDING == err)
+ { /* operation queued */
+ output_facility->facility_state = IOSTATE_QUEUED;
+ }
+ else
+ { /* error occurred, close this path */
+ output_facility->path_open = FALSE;
+ output_facility->facility_state = IOSTATE_FAILED;
+ fprintf (stderr, "FATAL: Write to handle failed, exiting!\n");
+ }
+ }
+ return TRUE;
+ case IOSTATE_QUEUED:
+ // there was an operation going on already, check if that has completed now.
+ status = GetOverlappedResult (output_facility->handle,
+ &output_facility->overlapped,
+ &output_facility->buffer_size_written,
+ FALSE);
+ if (status &&
+ output_facility->buffer_size_written == output_facility->buffer_size)
+ {/* successful return for a queued operation */
+ if (!ResetEvent (output_facility->overlapped.hEvent))
+ return FALSE;
+
+ /* we are now waiting for our buffer to be filled*/
+ output_facility->facility_state = IOSTATE_WAITING;
+ output_facility->buffer_size = 0;
+ output_facility->buffer_size_written = 0;
+
+ /* we successfully wrote something and now need to reset our reader */
+ if (IOSTATE_WAITING == input_facility->facility_state)
+ input_facility->facility_state = IOSTATE_RESUME;
+ else if (IOSTATE_FAILED == input_facility->facility_state)
+ output_facility->path_open = FALSE;
+ }
+ else
+ { /* operation still pending/queued or failed? */
+ int err = GetLastError ();
+ if (ERROR_IO_INCOMPLETE != err && ERROR_IO_PENDING != err)
+ { /* error occurred, close this path */
+ output_facility->path_open = FALSE;
+ output_facility->facility_state = IOSTATE_FAILED;
+ fprintf (stderr, "FATAL: Write to handle failed, exiting!\n");
+ }
+ }
+ default:
+ return TRUE;
+ }
+}
+
+
/**
* Initialize a overlapped structure
*
* @param signaled if the hEvent created should default to signaled or not
* @return true on success, else false
*/
-static boolean
-initialize_overlapped_facility (struct overlapped_facility * elem,
- BOOL initial_state,
- BOOL signaled)
+static BOOL
+initialize_io_facility (struct io_facility * elem,
+ int initial_state,
+ BOOL signaled)
{
-
- elem->status = initial_state;
- elem->iostate = 0;
- elem->buffer_size = MAX_SIZE;
+ elem->path_open = TRUE;
+ elem->handle = INVALID_HANDLE_VALUE;
+ elem->facility_state = initial_state;
+ elem->buffer_size = 0;
elem->overlapped.hEvent = CreateEvent (NULL, TRUE, signaled, NULL);
if (NULL == elem->overlapped.hEvent)
return FALSE;
return TRUE;
}
+
/**
* Start forwarding to and from the tunnel.
*
* @param fd_tun tunnel FD
*/
static void
-run (HANDLE handle)
+run (HANDLE tap_handle)
{
- /* read refers to reading from fd_tun, writing to stdout */
- int read_open = 1;
- /* write refers to reading from stdin, writing to fd_tun */
- int write_open = 1;
-
/* IO-Facility for reading from our virtual interface */
- struct overlapped_facility tap_read;
+ struct io_facility tap_read;
/* IO-Facility for writing to our virtual interface */
- struct overlapped_facility tap_write;
+ struct io_facility tap_write;
/* IO-Facility for reading from stdin */
- struct overlapped_facility std_in;
+ struct io_facility std_in;
/* IO-Facility for writing to stdout */
- struct overlapped_facility std_out;
+ struct io_facility std_out;
+
+ HANDLE parent_std_in_handle = GetStdHandle (STD_INPUT_HANDLE);
+ HANDLE parent_std_out_handle = GetStdHandle (STD_OUTPUT_HANDLE);
/* tun up: */
/* we do this HERE and not beforehand (in init_tun()), in contrast to openvpn
* DHCP and such are all features we will never use in gnunet afaik.
* But for openvpn those are essential.
*/
- if (!tun_up (handle))
- goto teardown;
-
- if (!)
- goto teardown;
+ if (!tun_up (tap_handle))
+ return;
/* Initialize our overlapped IO structures*/
- if (initialize_overlapped_facility (&tap_read, TRUE, FALSE)
- && initialize_overlapped_facility (&tap_write, FALSE, TRUE)
- && initialize_overlapped_facility (&std_in, TRUE, FALSE)
- && initialize_overlapped_facility (&std_out, FALSE, TRUE))
- goto teardown;
-
-
- //openvpn
- // Set Device to Subnet-Mode?
- // do we really need tun.c:2925 ?
- // Why does openvpn assign IPv4's there??? Foobar??
-
- // Setup should be complete here.
- // If something is missing, check init.c:3400+
+ if (!(initialize_io_facility (&tap_read, IOSTATE_READY, FALSE)
+ && initialize_io_facility (&tap_write, IOSTATE_WAITING, TRUE)
+ && initialize_io_facility (&std_in, IOSTATE_READY, FALSE)
+ && initialize_io_facility (&std_out, IOSTATE_WAITING, TRUE)))
+ goto teardown_final;
+
+ /* Handles for STDIN and STDOUT */
+ tap_read.handle = tap_handle;
+ tap_write.handle = tap_handle;
+
+#ifdef DEBUG_TO_CONSOLE
+ /* Debug output to console STDIN/STDOUT*/
+ std_in.handle = parent_std_in_handle;
+ std_out.handle = parent_std_out_handle;
+#else
+
+ /*
+ * Find out the types of our handles.
+ * This part is a problem, because in windows we need to handle files,
+ * pipes and the console differently.
+ */
+ if (FILE_TYPE_PIPE != GetFileType (parent_std_in_handle) ||
+ FILE_TYPE_PIPE != GetFileType (parent_std_out_handle))
+ {
+ fprintf (stderr, "ERROR: stdin/stdout must be named pipes!\n");
+ goto teardown;
+ }
- // mainloop:
- // tunnel_point_to_point
- // openvpn.c:62
+ std_in.handle = ReOpenFile (parent_std_in_handle,
+ GENERIC_READ,
+ FILE_SHARE_WRITE | FILE_SHARE_READ,
+ FILE_FLAG_OVERLAPPED);
- while ((1 == read_open) || (1 == write_open))
+ if (INVALID_HANDLE_VALUE == std_in.handle)
{
+ fprintf (stderr, "FATAL: Could not reopen stdin for in overlapped mode, has to be a named pipe!\n");
+ goto teardown;
+ }
- /* READ from stdin is possible */
- if (std_in.status)
- {
+ std_out.handle = ReOpenFile (parent_std_out_handle,
+ GENERIC_WRITE,
+ FILE_SHARE_READ,
+ FILE_FLAG_OVERLAPPED);
- }
- /* READ from tap is possible */
- if (tap_read.status)
- {
+ if (INVALID_HANDLE_VALUE == std_out.handle)
+ {
+ fprintf (stderr, "FATAL: Could not reopen stdout for in overlapped mode, has to be a named pipe!\n");
+ goto teardown;
+ }
+#endif
+
+ while (std_out.path_open || tap_write.path_open)
+ {
+ /* perform READ from stdin if possible */
+ if (std_in.path_open && (!attempt_read_stdin (&std_in, &tap_write)))
+ break;
- }
- /* WRITE to tap is possible */
- if (tap_write.status)
- {
+ /* perform READ from tap if possible */
+ if (tap_read.path_open && (!attempt_read_tap (&tap_read, &std_out)))
+ break;
- }
- /* WRITE to STDOUT is possible */
- if (std_out.status)
- {
+ /* perform WRITE to tap if possible */
+ if (tap_write.path_open && (!attempt_write (&tap_write, &std_in)))
+ break;
- }
+ /* perform WRITE to STDOUT if possible */
+ if (std_out.path_open && (!attempt_write (&std_out, &tap_read)))
+ break;
}
+
teardown:
- ;
- //init.c:3472
+
+ CancelIo (tap_handle);
+ CancelIo (std_in.handle);
+ CancelIo (std_out.handle);
+
+teardown_final:
+
+ CloseHandle (tap_handle);
}
+
/**
* Open VPN tunnel interface.
*
* @param argc must be 6
* @param argv 0: binary name (gnunet-helper-vpn)
- * 1: tunnel interface name (gnunet-vpn)
+ * 1: tunnel interface prefix (gnunet-vpn)
* 2: IPv6 address (::1), "-" to disable
* 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
* 4: IPv4 address (1.2.3.4), "-" to disable
{
char hwid[LINE_LEN];
HANDLE handle;
- int global_ret;
+ int global_ret = 0;
+ BOOL have_ip4 = FALSE;
+ BOOL have_ip6 = FALSE;
if (6 != argc)
{
- fprintf (stderr, "Fatal: must supply 5 arguments!\n");
+ fprintf (stderr, "FATAL: must supply 5 arguments!\nUsage:\ngnunet-helper-vpn <if name prefix> <address6 or \"-\"> <netbits6> <address4 or \"-\"> <netmask4>\n", argv[0]);
return 1;
}
if (INVALID_HANDLE_VALUE == (handle = init_tun ()))
{
- fprintf (stderr, "Fatal: could not initialize virtual-interface %s with IPv6 %s/%s and IPv4 %s/%s\n",
+ fprintf (stderr, "FATAL: could not initialize virtual-interface %s with IPv6 %s/%s and IPv4 %s/%s\n",
hwid,
argv[2],
argv[3],
argv[4],
argv[5]);
- return 1;
+ global_ret = -1;
+ goto cleanup;
}
if (0 != strcmp (argv[2], "-"))
if ((prefix_len < 1) || (prefix_len > 127))
{
- fprintf (stderr, "Fatal: prefix_len out of range\n");
+ fprintf (stderr, "FATAL: prefix_len out of range\n");
global_ret = -1;
goto cleanup;
}
- set_address6 (address, prefix_len);
+ if (0 != (global_ret = set_address6 (address, prefix_len)))
+ goto cleanup;
+
+ have_ip6 = TRUE;
}
if (0 != strcmp (argv[4], "-"))
const char *address = argv[4];
const char *mask = argv[5];
- set_address4 (address, mask);
- }
+ if (0 != (global_ret = set_address4 (address, mask)))
+ goto cleanup;
- //eventuell:
- // tap_allow_nonadmin_access
- //tun.c:2023
+ have_ip4 = TRUE;
+ }
run (handle);
global_ret = 0;
cleanup:
- remove_interface ();
+ if (have_ip4)
+ {
+ const char *address = argv[4];
+ remove_address4 (address);
+ }
+ if (have_ip6)
+ {
+ const char *address = argv[2];
+ remove_address6 (address);
+ }
+ remove_interface ();
return global_ret;
}