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.