资源简介
用winpcap编的流量统计程序。

代码片段和文件信息
#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\n“ errbuf);
exit(1);
}
/* 打印列表 */
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(“%d“ &inum);
if(inum < 1 || inum > i)
{
printf(“\nInterface number out of range.\n“);
/* 释放设备列表 */
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“\nUnable to open the adapter. %s is not supported by WinPcap\n“ d->name);
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
printf(“\nlistening on %s...\n“ d->description);
fp=adhandle;
/* 将接口设置为统计模式 */
if (pcap_setmode(fp MODE_STAT)<0)
{
fprintf(stderr“\nError setting the mode.\n“);
pcap_close(fp);
/* 释放设备列表 */
return 1;
}
printf(“NetWork traffic summary:\n“);
/* 开始主循环 */
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 流量统计程序\Debug\vc60.pdb
文件 517120 2008-04-12 20:18 流量统计程序\Debug\流量统计程序.pdb
文件 164864 2008-04-12 20:27 流量统计程序\Debug\vc60.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
- 上一篇:非常好用的数据恢复专业破解版
- 下一篇:大灰狼企业远程管理系统.密码123.rar
相关资源
- bp神经网络源代码,可直接运行
- 04741计算机网络原理知识点整理.docx(
- Visio图标-最新最全的网络通信图标库
- WPF USB 网络 串口 通信软件
- ManageEngine_OpManager_12.3安装手册+管理员
- WPE三件套(WPE+CCP+E2G)冰枫修改版
- TCL-S1916 ,1924交换机管理程序
- 连续hopfield神经网络解决TSP问题
- Windows异步套接字网络编程
- 改进的BP神经网络算法
- 通风网络解算程序 fortran90
- 证券总部网络拓扑结构示意图网络拓
- 校园网络规划与设计课程设计
- 网络唤醒代码
- delphi 通过Ping命令检测网络是否正常
- ipv6网络抓包程序
- 防火墙编程-Visual C 网络通信开发
- 基于bp神经网络的表情识别
- MFC网络编程实例
- IpHlpApi.h&IpHlpApi.lib
- 用C 实现的对网络上的ARP数据包进行
- 网络(UDP)转串口程序
- SOCKET 网络编程 计算机网络 作业 客户
- 一个网络封包截取工具[不是WPE]
- 网络信息安全技术 论文
- RF电阻衰减网络计算-用以RF设计中
- 计算机网络课程设计—ARQ协议模拟实
- linux 网络实验 ftp程序
- 毕业设计:网络围棋对弈
- 用Socket写的简易FTP服务器和客户端
评论
共有 条评论