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