流量统计程序源码
用winpcap编的流量统计程序。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 549 2008-03-27 15:10 流量统计程序流量统计程序.dsw
文件 50176 2008-04-12 20:27 流量统计程序流量统计程序.ncb
文件 1390 2008-04-12 20:18 流量统计程序流量统计程序.plg
文件 77824 2008-04-12 20:18 流量统计程序Debugvc60.pdb
文件 517120 2008-04-12 20:18 流量统计程序Debug流量统计程序.pdb
文件 164864 2008-04-12 20:27 流量统计程序Debugvc60.idb
文件 188504 2008-04-12 20:18 流量统计程序Debug流量统计程序.exe
文件 220276 2008-04-12 20:18 流量统计程序Debug流量统计程序.ilk
文件 0 2008-04-12 20:18 流量统计程序Debug流量统计程序.sbr
文件 4697864 2008-04-12 20:18 流量统计程序Debug流量统计程序.pch
文件 19971 2008-04-12 20:18 流量统计程序Debug流量统计程序.obj
文件 2008064 2008-04-12 20:18 流量统计程序Debug流量统计程序.bsc
文件 4381 2008-04-12 20:18 流量统计程序流量统计程序.cpp
文件 3682 2008-04-12 20:26 流量统计程序流量统计程序.dsp
文件 54784 2008-04-12 20:27 流量统计程序流量统计程序.opt
目录 0 2008-04-12 19:30 流量统计程序Debug
目录 0 2008-04-12 19:30 流量统计程序
----------- --------- ---------- ----- ----
8009449 17
#include
#include
#include “pcap.h“
void usage();
void dispatcher_handler(u_char * const struct pcap_pkthdr * const u_char *);
int main()
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
struct timeval st_ts;
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
/* 获取本机设备列表 */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING NULL &alldevs errbuf) == -1)
{
fprintf(stderr“Error in pcap_findalldevs: %s
“ errbuf);
exit(1);
}
/* 打印列表 */
for(d=alldevs; d; d=d->next)
{
printf(“%d. %s“ ++i d->name);
if (d->description)
printf(“ (%s)
“ d->description);
else
printf(“ (No description available)
“);
}
if(i==0)
{
printf(“
No interfaces found! Make sure WinPcap is installed.
“);
return -1;
}
printf(“Enter the interface number (1-%d):“i);
scanf(“%d“ &inum);
if(inum < 1 || inum > i)
{
printf(“
Interface number out of range.
“);
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳转到选中的适配器 */
for(d=alldevs i=0; i< inum-1 ;d=d->next i++);
/* 打开设备 */
if ( (adhandle= pcap_open(d->name // 设备名
65536 // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS // 混杂模式
1000 // 读取超时时间
NULL // 远程机器验证
errbuf // 错误缓冲池
) ) == NULL)
{
fprintf(stderr“
Unable to open the adapter. %s is not supported by WinPcap
“ d->name);
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
printf(“
listening on %s...
“ d->description);
fp=adhandle;
/* 将接口设置为统计模式 */
if (pcap_setmode(fp MODE_STAT)<0)
{
fprintf(stderr“
Error setting the mode.
“);
pcap_close(fp);
/* 释放设备列表 */
return 1;
}
printf(“NetWork traffic summary:
“);
/* 开始主循环 */
pcap_loop(fp 0 dispatcher_handler (PUCHAR)&st_ts);
pcap_close(fp);
return 1;
}
/* 每次捕获到数据包时,libpcap都会自动调用这个回调函数 */
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 BpsPps;
struct tm *ltime;
char timestr[16];
time_t local_tv_sec;
/* 以毫秒计算上一次采样的延迟时间 */
/* 这个值通过采样到的时间戳获得 */
delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
/* 获取每秒的比特数b/s */
Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
/* ^ ^
|
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 549 2008-03-27 15:10 流量统计程序流量统计程序.dsw
文件 50176 2008-04-12 20:27 流量统计程序流量统计程序.ncb
文件 1390 2008-04-12 20:18 流量统计程序流量统计程序.plg
文件 77824 2008-04-12 20:18 流量统计程序Debugvc60.pdb
文件 517120 2008-04-12 20:18 流量统计程序Debug流量统计程序.pdb
文件 164864 2008-04-12 20:27 流量统计程序Debugvc60.idb
文件 188504 2008-04-12 20:18 流量统计程序Debug流量统计程序.exe
文件 220276 2008-04-12 20:18 流量统计程序Debug流量统计程序.ilk
文件 0 2008-04-12 20:18 流量统计程序Debug流量统计程序.sbr
文件 4697864 2008-04-12 20:18 流量统计程序Debug流量统计程序.pch
文件 19971 2008-04-12 20:18 流量统计程序Debug流量统计程序.obj
文件 2008064 2008-04-12 20:18 流量统计程序Debug流量统计程序.bsc
文件 4381 2008-04-12 20:18 流量统计程序流量统计程序.cpp
文件 3682 2008-04-12 20:26 流量统计程序流量统计程序.dsp
文件 54784 2008-04-12 20:27 流量统计程序流量统计程序.opt
目录 0 2008-04-12 19:30 流量统计程序Debug
目录 0 2008-04-12 19:30 流量统计程序
----------- --------- ---------- ----- ----
8009449 17
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)