For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-h1.c
1 /* packet-h1.c
2  * Routines for Sinec H1 packet disassembly
3  * Gerrit Gehnen <G.Gehnen@atrie.de>
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., 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
33 static int proto_h1 = -1;
34 static int hf_h1_header = -1;
35 static int hf_h1_len = -1;
36 static int hf_h1_opfield = -1;
37 static int hf_h1_oplen = -1;
38 static int hf_h1_opcode = -1;
39 static int hf_h1_requestblock = -1;
40 static int hf_h1_requestlen = -1;
41 static int hf_h1_dbnr = -1;
42 static int hf_h1_dwnr = -1;
43 static int hf_h1_dlen = -1;
44 static int hf_h1_org = -1;
45 static int hf_h1_response = -1;
46 static int hf_h1_response_len = -1;
47 static int hf_h1_response_value = -1;
48 static int hf_h1_empty_len = -1;
49 static int hf_h1_empty = -1;
50
51 static dissector_handle_t data_handle;
52
53 #define EMPTY_BLOCK     0xFF
54 #define OPCODE_BLOCK    0x01
55 #define REQUEST_BLOCK   0x03
56 #define RESPONSE_BLOCK  0x0F
57
58 static const value_string opcode_vals[] = {
59   {3, "Write Request"},
60   {4, "Write Response"},
61   {5, "Read Request"},
62   {6, "Read Response"},
63   {0, NULL}
64 };
65
66 static const value_string org_vals[] = {
67   {0x01, "DB"},
68   {0x02, "MB"},
69   {0x03, "EB"},
70   {0x04, "AB"},
71   {0x05, "PB"},
72   {0x06, "ZB"},
73   {0x07, "TB"},
74   {0x08, "BS"},
75   {0x09, "AS"},
76   {0x0a, "DX"},
77   {0x10, "DE"},
78   {0x11, "QB"},
79   {0, NULL}
80 };
81
82 static const value_string returncode_vals[] = {
83   {0x00, "No error"},
84   {0x02, "Requested block does not exist"},
85   {0x03, "Requested block too small"},
86   {0xFF, "Error, reason unknown"},
87   {0, NULL}
88 };
89
90 static gint ett_h1 = -1;
91 static gint ett_opcode = -1;
92 static gint ett_org = -1;
93 static gint ett_response = -1;
94 static gint ett_empty = -1;
95
96 static gboolean dissect_h1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
97 {
98   tvbuff_t *next_tvb;
99
100   proto_tree *h1_tree = NULL;
101
102   proto_item *ti;
103   proto_tree *opcode_tree = NULL;
104   proto_tree *org_tree = NULL;
105   proto_tree *response_tree = NULL;
106   proto_tree *empty_tree = NULL;
107
108   unsigned int position = 3;
109   unsigned int offset=0;
110
111   if (tvb_length(tvb) < 2)
112     {
113       /* Not enough data captured to hold the "S5" header; don't try
114          to interpret it as H1. */
115       return FALSE;
116     }
117
118   if (!(tvb_get_guint8(tvb,offset) == 'S' && tvb_get_guint8(tvb,offset+1) == '5'))
119     {
120       return FALSE;
121     }
122
123   col_set_str (pinfo->cinfo, COL_PROTOCOL, "H1");
124   col_set_str(pinfo->cinfo, COL_INFO, "S5: ");
125   if (tree)
126     {
127       ti = proto_tree_add_item (tree, proto_h1, tvb, offset, 16, ENC_NA);
128       h1_tree = proto_item_add_subtree (ti, ett_h1);
129       proto_tree_add_uint (h1_tree, hf_h1_header, tvb, offset, 2,
130                            tvb_get_ntohs(tvb,offset));
131       proto_tree_add_uint (h1_tree, hf_h1_len, tvb, offset + 2, 1,
132                            tvb_get_guint8(tvb,offset+2));
133     }
134
135   while (position < tvb_get_guint8(tvb,offset+2))
136     {
137       switch (tvb_get_guint8(tvb,offset + position))
138         {
139           case OPCODE_BLOCK:
140             if (h1_tree)
141               {
142                 ti = proto_tree_add_uint (h1_tree, hf_h1_opfield, tvb,
143                                           offset + position,
144                                           tvb_get_guint8(tvb,offset+position+1),
145                                           tvb_get_guint8(tvb,offset+position));
146                 opcode_tree = proto_item_add_subtree (ti, ett_opcode);
147                 proto_tree_add_uint (opcode_tree, hf_h1_oplen, tvb,
148                                      offset + position + 1, 1,
149                                      tvb_get_guint8(tvb,offset + position + 1));
150                 proto_tree_add_uint (opcode_tree, hf_h1_opcode, tvb,
151                                      offset + position + 2, 1,
152                                      tvb_get_guint8(tvb,offset + position + 2));
153               }
154             if (check_col (pinfo->cinfo, COL_INFO))
155               {
156                 col_append_str (pinfo->cinfo, COL_INFO,
157                                 val_to_str (tvb_get_guint8(tvb,offset + position + 2),
158                                             opcode_vals,"Unknown Opcode (0x%2.2x)"));
159               }
160             break;
161           case REQUEST_BLOCK:
162             if (h1_tree)
163               {
164                 ti = proto_tree_add_uint (h1_tree, hf_h1_requestblock, tvb,
165                                           offset + position,
166                                           tvb_get_guint8(tvb,offset + position + 1),
167                                           tvb_get_guint8(tvb,offset + position));
168                 org_tree = proto_item_add_subtree (ti, ett_org);
169                 proto_tree_add_uint (org_tree, hf_h1_requestlen, tvb,
170                                      offset + position + 1, 1,
171                                      tvb_get_guint8(tvb,offset + position+1));
172                 proto_tree_add_uint (org_tree, hf_h1_org, tvb,
173                                      offset + position + 2, 1,
174                                      tvb_get_guint8(tvb,offset + position+2));
175                 proto_tree_add_uint (org_tree, hf_h1_dbnr, tvb,
176                                      offset + position + 3, 1,
177                                      tvb_get_guint8(tvb,offset + position+3));
178                 proto_tree_add_uint (org_tree, hf_h1_dwnr, tvb,
179                                      offset + position + 4, 2,
180                                      tvb_get_ntohs(tvb,offset+position+4));
181                 proto_tree_add_int (org_tree, hf_h1_dlen, tvb,
182                                     offset + position + 6, 2,
183                                     tvb_get_ntohs(tvb,offset+position+6));
184               }
185             if (check_col (pinfo->cinfo, COL_INFO))
186               {
187                 col_append_fstr (pinfo->cinfo, COL_INFO, " %s %d",
188                                  val_to_str (tvb_get_guint8(tvb,offset + position + 2),
189                                              org_vals,"Unknown Type (0x%2.2x)"),
190                                  tvb_get_guint8(tvb,offset + position + 3));
191                 col_append_fstr (pinfo->cinfo, COL_INFO, " DW %d",
192                                  tvb_get_ntohs(tvb,offset+position+4));
193                 col_append_fstr (pinfo->cinfo, COL_INFO, " Count %d",
194                                  tvb_get_ntohs(tvb,offset+position+6));
195               }
196             break;
197           case RESPONSE_BLOCK:
198             if (h1_tree)
199               {
200                 ti = proto_tree_add_uint (h1_tree, hf_h1_response, tvb,
201                                           offset + position,
202                                           tvb_get_guint8(tvb,offset + position + 1),
203                                           tvb_get_guint8(tvb,offset + position));
204                 response_tree = proto_item_add_subtree (ti, ett_response);
205                 proto_tree_add_uint (response_tree, hf_h1_response_len, tvb,
206                                      offset + position + 1, 1,
207                                      tvb_get_guint8(tvb,offset + position+1));
208                 proto_tree_add_uint (response_tree, hf_h1_response_value, tvb,
209                                      offset + position + 2, 1,
210                                      tvb_get_guint8(tvb,offset + position+2));
211               }
212             if (check_col (pinfo->cinfo, COL_INFO))
213               {
214                 col_append_fstr (pinfo->cinfo, COL_INFO, " %s",
215                                  val_to_str (tvb_get_guint8(tvb,offset + position + 2),
216                                              returncode_vals,"Unknown Returncode (0x%2.2x"));
217               }
218             break;
219           case EMPTY_BLOCK:
220             if (h1_tree)
221               {
222                 ti = proto_tree_add_uint (h1_tree, hf_h1_empty, tvb,
223                                           offset + position,
224                                           tvb_get_guint8(tvb,offset + position + 1),
225                                           tvb_get_guint8(tvb,offset + position));
226                 empty_tree = proto_item_add_subtree (ti, ett_empty);
227
228                 proto_tree_add_uint (empty_tree, hf_h1_empty_len, tvb,
229                                      offset + position + 1, 1,
230                                      tvb_get_guint8(tvb,offset + position+1));
231               }
232             break;
233           default:
234             /* This is not a valid telegram. So cancel dissection
235                and try the next dissector */
236             return FALSE;
237         }
238         if (tvb_get_guint8(tvb,offset + position + 1) < 1)
239             THROW(ReportedBoundsError);
240         position += tvb_get_guint8(tvb,offset + position + 1);  /* Goto next section */
241     }                   /* ..while */
242   next_tvb = tvb_new_subset(tvb, offset+tvb_get_guint8(tvb,offset+2), -1, -1);
243   call_dissector(data_handle,next_tvb, pinfo, tree);
244
245   return TRUE;
246 }
247
248
249 void
250 proto_register_h1 (void)
251 {
252   static hf_register_info hf[] = {
253     {&hf_h1_header,
254      {"H1-Header", "h1.header", FT_UINT16, BASE_HEX, NULL, 0x0,
255       NULL, HFILL }},
256     {&hf_h1_len,
257      {"Length indicator", "h1.len", FT_UINT16, BASE_DEC, NULL, 0x0,
258       NULL, HFILL }},
259     {&hf_h1_opfield,
260      {"Operation identifier", "h1.opfield", FT_UINT8, BASE_HEX, NULL, 0x0,
261       NULL, HFILL }},
262     {&hf_h1_oplen,
263      {"Operation length", "h1.oplen", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
264     {&hf_h1_opcode,
265      {"Opcode", "h1.opcode", FT_UINT8, BASE_HEX, VALS (opcode_vals), 0x0,
266       NULL, HFILL }},
267     {&hf_h1_requestblock,
268      {"Request identifier", "h1.request", FT_UINT8, BASE_HEX, NULL, 0x0,
269       NULL, HFILL }},
270     {&hf_h1_requestlen,
271      {"Request length", "h1.reqlen", FT_UINT8, BASE_HEX, NULL, 0x0,
272       NULL, HFILL }},
273     {&hf_h1_org,
274      {"Memory type", "h1.org", FT_UINT8, BASE_HEX, VALS (org_vals), 0x0,
275       NULL, HFILL }},
276     {&hf_h1_dbnr,
277      {"Memory block number", "h1.dbnr", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
278     {&hf_h1_dwnr,
279      {"Address within memory block", "h1.dwnr", FT_UINT16, BASE_DEC, NULL, 0x0,
280       NULL, HFILL }},
281     {&hf_h1_dlen,
282      {"Length in words", "h1.dlen", FT_INT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
283     {&hf_h1_response,
284      {"Response identifier", "h1.response", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
285     {&hf_h1_response_len,
286      {"Response length", "h1.reslen", FT_UINT8, BASE_DEC, NULL, 0x0,
287       NULL, HFILL }},
288     {&hf_h1_response_value,
289      {"Response value", "h1.resvalue", FT_UINT8, BASE_DEC,
290       VALS (returncode_vals), 0x0, NULL, HFILL }},
291     {&hf_h1_empty,
292      {"Empty field", "h1.empty", FT_UINT8, BASE_HEX, NULL, 0x0,
293       NULL, HFILL }},
294     {&hf_h1_empty_len,
295      {"Empty field length", "h1.empty_len", FT_UINT8, BASE_DEC, NULL, 0x0,
296       NULL, HFILL }}
297   };
298
299   static gint *ett[] = {
300     &ett_h1,
301     &ett_opcode,
302     &ett_response,
303     &ett_org,
304     &ett_empty
305   };
306
307   proto_h1 = proto_register_protocol ("Sinec H1 Protocol", "H1", "h1");
308   proto_register_field_array (proto_h1, hf, array_length (hf));
309   proto_register_subtree_array (ett, array_length (ett));
310 }
311
312 void
313 proto_reg_handoff_h1(void)
314 {
315   heur_dissector_add("cotp", dissect_h1, proto_h1);
316   heur_dissector_add("cotp_is", dissect_h1, proto_h1);
317   heur_dissector_add("tcp", dissect_h1, proto_h1);
318   data_handle = find_dissector("data");
319 }