Rename the routines that handle dissector tables with unsigned integer
[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   col_clear(pinfo->cinfo, COL_INFO);
114
115   ti = proto_tree_add_item(tree, proto_omapi, tvb, 0, -1, FALSE);
116   omapi_tree = proto_item_add_subtree(ti, ett_omapi);
117   cursor = ptvcursor_new(omapi_tree, tvb, 0);
118
119   if (tvb_reported_length_remaining(tvb, 0) < 8)
120   {
121     /* Payload too small for OMAPI */
122     DISSECTOR_ASSERT_NOT_REACHED();
123   }
124   else if (tvb_reported_length_remaining(tvb, 0) < 24)
125   {
126     /* This is a startup message */
127     ptvcursor_add(cursor, hf_omapi_version, 4, FALSE);
128     ptvcursor_add(cursor, hf_omapi_hlength, 4, FALSE);
129
130     col_set_str(pinfo->cinfo, COL_INFO, "Status message");
131     proto_item_append_text(ti, ", Status message"); 
132
133     return;
134   }
135   else if ( !(tvb_get_ntohl(tvb, 8) || tvb_get_ntohl(tvb, 12)) )
136   {
137     /* This is a startup message, and more */
138     ptvcursor_add(cursor, hf_omapi_version, 4, FALSE);
139     ptvcursor_add(cursor, hf_omapi_hlength, 4, FALSE);
140
141     if (check_col(pinfo->cinfo, COL_INFO))
142     {
143       col_append_str(pinfo->cinfo, COL_INFO, "Status message");
144     }
145     proto_item_append_text(ti, ", Status message"); 
146   }
147
148   ptvcursor_add(cursor, hf_omapi_auth_id, 4, FALSE);
149   authlength = tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor));
150   ptvcursor_add(cursor, hf_omapi_auth_len, 4, FALSE);
151
152   if (check_col(pinfo->cinfo, COL_INFO)) 
153   {
154     col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
155       val_to_str(tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor)), omapi_opcode_vals, "Unknown opcode (0x%04x)"));
156   }
157   proto_item_append_text(ti, ", Opcode: %s", 
158     val_to_str(tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor)), omapi_opcode_vals, "Unknown opcode (0x%04x)"));
159
160   ptvcursor_add(cursor, hf_omapi_opcode, 4, FALSE);
161   ptvcursor_add(cursor, hf_omapi_handle, 4, FALSE);
162   ptvcursor_add(cursor, hf_omapi_id, 4, FALSE);
163   ptvcursor_add(cursor, hf_omapi_rid, 4, FALSE);
164
165   msglength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
166   while (msglength)
167   {
168     ptvcursor_add(cursor, hf_omapi_msg_name_len, 2, FALSE);
169     ptvcursor_add(cursor, hf_omapi_msg_name, msglength, FALSE);
170     msglength = tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor));
171     ptvcursor_add(cursor, hf_omapi_msg_value_len, 4, FALSE);
172
173     if (msglength == 0)
174     {
175       proto_tree_add_text(omapi_tree, tvb, 0, 0, "Empty string");
176     }
177     else if (msglength == (guint32)~0)
178     {
179       proto_tree_add_text(omapi_tree, tvb, 0, 0, "No value");
180     }        
181     else
182     {
183       ptvcursor_add(cursor, hf_omapi_msg_value, msglength, FALSE);
184     }
185
186     msglength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
187   }
188
189   proto_tree_add_text(omapi_tree, tvb, ptvcursor_current_offset(cursor), 2, "Message end tag");
190   ptvcursor_advance(cursor, 2);
191
192   objlength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
193   while (objlength)
194   {
195     ptvcursor_add(cursor, hf_omapi_obj_name_len, 2, FALSE);
196     ptvcursor_add(cursor, hf_omapi_obj_name, objlength, FALSE);
197     objlength = tvb_get_ntohl(tvb, ptvcursor_current_offset(cursor));
198     ptvcursor_add(cursor, hf_omapi_obj_value_len, 4, FALSE);
199
200     if (objlength == 0)
201     {
202       proto_tree_add_text(omapi_tree, tvb, 0, 0, "Empty string");
203     }
204     else if (objlength == (guint32)~0)
205     {
206       proto_tree_add_text(omapi_tree, tvb, 0, 0, "No value");
207     }        
208     else
209     {
210       ptvcursor_add(cursor, hf_omapi_obj_value, objlength, FALSE);
211     }
212
213     objlength = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
214   }
215
216   proto_tree_add_text(omapi_tree, tvb, ptvcursor_current_offset(cursor), 2, "Object end tag");
217   ptvcursor_advance(cursor, 2);
218
219   if (authlength > 0) {
220     ptvcursor_add(cursor, hf_omapi_signature, authlength, FALSE);
221   }
222 }
223
224 void
225 proto_register_omapi(void)
226 {
227   static hf_register_info hf[] = {
228     { &hf_omapi_version,
229       { "Version", "omapi.version",
230         FT_UINT32, BASE_DEC, NULL, 0x0,
231         NULL, HFILL }},
232     { &hf_omapi_hlength,
233       { "Header length", "omapi.hlength",
234         FT_UINT32, BASE_DEC, NULL, 0x0,
235         NULL, HFILL }},
236     { &hf_omapi_auth_id,
237       { "Authentication ID", "omapi.authid",
238         FT_UINT32, BASE_DEC, NULL, 0x0,
239         NULL, HFILL }},
240     { &hf_omapi_auth_len,
241       { "Authentication length", "omapi.authlength",
242         FT_UINT32, BASE_DEC, NULL, 0x0,
243         NULL, HFILL }},
244     { &hf_omapi_opcode,
245       { "Opcode", "omapi.opcode",
246         FT_UINT32, BASE_DEC, VALS(omapi_opcode_vals), 0x0,
247         NULL, HFILL }},
248     { &hf_omapi_handle,
249       { "Handle", "omapi.handle",
250         FT_UINT32, BASE_DEC, NULL, 0x0,
251         NULL, HFILL }},
252     { &hf_omapi_id,
253       { "ID", "omapi.id",
254         FT_UINT32, BASE_DEC, NULL, 0x0,
255         NULL, HFILL }},
256     { &hf_omapi_rid,
257       { "Response ID", "omapi.rid",
258         FT_UINT32, BASE_DEC, NULL, 0x0,
259         NULL, HFILL }},
260     { &hf_omapi_msg_name_len,
261       { "Message name length", "omapi.msg_name_length",
262         FT_UINT16, BASE_DEC, NULL, 0x0,
263         NULL, HFILL }},
264     { &hf_omapi_msg_name,
265       { "Message name", "omapi.msg_name",
266         FT_STRING, BASE_NONE, NULL, 0x0,
267         NULL, HFILL }},
268     { &hf_omapi_msg_value_len,
269       { "Message value length", "omapi.msg_value_length",
270         FT_UINT32, BASE_DEC, NULL, 0x0,
271         NULL, HFILL }},
272     { &hf_omapi_msg_value,
273       { "Message value", "omapi.msg_value",
274         FT_STRING, BASE_NONE, NULL, 0x0,
275         NULL, HFILL }},
276     { &hf_omapi_obj_name_len,
277       { "Object name length", "omapi.obj_name_length",
278         FT_UINT16, BASE_DEC, NULL, 0x0,
279         NULL, HFILL }},
280     { &hf_omapi_obj_name,
281       { "Object name", "omapi.obj_name",
282         FT_STRING, BASE_NONE, NULL, 0x0,
283         NULL, HFILL }},
284     { &hf_omapi_obj_value_len,
285       { "Object value length", "omapi.object_value_length",
286         FT_UINT32, BASE_DEC, NULL, 0x0,
287         NULL, HFILL }},
288     { &hf_omapi_obj_value,
289       { "Object value", "omapi.obj_value",
290         FT_BYTES, BASE_NONE, NULL, 0x0,
291         NULL, HFILL }},
292     { &hf_omapi_signature,
293       { "Signature", "omapi.signature",
294         FT_BYTES, BASE_NONE, NULL, 0x0,
295         NULL, HFILL }}
296   };
297
298   static gint *ett[] = {
299     &ett_omapi
300   };
301
302   proto_omapi = proto_register_protocol("ISC Object Management API", "OMAPI", "omapi");
303   proto_register_field_array(proto_omapi, hf, array_length(hf));
304   proto_register_subtree_array(ett, array_length(ett));
305 }
306
307 void
308 proto_reg_handoff_omapi(void)
309 {
310   dissector_handle_t omapi_handle;
311
312   omapi_handle = create_dissector_handle(dissect_omapi, proto_omapi);
313   dissector_add_uint("tcp.port", OMAPI_PORT, omapi_handle);
314 }