Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1259728
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2012-02-17 12:27:13

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

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

 //在线的文档 。。

//必须包含的头文件

#define _WSPIAPI_COUNTOF

#include

#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\n", errbuf);

exit(1);

}

//1       . \Device\NPF_{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)\n", d->description);

else

printf(" (No description available)\n");

}

if(i==0)

{

printf("\nNo interfaces found! Make sure WinPcap is installed.\n");

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,"\nUnable to open the adapter. %s is not supported by WinPcap\n", 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\n", timestr, header->ts.tv_usec, header->len);

}

if(res == -1){

printf("Error reading the packets: %s\n", 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 don't point to a valid packet

    超时了。。

-1 if an error occurred  

   一个错误发生了。

-2 if EOF was reached reading from an offline capturp

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

阅读(1201) | 评论(0) | 转发(0) |
0

上一篇:linux RTC测试

下一篇:新版西游记

给主人留下些什么吧!~~