does not terminate on invalid uri
[oweals/gnunet.git] / src / vpn / gnunet-helper-vpn-windows.c
index f3c61a6e0ead6e4c479c74ec4544d447856fdf11..b810dc706ccdc90b7594c60f7e2deea788355983 100644 (file)
  * Should we print (interesting|debug) messages that can happen during
  * normal operation?
  */
-//#define DEBUG GNUNET_YES
+#define DEBUG GNUNET_NO
+
+#if DEBUG
+/* FIXME: define with varargs... */
+#define LOG_DEBUG(msg) fprintf (stderr, "%s", msg);
+#else
+#define LOG_DEBUG(msg) do {} while (0)
+#endif
+
 
 /**
  * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
 #define MAX_SIZE 65536
 
 /**
- * Name or Path+Name of our driver in Unicode.
+ * 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"
 
+/**
+ * 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"
+
 /**
  * Hardware ID used in the inf-file. 
  * This might change over time, as openvpn advances their driver
@@ -204,7 +218,12 @@ struct io_facility
   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;
 };
@@ -214,7 +233,34 @@ struct io_facility
  */
 WINBASEAPI HANDLE WINAPI ReOpenFile (HANDLE, DWORD, DWORD, DWORD);
 
+/**
+ * IsWow64Process definition for our is_win64, as this is a kernel function
+ */
+typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
 
+/**
+ * Determines if the host OS is win32 or win64
+ * 
+ * @return true if 
+ */
+BOOL
+is_win64 ()
+{
+#if defined(_WIN64)
+  //this is a win64 binary, 
+  return TRUE; 
+#elif defined(_WIN32)
+  //this is a 32bit binary, and we need to check if we are running in WOW64
+  BOOL success = FALSE;
+  BOOL on_wow64 = FALSE;
+  LPFN_ISWOW64PROCESS IsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress (GetModuleHandle ("kernel32"), "IsWow64Process");
+  
+  if (NULL != IsWow64Process)
+      success = IsWow64Process (GetCurrentProcess (), &on_wow64);
+  
+  return success && on_wow64;
+#endif
+}
 /**
  * Wrapper for executing a shellcommand in windows.
  * 
@@ -232,7 +278,7 @@ execute_shellcommand (const char *command)
        (NULL == (pipe = _popen (command, "rt"))) )
     return EINVAL;
 
-#ifdef DEBUG
+#if DEBUG
   fprintf (stderr, "DEBUG: Command output: \n");
   char output[LINE_LEN];
   while (NULL != fgets (output, sizeof (output), pipe))
@@ -433,12 +479,12 @@ setup_interface ()
   
   /** 
    * Locate the inf-file, we need to store it somewhere where the system can
-   * find it. A good choice would be CWD/PDW or %WINDIR$\system32\
-   * 
-   * TODO: How about win64 in the future? 
-   *       We need to use a different driver for amd64/i386 !
+   * find it. We need to pick the correct driver for win32/win64.
    */
-  GetFullPathNameA (INF_FILE, MAX_PATH, inf_file_path, &temp_inf_filename);
+  if (is_win64())
+    GetFullPathNameA (INF_FILE64, MAX_PATH, inf_file_path, &temp_inf_filename);
+  else
+    GetFullPathNameA (INF_FILE, MAX_PATH, inf_file_path, &temp_inf_filename);
 
   fprintf (stderr, "INFO: Located our driver's .inf file at %s\n", inf_file_path);
   /** 
@@ -876,10 +922,8 @@ attempt_read_tap (struct io_facility * input_facility,
                 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*/
           {
@@ -1005,31 +1049,33 @@ 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:
       {
+        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))
               {
@@ -1037,44 +1083,36 @@ attempt_read_stdin (struct io_facility * input_facility,
                 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;
@@ -1093,7 +1131,7 @@ attempt_read_stdin (struct io_facility * input_facility,
         // 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;
@@ -1102,6 +1140,7 @@ attempt_read_stdin (struct io_facility * input_facility,
               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)))
@@ -1110,14 +1149,15 @@ attempt_read_stdin (struct io_facility * input_facility,
                 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),
@@ -1126,12 +1166,9 @@ attempt_read_stdin (struct io_facility * input_facility,
                 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
@@ -1175,37 +1212,36 @@ static BOOL
 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)
@@ -1230,22 +1266,24 @@ attempt_write (struct io_facility * output_facility,
       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)