Add tvbuff class.
[metze/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.8 2000/05/11 08:15:09 gram Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36
37 #include <glib.h>
38 #include "packet.h"
39 #include "globals.h"
40
41 static int proto_h1 = -1;
42 static int hf_h1_header = -1;
43 static int hf_h1_len = -1;
44 static int hf_h1_opfield = -1;
45 static int hf_h1_oplen = -1;
46 static int hf_h1_opcode = -1;
47 static int hf_h1_requestblock = -1;
48 static int hf_h1_requestlen = -1;
49 static int hf_h1_dbnr = -1;
50 static int hf_h1_dwnr = -1;
51 static int hf_h1_dlen = -1;
52 static int hf_h1_org = -1;
53 static int hf_h1_response = -1;
54 static int hf_h1_response_len = -1;
55 static int hf_h1_response_value = -1;
56 static int hf_h1_empty_len = -1;
57 static int hf_h1_empty = -1;
58
59 #define EMPTY_BLOCK     0xFF
60 #define OPCODE_BLOCK    0x01
61 #define REQUEST_BLOCK   0x03
62 #define RESPONSE_BLOCK  0x0F
63
64 static const value_string opcode_vals[] = {
65   {3, "Write Request"},
66   {4, "Write Response"},
67   {5, "Read Request"},
68   {6, "Read Response"},
69   {0, NULL}
70 };
71
72 static const value_string org_vals[] = {
73   {0x01, "DB"},
74   {0x02, "MB"},
75   {0x03, "EB"},
76   {0x04, "AB"},
77   {0x05, "PB"},
78   {0x06, "ZB"},
79   {0x07, "TB"},
80   {0x08, "BS"},
81   {0x09, "AS"},
82   {0x0a, "DX"},
83   {0x10, "DE"},
84   {0x11, "QB"},
85   {0, NULL}
86 };
87
88 static const value_string returncode_vals[] = {
89   {0x00, "No error"},
90   {0x02, "Requested block does not exist"},
91   {0x03, "Requested block too small"},
92   {0xFF, "Error, reason unknown"},
93   {0, NULL}
94 };
95
96 static gint ett_h1 = -1;
97 static gint ett_opcode = -1;
98 static gint ett_org = -1;
99 static gint ett_response = -1;
100 static gint ett_empty = -1;
101
102
103 static gboolean
104 dissect_h1 (const u_char * pd, int offset, frame_data * fd, proto_tree * tree)
105 {
106   proto_tree *h1_tree = NULL;
107   proto_item *ti;
108   proto_tree *opcode_tree = NULL;
109   proto_tree *org_tree = NULL;
110   proto_tree *response_tree = NULL;
111   proto_tree *empty_tree = NULL;
112
113   unsigned int position = 3;
114
115   if (!(pd[offset] == 'S' && pd[offset + 1] == '5')) {
116     return FALSE;
117     }
118
119       if (check_col (fd, COL_PROTOCOL))
120         col_add_str (fd, COL_PROTOCOL, "H1");
121       if (check_col (fd, COL_INFO))
122         col_add_str (fd, COL_INFO, "S5: ");
123       if (tree)
124         {
125           ti = proto_tree_add_item (tree, proto_h1, NullTVB, offset, 16, NULL);
126           h1_tree = proto_item_add_subtree (ti, ett_h1);
127           proto_tree_add_item (h1_tree, hf_h1_header, NullTVB, offset, 2,
128                                pd[offset] * 0x100 + pd[offset + 1]);
129           proto_tree_add_item (h1_tree, hf_h1_len, NullTVB, offset + 2, 1,
130                                pd[offset + 2]);
131         }
132
133       while (position < pd[offset + 2])
134         {
135           switch (pd[offset + position])
136             {
137             case OPCODE_BLOCK:
138               if (h1_tree)
139                 {
140                   ti = proto_tree_add_item (h1_tree, hf_h1_opfield, NullTVB,
141                                             offset + position,
142                                             pd[offset + position + 1],
143                                             pd[offset + position]);
144                   opcode_tree = proto_item_add_subtree (ti, ett_opcode);
145                   proto_tree_add_item (opcode_tree, hf_h1_oplen, NullTVB,
146                                        offset + position + 1, 1,
147                                        pd[offset + position + 1]);
148                   proto_tree_add_item (opcode_tree, hf_h1_opcode, NullTVB,
149                                        offset + position + 2, 1,
150                                        pd[offset + position + 2]);
151                 }
152               if (check_col (fd, COL_INFO))
153                 {
154                   col_append_str (fd, COL_INFO,
155                                   val_to_str (pd[offset + position + 2],
156                                                 opcode_vals,"Unknown Opcode (0x%2.2x)"));
157                 }
158               break;
159             case REQUEST_BLOCK:
160               if (h1_tree)
161                 {
162                   ti = proto_tree_add_item (h1_tree, hf_h1_requestblock, NullTVB,
163                                             offset + position,
164                                             pd[offset + position + 1],
165                                             pd[offset + position]);
166                   org_tree = proto_item_add_subtree (ti, ett_org);
167                   proto_tree_add_item (org_tree, hf_h1_requestlen, NullTVB,
168                                        offset + position + 1, 1,
169                                        pd[offset + position + 1]);
170                   proto_tree_add_item (org_tree, hf_h1_org, NullTVB,
171                                        offset + position + 2, 1,
172                                        pd[offset + position + 2]);
173                   proto_tree_add_item (org_tree, hf_h1_dbnr, NullTVB,
174                                        offset + position + 3, 1,
175                                        pd[offset + position + 3]);
176                   proto_tree_add_item (org_tree, hf_h1_dwnr, NullTVB,
177                                        offset + position + 4, 2,
178                                        pd[offset + position + 4] * 0x100 +
179                                        pd[offset + position + 5]);
180                   proto_tree_add_item (org_tree, hf_h1_dlen, NullTVB,
181                                        offset + position + 6, 2,
182                                        pd[offset + position + 6] * 0x100 +
183                                        pd[offset + position + 7]);
184                 }
185               if (check_col (fd, COL_INFO))
186                 {
187                   col_append_fstr (fd, COL_INFO, " %s %d",
188                                    val_to_str (pd[offset + position + 2],
189                                                  org_vals,"Unknown Type (0x%2.2x)"),
190                                    pd[offset + position + 3]);
191                   col_append_fstr (fd, COL_INFO, " DW %d",
192                                    pd[offset + position + 4] * 0x100 +
193                                    pd[offset + position + 5]);
194                   col_append_fstr (fd, COL_INFO, " Count %d",
195                                    pd[offset + position + 6] * 0x100 +
196                                    pd[offset + position + 7]);
197                 }
198               break;
199             case RESPONSE_BLOCK:
200               if (h1_tree)
201                 {
202                   ti = proto_tree_add_item (h1_tree, hf_h1_response, NullTVB,
203                                             offset + position,
204                                             pd[offset + position + 1],
205                                             pd[offset + position]);
206                   response_tree = proto_item_add_subtree (ti, ett_response);
207                   proto_tree_add_item (response_tree, hf_h1_response_len, NullTVB,
208                                        offset + position + 1, 1,
209                                        pd[offset + position + 1]);
210                   proto_tree_add_item (response_tree, hf_h1_response_value, NullTVB,
211                                        offset + position + 2, 1,
212                                        pd[offset + position + 2]);
213                 }
214               if (check_col (fd, COL_INFO))
215                 {
216                   col_append_fstr (fd, COL_INFO, " %s",
217                                    val_to_str (pd[offset + position + 2],
218                                                  returncode_vals,"Unknown Returcode (0x%2.2x"));
219                 }
220               break;
221             case EMPTY_BLOCK:
222               if (h1_tree)
223                 {
224                   ti = proto_tree_add_item (h1_tree, hf_h1_empty, NullTVB,
225                                             offset + position,
226                                             pd[offset + position + 1],
227                                             pd[offset + position]);
228                   empty_tree = proto_item_add_subtree (ti, ett_empty);
229
230                   proto_tree_add_item (empty_tree, hf_h1_empty_len, NullTVB,
231                                        offset + position + 1, 1,
232                                        pd[offset + position + 1]);
233                 }
234               break;
235             default:
236               /* This is not a valid telegram. So cancel dissection 
237                  and try the next dissector */
238                 return FALSE; 
239                 break;
240             }
241           position += pd[offset + position + 1];        /* Goto next section */
242         }                       /* ..while */
243
244       dissect_data (pd, offset + pd[offset + 2], fd, tree);
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       ""}},
256     {&hf_h1_len,
257      {"Length indicator", "h1.len", FT_UINT16, BASE_DEC, NULL, 0x0,
258       ""}},
259     {&hf_h1_opfield,
260      {"Operation identifier", "h1.opfield", FT_UINT8, BASE_HEX, NULL, 0x0,
261       ""}},
262     {&hf_h1_oplen,
263      {"Operation length", "h1.oplen", FT_UINT8, BASE_HEX, NULL, 0x0, ""}},
264     {&hf_h1_opcode,
265      {"Opcode", "h1.opcode", FT_UINT8, BASE_HEX, VALS (opcode_vals), 0x0,
266       ""}},
267     {&hf_h1_requestblock,
268      {"Request identifier", "h1.request", FT_UINT8, BASE_HEX, NULL, 0x0,
269       ""}},
270     {&hf_h1_requestlen,
271      {"Request length", "h1.reqlen", FT_UINT8, BASE_HEX, NULL, 0x0,
272       ""}},
273     {&hf_h1_org,
274      {"Memory type", "h1.org", FT_UINT8, BASE_HEX, VALS (org_vals), 0x0,
275       ""}},
276     {&hf_h1_dbnr,
277      {"Memory block number", "h1.dbnr", FT_UINT8, BASE_DEC, NULL, 0x0, ""}},
278     {&hf_h1_dwnr,
279      {"Address within memory block", "h1.dwnr", FT_UINT16, BASE_DEC, NULL, 0x0,
280       ""}},
281     {&hf_h1_dlen,
282      {"Length in words", "h1.dlen", FT_INT16, BASE_DEC, NULL, 0x0, ""}},
283     {&hf_h1_response,
284      {"Response identifier", "h1.response", FT_UINT8, BASE_DEC, NULL, 0x0, ""}},
285     {&hf_h1_response_len,
286      {"Response length", "h1.reslen", FT_UINT8, BASE_DEC, NULL, 0x0,
287       ""}},
288     {&hf_h1_response_value,
289      {"Response value", "h1.resvalue", FT_UINT8, BASE_DEC,
290       VALS (returncode_vals), 0x0, ""}},
291     {&hf_h1_empty,
292      {"Empty field", "h1.empty", FT_UINT8, BASE_HEX, NULL, 0x0,
293       ""}},
294     {&hf_h1_empty_len,
295      {"Empty field length", "h1.empty_len", FT_UINT8, BASE_DEC, NULL, 0x0,
296       ""}}
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");
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_is", dissect_h1);
316 }