|
|
这一节将展示WinPcap的另一高级功能:收集网络流量的统计信息。WinPcap的统计引擎在内核层次上对到来的数据进行分类。如果你想了解更多的细节请查看NPF驱动指南。 这一节将展示WinPcap的另一高级功能:收集网络流量的统计信息。WinPcap的统计引擎在内核层次上对到来的数据进行分类。如果你想了解更多的细节请查看NPF驱动指南。 为了利用这个功能来监视网络,我门的程序必须打开一个网卡并用pcap_setmode()将其设置为统计模式。注意pcap_setmode()要用 MODE_STAT来将网卡设置为统计模式。 在统计模式下编写一个程序来监视TCP流量只是几行代码的事情,下面的例子说明了如何来实现该功能的。
/* * Copyright (c) 1999 - 2002 * Politecnico di Torino. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that: (1) source code distributions * retain the above copyright notice and this paragraph in its entirety, (2) * distributions including binary code include the above copyright notice and * this paragraph in its entirety in the documentation or other materials * provided with the distribution, and (3) all advertising materials mentioning * features or use of this software display the following acknowledgement: * ``This product includes software developed by the Politecnico * di Torino, and its contributors.’’ Neither the name of * the University nor the names of its contributors may be used to endorse * or promote products derived from this software without specific prior * written permission. * THIS SOFTWARE IS PROVIDED ``AS IS’’ AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
#include #include
#include
void usage();
void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);
void main(int argc, char **argv) { pcap_t *fp; char error[PCAP_ERRBUF_SIZE]; struct timeval st_ts; u_int netmask; struct bpf_program fcode;
/* Check the validity of the command line */ if (argc != 2) { usage(); return; } /* Open the output adapter */ if((fp = pcap_open_live(argv[1], 100, 1, 1000, error) ) == NULL) { fprintf(stderr,"\nError opening adapter: %s\n", error); return; }
/* Don’t care about netmask, it won’t be used for this filter */ netmask=0xffffff;
//compile the filter if(pcap_compile(fp, &fcode, "tcp", 1, netmask) <0 ){ fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n"); /* Free the device list */ return; } //set the filter if(pcap_setfilter(fp, &fcode)<0){ fprintf(stderr,"\nError setting the filter.\n"); /* Free the device list */ return; }
/* 将网卡设置为统计模式 */ pcap_setmode(fp, MODE_STAT);
printf("TCP traffic summary:\n");
/* Start the main loop */ pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);
return; }
void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data) { struct timeval *old_ts = (struct timeval *)state; u_int delay; LARGE_INTEGER Bps,Pps; struct tm *ltime; char timestr[16];
/* 从最近一次的采样以微秒计算延迟时间 */ /* This value is obtained from the timestamp that the associated with the sample. */ delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec; /* 获得每秒的比特数 */ Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay)); /* ^ ^ | | | |
|