get a list of devices
1. In general, the first thing to write a WinPcap-based application is to get a list of connected network adapters. Both Libpcap and WinPcap provide the PCAP_FINDALLDEVS_EX () function to implement this function: This function returns a list of pcap_if structures, each of which contains the details of an adapter. It is important to note that the data field name and description represent an adapter name and a description that people can understand.
The following code can get the list of adapters and display them on the screen, and if the adapter is not found, an error message will be printed.
A few notes about this piece of code
First, PCAP_FINDALLDEVS_EX (), like the other Libpcap functions, has a errbuf parameter. Once an error occurs, this parameter is Libpcap written to the string type error message.
Second, remember that not all operating systems support the LIBPCAP provided by the network program interface, so if we want to write a portable application, we have to consider under what circumstances, description is null. In this program, when we encounter this situation, the prompt statement "No description available" is printed.
Finally, remember that when we complete the use of the device list, we call the Pcap_freealldevs () function to release the memory resources it occupies.
Let's compile and run our first sample program! In order to compile this program on a UNIX or Cygwin platform, you need to simply enter:
Gcc-o TestProg Testprog.c-lpcap
2, the specific implementation of the code:
1406404002.cpp: Defines the entry point of the console application.
#include "stdafx.h"
#include "Pcap.h"
int _tmain (int argc, _tchar* argv[])
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
Char Errbuf[pcap_errbuf_size];
/* Get a list of local machine devices */
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;
}
/* No more device list required, release it */
Pcap_freealldevs (Alldevs);
}
3, the specific configuration: **= (1406404002)
Item-->** Properties (ALT+F7)
Configuration Properties---inventory tools--input and output--embedded list--No
Item-->** Properties (ALT+F7)
Configuration Properties-->c/c++--> General-and additional Include directories--Example: C:\WpdPack_4_1_2\WpdPack\Include
Item-->** Properties (ALT+F7)
Configuration Properties--linker--general--Add Library directory--Example: C:\WpdPack_4_1_2\WpdPack\Lib
Item-->** Properties (ALT+F7)
Configuration Properties--linker--Add dependency--add "; Packet.lib;wpcap.lib "
Item-->** Properties (ALT+F7)
Configuration Properties-->c/c++--> Preprocessor---preprocessor definition--add "; Have_remote "
4. On the Windows platform, you need to create a project and follow the steps in using WINPCAP programming. However, use WinPcap Developer's pack (for details, visit the WinPcap website, http://www.winpcap.org), as it provides many examples that have been configured, including all the sample code in this tutorial, and what you need to compile the runtime The include file (include) and library (libraries) Jiash assume that you have completed compiling the program and then run it. On a WinXP computer, we get the result:
1. \device\npf_{4e273621-5161-46c8-895a-48d0e52a0b83} (Realtek RTL8029 (AS) Ethernet Adapter)
2. \device\npf_{5d24ae04-c486-4a96-83fb-8b5ec6c7f430} (3Com EtherLink PCI)
Operation Result:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/89/4C/wKiom1gPCH6D5gCtAAAnlsgpyXU960.png-wh_500x0-wm_3 -wmp_4-s_2516066217.png "title=" L$ao (8e2f71et{gy~wn50]w.png "alt=" Wkiom1gpch6d5gctaaanlsgpyxu960.png-wh_50 "/>
This article is from "Sorry, your WiFi has been disconnected ~" blog, declined reprint!
Development and application of WinPcap: Getting a list of devices