一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
 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

    获取本机 / 远程机器上网卡的列表和相关数据

 1 /*******************************
 2 函数成功返回 0
 3 失败返回      -1
 4 *******************************/
 5 int 
 6 pcap_findalldevs_ex(
 7 char *source,                //本机/远程机器/文件
 8 struct pcap_rmtauth *auth,   //目标机器用户名 密码
 9 pcap_if_t **alldevs,         //输出参数,详细信息
10 char *errbuf                 //缓冲区 大小为PCAP_BUF_SIZE,函数失败时保存错误信息
11 );
pcap_findalldevs_ex函数指定本机时指定参数"rpcap://" 或 预定义宏PCAP_SRC_IF_STRING
当指定远程机器时需要按照"rpcap://host:port"的格式,默认端口号为2002
远程机器有密码时需要指定用户名和密码。
1 struct pcap_rmtauth
2 {
3     
4     int type;   //#define RPCAP_RMTAUTH_NULL 0  或   用户名密码验证 #define RPCAP_RMTAUTH_PWD 1
5     
6     char *username;  //用户名
7    
8     char *password;  //密码
9 };
  1 // demo1.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <iostream>
  6 #include <WinSock2.h>
  7 #include <Windows.h>
  8 
  9 //the macro HAVE_REMOTE must define before
 10 #ifndef  HAVE_REMOTE
 11 #define HAVE_REMOTE
 12 #endif
 13 
 14 #include <pcap.h>
 15 #include <remote-ext.h>
 16 
 17 #pragma comment(lib, "ws2_32.lib")
 18 #pragma comment(lib, "packet.lib")
 19 #pragma comment(lib, "wpcap.lib")
 20 
 21 using namespace std;
 22 
 23 
 24 /************************************************************************/
 25 /* platfor win7 x64
 26  * version of winpcap: 4.1.3
 27  * version of developping tool: 4.1.2
 28 
 29  * notes: The local/remote machine must install the Winpcap
 30           and 
 31           Start the server(go to the install path and double click rpcapd.exe).
 32 
 33           You must look out that the DEFAULT PORT  is 2002. 
 34           If you use another port, the pcap_findalldevs_ex  function return -1
 35           and the erro information in errbuf is 
 36           [Is the server properly installed on XXX.XXX.XXX.XXX?  
 37           connect() failed: 由于目标计算机积极拒绝,无法连接。  (code 10061) ]
 38 
 39 /************************************************************************/
 40 
 41 int _tmain(int argc, _TCHAR* argv[])
 42 {
 43     //char* pSource = "rpcap://";                  //localhost
 44     char* pSource = "rpcap://XXX.XXX.XXX.XXX:2002";  //remote PC
 45 
 46     struct pcap_rmtauth stAuth = {0};
 47     stAuth.type = RPCAP_RMTAUTH_PWD;     
 48     stAuth.username = "xxxxx";
 49     stAuth.password = "xxxxxxxxxxx";
 50 
 51     pcap_if_t* pPcapIft = NULL;
 52     char chBuffer[PCAP_BUF_SIZE] = {0};
 53 
 54     
 55     int nCount = 0;
 56 
 57     if (0 == pcap_findalldevs_ex(pSource, &stAuth, &pPcapIft, chBuffer))
 58     {
 59         for (pcap_if_t* pcap = pPcapIft; pcap != NULL; pcap = pcap->next)
 60         {
 61             cout << endl << "-----------  device "
 62                  << nCount ++
 63                  << " -------------" << endl;
 64 
 65             cout << pcap->name 
 66                  << endl
 67                  << pcap->description
 68                  << endl
 69                  << pcap->flags
 70                  << endl;
 71 
 72             cout << "-------- Output details below -----" << endl;
 73 
 74             for (struct pcap_addr* pAddr = pcap->addresses;
 75                 pAddr != NULL; pAddr = pAddr->next)
 76             {
 77                 
 78                 struct sockaddr_in* psockAddr = (struct sockaddr_in*)(pAddr->addr);
 79                 if (NULL != psockAddr)
 80                 {
 81                     cout << "IP is " << inet_ntoa(psockAddr->sin_addr) << endl;
 82                     cout << "Port is " << ntohs(psockAddr->sin_port) << endl;
 83                     cout << "Family is " << psockAddr->sin_family << endl;
 84 
 85                     cout << "-------" << endl;
 86                 }
 87                 
 88 
 89                 psockAddr = (struct sockaddr_in*)(pAddr->dstaddr);
 90                 if (NULL != psockAddr)
 91                 {
 92                     cout << "Mask IP is " << inet_ntoa(psockAddr->sin_addr) << endl;
 93                     cout << "Mask Port is " << ntohs(psockAddr->sin_port) << endl;
 94                     cout << "Mask Family is " << psockAddr->sin_family << endl;
 95 
 96                     cout << "-------" << endl;
 97                 }
 98 
 99                 
100 
101 
102                 psockAddr = (struct sockaddr_in*)(pAddr->broadaddr);
103                 if (NULL != psockAddr)
104                 {
105                     cout << "Broadcast IP is " << inet_ntoa(psockAddr->sin_addr) << endl;
106                     cout << "Broadcast Port is " << ntohs(psockAddr->sin_port) << endl;
107                     cout << "Broadcast Family is " << psockAddr->sin_family << endl;
108 
109                 }
110 
111 
112                 psockAddr = (struct sockaddr_in*)(pAddr->dstaddr);
113                 if (NULL != psockAddr)
114                 {
115                     cout << "P2P IP is " << inet_ntoa(psockAddr->sin_addr) << endl;
116                     cout << "P2P Port is " << ntohs(psockAddr->sin_port) << endl;
117                     cout << "P2P Family is " << psockAddr->sin_family << endl;
118                 }
119 
120                 cout << "---------------------------------------" << endl << endl << endl;
121                 
122             } //for
123 
124 
125         } //for
126 
127 
128         pcap_freealldevs(pPcapIft);
129 
130     } //if
131     else
132     {
133         cerr << endl << "Last error is " << GetLastError() << endl
134              << chBuffer << endl;
135     }
136 
137     system("pause");
138 
139     return 0;
140 }

5、运行结果

     本机测试

  远程机器测试

posted on 2024-04-08 14:24  一杯清酒邀明月  阅读(13)  评论(0编辑  收藏  举报

/* 看板娘 */