Wireshark-dev: [Wireshark-dev] Problem writing a file dissector for vwr capture files
From: Joerg Mayer <[email protected]>
Date: Sun, 30 Aug 2015 10:22:50 +0200
Hello,

I'm trying to write a file dissector for the IxVeriWave (.vwr) capture files
(without loosing the ability to open said capture files normally of course)
and am failing:
Running  "tshark -X 'read_format:MIME Files Format' -V -r testfile.vwr" (or
the equivalent steps in wireshark) results in
tshark: The file "testfile.vwr" isn't a capture file in a format TShark understands.
Trying to just take over the complete capture file was also unsuccessful.
I've attached the current source of the dissector. Simple question: What am
I missing ;-)
In case you want to test, use the capture attached to bug 11464.

Thanks
   Jörg


-- 
Joerg Mayer                                           <[email protected]>
We are stuck with technology when what we really want is just stuff that
works. Some say that should read Microsoft instead of technology.
/* file-vwr.c
 * Routines for IxVeriWave (.vwr) dissection
 * Documentation only available in form of source code
 *
 * Copyright 2015, Joerg Mayer (see AUTHORS file)
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <[email protected]>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/expert.h>
#include <wiretap/wtap.h>

void proto_register_vwr(void);
void proto_reg_handoff_vwr(void);

static int proto_vwr = -1;

static dissector_handle_t  vwr_handle;

static int hf_vwr_record = -1;

static int hf_vwr_record_header = -1;
static int hf_vwr_record_data = -1;
static int hf_vwr_record_cmd = -1;

static int hf_vwr_header_unknown1 = -1;
static int hf_vwr_header_word1 = -1;
static int hf_vwr_header_word2 = -1;
static int hf_vwr_header_word3 = -1;

static int hf_vwr_data_unknown1 = -1;

static expert_field ei_invalid_header_length = EI_INIT;
static expert_field ei_invalid_data_length = EI_INIT;

static gint ett_vwr_record = -1;
static gint ett_vwr_record_header = -1;
static gint ett_vwr_record_data = -1;

#define VWR_HEADER_LENGTH 20

/*
 * Fetch a 64-bit value in "Corey-endian" form.
 */
#define pcoreytohll(p)  ((guint64)*((const guint8 *)(p)+4)<<56|  \
                         (guint64)*((const guint8 *)(p)+5)<<48|  \
                         (guint64)*((const guint8 *)(p)+6)<<40|  \
                         (guint64)*((const guint8 *)(p)+7)<<32|  \
                         (guint64)*((const guint8 *)(p)+0)<<24|  \
                         (guint64)*((const guint8 *)(p)+1)<<16|  \
                         (guint64)*((const guint8 *)(p)+2)<<8|   \
                         (guint64)*((const guint8 *)(p)+3)<<0)

struct record_data {
    guint32 data_length;
};

static const value_string record_cmd_vals[] = {

    { 0, NULL }
};

static void
pre_dissect_header(tvbuff_t *tvb, struct record_data *record_data)
{
    guint8       record_cmd;
    guint32	 word2;
    guint32	 word3;
    guint32	 data_size;

    record_cmd = tvb_get_guint8(tvb, 0);
    word2 = tvb_get_guint32(tvb, 8, ENC_BIG_ENDIAN);
    word3 = tvb_get_guint32(tvb, 12, ENC_BIG_ENDIAN);

    switch (record_cmd) {
    case 0x21:
    case 0x31:
    case 0x8B:
    case 0xC1:
            data_size = word2 & 0xFFFF;
            break;
    case 0xFE:
            data_size = word3 & 0xFFFF;
            break;
    default:
	    data_size = 0;
            break;
    }
    record_data->data_length = data_size;
}

