简体   繁体   English

为什么pcap_next_ex在Window上始终返回0?

[英]Why pcap_next_ex keeps returning 0 on Window?

I'm using winpcap to implement a sniffer, I keep polling the following readpacket function but randomly (some times after 1 minute some times after 5 hours) my program get stuck in this procedure's while since rc keep being equal 0. Why is this happening? 我正在使用winpcap来实现嗅探器,我一直在轮询以下readpacket函数,但由于rc始终等于0,所以我的程序在此过程中被卡住了(随机地(在1分钟后的某些时间,有时在5小时后的某些时间))。 ? What am I missing? 我想念什么?

Relevant configuration: 相关配置:

    pcap_set_snaplen (_pPCapHandle, 65535);
    pcap_set_promisc (_pPCapHandle, 1);
    pcap_set_timeout (_pPCapHandle, msPacketValid); //msPacketValid = 0

int PCapInterface::readPacket (uint8 *pui8Buf, uint16 ui16BufSize)
{
    struct pcap_pkthdr *pPacketHeader;
    const u_char *pPacketData;
    while (true) {
        int rc = pcap_next_ex (_pPCapHandle, &pPacketHeader, &pPacketData);

        if (rc > 0) {

            uint32 ui32PacketSize = ui16BufSize < pPacketHeader->caplen ? ui16BufSize : pPacketHeader->caplen;
            memcpy (pui8Buf, pPacketData, ui32PacketSize);

            return ui32PacketSize;
        }
        else if (rc == 0) {
            printf("read rc = 0...\n");
            if (_bIsTerminationRequested) {

                return 0;
            }
        }
        else if (rc < 0) 
        {
            checkAndLogMsg ("PCapInterface::readPacket", Logger::L_MildError, "pcap_next_ex() returned %d\n", rc);
            return -1;
        }
    }
}

I discovered that the reason why the function was not working was because the polling function was too slow. 我发现该功能不起作用的原因是因为轮询功能太慢。 Now that I'm using a thread just for the reading phase the problem is not present anymore. 现在,我仅在读取阶段使用了线程,就不再存在问题了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM