From Didier Gautheron:
[obnox/wireshark/wip.git] / epan / dissectors / packet-miop.c
1 /* packet-miop.c
2  * Routines for CORBA MIOP packet disassembly
3  * Significantly based on packet-giop.c
4  * Copyright 2009 Alvaro Vega Garcia <avega at tid dot es>
5  *
6  * According with Unreliable Multicast Draft Adopted Specification 
7  * 2001 October (OMG)
8  * Chapter 29: Unreliable Multicast Inter-ORB Protocol (MIOP)
9  * http://www.omg.org/technology/documents/specialized_corba.htm
10  *
11  * $Id$
12  *
13  * Wireshark - Network traffic analyzer
14  * By Gerald Combs <gerald@wireshark.org>
15  * Copyright 1998 Gerald Combs
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
30  */
31
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <string.h>
38 #include <stdio.h>
39 #include <errno.h>
40 #include <ctype.h>
41 #include <glib.h>
42 #include <math.h>
43
44 #include "isprint.h"
45
46 #include <epan/packet.h>
47 #include <epan/emem.h>
48 #include <epan/expert.h>
49
50 #include "packet-giop.h"
51 #include "packet-tcp.h"
52
53 /*
54  * Useful visible data/structs
55  */
56
57 #define MIOP_MAX_UNIQUE_ID_LENGTH   252
58
59 #define MIOP_HEADER_SIZE 16
60
61 /*
62  * Set to 1 for DEBUG output - TODO make this a runtime option
63  */
64
65 #define DEBUG   0
66
67 /*
68  * ------------------------------------------------------------------------------------------+
69  *                                 Data/Variables/Structs
70  * ------------------------------------------------------------------------------------------+
71  */
72
73
74 static int proto_miop = -1;
75
76 /*
77  * (sub)Tree declares
78  */
79
80
81 static gint hf_miop_magic = -1;
82 static gint hf_miop_hdr_version = -1;
83 static gint hf_miop_flags = -1;
84 static gint hf_miop_packet_length = -1;
85 static gint hf_miop_packet_number = -1;
86 static gint hf_miop_number_of_packets = -1;
87 static gint hf_miop_unique_id_len = -1;
88 static gint hf_miop_unique_id = -1;
89
90 static gint ett_miop = -1;
91
92 #define MIOP_MAGIC       "MIOP"
93
94 static void dissect_miop (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
95
96 static gboolean
97 dissect_miop_heur (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree) {
98
99   guint tot_len;
100
101   /* check magic number and version */
102
103
104   tot_len = tvb_length(tvb);
105
106   if (tot_len < MIOP_HEADER_SIZE) /* tot_len < 16 */
107     {
108       /* Not enough data captured to hold the GIOP header; don't try
109          to interpret it as GIOP. */
110       return FALSE;
111     }
112
113   if ( tvb_memeql(tvb, 0, MIOP_MAGIC ,4) != 0)
114     return FALSE;
115
116   if (pinfo->ptype != PT_UDP)
117     return FALSE;
118   
119   dissect_miop (tvb, pinfo, tree);
120
121   /* TODO: make reasembly */
122
123   return TRUE;
124
125 }
126
127
128 /* Main entry point */
129 static void dissect_miop (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree) {
130   guint offset = 0;
131
132   proto_tree *miop_tree = NULL;
133   proto_item *ti;
134
135   guint8 hdr_version;
136   guint version_major;
137   guint version_minor;
138   
139   guint8 flags;
140
141   guint16 packet_length;
142   guint packet_number;
143   guint number_of_packets;
144   gboolean little_endian;
145   
146   guint32 unique_id_len;
147   
148   emem_strbuf_t *flags_strbuf = ep_strbuf_new_label("none");
149
150   col_set_str (pinfo->cinfo, COL_PROTOCOL, MIOP_MAGIC);
151   /* Clear out stuff in the info column */
152   col_clear(pinfo->cinfo, COL_INFO);
153
154   /* Extract major and minor version numbers */
155   hdr_version = tvb_get_guint8(tvb, 4);
156   version_major = ((hdr_version & 0xf0) >> 4);
157   version_minor =  (hdr_version & 0x0f);
158
159   if (hdr_version != 16)
160     {
161       col_add_fstr (pinfo->cinfo, COL_INFO, "Version %u.%u",
162                     version_major, version_minor);
163       if (tree)
164         {
165           ti = proto_tree_add_item (tree, proto_miop, tvb, 0, -1, FALSE);
166           miop_tree = proto_item_add_subtree (ti, ett_miop);
167           proto_tree_add_text (miop_tree, tvb, 0, -1,
168                                "Version %u.%u",
169                                version_major, version_minor);
170           expert_add_info_format(pinfo, ti, PI_UNDECODED, PI_WARN,
171                                "MIOP version %u.%u not supported",
172                                version_major, version_minor);
173         }
174       return;
175     }
176
177   flags = tvb_get_guint8(tvb, 5);
178   little_endian = flags & 0x01;
179
180   if (little_endian) {
181     packet_length = tvb_get_letohs(tvb, 6);
182     packet_number = tvb_get_letohl(tvb, 8);
183     number_of_packets = tvb_get_letohl(tvb, 12);
184     unique_id_len = tvb_get_letohl(tvb, 16);
185   }
186   else {
187     packet_length = tvb_get_ntohs(tvb, 6);
188     packet_number = tvb_get_ntohl(tvb, 8);
189     number_of_packets = tvb_get_ntohl(tvb, 12);
190     unique_id_len = tvb_get_ntohl(tvb, 16);
191   }
192
193   col_add_fstr (pinfo->cinfo, COL_INFO, "MIOP %u.%u Packet s=%d (%u of %u)",
194                 version_major, version_minor, packet_length, 
195                 packet_number + 1, 
196                 number_of_packets);
197
198   if (tree)
199     {
200           
201       ti = proto_tree_add_item (tree, proto_miop, tvb, 0, -1, FALSE);
202       miop_tree = proto_item_add_subtree (ti, ett_miop);
203
204       /* XXX - Should we bail out if we don't have the right magic number? */
205       proto_tree_add_item(miop_tree, hf_miop_magic, tvb, offset, 4, FALSE);
206       offset += 4;
207       proto_tree_add_uint_format(miop_tree, hf_miop_hdr_version, tvb, offset, 1, hdr_version,
208                            "Version: %u.%u", version_major, version_minor);
209       offset++;
210       if (flags & 0x01) {
211         ep_strbuf_printf(flags_strbuf, "little-endian");
212       }
213       if (flags & 0x02) {
214         ep_strbuf_append_printf(flags_strbuf, "%s%s",
215                                 flags_strbuf->len ? ", " : "", "last message");
216       }
217       ti = proto_tree_add_uint_format_value(miop_tree, hf_miop_flags, tvb, offset, 1,
218                                             flags, "0x%02x (%s)", flags, flags_strbuf->str);
219       offset++;
220       proto_tree_add_item(miop_tree, hf_miop_packet_length, tvb, offset, 2, little_endian);
221       offset += 2;
222       proto_tree_add_item(miop_tree, hf_miop_packet_number, tvb, offset, 4, little_endian);
223       offset += 4;
224       proto_tree_add_item(miop_tree, hf_miop_number_of_packets, tvb, offset, 4, little_endian);
225
226       offset += 4;
227       ti = proto_tree_add_item(miop_tree, hf_miop_unique_id_len, tvb, offset, 4, little_endian);
228
229       if (unique_id_len >= MIOP_MAX_UNIQUE_ID_LENGTH) {
230         expert_add_info_format(pinfo, ti, PI_MALFORMED, PI_WARN,
231                        "Unique Id length (%u) exceeds max value (%u)",
232                        unique_id_len, MIOP_MAX_UNIQUE_ID_LENGTH);
233         return;
234       }
235
236       offset += 4;
237       proto_tree_add_item(miop_tree, hf_miop_unique_id, tvb, offset, unique_id_len,
238                           little_endian);
239
240       if (packet_number == 0) {
241         /*  It is the first packet of the collection
242             We can call to GIOP dissector to show more about this first 
243             uncompleted GIOP message 
244         */
245         tvbuff_t *payload_tvb;
246       
247         offset += unique_id_len;
248         payload_tvb = tvb_new_subset_remaining (tvb, offset);
249         dissect_giop(payload_tvb, pinfo, tree);
250       }
251     }
252
253
254 }
255
256
257 void proto_register_miop (void) {
258
259
260   /* A header field is something you can search/filter on.
261    * 
262    * We create a structure to register our fields. It consists of an
263    * array of hf_register_info structures, each of which are of the format
264    * {&(field id), {name, abbrev, type, display, strings, bitmask, blurb, HFILL}}.
265    */
266   static hf_register_info hf[] = {
267     { &hf_miop_magic,
268       { "Magic", "miop.magic", FT_STRING, BASE_NONE, NULL, 0x0,
269         "PacketHeader magic", HFILL }},
270     { &hf_miop_hdr_version,
271       { "Version", "miop.hdr_version", FT_UINT8, BASE_HEX, NULL, 0x0,
272         "PacketHeader hdr_version", HFILL }},
273     { &hf_miop_flags,
274       { "Flags", "miop.flags", FT_UINT8, BASE_OCT, NULL, 0x0,
275         "PacketHeader flags", HFILL }},
276     { &hf_miop_packet_length,
277       { "Length", "miop.packet_length", FT_UINT16, BASE_DEC, NULL, 0x0,
278         "PacketHeader packet_length", HFILL }},
279     { &hf_miop_packet_number,
280       { "PacketNumber", "miop.packet_number",  FT_UINT32, BASE_DEC, NULL, 0x0,
281         "PacketHeader packet_number", HFILL }},
282     { &hf_miop_number_of_packets,
283       { "NumberOfPackets", "miop.number_of_packets", FT_UINT32, BASE_DEC, NULL, 0x0,
284         "PacketHeader number_of_packets", HFILL }},
285     { &hf_miop_unique_id_len,
286       { "UniqueIdLength", "miop.unique_id_len", FT_UINT32, BASE_DEC, NULL, 0x0,
287         "UniqueId length", HFILL }},
288     { &hf_miop_unique_id,
289       { "UniqueId", "miop.unique_id", FT_BYTES, BASE_NONE, NULL, 0x0,
290         "UniqueId id", HFILL }},
291   };
292
293
294   static gint *ett[] = {
295     &ett_miop
296   };
297
298   proto_miop = proto_register_protocol("Unreliable Multicast Inter-ORB Protocol", "MIOP",
299                                        "miop");
300   proto_register_field_array (proto_miop, hf, array_length (hf));
301   proto_register_subtree_array (ett, array_length (ett));
302
303   register_dissector("miop", dissect_miop, proto_miop);
304
305 }
306
307
308 void proto_reg_handoff_miop (void) {
309
310   dissector_handle_t miop_handle;
311
312   miop_handle = find_dissector("miop");
313   dissector_add_handle("udp.port", miop_handle);    /* for 'Decode As' */
314
315   heur_dissector_add("udp", dissect_miop_heur, proto_miop);
316     
317 }