WinPcap教程2——据说为珊瑚虫所译

来源:互联网 发布:mac excel 提取数据 编辑:程序博客网 时间:2024/06/04 07:20

(三)打开网卡捕获数据包


现在我门已经知道了如何去获得网卡的信息现在就让我们开始真正的工作:打开网卡并捕获数据流。在这一节

里我们将写一个打印流经网络的每个数据包信息的程序。打开网卡的功能是通过pcap_open_live()来实现的它

有三个参数snaplen promisc to_ms。

snaplen用于指定所捕获包的特定部分,在一些系统上(象xBSD and Win32等)驱动只给出所捕获数据包

的一部分而不是全部,这样就减少了拷贝数据的数量从而提高了包捕获的效率。

promisc指明网卡处于混杂模式,在正常情况下网卡只接受去往它的包而去往其他主机的数据包则被忽略

。相反当网卡处于混杂 模式时他将接收所有的流经它的数据包:这就意味着在共享介质的情况下我门可以捕获

到其它主机的数据包。大部分的包捕获程序都将混杂模式设为默认,所有我们在下面的例子里也将网卡设为混杂模式。

to_ms 参数指定读数据的超时控制,超时以毫秒计算。当在超时时间内网卡上没有数据到来时对网卡的读

操作将返回(如pcap_dispatch() or pcap_next_ex()等函数)。还有,如果网卡处于统计模式下(请查看“统计和收集网络数据流一节”)to_ms还定义了统计的时间间隔。如果该参数为0那么意味着没有超时控制,对网卡的读操作在没有数据到来是将永远堵塞。如果为-1那么对网卡的读操作将立即返回不管有没有数据可读。

 

#include "pcap.h"

/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];

/* 获得网卡的列表 */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}

/* 打印网卡信息 */
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 inte***ces found! Make sure WinPcap is installed.\n");
return -1;
}

printf("Enter the inte***ce number (1-%d):",i);
scanf("%d", &inum); //输入要选择打开的网卡号

if(inum < 1 || inum > i) //判断号的合法性
{
printf("\nInte***ce number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

/* 找到要选择的网卡结构 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* 打开选择的网卡 */
if ( (adhandle= pcap_open_live(d->name, // 设备名称
65536, // portion of the packet to capture. 
// 65536 grants that the whole packet will be captured on all the MACs.
1, // 混杂模式
1000, // 读超时为1秒
errbuf // error buffer
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

printf("\nlistening on %s...\n", d->description);

/* At this point, we don‘t need any more the device list. Free it */
pcap_freealldevs(alldevs);

/* 开始捕获包 */
pcap_loop(adhandle, 0, packet_handler, NULL);

return 0;
}


/* 对每一个到来的数据包调用该函数 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];

/* 将时间戳转变为易读的标准格式*/
ltime=localtime(&header->ts.tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

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

}

一旦网卡被打开,旧可以调用pcap_dispatch() 或pcap_loop()进行数据的捕获,这两个函数的功能十分相似不

同的是pcap_ dispatch()可以不被阻塞,而pcap_loop()在没有数据流到达时将阻塞。在这个简单的例子里用

pcap_loop()就足够了,而在一些复杂的程序里往往用pcap_dispatch()。
Once the adapter is opened, the capture can be started with pcap_dispatch() or pcap_loop(). These 

two functions are very similar, the difference is that pcap_ dispatch() is granted to return when 

the expires while pcap_loop() doesn‘t return until cnt packets have been captured, so it can 

block for an arbitrary period on a few utilized network. pcap_loop() is enough for the purpose of 

this sample, while pcap_dispatch() is normally used in more complex program.
这两个函数都有返回的参数,一个指向某个函数(该函数用来接受数据如该程序中的packet_handler)的指针
,libpcap调用该函数对每个从网上到来的数据包进行处理和接收数据包。另一个参数是带有时间戳和包长等信

息的头部,最后一个是含有所有协议头部数据报的实际数据。注意MAC的冗余校验码一般不出现,因为当一个桢

到达并被确认后网卡就把它删除了,同样需要注意的是大多数网卡会丢掉冗余码出错的数据包,所以WinPcap一

般不能够捕获这些出错的数据报。

刚才的例子里从pcap_pkthdr中提取出了每个数据报的时间戳和长度并在显示器上打印出了他们。 


(四)不用loopback捕获数据报

这节的例子很象先前的一章(获得网卡的高级信息)但是这一节中是用pcap_next_ex()来代替pcap_loop()来捕

获数据包。基于回调包捕获机制的pcap_loop()在某些情况下是不错的选择。但是在一些情况下处理回调并不特

别好:这会使程序变的复杂并且在象多线程或C++类这些情况下它看起来到象一块绊脚石。

在这些情况下pcap_next_ex()允许直接调用来接收包,它的参数和pcap_loop()相同:有一个网卡描述副,和两

个指针,这两个指针会被初始化并返回给用户,一个是pcap_pkthdr结构,另一个是接收数据的缓冲区。

下面的程序我门将循环调用前一节的例子中的回掉部分,只是把它移到了main里面了。

 

#include "pcap.h"


main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
int res;
char errbuf[PCAP_ERRBUF_SIZE];
struct tm *ltime;
char timestr[16];
struct pcap_pkthdr *header;
u_char *pkt_data;


/* Retrieve the device list */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(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 inte***ces found! Make sure WinPcap is installed.\n");
return -1;
}

printf("Enter the inte***ce number (1-%d):",i);
scanf("%d", &inum);

if(inum < 1 || inum > i)
{
printf("\nInte***ce number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* 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
1000, // read timeout
errbuf // error buffer
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

printf("\nlistening on %s...\n", d->description);

/* At this point, we don‘t need any more the device list. Free it */
pcap_freealldevs(alldevs);

/* 此处循环调用 pcap_next_ex来接受数据报*/
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){

if(res == 0)
/* Timeout elapsed */
continue;

/* convert the timestamp to readable format */
ltime=localtime(&header->ts.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;
}

return 0;
}


注:pcap_next_ex()只在Win32环境下才行因为它不是原始libpcap API中的一部分,这就意味着依赖于这个函

数的代码不会在UNIX下工作。那么为什么我们用pcap_next_ex()而不用pcap_next()?因为pcap_next()有许多

限制在很多情况下并不鼓励用它。首先它的效率很低因为它隐藏了回掉方法并且还依赖于pcap_dispatch()这个

函数。再次它不能够识别文件结束标志EOF所以对来自文件的数据流它几乎无能为力。
注意当pcap_next_ex()在成功,超时,出错和文件结束的情况下会返回不同的值。

 

此文章来自于【http://blog.163.com/szlear@126/blog/static/637030312005710017340/】

原创粉丝点击