<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Webdings;
panose-1:5 3 1 2 1 5 9 6 7 3;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:258677955;
mso-list-type:hybrid;
mso-list-template-ids:563381868 67895311 67895321 67895323 67895311 67895321 67895323 67895311 67895321 67895323;}
@list l0:level1
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=FR link=blue vlink=purple><div class=WordSection1><p class=MsoNormal> Good afternoon,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span lang=EN-US>I have write a code with WinPcap 4.1.3.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>The code is simple and uses :<o:p></o:p></span></p><p class=MsoListParagraph style='text-indent:-18.0pt;mso-list:l0 level1 lfo1'><![if !supportLists]><span lang=EN-US><span style='mso-list:Ignore'>1.<span style='font:7.0pt "Times New Roman"'> </span></span></span><![endif]><span lang=EN-US>pcap_open_live to open the device<o:p></o:p></span></p><p class=MsoListParagraph style='text-indent:-18.0pt;mso-list:l0 level1 lfo1'><![if !supportLists]><span lang=EN-US><span style='mso-list:Ignore'>2.<span style='font:7.0pt "Times New Roman"'> </span></span></span><![endif]><span lang=EN-US>a thread for calling pcap_loop with a packet handler<o:p></o:p></span></p><p class=MsoListParagraph style='text-indent:-18.0pt;mso-list:l0 level1 lfo1'><![if !supportLists]><span lang=EN-US><span style='mso-list:Ignore'>3.<span style='font:7.0pt "Times New Roman"'> </span></span></span><![endif]><span lang=EN-US>pcap_sendpacket to send packet to the network<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>No problem to send the packet.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>The only problem is the time needed to read a packet. It takes <b>1ms each time</b>, as the read timeout specified in pcap_open_live.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>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.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Is there a special configuration to call the packet handler function directly when a packet is received without wait the timeout ?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Below is the source code :<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> …<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> /* Open the adapter */<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> d = alldevs;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> if ((adhandle = pcap_open_live(d->name, // name of the device<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> 65536, // portion of the packet to capture.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> // 65536 grants that the whole packet will be captured on all the MACs.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> 1, // promiscuous mode (nonzero means promiscuous)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> 1, // read timeout (1 ms)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> errbuf // error buffer<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> )) == NULL)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> {<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> /* error management */<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> }<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> /* Free the device list */<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> pcap_freealldevs(alldevs);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US> /* Create thread to capture the live packet */<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> hThread = CreateThread( NULL, // default security attributes<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> 0, // use default stack size<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> pcapLoopThread, // thread function name<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> NULL, // argument to thread function<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> 0, // use default creation flags<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> &dwThreadId); // returns the thread identifier<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> …<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Below is the packet handler function :<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US>DWORD WINAPI pcapLoopThread( LPVOID lpParam )<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US>{<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> /* start the capture */<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> pcap_loop(adhandle, 0, packet_handler, NULL);<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> return 0;<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US>}<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US>/* Callback function invoked by libpcap for every incoming packet */<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US>void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'>{<o:p></o:p></p><p class=MsoNormal style='margin-left:35.4pt'> /* Filtrer les adresses MAC reçues = 0x32 0x31 0x30 0x33 0x34 0x35 */<o:p></o:p></p><p class=MsoNormal style='margin-left:35.4pt'> <span lang=EN-US>if (memcmp((void*)&pkt_data[6],(void*)"210345",6)==0)<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> {<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> /* Sauvegarde du packet reçu */<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> memcpy(DataPacket,pkt_data,header->len);<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> if (! SetEvent(packetReceived) )<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> {<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> return;<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> }<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> nbByteReceived = header->len;<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US> }<o:p></o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-US>}<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Best regards / Cordialement,</span><br><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><br>Jean-Loïc MARTIN<br>Software engineer/ Ingénieur développement logiciel</span><br><b><span style='font-size:7.5pt;font-family:"Arial","sans-serif"'><br>Meggitt Sensing Systems "MSS"<o:p></o:p></span></b></p><p class=MsoNormal><b><span style='font-size:7.5pt;font-family:"Arial","sans-serif"'>196 rue Louis Rustin<o:p></o:p></span></b></p><p class=MsoNormal><b><span style='font-size:7.5pt;font-family:"Arial","sans-serif"'>BP63108</span></b><span style='font-size:7.5pt;font-family:"Arial","sans-serif"'><br>Archamps Technopole<br>74166 Archamps<br>FRANCE</span><br><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><br>(tel) +33 (0) 450 95 43 55 Poste : 519<br><a href="mailto:jean-loic.martin@fr.meggitt.com"><span style='color:blue'>jean-loic.martin@fr.meggitt.com</span></a></span><br><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><br><a href="http://www.meggitt.com"><span style='color:blue'>www.meggitt.com</span></a><br><a href="http://www.sensorex.fr"><span style='color:blue'>www.sensorex.fr</span></a></span><span style='font-size:18.0pt;font-family:Webdings;color:#00A000'><br>P</span> <span style='font-size:7.5pt;font-family:"Verdana","sans-serif";color:#00A000'>N'imprimez que si necessaire / Print only if necessary</span><o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>