static gint
dissect_header(proto_tree *tree, tvbuff_t *tvb)
{
    proto_tree  *header_tree;
    proto_item  *header_item;
    gint         offset = 0;

    header_item = proto_tree_add_item(tree, hf_vwr_record_header, tvb, offset, VWR_HEADER_LENGTH, ENC_NA);
    header_tree = proto_item_add_subtree(header_item, ett_vwr_record_header);

    proto_tree_add_item(header_tree, hf_vwr_record_cmd, tvb, offset, 1, ENC_NA);
    offset +=1;
    proto_tree_add_item(header_tree, hf_vwr_header_unknown1, tvb, offset, 3, ENC_NA);
    offset +=3;
    proto_tree_add_item(header_tree, hf_vwr_header_word1, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset +=4;
    proto_tree_add_item(header_tree, hf_vwr_header_word2, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset +=4;
    proto_tree_add_item(header_tree, hf_vwr_header_word3, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset +=4;

    return offset;
}

static gint
dissect_data(proto_tree *tree, tvbuff_t *tvb, struct record_data *record_data)
{
    proto_tree  *data_tree;
    proto_item  *data_item;
    gint         offset = 0;
    guint32	 data_length;

    data_length = record_data->data_length;

    data_item = proto_tree_add_item(tree, hf_vwr_record_data, tvb, offset,
	data_length, ENC_NA);
    data_tree = proto_item_add_subtree(data_item, ett_vwr_record_data);

    proto_tree_add_item(data_tree, hf_vwr_data_unknown1, tvb, offset,
	data_length, ENC_NA);
    offset +=data_length;

    return offset;
}

static int
dissect_vwr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    gint             offset = 0;
    proto_tree      *record_tree;
    proto_item      *record_item;
    tvbuff_t  	    *sub_tvb;
    guint32	     record_length;
    guint32	     bytes_remaining;
    struct record_data record_data;
printf("dissect_vwr\n");

    bytes_remaining = tvb_captured_length_remaining(tvb, offset);

    while (bytes_remaining >= VWR_HEADER_LENGTH) {
	pre_dissect_header(tvb, &record_data);
	record_length = VWR_HEADER_LENGTH + record_data.data_length;

        record_item = proto_tree_add_item(tree, proto_vwr, tvb, offset,
	    record_length, ENC_NA);
        record_tree = proto_item_add_subtree(record_item, ett_vwr_record);

        sub_tvb = tvb_new_subset_length(tvb, offset, VWR_HEADER_LENGTH);
        offset += dissect_header(record_tree, sub_tvb);
	if (record_data.data_length > 0) {
            sub_tvb = tvb_new_subset_length(tvb, offset, record_data.data_length);
            offset += dissect_data(record_tree, sub_tvb, &record_data);
	}
	bytes_remaining -= record_length;
    }
    if (bytes_remaining > 0) {
        proto_tree_add_expert(tree, pinfo, &ei_invalid_header_length, tvb, offset,
	    bytes_remaining);
        offset += bytes_remaining;
    }

    return offset;
}

static gboolean
dissect_vwr_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
printf("dissect_vwr_heur\n");
    return dissect_vwr(tvb, pinfo, tree, NULL) > 0;
}

void
proto_register_vwr(void)
{
    expert_module_t  *expert_module;

    static hf_register_info hf[] = {
        { &hf_vwr_record,
            { "Record", "vwr.record",
            FT_PROTOCOL, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_vwr_record_header,
            { "Header", "vwr.header",
            FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_vwr_record_data,
            { "Data", "vwr.data",
            FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_vwr_record_cmd,
            { "Record CMD", "vwr.record.cmd",
            FT_UINT32, BASE_HEX, VALS(record_cmd_vals), 0x00, NULL, HFILL }
        },
        { &hf_vwr_header_unknown1,
            { "Unknown 1", "vwr.header.unknown1",
            FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_vwr_header_word1,
            { "Word 1", "vwr.header.word1",
            FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }
        },
        { &hf_vwr_header_word2,
            { "Word 2", "vwr.header.word2",
            FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }
        },
        { &hf_vwr_header_word3,
            { "Word 3", "vwr.header.word3",
            FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }
        },
        { &hf_vwr_data_unknown1,
            { "Unknown 1", "vwr.data.unknown1",
            FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
    };

    static ei_register_info ei[] = {
        { &ei_invalid_header_length, { "vwr.invalid_header_length", PI_PROTOCOL, PI_ERROR, "Header too short", EXPFILL }},
        { &ei_invalid_data_length, { "vwr.invalid_data_length", PI_PROTOCOL, PI_ERROR, "Invalid data length", EXPFILL }},
    };

    static gint *ett[] = {
        &ett_vwr_record,
        &ett_vwr_record_header,
        &ett_vwr_record_data
    };

    proto_vwr = proto_register_protocol("IxVeriWave File Format", "File-IxVeriWave", "file-vwr");
    proto_register_field_array(proto_vwr, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    vwr_handle = new_register_dissector("file-vwr", dissect_vwr, proto_vwr);

    expert_module = expert_register_protocol(proto_vwr);
    expert_register_field_array(expert_module, ei, array_length(ei));
}

void
proto_reg_handoff_vwr(void)
{
    // dissector_add_uint("wtap_encap", WTAP_ENCAP_IXVERIWAVE, vwr_handle);
    heur_dissector_add("wtap_file", dissect_vwr_heur, "IxVeriWave File", "vwr_wtap", proto_vwr, HEURISTIC_ENABLE);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */