From Ronnie Sahlberg:
[obnox/wireshark/wip.git] / packet-skinny.c
1 /* packet-skinny.c
2  *
3  * Dissector for the Skinny Client Control Protocol
4  *   (The "D-Channel"-Protocol for Cisco Systems' IP-Phones)
5  * Copyright 2001, Joerg Mayer (email: see AUTHORS file)
6  *
7  * This file is based on packet-aim.c, which is
8  * Copyright 2000, Ralf Hoelzer <ralf@well.com>
9  *
10  * $Id: packet-skinny.c,v 1.9 2002/01/21 23:35:32 guy Exp $
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs <gerald@ethereal.com>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * 
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 /* This implementation is based on a draft version of the 3.0
32  * specification
33  */
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 /* REMOVE?
40  * #include <stdio.h>
41  * #include <stdlib.h>
42  * #include <string.h>
43  * #include <ctype.h>
44  * 
45  * #ifdef HAVE_SYS_TYPES_H
46  * # include <sys/types.h>
47  * #endif
48  * 
49  * #ifdef HAVE_NETINET_IN_H
50  * # include <netinet/in.h>
51  * #endif
52  * 
53  * #include <glib.h> // already in packet.h -> tvbuff.h
54  *
55  * #ifdef NEED_SNPRINTF_H
56  * # include "snprintf.h"
57  * #endif
58  */
59
60 #include <epan/packet.h>
61 /* REMOVE?
62  * #include <epan/strutil.h>
63  */
64
65 #define TCP_PORT_SKINNY 2000
66
67 /* I will probably need this again when I change things
68  * to function pointers, but let me use the existing
69  * infrastructure for now
70  *
71  * typedef struct {
72  *   guint32    id;
73  *   char *     name;
74  * } message_id_t;
75  */
76
77 static const value_string  message_id[] = {
78
79   /* Station -> Callmanager */
80   {0x0000, "KeepAliveMessage"},
81   {0x0001, "RegisterMessage"},
82   {0x0002, "IpPortMessage"},
83   {0x0003, "KeypadButtonMessage"},
84   {0x0004, "EnblocCallMessage"},
85   {0x0005, "StimulusMessage"},
86   {0x0006, "OffHookMessage"},
87   {0x0007, "OnHookMessage"},
88   {0x0008, "HookFlashMessage"},
89   {0x0009, "ForwardStatReqMessage"},
90   {0x000A, "SpeedDialStatReqMessage"},
91   {0x000B, "LineStatReqMessage"},
92   {0x000C, "ConfigStatReqMessage"},
93   {0x000D, "TimeDateReqMessage"},
94   {0x000E, "ButtonTemplateReqMessage"},
95   {0x000F, "VersionReqMessage"},
96   {0x0010, "CapabilitiesResMessage"},
97   {0x0011, "MediaPortListMessage"},
98   {0x0012, "ServerReqMessage"},
99   {0x0020, "AlarmMessage"},
100   {0x0021, "MulticastMediaReceptionAck"},
101   {0x0022, "OpenReceiveChannelAck"},
102   {0x0023, "ConnectionStatisticsRes"},
103   {0x0024, "OffHookWithCgpnMessage"},
104   {0x0025, "SoftKeySetReqMessage"},
105   {0x0026, "SoftKeyEventMessage"},
106   {0x0027, "UnregisterMessage"},
107   {0x0028, "SoftKeyEventMessage"},
108   {0x0029, "RegisterTokenReq"},
109   
110   /* Callmanager -> Station */
111   /* 0x0000, 0x0003? */
112   {0x0081, "RegisterAckMessage"},
113   {0x0082, "StartToneMessage"},
114   {0x0083, "StopToneMessage"},
115   {0x0085, "SetRingerMessage"},
116   {0x0086, "SetLampMessage"},
117   {0x0087, "SetHkFDetectMessage"},
118   {0x0088, "SetSpeakerModeMessage"},
119   {0x0089, "SetMicroModeMessage"},
120   {0x008A, "StartMediaTransmission"},
121   {0x008B, "StopMediaTransmission"},
122   {0x008C, "StartMediaReception"},
123   {0x008D, "StopMediaReception"},
124   {0x008F, "CallInfoMessage"},
125   {0x0090, "ForwardStatMessage"},
126   {0x0091, "SpeedDialStatMessage"},
127   {0x0092, "LineStatMessage"},
128   {0x0093, "ConfigStatMessage"},
129   {0x0094, "DefineTimeDate"},
130   {0x0095, "StartSessionTransmission"},
131   {0x0096, "StopSessionTransmission"},
132   {0x0097, "ButtonTemplateMessage"},
133   {0x0098, "VersionMessage"},
134   {0x0099, "DisplayTextMessage"},
135   {0x009A, "ClearDisplay"},
136   {0x009B, "CapabilitiesReqMessage"},
137   {0x009C, "EnunciatorCommandMessage"},
138   {0x009D, "RegisterRejectMessage"},
139   {0x009E, "ServerResMessage"},
140   {0x009F, "Reset"},
141   {0x0100, "KeepAliveAckMessage"},
142   {0x0101, "StartMulticastMediaReception"},
143   {0x0102, "StartMulticastMediaTransmission"},
144   {0x0103, "StopMulticastMediaReception"},
145   {0x0104, "StopMulticastMediaTransmission"},
146   {0x0105, "OpenReceiveChannel"},
147   {0x0106, "CloseReceiveChannel"},
148   {0x0107, "ConnectionStatisticsReq"},
149   {0x0108, "SoftKeyTemplateResMessage"},
150   {0x0109, "SoftKeySetResMessage"},
151   {0x0110, "SelectSoftKeysMessage"},
152   {0x0111, "CallStateMessage"},
153   {0x0112, "DisplayPromptStatusMessage"},
154   {0x0113, "ClearPromptStatusMessage"},
155   {0x0114, "DisplayNotifyMessage"},
156   {0x0115, "ClearNotifyMessage"},
157   {0x0116, "ActivateCallPlaneMessage"},
158   {0x0117, "DeactivateCallPlaneMessage"},
159   {0x0118, "UnregisterAckMessage"},
160   {0x0119, "BackSpaceReqMessage"},
161   {0x011A, "RegisterTokenAck"},
162   {0x011B, "RegisterTokenReject"},
163
164   {0     , NULL}        /* needed for value_string automagic */
165 };
166
167 static void dissect_skinny(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
168
169 /* Initialize the protocol and registered fields */
170 static int proto_skinny          = -1;
171 static int hf_skinny_data_length = -1;
172 static int hf_skinny_reserved    = -1;
173 static int hf_skinny_messageid   = -1;
174
175 /* Initialize the subtree pointers */
176 static gint ett_skinny          = -1;
177
178 static dissector_handle_t data_handle;
179
180 /* Code to actually dissect the packets */
181 static void dissect_skinny(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
182 {
183   /* The general structure of a packet: {IP-Header|TCP-Header|n*SKINNY}
184    * SKINNY-Packet: {Header(Size, Reserved)|Data(MessageID, Message-Data)}
185    */
186
187   /* Header fields */
188   guint32 hdr_data_length;
189   guint32 hdr_reserved;
190   guint32 data_messageid;
191   gchar   *messageid_str;
192   guint32 data_size;
193
194 /* Set up structures we will need to add the protocol subtree and manage it */
195   proto_item *ti;
196   proto_tree *skinny_tree = NULL;
197
198 /* check, if this is really an SKINNY packet, they start with a length + 0 */
199
200 /* get relevant header information */
201   hdr_data_length = tvb_get_letohl(tvb, 0);
202   hdr_reserved    = tvb_get_letohl(tvb, 4);
203   data_messageid   = tvb_get_letohl(tvb, 8);
204   data_size       = MIN(8+hdr_data_length, tvb_length(tvb)) - 0xC;
205
206   /* hdr_data_length > 1024 is just a heuristic. Better values/checks welcome */
207   if (hdr_data_length < 4 || hdr_data_length > 1024 || hdr_reserved != 0) {
208     /* Not an SKINNY packet, just happened to use the same port */
209     call_dissector(data_handle,tvb, pinfo, tree);
210     return;
211   }
212   
213   /* Make entries in Protocol column and Info column on summary display */
214   if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
215     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SKINNY");
216     
217   if (check_col(pinfo->cinfo, COL_INFO)) 
218     col_set_str(pinfo->cinfo, COL_INFO, "Skinny Client Control Protocol");
219
220   /* In the interest of speed, if "tree" is NULL, don't do any work not
221    * necessary to generate protocol tree items. */
222   if (tree) {
223     ti = proto_tree_add_item(tree, proto_skinny, tvb, 0,
224                 data_size + 0xC, FALSE); 
225     skinny_tree = proto_item_add_subtree(ti, ett_skinny);
226     proto_tree_add_uint(skinny_tree, hf_skinny_data_length, tvb,
227                 0, 4, hdr_data_length);  
228     proto_tree_add_uint(skinny_tree, hf_skinny_reserved, tvb,
229                 4, 4, hdr_reserved);
230   }
231   messageid_str = val_to_str(data_messageid, message_id, "0x%08X (Unknown)");
232   if (check_col(pinfo->cinfo, COL_INFO)) {
233         col_add_str(pinfo->cinfo, COL_INFO, messageid_str);
234   }
235   if (tree) {
236     proto_tree_add_uint(skinny_tree, hf_skinny_messageid, tvb,
237                 8, 4, data_messageid /* FIXME: add messageid_str */ );
238     if (data_size > 0) {
239         proto_tree_add_protocol_format(skinny_tree, proto_skinny, tvb,
240                 0xC,
241                 data_size, "Data (%d byte%s)", data_size,
242                 plurality(data_size, "", "s"));
243     }
244   }
245
246   /*FIXME: call dissect_skinny recursively until all data is used up */
247 }
248
249 /* Register the protocol with Ethereal */
250 void 
251 proto_register_skinny(void)
252 {                 
253
254 /* Setup list of header fields */
255   static hf_register_info hf[] = {
256     { &hf_skinny_data_length,
257       { "Data Length", "skinny.data_length",
258                 FT_UINT32, BASE_HEX, NULL, 0x0,
259                 "Number of bytes in the data portion.",
260                 HFILL }
261     },
262     { &hf_skinny_reserved,
263       { "Reserved", "skinny.reserved",
264                 FT_UINT32, BASE_HEX, NULL, 0x0,
265                 "Reserved for furture(?) use.",
266                 HFILL }
267     },
268     /* FIXME: Enable use of message name ???  */
269     { &hf_skinny_messageid,
270       { "Message ID", "skinny.messageid",
271                 FT_UINT32, BASE_HEX, VALS(message_id), 0x0,
272                 "The function requested/done with this message.",
273                 HFILL }
274     },
275   };
276
277 /* Setup protocol subtree array */
278   static gint *ett[] = {
279     &ett_skinny,
280   };
281
282 /* Register the protocol name and description */
283   proto_skinny = proto_register_protocol("Skinny Client Control Protocol",
284                 "SKINNY", "skinny");
285
286 /* Required function calls to register the header fields and subtrees used */
287   proto_register_field_array(proto_skinny, hf, array_length(hf));
288   proto_register_subtree_array(ett, array_length(ett));
289 };
290
291 void
292 proto_reg_handoff_skinny(void)
293 {
294   dissector_handle_t skinny_handle;
295
296   data_handle = find_dissector("data");
297   skinny_handle = create_dissector_handle(dissect_skinny, proto_skinny);
298   dissector_add("tcp.port", TCP_PORT_SKINNY, skinny_handle);
299 }