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