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