Winpcap Study Notes 1 _ getting the device list)

Source: Internet
Author: User

Generally, write Winpcap-based applicationsProgramThe first thing is to obtain the list of connected network adapters. Both libpcap and Winpcap are mentionedSuppliedPcap_findalldevs_ex ()LetterNumber to achieve this function: This function returns a linked list of pcap_if structures, each of which contains the details of an adapter. It is worth noting that the data domainNameAndDescriptionIndicates an adapter name and a description that can be understood by people.

BelowCodeThe list of adapters can be obtained and displayed on the screen. If no adapter is found, an error message is printed.

 

  1   # Include  "  Pcap. h  "  
2
3 Main ()
4 {
5 Pcap_if_t * Alldevs;
6 Pcap_if_t * D;
7 Int I = 0 ;
8 Char Errbuf [pcap_errbuf_size];
9
10 /* Obtains the list of devices on the local machine. */
11 If (Pcap_findalldevs_ex (pcap_src_if_string, null /* Auth is not needed */ , & Alldevs, errbuf) = - 1 )
12 {
13 Fprintf (stderr, " Error in pcap_findalldevs_ex: % s \ n " , Errbuf );
14 Exit ( 1 );
15 }
16
17 /* Print list */
18 For (D = Alldevs; d ! = NULL; d = D -> Next)
19 {
20 Printf ( " % D. % s " , ++ I, d -> Name );
21 If (D -> Description)
22 Printf ( " (% S) \ n " , D -> Description );
23 Else
24 Printf ( " (No description available) \ n " );
25 }
26
27 If (I = 0 )
28 {
29 Printf ( " \ Nno interfaces found! Make sure Winpcap is installed. \ n " );
30 Return ;
31 }
32
33 /* The device list is no longer required. Release it. */
34 Pcap_freealldevs (alldevs );
35 }
36
37  

Some instructions on this code

First, pcap_findalldevs_ex (), like other libpcap functions, hasErrbufParameters. If an error occurs, this parameter will be written to the string type error message by Libpcap.

Second, remember that not all operating systems support the Network Program interfaces provided by Libpcap. Therefore, if we want to write a portable application, we must consider under what circumstances,DescriptionIs null. In this program, we encounter this situation, the prompt statement "no description available" will be printed ".

Finally, remember that when we use the device list, we need to call the pcap_freealldevs () function to release the occupied memory resources.

Let's compile and run our first sample program! To compile this program on Unix or cygwin platform, you need to enter the following simple:

 
Gcc-O testprog. C-lpcap

On Windows, you need to create a project and follow the steps in Winpcap programming. However, we recommend that you use the Winpcap developer's Pack (for more information, visit the Winpcap website, http://www.winpcap.org) because it provides many configured examples, including all the sample code in this tutorial, andInclude)AndLibrary (Libraries)

Let's run jiash if we have compiled the program. On a WINXP computer, the result is:

 
1. \ device \ NPF _ {4e273621-5161-46c8-895a-48d0e52a0b83} (RealTek rtl8029 (AS) Ethernet adapter)
 
2. \ device \ NPF _ {5d24ae04-c486-4a96-83fb-8b5ec6c7f430} (3Com etherlink PCI)

As you can see, the name of the network adapter on Windows is quite easy to read. It can be seen how helpful an explanatory description is!

 

Author: the Winpcap team home: http://www.winpcap.org

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.