"col_format_to_pref_str()" is used only in "prefs.c", and knows about
[obnox/wireshark/wip.git] / packet-sual.c
1 /* packet-sual.c
2  * Routines for SUA light, a Siemens propriatary protocol
3  *
4  * Copyright 2001, Martin Held <Martin.Held@icn.siemens.de>
5  *                 Michael Tüxen <Michael.Tuexen@icn.siemens.de>
6  *
7  * $Id: packet-sual.c,v 1.6 2001/06/18 02:17:53 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from README.developer
14  * 
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  * 
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_NETINET_IN_H
43 # include <netinet/in.h>
44 #endif
45
46 #include <string.h>
47 #include <glib.h>
48
49 #ifdef NEED_SNPRINTF_H
50 # include "snprintf.h"
51 #endif
52
53 #include "packet.h"
54
55 #define SUAL_PAYLOAD_PROTO_ID 4
56
57 #define VERSION_LENGTH          1
58 #define SPARE_1_LENGTH          1
59 #define MESSAGE_TYPE_LENGTH     2
60 #define SUBSYSTEM_NUMBER_LENGTH 2
61 #define SPARE_2_LENGTH          2
62 #define MESSAGE_LENGTH_LENGTH   4
63 #define COMMON_HEADER_LENGTH   (VERSION_LENGTH + SPARE_1_LENGTH + MESSAGE_TYPE_LENGTH + \
64                                 SUBSYSTEM_NUMBER_LENGTH + SPARE_2_LENGTH + MESSAGE_LENGTH_LENGTH)
65
66 #define VERSION_OFFSET          0
67 #define SPARE_1_OFFSET          (VERSION_OFFSET + VERSION_LENGTH)
68 #define MESSAGE_TYPE_OFFSET     (SPARE_1_OFFSET + SPARE_1_LENGTH)
69 #define SUBSYSTEM_NUMBER_OFFSET (MESSAGE_TYPE_OFFSET + MESSAGE_TYPE_LENGTH)
70 #define SPARE_2_OFFSET          (SUBSYSTEM_NUMBER_OFFSET + SUBSYSTEM_NUMBER_LENGTH)
71 #define MESSAGE_LENGTH_OFFSET   (SPARE_2_OFFSET + SPARE_2_LENGTH)
72
73 /* SUAL message type coding */
74 #define SUAL_MSG_CLDT                     0x0501
75 #define SUAL_MSG_CORE                     0x0701
76 #define SUAL_MSG_COAK_CC                  0x0702
77 #define SUAL_MSG_COAK_CREF                0x0712
78 #define SUAL_MSG_RELRE                    0x0703
79 #define SUAL_MSG_RELCO                    0x0704
80 #define SUAL_MSG_CODT                     0x0707
81 #define SUAL_MSG_ERR                      0x0000
82
83 static const value_string   sual_message_type_values[] = {
84   {  SUAL_MSG_CLDT,              "Connectionless Data Transfer (CLDT)"},
85   {  SUAL_MSG_CORE,              "Connection Request (CORE)"},
86   {  SUAL_MSG_COAK_CC,           "Connection Acknowledge (COAK_CC)"},
87   {  SUAL_MSG_COAK_CREF,         "Connection Acknowledge (COAK_CREF)"},
88   {  SUAL_MSG_RELRE,             "Release Request (RELRE)"},
89   {  SUAL_MSG_RELCO,             "Release Complete (RELCO)"},
90   {  SUAL_MSG_CODT,              "Connection Oriented Data Transfer (CODT)"},
91   {  SUAL_MSG_ERR,               "Error (ERR)"},
92   {  0,                          NULL}};
93
94 static const value_string sual_message_type_acro_values[] = {
95   {  SUAL_MSG_CLDT,              "CLDT"},
96   {  SUAL_MSG_CORE,              "CORE"},
97   {  SUAL_MSG_COAK_CC,           "COAK_CC"},
98   {  SUAL_MSG_COAK_CREF,         "COAK_CREF"},
99   {  SUAL_MSG_RELRE,             "RELRE"},
100   {  SUAL_MSG_RELCO,             "RELCO"},
101   {  SUAL_MSG_CODT,              "CODT"},
102   {  SUAL_MSG_ERR,               "ERR"},
103   {  0,                          NULL}};
104
105
106 /* SUAL Error Codes */
107 #define SUAL_ERR_INVVERS                0x0001
108 #define SUAL_ERR_INVSTRID               0x0005
109 #define SUAL_ERR_INVMSGTYP              0x0006
110
111 static const value_string sual_error_code_values[] = {
112   {  SUAL_ERR_INVVERS,          "Invalid Protocol Version"},    
113   {  SUAL_ERR_INVSTRID,         "Invalid Stream Identifier"},   
114   {  SUAL_ERR_INVMSGTYP,        "Invalid Message Type"},
115   {  0,                          NULL}};
116
117
118 /* Initialize the protocol and registered fields */
119 static int proto_sual = -1;
120 static int hf_sual_version = -1;
121 static int hf_sual_spare_1 = -1;
122 static int hf_sual_message_type = -1;
123 static int hf_sual_subsystem_number = -1;
124 static int hf_sual_spare_2 = -1;
125 static int hf_sual_message_length = -1;
126 static int hf_sual_error_code = -1;
127
128 /* Initialize the subtree pointers */
129 static gint ett_sual = -1;
130
131 static dissector_table_t sual_dissector_table;
132
133 static void
134 dissect_sual_common_header(tvbuff_t *common_header_tvb, packet_info *pinfo, 
135                            proto_tree *sual_tree, guint16  *subsystem_number)
136 {
137   guint8  version, spare_1;
138   guint16 message_type, spare_2; 
139   guint32 message_length;
140
141   /* Extract the common header */
142   version           = tvb_get_guint8(common_header_tvb, VERSION_OFFSET);
143   spare_1           = tvb_get_guint8(common_header_tvb, SPARE_1_OFFSET);
144   message_type      = tvb_get_ntohs(common_header_tvb, MESSAGE_TYPE_OFFSET);
145   *subsystem_number = tvb_get_ntohs(common_header_tvb, SUBSYSTEM_NUMBER_OFFSET);
146   spare_2           = tvb_get_ntohs(common_header_tvb, SPARE_2_OFFSET);
147   message_length    = tvb_get_ntohl (common_header_tvb, MESSAGE_LENGTH_OFFSET);
148
149   if (check_col(pinfo->fd, COL_INFO)) {
150     col_append_str(pinfo->fd, COL_INFO, val_to_str(message_type, sual_message_type_acro_values, "Unknown"));
151     col_append_str(pinfo->fd, COL_INFO, " ");
152   };
153
154   if (sual_tree) {
155     /* add the components of the common header to the protocol tree */
156     proto_tree_add_uint(sual_tree, hf_sual_version, 
157                         common_header_tvb, VERSION_OFFSET, VERSION_LENGTH,
158                         version);
159     proto_tree_add_uint(sual_tree, hf_sual_spare_1,
160                         common_header_tvb, SPARE_1_OFFSET, SPARE_1_LENGTH,
161                         spare_1);
162     proto_tree_add_uint_format(sual_tree, hf_sual_message_type, 
163                                common_header_tvb, MESSAGE_TYPE_OFFSET, MESSAGE_TYPE_LENGTH,
164                                message_type, "Message type: %u (%s)",
165                                message_type, val_to_str(message_type, sual_message_type_values, "Unknown"));
166     proto_tree_add_uint(sual_tree, hf_sual_subsystem_number,
167                         common_header_tvb, SUBSYSTEM_NUMBER_OFFSET, SUBSYSTEM_NUMBER_LENGTH,
168                         *subsystem_number);
169     proto_tree_add_uint(sual_tree, hf_sual_spare_2,
170                         common_header_tvb, SPARE_2_OFFSET, SPARE_2_LENGTH,
171                         spare_2);
172     proto_tree_add_uint(sual_tree, hf_sual_message_length,
173                         common_header_tvb, MESSAGE_LENGTH_OFFSET, MESSAGE_LENGTH_LENGTH,
174                         message_length);
175   };
176 }
177
178 static void
179 dissect_payload(tvbuff_t *payload_tvb, packet_info *pinfo,
180                  guint16 subsystem_number, proto_tree *sual_tree, proto_tree *tree)
181 {
182   guint         payload_length = tvb_length(payload_tvb);
183         
184   /* do lookup with the subdissector table */
185   if ( ! dissector_try_port (sual_dissector_table, subsystem_number, payload_tvb,
186                               pinfo, tree))
187   {
188      if (sual_tree) {
189        proto_tree_add_text(sual_tree, payload_tvb, 0, payload_length,
190                            "Payload: %u byte%s",
191                            payload_length,
192                            plurality(payload_length, "", "s"));
193      }
194   }
195 }
196
197 static void
198 dissect_error_payload(tvbuff_t *payload_tvb, proto_tree *sual_tree)
199 {
200     if (sual_tree) 
201     {
202        proto_tree_add_item(sual_tree, hf_sual_error_code, payload_tvb, 0, 2, FALSE); 
203     }           
204 }
205
206
207 static void
208 dissect_sual_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *sual_tree, proto_tree *tree)
209 {
210   gint     offset, payload_length;
211   guint16  subsystem_number;
212   guint16  message_type;
213   tvbuff_t *common_header_tvb;
214   tvbuff_t *payload_tvb;
215
216   offset = 0;
217   /* extract and process the common header */
218   common_header_tvb = tvb_new_subset(message_tvb, offset, COMMON_HEADER_LENGTH, COMMON_HEADER_LENGTH);
219   message_type = tvb_get_ntohs(common_header_tvb, MESSAGE_TYPE_OFFSET);
220   dissect_sual_common_header(common_header_tvb, pinfo, sual_tree, &subsystem_number);
221   offset += COMMON_HEADER_LENGTH;
222   
223   payload_length = tvb_length(message_tvb) - COMMON_HEADER_LENGTH;
224   if (payload_length != 0)
225   {
226      payload_tvb = tvb_new_subset(message_tvb, offset, payload_length, payload_length);
227
228      if (message_type != SUAL_MSG_ERR)
229      {
230         dissect_payload(payload_tvb, pinfo, subsystem_number, sual_tree, tree);
231      }
232      else
233      {
234         dissect_error_payload(payload_tvb, sual_tree);
235      }
236    
237   }
238
239 }
240
241 static void
242 dissect_sual(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
243 {
244   proto_item *sual_item;
245   proto_tree *sual_tree;
246
247   /* make entry in the Protocol column on summary display */
248   if (check_col(pinfo->fd, COL_PROTOCOL)) 
249     col_set_str(pinfo->fd, COL_PROTOCOL, "SUAL");
250   
251   /* In the interest of speed, if "tree" is NULL, don't do any work not
252      necessary to generate protocol tree items. */
253   if (tree) {
254     /* create the sual protocol tree */
255     sual_item = proto_tree_add_item(tree, proto_sual, message_tvb, 0, tvb_length(message_tvb), FALSE);
256     sual_tree = proto_item_add_subtree(sual_item, ett_sual);
257   } else {
258     sual_tree = NULL;
259   };
260   /* dissect the message */
261   dissect_sual_message(message_tvb, pinfo, sual_tree, tree);
262 }
263
264 /* Register the protocol with Ethereal */
265 void
266 proto_register_sual(void)
267 {                 
268
269   /* Setup list of header fields */
270   static hf_register_info hf[] = {
271     { &hf_sual_version,
272       { "Version", "sual.version",
273         FT_UINT8, BASE_DEC, NULL, 0x0,          
274         "", HFILL }
275     },
276     { &hf_sual_spare_1,
277       { "Spare", "sual.spare_1",
278         FT_UINT8, BASE_HEX, NULL, 0x0,          
279         "", HFILL }
280     }, 
281     { &hf_sual_message_type,
282       { "Message Type", "sual.message_type",
283         FT_UINT16, BASE_DEC, NULL, 0x0,          
284         "", HFILL }
285     },
286     { &hf_sual_subsystem_number,
287       { "Subsystem number", "sual.subsystem_number",
288         FT_UINT16, BASE_DEC, NULL, 0x0,          
289         "", HFILL }
290     },
291     { &hf_sual_spare_2,
292       { "Spare", "sual.spare_2",
293         FT_UINT16, BASE_DEC, NULL, 0x0,          
294         "", HFILL }
295     },
296     { &hf_sual_message_length,
297       { "Message length", "sual.message_length",
298         FT_UINT32, BASE_DEC, NULL, 0x0,          
299         "", HFILL }
300     },
301     { &hf_sual_error_code,
302       { "Error Code", "sual.error_code",
303         FT_UINT16, BASE_HEX, VALS(&sual_error_code_values), 0x0,
304         "", HFILL }
305     }
306   };
307   
308   /* Setup protocol subtree array */
309   static gint *ett[] = {
310     &ett_sual,
311   };
312   
313   /* Register the protocol name and description */
314   proto_sual = proto_register_protocol("SCCP user adaptation layer light",
315                                        "SUAL",  "sual");
316   
317   /* Required function calls to register the header fields and subtrees used */
318   proto_register_field_array(proto_sual, hf, array_length(hf));
319   proto_register_subtree_array(ett, array_length(ett));
320   
321   /* subdissector code */
322   sual_dissector_table = register_dissector_table("sual.subsystem_number");
323   
324 };
325
326 void
327 proto_reg_handoff_sual(void)
328 {
329   dissector_add("sctp.ppi",  SUAL_PAYLOAD_PROTO_ID, dissect_sual, proto_sual);
330 }