Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
-
/**
* @file vpn/gnunet-helper-vpn-windows.c
* @brief the helper for the VPN service in win32 builds.
*/
#include <stdio.h>
+#include <Winsock2.h>
#include <windows.h>
#include <setupapi.h>
+#ifndef __MINGW64_VERSION_MAJOR
#include <ddk/cfgmgr32.h>
#include <ddk/newdev.h>
-#include <Winsock2.h>
+#else
+#include <cfgmgr32.h>
+#include <newdev.h>
+#endif
#include <time.h>
#include "platform.h"
#include "tap-windows.h"
#define LOG_DEBUG(msg) do {} while (0)
#endif
+/**
+ * Will this binary be run in permissions testing mode?
+ */
+static boolean privilege_testing = FALSE;
/**
* Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
* Name or Path+Name of our win32 driver.
* The .sys and .cat files HAVE to be in the same location as this file!
*/
-#define INF_FILE "share/gnunet/tapw32/OemWin2k.inf"
+#define INF_FILE "share/gnunet/openvpn-tap32/tapw32/OemWin2k.inf"
/**
* Name or Path+Name of our win64 driver.
* The .sys and .cat files HAVE to be in the same location as this file!
*/
-#define INF_FILE64 "share/gnunet/tapw64/OemWin2k.inf"
+#define INF_FILE64 "share/gnunet/openvpn-tap32/tapw64/OemWin2k.inf"
/**
* Hardware ID used in the inf-file.
DWORD buffer_size;
/**
- * Amount of data written, is compared to buffer_size.
+ * Amount of data actually written or read by readfile/writefile.
+ */
+ DWORD buffer_size_processed;
+
+ /**
+ * How much of this buffer we have writte in total
*/
DWORD buffer_size_written;
};
/**
* @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)
/**
* @brief Sets the IPv4-Address given in address on the interface dev
*
- * @param dev the interface to configure
* @param address the IPv4-Address
* @param mask the netmask
*/
strerror (errno));
return -1;
}
- // Set Device to Subnet-Mode?
- // do we really need tun.c:2925 ?
+ // Set Device to Subnet-Mode? do we really need openvpn/tun.c:2925 ?
/*
* prepare the command
/**
* @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)
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*/
+ /* 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*/
{
struct io_facility * output_facility)
{
struct GNUNET_MessageHeader * hdr;
- BOOL status;
+
switch (input_facility->facility_state)
{
case IOSTATE_READY:
{
+ input_facility->buffer_size = 0;
+
+partial_read_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))
+ if (ReadFile (input_facility->handle,
+ input_facility->buffer + input_facility->buffer_size,
+ sizeof (input_facility->buffer) - input_facility->buffer_size,
+ &input_facility->buffer_size_processed,
+ &input_facility->overlapped))
{/* async event processed immediately*/
hdr = (struct GNUNET_MessageHeader *) input_facility->buffer;
/* reset event manually*/
- if (! SetEvent (input_facility->overlapped.hEvent))
+ if (!SetEvent (input_facility->overlapped.hEvent))
return FALSE;
fprintf (stderr, "DEBUG: stdin read succeeded immediately\n");
-
+ input_facility->buffer_size += input_facility->buffer_size_processed;
+
if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER ||
ntohs (hdr->size) > sizeof (input_facility->buffer))
{
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
+ /* we got the a part of a packet */
+ if (ntohs (hdr->size) > input_facility->buffer_size)
+ goto partial_read_iostate_ready;
+
+ /* have we read more than 0 bytes of payload? (sizeread > header)*/
+ if (input_facility->buffer_size > sizeof (struct GNUNET_MessageHeader) &&
+ ((IOSTATE_READY == output_facility->facility_state) ||
+ (IOSTATE_WAITING == output_facility->facility_state)))
+ {/* 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,
- input_facility->buffer + sizeof (struct GNUNET_MessageHeader),
- 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;
- }
+ /* hand over this buffers content and strip gnunet message header */
+ memcpy (output_facility->buffer,
+ input_facility->buffer + sizeof (struct GNUNET_MessageHeader),
+ 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 (input_facility->buffer_size > sizeof (struct GNUNET_MessageHeader))
+ /* If we have have read our buffer, wait for our write-partner*/
+ input_facility->facility_state = IOSTATE_WAITING;
+ else /* we read nothing */
+ input_facility->facility_state = IOSTATE_READY;
+ }
else /* operation was either queued or failed*/
{
int err = GetLastError ();
- if (ERROR_IO_PENDING == err)
- { /* operation queued */
+ 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;
// there was an operation going on already, check if that has completed now.
if (GetOverlappedResult (input_facility->handle,
&input_facility->overlapped,
- &input_facility->buffer_size,
+ &input_facility->buffer_size_processed,
FALSE))
{/* successful return for a queued operation */
hdr = (struct GNUNET_MessageHeader *) input_facility->buffer;
return FALSE;
fprintf (stderr, "DEBUG: stdin read succeeded delayed\n");
+ input_facility->buffer_size += input_facility->buffer_size_processed;
if ((ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) ||
(ntohs (hdr->size) > sizeof (input_facility->buffer)))
input_facility->facility_state = IOSTATE_READY;
return TRUE;
}
+ /* we got the a part of a packet */
if (ntohs (hdr->size) > input_facility->buffer_size );
- // TODO: add support for partial read
+ goto partial_read_iostate_ready;
/* 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)
+ && input_facility->buffer_size > sizeof(struct GNUNET_MessageHeader))
{ /* hand over this buffers content and strip gnunet message header */
memcpy (output_facility->buffer,
input_facility->buffer + 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)
+ else if (input_facility->buffer_size > sizeof(struct GNUNET_MessageHeader))
+ input_facility->facility_state = IOSTATE_WAITING;
+ else
input_facility->facility_state = IOSTATE_READY;
}
else
attempt_write (struct io_facility * output_facility,
struct io_facility * input_facility)
{
- BOOL status;
-
switch (output_facility->facility_state)
{
case IOSTATE_READY:
-
+ output_facility->buffer_size_written = 0;
+
+continue_partial_write:
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)
+ if (WriteFile (output_facility->handle,
+ output_facility->buffer + output_facility->buffer_size_written,
+ output_facility->buffer_size - output_facility->buffer_size_written,
+ &output_facility->buffer_size_processed,
+ &output_facility->overlapped))
{/* async event processed immediately*/
fprintf (stderr, "DEBUG: write succeeded immediately\n");
+ output_facility->buffer_size_written += output_facility->buffer_size_processed;
/* reset event manually*/
if (! SetEvent (output_facility->overlapped.hEvent))
return FALSE;
+ /* partial write */
+ if (output_facility->buffer_size_written < output_facility->buffer_size)
+ goto continue_partial_write;
+
/* 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)
return TRUE;
case IOSTATE_QUEUED:
// there was an operation going on already, check if that has completed now.
- status = GetOverlappedResult (output_facility->handle,
+
+ if (GetOverlappedResult (output_facility->handle,
&output_facility->overlapped,
- &output_facility->buffer_size_written,
- FALSE);
- if (status &&
- output_facility->buffer_size_written == output_facility->buffer_size)
+ &output_facility->buffer_size_processed,
+ FALSE))
{/* successful return for a queued operation */
if (! ResetEvent (output_facility->overlapped.hEvent))
return FALSE;
fprintf (stderr, "DEBUG: write succeeded delayed\n");
-
+ output_facility->buffer_size_written += output_facility->buffer_size_processed;
+
+ /* partial write */
+ if (output_facility->buffer_size_written < output_facility->buffer_size)
+ goto continue_partial_write;
+
/* 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)
/**
* Start forwarding to and from the tunnel.
*
- * @param fd_tun tunnel FD
+ * @param tap_handle device handle for interacting with the Virtual interface
*/
static void
run (HANDLE tap_handle)
* DHCP and such are all features we will never use in gnunet afaik.
* But for openvpn those are essential.
*/
- if (! tun_up (tap_handle))
- return;
+ if ((privilege_testing) || (! tun_up (tap_handle)))
+ goto teardown_final;
/* Initialize our overlapped IO structures*/
if (! (initialize_io_facility (&tap_read, IOSTATE_READY, FALSE)
goto teardown;
}
#endif
-
+
fprintf (stderr, "DEBUG: mainloop has begun\n");
-
+
while (std_out.path_open || tap_write.path_open)
{
/* perform READ from stdin if possible */
break;
}
-teardown:
-
fprintf (stderr, "DEBUG: teardown initiated\n");
-
+teardown:
CancelIo (tap_handle);
CancelIo (std_in.handle);
CancelIo (std_out.handle);
-
teardown_final:
-
CloseHandle (tap_handle);
}
*
* @param argc must be 6
* @param argv 0: binary name (gnunet-helper-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
- * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
+ * [1: dryrun/testrun (does not execute mainloop)]
+ * 2: tunnel interface prefix (gnunet-vpn)
+ * 3: IPv6 address (::1), "-" to disable
+ * 4: IPv6 netmask length in bits (64), ignored if #2 is "-"
+ * 5: IPv4 address (1.2.3.4), "-" to disable
+ * 6: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
*/
int
main (int argc, char **argv)
int global_ret = 0;
BOOL have_ip4 = FALSE;
BOOL have_ip6 = FALSE;
-
+
+ if (argc > 1 && 0 != strcmp (argv[1], "-d")){
+ privilege_testing = TRUE;
+ fprintf (stderr, "DEBUG: Running binary in privilege testing mode.", argv[0]);
+ argv++;
+ argc--;
+ }
+
if (6 != argc)
{
- fprintf (stderr, "FATAL: must supply 5 arguments\nUsage:\ngnunet-helper-vpn <if name prefix> <address6 or \"-\"> <netbits6> <address4 or \"-\"> <netmask4>\n", argv[0]);
+ fprintf (stderr, "FATAL: must supply 5 arguments\nUsage:\ngnunet-helper-vpn [-d] <if name prefix> <address6 or \"-\"> <netbits6> <address4 or \"-\"> <netmask4>\n", argv[0]);
return 1;
}
}
run (handle);
- global_ret = 0;
cleanup:
if (have_ip4)