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