winPcap_6_不用回调方法捕获数据包
用 pcap_next_ex() 函数代替 _5_ 中的 pcap_loop()函数;
pcap_loop()函数是基于回调的原理来进行数据捕获,这是一种精妙的方法,并且在某些场合中,它是一种很好的选择。 然而,处理回调有时候并不实用 -- 它会增加程序的复杂度,特别是在拥有多线程的C++程序中。
可以通过直接调用pcap_next_ex() 函数来获得一个数据包 -- 只有当编程人员使用了 pcap_next_ex() 函数才能收到数据包。
这个函数的参数和捕获回调函数的参数是一样的 -- 它包含一个网络适配器的描述符和两个可以初始化和返回给用户的指针 (一个指向 pcap_pkthdr 结构体,另一个指向数据报数据的缓冲)。
在下面的程序中,我们会再次用到上一讲中的有关回调方面的代码,只是我们将它放入了main()函数,之后调用 pcap_next_ex()函数。
int pcap_next_ex ( pcap_t * p,
struct pcap_pkthdr ** pkt_header,
const u_char ** pkt_data
)
Read a packet from an interface or from an offline capture.
This function is used to retrieve the next available packet, bypassing the callback method traditionally provided by libpcap.
pcap_next_ex fills the pkt_header and pkt_data parameters (see pcap_handler()) with the pointers to the header and to the data of the next captured packet.
The return value can be:
- 1 if the packet has been read without problems
- 0 if the timeout set with pcap_open_live() has elapsed. In this case pkt_header and pkt_data don't point to a valid packet
- -1 if an error occurred
- -2 if EOF was reached reading from an offline capture
typedef void(*) pcap_handler(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
Prototype of the callback function that receives the packets.
When pcap_dispatch() or pcap_loop() are called by the user, the packets are passed to the application by means of this callback. user is a user-defined parameter that contains the state of the capture session, it corresponds to the user parameter of pcap_dispatch() and pcap_loop(). pkt_header is the header associated by the capture driver to the packet. It is NOT a protocol header. pkt_data points to the data of the packet, including the protocol headers.
#include "pcap.h"
#pragma comment(lib, "wpcap.lib")
#pragma comment(lib, "Packet.lib")
#pragma comment(lib, "wsock32.lib") #include "pcap.h" main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=;
pcap_t *adhandle;
int res;
char errbuf[PCAP_ERRBUF_SIZE];
struct tm *ltime;
char timestr[];
struct pcap_pkthdr *header;
const u_char *pkt_data;
time_t local_tv_sec; /* 获取本机设备列表 */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit();
} /* 打印列表 */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
} if(i==)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -;
} printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum); if(inum < || inum > i)
{
printf("\nInterface number out of range.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -;
} /* 跳转到已选中的适配器 */
for(d=alldevs, i=; i< inum- ;d=d->next, i++); /* 打开设备 */
if ( (adhandle= pcap_open(d->name, // 设备名
, // 要捕捉的数据包的部分
// 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
, // 读取超时时间
NULL, // 远程机器验证
errbuf // 错误缓冲池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* 释放设列表 */
pcap_freealldevs(alldevs);
return -;
} printf("\nlistening on %s...\n", d->description); /* 释放设备列表 */
pcap_freealldevs(alldevs); /* 获取数据包 */
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= ){ if(res == )
/* 超时时间到 */
continue; /* 将时间戳转换成可识别的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime); printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
} if(res == -){
printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
return -;
} return ;
}
不用回调方法捕获数据包.c
·结果:
为什么我们要用 pcap_next_ex() 代替以前的 pcap_next()? 因为 pcap_next() 有一些不好的地方。首先,它效率低下,尽管它隐藏了回调的方式,但它依然依赖于函数 pcap_dispatch()。第二,它不能检测到文件末尾这个状态(EOF),因此,如果数据包是从文件读取来的,那么它就不那么有用了。
值得注意的是, pcap_next_ex() 在成功,超时,出错或EOF的情况下,会返回不同的值。
winPcap_6_不用回调方法捕获数据包的更多相关文章
- winPcap编程之不用回调方法捕获数据包(五 转)
这一次要分析的实例程序跟上一讲非常类似(“打开适配器并捕获数据包”),略微不同的一点是本次将pcap_loop()函数替换成了pcap_next_ex()函数.本节的重点也就是说一下这两个函数之间的差 ...
- Winpcap笔记4之不用回调函数捕获数据包
函数1: pcap_next_ex(pcap_t* p, struct pcap_pkthdr** pkt_header, const u_char* ...
- winPcap_5_打开适配器并捕获数据包
知道如何获取适配器的信息了,那我们就开始一项更具意义的工作,打开适配器并捕获数据包.编写一个程序,将每一个通过适配器的数据包打印出来. 打开设备的函数是 pcap_open(). (Open a ge ...
- Winpcap笔记3之打开适配器并捕获数据包
上一讲中知道了如何获取适配的信息,这一将我们讲写一个程序蒋每一个通过适配器的数据包打印出来. 打开设备的函数是pcap_open().函数原型是 pcap_t* pcap_open(const cha ...
- winpcap使用之捕获数据包
第一种方法,调用回调函数 #include "pcap.h" /* packet handler 函数原型 */ void packet_handler(u_char *param ...
- winPcap编程之打开适配器并捕获数据包(四 转)
在贴源码之前先介绍一个将要用到的很重要的函数--pcap_open(),下面是pcap_open()在remote-ex.h中的声明: pcap_t *pcap_open(const char *so ...
- Python3+pyshark捕获数据包并保存为文件
一.直接使用wireshark捕获数据包并保存为文件 可以使用wireshark通过图形界面的操作来实现捕获数据包并保存为文件. wireshark默认捕获的数据包保存为临时文件,如果最后退出时不选择 ...
- Linux系统捕获数据包流程
Linux系统捕获数据包流程 为了提高数据包的捕获效率,瓶颈问题是一个需要非常关注的焦点.减少在捕获数据包过程中的瓶颈,就能够提高数据包捕获的整体性能.下面本文将以Linux操作系统为平台,分析捕获数 ...
- ARPSpoofing教程(三) - 捕获数据包
1: #include"pcap.h" 2: //每次捕获到数据包时,libpcap都会自动调用这个回调函数 3: void packet_handler(u_char *para ...
随机推荐
- jq:get获取json数据并以表格形式生成到页面
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- web标准(复习)--2 列布局
今天我们开始学习一列布局,包含以下几种形式: 1.一列固定宽度 2.一列固定宽度居中 3.一列自适应宽度 4.一列自适应宽度居中 5.一列二至多块布局 前一节我们回顾了xhtml基础和css基础部分, ...
- POJ3253 Haffman
POJ3253 分析: 简单的哈弗曼树的应用. AC代码: //Memory: 316K Time: 16MS #include <iostream> #include <cstri ...
- 让IE支持CSS3 Media Query实现响应式Web设计
如今的屏幕分辨率,小至320px(iPhone),大到2560px甚至更高(大显示器),变化范围极大.除了使用传统的台式机,用户会越来越多的通过手机.上网本.iPad一类的平板设备来浏览页面.这种情况 ...
- 后台获取HTMLTABLE的innerHtml
c#后台动态创建了htmltable,取其innerHtml时,会报错,因为,htmltable控件不支持该属性,要获取其innerHtml使用如下方法 HtmlTable tb=new Htm ...
- 背包问题lingo求解
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang !背包问题 题目: 8件物品 重量分别为 1,3,4,3,3,1,5,10 价值分别为 2,9 ...
- 封装JDBC事务操作,执行存储过程测试
Oracle数据库端测试环境见:http://www.cnblogs.com/yshyee/p/4392328.html package com.mw.utils; import java.sql.C ...
- documentElement vs body区别
documentElement.scrollTop------>0因为,他包含head, body body.scrollTop------------------>才是正确的 scrol ...
- 关于bootstrap--列表(ol、ul)
1.list-unstyled : 在<ol>(有序列表)</ol><ul>(无序列表)</ul>中加入class="list-styled& ...
- 第23讲 UI_布局 之相对布局
第23讲 UI_布局 之相对布局 .RelativeLayout(相对布局): RelativeLayout(相对布局)是指组件的位置总是相对兄弟组件.父容器来决定的(相对位置),如某个组件的左边右边 ...