庖丁解牛—winpcap源码彻底解密系列的续集(8)

2024-01-13 01:48

本文主要是介绍庖丁解牛—winpcap源码彻底解密系列的续集(8),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

设置超时,设置mintoCopy,设置内核缓冲,设置用户缓冲,设置MTU的大小,这些实现都和设置混合模式相似。讲解如下:

 

如设置内核缓冲区,代码段如下pcap_win32的pcap_activate_win32函数:

          if (p->opt.buffer_size == 0)

              p->opt.buffer_size = WIN32_DEFAULT_KERNEL_BUFFER_SIZE;

 

         if(PacketSetBuff(p->adapter,p->opt.buffer_size)==FALSE)

         {

              snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");

              goto bad;

         }

WIN32_DEFAULT_KERNEL_BUFFER_SIZE为默认的内核缓冲区大小,#define  WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000

即winpcap默认的内核缓冲区的大小为1M。PacketSetBuff在Packet.c中定义,

*/

BOOLEAN PacketSetBuff(LPADAPTER AdapterObject,int dim)

{

     DWORD BytesReturned;

     BOOLEAN Result;

 

     TRACE_ENTER("PacketSetBuff");

 

#ifdef HAVE_WANPACKET_API

     if (AdapterObject->Flags == INFO_FLAG_NDISWAN_ADAPTER)

     {

        Result = WanPacketSetBufferSize(AdapterObject->pWanAdapter, dim);

        

         TRACE_EXIT("PacketSetBuff");

         return Result;

     }

#endif

 

#ifdef HAVE_AIRPCAP_API

     if(AdapterObject->Flags == INFO_FLAG_AIRPCAP_CARD)

     {

         Result = (BOOLEAN)g_PAirpcapSetKernelBuffer(AdapterObject->AirpcapAd, dim);

        

         TRACE_EXIT("PacketSetBuff");

         return Result;

     }

#endif // HAVE_AIRPCAP_API

 

#ifdef HAVE_NPFIM_API

     if(AdapterObject->Flags == INFO_FLAG_NPFIM_DEVICE)

     {

         Result = (BOOLEAN)g_NpfImHandlers.NpfImSetCaptureBufferSize(AdapterObject->NpfImHandle, dim);

 

         TRACE_EXIT("PacketSetBuff");

         return Result;

     }

#endif // HAVE_NPFIM_API

 

#ifdef HAVE_DAG_API

     if(AdapterObject->Flags == INFO_FLAG_DAG_CARD)

     {

         // We can't change DAG buffers

         TRACE_EXIT("PacketSetBuff");

         return TRUE;

     }

#endif // HAVE_DAG_API

 

     if (AdapterObject->Flags == INFO_FLAG_NDIS_ADAPTER)

     {

         Result = (BOOLEAN)DeviceIoControl(AdapterObject->hFile,BIOCSETBUFFERSIZE,&dim,sizeof(dim),NULL,0,&BytesReturned,NULL);

     }

     else

     {

         TRACE_PRINT1("Request to set buf size on an unknown device type (%u)", AdapterObject->Flags);

         Result = FALSE;

     }

    

     TRACE_EXIT("PacketSetBuff");

 

     return Result;

}

 

该函数同样是通过DeviceIoControl将缓冲区的size传递给内核的,IOControl码为BIOCSETBUFFERSIZE,在npf.sys中进行搜索:通样在NPF_IoControl中实现。

