WinPcap. 获取本地驱动设备列表
2016-06-12 15:09
316 查看
第一节:获取驱动列表
在这一节中讲述了,如何给予WinPcap获取网络适配器列表。主要通过函数pcap_findalldevs_ex(),它返回一个pcap_if结构的链表,每一个pcap_if结构保存一个适配器的信息,遍历输出所有的适配器信息后,要记得调用pcap_freealldevs()函数释放。
首先看一下pcap_findalldevs_ex()函数原型:
该函数是pcap_findalldevs()的扩展形式,它只列出本地机器的驱动设备。
Pcap_if结构:
在这一节中讲述了,如何给予WinPcap获取网络适配器列表。主要通过函数pcap_findalldevs_ex(),它返回一个pcap_if结构的链表,每一个pcap_if结构保存一个适配器的信息,遍历输出所有的适配器信息后,要记得调用pcap_freealldevs()函数释放。
首先看一下pcap_findalldevs_ex()函数原型:
int pcap_findalldevs_ex ( char * source, struct pcap_rmtauth * auth, pcap_if_t ** alldevs, char * errbuf )
该函数是pcap_findalldevs()的扩展形式,它只列出本地机器的驱动设备。
1 参数说明: 2 3 Char * source ; 适配器文件所在位置,取值有: 4 5 (1), 'rpcap://' 表示本地的适配器,此时可以用宏定义PCAP_SRC_IF_STRING。 6 7 (2), ’rpcap://hostname:prot’ 表示主机名称为hostname并且端口号为port,如本地的hostname为”localhost”,端口号一般为”80”. 8 9 (3), 'file://c:/myfolder/', 指定路径。 10 11 Struct pcap_rmtauth * auth; 指向pcap_rmtauth结构体,当连接到远程host时,需要它保存一些信息。对于本地连接时没有意义,一般去NULL。 12 13 Pcap_if_t ** alldevs; 利用结构体pcap_if存储适配器信息,并保存在链表结构的alldevs中。 14 15 Char * errbuf; 保存错误信息。 16 17 返回为0成功,否则返回-1,失败信息保存在errbuf中。
Pcap_if结构:
1 变量说明: 2 3 Struct pcap_if * next; 指向下一个结构体。 4 5 Char * name; 指向该适配器名称。 6 7 Char * description; 适配器描述内容。 8 9 Struct pcap_addr * address; 10 11 U_int flags;
/********************************************* * 代码贡献: * 注意事项: 1,第一行加上“#define HAVE_REMOTE”,这样就不要加上头文件 remote-ext.h,也就是说两者效果一样,但推荐前者。 2,很多教程中都只是添加头文件”pcap.h” 会提示相关函数无法解析,需要添加依赖库wpcap.lib 3,代码最后记得pcap_freealldevs()。 *********************************************/ #define HAVE_REMOTE #include "pcap.h" #pragma comment(lib,"wpcap.lib") int main() { pcap_if_t *alldevs; pcap_if_t *d; int i = 0; char errbuf[PCAP_ERRBUF_SIZE]; /* get local devices */ if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs_ex:%s\n",errbuf); exit(1); } /* print devices list */ for (d = alldevs;d != NULL; d = d->next ) { printf("%d. %s",++i,d->name); if (d->description) printf("(%s)\n",d->description); else printf("(No description avaliable)\n"); } if (i == 0) { printf("\nNo interfaces found! Make sure Winpcap is installed.\n"); return -1; } pcap_freealldevs(alldevs); system("pause"); return 0; }
相关文章推荐
- 按照“规则”拆分字符串
- Javascript面向对象编程一
- 数据库原理(五)--关系数据库标准语言SQL(二)
- [算法][三轴、六轴、九轴传感器算法分析] 1、分享一个三轴加速计matlab动态可视化脚本
- 【linux】/etc/passwd文件
- Protobuf3 + Netty4: 在socket上传输多种类型的protobuf数据
- 大牛胡百敬的博客
- Android中moveTo、lineTo、quadTo、cubicTo、arcTo详解(实例)
- <Flag> 从今天开始使用CSDN做笔记啦
- betterdownload.sh: 42: betterdownload.sh: Syntax error: "(" unexpected
- 数据库原理(四)--关系数据库标准语言SQL(一)
- 交换机与网桥的区别
- Google VR SDK-cardborad 播放vr视频-Android
- ASP.NET MVC 应用程序的安全性
- php测试题
- 【多个客户端同时调用存储过程,防止数据重复】的解决方法
- springMVC 415 (Unsupported Media Type)
- root账户不允许远程登陆
- leetcode 14. Longest Common Prefix
- 如何将pdf转换成jpg图片格式