Copyright information:
This article is from the Internet. reposted here for the study and use by network programmers. Please respect the author's Labor achievements. Unauthorized commercial use of the original author'sArticleThe consequences of infringement shall be borne by the user, and I shall not assume any legal liability.
Note: this series of articles is a learning note prepared after I read the Winpcap manual. AllCodeI learned the sample code in the Winpcap manual and passed the debugging. I modified some of the code. I respect the copyright in the Winpcap manual, I am not liable for any copyright or security threats caused by using the code in this series.
Download wpdpack_3_2_alpha1.zip (: http://www.winpcap.org/install/bin/WpdPack_3_2_alpha1.zip), decompress in addition to the documentation, examples include and Lib, so I want to use TC2 for development environment, but the compilation always has a problem, so give up. Later, after reading the Winpcap manual, I learned that because it was developed on Windows, VC ++ 6.0 is recommended, so I switched to VC instead.
The first test is:
# Include
# Include
Int main (){
Pcap_if_t * alldevs;
Pcap_if_t * D;
Int I = 0;
Char errbuf [pcap_errbuf_size];
/* Retrieve the device list from the local machine */
If (pcap_findalldevs_ex (pcap_src_if_string, null/* auth is not needed */, & alldevs, errbuf) =-1)
{
Printf ("error in pcap_findalldevs_ex: % s/n", errbuf );
Exit (1 );
}
/* Print the list */
For (D = alldevs; D! = NULL; D = D-> next)
{
/* Print the device's name */
Printf ("% d. % s", ++ I, d-> name );
/* Print the device's dashboard */
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;
}
/* We don't need any more the device list. Free It */
Pcap_freealldevs (alldevs );
Return 1;
}
Another problem occurs during compilation -- "pcap. h cannot be enabled ". Check the development manual to find the solution:
1. Install the Winpcap driver. : Http://www.winpcap.org/install/bin/winpcap_3_1.exe.
2. Add the include and Lib directories of Winpcap to the environment variables of vc6.0;
3. for each project, open the project with VC, and select "C/C ++" in "Project-> Settings ", add "wpcap" in the "Preprocessor definitions" input box, select "Link", and add "wpcap" in the "Object/library modules" input box. lib packet. lib ".
After compilation, the system is finally okay. After reading the code and reading the development manual, I learned the following:
Pcap_if is a struct. Its node is a linked list. Its definition is as follows:
Struct pcap_if {
Struct pcap_if * next;
Char * Name;
Char * description;
Struct pcap_addr * addresses;
U_int flags;
}
In pcap. h, there is a "typedef struct pcap_if pcap_if_t;", so you can also replace pcap_if_t with pcap_if.
Int pcap_findalldevs_ex (char * source,
Struct pcap_rmtauth * auth,
Pcap_if_t ** alldevs,
Char * errbuf
)
This function is a superset of 'pcap _ findalldevs. 'Pcap _ findalldevs () 'is old. It only allows listing devices on local machines. However, 'pcap _ findalldevs_ex () 'can not only list local devices but also devices on remote machines. In addition, it can list all available pcap files to the specified folder. 'Pcap _ findalldevs_ex () 'is platform independent. However, it has obtained the address of the local machine from the standard 'pcap _ findalldevs.
This article from China protocol analysis network | www.cnpaf.net Original article links: http://www.cnpaf.net/Class/winpcap/200610/16292.html