case BIOCSETBUFFERSIZE:

        

         TRACE_MESSAGE(PACKET_DEBUG_LOUD, "BIOCSETBUFFERSIZE");

 

 

         if(IrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof(ULONG))

         {            

              SET_FAILURE_BUFFER_SMALL();

              break;

         }

 

         // Get the number of bytes to allocate

          dim = *((PULONG)Irp->AssociatedIrp.SystemBuffer);

         //dim为获取的SystemBuffer,其值为内核缓冲区的大小;

 

         if (dim / g_NCpu < sizeof(struct PacketHeader))

         {

              dim = 0; //如果dim的size分到每个cpu后都比包头的size小,且dim=0

         }

         else

         {

              tpointer = ExAllocatePoolWithTag(NonPagedPool, dim, '6PWA'); //分配内存

              if (tpointer == NULL)

              {

                   // no memory  内存分配失败

                   SET_FAILURE_NOMEM();

                   break;

              }

         }

 

         //

         // acquire the locks for all the buffers

         //

         for (i = 0; i < g_NCpu ; i++)

         {

#pragma prefast(suppress:8103, "There's no Spinlock leak here, as it's released some lines below.")

              NdisAcquireSpinLock(&Open->CpuData[i].BufferLock); //获取每个cpu的锁

         }

 

         //

         // free the old buffer, if any

         //

         if (Open->CpuData[0].Buffer != NULL)

         {

              ExFreePool(Open->CpuData[0].Buffer); //释放原有内存

         }

 

         for (i = 0 ; i < g_NCpu ; i++)

         {

              if (dim > 0)

                   Open->CpuData[i].Buffer=(PUCHAR)tpointer + (dim/g_NCpu)*i;  //对每个cpu平均分配缓冲

              else

                   Open->CpuData[i].Buffer = NULL;

              Open->CpuData[i].Free = dim/g_NCpu;       //剩余缓冲

              Open->CpuData[i].P = 0;               //生产者

              Open->CpuData[i].C = 0;              //消费者

              Open->CpuData[i].Accepted = 0;

              Open->CpuData[i].Dropped = 0;

              Open->CpuData[i].Received = 0;

          }

 

         Open->ReaderSN=0;                    //读序列号

         Open->WriterSN=0;                     //写序列号

 

         Open->Size = dim/g_NCpu;             //open上下文的size

 

         //

         // acquire the locks for all the buffers  释放锁

         //

         i = g_NCpu;

 

         do

         {

              i--;

 

#pragma prefast(suppress:8107, "There's no Spinlock leak here, as it's acquired some lines above.")

              NdisReleaseSpinLock(&Open->CpuData[i].BufferLock);

         }while(i != 0);

 

         SET_RESULT_SUCCESS(0);

         break;

 

该函数将内核缓冲区的size平均到每一个cpu,并将它们保存在Open上下文中;open上下文的结构如下:

typedef struct _OPEN_INSTANCE

