64,282
社区成员
发帖
与我相关
我的任务
分享
static UINT ThreadFunc(LPVOID pParam)
{
struct pcap_pkthdr *header;
CNetstateDlg *Test=static_cast<CNetstateDlg *>(pParam);
pcap_t *adhandle;
const u_char *pkt_data;
int res=0;
int dataLen;
if ((adhandle=Test->d->openAndComplie(Test->packet_filter,Test->d->device))==NULL)
return 0;
// pcap_loop(adhandle, 0,packet_handler, NULL);
//}
// d->FreeDevice();
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0)
{
if(res == 0)
continue; // 超时时间到
ip_header *ih;
udp_header *uh;
tcp_head *th;
u_int ip_len; //tsport代表tcp的源端口,tdport代表tcp的目的端口
u_short usport,udport,tsport,tdport; //u-代表udp,t代表tcp,d代表目的地址,h代表源地址
//usport表示udp的源端口,udport代表udp的目的端口
int procID; //协议的ID号码
CString SourceIP,DesIp,SourcePort,
DesPort,Length;
// 获得IP数据包头部的位置
ih = (ip_header *) (pkt_data +
14); //以太网头部长度
// 获得UDP首部的位置
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *) ((u_char*)ih + ip_len);
th = (tcp_head *) ((u_char*)ih + ip_len);
//将网络字节序列转换成主机字节序列
usport = ntohs( uh->sport );//udp的源端口
udport = ntohs( uh->dport );//udp的目的端口
tsport = ntohs( th->SourPort );//tcp的源端口
tdport = ntohs( th->DestPort );//tcp的目的端口
procID = ntohs( ih->proto );
// switch(procID) {
// case TCP:
{
dataLen=header->len;
SourceIP.Format("%d.%d.%d.%d", ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4 );
DesIp.Format("%d.%d.%d.%d", ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4);
SourcePort.Format("%d",tsport);
DesPort.Format("%d",tdport);
Length.Format("%d",dataLen);
Test->AddData("TCP",SourceIP,SourcePort,DesIp,DesPort,Length);
// break;
// }
}
}
if(res == -1)
exit(0);
return 0;
}
AfxBeginThread( ThreadFunc , (LPVOID)this );
这个是线程函数pcap_t* WinpCat::openAndComplie(CString packet_filter,pcap_if_t *devices)//返回一个打开并且设置好过滤规则的pcap_t
{
pcap_t *adhandle;
pcap_if_t *tempDevc;
struct bpf_program fcode;
tempDevc=devices;
while(tempDevc->description!=::device_name) //跳到所选择的设备
tempDevc=tempDevc->next;
char errbuf[PCAP_ERRBUF_SIZE];
if ( (adhandle= pcap_open(tempDevc->name, // 设备名
65536, // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
1000, // 读取超时时间
NULL, // 远程机器验证
errbuf // 错误缓冲池
) ) == NULL)
{
MessageBox(NULL,errbuf,"警告",MB_OK);
exit(0);
}
// adhandle=device->addresses->next;
if(pcap_datalink(adhandle) != DLT_EN10MB) //检查数据链路层,为了简单,我们只考虑以太网
{
// fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
MessageBox(NULL,"非以太网","警告",MB_OK);
/* 释放设备列表 */
pcap_freealldevs(tempDevc);
exit(0);
}
// des->addresses=des->addresses->next;
if(tempDevc->addresses != NULL)
/* 获得接口第一个地址的掩码 */
netmask=((struct sockaddr_in *)(tempDevc->addresses->netmask))->sin_addr.S_un.S_addr;
else
// 如果接口没有地址,那么我们假设一个C类的掩码 */
netmask=0xffffff;
//编译过滤器
CString temp=packet_filter;
char *method=(LPSTR)(LPCTSTR)temp;
if (pcap_compile(adhandle, &fcode, method, 1, netmask) <0 )//将CString强制转换成了char*不知会不会报错
{
// fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* 释放设备列表 */
pcap_freealldevs(tempDevc);
MessageBox(NULL,"compile the packet 失败","警告",MB_OK);
exit(0);
}
//设置过滤器
if (pcap_setfilter(adhandle, &fcode)<0)
{
// fprintf(stderr,"\nError setting the filter.\n");
/* 释放设备列表 */
pcap_freealldevs(tempDevc);
MessageBox(NULL,"set the packet 失败","警告",MB_OK);
exit(0);
}
// printf("\nlistening on %s...\n", d->description);
/* 释放设备列表 */
// pcap_freealldevs(des);2013年5月7日 14:43:43刚去掉的
return adhandle; //要加判空操作后面dazo.
}