some compilers dont like unnamed unions and structs
[obnox/wireshark/wip.git] / epan / dissectors / packet-sccpmg.c
1 /* packet-sccp.c
2  * Routines for Signalling Connection Control Part (SCCP) Management dissection
3  *
4  * It is hopefully compliant to:
5  *   ANSI T1.112.3-1996
6  *   ITU-T Q.713 7/1996
7  *   YDN 038-1997 (Chinese ITU variant)
8  *   JT-Q714 and NTT-Q714 (Japan)
9  *
10  *   Note that NTT Annex E (SCCP Management Procedure (Global Title Status
11  *   Management)) is not implemented (yet)
12  *
13  * Copyright 2002, Jeff Morriss <jeff.morriss[AT]ulticom.com>
14  *
15  * $Id$
16  *
17  * Wireshark - Network traffic analyzer
18  * By Gerald Combs <gerald@wireshark.org>
19  * Copyright 1998 Gerald Combs
20  *
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation; either version 2
24  * of the License, or (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <glib.h>
45
46 #include <epan/packet.h>
47 #include "packet-mtp3.h"
48
49 #define SCCPMG_SSN 1
50
51 #define SCCPMG_MESSAGE_TYPE_SSA 0x01
52 #define SCCPMG_MESSAGE_TYPE_SSP 0x02
53 #define SCCPMG_MESSAGE_TYPE_SST 0x03
54 #define SCCPMG_MESSAGE_TYPE_SOR 0x04
55 #define SCCPMG_MESSAGE_TYPE_SOG 0x05
56 /* SSC is ITU only */
57 #define SCCPMG_MESSAGE_TYPE_SSC 0x06
58 /* Below are ANSI only */
59 #define SCCPMG_MESSAGE_TYPE_SBR 0xfd
60 #define SCCPMG_MESSAGE_TYPE_SNR 0xfe
61 #define SCCPMG_MESSAGE_TYPE_SRT 0xff
62
63 /* Same as below but with names typed out */
64 static const value_string sccpmg_message_type_values[] = {
65   { SCCPMG_MESSAGE_TYPE_SSA,   "SubSystem Allowed" },
66   { SCCPMG_MESSAGE_TYPE_SSP,   "SubSystem Prohibited" },
67   { SCCPMG_MESSAGE_TYPE_SST,   "Subsystem Status Test" },
68   { SCCPMG_MESSAGE_TYPE_SOR,   "Subsystem Out of service Request" },
69   { SCCPMG_MESSAGE_TYPE_SOG,   "Subsystem Out of service Grant" },
70   { SCCPMG_MESSAGE_TYPE_SSC,   "SubSystem Congested (ITU)" },
71   { SCCPMG_MESSAGE_TYPE_SBR,   "Subsystem Backup Routing (ANSI)" },
72   { SCCPMG_MESSAGE_TYPE_SNR,   "Subsystem Normal Routing (ANSI)" },
73   { SCCPMG_MESSAGE_TYPE_SRT,   "Subsystem Routing status Test (ANSI)" },
74   { 0,                       NULL } };
75
76 /* Same as above but in acronym for (for the Info column) */
77 static const value_string sccpmg_message_type_acro_values[] = {
78   { SCCPMG_MESSAGE_TYPE_SSA,   "SSA" },
79   { SCCPMG_MESSAGE_TYPE_SSP,   "SSP" },
80   { SCCPMG_MESSAGE_TYPE_SST,   "SST" },
81   { SCCPMG_MESSAGE_TYPE_SOR,   "SOR" },
82   { SCCPMG_MESSAGE_TYPE_SOG,   "SOG" },
83   { SCCPMG_MESSAGE_TYPE_SSC,   "SSC" },
84   { SCCPMG_MESSAGE_TYPE_SBR,   "SBR" },
85   { SCCPMG_MESSAGE_TYPE_SNR,   "SNR" },
86   { SCCPMG_MESSAGE_TYPE_SRT,   "SRT" },
87   { 0,                       NULL } };
88
89
90 #define SCCPMG_MESSAGE_TYPE_OFFSET 0
91 #define SCCPMG_MESSAGE_TYPE_LENGTH 1
92
93 #define SCCPMG_AFFECTED_SSN_OFFSET SCCPMG_MESSAGE_TYPE_LENGTH
94 #define SCCPMG_AFFECTED_SSN_LENGTH 1
95
96 #define SCCPMG_AFFECTED_PC_OFFSET (SCCPMG_AFFECTED_SSN_OFFSET + SCCPMG_AFFECTED_SSN_LENGTH)
97 #define ITU_SCCPMG_AFFECTED_PC_LENGTH 2
98 #define ANSI_SCCPMG_AFFECTED_PC_LENGTH 3
99
100 #define ITU_SCCPMG_SMI_OFFSET (SCCPMG_AFFECTED_PC_OFFSET + ITU_SCCPMG_AFFECTED_PC_LENGTH)
101 #define ANSI_SCCPMG_SMI_OFFSET (SCCPMG_AFFECTED_PC_OFFSET + ANSI_SCCPMG_AFFECTED_PC_LENGTH)
102 #define SCCPMG_SMI_LENGTH 1
103 #define SCCPMG_SMI_MASK 0x3
104
105 #define ITU_SCCPMG_CONGESTION_OFFSET (ITU_SCCPMG_SMI_OFFSET + SCCPMG_SMI_LENGTH)
106 #define ITU_SCCPMG_CONGESTION_LENGTH 1
107 #define ITU_SCCPMG_CONGESTION_MASK 0x0f
108 #define CHINESE_ITU_SCCPMG_CONGESTION_OFFSET (ANSI_SCCPMG_SMI_OFFSET + SCCPMG_SMI_LENGTH)
109
110 #define SCCPMG_SSN_LENGTH    1
111
112 /* Initialize the protocol and registered fields */
113 static int proto_sccpmg = -1;
114 static int hf_sccpmg_message_type = -1;
115 static int hf_sccpmg_affected_ssn = -1;
116 static int hf_sccpmg_affected_itu_pc = -1;
117 static int hf_sccpmg_affected_japan_pc = -1;
118 static int hf_sccpmg_affected_ansi_pc = -1;
119 static int hf_sccpmg_affected_chinese_pc = -1;
120 static int hf_sccpmg_affected_pc_member = -1;
121 static int hf_sccpmg_affected_pc_cluster = -1;
122 static int hf_sccpmg_affected_pc_network = -1;
123 static int hf_sccpmg_smi = -1;
124 static int hf_sccpmg_congestion_level = -1;
125
126 /* Initialize the subtree pointers */
127 static gint ett_sccpmg = -1;
128 static gint ett_sccpmg_affected_pc = -1;
129
130 static void
131 dissect_sccpmg_unknown_message(tvbuff_t *message_tvb, proto_tree *sccpmg_tree)
132 {
133         guint32 message_length;
134
135         message_length = tvb_length(message_tvb);
136
137         proto_tree_add_text(sccpmg_tree, message_tvb, 0, message_length,
138                             "Unknown message (%u byte%s)", message_length,
139                             plurality(message_length, "", "s"));
140 }
141
142 static void
143 dissect_sccpmg_affected_ssn(tvbuff_t *tvb, proto_tree *sccpmg_tree)
144 {
145         proto_tree_add_item(sccpmg_tree, hf_sccpmg_affected_ssn, tvb,
146                             SCCPMG_AFFECTED_SSN_OFFSET, SCCPMG_SSN_LENGTH,
147                             FALSE);
148 }
149
150 static void
151 dissect_sccpmg_affected_pc(tvbuff_t *tvb, proto_tree *sccpmg_tree)
152 {
153         guint8 offset = SCCPMG_AFFECTED_PC_OFFSET;
154
155         if (mtp3_standard == ITU_STANDARD) {
156                 proto_tree_add_item(sccpmg_tree, hf_sccpmg_affected_itu_pc, tvb,
157                                     offset, ITU_PC_LENGTH, TRUE);
158         } else if (mtp3_standard == JAPAN_STANDARD) {
159                 proto_tree_add_item(sccpmg_tree, hf_sccpmg_affected_japan_pc,
160                                     tvb, offset, JAPAN_PC_LENGTH, TRUE);
161         } else /* ANSI_STANDARD and CHINESE_ITU_STANDARD */ {
162                 int *hf_affected_pc;
163
164                 if (mtp3_standard == ANSI_STANDARD)
165                 {
166                         hf_affected_pc = &hf_sccpmg_affected_ansi_pc;
167                 } else /* CHINESE_ITU_STANDARD */ {
168                         hf_affected_pc = &hf_sccpmg_affected_chinese_pc;
169                 }
170
171                 /* create and fill the PC tree */
172                 dissect_mtp3_3byte_pc(tvb, offset, sccpmg_tree,
173                                       ett_sccpmg_affected_pc, *hf_affected_pc,
174                                       hf_sccpmg_affected_pc_network,
175                                       hf_sccpmg_affected_pc_cluster,
176                                       hf_sccpmg_affected_pc_member, 0, 0);
177
178                 offset += ANSI_PC_LENGTH;
179         }
180 }
181
182 static void
183 dissect_sccpmg_smi(tvbuff_t *tvb, proto_tree *sccpmg_tree)
184 {
185         guint8 offset = 0;
186
187         if (mtp3_standard == ITU_STANDARD || mtp3_standard == JAPAN_STANDARD)
188                 offset = ITU_SCCPMG_SMI_OFFSET;
189         else /* ANSI_STANDARD and CHINESE_ITU_STANDARD */
190                 offset = ANSI_SCCPMG_SMI_OFFSET;
191
192         proto_tree_add_item(sccpmg_tree, hf_sccpmg_smi, tvb, offset,
193                             SCCPMG_SMI_LENGTH, FALSE);
194 }
195
196 static void
197 dissect_sccpmg_congestion_level(tvbuff_t *tvb, proto_tree *sccpmg_tree)
198 {
199         guint8 offset = 0;
200
201         if (mtp3_standard == CHINESE_ITU_STANDARD)
202                 offset = CHINESE_ITU_SCCPMG_CONGESTION_OFFSET;
203         else /* ITU_STANDARD or JAPAN_STANDARD */
204                 offset = ITU_SCCPMG_CONGESTION_OFFSET;
205
206         proto_tree_add_item(sccpmg_tree, hf_sccpmg_congestion_level, tvb,
207                             offset, ITU_SCCPMG_CONGESTION_LENGTH, FALSE);
208 }
209
210 static void
211 dissect_sccpmg_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *sccpmg_tree)
212 {
213         guint8 message_type;
214         guint8 offset = 0;
215
216         /* Extract the message type;  all other processing is based on this */
217         message_type   = tvb_get_guint8(tvb, SCCPMG_MESSAGE_TYPE_OFFSET);
218         offset = SCCPMG_MESSAGE_TYPE_LENGTH;
219
220         if (check_col(pinfo->cinfo, COL_INFO))
221                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str(message_type, sccpmg_message_type_acro_values, "Unknown"));
222
223         if (sccpmg_tree) {
224                 /* add the message type to the protocol tree */
225                 proto_tree_add_uint(sccpmg_tree, hf_sccpmg_message_type, tvb,
226                                     SCCPMG_MESSAGE_TYPE_OFFSET,
227                                     SCCPMG_MESSAGE_TYPE_LENGTH, message_type);
228         }
229
230         switch(message_type) {
231         case SCCPMG_MESSAGE_TYPE_SBR:
232         case SCCPMG_MESSAGE_TYPE_SNR:
233         case SCCPMG_MESSAGE_TYPE_SRT:
234                 if (mtp3_standard != ANSI_STANDARD)
235                 {
236                         dissect_sccpmg_unknown_message(tvb, sccpmg_tree);
237                         break;
238                 }
239                 /* else fallthrough */
240         case SCCPMG_MESSAGE_TYPE_SSA:
241         case SCCPMG_MESSAGE_TYPE_SSP:
242         case SCCPMG_MESSAGE_TYPE_SST:
243         case SCCPMG_MESSAGE_TYPE_SOR:
244         case SCCPMG_MESSAGE_TYPE_SOG:
245                 dissect_sccpmg_affected_ssn(tvb, sccpmg_tree);
246                 dissect_sccpmg_affected_pc(tvb, sccpmg_tree);
247                 dissect_sccpmg_smi(tvb, sccpmg_tree);
248
249                 break;
250         case SCCPMG_MESSAGE_TYPE_SSC:
251                 if (mtp3_standard != ANSI_STANDARD)
252                 {
253                         dissect_sccpmg_affected_ssn(tvb, sccpmg_tree);
254                         dissect_sccpmg_affected_pc(tvb, sccpmg_tree);
255                         dissect_sccpmg_smi(tvb, sccpmg_tree);
256                         dissect_sccpmg_congestion_level(tvb, sccpmg_tree);
257                 }
258                 /* else fallthrough */
259
260         default:
261                 dissect_sccpmg_unknown_message(tvb, sccpmg_tree);
262         }
263 }
264
265 static void
266 dissect_sccpmg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
267 {
268         proto_item *sccpmg_item;
269         proto_tree *sccpmg_tree = NULL;
270
271         /* Make entry in the Protocol column on summary display */
272         if (check_col(pinfo->cinfo, COL_PROTOCOL))
273                 switch(mtp3_standard) {
274                 case ITU_STANDARD:
275                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCPMG (Int. ITU)");
276                         break;
277                 case ANSI_STANDARD:
278                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCPMG (ANSI)");
279                         break;
280                 case CHINESE_ITU_STANDARD:
281                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCPMG (Chin. ITU)");
282                         break;
283                 };      
284
285         /* In the interest of speed, if "tree" is NULL, don't do any work not
286            necessary to generate protocol tree items. */
287         if (tree) {
288                 /* create the sccpmg protocol tree */
289                 sccpmg_item = proto_tree_add_item(tree, proto_sccpmg, tvb, 0,
290                                                   -1, FALSE);
291                 sccpmg_tree = proto_item_add_subtree(sccpmg_item, ett_sccpmg);
292         }
293
294         /* dissect the message */
295         dissect_sccpmg_message(tvb, pinfo, sccpmg_tree);
296 }
297
298 /* Register the protocol with Wireshark */
299 void
300 proto_register_sccpmg(void)
301 {
302         /* Setup list of header fields */
303         static hf_register_info hf[] = {
304           { &hf_sccpmg_message_type,
305             { "Message Type", "sccpmg.message_type",
306               FT_UINT8, BASE_HEX, VALS(sccpmg_message_type_values), 0x0,
307               "", HFILL}},
308           { &hf_sccpmg_affected_ssn,
309             { "Affected SubSystem Number", "sccpmg.ssn",
310               FT_UINT8, BASE_DEC, NULL, 0x0,
311               "", HFILL}},
312           { &hf_sccpmg_affected_itu_pc,
313             { "Affected Point Code", "sccpmg.pc",
314               FT_UINT16, BASE_DEC, NULL, ITU_PC_MASK,
315               "", HFILL}},
316           { &hf_sccpmg_affected_japan_pc,
317             { "Affected Point Code", "sccpmg.pc",
318               FT_UINT16, BASE_DEC, NULL, 0x0,
319               "", HFILL}},
320           { &hf_sccpmg_affected_ansi_pc,
321             { "Affected Point Code", "sccpmg.ansi_pc",
322               FT_STRING, BASE_NONE, NULL, 0x0,
323               "", HFILL}},
324           { &hf_sccpmg_affected_chinese_pc,
325             { "Affected Point Code", "sccpmg.chinese_pc",
326               FT_STRING, BASE_NONE, NULL, 0x0,
327               "", HFILL}},
328           { &hf_sccpmg_affected_pc_network,
329             { "Affected PC Network", "sccpmg.network",
330               FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
331               "", HFILL}},
332           { &hf_sccpmg_affected_pc_cluster,
333             { "Affected PC Cluster", "sccpmg.cluster",
334               FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
335               "", HFILL}},
336           { &hf_sccpmg_affected_pc_member,
337             { "Affected PC Member", "sccpmg.member",
338               FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
339               "", HFILL}},
340           { &hf_sccpmg_smi,
341             { "Subsystem Multiplicity Indicator", "sccpmg.smi",
342               FT_UINT8, BASE_DEC, NULL, SCCPMG_SMI_MASK,
343               "", HFILL}},
344           { &hf_sccpmg_congestion_level,
345             { "SCCP Congestionl Level (ITU)", "sccpmg.congestion",
346               FT_UINT8, BASE_DEC, NULL, ITU_SCCPMG_CONGESTION_MASK,
347               "", HFILL}}
348         };
349
350         /* Setup protocol subtree array */
351         static gint *ett[] = {
352                 &ett_sccpmg,
353                 &ett_sccpmg_affected_pc
354         };
355
356         /* Register the protocol name and description */
357         proto_sccpmg = proto_register_protocol("Signalling Connection Control Part Management",
358                                                "SCCPMG", "sccpmg");
359
360         /* Required function calls to register the header fields and subtrees
361            used */
362         proto_register_field_array(proto_sccpmg, hf, array_length(hf));
363         proto_register_subtree_array(ett, array_length(ett));
364 }
365
366 void
367 proto_reg_handoff_sccpmg(void)
368 {
369         dissector_handle_t sccpmg_handle;
370
371         sccpmg_handle = create_dissector_handle(dissect_sccpmg, proto_sccpmg);
372
373         /* Register for SCCP SSN=1 messages */
374         dissector_add("sccp.ssn", SCCPMG_SSN, sccpmg_handle);
375 }
376