Fix some of the Errors/warnings detected by checkapi.
[metze/wireshark/wip.git] / epan / dissectors / packet-brdwlk.c
1 /* packet-brdwlk.c
2  * Routines for decoding MDS Port Analyzer Adapter (FC in Eth) Header
3  * Copyright 2001, Dinesh G Dutt <ddutt@andiamo.com>
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
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <glib.h>
43
44 #include <epan/packet.h>
45 #include <epan/etypes.h>
46
47 #define BRDWLK_MAX_PACKET_CNT  0xFFFF
48 #define BRDWLK_TRUNCATED_BIT   0x8
49 #define BRDWLK_HAS_PLEN        0x1
50
51 #define FCM_DELIM_SOFC1         0x01
52 #define FCM_DELIM_SOFI1         0x02
53 #define FCM_DELIM_SOFI2         0x04
54 #define FCM_DELIM_SOFI3         0x06
55 #define FCM_DELIM_SOFN1         0x03
56 #define FCM_DELIM_SOFN2         0x05
57 #define FCM_DELIM_SOFN3         0x07
58 #define FCM_DELIM_SOFF          0x08
59 #define FCM_DELIM_SOFC4         0x09
60 #define FCM_DELIM_SOFI4         0x0A
61 #define FCM_DELIM_SOFN4         0x0B
62
63 #define FCM_DELIM_EOFT          0x01
64 #define FCM_DELIM_EOFDT         0x02
65 #define FCM_DELIM_EOFN          0x03
66 #define FCM_DELIM_EOFA          0x04
67 #define FCM_DELIM_EOFNI         0x07
68 #define FCM_DELIM_EOFDTI        0x06
69 #define FCM_DELIM_EOFRT         0x0A
70 #define FCM_DELIM_EOFRTI        0x0E
71 #define FCM_DELIM_NOEOF         0xF0
72 #define FCM_DELIM_EOFJUMBO      0xF1
73
74 static const value_string brdwlk_sof_vals[] = {
75     {FCM_DELIM_SOFI1, "SOFi1"},
76     {FCM_DELIM_SOFI2, "SOFi2"},
77     {FCM_DELIM_SOFI3, "SOFi3"},
78     {FCM_DELIM_SOFN1, "SOFn1"},
79     {FCM_DELIM_SOFN2, "SOFn2"},
80     {FCM_DELIM_SOFN3, "SOFn3"},
81     {FCM_DELIM_SOFF,  "SOFf"},
82     {0, NULL},
83 };
84
85 static const value_string brdwlk_eof_vals[] = {
86     {FCM_DELIM_EOFDT, "EOFdt"},
87     {FCM_DELIM_EOFA,  "EOFa"},
88     {FCM_DELIM_EOFN,  "EOFn"},
89     {FCM_DELIM_EOFT,  "EOFt"},
90     {0, NULL},
91 };
92
93 static int hf_brdwlk_sof = -1;
94 static int hf_brdwlk_eof = -1;
95 static int hf_brdwlk_error = -1;
96 static int hf_brdwlk_vsan = -1;
97 static int hf_brdwlk_pktcnt = -1;
98 static int hf_brdwlk_drop = -1;
99 static int hf_brdwlk_plen = -1;
100 static int hf_brdwlk_error_plp = -1;
101 static int hf_brdwlk_error_ef = -1;
102 static int hf_brdwlk_error_nd = -1;
103 static int hf_brdwlk_error_tr = -1;
104 static int hf_brdwlk_error_badcrc = -1;
105 static int hf_brdwlk_error_ff = -1;
106 static int hf_brdwlk_error_jumbo = -1;
107 static int hf_brdwlk_error_ctrl = -1;
108
109 /* Initialize the subtree pointers */
110 static gint ett_brdwlk = -1;
111 static gint ett_brdwlk_error = -1;
112
113 static gint proto_brdwlk = -1;
114
115 static guint16 packet_count = 0;
116 static gboolean first_pkt = TRUE;                /* start of capture */
117
118 static dissector_handle_t data_handle;
119 static dissector_handle_t fc_dissector_handle;
120
121
122 static const true_false_string tfs_error_plp = {
123         "Packet Length is PRESENT",
124         "Packet length is NOT present"
125 };
126 static const true_false_string tfs_error_ef = {
127         "This is an Empty Frame",
128         "Frame is NOT empty"
129 };
130 static const true_false_string tfs_error_nd = {
131         "This Frame has NO Data",
132         "This frame carries data"
133 };
134 static const true_false_string tfs_error_tr = {
135         "This frame is TRUNCATED",
136         "This frame is NOT truncated"
137 };
138 static const true_false_string tfs_error_crc = {
139         "This Frame has a BAD FC CRC",
140         "This frame has a valid crc"
141 };
142 static const true_false_string tfs_error_ff = {
143         "Fifo is Full",
144         "Fifo is NOT full"
145 };
146 static const true_false_string tfs_error_jumbo = {
147         "This is a JUMBO FC Frame",
148         "This is a NORMAL FC Frame"
149 };
150 static const true_false_string tfs_error_ctrl = {
151         "Ctrl Characters inside the frame",
152         "No ctrl chars inside the frame"
153 };
154
155 static void
156 dissect_brdwlk_err(proto_tree *parent_tree, tvbuff_t *tvb, int offset)
157 {
158         proto_item *item=NULL;
159         proto_tree *tree=NULL;
160         guint8 flags;
161
162         flags = tvb_get_guint8 (tvb, offset);
163         if(parent_tree){
164                 item=proto_tree_add_uint(parent_tree, hf_brdwlk_error, 
165                                 tvb, offset, 1, flags);
166                 tree=proto_item_add_subtree(item, ett_brdwlk_error);
167         }
168
169
170         proto_tree_add_boolean(tree, hf_brdwlk_error_plp, tvb, offset, 1, flags);
171         if (flags&0x01){
172                 proto_item_append_text(item, "  Packet Length Present");
173         }
174         flags&=(~( 0x01 ));
175
176         proto_tree_add_boolean(tree, hf_brdwlk_error_ef, tvb, offset, 1, flags);
177         if (flags&0x02){
178                 proto_item_append_text(item, "  Empty Frame");
179         }
180         flags&=(~( 0x02 ));
181
182         proto_tree_add_boolean(tree, hf_brdwlk_error_nd, tvb, offset, 1, flags);
183         if (flags&0x04){
184                 proto_item_append_text(item, "  No Data");
185         }
186         flags&=(~( 0x04 ));
187
188         proto_tree_add_boolean(tree, hf_brdwlk_error_tr, tvb, offset, 1, flags);
189         if (flags&0x08){
190                 proto_item_append_text(item, "  Truncated");
191         }
192         flags&=(~( 0x08 ));
193
194         proto_tree_add_boolean(tree, hf_brdwlk_error_badcrc, tvb, offset, 1, flags);
195         if (flags&0x10){
196                 proto_item_append_text(item, "  Bad FC CRC");
197         }
198         flags&=(~( 0x10 ));
199
200         proto_tree_add_boolean(tree, hf_brdwlk_error_ff, tvb, offset, 1, flags);
201         if (flags&0x20){
202                 proto_item_append_text(item, "  Fifo Full");
203         }
204         flags&=(~( 0x20 ));
205
206         proto_tree_add_boolean(tree, hf_brdwlk_error_jumbo, tvb, offset, 1, flags);
207         if (flags&0x40){
208                 proto_item_append_text(item, "  Jumbo FC Frame");
209         }
210         flags&=(~( 0x40 ));
211
212         proto_tree_add_boolean(tree, hf_brdwlk_error_ctrl, tvb, offset, 1, flags);
213         if (flags&0x80){
214                 proto_item_append_text(item, "  Ctrl Char Inside Frame");
215         }
216         flags&=(~( 0x80 ));
217 }
218
219 /* Code to actually dissect the packets */
220 static void
221 dissect_brdwlk (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
222 {
223
224 /* Set up structures needed to add the protocol subtree and manage it */
225     proto_item *ti, *hidden_item;
226     proto_tree *brdwlk_tree = NULL;
227     tvbuff_t *next_tvb;
228     guint8 error, eof, sof;
229     int hdrlen = 2,
230         offset = 0;
231     gint len, reported_len, plen;
232     guint16 pkt_cnt;
233     gboolean dropped_packets;
234
235     /* Make entries in Protocol column and Info column on summary display */
236     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
237         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Boardwalk");
238     
239     if (check_col(pinfo->cinfo, COL_INFO)) 
240         col_clear(pinfo->cinfo, COL_INFO);
241
242     pinfo->vsan = (tvb_get_ntohs (tvb, offset) & 0xFFF);
243     sof = (tvb_get_guint8 (tvb, offset) & 0xF0) >> 4;
244
245     if ((sof == FCM_DELIM_SOFI3) || (sof == FCM_DELIM_SOFI2) || (sof == FCM_DELIM_SOFI1)
246         || (sof == FCM_DELIM_SOFI4)) {
247         pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
248     }
249     else if (sof == FCM_DELIM_SOFF) {
250         pinfo->sof_eof = PINFO_SOF_SOFF;
251     }
252
253     if (tree) {
254         ti = proto_tree_add_protocol_format (tree, proto_brdwlk, tvb, 0,
255                                              hdrlen, "Boardwalk");
256
257         brdwlk_tree = proto_item_add_subtree (ti, ett_brdwlk);
258
259         proto_tree_add_item (brdwlk_tree, hf_brdwlk_sof, tvb, offset, 1, 0);
260         proto_tree_add_item (brdwlk_tree, hf_brdwlk_vsan, tvb, offset, 2, 0);
261
262     }
263
264     /* Locate EOF which is the last 4 bytes of the frame */
265     len = tvb_length_remaining(tvb, hdrlen);
266     reported_len = tvb_reported_length_remaining(tvb, hdrlen);
267     if (reported_len < 4) {
268         /*
269          * This packet is claimed not to even have enough data for
270          * a 4-byte EOF.
271          * Don't try to process the EOF.
272          */
273         ;
274     }
275     else if (len < reported_len) {
276         /*
277          * This packet is claimed to have enough data for a 4-byte EOF,
278          * but we didn't capture all of the packet.
279          * Slice off the 4-byte EOF from the reported length, and trim
280          * the captured length so it's no more than the reported length;
281          * that will slice off what of the EOF, if any, is in the
282          * captured length.
283          */
284         reported_len -= 4;
285         if (len > reported_len)
286             len = reported_len;
287     }
288     else {
289         /*
290          * We have the entire packet, and it includes a 4-byte EOF.
291          * Slice it off, and put it into the tree if we're building
292          * a tree.
293          */
294         len -= 4;
295         reported_len -= 4;
296         offset = tvb_reported_length(tvb) - 4;
297         pkt_cnt = tvb_get_ntohs (tvb, offset);
298         if (tree) {
299             proto_tree_add_uint (brdwlk_tree, hf_brdwlk_pktcnt, tvb, offset,
300                                  2, pkt_cnt);
301         }
302         dropped_packets = FALSE;
303         if (pinfo->fd->flags.visited) {
304             /*
305              * This isn't the first pass, so we can't use the global
306              * "packet_count" variable to determine whether there were
307              * any dropped frames or not.
308              * We therefore attach a non-null pointer as frame data to
309              * any frame preceded by dropped packets.
310              */
311             if (p_get_proto_data(pinfo->fd, proto_brdwlk) != NULL)
312                 dropped_packets = TRUE;
313         } else {
314             /*
315              * This is the first pass, so we have to use the global
316              * "packet_count" variable to determine whether there were
317              * any dropped frames or not.
318              *
319              * XXX - can there be more than one stream of packets, so that
320              * we can't just use a global variable?
321              */
322             if (pkt_cnt != packet_count + 1) {
323                 if (!first_pkt &&
324                     (pkt_cnt != 0 || (packet_count != BRDWLK_MAX_PACKET_CNT))) {
325                     dropped_packets = TRUE;
326
327                     /*
328                      * Mark this frame as having been preceded by dropped
329                      * packets.  (The data we use as the frame data doesn't
330                      * matter - it just matters that it's non-null.)
331                      */
332                     p_add_proto_data(pinfo->fd, proto_brdwlk, &packet_count);
333                 }
334             }
335
336             if (tree) {
337                 hidden_item = proto_tree_add_boolean (brdwlk_tree, hf_brdwlk_drop,
338                                                tvb, offset, 0, dropped_packets);
339                 PROTO_ITEM_SET_HIDDEN(hidden_item);
340             }
341         }
342         packet_count = pkt_cnt;
343
344         error=tvb_get_guint8(tvb, offset+2);           
345         dissect_brdwlk_err(brdwlk_tree, tvb, offset+2);
346
347         eof = tvb_get_guint8 (tvb, offset+3);
348         if (eof != FCM_DELIM_EOFN) {
349             pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
350         }
351         else if (eof != FCM_DELIM_EOFT) {
352             pinfo->sof_eof |= PINFO_EOF_INVALID;
353         }
354         
355         if (tree) {
356             proto_tree_add_item (brdwlk_tree, hf_brdwlk_eof, tvb, offset+3,
357                                  1, 0);
358         }
359
360         if ((error & BRDWLK_HAS_PLEN) && tree) {
361             /* In newer Boardwalks, if this bit is set, the actual frame length
362              * is also provided. This length is the size between SOF & EOF
363              * including FC CRC.
364              */
365             plen = tvb_get_ntohl (tvb, offset-4);
366             plen *= 4;
367             proto_tree_add_uint (brdwlk_tree, hf_brdwlk_plen, tvb, offset-4,
368                                  4, plen);
369             
370 #if 0
371             /* XXX - this would throw an exception if it would increase
372              * the reported length.
373              */
374             if (error & BRDWLK_TRUNCATED_BIT) {
375                 tvb_set_reported_length (tvb, plen);
376             }
377 #endif
378         }
379     }
380     
381     next_tvb = tvb_new_subset (tvb, 2, len, reported_len);
382     if (fc_dissector_handle) {
383         call_dissector (fc_dissector_handle, next_tvb, pinfo, tree);
384     }
385 }
386
387 static void
388 brdwlk_init(void)
389 {
390     packet_count = 0;
391     first_pkt = TRUE;
392 }
393
394 /* Register the protocol with Wireshark */
395
396 /* this format is require because a script is used to build the C function
397    that calls all the protocol registration.
398 */
399
400 void
401 proto_register_brdwlk (void)
402 {                 
403
404 /* Setup list of header fields  See Section 1.6.1 for details*/
405     static hf_register_info hf[] = {
406         { &hf_brdwlk_sof,
407           {"SOF", "brdwlk.sof", FT_UINT8, BASE_HEX, VALS (brdwlk_sof_vals),
408            0xF0, "SOF", HFILL}},
409         { &hf_brdwlk_eof,
410           {"EOF", "brdwlk.eof", FT_UINT8, BASE_HEX, VALS (brdwlk_eof_vals),
411            0x0F, "EOF", HFILL}},
412         { &hf_brdwlk_error,
413           {"Error", "brdwlk.error", FT_UINT8, BASE_HEX, NULL, 0x0, "Error",
414            HFILL}},
415         { &hf_brdwlk_pktcnt,
416           {"Packet Count", "brdwlk.pktcnt", FT_UINT16, BASE_DEC, NULL, 0x0,
417            "", HFILL}},
418         { &hf_brdwlk_drop,
419           {"Packet Dropped", "brdwlk.drop", FT_BOOLEAN, BASE_DEC, NULL, 0x0,
420            "", HFILL}},
421         { &hf_brdwlk_vsan,
422           {"VSAN", "brdwlk.vsan", FT_UINT16, BASE_DEC, NULL, 0xFFF, "",
423            HFILL}},
424         { &hf_brdwlk_plen,
425           {"Original Packet Length", "brdwlk.plen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
426            HFILL}},
427         { &hf_brdwlk_error_plp,
428           {"Packet Length Present", "brdwlk.error.plp", FT_BOOLEAN, 8, TFS(&tfs_error_plp), 0x01, "",
429            HFILL}},
430         { &hf_brdwlk_error_ef,
431           {"Empty Frame", "brdwlk.error.ef", FT_BOOLEAN, 8, TFS(&tfs_error_ef), 0x02, "",
432            HFILL}},
433         { &hf_brdwlk_error_nd,
434           {"No Data", "brdwlk.error.nd", FT_BOOLEAN, 8, TFS(&tfs_error_nd), 0x04, "",
435            HFILL}},
436         { &hf_brdwlk_error_tr,
437           {"Truncated", "brdwlk.error.tr", FT_BOOLEAN, 8, TFS(&tfs_error_tr), 0x08, "",
438            HFILL}},
439         { &hf_brdwlk_error_badcrc,
440           {"CRC", "brdwlk.error.crc", FT_BOOLEAN, 8, TFS(&tfs_error_crc), 0x10, "",
441            HFILL}},
442         { &hf_brdwlk_error_ff,
443           {"Fifo Full", "brdwlk.error.ff", FT_BOOLEAN, 8, TFS(&tfs_error_ff), 0x20, "",
444            HFILL}},
445         { &hf_brdwlk_error_jumbo,
446           {"Jumbo FC Frame", "brdwlk.error.jumbo", FT_BOOLEAN, 8, TFS(&tfs_error_jumbo), 0x40, "",
447            HFILL}},
448         { &hf_brdwlk_error_ctrl,
449           {"Ctrl Char Inside Frame", "brdwlk.error.ctrl", FT_BOOLEAN, 8, TFS(&tfs_error_ctrl), 0x80, "",
450            HFILL}},
451     };
452
453 /* Setup protocol subtree array */
454     static gint *ett[] = {
455         &ett_brdwlk,
456         &ett_brdwlk_error,
457     };
458
459 /* Register the protocol name and description */
460     proto_brdwlk = proto_register_protocol("Boardwalk",
461                                            "Boardwalk", "brdwlk");
462
463 /* Required function calls to register the header fields and subtrees used */
464     proto_register_field_array(proto_brdwlk, hf, array_length(hf));
465     proto_register_subtree_array(ett, array_length(ett));
466
467     register_init_routine(&brdwlk_init);
468 }
469
470
471 /* If this dissector uses sub-dissector registration add a registration routine.
472    This format is required because a script is used to find these routines and
473    create the code that calls these routines.
474 */
475 void
476 proto_reg_handoff_brdwlk(void)
477 {
478     dissector_handle_t brdwlk_handle;
479
480     brdwlk_handle = create_dissector_handle (dissect_brdwlk, proto_brdwlk);
481     dissector_add("ethertype", ETHERTYPE_BRDWALK, brdwlk_handle);
482     dissector_add("ethertype", 0xABCD, brdwlk_handle);
483     data_handle = find_dissector("data");
484     fc_dissector_handle = find_dissector ("fc");
485 }