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

📄 group__wpcap__tut2.html

📁 Winpcap是一个强大的网络开发库
💻 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&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"><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 &lt;stdio.h&gt;</span><span class="preprocessor">#include "pcap.h"</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#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://              ==&gt; lists interfaces in the local machine\n"</span>            <span class="stringliteral">"rpcap://hostname:port ==&gt; 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     ==&gt; 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, &amp;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-&gt;<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-&gt;<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-&gt;<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-&gt;<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-&gt;<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> &amp; <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-&gt;<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-&gt;<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-&gt;<a class="code" href="structpcap__addr.html#4863f5b8767cd19fe6ea4db75456e5df" 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#4863f5b8767cd19fe6ea4db75456e5df" 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">"\tAddress Family Name: AF_INET\n"</span>);        <span class="keywordflow">if</span> (a-&gt;<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-&gt;<a class="code" href="structpcap__addr.html#4863f5b8767cd19fe6ea4db75456e5df" 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#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-&gt;<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>)-&gt;sin_addr.s_addr));        <span class="keywordflow">if</span> (a-&gt;<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 + -