﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-saga's blog-随笔分类-Winpcap</title><link>http://www.cppblog.com/saga/category/1425.html</link><description>学习，钻研，思考，创新！
我才刚上路，有好多的期待！</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 09:18:35 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 09:18:35 GMT</pubDate><ttl>60</ttl><item><title>[原创]用Winpcap函数转发包需要留心的一点</title><link>http://www.cppblog.com/saga/archive/2006/04/16/5654.html</link><dc:creator>saga.constantine</dc:creator><author>saga.constantine</author><pubDate>Sun, 16 Apr 2006 09:13:00 GMT</pubDate><guid>http://www.cppblog.com/saga/archive/2006/04/16/5654.html</guid><wfw:comment>http://www.cppblog.com/saga/comments/5654.html</wfw:comment><comments>http://www.cppblog.com/saga/archive/2006/04/16/5654.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/saga/comments/commentRss/5654.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/saga/services/trackbacks/5654.html</trackback:ping><description><![CDATA[        <img height="20" src="http://www.cppblog.com/Emoticons/QQ/31.gif" width="20" border="0" />最近自己因为实际需要,用winpcap开发一个程序里面有需要转发包.自己就写了一个,发现转发时延&gt;500ms.也确实把自己吓到了.便把转发的那段代码开始精减,大致如下 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 102.24%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 427px; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">((j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">pcap_next_ex(slecadopt,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">pkt_header,(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> u_char</span><span style="COLOR: #000000">**</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">pkt_data))</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br /><img id="Codehighlighter1_76_685_Open_Image" onclick="this.style.display='none'; Codehighlighter1_76_685_Open_Text.style.display='none'; Codehighlighter1_76_685_Closed_Image.style.display='inline'; Codehighlighter1_76_685_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_76_685_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_76_685_Closed_Text.style.display='none'; Codehighlighter1_76_685_Open_Image.style.display='inline'; Codehighlighter1_76_685_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span id="Codehighlighter1_76_685_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_76_685_Open_Text"><span style="COLOR: #000000">{   <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />     </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br /><img id="Codehighlighter1_100_122_Open_Image" onclick="this.style.display='none'; Codehighlighter1_100_122_Open_Text.style.display='none'; Codehighlighter1_100_122_Closed_Image.style.display='inline'; Codehighlighter1_100_122_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_100_122_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_100_122_Closed_Text.style.display='none'; Codehighlighter1_100_122_Open_Image.style.display='inline'; Codehighlighter1_100_122_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />     </span><span id="Codehighlighter1_100_122_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_100_122_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />     </span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />     }</span></span><span style="COLOR: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />     </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(unsigned </span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)(pkt_data</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">htons(ETH_IP)</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">(memcmp(pkt_data</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,myip</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">mac,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">))</span><span style="COLOR: #000000">&amp;&amp;*</span><span style="COLOR: #000000">(unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)(pkt_data</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">30</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">myip</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">ip</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">(memcmp(pkt_data,bcast,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)))<br /><img id="Codehighlighter1_303_683_Open_Image" onclick="this.style.display='none'; Codehighlighter1_303_683_Open_Text.style.display='none'; Codehighlighter1_303_683_Closed_Image.style.display='inline'; Codehighlighter1_303_683_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_303_683_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_303_683_Closed_Text.style.display='none'; Codehighlighter1_303_683_Open_Image.style.display='inline'; Codehighlighter1_303_683_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />     </span><span id="Codehighlighter1_303_683_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_303_683_Open_Text"><span style="COLOR: #000000">{ <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />     </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(z</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Pipmachead;z</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">NULL;z</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next)<br /><img id="Codehighlighter1_346_676_Open_Image" onclick="this.style.display='none'; Codehighlighter1_346_676_Open_Text.style.display='none'; Codehighlighter1_346_676_Closed_Image.style.display='inline'; Codehighlighter1_346_676_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_346_676_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_346_676_Closed_Text.style.display='none'; Codehighlighter1_346_676_Open_Image.style.display='inline'; Codehighlighter1_346_676_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />     </span><span id="Codehighlighter1_346_676_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_346_676_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />         </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)(pkt_data</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">30</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">ip)<br /><img id="Codehighlighter1_410_655_Open_Image" onclick="this.style.display='none'; Codehighlighter1_410_655_Open_Text.style.display='none'; Codehighlighter1_410_655_Closed_Image.style.display='inline'; Codehighlighter1_410_655_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_410_655_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_410_655_Closed_Text.style.display='none'; Codehighlighter1_410_655_Open_Image.style.display='inline'; Codehighlighter1_410_655_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />             </span><span id="Codehighlighter1_410_655_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_410_655_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />                        memcpy(cmac</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">mac,z</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">mac,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />                        memcpy(cmac</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">mac</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,myip</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">mac,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />                        memcpy(sendbuf,pkt_data,pkt_header</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">caplen);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />                        memcpy(sendbuf,cmac,</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />                       pcap_sendpacket(slecadopt,sendbuf,pkt_header</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">caplen);      <br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />         }</span></span><span style="COLOR: #000000"> <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />         </span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />     }</span></span><span style="COLOR: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />     }</span></span><span style="COLOR: #000000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>        过程很简单,pcap_next_ex()收包,在做判断处理,然后用pcap_sendpacket()发包.<img height="20" src="http://www.cppblog.com/Emoticons/QQ/icon18.gif" width="25" border="0" />怎么想都不会有那么大的时延.很不解就搜索了下关于pcap_sendpacket()发包效能的网页,其中有一个<a href="http://community.csdn.net/Expert/TopicView3.asp?id=4153633">http://community.csdn.net/Expert/TopicView3.asp?id=4153633</a>来里面有人说 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">我用winpcap做个一个初步的试验，我得机器是奔4 </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">。</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">，内存512，我一个线程接包，一个线程收包，用动态连表做缓存，一秒钟转发1－2个包没有多大问题，要是再多了，就会又丢包，转发上千万包要丢几百个。</span></div>还有人说 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">winpcap的sendpacket超级慢，而且延迟很大。<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />我试图实现一个bridge，即一个端口进入的包不经修改从一个端口出，反之亦然。结果ping的结果显示延迟为数百毫秒级别，个别时候甚至到了1秒。运行其它应用更是不可忍受。<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />我想使用sendQuene，结果sendQuene的具体用法只见过预先准备好所有的包然后整体发送的，没见过随着包来随着发送的例子。或许对于我的bridge，winpcap是一个错误的选择？又或者大家有什么新鲜的思路？愿意听大家说说。</span></div>        使我也开始怀疑pcap_sendpacket()了.难道真实pcap_sendpacket()效率底.但是我还是不相信.堂堂winpcap怎么可能怎么慢.使我发现破绽的使贴子里最后一的人的说法: 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">我也写了个类似的程序，延时稳定在20ms左右，带宽可以达到10M<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />我发现一个有趣的事情，即我在运行brigde的服务器上，运行一个fluke的抓包工具，居然发现运行后，通过bridge后的时延居然只有1ms左右了，停止抓包工具后，延时又回到20ms左右，试了多次情况都一样<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />打开网卡的句柄是按下面的参数执行的<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />pcap_open(d</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">name, </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> name of the device </span><span style="COLOR: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">65536</span><span style="COLOR: #000000">, </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> portion of the packet to capture.</span><span style="COLOR: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">PCAP_OPENFLAG_PROMISCUOUS, </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> promiscuous mode </span><span style="COLOR: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">, </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> read timeout </span><span style="COLOR: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">NULL, </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> remote authentication </span><span style="COLOR: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">errbuf </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> error buffer </span><span style="COLOR: #008000"><br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">); <br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />对数据报的捕获是用pcap_loop函数，我试过将pcap_open的read timeout参数设置为</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,即读取立即返回，但这样对系统资源占用太大，cpu利用率增加到100％，此时bridge的延时就很不稳定了，从1ms到1s都有。</span></div>       他在里面特意提到了pcap_open()的read timeout参数,我回想起我的设的1000ms,也就是一秒.嫌疑很大.(注:我用的是pcap_open_live())<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">pcap_open_live(d</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">name, </span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">, error)</span></div>然后就去查Winpcap的官方资料<a href="http://www.winpcap.org/docs/docs31/html/group__wpcapfunc.html">http://www.winpcap.org/docs/docs31/html/group__wpcapfunc.html</a><br />里面说的很清楚: 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">to_ms specifies the read timeout in milliseconds. The read timeout is used to arrange that the read not necessarily </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> immediately when a packet is seen, but that it wait </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operation. </span></div><p>     当已经发现包之后还会等多一些的包一起返回,等待时间就是read timeout.明白了,我把read timeout设为1以后时延降到了&lt;10ms.<br /><br />     其中还需要提醒的是用Ethereal等一些抓包工具看到收包到转发时延很大时就以为是pcap_next_ex()和pcap_sendpacket()函数之间的处理造成了时延.这里容易给我们产生一个假相.以为网卡收到包的时刻就是转发程序就要开始处理.实际上pcap_next_ex()还在等待更多的包一起返回.<br />     所以<font style="BACKGROUND-COLOR: #ffffff" color="#ff1493">事发的原因离事发现场比较远</font>.在pcap_open()那.程序就就是怎么千丝万缕的关联着.<br />     大家一定要多怀疑自己,别去怪罪工具.winpcap很无辜的.嘻嘻!!<img height="20" src="http://www.cppblog.com/Emoticons/QQ/14.gif" width="20" border="0" />肚子饿了去吃饭<img height="20" src="http://www.cppblog.com/Emoticons/QQ/32.gif" width="20" border="0" />!!</p><p>                                                                     -----saga.constantine</p><img src ="http://www.cppblog.com/saga/aggbug/5654.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/saga/" target="_blank">saga.constantine</a> 2006-04-16 17:13 <a href="http://www.cppblog.com/saga/archive/2006/04/16/5654.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[原创]用winpcap获取本地mac的一段代码!</title><link>http://www.cppblog.com/saga/archive/2006/04/13/5468.html</link><dc:creator>saga.constantine</dc:creator><author>saga.constantine</author><pubDate>Thu, 13 Apr 2006 07:02:00 GMT</pubDate><guid>http://www.cppblog.com/saga/archive/2006/04/13/5468.html</guid><wfw:comment>http://www.cppblog.com/saga/comments/5468.html</wfw:comment><comments>http://www.cppblog.com/saga/archive/2006/04/13/5468.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/saga/comments/commentRss/5468.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/saga/services/trackbacks/5468.html</trackback:ping><description><![CDATA[
		<p align="justify">
				<img height="19" src="http://www.cppblog.com/Emoticons/coffee.gif" width="19" border="0" />最近用winpcap编程发现winpcap不能直接的提取本地网卡的mac地址.<br />     于是通过向自己发arp请求来获得本地的mac.自己写了段代码,如下:</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> getmmac()<br />{   <br />    unsigned </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000">   sendbuf[</span>
				<span style="COLOR: #000000">42</span>
				<span style="COLOR: #000000">];<br />    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000">    i</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">7</span>
				<span style="COLOR: #000000">,k;<br />    ETHDR  eth;<br />    ARPHDR arp;<br />    </span>
				<span style="COLOR: #0000ff">struct</span>
				<span style="COLOR: #000000"> pcap_pkthdr </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">  pkt_header;<br />    u_char </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000"> pkt_data; <br /><br />    </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(k</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;k</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">6</span>
				<span style="COLOR: #000000">;k</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br />    {<br />        eth.eh_dst[k]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0xff</span>
				<span style="COLOR: #000000">;<br />        eth.eh_src[k]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0x0f</span>
				<span style="COLOR: #000000">;<br />        arp.arp_sha[k]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0x0f</span>
				<span style="COLOR: #000000">;<br />        arp.arp_tha[k]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0x00</span>
				<span style="COLOR: #000000">;<br />    }<br />    eth.eh_type</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">htons(ETH_ARP);<br />    arp.arp_hdr</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">htons(ARP_HARDWARE);<br />    arp.arp_pro</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">htons(ETH_IP);<br />    arp.arp_hln</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">6</span>
				<span style="COLOR: #000000">;<br />    arp.arp_pln</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">4</span>
				<span style="COLOR: #000000">;<br />    arp.arp_opt</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">htons(ARP_REQUEST);<br />    arp.arp_tpa</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">myip</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">ip;<br />    arp.arp_spa</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">inet_addr(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">127.0.0.2</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br /><br />    memset(sendbuf,</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #0000ff">sizeof</span>
				<span style="COLOR: #000000">(sendbuf));<br />    memcpy(sendbuf,</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000">eth,</span>
				<span style="COLOR: #0000ff">sizeof</span>
				<span style="COLOR: #000000">(eth));<br />    memcpy(sendbuf</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #0000ff">sizeof</span>
				<span style="COLOR: #000000">(eth),</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000">arp,</span>
				<span style="COLOR: #0000ff">sizeof</span>
				<span style="COLOR: #000000">(arp));<br /><br />    </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000">(pcap_sendpacket(slecadopt,sendbuf,</span>
				<span style="COLOR: #000000">42</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">)<br />    {<br />        printf(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">PacketSend succeed\n\n</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br />    }<br />    </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000">
						<br />    {<br />        printf(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">PacketSendPacket in getmine Error: %d\n</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">,GetLastError());<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;<br />    }<br />    <br />    </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000">((k</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">pcap_next_ex(slecadopt,</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000">pkt_header,(</span>
				<span style="COLOR: #0000ff">const</span>
				<span style="COLOR: #000000"> u_char</span>
				<span style="COLOR: #000000">**</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000">pkt_data))</span>
				<span style="COLOR: #000000">&gt;=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">)<br />    {       <br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">(unsigned </span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">)(pkt_data</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">12</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000">htons(ETH_ARP)</span>
				<span style="COLOR: #000000">&amp;&amp;*</span>
				<span style="COLOR: #000000">(unsigned </span>
				<span style="COLOR: #0000ff">short</span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">)(pkt_data</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">20</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000">htons(ARP_REPLY)</span>
				<span style="COLOR: #000000">&amp;&amp;*</span>
				<span style="COLOR: #000000">(unsigned </span>
				<span style="COLOR: #0000ff">long</span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">)(pkt_data</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">38</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000">inet_addr(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">127.0.0.2</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">))<br />        {<br />            <br />            </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(i</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;i</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">6</span>
				<span style="COLOR: #000000">;i</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br />            {<br />                myip</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">mac[i]</span>
				<span style="COLOR: #000000">=*</span>
				<span style="COLOR: #000000">(unsigned </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">)(pkt_data</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">22</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">i);<br />            }<br />                </span>
				<span style="COLOR: #0000ff">break</span>
				<span style="COLOR: #000000">;<br />        }<br />    }<br />    </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000">(i</span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000">6</span>
				<span style="COLOR: #000000">)<br />    {<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br />    }<br />    </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000">
						<br />    {<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;<br />    }<br />}</span>
		</div>
		<br />写的是一个函数,其中arp请求的发送方地址填的是127.0.0.2.这个都没什么关系.<br />当然提取本地的mac还可以用api直接获取.但是不知道用pcap还有别的好方法.高手指教.谢谢<img src ="http://www.cppblog.com/saga/aggbug/5468.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/saga/" target="_blank">saga.constantine</a> 2006-04-13 15:02 <a href="http://www.cppblog.com/saga/archive/2006/04/13/5468.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>