Add a "contains" operator for byte-strings, strings, and tvbuffs (protocols).
[obnox/wireshark/wip.git] / 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  *
9  * Copyright 2002, Jeff Morriss <jeff.morriss[AT]ulticom.com>
10  *
11  * $Id: packet-sccpmg.c,v 1.5 2003/04/22 13:47:38 tuexen Exp $
12  *
13  * Ethereal - Network traffic analyzer
14  * By Gerald Combs <gerald@ethereal.com>
15  * Copyright 1998 Gerald Combs
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <glib.h>
41
42 #ifdef NEED_SNPRINTF_H
43 #include "snprintf.h"
44 #endif
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_pc = -1;
117 static int hf_sccpmg_affected_ansi_pc = -1;
118 static int hf_sccpmg_affected_chinese_pc = -1;
119 static int hf_sccpmg_affected_pc_member = -1;
120 static int hf_sccpmg_affected_pc_cluster = -1;
121 static int hf_sccpmg_affected_pc_network = -1;
122 static int hf_sccpmg_smi = -1;
123 static int hf_sccpmg_congestion_level = -1;
124
125 /* Initialize the subtree pointers */
126 static gint ett_sccpmg = -1;
127 static gint ett_sccpmg_affected_pc = -1;
128
129 static void
130 dissect_sccpmg_unknown_message(tvbuff_t *message_tvb, proto_tree *sccpmg_tree)
131 {
132         guint32 message_length;
133
134         message_length = tvb_length(message_tvb);
135
136         proto_tree_add_text(sccpmg_tree, message_tvb, 0, message_length,
137                             "Unknown message (%u byte%s)", message_length,
138                             plurality(message_length, "", "s"));
139 }
140
141 static void
142 dissect_sccpmg_affected_ssn(tvbuff_t *tvb, proto_tree *sccpmg_tree)
143 {
144         proto_tree_add_item(sccpmg_tree, hf_sccpmg_affected_ssn, tvb,
145                             SCCPMG_AFFECTED_SSN_OFFSET, SCCPMG_SSN_LENGTH,
146                             FALSE);
147 }
148
149 static void
150 dissect_sccpmg_affected_pc(tvbuff_t *tvb, proto_tree *sccpmg_tree)
151 {
152         proto_item *pc_item = 0;
153         proto_tree *pc_tree = 0;
154         guint32 dpc;
155         guint8 offset = SCCPMG_AFFECTED_PC_OFFSET;
156         char pc[ANSI_PC_STRING_LENGTH];
157
158         if (mtp3_standard == ITU_STANDARD) {
159                 proto_tree_add_item(sccpmg_tree, hf_sccpmg_affected_pc, tvb,
160                                     offset, ITU_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 the DPC tree; modified from that in packet-sccp.c */
172                 dpc = tvb_get_ntoh24(tvb, offset);
173                 snprintf(pc, sizeof(pc), "%d-%d-%d",
174                          (dpc & ANSI_NETWORK_MASK),
175                          ((dpc & ANSI_CLUSTER_MASK) >> 8),
176                          ((dpc & ANSI_MEMBER_MASK) >> 16));
177
178                 pc_item = proto_tree_add_string_format(sccpmg_tree,
179                                                        *hf_affected_pc,
180                                                        tvb, offset,
181                                                        ANSI_PC_LENGTH, pc,
182                                                        "PC (%s)", pc);
183
184                 pc_tree = proto_item_add_subtree(pc_item,
185                                                  ett_sccpmg_affected_pc);
186
187                 proto_tree_add_uint(pc_tree, hf_sccpmg_affected_pc_member, tvb,
188                                     offset, ANSI_NCM_LENGTH, dpc);
189                 offset += ANSI_NCM_LENGTH;
190                 proto_tree_add_uint(pc_tree, hf_sccpmg_affected_pc_cluster, tvb,
191                                     offset, ANSI_NCM_LENGTH, dpc);
192                 offset += ANSI_NCM_LENGTH;
193                 proto_tree_add_uint(pc_tree, hf_sccpmg_affected_pc_network,
194                                     tvb, offset, ANSI_NCM_LENGTH, dpc);
195         }
196 }
197
198 static void
199 dissect_sccpmg_smi(tvbuff_t *tvb, proto_tree *sccpmg_tree)
200 {
201         guint8 offset = 0;
202
203         if (mtp3_standard == ITU_STANDARD)
204                 offset = ITU_SCCPMG_SMI_OFFSET;
205         else /* ANSI_STANDARD and CHINESE_ITU_STANDARD */
206                 offset = ANSI_SCCPMG_SMI_OFFSET;
207
208         proto_tree_add_item(sccpmg_tree, hf_sccpmg_smi, tvb, offset,
209                             SCCPMG_SMI_LENGTH, FALSE);
210 }
211
212 static void
213 dissect_sccpmg_congestion_level(tvbuff_t *tvb, proto_tree *sccpmg_tree)
214 {
215         guint8 offset = 0;
216
217         if (mtp3_standard == CHINESE_ITU_STANDARD)
218                 offset = CHINESE_ITU_SCCPMG_CONGESTION_OFFSET;
219         else /* ITU_STANDARD */
220                 offset = ITU_SCCPMG_CONGESTION_OFFSET;
221
222         proto_tree_add_item(sccpmg_tree, hf_sccpmg_congestion_level, tvb,
223                             offset, ITU_SCCPMG_CONGESTION_LENGTH, FALSE);
224 }
225
226 static void
227 dissect_sccpmg_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *sccpmg_tree)
228 {
229         guint8 message_type;
230         guint8 offset = 0;
231
232         /* Extract the message type;  all other processing is based on this */
233         message_type   = tvb_get_guint8(tvb, SCCPMG_MESSAGE_TYPE_OFFSET);
234         offset = SCCPMG_MESSAGE_TYPE_LENGTH;
235
236     if (check_col(pinfo->cinfo, COL_INFO))
237         col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str(message_type, sccpmg_message_type_acro_values, "Unknown"));
238
239         if (sccpmg_tree) {
240                 /* add the message type to the protocol tree */
241                 proto_tree_add_uint(sccpmg_tree, hf_sccpmg_message_type, tvb,
242                                     SCCPMG_MESSAGE_TYPE_OFFSET,
243                                     SCCPMG_MESSAGE_TYPE_LENGTH, message_type);
244         }
245
246         switch(message_type) {
247         case SCCPMG_MESSAGE_TYPE_SBR:
248         case SCCPMG_MESSAGE_TYPE_SNR:
249         case SCCPMG_MESSAGE_TYPE_SRT:
250                 if (mtp3_standard != ANSI_STANDARD)
251                 {
252                         dissect_sccpmg_unknown_message(tvb, sccpmg_tree);
253                         break;
254                 }
255                 /* else fallthrough */
256         case SCCPMG_MESSAGE_TYPE_SSA:
257         case SCCPMG_MESSAGE_TYPE_SSP:
258         case SCCPMG_MESSAGE_TYPE_SST:
259         case SCCPMG_MESSAGE_TYPE_SOR:
260         case SCCPMG_MESSAGE_TYPE_SOG:
261                 dissect_sccpmg_affected_ssn(tvb, sccpmg_tree);
262                 dissect_sccpmg_affected_pc(tvb, sccpmg_tree);
263                 dissect_sccpmg_smi(tvb, sccpmg_tree);
264
265                 break;
266         case SCCPMG_MESSAGE_TYPE_SSC:
267                 if (mtp3_standard != ANSI_STANDARD)
268                 {
269                         dissect_sccpmg_affected_ssn(tvb, sccpmg_tree);
270                         dissect_sccpmg_affected_pc(tvb, sccpmg_tree);
271                         dissect_sccpmg_smi(tvb, sccpmg_tree);
272                         dissect_sccpmg_congestion_level(tvb, sccpmg_tree);
273                 }
274                 /* else fallthrough */
275
276         default:
277                 dissect_sccpmg_unknown_message(tvb, sccpmg_tree);
278         }
279 }
280
281 static void
282 dissect_sccpmg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
283 {
284         proto_item *sccpmg_item;
285         proto_tree *sccpmg_tree = NULL;
286
287         /* Make entry in the Protocol column on summary display */
288         if (check_col(pinfo->cinfo, COL_PROTOCOL))
289                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCPMG");
290
291         /* In the interest of speed, if "tree" is NULL, don't do any work not
292            necessary to generate protocol tree items. */
293         if (tree) {
294                 /* create the sccpmg protocol tree */
295                 sccpmg_item = proto_tree_add_item(tree, proto_sccpmg, tvb, 0,
296                                                   -1, FALSE);
297                 sccpmg_tree = proto_item_add_subtree(sccpmg_item, ett_sccpmg);
298         }
299
300         /* dissect the message */
301         dissect_sccpmg_message(tvb, pinfo, sccpmg_tree);
302 }
303
304 /* Register the protocol with Ethereal */
305 void
306 proto_register_sccpmg(void)
307 {
308         /* Setup list of header fields */
309         static hf_register_info hf[] = {
310           { &hf_sccpmg_message_type,
311             { "Message Type", "sccpmg.message_type",
312               FT_UINT8, BASE_HEX, VALS(sccpmg_message_type_values), 0x0,
313               "", HFILL}},
314           { &hf_sccpmg_affected_ssn,
315             { "Affected SubSystem Number", "sccpmg.ssn",
316               FT_UINT8, BASE_DEC, NULL, 0x0,
317               "", HFILL}},
318           { &hf_sccpmg_affected_pc,
319             { "Affected Point Code", "sccpmg.pc",
320               FT_UINT16, BASE_DEC, NULL, ITU_PC_MASK,
321               "", HFILL}},
322           { &hf_sccpmg_affected_ansi_pc,
323             { "Affected Point Code", "sccpmg.ansi_pc",
324               FT_STRING, BASE_NONE, NULL, 0x0,
325               "", HFILL}},
326           { &hf_sccpmg_affected_chinese_pc,
327             { "Affected Point Code", "sccpmg.chinese_pc",
328               FT_STRING, BASE_NONE, NULL, 0x0,
329               "", HFILL}},
330           { &hf_sccpmg_affected_pc_network,
331             { "Affected PC Network", "sccpmg.network",
332               FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
333               "", HFILL}},
334           { &hf_sccpmg_affected_pc_cluster,
335             { "Affected PC Cluster", "sccpmg.cluster",
336               FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
337               "", HFILL}},
338           { &hf_sccpmg_affected_pc_member,
339             { "Affected PC Member", "sccpmg.member",
340               FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
341               "", HFILL}},
342           { &hf_sccpmg_smi,
343             { "Subsystem Multiplicity Indicator", "sccpmg.smi",
344               FT_UINT8, BASE_DEC, NULL, SCCPMG_SMI_MASK,
345               "", HFILL}},
346           { &hf_sccpmg_congestion_level,
347             { "SCCP Congestionl Level (ITU)", "sccpmg.congestion",
348               FT_UINT8, BASE_DEC, NULL, ITU_SCCPMG_CONGESTION_MASK,
349               "", HFILL}}
350         };
351
352         /* Setup protocol subtree array */
353         static gint *ett[] = {
354                 &ett_sccpmg,
355                 &ett_sccpmg_affected_pc
356         };
357
358         /* Register the protocol name and description */
359         proto_sccpmg = proto_register_protocol("Signalling Connection Control Part Management",
360                                                "SCCPMG", "sccpmg");
361
362         /* Required function calls to register the header fields and subtrees
363            used */
364         proto_register_field_array(proto_sccpmg, hf, array_length(hf));
365         proto_register_subtree_array(ett, array_length(ett));
366 }
367
368 void
369 proto_reg_handoff_sccpmg(void)
370 {
371         dissector_handle_t sccpmg_handle;
372
373         sccpmg_handle = create_dissector_handle(dissect_sccpmg, proto_sccpmg);
374
375         /* Register for SCCP SSN=1 messages */
376         dissector_add("sccp.ssn", SCCPMG_SSN, sccpmg_handle);
377 }
378