Use correct parent tree when creating a subtree: Fixes Coverity 902 [UNUSED];
[obnox/wireshark/wip.git] / epan / dissectors / packet-fefd.c
1 /* packet-fefd.c
2  * Routines for the disassembly of the "Far End Failure Detection"
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-udld.c
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <epan/packet.h>
32 #include <epan/strutil.h>
33
34 #include <epan/oui.h>
35 #include <epan/nlpid.h>
36
37
38
39 /* Offsets in TLV structure. */
40 #define TLV_TYPE        0
41 #define TLV_LENGTH      2
42
43 static int proto_fefd = -1;
44 static int hf_fefd_version = -1;
45 static int hf_fefd_opcode = -1;
46 static int hf_fefd_flags = -1;
47 static int hf_fefd_flags_rt = -1;
48 static int hf_fefd_flags_rsy = -1;
49 static int hf_fefd_checksum = -1;
50 static int hf_fefd_tlvtype = -1;
51 static int hf_fefd_tlvlength = -1;
52
53 static gint ett_fefd = -1;
54 static gint ett_fefd_flags = -1;
55 static gint ett_fefd_tlv = -1;
56
57 static dissector_handle_t data_handle;
58
59 #define TYPE_DEVICE_ID        0x0001
60 #define TYPE_PORT_ID          0x0002
61 #define TYPE_ECHO             0x0003
62 #define TYPE_MESSAGE_INTERVAL 0x0004
63 #define TYPE_TIMEOUT_INTERVAL 0x0005
64 #define TYPE_DEVICE_NAME      0x0006
65 #define TYPE_SEQUENCE_NUMBER  0x0007
66
67
68 static const value_string type_vals[] = {
69     { TYPE_DEVICE_ID,          "Device ID" },
70     { TYPE_PORT_ID,            "Port ID" },
71     { TYPE_ECHO,               "Echo" },
72     { TYPE_MESSAGE_INTERVAL,   "Message interval" },
73     { TYPE_TIMEOUT_INTERVAL,   "Timeout interval" },
74     { TYPE_DEVICE_NAME,        "Device name" },
75     { TYPE_SEQUENCE_NUMBER,    "Sequence number" },
76     { 0,                       NULL }
77 };
78
79 #define OPCODE_RESERVED       0x00
80 #define OPCODE_PROBE          0x01
81 #define OPCODE_ECHO           0x02
82 #define OPCODE_FLUSH          0x03
83
84 static const value_string opcode_vals[] = {
85     { OPCODE_RESERVED, "Reserved" },
86     { OPCODE_PROBE,    "Probe" },
87     { OPCODE_ECHO,     "Echo" },
88     { OPCODE_FLUSH,    "Flush" },
89     { 0,               NULL }
90 };
91
92 static void
93 dissect_fefd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
94 {
95     proto_item *ti;
96     proto_tree *fefd_tree = NULL;
97     int offset = 0;
98     guint16 type;
99     guint16 length;
100     proto_item *tlvi;
101     proto_tree *tlv_tree;
102     int real_length;
103
104     col_set_str(pinfo->cinfo, COL_PROTOCOL, "FEFD");
105     col_clear(pinfo->cinfo, COL_INFO);
106
107     if (tree) {
108         proto_item *flags_ti;
109         proto_tree *flags_tree;
110
111         ti = proto_tree_add_item(tree, proto_fefd, tvb, offset, -1, FALSE);
112         fefd_tree = proto_item_add_subtree(ti, ett_fefd);
113
114         /* FEFD header */
115         proto_tree_add_item(fefd_tree, hf_fefd_version, tvb, offset, 1, FALSE);
116         proto_tree_add_item(fefd_tree, hf_fefd_opcode, tvb, offset, 1, FALSE);
117         offset += 1;
118         flags_ti = proto_tree_add_item(fefd_tree, hf_fefd_flags, tvb, offset, 1, FALSE);
119         flags_tree = proto_item_add_subtree(flags_ti, ett_fefd_flags);
120         proto_tree_add_item(flags_tree, hf_fefd_flags_rt, tvb, offset, 1, FALSE);
121         proto_tree_add_item(flags_tree, hf_fefd_flags_rsy, tvb, offset, 1, FALSE);
122         offset += 1;
123         proto_tree_add_item(fefd_tree, hf_fefd_checksum, tvb, offset, 2, FALSE);
124         offset += 2;
125     } else {
126         offset += 4; /* The version/opcode/flags/checksum fields from above */
127     }
128
129     while (tvb_reported_length_remaining(tvb, offset) != 0) {
130         type = tvb_get_ntohs(tvb, offset + TLV_TYPE);
131         length = tvb_get_ntohs(tvb, offset + TLV_LENGTH);
132         if (length < 4) {
133             if (tree) {
134                 tlvi = proto_tree_add_text(fefd_tree, tvb, offset, 4,
135                                            "TLV with invalid length %u (< 4)",
136                                            length);
137                 tlv_tree = proto_item_add_subtree(tlvi, ett_fefd_tlv);
138                 proto_tree_add_uint(tlv_tree, hf_fefd_tlvtype, tvb,
139                                     offset + TLV_TYPE, 2, type);
140                 proto_tree_add_uint(tlv_tree, hf_fefd_tlvlength, tvb,
141                                     offset + TLV_LENGTH, 2, length);
142             }
143             offset += 4;
144             break;
145         }
146
147         switch (type) {
148
149         case TYPE_DEVICE_ID:
150             /* Device ID */
151
152             if (check_col(pinfo->cinfo, COL_INFO))
153                 col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
154                                     "Device ID: %s",
155                                     tvb_format_stringzpad(tvb, offset + 4,
156                                                           length - 4));
157
158             if (tree) {
159                 tlvi = proto_tree_add_text(fefd_tree, tvb, offset,
160                                            length, "Device ID: %s",
161                                            tvb_format_stringzpad(tvb, offset + 4, length - 4));
162                 tlv_tree = proto_item_add_subtree(tlvi, ett_fefd_tlv);
163                 proto_tree_add_uint(tlv_tree, hf_fefd_tlvtype, tvb,
164                                     offset + TLV_TYPE, 2, type);
165                 proto_tree_add_uint(tlv_tree, hf_fefd_tlvlength, tvb,
166                                     offset + TLV_LENGTH, 2, length);
167                 proto_tree_add_text(tlv_tree, tvb, offset + 4,
168                                     length - 4, "Device ID: %s",
169                                     tvb_format_stringzpad(tvb, offset + 4, length - 4));
170             }
171             offset += length;
172             break;
173
174         case TYPE_PORT_ID:
175             real_length = length;
176             if (tvb_get_guint8(tvb, offset + real_length) != 0x00) {
177                 /* The length in the TLV doesn't appear to be the
178                    length of the TLV, as the byte just past it
179                    isn't the first byte of a 2-byte big-endian
180                    small integer; make the length of the TLV the length
181                    in the TLV, plus 4 bytes for the TLV type and length,
182                    minus 1 because that's what makes one capture work. */
183                 real_length = length + 3;
184             }
185
186             if (check_col(pinfo->cinfo, COL_INFO))
187                 col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
188                                     "Port ID: %s",
189                                     tvb_format_stringzpad(tvb, offset + 4, real_length - 4));
190
191             if (tree) {
192                 tlvi = proto_tree_add_text(fefd_tree, tvb, offset,
193                                            real_length, "Port ID: %s",
194                                            tvb_format_text(tvb, offset + 4, real_length - 4));
195                 tlv_tree = proto_item_add_subtree(tlvi, ett_fefd_tlv);
196                 proto_tree_add_uint(tlv_tree, hf_fefd_tlvtype, tvb,
197                                     offset + TLV_TYPE, 2, type);
198                 proto_tree_add_uint(tlv_tree, hf_fefd_tlvlength, tvb,
199                                     offset + TLV_LENGTH, 2, length);
200                 proto_tree_add_text(tlv_tree, tvb, offset + 4,
201                                     real_length - 4,
202                                     "Sent through Interface: %s",
203                                     tvb_format_text(tvb, offset + 4, real_length - 4));
204             }
205             offset += real_length;
206             break;
207
208         case TYPE_ECHO:
209         case TYPE_MESSAGE_INTERVAL:
210         case TYPE_TIMEOUT_INTERVAL:
211         case TYPE_DEVICE_NAME:
212         case TYPE_SEQUENCE_NUMBER:
213         default:
214             tlvi = proto_tree_add_text(fefd_tree, tvb, offset,
215                                        length, "Type: %s, length: %u",
216                                        val_to_str(type, type_vals, "Unknown (0x%04x)"),
217                                        length);
218             tlv_tree = proto_item_add_subtree(tlvi, ett_fefd_tlv);
219             proto_tree_add_uint(tlv_tree, hf_fefd_tlvtype, tvb,
220                                 offset + TLV_TYPE, 2, type);
221             proto_tree_add_uint(tlv_tree, hf_fefd_tlvlength, tvb,
222                                 offset + TLV_LENGTH, 2, length);
223             if (length > 4) {
224                 proto_tree_add_text(tlv_tree, tvb, offset + 4,
225                                     length - 4, "Data");
226             } else {
227                 return;
228             }
229             offset += length;
230         }
231     }
232
233     call_dissector(data_handle, tvb_new_subset_remaining(tvb, offset), pinfo, fefd_tree);
234 }
235
236 void
237 proto_register_fefd(void)
238 {
239     static hf_register_info hf[] = {
240         { &hf_fefd_version,
241           { "Version",          "fefd.version",  FT_UINT8, BASE_DEC, NULL, 0xE0,
242             NULL, HFILL }},
243
244         { &hf_fefd_opcode,
245           { "Opcode",           "fefd.opcode", FT_UINT8, BASE_DEC, VALS(opcode_vals), 0x1F,
246             NULL, HFILL }},
247
248         { &hf_fefd_flags,
249           { "Flags",            "fefd.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
250             NULL, HFILL }},
251
252         { &hf_fefd_flags_rt,
253           { "Recommended timeout",              "fefd.flags.rt", FT_BOOLEAN, 8, NULL, 0x80,
254             NULL, HFILL }},
255
256         { &hf_fefd_flags_rsy,
257           { "ReSynch",          "fefd.flags.rsy", FT_BOOLEAN, 8, NULL, 0x40,
258             NULL, HFILL }},
259
260         { &hf_fefd_checksum,
261           { "Checksum",         "fefd.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
262             NULL, HFILL }},
263
264         { &hf_fefd_tlvtype,
265           { "Type",             "fefd.tlv.type", FT_UINT16, BASE_HEX, VALS(type_vals), 0x0,
266             NULL, HFILL }},
267
268         { &hf_fefd_tlvlength,
269           { "Length",           "fefd.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
270             NULL, HFILL }}
271     };
272     static gint *ett[] = {
273         &ett_fefd,
274         &ett_fefd_flags,
275         &ett_fefd_tlv
276     };
277
278     proto_fefd = proto_register_protocol("Far End Failure Detection", "FEFD", "fefd");
279     proto_register_field_array(proto_fefd, hf, array_length(hf));
280     proto_register_subtree_array(ett, array_length(ett));
281 }
282
283 void
284 proto_reg_handoff_fefd(void)
285 {
286     dissector_handle_t fefd_handle;
287
288     data_handle = find_dissector("data");
289     fefd_handle = create_dissector_handle(dissect_fefd, proto_fefd);
290     dissector_add_uint("llc.force10_pid", 0x0111, fefd_handle);
291 }