• 大小: 1.97MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-20
  • 语言: 其他
  • 标签: 网络  

资源简介

用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


评论

共有 条评论