求libpcap抓包c语言程序,能把数据包内容显示出来的

2022-03-16 科技 54阅读

#include"headFile.h"
voidPcallBack(unsignedchar*argument,
conststructpcap_pkthdr*pcap_header,
constunsignedchar*packet_content);
intprintPkt(char*device,unsignedchar*pname,intflag,intflt,char*filterStr){
char*ebuf;
pcap_t*t;
bpf_u_int32net;//所在IP地址
bpf_u_int32mask;//所在网络的掩码
pcap_lookupnet(device,&net,&mask,ebuf);//获取指定设备的信息
longintto_ms=0;
t=pcap_open_live(device,snaplen,flag,to_ms,ebuf);
if(NULL==t){
printf("pcap_open_liveiserror:%s\n",ebuf);
return-1;
}
if(1==flt){
structbpf_programfilter;
if(-1==pcap_compile(t,&filter,filterStr,0,net)){
printf("pcap_compileiserror\n");
return-1;
}
if(-1==pcap_setfilter(t,&filter)){
printf("pcap_setfilteriserror\n");
return-1;
}
}
if(-1==pcap_loop(t,-1,PcallBack,pname)){
ebuf=pcap_geterr(t);
printf("pcap_loopiserror:%s\n",ebuf);
return-1;
}
pcap_close(t);
return0;
}
voidPcallBack(unsignedchar*argument,
conststructpcap_pkthdr*pcap_header,
constunsignedchar*packet_content){
inti=0;
printf("packet_len=%d",pcap_header->len);
for(i=0;ilen;i++){
if(i%10==0)
printf("\n%s:",argument);
if((int)packet_content[i]<16)
printf("0%x",packet_content[i]);
else
printf("%x",packet_content[i]);
}
printf("\n\n");
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com