Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Obtaining equipment list based on WinPcap

Shulou Source: shulou.com Published: 2022-06-01 03:58:18 10月06日 Update

Typically, the first thing you do when writing WinPcap-based applications is to get a list of connected network adapters. Both libpcap and WinPcap provide the pcap_findalldevs_ex () function to do this: 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 fields name and description represent an adapter name and a description that people can understand.

First, pcap_findalldevs_ex (), like other libpcap functions, has an errbuf parameter. Once an error occurs, this parameter will be written to an error message of type string by libpcap. Second, keep in mind that not all operating systems support the network program interface provided by libpcap, so if we want to write a portable application, we must consider under what circumstances description is null. In this procedure, when we encounter this situation, we will print the prompt statement "No description available".

Finally, remember that when we have finished using the device list, we will call the pcap_freealldevs () function to release the memory resources it occupies.

Let's compile and run our first sample program!

Implementation code:

/ / aa.cpp: defines the entry point for the console application.

# include "stdafx.h"

# include "pcap.h"

Int _ tmain (int argc, _ TCHAR* argv [])

{

Pcap_if_t * alldevs

Pcap_if_t * d

Int iTunes 0

Char errbuf[PCAP _ ERRBUF_SIZE]

/ * get the list of local machines and equipment * /

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 0

}

/ * the device list is no longer needed, release it * /

Pcap_freealldevs (alldevs)

}

Running

Tags: Programs functions devices applications adapters applications adaptations information parameters situations structures networks errors runs operating systems things people code portals memory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info OPPO Reno Huawei NVidia