使用pcap_findalldevs_ex 编程出现问题

使用pcap_findalldevs_ex 编程出现问题

分类: C/C++/MFC 353人阅读 评论(0) 收藏 举报
       使用pcap_findalldevs_ex()函数编程,在文档中是这样介绍这个函数的:
This function is a superset of the old 'pcap_findalldevs()', which is obsolete, and which allows listing only the devices present on the local machine.   Vice versa, pcap_findalldevs_ex() allows listing the devices present on a remote machine as well.

简单说pcap_findalldevs_ex()是pcap_findalldevs()的一个超集, 他不仅可以获取本地的设备列表,还可以获取远程计算机的社别列表,但是在将pcap_findalldevs()换成pcap_findalldevs_ex()的过程中却出现了意想不到的错误:(代码如下)

  1. #include <iostream.h>    
  2. #include <stdio.h>  
  3. #include <pcap.h>  
  4.   
  5. #define _CRT_SECURE_NO_WARNINGS  
  6. #pragma comment (lib,"wpcap.lib")  
  7.   
  8. void packet_handler(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);  
  9.   
  10. int main()  
  11. {  
  12.     pcap_t *cap_ins_des;      
  13.     pcap_if_t *alldevs;       
  14.     pcap_if_t *d;  
  15.     char errbuf[PCAP_ERRBUF_SIZE];    
  16.     int i;  
  17.   
  18.     if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1) {  
  19.         printf("%s\n", errbuf);  
  20.         exit(-1);  
  21.     }  
  22.       
  23.       
  24.     d = alldevs;  
  25.     while (d != NULL) {  
  26.         printf("%s\n", d->description == NULL ? NULL : d->description);  
  27.         d = d->next;  
  28.     }  
  29.     d = alldevs;  
  30.     scanf("%d", &i);  
  31.     while (i--)  
  32.         d = d->next;  
  33.       
  34.   
  35.     cap_ins_des = pcap_open(d->name, 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf);  
  36.     if (cap_ins_des == NULL) {  
  37.         printf("%s\n", errbuf);  
  38.         exit(-1);  
  39.     }  
  40.       
  41.   
  42.     pcap_freealldevs(alldevs);  
  43.       
  44.   
  45.     pcap_loop(cap_ins_des, 30 , packet_handler, NULL);  
  46.       
  47.     return 0;  
  48. }  
  49.   
  50. void packet_handler(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)  
  51. {  
  52.     time_t time = pkt_header->ts.tv_sec;  
  53.     struct tm *ltime = localtime(&time);  
  54.     char timestr[16];  
  55.       
  56.     (VOID)user;  
  57.     (VOID)pkt_data;  
  58.       
  59.     strftime(timestr, sizeof timestr, "%H:%M:%S", ltime);  
  60.       
  61.     printf("%s. %d, %d, %d\n", timestr, pkt_header->ts.tv_usec, pkt_header->caplen, pkt_header->len);  
  62. }  
出现错误如下:
  1. --------------------Configuration: 2 - Win32 Debug--------------------  
  2. Compiling...  
  3. 2.cpp  
  4. c:\users\administrator\desktop\mfc__temp\2.cpp(19) : error C2065: 'pcap_findalldevs_ex' : undeclared identifier  
  5. c:\users\administrator\desktop\mfc__temp\2.cpp(19) : error C2065: 'PCAP_SRC_IF_STRING' : undeclared identifier  
  6. c:\users\administrator\desktop\mfc__temp\2.cpp(36) : error C2065: 'pcap_open' : undeclared identifier  
  7. c:\users\administrator\desktop\mfc__temp\2.cpp(36) : error C2065: 'PCAP_OPENFLAG_PROMISCUOUS' : undeclared identifier  
  8. c:\users\administrator\desktop\mfc__temp\2.cpp(36) : error C2440: '=' : cannot convert from 'int' to 'struct pcap *'  
  9.         Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast  
  10. 执行 cl.exe 时出错.  
  11.   
  12. 2.obj - 1 error(s), 0 warning(s)  

有人说这是wincap的一个失误,忘记把该函数的声明文件包含进去了,我打开pcap.h看了一下,确实没有pcap_findalldevs_ex函数的声明,其实,现在的Winpcap做了更新,因为winpcap现在增加了远程捕获的功能, 在pcap_findalldevs_ex和pcap_open函数中增加了远程主机身份验证的参数struct   pcap_rmtauth   *     auth,所以将两个函数的定义转移到remote-ext.h中去了。

所以现在使用这两个参数的时候需要包含#include   <remote-ext.h>  ,但包含之后又出现问题:

  1. --------------------Configuration: 2 - Win32 Debug--------------------  
  2. Compiling...  
  3. 2.cpp  
  4. c:\program files\microsoft visual studio\vc98\include\remote-ext.h (39) : fatal error C1189: #error :  Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h  
  5. 执行 cl.exe 时出错.  
  6. Creating browse info file...  
  7. BSCMAKE: error BK1506 : cannot open file '.\Debug\2.sbr': No such file or directory  
  8. 执行 bscmake.exe 时出错.  
  9.   
  10. 2.exe - 1 error(s), 0 warning(s)  

查看错误的地方:
  1. #ifndef HAVE_REMOTE  
  2. #error Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h  
  3. #endif  

之后,修改为:


  1. #include <stdio.h>  
  2. #define HAVE_REMOTE  
  3. #include <pcap.h>  

程序终于正常运行!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值