![]() |
#2
yuccn2013-05-08 20:39
|
我用的其中一个pcap_next_ex()来捕获数据,新建了一个线程将捕获到的数据输出到扩展组合框中,但是
分析出的各种数据都不对头...
真不知道怎么办了.
就贴些线程的代码吧,里面有些错误,但是不是主要,想先把正确的数据截出来再改

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;
}
{
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;
}