- 論壇徽章:
- 0
|
void *packets_analyze(void *param)
{
unsigned long packets_captured_num=0;
struct config_result *pcfg_res=(struct config_result*)param;
struct test_config *pcfg=pcfg_res->tc;
struct test_result *presult=pcfg_res->tr;
pcap_t *pcap_handle;
char error_content[PCAP_ERRBUF_SIZE];
char *net_interface;
struct bpf_program bpf_filter;
char bpf_filter_string[100] = "icmp and src 192.168.10.91";
bpf_u_int32 net_mask;
bpf_u_int32 net_ip;
struct pcap_pkthdr *pkt_header;
const u_char *pkt_data;
printf("\npacket_analyze thread starts");
net_interface = pcap_lookupdev(error_content);
pcap_lookupnet(net_interface, &net_ip,&net_mask, error_content);
pcap_handle = pcap_open_live(net_interface, BUFSIZ, 1, 6000, error_content);
pcap_compile(pcap_handle,&bpf_filter, bpf_filter_string, 0, net_ip);
pcap_setfilter(pcap_handle, &bpf_filter);
int b;
while((b=pcap_next_ex(pcap_handle,&pkt_header,&pkt_data))==1)
{
ethernet_protocol_packet_callback((u_char*)(&packets_captured_num),pkt_header,pkt_data);
}
printf("\npackets_analyze thread finished!The result is:");
switch(pcfg->t_type)
{
case 1:
printf("\npackets sent:%ld",packets_send_num);
printf("\nicmp packets captured:%ld",packets_captured_num);
presult->frame_loss_rate=1-(double)packets_captured_num/packets_send_num;
printf("\nframe_loss_rate=%.2f%\n",presult->frame_loss_rate*100);
break;
case 2:
presult->latency=(double)timedelay_sum/packets_send_num;
printf("\nlatency=%lfms",presult->latency);
break;
case 0:
break;
case 3:
break;
case 4:
break;
case 5:
default:
break;
}
pcap_close(pcap_handle);
return 0;
}
以上是一個捕包的線程函數(shù),test_config,test_result分別是存配置信息和結(jié)果的結(jié)構(gòu)體。
昨天我運行的時候,還正常,今天就不正常了,一直捕不到包。 |
|