阅读:1826回复:0
自写的一个日志模块
随便咨询下网卡开启offload的情况下,NDIS层的checksum要怎么计算才可以,清0或者重启计算都不行
//passive level VOID LogProc(PVOID context) { TIME_FIELDS time_fields; LARGE_INTEGER sleepnum; int i; char* pLogMsg = NULL; char* pLogTime = NULL; int f=0; // 要返回的文件句柄 HANDLE file_handle = NULL; //IO_STATUS_BLOCK IO_STATUS_BLOCK io_status = { 0 }; // 返回值 NTSTATUS ntstatus; // 首先初始化含有文件路径的OBJECT_ATTRIBUTES OBJECT_ATTRIBUTES object_attributes; UNICODE_STRING ufile_name; RtlInitUnicodeString(&ufile_name,L"\\??\\C:\\passthru.log");//不用释放 因为没有动态申请内存 KeInitializeSpinLock(&LogSpinLock); InitializeObjectAttributes( &object_attributes, &ufile_name, OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE, NULL, NULL); // 以OPEN_IF方式打开文件。 ntstatus = ZwCreateFile( &file_handle, FILE_APPEND_DATA, &object_attributes, &io_status, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN_IF, FILE_NON_DIRECTORY_FILE | FILE_RANDOM_ACCESS | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if ( NT_SUCCESS(ntstatus)) { //LogItemCache 初始化缓冲区 LogItemCache = (CHAR*)ExAllocatePoolWithTag(NonPagedPool, MAX_CACHE_ITEM_NUM*MAX_CACHE_ITEM_LEN,TAG); RtlZeroMemory(LogItemCache,MAX_CACHE_ITEM_NUM*MAX_CACHE_ITEM_LEN); pLogTime = (CHAR*)ExAllocatePoolWithTag(NonPagedPool, 64,TAG); //线程睡眠时间 1 秒 sleepnum.QuadPart = -10000*1000*1; //nLogExitFlag == 1 说明需要退出 while(0 == nLogExitFlag) { //sleep 1秒 KeDelayExecutionThread(KernelMode,FALSE,&sleepnum); docachelog: //被dpc打断没事 //KeAcquireSpinLock(&LogWorkerSpinLock,&OldIrql); //检测缓冲区 f=0; for(i=0;i<MAX_CACHE_ITEM_NUM;i++) { pLogMsg = LogItemCache+i*MAX_CACHE_ITEM_LEN; if(0 == pLogMsg[0]) { continue; } //DBGPRINT(("日志读出缓冲区: 1!\n")); //写入 ZwWriteFile(file_handle,NULL,NULL,NULL, &io_status, pLogMsg,strlen(pLogMsg),0, NULL); pLogMsg[0] = 0; //释放缓冲位置 f=1; //DBGPRINT(("日志读出缓冲区: 2!\n")); } if(f) { //获取当前时间 MyGetTime(&time_fields); //写入时间 RtlStringCbPrintfA(pLogTime,64,"%d-%d-%d %d:%d:%d \r\n", time_fields.Year,time_fields.Month,time_fields.Day, time_fields.Hour,time_fields.Minute,time_fields.Second); //写入 ZwWriteFile(file_handle,NULL,NULL,NULL, &io_status, pLogTime,strlen(pLogTime),0, NULL); } //KeReleaseSpinLock(&LogWorkerSpinLock,OldIrql); if(nLogExitFlag) { goto exitlog; } } //退出清理 goto docachelog; exitlog: //LogItemCache 缓冲区释放 ExFreePool(LogItemCache); LogItemCache = NULL; ExFreePool(pLogTime); } DbgPrint("日志线程退出: 1!\n"); //关闭文件句柄 ZwClose(file_handle); //结束当前线程 PsTerminateSystemThread(STATUS_SUCCESS); } //dpclevel or or lower NTSTATUS passthru_log(char* fmt,...) { #ifdef ENABLE_LOG KIRQL OldIrql; int curIRQLev = 0; //日志临时缓冲区 char tempBuff[256]; va_list ap; char* pLogMsg = NULL; int i = 0; if(!LogItemCache || nLogExitFlag) { goto goexit; } //send and recive 都有可能调用 curIRQLev = KeGetCurrentIrql() ; if(DISPATCH_LEVEL == curIRQLev) { KeAcquireSpinLockAtDpcLevel(&LogSpinLock); }else{ KeAcquireSpinLock(&LogSpinLock,&OldIrql); } //复制参数 va_start(ap, fmt); RtlStringCbVPrintfA(tempBuff,256,fmt,ap); va_end(ap); //检测缓冲区 for(i=0;i<MAX_CACHE_ITEM_NUM;i++) { pLogMsg = LogItemCache+i*MAX_CACHE_ITEM_LEN; if(0 != pLogMsg[0]) { continue; } //复制日志内容 RtlMoveMemory(pLogMsg,tempBuff,strlen(tempBuff)+1); break; } if(i == MAX_CACHE_ITEM_NUM) { DbgPrint("缓冲位已满-%d: 0!\n",MAX_CACHE_ITEM_NUM); } if(DISPATCH_LEVEL == curIRQLev) { KeReleaseSpinLockFromDpcLevel(&LogSpinLock); }else{ KeReleaseSpinLock(&LogSpinLock,OldIrql); } goexit: #endif return STATUS_SUCCESS; } |
|