MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-kdp.c
bloba7f73f7f82c84830d3c007d46e039fd4a3189710
1 /* packet-kdp.c
2 * Routines for KDP (Kontiki Delivery Protocol) packet disassembly
4 * $Id$
6 * Copyright (c) 2008 by Kontiki Inc.
7 * Wade Hennessey <wade@kontiki.com>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1999 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "config.h"
30 #include <epan/packet.h>
31 #include <glib.h>
33 #define KDP_PORT 19948
34 #define BUFFER_SIZE 80
35 static int proto_kdp = -1;
36 static gint ett_kdp = -1;
37 static gint ett_kdp_flags = -1;
39 static int hf_kdp_version = -1;
40 static int hf_kdp_headerlen = -1;
41 static int hf_kdp_flags = -1;
42 static int hf_kdp_errors = -1;
43 static int hf_kdp_destflowid = -1;
44 static int hf_kdp_srcflowid = -1;
45 static int hf_kdp_sequence = -1;
46 static int hf_kdp_ack = -1;
47 static int hf_kdp_maxsegmentsize = -1;
48 static int hf_kdp_optionnumber = -1;
49 static int hf_kdp_optionlen = -1;
50 static int hf_kdp_option1 = -1;
51 static int hf_kdp_option2 = -1;
52 static int hf_kdp_option3 = -1;
53 static int hf_kdp_option4 = -1;
54 static int hf_kdp_option5 = -1;
55 static int hf_kdp_option6 = -1;
56 static int hf_kdp_option7 = -1;
57 static int hf_kdp_option8 = -1;
58 static int hf_kdp_option9 = -1;
59 static int hf_kdp_option_unknown = -1;
60 static int hf_kdp_fragment = -1;
61 static int hf_kdp_fragtotal = -1;
62 static int hf_kdp_body = -1;
63 static int hf_kdp_xml_body = -1;
65 #define KDP_DROP_FLAG (1 << 0)
66 #define KDP_SYN_FLAG (1 << 1)
67 #define KDP_ACK_FLAG (1 << 2)
68 #define KDP_RST_FLAG (1 << 3)
69 #define KDP_BCST_FLAG (1 << 4)
70 #define KDP_DUP_FLAG (1 << 5)
72 static int hf_kdp_drop_flag = -1;
73 static int hf_kdp_syn_flag = -1;
74 static int hf_kdp_ack_flag = -1;
75 static int hf_kdp_rst_flag = -1;
76 static int hf_kdp_bcst_flag = -1;
77 static int hf_kdp_dup_flag = -1;
79 static void dissect_kdp(tvbuff_t *tvb,
80 packet_info *pinfo,
81 proto_tree *tree) {
82 guint body_len;
83 guint8 version = 0;
84 guint8 header_len = 0;
85 guint8 packet_flags = 0;
86 guint8 packet_errors = 0;
87 guint32 sequence_number = G_MAXUINT32;
88 guint32 ack_number = G_MAXUINT32;
89 guint32 src_flowid = G_MAXUINT32;
90 int offset;
92 col_set_str(pinfo->cinfo, COL_PROTOCOL, "KDP");
93 col_clear(pinfo->cinfo, COL_INFO);
94 if (tree) {
95 proto_item *ti;
96 proto_tree *kdp_tree, *flags_tree;
97 ti = NULL;
98 kdp_tree = NULL;
99 flags_tree = NULL;
101 ti = proto_tree_add_item(tree, proto_kdp, tvb, 0, -1, ENC_NA);
102 kdp_tree = proto_item_add_subtree(ti, ett_kdp);
104 version = tvb_get_guint8(tvb, 0);
105 if (version != 2) {
106 /* Version other than 2 is really SDDP in UDP */
107 proto_tree_add_item(kdp_tree, hf_kdp_version, tvb, 0, 1, ENC_BIG_ENDIAN);
108 proto_tree_add_item(kdp_tree, hf_kdp_xml_body, tvb, 0, -1, ENC_ASCII|ENC_NA);
109 } else {
110 header_len = tvb_get_guint8(tvb, 1) * 4;
111 body_len = tvb_reported_length(tvb);
112 if (header_len > body_len) {
113 body_len = 0; /* malformed packet */
114 } else {
115 body_len = body_len - header_len;
117 packet_flags = tvb_get_guint8(tvb, 2);
118 packet_errors = tvb_get_guint8(tvb, 3);
119 proto_tree_add_item(kdp_tree, hf_kdp_version, tvb, 0, 1, ENC_BIG_ENDIAN);
120 proto_tree_add_item(kdp_tree, hf_kdp_headerlen, tvb, 1, 1, ENC_BIG_ENDIAN);
121 ti = proto_tree_add_item(kdp_tree, hf_kdp_flags, tvb, 2, 1, ENC_BIG_ENDIAN);
122 flags_tree = proto_item_add_subtree(ti, ett_kdp_flags);
124 proto_tree_add_item(flags_tree, hf_kdp_drop_flag, tvb, 2, 1, ENC_BIG_ENDIAN);
125 proto_tree_add_item(flags_tree, hf_kdp_syn_flag, tvb, 2, 1, ENC_BIG_ENDIAN);
126 proto_tree_add_item(flags_tree, hf_kdp_ack_flag, tvb, 2, 1, ENC_BIG_ENDIAN);
127 proto_tree_add_item(flags_tree, hf_kdp_rst_flag, tvb, 2, 1, ENC_BIG_ENDIAN);
128 proto_tree_add_item(flags_tree, hf_kdp_bcst_flag, tvb, 2, 1, ENC_BIG_ENDIAN);
129 proto_tree_add_item(flags_tree, hf_kdp_dup_flag, tvb, 2, 1, ENC_BIG_ENDIAN);
131 proto_tree_add_item(kdp_tree, hf_kdp_errors, tvb, 3, 1, ENC_BIG_ENDIAN);
133 if (header_len > 4) {
134 offset = 4;
135 if (packet_flags & KDP_ACK_FLAG) {
136 proto_tree_add_item(kdp_tree, hf_kdp_destflowid, tvb, offset, 4, ENC_BIG_ENDIAN);
137 offset = offset + 4;
140 if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
141 proto_tree_add_item(kdp_tree, hf_kdp_srcflowid, tvb, offset, 4, ENC_BIG_ENDIAN);
142 src_flowid = tvb_get_ntohl(tvb, offset);
143 offset = offset + 4;
146 proto_tree_add_item(kdp_tree, hf_kdp_sequence, tvb, offset, 4, ENC_BIG_ENDIAN);
147 sequence_number = tvb_get_ntohl(tvb, offset);
148 offset = offset + 4;
150 if (packet_flags & KDP_ACK_FLAG) {
151 proto_tree_add_item(kdp_tree, hf_kdp_ack, tvb, offset, 4, ENC_BIG_ENDIAN);
152 ack_number = tvb_get_ntohl(tvb, offset);
153 offset = offset + 4;
155 if (packet_flags & KDP_SYN_FLAG) {
156 proto_tree_add_item(kdp_tree, hf_kdp_maxsegmentsize, tvb, offset, 4, ENC_BIG_ENDIAN);
157 offset = offset + 4;
160 while (offset < ((body_len > 0) ? header_len - 4 : header_len)) {
161 guint8 option_number;
162 guint8 option_len = 0;
164 option_number = tvb_get_guint8(tvb, offset);
166 proto_tree_add_item(kdp_tree, hf_kdp_optionnumber, tvb, offset, 1, ENC_BIG_ENDIAN);
167 offset = offset + 1;
168 if (option_number > 0) {
169 option_len = tvb_get_guint8(tvb, offset);
170 proto_tree_add_item(kdp_tree, hf_kdp_optionlen, tvb, offset, 1, ENC_BIG_ENDIAN);
171 offset = offset + 1;
174 switch (option_number) {
175 case 0:
176 break;
177 case 1:
178 proto_tree_add_item(kdp_tree, hf_kdp_option1, tvb, offset, 2, ENC_BIG_ENDIAN);
179 offset = offset + 2;
180 break;
181 case 2:
182 proto_tree_add_item(kdp_tree, hf_kdp_option2, tvb, offset, 2, ENC_BIG_ENDIAN);
183 offset = offset + 2;
184 break;
185 case 3:
186 proto_tree_add_item(kdp_tree, hf_kdp_option3, tvb, offset, 2, ENC_BIG_ENDIAN);
187 offset = offset + 2;
188 break;
189 case 4:
190 proto_tree_add_item(kdp_tree, hf_kdp_option4, tvb, offset, 0, ENC_NA);
191 break;
192 case 5:
193 proto_tree_add_item(kdp_tree, hf_kdp_option5, tvb, offset, 0, ENC_NA);
194 break;
195 case 6:
196 proto_tree_add_item(kdp_tree, hf_kdp_option6, tvb, offset, option_len - 2, ENC_NA);
197 offset = offset + option_len - 2;
198 break;
199 case 7:
200 proto_tree_add_item(kdp_tree, hf_kdp_option7, tvb, offset, 2, ENC_BIG_ENDIAN);
201 offset = offset + 2;
202 break;
203 case 8:
204 proto_tree_add_item(kdp_tree, hf_kdp_option8, tvb, offset, 2, ENC_BIG_ENDIAN);
205 offset = offset + 2;
206 break;
207 case 9:
208 proto_tree_add_item(kdp_tree, hf_kdp_option9, tvb, offset, 2, ENC_BIG_ENDIAN);
209 offset = offset + 2;
210 break;
211 default:
212 proto_tree_add_item(kdp_tree, hf_kdp_option_unknown, tvb, offset, option_len - 2, ENC_NA);
213 offset = offset + option_len - 2;
214 break;
218 if (body_len > 0) {
219 proto_tree_add_item(kdp_tree, hf_kdp_fragment, tvb, offset, 2, ENC_BIG_ENDIAN);
220 offset = offset + 2;
222 proto_tree_add_item(kdp_tree, hf_kdp_fragtotal, tvb, offset, 2, ENC_BIG_ENDIAN);
223 offset = offset + 2;
225 proto_tree_add_item(kdp_tree, hf_kdp_body, tvb, offset, -1, ENC_NA);
230 /* Now that we know sequence number and optional ack number, we can
231 print more detailed summary info */
232 if (version != 2) {
233 col_set_str(pinfo->cinfo, COL_INFO, "SDDP message");
234 } else {
235 char ack_string[BUFFER_SIZE];
236 char seq_num_string[BUFFER_SIZE];
237 char src_flowid_string[BUFFER_SIZE];
239 if (packet_flags & KDP_ACK_FLAG) {
240 g_snprintf(ack_string, sizeof(ack_string), "ACK=%x ", ack_number);
241 } else {
242 ack_string[0] = '\0';
244 if (header_len > 4) {
245 g_snprintf(seq_num_string, sizeof(seq_num_string), "SEQ=%x ", sequence_number);
246 } else {
247 seq_num_string[0] = '\0';
249 if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
250 g_snprintf(src_flowid_string, sizeof(src_flowid_string), "SRC_FLOWID=%x ", src_flowid);
251 } else {
252 src_flowid_string[0] = '\0';
254 col_add_fstr(pinfo->cinfo, COL_INFO, "%s%s%s%s%s%s%s%serrors=%d",
255 ((packet_flags & KDP_DROP_FLAG) ? "DROP " : ""),
256 ((packet_flags & KDP_SYN_FLAG) ? "SYN " : ""),
257 ((packet_flags & KDP_RST_FLAG) ? "RST " : ""),
258 ((packet_flags & KDP_BCST_FLAG) ? "BCST " : ""),
259 ((packet_flags & KDP_DUP_FLAG) ? "DUP " : ""),
260 ack_string,
261 seq_num_string,
262 src_flowid_string,
263 packet_errors);
267 void proto_register_kdp(void) {
269 static hf_register_info hf[] = {
270 { &hf_kdp_version,
271 {"KDP version", "kdp.version",
272 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
274 { &hf_kdp_headerlen,
275 {"KDP header len", "kdp.headerlen",
276 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
278 { &hf_kdp_flags,
279 {"KDP flags", "kdp.flags",
280 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
282 { &hf_kdp_drop_flag,
283 {"KDP DROP Flag", "kdp.flags.drop",
284 FT_BOOLEAN, 8, NULL, KDP_DROP_FLAG, NULL, HFILL}
286 { &hf_kdp_syn_flag,
287 {"KDP SYN Flag", "kdp.flags.syn",
288 FT_BOOLEAN, 8, NULL, KDP_SYN_FLAG, NULL, HFILL}
290 { &hf_kdp_ack_flag,
291 {"KDP ACK Flag", "kdp.flags.ack",
292 FT_BOOLEAN, 8, NULL, KDP_ACK_FLAG, NULL, HFILL}
294 { &hf_kdp_rst_flag,
295 {"KDP RST Flag", "kdp.flags.rst",
296 FT_BOOLEAN, 8, NULL, KDP_RST_FLAG, NULL, HFILL}
298 { &hf_kdp_bcst_flag,
299 {"KDP BCST Flag", "kdp.flags.bcst",
300 FT_BOOLEAN, 8, NULL, KDP_BCST_FLAG, NULL, HFILL}
302 { &hf_kdp_dup_flag,
303 {"KDP DUP Flag", "kdp.flags.dup",
304 FT_BOOLEAN, 8, NULL, KDP_DUP_FLAG, NULL, HFILL}
306 { &hf_kdp_errors,
307 {"KDP errors", "kdp.errors",
308 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
310 { &hf_kdp_destflowid,
311 { "DestFlowID", "kdp.destflowid",
312 FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
314 { &hf_kdp_srcflowid,
315 { "SrcFlowID", "kdp.srcflowid",
316 FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
318 { &hf_kdp_sequence,
319 { "Sequence", "kdp.sequence",
320 FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
322 { &hf_kdp_ack,
323 { "Ack", "kdp.ack",
324 FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
326 { &hf_kdp_maxsegmentsize,
327 { "MaxSegmentSize", "kdp.maxsegmentsize",
328 FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
330 { &hf_kdp_optionnumber,
331 { "Option Number", "kdp.optionnumber",
332 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
334 { &hf_kdp_optionlen,
335 { "Option Len", "kdp.option",
336 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
338 { &hf_kdp_option1,
339 { "Option1 - Max Window", "kdp.option1",
340 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
342 { &hf_kdp_option2,
343 { "Option2 - TCP Fraction", "kdp.option2",
344 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
346 { &hf_kdp_option3,
347 { "Option3 - KDP Version", "kdp.option3",
348 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
350 { &hf_kdp_option4,
351 { "Option4 - Enable Reliable", "kdp.option4",
352 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}
354 { &hf_kdp_option5,
355 { "Option5 - Disable Reliable", "kdp.option5",
356 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}
358 { &hf_kdp_option6,
359 { "Option6 - SACK", "kdp.option6",
360 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL}
362 { &hf_kdp_option7,
363 { "Option7 - COS", "kdp.option7",
364 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
366 { &hf_kdp_option8,
367 { "Option8 - BWMIN", "kdp.option8",
368 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
370 { &hf_kdp_option9,
371 { "Option9 - INT", "kdp.option9",
372 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
374 { &hf_kdp_option_unknown,
375 { "Unknown option", "kdp.option_unknown",
376 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL}
378 { &hf_kdp_fragment,
379 { "Fragment", "kdp.fragment",
380 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
382 { &hf_kdp_fragtotal,
383 { "FragTotal", "kdp.fragtotal",
384 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
386 { &hf_kdp_body,
387 { "Encrypted Body", "kdp.body",
388 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL}
390 { &hf_kdp_xml_body,
391 { "XML Body", "kdp.xml_body",
392 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}
396 /* Setup protocol subtree array */
397 static gint *ett[] = {
398 &ett_kdp, &ett_kdp_flags
401 proto_kdp = proto_register_protocol("Kontiki Delivery Protocol", /* name */
402 "KDP", /* short name */
403 "kdp"); /* abbrev */
404 proto_register_field_array(proto_kdp, hf, array_length(hf));
405 proto_register_subtree_array(ett, array_length(ett));
408 void
409 proto_reg_handoff_kdp(void) {
410 dissector_handle_t kdp_handle;
411 kdp_handle = create_dissector_handle(dissect_kdp, proto_kdp);
412 dissector_add_uint("udp.port", KDP_PORT, kdp_handle);