zoukankan      html  css  js  c++  java
  • 使用WinPcap编程(1)——获取网络设备信息

    image

          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_FILEPCAP_SRC_IFLOCALPCAP_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_NULLRPCAP_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;
    }
  • 相关阅读:
    JFreeChart学习笔记
    Java基于JavaMail实现向QQ邮箱发送邮件遇到的问题
    JBPM入门
    eclipse jbmp 4.4 插件的安装及配置
    Redis开启远程登录连接
    springmvc @ResponseBody返回json 报406 not acceptable
    generatorConfiguration配置详解
    WCF、WebAPI、WCFREST、WebService之间的区别
    理解nodejs和react之间的关系
    DB2 sqlCode-668
  • 原文地址:https://www.cnblogs.com/wangshuo/p/2114164.html
Copyright © 2011-2022 走看看