大鹏鸟是谁的坐骑:WinPcap基础知识(第九课:统计网络的通信量)
来源:百度文库 编辑:中财网 时间:2024/05/08 19:32:46
这节课我们将要学习WinPcap的另外一个高级特性:对网络通信量进行统计。统计引擎使得内核级的包过滤器能有效的把收集到的包分类。你可以查阅NPF 驱动内部手册看到更多的细节。为了用这个功能来监视网络,程序员必须要打开一个适配器,并且把它设为统计模式。可以用函数pcap_setmode()来完成这个功能。特别的,在这个函数里面MODE_STAT必须作为模式参数。 用统计模式,我们写一个监视TCP通信负载的应用程序,只需要很少行代码。下面是例子代码:#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 errbuf[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(argv[1], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL) { fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf); 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"); pcap_close(fp); /* Free the device list */ return; } /* Put the interface in statstics mode */ if (pcap_setmode(fp, MODE_STAT)<0) { fprintf(stderr,"\nError setting the mode.\n"); pcap_close(fp); /* Free the device list */ return; } printf("TCP traffic summary:\n"); /* Start the main loop */ pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts); pcap_close(fp); 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]; time_t local_tv_sec; /* Calculate the delay in microseconds from the last sample. */ /* 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; /* Get the number of Bits per second */ Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay)); /* ^ ^ | | | | | | converts bytes in bits -- | | delay is expressed in microseconds -- */ /* Get the number of Packets per second */ Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay)); /* Convert the timestamp to readable format */ local_tv_sec = header->ts.tv_sec; localtime_s( strftime( timestr, sizeof timestr, "%H:%M:%S", /* Print timestamp*/ printf("%s ", timestr); /* Print the samples */ printf("BPS=%I64u ", Bps.QuadPart); printf("PPS=%I64u\n", Pps.QuadPart); //store current timestamp old_ts->tv_sec=header->ts.tv_sec; old_ts->tv_usec=header->ts.tv_usec; } void usage() { printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n"); printf("\nUsage:\n"); printf("\t tcptop adapter\n"); printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n"); exit(0); }
- #include
网络基础知识问答
网络方面的基础知识
网络基础知识视频教程下载
关于网络的基础知识
网络方面的最基础知识
关于网络方面的基础知识
什么是WinPcap?
WinPcap是什么?
使用网络剪刀手时出现“Sinff err!Please Reinstall Winpcap!”的提示,怎么解决?
我装的网络剪刀为什么会出现:sniff err!please Reinstall winpcap
使用C语言 WinPcap 网络底层编程中如何手动输入MAC地址
安装ARP病毒补丁后为什么网络嗅探软件的WinPcap加载不了?
问几个关于网络基础知识的问题!
求助网络基础知识的相关网站
网络的基础知识必须知道哪些??
介绍一下网络系统基础知识~谢谢哈
介绍一下网络系统基础知识~谢谢哈
基础知识
基础知识
基础知识
基础知识
人教版初二英语第九课
我下载了个网络剪刀,安装了,在桌面上却打不开,总说“sniff errplease reinstall winpcap!
哪里买得到<<高中化学基础知识手册>>第九次修订版,生物第一次修订版,比较古老