{

     PDEVICE_EXTENSION   DeviceExtension; ///< Pointer to the _DEVICE_EXTENSION structure of the device on which

                                                   ///< the instance is bound.

     NDIS_HANDLE         AdapterHandle;        ///< NDIS idetifier of the adapter used by this instance.

     UINT               Medium;                ///< Type of physical medium the underlying NDIS driver uses. See the

                                               ///< documentation of NdisOpenAdapter in the MS DDK for details.

     NDIS_HANDLE         PacketPool;           ///< Pool of NDIS_PACKET structures used to transfer the packets from and to the NIC driver.

     KSPIN_LOCK             RequestSpinLock;   ///< SpinLock used to synchronize the OID requests.

     LIST_ENTRY          RequestList;     ///< List of pending OID requests.

     LIST_ENTRY          ResetIrpList;         ///< List of pending adapter reset requests.

    INTERNAL_REQUEST    Requests[MAX_REQUESTS]; ///< Array of structures that wrap every single OID request.

     PMDL               BufferMdl;             ///< Pointer to a Memory descriptor list (MDL) that maps the circular buffer's memory.

     PKEVENT                ReadEvent;             ///< Pointer to the event on which the read calls on this instance must wait.

     PUCHAR                 bpfprogram;            ///< Pointer to the filtering pseudo-code associated with current instance of the driver.

                                                   ///< This code is used only in particular situations (for example when the packet received

                                                   ///< from the NIC driver is stored in two non-consecutive buffers. In normal situations

                                                   ///< the filtering routine created by the JIT compiler and pointed by the next field

                                                   ///< is used. See \ref NPF for details on the filtering process.

#ifdef _X86_

     JIT_BPF_Filter         *Filter;           ///< Pointer to the native filtering function created by the jitter.

                                                   ///< See BPF_jitter() for details.

#endif //_X86_

     UINT               MinToCopy;             ///< Minimum amount of data in the circular buffer that unlocks a read. Set with the

                                                   ///< BIOCSMINTOCOPY IOCTL.

     LARGE_INTEGER      TimeOut;           ///< Timeout after which a read is released, also if the amount of data in the buffer is

                                                   ///< less than MinToCopy. Set with the BIOCSRTIMEOUT IOCTL.

                                                  

     int                    mode;                  ///< Working mode of the driver. See PacketSetMode() for details.

     LARGE_INTEGER      Nbytes;                ///< Amount of bytes accepted by the filter when this instance is in statistical mode.

     LARGE_INTEGER      Npackets;          ///< Number of packets accepted by the filter when this instance is in statistical mode.

     NDIS_SPIN_LOCK         CountersLock;      ///< SpinLock that protects the statistical mode counters.

     UINT               Nwrites;           ///< Number of times a single write must be physically repeated. See \ref NPF for an

                                                   ///< explanation

     ULONG                  Multiple_Write_Counter; ///< Counts the number of times a single write has already physically repeated.

     NDIS_EVENT             WriteEvent;            ///< Event used to synchronize the multiple write process.

     BOOLEAN                WriteInProgress;   ///< True if a write is currently in progress. NPF currently allows a single wite on

                                                   ///< the same open instance.

     NDIS_SPIN_LOCK         WriteLock;             ///< SpinLock that protects the WriteInProgress variable.

     NDIS_EVENT             NdisRequestEvent;  ///< Event used to synchronize I/O requests with the callback structure of NDIS.

     BOOLEAN                SkipSentPackets;   ///< True if this instance should not capture back the packets that it transmits.

     NDIS_STATUS            IOStatus;          ///< Maintains the status of and OID request call, that will be passed to the application.

     HANDLE                 DumpFileHandle;        ///< Handle of the file used in dump mode.

     PFILE_OBJECT       DumpFileObject;        ///< Pointer to the object of the file used in dump mode.

     PKTHREAD           DumpThreadObject;  ///< Pointer to the object of the thread used in dump mode.

     HANDLE                 DumpThreadHandle;  ///< Handle of the thread created by dump mode to asynchronously move the buffer to disk.

     NDIS_EVENT             DumpEvent;             ///< Event used to synchronize the dump thread with the tap when the instance is in dump mode.

     LARGE_INTEGER      DumpOffset;            ///< Current offset in the dump file.

     UNICODE_STRING      DumpFileName;         ///< String containing the name of the dump file.

     UINT               MaxDumpBytes;      ///< Maximum dimension in bytes of the dump file. If the dump file reaches this size it

                                                   ///< will be closed. A value of 0 means unlimited size.

     UINT               MaxDumpPacks;      ///< Maximum number of packets that will be saved in the dump file. If this number of

                                                   ///< packets is reached the dump will be closed. A value of 0 means unlimited number of

                                                   ///< packets.

     BOOLEAN                DumpLimitReached;  ///< TRUE if the maximum dimension of the dump file (MaxDumpBytes or MaxDumpPacks) is

                                                   ///< reached.

#ifdef HAVE_BUGGY_TME_SUPPORT

     MEM_TYPE           mem_ex;                ///< Memory used by the TME virtual co-processor

     TME_CORE           tme;               ///< Data structure containing the virtualization of the TME co-processor

#endif //HAVE_BUGGY_TME_SUPPORT

 

     NDIS_SPIN_LOCK         MachineLock;       ///< SpinLock that protects the BPF filter and the TME engine, if in use.

     UINT               MaxFrameSize;      ///< Maximum frame size that the underlying MAC acceptes. Used to perform a check on the

                                                   ///< size of the frames sent with NPF_Write() or NPF_BufferedWrite().

     //

     // KAFFINITY is used as a bit mask for the affinity in the system. So on every supported OS is big enough for all the CPUs on the system (32 bits on x86, 64 on x64?).

     // We use its size to compute the max number of CPUs.

     //

     CpuPrivateData         CpuData[sizeof(KAFFINITY) * 8];      ///< Pool of kernel buffer structures, one for each CPU.

     ULONG                  ReaderSN;          ///< Sequence number of the next packet to be read from the pool of kernel buffers.

     ULONG                  WriterSN;          ///< Sequence number of the next packet to be written in the pool of kernel buffers.

                                                   ///< These two sequence numbers are unique for each capture instance.

     ULONG                  Size;                  ///< Size of each kernel buffer contained in the CpuData field.

     ULONG                 AdapterHandleUsageCounter;

     NDIS_SPIN_LOCK        AdapterHandleLock;

     ULONG                 AdapterBindingStatus;    ///< Specifies if NPF is still bound to the adapter used by this instance, it's unbinding or it's not bound.   

 

     NDIS_EVENT            NdisOpenCloseCompleteEvent;

     NDIS_EVENT            NdisWriteCompleteEvent;  ///< Event that is signalled when all the packets have been successfully sent by NdisSend (and corresponfing sendComplete has been called)

     NTSTATUS         OpenCloseStatus;

     ULONG                 TransmitPendingPackets;  ///< Specifies the number of packets that are pending to be transmitted, i.e. have been submitted to NdisSendXXX but the SendComplete has not been called yet.

     ULONG                 NumPendingIrps;

     BOOLEAN               ClosePending;

     NDIS_SPIN_LOCK        OpenInUseLock;

}

