diff --git a/dll/divert.c b/dll/divert.c index 5f17309..2012b1b 100644 --- a/dll/divert.c +++ b/dll/divert.c @@ -172,7 +172,7 @@ static BOOLEAN DivertDriverFiles(LPWSTR *divert_dir_ptr, static BOOLEAN DivertDriverInstall(VOID); static BOOL DivertCompileFilter(const char *filter_str, divert_ioctl_filter_t filter, UINT8 *fp); -static int DivertFilterTokenNameCompare(const void *a, const void *b); +static int __cdecl DivertFilterTokenNameCompare(const void *a, const void *b); static BOOL DivertTokenizeFilter(const char *filter, FILTER_TOKEN *tokens, UINT8 tokensmax); static BOOL DivertParseIPv4Address(char *str, UINT32 *addr_ptr); @@ -485,7 +485,7 @@ extern HANDLE DivertOpen(const char *filter) ioctl.version = DIVERT_VERSION; ioctl.magic = DIVERT_MAGIC; ioctl.reserved = 0x0; - ioctl.arg = NULL; + ioctl.arg = (UINT64)NULL; if (!DeviceIoControl(handle, IOCTL_DIVERT_SET_FILTER, &ioctl, sizeof(ioctl), ioctl_filter, filter_len*sizeof(struct divert_ioctl_filter_s), &iolen, NULL)) @@ -510,7 +510,7 @@ extern BOOL DivertRecv(HANDLE handle, PVOID pPacket, UINT packetLen, ioctl.version = DIVERT_VERSION; ioctl.magic = DIVERT_MAGIC; ioctl.reserved = 0x0; - ioctl.arg = (PVOID)addr; + ioctl.arg = (UINT64)addr; if (!DeviceIoControl(handle, IOCTL_DIVERT_RECV, &ioctl, sizeof(ioctl), pPacket, packetLen, &readlen0, NULL)) { @@ -535,7 +535,7 @@ extern BOOL DivertSend(HANDLE handle, PVOID pPacket, UINT packetLen, ioctl.version = DIVERT_VERSION; ioctl.magic = DIVERT_MAGIC; ioctl.reserved = 0x0; - ioctl.arg = (PVOID)addr; + ioctl.arg = (UINT64)addr; if (!DeviceIoControl(handle, IOCTL_DIVERT_SEND, &ioctl, sizeof(ioctl), pPacket, packetLen, &writelen0, NULL)) { @@ -586,7 +586,7 @@ static BOOL DivertCompileFilter(const char *filter_str, /* * Compare two FILTER_TOKEN_NAMEs. */ -static int DivertFilterTokenNameCompare(const void *a, const void *b) +static int __cdecl DivertFilterTokenNameCompare(const void *a, const void *b) { PFILTER_TOKEN_NAME na = (PFILTER_TOKEN_NAME)a; PFILTER_TOKEN_NAME nb = (PFILTER_TOKEN_NAME)b; diff --git a/doc/divert.html b/doc/divert.html index 3697908..cb94b7f 100644 --- a/doc/divert.html +++ b/doc/divert.html @@ -263,7 +263,7 @@ BOOL DivertRecv( __in HANDLE handle, __out PVOID pPacket, __in UINT packetLen, - __out PDIVERT_ADDRESS pAddr, + __out_opt PDIVERT_ADDRESS pAddr, __out_opt UINT *recvLen ); @@ -296,7 +296,8 @@ If the captured packet is larger than the pPacket buffer length, then the packet will be truncated. If recvLen is non-NULL, then the total number of bytes written to pPacket is placed there. -The address of the captured packet is written to pAddr. +If non-NULL, the address of the captured packet is written to +pAddr.

An application should call DivertRecv() as soon as possible after a successful call to DivertOpen(). diff --git a/examples/netdump/netdump.c b/examples/netdump/netdump.c index fb67a7d..2973ffb 100644 --- a/examples/netdump/netdump.c +++ b/examples/netdump/netdump.c @@ -40,7 +40,7 @@ /* * Entry. */ -int main(int argc, char **argv) +int __cdecl main(int argc, char **argv) { HANDLE handle, console; size_t slen, flen; diff --git a/examples/netdump/sources b/examples/netdump/sources index e0d4311..5beea91 100644 --- a/examples/netdump/sources +++ b/examples/netdump/sources @@ -14,6 +14,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +!IF "$(_BUILDARCH)" == "x86" +CPU=i386 +!ELSE +CPU=$(_BUILDARCH) +!ENDIF + TARGETNAME=netdump TARGETTYPE=PROGRAM TARGETPATH=..\..\install @@ -21,7 +27,7 @@ TARGETLIBS=\ $(SDK_LIB_PATH)\setupapi.lib \ $(SDK_LIB_PATH)\user32.lib \ $(SDK_LIB_PATH)\ws2_32.lib \ - $(TARGETPATH)\$(_BUILDARCH)\divert.lib + $(TARGETPATH)\$(CPU)\divert.lib UMTYPE=console UMENTRY=main USE_MSVCRT=1 diff --git a/examples/netfilter/netfilter.c b/examples/netfilter/netfilter.c index 0575cf6..f872441 100644 --- a/examples/netfilter/netfilter.c +++ b/examples/netfilter/netfilter.c @@ -83,7 +83,7 @@ static void PacketIpv6Icmpv6Init(PICMPV6PACKET packet); /* * Entry. */ -int main(int argc, char **argv) +int __cdecl main(int argc, char **argv) { HANDLE handle, console; size_t slen, flen; diff --git a/examples/netfilter/sources b/examples/netfilter/sources index 0178768..746e9ca 100644 --- a/examples/netfilter/sources +++ b/examples/netfilter/sources @@ -14,6 +14,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +!IF "$(_BUILDARCH)" == "x86" +CPU=i386 +!ELSE +CPU=$(_BUILDARCH) +!ENDIF + TARGETNAME=netfilter TARGETTYPE=PROGRAM TARGETPATH=..\..\install @@ -21,7 +27,7 @@ TARGETLIBS=\ $(SDK_LIB_PATH)\setupapi.lib \ $(SDK_LIB_PATH)\user32.lib \ $(SDK_LIB_PATH)\ws2_32.lib \ - $(TARGETPATH)\$(_BUILDARCH)\divert.lib + $(TARGETPATH)\$(CPU)\divert.lib UMTYPE=console UMENTRY=main USE_MSVCRT=1 diff --git a/examples/webfilter/sources b/examples/webfilter/sources index 27d96d6..0ca83bd 100644 --- a/examples/webfilter/sources +++ b/examples/webfilter/sources @@ -14,6 +14,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +!IF "$(_BUILDARCH)" == "x86" +CPU=i386 +!ELSE +CPU=$(_BUILDARCH) +!ENDIF + TARGETNAME=webfilter TARGETTYPE=PROGRAM TARGETPATH=..\..\install @@ -21,7 +27,7 @@ TARGETLIBS=\ $(SDK_LIB_PATH)\setupapi.lib \ $(SDK_LIB_PATH)\user32.lib \ $(SDK_LIB_PATH)\ws2_32.lib \ - $(TARGETPATH)\$(_BUILDARCH)\divert.lib + $(TARGETPATH)\$(CPU)\divert.lib UMTYPE=console UMENTRY=main USE_MSVCRT=1 diff --git a/examples/webfilter/webfilter.c b/examples/webfilter/webfilter.c index 51a4d7f..c631a8f 100644 --- a/examples/webfilter/webfilter.c +++ b/examples/webfilter/webfilter.c @@ -88,7 +88,7 @@ const char block_data[] = * Prototypes */ static void PacketInit(PPACKET packet); -static int UrlCompare(const void *a, const void *b); +static int __cdecl UrlCompare(const void *a, const void *b); static int UrlMatch(PURL urla, PURL urlb); static PBLACKLIST BlackListInit(void); static void BlackListInsert(PBLACKLIST blacklist, PURL url); @@ -101,7 +101,7 @@ static BOOL BlackListPayloadMatch(PBLACKLIST blacklist, char *data, /* * Entry. */ -int main(int argc, char **argv) +int __cdecl main(int argc, char **argv) { HANDLE handle; DIVERT_ADDRESS addr; @@ -536,7 +536,7 @@ static BOOL BlackListPayloadMatch(PBLACKLIST blacklist, char *data, UINT16 len) /* * URL comparison. */ -static int UrlCompare(const void *a, const void *b) +static int __cdecl UrlCompare(const void *a, const void *b) { PURL urla = *(PURL *)a; PURL urlb = *(PURL *)b; diff --git a/include/divert.h b/include/divert.h index cfd6f59..ebda134 100644 --- a/include/divert.h +++ b/include/divert.h @@ -1,5 +1,5 @@ /* - * divert.c + * divert.h * (C) 2011, all rights reserved, * * This program is free software: you can redistribute it and/or modify @@ -61,7 +61,7 @@ extern DIVERTEXPORT BOOL DivertRecv( __in HANDLE handle, __out PVOID pPacket, __in UINT packetLen, - __out PDIVERT_ADDRESS pAddr, + __out_opt PDIVERT_ADDRESS pAddr, __out_opt UINT *readLen); /* diff --git a/include/divert_device.h b/include/divert_device.h index ad73880..5570744 100644 --- a/include/divert_device.h +++ b/include/divert_device.h @@ -110,12 +110,13 @@ /* * Message definitions. */ +#pragma pack(push, 1) struct divert_ioctl_s { UINT16 magic; // DIVERT_MAGIC UINT8 version; // DIVERT_VERSION UINT8 reserved; // Reserved (set to 0x0) - PVOID arg; // Pointer to buffer + UINT64 arg; // Pointer to buffer }; typedef struct divert_ioctl_s *divert_ioctl_t; @@ -131,6 +132,7 @@ struct divert_ioctl_filter_s UINT32 arg[4]; // Argument. }; typedef struct divert_ioctl_filter_s *divert_ioctl_filter_t; +#pragma pack(pop) /* * IOCTL codes. diff --git a/sys/Makefile.inc b/sys/Makefile.inc index 8639070..e73a2e7 100644 --- a/sys/Makefile.inc +++ b/sys/Makefile.inc @@ -1,4 +1,10 @@ -$(TARGETPATH)\$(_BUILDARCH)\divert.inf: divert.inf - copy /y divert.inf $@ -$(TARGETPATH)\$(_BUILDARCH)\WdfCoInstaller01009.dll: - copy /y $(BASEDIR)\redist\wdf\$(_BUILDARCH)\WdfCoInstaller01009.dll $@ +!IF "$(_BUILDARCH)" == "x86" +CPU=i386 +!ELSE +CPU=$(_BUILDARCH) +!ENDIF + +$(TARGETPATH)\$(CPU)\divert.inf: divert.inf + copy /y divert.inf $@ +$(TARGETPATH)\$(CPU)\WdfCoInstaller01009.dll: + copy /y $(BASEDIR)\redist\wdf\$(_BUILDARCH)\WdfCoInstaller01009.dll $@ diff --git a/sys/divert.c b/sys/divert.c index 0b83044..5a0e3b6 100644 --- a/sys/divert.c +++ b/sys/divert.c @@ -964,9 +964,12 @@ static void divert_read_service(context_t context) // Write the address information. req_context = divert_req_context_get(request); addr = req_context->addr; - addr->IfIdx = packet->if_idx; - addr->SubIfIdx = packet->sub_if_idx; - addr->Direction = packet->direction; + if (addr != NULL) + { + addr->IfIdx = packet->if_idx; + addr->SubIfIdx = packet->sub_if_idx; + addr->Direction = packet->direction; + } // Compute the IP/TCP/UDP checksums here if required. divert_update_checksums(dst, dst_len, packet->ip_checksum, @@ -1208,14 +1211,21 @@ extern VOID divert_ioctl(IN WDFQUEUE queue, IN WDFREQUEST request, switch (code) { case IOCTL_DIVERT_RECV: - status = WdfRequestProbeAndLockUserBufferForWrite(request, - ioctl->arg, sizeof(struct divert_addr_s), &memobj); - if (!NT_SUCCESS(status)) + if ((PVOID)ioctl->arg != NULL) { - DEBUG_ERROR("invalid arg pointer for RECV ioctl", status); - goto divert_ioctl_exit; + status = WdfRequestProbeAndLockUserBufferForWrite(request, + (PVOID)ioctl->arg, sizeof(struct divert_addr_s), &memobj); + if (!NT_SUCCESS(status)) + { + DEBUG_ERROR("invalid arg pointer for RECV ioctl", status); + goto divert_ioctl_exit; + } + addr = (divert_addr_t)WdfMemoryGetBuffer(memobj, NULL); + } + else + { + addr = NULL; } - addr = (divert_addr_t)WdfMemoryGetBuffer(memobj, NULL); WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, req_context_s); @@ -1238,7 +1248,7 @@ extern VOID divert_ioctl(IN WDFQUEUE queue, IN WDFREQUEST request, case IOCTL_DIVERT_SEND: status = WdfRequestProbeAndLockUserBufferForRead(request, - ioctl->arg, sizeof(struct divert_addr_s), &memobj); + (PVOID)ioctl->arg, sizeof(struct divert_addr_s), &memobj); if (!NT_SUCCESS(status)) { DEBUG_ERROR("invalid arg pointer for SEND ioctl", status); @@ -1253,7 +1263,7 @@ extern VOID divert_ioctl(IN WDFQUEUE queue, IN WDFREQUEST request, break; case IOCTL_DIVERT_SET_FILTER: - if (ioctl->arg != NULL) + if ((PVOID)ioctl->arg != NULL) { status = STATUS_INVALID_DEVICE_REQUEST; DEBUG_ERROR("arg pointer is non-NULL for SET_FILTER ioctl", diff --git a/sys/sources b/sys/sources index 8ddfe49..9dffb97 100644 --- a/sys/sources +++ b/sys/sources @@ -14,6 +14,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +!IF "$(_BUILDARCH)" == "x86" +CPU=i386 +!ELSE +CPU=$(_BUILDARCH) +!ENDIF + TARGETNAME=divert TARGETTYPE=DRIVER TARGETPATH=..\install @@ -23,8 +29,8 @@ TARGETLIBS=\ $(DDK_LIB_PATH)\fwpkclnt.lib \ $(SDK_LIB_PATH)\uuid.lib NTTARGETFILES=\ - $(TARGETPATH)\$(_BUILDARCH)\divert.inf \ - $(TARGETPATH)\$(_BUILDARCH)\WdfCoInstaller01009.dll + $(TARGETPATH)\$(CPU)\divert.inf \ + $(TARGETPATH)\$(CPU)\WdfCoInstaller01009.dll KMDF_VERSION_MAJOR=1 C_DEFINES=$(C_DEFINES) -DBINARY_COMPATIBLE=0 -DNT -DUNICODE -D_UNICODE \ -DNDIS60 -DNDIS_SUPPORT_NDIS60