Don't guard col_set_str (COL_INFO/COL_PROTOCOL) with col_check
[obnox/wireshark/wip.git] / epan / dissectors / packet-kdp.c
1 /* packet-kdp.c
2  * Routines for KDP (Kontiki Delivery Protocol) packet disassembly
3  *
4  * $Id$
5  *
6  * Copyright (c) 2008 by Kontiki Inc.
7  *                    Wade Hennessey <wade@kontiki.com>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1999 Gerald Combs
12  *
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.
17  *
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.
22  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <epan/packet.h>
33 #include <glib.h>
34
35 #define KDP_PORT 19948
36 #define BUFFER_SIZE 80
37 static int proto_kdp = -1;
38 static gint ett_kdp = -1;
39 static gint ett_kdp_flags = -1;
40
41 static int hf_kdp_version = -1;
42 static int hf_kdp_headerlen = -1;
43 static int hf_kdp_flags = -1;
44 static int hf_kdp_errors = -1;
45 static int hf_kdp_destflowid = -1;
46 static int hf_kdp_srcflowid = -1;
47 static int hf_kdp_sequence = -1;
48 static int hf_kdp_ack = -1;
49 static int hf_kdp_maxsegmentsize = -1;
50 static int hf_kdp_optionnumber = -1;
51 static int hf_kdp_optionlen = -1;
52 static int hf_kdp_option1 = -1;
53 static int hf_kdp_option2 = -1;
54 static int hf_kdp_fragment = -1;
55 static int hf_kdp_fragtotal = -1;
56 static int hf_kdp_body = -1;
57 static int hf_kdp_xml_body = -1;
58
59 #define KDP_DROP_FLAG (1 << 0)
60 #define KDP_SYN_FLAG  (1 << 1)
61 #define KDP_ACK_FLAG  (1 << 2)
62 #define KDP_RST_FLAG  (1 << 3)
63 #define KDP_BCST_FLAG (1 << 4)
64 #define KDP_DUP_FLAG  (1 << 5)
65
66 static int hf_kdp_drop_flag = -1;
67 static int hf_kdp_syn_flag = -1;
68 static int hf_kdp_ack_flag = -1;
69 static int hf_kdp_rst_flag = -1;
70 static int hf_kdp_bcst_flag = -1;
71 static int hf_kdp_dup_flag = -1;
72
73 static void dissect_kdp(tvbuff_t *tvb,
74                         packet_info *pinfo,
75                         proto_tree *tree) {
76   guint body_len;
77   guint8 version = 0;
78   guint8 header_len = 0;
79   guint8 packet_flags = 0;
80   guint8 packet_errors = 0;
81   guint32 sequence_number = G_MAXUINT32;
82   guint32 ack_number = G_MAXUINT32; 
83   guint32 src_flowid = G_MAXUINT32; 
84   int offset;
85
86   col_set_str(pinfo->cinfo, COL_PROTOCOL, "KDP");
87   if (check_col(pinfo->cinfo, COL_INFO)) {
88     col_clear(pinfo->cinfo, COL_INFO);
89   }
90   if (tree) {
91     proto_item *ti;
92     proto_tree *kdp_tree, *flags_tree;
93     ti = NULL;
94     kdp_tree = NULL;
95     flags_tree = NULL;
96
97     ti = proto_tree_add_item(tree, proto_kdp, tvb, 0, -1, FALSE);
98     kdp_tree = proto_item_add_subtree(ti, ett_kdp);
99
100     version = tvb_get_guint8(tvb, 0);
101     if (version != 2) {
102       /* Version other than 2 is really SDDP in UDP */
103       proto_tree_add_item(kdp_tree, hf_kdp_version, tvb, 0, 1, FALSE);
104       proto_tree_add_item(kdp_tree, hf_kdp_xml_body, tvb, 0, -1, FALSE);
105     } else {
106       header_len = tvb_get_guint8(tvb, 1) * 4;
107       body_len = tvb_reported_length(tvb);
108       if (header_len > body_len) {
109         body_len = 0;           /* malformed packet */
110       } else {
111         body_len = body_len - header_len;
112       }
113       packet_flags = tvb_get_guint8(tvb, 2);
114       packet_errors = tvb_get_guint8(tvb, 3);
115       proto_tree_add_item(kdp_tree, hf_kdp_version, tvb, 0, 1, FALSE);
116       proto_tree_add_item(kdp_tree, hf_kdp_headerlen, tvb, 1, 1, FALSE);
117       ti = proto_tree_add_item(kdp_tree, hf_kdp_flags, tvb, 2, 1, FALSE);
118       flags_tree = proto_item_add_subtree(ti, ett_kdp_flags);
119
120       proto_tree_add_item(flags_tree, hf_kdp_drop_flag, tvb, 2, 1, FALSE);
121       proto_tree_add_item(flags_tree, hf_kdp_syn_flag, tvb, 2, 1, FALSE);
122       proto_tree_add_item(flags_tree, hf_kdp_ack_flag, tvb, 2, 1, FALSE);
123       proto_tree_add_item(flags_tree, hf_kdp_rst_flag, tvb, 2, 1, FALSE);
124       proto_tree_add_item(flags_tree, hf_kdp_bcst_flag, tvb, 2, 1, FALSE);
125       proto_tree_add_item(flags_tree, hf_kdp_dup_flag, tvb, 2, 1, FALSE);
126
127       proto_tree_add_item(kdp_tree, hf_kdp_errors, tvb, 3, 1, FALSE);  
128
129       if (header_len > 4) {
130         offset = 4;
131         if (packet_flags & KDP_ACK_FLAG) {
132           proto_tree_add_item(kdp_tree, hf_kdp_destflowid, tvb, offset, 4, FALSE);
133           offset = offset + 4;
134         }
135
136         if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
137           proto_tree_add_item(kdp_tree, hf_kdp_srcflowid, tvb, offset, 4, FALSE);
138           src_flowid = tvb_get_ntohl(tvb, offset);
139           offset = offset + 4;
140         }
141
142         proto_tree_add_item(kdp_tree, hf_kdp_sequence, tvb, offset, 4, FALSE);
143         sequence_number = tvb_get_ntohl(tvb, offset);
144         offset = offset + 4;
145
146         if (packet_flags & KDP_ACK_FLAG) {
147           proto_tree_add_item(kdp_tree, hf_kdp_ack, tvb, offset, 4, FALSE);
148           ack_number = tvb_get_ntohl(tvb, offset);
149           offset = offset + 4;
150         }
151         if (packet_flags & KDP_SYN_FLAG) {
152           proto_tree_add_item(kdp_tree,
153                               hf_kdp_maxsegmentsize, tvb, offset, 4, FALSE);
154           offset = offset + 4;
155         }
156
157         while (offset < ((body_len > 0) ? header_len - 4 : header_len)) {
158           guint8 option_number;
159
160           option_number = tvb_get_guint8(tvb, offset);
161       
162           proto_tree_add_item(kdp_tree,
163                               hf_kdp_optionnumber, tvb, offset, 1, FALSE);
164           offset = offset + 1;
165           if (option_number > 0) {
166             proto_tree_add_item(kdp_tree,
167                                 hf_kdp_optionlen, tvb, offset, 1, FALSE);
168             offset = offset + 1;
169           }
170
171           switch (option_number) {
172           case 0:
173             break;
174           case 1:
175             proto_tree_add_item(kdp_tree, hf_kdp_option1, tvb, offset, 2, FALSE);
176             offset = offset + 2;
177             break;
178           case 2:
179             proto_tree_add_item(kdp_tree, hf_kdp_option2, tvb, offset, 2, FALSE);
180             offset = offset + 2;
181             break;
182           default: body_len = 0; /* Invalid option - ignore rest of packet */
183           }
184         }
185       
186         if (body_len > 0) {
187           proto_tree_add_item(kdp_tree, hf_kdp_fragment, tvb, offset, 2, FALSE);
188           offset = offset + 2;
189
190           proto_tree_add_item(kdp_tree, hf_kdp_fragtotal, tvb, offset, 2, FALSE);
191           offset = offset + 2;
192
193           proto_tree_add_item(kdp_tree, hf_kdp_body, tvb, offset, -1, FALSE);
194         }
195       }
196     }
197   }
198   /* Now that we know sequence number and optional ack number, we can
199      print more detailed summary info */
200   if (check_col(pinfo->cinfo, COL_INFO)) {
201     if (version != 2) {
202       col_add_fstr(pinfo->cinfo, COL_INFO, "SDDP message");
203     } else {
204       char ack_string[BUFFER_SIZE];
205       char seq_num_string[BUFFER_SIZE];
206       char src_flowid_string[BUFFER_SIZE];
207
208       if (packet_flags & KDP_ACK_FLAG) {
209         g_snprintf(ack_string, sizeof(ack_string), "ACK=%x ", ack_number);
210       } else {
211         ack_string[0] = '\0';
212       }
213       if (header_len > 4) {
214         g_snprintf(seq_num_string, sizeof(seq_num_string), "SEQ=%x ", sequence_number);
215       } else {
216         seq_num_string[0] = '\0';
217       }
218       if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
219         g_snprintf(src_flowid_string, sizeof(src_flowid_string), "SRC_FLOWID=%x ", src_flowid);
220       } else {
221         src_flowid_string[0] = '\0';
222       }
223       col_add_fstr(pinfo->cinfo, COL_INFO, "%s%s%s%s%s%s%s%serrors=%d",
224                    ((packet_flags & KDP_DROP_FLAG) ? "DROP " : ""),
225                    ((packet_flags & KDP_SYN_FLAG) ? "SYN " : ""),
226                    ((packet_flags & KDP_RST_FLAG) ? "RST " : ""),
227                    ((packet_flags & KDP_BCST_FLAG) ? "BCST " : ""),
228                    ((packet_flags & KDP_DUP_FLAG) ? "DUP " : ""),
229                    ack_string,
230                    seq_num_string,
231                    src_flowid_string,
232                    packet_errors);
233     }
234   }
235 }
236
237 void proto_register_kdp(void) {
238
239   static hf_register_info hf[] = {
240     { &hf_kdp_version,
241       {"KDP version", "kdp.version",
242        FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
243     },
244     { &hf_kdp_headerlen,
245       {"KDP header len", "kdp.headerlen",
246        FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
247     },
248     { &hf_kdp_flags,
249       {"KDP flags", "kdp.flags",
250        FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
251     },
252     { &hf_kdp_drop_flag,
253       {"KDP DROP Flag", "kdp.flags.drop",
254        FT_BOOLEAN, 8, NULL, KDP_DROP_FLAG, NULL, HFILL}
255     },
256     { &hf_kdp_syn_flag,
257       {"KDP SYN Flag", "kdp.flags.syn",
258        FT_BOOLEAN, 8, NULL, KDP_SYN_FLAG, NULL, HFILL}
259     },
260     { &hf_kdp_ack_flag,
261       {"KDP ACK Flag", "kdp.flags.ack",
262        FT_BOOLEAN, 8, NULL, KDP_ACK_FLAG, NULL, HFILL}
263     },
264     { &hf_kdp_rst_flag,
265       {"KDP RST Flag", "kdp.flags.rst",
266        FT_BOOLEAN, 8, NULL, KDP_RST_FLAG, NULL, HFILL}
267     },
268     { &hf_kdp_bcst_flag,
269       {"KDP BCST Flag", "kdp.flags.bcst",
270        FT_BOOLEAN, 8, NULL, KDP_BCST_FLAG, NULL, HFILL}
271     },
272     { &hf_kdp_dup_flag,
273       {"KDP DUP Flag", "kdp.flags.dup",
274        FT_BOOLEAN, 8, NULL, KDP_DUP_FLAG, NULL, HFILL}
275     },
276     { &hf_kdp_errors,
277       {"KDP errors", "kdp.errors",
278        FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
279     },
280     { &hf_kdp_destflowid,
281       { "DestFlowID", "kdp.destflowid",
282         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
283     },
284     { &hf_kdp_srcflowid,
285       { "SrcFlowID", "kdp.srcflowid",
286         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
287     },
288     { &hf_kdp_sequence,
289       { "Sequence", "kdp.sequence",
290         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
291     },
292     { &hf_kdp_ack,
293       { "Ack", "kdp.ack",
294         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
295     },
296     { &hf_kdp_maxsegmentsize,
297       { "MaxSegmentSize", "kdp.maxsegmentsize",
298         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
299     },
300     { &hf_kdp_optionnumber,
301       { "Option Number", "kdp.optionnumber",
302         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
303     },
304     { &hf_kdp_optionlen,
305       { "Option Len", "kdp.option",
306         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
307     },
308     { &hf_kdp_option1,
309       { "Option1 - Max Window", "kdp.option1",
310         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
311     },
312     { &hf_kdp_option2,
313       { "Option2 - TCP Fraction", "kdp.option2",
314         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
315     },
316     { &hf_kdp_fragment,
317       { "Fragment", "kdp.fragment",
318         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
319     },
320     { &hf_kdp_fragtotal,
321       { "FragTotal", "kdp.fragtotal",
322         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
323     },
324     { &hf_kdp_body,
325       { "Encrypted Body", "kdp.body",
326         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL}
327     },
328     { &hf_kdp_xml_body,
329       { "XML Body", "kdp.body",
330         FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}
331     }
332   };
333
334   /* Setup protocol subtree array */
335   static gint *ett[] = {
336     &ett_kdp, &ett_kdp_flags
337   };
338
339   proto_kdp = proto_register_protocol("Kontiki Delivery Protocol", /* name */
340                                       "KDP",            /* short name */
341                                       "kdp");           /* abbrev */
342   proto_register_field_array(proto_kdp, hf, array_length(hf));
343   proto_register_subtree_array(ett, array_length(ett));
344 }
345
346 void
347 proto_reg_handoff_kdp(void) {
348   dissector_handle_t kdp_handle;
349   kdp_handle = create_dissector_handle(dissect_kdp, proto_kdp);
350   dissector_add("udp.port", KDP_PORT, kdp_handle);
351 }
352