玉皇大帝他妈是谁:WinPcap基础知识(第七课:处理离线dump文件)

来源:百度文库 编辑:中财网 时间:2024/04/30 05:05:06
在这节课中我们来学习怎样将数据包保存到一个文件中。Winpcap提供了一系列保存网络数据包到一个文件和从文件中读取保存内容的函数 -- 这节课就是讲述怎样使用这些函数的。同时也会展示怎样winpcap核心中的保存特性来获得高性能的存储(注意:现在,由于新的内核缓存的一些问题,这个特性已经不能使用了)。
 dump文件的格式和libpcap的是一样的。在这个格式里捕获的数据包是用二进制的形式来保存的,现在已经作为标准被许多网络工具使用,其中就包括WinDump, Ethereal 和 Snort。
保存数据包到一个dump文件:
 首先,让我们看看怎样用libpcap格式来写数据包。下面的例子从选择的网络接口中捕获数据并保存到一个由用户提供名字的文件中。view plaincopy to clipboardprint?
#define HAVE_REMOTE  
#include   
 
#pragma comment(lib,"wpcap.lib")  
 
/* prototype of the packet handler */ 
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);  
 
main(int argc, char **argv)  
{  
pcap_if_t *alldevs;  
pcap_if_t *d;  
int inum;  
int i=0;  
pcap_t *adhandle;  
char errbuf[PCAP_ERRBUF_SIZE];  
pcap_dumper_t *dumpfile;  
 
 
      
    /* Check command line */ 
    if(argc != 2)  
    {  
        printf("usage: %s filename", argv[0]);  
        return -1;  
    }  
      
    /* Retrieve the device list on the local machine */ 
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)  
    {  
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);  
        exit(1);  
    }  
      
    /* Print the list */ 
    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");  
        /* Free the device list */ 
        pcap_freealldevs(alldevs);  
        return -1;  
    }  
          
    /* Jump to the selected adapter */ 
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);  
      
      
    /* Open the device */ 
    if ( (adhandle= pcap_open(d->name,          // name of the device  
                              65536,            // portion of the packet to capture  
                                                // 65536 guarantees that the whole packet will be captured on all the link layers  
                              PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode  
                              1000,             // read timeout  
                              NULL,             // authentication on the remote machine  
                              errbuf            // error buffer  
                              ) ) == NULL)  
    {  
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);  
        /* Free the device list */ 
        pcap_freealldevs(alldevs);  
        return -1;  
    }  
 
    /* Open the dump file */ 
    dumpfile = pcap_dump_open(adhandle, argv[1]);  
 
    if(dumpfile==NULL)  
    {  
        fprintf(stderr,"\nError opening output file\n");  
        return -1;  
    }  
      
    printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);  
      
    /* At this point, we no longer need the device list. Free it */ 
    pcap_freealldevs(alldevs);  
      
    /* start the capture */ 
    pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);  
 
    return 0;  
}  
 
/* Callback function invoked by libpcap for every incoming packet */ 
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)  
{  
    /* save the packet on the dump file */ 
    pcap_dump(dumpfile, header, pkt_data);  

#define HAVE_REMOTE
#include #pragma comment(lib,"wpcap.lib")/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);main(int argc, char **argv)
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_dumper_t *dumpfile;
   
    /* Check command line */
    if(argc != 2)
    {
        printf("usage: %s filename", argv[0]);
        return -1;
    }
   
    /* Retrieve the device list on the local machine */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
   
    /* Print the list */
    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");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }
       
    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
   
   
    /* Open the device */
    if ( (adhandle= pcap_open(d->name,          // name of the device
                              65536,            // portion of the packet to capture
                                                // 65536 guarantees that the whole packet will be captured on all the link layers
                              PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
                              1000,             // read timeout
                              NULL,             // authentication on the remote machine
                              errbuf            // error buffer
                              ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }    /* Open the dump file */
    dumpfile = pcap_dump_open(adhandle, argv[1]);    if(dumpfile==NULL)
    {
        fprintf(stderr,"\nError opening output file\n");
        return -1;
    }
   
    printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
   
    /* At this point, we no longer need the device list. Free it */
    pcap_freealldevs(alldevs);
   
    /* start the capture */
    pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);    return 0;
}/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    /* save the packet on the dump file */
    pcap_dump(dumpfile, header, pkt_data);
}可以看出:这个程序的结构和前面我们学过的课程里面的结构非常相似,不同在于:
⑴接口一打开,就马上调用pcap_dump_open()打开一个dump文件并且把它联系到此接口⑵在packet_handler()回调函数执行中,数据包被pcap_dump()写入到文件中。参数dumpfile被pcap_handler()函数接收,所以保存一个数据包是非常简单的事情。大家要控制好时间,因为网络数据包太多了,时间久了dump的文件就会特别大,到时候你直接打开都要等一会儿.......   
从dump文件中读取数据包  既然现在我们有一个dump文件,我们就来看看怎么读取它的内容。下面的代码向我们展示了怎么样打开一个winpcap/libpcap dump文件,然后显示它里面的每一个数据包。这个文件是用pcap_open_offline()打开的,然后在数据包中循环使用pcap_loop()。你可以看到,其实从一个离线的捕获中读取数据包和从一个物理接口上接收他们几乎一样。 下面的例子也介绍了另外一个函数:pcap_createsrcsrc()。这个函数用来创建一个源字符串,这个字符串的开始部分是用来告诉WinPcap要使用的类型,例如:如果我们要打开一个适配器就用”rpcap://”;如果我们要打开一个文件就用file://。在你使用pcap_findalldevs_ex()时这个步骤不是必须的(因为返回值已经包含了这些字符串)。但是,在此例中必需,因为我们要从用户的输入中读取到文件的名字。 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qsycn/archive/2009/08/18/4458783.aspx