大鹏鸟是谁的坐骑:WinPcap基础知识(第九课:统计网络的通信量)

来源:百度文库 编辑:中财网 时间:2024/05/08 19:32:46
这节课我们将要学习WinPcap的另外一个高级特性:对网络通信量进行统计。统计引擎使得内核级的包过滤器能有效的把收集到的包分类。你可以查阅NPF 驱动内部手册看到更多的细节。为了用这个功能来监视网络,程序员必须要打开一个适配器,并且把它设为统计模式。可以用函数pcap_setmode()来完成这个功能。特别的,在这个函数里面MODE_STAT必须作为模式参数。  用统计模式,我们写一个监视TCP通信负载的应用程序,只需要很少行代码。下面是例子代码:

   

  1. #include    
  • #include    
  •   
  • #include    
  •   
  • 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;   
  •     localtime_s(
  •     strftime( timestr, sizeof timestr, "%H:%M:%S"
  •   
  •     /* 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);   
  • }