流沙团
网卡流量探测器
2018-7-23 流沙团


只是一个简单的demo



可以优化的更好



(思路: 上行展示, 下行展示)







// NetSpeed.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <pcap.h>
#include <remote-ext.h>
#include <Winsock2.h>
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"ws2_32.lib")

timeval timebegin={0};
timeval timeend={0};
int ktime = 0;
int Capturelen = 0;
/* packet handler 函数原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];

/* 获取本机设备列表 */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
return 0;
}

/* 打印列表 */
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);

/* 释放设备列表 */
pcap_freealldevs(alldevs);

/* 开始捕获 */
pcap_loop(adhandle, 0, packet_handler, NULL);

return 0;
}


/* 每次捕获到数据包时,libpcap都会自动调用这个回调函数 */
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;
if(ktime == 0)
{
timebegin = header->ts;
timeend = header->ts;
ktime++;
return;
}
timeend = header->ts;
/* 将时间戳转换成可识别的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
//printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);

Capturelen += header->caplen;
/*
float timeSec = (float)header->ts.tv_sec + (header->ts.tv_usec/1000);
printf("sec: %d , microsec %d \n",header->ts.tv_sec,header->ts.tv_usec);
printf("len: %d , time: %.1f \n",Capturelen,timeSec);
*/
int timeSec = (timeend.tv_sec-timebegin.tv_sec)*1000000 + timeend.tv_usec - timebegin.tv_usec;

/*

*/
if(timeSec > 1000000)
{
float speed = (float)Capturelen/timeSec*1000000;
//printf("speed is: %.1f B/s \n",speed);

if(speed>=0.0 && speed <1024.0)
{
printf("speed is: %.1f B/s \n",speed);
}else if(speed>=1024.0 && speed< 1024*1024)
{
printf("speed is: %.1f KB/s \n",speed/1024);
}else if(speed>=1024*1024 && speed< 1024*1024*1024)
{
printf("speed is: %.1f MB/s \n",speed/1024/1024);
}
timebegin = timeend;
Capturelen = 0;

}
}

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容