/* packet-simp.c
 * Routines for SIMP packet dissection
 * Copyright 2004, Bobby N <bobby [at] strayprocess [dot] com>
 *
 * Heavily based on packet-pop.c
 *   Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
 *
 * $Id: packet-simp.c,v 0.3 2004/03/16 12:15:52 guy Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-tftp.c
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

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

#include <stdio.h>

#include <string.h>
#include <glib.h>
#include <epan/packet.h>
#include <epan/strutil.h>

static int proto_simp = -1;
static int hf_simp_response = -1;
static int hf_simp_request = -1;

static gint ett_simp = -1;
static gint ett_simp_reqresp = -1;

#define TCP_PORT_SIMP			7678
#define COMMAND_LEN				8

static void
dissect_simp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
        gboolean        is_request;
        proto_tree      *simp_tree, *reqresp_tree;
        proto_item      *ti;
	gint		offset = 0;
	const guchar	*line;
	gint		next_offset;
	int		linelen;
	int		tokenlen;
	const guchar	*next_token;

	if (check_col(pinfo->cinfo, COL_PROTOCOL))
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "SIMP");

	/*
	 * Find the end of the first line.
	 *
	 * Note that "tvb_find_line_end()" will return a value that is
	 * not longer than what's in the buffer, so the "tvb_get_ptr()"
	 * call won't throw an exception.
	 */
	linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
	line = tvb_get_ptr(tvb, offset, linelen);

	if (pinfo->match_port == pinfo->destport)
		is_request = TRUE;
	else
		is_request = FALSE;

	if (check_col(pinfo->cinfo, COL_INFO)) {
		/*
		 * Put the first line from the buffer into the summary
		 * (but leave out the line terminator).
		 */
		col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
		    is_request ? "Request" : "Response",
		    format_text(line, linelen));
	}

	if (tree) {
		ti = proto_tree_add_item(tree, proto_simp, tvb, offset, -1,
		    FALSE);
		simp_tree = proto_item_add_subtree(ti, ett_simp);

		if (is_request) {
			proto_tree_add_boolean_hidden(simp_tree,
				hf_simp_request, tvb, 0, 0, TRUE);
		} else {
			proto_tree_add_boolean_hidden(simp_tree,
				hf_simp_response, tvb, 0, 0, TRUE);
		}

		/*
		 * Put the line into the protocol tree.
		 */
		ti = proto_tree_add_text(simp_tree, tvb, offset,
		    next_offset - offset, "%s",
		    tvb_format_text(tvb, offset, next_offset - offset));
		reqresp_tree = proto_item_add_subtree(ti, ett_simp_reqresp);

		tokenlen = get_token_len(line, line + linelen, &next_token);
		if (tokenlen != 0) {
			if (is_request) {
				proto_tree_add_text(reqresp_tree, tvb, offset,
					COMMAND_LEN, "Request: %s",
					format_text(line, COMMAND_LEN));
			} else {
				proto_tree_add_text(reqresp_tree, tvb, offset,
					COMMAND_LEN, "Response: %s",
					format_text(line, COMMAND_LEN));
			}
			offset += COMMAND_LEN;
			linelen -= COMMAND_LEN;
		}

		/*
		 * Add the rest of the line as request or reply data
		 */
		if (linelen != 0) {
  			if (is_request) {
  				proto_tree_add_text(reqresp_tree, tvb, offset,
					linelen, "Request Arg : %s",
					format_text(tvb_format_text(tvb, offset, linelen),linelen));
			} else {
				proto_tree_add_text(reqresp_tree, tvb, offset,
					linelen, "Response Arg: %s",
					format_text(tvb_format_text(tvb, offset, linelen),linelen));
			}
		}
		offset = next_offset;

		/*
		 * Show the rest of the request or response as text,
		 * a line at a time
		 */
		while (tvb_offset_exists(tvb, offset)) {
			/*
			 * Find the end of the line
			 */
			linelen = tvb_find_line_end(tvb, offset, -1,
				&next_offset, FALSE);

			/*
			 * Put this line
			 */
			proto_tree_add_text(simp_tree, tvb, offset,
				next_offset - offset, "%s",
				tvb_format_text(tvb, offset, next_offset - offset));
			offset = next_offset;
		}
	}
}

void
proto_register_simp(void)
{
  static hf_register_info hf[] = {
    { &hf_simp_response,
      { "Response",           "simp.response",
	FT_BOOLEAN, BASE_NONE, NULL, 0x0,
      	"TRUE if SIMP response", HFILL }},

    { &hf_simp_request,
      { "Request",            "simp.request",
	FT_BOOLEAN, BASE_NONE, NULL, 0x0,
      	"TRUE if SIMP request", HFILL }}

  };
  static gint *ett[] = {
    &ett_simp,
    &ett_simp_reqresp,
  };

  proto_simp = proto_register_protocol("Secure Instant Messaging Protocol",
				       "SIMP", "simp");
  proto_register_field_array(proto_simp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_simp(void)
{
  dissector_handle_t simp_handle;

  simp_handle = create_dissector_handle(dissect_simp, proto_simp);
  dissector_add("tcp.port", TCP_PORT_SIMP, simp_handle);
}
