最近在做网络流量的分析,用到了winPcap。
winPcap的文档中有个源程序,如下:#include <stdlib.h>
#include <stdio.h>#include <pcap.h>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;
    ltime=localtime(&local_tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);    /* 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);
}无论我怎么改pcap_compile(fp, &fcode, "tcp", 1, netmask)中的过滤规则(比如将"tcp"改为"arp"),我所得到的流量
都没多大的改变,请问这是什么问题啊?
谢谢

解决方案 »

  1.   

    LZ是不是用的ADSL之类的?因为ADSL要自己封装包头,这样的话设置"tcp"会监测不到的,估计是pcap的BUG要这样设置,如果是ADSL的话pcap_compile(fp, &fcode, "ether proto 0x8864", 1, netmask)其他网络自己抓下包看,或者直接设置偏移量来判断tcp udp之类的ip协议
      

  2.   

    我用的不是ADSL,我还有个疑问是:
    为什么在回掉函数中计算每秒收到的bit数用这句话来计算啊:
    Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
      

  3.   

    我也不是很清楚,估计统计模式下pkt_data获得的是指向连续2个LONGLONG的量,一个LONGLONG量是前1秒流过的字节,另一个是数据包数量后面除delay是为了精确,因为时间戳是微秒形势,不可能是绝对的1000000,总有点小误差的