Add a inflateEnd() call to free up the stream in the re-init block.
[obnox/wireshark/wip.git] / epan / dissectors / packet-2dparityfec.c
1 /* packet-2dparityfec.c
2  * Mark Lewis <mlewis@altera.com>
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 /*
26 ** RTP Payload dissector for packets as specified in:
27 ** Pro-MPEG Code of Practice #3 release 2
28 **
29 ** This protocol defines a format for FEC data embedded within RTP packets with
30 ** a payload type of 96 (0x60). The format of the FEC packets, which reside within
31 ** the RTP payload, is as follows...
32 **
33 **   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 **   |         SNBase low bits       |        Length Recovery        |
35 **   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 **   |E| PT recovery |                    Mask                       |
37 **   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 **   |                           TS recovery                         |
39 **   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 **   |X|D|type |index|    Offset     |       NA      |SNBase ext bits|
41 **   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 **   |                                                               |
43 **   :                          FEC Payload                          :
44 **   |                                                               |
45 **   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 **
47 ** For more information on this protocol see...
48 ** http://www.pro-mpeg.org/publications/pdf/Vid-on-IP-CoP3-r2.pdf
49 **
50 **
51 ** Notes:
52 **
53 ** This protocol always resides in RTP packets with payload type 96. However,
54 ** type 96 is dynamic and may refer to other protocols. As Pro-MPEG FEC must
55 ** function in the absence of a control channel, and because this data is
56 ** likely to be transmitted within closed networks, no automatic mechanism
57 ** exists for specifying the existance of Pro-MPEG FEC on payload type 96.
58 ** This dissector is thus disabled by default. Dissection of this protocol
59 ** may be enabled from the 2dparityfec panel under Preferences->Protocols.
60 **
61 ** Mark Lewis - 20th June 2006
62 */
63
64 #ifdef HAVE_CONFIG_H
65 # include "config.h"
66 #endif
67
68 #include <epan/packet.h>
69 #include <epan/prefs.h>
70
71 /* forward reference */
72 void proto_register_2dparityfec(void);
73 void proto_reg_handoff_2dparityfec(void);
74
75 static gboolean dissect_fec = FALSE;
76
77 static int proto_2dparityfec = -1;
78 static int fec_rtp_payload_type = 96;
79 static gint ett_2dparityfec = -1;
80
81 static int hf_2dparityfec_snbase_low      = -1;
82 static int hf_2dparityfec_length_recovery = -1;
83 static int hf_2dparityfec_rfc2733_ext     = -1;
84 static int hf_2dparityfec_pt_recovery     = -1;
85 static int hf_2dparityfec_mask            = -1;
86 static int hf_2dparityfec_ts_recovery     = -1;
87 static int hf_2dparityfec_ts_pro_mpeg_ext = -1;
88 static int hf_2dparityfec_row_flag        = -1;
89 static int hf_2dparityfec_type            = -1;
90 static int hf_2dparityfec_index           = -1;
91 static int hf_2dparityfec_offset          = -1;
92 static int hf_2dparityfec_na              = -1;
93 static int hf_2dparityfec_snbase_ext      = -1;
94 static int hf_2dparityfec_payload         = -1;
95
96 static const value_string fec_type_names[] = {
97       {0, "XOR"},
98       {1, "Hamming"},
99       {2, "Reed-Solomon"}};
100
101 static void dissect_2dparityfec(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
102 {
103    guint8   OffsetField;
104    guint8   NAField;
105    guint32  SNBase;
106    guint8   D;
107
108    /* Extract SNBase */
109    SNBase  = (guint32)tvb_get_guint8(tvb, 0)<<8;
110    SNBase |= (guint32)tvb_get_guint8(tvb, 1);
111    SNBase |= (guint32)tvb_get_guint8(tvb, 15)<<16;
112
113    /* Extract D */
114    D = (tvb_get_guint8(tvb, 12)>>6) & 0x1;
115
116    /* Extract Offset and NA */
117    OffsetField    = tvb_get_guint8(tvb, 13);
118    NAField        = tvb_get_guint8(tvb, 14);
119
120    if(check_col(pinfo->cinfo, COL_PROTOCOL))
121    {
122       col_set_str(pinfo->cinfo, COL_PROTOCOL, "2dFEC");
123    }
124
125    /* Configure the info column */
126    if(check_col(pinfo->cinfo, COL_INFO))
127    {
128       col_clear(pinfo->cinfo, COL_INFO);
129
130       if(D)
131       {
132          col_add_fstr(pinfo->cinfo, COL_INFO, "Row FEC - SNBase=%u, Offset=%u, NA=%u",
133                                                 SNBase, OffsetField, NAField);
134       }
135       else
136       {
137          col_add_fstr(pinfo->cinfo, COL_INFO, "Column FEC - SNBase=%u, Offset=%u, NA=%u",
138                                                 SNBase, OffsetField, NAField);
139       }
140    }
141
142    if(tree)
143    {
144       /* we are being asked for details */
145       proto_item *ti = NULL;
146       proto_tree *tree_2dparityfec = NULL;
147       gint offset = 0;
148
149       ti = proto_tree_add_item(tree, proto_2dparityfec, tvb, 0, -1, FALSE);
150       tree_2dparityfec = proto_item_add_subtree(ti, ett_2dparityfec);
151
152       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_snbase_low,      tvb, offset, 2, FALSE); offset += 2;
153       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_length_recovery, tvb, offset, 2, FALSE); offset += 2;
154       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_rfc2733_ext,     tvb, offset, 1, FALSE);
155       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_pt_recovery,     tvb, offset, 1, FALSE); offset += 1;
156       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_mask,            tvb, offset, 3, FALSE); offset += 3;
157       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_ts_recovery,     tvb, offset, 4, FALSE); offset += 4;
158       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_ts_pro_mpeg_ext, tvb, offset, 1, FALSE);
159       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_row_flag,        tvb, offset, 1, FALSE);
160       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_type,            tvb, offset, 1, FALSE);
161       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_index,           tvb, offset, 1, FALSE); offset += 1;
162       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_offset,          tvb, offset, 1, FALSE); offset += 1;
163       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_na,              tvb, offset, 1, FALSE); offset += 1;
164       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_snbase_ext,      tvb, offset, 1, FALSE); offset += 1;
165       proto_tree_add_item(tree_2dparityfec, hf_2dparityfec_payload,         tvb, offset, -1, FALSE);
166    }
167 }
168
169 /* Payload type definitions */
170 static hf_register_info hf[] = {
171         {&hf_2dparityfec_snbase_low,
172                 {  "SNBase low",
173                         "2dparityfec.snbase_low",
174                         FT_UINT16,
175                         BASE_DEC,
176                         NULL,
177                         0x0,
178                         "",
179                         HFILL}   },
180
181         {&hf_2dparityfec_length_recovery,
182                 {  "Length recovery",
183                         "2dparityfec.lr",
184                         FT_UINT16,
185                         BASE_HEX,
186                         NULL,
187                         0x0,
188                         "",
189                         HFILL}   },
190
191         {&hf_2dparityfec_rfc2733_ext,
192                 {  "RFC2733 Extension (E)",
193                         "2dparityfec.e",
194                         FT_BOOLEAN,
195                         8,
196                         NULL,
197                         0x80,
198                         "",
199                         HFILL}   },
200
201         {&hf_2dparityfec_pt_recovery,
202                 {  "Payload Type recovery",
203                         "2dparityfec.ptr",
204                         FT_UINT8,
205                         BASE_HEX,
206                         NULL,
207                         0x7f,
208                         "",
209                         HFILL}   },
210
211         {&hf_2dparityfec_mask,
212                 {  "Mask",
213                         "2dparityfec.mask",
214                         /*FT_UINT32*/FT_UINT24,
215                         BASE_HEX,
216                         NULL,
217                         /*0x00ffffff*/0x0,
218                         "",
219                         HFILL}   },
220
221         {&hf_2dparityfec_ts_recovery,
222                 {  "Timestamp recovery",
223                         "2dparityfec.tsr",
224                         FT_UINT32,
225                         BASE_HEX,
226                         NULL,
227                         0x0,
228                         "",
229                         HFILL}   },
230
231         {&hf_2dparityfec_ts_pro_mpeg_ext,
232                 {  "Pro-MPEG Extension (X)",
233                         "2dparityfec.x",
234                         FT_BOOLEAN,
235                         8,
236                         NULL,
237                         0x80,
238                         "",
239                         HFILL}   },
240
241         {&hf_2dparityfec_row_flag,
242                 {  "Row FEC (D)",
243                         "2dparityfec.d",
244                         FT_BOOLEAN,
245                         8,
246                         NULL,
247                         0x40,
248                         "",
249                         HFILL}   },
250
251         {&hf_2dparityfec_type,
252                 {  "Type",
253                         "2dparityfec.type",
254                         FT_UINT8,
255                         BASE_DEC,
256                         VALS(fec_type_names),
257                         0x38,
258                         "",
259                         HFILL}   },
260
261         {&hf_2dparityfec_index,
262                 {  "Index",
263                         "2dparityfec.index",
264                         FT_UINT8,
265                         BASE_DEC,
266                         NULL,
267                         0x07,
268                         "",
269                         HFILL}   },
270
271         {&hf_2dparityfec_offset,
272                 {  "Offset",
273                         "2dparityfec.offset",
274                         FT_UINT8,
275                         BASE_DEC,
276                         NULL,
277                         0x0,
278                         "",
279                         HFILL}   },
280
281         {&hf_2dparityfec_na,
282                 {  "NA",
283                         "2dparityfec.na",
284                         FT_UINT8,
285                         BASE_DEC,
286                         NULL,
287                         0x0,
288                         "",
289                         HFILL}   },
290
291         {&hf_2dparityfec_snbase_ext,
292                 {  "SNBase ext",
293                         "2dparityfec.snbase_ext",
294                         FT_UINT8,
295                         BASE_DEC,
296                         NULL,
297                         0x0,
298                         "",
299                         HFILL}   },
300
301         {&hf_2dparityfec_payload,
302                 {  "FEC Payload",
303                         "2dparityfec.payload",
304                         FT_BYTES,
305                         BASE_HEX,
306                         NULL,
307                         0x0,
308                         "",
309                         HFILL}   }
310
311
312 };
313
314 /* Setup protocol subtree array */
315 static gint *ett[] = {
316         &ett_2dparityfec,
317 };
318
319 void proto_register_2dparityfec(void)
320 {
321    module_t *module_2dparityfec;
322
323    proto_2dparityfec = proto_register_protocol(
324         "Pro-MPEG Code of Practice #3 release 2 FEC Protocol",   /* name */
325         "2dparityfec",            /* short name */
326         "2dparityfec");           /* abbrev */
327
328    proto_register_field_array(proto_2dparityfec, hf, array_length(hf));
329    proto_register_subtree_array(ett, array_length(ett));
330
331    module_2dparityfec = prefs_register_protocol(proto_2dparityfec,
332                                                 proto_reg_handoff_2dparityfec);
333
334    prefs_register_bool_preference(module_2dparityfec, "enable",
335       "Decode Pro-MPEG FEC on RTP dynamic payload type 96",
336       "Enable this option to recognise all traffic on RTP dynamic payload type 96 (0x60) "
337       "as FEC data corresponding to Pro-MPEG Code of Practice #3 release 2",
338       &dissect_fec);
339
340 }
341
342 void proto_reg_handoff_2dparityfec(void)
343 {
344       static dissector_handle_t handle_2dparityfec = NULL;
345  
346       if (!handle_2dparityfec) {
347         handle_2dparityfec = create_dissector_handle(dissect_2dparityfec,
348                                                         proto_2dparityfec);
349       }
350
351       if (dissect_fec) {
352         dissector_add("rtp.pt", fec_rtp_payload_type, handle_2dparityfec);
353       } else {
354         dissector_delete("rtp.pt", fec_rtp_payload_type, handle_2dparityfec);
355       }
356 }