Ethereal-dev: [ethereal-dev] packet-sna.c

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

From: Gilbert Ramirez <gram@xxxxxxxxxx>
Date: Fri, 08 Oct 1999 07:55:26 -0500
Oops. Here's the SNA dissector. :)

--gilbert
/* packet-sna.c
 * Routines for SNA
 * Gilbert Ramirez <gram@xxxxxxxxxx>
 *
 * $Id$
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@xxxxxxxxxx>
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

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

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#include <glib.h>
#include "packet.h"

/*
 * http://www.wanresources.com/snacell.html
 *
 */

static int proto_sna = -1;
static int hf_sna_th = -1;
static int hf_sna_th_0 = -1;
static int hf_sna_th_fid = -1;
static int hf_sna_th_mpf = -1;
static int hf_sna_th_odai = -1;
static int hf_sna_th_efi = -1;
static int hf_sna_th_daf = -1;
static int hf_sna_th_oaf = -1;
static int hf_sna_th_snf = -1;
static int hf_sna_rh = -1;
static int hf_sna_rh_0 = -1;
static int hf_sna_rh_1 = -1;
static int hf_sna_rh_2 = -1;
static int hf_sna_rh_rri = -1;
static int hf_sna_ru = -1;

/* Format Identifier */
static const value_string sna_th_fid_vals[] = {
	{ 0x0,	"SNA device <--> Non-SNA Device" },
	{ 0x1,	"Subarea Node <--> Subarea Node" },
	{ 0x2,	"Subarea Node <--> PU2" },
	{ 0x3,	"Subarea Node or SNA host <--> Subarea Node" },
	{ 0x4,	"?" },
	{ 0x5,	"?" },
	{ 0xf,	"Adjaced Subarea Nodes" },
	{ 0,	NULL }
};

/* Mapping Field */
static const value_string sna_th_mpf_vals[] = {
	{ 0, "Middle segment of a BIU" },
	{ 1, "Last segment of a BIU" },
	{ 2, "First segment of a BIU" },
	{ 3 , "Whole BIU" },
	{ 0x00,   NULL }
};

/* Expedited Flow Indicator */
static const value_string sna_th_efi_vals[] = {
	{ 0, "Normal Flow" },
	{ 1, "Expedited Flow" }
};

/* Request/Response Indicator */
static const value_string sna_rh_rri_vals[] = {
	{ 0, "Request" },
	{ 1, "Response" }
};


static int  dissect_fid2 (const u_char*, int, frame_data*, proto_tree*);
static void dissect_rh (const u_char*, int, frame_data*, proto_tree*);

