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.
#include "Pcap.h" main () { 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; } /* no longer requires a list of devices, release it */ pcap_freealldevs (Alldevs);}
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
On the Windows platform, you need to create a project and follow the steps in using WINPCAP programming. However, we recommend that you use the 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 the include files (include) and Libraries (libraries) required to compile the runtime
Jiash Suppose we've finished compiling the program, let's 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)
As you can see, the name of the network adapter under the Windows platform is quite easy to read, and the explanatory description is helpful!
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/89/49/wKioL1gPCPfS9yJoAAAgU_wFZNg693.png-wh_500x0-wm_3 -wmp_4-s_178393822.png "title=" 111.PNG "alt=" wkiol1gpcpfs9yjoaaagu_wfzng693.png-wh_50 "/>650) this.width=650;" Src= "http://s4.51cto.com/wyfs02/M02/89/4C/wKiom1gPCRqzJvVDAAEzYRDQhIE473.png-wh_500x0-wm_3-wmp_4-s_2239283919. PNG "title=" 222.PNG "alt=" wkiom1gpcrqzjvvdaaezyrdqhie473.png-wh_50 "/>650" this.width=650; "src=" http:// S1.51cto.com/wyfs02/m00/89/4c/wkiom1gpctkaewnqaaawk8dpuai599.png "title=" 333.PNG "alt=" Wkiom1gpctkaewnqaaawk8dpuai599.png "/>
Get a list of devices