?? group__wpcap__tut2.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>WinPcap: Obtaining advanced information about installed devices</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.6 --><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"><tr><td></td></tr></table>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#g98f36e62c95c6ad81eaa8b2bbeb8f16e" 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:<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#g98f36e62c95c6ad81eaa8b2bbeb8f16e" 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>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#g98f36e62c95c6ad81eaa8b2bbeb8f16e" 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#gcd448353957d92c98fccc29e1fc8d927" 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#gcd448353957d92c98fccc29e1fc8d927" 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#gcd448353957d92c98fccc29e1fc8d927" 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#gcd448353957d92c98fccc29e1fc8d927" 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#g98f36e62c95c6ad81eaa8b2bbeb8f16e" 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#81508e6e4e41ca4235c8d6b51913c536" 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#g346b4b0b7fd1cda4abb9a39f767dbeb1" 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#5ac083a645d964373f022d03df4849c8" 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#8444d6e0dfe2bbab0b5e7b24308f1559" 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#8444d6e0dfe2bbab0b5e7b24308f1559" 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#304e038dd1326c14b31a206cbad18756" 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#g43a6601bfd438efc02b0ba71c5439647" 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#3910004677550db6d9b09792ba3e2cca" 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#b151e8e96bdb23ae8dd8d644de561999" 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#4863f5b8767cd19fe6ea4db75456e5df" 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#4863f5b8767cd19fe6ea4db75456e5df" 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#4863f5b8767cd19fe6ea4db75456e5df" 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#4863f5b8767cd19fe6ea4db75456e5df" 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#c43963e42e4d901e55e433ab9c3ea686" 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#c43963e42e4d901e55e433ab9c3ea686" 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#0077647e1560caa72d457120b36c248c" title="if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -