Don't let the user specify a maximum capture file size if they're not
[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.22 2001/12/10 00:25:28 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 static dissector_handle_t data_handle;
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 static gboolean dissect_h1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
103 {
104   tvbuff_t *next_tvb; 
105   
106   proto_tree *h1_tree = NULL;
107   
108   proto_item *ti;
109   proto_tree *opcode_tree = NULL;
110   proto_tree *org_tree = NULL;
111   proto_tree *response_tree = NULL;
112   proto_tree *empty_tree = NULL;
113
114   unsigned int position = 3;
115   unsigned int offset=0;
116
117   if (tvb_length_remaining(tvb, 0) < 2)
118     {
119       /* Not enough data captured to hold the "S5" header; don't try
120          to interpret it as H1. */
121       return FALSE;
122     }
123
124   if (!(tvb_get_guint8(tvb,offset) == 'S' && tvb_get_guint8(tvb,offset+1) == '5'))
125     {
126       return FALSE;
127     }
128
129   if (check_col (pinfo->cinfo, COL_PROTOCOL))
130     col_set_str (pinfo->cinfo, COL_PROTOCOL, "H1");
131   if (check_col (pinfo->cinfo, COL_INFO))
132     col_add_str (pinfo->cinfo, COL_INFO, "S5: ");
133   if (tree)
134     {
135       ti = proto_tree_add_item (tree, proto_h1, tvb, offset, 16, FALSE);
136       h1_tree = proto_item_add_subtree (ti, ett_h1);
137       proto_tree_add_uint (h1_tree, hf_h1_header, tvb, offset, 2,
138                            tvb_get_ntohs(tvb,offset));
139       proto_tree_add_uint (h1_tree, hf_h1_len, tvb, offset + 2, 1,
140                            tvb_get_guint8(tvb,offset+2));
141     }
142
143   while (position < tvb_get_guint8(tvb,offset+2))
144     {
145       switch (tvb_get_guint8(tvb,offset + position))
146         {
147           case OPCODE_BLOCK:
148             if (h1_tree)
149               {
150                 ti = proto_tree_add_uint (h1_tree, hf_h1_opfield, tvb,
151                                           offset + position,
152                                           tvb_get_guint8(tvb,offset+position+1),
153                                           tvb_get_guint8(tvb,offset+position));
154                 opcode_tree = proto_item_add_subtree (ti, ett_opcode);
155                 proto_tree_add_uint (opcode_tree, hf_h1_oplen, tvb,
156                                      offset + position + 1, 1,
157                                      tvb_get_guint8(tvb,offset + position + 1));
158                 proto_tree_add_uint (opcode_tree, hf_h1_opcode, tvb,
159                                      offset + position + 2, 1,
160                                      tvb_get_guint8(tvb,offset + position + 2));
161               }
162             if (check_col (pinfo->cinfo, COL_INFO))
163               {
164                 col_append_str (pinfo->cinfo, COL_INFO,
165                                 val_to_str (tvb_get_guint8(tvb,offset + position + 2),
166                                             opcode_vals,"Unknown Opcode (0x%2.2x)"));
167               }
168             break;
169           case REQUEST_BLOCK:
170             if (h1_tree)
171               {
172                 ti = proto_tree_add_uint (h1_tree, hf_h1_requestblock, tvb,
173                                           offset + position,
174                                           tvb_get_guint8(tvb,offset + position + 1),
175                                           tvb_get_guint8(tvb,offset + position));
176                 org_tree = proto_item_add_subtree (ti, ett_org);
177                 proto_tree_add_uint (org_tree, hf_h1_requestlen, tvb,
178                                      offset + position + 1, 1,
179                                      tvb_get_guint8(tvb,offset + position+1));
180                 proto_tree_add_uint (org_tree, hf_h1_org, tvb,
181                                      offset + position + 2, 1,
182                                      tvb_get_guint8(tvb,offset + position+2));
183                 proto_tree_add_uint (org_tree, hf_h1_dbnr, tvb,
184                                      offset + position + 3, 1,
185                                      tvb_get_guint8(tvb,offset + position+3));
186                 proto_tree_add_uint (org_tree, hf_h1_dwnr, tvb,
187                                      offset + position + 4, 2,
188                                      tvb_get_ntohs(tvb,offset+position+4));
189                 proto_tree_add_int (org_tree, hf_h1_dlen, tvb,
190                                     offset + position + 6, 2,
191                                     tvb_get_ntohs(tvb,offset+position+6));
192               }
193             if (check_col (pinfo->cinfo, COL_INFO))
194               {
195                 col_append_fstr (pinfo->cinfo, COL_INFO, " %s %d",
196                                  val_to_str (tvb_get_guint8(tvb,offset + position + 2),
197                                              org_vals,"Unknown Type (0x%2.2x)"),
198                                  tvb_get_guint8(tvb,offset + position + 3));
199                 col_append_fstr (pinfo->cinfo, COL_INFO, " DW %d",
200                                  tvb_get_ntohs(tvb,offset+position+4));
201                 col_append_fstr (pinfo->cinfo, COL_INFO, " Count %d",
202                                  tvb_get_ntohs(tvb,offset+position+6));
203               }
204             break;
205           case RESPONSE_BLOCK:
206             if (h1_tree)
207               {
208                 ti = proto_tree_add_uint (h1_tree, hf_h1_response, tvb,
209                                           offset + position,
210                                           tvb_get_guint8(tvb,offset + position + 1),
211                                           tvb_get_guint8(tvb,offset + position));
212                 response_tree = proto_item_add_subtree (ti, ett_response);
213                 proto_tree_add_uint (response_tree, hf_h1_response_len, tvb,
214                                      offset + position + 1, 1,
215                                      tvb_get_guint8(tvb,offset + position+1));
216                 proto_tree_add_uint (response_tree, hf_h1_response_value, tvb,
217                                      offset + position + 2, 1,
218                                      tvb_get_guint8(tvb,offset + position+2));
219               }
220             if (check_col (pinfo->cinfo, COL_INFO))
221               {
222                 col_append_fstr (pinfo->cinfo, COL_INFO, " %s",
223                                  val_to_str (tvb_get_guint8(tvb,offset + position + 2),
224                                              returncode_vals,"Unknown Returcode (0x%2.2x"));
225               }
226             break;
227           case EMPTY_BLOCK:
228             if (h1_tree)
229               {
230                 ti = proto_tree_add_uint (h1_tree, hf_h1_empty, tvb,
231                                           offset + position,
232                                           tvb_get_guint8(tvb,offset + position + 1),
233                                           tvb_get_guint8(tvb,offset + position));
234                 empty_tree = proto_item_add_subtree (ti, ett_empty);
235
236                 proto_tree_add_uint (empty_tree, hf_h1_empty_len, tvb,
237                                      offset + position + 1, 1,
238                                      tvb_get_guint8(tvb,offset + position+1));
239               }
240             break;
241           default:
242             /* This is not a valid telegram. So cancel dissection 
243                and try the next dissector */
244             return FALSE; 
245             break;
246         }
247         position += tvb_get_guint8(tvb,offset + position + 1);  /* Goto next section */
248     }                   /* ..while */
249   next_tvb = tvb_new_subset(tvb, offset+tvb_get_guint8(tvb,offset+2), -1, -1);
250   call_dissector(data_handle,next_tvb, pinfo, tree);
251
252   return TRUE;
253 }
254
255
256 void
257 proto_register_h1 (void)
258 {
259   static hf_register_info hf[] = {
260     {&hf_h1_header,
261      {"H1-Header", "h1.header", FT_UINT16, BASE_HEX, NULL, 0x0,
262       "", HFILL }},
263     {&hf_h1_len,
264      {"Length indicator", "h1.len", FT_UINT16, BASE_DEC, NULL, 0x0,
265       "", HFILL }},
266     {&hf_h1_opfield,
267      {"Operation identifier", "h1.opfield", FT_UINT8, BASE_HEX, NULL, 0x0,
268       "", HFILL }},
269     {&hf_h1_oplen,
270      {"Operation length", "h1.oplen", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
271     {&hf_h1_opcode,
272      {"Opcode", "h1.opcode", FT_UINT8, BASE_HEX, VALS (opcode_vals), 0x0,
273       "", HFILL }},
274     {&hf_h1_requestblock,
275      {"Request identifier", "h1.request", FT_UINT8, BASE_HEX, NULL, 0x0,
276       "", HFILL }},
277     {&hf_h1_requestlen,
278      {"Request length", "h1.reqlen", FT_UINT8, BASE_HEX, NULL, 0x0,
279       "", HFILL }},
280     {&hf_h1_org,
281      {"Memory type", "h1.org", FT_UINT8, BASE_HEX, VALS (org_vals), 0x0,
282       "", HFILL }},
283     {&hf_h1_dbnr,
284      {"Memory block number", "h1.dbnr", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
285     {&hf_h1_dwnr,
286      {"Address within memory block", "h1.dwnr", FT_UINT16, BASE_DEC, NULL, 0x0,
287       "", HFILL }},
288     {&hf_h1_dlen,
289      {"Length in words", "h1.dlen", FT_INT16, BASE_DEC, NULL, 0x0, "", HFILL }},
290     {&hf_h1_response,
291      {"Response identifier", "h1.response", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }},
292     {&hf_h1_response_len,
293      {"Response length", "h1.reslen", FT_UINT8, BASE_DEC, NULL, 0x0,
294       "", HFILL }},
295     {&hf_h1_response_value,
296      {"Response value", "h1.resvalue", FT_UINT8, BASE_DEC,
297       VALS (returncode_vals), 0x0, "", HFILL }},
298     {&hf_h1_empty,
299      {"Empty field", "h1.empty", FT_UINT8, BASE_HEX, NULL, 0x0,
300       "", HFILL }},
301     {&hf_h1_empty_len,
302      {"Empty field length", "h1.empty_len", FT_UINT8, BASE_DEC, NULL, 0x0,
303       "", HFILL }}
304   };
305
306   static gint *ett[] = {
307     &ett_h1,
308     &ett_opcode,
309     &ett_response,
310     &ett_org,
311     &ett_empty
312   };
313
314   proto_h1 = proto_register_protocol ("Sinec H1 Protocol", "H1", "h1");
315   proto_register_field_array (proto_h1, hf, array_length (hf));
316   proto_register_subtree_array (ett, array_length (ett));
317 }
318
319 void
320 proto_reg_handoff_h1(void)
321 {
322   heur_dissector_add("cotp_is", dissect_h1, proto_h1);
323   data_handle = find_dissector("data");
324 }