Remove dead code. Fixes Coverity CID 228. Squelch various compiler warnings.
[obnox/wireshark/wip.git] / epan / dissectors / packet-dcp-etsi.c
1 /* packet-dcp-etsi.c
2  * Routines for ETSI Distribution & Communication Protocol
3  * Copyright 2006, British Broadcasting Corporation
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  *
25  * Protocol info
26  * Ref: ETSI DCP (ETSI TS 102 821)
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <gmodule.h>
34 #include <epan/packet.h>
35 #include <epan/prefs.h>
36 #include <epan/reassemble.h>
37 #include <epan/crcdrm.h>
38 #include <epan/reedsolomon.h>
39 #include <string.h>
40
41 /* forward reference */
42
43 static gboolean dissect_dcp_etsi (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
44 static void dissect_af (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
45 static void dissect_pft (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
46 static void dissect_tpl(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree);
47
48 static dissector_table_t dcp_dissector_table;
49 static dissector_table_t af_dissector_table;
50 static dissector_table_t tpl_dissector_table;
51
52 static int proto_dcp_etsi = -1;
53 static int proto_af = -1;
54 static int proto_pft = -1;
55 static int proto_tpl = -1;
56 static dissector_handle_t af_handle;
57 static dissector_handle_t pft_handle;
58 static dissector_handle_t tpl_handle;
59 static int hf_edcp_sync = -1;
60 static int hf_edcp_len = -1;
61 static int hf_edcp_seq = -1;
62 static int hf_edcp_crcflag = -1;
63 static int hf_edcp_maj = -1;
64 static int hf_edcp_min = -1;
65 static int hf_edcp_pt = -1;
66 static int hf_edcp_crc = -1;
67 static int hf_edcp_crc_ok = -1;
68 static int hf_edcp_pft_pt = -1;
69 static int hf_edcp_pseq = -1;
70 static int hf_edcp_findex = -1;
71 static int hf_edcp_fcount = -1;
72 static int hf_edcp_fecflag = -1;
73 static int hf_edcp_addrflag = -1;
74 static int hf_edcp_plen = -1;
75 static int hf_edcp_rsk = -1;
76 static int hf_edcp_rsz = -1;
77 static int hf_edcp_source = -1;
78 static int hf_edcp_dest = -1;
79 static int hf_edcp_hcrc = -1;
80 static int hf_edcp_hcrc_ok = -1;
81 static int hf_edcp_c_max = -1;
82 static int hf_edcp_rx_min = -1;
83 static int hf_edcp_rs_corrected = -1;
84 static int hf_edcp_rs_ok = -1;
85 static int hf_edcp_pft_payload = -1;
86
87 static int hf_tpl_tlv = -1;
88 static int hf_tpl_ptr = -1;
89
90 static int hf_edcp_fragments = -1;
91 static int hf_edcp_fragment = -1;
92 static int hf_edcp_fragment_overlap = -1;
93 static int hf_edcp_fragment_overlap_conflicts = -1;
94 static int hf_edcp_fragment_multiple_tails = -1;
95 static int hf_edcp_fragment_too_long_fragment = -1;
96 static int hf_edcp_fragment_error = -1;
97 static int hf_edcp_reassembled_in = -1;
98
99 /* Initialize the subtree pointers */
100 static gint ett_edcp = -1;
101 static gint ett_af = -1;
102 static gint ett_pft = -1;
103 static gint ett_tpl = -1;
104 static gint ett_edcp_fragment = -1;
105 static gint ett_edcp_fragments = -1;
106
107 static GHashTable *dcp_fragment_table = NULL;
108 static GHashTable *dcp_reassembled_table = NULL;
109
110 static const fragment_items dcp_frag_items = {
111 /* Fragment subtrees */
112   &ett_edcp_fragment,
113   &ett_edcp_fragments,
114 /* Fragment fields */
115   &hf_edcp_fragments,
116   &hf_edcp_fragment,
117   &hf_edcp_fragment_overlap,
118   &hf_edcp_fragment_overlap_conflicts,
119   &hf_edcp_fragment_multiple_tails,
120   &hf_edcp_fragment_too_long_fragment,
121   &hf_edcp_fragment_error,
122 /* Reassembled in field */
123   &hf_edcp_reassembled_in,
124 /* Tag */
125   "Message fragments"
126 };
127
128 /** initialise the DCP protocol. Details follow
129  *  here.
130  */
131 static void
132 dcp_init_protocol(void)
133 {
134   fragment_table_init (&dcp_fragment_table);
135   reassembled_table_init (&dcp_reassembled_table);
136 }
137
138
139 /** Dissect a DCP packet. Details follow
140  *  here.
141  *  \param[in,out] tvb The buffer containing the packet
142  *  \param[in,out] pinfo The packet info structure
143  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
144 static void
145  */
146 static gboolean
147 dissect_dcp_etsi (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
148 {
149   guint8 *sync;
150   proto_tree *dcp_tree = NULL;
151   sync = tvb_get_string (tvb, 0, 2);
152   if((sync[0]!='A' && sync[0]!='P') || sync[1]!='F')
153     return FALSE;
154
155   pinfo->current_proto = "DCP (ETSI)";
156
157   /* Clear out stuff in the info column */
158   if (check_col (pinfo->cinfo, COL_INFO)) {
159     col_clear (pinfo->cinfo, COL_INFO);
160   }
161   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
162     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP (ETSI)");
163     /*col_append_fstr (pinfo->cinfo, COL_INFO, " tvb %d", tvb_length(tvb));*/
164   }
165
166   if(tree) {
167     proto_item *ti = NULL;
168     ti = proto_tree_add_item (tree, proto_dcp_etsi, tvb, 0, -1, FALSE);
169     dcp_tree = proto_item_add_subtree (ti, ett_edcp);
170   }
171
172   dissector_try_string(dcp_dissector_table, (char*)sync, tvb, pinfo, dcp_tree);
173   g_free (sync);
174   return TRUE;
175 }
176
177 #define PFT_RS_N_MAX 207
178 #define PFT_RS_K 255
179 #define PFT_RS_P (PFT_RS_K - PFT_RS_N_MAX)
180
181
182 static
183 void rs_deinterleave(const guint8 *input, guint8 *output, guint16 plen, guint32 fcount)
184 {
185   guint fidx;
186   for(fidx=0; fidx<fcount; fidx++)
187   {
188     int r;
189     for (r=0; r<plen; r++)
190     {
191       output[fidx+r*fcount] = input[fidx*plen+r];
192     }
193   }
194 }
195
196 static
197 gboolean rs_correct_data(guint8 *deinterleaved, guint8 *output,
198  guint32 c_max, guint16 rsk, guint16 rsz _U_)
199 {
200   guint32 i, index_coded = 0, index_out = 0;
201   int err_corr;
202   for (i=0; i<c_max; i++)
203   {
204     memcpy(output+index_out, deinterleaved+index_coded, rsk);
205     index_coded += rsk;
206     memcpy(output+index_out+PFT_RS_N_MAX, deinterleaved+index_coded, PFT_RS_P);
207     index_coded += PFT_RS_P;
208     err_corr = eras_dec_rs(output+index_out, NULL, 0);
209     if (err_corr<0) {
210       return FALSE;
211     }
212     index_out += rsk;
213   }
214   return TRUE;
215 }
216
217
218
219 static tvbuff_t *
220 dissect_pft_fec_detailed(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree,
221   guint32 findex,
222   guint32 fcount,
223   guint16 seq,
224   gint offset,
225   guint16 plen,
226   gboolean fec _U_,
227   guint16 rsk,
228   guint16 rsz,
229   fragment_data *fd
230 )
231 {
232   guint16 decoded_size;
233   guint32 c_max;
234   guint32 rx_min;
235   gboolean first, last, decoded = TRUE;
236   tvbuff_t *new_tvb=NULL;
237
238   first = findex == 0;
239   last = fcount == (findex+1);
240   decoded_size = fcount*plen;
241   c_max = fcount*plen/(rsk+PFT_RS_P);  /* rounded down */
242   rx_min = c_max*rsk/plen;
243   if(rx_min*plen<c_max*rsk)
244     rx_min++;
245   if (fd)
246     new_tvb = process_reassembled_data (tvb, offset, pinfo,
247                                             "Reassembled Message",
248                                             fd, &dcp_frag_items,
249                                             NULL, tree);
250   else {
251     guint fragments=0;
252     guint32 *got = g_malloc(fcount*sizeof(guint32));
253
254     fragment_data *fd = fragment_get(pinfo, seq, dcp_fragment_table);
255     fragment_data *fd_head;
256         for (fd_head = fd; fd_head != NULL; fd_head = fd_head->next) {
257       if(fd_head->data) {
258         got[fragments] = fd_head->offset;
259         fragments++;
260       }
261         }
262         if(fragments>=rx_min) {
263       guint i,j;
264       fragment_data *frag=NULL;
265       guint8 *dummy_data = (guint8*) g_malloc (plen);
266       tvbuff_t *dummytvb = tvb_new_real_data(dummy_data, plen, plen);
267       /* try and decode with missing fragments */
268       if(tree)
269           proto_tree_add_text (tree, tvb, 0, -1, "want %d, got %d need %d",
270           fcount, fragments, rx_min
271           );
272       memset(dummy_data, 0, plen);
273       for(i=0,j=0; i<fragments; i++,j++) {
274         while(j<got[i]) {
275           frag = fragment_add_seq_check (dummytvb, 0, pinfo, seq,
276             dcp_fragment_table, dcp_reassembled_table, j, plen, (j+1!=fcount));
277           if(tree) {
278               proto_tree_add_text (tree, tvb, 0, -1, "missing %d", j);
279               if(frag) {
280                 proto_tree_add_text (tree, tvb, 0, -1, "fragment %d was what we needed", j);
281                 break;
282               } else {
283                 proto_tree_add_text (tree, tvb, 0, -1, "added %d but still not reassembled", j);
284               }
285           }
286           j++;
287         }
288       }
289       if(frag)
290         new_tvb = process_reassembled_data (tvb, offset, pinfo,
291                                             "Reassembled Message",
292                                             frag, &dcp_frag_items,
293                                             NULL, tree);
294     }
295     g_free(got);
296   }
297   if(new_tvb) {
298     tvbuff_t *dtvb = NULL;
299     const guint8 *input = tvb_get_ptr(new_tvb, 0, -1);
300     guint16 reassembled_size = tvb_length(new_tvb);
301     guint8 *deinterleaved = (guint8*) g_malloc (reassembled_size);
302     guint8 *output = (guint8*) g_malloc (decoded_size);
303     rs_deinterleave(input, deinterleaved, plen, fcount);
304
305     dtvb = tvb_new_real_data(deinterleaved, reassembled_size, reassembled_size);
306     tvb_set_child_real_data_tvbuff(tvb, dtvb);
307     add_new_data_source(pinfo, dtvb, "Deinterleaved");
308     tvb_set_free_cb(dtvb, g_free);
309
310     decoded = rs_correct_data(deinterleaved, output, c_max, rsk, rsz);
311     if(tree)
312         proto_tree_add_boolean (tree, hf_edcp_rs_ok, tvb, offset, 2, decoded);
313
314     new_tvb = tvb_new_real_data(output, decoded_size, decoded_size);
315     tvb_set_child_real_data_tvbuff(dtvb, new_tvb);
316     add_new_data_source(pinfo, new_tvb, "RS Error Corrected Data");
317     tvb_set_free_cb(new_tvb, g_free);
318   }
319   return new_tvb;
320 }
321
322
323 /** Handle a PFT packet which has the fragmentation header. This uses the
324  * standard ethereal methods for reassembling fragments. If FEC is used,
325  * the FEC is handled too. For the moment, all the fragments must be
326  * available but this could be improved.
327  *  \param[in,out] tvb The buffer containing the current fragment
328  *  \param[in,out] pinfo The packet info structure
329  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
330  *  \param[in] findex the fragment count
331  *  \param[in] fcount the number of fragments
332  *  \param[in] seq the sequence number of the reassembled packet
333  *  \param[in] offset the offset into the tvb of the fragment
334  *  \param[in] plen the length of each fragment
335  *  \param[in] fec is fec used
336  *  \param[in] rsk the number of useful bytes in each chunk
337  *  \param[in] rsz the number of padding bytes in each chunk
338  */
339 static tvbuff_t *
340 dissect_pft_fragmented(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree,
341   guint32 findex,
342   guint32 fcount,
343   guint16 seq,
344   gint offset,
345   guint16 plen,
346   gboolean fec,
347   guint16 rsk,
348   guint16 rsz
349 )
350 {
351   gboolean first, last;
352   tvbuff_t *new_tvb=NULL;
353   fragment_data *frag_edcp = NULL;
354   pinfo->fragmented = TRUE;
355   first = findex == 0;
356   last = fcount == (findex+1);
357   frag_edcp = fragment_add_seq_check (
358      tvb, offset, pinfo,
359      seq,
360          dcp_fragment_table, dcp_reassembled_table,
361          findex,
362      plen,
363          !last);
364   if(fec) {
365     new_tvb = dissect_pft_fec_detailed(
366           tvb, pinfo, tree, findex, fcount, seq, offset, plen, fec, rsk, rsz, frag_edcp
367         );
368   } else {
369     new_tvb = process_reassembled_data (tvb, offset, pinfo,
370                                             "Reassembled Message",
371                                             frag_edcp, &dcp_frag_items,
372                                             NULL, tree);
373   }
374   if (check_col (pinfo->cinfo, COL_INFO)) {
375     if(new_tvb) {
376       col_append_str (pinfo->cinfo, COL_INFO, " (Message Reassembled)");
377     } else {
378       if(last) {
379         col_append_str (pinfo->cinfo, COL_INFO, " (Message Reassembly failure)");
380       } else {
381         col_append_fstr (pinfo->cinfo, COL_INFO, " (Message fragment %u)", findex);
382       }
383     }
384     if(first)
385       col_append_str (pinfo->cinfo, COL_INFO, " (first)");
386     if(last)
387       col_append_str (pinfo->cinfo, COL_INFO, " (last)");
388   }
389   return new_tvb;
390 }
391
392 /** Dissect a PFT packet. Details follow
393  *  here.
394  *  \param[in,out] tvb The buffer containing the packet
395  *  \param[in,out] pinfo The packet info structure
396  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
397  */
398 static void
399 dissect_pft(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
400 {
401   guint16 plen;
402   gint offset = 0;
403   guint16 seq, payload_len, hcrc;
404   guint32 findex, fcount;
405   proto_tree *pft_tree = NULL;
406   proto_item *ti = NULL, *li = NULL;
407   tvbuff_t *next_tvb = NULL;
408   gboolean fec = FALSE;
409   guint16 rsk=0, rsz=0;
410
411   pinfo->current_proto = "DCP-PFT";
412   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
413     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP-PFT");
414   }
415
416   if (tree) {                   /* we are being asked for details */
417     ti = proto_tree_add_item (tree, proto_pft, tvb, 0, -1, FALSE);
418     pft_tree = proto_item_add_subtree (ti, ett_pft);
419     proto_tree_add_item (pft_tree, hf_edcp_sync, tvb, offset, 2, FALSE);
420   }
421   offset += 2;
422   seq = tvb_get_ntohs (tvb, offset);
423   if (tree) {
424     proto_tree_add_item (pft_tree, hf_edcp_pseq, tvb, offset, 2, FALSE);
425   }
426   offset += 2;
427   findex = tvb_get_ntoh24 (tvb, offset);
428   if (tree) {
429     proto_tree_add_item (pft_tree, hf_edcp_findex, tvb, offset, 3, FALSE);
430   }
431   offset += 3;
432   fcount = tvb_get_ntoh24 (tvb, offset);
433   if (tree) {
434     proto_tree_add_item (pft_tree, hf_edcp_fcount, tvb, offset, 3, FALSE);
435   }
436   offset += 3;
437   plen = tvb_get_ntohs (tvb, offset);
438   payload_len = plen & 0x3fff;
439   if (tree) {
440     proto_tree_add_item (pft_tree, hf_edcp_fecflag, tvb, offset, 2, FALSE);
441     proto_tree_add_item (pft_tree, hf_edcp_addrflag, tvb, offset, 2, FALSE);
442     li = proto_tree_add_item (pft_tree, hf_edcp_plen, tvb, offset, 2, FALSE);
443   }
444   offset += 2;
445   if (plen & 0x8000) {
446     fec = TRUE;
447     rsk = tvb_get_guint8 (tvb, offset);
448     if (tree)
449           proto_tree_add_item (pft_tree, hf_edcp_rsk, tvb, offset, 1, FALSE);
450     offset += 1;
451     rsz = tvb_get_guint8 (tvb, offset);
452     if (tree)
453           proto_tree_add_item (pft_tree, hf_edcp_rsz, tvb, offset, 1, FALSE);
454     offset += 1;
455   }
456   if (plen & 0x4000) {
457     if (tree)
458       proto_tree_add_item (pft_tree, hf_edcp_source, tvb, offset, 2, FALSE);
459     offset += 2;
460     if (tree)
461           proto_tree_add_item (pft_tree, hf_edcp_dest, tvb, offset, 2, FALSE);
462     offset += 2;
463   }
464   if (tree) {
465     proto_item *ci = NULL;
466     guint header_len = offset+2;
467     const char *crc_buf = (const char *) tvb_get_ptr(tvb, 0, header_len);
468     unsigned long c = crc_drm(crc_buf, header_len, 16, 0x11021, 1);
469     ci = proto_tree_add_item (pft_tree, hf_edcp_hcrc, tvb, offset, 2, FALSE);
470     proto_item_append_text(ci, " (%s)", (c==0xe2f0)?"Ok":"bad");
471     proto_tree_add_boolean(pft_tree, hf_edcp_hcrc_ok, tvb, offset, 2, c==0xe2f0);
472   }
473   hcrc = tvb_get_ntohs (tvb, offset);
474   offset += 2;
475   if (fcount > 1) {             /* fragmented*/
476     gboolean save_fragmented = pinfo->fragmented;
477     guint16 real_len = tvb_length(tvb)-offset;
478     proto_tree_add_item (pft_tree, hf_edcp_pft_payload, tvb, offset, real_len, FALSE);
479     if(real_len != payload_len) {
480       if(li)
481         proto_item_append_text(li, " (length error (%d))", real_len);
482     }
483     next_tvb = dissect_pft_fragmented(tvb, pinfo, pft_tree,
484                                       findex, fcount, seq, offset, real_len,
485                                       fec, rsk, rsz
486                                       );
487     pinfo->fragmented = save_fragmented;
488   } else {
489     next_tvb = tvb_new_subset (tvb, offset, -1, -1);
490   }
491   if(next_tvb) {
492     dissect_af(next_tvb, pinfo, tree);
493   }
494 }
495
496 /** Dissect an AF Packet. Parse an AF packet, checking the CRC if the CRC valid
497  * flag is set and calling any registered sub dissectors on the payload type.
498  * Currently only a payload type 'T' is defined which is the tag packet layer.
499  * If any others are defined then they can register themselves.
500  *  \param[in,out] tvb The buffer containing the packet
501  *  \param[in,out] pinfo The packet info structure
502  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
503  */
504 static void
505 dissect_af (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
506 {
507   gint offset = 0;
508   proto_item *ti = NULL;
509   proto_item *li = NULL;
510   proto_item *ci = NULL;
511   proto_tree *af_tree = NULL;
512   guint8 ver, pt;
513   guint32 payload_len;
514   tvbuff_t *next_tvb = NULL;
515
516   pinfo->current_proto = "DCP-AF";
517   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
518     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP-AF");
519   }
520
521   if (tree) {                   /* we are being asked for details */
522     ti = proto_tree_add_item (tree, proto_af, tvb, 0, -1, FALSE);
523     af_tree = proto_item_add_subtree (ti, ett_af);
524     proto_tree_add_item (af_tree, hf_edcp_sync, tvb, offset, 2, FALSE);
525   }
526   offset += 2;
527   payload_len = tvb_get_ntohl(tvb, offset);
528   if (tree) {
529     guint32 real_payload_len = tvb_length(tvb)-12;
530     li = proto_tree_add_item (af_tree, hf_edcp_len, tvb, offset, 4, FALSE);
531     if(real_payload_len < payload_len) {
532       proto_item_append_text (li, " (wrong len claims %d is %d)",
533       payload_len, real_payload_len
534       );
535     } else if(real_payload_len > payload_len) {
536       proto_item_append_text (li, " (%d bytes in packet after end of AF frame)",
537       real_payload_len-payload_len
538       );
539     }
540   }
541   offset += 4;
542   if (tree)
543     proto_tree_add_item (af_tree, hf_edcp_seq, tvb, offset, 2, FALSE);
544   offset += 2;
545   ver = tvb_get_guint8 (tvb, offset);
546   if (tree) {
547     proto_tree_add_item (af_tree, hf_edcp_crcflag, tvb, offset, 1, FALSE);
548     proto_tree_add_item (af_tree, hf_edcp_maj, tvb, offset, 1, FALSE);
549     proto_tree_add_item (af_tree, hf_edcp_min, tvb, offset, 1, FALSE);
550   }
551   offset += 1;
552   pt = tvb_get_guint8 (tvb, offset);
553   if (tree)
554     proto_tree_add_item (af_tree, hf_edcp_pt, tvb, offset, 1, FALSE);
555   offset += 1;
556   next_tvb = tvb_new_subset (tvb, offset, payload_len, -1);
557   offset += payload_len;
558   if (tree)
559     ci = proto_tree_add_item (af_tree, hf_edcp_crc, tvb, offset, 2, FALSE);
560   if (ver & 0x80) { /* crc valid */
561     guint len = offset+2;
562     const char *crc_buf = (const char *) tvb_get_ptr(tvb, 0, len);
563     unsigned long c = crc_drm(crc_buf, len, 16, 0x11021, 1);
564     if (tree) {
565           proto_item_append_text(ci, " (%s)", (c==0xe2f0)?"Ok":"bad");
566       proto_tree_add_boolean(af_tree, hf_edcp_crc_ok, tvb, offset, 2, c==0xe2f0);
567     }
568   }
569   offset += 2;
570   dissector_try_port(af_dissector_table, pt, next_tvb, pinfo, tree);
571 }
572
573 /** Dissect the Tag Packet Layer.
574  *  Split the AF packet into its tag items. Each tag item has a 4 character
575  *  tag, a length in bits and a value. The *ptr tag is dissected in the routine.
576  *  All other tags are listed and may be handled by other dissectors.
577  *  Child dissectors are tied to the parent tree, not to this tree, so that
578  *  they appear at the same level as DCP.
579  *  \param[in,out] tvb The buffer containing the packet
580  *  \param[in,out] pinfo The packet info structure
581  *  \param[in,out] tree The structure containing the details which will be displayed, filtered, etc.
582  */
583 static void
584 dissect_tpl(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
585 {
586   proto_tree *tpl_tree = NULL;
587   guint offset=0;
588   char *prot=NULL;
589   guint16 maj, min;
590
591   pinfo->current_proto = "DCP-TPL";
592   if (check_col (pinfo->cinfo, COL_PROTOCOL)) {
593     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DCP-TPL");
594   }
595
596   if(tree) {
597     proto_item *ti = NULL;
598     ti = proto_tree_add_item (tree, proto_tpl, tvb, 0, -1, FALSE);
599     tpl_tree = proto_item_add_subtree (ti, ett_tpl);
600   }
601   while(offset<tvb_length(tvb)) {
602     guint32 bits;
603     guint32 bytes;
604     char *tag = (char*)tvb_get_string (tvb, offset, 4); offset += 4;
605     bits = tvb_get_ntohl(tvb, offset); offset += 4;
606     bytes = bits / 8;
607     if(bits % 8)
608       bytes++;
609     if(tree) {
610       proto_item *i = NULL;
611       const guint8 *p = tvb_get_ptr(tvb, offset, bytes);
612       if(strcmp(tag, "*ptr")==0) {
613         prot = (char*)tvb_get_string (tvb, offset, 4);
614         maj = tvb_get_ntohs(tvb, offset+4);
615         min = tvb_get_ntohs(tvb, offset+6);
616         i = proto_tree_add_bytes_format(tpl_tree, hf_tpl_tlv, tvb,
617               offset-8, bytes+8, p, "%s %s rev %d.%d", tag, prot, maj, min);
618       } else {
619         i = proto_tree_add_bytes_format(tpl_tree, hf_tpl_tlv, tvb,
620               offset-8, bytes+8, p, "%s (%u bits)", tag, bits);
621       }
622     }
623     offset += bytes;
624   }
625   if(prot) {  /* prot is non-NULL only if we have our tree. */
626     dissector_try_string(tpl_dissector_table, prot, tvb, pinfo, tree->parent);
627   }
628 }
629
630 void
631 proto_reg_handoff_dcp_etsi (void)
632 {
633   static int Initialized = FALSE;
634
635   if (!Initialized) {
636         af_handle = create_dissector_handle(dissect_af, proto_af);
637         pft_handle = create_dissector_handle(dissect_pft, proto_pft);
638         tpl_handle = create_dissector_handle(dissect_tpl, proto_tpl);
639         heur_dissector_add("udp", dissect_dcp_etsi, proto_dcp_etsi);
640      dissector_add_string("dcp-etsi.sync", "AF", af_handle);
641     dissector_add_string("dcp-etsi.sync", "PF", pft_handle);
642     /* if there are ever other payload types ...*/
643     dissector_add("dcp-af.pt", 'T', tpl_handle);
644   }
645 }
646
647 void
648 proto_register_dcp_etsi (void)
649 {
650   module_t *dcp_module;
651   static hf_register_info hf_edcp[] = {
652     {&hf_edcp_sync,
653      {"sync", "dcp-etsi.sync",
654       FT_STRING, BASE_NONE, NULL, 0,
655       "AF or PF", HFILL}
656      }
657     };
658   static hf_register_info hf_af[] = {
659     {&hf_edcp_len,
660      {"length", "dcp-af.len",
661       FT_UINT32, BASE_DEC, NULL, 0,
662       "length in bytes of the payload", HFILL}
663      },
664     {&hf_edcp_seq,
665      {"frame count", "dcp-af.seq",
666       FT_UINT16, BASE_DEC, NULL, 0,
667       "Logical Frame Number", HFILL}
668      },
669     {&hf_edcp_crcflag,
670      {"crc flag", "dcp-af.crcflag",
671       FT_BOOLEAN, BASE_NONE, NULL, 0x80,
672       "Frame is protected by CRC", HFILL}
673      },
674     {&hf_edcp_maj,
675      {"Major Revision", "dcp-af.maj",
676       FT_UINT8, BASE_DEC, NULL, 0x70,
677       "Major Protocol Revision", HFILL}
678      },
679     {&hf_edcp_min,
680      {"Minor Revision", "dcp-af.min",
681       FT_UINT8, BASE_DEC, NULL, 0x0f,
682       "Minor Protocol Revision", HFILL}
683      },
684     {&hf_edcp_pt,
685      {"Payload Type", "dcp-af.pt",
686       FT_STRING, BASE_NONE, NULL, 0,
687       "T means Tag Packets, all other values reserved", HFILL}
688      },
689     {&hf_edcp_crc,
690      {"CRC", "dcp-af.crc",
691       FT_UINT16, BASE_HEX, NULL, 0,
692       "CRC", HFILL}
693      },
694     {&hf_edcp_crc_ok,
695      {"CRC OK", "dcp-af.crc_ok",
696       FT_BOOLEAN, BASE_NONE, NULL, 0,
697       "AF CRC OK", HFILL}
698      }
699     };
700
701   static hf_register_info hf_pft[] = {
702     {&hf_edcp_pft_pt,
703      {"Sub-protocol", "dcp-pft.pt",
704       FT_UINT8, BASE_DEC, NULL, 0,
705       "Always AF", HFILL}
706      },
707     {&hf_edcp_pseq,
708      {"Sequence No", "dcp-pft.seq",
709       FT_UINT16, BASE_DEC, NULL, 0,
710       "PFT Sequence No", HFILL}
711      },
712     {&hf_edcp_findex,
713      {"Fragment Index", "dcp-pft.findex",
714       FT_UINT24, BASE_DEC, NULL, 0,
715       "Index of the fragment within one AF Packet", HFILL}
716      },
717     {&hf_edcp_fcount,
718      {"Fragment Count", "dcp-pft.fcount",
719       FT_UINT24, BASE_DEC, NULL, 0,
720       "Number of fragments produced from this AF Packet", HFILL}
721      },
722     {&hf_edcp_fecflag,
723      {"FEC", "dcp-pft.fec",
724       FT_BOOLEAN, BASE_NONE, NULL, 0x8000,
725       "When set the optional RS header is present", HFILL}
726      },
727     {&hf_edcp_addrflag,
728      {"Addr", "dcp-pft.addr",
729       FT_BOOLEAN, BASE_NONE, NULL, 0x4000,
730       "When set the optional transport header is present", HFILL}
731      },
732     {&hf_edcp_plen,
733      {"fragment length", "dcp-pft.len",
734       FT_UINT16, BASE_DEC, NULL, 0x3fff,
735       "length in bytes of the payload of this fragment", HFILL}
736      },
737     {&hf_edcp_rsk,
738      {"RSk", "dcp-pft.rsk",
739       FT_UINT8, BASE_DEC, NULL, 0,
740       "The length of the Reed Solomon data word", HFILL}
741      },
742     {&hf_edcp_rsz,
743      {"RSz", "dcp-pft.rsz",
744       FT_UINT8, BASE_DEC, NULL, 0,
745       "The number of padding bytes in the last Reed Solomon block", HFILL}
746      },
747     {&hf_edcp_source,
748      {"source addr", "dcp-pft.source",
749       FT_UINT16, BASE_DEC, NULL, 0,
750       "PFT source identifier", HFILL}
751      },
752     {&hf_edcp_dest,
753      {"dest addr", "dcp-pft.dest",
754       FT_UINT16, BASE_DEC, NULL, 0,
755       "PFT destination identifier", HFILL}
756      },
757     {&hf_edcp_hcrc,
758      {"header CRC", "dcp-pft.crc",
759       FT_UINT16, BASE_HEX, NULL, 0,
760       "PFT Header CRC", HFILL}
761      },
762     {&hf_edcp_hcrc_ok,
763      {"PFT CRC OK", "dcp-pft.crc_ok",
764       FT_BOOLEAN, BASE_NONE, NULL, 0,
765       "PFT Header CRC OK", HFILL}
766      },
767     {&hf_edcp_fragments,
768      {"Message fragments", "dcp-pft.fragments",
769       FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL}},
770     {&hf_edcp_fragment,
771      {"Message fragment", "dcp-pft.fragment",
772       FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL}},
773     {&hf_edcp_fragment_overlap,
774      {"Message fragment overlap", "dcp-pft.fragment.overlap",
775       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
776     {&hf_edcp_fragment_overlap_conflicts,
777      {"Message fragment overlapping with conflicting data",
778       "dcp-pft.fragment.overlap.conflicts",
779       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
780     {&hf_edcp_fragment_multiple_tails,
781      {"Message has multiple tail fragments",
782       "dcp-pft.fragment.multiple_tails",
783       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
784     {&hf_edcp_fragment_too_long_fragment,
785      {"Message fragment too long", "dcp-pft.fragment.too_long_fragment",
786       FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL}},
787     {&hf_edcp_fragment_error,
788      {"Message defragmentation error", "dcp-pft.fragment.error",
789       FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL}},
790     {&hf_edcp_reassembled_in,
791      {"Reassembled in", "dcp-pft.reassembled.in",
792       FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL}},
793     {&hf_edcp_c_max,
794      {"C max", "dcp-pft.cmax",
795       FT_UINT16, BASE_DEC, NULL, 0,
796       "Maximum number of RS chunks sent", HFILL}
797      },
798     {&hf_edcp_rx_min,
799      {"Rx min", "dcp-pft.rxmin",
800       FT_UINT16, BASE_DEC, NULL, 0,
801       "Minimum number of fragments needed for RS decode", HFILL}
802      },
803     {&hf_edcp_rs_corrected,
804      {"RS Symbols Corrected", "dcp-pft.rs_corrected",
805       FT_INT16, BASE_DEC, NULL, 0,
806       "Number of symbols corrected by RS decode or -1 for failure", HFILL}
807      },
808     {&hf_edcp_rs_ok,
809      {"RS decode OK", "dcp-pft.rs_ok",
810       FT_BOOLEAN, BASE_NONE, NULL, 0,
811       "successfully decoded RS blocks", HFILL}
812      },
813     {&hf_edcp_pft_payload,
814      {"payload", "dcp-pft.payload",
815       FT_BYTES, BASE_HEX, NULL, 0,
816       "PFT Payload", HFILL}
817     }
818   };
819
820   static hf_register_info hf_tpl[] = {
821     {&hf_tpl_tlv,
822      {"tag", "dcp-tpl.tlv",
823       FT_BYTES, BASE_HEX, NULL, 0,
824       "Tag Packet", HFILL}
825      },
826     {&hf_tpl_ptr,
827      {"Type", "dcp-tpl.ptr",
828       FT_STRING, BASE_NONE, NULL, 0,
829       "Protocol Type & Revision", HFILL}
830      }
831     };
832
833 /* Setup protocol subtree array */
834   static gint *ett[] = {
835     &ett_edcp,
836     &ett_af,
837     &ett_pft,
838     &ett_tpl,
839     &ett_edcp_fragment,
840     &ett_edcp_fragments
841   };
842
843   if (proto_dcp_etsi == -1) {
844     proto_dcp_etsi = proto_register_protocol ("ETSI Distribution & Communication Protocol (for DRM)",   /* name */
845                                          "DCP (ETSI)",  /* short name */
846                                          "dcp-etsi"     /* abbrev */
847       );
848     proto_af = proto_register_protocol ("DCP Application Framing Layer", "DCP-AF", "dcp-af");
849     proto_pft = proto_register_protocol ("DCP Protection, Fragmentation & Transport Layer", "DCP-PFT", "dcp-pft");
850     proto_tpl = proto_register_protocol ("DCP Tag Packet Layer", "DCP-TPL", "dcp-tpl");
851
852
853   }
854   dcp_module = prefs_register_protocol (proto_dcp_etsi, proto_reg_handoff_dcp_etsi);
855   proto_register_field_array (proto_dcp_etsi, hf_edcp, array_length (hf_edcp));
856   proto_register_field_array (proto_af, hf_af, array_length (hf_af));
857   proto_register_field_array (proto_pft, hf_pft, array_length (hf_pft));
858   proto_register_field_array (proto_tpl, hf_tpl, array_length (hf_tpl));
859   proto_register_subtree_array (ett, array_length (ett));
860
861   /* subdissector code */
862   dcp_dissector_table = register_dissector_table("dcp-etsi.sync",
863             "DCP Sync", FT_STRING, BASE_NONE);
864   af_dissector_table = register_dissector_table("dcp-af.pt",
865             "AF Payload Type", FT_UINT8, BASE_DEC);
866
867   tpl_dissector_table = register_dissector_table("dcp-tpl.ptr",
868             "AF Payload Type", FT_STRING, BASE_NONE);
869
870   register_init_routine(dcp_init_protocol);
871
872 }
873
874