mirror of
https://github.com/holub/mame
synced 2025-05-01 04:06:58 +03:00
217 lines
18 KiB
HTML
217 lines
18 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 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 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 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"> * "AS IS" 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 <stdio.h></span>
|
||
|
||
<span class="preprocessor">#include "pcap.h"</span>
|
||
|
||
<span class="preprocessor">#ifndef WIN32</span>
|
||
<span class="preprocessor"></span><span class="preprocessor"> #include <sys/socket.h></span>
|
||
<span class="preprocessor"> #include <netinet/in.h></span>
|
||
<span class="preprocessor">#else</span>
|
||
<span class="preprocessor"></span><span class="preprocessor"> #include <winsock.h></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">"Enter the device you want to list:\n"</span>
|
||
<span class="stringliteral">"rpcap:// ==> lists interfaces in the local machine\n"</span>
|
||
<span class="stringliteral">"rpcap://hostname:port ==> lists interfaces in a remote machine\n"</span>
|
||
<span class="stringliteral">" (rpcapd daemon must be up and running\n"</span>
|
||
<span class="stringliteral">" and it must accept 'null' authentication)\n"</span>
|
||
<span class="stringliteral">"file://foldername ==> lists all pcap files in the give folder\n\n"</span>
|
||
<span class="stringliteral">"Enter your choice: "</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">'\0'</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, &alldevs, errbuf) == -1)
|
||
{
|
||
fprintf(stderr,<span class="stringliteral">"Error in pcap_findalldevs: %s\n"</span>,errbuf);
|
||
exit(1);
|
||
}
|
||
|
||
<span class="comment">/* Scan the list printing every entry */</span>
|
||
<span class="keywordflow">for</span>(d=alldevs;d;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>)
|
||
{
|
||
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">"%s\n"</span>,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="comment">/* Description */</span>
|
||
<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">"\tDescription: %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="comment">/* Loopback Address*/</span>
|
||
printf(<span class="stringliteral">"\tLoopback: %s\n"</span>,(d-><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> & <a class="code" href="group__wpcap__def.html#ga43a6601bfd438efc02b0ba71c5439647" title="interface is loopback">PCAP_IF_LOOPBACK</a>)?<span class="stringliteral">"yes"</span>:<span class="stringliteral">"no"</span>);
|
||
|
||
<span class="comment">/* IP addresses */</span>
|
||
<span class="keywordflow">for</span>(a=d-><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-><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">"\tAddress Family: #%d\n"</span>,a-><a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>->sa_family);
|
||
|
||
<span class="keywordflow">switch</span>(a-><a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>->sa_family)
|
||
{
|
||
<span class="keywordflow">case</span> AF_INET:
|
||
printf(<span class="stringliteral">"\tAddress Family Name: AF_INET\n"</span>);
|
||
<span class="keywordflow">if</span> (a-><a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>)
|
||
printf(<span class="stringliteral">"\tAddress: %s\n"</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-><a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>)->sin_addr.s_addr));
|
||
<span class="keywordflow">if</span> (a-><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">"\tNetmask: %s\n"</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-><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>)->sin_addr.s_addr));
|
||
<span class="keywordflow">if</span> (a-><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">"\tBroadcast Address: %s\n"</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-><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>)->sin_addr.s_addr));
|
||
<span class="keywordflow">if</span> (a-><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">"\tDestination Address: %s\n"</span>,iptos(((<span class="keyword">struct</span> sockaddr_in *)a-><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>)->sin_addr.s_addr));
|
||
<span class="keywordflow">break</span>;
|
||
|
||
<span class="keywordflow">case</span> AF_INET6:
|
||
printf(<span class="stringliteral">"\tAddress Family Name: AF_INET6\n"</span>);
|
||
<span class="keywordflow">if</span> (a-><a class="code" href="structpcap__addr.html#a4863f5b8767cd19fe6ea4db75456e5df" title="a pointer to a struct sockaddr containing an address">addr</a>)
|
||
printf(<span class="stringliteral">"\tAddress: %s\n"</span>, ip6tos(a-><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">"\tAddress Family Name: Unknown\n"</span>);
|
||
<span class="keywordflow">break</span>;
|
||
}
|
||
}
|
||
printf(<span class="stringliteral">"\n"</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 *)&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">"%d.%d.%d.%d"</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"><<< Previous</a> <a class="el" href="group__wpcap__tut3.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>
|