HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / dissectors / packet-lsc.c
blob464ec8fe1c917224610b0578df01753ad9653b25
1 /* packet-lsc.c
2 * Routines for Pegasus LSC packet disassembly
3 * Copyright 2006, Sean Sheedy <seansh@users.sourceforge.net>
5 * $Id$
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <glib.h>
30 #include <epan/packet.h>
31 #include <epan/prefs.h>
33 #include <packet-tcp.h>
35 /* Forward declaration we need below */
36 void proto_reg_handoff_lsc(void);
38 #define LSC_PAUSE 0x01
39 #define LSC_RESUME 0x02
40 #define LSC_STATUS 0x03
41 #define LSC_RESET 0x04
42 #define LSC_JUMP 0x05
43 #define LSC_PLAY 0x06
44 #define LSC_DONE 0x40
45 #define LSC_PAUSE_REPLY 0x81
46 #define LSC_RESUME_REPLY 0x82
47 #define LSC_STATUS_REPLY 0x83
48 #define LSC_RESET_REPLY 0x84
49 #define LSC_JUMP_REPLY 0x85
50 #define LSC_PLAY_REPLY 0x86
52 #define isReply(o) ((o) & 0x80)
54 static const value_string op_code_vals[] = {
55 { LSC_PAUSE, "LSC_PAUSE" },
56 { LSC_RESUME, "LSC_RESUME" },
57 { LSC_STATUS, "LSC_STATUS" },
58 { LSC_RESET, "LSC_RESET" },
59 { LSC_JUMP, "LSC_JUMP" },
60 { LSC_PLAY, "LSC_PLAY" },
61 { LSC_DONE, "LSC_DONE" },
62 { LSC_PAUSE_REPLY, "LSC_PAUSE_REPLY" },
63 { LSC_RESUME_REPLY, "LSC_RESUME_REPLY" },
64 { LSC_STATUS_REPLY, "LSC_STATUS_REPLY" },
65 { LSC_RESET_REPLY, "LSC_RESET_REPLY" },
66 { LSC_JUMP_REPLY, "LSC_JUMP_REPLY" },
67 { LSC_PLAY_REPLY, "LSC_PLAY_REPLY" },
68 { 0, NULL }
71 #define LSC_OPCODE_LEN 3 /* Length to find op code */
72 #define LSC_MIN_LEN 8 /* Minimum packet length */
73 /* Length of each packet type */
74 #define LSC_PAUSE_LEN 12
75 #define LSC_RESUME_LEN 16
76 #define LSC_STATUS_LEN 8
77 #define LSC_RESET_LEN 8
78 #define LSC_JUMP_LEN 20
79 #define LSC_PLAY_LEN 20
80 #define LSC_REPLY_LEN 17
82 static const value_string status_code_vals[] = {
83 { 0x00, "LSC_OK" },
84 { 0x10, "LSC_BAD_REQUEST" },
85 { 0x11, "LSC_BAD_STREAM" },
86 { 0x12, "LSC_WRONG_STATE" },
87 { 0x13, "LSC_UNKNOWN" },
88 { 0x14, "LSC_NO_PERMISSION" },
89 { 0x15, "LSC_BAD_PARAM" },
90 { 0x16, "LSC_NO_IMPLEMENT" },
91 { 0x17, "LSC_NO_MEMORY" },
92 { 0x18, "LSC_IMP_LIMIT" },
93 { 0x19, "LSC_TRANSIENT" },
94 { 0x1a, "LSC_NO_RESOURCES" },
95 { 0x20, "LSC_SERVER_ERROR" },
96 { 0x21, "LSC_SERVER_FAILURE" },
97 { 0x30, "LSC_BAD_SCALE" },
98 { 0x31, "LSC_BAD_START" },
99 { 0x32, "LSC_BAD_STOP" },
100 { 0x40, "LSC_MPEG_DELIVERY" },
101 { 0, NULL }
104 static const value_string mode_vals[] = {
105 { 0x00, "O - Open Mode" },
106 { 0x01, "P - Pause Mode" },
107 { 0x02, "ST - Search Transport" },
108 { 0x03, "T - Transport" },
109 { 0x04, "TP - Transport Pause" },
110 { 0x05, "STP - Search Transport Pause" },
111 { 0x06, "PST - Pause Search Transport" },
112 { 0x07, "EOS - End of Stream" },
113 { 0, NULL }
116 /* Initialize the protocol and registered fields */
117 static int proto_lsc = -1;
118 static int hf_lsc_version = -1;
119 static int hf_lsc_trans_id = -1;
120 static int hf_lsc_op_code = -1;
121 static int hf_lsc_status_code = -1;
122 static int hf_lsc_stream_handle = -1;
123 static int hf_lsc_start_npt = -1;
124 static int hf_lsc_stop_npt = -1;
125 static int hf_lsc_current_npt = -1;
126 static int hf_lsc_scale_num = -1;
127 static int hf_lsc_scale_denom = -1;
128 static int hf_lsc_mode = -1;
130 /* Preferences */
131 static guint global_lsc_port = 0;
133 /* Initialize the subtree pointers */
134 static gint ett_lsc = -1;
136 /* Code to actually dissect the packets */
137 static int
138 dissect_lsc_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
140 proto_item *ti;
141 proto_tree *lsc_tree;
142 guint8 op_code;
143 guint32 stream;
144 guint expected_len;
146 /* Protocol is LSC, packet summary is not yet known */
147 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LSC");
148 col_clear(pinfo->cinfo, COL_INFO);
150 /* Too little data? */
151 if (tvb_length(tvb) < LSC_MIN_LEN)
153 col_set_str(pinfo->cinfo, COL_INFO, "[Too short]");
154 return 0;
157 /* Get the op code */
158 op_code = tvb_get_guint8(tvb, 2);
159 /* And the stream handle */
160 stream = tvb_get_ntohl(tvb, 4);
162 /* Check the data length against what we actually received */
163 switch (op_code)
165 case LSC_PAUSE:
166 expected_len = LSC_PAUSE_LEN;
167 break;
168 case LSC_RESUME:
169 expected_len = LSC_RESUME_LEN;
170 break;
171 case LSC_STATUS:
172 expected_len = LSC_STATUS_LEN;
173 break;
174 case LSC_RESET:
175 expected_len = LSC_RESET_LEN;
176 break;
177 case LSC_JUMP:
178 expected_len = LSC_JUMP_LEN;
179 break;
180 case LSC_PLAY:
181 expected_len = LSC_PLAY_LEN;
182 break;
183 case LSC_DONE:
184 case LSC_PAUSE_REPLY:
185 case LSC_RESUME_REPLY:
186 case LSC_STATUS_REPLY:
187 case LSC_RESET_REPLY:
188 case LSC_JUMP_REPLY:
189 case LSC_PLAY_REPLY:
190 expected_len = LSC_REPLY_LEN;
191 break;
192 default:
193 /* Unrecognized op code */
194 expected_len = LSC_MIN_LEN;
195 break;
198 /* Display the op code in the summary */
199 col_add_fstr(pinfo->cinfo, COL_INFO, "%s, session %.8u",
200 val_to_str(op_code, op_code_vals, "Unknown op code (0x%x)"),
201 stream);
203 if (tvb_length(tvb) < expected_len)
204 col_append_str(pinfo->cinfo, COL_INFO, " [Too short]");
205 else if (tvb_length(tvb) > expected_len)
206 col_append_str(pinfo->cinfo, COL_INFO, " [Too long]");
208 if (tree) {
209 /* Create display subtree for the protocol */
210 ti = proto_tree_add_item(tree, proto_lsc, tvb, 0, -1, ENC_NA);
211 lsc_tree = proto_item_add_subtree(ti, ett_lsc);
213 /* Add already fetched items to the tree */
214 proto_tree_add_uint(lsc_tree, hf_lsc_op_code, tvb, 2, 1, op_code);
215 proto_tree_add_uint_format_value(lsc_tree, hf_lsc_stream_handle, tvb, 4, 4,
216 stream, "%.8u", stream);
218 /* Add rest of LSC header */
219 proto_tree_add_uint(lsc_tree, hf_lsc_version, tvb, 0, 1,
220 tvb_get_guint8(tvb, 0));
221 proto_tree_add_uint(lsc_tree, hf_lsc_trans_id, tvb, 1, 1,
222 tvb_get_guint8(tvb, 1));
224 /* Only replies contain a status code */
225 if (isReply(op_code))
226 proto_tree_add_uint(lsc_tree, hf_lsc_status_code, tvb, 3, 1,
227 tvb_get_guint8(tvb, 3));
229 /* Add op code specific parts */
230 switch (op_code)
232 case LSC_PAUSE:
233 proto_tree_add_int(lsc_tree, hf_lsc_stop_npt, tvb, 8, 4,
234 tvb_get_ntohl(tvb, 8));
235 break;
236 case LSC_RESUME:
237 proto_tree_add_int(lsc_tree, hf_lsc_start_npt, tvb, 8, 4,
238 tvb_get_ntohl(tvb, 8));
239 proto_tree_add_int(lsc_tree, hf_lsc_scale_num, tvb, 12, 2,
240 tvb_get_ntohs(tvb, 12));
241 proto_tree_add_uint(lsc_tree, hf_lsc_scale_denom, tvb, 14, 2,
242 tvb_get_ntohs(tvb, 14));
243 break;
244 case LSC_JUMP:
245 case LSC_PLAY:
246 proto_tree_add_int(lsc_tree, hf_lsc_start_npt, tvb, 8, 4,
247 tvb_get_ntohl(tvb, 8));
248 proto_tree_add_int(lsc_tree, hf_lsc_stop_npt, tvb, 12, 4,
249 tvb_get_ntohl(tvb, 12));
250 proto_tree_add_int(lsc_tree, hf_lsc_scale_num, tvb, 16, 2,
251 tvb_get_ntohs(tvb, 16));
252 proto_tree_add_uint(lsc_tree, hf_lsc_scale_denom, tvb, 18, 2,
253 tvb_get_ntohs(tvb, 18));
254 break;
255 case LSC_DONE:
256 case LSC_PAUSE_REPLY:
257 case LSC_RESUME_REPLY:
258 case LSC_STATUS_REPLY:
259 case LSC_RESET_REPLY:
260 case LSC_JUMP_REPLY:
261 case LSC_PLAY_REPLY:
262 proto_tree_add_int(lsc_tree, hf_lsc_current_npt, tvb, 8, 4,
263 tvb_get_ntohl(tvb, 8));
264 proto_tree_add_int(lsc_tree, hf_lsc_scale_num, tvb, 12, 2,
265 tvb_get_ntohs(tvb, 12));
266 proto_tree_add_uint(lsc_tree, hf_lsc_scale_denom, tvb, 14, 2,
267 tvb_get_ntohs(tvb, 14));
268 proto_tree_add_uint(lsc_tree, hf_lsc_mode, tvb, 16, 1,
269 tvb_get_guint8(tvb, 16));
270 break;
271 default:
272 break;
276 return tvb_length(tvb);
279 /* Decode LSC over UDP */
280 static int
281 dissect_lsc_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
283 return dissect_lsc_common(tvb, pinfo, tree, data);
286 /* Determine length of LSC message */
287 static guint
288 get_lsc_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
290 guint8 op_code;
291 guint pdu_len;
293 /* Get the op code */
294 op_code = tvb_get_guint8(tvb, offset + 2);
296 switch (op_code)
298 case LSC_PAUSE:
299 pdu_len = LSC_PAUSE_LEN;
300 break;
301 case LSC_RESUME:
302 pdu_len = LSC_RESUME_LEN;
303 break;
304 case LSC_STATUS:
305 pdu_len = LSC_STATUS_LEN;
306 break;
307 case LSC_RESET:
308 pdu_len = LSC_RESET_LEN;
309 break;
310 case LSC_JUMP:
311 pdu_len = LSC_JUMP_LEN;
312 break;
313 case LSC_PLAY:
314 pdu_len = LSC_PLAY_LEN;
315 break;
316 case LSC_DONE:
317 case LSC_PAUSE_REPLY:
318 case LSC_RESUME_REPLY:
319 case LSC_STATUS_REPLY:
320 case LSC_RESET_REPLY:
321 case LSC_JUMP_REPLY:
322 case LSC_PLAY_REPLY:
323 pdu_len = LSC_REPLY_LEN;
324 break;
325 default:
326 /* Unrecognized op code */
327 pdu_len = LSC_OPCODE_LEN;
328 break;
331 return pdu_len;
334 /* Decode LSC over TCP */
335 static int
336 dissect_lsc_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
338 tcp_dissect_pdus(tvb, pinfo, tree, TRUE, LSC_OPCODE_LEN, get_lsc_pdu_len,
339 dissect_lsc_common, data);
340 return tvb_length(tvb);
343 /* Register the protocol with Wireshark */
344 void
345 proto_register_lsc(void)
347 module_t *lsc_module;
349 /* Setup list of header fields */
350 static hf_register_info hf[] = {
351 { &hf_lsc_version,
352 { "Version", "lsc.version",
353 FT_UINT8, BASE_DEC, NULL, 0,
354 "Version of the Pegasus LSC protocol", HFILL }
356 { &hf_lsc_trans_id,
357 { "Transaction ID", "lsc.trans_id",
358 FT_UINT8, BASE_DEC, NULL, 0,
359 NULL, HFILL }
361 { &hf_lsc_op_code,
362 { "Op Code", "lsc.op_code",
363 FT_UINT8, BASE_HEX, VALS(op_code_vals), 0,
364 "Operation Code", HFILL }
366 { &hf_lsc_status_code,
367 { "Status Code", "lsc.status_code",
368 FT_UINT8, BASE_HEX, VALS(status_code_vals), 0,
369 NULL, HFILL }
371 { &hf_lsc_stream_handle,
372 { "Stream Handle", "lsc.stream_handle",
373 FT_UINT32, BASE_DEC, NULL, 0,
374 "Stream identification handle", HFILL }
376 { &hf_lsc_start_npt,
377 { "Start NPT", "lsc.start_npt",
378 FT_INT32, BASE_DEC, NULL, 0,
379 "Start Time (milliseconds)", HFILL }
381 { &hf_lsc_stop_npt,
382 { "Stop NPT", "lsc.stop_npt",
383 FT_INT32, BASE_DEC, NULL, 0,
384 "Stop Time (milliseconds)", HFILL }
386 { &hf_lsc_current_npt,
387 { "Current NPT", "lsc.current_npt",
388 FT_INT32, BASE_DEC, NULL, 0,
389 "Current Time (milliseconds)", HFILL }
391 { &hf_lsc_scale_num,
392 { "Scale Numerator", "lsc.scale_num",
393 FT_INT16, BASE_DEC, NULL, 0,
394 NULL, HFILL }
396 { &hf_lsc_scale_denom,
397 { "Scale Denominator", "lsc.scale_denum",
398 FT_UINT16, BASE_DEC, NULL, 0,
399 NULL, HFILL }
401 { &hf_lsc_mode,
402 { "Server Mode", "lsc.mode",
403 FT_UINT8, BASE_HEX, VALS(mode_vals), 0,
404 "Current Server Mode", HFILL }
408 /* Setup protocol subtree array */
409 static gint *ett[] = {
410 &ett_lsc,
413 /* Register the protocol name and description */
414 proto_lsc = proto_register_protocol("Pegasus Lightweight Stream Control",
415 "LSC", "lsc");
417 /* Required function calls to register the header fields and subtrees used */
418 proto_register_field_array(proto_lsc, hf, array_length(hf));
419 proto_register_subtree_array(ett, array_length(ett));
421 /* Register preferences module */
422 lsc_module = prefs_register_protocol(proto_lsc, proto_reg_handoff_lsc);
424 /* Register preferences */
425 prefs_register_uint_preference(lsc_module, "port",
426 "LSC Port",
427 "Set the TCP or UDP port for Pegasus LSC messages",
428 10, &global_lsc_port);
431 void
432 proto_reg_handoff_lsc(void)
434 static gboolean initialized = FALSE;
435 static dissector_handle_t lsc_udp_handle;
436 static dissector_handle_t lsc_tcp_handle;
437 static guint saved_lsc_port;
439 if (!initialized) {
440 lsc_udp_handle = new_create_dissector_handle(dissect_lsc_udp, proto_lsc);
441 lsc_tcp_handle = new_create_dissector_handle(dissect_lsc_tcp, proto_lsc);
442 dissector_add_handle("udp.port", lsc_udp_handle); /* for 'decode-as' */
443 dissector_add_handle("tcp.port", lsc_tcp_handle); /* ... */
444 initialized = TRUE;
445 } else {
446 if (saved_lsc_port != 0) {
447 dissector_delete_uint("udp.port", saved_lsc_port, lsc_udp_handle);
448 dissector_delete_uint("tcp.port", saved_lsc_port, lsc_tcp_handle);
452 /* Set the port number */
453 if (global_lsc_port != 0) {
454 dissector_add_uint("udp.port", global_lsc_port, lsc_udp_handle);
455 dissector_add_uint("tcp.port", global_lsc_port, lsc_tcp_handle);
457 saved_lsc_port = global_lsc_port;