用matlab读pcap文件,libpcap读取本地pcap文件

假如本地已经通过抓包工具保存了pcap格式的数据包文件,通过libpcap的库也能简单地进行离线分析

pcap_t *

pcap_open_offline(const char *fname, char *errbuf)

函数打开保存的数据包文件,用于读取,返回文件描述符

fname参数指定了pcap文件名

errbuf依旧是函数出错的时候返回错误信息

这样直接分析离线数据包文件,然后通过pcap_next读取一个又一个包,最后close掉释放,可以简单如下来写

[lihui@master work]$ cat offline.c

#include

#include

#include

int main(){

char ebuf[PCAP_ERRBUF_SIZE];

char *pcap_file = “one.pcap”;

pcap_t *p = pcap_open_offline(pcap_file, ebuf);

struct pcap_pkthdr pkthdr;

while (1){

const u_char *pktStr = pcap_next(p, &pkthdr);

if (!pktStr){

printf(“Pcap file parse over !\n”);

exit(1);

}

printf(“Length: %d\n”, pkthdr.len);

}

pcap_close(p);

return 0;

}

[lihui@master work]$ gcc offline.c -lpcap

[lihui@master work]$ ./a.out

Length: 74

Length: 74

Length: 66

Length: 474

Length: 66

Length: 1480

Length: 66

Length: 1359

Length: 66

Length: 487

Length: 773

Length: 432

Length: 1480

Length: 859

Length: 66

Length: 66

Length: 66

Length: 66

Length: 66

Pcap file parse over !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值