[Winpcap-users] sleep in pcap_next_ex api
Prasanna Kumar Nelam
prasannakumar.n at imimobile.com
Tue Jul 14 03:27:55 PDT 2009
Dear All,
I am using pcap_next_ex() API to read the packets from Network. In my
application I implemented 2 threads one for reading the packets from
network, and other for process these packets.
I am giving the code snippet, can u please suggest me whether it is correct
or not.
while(true)
{
while((res=pcap_next_ex(pcap.m_padhandle,&header,&pkt_data))>0)
{
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
The lines below are used to copy the captured pkt into our local buffer
packet_capture *packet=new packet_capture();
packet->header=(pcap_pkthdr*)malloc(sizeof(pcap_pkthdr)+1);//header;
memset((void*)packet->header,0,(sizeof(pcap_pkthdr)+1));
memcpy(packet->header,header,sizeof(pcap_pkthdr));
packet->pkt_data=(unsigned
char*)malloc((int)header->caplen+1);
memset((void*)packet->pkt_data,0,(int)header->caplen+1);
memcpy((void*)packet->pkt_data,(void*)pkt_data,(int)header->caplen);
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////
****************************************************************************
********************************************************
The following lines are used to put the packet into
queue, the packets from this C++queue are read by another thread
{
CAutoLock lock(g_syncpkt);
pkt_capture.push(packet);
printf("\n the queue size
is:%d\n",pkt_capture.size());
}
****************************************************************************
***********************************************************
Sleep(1);
}
if(res==-1)
{
printf("Error reading the packets: %s\n",
pcap_geterr(pcap.m_padhandle));
FILE *fp;
fp=fopen("exceptionhandler.log","a");
fprintf(fp,"%s-%s\n","Error reading the packets",
pcap_geterr(pcap.m_padhandle));
fclose(fp);
}
Sleep(1);
}
My doubt is it correct to write Sleep() at the
while((res=pcap_next_ex(pcap.m_padhandle,&header,&pkt_data))>0) loop, by
putting the sleep shall we loss any packet.
Before intrducing the Sleep() in the code, I am getting delayed processing
thread, and I am not able to process the packets immediately after
capturing, because of that the virtual memory is increasing.
Thanks in Advance for your advice.
Thanks and Regds,
Prasanna Kumar.N,
Software Engineer,
R&D Networks,
Mob:9000086538
=============================================
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error, please notify the sender
immediately by e-mail and delete this e-mail from your system. The sender
therefore does not accept liability for any errors or omissions in the
contents of this message, which arise as a result of e-mail transmission.
Please note that any views or opinions presented in this email are solely
those of the author and do not necessarily represent those of the company.
Finally, the recipient should check this email and any attachments for the
presence of viruses. The company accepts no liability for any damage caused
by any attachment with this email.
IMImobile, Plot No:770, Road No : 44, Jubilee Hills, Hyderabad, India,
500033. www.imimobile.com <http://www.imimobile.com/>
============================================================================
============================================================================
==
This e-mail message has been scanned for Viruses and Content and cleared by
Symantec Mail Security
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winpcap.org/pipermail/winpcap-users/attachments/20090714/97db8f6f/attachment.htm
More information about the Winpcap-users
mailing list