我用MFC做一个网络测试程序,主窗口用来获取device、ip、mac,然后分别用两个线程用Libnet和Winpcap来获取对方IP和MAC,再转到另一个对话框,但是在这个对话框中,新建的对ICMP的收包线程在收到回包后就整个死掉了,提示File:wincore.cpp Line:879的错误,请大虾帮忙!!
问题代码如下:
void op_icmp_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
{
int i=0;
struct icmp_header *icmp_protocol; // ICMP协议变量 icmp_protocol = (struct icmp_header*)(packet_content + 14+20); // 获得ICMP协议内容 // 获得ICMP类型
switch (icmp_protocol->icmp_type)
{
case 8:
i=1;
break;
case 0:
i=2;
break;
default:
i=0;
break;
} if(i==2)
{
opdlg->settim("ok");
}}
DWORD WINAPI op_send_icmp(LPVOID lpparameter)
{ sendpack.sendicmp(openapp->pdevice,openapp->pdestination_ip_str,openapp->psource_ip_str);
Sleep(200);
op_bExit=true;
TerminateThread(op_thget_icmp,0);
CloseHandle(op_thget_icmp);
return 0;
}
DWORD WINAPI op_get_icmp(LPVOID lpparameter)
{
char* s="icmp"; pcap_t *op_pcap_handle; // Winpcap句柄
char op_error_content[PCAP_ERRBUF_SIZE]; // 存储错误信息
char *op_net_interface = openapp->pdevice;
struct bpf_program op_bpf_filter; // BPF过滤规则
bpf_u_int32 op_net_mask; // 掩码
bpf_u_int32 op_net_ip; // 网路地址
pcap_lookupnet(op_net_interface, &op_net_ip, &op_net_mask, op_error_content); // 获得网络地址和掩码地址
op_pcap_handle = pcap_open_live(op_net_interface, BUFSIZ, 1, 100, op_error_content); // 打开网路接口
char *op_bpf_filter_string = s; // 过滤规则字符串
pcap_compile(op_pcap_handle, &op_bpf_filter, op_bpf_filter_string, 0, op_net_ip); // 编译BPF过滤规则
pcap_setfilter(op_pcap_handle, &op_bpf_filter); // 设置过滤规则
if (pcap_datalink(op_pcap_handle) != DLT_EN10MB)
return -1;
while(1)
{
pcap_dispatch(op_pcap_handle,-1, op_icmp_packet_callback, NULL);}
pcap_close(op_pcap_handle);
return 0;
}
void opentest::Onstart()
{
DWORD thid; // TODO: Add your control notification handler code here
switch(m_type)
{
case 0:
op_thget_icmp=CreateThread(NULL,0,op_get_icmp,NULL,0,&thid);
op_thsend_icmp=CreateThread(NULL,0,op_send_icmp,NULL,0,&thid);
break;
case 1:
op_thget_ip=CreateThread(NULL,0,op_get_ip,NULL,0,&thid);
op_thsend_ip=CreateThread(NULL,0,op_send_ip,NULL,0,&thid);
break;
default:
MessageBox("请选择类型!");
break;
}
}
问题代码如下:
void op_icmp_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
{
int i=0;
struct icmp_header *icmp_protocol; // ICMP协议变量 icmp_protocol = (struct icmp_header*)(packet_content + 14+20); // 获得ICMP协议内容 // 获得ICMP类型
switch (icmp_protocol->icmp_type)
{
case 8:
i=1;
break;
case 0:
i=2;
break;
default:
i=0;
break;
} if(i==2)
{
opdlg->settim("ok");
}}
DWORD WINAPI op_send_icmp(LPVOID lpparameter)
{ sendpack.sendicmp(openapp->pdevice,openapp->pdestination_ip_str,openapp->psource_ip_str);
Sleep(200);
op_bExit=true;
TerminateThread(op_thget_icmp,0);
CloseHandle(op_thget_icmp);
return 0;
}
DWORD WINAPI op_get_icmp(LPVOID lpparameter)
{
char* s="icmp"; pcap_t *op_pcap_handle; // Winpcap句柄
char op_error_content[PCAP_ERRBUF_SIZE]; // 存储错误信息
char *op_net_interface = openapp->pdevice;
struct bpf_program op_bpf_filter; // BPF过滤规则
bpf_u_int32 op_net_mask; // 掩码
bpf_u_int32 op_net_ip; // 网路地址
pcap_lookupnet(op_net_interface, &op_net_ip, &op_net_mask, op_error_content); // 获得网络地址和掩码地址
op_pcap_handle = pcap_open_live(op_net_interface, BUFSIZ, 1, 100, op_error_content); // 打开网路接口
char *op_bpf_filter_string = s; // 过滤规则字符串
pcap_compile(op_pcap_handle, &op_bpf_filter, op_bpf_filter_string, 0, op_net_ip); // 编译BPF过滤规则
pcap_setfilter(op_pcap_handle, &op_bpf_filter); // 设置过滤规则
if (pcap_datalink(op_pcap_handle) != DLT_EN10MB)
return -1;
while(1)
{
pcap_dispatch(op_pcap_handle,-1, op_icmp_packet_callback, NULL);}
pcap_close(op_pcap_handle);
return 0;
}
void opentest::Onstart()
{
DWORD thid; // TODO: Add your control notification handler code here
switch(m_type)
{
case 0:
op_thget_icmp=CreateThread(NULL,0,op_get_icmp,NULL,0,&thid);
op_thsend_icmp=CreateThread(NULL,0,op_send_icmp,NULL,0,&thid);
break;
case 1:
op_thget_ip=CreateThread(NULL,0,op_get_ip,NULL,0,&thid);
op_thsend_ip=CreateThread(NULL,0,op_send_ip,NULL,0,&thid);
break;
default:
MessageBox("请选择类型!");
break;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货