Don't guard col_set_str (COL_INFO/COL_PROTOCOL) with col_check
[obnox/wireshark/wip.git] / epan / dissectors / packet-omapi.c
1 /* packet-omapi.c
2  * ISC OMAPI (Object Management API) dissector
3  * Copyright 2006, Jaap Keuter <jaap.keuter@xs4all.nl>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 /*
27  * From the description api+protocol.
28  * All fields are 32 bit unless stated otherwise.
29  *
30  * On startup, each side sends a status message indicating what version
31  * of the protocol they are speaking. The status message looks like this:
32  * +---------+---------+
33  * | version | hlength |
34  * +---------+---------+
35  *
36  * The fixed-length header consists of:
37  * +--------+----+--------+----+-----+---------+------------+------------+-----+
38  * | authid | op | handle | id | rid | authlen | msg values | obj values | sig |
39  * +--------+----+--------+----+-----+---------+------v-----+-----v------+--v--+
40  * NOTE: real life capture shows order to be: authid, authlen, opcode, handle...
41  *
42  * The message and object values consists of:
43  * +---------+------+----------+-------+
44  * | namelen | name | valuelen | value |
45  * +---16b---+--v---+----------+---v---+
46  */
47
48 #ifdef HAVE_CONFIG_H
49 # include "config.h"
50 #endif
51
52 #include <epan/packet.h>
53 #include <epan/ptvcursor.h>
54
55 static int proto_omapi = -1;
56 static int hf_omapi_version = -1;
57 static int hf_omapi_hlength = -1;
58 static int hf_omapi_auth_id = -1;
59 static int hf_omapi_auth_len = -1;
60 static int hf_omapi_opcode = -1;
61 static int hf_omapi_handle = -1;
62 static int hf_omapi_id = -1;
63 static int hf_omapi_rid = -1;
64 static int hf_omapi_msg_name_len = -1; /* 16bit */
65 static int hf_omapi_msg_name = -1;
66 static int hf_omapi_msg_value_len = -1;
67 static int hf_omapi_msg_value = -1;
68 static int hf_omapi_obj_name_len = -1; /* 16bit */
69 static int hf_omapi_obj_name = -1;
70 static int hf_omapi_obj_value_len = -1;
71 static int hf_omapi_obj_value = -1;
72 static int hf_omapi_signature = -1;
73
74 static gint ett_omapi = -1;
75
76 #define OMAPI_PORT 7911
77
78 #define OP_OPEN         1
79 #define OP_REFRESH      2
80 #define OP_UPDATE       3
81 #define OP_NOTIFY       4
82 #define OP_ERROR        5
83 #define OP_DELETE       6
84 #define OP_NOTIFY_CANCEL        7
85 #define OP_NOTIFY_CANCELLED     8
86
87 static const value_string omapi_opcode_vals[] = {
88   { OP_OPEN,    "Open" },
89   { OP_REFRESH, "Refresh" },
90   { OP_UPDATE,  "Update" },
91   { OP_NOTIFY,  "Notify" },
92   { OP_ERROR,   "Error" },
93   { OP_DELETE,  "Delete" },
94   { OP_NOTIFY_CANCEL,   "Notify cancel" },
95   { OP_NOTIFY_CANCELLED,"Notify cancelled" },
96   { 0, NULL }
97 };
98
99 static void
100 dissect_omapi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
101 {
102   proto_item *ti;
103   proto_tree *omapi_tree;
104   ptvcursor_t* cursor;
105
106   guint32 authlength;
107   guint32 msglength;
108   guint32 objlength;
109
110
111   col_set_str(pinfo->cinfo, COL_PROTOCOL, "OMAPI");
112
113   if (check_col(pinfo->cinfo, COL_INFO)) 
114   {
115     col_clear(pinfo->cinfo, COL_INFO);
116   }
117
118   ti = proto_tree_add_item(tree, proto_omapi, tvb, 0, -1, FALSE);
119   omapi_tree = proto_item_add_subtree(ti, ett_omapi);
120   cursor = ptvcursor_new(omapi_tree, tvb, 0);
121
122   if (tvb_reported_length_remaining(tvb, 0) < 8)
123   {
124     /* Payload too small for OMAPI */
125     DISSECTOR_ASSERT_NOT_REACHED();
126   }
127   else if (tvb_reported_length_remaining(tvb, 0) < 24)
128   {
129     /* This is a startup message */
130     ptvcursor_add(cursor, hf_omapi_version, 4, FALSE);
131     ptvcursor_add(cursor, hf_omapi_hlength, 4, FALSE);
132
133     col_set_str(pinfo->cinfo, COL_INFO, "Status message");
134     proto_item_append_text(ti, ", Status message"); 
135
136     return;
137   }
138
139   ptvcursor_add(cursor, hf_omapi_auth_id, 4, FALSE);
140   authlength = tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor));
141   ptvcursor_add(cursor, hf_omapi_auth_len, 4, FALSE);
142
143   if (check_col(pinfo->cinfo, COL_INFO)) 
144   {
145     col_add_str(pinfo->cinfo, COL_INFO, 
146       val_to_str(tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor)), omapi_opcode_vals, "Unknown opcode (0x%04x)"));
147   }
148   proto_item_append_text(ti, ", Opcode: %s", 
149     val_to_str(tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor)), omapi_opcode_vals, "Unknown opcode (0x%04x)"));
150
151   ptvcursor_add(cursor, hf_omapi_opcode, 4, FALSE);
152   ptvcursor_add(cursor, hf_omapi_handle, 4, FALSE);
153   ptvcursor_add(cursor, hf_omapi_id, 4, FALSE);
154   ptvcursor_add(cursor, hf_omapi_rid, 4, FALSE);
155
156   msglength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
157   while (msglength)
158   {
159     ptvcursor_add(cursor, hf_omapi_msg_name_len, 2, FALSE);
160     ptvcursor_add(cursor, hf_omapi_msg_name, msglength, FALSE);
161     msglength = tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor));
162     ptvcursor_add(cursor, hf_omapi_msg_value_len, 4, FALSE);
163
164     if (msglength == 0)
165     {
166       proto_tree_add_text(omapi_tree, tvb, 0, 0, "Empty string");
167     }
168     else if (msglength == (guint32)~0)
169     {
170       proto_tree_add_text(omapi_tree, tvb, 0, 0, "No value");
171     }        
172     else
173     {
174       ptvcursor_add(cursor, hf_omapi_msg_value, msglength, FALSE);
175     }
176
177     msglength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
178   }
179
180   proto_tree_add_text(omapi_tree, tvb, ptvcursor_current_offset(cursor), 2, "Message end tag");
181   ptvcursor_advance(cursor, 2);
182
183   objlength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
184   while (objlength)
185   {
186     ptvcursor_add(cursor, hf_omapi_obj_name_len, 2, FALSE);
187     ptvcursor_add(cursor, hf_omapi_obj_name, objlength, FALSE);
188     objlength = tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor));
189     ptvcursor_add(cursor, hf_omapi_obj_value_len, 4, FALSE);
190
191     if (objlength == 0)
192     {
193       proto_tree_add_text(omapi_tree, tvb, 0, 0, "Empty string");
194     }
195     else if (objlength == (guint32)~0)
196     {
197       proto_tree_add_text(omapi_tree, tvb, 0, 0, "No value");
198     }        
199     else
200     {
201       ptvcursor_add(cursor, hf_omapi_obj_value, objlength, FALSE);
202     }
203
204     objlength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
205   }
206
207   proto_tree_add_text(omapi_tree, tvb, ptvcursor_current_offset(cursor), 2, "Object end tag");
208   ptvcursor_advance(cursor, 2);
209
210   if (authlength > 0) {
211     ptvcursor_add(cursor, hf_omapi_signature, authlength, FALSE);
212   }
213 }
214
215 void
216 proto_register_omapi(void)
217 {
218   static hf_register_info hf[] = {
219     { &hf_omapi_version,
220       { "Version", "omapi.version",
221         FT_UINT32, BASE_DEC, NULL, 0x0,
222         NULL, HFILL }},
223     { &hf_omapi_hlength,
224       { "Header length", "omapi.hlength",
225         FT_UINT32, BASE_DEC, NULL, 0x0,
226         NULL, HFILL }},
227     { &hf_omapi_auth_id,
228       { "Authentication ID", "omapi.authid",
229         FT_UINT32, BASE_DEC, NULL, 0x0,
230         NULL, HFILL }},
231     { &hf_omapi_auth_len,
232       { "Authentication length", "omapi.authlength",
233         FT_UINT32, BASE_DEC, NULL, 0x0,
234         NULL, HFILL }},
235     { &hf_omapi_opcode,
236       { "Opcode", "omapi.opcode",
237         FT_UINT32, BASE_DEC, VALS(omapi_opcode_vals), 0x0,
238         NULL, HFILL }},
239     { &hf_omapi_handle,
240       { "Handle", "omapi.handle",
241         FT_UINT32, BASE_DEC, NULL, 0x0,
242         NULL, HFILL }},
243     { &hf_omapi_id,
244       { "ID", "omapi.id",
245         FT_UINT32, BASE_DEC, NULL, 0x0,
246         NULL, HFILL }},
247     { &hf_omapi_rid,
248       { "Response ID", "omapi.rid",
249         FT_UINT32, BASE_DEC, NULL, 0x0,
250         NULL, HFILL }},
251     { &hf_omapi_msg_name_len,
252       { "Message name length", "omapi.msg_name_length",
253         FT_UINT16, BASE_DEC, NULL, 0x0,
254         NULL, HFILL }},
255     { &hf_omapi_msg_name,
256       { "Message name", "omapi.msg_name",
257         FT_STRING, BASE_NONE, NULL, 0x0,
258         NULL, HFILL }},
259     { &hf_omapi_msg_value_len,
260       { "Message value length", "omapi.msg_value_length",
261         FT_UINT32, BASE_DEC, NULL, 0x0,
262         NULL, HFILL }},
263     { &hf_omapi_msg_value,
264       { "Message value", "omapi.msg_value",
265         FT_STRING, BASE_NONE, NULL, 0x0,
266         NULL, HFILL }},
267     { &hf_omapi_obj_name_len,
268       { "Object name length", "omapi.obj_name_length",
269         FT_UINT16, BASE_DEC, NULL, 0x0,
270         NULL, HFILL }},
271     { &hf_omapi_obj_name,
272       { "Object name", "omapi.obj_name",
273         FT_STRING, BASE_NONE, NULL, 0x0,
274         NULL, HFILL }},
275     { &hf_omapi_obj_value_len,
276       { "Object value length", "omapi.object_value_length",
277         FT_UINT32, BASE_DEC, NULL, 0x0,
278         NULL, HFILL }},
279     { &hf_omapi_obj_value,
280       { "Object value", "omapi.obj_value",
281         FT_BYTES, BASE_NONE, NULL, 0x0,
282         NULL, HFILL }},
283     { &hf_omapi_signature,
284       { "Signature", "omapi.signature",
285         FT_BYTES, BASE_NONE, NULL, 0x0,
286         NULL, HFILL }}
287   };
288
289   static gint *ett[] = {
290     &ett_omapi
291   };
292
293   proto_omapi = proto_register_protocol("ISC Object Management API", "OMAPI", "omapi");
294   proto_register_field_array(proto_omapi, hf, array_length(hf));
295   proto_register_subtree_array(ett, array_length(ett));
296 }
297
298 void
299 proto_reg_handoff_omapi(void)
300 {
301   dissector_handle_t omapi_handle;
302
303   omapi_handle = create_dissector_handle(dissect_omapi, proto_omapi);
304   dissector_add("tcp.port", OMAPI_PORT, omapi_handle);
305 }