Get advanced information for installed network drives
In the first chapter, how to get static information that has existing adapters. In fact, WinPCAP also provides other advanced information, especially the PCAP_FINDALDEVS () of this function, each PCAP_IF structure returned, also contains a list of PCAP_ADDR structures, and he contains:
An address list, a mask list, a broadcast address list, and a list of destination addresses.
The following example prints all the field information of the PCAP_IF structure via an Ifprint () function, which calls ifprint () in each PCAP_FINDALDEVS (), which calls ifprint () to display the detailed field information.
#include "pcap.h"
#ifndef Win32
#include
#include
#ELSE
#include
#ENDIF
Void ifprint (PCAP_IF_T * D);
Char * iptos (u_long in);
int main ()
{
PCAP_IF_T * ALLDEVS;
PCAP_IF_T * D;
Char Errbuf [PCAP_ERRBUF_SIZE 1];
/ * Get a list of NIC * /
IF (PCAP_FINDALDEVS (& alldevs, errbuf) == -1)
{
FPrintf (stderr, "error in pcap_findallDalldevs:% s / n", errbuf);
Exit (1);
}
/ * Circular call ifprint () to display the information of the PCAP_IF structure * /
For (d = alldevs; d; d = d-> next)
{
Ifprint (d);
}
Return 1;
}
/ * Print All The Available Information on The Given Interface * /
Void ifprint (PCAP_IF_T * D)
{
PCAP_ADDR_T * A;
/ * Name * /
Printf ("% S / N", D-> Name);
/ * Description * /
IF (D-> Description)
Printf ("/ TDESCRIPTION:% S / N", D-> Description);
/ * Loopback address * /
Printf ("/ TLOOPBACK:% S / N", (D-> Flags & PCAP_IF_LOOPBACK)? "YES": "no");
/ * IP addresses * /
For (a = d-> addresses; a; a = a-> next) {
Printf ("/ Taddress Family: #% D / N", A-> Addr-> Sa_Family);
/ * For the SockAddr_in structure, please refer to other network programings * /
Switch (A-> Addr-> sa_family)
{
Case AF_INET:
Printf ("/ taddress family name: AF_INET / N"); // Print Network Address Type
IF (A-> AddR) // Print IP Address
Printf ("/ TADDRESS:% S / N", IPTOS ((Struct SockAddr_in *) A-> Addr) -> SIN_ADDR.S_ADDR));
IF (A-> Netmask) // Print Mask
Printf ("/ TNetmask:% S / N", IPTOS ((Struct SockAddr_in *) A-> Netmask) -> SIN_ADDR.S_ADDR); if (A-> Broadddr) // Print Broadcast Address
Printf ("/ TBroadcast Address:% S / N", IPTOS ((Struct SockAddr_in *) A-> Broaddddr) -> SIN_ADDR.S_ADDR));
IF (a-> dstaddr) // destination address
Printf ("/ TDestination Address:% S / N", IPTOS ((Struct SockAddr_in *) A-> Dstaddr) -> SIN_ADDR.S_ADDR));
Break;
DEFAULT:
Printf ("/ TADDRESS FAMILY NAME: UNKNOWN / N");
Break;
}
}
Printf ("/ n");
}
/ * Convert a unsigned long-type IP to string type IP * /
#define iptosbuffers 12
Char * iptos (u_long in)
{
Static char Output [iptosbuffers] [3 * 4 3 1];
Static short which;
u_char * p;
P = (u_char *) & in;
Which = (Which 1 == iptosbuffers? 0: Which 1);
Sprintf (Output [Which], "% D.% d.% d.% d", p [0], p [1], p [2], p [3]);
Return Output [Which];
}