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

2024-01-13 01:48

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

设置用户缓冲区:pcap_setuserbuffer源码如下:

     int

pcap_setuserbuffer(pcap_t *p, int size)

 

{

     unsigned char *new_buff;

 

     if (!p->adapter) {

         sprintf(p->errbuf,"Impossible to set user buffer while reading from a file or on a TurboCap port");

         return -1;

     }

 

     if (size<=0) {

         /* Bogus parameter */

         sprintf(p->errbuf,"Error: invalid size %d",size);

         return -1;

     }

 

     /* Allocate the buffer */

     new_buff=(unsigned char*)malloc(sizeof(char)*size);   //分配用户缓冲区的size

 

     if (!new_buff) {

         sprintf(p->errbuf,"Error: not enough memory");

         return -1;

     }

 

     free(p->buffer);

    

     p->buffer=new_buff;              //保存用户缓冲区的地址

     p->bufsize=size;                 //保存用户缓冲区的size

 

     /* Associate the buffer with the capture packet */

     PacketInitPacket(p->Packet,(BYTE*)p->buffer,p->bufsize);

 

     return 0;

}

PacketInitPacket为初始化p->packet结构,函数源码如下:

/*!

  \brief Initializes a _PACKET structure.

  \param lpPacket The structure to initialize.

  \param Buffer A pointer to a user-allocated buffer that will contain the captured data.

  \param Length the length of the buffer. This is the maximum buffer size that will be

   transferred from the driver to the application using a single read.

 

  \note the size of the buffer associated with the PACKET structure is a parameter that can sensibly

  influence the performance of the capture process, since this buffer will contain the packets received

  from the the driver. The driver is able to return several packets using a single read call

  (see the PacketReceivePacket() function for details), and the number of packets transferable to the

  application in a call is limited only by the size of the buffer associated with the PACKET structure

  passed to PacketReceivePacket(). Therefore setting a big buffer with PacketInitPacket can noticeably

  decrease the number of system calls, reducing the impcat of the capture process on the processor.

*/

 

VOID PacketInitPacket(LPPACKET lpPacket,PVOID Buffer,UINT Length)

 

{

     TRACE_ENTER("PacketInitPacket");

 

    lpPacket->Buffer = Buffer;

    lpPacket->Length = Length;

     lpPacket->ulBytesReceived = 0;   //初始化lpPacket结构

     lpPacket->bIoComplete = FALSE;

 

     TRACE_EXIT("PacketInitPacket");

}

p->packet结构在pcap_read_win32_npf里面使用,用来接收数据包。

static int

pcap_read_win32_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)

{

     int cc;

     int n = 0;

     register u_char *bp, *ep;

 

#ifdef HAVE_REMOTE

     static int samp_npkt;                // parameter needed for sampling, with '1 out of N' method has been requested

     static struct timeval samp_time; // parameter needed for sampling, with '1 every N ms' method has been requested

#endif   /* HAVE_REMOTE */

 

     cc = p->cc;

     if (p->cc == 0) {

         /*

          * Has "pcap_breakloop()" been called?

          */

         if (p->break_loop) {

              /*

               * Yes - clear the flag that indicates that it

               * has, and return -2 to indicate that we were

               * told to break out of the loop.

               */

              p->break_loop = 0;

              return (-2);

         }

 

         /* capture the packets */

         if(PacketReceivePacket(p->adapter,p->Packet,TRUE)==FALSE){

              snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");

              return (-1);

         }

             

         cc = p->Packet->ulBytesReceived;

 

         bp = p->Packet->Buffer;

     }

     else

         bp = p->bp;

 

。。。。。。。。。。。。。。。。。。。。。

}

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



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

相关文章

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

工厂ERP管理系统实现源码(JAVA)

工厂进销存管理系统是一个集采购管理、仓库管理、生产管理和销售管理于一体的综合解决方案。该系统旨在帮助企业优化流程、提高效率、降低成本,并实时掌握各环节的运营状况。 在采购管理方面,系统能够处理采购订单、供应商管理和采购入库等流程,确保采购过程的透明和高效。仓库管理方面,实现库存的精准管理,包括入库、出库、盘点等操作,确保库存数据的准确性和实时性。 生产管理模块则涵盖了生产计划制定、物料需求计划、

flume系列之:查看flume系统日志、查看统计flume日志类型、查看flume日志

遍历指定目录下多个文件查找指定内容 服务器系统日志会记录flume相关日志 cat /var/log/messages |grep -i oom 查找系统日志中关于flume的指定日志 import osdef search_string_in_files(directory, search_string):count = 0