mame/3rdparty/winpcap/docs/html/group__wpcap__tut2.html
2015-01-10 12:21:12 +01:00

217 lines
18 KiB
HTML
Raw Blame History

<!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 advanced information about installed devices</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&nbsp;Page</span></a></li>
<li><a href="pages.html"><span>Related&nbsp;Pages</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>
</ul>
</div>
</div>
<div class="contents">
<h1>Obtaining advanced information about installed devices</h1><table border="0" cellpadding="0" cellspacing="0">
</table>
<p>Lesson 1 (<a class="el" href="group__wpcap__tut1.html">Obtaining the device list</a>) demonstrated how to get basic information (i.e. device name and description) about available adapters. Actually, WinPcap provides also other advanced information. In particular, every <a class="el" href="structpcap__if.html" title="Item in a list of interfaces, used by pcap_findalldevs().">pcap_if</a> structure returned by <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> contains also a list of <a class="el" href="structpcap__addr.html" title="Representation of an interface address, used by pcap_findalldevs().">pcap_addr</a> structures, with:</p>
<ul>
<li>a list of addresses for that interface.</li>
<li>a list of netmasks (each of which corresponds to an entry in the addresses list).</li>
<li>a list of broadcast addresses (each of which corresponds to an entry in the addresses list).</li>
<li>a list of destination addresses (each of which corresponds to an entry in the addresses list).</li>
</ul>
<p>Additionally, <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> can also return remote adapters and a list of pcap files that are located in a given local folder.</p>
<p>The following sample provides an ifprint() function that prints the complete contents of a <a class="el" href="structpcap__if.html" title="Item in a list of interfaces, used by pcap_findalldevs().">pcap_if</a> structure. It is invoked by the program for every entry returned by <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>.</p>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)</span>
<span class="comment"> * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)</span>
<span class="comment"> * All rights reserved.</span>
<span class="comment"> *</span>
<span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<span class="comment"> * modification, are permitted provided that the following conditions</span>
<span class="comment"> * are met:</span>
<span class="comment"> *</span>
<span class="comment"> * 1. Redistributions of source code must retain the above copyright</span>
<span class="comment"> * notice, this list of conditions and the following disclaimer.</span>
<span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
<span class="comment"> * notice, this list of conditions and the following disclaimer in the</span>
<span class="comment"> * documentation and/or other materials provided with the distribution.</span>
<span class="comment"> * 3. Neither the name of the Politecnico di Torino, CACE Technologies </span>
<span class="comment"> * nor the names of its contributors may be used to endorse or promote </span>
<span class="comment"> * products derived from this software without specific prior written </span>
<span class="comment"> * permission.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<span class="comment"> * &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<span class="comment"> * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
<span class="comment"> * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span>
<span class="comment"> * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<span class="comment"> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
<span class="comment"> * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span>
<span class="comment"> * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span>
<span class="comment"> * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<span class="comment"> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<span class="comment"> * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<span class="comment"> *</span>
<span class="comment"> */</span>
<span class="preprocessor">#include &lt;stdio.h&gt;</span>
<span class="preprocessor">#include &quot;pcap.h&quot;</span>
<span class="preprocessor">#ifndef WIN32</span>
<span class="preprocessor"></span><span class="preprocessor"> #include &lt;sys/socket.h&gt;</span>
<span class="preprocessor"> #include &lt;netinet/in.h&gt;</span>
<span class="preprocessor">#else</span>
<span class="preprocessor"></span><span class="preprocessor"> #include &lt;winsock.h&gt;</span>
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="comment">// Function prototypes</span>
<span class="keywordtype">void</span> ifprint(<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">char</span> *iptos(u_long in);
<span class="keywordtype">char</span>* ip6tos(<span class="keyword">struct</span> sockaddr *sockaddr, <span class="keywordtype">char</span> *address, <span class="keywordtype">int</span> addrlen);
<span class="keywordtype">int</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">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>+1];
<span class="keywordtype">char</span> source[<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>+1];
printf(<span class="stringliteral">&quot;Enter the device you want to list:\n&quot;</span>
<span class="stringliteral">&quot;rpcap:// ==&gt; lists interfaces in the local machine\n&quot;</span>
<span class="stringliteral">&quot;rpcap://hostname:port ==&gt; lists interfaces in a remote machine\n&quot;</span>
<span class="stringliteral">&quot; (rpcapd daemon must be up and running\n&quot;</span>
<span class="stringliteral">&quot; and it must accept &apos;null&apos; authentication)\n&quot;</span>
<span class="stringliteral">&quot;file://foldername ==&gt; lists all pcap files in the give folder\n\n&quot;</span>
<span class="stringliteral">&quot;Enter your choice: &quot;</span>);
fgets(source, <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>, stdin);
source[<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="charliteral">&apos;\0&apos;</span>;
<span class="comment">/* Retrieve the interfaces list */</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>(source, NULL, &amp;alldevs, errbuf) == -1)
{
fprintf(stderr,<span class="stringliteral">&quot;Error in pcap_findalldevs: %s\n&quot;</span>,errbuf);
exit(1);
}
<span class="comment">/* Scan the list printing every entry */</span>
<span class="keywordflow">for</span>(d=alldevs;d;d=d-&gt;<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>)
{
ifprint(d);
}
<a class="code" href="group__wpcapfunc.html#ga346b4b0b7fd1cda4abb9a39f767dbeb1" title="Free an interface list returned by pcap_findalldevs().">pcap_freealldevs</a>(alldevs);
<span class="keywordflow">return</span> 1;
}
<span class="comment">/* Print all the available information on the given interface */</span>
<span class="keywordtype">void</span> ifprint(<a class="code" href="structpcap__if.html" title="Item in a list of interfaces, used by pcap_findalldevs().">pcap_if_t</a> *d)
{
<a class="code" href="structpcap__addr.html" title="Representation of an interface address, used by pcap_findalldevs().">pcap_addr_t</a> *a;
<span class="keywordtype">char</span> ip6str[128];
<span class="comment">/* Name */</span>
printf(<span class="stringliteral">&quot;%s\n&quot;</span>,d-&gt;<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="comment">/* Description */</span>
<span class="keywordflow">if</span> (d-&gt;<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">&quot;\tDescription: %s\n&quot;</span>,d-&gt;<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="comment">/* Loopback Address*/</span>
printf(<span class="stringliteral">&quot;\tLoopback: %s\n&quot;</span>,(d-&gt;<a class="code" href="structpcap__if.html#a304e038dd1326c14b31a206cbad18756" title="PCAP_IF_ interface flags. Currently the only possible flag is PCAP_IF_LOOPBACK, that...">flags</a> &amp; <a class="code" href="group__wpcap__def.html#ga43a6601bfd438efc02b0ba71c5439647" title="interface is loopback">PCAP_IF_LOOPBACK</a>)?<span class="stringliteral">&quot;yes&quot;</span>:<span class="stringliteral">&quot;no&quot;</span>);
<span class="comment">/* IP addresses */</span>
<span class="keywordflow">for</span>(a=d-&gt;<a class="code" href="structpcap__if.html#a3910004677550db6d9b09792ba3e2cca" title="a pointer to the first element of a list of addresses for the interface">addresses</a>;a;a=a-&gt;<a class="code" href="structpcap__addr.html#ab151e8e96bdb23ae8dd8d644de561999" title="if not NULL, a pointer to the next element in the list; NULL for the last element...">next</a>) {
printf(<span class="stringliteral">&quot;\tAddress Family: #%d\n&quot;</span>,a-&gt;<a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>-&gt;sa_family);
<span class="keywordflow">switch</span>(a-&gt;<a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>-&gt;sa_family)
{
<span class="keywordflow">case</span> AF_INET:
printf(<span class="stringliteral">&quot;\tAddress Family Name: AF_INET\n&quot;</span>);
<span class="keywordflow">if</span> (a-&gt;<a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>)
printf(<span class="stringliteral">&quot;\tAddress: %s\n&quot;</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-&gt;<a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>)-&gt;sin_addr.s_addr));
<span class="keywordflow">if</span> (a-&gt;<a class="code" href="structpcap__addr.html#ac43963e42e4d901e55e433ab9c3ea686" title="if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding...">netmask</a>)
printf(<span class="stringliteral">&quot;\tNetmask: %s\n&quot;</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-&gt;<a class="code" href="structpcap__addr.html#ac43963e42e4d901e55e433ab9c3ea686" title="if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding...">netmask</a>)-&gt;sin_addr.s_addr));
<span class="keywordflow">if</span> (a-&gt;<a class="code" href="structpcap__addr.html#a0077647e1560caa72d457120b36c248c" title="if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre<72>...">broadaddr</a>)
printf(<span class="stringliteral">&quot;\tBroadcast Address: %s\n&quot;</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-&gt;<a class="code" href="structpcap__addr.html#a0077647e1560caa72d457120b36c248c" title="if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre<72>...">broadaddr</a>)-&gt;sin_addr.s_addr));
<span class="keywordflow">if</span> (a-&gt;<a class="code" href="structpcap__addr.html#a4a32e00e83d4b1b582ba9cef24a5d62a" title="if not NULL, a pointer to a struct sockaddr that contains the destination address...">dstaddr</a>)
printf(<span class="stringliteral">&quot;\tDestination Address: %s\n&quot;</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-&gt;<a class="code" href="structpcap__addr.html#a4a32e00e83d4b1b582ba9cef24a5d62a" title="if not NULL, a pointer to a struct sockaddr that contains the destination address...">dstaddr</a>)-&gt;sin_addr.s_addr));
<span class="keywordflow">break</span>;
<span class="keywordflow">case</span> AF_INET6:
printf(<span class="stringliteral">&quot;\tAddress Family Name: AF_INET6\n&quot;</span>);
<span class="keywordflow">if</span> (a-&gt;<a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>)
printf(<span class="stringliteral">&quot;\tAddress: %s\n&quot;</span>, ip6tos(a-&gt;<a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>, ip6str, <span class="keyword">sizeof</span>(ip6str)));
<span class="keywordflow">break</span>;
<span class="keywordflow">default</span>:
printf(<span class="stringliteral">&quot;\tAddress Family Name: Unknown\n&quot;</span>);
<span class="keywordflow">break</span>;
}
}
printf(<span class="stringliteral">&quot;\n&quot;</span>);
}
<span class="comment">/* From tcptraceroute, convert a numeric IP address to a string */</span>
<span class="preprocessor">#define IPTOSBUFFERS 12</span>
<span class="preprocessor"></span><span class="keywordtype">char</span> *iptos(u_long in)
{
<span class="keyword">static</span> <span class="keywordtype">char</span> output[IPTOSBUFFERS][3*4+3+1];
<span class="keyword">static</span> <span class="keywordtype">short</span> which;
u_char *p;
p = (u_char *)&amp;in;
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
_snprintf_s(output[which], <span class="keyword">sizeof</span>(output[which]), <span class="keyword">sizeof</span>(output[which]),<span class="stringliteral">&quot;%d.%d.%d.%d&quot;</span>, p[0], p[1], p[2], p[3]);
<span class="keywordflow">return</span> output[which];
}
<span class="keywordtype">char</span>* ip6tos(<span class="keyword">struct</span> sockaddr *sockaddr, <span class="keywordtype">char</span> *address, <span class="keywordtype">int</span> addrlen)
{
socklen_t sockaddrlen;
<span class="preprocessor"> #ifdef WIN32</span>
<span class="preprocessor"></span> sockaddrlen = <span class="keyword">sizeof</span>(<span class="keyword">struct </span>sockaddr_in6);
<span class="preprocessor"> #else</span>
<span class="preprocessor"></span> sockaddrlen = <span class="keyword">sizeof</span>(<span class="keyword">struct </span>sockaddr_storage);
<span class="preprocessor"> #endif</span>
<span class="preprocessor"></span>
<span class="keywordflow">if</span>(getnameinfo(sockaddr,
sockaddrlen,
address,
addrlen,
NULL,
0,
NI_NUMERICHOST) != 0) address = NULL;
<span class="keywordflow">return</span> address;
}
</pre></div><p><a class="el" href="group__wpcap__tut1.html">&lt;&lt;&lt; Previous</a> <a class="el" href="group__wpcap__tut3.html">Next &gt;&gt;&gt;</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>