当前位置:编程学习 > 网站相关 >>

利用WinpCap编写网络数据包分析程序

 WinPcap 是一个开源的库 用来进行windows32位平台的网络数据包的捕获和网络分析

这里的代码摘自 wpcap 里的例子  使用 C语言描述的。。

html/group__wpcapfunc.html">http://www.winpcap.org/docs/docs_40_2/html/group__wpcapfunc.html  //在线的文档 。。

//必须包含的头文件

#define _WSPIAPI_COUNTOF

#include <pcap/pcap.h>

#pragma  comment(lib,"Wpcap.lib")

//pcap 的一些函数 :

pcap_findalldevs(pcap_if_t *,char *)//得到本机所有的网卡信息 。

函数调用成功后 要使用 pcap_freealldevs(pcap_if_t *)//释放资源。。

实例代码:

pcap_if_t *alldevs;

pcap_if_t *d;

int inum;

int i=0;

pcap_t *adhandle;

char errbuf[PCAP_ERRBUF_SIZE];  //存储错误信息的数组 ,,

/* Retrieve the device list */

if(pcap_findalldevs(&alldevs, errbuf) == -1)

{

fprintf(stderr,"Error in pcap_findalldevs: %s ", errbuf);

exit(1);

}

//1       . DeviceNPF_{7FDCE731-F401-43EA-A7D6-761CAA4FFA32} (VMware Virtual Ethernet Adapter)

 网卡序号        驱动的名称                                              网卡的描述。

  i               pcap_if_t->name                                      pcap_if_t->description

  网卡的序号是从1开始的。。

/* Print the list */

for(d=alldevs; d; d=d->next)

{

printf("%d. %s", ++i, d->name);

if (d->description)

printf(" (%s) ", d->description);

else

printf(" (No description available) ");

}

if(i==0)

{

printf(" No interfaces found! Make sure WinPcap is installed. ");

return -1;

}

pcap_t*pcap_open_live(const char *, int, int, int, char *);//用来打开指定的网络适配器 就是网卡。。

/* Open the adapter */

if ((adhandle= pcap_open_live(d->name,// name of the device

65536,// portion of the packet to capture.

// 65536 grants that the whole packet will be captured on all the MACs.

1,// promiscuous mode (nonzero means promiscuous)

               //把网卡设为混杂模式

//就是接收所有经网络传送的数据包并读取的方式

1000,// read timeout  //超时的时间 毫秒

errbuf// error buffer

)) == NULL)

{

fprintf(stderr," Unable to open the adapter. %s is not supported by WinPcap ", d->name);

/* Free the device list */

pcap_freealldevs(alldevs);

return -1;

}

//开始监听 指定的网卡

intpcap_loop(pcap_t *,  //pcap_open_live 函数的返回值。相当于一个句柄 ,,

                 int,

pcap_handler,  //一个回调函数 每当数据包到来时 这个函数就被调用。。

u_char *  //指向网卡收到或发送 数据缓冲区的指针,。这个指针配合

);

void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)//回调函数的格式。。 

{

pcap_close(adhandle); //程序要调用 pcap_close 去关闭pcap_open_live返回的”句柄“

调用 pcap_loop 配合回调函数是得到网卡数据的一种方法 ,还有一种方法是使用 pcap_next_ex

int pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);

使用的实例:

while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0)

{

if(res == 0)  //超时了。。

/* Timeout elapsed */

continue;

/* convert the timestamp to readable format */

local_tv_sec = header->ts.tv_sec;

ltime=localtime(&local_tv_sec);

strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

printf("%s,%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

}

if(res == -1){

printf("Error reading the packets: %s ", pcap_geterr(adhandle));

return -1;

}

这个函数的返回值 :


1 if the packet has been read without problems


0 if the timeout set with pcap_open_live() has elapsed. In this case pkt_header and pkt_data dont point to a valid packet

    超时了。。

-1 if an error occurred 

   一个错误发生了。

-2 if EOF was reached reading from an offline capturp

   一个数据包文件已经度完了。。

 

补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,