From Lars Roland:
[obnox/wireshark/wip.git] / packet-mtp2.c
1 /* packet-mtp2.c
2  * Routines for MTP2 dissection
3  * It is hopefully (needs testing) compliant to
4  * ITU-T Q.703
5  *
6  * Copyright 2001, Michael Tuexen <tuexen [AT] fh-muenster.de>
7  *
8  * $Id: packet-mtp2.c,v 1.8 2003/04/22 13:47:38 tuexen Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * Copied from packet-m2pa.c
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <glib.h>
36 #include <epan/packet.h>
37
38 #define LITTLE_ENDIAN_BYTE_ORDER TRUE
39
40 /* Initialize the protocol and registered fields */
41 static int proto_mtp2      = -1;
42 static int hf_mtp2_bsn     = -1;
43 static int hf_mtp2_bib     = -1;
44 static int hf_mtp2_fsn     = -1;
45 static int hf_mtp2_fib     = -1;
46 static int hf_mtp2_li      = -1;
47 static int hf_mtp2_spare   = -1;
48 static int hf_mtp2_sf      = -1;
49 static int hf_mtp2_long_sf = -1;
50
51 /* Initialize the subtree pointers */
52 static gint ett_mtp2       = -1;
53
54 static dissector_handle_t mtp3_handle;
55 static int mtp3_proto_id;
56
57 #define BSN_BIB_LENGTH 1
58 #define FSN_FIB_LENGTH 1
59 #define LI_LENGTH      1
60 #define HEADER_LENGTH  (BSN_BIB_LENGTH + FSN_FIB_LENGTH + LI_LENGTH)
61
62 #define BSN_BIB_OFFSET 0
63 #define FSN_FIB_OFFSET (BSN_BIB_OFFSET + BSN_BIB_LENGTH)
64 #define LI_OFFSET      (FSN_FIB_OFFSET + FSN_FIB_LENGTH)
65 #define SIO_OFFSET     (LI_OFFSET + LI_LENGTH)
66
67 #define BSN_MASK       0x7f
68 #define BIB_MASK       0x80
69 #define FSN_MASK       0x7f
70 #define FIB_MASK       0x80
71 #define LI_MASK        0x3f
72 #define SPARE_MASK     0xc0
73
74 static void
75 dissect_mtp2_header(tvbuff_t *su_tvb, proto_item *mtp2_tree)
76 {
77   if (mtp2_tree) {
78     proto_tree_add_item(mtp2_tree, hf_mtp2_bsn,   su_tvb, BSN_BIB_OFFSET, BSN_BIB_LENGTH, LITTLE_ENDIAN_BYTE_ORDER);
79     proto_tree_add_item(mtp2_tree, hf_mtp2_bib,   su_tvb, BSN_BIB_OFFSET, BSN_BIB_LENGTH, LITTLE_ENDIAN_BYTE_ORDER);
80     proto_tree_add_item(mtp2_tree, hf_mtp2_fsn,   su_tvb, FSN_FIB_OFFSET, FSN_FIB_LENGTH, LITTLE_ENDIAN_BYTE_ORDER);
81     proto_tree_add_item(mtp2_tree, hf_mtp2_fib,   su_tvb, FSN_FIB_OFFSET, FSN_FIB_LENGTH, LITTLE_ENDIAN_BYTE_ORDER);
82     proto_tree_add_item(mtp2_tree, hf_mtp2_li,    su_tvb, LI_OFFSET,      LI_LENGTH,      LITTLE_ENDIAN_BYTE_ORDER);
83     proto_tree_add_item(mtp2_tree, hf_mtp2_spare, su_tvb, LI_OFFSET,      LI_LENGTH,      LITTLE_ENDIAN_BYTE_ORDER);
84   }
85 }
86
87 static void
88 dissect_mtp2_fisu(packet_info *pinfo)
89 {
90   if (check_col(pinfo->cinfo, COL_INFO))
91     col_set_str(pinfo->cinfo, COL_INFO, "FISU ");
92 }
93
94 static const value_string status_field_vals[] = {
95   { 0x0, "Status Indication O" },
96   { 0x1, "Status Indication N" },
97   { 0x2, "Status Indication E" },
98   { 0x3, "Status Indication OS" },
99   { 0x4, "Status Indication PO" },
100   { 0x5, "Status Indication BO" },
101   { 0,   NULL}
102 };
103
104 #define SF_OFFSET      (LI_OFFSET + LI_LENGTH)
105 #define SF_LENGTH      1
106 #define LONG_SF_LENGTH 2
107
108 static void
109 dissect_mtp2_lssu(tvbuff_t *su_tvb, packet_info *pinfo, proto_item *mtp2_tree)
110 {
111   guint8  li;
112
113   if (check_col(pinfo->cinfo, COL_INFO))
114     col_set_str(pinfo->cinfo, COL_INFO, "LSSU ");
115   
116   if (mtp2_tree) {
117     li = tvb_get_guint8(su_tvb, LI_OFFSET);
118     if ((li & LI_MASK) == 1)
119       proto_tree_add_item(mtp2_tree, hf_mtp2_sf,      su_tvb, SF_OFFSET, SF_LENGTH,      LITTLE_ENDIAN_BYTE_ORDER);
120     else
121       proto_tree_add_item(mtp2_tree, hf_mtp2_long_sf, su_tvb, SF_OFFSET, LONG_SF_LENGTH, LITTLE_ENDIAN_BYTE_ORDER);
122   }
123 }
124
125 static void
126 dissect_mtp2_msu(tvbuff_t *su_tvb, packet_info *pinfo, proto_item *mtp2_item, proto_item *tree)
127 {
128   gint sif_sio_length;
129   tvbuff_t *sif_sio_tvb;
130
131   if (check_col(pinfo->cinfo, COL_INFO))
132     col_set_str(pinfo->cinfo, COL_INFO, "MSU ");
133
134   sif_sio_length = tvb_length(su_tvb) - HEADER_LENGTH;
135   sif_sio_tvb = tvb_new_subset(su_tvb, SIO_OFFSET, sif_sio_length, sif_sio_length);
136   call_dissector(mtp3_handle, sif_sio_tvb, pinfo, tree);
137
138   if (tree)
139     proto_item_set_len(mtp2_item, HEADER_LENGTH);
140
141 }
142
143 static void
144 dissect_mtp2_su(tvbuff_t *su_tvb, packet_info *pinfo, proto_item *mtp2_item, proto_item *mtp2_tree, proto_tree *tree)
145 {
146   guint8 li;
147
148   dissect_mtp2_header(su_tvb, mtp2_tree);
149   li = tvb_get_guint8(su_tvb, LI_OFFSET);
150   switch(li & LI_MASK) {
151   case 0:
152     dissect_mtp2_fisu(pinfo);
153     break;
154   case 1:
155   case 2:
156     dissect_mtp2_lssu(su_tvb, pinfo, mtp2_tree);
157     break;
158   default:
159     dissect_mtp2_msu(su_tvb, pinfo, mtp2_item, tree);
160     break;
161   }
162 }
163
164 static void
165 dissect_mtp2(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
166 {
167   proto_item *mtp2_item = NULL;
168   proto_tree *mtp2_tree = NULL;
169
170   if (check_col(pinfo->cinfo, COL_PROTOCOL))
171     col_set_str(pinfo->cinfo, COL_PROTOCOL, "MTP2");
172
173   if (tree) {
174     mtp2_item = proto_tree_add_item(tree, proto_mtp2, tvb, 0, -1, FALSE);
175     mtp2_tree = proto_item_add_subtree(mtp2_item, ett_mtp2);
176   };
177
178   dissect_mtp2_su(tvb, pinfo, mtp2_item, mtp2_tree, tree);
179 }
180
181 void
182 proto_register_mtp2(void)
183 {
184
185   static hf_register_info hf[] = {
186     { &hf_mtp2_bsn,     { "Backward sequence number", "mtp2.bsn",   FT_UINT8,  BASE_DEC, NULL,                    BSN_MASK,   "", HFILL } },
187     { &hf_mtp2_bib,     { "Backward indicator bit",   "mtp2.bib",   FT_UINT8,  BASE_DEC, NULL,                    BIB_MASK,   "", HFILL } },
188     { &hf_mtp2_fsn,     { "Forward sequence number",  "mtp2.fsn",   FT_UINT8,  BASE_DEC, NULL,                    FSN_MASK,   "", HFILL } },
189     { &hf_mtp2_fib,     { "Forward indicator bit",    "mtp2.fib",   FT_UINT8,  BASE_DEC, NULL,                    FIB_MASK,   "", HFILL } },
190     { &hf_mtp2_li,      { "Length Indicator",         "mtp2.li",    FT_UINT8,  BASE_DEC, NULL,                    LI_MASK,    "", HFILL } },
191     { &hf_mtp2_spare,   { "Spare",                    "mtp2.spare", FT_UINT8,  BASE_DEC, NULL,                    SPARE_MASK, "", HFILL } },
192     { &hf_mtp2_sf,      { "Status field",             "mtp2.sf",    FT_UINT8,  BASE_DEC, VALS(status_field_vals), 0x0,        "", HFILL } },
193     { &hf_mtp2_long_sf, { "Status field",             "mtp2.sf",    FT_UINT16, BASE_HEX, NULL,                    0x0,        "", HFILL } }
194   };
195
196   static gint *ett[] = {
197     &ett_mtp2
198   };
199
200   proto_mtp2 = proto_register_protocol("Message Transfer Part Level 2", "MTP2", "mtp2");
201   register_dissector("mtp2", dissect_mtp2, proto_mtp2);
202
203   proto_register_field_array(proto_mtp2, hf, array_length(hf));
204   proto_register_subtree_array(ett, array_length(ett));
205
206 };
207
208 void
209 proto_reg_handoff_mtp2(void)
210 {
211   mtp3_handle   = find_dissector("mtp3");
212   mtp3_proto_id = proto_get_id_by_filter_name("mtp3");
213 }