注册 登录
编程论坛 VC++/MFC

话说大有有在MFC上用Winpcap写程序的么,我写了个不过截的都是些错误数据

关鱼 发布于 2013-05-07 20:47, 622 次点击
话说大有有在MFC上用Winpcap写程序的么,我写了个不过截的都是些错误数据

我用的其中一个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;
}
3 回复
#2
yuccn2013-05-08 20:39
Winpcap 是开源的吧
好像有专门的说明文档,你去翻下咯

没有在windows下用过winpcap
#3
yuccn2013-05-08 20:41
《小小黑客之路》第7章 有专门的说明,去下载一部电子书来参考下吧
#4
邓士林2013-05-08 20:57
学习下,就看到这个winpcap(windows packet capture)是windows平台下一个免费,公共的网络访问系统。开发winpcap这个项目的目的在于为win32应用程序提供访问网络底层的能力。
1