Winpcap programming 3 -- get the network adapter list

Source: Internet
Author: User

The first thing for a Winpcap application is to obtain the list of connected network adapters.

Winpcap provides the pcap_findalldevs_ex () function to implement this function: it returns a linked list of pcap_if structures, each of which contains the details of an adapter.

The following code obtains the list of adapters and prints their names and descriptions on the screen. If no adapter is found, an error message is printed.

# Include
"Pcap. H"

Main ()
{
Pcap_if_t * alldevs;
Pcap_if_t * D;
Int I = 0;
Char errbuf [pcap_errbuf_size];
/* Get the device list of the Local Machine */
If (pcap_findalldevs_ex (pcap_src_if_string, null/* auth is not needed */, & alldevs, errbuf) =-1)
{
Fprintf (stderr, "error in pcap_findalldevs_ex: % s \ n", errbuf );
Exit (1 );
}
/* Print the list */
For (D = alldevs; D! = NULL; D = D-> next)
{
Printf ("% d. % s", ++ I, d-> name );
If (D-> description)
Printf ("(% s) \ n", D-> description );
Else
Printf ("(no description available) \ n ");
}
If (I = 0)
{
Printf ("\ Nno interfaces found! Make sure Winpcap is installed. \ n ");
Return;
}
/* The device list is no longer needed. Release it */
Pcap_freealldevs (alldevs );
}

This is a very typical C code. The process is relatively simple: first obtain the network adapter list through pcap_findalldevs_ex, and the network adapter list information is kept in a pcap_if_t pointer, it is a classic C-linked list structure. You can use this pointer to traverse all network adapters. Finally, call pcap_freealldevs to release the resource.

It can be seen that the information of each network adapter is saved in a pcap_if_t structure, except for some information shown in this example, it also contains a lot of useful information (such as MAC addresses and IP addresses). If you are interested, you can study it.

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.