In HTTP-over-TCP, handle more than one HTTP message in a TCP segment.
[obnox/wireshark/wip.git] / packet-hyperscsi.c
1 /* packet-ip.c
2  * Routines for dissassembly of the Hyper SCSI protocol.
3  *
4  * $Id: packet-hyperscsi.c,v 1.4 2002/12/03 01:09:00 jmayer Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * Copyright 2002 Richard Sharpe <rsharpe@richardsharpe.com> 
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 <stdio.h>
31 #include <string.h>
32 #include <glib.h>
33
34 #ifdef NEED_SNPRINTF_H
35 # include "snprintf.h"
36 #endif
37
38 #include <epan/packet.h>
39
40 static int proto_hyperscsi;
41
42 static int hf_hs_cmd = -1;
43 static int hf_hs_ver = -1;
44 static int hf_hs_res = -1;
45 static int hf_hs_tagno = -1;
46 static int hf_hs_lastfrag = -1;
47 static int hf_hs_fragno = -1;
48
49 static gint ett_hyperscsi = -1; 
50 static gint ett_hs_hdr = -1;
51 static gint ett_hs_pdu = -1;
52
53 static const true_false_string tfs_lastfrag = {
54   "Last Fragment",
55   "Not Last Fragment"
56 };
57
58 #define HSCSI_OPCODE_REQUEST                  0x00
59 #define HSCSI_OPCODE_REPLY                    0x01
60 #define HSCSI_OPCODE_DEV_DISCOVERY            0x10
61 #define HSCSI_OPCODE_ADN_REQUEST              0x11
62 #define HSCSI_OPCODE_ADN_REPLY                0x12
63 #define HSCSI_OPCODE_DISCONNECT               0x13
64 #define HSCSI_OPCODE_ACK_SNR                  0x20
65 #define HSCSI_OPCODE_ACK_REPLY                0x21
66 #define HSCSI_OPCODE_ADDR_REPORT              0x30
67 #define HSCSI_OPCODE_ADDR_REPLY               0x31
68 #define HSCSI_OPCODE_LOCAL_REQUEST            0x32
69 #define HSCSI_OPCODE_LOCAL_REPLY              0x33
70 #define HSCSI_OPCODE_REMOTE_REQUEST           0x34
71 #define HSCSI_OPCODE_REMOTE_REPLY             0x35
72
73 static const value_string hscsi_opcodes[] = {
74   { HSCSI_OPCODE_REQUEST,         "Command Block Encap Request"}, 
75   { HSCSI_OPCODE_REPLY,           "Command Block Encap Reply"},
76   { HSCSI_OPCODE_DEV_DISCOVERY,   "Device Discovery Reply"},
77   { HSCSI_OPCODE_ADN_REQUEST,     "Auth/Device Neg Request"},
78   { HSCSI_OPCODE_ADN_REPLY,       "Auth/Device Neg Reply"},
79   { HSCSI_OPCODE_DISCONNECT,      "Disconnect Request"},
80   { HSCSI_OPCODE_ACK_SNR,         "Flow Control Setup/Ack Request"},
81   { HSCSI_OPCODE_ACK_REPLY,       "Flow Control Ack Reply"},
82   { 0, NULL}
83 };
84
85 #define OPCODE_MASK 0x7F
86
87 static void
88 dissect_hyperscsi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
89 {
90   guint      hs_hdr1, hs_hdr2, hs_hdr3;
91   guint8     hs_res;
92   guint16    hs_tagno;
93   guint16    hs_fragno;
94   gint       offset = 0;
95   proto_tree *hs_hdr_tree, *hs_pdu_tree;
96   proto_tree *hs_tree = NULL;
97   proto_item *ti;
98   guint8     hs_cmd, hs_ver;
99   char       *opcode_str;
100
101   if (check_col(pinfo->cinfo, COL_PROTOCOL))
102     col_set_str(pinfo->cinfo, COL_PROTOCOL, "HYPERSCSI");
103   if (check_col(pinfo->cinfo, COL_INFO))
104     col_clear(pinfo->cinfo, COL_INFO);
105
106   if (tree) {
107     ti = proto_tree_add_item(tree, proto_hyperscsi, tvb, offset, -1, FALSE);
108     hs_tree = proto_item_add_subtree(ti, ett_hyperscsi);
109   } 
110
111   hs_hdr1 = tvb_get_guint8(tvb, offset);
112   offset++;
113   hs_hdr2 = tvb_get_guint8(tvb, offset);
114   offset++;
115   hs_hdr3 = tvb_get_guint8(tvb, offset);
116   offset++;
117
118   hs_res = hs_hdr1 >> 4;
119   hs_tagno = ((hs_hdr1 & 0x0F) << 5 ) | (hs_hdr2 >> 3);
120   hs_fragno = ((hs_hdr2 &0X03) << 8 ) | hs_hdr3;
121
122   /*
123    * Add the header ... three bytes
124    */
125
126   if (tree) {
127     ti = proto_tree_add_text(hs_tree, tvb, 0, 3, "HyperSCSI Header");
128     hs_hdr_tree = proto_item_add_subtree(ti, ett_hs_hdr);
129
130     /*
131      * Now, add the header items
132      */
133
134     proto_tree_add_uint(hs_hdr_tree, hf_hs_res, tvb, 0, 1, hs_res);
135     proto_tree_add_uint(hs_hdr_tree, hf_hs_tagno, tvb, 0, 2, hs_tagno);
136     proto_tree_add_item(hs_hdr_tree, hf_hs_lastfrag, tvb, 1, 1, FALSE);
137     proto_tree_add_uint(hs_hdr_tree, hf_hs_fragno, tvb, 1, 2, hs_fragno);
138
139   }
140
141   /* 
142    * Now, add the PDU 
143    */
144
145   hs_ver = tvb_get_guint8(tvb, offset++);
146
147   hs_cmd = tvb_get_guint8(tvb, offset++);
148
149   hs_cmd &= OPCODE_MASK;
150
151   opcode_str = match_strval(hs_cmd, hscsi_opcodes);
152
153   if (!opcode_str) 
154     opcode_str = "Unknown HyperSCSI Request or Response";
155
156   if (check_col(pinfo->cinfo, COL_INFO)) {
157     col_append_str(pinfo->cinfo, COL_INFO, (char *)opcode_str);
158   }
159
160   if (tree) {
161     ti = proto_tree_add_text(hs_tree, tvb, 3, -1, "HyperSCSI PDU");
162     hs_pdu_tree = proto_item_add_subtree(ti, ett_hs_pdu);
163
164     proto_tree_add_uint(hs_pdu_tree, hf_hs_ver, tvb, 3, 1, hs_ver);
165
166     proto_tree_add_uint_format(hs_pdu_tree, hf_hs_cmd, tvb, 4, 1, hs_cmd, "HyperSCSI Command: %s", opcode_str);
167   }
168
169 }
170
171 void
172 proto_register_hyperscsi(void)
173 {
174
175   static hf_register_info hf[] = {
176     { &hf_hs_res, 
177       { "Reserved", "hyperscsi.reserved", FT_UINT8, BASE_DEC, NULL, 0x0, 
178         "", HFILL}},
179
180     { &hf_hs_tagno,
181       { "Tag No", "hyperscsi.tagno", FT_UINT16, BASE_DEC, NULL, 0x0,
182         "", HFILL }},
183
184     { &hf_hs_lastfrag,
185       { "Last Fragment", "hyperscsi.lastfrag", FT_BOOLEAN, 8, TFS(&tfs_lastfrag), 0x04, "", HFILL}},
186
187     { &hf_hs_fragno, 
188       { "Fragment No", "hyperscsi.fragno", FT_UINT16, BASE_DEC, NULL, 0x0,
189         "", HFILL}},
190
191     { &hf_hs_ver,
192       { "HyperSCSI Version", "hyperscsi.version", FT_UINT8, BASE_DEC, NULL, 
193         0x0, "", HFILL}},
194
195     { &hf_hs_cmd,
196       { "HyperSCSI Command", "hyperscsi.cmd", FT_UINT8, BASE_DEC, NULL, 0x0, 
197         "", HFILL}},
198   };
199
200   static gint *ett[] = {
201     &ett_hyperscsi,
202     &ett_hs_hdr,
203     &ett_hs_pdu,
204   };
205   
206   proto_hyperscsi = proto_register_protocol("HyperSCSI", "HyperSCSI", "hyperscsi");
207   proto_register_field_array(proto_hyperscsi, hf, array_length(hf));
208   proto_register_subtree_array(ett, array_length(ett));
209
210   register_dissector("hyperscsi", dissect_hyperscsi, proto_hyperscsi);
211 }
212
213 #define ETHERTYPE_HYPERSCSI 0x889A
214
215 void
216 proto_reg_handoff_hyperscsi(void)
217 {
218   dissector_handle_t hs_handle;
219
220   hs_handle = find_dissector("hyperscsi");
221   dissector_add("ethertype", ETHERTYPE_HYPERSCSI, hs_handle);
222
223 }