Improve HP Switch Protocol Dissection.
[obnox/wireshark/wip.git] / epan / dissectors / packet-hpsw.c
1 /* packet-hpsw.c
2  * Routines for HP Switch Config protocol
3  * Charlie Lenahan <clenahan@fortresstech.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/etypes.h>
33
34 #include "packet-hpext.h"
35
36 static void dissect_hpsw_tlv(tvbuff_t *tvb, int offset, int length,
37      proto_tree *tree, proto_item *ti, guint8 type);
38
39 static int proto_hpsw = -1;
40
41 static int hf_hpsw_version = -1;
42 static int hf_hpsw_type = -1;
43 static int hf_hpsw_tlvtype = -1;
44 static int hf_hpsw_tlvlength = -1;
45
46
47 static gint ett_hpsw = -1;
48 static gint ett_hpsw_tlv = -1;
49
50
51
52 #define HPFOO_DEVICE_NAME   0x1
53 #define HPFOO_DEVICE_VERSION 0x2
54 #define HPFOO_CONFIG_NAME 0x3
55 #define HPFOO_IP_ADDR 0x5
56 #define HPFOO_FIELD_7 0x7
57 #define HPFOO_FIELD_8 0x8
58 #define HPFOO_FIELD_9 0x9
59 #define HPFOO_FIELD_10 0xa
60 #define HPFOO_FIELD_12 0xc
61 #define HPFOO_DEVICE_ID 0xd /* Interpretation of this field is an educated guess */
62 #define HPFOO_MAC_ADDR 0xe
63
64 static const value_string hpsw_tlv_type_vals[] = {
65         { HPFOO_DEVICE_NAME,       "Device Name" },
66         { HPFOO_DEVICE_VERSION,     "Version" },
67         { HPFOO_CONFIG_NAME,     "Config" },
68         { HPFOO_IP_ADDR,     "IP Addr" },
69         { HPFOO_FIELD_7,     "Field 7" },
70         { HPFOO_FIELD_8,     "Field 8" },
71         { HPFOO_FIELD_9,     "Field 9" },
72         { HPFOO_FIELD_10,     "Field 10" },
73         { HPFOO_FIELD_12,     "Field 12" },
74         { HPFOO_DEVICE_ID,    "Device ID" },
75         { HPFOO_MAC_ADDR,     "MAC Addr" },
76         { 0x00,               NULL }
77 };
78
79
80 static void
81 dissect_hpsw(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
82 {
83         proto_tree      *hp_tree = NULL;
84         proto_tree      *tlv_tree = NULL;
85         proto_item      *ti = NULL;
86         guint8          version;
87
88         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
89                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "HP");
90         }
91
92         if (check_col(pinfo->cinfo, COL_INFO)) {
93                 col_set_str(pinfo->cinfo, COL_INFO, "HP Switch Protocol");
94         }
95
96         version = tvb_get_guint8(tvb, 0);
97
98         if (tree) {
99                 guint16 offset =0;
100
101                 ti = proto_tree_add_item(tree, proto_hpsw, tvb, 0, -1, FALSE);
102                 hp_tree = proto_item_add_subtree(ti, ett_hpsw);
103                 proto_tree_add_uint(hp_tree, hf_hpsw_version, tvb, 0, 1, version);
104                 offset++;
105
106                 proto_tree_add_item(hp_tree, hf_hpsw_type, tvb, 1, 1, FALSE);
107                 offset++;
108
109                 while ( tvb_reported_length_remaining(tvb, offset) > 0 )
110                 {
111                         guint8 type,length;
112                         
113                         type = tvb_get_guint8(tvb, offset);
114                         length = tvb_get_guint8(tvb, offset+1);
115
116                         /* make sure still in valid tlv */ 
117                         if (( length < 1 ) || ( length > tvb_length_remaining(tvb,offset+2)))
118                                 break;
119                    
120                         ti = proto_tree_add_text(hp_tree,tvb,offset,length+2,"%s",
121                                                 val_to_str(type,hpsw_tlv_type_vals,"Unknown TLV type: 0x%02x"));
122
123                         tlv_tree=proto_item_add_subtree(ti,ett_hpsw_tlv);
124
125                         /* type */
126                         proto_tree_add_uint(tlv_tree, hf_hpsw_tlvtype, tvb, offset, 1, type);
127                         offset++;
128
129                         /* LENGTH (not inclusive of type and length bytes) */
130                         proto_tree_add_uint(tlv_tree, hf_hpsw_tlvlength, tvb, offset, 1, length);
131                         offset++; 
132                         
133                         dissect_hpsw_tlv(tvb,offset,length,tlv_tree,ti,type);
134         
135                         offset += length;  
136
137                 }
138
139         }
140 }
141
142 static void
143 dissect_hpsw_tlv(tvbuff_t *tvb, int offset, int length,
144     proto_tree *tree, proto_item *ti, guint8 type)
145 {
146     switch (type) {
147
148     case HPFOO_DEVICE_NAME:
149         if (length > 0) {
150             proto_item_set_text(ti, "Device Name: %s", tvb_format_text(tvb, offset, length - 1));
151             proto_tree_add_text(tree, tvb, offset, length, "Device Name: %s", tvb_format_text(tvb, offset, length - 1));
152         } else {
153             proto_item_set_text(ti, "Device Name: Bad length %u", length);
154             proto_tree_add_text(tree, tvb, offset, length, "Device Name: Bad length %u", length);
155         }
156         break;
157
158     case HPFOO_DEVICE_VERSION:
159         if (length > 0) {
160             proto_item_set_text(ti, "Version: %s", tvb_format_text(tvb, offset, length - 1));
161             proto_tree_add_text(tree, tvb, offset, length, "Version: %s", tvb_format_text(tvb, offset, length - 1));
162         } else {
163             proto_item_set_text(ti, "Version: Bad length %u", length);
164             proto_tree_add_text(tree, tvb, offset, length, "Version: Bad length %u", length);
165         }
166         break;
167
168     case HPFOO_CONFIG_NAME:
169         if (length > 0) {
170             proto_item_set_text(ti, "Config: %s", tvb_format_text(tvb, offset, length - 1));
171             proto_tree_add_text(tree, tvb, offset, length, "Config: %s", tvb_format_text(tvb, offset, length - 1));
172         } else {
173             proto_item_set_text(ti, "Config: Bad length %u", length);
174             proto_tree_add_text(tree, tvb, offset, length, "Config: Bad length %u", length);
175         }
176         break;
177
178     case HPFOO_IP_ADDR:
179         if (length == 4) {
180             const guint8 *ipptr=tvb_get_ptr(tvb,offset,length);
181             proto_item_set_text(ti, "IP Addr: %s", ip_to_str(ipptr));
182             proto_tree_add_text(tree, tvb, offset, length, "IP Addr: %s", ip_to_str(ipptr));
183         } else {
184             proto_item_set_text(ti, "IP Addr: Bad length %u", length);
185             proto_tree_add_text(tree, tvb, offset, length, "IP Addr: Bad length %u", length);
186         }
187         break;
188
189     case HPFOO_FIELD_7:
190         if (length == 1) {
191             proto_item_set_text(ti, "Field 7: 0x%02x", tvb_get_guint8(tvb,offset));
192             proto_tree_add_text(tree, tvb, offset, length, "Field 7: 0x%02x", tvb_get_guint8(tvb,offset));
193         } else {
194             proto_item_set_text(ti, "Field 7: Bad length %u", length);
195             proto_tree_add_text(tree, tvb, offset, length, "Field 7: Bad length %u", length);
196         }
197         break;
198
199     case HPFOO_FIELD_8:
200         if (length == 2) {
201             proto_item_set_text(ti, "Field 8: 0x%04x", tvb_get_ntohs(tvb,offset));
202             proto_tree_add_text(tree, tvb, offset, length, "Field 8: 0x%04x", tvb_get_ntohs(tvb,offset));
203         } else {
204             proto_item_set_text(ti, "Field 8: Bad length %u", length);
205             proto_tree_add_text(tree, tvb, offset, length, "Field 8: Bad length %u", length);
206         }
207         break;
208
209     case HPFOO_FIELD_9:
210         if (length == 2) {
211             proto_item_set_text(ti, "Field 9: 0x%04x", tvb_get_ntohs(tvb,offset));
212             proto_tree_add_text(tree, tvb, offset, length, "Field 9: 0x%04x", tvb_get_ntohs(tvb,offset));
213         } else {
214             proto_item_set_text(ti, "Field 9: Bad length %u", length);
215             proto_tree_add_text(tree, tvb, offset, length, "Field 9: Bad length %u", length);
216         }
217         break;
218
219     case HPFOO_FIELD_10:
220         if (length == 4) {
221             proto_item_set_text(ti, "Field 10: 0x%08x", tvb_get_ntohl(tvb,offset));
222             proto_tree_add_text(tree, tvb, offset, length, "Field 10: 0x%08x", tvb_get_ntohl(tvb,offset));
223         } else {
224             proto_item_set_text(ti, "Field 10: Bad length %u", length);
225             proto_tree_add_text(tree, tvb, offset, length, "Field 10: Bad length %u", length);
226         }
227         break;
228
229     case HPFOO_FIELD_12:
230         if (length == 1) {
231             proto_item_set_text(ti, "Field 12: 0x%02x", tvb_get_guint8(tvb,offset));
232             proto_tree_add_text(tree, tvb, offset, length, "Field 12: 0x%02x", tvb_get_guint8(tvb,offset));
233         } else {
234             proto_item_set_text(ti, "Field 12: Bad length %u", length);
235             proto_tree_add_text(tree, tvb, offset, length, "Field 12: Bad length %u", length);
236         }
237         break;
238
239     case HPFOO_DEVICE_ID:
240         if (length == 10) {
241             const guint8 *macptr=tvb_get_ptr(tvb,offset,6);
242             guint32 id=tvb_get_ntohl(tvb, offset+6);
243             proto_item_set_text(ti, "Device ID: %s / %u", ether_to_str(macptr), id);
244             proto_tree_add_text(tree, tvb, offset, 10, "Device ID: %s / %u", ether_to_str(macptr), id);
245         } else {
246             proto_item_set_text(ti, "Device ID: Bad length %u", length);
247             proto_tree_add_text(tree, tvb, offset, length, "Device ID: Bad length %u", length);
248         }
249         break;
250
251     case HPFOO_MAC_ADDR:
252         if (length == 6) {
253             const guint8 *macptr=tvb_get_ptr(tvb,offset,length);
254             proto_item_set_text(ti, "MAC Addr: %s", ether_to_str(macptr));
255             proto_tree_add_text(tree, tvb, offset, length, "MAC Addr: %s", ether_to_str(macptr));
256         } else {
257             proto_item_set_text(ti, "MAC Addr: Bad length %u", length);
258             proto_tree_add_text(tree, tvb, offset, length, "MAC Addr: Bad length %u", length);
259         }
260         break;
261
262     default:
263         proto_tree_add_text(tree, tvb, offset, length, "Data");
264         break;
265     }
266 }
267
268
269
270
271 void
272 proto_register_hpsw(void)
273 {
274         static hf_register_info hf[] = {
275                 { &hf_hpsw_version,
276                 { "Version", "hpsw.version", FT_UINT8, BASE_HEX,
277                         NULL, 0x0, "", HFILL }},
278                 { &hf_hpsw_type,
279                 { "Type", "hpsw.type", FT_UINT8, BASE_HEX,
280                         NULL, 0x0, "", HFILL }},
281                 { &hf_hpsw_tlvtype,
282                 { "Type", "hpsw.tlv_type", FT_UINT8, BASE_HEX,
283                         VALS(hpsw_tlv_type_vals), 0x0, "", HFILL }},
284                 { &hf_hpsw_tlvlength,
285                 { "Length", "hpsw.tlv_len", FT_UINT8, BASE_DEC,
286                         NULL, 0x0, "", HFILL }}
287         };
288
289         static gint *ett[] = {
290                 &ett_hpsw,
291                 &ett_hpsw_tlv
292         };
293
294         proto_hpsw = proto_register_protocol( "HP Switch Protocol", "HPSW", "hpsw");
295         proto_register_field_array(proto_hpsw, hf, array_length(hf));
296         proto_register_subtree_array(ett, array_length(ett));
297
298         register_dissector("hpsw", dissect_hpsw, proto_hpsw);
299 }
300
301 void
302 proto_reg_handoff_hpsw(void)
303 {
304         dissector_handle_t hpsw_handle;
305
306         hpsw_handle = find_dissector("hpsw");
307
308         dissector_add("hpext.dxsap", HPEXT_HPSW, hpsw_handle);
309 }