【问题标题】:Reading a list of PCAP files读取 PCAP 文件列表
【发布时间】:2013-08-07 14:58:58
【问题描述】:

这里有没有人有一次性打开 PCAP 文件列表并将 PCAP 文件列表输出到一个输出文件的经验?例如,我有 1.pcap、2.pcap 和 3.pcap,我想对 1.pcap、2.pcap 和 3.pcap 进行一些处理,然后将结果合并到一个输出 pcap 文件(output.pcap )。以下是我现在的代码:

static pcap_t *input = NULL;
input = pcap_open_offline(packet_path, errbuf);
if (input == NULL){exit(0);}
pktMatch = pcap_dump_open(input, "-");
/*Do some processing, eg to find an IP*/
compareIP=true;
if (compareIP){
    pcap_dump(pktMatch, &pktHeader, pktData);
    continue;
}

上面的代码可以用于读取单个输入 pcap 文件。问题:如果我想修改此代码,使其可以在单个 pcap_open_offline() 方法中打开文件列表(1.pcap、2.pcap、3.pcap),我需要更改什么?有没有高手可以给点建议?谢谢

【问题讨论】:

    标签: c pcap libpcap tcpdump


    【解决方案1】:

    这是一些伪代码;把它变成真正的代码是你的工作:

    for (all files) {
        new pcap = pcap_open_offline(the file, errbuf);
        if (new pcap == NULL) {
            fprintf(stderr, "Opening \"%s\" failed: %s\n", the file, errbuf);
            exit(1);
        }
        add new pcap to the list of pcaps to read;
    }
    mark all files as not having a packet yet;
    for (;;) {
        for (all open files) {
            if (the file doesn't have a packet yet)
                read a packet from the file and make it that file's current packet;
        }
        packet time = nothing;
        for (all files) {
            /* note: "nothing" is older than all possible times */
            if (that file's packet's time is newer than packet time) {
                make that file's packet the one to process next;
                packet time = that packet's time;
            }
        }
        /*Do some processing on the packet we selected, eg to find an IP*/
        if (compareIP)
            pcap_dump(pktMatch, &pktHeader, pktData);
        mark the file whose packet we selected as not having a packet yet;
    }        
    

    【讨论】:

    • 感谢盖伊先生!我会努力做到的!无论如何感谢您的建议!
    • 盖伊先生,add new pcap to the list of pcaps to read 会怎样?那是一个链表吗?例如,我有 1.pcap 文件,那么如何将其附加到 1.pcap 中?
    • 我怎样才能把pcap_t *input作为一个数组? pcap_t[] *input 保留 pcaps 列表?
    • 你可以把它们放在一个列表中,你可以把它们放在一个数组中(如果你用 C 编程,你必须自己动态分配和重新分配)等等;正如我所说,把它变成真正的代码是你的工作。
    猜你喜欢
    • 2017-08-15
    • 2013-05-27
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2010-11-14
    相关资源
    最近更新 更多