As the gtk2 directory is no longer needed (GTK1 and 2 are using the same sources...
[obnox/wireshark/wip.git] / 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  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef NEED_SNPRINTF_H
45 # include "snprintf.h"
46 #endif
47
48 #include <epan/packet.h>
49 #include "etypes.h"
50
51 #define BRDWLK_MAX_PACKET_CNT  0xFFFF
52 #define BRDWLK_TRUNCATED_BIT   0x8
53 #define BRDWLK_HAS_PLEN        0x1
54
55 #define FCM_DELIM_SOFC1         0x01
56 #define FCM_DELIM_SOFI1         0x02
57 #define FCM_DELIM_SOFI2         0x04
58 #define FCM_DELIM_SOFI3         0x06
59 #define FCM_DELIM_SOFN1         0x03
60 #define FCM_DELIM_SOFN2         0x05
61 #define FCM_DELIM_SOFN3         0x07
62 #define FCM_DELIM_SOFF          0x08
63 #define FCM_DELIM_SOFC4         0x09
64 #define FCM_DELIM_SOFI4         0x0A
65 #define FCM_DELIM_SOFN4         0x0B
66
67 #define FCM_DELIM_EOFT          0x01
68 #define FCM_DELIM_EOFDT         0x02
69 #define FCM_DELIM_EOFN          0x03
70 #define FCM_DELIM_EOFA          0x04
71 #define FCM_DELIM_EOFNI         0x07
72 #define FCM_DELIM_EOFDTI        0x06
73 #define FCM_DELIM_EOFRT         0x0A
74 #define FCM_DELIM_EOFRTI        0x0E
75 #define FCM_DELIM_NOEOF         0xF0
76 #define FCM_DELIM_EOFJUMBO      0xF1
77
78 static const value_string brdwlk_sof_vals[] = {
79     {FCM_DELIM_SOFI1, "SOFi1"},
80     {FCM_DELIM_SOFI2, "SOFi2"},
81     {FCM_DELIM_SOFI3, "SOFi3"},
82     {FCM_DELIM_SOFN1, "SOFn1"},
83     {FCM_DELIM_SOFN2, "SOFn2"},
84     {FCM_DELIM_SOFN3, "SOFn3"},
85     {FCM_DELIM_SOFF,  "SOFf"},
86     {0, NULL},
87 };
88
89 static const value_string brdwlk_eof_vals[] = {
90     {FCM_DELIM_EOFDT, "EOFdt"},
91     {FCM_DELIM_EOFA,  "EOFa"},
92     {FCM_DELIM_EOFN,  "EOFn"},
93     {FCM_DELIM_EOFT,  "EOFt"},
94     {0, NULL},
95 };
96
97 static int hf_brdwlk_sof = -1;
98 static int hf_brdwlk_eof = -1;
99 static int hf_brdwlk_error = -1;
100 static int hf_brdwlk_vsan = -1;
101 static int hf_brdwlk_pktcnt = -1;
102 static int hf_brdwlk_drop = -1;
103 static int hf_brdwlk_plen = -1;
104
105 /* Initialize the subtree pointers */
106 static gint ett_brdwlk = -1;
107
108 static gint proto_brdwlk = -1;
109
110 static guint16 packet_count = 0;
111 static gboolean first_pkt = TRUE;                /* start of capture */
112
113 static dissector_handle_t data_handle;
114 static dissector_handle_t fc_dissector_handle;
115
116 static gchar *
117 brdwlk_err_to_str (guint8 error, char *str)
118 {
119     if (str != NULL) {
120         str[0] = '\0';
121
122         if (error & 0x1) {
123             strcat (str, "Packet Length Present");
124         }
125         
126         if (error & 0x2) {
127             strcat (str, "Empty Frame, ");
128         }
129
130         if (error & 0x4) {
131             strcat (str, "No Data, ");
132         }
133
134         if (error & 0x8) {
135             strcat (str, "Truncated, ");
136         }
137
138         if (error & 0x10) {
139             strcat (str, "Bad FC CRC, ");
140         }
141
142         if (error & 0x20) {
143             strcat (str, "Fifo Full, ");
144         }
145
146         if (error & 0x40) {
147             strcat (str, "Jumbo FC Frame, ");
148         }
149
150         if (error & 0x80) {
151             strcat (str, "Ctrl Char Inside Frame");
152         }
153     }
154
155     return (str);
156 }
157
158 /* Code to actually dissect the packets */
159 static void
160 dissect_brdwlk (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
161 {
162
163 /* Set up structures needed to add the protocol subtree and manage it */
164     proto_item *ti;
165     proto_tree *brdwlk_tree = NULL;
166     tvbuff_t *next_tvb;
167     guint8 error, eof, sof;
168     int hdrlen = 2,
169         offset = 0;
170     gint len, reported_len, plen;
171     guint16 pkt_cnt;
172     gboolean dropped_packets;
173     gchar errstr[512];
174
175     /* Make entries in Protocol column and Info column on summary display */
176     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
177         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Boardwalk");
178     
179     if (check_col(pinfo->cinfo, COL_INFO)) 
180         col_clear(pinfo->cinfo, COL_INFO);
181
182     pinfo->vsan = (tvb_get_ntohs (tvb, offset) & 0xFFF);
183     sof = (tvb_get_guint8 (tvb, offset) & 0xF0) >> 4;
184
185     if ((sof == FCM_DELIM_SOFI3) || (sof == FCM_DELIM_SOFI2) || (sof == FCM_DELIM_SOFI1)
186         || (sof == FCM_DELIM_SOFI4)) {
187         pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
188     }
189     else if (sof == FCM_DELIM_SOFF) {
190         pinfo->sof_eof = PINFO_SOF_SOFF;
191     }
192
193     if (tree) {
194         ti = proto_tree_add_protocol_format (tree, proto_brdwlk, tvb, 0,
195                                              hdrlen, "Boardwalk");
196
197         brdwlk_tree = proto_item_add_subtree (ti, ett_brdwlk);
198
199         proto_tree_add_item (brdwlk_tree, hf_brdwlk_sof, tvb, offset, 1, 0);
200         proto_tree_add_item (brdwlk_tree, hf_brdwlk_vsan, tvb, offset, 2, 0);
201
202     }
203
204     /* Locate EOF which is the last 4 bytes of the frame */
205     len = tvb_length_remaining(tvb, hdrlen);
206     reported_len = tvb_reported_length_remaining(tvb, hdrlen);
207     if (reported_len < 4) {
208         /*
209          * This packet is claimed not to even have enough data for
210          * a 4-byte EOF.
211          * Don't try to process the EOF.
212          */
213         ;
214     }
215     else if (len < reported_len) {
216         /*
217          * This packet is claimed to have enough data for a 4-byte EOF,
218          * but we didn't capture all of the packet.
219          * Slice off the 4-byte EOF from the reported length, and trim
220          * the captured length so it's no more than the reported length;
221          * that will slice off what of the EOF, if any, is in the
222          * captured length.
223          */
224         reported_len -= 4;
225         if (len > reported_len)
226             len = reported_len;
227     }
228     else {
229         /*
230          * We have the entire packet, and it includes a 4-byte EOF.
231          * Slice it off, and put it into the tree if we're building
232          * a tree.
233          */
234         len -= 4;
235         reported_len -= 4;
236         offset = tvb_reported_length(tvb) - 4;
237         pkt_cnt = tvb_get_ntohs (tvb, offset);
238         if (tree) {
239             proto_tree_add_uint (brdwlk_tree, hf_brdwlk_pktcnt, tvb, offset,
240                                  2, pkt_cnt);
241         }
242         dropped_packets = FALSE;
243         if (pinfo->fd->flags.visited) {
244             /*
245              * This isn't the first pass, so we can't use the global
246              * "packet_count" variable to determine whether there were
247              * any dropped frames or not.
248              * We therefore attach a non-null pointer as frame data to
249              * any frame preceded by dropped packets.
250              */
251             if (p_get_proto_data(pinfo->fd, proto_brdwlk) != NULL)
252                 dropped_packets = TRUE;
253         } else {
254             /*
255              * This is the first pass, so we have to use the global
256              * "packet_count" variable to determine whether there were
257              * any dropped frames or not.
258              *
259              * XXX - can there be more than one stream of packets, so that
260              * we can't just use a global variable?
261              */
262             if (pkt_cnt != packet_count + 1) {
263                 if (!first_pkt &&
264                     (pkt_cnt != 0 || (packet_count != BRDWLK_MAX_PACKET_CNT))) {
265                     dropped_packets = TRUE;
266
267                     /*
268                      * Mark this frame as having been preceded by dropped
269                      * packets.  (The data we use as the frame data doesn't
270                      * matter - it just matters that it's non-null.)
271                      */
272                     p_add_proto_data(pinfo->fd, proto_brdwlk, &packet_count);
273                 }
274             }
275
276             if (tree) {
277                 proto_tree_add_boolean_hidden (brdwlk_tree, hf_brdwlk_drop,
278                                                tvb, offset, 0, dropped_packets);
279             }
280         }
281         packet_count = pkt_cnt;
282             
283         error = tvb_get_guint8 (tvb, offset+2);
284         if (tree) {
285             proto_tree_add_uint_format (brdwlk_tree, hf_brdwlk_error, tvb,
286                                         offset+2, 1, error, "Error: 0x%x (%s)",
287                                         error,
288                                         brdwlk_err_to_str (error, errstr));
289         }
290
291         eof = tvb_get_guint8 (tvb, offset+3);
292         if (eof != FCM_DELIM_EOFN) {
293             pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
294         }
295         else if (eof != FCM_DELIM_EOFT) {
296             pinfo->sof_eof |= PINFO_EOF_INVALID;
297         }
298         
299         if (tree) {
300             proto_tree_add_item (brdwlk_tree, hf_brdwlk_eof, tvb, offset+3,
301                                  1, 0);
302         }
303
304         if ((error & BRDWLK_HAS_PLEN) && tree) {
305             /* In newer Boardwalks, if this bit is set, the actual frame length
306              * is also provided. This length is the size between SOF & EOF
307              * including FC CRC.
308              */
309             plen = tvb_get_ntohl (tvb, offset-4);
310             plen *= 4;
311             proto_tree_add_uint (brdwlk_tree, hf_brdwlk_plen, tvb, offset-4,
312                                  4, plen);
313             
314 #if 0
315             /* XXX - this would throw an exception if it would increase
316              * the reported length.
317              */
318             if (error & BRDWLK_TRUNCATED_BIT) {
319                 tvb_set_reported_length (tvb, plen);
320             }
321 #endif
322         }
323     }
324     
325     next_tvb = tvb_new_subset (tvb, 2, len, reported_len);
326     if (fc_dissector_handle) {
327         call_dissector (fc_dissector_handle, next_tvb, pinfo, tree);
328     }
329 }
330
331 static void
332 brdwlk_init(void)
333 {
334     packet_count = 0;
335     first_pkt = TRUE;
336 }
337
338 /* Register the protocol with Ethereal */
339
340 /* this format is require because a script is used to build the C function
341    that calls all the protocol registration.
342 */
343
344 void
345 proto_register_brdwlk (void)
346 {                 
347
348 /* Setup list of header fields  See Section 1.6.1 for details*/
349     static hf_register_info hf[] = {
350         { &hf_brdwlk_sof,
351           {"SOF", "brdwlk.sof", FT_UINT8, BASE_HEX, VALS (brdwlk_sof_vals),
352            0xF0, "SOF", HFILL}},
353         { &hf_brdwlk_eof,
354           {"EOF", "brdwlk.eof", FT_UINT8, BASE_HEX, VALS (brdwlk_eof_vals),
355            0x0F, "EOF", HFILL}},
356         { &hf_brdwlk_error,
357           {"Error", "brdwlk.error", FT_UINT8, BASE_DEC, NULL, 0x0, "Error",
358            HFILL}},
359         { &hf_brdwlk_pktcnt,
360           {"Packet Count", "brdwlk.pktcnt", FT_UINT16, BASE_DEC, NULL, 0x0,
361            "", HFILL}},
362         { &hf_brdwlk_drop,
363           {"Packet Dropped", "brdwlk.drop", FT_BOOLEAN, BASE_DEC, NULL, 0x0,
364            "", HFILL}},
365         { &hf_brdwlk_vsan,
366           {"VSAN", "brdwlk.vsan", FT_UINT16, BASE_DEC, NULL, 0xFFF, "",
367            HFILL}},
368         { &hf_brdwlk_plen,
369           {"Original Packet Length", "brdwlk.plen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
370            HFILL}},
371     };
372
373 /* Setup protocol subtree array */
374     static gint *ett[] = {
375         &ett_brdwlk,
376     };
377
378 /* Register the protocol name and description */
379     proto_brdwlk = proto_register_protocol("Boardwalk",
380                                            "Boardwalk", "brdwlk");
381
382 /* Required function calls to register the header fields and subtrees used */
383     proto_register_field_array(proto_brdwlk, hf, array_length(hf));
384     proto_register_subtree_array(ett, array_length(ett));
385
386     register_init_routine(&brdwlk_init);
387 }
388
389
390 /* If this dissector uses sub-dissector registration add a registration routine.
391    This format is required because a script is used to find these routines and
392    create the code that calls these routines.
393 */
394 void
395 proto_reg_handoff_brdwlk(void)
396 {
397     dissector_handle_t brdwlk_handle;
398
399     brdwlk_handle = create_dissector_handle (dissect_brdwlk, proto_brdwlk);
400     dissector_add("ethertype", ETHERTYPE_BRDWALK, brdwlk_handle);
401     dissector_add("ethertype", 0xABCD, brdwlk_handle);
402     data_handle = find_dissector("data");
403     fc_dissector_handle = find_dissector ("fc");
404 }