2

古いカスタム イーサネット ログ (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;
}
4

2 に答える 2

3

pcap_tWinPcap を使用する方が自分で作成するよりも優れているため、使用することをお勧めします。

次の手順は、その方法です。

  1. pcap_open_dead()関数を使用して を作成しpcap_tます。ここで関数の説明を読んでください。linktypeイーサネットのは 1 です。
  2. pcap_dump_open()関数を使用して を作成しpcap_dumper_tます。
  3. 関数を使用pcap_dump()して、パケットをダンプ ファイルに書き込みます。

これがお役に立てば幸いです。

于 2010-06-25T08:23:16.367 に答える
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;
}
于 2010-06-24T23:42:08.967 に答える