void
dissect_sna(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {

	proto_tree	*sna_tree = NULL, *th_tree = NULL, *rh_tree = NULL;
	proto_item	*sna_ti, *th_ti, *rh_ti;
	guint8		th_fid;
	int		sna_header_len = 0, th_header_len = 0;

	if (IS_DATA_IN_FRAME(offset)) {
		/* Transmission Header Format Identifier */
		th_fid = hi_nibble(pd[offset]);
	}
	else {
		/* If our first byte isn't here, stop dissecting */
		return;
	}

	/* Summary information */
	if (check_col(fd, COL_PROTOCOL))
		col_add_str(fd, COL_PROTOCOL, "SNA");
	if (check_col(fd, COL_INFO))
		col_add_str(fd, COL_INFO, val_to_str(th_fid, sna_th_fid_vals, "Unknown FID: %01x"));

	if (tree) {

		/* Don't bother setting length. We'll set it later after we find
		 * the lengths of TH/RH/RU */
		sna_ti = proto_tree_add_item(tree, proto_sna, offset, 0, NULL);
		sna_tree = proto_item_add_subtree(sna_ti, ETT_SNA);

		/* --- TH --- */
		/* Don't bother setting length. We'll set it later after we find
		 * the length of TH */
		th_ti = proto_tree_add_item(sna_tree, hf_sna_th,  offset, 0, NULL);
		th_tree = proto_item_add_subtree(th_ti, ETT_SNA_TH);

		switch(th_fid) {
			case 0x2:
				th_header_len = dissect_fid2(pd, offset, fd, th_tree);
				break;
			default:
				dissect_data(pd, offset+1, fd, tree);
		}

		sna_header_len += th_header_len;
		offset += th_header_len;

		proto_item_set_len(th_ti, th_header_len);

		/* --- RH --- */
		if (BYTES_ARE_IN_FRAME(offset, 3)) {
			rh_ti = proto_tree_add_item(sna_tree, hf_sna_rh, offset, 3, NULL);
			rh_tree = proto_item_add_subtree(rh_ti, ETT_SNA_RH);
			dissect_rh(pd, offset, fd, rh_tree);
			sna_header_len += 3;
		}
		else {
			/* If our first byte isn't here, stop dissecting */
			return;
		}

		proto_item_set_len(sna_ti, sna_header_len);
	}
}

/* FID Type 2 (FID2) is used by SNA to transmit information
 * between a Subarea Node and a Physical Unit Type 2 (PU2) Node. The
 * addressing used for FID2 is local rather than network-wide.
 */
static int
dissect_fid2 (const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {

	proto_tree	*bf_tree;
	proto_item	*bf_item;
	guint8	th_0, daf, oaf;
	guint16 snf;

	static int bytes_in_header = 6;

	if (!BYTES_ARE_IN_FRAME(offset, bytes_in_header)) {
		return 0;
	}

	th_0 = pd[offset+0];
	daf = pd[offset+2];
	oaf = pd[offset+3];

	snf = pntohs(&pd[offset+4]);

	/* Create the bitfield tree */
	bf_item = proto_tree_add_item(tree, hf_sna_th_0, offset, 1, th_0);
	bf_tree = proto_item_add_subtree(bf_item, ETT_SNA_TH_FID);

	proto_tree_add_item(bf_tree, hf_sna_th_fid, offset, 1, th_0);
	proto_tree_add_item(bf_tree, hf_sna_th_mpf, offset, 1, th_0);
	proto_tree_add_item(bf_tree, hf_sna_th_odai ,offset, 1, th_0);
	proto_tree_add_item(bf_tree, hf_sna_th_efi ,offset, 1, th_0);

	proto_tree_add_text(tree, offset+1, 1, "Reserved");
	proto_tree_add_item(tree, hf_sna_th_daf ,offset+2, 1, daf);
	proto_tree_add_item(tree, hf_sna_th_oaf ,offset+3, 1, oaf);
	proto_tree_add_item(tree, hf_sna_th_snf ,offset+4, 2, snf);

	return bytes_in_header;
}


static void
dissect_rh (const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {

	proto_tree	*bf_tree;
	proto_item	*bf_item;
	gboolean	is_response;
	guint8		rh_0, rh_1, rh_2;

	rh_0 = pd[offset+0];
	rh_1 = pd[offset+1];
	rh_2 = pd[offset+2];

	is_response = (rh_0 & 0x80);

	/* Create the bitfield tree for byte 0*/
	bf_item = proto_tree_add_item(tree, hf_sna_rh_0, offset, 1, rh_0);
	bf_tree = proto_item_add_subtree(bf_item, ETT_SNA_RH_0);

	proto_tree_add_item(bf_tree, hf_sna_rh_rri, offset, 1, rh_0);
}

void
proto_register_sna(void)
{
        static hf_register_info hf[] = {
                { &hf_sna_th,
                { "Transmission Header",	"sna.th", FT_NONE, BASE_NONE, NULL, { FALSE },
			"" }},

                { &hf_sna_th_0,
                { "Transmission Header Byte 0",	"sna.th.0", FT_UINT8, BASE_HEX, NULL, { FALSE },
			"Byte 0 of Tranmission Header contains FID, MPF, ODAI,"
			" and EFI as bitfields." }},

                { &hf_sna_th_fid,
                { "Format Identifer",		"sna.th.fid", FT_UINT8, BASE_HEX, VALS(sna_th_fid_vals),
			{ TRUE, 0xf0, 4 },
			"Format Identification" }},

                { &hf_sna_th_mpf,
                { "Mapping Field",		"sna.th.mpf", FT_UINT8, BASE_NONE, VALS(sna_th_mpf_vals),
			{ TRUE, 0x0c, 2 },
			"The Mapping Field specifies whether the information field"
			" associated with the TH is a complete or partial BIU." }},

		{ &hf_sna_th_odai,
		{ "ODAI Assignment Indicator",	"sna.th.odai", FT_UINT8, BASE_DEC, NULL,
			{ TRUE, 0x02, 1 },
			"The ODAI indicates which node assigned the OAF'-DAF' values"
			" carried in the TH." }},

                { &hf_sna_th_efi,
                { "Expedited Flow Indicator",	"sna.th.efi", FT_UINT8, BASE_DEC, VALS(sna_th_efi_vals),
			{ TRUE, 0x01, 0 },
			"The EFI designates whether the PIU belongs to the normal"
			" or expedited flow." }},

                { &hf_sna_th_daf,
                { "Destination Address Field",	"sna.th.daf", FT_UINT8, BASE_HEX, NULL, { FALSE },
			"" }},

                { &hf_sna_th_oaf,
                { "Origin Address Field",	"sna.th.oaf", FT_UINT8, BASE_HEX, NULL, { FALSE },
			"" }},

                { &hf_sna_th_snf,
                { "Sequence Number Field",	"sna.th.snf", FT_UINT16, BASE_NONE, NULL, { FALSE },
			"The Sequence Number Field contains a numerical identifier for"
			" the associated BIU."}},


                { &hf_sna_rh,
                { "Request/Response Header",	"sna.rh", FT_NONE, BASE_NONE, NULL, { FALSE },
			"" }},

                { &hf_sna_rh_0,
                { "Request/Response Header Byte 0",	"sna.rh.0", FT_UINT8, BASE_NONE, NULL, { FALSE },
			"" }},

                { &hf_sna_rh_1,
                { "Request/Response Header Byte 1",	"sna.rh.1", FT_UINT8, BASE_NONE, NULL, { FALSE },
			"" }},

                { &hf_sna_rh_2,
                { "Request/Response Header Byte 2",	"sna.rh.2", FT_UINT8, BASE_NONE, NULL, { FALSE },
			"" }},

                { &hf_sna_rh_rri,
                { "Request/Response Indicator",	"sna.rh.rri", FT_UINT8, BASE_NONE, NULL,
			{ TRUE, 0x80, 7 },
			"Denotes whether this is a request or a response." }},


                { &hf_sna_ru,
                { "Request/Response Unit",	"sna.ru", FT_NONE, BASE_NONE, NULL, { FALSE },
			""}},
        };

        proto_sna = proto_register_protocol("Systems Network Architecture", "sna");
	proto_register_field_array(proto_sna, hf, array_length(hf));
}