From Charlie Lenahan: support for some HP switch protocol.
[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  * 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 <glib.h>
31 #include <epan/packet.h>
32 #include "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
61 static const value_string hpsw_tlv_type_vals[] = {
62         { HPFOO_DEVICE_NAME,       "Device Name" },
63         { HPFOO_DEVICE_VERSION,     "Version" },
64         { HPFOO_CONFIG_NAME,     "Config" },
65         { HPFOO_IP_ADDR,     "IP Addr" },
66         { HPFOO_FIELD_7,     "Field 7" },
67         { HPFOO_FIELD_8,     "Field 8" },
68         { HPFOO_FIELD_9,     "Field 9" },
69         { HPFOO_FIELD_10,     "Field 10" },
70         { 0x00,               NULL }
71 };
72
73
74 static void
75 dissect_hpsw(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
76 {
77         proto_tree      *hp_tree = NULL;
78         proto_tree      *tlv_tree = NULL;
79         proto_item      *ti = NULL;
80         guint8          version;
81
82         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
83                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "HP");
84         }
85
86         if (check_col(pinfo->cinfo, COL_INFO)) {
87                 col_set_str(pinfo->cinfo, COL_INFO, "HP Switch Protocol");
88         }
89
90         version = tvb_get_guint8(tvb, 0);
91
92         if (tree) {
93                 guint16 offset =0;
94
95                 ti = proto_tree_add_item(tree, proto_hpsw, tvb, 0, -1, FALSE);
96                 hp_tree = proto_item_add_subtree(ti, ett_hpsw);
97                 proto_tree_add_uint(hp_tree, hf_hpsw_version, tvb, 0, 1, version);
98                 offset++;
99
100                 proto_tree_add_item(hp_tree, hf_hpsw_type, tvb, 1, 1, FALSE);
101                 offset++;
102
103                 while ( tvb_reported_length_remaining(tvb, offset) > 0 )
104                 {
105                         guint8 type,length;
106                         
107                         type = tvb_get_guint8(tvb, offset);
108                         length = tvb_get_guint8(tvb, offset+1);
109
110                         /* make sure still in valid tlv */ 
111                         if (( length < 1 ) || ( length > tvb_length_remaining(tvb,offset+2)))
112                                 break;
113                    
114                         ti = proto_tree_add_text(hp_tree,tvb,offset,length+2,"%s",
115                                                 val_to_str(type,hpsw_tlv_type_vals,"Unknown TLV type: 0x%02x"));
116
117                         tlv_tree=proto_item_add_subtree(ti,ett_hpsw_tlv);
118
119                         /* type */
120                         proto_tree_add_uint(tlv_tree, hf_hpsw_tlvtype, tvb, offset, 1, type);
121                         offset++;
122
123                         /* LENGTH (not inclusive of type and length bytes) */
124                         proto_tree_add_uint(tlv_tree, hf_hpsw_tlvlength, tvb, offset, 1, length);
125                         offset++; 
126                         
127                         dissect_hpsw_tlv(tvb,offset,length,tlv_tree,ti,type);
128         
129                         offset += length;  
130
131                 }
132
133         }
134 }
135
136 static void
137 dissect_hpsw_tlv(tvbuff_t *tvb, int offset, int length,
138     proto_tree *tree, proto_item *ti, guint8 type)
139 {
140     switch (type) {
141
142     case HPFOO_DEVICE_NAME:
143         if (length > 0) {
144             proto_item_set_text(ti, "Device Name: %s", tvb_format_text(tvb, offset, length - 1));
145             proto_tree_add_text(tree, tvb, offset, length, "Device Name: %s", tvb_format_text(tvb, offset, length - 1));
146         } else {
147             proto_item_set_text(ti, "Device Name: Bad length %u", length);
148             proto_tree_add_text(tree, tvb, offset, length, "Device Name: Bad length %u", length);
149         }
150         break;
151
152     case HPFOO_DEVICE_VERSION:
153         if (length > 0) {
154             proto_item_set_text(ti, "Version: %s", tvb_format_text(tvb, offset, length - 1));
155             proto_tree_add_text(tree, tvb, offset, length, "Version: %s", tvb_format_text(tvb, offset, length - 1));
156         } else {
157             proto_item_set_text(ti, "Version: Bad length %u", length);
158             proto_tree_add_text(tree, tvb, offset, length, "Version: Bad length %u", length);
159         }
160         break;
161
162     case HPFOO_CONFIG_NAME:
163         if (length > 0) {
164             proto_item_set_text(ti, "Config: %s", tvb_format_text(tvb, offset, length - 1));
165             proto_tree_add_text(tree, tvb, offset, length, "Config: %s", tvb_format_text(tvb, offset, length - 1));
166         } else {
167             proto_item_set_text(ti, "Config: Bad length %u", length);
168             proto_tree_add_text(tree, tvb, offset, length, "Config: Bad length %u", length);
169         }
170         break;
171
172     case HPFOO_IP_ADDR:
173         if (length == 4) {
174             const guint8 *ipptr=tvb_get_ptr(tvb,offset,length);
175             proto_item_set_text(ti, "IP Addr: %s", ip_to_str(ipptr));
176             proto_tree_add_text(tree, tvb, offset, length, "IP Addr: %s", ip_to_str(ipptr));
177         } else {
178             proto_item_set_text(ti, "IP Addr: Bad length %u", length);
179             proto_tree_add_text(tree, tvb, offset, length, "IP Addr: Bad length %u", length);
180         }
181         break;
182
183     case HPFOO_FIELD_7:
184         if (length == 1) {
185             proto_item_set_text(ti, "Field 7: 0x%02x", tvb_get_guint8(tvb,offset));
186             proto_tree_add_text(tree, tvb, offset, length, "Field 7: 0x%02x", tvb_get_guint8(tvb,offset));
187         } else {
188             proto_item_set_text(ti, "Field 7: Bad length %u", length);
189             proto_tree_add_text(tree, tvb, offset, length, "Field 7: Bad length %u", length);
190         }
191         break;
192
193     case HPFOO_FIELD_8:
194         if (length == 2) {
195             proto_item_set_text(ti, "Field 8: 0x%02x", tvb_get_ntohs(tvb,offset));
196             proto_tree_add_text(tree, tvb, offset, length, "Field 8: 0x%02x", tvb_get_ntohs(tvb,offset));
197         } else {
198             proto_item_set_text(ti, "Field 8: Bad length %u", length);
199             proto_tree_add_text(tree, tvb, offset, length, "Field 8: Bad length %u", length);
200         }
201         break;
202
203     case HPFOO_FIELD_9:
204         if (length == 2) {
205             proto_item_set_text(ti, "Field 9: 0x%02x", tvb_get_ntohs(tvb,offset));
206             proto_tree_add_text(tree, tvb, offset, length, "Field 9: 0x%02x", tvb_get_ntohs(tvb,offset));
207         } else {
208             proto_item_set_text(ti, "Field 9: Bad length %u", length);
209             proto_tree_add_text(tree, tvb, offset, length, "Field 9: Bad length %u", length);
210         }
211         break;
212
213     case HPFOO_FIELD_10:
214         if (length == 4) {
215             proto_item_set_text(ti, "Field 10: 0x%04x", tvb_get_ntohl(tvb,offset));
216             proto_tree_add_text(tree, tvb, offset, length, "Field 9: 0x%04x", tvb_get_ntohl(tvb,offset));
217         } else {
218             proto_item_set_text(ti, "Field 10: Bad length %u", length);
219             proto_tree_add_text(tree, tvb, offset, length, "Field 10: Bad length %u", length);
220         }
221         break;
222
223     default:
224         proto_tree_add_text(tree, tvb, offset, length, "Data");
225         break;
226     }
227 }
228
229
230
231
232 void
233 proto_register_hpsw(void)
234 {
235         static hf_register_info hf[] = {
236                 { &hf_hpsw_version,
237                 { "Version", "hpsw.version", FT_UINT8, BASE_HEX,
238                         NULL, 0x0, "", HFILL }},
239                 { &hf_hpsw_type,
240                 { "Type", "hpsw.type", FT_UINT8, BASE_HEX,
241                         NULL, 0x0, "", HFILL }},
242                 { &hf_hpsw_tlvtype,
243                 { "Type", "hpsw.tlv_type", FT_UINT8, BASE_HEX,
244                         VALS(hpsw_tlv_type_vals), 0x0, "", HFILL }},
245                 { &hf_hpsw_tlvlength,
246                 { "Length", "hpsw.tlv_len", FT_UINT8, BASE_DEC,
247                         NULL, 0x0, "", HFILL }},
248         };
249
250         static gint *ett[] = {
251                 &ett_hpsw,
252                 &ett_hpsw_tlv,
253         };
254
255         proto_hpsw = proto_register_protocol( "HP Switch Protocol", "HPSW", "hpsw");
256         proto_register_field_array(proto_hpsw, hf, array_length(hf));
257         proto_register_subtree_array(ett, array_length(ett));
258
259         register_dissector("hpsw", dissect_hpsw, proto_hpsw);
260 }
261
262 void
263 proto_reg_handoff_hpsw(void)
264 {
265         dissector_handle_t hpsw_handle;
266
267         hpsw_handle = find_dissector("hpsw");
268
269         dissector_add("hpext.dxsap", 0x0623, hpsw_handle);
270 }