WinPcap Pcap_findalldevs_ex() pcap_if

来源:互联网 发布:自制手机铃声软件 编辑:程序博客网 时间:2024/05/17 09:36

①pcap_if 结构:


typedef struct pcap_if pcap_if_t

cap_if在incs/pcap.h文件的72行有定义
⑵pcap_if结构里面的数据域:

struct pcap_if{ pcap_if *  next; //指向下一个元素的指针;如果是NULL,表示链表结束。 char *  name  ;//一个指向字符串的指针,该字符串是winpcap为网络接口卡分配的名字,作为一个参数传递给pcap_open_live(),用于打开网卡 char *description;//   textual description of interface, or NULL pcap_addr * addresses ;//指向接口的地址列表的第一个元素 u_int flags;//标志是不是回送网卡。 PCAP_IF_INTERFACE 标志。当接口是回送接口(loopback)标志是PCAP_IF_LOOPBACK  }

②pcap_addr结构:代表一个接口地址,在pcap.h中定义
数据域:
pcap_addr * next
//if not NULL, a pointer to the next element in the list; NULL for the last element of the list
sockaddr * addr
//a pointer to a struct sockaddr containing an address
sockaddr * netmask
// if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding to the address pointed to by addr.
sockaddr *broadaddr
// if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre­ sponding to the address pointed to by addr; may be null if the interface doesn’t support broadcasts
sockaddr * dstaddr
// if not NULL, a pointer to a struct sockaddr that contains the destination address corre­ sponding to the address pointed to by addr; may be null if the interface isn’t a point- to-point interface
⑶函数说明

pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf  )

pcap_findalldevs_ex函数获得网络设备的一个列表,并且这个列表可以被pcap_open()打开。
Pcap_findalldevs_ex()是pcap_findalldevs()的超集,后者是以前的老函数,它只能列出在本地机器的设备。相反,pcap_findalldevs_ex可以列出远程及其上面的设备。除此之外,它还可以列出某个给定的具体目录下面的所有的pcap文件的列表。而且,pcap_findalldevs_ex()还是平台独立的,因为它依靠标准的pcap_findalldevs()来在本地机器上获得地址 。和pcap_findalldevs()获得接口名称(alldevs->name )已经可以直接作为参数传递给pcap_open()进行调用。但是pcap_findalldevs_ex()不能,它得到的结果必须要先用Pcap_createsrcstr()进行格式化,然后把source 标识符传递给pcap_open()。
如果该函数必须列出远程机器上的接口,它打开一个新的控制连接来连接远程机器,然后获得接口,完成后就释放连接。但是,如果该函数检测到远程机器是处于非活动状态,这个连接不会被释放。
参数说明:
Source : 指定从哪获取网络接口设备列表。它和pcap_open()使用通用的语法。它是一个字符串,用来存放源位置(source location),例如:source 可以是”rpcap://”,表示本地适配器;也可以是”rpcap://host:port”,表示远程主机上面的适配器;还可以是pcap文件,例如”rpcap://c:/myfolder/”。

auth:指向pcap_rmtauth结构的指针,用来保存连接到远程主机上授权信息。在查询本地机器时,此参数没什么意义,可以为NULL。

alldevs: 指向pcap_if_t结构的指针,该指针不需要初始化,它会在函数的调用过程中进行初始化。此函数返回时,该指针被设置为所获得的设备接口列表的第一个元素,列表的每一个元素都是Pcap_if_t结构。

返回值:成功返回0,alldevs返回设备列表,alldevs不会为NULL。否则返回-1,那就是说系统没有任何接口可以列举的。出错的消息在errbuf里面返回,错误可能由下面的原因造成的:

注意:
接口列表一定要手动释放,通过调用pcap_freealldevs()函数。

Source参数的语法:
⑴两个宏定义:
#define PCAP_SRC_FILE_STRING “ file://”
#define PCAP_SRC_IF_STRING “rpcap://”
此两个宏在remote-ext.h里面定义。
(2)详细描述
下面列举出了能够被pcap_open()函数打开的格式:
file://path_and_filename [打开一个本地文件]
rpcap://devicename [打开本地机器上面的可以打开的设备,不使用rpcap协议]
rpcap://host/devicename [打开远程机子上可以打开的设备]
rpcap://host:port/devicename [打开远程机器上面选择的设备,用一个非标准端口作为rpcap]
adaptername [打开一个本地适配器,kept for compability,不推荐]
(NULL) [打开第一个本地适配器,kept for compability,不推荐]

Pcap_findalldevs_ex()允许的格式如下:

file://folder/ [列出指定目录的所有文件]
rpcap:// [列出本地的适配器]
rpcap://host:port/ [列出远程机器上的可以列出的设备]

关于host和port参数,可以为数字或者字母。由于支持IPV6,它们可以是下面的格式:
host (literal): e.g. host.foo.bar
host (numeric IPv4): e.g. 10.11.12.13
host (numeric IPv4, IPv6 style): e.g. [10.11.12.13]
host (numeric IPv6): e.g. [1:2:3::4]
port: can be either numeric (e.g. ‘80’) or literal (e.g. ‘http’)

0 0