All pastes #439248 Raw Edit

Someone

public text v1 · immutable
·published 2007-04-14 02:57 UTC
rendered paste body
/*
 * CS417 Assignment 4
 * By: Nancy Wang (53553020 / w0s4)
 * File: packet-mbp.c
 * This file cotains a dissector for an application protocol 
 * called MBP(My bogus protocol)
 */

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

#include <gmodule.h>
#include <epan/packet.h>
#include <epan/prefs.h>

/* forward reference */
void proto_register_mbp();
void proto_reg_handoff_mbp();
void dissect_mbp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);

static int proto_mbp = -1;
static int global_mbp_port = 5678;
static dissector_handle_t mbp_handle;

/* task1: declare your packet structure and other fields here */

static int hf_mbp_pdu_type = -1;
static gint ett_mbp = -1;

//declares packet structure variables
static int hf_mbp_flags = -1;
static int hf_mbp_sequenceno = -1;
static int hf_mbp_initialip = -1;
static int hf_mbp_ackflag = -1;
static int hf_mbp_rstflag = -1;
static int hf_mbp_priorityflag = -1;
static int hf_mbp_time = -1;
static int hf_mbp_msg = -1;

//Improves display. Names packet types
static const value_string packettypenames[] = {
	{ 1, "INIT" },
	{ 2, "CONNECTED" },
	{ 3, "HEARTBEAT" },
	{ 0, "Unknown" }
};

//For info column. Names packet types
static const value_string packettypeinfo[] = {
	{1, "MBP Initialize Request"},
	{2, "MBP Connected"},
	{3, "MBP Heartbeat"},
	{0, "UNKNOWN MBP type"}
};

//Improves display. Adds and names flags
#define MBP_ACK_FLAG	0x01
#define MBP_RST_FLAG		0x02
#define MBP_PRIORITY_FLAG	0x04


/* end task1 */


void
proto_register_mbp(void)
{
	module_t *mbp_module;

	if (proto_mbp == -1) {
		proto_mbp = proto_register_protocol (
			"MBP Protocol",		/* name */
			"MBP",		/* short name */
			"mbp"		/* abbrev */
			);
	}

	/* task2: register your packet structure */
	
	static hf_register_info hf[] = {
		
		//registers packet type structure
		{ &hf_mbp_pdu_type,
			{ "MBP PDU Type", "mbp.type", FT_UINT8, BASE_DEC, VALS(packettypenames), 0x0, "", HFILL }},
		//registers sequence number structure
		{ &hf_mbp_sequenceno, 
			{ "MBP PDU Sequence Number", "mbp.seqn", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
		//registers flag structures
		{ &hf_mbp_flags,
			{ "MBP PDU Flags", "mbp.flags", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
		{ &hf_mbp_ackflag,
			{ "MBP PDU ACK Flags", "mbp.flags.ack",	FT_BOOLEAN, 8, NULL, MBP_ACK_FLAG, NULL, HFILL }},
		{ &hf_mbp_rstflag,
			{ "MBP PDU RST Flags", "mbp.flags.rst",	FT_BOOLEAN, 8, NULL, MBP_RST_FLAG, NULL, HFILL }},
		{ &hf_mbp_priorityflag,
			{ "MBP PDU Priority Flags", "mbp.flags.priority", FT_BOOLEAN, 8, NULL, 
				MBP_PRIORITY_FLAG, NULL, HFILL }},
		//registers IPv4 address structure
		{ &hf_mbp_initialip, 
			{ "MBP PDU Initial IP", "mbp.initialip", FT_IPv4, BASE_NONE, NULL, 0x0,	"", HFILL }},
		//registers application message strucutre
		{ &hf_mbp_msg, 
			{ "MBP DATA", "mbp.msg", FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }},
	};
	
	/* Setup protocol subtree array */
	static gint *ett[] = {
		&ett_mbp,
	};	
	
	//registers arrays	
	proto_register_field_array(proto_mbp, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

 
	/* end task2 */

	mbp_module	= prefs_register_protocol(proto_mbp, proto_reg_handoff_mbp);
}	


void
proto_reg_handoff_mbp(void)
{
	static int Initialized=FALSE;

	if (!Initialized) {
		mbp_handle = create_dissector_handle(dissect_mbp, proto_mbp);
		dissector_add("udp.port", global_mbp_port, mbp_handle);
	}
}	

void
dissect_mbp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	guint8 packet_type = tvb_get_guint8(tvb, 0);
	guint16 seq_no = tvb_get_ntohs(tvb, 1);
	guint32 time;
	char* time_str;
	if(packet_type == 3){
		time = tvb_get_ntohl(tvb, 3);
		time_str = ctime(&time);
	}
	
	if (check_col(pinfo->cinfo, COL_PROTOCOL))
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "MBP");

  	/* Clear out stuff in the info column */
  	if(check_col(pinfo->cinfo,COL_INFO)){
    	col_clear(pinfo->cinfo,COL_INFO);
  	}	
  	if (check_col(pinfo->cinfo, COL_INFO)) {
		col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
		val_to_str(packet_type, packettypeinfo, "Unknown (0x%02x)"));
   	}
  

  /* task3 parsing code goes here */
  if (tree) { /* we are being asked for details */
    proto_item *ti = NULL;
    proto_tree *mbp_tree = NULL;   
    gint offset = 0;
    
    //MBP Protocol information
    ti = proto_tree_add_item(tree, proto_mbp, tvb, 0, -1, FALSE);
    proto_item_append_text(ti, ", Type: %s Seq: %d",		//better display 
    	val_to_str(packet_type, packettypenames, "Unknown (0x%02x)"), seq_no);
    mbp_tree = proto_item_add_subtree(ti, ett_mbp);
		    
    //Packet type and sequence # subtree info for all packet types except "Unknown"
    if(packet_type != 0){
		proto_tree_add_item(mbp_tree, hf_mbp_pdu_type, tvb, offset, 1, FALSE); offset += 1;
		proto_tree_add_item(mbp_tree, hf_mbp_sequenceno, tvb, offset, 2, FALSE); offset += 2;
		
		//Flag and IP information relevant to packet type "INIT"
		if(packet_type == 1){ 
			proto_tree_add_item(mbp_tree, hf_mbp_flags, tvb, offset, 1, FALSE);
			proto_tree_add_item(mbp_tree, hf_mbp_ackflag, tvb, offset, 1, FALSE);
			proto_tree_add_item(mbp_tree, hf_mbp_rstflag, tvb, offset, 1, FALSE);
			proto_tree_add_item(mbp_tree, hf_mbp_priorityflag, tvb, offset, 1, FALSE); offset += 1;
			proto_tree_add_item(mbp_tree, hf_mbp_initialip, tvb, offset, 4, FALSE); offset += 4;
		}
		
		//Message information relevant to packet type "CONNECTED"
		if(packet_type == 2){
			proto_tree_add_item(mbp_tree, hf_mbp_msg, tvb, offset, -1, FALSE);
		}
		
		//Time information relevant to packet type "HEARTBEAT"
		if(packet_type == 3){
			proto_tree_add_text(ti, tvb, offset, 4, "Time: %s", time_str);
		}
    }
  }

  /* end task3 */
}