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