当前位置: 首页 > news >正文

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、运行结果

     

     本机测试

     

 

 

    远程机器测试

    

 

  

  

 

转载于:https://www.cnblogs.com/cuish/p/4180632.html

相关文章:

  • Realtime Rendering 3rd笔记 9
  • Android中的尺寸单位
  • Android提高第七篇之XML解析与生成
  • [汇总I]精选微软等公司数据结构+算法面试100题[第1-60题汇总]
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • LSPCI具体解释分析
  • urb传输的代码分析
  • 32位Windows 7环境安装Pydasm和Pydbg
  • usb学习有感
  • LoadImage()的使用
  • linux下删除.svn命令
  • 嵌入式开发资料集锦
  • Cassandra的事务支持及数据一致性解决方案
  • 表变量和临时表
  • 常见排序算法时间复杂度
  • php的引用
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • 【347天】每日项目总结系列085(2018.01.18)
  • Flex布局到底解决了什么问题
  • JS笔记四:作用域、变量(函数)提升
  • LeetCode算法系列_0891_子序列宽度之和
  • Python 基础起步 (十) 什么叫函数?
  • 使用权重正则化较少模型过拟合
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 写代码的正确姿势
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • #HarmonyOS:Web组件的使用
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (C语言)共用体union的用法举例
  • (poj1.2.1)1970(筛选法模拟)
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (一)Linux+Windows下安装ffmpeg
  • **PHP分步表单提交思路(分页表单提交)
  • .bat批处理出现中文乱码的情况
  • .net 程序发生了一个不可捕获的异常
  • .Net(C#)自定义WinForm控件之小结篇
  • .NET导入Excel数据
  • .NET命令行(CLI)常用命令
  • .NET设计模式(7):创建型模式专题总结(Creational Pattern)
  • .Net中的集合
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑
  • @Transactional注解下,循环取序列的值,但得到的值都相同的问题
  • [145] 二叉树的后序遍历 js
  • [AI]文心一言爆火的同时,ChatGPT带来了这么多的开源项目你了解吗
  • [AMQP Connection 127.0.0.1:5672] An unexpected connection driver error occured
  • [BetterExplained]书写是为了更好的思考(转载)
  • [BZOJ 3282] Tree 【LCT】
  • [GN] Vue3.2 快速上手 ---- 核心语法2
  • [HackMyVM]靶场Crossbow
  • [hdu 3746] Cyclic Nacklace [kmp]