2010-06-24 68 views
2

我正在尝试构建一个将我的旧自定义以太网日志(bin文件)转换为标准winpcap样式日志的应用程序。Winpcap保存原始数据包不是来自适配器

问题是,我似乎无法找到如何打开pcap_t *而不使用适配器(网卡)的示例。 temp.pkt尚未创建。

我查看了Winpcap提供的示例,并且它们都在转储数据包时使用了活适配器。这个例子是最接近的\ WpdPack \ Examples-pcap \ savedump \ savedump.c是最接近的,看下面的例子稍作修改。

#ifdef _MSC_VER 
/* 
* we do not want the warnings about the old deprecated and unsecure CRT functions 
* since these examples can be compiled under *nix as well 
*/ 
#define _CRT_SECURE_NO_WARNINGS 
#endif 
#include "pcap.h" 

int 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; 


    /* Open the adapter */ 
    if ((adhandle= pcap_open(??????, // name of the device 
          65536,   // portion of the packet to capture. 
              // 65536 grants that the whole packet will be captured on all the MACs. 
          1,    // promiscuous mode (nonzero means promiscuous) 
          1000,   // read timeout 
          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; 
    }  

    // --------------------------- 
    struct pcap_pkthdr header; 
    header.ts.tv_sec = 1 ; /* seconds */ 
    header.ts.tv_usec = 1; /* and microseconds */ 
    header.caplen  = 100; /* length of portion present */ 
    header.len   = 100 ; /* length this packet (off wire) */ 

    u_char pkt_data[100];  
    for(int i = 0 ; i < 100 ; i++) { 
     pkt_data[i] = i ; 
    } 

    pcap_dump((u_char *) dumpfile, &header, (u_char *) &pkt_data); 
    // --------------------------- 

    /* start the capture */ 
    // pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile); 

    pcap_close(adhandle); 
    return 0; 
} 

回答

2

如果你正在做的是将自己的文件格式.pcap,你并不需要一个pcap_t *,你可以使用类似:

FILE* create_pcap_file(const char *filename, int linktype) 
{ 
    struct pcap_file_header fh; 
    fh.magic = TCPDUMP_MAGIC; 
    fh.sigfigs = 0; 
    fh.version_major = 2; 
    fh.version_minor = 4; 
    fh.snaplen = 2<<15; 
    fh.thiszone = 0; 
    fh.linktype = linktype; 

    FILE *file = fopen(filename, "wb"); 
    if(file != NULL) { 
     if(fwrite(&fh, sizeof(fh), 1, file) != 1) { 
      fclose(file); 
      file = NULL; 
     } 
    } 

    return file; 
} 

int write_pcap_packet(FILE* file,size_t length,const unsigned char *data,const struct timeval *tval) 
{ 
    struct pcap_pkthdr pkhdr; 
    pkhdr.caplen = length; 
    pkhdr.len = length; 
    pkhdr.ts = *tval; 

    if(fwrite(&pkhdr, sizeof(pkhdr), 1, file) != 1) { 
     return 1; 
    } 

    if(fwrite(data, 1, length, file) != length) { 
     return 2; 
    } 

    return 0; 
} 
+0

很好用,你是自己编写这段代码还是在http://www.winpcap.org/docs/上的某个地方引用过,我只是没有注意到它? 谢谢 – 2010-06-24 23:58:23

+0

去年我曾经做过类似的事情。 – nos 2010-06-25 07:49:28

3

我建议这样做使用pcap_t,因为使用WinPcap比自己写的更好。

下面的步骤是如何做到这一点:

  1. 使用pcap_open_dead()函数来创建一个pcap_t。阅读功能描述here。以太网的linktype为1.
  2. 使用pcap_dump_open()函数创建一个pcap_dumper_t
  3. 使用pcap_dump()函数将数据包写入转储文件。

我希望这会帮助你。

相关问题