Winpcap programming 3 -- get the network adapter list

Source: Internet
Author: User

Http://www.cnblogs.com/TianFang/archive/2007/11/27/973673.html

Winpcap programming 3 -- get the network adapter list

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 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];
/*
Obtain the device list of a 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 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.

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 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];
/*
Obtain the device list of a 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 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.