mirror of
https://github.com/holub/mame
synced 2025-04-28 11:11:48 +03:00
82 lines
7.5 KiB
HTML
82 lines
7.5 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<title>WinPcap: Obtaining the device list</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<link href="style.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<!-- Generated by Doxygen 1.6.1 -->
|
|
<div class="navigation" id="top">
|
|
<div class="tabs">
|
|
<ul>
|
|
<li><a href="main.html"><span>Main Page</span></a></li>
|
|
<li><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="modules.html"><span>Modules</span></a></li>
|
|
<li><a href="annotated.html"><span>Data Structures</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="contents">
|
|
<h1>Obtaining the device list</h1><table border="0" cellpadding="0" cellspacing="0">
|
|
</table>
|
|
<p>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#ga98f36e62c95c6ad81eaa8b2bbeb8f16e" title="Create a list of network devices that can be opened with pcap_open().">pcap_findalldevs_ex()</a> function for this purpose: this function returns a linked list of <a class="el" href="structpcap__if.html" title="Item in a list of interfaces, used by pcap_findalldevs().">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>
|
|
<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" title="Item in a list of interfaces, used by pcap_findalldevs().">pcap_if_t</a> *alldevs;
|
|
<a class="code" href="structpcap__if.html" title="Item in a list of interfaces, used by pcap_findalldevs().">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#gacd448353957d92c98fccc29e1fc8d927" title="Size to use when allocating the buffer that contains the libpcap errors.">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#ga98f36e62c95c6ad81eaa8b2bbeb8f16e" title="Create a list of network devices that can be opened with pcap_open().">pcap_findalldevs_ex</a>(<a class="code" href="group__remote__source__string.html#ga6d7103b8a7e1eca8c325bd8f32c361c3" title="String that will be used to determine the type of source in use (file, remote/local...">PCAP_SRC_IF_STRING</a>, NULL <span class="comment">/* auth is not needed */</span>, &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-><a class="code" href="structpcap__if.html#a81508e6e4e41ca4235c8d6b51913c536" title="if not NULL, a pointer to the next element in the list; NULL for the last element...">next</a>)
|
|
{
|
|
printf(<span class="stringliteral">"%d. %s"</span>, ++i, d-><a class="code" href="structpcap__if.html#a5ac083a645d964373f022d03df4849c8" title="a pointer to a string giving a name for the device to pass to pcap_open_live()">name</a>);
|
|
<span class="keywordflow">if</span> (d-><a class="code" href="structpcap__if.html#a8444d6e0dfe2bbab0b5e7b24308f1559" title="if not NULL, a pointer to a string giving a human-readable description of the device...">description</a>)
|
|
printf(<span class="stringliteral">" (%s)\n"</span>, d-><a class="code" href="structpcap__if.html#a8444d6e0dfe2bbab0b5e7b24308f1559" title="if not NULL, a pointer to a string giving a human-readable description of the device...">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#ga346b4b0b7fd1cda4abb9a39f767dbeb1" title="Free an interface list returned by pcap_findalldevs().">pcap_freealldevs</a>(alldevs);
|
|
}
|
|
</pre></div><p>Some comments about this code.</p>
|
|
<p>First of all, <a class="el" href="group__wpcapfunc.html#ga98f36e62c95c6ad81eaa8b2bbeb8f16e" title="Create a list of network devices that can be opened with pcap_open().">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>
|
|
<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>
|
|
<p>Note finally that we free the list with <a class="el" href="group__wpcapfunc.html#ga346b4b0b7fd1cda4abb9a39f767dbeb1" title="Free an interface list returned by pcap_findalldevs().">pcap_freealldevs()</a> once when we have finished with it.</p>
|
|
<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>
|
|
<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>
|
|
<p><a class="el" href="group__wpcap__tut.html"><<< Previous</a> <a class="el" href="group__wpcap__tut2.html">Next >>></a> </p>
|
|
</div>
|
|
|
|
<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-2009
|
|
CACE Technologies. All rights reserved.</p>
|