Update Laurent Meyer's e-mail address.
[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: packet-brdwlk.c,v 1.2 2003/01/23 07:01:52 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
12  * is a dissector file; if you just copied this from README.developer,
13  * don't bother with the "Copied from" - you don't even need to put
14  * in a "Copied from" if you copied an existing dissector, especially
15  * if the bulk of the code in the new dissector is your code)
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 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #ifdef HAVE_NETINET_IN_H
45 # include <netinet/in.h>
46 #endif
47
48 #include <glib.h>
49
50 #ifdef NEED_SNPRINTF_H
51 # include "snprintf.h"
52 #endif
53
54 #include <epan/packet.h>
55 #include "etypes.h"
56
57 #define BRDWLK_MAX_PACKET_CNT  0xFFFF
58 #define BRDWLK_TRUNCATED_BIT   0x8
59
60 #define FCM_DELIM_SOFC1         0x01
61 #define FCM_DELIM_SOFI1         0x02
62 #define FCM_DELIM_SOFI2         0x04
63 #define FCM_DELIM_SOFI3         0x06
64 #define FCM_DELIM_SOFN1         0x03
65 #define FCM_DELIM_SOFN2         0x05
66 #define FCM_DELIM_SOFN3         0x07
67 #define FCM_DELIM_SOFF          0x08
68 #define FCM_DELIM_SOFC4         0x09
69 #define FCM_DELIM_SOFI4         0x0A
70 #define FCM_DELIM_SOFN4         0x0B
71
72 #define FCM_DELIM_EOFT          0x01
73 #define FCM_DELIM_EOFDT         0x02
74 #define FCM_DELIM_EOFN          0x03
75 #define FCM_DELIM_EOFA          0x04
76 #define FCM_DELIM_EOFNI         0x07
77 #define FCM_DELIM_EOFDTI        0x06
78 #define FCM_DELIM_EOFRT         0x0A
79 #define FCM_DELIM_EOFRTI        0x0E
80 #define FCM_DELIM_NOEOF         0xF0
81 #define FCM_DELIM_EOFJUMBO      0xF1
82
83 static const value_string brdwlk_sof_vals[] = {
84     {FCM_DELIM_SOFI1, "SOFi1"},
85     {FCM_DELIM_SOFI2, "SOFi2"},
86     {FCM_DELIM_SOFI3, "SOFi3"},
87     {FCM_DELIM_SOFN1, "SOFn1"},
88     {FCM_DELIM_SOFN2, "SOFn2"},
89     {FCM_DELIM_SOFN3, "SOFn3"},
90     {FCM_DELIM_SOFF,  "SOFf"},
91     {0, NULL},
92 };
93
94 static const value_string brdwlk_eof_vals[] = {
95     {FCM_DELIM_EOFDT, "EOFdt"},
96     {FCM_DELIM_EOFA,  "EOFa"},
97     {FCM_DELIM_EOFN,  "EOFn"},
98     {FCM_DELIM_EOFT,  "EOFt"},
99     {0, NULL},
100 };
101
102 static const value_string brdwlk_frametype_vals[] = {
103     {0, NULL},
104 };
105
106 static const value_string brdwlk_error_vals[] = {
107     {0, NULL},
108 };
109
110 static int hf_brdwlk_sof = -1;
111 static int hf_brdwlk_eof = -1;
112 static int hf_brdwlk_error = -1;
113 static int hf_brdwlk_vsan = -1;
114 static int hf_brdwlk_pktcnt = -1;
115 static int hf_brdwlk_drop = -1;
116
117 /* Initialize the subtree pointers */
118 static gint ett_brdwlk = -1;
119
120 static gint proto_brdwlk = -1;
121
122 static guint16 packet_count = 0;
123 static gboolean first_pkt = TRUE;                /* start of capture */
124
125 static dissector_handle_t data_handle;
126 static dissector_handle_t fc_dissector_handle;
127
128 static gchar *
129 brdwlk_err_to_str (guint8 error, char *str)
130 {
131     if (str != NULL) {
132         str[0] = '\0';
133
134         if (error & 0x2) {
135             strcat (str, "Empty Frame, ");
136         }
137
138         if (error & 0x4) {
139             strcat (str, "No Data, ");
140         }
141
142         if (error & 0x8) {
143             strcat (str, "Truncated, ");
144         }
145
146         if (error & 0x10) {
147             strcat (str, "Bad FC CRC, ");
148         }
149
150         if (error & 0x20) {
151             strcat (str, "Fifo Full, ");
152         }
153
154         if (error & 0x40) {
155             strcat (str, "Jumbo FC Frame, ");
156         }
157
158         if (error & 0x80) {
159             strcat (str, "Ctrl Char Inside Frame");
160         }
161     }
162
163     return (str);
164 }
165
166 /* Code to actually dissect the packets */
167 static void
168 dissect_brdwlk (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
169 {
170
171 /* Set up structures needed to add the protocol subtree and manage it */
172     proto_item *ti;
173     proto_tree *brdwlk_tree;
174     tvbuff_t *next_tvb;
175     guint8 error;
176     int hdrlen = 2,
177         offset = 0;
178     guint16 pkt_cnt;
179     gchar errstr[512];
180
181     /* Make entries in Protocol column and Info column on summary display */
182     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
183         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Boardwalk");
184     
185     if (check_col(pinfo->cinfo, COL_INFO)) 
186         col_clear(pinfo->cinfo, COL_INFO);
187
188     if (tree) {
189         ti = proto_tree_add_protocol_format (tree, proto_brdwlk, tvb, 0,
190                                              hdrlen, "Boardwalk");
191
192         brdwlk_tree = proto_item_add_subtree (ti, ett_brdwlk);
193
194         proto_tree_add_item (brdwlk_tree, hf_brdwlk_sof, tvb, offset, 1, 0);
195         proto_tree_add_item (brdwlk_tree, hf_brdwlk_vsan, tvb, offset, 2, 0);
196
197         pinfo->vsan = (tvb_get_ntohs (tvb, offset) & 0xFFF);
198
199         /* Locate EOF which is the last 4 bytes of the frame */
200         offset = tvb_reported_length (tvb) - 4;
201         proto_tree_add_item (brdwlk_tree, hf_brdwlk_pktcnt, tvb, offset,
202                              2, 0);
203         pkt_cnt = tvb_get_ntohs (tvb, offset);
204         if (pkt_cnt != packet_count + 1) {
205             if (first_pkt ||
206                 (!pkt_cnt && (packet_count == BRDWLK_MAX_PACKET_CNT))) {
207                 proto_tree_add_boolean_hidden (brdwlk_tree, hf_brdwlk_drop, tvb,
208                                                offset, 1, 0);
209             }
210             else {
211                 proto_tree_add_boolean_hidden (brdwlk_tree, hf_brdwlk_drop, tvb,
212                                                offset, 1, 1);
213             }
214         }
215         packet_count = pkt_cnt;
216             
217         error = tvb_get_guint8 (tvb, offset+2);
218         proto_tree_add_uint_format (brdwlk_tree, hf_brdwlk_error, tvb, offset+2,
219                                     1, error, "Error: 0x%x (%s)",
220                                     error, brdwlk_err_to_str (error, errstr));
221 #if 0
222         /* If received frame is truncated, set is_truncated flag */
223         if (error & BRDWLK_TRUNCATED_BIT) {
224             pinfo->is_truncated = TRUE;
225         }
226 #endif
227         
228         proto_tree_add_item (brdwlk_tree, hf_brdwlk_eof, tvb, offset+3,
229                              1, 0);
230     }
231     
232     next_tvb = tvb_new_subset (tvb, 2, -1, -1);
233     if (fc_dissector_handle) {
234         call_dissector (fc_dissector_handle, next_tvb, pinfo, tree);
235     }
236 }
237
238
239 /* Register the protocol with Ethereal */
240
241 /* this format is require because a script is used to build the C function
242    that calls all the protocol registration.
243 */
244
245 void
246 proto_register_brdwlk (void)
247 {                 
248
249 /* Setup list of header fields  See Section 1.6.1 for details*/
250     static hf_register_info hf[] = {
251         { &hf_brdwlk_sof,
252           {"SOF", "brdwlk.sof", FT_UINT8, BASE_HEX, VALS (brdwlk_sof_vals),
253            0xF0, "SOF", HFILL}},
254         { &hf_brdwlk_eof,
255           {"EOF", "brdwlk.eof", FT_UINT8, BASE_HEX, VALS (brdwlk_eof_vals),
256            0x0, "EOF", HFILL}},
257         { &hf_brdwlk_error,
258           {"Error", "brdwlk.error", FT_UINT8, BASE_DEC, NULL, 0x0, "Error",
259            HFILL}},
260         { &hf_brdwlk_pktcnt,
261           {"Packet Count", "brdwlk.pktcnt", FT_UINT16, BASE_DEC, NULL, 0x0,
262            "", HFILL}},
263         { &hf_brdwlk_drop,
264           {"Packet Dropped", "brdwlk.drop", FT_BOOLEAN, BASE_DEC, NULL, 0x0,
265            "", HFILL}},
266         { &hf_brdwlk_vsan,
267           {"VSAN", "brdwlk.vsan", FT_UINT16, BASE_DEC, NULL, 0xFFF, "",
268            HFILL}},
269     };
270
271 /* Setup protocol subtree array */
272     static gint *ett[] = {
273         &ett_brdwlk,
274     };
275
276 /* Register the protocol name and description */
277     proto_brdwlk = proto_register_protocol("Boardwalk",
278                                            "Boardwalk", "brdwlk");
279
280 /* Required function calls to register the header fields and subtrees used */
281     proto_register_field_array(proto_brdwlk, hf, array_length(hf));
282     proto_register_subtree_array(ett, array_length(ett));
283
284 }
285
286
287 /* If this dissector uses sub-dissector registration add a registration routine.
288    This format is required because a script is used to find these routines and
289    create the code that calls these routines.
290 */
291 void
292 proto_reg_handoff_brdwlk(void)
293 {
294     dissector_handle_t brdwlk_handle;
295
296     brdwlk_handle = create_dissector_handle (dissect_brdwlk, proto_brdwlk);
297     dissector_add("ethertype", ETHERTYPE_BRDWALK, brdwlk_handle);
298     data_handle = find_dissector("data");
299     fc_dissector_handle = find_dissector ("fc");
300 }