winPcap编程之不用回调方法捕获数据包(五 转)
2016-10-13 16:17
288 查看
这一次要分析的实例程序跟上一讲非常类似(“打开适配器并捕获数据包”),略微不同的一点是本次将pcap_loop()函数替换成了pcap_next_ex()函数。本节的重点也就是说一下这两个函数之间的差异。我们知道pcap_loop()函数是基于回调的原理来进行数据捕获的,如技术文档所说,这是一种精妙的方法,并且在某些场合下,它是一种很好的选择。但是在处理回调有时候会并不实用,它会增加程序的复杂度,特别是在多线程的C++程序中。而对于pcap_next_ex()函数而言,可以通过直接调用它来获得一个数据包,也只有在调用了这个函数才能收到数据包。pcap_next_ex()函数跟pcap_loop()的回调函数参数是相同的:
第一个参数是网络适配器的描述符;第二个参数是一个指向pcap_pkthdr结构体的指针;第三个参数是指向数据报数据的缓冲的指针。
来看一下实例程序的代码:
文档上在最后简单地比较了一下pcap_next_ex()函数和pcap_next()函数的区别,通过函数名我们知道pcap_next_ex()函数是在pcap_next()基础上扩展得到的。为什么会扩展?根据文档说明可以知道,pcap_next()函数有一些缺陷。比如它效率很低,尽管隐藏了回调的方式,但它仍然依赖于函数pcap_dispatch();另外,它不能检测到EOF这个状态,那么如果数据包是从文件中读取过来的,那么它就不那么好用了。显然,pcap_next_ex()函数在此基础上做出了一些改进。最后我们来看一下pcap_next_ex()函数的返回值,引用文档中的描述:
The return value can be:
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
(pcap_open_live()设置的超时时间超时,在这种情况下pkt_header和pkt_data指向一个非法的数据)
-1 if an error occurred (出错)
-2 if EOF was reached reading from an offline capture (读取到EOF,应该是文件)
int pcap_next_ex ( pcap_t * p, struct pcap_pkthdr ** pkt_header, const u_char ** pkt_data )
第一个参数是网络适配器的描述符;第二个参数是一个指向pcap_pkthdr结构体的指针;第三个参数是指向数据报数据的缓冲的指针。
来看一下实例程序的代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #include "pcap.h" typedef struct sockaddr_in sockad; /* 从tcptraceroute数字IP地址转换为字符串 */ #define IPTOSBUFFERS 12 char *iptos(u_long in) { static char output[IPTOSBUFFERS][3*4+3+1]; static short which; u_char *p; p = (u_char *)∈ which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1); sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); return output[which]; } void ifprint(pcap_if_t *d,int* i) { pcap_addr_t *a; printf("%d. %s", ++(*i), d->name); if (d->description) printf(" (%s)\n", d->description); else printf(" (No description available)\n"); for(a=d->addresses;a;a=a->next) { switch(a->addr->sa_family) { case AF_INET: if(a->addr) printf("\tIPv4地址:%s\n",iptos(((sockad *)a->addr)->sin_addr.s_addr)); break; } } } int 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; /* 获取设备列表 */ if (pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); exit(1); } /* 打印列表 */ for(d=alldevs; d; d=d->next) { ifprint(d,&i); } if(i==0) { printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); return -1; } printf("Enter the interface number (1-%d):",i); scanf("%d", &inum); if(inum < 1 || inum > i) { printf("\nInterface 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++); /* 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); /* 检索包 */ 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()函数和pcap_next()函数的区别,通过函数名我们知道pcap_next_ex()函数是在pcap_next()基础上扩展得到的。为什么会扩展?根据文档说明可以知道,pcap_next()函数有一些缺陷。比如它效率很低,尽管隐藏了回调的方式,但它仍然依赖于函数pcap_dispatch();另外,它不能检测到EOF这个状态,那么如果数据包是从文件中读取过来的,那么它就不那么好用了。显然,pcap_next_ex()函数在此基础上做出了一些改进。最后我们来看一下pcap_next_ex()函数的返回值,引用文档中的描述:
The return value can be:
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
(pcap_open_live()设置的超时时间超时,在这种情况下pkt_header和pkt_data指向一个非法的数据)
-1 if an error occurred (出错)
-2 if EOF was reached reading from an offline capture (读取到EOF,应该是文件)
相关文章推荐
- WinPcap编程【5】不用回调方法捕获数据包
- WinPcap编程【5】不用回调方法捕获数据包
- WinPcap笔记(5):不用回调方法捕获数据包
- WinPcap学习(五)不用回调方法捕获数据包
- Winpcap打开适配器并捕获数据包:不用回调方法捕获数据包
- winPcap_6_不用回调方法捕获数据包
- Winpcap打开适配器并捕获数据包:不用回调方法捕获数据包
- WinPcap教程:(4)不用回调方式捕获数据包
- 不用回调方法捕获数据包
- WinPcap编程入门(4)——不使用回调方法获取数据包
- 不用回调方法捕获数据包
- 不用回调方法捕获数据包
- 不用回调方法捕获数据包
- winPcap学习笔记4_不用回调方法捕获数据包(转)
- 不用回调方法捕获数据包
- WinPcap编程【4】打开适配器并捕获数据包
- WinPcap编程入门(3)——打开适配器并捕获数据包
- 使用WinPcap编程(2)——打开网络设备并且开始捕获数据包
- WinPcap编程4——捕获数据包
- WinPcap编程4——捕获数据包