Windows下配置使用WinPcap

來源:互聯網
上載者:User

標籤:

 

 0、前提

     windows: win7 x64

     WinPcap版本:4.1.3

     WinPcap開發包:4.1.2

     目標:在VS2010中配置使用winpcap 擷取目標電腦中安裝的網卡列表

 

 1、下載

    http://www.winpcap.org/

 

    

 

 

    下載winpcap安裝包 和 開發包

    安裝包安裝完畢後,解壓開發包到某個目錄即可,開發包免安裝。

    

 

 

 3、在VS2010中配置

 

    配置標頭檔 和 庫檔案

    項目屬性--VC++目錄--包含目錄 / 庫目錄

    

    

 

 

 

 4、Demo

    擷取本機 / 遠程機器上網卡的列表和相關資料

    

/*******************************函數成功返回 0失敗返回      -1*******************************/int pcap_findalldevs_ex(char *source,                //本機/遠程機器/檔案struct pcap_rmtauth *auth,   //目標機器使用者名稱 密碼pcap_if_t **alldevs,         //輸出參數,詳細資料char *errbuf                 //緩衝區 大小為PCAP_BUF_SIZE,函數失敗時儲存錯誤資訊);

  

pcap_findalldevs_ex函數指定本機時指定參數"rpcap://" 或 預定義宏PCAP_SRC_IF_STRING
當指定遠程機器時需要按照"rpcap://host:port"的格式,預設連接埠號碼為2002
遠程機器有密碼時需要指定使用者名稱和密碼。

struct pcap_rmtauth{        int type;   //#define RPCAP_RMTAUTH_NULL 0  或   使用者名稱密碼驗證 #define RPCAP_RMTAUTH_PWD 1        char *username;  //使用者名稱        char *password;  //密碼};

 

    

// demo1.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include <iostream>#include <WinSock2.h>#include <Windows.h>//the macro HAVE_REMOTE must define before#ifndef  HAVE_REMOTE#define HAVE_REMOTE#endif#include <pcap.h>#include <remote-ext.h>#pragma comment(lib, "ws2_32.lib")#pragma comment(lib, "packet.lib")#pragma comment(lib, "wpcap.lib")using namespace std;/************************************************************************//* platfor win7 x64 * version of winpcap: 4.1.3 * version of developping tool: 4.1.2 * notes: The local/remote machine must install the Winpcap          and           Start the server(go to the install path and double click rpcapd.exe).          You must look out that the DEFAULT PORT  is 2002.           If you use another port, the pcap_findalldevs_ex  function return -1          and the erro information in errbuf is           [Is the server properly installed on XXX.XXX.XXX.XXX?            connect() failed: 由於目標電腦積極拒絕,無法串連。  (code 10061) ]/************************************************************************/int _tmain(int argc, _TCHAR* argv[]){    //char* pSource = "rpcap://";                  //localhost    char* pSource = "rpcap://XXX.XXX.XXX.XXX:2002";  //remote PC    struct pcap_rmtauth stAuth = {0};    stAuth.type = RPCAP_RMTAUTH_PWD;         stAuth.username = "xxxxx";    stAuth.password = "xxxxxxxxxxx";    pcap_if_t* pPcapIft = NULL;    char chBuffer[PCAP_BUF_SIZE] = {0};        int nCount = 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 (struct pcap_addr* pAddr = pcap->addresses;                pAddr != NULL; pAddr = pAddr->next)            {                                struct sockaddr_in* psockAddr = (struct sockaddr_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 = (struct sockaddr_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 = (struct sockaddr_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 = (struct sockaddr_in*)(pAddr->dstaddr);                if (NULL != psockAddr)                {                    cout << "P2P IP is " << inet_ntoa(psockAddr->sin_addr) << endl;                    cout << "P2P Port is " << ntohs(psockAddr->sin_port) << endl;                    cout << "P2P Family is " << psockAddr->sin_family << endl;                }                cout << "---------------------------------------" << endl << endl << endl;                            } //for        } //for        pcap_freealldevs(pPcapIft);    } //if    else    {        cerr << endl << "Last error is " << GetLastError() << endl             << chBuffer << endl;    }    system("pause");    return 0;}

 

    

 

 5、運行結果

     

     本機測試

     

 

 

    遠程機器測試

    

 

  

  

 

Windows下配置使用WinPcap

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.