[Winpcap-users] pcap_loop wait

MARTIN, Jean-loic (FR - SX) jean-loic.martin at fr.meggitt.com
Fri Feb 14 12:42:06 UTC 2014


Good afternoon,

I have write a code with WinPcap 4.1.3.
The code is simple and uses :

1.       pcap_open_live to open the device

2.       a thread for calling pcap_loop with a packet handler

3.       pcap_sendpacket to send packet to the network

No problem to send the packet.
The only problem is the time needed to read a packet. It takes 1ms each time, as the read timeout specified in pcap_open_live.
I need to suppress this 1ms delay. Because I have a lot of packet to read. So this 1ms cause a large amount of time in final.

Is there a special configuration to call the packet handler function directly when a packet is received without wait the timeout ?

Below is the source code :
        ...
    /* Open the adapter */
    d = alldevs;
    if ((adhandle = pcap_open_live(d->name,         // name of the device
                                   65536,                              // portion of the packet to capture.
                                                                               // 65536 grants that the whole packet will be captured on all the MACs.
                                   1,                                        // promiscuous mode (nonzero means promiscuous)
                                   1,                                       // read timeout (1 ms)
                                   errbuf                              // error buffer
                                   )) == NULL)
    {
                /* error management */
    }
    /* Free the device list */
    pcap_freealldevs(alldevs);

    /* Create thread to capture the live packet */
    hThread = CreateThread( NULL,                   // default security attributes
                            0,                      // use default stack size
                            pcapLoopThread,         // thread function name
                            NULL,                   // argument to thread function
                            0,                      // use default creation flags
                            &dwThreadId);           // returns the thread identifier
                ...

Below is the packet handler function :
DWORD WINAPI pcapLoopThread( LPVOID lpParam )
{
    /* start the capture */
    pcap_loop(adhandle, 0, packet_handler, NULL);
    return 0;
}

/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    /* Filtrer les adresses MAC reçues = 0x32 0x31 0x30 0x33 0x34 0x35 */
    if (memcmp((void*)&pkt_data[6],(void*)"210345",6)==0)
    {
        /* Sauvegarde du packet reçu */
        memcpy(DataPacket,pkt_data,header->len);
        if (! SetEvent(packetReceived) )
        {
            return;
        }
        nbByteReceived = header->len;
    }
}


Best regards / Cordialement,

Jean-Loïc MARTIN
Software engineer/ Ingénieur développement logiciel

Meggitt Sensing Systems "MSS"
196 rue Louis Rustin
BP63108
Archamps Technopole
74166 Archamps
FRANCE

(tel) +33 (0) 450 95 43 55 Poste : 519
jean-loic.martin at fr.meggitt.com<mailto:jean-loic.martin at fr.meggitt.com>

www.meggitt.com<http://www.meggitt.com>
www.sensorex.fr<http://www.sensorex.fr>
P N'imprimez que si necessaire / Print only if necessary

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.winpcap.org/pipermail/winpcap-users/attachments/20140214/de1c0849/attachment.html>


More information about the Winpcap-users mailing list