0. Prerequisites
Windows:win7 x64
WinPcap version: 4.1.3
WinPcap Development Kit: 4.1.2
Target: Configure the list of network adapters installed in the target computer using WinPcap in VS2010
1. Download
http://www.winpcap.org/
Download the WinPcap installation package and development package
After the installation package is installed, unpack the development package to a directory, and the development package is free of installation.
3, in the VS2010 configuration
Configuring Header and library files
Project Properties--vc++ Directory--Include directory/library directory
4. Demo
Get a list of network cards and related data on a native/remote machine
/* ****************************** function successfully returned 0 failed to return -1*******************************/int pcap_findalldevs_ex (char *source, // Native/remote machine/File struct pcap_rmtauth *auth, // target machine user name password pcap_if_t **alldevs, // output parameters, details char *errbuf // buffer size is pcap_buf_size, function fails when error message is saved ) ;
The PCAP_FINDALLDEVS_EX function specifies the parameter "rpcap://" or a predefined macro when you specify the machine pcap_src_if_string
When specifying a remote machine, you need to follow the format "Rpcap://host:port" and the default port number is 2002
You need to specify a user name and password when the remote machine has a password.
struct pcap_rmtauth{ int type; //#define RPCAP_RMTAUTH_NULL 0 or user name password Authentication #define RPCAP_RMTAUTH_PWD 1 Char * Username; // User name Char *password; // password };
//demo1.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>#include<WinSock2.h>#include<Windows.h>//The macro have_remote must define before#ifndef have_remote#defineHave_remote#endif#include<pcap.h>#include<remote-ext.h>#pragmaComment (lib, "Ws2_32.lib")#pragmaComment (lib, "Packet.lib")#pragmaComment (lib, "Wpcap.lib")using namespacestd;/************************************************************************//*platfor Win7 x64 * version of winpcap:4.1.3 * version of developping tool:4.1.2 * notes:the local/remote machine M UST install the WINPCAP and Start the server (go to the Install path and double click Rpcapd.exe). You must the DEFAULT PORT is 2002. If another port, the PCAP_FINDALLDEVS_EX function return-1 and the Erro information in Errbuf are [is the server properly installed on XXX.XXX.XXX.XXX? Connect () Failed: Unable to connect because the target computer is actively rejecting. (Code 10061)] /************************************************************************/int_tmain (intARGC, _tchar*argv[]) { //char* Psource = "Rpcap://"; //localhost Char* Psource ="rpcap://xxx.xxx.xxx.xxx:2002";//Remote PC structPcap_rmtauth Stauth = {0}; Stauth.type=rpcap_rmtauth_pwd; Stauth.username="xxxxx"; Stauth.password="Xxxxxxxxxxx"; pcap_if_t* Ppcapift =NULL; CharChbuffer[pcap_buf_size] = {0}; intNcount =0; if(0= = PCAP_FINDALLDEVS_EX (Psource, &stauth, &Ppcapift, Chbuffer)) { for(pcap_if_t* pcap = ppcapift; Pcap! = NULL; pcap = pcap->next) {cout<< Endl <<"-----------Device"<< ncount + + <<" -------------"<<Endl; cout<< pcap->name<<Endl<< pcap->Description<<Endl<< pcap->Flags<<Endl; cout<<"--------Output details below-----"<<Endl; for(structpcap_addr* paddr = pcap->addresses; Paddr! = NULL; PADDR = paddr->next) { structsockaddr_in* psockaddr = (structsockaddr_in*) (paddr->addr); if(NULL! =psockaddr) {cout<<"IP is"<< Inet_ntoa (PSOCKADDR->SIN_ADDR) <<Endl; cout<<"Port is"<< Ntohs (Psockaddr->sin_port) <<Endl; cout<<"Family is"<< psockaddr->sin_family <<Endl; cout<<"-------"<<Endl; } psockaddr= (structsockaddr_in*) (paddr->dstaddr); if(NULL! =psockaddr) {cout<<"Mask IP is"<< Inet_ntoa (PSOCKADDR->SIN_ADDR) <<Endl; cout<<"Mask Port is"<< Ntohs (Psockaddr->sin_port) <<Endl; cout<<"Mask Family is"<< psockaddr->sin_family <<Endl; cout<<"-------"<<Endl; } psockaddr= (structsockaddr_in*) (paddr->broadaddr); if(NULL! =psockaddr) {cout<<"Broadcast IP is"<< Inet_ntoa (PSOCKADDR->SIN_ADDR) <<Endl; cout<<"Broadcast Port is"<< Ntohs (Psockaddr->sin_port) <<Endl; cout<<"Broadcast Family is"<< psockaddr->sin_family <<Endl; } psockaddr= (structsockaddr_in*) (paddr->dstaddr); if(NULL! =psockaddr) {cout<<"Peer IP is"<< Inet_ntoa (PSOCKADDR->SIN_ADDR) <<Endl; cout<<"Peer Port is"<< Ntohs (Psockaddr->sin_port) <<Endl; cout<<"Peer Family is"<< psockaddr->sin_family <<Endl; } cout<<"---------------------------------------"<< Endl << Endl <<Endl; } // for } // forPcap_freealldevs (Ppcapift); } //if Else{Cerr<< Endl <<"Last error is"<< GetLastError () <<Endl<< Chbuffer <<Endl; } System ("Pause"); return 0;}
5. Operation Result
Native test
Remote machine Testing
Under Windows configuration using WinPcap