Use col_set_str instead of col_add_fstr when adding constant strings to COL_INFO
[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   col_clear(pinfo->cinfo, COL_INFO);
88   if (tree) {
89     proto_item *ti;
90     proto_tree *kdp_tree, *flags_tree;
91     ti = NULL;
92     kdp_tree = NULL;
93     flags_tree = NULL;
94
95     ti = proto_tree_add_item(tree, proto_kdp, tvb, 0, -1, FALSE);
96     kdp_tree = proto_item_add_subtree(ti, ett_kdp);
97
98     version = tvb_get_guint8(tvb, 0);
99     if (version != 2) {
100       /* Version other than 2 is really SDDP in UDP */
101       proto_tree_add_item(kdp_tree, hf_kdp_version, tvb, 0, 1, FALSE);
102       proto_tree_add_item(kdp_tree, hf_kdp_xml_body, tvb, 0, -1, FALSE);
103     } else {
104       header_len = tvb_get_guint8(tvb, 1) * 4;
105       body_len = tvb_reported_length(tvb);
106       if (header_len > body_len) {
107         body_len = 0;           /* malformed packet */
108       } else {
109         body_len = body_len - header_len;
110       }
111       packet_flags = tvb_get_guint8(tvb, 2);
112       packet_errors = tvb_get_guint8(tvb, 3);
113       proto_tree_add_item(kdp_tree, hf_kdp_version, tvb, 0, 1, FALSE);
114       proto_tree_add_item(kdp_tree, hf_kdp_headerlen, tvb, 1, 1, FALSE);
115       ti = proto_tree_add_item(kdp_tree, hf_kdp_flags, tvb, 2, 1, FALSE);
116       flags_tree = proto_item_add_subtree(ti, ett_kdp_flags);
117
118       proto_tree_add_item(flags_tree, hf_kdp_drop_flag, tvb, 2, 1, FALSE);
119       proto_tree_add_item(flags_tree, hf_kdp_syn_flag, tvb, 2, 1, FALSE);
120       proto_tree_add_item(flags_tree, hf_kdp_ack_flag, tvb, 2, 1, FALSE);
121       proto_tree_add_item(flags_tree, hf_kdp_rst_flag, tvb, 2, 1, FALSE);
122       proto_tree_add_item(flags_tree, hf_kdp_bcst_flag, tvb, 2, 1, FALSE);
123       proto_tree_add_item(flags_tree, hf_kdp_dup_flag, tvb, 2, 1, FALSE);
124
125       proto_tree_add_item(kdp_tree, hf_kdp_errors, tvb, 3, 1, FALSE);  
126
127       if (header_len > 4) {
128         offset = 4;
129         if (packet_flags & KDP_ACK_FLAG) {
130           proto_tree_add_item(kdp_tree, hf_kdp_destflowid, tvb, offset, 4, FALSE);
131           offset = offset + 4;
132         }
133
134         if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
135           proto_tree_add_item(kdp_tree, hf_kdp_srcflowid, tvb, offset, 4, FALSE);
136           src_flowid = tvb_get_ntohl(tvb, offset);
137           offset = offset + 4;
138         }
139
140         proto_tree_add_item(kdp_tree, hf_kdp_sequence, tvb, offset, 4, FALSE);
141         sequence_number = tvb_get_ntohl(tvb, offset);
142         offset = offset + 4;
143
144         if (packet_flags & KDP_ACK_FLAG) {
145           proto_tree_add_item(kdp_tree, hf_kdp_ack, tvb, offset, 4, FALSE);
146           ack_number = tvb_get_ntohl(tvb, offset);
147           offset = offset + 4;
148         }
149         if (packet_flags & KDP_SYN_FLAG) {
150           proto_tree_add_item(kdp_tree,
151                               hf_kdp_maxsegmentsize, tvb, offset, 4, FALSE);
152           offset = offset + 4;
153         }
154
155         while (offset < ((body_len > 0) ? header_len - 4 : header_len)) {
156           guint8 option_number;
157
158           option_number = tvb_get_guint8(tvb, offset);
159       
160           proto_tree_add_item(kdp_tree,
161                               hf_kdp_optionnumber, tvb, offset, 1, FALSE);
162           offset = offset + 1;
163           if (option_number > 0) {
164             proto_tree_add_item(kdp_tree,
165                                 hf_kdp_optionlen, tvb, offset, 1, FALSE);
166             offset = offset + 1;
167           }
168
169           switch (option_number) {
170           case 0:
171             break;
172           case 1:
173             proto_tree_add_item(kdp_tree, hf_kdp_option1, tvb, offset, 2, FALSE);
174             offset = offset + 2;
175             break;
176           case 2:
177             proto_tree_add_item(kdp_tree, hf_kdp_option2, tvb, offset, 2, FALSE);
178             offset = offset + 2;
179             break;
180           default: body_len = 0; /* Invalid option - ignore rest of packet */
181           }
182         }
183       
184         if (body_len > 0) {
185           proto_tree_add_item(kdp_tree, hf_kdp_fragment, tvb, offset, 2, FALSE);
186           offset = offset + 2;
187
188           proto_tree_add_item(kdp_tree, hf_kdp_fragtotal, tvb, offset, 2, FALSE);
189           offset = offset + 2;
190
191           proto_tree_add_item(kdp_tree, hf_kdp_body, tvb, offset, -1, FALSE);
192         }
193       }
194     }
195   }
196   /* Now that we know sequence number and optional ack number, we can
197      print more detailed summary info */
198   if (check_col(pinfo->cinfo, COL_INFO)) {
199     if (version != 2) {
200       col_set_str(pinfo->cinfo, COL_INFO, "SDDP message");
201     } else {
202       char ack_string[BUFFER_SIZE];
203       char seq_num_string[BUFFER_SIZE];
204       char src_flowid_string[BUFFER_SIZE];
205
206       if (packet_flags & KDP_ACK_FLAG) {
207         g_snprintf(ack_string, sizeof(ack_string), "ACK=%x ", ack_number);
208       } else {
209         ack_string[0] = '\0';
210       }
211       if (header_len > 4) {
212         g_snprintf(seq_num_string, sizeof(seq_num_string), "SEQ=%x ", sequence_number);
213       } else {
214         seq_num_string[0] = '\0';
215       }
216       if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
217         g_snprintf(src_flowid_string, sizeof(src_flowid_string), "SRC_FLOWID=%x ", src_flowid);
218       } else {
219         src_flowid_string[0] = '\0';
220       }
221       col_add_fstr(pinfo->cinfo, COL_INFO, "%s%s%s%s%s%s%s%serrors=%d",
222                    ((packet_flags & KDP_DROP_FLAG) ? "DROP " : ""),
223                    ((packet_flags & KDP_SYN_FLAG) ? "SYN " : ""),
224                    ((packet_flags & KDP_RST_FLAG) ? "RST " : ""),
225                    ((packet_flags & KDP_BCST_FLAG) ? "BCST " : ""),
226                    ((packet_flags & KDP_DUP_FLAG) ? "DUP " : ""),
227                    ack_string,
228                    seq_num_string,
229                    src_flowid_string,
230                    packet_errors);
231     }
232   }
233 }
234
235 void proto_register_kdp(void) {
236
237   static hf_register_info hf[] = {
238     { &hf_kdp_version,
239       {"KDP version", "kdp.version",
240        FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
241     },
242     { &hf_kdp_headerlen,
243       {"KDP header len", "kdp.headerlen",
244        FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
245     },
246     { &hf_kdp_flags,
247       {"KDP flags", "kdp.flags",
248        FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
249     },
250     { &hf_kdp_drop_flag,
251       {"KDP DROP Flag", "kdp.flags.drop",
252        FT_BOOLEAN, 8, NULL, KDP_DROP_FLAG, NULL, HFILL}
253     },
254     { &hf_kdp_syn_flag,
255       {"KDP SYN Flag", "kdp.flags.syn",
256        FT_BOOLEAN, 8, NULL, KDP_SYN_FLAG, NULL, HFILL}
257     },
258     { &hf_kdp_ack_flag,
259       {"KDP ACK Flag", "kdp.flags.ack",
260        FT_BOOLEAN, 8, NULL, KDP_ACK_FLAG, NULL, HFILL}
261     },
262     { &hf_kdp_rst_flag,
263       {"KDP RST Flag", "kdp.flags.rst",
264        FT_BOOLEAN, 8, NULL, KDP_RST_FLAG, NULL, HFILL}
265     },
266     { &hf_kdp_bcst_flag,
267       {"KDP BCST Flag", "kdp.flags.bcst",
268        FT_BOOLEAN, 8, NULL, KDP_BCST_FLAG, NULL, HFILL}
269     },
270     { &hf_kdp_dup_flag,
271       {"KDP DUP Flag", "kdp.flags.dup",
272        FT_BOOLEAN, 8, NULL, KDP_DUP_FLAG, NULL, HFILL}
273     },
274     { &hf_kdp_errors,
275       {"KDP errors", "kdp.errors",
276        FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL}
277     },
278     { &hf_kdp_destflowid,
279       { "DestFlowID", "kdp.destflowid",
280         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
281     },
282     { &hf_kdp_srcflowid,
283       { "SrcFlowID", "kdp.srcflowid",
284         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
285     },
286     { &hf_kdp_sequence,
287       { "Sequence", "kdp.sequence",
288         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
289     },
290     { &hf_kdp_ack,
291       { "Ack", "kdp.ack",
292         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
293     },
294     { &hf_kdp_maxsegmentsize,
295       { "MaxSegmentSize", "kdp.maxsegmentsize",
296         FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}
297     },
298     { &hf_kdp_optionnumber,
299       { "Option Number", "kdp.optionnumber",
300         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
301     },
302     { &hf_kdp_optionlen,
303       { "Option Len", "kdp.option",
304         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL}
305     },
306     { &hf_kdp_option1,
307       { "Option1 - Max Window", "kdp.option1",
308         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
309     },
310     { &hf_kdp_option2,
311       { "Option2 - TCP Fraction", "kdp.option2",
312         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
313     },
314     { &hf_kdp_fragment,
315       { "Fragment", "kdp.fragment",
316         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
317     },
318     { &hf_kdp_fragtotal,
319       { "FragTotal", "kdp.fragtotal",
320         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}
321     },
322     { &hf_kdp_body,
323       { "Encrypted Body", "kdp.body",
324         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL}
325     },
326     { &hf_kdp_xml_body,
327       { "XML Body", "kdp.body",
328         FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL}
329     }
330   };
331
332   /* Setup protocol subtree array */
333   static gint *ett[] = {
334     &ett_kdp, &ett_kdp_flags
335   };
336
337   proto_kdp = proto_register_protocol("Kontiki Delivery Protocol", /* name */
338                                       "KDP",            /* short name */
339                                       "kdp");           /* abbrev */
340   proto_register_field_array(proto_kdp, hf, array_length(hf));
341   proto_register_subtree_array(ett, array_length(ett));
342 }
343
344 void
345 proto_reg_handoff_kdp(void) {
346   dissector_handle_t kdp_handle;
347   kdp_handle = create_dissector_handle(dissect_kdp, proto_kdp);
348   dissector_add("udp.port", KDP_PORT, kdp_handle);
349 }
350