打开适配器并捕获数据包
现在我们已经看到如何获得适配器来玩,让我们开始真正的工作,打开适配器并捕获一些流量。
我们将编写一个程序,其中打印出一些关于流经适配器的数据包的信息。
打开捕获设备的功能是pcap_open()。参数,snaplen,标志和to_ms值得一些解释。
打开捕获设备的功能是pcap_open()。参数,snaplen,标志和to_ms值得一些解释。
snaplen指定要捕获的数据包的部分。在某些操作系统(如xBSD和Win32)上,数据包驱动程序可以配置为仅捕获任何数据包的初始部分:这会减少要复制到应用程序的数据量,从而提高捕获效率。在这种情况下,我们使用的值65536高于我们可能遇到的最大MTU值。以这种方式,我们确保应用程序将始终收到整个数据包。
标志:最重要的标志是指示适配器是否将处于混杂模式的标志。在正常操作中,适配器仅捕获来自网络的分发给它的分组; 因此,其他主机交换的数据包被忽略。相反,当适配器处于混杂模式时,它将捕获所有数据包是否注定到它。这意味着在共享介质(如非交换式以太网)上,WinPcap将能够捕获其他主机的数据包。混杂模式是大多数捕获应用程序的默认模式,因此我们在以下示例中启用它。
to_ms指定读取超时(以毫秒为单位)。即使没有来自网络的数据包,适配器上的读取(例如,使用pcap_dispatch()或pcap_next_ex())将始终在to_ms毫秒之后返回。如果适配器处于统计模式,to_ms还会定义统计报告之间的间隔。将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);
int main()
{
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 on the local machine */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &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 interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf_s("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface 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 device */
if ( (adhandle= pcap_open(d->name, // name of the device
65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
1000, // read timeout
NULL, // authentication on the remote machine
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;
}
printf("\nlistening on %s...\n", d->description);
/* At this point, we don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
/* start the capture */
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm ltime;
char timestr[16];
time_t local_tv_sec;
/*
* unused variables
*/
(VOID)(param);
(VOID)(pkt_data);
/* convert the timestamp to readable format */
local_tv_sec = header->ts.tv_sec;
localtime_s(<ime, &local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", <ime);
printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
}
一旦适配器打开,捕获可以用pcap_dispatch()或pcap_loop()启动。这两个函数非常相似,不同之处在于pcap_ dispatch()返回(尽管不能保证)超时时pcap_loop()不会返回,直到cnt数据包被捕获为止,因此它可以在利用不足的网络。pcap_loop()足以用于此示例,而pcap_dispatch()通常用于更复杂的程序中。
这两个函数都有一个回调参数packet_handler,指向一个将接收数据包的函数。该函数由libpcap为来自网络的每个新数据包调用,并接收通用状态(对应于pcap_loop()和pcap_dispatch()的用户参数),该报头具有数据包的一些信息,如时间戳和长度,数据包的实际数据包括所有协议头。请注意,帧CRC通常不存在,因为在帧验证后由网络适配器删除它。还要注意,大多数适配器丢弃CRC错误的数据包,因此WinPcap通常不能捕获它们。
上述示例从pcap_pkthdr头提取每个数据包的时间戳和长度,并将其打印在屏幕上。
请注意,使用pcap_loop()可能存在缺点,主要涉及到数据包捕获驱动程序调用处理程序的事实; 因此用户应用程序没有直接的控制权。另一种方法(并且具有更多可读程序)是使用pcap_next_ex()函数,该函数在下一个示例中显示(捕获不带回调的数据包)。