ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
July 17th, 2024 | 10:00am-11:55am SGT (UTC+8) | Online

Ethereal-dev: [Ethereal-dev] please help. Junk tvb data

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Nina Pham <nina@xxxxxxxxxxx>
Date: Tue, 22 Mar 2005 15:07:38 -0800
Ahh, this is making me crazy. I attached the file so you can see. Please take a look a dissect_MCI function. Somehow, I can set mciOffset. Ater I do int mciOffset = 0, it returns some junk number. Then I can't ever set mciOffset again, nor getting mci_signature, mci_type, mci_size, using tvb_get_ functions. By the way, I'm using MSCV++ to debug. It is really making me nut here. Don't know what's going on.
/* packet-mci.c
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "plugins/plugin_api.h"
#include "moduleinfo.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gmodule.h>
#include <glib.h>

#include "plugins/plugin_api_defs.h"

#include <epan/packet.h>

#include "mci.h"

#ifndef ENABLE_STATIC
G_MODULE_EXPORT const gchar version[] = VERSION; 
#endif

#define SIGNATURE_OFFSET    0
#define TYPE_OFFSET         4
#define SIZE_OFFSET         6
#define DATA_OFFSET         8

/* IF PROTO exposes code to other dissectors, then it must be exported
   in a header file. If not, a header file is not needed at all. */

/* Initialize the protocol and registered fields */
static int proto_MCI = -1;
static int hf_MCI_request = -1;
static int hf_MCI_request_name = -1;
static int hf_MCI_type= -1;
static int hf_MCI_response= -1;

/* Initialize the subtree pointers */
static gint ett_MCI= -1;
static gint ett_ReqRsp= -1;
static gint ett_Data = -1;

const MciReq mciReqItem[REQ_RSP_CNT] =
{
	MCI_REQ( MCI_Request),
	MCI_REQ( MCI_Request_Test),
	MCI_REQ( MCI_Request_Reset),
	MCI_REQ( MCI_Data_MDTCMon)
};

/* Code to actually dissect the packets */
static void
dissect_MCI(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	
	proto_item *ti;
	proto_item *data_item;
	proto_tree *MCI_tree;
	proto_tree *ReqRsp_tree;
	proto_tree *Data_tree;

    gboolean    is_response;
    const char  *req_name;      
	guint32     mci_signature;
    guint16     mci_type;
    guint16     mci_size;
    gboolean    is_read = TRUE;
    gboolean    is_data = FALSE;
    guint16     bc;
    guint64     tra;
    guint64     datalen;   
    int         cmd_code = 0;
    guint       tvbLen;
    int         mciOffset = 0;
    
	tvbLen = tvb_length(tvb);
    bc = tvbLen;

    mciOffset = SIGNATURE_OFFSET;
    mci_signature = tvb_get_ntohl(tvb, mciOffset);
	mciOffset = TYPE_OFFSET;
    mci_type = tvb_get_ntohs(tvb, mciOffset);
	mciOffset = SIZE_OFFSET;
    mci_size = tvb_get_ntohs(tvb, mciOffset);

    if (tvbLen) {
        /*
        ** check if request or response
        */
        if (pinfo->match_port == pinfo->destport)
            is_response = FALSE;
        else
            is_response = TRUE;
        
        /* Make entries in Protocol column and Info column on summary display */
        if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
            col_set_str(pinfo->cinfo, COL_PROTOCOL, "MCI");
            
        /* put request name into information colume */

        if (mci_signature == MCI_PacketSignature) {
            int i;
            for (i=0; i < REQ_RSP_CNT; i++)
            {
               if (mci_type == mciReqItem[i].type) {
                    if (check_col(pinfo->cinfo, COL_INFO)) 
                        col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
                                is_response ? "REPONSE" : "REQUEST", 
                                mciReqItem[i].name);
               }
            }
        } else {
            is_data = TRUE;
            if (check_col(pinfo->cinfo, COL_INFO)) 
                col_set_str(pinfo->cinfo, COL_INFO, "MCI DATA");
        }

        if (tree) {
            /* create display subtree for the protocol */
            ti = proto_tree_add_item(tree, proto_MCI, tvb, 0, -1, FALSE);

            MCI_tree = proto_item_add_subtree(ti, ett_MCI);

            /* add an item to the subtree */
            if (ti) {
                if (is_response) {
                    ti = proto_tree_add_text(MCI_tree, 
                            tvb, 0, -1, "Response");
                } else {
                    ti = proto_tree_add_text(MCI_tree, 
                            tvb, 0, -1, "Request");
                }
                
                ReqRsp_tree = proto_item_add_subtree(ti, ett_ReqRsp);

                if (is_response) {
                    proto_tree_add_boolean_hidden(ReqRsp_tree,
                            hf_MCI_response, tvb, 0,0,TRUE);
                    proto_tree_add_boolean_hidden(ReqRsp_tree,
                            hf_MCI_request, tvb, 0,0,FALSE);
                } else {
                    proto_tree_add_boolean_hidden(ReqRsp_tree,
                            hf_MCI_response, tvb, 0,0,FALSE);
                    proto_tree_add_boolean_hidden(ReqRsp_tree,
                            hf_MCI_request, tvb, 0,0,TRUE);
                }

            }
        }
    }
}


/* Register the protocol with Ethereal */

/* this format is require because a script is used to build the C function
   that calls all the protocol registration.
*/

void
proto_register_MCI(void)
{                 

/* Setup list of header fields  See Section 1.6.1 for details*/
	static hf_register_info hf[] = {
        { &hf_MCI_request,
			{ "Request",           "mci.request",
			FT_BOOLEAN, BASE_NONE, NULL, 0x0,          
			"TRUE if  request", HFILL }
		},
        { &hf_MCI_response,
			{ "Response",           "mci.response",
			FT_BOOLEAN, BASE_NONE, NULL, 0x0,          
			"TRUE if  response", HFILL }
		},
        { &hf_MCI_request_name,
			{ "Request Name",           "mci.request.name",
			FT_STRING, BASE_NONE, NULL, 0x0,          
			"", HFILL }
		},
        { &hf_MCI_type,
			{ "Type",           "mci.type",
			FT_UINT32, BASE_DEC, NULL, 0x0,          
			"Type", HFILL }
		}
	};


/* Setup protocol subtree array */
	static gint *ett[] = {
		&ett_MCI,
		&ett_ReqRsp,
        &ett_Data,
	};

/* Register the protocol name and description */
	proto_MCI = proto_register_protocol("MCI Protocal",
	    "MCI", "mci");

/* Required function calls to register the header fields and subtrees used */
	proto_register_field_array(proto_MCI, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}


/* If this dissector uses sub-dissector registration add a registration routine.
   This format is required because a script is used to find these routines and
   create the code that calls these routines.
*/
void
proto_reg_handoff_MCI(void)
{
	dissector_handle_t MCI_handle;

	MCI_handle = create_dissector_handle(dissect_MCI,
	    proto_MCI);
	dissector_add("tcp.port", MCIPort, MCI_handle);
}

#ifndef ENABLE_STATIC
G_MODULE_EXPORT void 
plugin_init(plugin_address_table_t *pat) {
    plugin_address_table_init(pat);

    if (proto_MCI == -1)
        proto_register_MCI();
}

G_MODULE_EXPORT void 
plugin_reg_handoff(void) {
    proto_reg_handoff_MCI();
}
#endif