⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 group__wpcap__tut1.html

📁 WinPcap V4.01技术手册
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>WinPcap: Obtaining the device list</title><link href="style.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.1 --><div class="tabs">  <ul>    <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>    <li><a href="modules.html"><span>Modules</span></a></li>    <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>    <li><a href="files.html"><span>Files</span></a></li>    <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>  </ul></div><h1>Obtaining the device list</h1><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr></table>Typically, the first thing that a WinPcap-based application does is get a list of attached network adapters. Both libpcap and WinPcap provide the <a class="el" href="group__wpcapfunc.html#g98f36e62c95c6ad81eaa8b2bbeb8f16e">pcap_findalldevs_ex()</a> function for this purpose: this function returns a linked list of <a class="el" href="structpcap__if.html">pcap_if</a> structures, each of which contains comprehensive information about an attached adapter. In particular, the fields <em>name</em> and <em>description</em> contain the name and a human readable description, respectively, of the corresponding device.<p>The following code retrieves the adapter list and shows it on the screen, printing an error if no adapters are found.<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "pcap.h"</span>main(){    <a class="code" href="structpcap__if.html">pcap_if_t</a> *alldevs;    <a class="code" href="structpcap__if.html">pcap_if_t</a> *d;    <span class="keywordtype">int</span> i=0;    <span class="keywordtype">char</span> errbuf[<a class="code" href="group__wpcap__def.html#gcd448353957d92c98fccc29e1fc8d927">PCAP_ERRBUF_SIZE</a>];        <span class="comment">/* Retrieve the device list from the local machine */</span>    <span class="keywordflow">if</span> (<a class="code" href="group__wpcapfunc.html#g98f36e62c95c6ad81eaa8b2bbeb8f16e">pcap_findalldevs_ex</a>(<a class="code" href="group__remote__source__string.html#g6d7103b8a7e1eca8c325bd8f32c361c3">PCAP_SRC_IF_STRING</a>, NULL <span class="comment">/* auth is not needed */</span>, &amp;alldevs, errbuf) == -1)    {        fprintf(stderr,<span class="stringliteral">"Error in pcap_findalldevs_ex: %s\n"</span>, errbuf);        exit(1);    }        <span class="comment">/* Print the list */</span>    <span class="keywordflow">for</span>(d= alldevs; d != NULL; d= d-&gt;<a class="code" href="structpcap__if.html#81508e6e4e41ca4235c8d6b51913c536">next</a>)    {        printf(<span class="stringliteral">"%d. %s"</span>, ++i, d-&gt;<a class="code" href="structpcap__if.html#5ac083a645d964373f022d03df4849c8">name</a>);        if (d-&gt;<a class="code" href="structpcap__if.html#8444d6e0dfe2bbab0b5e7b24308f1559">description</a>)            printf(<span class="stringliteral">" (%s)\n"</span>, d-&gt;<a class="code" href="structpcap__if.html#8444d6e0dfe2bbab0b5e7b24308f1559">description</a>);        <span class="keywordflow">else</span>            printf(<span class="stringliteral">" (No description available)\n"</span>);    }        <span class="keywordflow">if</span> (i == 0)    {        printf(<span class="stringliteral">"\nNo interfaces found! Make sure WinPcap is installed.\n"</span>);        <span class="keywordflow">return</span>;    }    <span class="comment">/* We don't need any more the device list. Free it */</span>    <a class="code" href="group__wpcapfunc.html#g346b4b0b7fd1cda4abb9a39f767dbeb1">pcap_freealldevs</a>(alldevs);}</pre></div><p>Some comments about this code.<p>First of all, <a class="el" href="group__wpcapfunc.html#g98f36e62c95c6ad81eaa8b2bbeb8f16e">pcap_findalldevs_ex()</a>, like other libpcap functions, has an <em>errbuf</em> parameter. This parameter points to a string filled by libpcap with a description of the error if something goes wrong.<p>Second, remember that not all the OSes supported by libpcap provide a description of the network interfaces, therefore if we want to write a portable application, we must consider the case in which <em>description</em> is null: we print the string "No description available" in that situation.<p>Note finally that we free the list with <a class="el" href="group__wpcapfunc.html#g346b4b0b7fd1cda4abb9a39f767dbeb1">pcap_freealldevs()</a> once when we have finished with it.<p>Let's try to compile and run the code of this first sample. In order to compile it under Unix or Cygwin, simply type:<p><pre>  gcc -o testprog testprog.c -lpcap</pre><p>On Windows, you will need to create a project, following the instructions in the <a class="el" href="group__wpcapsamps.html">Using WinPcap in your programs</a> section of this manual. However, we suggest that you use the WinPcap developer's pack (available at the WinPcap website, <a href="http://www.winpcap.org">http://www.winpcap.org</a> ), since it provides many examples already configured as projects including all the code presented in this tutorial and the <em>includes</em> and <em>libraries</em> needed to compile and run the examples.<p>Assuming we have compiled the program, let's try to run it. On a particular WinXP workstation, the result we optained is<p><pre>   1. \Device\NPF_{4E273621-5161-46C8-895A-48D0E52A0B83} (Realtek RTL8029(AS) Ethernet Adapter)   2. \Device\NPF_{5D24AE04-C486-4A96-83FB-8B5EC6C7F430} (3Com EtherLink PCI) </pre><p>As you can see, the name of the network adapters (that will be passed to libpcap when opening the devices) under Windows are quite unreadable, so the parenthetical descriptions can be very helpful.<p><a class="el" href="group__wpcap__tut.html">&lt;&lt;&lt; Previous</a> <a class="el" href="group__wpcap__tut2.html">Next &gt;&gt;&gt;</a> <hr><p align="right"><img border="0" src="winpcap_small.gif" align="absbottom" width="91" height="27">documentation. Copyright (c) 2002-2005 Politecnico di Torino. Copyright (c) 2005-2007 CACE Technologies. All rights reserved.</p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -