<NME> mingw.c
<BEF> #include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>

#include "divert.h"
#include "tcpcryptd.h"

static HANDLE _h, _h2;

// XXX signal 1 byte and have main thread ReadFile directly.  Peek here.
static WINAPI DWORD reader(void *arg)
{
	int s;
	struct sockaddr_in s_in;
        ULONG r;
        int rc;
	unsigned char buf[2048];
	
	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
		err(1, "socket()");
	s_in.sin_family	     = PF_INET;
	s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
	s_in.sin_port	     = htons(619);

	while (1) {
	s_in.sin_port	     = htons(619);

	while (1) {
		rc = ReadFile(_h, buf, sizeof(buf), &r, NULL);
		if (!rc)
			err(1, "ReadFile()");

		if (sendto(s, (void*) buf, r, 0,
			   (struct sockaddr*) &s_in, sizeof(s_in)) != r)
			err(1, "sendto()");
	}

	return 0;
}

int do_divert_open(char *dev)
{
	// XXX i know this is lame
	struct sockaddr_in s_in;
	int s;

	memset(&s_in, 0, sizeof(s_in));
	s_in.sin_family	     = PF_INET;
	s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
	s_in.sin_port	= htons(619);

	if (bind(s, (struct sockaddr*) &s_in, sizeof(s_in)) == -1)
		err(1, "bind(divert)");

	// XXX: Currently TCP port 80 only...
	if (bind(s, (struct sockaddr*) &s_in, sizeof(s_in)) == -1)
		err(1, "bind(divert)");

        _h = CreateFile(dev,
                GENERIC_READ,
                0,
                NULL,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                INVALID_HANDLE_VALUE);

        _h2 = CreateFile(dev,
                 GENERIC_WRITE,
                0,
                NULL,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                INVALID_HANDLE_VALUE);

	if (_h == INVALID_HANDLE_VALUE || _h2 == INVALID_HANDLE_VALUE)
		err(1, "CreateFile()");

	if (!CreateThread(NULL, 0, reader, NULL, 0, NULL))
		err(1, "CreateThread()");

	if (!CreateThread(NULL, 0, reader, NULL, 0, NULL))
		err(1, "CreateThread()");

void do_divert_close(int s)
{
        CloseHandle(_h);
        CloseHandle(_h2);
}

int do_divert_read(int s, void *buf, int len)
}

}

int do_divert_write(int s, void *buf, int len)                                      
{                                                                                   
        ULONG r;
        int rc;

        rc = WriteFile(_h2, buf, len, &r, NULL);
        if (!rc)
                return -1;

        return r;
}
	return r + MAC_SIZE;
}


<MSG> Replace the PassThru driver with WinDivert package.

<DFF> @@ -8,16 +8,25 @@
 #include "divert.h"
 #include "tcpcryptd.h"
 
-static HANDLE _h, _h2;
+// XXX: include WinDivert header
+#define UINT8   unsigned char
+#define UINT16  unsigned short
+#include "../../contrib/divert/divert.h"
+
+#define MAC_SIZE        14
+
+static HANDLE _h;
 
 // XXX signal 1 byte and have main thread ReadFile directly.  Peek here.
 static WINAPI DWORD reader(void *arg)
 {
 	int s;
 	struct sockaddr_in s_in;
-        ULONG r;
-        int rc;
+        UINT r;
 	unsigned char buf[2048];
+        
+        // XXX: the DIVERT_ADDRESS is stored in the ethhdr.
+        PDIVERT_ADDRESS addr = (PDIVERT_ADDRESS)buf;
 	
 	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
 		err(1, "socket()");
@@ -29,21 +38,24 @@ static WINAPI DWORD reader(void *arg)
 	s_in.sin_port	     = htons(619);
 
 	while (1) {
-		rc = ReadFile(_h, buf, sizeof(buf), &r, NULL);
-		if (!rc)
-			err(1, "ReadFile()");
-
-		if (sendto(s, (void*) buf, r, 0,
-			   (struct sockaddr*) &s_in, sizeof(s_in)) != r)
+                memset(buf, 0, MAC_SIZE);
+                if (!DivertRecv(_h, buf + MAC_SIZE, sizeof(buf) - MAC_SIZE,
+                           addr, &r))
+                        err(1, "DivertRead()");
+
+		if (sendto(s, (void*) buf, r + MAC_SIZE, 0,
+			   (struct sockaddr*) &s_in, sizeof(s_in)) !=
+                           r + MAC_SIZE)
 			err(1, "sendto()");
 	}
 
 	return 0;
 }
 
-int do_divert_open(char *dev)
+int do_divert_open(void)
 {
 	// XXX i know this is lame
+        // XXX yeah
 	struct sockaddr_in s_in;
 	int s;
 
@@ -59,24 +71,13 @@ int do_divert_open(char *dev)
 	if (bind(s, (struct sockaddr*) &s_in, sizeof(s_in)) == -1)
 		err(1, "bind(divert)");
 
-        _h = CreateFile(dev,
-                GENERIC_READ,
-                0,
-                NULL,
-                OPEN_EXISTING,
-                FILE_ATTRIBUTE_NORMAL,
-                INVALID_HANDLE_VALUE);
-
-        _h2 = CreateFile(dev,
-                 GENERIC_WRITE,
-                0,
-                NULL,
-                OPEN_EXISTING,
-                FILE_ATTRIBUTE_NORMAL,
-                INVALID_HANDLE_VALUE);
-
-	if (_h == INVALID_HANDLE_VALUE || _h2 == INVALID_HANDLE_VALUE)
-		err(1, "CreateFile()");
+        _h = DivertOpen(
+                "ip and "
+                "((outbound and tcp.DstPort == 80) or "
+                " (inbound and tcp.SrcPort == 80))");
+
+	if (_h == INVALID_HANDLE_VALUE)
+		err(1, "DivertOpen()");
 
 	if (!CreateThread(NULL, 0, reader, NULL, 0, NULL))
 		err(1, "CreateThread()");
@@ -86,8 +87,7 @@ int do_divert_open(char *dev)
 
 void do_divert_close(int s)
 {
-        CloseHandle(_h);
-        CloseHandle(_h2);
+        DivertClose(_h);
 }
 
 int do_divert_read(int s, void *buf, int len)
@@ -96,13 +96,19 @@ int do_divert_read(int s, void *buf, int len)
 }
 
 int do_divert_write(int s, void *buf, int len)                                      
-{                                                                                   
-        ULONG r;
-        int rc;
+{
+        UINT r;
+        PDIVERT_ADDRESS addr = (PDIVERT_ADDRESS)buf;
 
-        rc = WriteFile(_h2, buf, len, &r, NULL);
-        if (!rc)
+        if (len <= MAC_SIZE)
                 return -1;
 
-        return r;
+        buf += MAC_SIZE;
+        len -= MAC_SIZE;
+
+        if (!DivertSend(_h, buf, len, addr, &r))
+                return -1;
+
+        return r + MAC_SIZE;
 }
+

Replace the PassThru driver with WinDivert package.