Added two new arguments to epan_init() and proto_init() to
[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\9fxen <Michael.Tuexen@icn.siemens.de>
6  *
7  * $Id: packet-sual.c,v 1.1 2001/01/22 09:04:09 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@unicom.net>
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 #include "packet-ip.h"
55
56 #define SUAL_PAYLOAD_PROTO_ID 4
57
58 #define VERSION_LENGTH          1
59 #define SPARE_1_LENGTH          1
60 #define MESSAGE_TYPE_LENGTH     2
61 #define SUBSYSTEM_NUMBER_LENGTH 2
62 #define SPARE_2_LENGTH          2
63 #define MESSAGE_LENGTH_LENGTH   4
64 #define COMMON_HEADER_LENGTH   (VERSION_LENGTH + SPARE_1_LENGTH + MESSAGE_TYPE_LENGTH + \
65                                 SUBSYSTEM_NUMBER_LENGTH + SPARE_2_LENGTH + MESSAGE_LENGTH_LENGTH)
66
67 #define VERSION_OFFSET          0
68 #define SPARE_1_OFFSET          (VERSION_OFFSET + VERSION_LENGTH)
69 #define MESSAGE_TYPE_OFFSET     (SPARE_1_OFFSET + SPARE_1_LENGTH)
70 #define SUBSYSTEM_NUMBER_OFFSET (MESSAGE_TYPE_OFFSET + MESSAGE_TYPE_LENGTH)
71 #define SPARE_2_OFFSET          (SUBSYSTEM_NUMBER_OFFSET + SUBSYSTEM_NUMBER_LENGTH)
72 #define MESSAGE_LENGTH_OFFSET   (SPARE_2_OFFSET + SPARE_2_LENGTH)
73
74 /* SUAL message type coding */
75 #define SUAL_MSG_CLDT                     0x0501
76 #define SUAL_MSG_CORE                     0x0701
77 #define SUAL_MSG_COAK_CC                  0x0702
78 #define SUAL_MSG_COAK_CREF                0x0712
79 #define SUAL_MSG_RELRE                    0x0703
80 #define SUAL_MSG_RELCO                    0x0704
81 #define SUAL_MSG_CODT                     0x0707
82 #define SUAL_MSG_ERR                      0x0000
83
84 static const value_string   sual_message_type_values[] = {
85   {  SUAL_MSG_CLDT,              "Connectionless Data Transfer (CLDT)"},
86   {  SUAL_MSG_CORE,              "Connection Request (CORE)"},
87   {  SUAL_MSG_COAK_CC,           "Connection Acknowledge (COAK_CC)"},
88   {  SUAL_MSG_COAK_CREF,         "Connection Acknowledge (COAK_CREF)"},
89   {  SUAL_MSG_RELRE,             "Release Request (RELRE)"},
90   {  SUAL_MSG_RELCO,             "Release Complete (RELCO)"},
91   {  SUAL_MSG_CODT,              "Connection Oriented Data Transfer (CODT)"},
92   {  SUAL_MSG_ERR,               "Error (ERR)"},
93   {  0,                          NULL}};
94
95 static const value_string sual_message_type_acro_values[] = {
96   {  SUAL_MSG_CLDT,              "CLDT"},
97   {  SUAL_MSG_CORE,              "CORE"},
98   {  SUAL_MSG_COAK_CC,           "COAK_CC"},
99   {  SUAL_MSG_COAK_CREF,         "COAK_CREF"},
100   {  SUAL_MSG_RELRE,             "RELRE"},
101   {  SUAL_MSG_RELCO,             "RELCO"},
102   {  SUAL_MSG_CODT,              "CODT"},
103   {  SUAL_MSG_ERR,               "ERR"},
104   {  0,                          NULL}};
105
106 /* Initialize the protocol and registered fields */
107 static int proto_sual = -1;
108 static int hf_sual_version = -1;
109 static int hf_sual_spare_1 = -1;
110 static int hf_sual_message_type = -1;
111 static int hf_sual_subsystem_number = -1;
112 static int hf_sual_spare_2 = -1;
113 static int hf_sual_message_length = -1;
114
115 /* Initialize the subtree pointers */
116 static gint ett_sual = -1;
117
118 static void
119 dissect_sual_common_header(tvbuff_t *common_header_tvb, packet_info *pinfo, proto_tree *sual_tree)
120 {
121   guint8  version, spare_1;
122   guint16 message_type, subsystem_number, spare_2; 
123   guint32 message_length;
124
125   /* Extract the common header */
126   version          = tvb_get_guint8(common_header_tvb, VERSION_OFFSET);
127   spare_1          = tvb_get_guint8(common_header_tvb, SPARE_1_OFFSET);
128   message_type     = tvb_get_ntohs(common_header_tvb, MESSAGE_TYPE_OFFSET);
129   subsystem_number = tvb_get_ntohs(common_header_tvb, SUBSYSTEM_NUMBER_OFFSET);
130   spare_2          = tvb_get_ntohs(common_header_tvb, SPARE_2_OFFSET);
131   message_length   = tvb_get_ntohl (common_header_tvb, MESSAGE_LENGTH_OFFSET);
132
133   if (check_col(pinfo->fd, COL_INFO)) {
134     col_append_str(pinfo->fd, COL_INFO, val_to_str(message_type, sual_message_type_acro_values, "Unknown"));
135     col_append_str(pinfo->fd, COL_INFO, " ");
136   };
137
138   if (sual_tree) {
139     /* add the components of the common header to the protocol tree */
140     proto_tree_add_uint(sual_tree, hf_sual_version, 
141                         common_header_tvb, VERSION_OFFSET, VERSION_LENGTH,
142                         version);
143     proto_tree_add_uint(sual_tree, hf_sual_spare_1,
144                         common_header_tvb, SPARE_1_OFFSET, SPARE_1_LENGTH,
145                         spare_1);
146     proto_tree_add_uint_format(sual_tree, hf_sual_message_type, 
147                                common_header_tvb, MESSAGE_TYPE_OFFSET, MESSAGE_TYPE_LENGTH,
148                                message_type, "Message type: %u (%s)",
149                                message_type, val_to_str(message_type, sual_message_type_values, "Unknown"));
150     proto_tree_add_uint(sual_tree, hf_sual_subsystem_number,
151                         common_header_tvb, SUBSYSTEM_NUMBER_OFFSET, SUBSYSTEM_NUMBER_LENGTH,
152                         subsystem_number);
153     proto_tree_add_uint(sual_tree, hf_sual_spare_2,
154                         common_header_tvb, SPARE_2_OFFSET, SPARE_2_LENGTH,
155                         spare_2);
156     proto_tree_add_uint(sual_tree, hf_sual_message_length,
157                         common_header_tvb, MESSAGE_LENGTH_OFFSET, MESSAGE_LENGTH_LENGTH,
158                         message_length);
159   };
160 }
161
162 static void
163 dissect_sual_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *sual_tree)
164 {
165   gint offset, payload_length;
166   tvbuff_t *common_header_tvb;
167
168   offset = 0;
169   /* extract and process the common header */
170   common_header_tvb = tvb_new_subset(message_tvb, offset, COMMON_HEADER_LENGTH, COMMON_HEADER_LENGTH);
171   dissect_sual_common_header(common_header_tvb, pinfo, sual_tree);
172   offset += COMMON_HEADER_LENGTH;
173   
174   if (sual_tree) {
175     payload_length = tvb_length(message_tvb) - COMMON_HEADER_LENGTH;
176     proto_tree_add_text(sual_tree, message_tvb, offset, payload_length,
177                         "Payload: %u byte%s",
178                         payload_length, plurality(payload_length, "", "s"));
179   }
180 }
181
182 static void
183 dissect_sual(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
184 {
185   proto_item *sual_item;
186   proto_tree *sual_tree;
187
188   /* make entry in the Protocol column on summary display */
189   if (check_col(pinfo->fd, COL_PROTOCOL)) 
190     col_set_str(pinfo->fd, COL_PROTOCOL, "SUAL");
191   
192   /* In the interest of speed, if "tree" is NULL, don't do any work not
193      necessary to generate protocol tree items. */
194   if (tree) {
195     /* create the sual protocol tree */
196     sual_item = proto_tree_add_item(tree, proto_sual, message_tvb, 0, tvb_length(message_tvb), FALSE);
197     sual_tree = proto_item_add_subtree(sual_item, ett_sual);
198   } else {
199     sual_tree = NULL;
200   };
201   /* dissect the message */
202   dissect_sual_message(message_tvb, pinfo, sual_tree);
203 }
204
205 /* Register the protocol with Ethereal */
206 void
207 proto_register_sual(void)
208 {                 
209
210   /* Setup list of header fields */
211   static hf_register_info hf[] = {
212     { &hf_sual_version,
213       { "Version", "sual.version",
214         FT_UINT8, BASE_DEC, NULL, 0x0,          
215         ""}
216     },
217     { &hf_sual_spare_1,
218       { "Spare", "sual.spare_1",
219         FT_UINT8, BASE_HEX, NULL, 0x0,          
220         ""}
221     }, 
222     { &hf_sual_message_type,
223       { "Message Type", "sual.message_type",
224         FT_UINT16, BASE_DEC, NULL, 0x0,          
225         ""}
226     },
227     { &hf_sual_subsystem_number,
228       { "Subsystem number", "sual.subsystem_number",
229         FT_UINT16, BASE_DEC, NULL, 0x0,          
230         ""}
231     },
232     { &hf_sual_spare_2,
233       { "Spare", "sual.spare_2",
234         FT_UINT16, BASE_DEC, NULL, 0x0,          
235         ""}
236     },
237     { &hf_sual_message_length,
238       { "Message length", "sual.message_length",
239         FT_UINT32, BASE_DEC, NULL, 0x0,          
240         ""}
241     }, 
242   };
243   
244   /* Setup protocol subtree array */
245   static gint *ett[] = {
246     &ett_sual,
247   };
248   
249   /* Register the protocol name and description */
250   proto_sual = proto_register_protocol("SCCP user adaptation layer light",
251                                        "SUAL",  "sual");
252   
253   /* Required function calls to register the header fields and subtrees used */
254   proto_register_field_array(proto_sual, hf, array_length(hf));
255   proto_register_subtree_array(ett, array_length(ett));
256 };
257
258 void
259 proto_reg_handoff_sual(void)
260 {
261   dissector_add("sctp.ppi",  SUAL_PAYLOAD_PROTO_ID, dissect_sual, proto_sual);
262 }