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