簡體   English   中英

Winpcap保存不是來自適配器的原始數據包

[英]Winpcap saving raw packets not from an adapter

我正在嘗試構建一個將舊的自定義以太網日志(bin文件)轉換為標准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;
}

我建議使用pcap_t進行此操作,因為使用WinPcap優於自己編寫它。

以下步驟是如何執行此操作:

  1. 使用pcap_open_dead()函數創建一個pcap_t 此處閱讀功能說明。 以太網的linktype為1。
  2. 使用pcap_dump_open()函數創建一個pcap_dumper_t
  3. 使用pcap_dump()函數將數據包寫入轉儲文件。

希望對您有幫助。

如果您要做的只是將自己的文件格式轉換為.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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM