我正在尝试使用驱动程序中的 WinSock API 连接到另一台计算机。问题是WskSocketConnect
(WskConnect
如果我走那条路)失败了STATUS_INVALID_ADDRESS
. 我的代码是:
#pragma warning(push)
#pragma warning(disable:4510)
#pragma warning(disable:4512)
#pragma warning(disable:4610)
#include <ntddk.h>
#include <wsk.h>
#pragma warning(pop)
const WSK_CLIENT_DISPATCH WskClientDispatch = {
MAKE_WSK_VERSION(1, 0),
0,
NULL
};
WSK_REGISTRATION WskRegistration;
typedef struct _ASYNCHRONOUS_OPERATION_CONTEXT {
KEVENT Event;
PIRP Irp;
} ASYNCHRONOUS_OPERATION_CONTEXT, *PASYNCHRONOUS_OPERATION_CONTEXT;
extern "C" DRIVER_INITIALIZE DriverEntry;
extern "C" DRIVER_UNLOAD DriverUnload;
extern "C" KSTART_ROUTINE WorkerThread;
extern "C" IO_COMPLETION_ROUTINE SyncIrpCompletionRoutine;
extern "C" NTSTATUS WorkerThreadImpl();
extern "C" NTSTATUS InitializeAsynchronousOperationContext(PASYNCHRONOUS_OPERATION_CONTEXT Context);
extern "C" void FreeAsynchronousOperationContext(PASYNCHRONOUS_OPERATION_CONTEXT Context);
extern "C" void ReuseAsynchronousOperationContext(PASYNCHRONOUS_OPERATION_CONTEXT Context);
extern "C" NTSTATUS WaitForOperation(PASYNCHRONOUS_OPERATION_CONTEXT Context, NTSTATUS DispatchStatus);
NTSTATUS
DriverEntry(
__in PDRIVER_OBJECT DriverObject,
__in PUNICODE_STRING RegistryPath
);
VOID
WorkerThread(
__in PVOID Context
);
VOID
DriverUnload(
__in PDRIVER_OBJECT DriverObject
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, DriverUnload)
#pragma alloc_text(PAGE, WorkerThread)
#pragma alloc_text(PAGE, WorkerThreadImpl)
#pragma alloc_text(PAGE, InitializeAsynchronousOperationContext)
#pragma alloc_text(PAGE, FreeAsynchronousOperationContext)
#pragma alloc_text(PAGE, ReuseAsynchronousOperationContext)
#pragma alloc_text(PAGE, WaitForOperation)
#endif
#define DATA_BUFFER_POOL_TAG 'wskt'
NTSTATUS
DriverEntry(
__in PDRIVER_OBJECT DriverObject,
__in PUNICODE_STRING RegistryPath
)
{
NTSTATUS status;
HANDLE threadHandle;
WSK_CLIENT_NPI wskClientNpi;
UNREFERENCED_PARAMETER(RegistryPath);
PAGED_CODE();
DbgPrint("Loading");
wskClientNpi.ClientContext = NULL;
wskClientNpi.Dispatch = &WskClientDispatch;
status = WskRegister(&wskClientNpi, &WskRegistration);
if (!NT_SUCCESS(status)) {
return status;
}
status = PsCreateSystemThread(
&threadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL,
WorkerThread, NULL);
if (!NT_SUCCESS(status)) {
WskDeregister(&WskRegistration);
return status;
}
ZwClose(threadHandle);
DriverObject->DriverUnload = DriverUnload;
DbgPrint("Loaded");
return STATUS_SUCCESS;
}
VOID
DriverUnload(
__in PDRIVER_OBJECT DriverObject
)
{
UNREFERENCED_PARAMETER(DriverObject);
PAGED_CODE();
DbgPrint("Unloading");
WskDeregister(&WskRegistration);
DbgPrint("Unloaded");
}
WSK_PROVIDER_NPI wskProviderNpi;
//CHAR Foo[10]; // padding
PCHAR DataBuffer;
USHORT htons(USHORT value)
{
return value >> 8 | ((value & 0xff) << 8);
}
NTSTATUS InitializeAsynchronousOperationContext(PASYNCHRONOUS_OPERATION_CONTEXT Context)
{
PAGED_CODE();
Context->Irp = IoAllocateIrp(1, FALSE);
if (Context->Irp == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
}
KeInitializeEvent(&Context->Event, SynchronizationEvent, FALSE);
IoSetCompletionRoutine(Context->Irp,
SyncIrpCompletionRoutine,
&Context->Event, TRUE, TRUE, TRUE);
return STATUS_SUCCESS;
}
void FreeAsynchronousOperationContext(PASYNCHRONOUS_OPERATION_CONTEXT Context)
{
PAGED_CODE();
IoFreeIrp(Context->Irp);
}
void ReuseAsynchronousOperationContext(PASYNCHRONOUS_OPERATION_CONTEXT Context)
{
PAGED_CODE();
IoReuseIrp(Context->Irp, STATUS_UNSUCCESSFUL);
IoSetCompletionRoutine(Context->Irp,
SyncIrpCompletionRoutine,
&Context->Event, TRUE, TRUE, TRUE);
}
NTSTATUS WaitForOperation(PASYNCHRONOUS_OPERATION_CONTEXT Context, NTSTATUS DispatchStatus)
{
PAGED_CODE();
if (DispatchStatus == STATUS_PENDING) {
KeWaitForSingleObject(&Context->Event, Executive, KernelMode, FALSE, NULL);
}
return Context->Irp->IoStatus.Status;
}
NTSTATUS WorkerThreadImpl()
{
PAGED_CODE();
NTSTATUS status;
status = WskCaptureProviderNPI(
&WskRegistration,
WSK_INFINITE_WAIT,
&wskProviderNpi);
if (!NT_SUCCESS(status)) {
DbgPrint("WskCaptureProviderNPI FAIL");
return status;
}
DataBuffer = (PCHAR) ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, DATA_BUFFER_POOL_TAG);
if (DataBuffer == NULL) {
WskReleaseProviderNPI(&WskRegistration);
return STATUS_INSUFFICIENT_RESOURCES;
}
ASYNCHRONOUS_OPERATION_CONTEXT OperationContext;
status = InitializeAsynchronousOperationContext(&OperationContext);
if (!NT_SUCCESS(status)) {
WskReleaseProviderNPI(&WskRegistration);
ExFreePoolWithTag(DataBuffer, DATA_BUFFER_POOL_TAG);
return status;
}
SOCKADDR_IN localAddr = { AF_INET, 0, IN4ADDR_ANY_INIT };
SOCKADDR_IN remoteAddr = { AF_INET, htons(7777), IN4ADDR_LOOPBACK_INIT };
DbgPrint("%p", DataBuffer);
status = wskProviderNpi.Dispatch->WskSocketConnect(&wskProviderNpi.Client,
SOCK_STREAM, IPPROTO_TCP,
(PSOCKADDR) &localAddr, (PSOCKADDR) &remoteAddr, 0,
NULL, NULL, NULL, NULL, NULL,
OperationContext.Irp);
// BUGBUG here, DataBuffer has changed since the previous DbgPrint()
DbgPrint("%p", DataBuffer);
status = WaitForOperation(&OperationContext, status);
if (NT_SUCCESS(status)) {
PWSK_SOCKET socket = (PWSK_SOCKET) OperationContext.Irp->IoStatus.Information;
PWSK_PROVIDER_CONNECTION_DISPATCH connectionDispatch = (PWSK_PROVIDER_CONNECTION_DISPATCH) socket->Dispatch;
ReuseAsynchronousOperationContext(&OperationContext);
status = connectionDispatch->Basic.WskCloseSocket(socket, OperationContext.Irp);
status = WaitForOperation(&OperationContext, status);
}
FreeAsynchronousOperationContext(&OperationContext);
WskReleaseProviderNPI(&WskRegistration);
ExFreePoolWithTag(DataBuffer, DATA_BUFFER_POOL_TAG);
return STATUS_SUCCESS;
}
VOID
WorkerThread(
__in PVOID Context
)
{
UNREFERENCED_PARAMETER(Context);
PAGED_CODE();
PsTerminateSystemThread(WorkerThreadImpl());
}
NTSTATUS
SyncIrpCompletionRoutine(
__in PDEVICE_OBJECT Reserved,
__in PIRP Irp,
_In_reads_opt_(_Inexpressible_("varies")) PVOID Context
)
{
UNREFERENCED_PARAMETER(Reserved);
UNREFERENCED_PARAMETER(Irp);
ASSERT(Context != NULL);
if (Irp->PendingReturned) {
KeSetEvent((PKEVENT) Context, 2, FALSE);
}
return STATUS_MORE_PROCESSING_REQUIRED;
}
0xc0000141
调用与( )同步失败STATUS_INVALID_ADDRESS
。我确实有一个应用程序在本地主机上的该端口上侦听,并且它与远程计算机的行为方式相同。
任何提示将不胜感激!
编辑现在事情变得越来越奇怪了。我用我的完整文件更新了源代码。
我发现了问题WskSocketConnect
:我正在重构东西,我试图将WskCaptureProviderNPI
and移动WskReleaseProviderNPI
到不同的函数,但我开始遇到另一个错误。这很奇怪,所以我把我的WSK_PROVIDER_NPI
全局设置为——它起作用了。
稍后,我尝试在该套接字上发送一条消息,但我在它之前收到了两个字节的垃圾。经过几个小时和一些调试消息后,我注意到WskSocketConnect
调用后缓冲区地址发生了变化。现在,缓冲区地址也是一个全局变量,在我的WSK_PROVIDER_NPI
结构之后声明。添加一些填充(搜索“ Foo
”和“ WorkerThread()
”)修复它。
现在怎么办?我对此完全陌生,但看起来内存损坏是由Wsk
函数引起的,特别是因为我的代码DriverEntry
并没有在WskSocketConnect
调用之间运行。有任何想法吗?
这是在 Windows 7 x64 上。
更新 2 我在代码开头禁用的编译警告如下:
warning C4510: '_WSK_TDI_MAP_INFO' : default constructor could not be generated
warning C4512: '_WSK_TDI_MAP_INFO' : assignment operator could not be generated
warning C4610: struct '_WSK_TDI_MAP_INFO' can never be instantiated - user defined constructor required
我想将文件编译为 C 会修复它们。
关于 my WSK_CLIENT_NPI
,文档WskRegister
没有提到它应该保持有效(就像它对另一个参数所做的那样),并且echosrv
DDK 中的示例做同样的事情。可以肯定的是,我尝试将其设为全球性,但我得到了相同的行为。