日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Windows下配置使用WinPcap

發布時間:2023/12/13 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 Windows下配置使用WinPcap 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。