OPEN_INSTANCE, *POPEN_INSTANCE;

 

设置mintoCopy的方法和设置内核缓冲区类似,不在进行讲解。

这篇关于庖丁解牛—winpcap源码彻底解密系列的续集(8)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/599856

相关文章

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

macOS彻底卸载Python的超完整指南(推荐!)

《macOS彻底卸载Python的超完整指南(推荐!)》随着python解释器的不断更新升级和项目开发需要,有时候会需要升级或者降级系统中的python的版本,系统中留存的Pytho版本如果没有卸载干... 目录MACOS 彻底卸载 python 的完整指南重要警告卸载前检查卸载方法(按安装方式)1. 卸载

一文详解MySQL索引(六张图彻底搞懂)

《一文详解MySQL索引(六张图彻底搞懂)》MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度,:本文主要介绍MySQL索引的相关资料,文中通过代码介绍的... 目录一、什么是索引?为什么需要索引?二、索引该用哪种数据结构?1. 哈希表2. 跳表3. 二叉排序树4.

java 恺撒加密/解密实现原理(附带源码)

《java恺撒加密/解密实现原理(附带源码)》本文介绍Java实现恺撒加密与解密,通过固定位移量对字母进行循环替换,保留大小写及非字母字符,由于其实现简单、易于理解,恺撒加密常被用作学习加密算法的入... 目录Java 恺撒加密/解密实现1. 项目背景与介绍2. 相关知识2.1 恺撒加密算法原理2.2 Ja

Nginx屏蔽服务器名称与版本信息方式(源码级修改)

《Nginx屏蔽服务器名称与版本信息方式(源码级修改)》本文详解如何通过源码修改Nginx1.25.4,移除Server响应头中的服务类型和版本信息,以增强安全性,需重新配置、编译、安装,升级时需重复... 目录一、背景与目的二、适用版本三、操作步骤修改源码文件四、后续操作提示五、注意事项六、总结一、背景与

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

一文解密Python进行监控进程的黑科技

《一文解密Python进行监控进程的黑科技》在计算机系统管理和应用性能优化中,监控进程的CPU、内存和IO使用率是非常重要的任务,下面我们就来讲讲如何Python写一个简单使用的监控进程的工具吧... 目录准备工作监控CPU使用率监控内存使用率监控IO使用率小工具代码整合在计算机系统管理和应用性能优化中,监

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具