【VS开发】使用WinPcap编程(1)——获取网络设备信息
pcap_if_t是一个interface数据结构,表明网络接口的信息。网络接口就是interface,就是我们用来上网的设备,一般为网卡,还有一些虚拟网卡也算作这样的接口。它的结构如下:
struct pcap_if {
struct pcap_if *next;
char *name;
char *description;
struct pcap_addr *addresses;
u_int flags;
}; typedef struct pcap_if pcap_if_t;
其中的struct pcap_addr为:
struct pcap_addr {
struct pcap_addr *next;
struct sockaddr *addr;
struct sockaddr *netmask;
struct sockaddr *broadaddr;
struct sockaddr *dstaddr;
};
当我们要获网络接口的时候,这个数据结构是非常重要的。
这里只说Local host的设备的获取,对于远程设备,虽然有方法可以获取,不过我试了试,总是出错。
首先使用pcap_createsrcstr()函数来初始化一个source,指明我们在哪里查找设备,可以是本地机器,可以是远程机器,可以是本地文件夹(这样找的就是存储好的tcpdump的数据文件)。其函数原型如下:
int pcap_createsrcstr (char *source, int type, const char *host, const char *port, const char *name, char *errbuf)
1、source就是我们要存储的位置信息。
2、type有本地文件、本地机器、远程机器可供选择,分别为:PCAP_SRC_FILE, PCAP_SRC_IFLOCAL, PCAP_SRC_IFREMOTE
3、host是远程机器的名字,可以是"1.2.3.4”这样,也可以是"a.com”这样。本地为NULL。
4、port为远程端口。本地为NULL。
5、name为接口的名字。比如eth0。一般为NULL。如果是本地文件就是本地文件夹地址。
6、errbuf存储错误信息。
然后使用函数pcap_findalldevs_ex()这个函数来获取网络设备,其原型如下:
int pcap_findalldevs_ex (char * source,
struct pcap_rmtauth * auth,
pcap_if_t ** alldevs,
char * errbuf
)
1、source可以使用上面设置好的source,也可以使用:PCAP_SRC_FILE_STRING 或者 PCAP_SRC_IF_STRING,分别是文件和接口的字符串。"file://",
"rpcap://"。
2、auth是远程登录信息,有用户名、密码、类型。用户名和密码都是字符指针,类型有:RPCAP_RMTAUTH_NULL 和 RPCAP_RMTAUTH_PWD。参看这里。
3、alldevs用于存储返回的接口信息。我们要事先定义pcap_if_t *alldevs,这是一个链表,存储接口信息。
4、errbuf出错信息。
这样所获得的alldevs就是一个接口的链表,如果是文件就是一个文件的链表。我们就可以用指针对这些链表进行操作了。
最后不用这些设备的时候要把他们释放掉,使用函数pcap_freealldevs(),其原型如下:
void pcap_freealldevs (pcap_if_t *alldevsp)
附上一个源代码:
#include "pcap.h" int main()
{
pcap_if_t *alldevs; // pcap_if_t is interface type
pcap_if_t *d;
char errbuf[PCAP_ERRBUF_SIZE]; // store error information
int i;
// struct pcap_rmtauth Ubuntu_cat;
char source[PCAP_BUF_SIZE]; if (pcap_createsrcstr(source, PCAP_SRC_FILE,
NULL, NULL,
"E:\\My Documents\\MyProgram\\WinPcapPro\\Read one packet\\Read one packet",
errbuf) == -1) {
printf("Error in create source string: %s\n", errbuf);
exit(-1);
} printf("%s\n", source); if(pcap_findalldevs_ex(source, // can be PCAP_SRC_IF_STRING for local host
NULL, // auth to remote host. NULL if local host
&alldevs,
errbuf) == -1) {
printf("Error in find all devices: %s\n", errbuf);
exit(1);
} d = alldevs;
while(d != NULL) {
printf("%s\n%s\nAddress: ", d->name, d->description);
for (i = 0; d->addresses != NULL && i < 14; i++)
printf("%d ", d->addresses->addr->sa_data[i]);
printf("\n\n");
d = d->next;
} pcap_freealldevs(alldevs); return 0;
}
最新文章
- JVM内存分析工具MAT使用
- Tiny PXE Server简介
- QA在网站建设中的作用
- DataSnap数据库连接池,数据集对象池的应用
- temorrow read
- Wix学习整理(1)——快速入门HelloWorld
- ASP提取字段中的图片地址
- VS2017打开VS2010项目报 “找不到*.xaml”错误
- SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)
- php 数组数字 补零
- English trip V1 - B 16. Giving Reasons 提供个人信息 Teacher:Lamb Key: Why/Because
- 异常处理(try...catch...final 和 throw , throws)
- bacula备份终端操作bconsole指令
- Wpf中显示Unicode字符
- (第九周)Beta-1阶段成员贡献分
- UnityShader 序列帧动画效果
- Logistic Regression-Cost Fuction
- 树莓派的WIFI配置
- 轻松完成excel读写操作- 基于POI的框架BingExcel的使用(2)
- Hadoop部署启动异常问题排查